Implement `org-export-custom-protocol-maybe' and use it
[org-mode.git] / testing / lisp / test-ox.el
blob290b02980bc6d6de0b2bf6569cc310acd3e6014b
1 ;;; test-ox.el --- Tests for ox.el
3 ;; Copyright (C) 2012, 2013, 2014 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
7 ;; This file is not part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Code:
24 (unless (featurep 'ox)
25 (signal 'missing-test-dependency "org-export"))
27 (defun org-test-default-backend ()
28 "Return a default export back-end.
29 This back-end simply returns parsed data as Org syntax."
30 (org-export-create-backend
31 :transcoders (let (transcode-table)
32 (dolist (type (append org-element-all-elements
33 org-element-all-objects)
34 transcode-table)
35 (push
36 (cons type
37 (lambda (obj contents info)
38 (funcall
39 (intern (format "org-element-%s-interpreter"
40 type))
41 obj contents)))
42 transcode-table)))))
44 (defmacro org-test-with-parsed-data (data &rest body)
45 "Execute body with parsed data available.
47 DATA is a string containing the data to be parsed. BODY is the
48 body to execute. Parse tree is available under the `tree'
49 variable, and communication channel under `info'.
51 This function calls `org-export-collect-tree-properties'. As
52 such, `:ignore-list' (for `org-element-map') and
53 `:parse-tree' (for `org-export-get-genealogy') properties are
54 already filled in `info'."
55 (declare (debug (form body)) (indent 1))
56 `(org-test-with-temp-text ,data
57 (let* ((tree (org-element-parse-buffer))
58 (info (org-export-collect-tree-properties
59 tree (org-export-get-environment))))
60 ,@body)))
64 ;;; Internal Tests
66 (ert-deftest test-org-export/bind-keyword ()
67 "Test reading #+BIND: keywords."
68 ;; Test with `org-export-allow-bind-keywords' set to t.
69 (should
70 (org-test-with-temp-text "#+BIND: test-ox-var value"
71 (let ((org-export-allow-bind-keywords t))
72 (org-export-get-environment)
73 (eq test-ox-var 'value))))
74 ;; Test with `org-export-allow-bind-keywords' set to nil.
75 (should-not
76 (org-test-with-temp-text "#+BIND: test-ox-var value"
77 (let ((org-export-allow-bind-keywords nil))
78 (org-export-get-environment)
79 (boundp 'test-ox-var))))
80 ;; BIND keywords are case-insensitive.
81 (should
82 (org-test-with-temp-text "#+bind: test-ox-var value"
83 (let ((org-export-allow-bind-keywords t))
84 (org-export-get-environment)
85 (eq test-ox-var 'value))))
86 ;; Preserve order of BIND keywords.
87 (should
88 (org-test-with-temp-text "#+BIND: test-ox-var 1\n#+BIND: test-ox-var 2"
89 (let ((org-export-allow-bind-keywords t))
90 (org-export-get-environment)
91 (eq test-ox-var 2))))
92 ;; Read BIND keywords in setup files.
93 (should
94 (org-test-with-temp-text
95 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
96 (let ((org-export-allow-bind-keywords t))
97 (org-export-get-environment)
98 (eq variable 'value))))
99 ;; Verify that bound variables are seen during export.
100 (should
101 (equal "Yes\n"
102 (org-test-with-temp-text "#+BIND: test-ox-var value"
103 (let ((org-export-allow-bind-keywords t))
104 (org-export-as
105 (org-export-create-backend
106 :transcoders
107 '((section . (lambda (s c i)
108 (if (eq test-ox-var 'value) "Yes" "No")))))))))))
110 (ert-deftest test-org-export/parse-option-keyword ()
111 "Test reading all standard #+OPTIONS: items."
112 (should
113 (equal
114 (org-export--parse-option-keyword
115 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
116 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
117 stat:t")
118 '(:headline-levels
119 1 :preserve-breaks t :section-numbers t :time-stamp-file t
120 :with-archived-trees t :with-author t :with-creator t :with-drawers t
121 :with-email t :with-emphasize t :with-entities t :with-fixed-width t
122 :with-footnotes t :with-inlinetasks nil :with-priority t
123 :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
124 :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
125 :with-todo-keywords t)))
126 ;; Test some special values.
127 (should
128 (equal
129 (org-export--parse-option-keyword
130 "arch:headline creator:comment d:(\"TEST\")
131 ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
132 '( :section-numbers
134 :with-archived-trees headline :with-creator comment
135 :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
136 :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
138 (ert-deftest test-org-export/get-inbuffer-options ()
139 "Test reading all standard export keywords."
140 ;; Properties should follow buffer order.
141 (should
142 (equal
143 (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
144 (org-export--get-inbuffer-options))
145 '(:language "fr" :creator "Me" :email "email")))
146 ;; Parse document keywords.
147 (should
148 (equal
149 (org-test-with-temp-text "#+AUTHOR: Me"
150 (org-export--get-inbuffer-options))
151 '(:author ("Me"))))
152 ;; Test `space' behaviour.
153 (should
154 (equal
155 (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
156 (org-export--get-inbuffer-options))
157 '(:title ("Some title with spaces"))))
158 ;; Test `newline' behaviour.
159 (should
160 (equal
161 (org-test-with-temp-text "#+DESCRIPTION: With\n#+DESCRIPTION: two lines"
162 (org-export--get-inbuffer-options))
163 '(:description "With\ntwo lines")))
164 ;; Test `split' behaviour.
165 (should
166 (equal
167 (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
168 (org-export--get-inbuffer-options))
169 '(:select-tags ("a" "b"))))
170 ;; Options set through SETUPFILE.
171 (should
172 (equal
173 (org-test-with-temp-text
174 (format "#+DESCRIPTION: l1
175 #+LANGUAGE: es
176 #+SELECT_TAGS: a
177 #+TITLE: a
178 #+SETUPFILE: \"%s/examples/setupfile.org\"
179 #+DESCRIPTION: l3
180 #+LANGUAGE: fr
181 #+SELECT_TAGS: c
182 #+TITLE: c"
183 org-test-dir)
184 (org-export--get-inbuffer-options))
185 '(:description "l1\nl2\nl3":language "fr" :select-tags ("a" "b" "c")
186 :title ("a b c"))))
187 ;; More than one property can refer to the same buffer keyword.
188 (should
189 (equal '(:k2 "value" :k1 "value")
190 (let ((backend (org-export-create-backend
191 :options '((:k1 "KEYWORD")
192 (:k2 "KEYWORD")))))
193 (org-test-with-temp-text "#+KEYWORD: value"
194 (org-export--get-inbuffer-options backend))))))
196 (ert-deftest test-org-export/get-subtree-options ()
197 "Test setting options from headline's properties."
198 ;; EXPORT_TITLE.
199 (org-test-with-temp-text "#+TITLE: Title
200 * Headline
201 :PROPERTIES:
202 :EXPORT_TITLE: Subtree Title
203 :END:
204 Paragraph"
205 (forward-line)
206 (should (equal (plist-get (org-export-get-environment nil t) :title)
207 '("Subtree Title"))))
208 :title
209 '("subtree-title")
210 ;; EXPORT_OPTIONS.
211 (org-test-with-temp-text "#+OPTIONS: H:1
212 * Headline
213 :PROPERTIES:
214 :EXPORT_OPTIONS: H:2
215 :END:
216 Paragraph"
217 (forward-line)
218 (should
219 (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
220 ;; EXPORT_DATE.
221 (org-test-with-temp-text "#+DATE: today
222 * Headline
223 :PROPERTIES:
224 :EXPORT_DATE: 29-03-2012
225 :END:
226 Paragraph"
227 (forward-line)
228 (should (equal (plist-get (org-export-get-environment nil t) :date)
229 '("29-03-2012"))))
230 ;; Properties with `split' behaviour are stored as a list of
231 ;; strings.
232 (should
233 (equal '("a" "b")
234 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
235 * Headline
236 :PROPERTIES:
237 :EXPORT_EXCLUDE_TAGS: a b
238 :END:
239 Paragraph"
240 (progn
241 (forward-line)
242 (plist-get (org-export-get-environment nil t) :exclude-tags)))))
243 ;; Handle :PROPERTY+: syntax.
244 (should
245 (equal '("a" "b")
246 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
247 * Headline
248 :PROPERTIES:
249 :EXPORT_EXCLUDE_TAGS: a
250 :EXPORT_EXCLUDE_TAGS+: b
251 :END:
252 Paragraph"
253 (progn
254 (forward-line)
255 (plist-get (org-export-get-environment nil t) :exclude-tags)))))
256 ;; Export properties are case-insensitive.
257 (org-test-with-temp-text "* Headline
258 :PROPERTIES:
259 :EXPORT_Date: 29-03-2012
260 :END:
261 Paragraph"
262 (should (equal (plist-get (org-export-get-environment nil t) :date)
263 '("29-03-2012"))))
264 ;; Still grab correct options when section above is empty.
265 (should
266 (equal '("H1")
267 (org-test-with-temp-text "* H1\n** H11\n** H12"
268 (progn (forward-line 2)
269 (plist-get (org-export-get-environment nil t) :title))))))
271 (ert-deftest test-org-export/set-title ()
272 "Test title setting."
273 ;; Without TITLE keyword.
274 (should
275 (equal
277 (org-test-with-temp-text "Test"
278 (org-export-as
279 (org-export-create-backend
280 :transcoders
281 '((template . (lambda (text info)
282 (org-element-interpret-data
283 (plist-get info :title))))))))))
284 ;; With a blank TITLE keyword.
285 (should
286 (equal
288 (org-test-with-temp-text "#+TITLE:\nTest"
289 (org-export-as
290 (org-export-create-backend
291 :transcoders
292 '((template . (lambda (text info)
293 (org-element-interpret-data
294 (plist-get info :title))))))))))
295 ;; With a non-empty TITLE keyword.
296 (should
297 (equal
298 "Title"
299 (org-test-with-temp-text "#+TITLE: Title\nTest"
300 (org-export-as
301 (org-export-create-backend
302 :transcoders
303 '((template . (lambda (text info)
304 (org-element-interpret-data
305 (plist-get info :title))))))))))
306 ;; When exporting a subtree, its heading becomes the headline of the
307 ;; document...
308 (should
309 (equal
310 "Headline"
311 (org-test-with-temp-text "* Headline\nBody"
312 (org-export-as
313 (org-export-create-backend
314 :transcoders
315 '((template . (lambda (text info)
316 (org-element-interpret-data
317 (plist-get info :title))))))
318 'subtree))))
319 ;; ... unless there is an EXPORT_TITLE property at the root of the
320 ;; subtree.
321 (should
322 (equal
324 (org-test-with-temp-text
325 "* A\n :PROPERTIES:\n :EXPORT_TITLE: B\n :END:\nBody"
326 (org-export-as
327 (org-export-create-backend
328 :transcoders
329 '((template . (lambda (text info)
330 (org-element-interpret-data
331 (plist-get info :title))))))
332 'subtree)))))
334 (ert-deftest test-org-export/handle-options ()
335 "Test if export options have an impact on output."
336 ;; Test exclude tags for headlines and inlinetasks.
337 (should
338 (equal ""
339 (org-test-with-temp-text "* Head1 :noexp:"
340 (org-export-as (org-test-default-backend)
341 nil nil nil '(:exclude-tags ("noexp"))))))
342 ;; Test include tags for headlines and inlinetasks.
343 (should
344 (equal "* H2\n** Sub :exp:\n*** Sub Sub\n"
345 (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
346 (let ((org-tags-column 0))
347 (org-export-as (org-test-default-backend)
348 nil nil nil '(:select-tags ("exp")))))))
349 ;; If there is an include tag, ignore the section before the first
350 ;; headline, if any.
351 (should
352 (equal "* H1 :exp:\nBody\n"
353 (org-test-with-temp-text "First section\n* H1 :exp:\nBody"
354 (let ((org-tags-column 0))
355 (org-export-as (org-test-default-backend)
356 nil nil nil '(:select-tags ("exp")))))))
357 (should-not
358 (equal "* H1 :exp:\n"
359 (org-test-with-temp-text "* H1 :exp:\nBody"
360 (let ((org-tags-column 0))
361 (org-export-as (org-test-default-backend)
362 nil nil nil '(:select-tags ("exp")))))))
363 ;; Test mixing include tags and exclude tags.
364 (should
365 (string-match
366 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
367 (org-test-with-temp-text "
368 * Head1 :export:
369 ** Sub-Head1 :noexport:
370 ** Sub-Head2
371 * Head2 :noexport:
372 ** Sub-Head1 :export:"
373 (org-export-as (org-test-default-backend) nil nil nil
374 '(:select-tags ("export") :exclude-tags ("noexport"))))))
375 ;; Ignore tasks.
376 (should
377 (equal ""
378 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
379 (org-test-with-temp-text "* TODO Head1"
380 (org-export-as (org-test-default-backend)
381 nil nil nil '(:with-tasks nil))))))
382 (should
383 (equal "* TODO Head1\n"
384 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
385 (org-test-with-temp-text "* TODO Head1"
386 (org-export-as (org-test-default-backend)
387 nil nil nil '(:with-tasks t))))))
388 ;; Archived tree.
389 (should
390 (equal ""
391 (org-test-with-temp-text "* Head1 :archive:"
392 (let ((org-archive-tag "archive"))
393 (org-export-as (org-test-default-backend)
394 nil nil nil '(:with-archived-trees nil))))))
395 (should
396 (string-match
397 "\\* Head1[ \t]+:archive:"
398 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
399 (let ((org-archive-tag "archive"))
400 (org-export-as (org-test-default-backend) nil nil nil
401 '(:with-archived-trees headline))))))
402 (should
403 (string-match
404 "\\`\\* Head1[ \t]+:archive:\n\\'"
405 (org-test-with-temp-text "* Head1 :archive:"
406 (let ((org-archive-tag "archive"))
407 (org-export-as (org-test-default-backend)
408 nil nil nil '(:with-archived-trees t))))))
409 ;; Clocks.
410 (should
411 (string-match "CLOCK: \\[2012-04-29 .* 10:45\\]"
412 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
413 (org-export-as (org-test-default-backend)
414 nil nil nil '(:with-clocks t)))))
415 (should
416 (equal ""
417 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
418 (org-export-as (org-test-default-backend)
419 nil nil nil '(:with-clocks nil)))))
420 ;; Drawers.
421 (should
422 (equal ""
423 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
424 (org-export-as (org-test-default-backend)
425 nil nil nil '(:with-drawers nil)))))
426 (should
427 (equal ":TEST:\ncontents\n:END:\n"
428 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
429 (org-export-as (org-test-default-backend)
430 nil nil nil '(:with-drawers t)))))
431 (should
432 (equal ":FOO:\nkeep\n:END:\n"
433 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
434 (org-export-as (org-test-default-backend)
435 nil nil nil '(:with-drawers ("FOO"))))))
436 (should
437 (equal ":FOO:\nkeep\n:END:\n"
438 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
439 (org-export-as (org-test-default-backend)
440 nil nil nil '(:with-drawers (not "BAR"))))))
441 ;; Fixed-width.
442 (should
443 (equal ": A\n"
444 (org-test-with-temp-text ": A"
445 (org-export-as (org-test-default-backend) nil nil nil
446 '(:with-fixed-width t)))))
447 (should
448 (equal ""
449 (org-test-with-temp-text ": A"
450 (org-export-as (org-test-default-backend) nil nil nil
451 '(:with-fixed-width nil)))))
452 ;; Footnotes.
453 (should
454 (equal "Footnote?"
455 (let ((org-footnote-section nil))
456 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
457 (org-trim (org-export-as (org-test-default-backend)
458 nil nil nil '(:with-footnotes nil)))))))
459 (should
460 (equal "Footnote?[fn:1]\n\n[fn:1] Def"
461 (let ((org-footnote-section nil))
462 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
463 (org-trim (org-export-as (org-test-default-backend)
464 nil nil nil '(:with-footnotes t)))))))
465 ;; Inlinetasks.
466 (when (featurep 'org-inlinetask)
467 (should
468 (equal
470 (let ((org-inlinetask-min-level 15))
471 (org-test-with-temp-text "*************** Task"
472 (org-export-as (org-test-default-backend)
473 nil nil nil '(:with-inlinetasks nil))))))
474 (should
475 (equal
477 (let ((org-inlinetask-min-level 15))
478 (org-test-with-temp-text
479 "*************** Task\nContents\n*************** END"
480 (org-export-as (org-test-default-backend)
481 nil nil nil '(:with-inlinetasks nil)))))))
482 ;; Plannings.
483 (should
484 (string-match
485 "* H\nCLOSED: \\[2012-04-29 .* 10:45\\]"
486 (let ((org-closed-string "CLOSED:"))
487 (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
488 (org-export-as (org-test-default-backend)
489 nil nil nil '(:with-planning t))))))
490 (should
491 (equal "* H\n"
492 (let ((org-closed-string "CLOSED:"))
493 (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
494 (org-export-as (org-test-default-backend)
495 nil nil nil '(:with-planning nil))))))
496 ;; Property Drawers.
497 (should
498 (equal "* H1\n"
499 (org-test-with-temp-text
500 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
501 (org-export-as (org-test-default-backend)
502 nil nil nil '(:with-properties nil)))))
503 (should
504 (equal "* H1\n:PROPERTIES:\n:PROP: value\n:END:\n"
505 (org-test-with-temp-text
506 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
507 (org-export-as (org-test-default-backend)
508 nil nil nil '(:with-properties t)))))
509 (should
510 (equal "* H1\n:PROPERTIES:\n:B: 2\n:END:\n"
511 (org-test-with-temp-text
512 "* H1\n :PROPERTIES:\n :A: 1\n :B: 2\n:END:"
513 (org-export-as (org-test-default-backend)
514 nil nil nil '(:with-properties ("B"))))))
515 ;; Statistics cookies.
516 (should
517 (equal ""
518 (org-test-with-temp-text "[0/0]"
519 (org-export-as (org-test-default-backend)
520 nil nil nil '(:with-statistics-cookies nil)))))
521 ;; Tables.
522 (should
523 (equal "| A |\n"
524 (org-test-with-temp-text "| A |"
525 (org-export-as (org-test-default-backend) nil nil nil
526 '(:with-tables t)))))
527 (should
528 (equal ""
529 (org-test-with-temp-text "| A |"
530 (org-export-as (org-test-default-backend) nil nil nil
531 '(:with-tables nil))))))
533 (ert-deftest test-org-export/with-timestamps ()
534 "Test `org-export-with-timestamps' specifications."
535 ;; t value.
536 (should
537 (string-match
538 "\\[2012-04-29 .*? 10:45\\]<2012-04-29 .*? 10:45>"
539 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
540 (org-export-as (org-test-default-backend)
541 nil nil nil '(:with-timestamps t)))))
542 ;; nil value.
543 (should
544 (equal
546 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
547 (org-export-as (org-test-default-backend)
548 nil nil nil '(:with-timestamps nil)))))
549 ;; `active' value.
550 (should
551 (string-match
552 "<2012-03-29 .*?>\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
553 (org-test-with-temp-text
554 "<2012-03-29 Thu>[2012-03-29 Thu]
556 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
557 (org-export-as (org-test-default-backend)
558 nil nil nil '(:with-timestamps active)))))
559 ;; `inactive' value.
560 (should
561 (string-match
562 "\\[2012-03-29 .*?\\]\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
563 (org-test-with-temp-text
564 "<2012-03-29 Thu>[2012-03-29 Thu]
566 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
567 (org-export-as (org-test-default-backend)
568 nil nil nil '(:with-timestamps inactive))))))
570 (ert-deftest test-org-export/comment-tree ()
571 "Test if export process ignores commented trees."
572 (should
573 (equal ""
574 (org-test-with-temp-text "* COMMENT Head1"
575 (org-export-as (org-test-default-backend))))))
577 (ert-deftest test-org-export/uninterpreted ()
578 "Test handling of uninterpreted elements."
579 ;; Entities.
580 (should
581 (equal "dummy\n"
582 (org-test-with-temp-text "\\alpha"
583 (org-export-as
584 (org-export-create-backend
585 :transcoders '((entity . (lambda (e c i) "dummy"))
586 (paragraph . (lambda (p c i) c))
587 (section . (lambda (s c i) c))))
588 nil nil nil '(:with-entities t)))))
589 (should
590 (equal "\\alpha\n"
591 (org-test-with-temp-text "\\alpha"
592 (org-export-as
593 (org-export-create-backend
594 :transcoders '((entity . (lambda (e c i) "dummy"))
595 (paragraph . (lambda (p c i) c))
596 (section . (lambda (s c i) c))))
597 nil nil nil '(:with-entities nil)))))
598 ;; Emphasis.
599 (should
600 (equal "dummy\n"
601 (org-test-with-temp-text "*bold*"
602 (org-export-as
603 (org-export-create-backend
604 :transcoders '((bold . (lambda (b c i) "dummy"))
605 (paragraph . (lambda (p c i) c))
606 (section . (lambda (s c i) c))))
607 nil nil nil '(:with-emphasize t)))))
608 (should
609 (equal "*bold*\n"
610 (org-test-with-temp-text "*bold*"
611 (org-export-as
612 (org-export-create-backend
613 :transcoders '((bold . (lambda (b c i) "dummy"))
614 (paragraph . (lambda (p c i) c))
615 (section . (lambda (s c i) c))))
616 nil nil nil '(:with-emphasize nil)))))
617 ;; LaTeX environment.
618 (should
619 (equal "dummy\n"
620 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
621 (org-export-as
622 (org-export-create-backend
623 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
624 (section . (lambda (s c i) c))))
625 nil nil nil '(:with-latex t)))))
626 (should
627 (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
628 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
629 (org-export-as
630 (org-export-create-backend
631 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
632 (section . (lambda (s c i) c))))
633 nil nil nil '(:with-latex verbatim)))))
634 ;; LaTeX fragment.
635 (should
636 (equal "dummy\n"
637 (org-test-with-temp-text "$1$"
638 (org-export-as
639 (org-export-create-backend
640 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
641 (paragraph . (lambda (p c i) c))
642 (section . (lambda (s c i) c))))
643 nil nil nil '(:with-latex t)))))
644 (should
645 (equal "$1$\n"
646 (org-test-with-temp-text "$1$"
647 (org-export-as
648 (org-export-create-backend
649 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
650 (paragraph . (lambda (p c i) c))
651 (section . (lambda (s c i) c))))
652 nil nil nil '(:with-latex verbatim)))))
653 ;; Sub/superscript.
654 (should
655 (equal "adummy\n"
656 (org-test-with-temp-text "a_b"
657 (org-export-as
658 (org-export-create-backend
659 :transcoders '((subscript . (lambda (s c i) "dummy"))
660 (paragraph . (lambda (p c i) c))
661 (section . (lambda (s c i) c))))
662 nil nil nil '(:with-sub-superscript t)))))
663 (should
664 (equal "a_b\n"
665 (org-test-with-temp-text "a_b"
666 (org-export-as
667 (org-export-create-backend
668 :transcoders '((subscript . (lambda (s c i) "dummy"))
669 (paragraph . (lambda (p c i) c))
670 (section . (lambda (s c i) c))))
671 nil nil nil '(:with-sub-superscript nil)))))
672 (should
673 (equal "a_b\n"
674 (org-test-with-temp-text "a_b"
675 (org-export-as
676 (org-export-create-backend
677 :transcoders '((subscript . (lambda (s c i) "dummy"))
678 (paragraph . (lambda (p c i) c))
679 (section . (lambda (s c i) c))))
680 nil nil nil '(:with-sub-superscript {})))))
681 (should
682 (equal "adummy\n"
683 (org-test-with-temp-text "a_{b}"
684 (org-export-as
685 (org-export-create-backend
686 :transcoders '((subscript . (lambda (s c i) "dummy"))
687 (paragraph . (lambda (p c i) c))
688 (section . (lambda (s c i) c))))
689 nil nil nil '(:with-sub-superscript {})))))
690 ;; Also handle uninterpreted objects in title.
691 (should
692 (equal "a_b"
693 (org-test-with-temp-text "#+TITLE: a_b"
694 (org-export-as
695 (org-export-create-backend
696 :transcoders
697 '((subscript . (lambda (s c i) "dummy"))
698 (template . (lambda (c i) (org-export-data
699 (plist-get i :title) i)))
700 (section . (lambda (s c i) c))))
701 nil nil nil '(:with-sub-superscript nil)))))
702 ;; Special case: multiples uninterpreted objects in a row.
703 (should
704 (equal "a_b_c_d\n"
705 (org-test-with-temp-text "a_b_c_d"
706 (org-export-as
707 (org-export-create-backend
708 :transcoders '((subscript . (lambda (s c i) "dummy"))
709 (paragraph . (lambda (p c i) c))
710 (section . (lambda (s c i) c))))
711 nil nil nil '(:with-sub-superscript {}))))))
713 (ert-deftest test-org-export/export-scope ()
714 "Test all export scopes."
715 (org-test-with-temp-text "
716 * Head1
717 ** Head2
718 text
719 *** Head3"
720 ;; Subtree.
721 (forward-line 3)
722 (should (equal (org-export-as (org-test-default-backend) 'subtree)
723 "text\n*** Head3\n"))
724 ;; Visible.
725 (goto-char (point-min))
726 (forward-line)
727 (org-cycle)
728 (should (equal (org-export-as (org-test-default-backend) nil 'visible)
729 "* Head1\n"))
730 ;; Region.
731 (goto-char (point-min))
732 (forward-line 3)
733 (transient-mark-mode 1)
734 (push-mark (point) t t)
735 (goto-char (point-at-eol))
736 (should (equal (org-export-as (org-test-default-backend)) "text\n")))
737 ;; Subtree with a code block calling another block outside.
738 (should
739 (equal ": 3\n"
740 (org-test-with-temp-text "
741 * Head1
742 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
743 <<test>>
744 #+END_SRC
745 * Head2
746 #+NAME: test
747 #+BEGIN_SRC emacs-lisp
748 \(+ 1 2)
749 #+END_SRC"
750 (forward-line 1)
751 (org-export-as (org-test-default-backend) 'subtree))))
752 ;; Body only.
753 (let ((backend (org-test-default-backend)))
754 (setf (org-export-backend-transcoders backend)
755 (cons '(template . (lambda (body i)
756 (format "BEGIN\n%sEND" body)))
757 (org-export-backend-transcoders backend)))
758 (org-test-with-temp-text "Text"
759 (should (equal (org-export-as backend nil nil 'body-only)
760 "Text\n"))
761 (should (equal (org-export-as backend) "BEGIN\nText\nEND")))))
763 (ert-deftest test-org-export/output-file-name ()
764 "Test `org-export-output-file-name' specifications."
765 ;; Export from a file: name is built from original file name.
766 (should
767 (org-test-with-temp-text-in-file "Test"
768 (equal (concat (file-name-as-directory ".")
769 (file-name-nondirectory
770 (file-name-sans-extension (buffer-file-name))))
771 (file-name-sans-extension (org-export-output-file-name ".ext")))))
772 ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
773 (should
774 (org-test-with-temp-text-in-file
775 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
776 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
777 ;; From a buffer not associated to a file, too.
778 (should
779 (org-test-with-temp-text
780 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
781 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
782 ;; When provided name is absolute, preserve it.
783 (should
784 (org-test-with-temp-text
785 (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
786 (expand-file-name "test"))
787 (file-name-absolute-p (org-export-output-file-name ".ext" t))))
788 ;; When PUB-DIR argument is provided, use it.
789 (should
790 (org-test-with-temp-text-in-file "Test"
791 (equal (file-name-directory
792 (org-export-output-file-name ".ext" nil "dir/"))
793 "dir/")))
794 ;; When returned name would overwrite original file, add EXTENSION
795 ;; another time.
796 (should
797 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
798 (equal (org-export-output-file-name ".org") "./normal.org.org"))))
800 (ert-deftest test-org-export/expand-include ()
801 "Test file inclusion in an Org buffer."
802 ;; Error when file isn't specified.
803 (should-error
804 (org-test-with-temp-text "#+INCLUDE: dummy.org"
805 (org-export-expand-include-keyword)))
806 ;; Full insertion with recursive inclusion.
807 (should
808 (equal
809 (with-temp-buffer
810 (insert-file
811 (expand-file-name "examples/include.org" org-test-dir))
812 (replace-regexp-in-string
813 (regexp-quote "#+INCLUDE: \"include2.org\"")
814 "Success!" (buffer-string)))
815 (org-test-with-temp-text
816 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
817 (org-export-expand-include-keyword)
818 (buffer-string))))
819 ;; Localized insertion.
820 (org-test-with-temp-text
821 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
822 org-test-dir)
823 (org-export-expand-include-keyword)
824 (should (equal (buffer-string)
825 "Small Org file with an include keyword.\n")))
826 ;; Insertion with constraints on headlines level.
827 (should
828 (equal
829 "* Top heading\n** Heading\nbody\n"
830 (org-test-with-temp-text
831 (format
832 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\""
833 org-test-dir)
834 (org-export-expand-include-keyword)
835 (buffer-string))))
836 (should
837 (equal
838 "* Top heading\n* Heading\nbody\n"
839 (org-test-with-temp-text
840 (format
841 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\" :minlevel 1"
842 org-test-dir)
843 (org-export-expand-include-keyword)
844 (buffer-string))))
845 ;; Inclusion within an example block.
846 (should
847 (equal
848 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
849 (org-test-with-temp-text
850 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
851 org-test-dir)
852 (org-export-expand-include-keyword)
853 (buffer-string))))
854 ;; Inclusion within a src-block.
855 (should
856 (equal
857 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
858 (org-test-with-temp-text
859 (format
860 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
861 org-test-dir)
862 (org-export-expand-include-keyword)
863 (buffer-string))))
864 ;; Inclusion within an html export-block.
865 (should
866 (equal
867 "#+BEGIN_HTML\n<p>HTML!</p>\n#+END_HTML\n"
868 (org-test-with-temp-text
869 (format
870 "#+INCLUDE: \"%s/examples/include.html\" HTML"
871 org-test-dir)
872 (org-export-expand-include-keyword)
873 (buffer-string))))
874 ;; Inclusion within an center paragraph
875 (should
876 (equal
877 "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
878 (org-test-with-temp-text
879 (format
880 "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
881 org-test-dir)
882 (org-export-expand-include-keyword)
883 (buffer-string))))
884 ;; Footnotes labels are local to each included file.
885 (should
886 (= 6
887 (length
888 (delete-dups
889 (let ((contents "
890 Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote].
891 \[fn:1] Footnote 1
892 \[fn:test] Footnote \"test\""))
893 (org-test-with-temp-text-in-file contents
894 (let ((file1 (buffer-file-name)))
895 (org-test-with-temp-text-in-file contents
896 (let ((file2 (buffer-file-name)))
897 (org-test-with-temp-text
898 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
899 file1 file2)
900 (org-export-expand-include-keyword)
901 (org-element-map (org-element-parse-buffer)
902 'footnote-reference
903 (lambda (ref)
904 (org-element-property :label ref)))))))))))))
905 ;; Footnotes labels are not local to each include keyword.
906 (should
907 (= 4
908 (length
909 (delete-dups
910 (let ((contents "
911 Footnotes[fn:1], [fn:test], [2] and [fn:inline:anonymous footnote].
912 \[fn:1] Footnote 1
913 \[2] Footnote 2
914 \[fn:test] Footnote \"test\""))
915 (org-test-with-temp-text-in-file contents
916 (let ((file (buffer-file-name)))
917 (org-test-with-temp-text
918 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
919 (org-export-expand-include-keyword)
920 (org-element-map (org-element-parse-buffer)
921 'footnote-reference
922 (lambda (ref) (org-element-property :label ref)))))))))))
923 ;; Footnotes are supported by :lines-like elements and unnecessary
924 ;; footnotes are dropped.
925 (should
926 (= 4
927 (length
928 (delete-dups
929 (let ((contents "
930 * foo
931 Footnotes[fn:1]
932 * bar
933 Footnotes[fn:2], foot[fn:test], digit only[3], and [fn:inline:anonymous footnote]
935 \[fn:1] Footnote 1
936 \[fn:2] Footnote 1
937 * Footnotes
938 \[fn:test] Footnote \"test\"
939 \[3] Footnote 3
941 (org-test-with-temp-text-in-file contents
942 (let ((file (buffer-file-name)))
943 (org-test-with-temp-text
944 (format "#+INCLUDE: \"%s::*bar\"
945 " file)
946 (org-export-expand-include-keyword)
947 (org-element-map (org-element-parse-buffer)
948 'footnote-definition
949 (lambda (ref) (org-element-property :label ref)))))))))))
950 ;; If only-contents is non-nil only include contents of element.
951 (should
952 (equal
953 "body\n"
954 (org-test-with-temp-text
955 (concat
956 (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" " org-test-dir)
957 ":only-contents t")
958 (org-export-expand-include-keyword)
959 (buffer-string))))
960 ;; Headings can be included via CUSTOM_ID.
961 (should
962 (org-test-with-temp-text
963 (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
964 (org-export-expand-include-keyword)
965 (goto-char (point-min))
966 (looking-at "* Another heading")))
967 ;; Named objects can be included.
968 (should
969 (equal
970 "| 1 |\n"
971 (org-test-with-temp-text
972 (format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t" org-test-dir)
973 (org-export-expand-include-keyword)
974 (buffer-string))))
975 ;; Including non-existing elements should result in an error.
976 (should-error
977 (org-test-with-temp-text
978 (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\"" org-test-dir)
979 (org-export-expand-include-keyword)))
980 ;; Lines work relatively to an included element.
981 (should
982 (equal
983 "2\n3\n"
984 (org-test-with-temp-text
985 (format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t :lines \"2-3\"" org-test-dir)
986 (org-export-expand-include-keyword)
987 (buffer-string))))
988 ;; Properties should be dropped from headlines.
989 (should
990 (equal
991 (org-test-with-temp-text
992 (format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t" org-test-dir)
993 (org-export-expand-include-keyword)
994 (buffer-string))
995 (org-test-with-temp-text
996 (format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
997 (org-export-expand-include-keyword)
998 (buffer-string))))
999 ;; Properties should be dropped, drawers should not be.
1000 (should
1001 (equal
1002 ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
1003 (org-test-with-temp-text
1004 (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t" org-test-dir)
1005 (org-export-expand-include-keyword)
1006 (buffer-string))))
1007 ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
1008 (should
1009 (org-every (lambda (level) (zerop (1- level)))
1010 (org-test-with-temp-text
1011 (concat
1012 (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n" org-test-dir)
1013 (format "#+INCLUDE: \"%s/examples/include.org::*Heading\"" org-test-dir))
1014 (org-export-expand-include-keyword)
1015 (org-element-map (org-element-parse-buffer) 'headline
1016 (lambda (head) (org-element-property :level head))))))
1017 ;; INCLUDE does not insert induced :minlevel for src-blocks.
1018 (should-not
1019 (equal
1020 (org-test-with-temp-text
1021 (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp" org-test-dir)
1022 (org-export-expand-include-keyword)
1023 (buffer-string))
1024 (org-test-with-temp-text
1025 (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1" org-test-dir)
1026 (org-export-expand-include-keyword)
1027 (buffer-string))))
1028 ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
1029 (should
1030 (org-test-with-temp-text-in-file
1031 (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
1032 (narrow-to-region (point) (point-max))
1033 (org-export-expand-include-keyword)
1034 (eq 1 (org-current-level))))
1035 ;; If :minlevel is present do not alter it.
1036 (should
1037 (org-test-with-temp-text
1038 (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 3" org-test-dir)
1039 (narrow-to-region (point) (point-max))
1040 (org-export-expand-include-keyword)
1041 (eq 3 (org-current-level)))))
1043 (ert-deftest test-org-export/expand-macro ()
1044 "Test macro expansion in an Org buffer."
1045 ;; Standard macro expansion.
1046 (should
1047 (equal "#+MACRO: macro1 value\nvalue\n"
1048 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
1049 (org-export-as (org-test-default-backend)))))
1050 ;; Expand specific macros.
1051 (should
1052 (equal "me 2012-03-29 me@here Title\n"
1053 (org-test-with-temp-text
1055 #+TITLE: Title
1056 #+DATE: 2012-03-29
1057 #+AUTHOR: me
1058 #+EMAIL: me@here
1059 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
1060 (let ((output (org-export-as (org-test-default-backend))))
1061 (substring output (string-match ".*\n\\'" output))))))
1062 ;; Expand specific macros when property contained a regular macro
1063 ;; already.
1064 (should
1065 (equal "value\n"
1066 (org-test-with-temp-text "
1067 #+MACRO: macro1 value
1068 #+TITLE: {{{macro1}}}
1069 {{{title}}}"
1070 (let ((output (org-export-as (org-test-default-backend))))
1071 (substring output (string-match ".*\n\\'" output))))))
1072 ;; Expand macros with templates in included files.
1073 (should
1074 (equal "success\n"
1075 (org-test-with-temp-text
1076 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
1077 {{{included-macro}}}" org-test-dir)
1078 (let ((output (org-export-as (org-test-default-backend))))
1079 (substring output (string-match ".*\n\\'" output)))))))
1081 (ert-deftest test-org-export/user-ignore-list ()
1082 "Test if `:ignore-list' accepts user input."
1083 (let ((backend (org-test-default-backend)))
1084 (setf (org-export-backend-transcoders backend)
1085 (cons '(template . (lambda (body i)
1086 (format "BEGIN\n%sEND" body)))
1087 (org-export-backend-transcoders backend)))
1088 (org-test-with-temp-text "Text"
1089 (should (equal (org-export-as backend nil nil 'body-only)
1090 "Text\n"))
1091 (should (equal (org-export-as backend) "BEGIN\nText\nEND"))))
1092 (should
1093 (equal
1094 "* Head1\n"
1095 (let ((org-export-filter-parse-tree-functions
1096 '((lambda (data backend info)
1097 ;; Ignore headlines with the word "note" in their title.
1098 (org-element-map data 'headline
1099 (lambda (headline)
1100 (when (string-match "\\<note\\>"
1101 (org-element-property :raw-value
1102 headline))
1103 (org-export-ignore-element headline info)))
1104 info)
1105 data))))
1106 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
1107 (org-export-as (org-test-default-backend)))))))
1109 (ert-deftest test-org-export/before-processing-hook ()
1110 "Test `org-export-before-processing-hook'."
1111 (should
1112 (equal
1113 "#+MACRO: mac val\nTest\n"
1114 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
1115 (let ((org-export-before-processing-hook
1116 '((lambda (backend)
1117 (while (re-search-forward "{{{" nil t)
1118 (let ((object (org-element-context)))
1119 (when (eq (org-element-type object) 'macro)
1120 (delete-region
1121 (org-element-property :begin object)
1122 (org-element-property :end object)))))))))
1123 (org-export-as (org-test-default-backend)))))))
1125 (ert-deftest test-org-export/before-parsing-hook ()
1126 "Test `org-export-before-parsing-hook'."
1127 (should
1128 (equal "Body 1\nBody 2\n"
1129 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
1130 (let ((org-export-before-parsing-hook
1131 '((lambda (backend)
1132 (goto-char (point-min))
1133 (while (re-search-forward org-outline-regexp-bol nil t)
1134 (delete-region
1135 (point-at-bol) (progn (forward-line) (point))))))))
1136 (org-export-as (org-test-default-backend)))))))
1140 ;;; Affiliated Keywords
1142 (ert-deftest test-org-export/read-attribute ()
1143 "Test `org-export-read-attribute' specifications."
1144 ;; Standard test.
1145 (should
1146 (equal
1147 (org-export-read-attribute
1148 :attr_html
1149 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
1150 (org-element-at-point)))
1151 '(:a "1" :b "2")))
1152 ;; Return nil on empty attribute.
1153 (should-not
1154 (org-export-read-attribute
1155 :attr_html
1156 (org-test-with-temp-text "Paragraph" (org-element-at-point))))
1157 ;; Return nil on "nil" string.
1158 (should
1159 (equal '(:a nil :b nil)
1160 (org-export-read-attribute
1161 :attr_html
1162 (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
1163 (org-element-at-point)))))
1164 ;; Return nil on empty string.
1165 (should
1166 (equal '(:a nil :b nil)
1167 (org-export-read-attribute
1168 :attr_html
1169 (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
1170 (org-element-at-point)))))
1171 ;; Return empty string when value is "".
1172 (should
1173 (equal '(:a "")
1174 (org-export-read-attribute
1175 :attr_html
1176 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
1177 (org-element-at-point)))))
1178 ;; Return \"\" when value is """".
1179 (should
1180 (equal '(:a "\"\"")
1181 (org-export-read-attribute
1182 :attr_html
1183 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
1184 (org-element-at-point)))))
1185 ;; Ignore text before first property.
1186 (should-not
1187 (member "ignore"
1188 (org-export-read-attribute
1189 :attr_html
1190 (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
1191 (org-element-at-point))))))
1193 (ert-deftest test-org-export/get-caption ()
1194 "Test `org-export-get-caption' specifications."
1195 ;; Without optional argument, return long caption
1196 (should
1197 (equal
1198 '("l")
1199 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1200 (org-export-get-caption (org-element-at-point)))))
1201 ;; With optional argument, return short caption.
1202 (should
1203 (equal
1204 '("s")
1205 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1206 (org-export-get-caption (org-element-at-point) t))))
1207 ;; Multiple lines are separated by white spaces.
1208 (should
1209 (equal
1210 '("a" " " "b")
1211 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
1212 (org-export-get-caption (org-element-at-point))))))
1216 ;;; Back-End Tools
1218 (ert-deftest test-org-export/define-backend ()
1219 "Test back-end definition and accessors."
1220 ;; Translate table.
1221 (should
1222 (equal '((headline . my-headline-test))
1223 (let (org-export--registered-backends)
1224 (org-export-define-backend 'test '((headline . my-headline-test)))
1225 (org-export-get-all-transcoders 'test))))
1226 ;; Filters.
1227 (should
1228 (equal '((:filter-headline . my-filter))
1229 (let (org-export--registered-backends)
1230 (org-export-define-backend 'test
1231 '((headline . my-headline-test))
1232 :filters-alist '((:filter-headline . my-filter)))
1233 (org-export-backend-filters (org-export-get-backend 'test)))))
1234 ;; Options.
1235 (should
1236 (equal '((:prop value))
1237 (let (org-export--registered-backends)
1238 (org-export-define-backend 'test
1239 '((headline . my-headline-test))
1240 :options-alist '((:prop value)))
1241 (org-export-backend-options (org-export-get-backend 'test)))))
1242 ;; Menu.
1243 (should
1244 (equal '(?k "Test Export" test)
1245 (let (org-export--registered-backends)
1246 (org-export-define-backend 'test
1247 '((headline . my-headline-test))
1248 :menu-entry '(?k "Test Export" test))
1249 (org-export-backend-menu (org-export-get-backend 'test)))))
1250 ;; Export Blocks.
1251 (should
1252 (equal '(("TEST" . org-element-export-block-parser))
1253 (let (org-export--registered-backends org-element-block-name-alist)
1254 (org-export-define-backend 'test
1255 '((headline . my-headline-test))
1256 :export-block '("test"))
1257 org-element-block-name-alist))))
1259 (ert-deftest test-org-export/define-derived-backend ()
1260 "Test `org-export-define-derived-backend' specifications."
1261 ;; Error when parent back-end is not defined.
1262 (should-error
1263 (let (org-export--registered-backends)
1264 (org-export-define-derived-backend 'test 'parent)))
1265 ;; Append translation table to parent's.
1266 (should
1267 (equal '((:headline . test) (:headline . parent))
1268 (let (org-export--registered-backends)
1269 (org-export-define-backend 'parent '((:headline . parent)))
1270 (org-export-define-derived-backend 'test 'parent
1271 :translate-alist '((:headline . test)))
1272 (org-export-get-all-transcoders 'test))))
1273 ;; Options defined in the new back have priority over those defined
1274 ;; in parent.
1275 (should
1276 (eq 'test
1277 (let (org-export--registered-backends)
1278 (org-export-define-backend 'parent
1279 '((:headline . parent))
1280 :options-alist '((:a nil nil 'parent)))
1281 (org-export-define-derived-backend 'test 'parent
1282 :options-alist '((:a nil nil 'test)))
1283 (plist-get (org-export--get-global-options
1284 (org-export-get-backend 'test))
1285 :a)))))
1287 (ert-deftest test-org-export/derived-backend-p ()
1288 "Test `org-export-derived-backend-p' specifications."
1289 ;; Non-nil with direct match.
1290 (should
1291 (let (org-export--registered-backends)
1292 (org-export-define-backend 'test '((headline . test)))
1293 (org-export-derived-backend-p 'test 'test)))
1294 (should
1295 (let (org-export--registered-backends)
1296 (org-export-define-backend 'test '((headline . test)))
1297 (org-export-define-derived-backend 'test2 'test)
1298 (org-export-derived-backend-p 'test2 'test2)))
1299 ;; Non-nil with a direct parent.
1300 (should
1301 (let (org-export--registered-backends)
1302 (org-export-define-backend 'test '((headline . test)))
1303 (org-export-define-derived-backend 'test2 'test)
1304 (org-export-derived-backend-p 'test2 'test)))
1305 ;; Non-nil with an indirect parent.
1306 (should
1307 (let (org-export--registered-backends)
1308 (org-export-define-backend 'test '((headline . test)))
1309 (org-export-define-derived-backend 'test2 'test)
1310 (org-export-define-derived-backend 'test3 'test2)
1311 (org-export-derived-backend-p 'test3 'test)))
1312 ;; Nil otherwise.
1313 (should-not
1314 (let (org-export--registered-backends)
1315 (org-export-define-backend 'test '((headline . test)))
1316 (org-export-define-backend 'test2 '((headline . test2)))
1317 (org-export-derived-backend-p 'test2 'test)))
1318 (should-not
1319 (let (org-export--registered-backends)
1320 (org-export-define-backend 'test '((headline . test)))
1321 (org-export-define-backend 'test2 '((headline . test2)))
1322 (org-export-define-derived-backend 'test3 'test2)
1323 (org-export-derived-backend-p 'test3 'test))))
1325 (ert-deftest test-org-export/get-all-transcoders ()
1326 "Test `org-export-get-all-transcoders' specifications."
1327 ;; Return nil when back-end cannot be found.
1328 (should-not (org-export-get-all-transcoders nil))
1329 ;; Same as `org-export-transcoders' if no parent.
1330 (should
1331 (equal '((headline . ignore))
1332 (org-export-get-all-transcoders
1333 (org-export-create-backend
1334 :transcoders '((headline . ignore))))))
1335 ;; But inherit from all ancestors whenever possible.
1336 (should
1337 (equal '((section . ignore) (headline . ignore))
1338 (let (org-export--registered-backends)
1339 (org-export-define-backend 'b1 '((headline . ignore)))
1340 (org-export-get-all-transcoders
1341 (org-export-create-backend
1342 :parent 'b1 :transcoders '((section . ignore)))))))
1343 (should
1344 (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
1345 (let (org-export--registered-backends)
1346 (org-export-define-backend 'b1 '((headline . ignore)))
1347 (org-export-define-derived-backend 'b2 'b1
1348 :translate-alist '((section . ignore)))
1349 (org-export-get-all-transcoders
1350 (org-export-create-backend
1351 :parent 'b2 :transcoders '((paragraph . ignore)))))))
1352 ;; Back-end transcoders overrule inherited ones.
1353 (should
1354 (eq 'b
1355 (let (org-export--registered-backends)
1356 (org-export-define-backend 'b1 '((headline . a)))
1357 (cdr (assq 'headline
1358 (org-export-get-all-transcoders
1359 (org-export-create-backend
1360 :parent 'b1 :transcoders '((headline . b))))))))))
1362 (ert-deftest test-org-export/get-all-options ()
1363 "Test `org-export-get-all-options' specifications."
1364 ;; Return nil when back-end cannot be found.
1365 (should-not (org-export-get-all-options nil))
1366 ;; Same as `org-export-options' if no parent.
1367 (should
1368 (equal '((headline . ignore))
1369 (org-export-get-all-options
1370 (org-export-create-backend
1371 :options '((headline . ignore))))))
1372 ;; But inherit from all ancestors whenever possible.
1373 (should
1374 (equal '((:key2 value2) (:key1 value1))
1375 (let (org-export--registered-backends)
1376 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1377 (org-export-get-all-options
1378 (org-export-create-backend
1379 :parent 'b1 :options '((:key2 value2)))))))
1380 (should
1381 (equal '((:key3 value3) (:key2 value2) (:key1 value1))
1382 (let (org-export--registered-backends)
1383 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1384 (org-export-define-derived-backend 'b2 'b1
1385 :options-alist '((:key2 value2)))
1386 (org-export-get-all-options
1387 (org-export-create-backend
1388 :parent 'b2 :options '((:key3 value3)))))))
1389 ;; Back-end options overrule inherited ones.
1390 (should
1391 (eq 'b
1392 (let (org-export--registered-backends)
1393 (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
1394 (cdr (assq :key1
1395 (org-export-get-all-options
1396 (org-export-create-backend
1397 :parent 'b1 :options '((:key1 . b))))))))))
1399 (ert-deftest test-org-export/get-all-filters ()
1400 "Test `org-export-get-all-filters' specifications."
1401 ;; Return nil when back-end cannot be found.
1402 (should-not (org-export-get-all-filters nil))
1403 ;; Same as `org-export-filters' if no parent.
1404 (should
1405 (equal '((:filter-headline . ignore))
1406 (org-export-get-all-filters
1407 (org-export-create-backend
1408 :filters '((:filter-headline . ignore))))))
1409 ;; But inherit from all ancestors whenever possible.
1410 (should
1411 (equal '((:filter-section . ignore) (:filter-headline . ignore))
1412 (let (org-export--registered-backends)
1413 (org-export-define-backend 'b1
1414 nil :filters-alist '((:filter-headline . ignore)))
1415 (org-export-get-all-filters
1416 (org-export-create-backend
1417 :parent 'b1 :filters '((:filter-section . ignore)))))))
1418 (should
1419 (equal '((:filter-paragraph . ignore)
1420 (:filter-section . ignore)
1421 (:filter-headline . ignore))
1422 (let (org-export--registered-backends)
1423 (org-export-define-backend 'b1
1424 nil :filters-alist '((:filter-headline . ignore)))
1425 (org-export-define-derived-backend 'b2 'b1
1426 :filters-alist '((:filter-section . ignore)))
1427 (org-export-get-all-filters
1428 (org-export-create-backend
1429 :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
1430 ;; Back-end filters overrule inherited ones.
1431 (should
1432 (eq 'b
1433 (let (org-export--registered-backends)
1434 (org-export-define-backend 'b1 '((:filter-headline . a)))
1435 (cdr (assq :filter-headline
1436 (org-export-get-all-filters
1437 (org-export-create-backend
1438 :parent 'b1 :filters '((:filter-headline . b))))))))))
1440 (ert-deftest test-org-export/with-backend ()
1441 "Test `org-export-with-backend' definition."
1442 ;; Error when calling an undefined back-end
1443 (should-error (org-export-with-backend nil "Test"))
1444 ;; Error when called back-end doesn't have an appropriate
1445 ;; transcoder.
1446 (should-error
1447 (org-export-with-backend
1448 (org-export-create-backend :transcoders '((headline . ignore)))
1449 "Test"))
1450 ;; Otherwise, export using correct transcoder
1451 (should
1452 (equal "Success"
1453 (let (org-export--registered-backends)
1454 (org-export-define-backend 'test
1455 '((plain-text . (lambda (text contents info) "Failure"))))
1456 (org-export-define-backend 'test2
1457 '((plain-text . (lambda (text contents info) "Success"))))
1458 (org-export-with-backend 'test2 "Test"))))
1459 ;; Provide correct back-end if transcoder needs to use recursive
1460 ;; calls anyway.
1461 (should
1462 (equal "Success\n"
1463 (let ((test-back-end
1464 (org-export-create-backend
1465 :transcoders
1466 '((headline . (lambda (headline contents info)
1467 (org-export-data
1468 (org-element-property :title headline)
1469 info)))
1470 (plain-text . (lambda (text info) "Success"))))))
1471 (org-export-string-as
1472 "* Test"
1473 (org-export-create-backend
1474 :transcoders
1475 '((headline . (lambda (headline contents info)
1476 (org-export-with-backend
1477 test-back-end headline contents info))))))))))
1479 (ert-deftest test-org-export/data-with-backend ()
1480 "Test `org-export-data-with-backend' specifications."
1481 ;; Error when calling an undefined back-end.
1482 (should-error (org-export-data-with-backend nil "nil" nil))
1483 ;; Otherwise, export data recursively, using correct back-end.
1484 (should
1485 (equal
1486 "Success!"
1487 (org-export-data-with-backend
1488 '(bold nil "Test")
1489 (org-export-create-backend
1490 :transcoders
1491 '((plain-text . (lambda (text info) "Success"))
1492 (bold . (lambda (bold contents info) (concat contents "!")))))
1493 '(:with-emphasize t)))))
1497 ;;; Export Snippets
1499 (ert-deftest test-org-export/export-snippet ()
1500 "Test export snippets transcoding."
1501 ;; Standard test.
1502 (org-test-with-temp-text "@@test:A@@@@t:B@@"
1503 (let ((backend (org-test-default-backend)))
1504 (setf (org-export-backend-name backend) 'test)
1505 (setf (org-export-backend-transcoders backend)
1506 (cons (cons 'export-snippet
1507 (lambda (snippet contents info)
1508 (when (eq (org-export-snippet-backend snippet) 'test)
1509 (org-element-property :value snippet))))
1510 (org-export-backend-transcoders backend)))
1511 (let ((org-export-snippet-translation-alist nil))
1512 (should (equal (org-export-as backend) "A\n")))
1513 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
1514 (should (equal (org-export-as backend) "AB\n")))))
1515 ;; Ignored export snippets do not remove any blank.
1516 (should
1517 (equal "begin end\n"
1518 (org-test-with-parsed-data "begin @@test:A@@ end"
1519 (org-export-data-with-backend
1520 tree
1521 (org-export-create-backend
1522 :transcoders
1523 '((paragraph . (lambda (paragraph contents info) contents))
1524 (section . (lambda (section contents info) contents))))
1525 info)))))
1529 ;;; Footnotes
1531 (ert-deftest test-org-export/footnotes ()
1532 "Test footnotes specifications."
1533 (let ((org-footnote-section nil)
1534 (org-export-with-footnotes t))
1535 ;; 1. Read every type of footnote.
1536 (should
1537 (equal
1538 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
1539 (org-test-with-parsed-data
1540 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
1541 (org-element-map tree 'footnote-reference
1542 (lambda (ref)
1543 (let ((def (org-export-get-footnote-definition ref info)))
1544 (cons (org-export-get-footnote-number ref info)
1545 (if (eq (org-element-property :type ref) 'inline) (car def)
1546 (car (org-element-contents
1547 (car (org-element-contents def))))))))
1548 info))))
1549 ;; 2. Test nested footnotes order.
1550 (should
1551 (equal
1552 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
1553 (org-test-with-parsed-data
1554 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
1555 (org-element-map tree 'footnote-reference
1556 (lambda (ref)
1557 (when (org-export-footnote-first-reference-p ref info)
1558 (cons (org-export-get-footnote-number ref info)
1559 (org-element-property :label ref))))
1560 info))))
1561 ;; 3. Test nested footnote in invisible definitions.
1562 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
1563 ;; Hide definitions.
1564 (narrow-to-region (point) (point-at-eol))
1565 (let* ((tree (org-element-parse-buffer))
1566 (info (org-combine-plists
1567 `(:parse-tree ,tree)
1568 (org-export-collect-tree-properties
1569 tree (org-export-get-environment)))))
1570 ;; Both footnotes should be seen.
1571 (should
1572 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
1573 ;; 4. Test footnotes definitions collection.
1574 (should
1575 (= 4
1576 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1578 \[fn:2] B [fn:3] [fn::D].
1580 \[fn:3] C."
1581 (length (org-export-collect-footnote-definitions tree info)))))
1582 ;; 5. Test export of footnotes defined outside parsing scope.
1583 (should
1584 (equal
1585 "ParagraphOut of scope\n"
1586 (org-test-with-temp-text "[fn:1] Out of scope
1587 * Title
1588 Paragraph[fn:1]"
1589 (let ((backend (org-test-default-backend)))
1590 (setf (org-export-backend-transcoders backend)
1591 (cons (cons 'footnote-reference
1592 (lambda (fn contents info)
1593 (org-element-interpret-data
1594 (org-export-get-footnote-definition fn info))))
1595 (org-export-backend-transcoders backend)))
1596 (forward-line)
1597 (org-export-as backend 'subtree)))))
1598 ;; 6. Footnotes without a definition should throw an error.
1599 (should-error
1600 (org-test-with-parsed-data "Text[fn:1]"
1601 (org-export-get-footnote-definition
1602 (org-element-map tree 'footnote-reference 'identity info t) info)))
1603 ;; 7. Footnote section should be ignored in TOC and in headlines
1604 ;; numbering.
1605 (should
1606 (= 1 (let ((org-footnote-section "Footnotes"))
1607 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
1608 (org-export-collect-headlines info))))))
1609 (should
1610 (equal '(2)
1611 (let ((org-footnote-section "Footnotes"))
1612 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
1613 (org-element-map tree 'headline
1614 (lambda (hl)
1615 (when (equal (org-element-property :raw-value hl) "H2")
1616 (org-export-get-headline-number hl info)))
1617 info t)))))))
1621 ;;; Headlines and Inlinetasks
1623 (ert-deftest test-org-export/get-relative-level ()
1624 "Test `org-export-get-relative-level' specifications."
1625 ;; Standard test.
1626 (should
1627 (equal '(1 2)
1628 (let ((org-odd-levels-only nil))
1629 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1630 (org-element-map tree 'headline
1631 (lambda (h) (org-export-get-relative-level h info))
1632 info)))))
1633 ;; Missing levels
1634 (should
1635 (equal '(1 3)
1636 (let ((org-odd-levels-only nil))
1637 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
1638 (org-element-map tree 'headline
1639 (lambda (h) (org-export-get-relative-level h info))
1640 info))))))
1642 (ert-deftest test-org-export/low-level-p ()
1643 "Test `org-export-low-level-p' specifications."
1644 (should
1645 (equal
1646 '(no yes)
1647 (let ((org-odd-levels-only nil))
1648 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1649 (org-element-map tree 'headline
1650 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
1651 (plist-put info :headline-levels 1)))))))
1653 (ert-deftest test-org-export/get-headline-number ()
1654 "Test `org-export-get-headline-number' specifications."
1655 ;; Standard test.
1656 (should
1657 (equal
1658 '((1) (1 1))
1659 (let ((org-odd-levels-only nil))
1660 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1661 (org-element-map tree 'headline
1662 (lambda (h) (org-export-get-headline-number h info))
1663 info)))))
1664 ;; Missing levels are replaced with 0.
1665 (should
1666 (equal
1667 '((1) (1 0 1))
1668 (let ((org-odd-levels-only nil))
1669 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
1670 (org-element-map tree 'headline
1671 (lambda (h) (org-export-get-headline-number h info))
1672 info))))))
1674 (ert-deftest test-org-export/numbered-headline-p ()
1675 "Test `org-export-numbered-headline-p' specifications."
1676 ;; If `:section-numbers' is nil, never number headlines.
1677 (should-not
1678 (org-test-with-parsed-data "* Headline"
1679 (org-element-map tree 'headline
1680 (lambda (h) (org-export-numbered-headline-p h info))
1681 (plist-put info :section-numbers nil))))
1682 ;; If `:section-numbers' is a number, only number headlines with
1683 ;; a level greater that it.
1684 (should
1685 (equal
1686 '(yes no)
1687 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1688 (org-element-map tree 'headline
1689 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
1690 (plist-put info :section-numbers 1)))))
1691 ;; Otherwise, headlines are always numbered.
1692 (should
1693 (org-test-with-parsed-data "* Headline"
1694 (org-element-map tree 'headline
1695 (lambda (h) (org-export-numbered-headline-p h info))
1696 (plist-put info :section-numbers t)))))
1698 (ert-deftest test-org-export/org-export-get-headline-id ()
1699 "Test `org-export-get-headline-id' specifications."
1700 ;; Numbered headlines have IDs akin to "sec-N".
1701 (should
1702 (equal "sec-1"
1703 (org-test-with-parsed-data "* H"
1704 (org-export-get-headline-id
1705 (org-element-map tree 'headline #'identity info t)
1706 info))))
1707 ;; The ID of numbered headlines reflect the hierarchy.
1708 (should
1709 (equal "sec-1-1"
1710 (org-test-with-parsed-data "* H1\n** H2"
1711 (org-export-get-headline-id
1712 (org-element-map tree 'headline
1713 (lambda (h)
1714 (and (equal "H2" (org-element-property :raw-value h)) h))
1715 info t)
1716 info))))
1717 ;; Unnumbered headlines have IDs akin to "unnumbered-N".
1718 (should
1719 (equal "unnumbered-1"
1720 (org-test-with-parsed-data
1721 "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
1722 (org-export-get-headline-id
1723 (org-element-map tree 'headline #'identity info t)
1724 info))))
1725 ;; The ID of Unnumbered headlines do not reflect the hierarchy.
1726 (should
1727 (equal "unnumbered-2"
1728 (org-test-with-parsed-data
1729 "* H1\n:PROPERTIES:\n:UNNUMBERED: t\n:END:\n** H2"
1730 (org-export-get-headline-id
1731 (org-element-map tree 'headline
1732 (lambda (h)
1733 (and (equal "H2" (org-element-property :raw-value h)) h))
1734 info t)
1735 info))))
1736 ;; When #+OPTIONS: num:nil all headlines are unnumbered.
1737 (should
1738 (equal "unnumbered-1"
1739 (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
1740 (org-export-get-headline-id
1741 (org-element-map tree 'headline 'identity info t)
1742 info))))
1743 ;; UNNUMBERED ignores inheritance. Any non-nil value among
1744 ;; ancestors disables numbering.
1745 (should
1746 (org-test-with-parsed-data
1747 "* H
1748 :PROPERTIES:
1749 :UNNUMBERED: t
1750 :END:
1751 ** H2
1752 :PROPERTIES:
1753 :UNNUMBERED: nil
1754 :END:
1755 *** H3"
1756 (org-every
1757 (lambda (h) (not (org-export-numbered-headline-p h info)))
1758 (org-element-map tree 'headline #'identity info)))))
1760 (ert-deftest test-org-export/number-to-roman ()
1761 "Test `org-export-number-to-roman' specifications."
1762 ;; If number is negative, return it as a string.
1763 (should (equal (org-export-number-to-roman -1) "-1"))
1764 ;; Otherwise, return it as a roman number.
1765 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
1767 (ert-deftest test-org-export/get-optional-title ()
1768 "Test `org-export-get-alt-title' specifications."
1769 ;; If ALT_TITLE property is defined, use it.
1770 (should
1771 (equal '("opt")
1772 (org-test-with-parsed-data
1773 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
1774 (org-export-get-alt-title
1775 (org-element-map tree 'headline 'identity info t)
1776 info))))
1777 ;; Otherwise, fall-back to regular title.
1778 (should
1779 (equal '("Headline")
1780 (org-test-with-parsed-data "* Headline"
1781 (org-export-get-alt-title
1782 (org-element-map tree 'headline 'identity info t)
1783 info)))))
1785 (ert-deftest test-org-export/get-tags ()
1786 "Test `org-export-get-tags' specifications."
1787 (let ((org-export-exclude-tags '("noexport"))
1788 (org-export-select-tags '("export")))
1789 ;; Standard test: tags which are not a select tag, an exclude tag,
1790 ;; or specified as optional argument shouldn't be ignored.
1791 (should
1792 (org-test-with-parsed-data "* Headline :tag:"
1793 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1794 info)))
1795 ;; Exclude tags are removed.
1796 (should-not
1797 (org-test-with-parsed-data "* Headline :noexport:"
1798 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1799 info)))
1800 ;; Select tags are removed.
1801 (should-not
1802 (org-test-with-parsed-data "* Headline :export:"
1803 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1804 info)))
1805 (should
1806 (equal
1807 '("tag")
1808 (org-test-with-parsed-data "* Headline :tag:export:"
1809 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1810 info))))
1811 ;; Tags provided in the optional argument are also ignored.
1812 (should-not
1813 (org-test-with-parsed-data "* Headline :ignore:"
1814 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1815 info '("ignore"))))
1816 ;; Allow tag inheritance.
1817 (should
1818 (equal
1819 '(("tag") ("tag"))
1820 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
1821 (org-element-map tree 'headline
1822 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
1823 ;; Tag inheritance checks FILETAGS keywords.
1824 (should
1825 (equal
1826 '(("a" "b" "tag"))
1827 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
1828 (org-element-map tree 'headline
1829 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
1831 (ert-deftest test-org-export/get-node-property ()
1832 "Test`org-export-get-node-property' specifications."
1833 ;; Standard test.
1834 (should
1835 (equal "value"
1836 (org-test-with-parsed-data "* Headline
1837 :PROPERTIES:
1838 :prop: value
1839 :END:"
1840 (org-export-get-node-property
1841 :PROP (org-element-map tree 'headline 'identity nil t)))))
1842 ;; Test inheritance.
1843 (should
1844 (equal "value"
1845 (org-test-with-parsed-data "* Parent
1846 :PROPERTIES:
1847 :prop: value
1848 :END:
1849 ** Headline
1850 Paragraph"
1851 (org-export-get-node-property
1852 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
1853 ;; Cannot return a value before the first headline.
1854 (should-not
1855 (org-test-with-parsed-data "Paragraph
1856 * Headline
1857 :PROPERTIES:
1858 :prop: value
1859 :END:"
1860 (org-export-get-node-property
1861 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
1863 (ert-deftest test-org-export/get-category ()
1864 "Test `org-export-get-category' specifications."
1865 ;; Standard test.
1866 (should
1867 (equal "value"
1868 (org-test-with-parsed-data "* Headline
1869 :PROPERTIES:
1870 :CATEGORY: value
1871 :END:"
1872 (org-export-get-category
1873 (org-element-map tree 'headline 'identity nil t) info))))
1874 ;; Test inheritance from a parent headline.
1875 (should
1876 (equal '("value" "value")
1877 (org-test-with-parsed-data "* Headline1
1878 :PROPERTIES:
1879 :CATEGORY: value
1880 :END:
1881 ** Headline2"
1882 (org-element-map tree 'headline
1883 (lambda (hl) (org-export-get-category hl info)) info))))
1884 ;; Test inheritance from #+CATEGORY keyword
1885 (should
1886 (equal "value"
1887 (org-test-with-parsed-data "#+CATEGORY: value
1888 * Headline"
1889 (org-export-get-category
1890 (org-element-map tree 'headline 'identity nil t) info))))
1891 ;; Test inheritance from file name.
1892 (should
1893 (equal "test"
1894 (org-test-with-parsed-data "* Headline"
1895 (let ((info (plist-put info :input-file "~/test.org")))
1896 (org-export-get-category
1897 (org-element-map tree 'headline 'identity nil t) info)))))
1898 ;; Fall-back value.
1899 (should
1900 (equal "???"
1901 (org-test-with-parsed-data "* Headline"
1902 (org-export-get-category
1903 (org-element-map tree 'headline 'identity nil t) info)))))
1905 (ert-deftest test-org-export/first-sibling-p ()
1906 "Test `org-export-first-sibling-p' specifications."
1907 ;; Standard test.
1908 (should
1909 (equal
1910 '(yes yes no)
1911 (org-test-with-parsed-data "* H\n** H 2\n** H 3"
1912 (org-element-map tree 'headline
1913 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1914 info))))
1915 (should
1916 (equal '(yes no)
1917 (org-test-with-parsed-data "- item\n\n para"
1918 (org-element-map tree 'paragraph
1919 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1920 info))))
1921 ;; Ignore sections for headlines.
1922 (should
1923 (equal '(yes yes)
1924 (org-test-with-parsed-data "* H\nSection\n** H 2"
1925 (org-element-map tree 'headline
1926 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1927 info))))
1928 ;; Ignore headlines not exported.
1929 (should
1930 (equal
1931 '(yes)
1932 (let ((org-export-exclude-tags '("ignore")))
1933 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
1934 (org-element-map tree 'headline
1935 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1936 info))))))
1938 (ert-deftest test-org-export/last-sibling-p ()
1939 "Test `org-export-last-sibling-p' specifications."
1940 ;; Standard test.
1941 (should
1942 (equal
1943 '(yes no yes)
1944 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1945 (org-element-map tree 'headline
1946 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1947 info))))
1948 (should
1949 (equal '(no yes)
1950 (org-test-with-parsed-data "- item\n\n para"
1951 (org-element-map tree 'paragraph
1952 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1953 info))))
1954 ;; Ignore headlines not exported.
1955 (should
1956 (equal
1957 '(yes)
1958 (let ((org-export-exclude-tags '("ignore")))
1959 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
1960 (org-element-map tree 'headline
1961 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1962 info))))))
1964 (ert-deftest test-org-export/handle-inlinetasks ()
1965 "Test inlinetask export."
1966 ;; Inlinetask with an exclude tag.
1967 (when (featurep 'org-inlinetask)
1968 (should
1969 (equal
1971 (let ((org-inlinetask-min-level 3))
1972 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
1973 (org-export-as (org-test-default-backend)
1974 nil nil nil '(:exclude-tags ("noexp")))))))
1975 ;; Inlinetask with an include tag.
1976 (should
1977 (equal
1978 "* H2\n*** Inline :exp:\n"
1979 (let ((org-inlinetask-min-level 3)
1980 (org-tags-column 0))
1981 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
1982 (org-export-as (org-test-default-backend)
1983 nil nil nil '(:select-tags ("exp")))))))
1984 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
1985 (should
1986 (equal ""
1987 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
1988 (org-inlinetask-min-level 3))
1989 (org-test-with-temp-text "*** TODO Inline"
1990 (org-export-as (org-test-default-backend)
1991 nil nil nil '(:with-tasks nil))))))))
1995 ;;; Keywords
1997 (ert-deftest test-org-export/get-date ()
1998 "Test `org-export-get-date' specifications."
1999 ;; Return a properly formatted string when
2000 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
2001 ;; consists in a single timestamp.
2002 (should
2003 (equal "29 03 2012"
2004 (let ((org-export-date-timestamp-format "%d %m %Y"))
2005 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2006 (org-export-get-date info)))))
2007 ;; Return a secondary string otherwise.
2008 (should-not
2009 (stringp
2010 (let ((org-export-date-timestamp-format nil))
2011 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2012 (org-export-get-date info)))))
2013 (should
2014 (equal '("Date")
2015 (org-test-with-parsed-data "#+DATE: Date"
2016 (org-export-get-date info))))
2017 ;; Optional argument has precedence over
2018 ;; `org-export-date-timestamp-format'.
2019 (should
2020 (equal "29 03"
2021 (let ((org-export-date-timestamp-format "%d %m %Y"))
2022 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2023 (org-export-get-date info "%d %m"))))))
2027 ;;; Links
2029 (ert-deftest test-org-export/custom-protocol-maybe ()
2030 "Test `org-export-custom-protocol-maybe' specifications."
2031 (should
2032 (string-match
2033 "success"
2034 (let ((org-link-types (copy-sequence org-link-types)))
2035 (org-add-link-type "foo" nil (lambda (p d f) "success"))
2036 (org-export-string-as
2037 "[[foo:path]]"
2038 (org-export-create-backend
2039 :name 'test
2040 :transcoders '((section . (lambda (s c i) c))
2041 (paragraph . (lambda (p c i) c))
2042 (link . (lambda (l c i)
2043 (or (org-export-custom-protocol-maybe l c i)
2044 "failure")))))))))
2045 (should-not
2046 (string-match
2047 "success"
2048 (let ((org-link-types (copy-sequence org-link-types)))
2049 (org-add-link-type
2050 "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
2051 (org-export-string-as
2052 "[[foo:path]]"
2053 (org-export-create-backend
2054 :name 'no-test
2055 :transcoders '((section . (lambda (s c i) c))
2056 (paragraph . (lambda (p c i) c))
2057 (link . (lambda (l c i)
2058 (or (org-export-custom-protocol-maybe l c i)
2059 "failure")))))))))
2060 ;; Ignore anonymous back-ends.
2061 (should-not
2062 (string-match
2063 "success"
2064 (let ((org-link-types (copy-sequence org-link-types)))
2065 (org-add-link-type
2066 "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
2067 (org-export-string-as
2068 "[[foo:path]]"
2069 (org-export-create-backend
2070 :transcoders '((section . (lambda (s c i) c))
2071 (paragraph . (lambda (p c i) c))
2072 (link . (lambda (l c i)
2073 (or (org-export-custom-protocol-maybe l c i)
2074 "failure"))))))))))
2076 (ert-deftest test-org-export/get-coderef-format ()
2077 "Test `org-export-get-coderef-format' specifications."
2078 ;; A link without description returns "%s"
2079 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
2080 "%s"))
2081 ;; Return "%s" when path is matched within description.
2082 (should (equal (org-export-get-coderef-format "path" "desc (path)")
2083 "desc %s"))
2084 ;; Otherwise return description.
2085 (should (equal (org-export-get-coderef-format "path" "desc")
2086 "desc")))
2088 (ert-deftest test-org-export/inline-image-p ()
2089 "Test `org-export-inline-image-p' specifications."
2090 (should
2091 (org-export-inline-image-p
2092 (org-test-with-temp-text "[[#id]]"
2093 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
2094 '(("custom-id" . "id")))))
2096 (ert-deftest test-org-export/fuzzy-link ()
2097 "Test fuzzy links specifications."
2098 ;; Link to an headline should return headline's number.
2099 (org-test-with-parsed-data
2100 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
2101 (should
2102 ;; Note: Headline's number is in fact a list of numbers.
2103 (equal '(2)
2104 (org-element-map tree 'link
2105 (lambda (link)
2106 (org-export-get-ordinal
2107 (org-export-resolve-fuzzy-link link info) info)) info t))))
2108 ;; Link to a target in an item should return item's number.
2109 (org-test-with-parsed-data
2110 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
2111 (should
2112 ;; Note: Item's number is in fact a list of numbers.
2113 (equal '(1 2)
2114 (org-element-map tree 'link
2115 (lambda (link)
2116 (org-export-get-ordinal
2117 (org-export-resolve-fuzzy-link link info) info)) info t))))
2118 ;; Link to a target in a footnote should return footnote's number.
2119 (org-test-with-parsed-data "
2120 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
2121 (should
2122 (equal '(2 3)
2123 (org-element-map tree 'link
2124 (lambda (link)
2125 (org-export-get-ordinal
2126 (org-export-resolve-fuzzy-link link info) info)) info))))
2127 ;; Link to a named element should return sequence number of that
2128 ;; element.
2129 (org-test-with-parsed-data
2130 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
2131 (should
2132 (= 2
2133 (org-element-map tree 'link
2134 (lambda (link)
2135 (org-export-get-ordinal
2136 (org-export-resolve-fuzzy-link link info) info)) info t))))
2137 ;; Link to a target not within an item, a table, a footnote
2138 ;; reference or definition should return section number.
2139 (org-test-with-parsed-data
2140 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
2141 (should
2142 (equal '(2)
2143 (org-element-map tree 'link
2144 (lambda (link)
2145 (org-export-get-ordinal
2146 (org-export-resolve-fuzzy-link link info) info)) info t))))
2147 ;; Space are not significant when matching a fuzzy link.
2148 (should
2149 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
2150 (org-element-map tree 'link
2151 (lambda (link) (org-export-resolve-fuzzy-link link info))
2152 info t)))
2153 ;; Statistics cookies are ignored for headline match.
2154 (should
2155 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
2156 (org-element-map tree 'link
2157 (lambda (link) (org-export-resolve-fuzzy-link link info))
2158 info t)))
2159 (should
2160 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
2161 (org-element-map tree 'link
2162 (lambda (link) (org-export-resolve-fuzzy-link link info))
2163 info t)))
2164 ;; Headline match is position dependent.
2165 (should-not
2166 (apply
2168 (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
2169 (org-element-map tree 'link
2170 (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
2172 (ert-deftest test-org-export/resolve-coderef ()
2173 "Test `org-export-resolve-coderef' specifications."
2174 (let ((org-coderef-label-format "(ref:%s)"))
2175 ;; 1. A link to a "-n -k -r" block returns line number.
2176 (org-test-with-parsed-data
2177 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
2178 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2179 (org-test-with-parsed-data
2180 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2181 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2182 ;; 2. A link to a "-n -r" block returns line number.
2183 (org-test-with-parsed-data
2184 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
2185 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2186 (org-test-with-parsed-data
2187 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2188 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2189 ;; 3. A link to a "-n" block returns coderef.
2190 (org-test-with-parsed-data
2191 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2192 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2193 (org-test-with-parsed-data
2194 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
2195 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2196 ;; 4. A link to a "-r" block returns line number.
2197 (org-test-with-parsed-data
2198 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2199 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2200 (org-test-with-parsed-data
2201 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
2202 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2203 ;; 5. A link to a block without a switch returns coderef.
2204 (org-test-with-parsed-data
2205 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2206 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2207 (org-test-with-parsed-data
2208 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
2209 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2210 ;; 6. Correctly handle continued line numbers. A "+n" switch
2211 ;; should resume numbering from previous block with numbered
2212 ;; lines, ignoring blocks not numbering lines in the process.
2213 ;; A "-n" switch resets count.
2214 (org-test-with-parsed-data "
2215 #+BEGIN_EXAMPLE -n
2216 Text.
2217 #+END_EXAMPLE
2219 #+BEGIN_SRC emacs-lisp
2220 \(- 1 1)
2221 #+END_SRC
2223 #+BEGIN_SRC emacs-lisp +n -r
2224 \(+ 1 1) (ref:addition)
2225 #+END_SRC
2227 #+BEGIN_EXAMPLE -n -r
2228 Another text. (ref:text)
2229 #+END_EXAMPLE"
2230 (should (= (org-export-resolve-coderef "addition" info) 2))
2231 (should (= (org-export-resolve-coderef "text" info) 1)))
2232 ;; 7. Recognize coderef with user-specified syntax.
2233 (org-test-with-parsed-data
2234 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
2235 (should (equal (org-export-resolve-coderef "text" info) "text")))))
2237 (ert-deftest test-org-export/resolve-fuzzy-link ()
2238 "Test `org-export-resolve-fuzzy-link' specifications."
2239 ;; Match target objects.
2240 (should
2241 (org-test-with-parsed-data "<<target>> [[target]]"
2242 (org-export-resolve-fuzzy-link
2243 (org-element-map tree 'link 'identity info t) info)))
2244 ;; Match named elements.
2245 (should
2246 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
2247 (org-export-resolve-fuzzy-link
2248 (org-element-map tree 'link 'identity info t) info)))
2249 ;; Match exact headline's name.
2250 (should
2251 (org-test-with-parsed-data "* My headline\n[[My headline]]"
2252 (org-export-resolve-fuzzy-link
2253 (org-element-map tree 'link 'identity info t) info)))
2254 ;; Targets objects have priority over named elements and headline
2255 ;; titles.
2256 (should
2257 (eq 'target
2258 (org-test-with-parsed-data
2259 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
2260 (org-element-type
2261 (org-export-resolve-fuzzy-link
2262 (org-element-map tree 'link 'identity info t) info)))))
2263 ;; Named elements have priority over headline titles.
2264 (should
2265 (eq 'paragraph
2266 (org-test-with-parsed-data
2267 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
2268 (org-element-type
2269 (org-export-resolve-fuzzy-link
2270 (org-element-map tree 'link 'identity info t) info)))))
2271 ;; If link's path starts with a "*", only match headline titles,
2272 ;; though.
2273 (should
2274 (eq 'headline
2275 (org-test-with-parsed-data
2276 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
2277 (org-element-type
2278 (org-export-resolve-fuzzy-link
2279 (org-element-map tree 'link 'identity info t) info)))))
2280 ;; Return nil if no match.
2281 (should-not
2282 (org-test-with-parsed-data "[[target]]"
2283 (org-export-resolve-fuzzy-link
2284 (org-element-map tree 'link 'identity info t) info)))
2285 ;; Match fuzzy link even when before first headline.
2286 (should
2287 (eq 'headline
2288 (org-test-with-parsed-data "[[hl]]\n* hl"
2289 (org-element-type
2290 (org-export-resolve-fuzzy-link
2291 (org-element-map tree 'link 'identity info t) info))))))
2293 (ert-deftest test-org-export/resolve-id-link ()
2294 "Test `org-export-resolve-id-link' specifications."
2295 ;; 1. Regular test for custom-id link.
2296 (org-test-with-parsed-data "* Headline1
2297 :PROPERTIES:
2298 :CUSTOM_ID: test
2299 :END:
2300 * Headline 2
2301 \[[#test]]"
2302 (should
2303 (org-export-resolve-id-link
2304 (org-element-map tree 'link 'identity info t) info)))
2305 ;; 2. Failing test for custom-id link.
2306 (org-test-with-parsed-data "* Headline1
2307 :PROPERTIES:
2308 :CUSTOM_ID: test
2309 :END:
2310 * Headline 2
2311 \[[#no-match]]"
2312 (should-not
2313 (org-export-resolve-id-link
2314 (org-element-map tree 'link 'identity info t) info)))
2315 ;; 3. Test for internal id target.
2316 (org-test-with-parsed-data "* Headline1
2317 :PROPERTIES:
2318 :ID: aaaa
2319 :END:
2320 * Headline 2
2321 \[[id:aaaa]]"
2322 (should
2323 (org-export-resolve-id-link
2324 (org-element-map tree 'link 'identity info t) info)))
2325 ;; 4. Test for external id target.
2326 (org-test-with-parsed-data "[[id:aaaa]]"
2327 (should
2328 (org-export-resolve-id-link
2329 (org-element-map tree 'link 'identity info t)
2330 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
2332 (ert-deftest test-org-export/resolve-radio-link ()
2333 "Test `org-export-resolve-radio-link' specifications."
2334 ;; Standard test.
2335 (should
2336 (org-test-with-temp-text "<<<radio>>> radio"
2337 (org-update-radio-target-regexp)
2338 (let* ((tree (org-element-parse-buffer))
2339 (info `(:parse-tree ,tree)))
2340 (org-export-resolve-radio-link
2341 (org-element-map tree 'link 'identity info t)
2342 info))))
2343 ;; Radio targets are case-insensitive.
2344 (should
2345 (org-test-with-temp-text "<<<RADIO>>> radio"
2346 (org-update-radio-target-regexp)
2347 (let* ((tree (org-element-parse-buffer))
2348 (info `(:parse-tree ,tree)))
2349 (org-export-resolve-radio-link
2350 (org-element-map tree 'link 'identity info t)
2351 info))))
2352 ;; Radio target with objects.
2353 (should
2354 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
2355 (org-update-radio-target-regexp)
2356 (let* ((tree (org-element-parse-buffer))
2357 (info `(:parse-tree ,tree)))
2358 (org-export-resolve-radio-link
2359 (org-element-map tree 'link 'identity info t)
2360 info))))
2361 ;; Radio target with objects at its beginning.
2362 (should
2363 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
2364 (org-update-radio-target-regexp)
2365 (let* ((tree (org-element-parse-buffer))
2366 (info `(:parse-tree ,tree)))
2367 (org-export-resolve-radio-link
2368 (org-element-map tree 'link 'identity info t)
2369 info))))
2370 ;; Radio link next to an apostrophe.
2371 (should
2372 (org-test-with-temp-text "<<<radio>>> radio's"
2373 (org-update-radio-target-regexp)
2374 (let* ((tree (org-element-parse-buffer))
2375 (info `(:parse-tree ,tree)))
2376 (org-export-resolve-radio-link
2377 (org-element-map tree 'link 'identity info t)
2378 info))))
2379 ;; Multiple radio targets.
2380 (should
2381 (equal '("radio1" "radio2")
2382 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
2383 (org-update-radio-target-regexp)
2384 (let* ((tree (org-element-parse-buffer))
2385 (info `(:parse-tree ,tree)))
2386 (org-element-map tree 'link
2387 (lambda (link)
2388 (org-element-property
2389 :value (org-export-resolve-radio-link link info)))
2390 info)))))
2391 ;; Radio target is whitespace insensitive.
2392 (should
2393 (org-test-with-temp-text "<<<a radio>>> a\n radio"
2394 (org-update-radio-target-regexp)
2395 (let* ((tree (org-element-parse-buffer))
2396 (info `(:parse-tree ,tree)))
2397 (org-element-map tree 'link
2398 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
2402 ;;; Src-block and example-block
2404 (ert-deftest test-org-export/unravel-code ()
2405 "Test `org-export-unravel-code' function."
2406 ;; Code without reference.
2407 (should
2408 (equal '("(+ 1 1)\n")
2409 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2410 (org-export-unravel-code (org-element-at-point)))))
2411 ;; Code with reference.
2412 (should
2413 (equal '("(+ 1 1)\n" (1 . "test"))
2414 (org-test-with-temp-text
2415 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
2416 (let ((org-coderef-label-format "(ref:%s)"))
2417 (org-export-unravel-code (org-element-at-point))))))
2418 ;; Code with user-defined reference.
2419 (should
2420 (equal
2421 '("(+ 1 1)\n" (1 . "test"))
2422 (org-test-with-temp-text
2423 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
2424 (let ((org-coderef-label-format "(ref:%s)"))
2425 (org-export-unravel-code (org-element-at-point))))))
2426 ;; Code references keys are relative to the current block.
2427 (should
2428 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
2429 (org-test-with-temp-text "
2430 #+BEGIN_EXAMPLE -n
2431 \(+ 1 1)
2432 #+END_EXAMPLE
2433 #+BEGIN_EXAMPLE +n
2434 \(+ 2 2)
2435 \(+ 3 3) (ref:one)
2436 #+END_EXAMPLE"
2437 (goto-line 5)
2438 (let ((org-coderef-label-format "(ref:%s)"))
2439 (org-export-unravel-code (org-element-at-point)))))))
2441 (ert-deftest test-org-export/format-code-default ()
2442 "Test `org-export-format-code-default' specifications."
2443 ;; Return the empty string when code is empty.
2444 (should
2445 (equal ""
2446 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
2447 (org-export-format-code-default
2448 (org-element-map tree 'src-block 'identity info t) info))))
2449 ;; Number lines, two whitespace characters before the actual loc.
2450 (should
2451 (equal "1 a\n2 b\n"
2452 (org-test-with-parsed-data
2453 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
2454 (org-export-format-code-default
2455 (org-element-map tree 'src-block 'identity info t) info))))
2456 ;; Put references 6 whitespace characters after the widest line,
2457 ;; wrapped within parenthesis.
2458 (should
2459 (equal "123 (a)\n1 (b)\n"
2460 (let ((org-coderef-label-format "(ref:%s)"))
2461 (org-test-with-parsed-data
2462 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
2463 (org-export-format-code-default
2464 (org-element-map tree 'src-block 'identity info t) info))))))
2468 ;;; Smart Quotes
2470 (ert-deftest test-org-export/activate-smart-quotes ()
2471 "Test `org-export-activate-smart-quotes' specifications."
2472 ;; Opening double quotes: standard test.
2473 (should
2474 (equal
2475 '("some &ldquo;paragraph")
2476 (let ((org-export-default-language "en"))
2477 (org-test-with-parsed-data "some \"paragraph"
2478 (org-element-map tree 'plain-text
2479 (lambda (s) (org-export-activate-smart-quotes s :html info))
2480 info)))))
2481 ;; Opening quotes: at the beginning of a paragraph.
2482 (should
2483 (equal
2484 '("&ldquo;begin")
2485 (let ((org-export-default-language "en"))
2486 (org-test-with-parsed-data "\"begin"
2487 (org-element-map tree 'plain-text
2488 (lambda (s) (org-export-activate-smart-quotes s :html info))
2489 info)))))
2490 ;; Opening quotes: after an object.
2491 (should
2492 (equal
2493 '("&ldquo;begin")
2494 (let ((org-export-default-language "en"))
2495 (org-test-with-parsed-data "=verb= \"begin"
2496 (org-element-map tree 'plain-text
2497 (lambda (s) (org-export-activate-smart-quotes s :html info))
2498 info)))))
2499 ;; Closing quotes: standard test.
2500 (should
2501 (equal
2502 '("some&rdquo; paragraph")
2503 (let ((org-export-default-language "en"))
2504 (org-test-with-parsed-data "some\" paragraph"
2505 (org-element-map tree 'plain-text
2506 (lambda (s) (org-export-activate-smart-quotes s :html info))
2507 info)))))
2508 ;; Closing quotes: at the end of a paragraph.
2509 (should
2510 (equal
2511 '("end&rdquo;")
2512 (let ((org-export-default-language "en"))
2513 (org-test-with-parsed-data "end\""
2514 (org-element-map tree 'plain-text
2515 (lambda (s) (org-export-activate-smart-quotes s :html info))
2516 info)))))
2517 ;; Apostrophe: standard test.
2518 (should
2519 (equal
2520 '("It shouldn&rsquo;t fail")
2521 (let ((org-export-default-language "en"))
2522 (org-test-with-parsed-data "It shouldn't fail"
2523 (org-element-map tree 'plain-text
2524 (lambda (s) (org-export-activate-smart-quotes s :html info))
2525 info)))))
2526 ;; Apostrophe: before an object.
2527 (should
2528 (equal
2529 '("a&rsquo;")
2530 (let ((org-export-default-language "en"))
2531 (org-test-with-parsed-data "a'=b="
2532 (org-element-map tree 'plain-text
2533 (lambda (s) (org-export-activate-smart-quotes s :html info))
2534 info)))))
2535 ;; Apostrophe: after an object.
2536 (should
2537 (equal
2538 '("&rsquo;s")
2539 (let ((org-export-default-language "en"))
2540 (org-test-with-parsed-data "=code='s"
2541 (org-element-map tree 'plain-text
2542 (lambda (s) (org-export-activate-smart-quotes s :html info))
2543 info)))))
2544 ;; Special case: isolated quotes.
2545 (should
2546 (equal '("&ldquo;" "&rdquo;")
2547 (let ((org-export-default-language "en"))
2548 (org-test-with-parsed-data "\"$x$\""
2549 (org-element-map tree 'plain-text
2550 (lambda (s) (org-export-activate-smart-quotes s :html info))
2551 info)))))
2552 ;; Smart quotes in secondary strings.
2553 (should
2554 (equal '("&ldquo;" "&rdquo;")
2555 (let ((org-export-default-language "en"))
2556 (org-test-with-parsed-data "* \"$x$\""
2557 (org-element-map tree 'plain-text
2558 (lambda (s) (org-export-activate-smart-quotes s :html info))
2559 info)))))
2560 ;; Smart quotes in document keywords.
2561 (should
2562 (equal '("&ldquo;" "&rdquo;")
2563 (let ((org-export-default-language "en"))
2564 (org-test-with-parsed-data "#+TITLE: \"$x$\""
2565 (org-element-map (plist-get info :title) 'plain-text
2566 (lambda (s) (org-export-activate-smart-quotes s :html info))
2567 info)))))
2568 ;; Smart quotes in parsed affiliated keywords.
2569 (should
2570 (equal '("&ldquo;" "&rdquo;" "Paragraph")
2571 (let ((org-export-default-language "en"))
2572 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
2573 (org-element-map tree 'plain-text
2574 (lambda (s) (org-export-activate-smart-quotes s :html info))
2575 info nil nil t))))))
2579 ;;; Tables
2581 (ert-deftest test-org-export/special-column ()
2582 "Test if the table's special column is properly recognized."
2583 ;; 1. First column is special if it contains only a special marking
2584 ;; characters or empty cells.
2585 (org-test-with-temp-text "
2586 | ! | 1 |
2587 | | 2 |"
2588 (should
2589 (org-export-table-has-special-column-p
2590 (org-element-map
2591 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2592 ;; 2. If the column contains anything else, it isn't special.
2593 (org-test-with-temp-text "
2594 | ! | 1 |
2595 | b | 2 |"
2596 (should-not
2597 (org-export-table-has-special-column-p
2598 (org-element-map
2599 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2600 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
2601 ;; and "!".
2602 (org-test-with-temp-text "
2603 | # | 1 |
2604 | ^ | 2 |
2605 | * | 3 |
2606 | _ | 4 |
2607 | / | 5 |
2608 | $ | 6 |
2609 | ! | 7 |"
2610 (should
2611 (org-export-table-has-special-column-p
2612 (org-element-map
2613 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2614 ;; 4. A first column with only empty cells isn't considered as
2615 ;; special.
2616 (org-test-with-temp-text "
2617 | | 1 |
2618 | | 2 |"
2619 (should-not
2620 (org-export-table-has-special-column-p
2621 (org-element-map
2622 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
2624 (ert-deftest test-org-export/table-row-is-special-p ()
2625 "Test `org-export-table-row-is-special-p' specifications."
2626 ;; 1. A row is special if it has a special marking character in the
2627 ;; special column.
2628 (org-test-with-parsed-data "| ! | 1 |"
2629 (should
2630 (org-export-table-row-is-special-p
2631 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2632 ;; 2. A row is special when its first field is "/"
2633 (org-test-with-parsed-data "
2634 | / | 1 |
2635 | a | b |"
2636 (should
2637 (org-export-table-row-is-special-p
2638 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2639 ;; 3. A row only containing alignment cookies is also considered as
2640 ;; special.
2641 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
2642 (should
2643 (org-export-table-row-is-special-p
2644 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2645 ;; 4. Everything else isn't considered as special.
2646 (org-test-with-parsed-data "| \alpha | | c |"
2647 (should-not
2648 (org-export-table-row-is-special-p
2649 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2650 ;; 5. Table's rules are never considered as special rows.
2651 (org-test-with-parsed-data "|---+---|"
2652 (should-not
2653 (org-export-table-row-is-special-p
2654 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
2656 (ert-deftest test-org-export/has-header-p ()
2657 "Test `org-export-table-has-header-p' specifications."
2658 ;; 1. With an header.
2659 (org-test-with-parsed-data "
2660 | a | b |
2661 |---+---|
2662 | c | d |"
2663 (should
2664 (org-export-table-has-header-p
2665 (org-element-map tree 'table 'identity info 'first-match)
2666 info)))
2667 ;; 2. Without an header.
2668 (org-test-with-parsed-data "
2669 | a | b |
2670 | c | d |"
2671 (should-not
2672 (org-export-table-has-header-p
2673 (org-element-map tree 'table 'identity info 'first-match)
2674 info)))
2675 ;; 3. Don't get fooled with starting and ending rules.
2676 (org-test-with-parsed-data "
2677 |---+---|
2678 | a | b |
2679 | c | d |
2680 |---+---|"
2681 (should-not
2682 (org-export-table-has-header-p
2683 (org-element-map tree 'table 'identity info 'first-match)
2684 info))))
2686 (ert-deftest test-org-export/table-row-group ()
2687 "Test `org-export-table-row-group' specifications."
2688 ;; 1. A rule creates a new group.
2689 (should
2690 (equal '(1 rule 2)
2691 (org-test-with-parsed-data "
2692 | a | b |
2693 |---+---|
2694 | 1 | 2 |"
2695 (org-element-map tree 'table-row
2696 (lambda (row)
2697 (if (eq (org-element-property :type row) 'rule) 'rule
2698 (org-export-table-row-group row info)))))))
2699 ;; 2. Special rows are ignored in count.
2700 (should
2701 (equal
2702 '(rule 1)
2703 (org-test-with-parsed-data "
2704 | / | < | > |
2705 |---|---+---|
2706 | | 1 | 2 |"
2707 (org-element-map tree 'table-row
2708 (lambda (row)
2709 (if (eq (org-element-property :type row) 'rule) 'rule
2710 (org-export-table-row-group row info)))
2711 info))))
2712 ;; 3. Double rules also are ignored in count.
2713 (should
2714 (equal '(1 rule rule 2)
2715 (org-test-with-parsed-data "
2716 | a | b |
2717 |---+---|
2718 |---+---|
2719 | 1 | 2 |"
2720 (org-element-map tree 'table-row
2721 (lambda (row)
2722 (if (eq (org-element-property :type row) 'rule) 'rule
2723 (org-export-table-row-group row info))))))))
2725 (ert-deftest test-org-export/table-row-number ()
2726 "Test `org-export-table-row-number' specifications."
2727 ;; Standard test. Number is 0-indexed.
2728 (should
2729 (equal '(0 1)
2730 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
2731 (org-element-map tree 'table-row
2732 (lambda (row) (org-export-table-row-number row info)) info))))
2733 ;; Number ignores separators.
2734 (should
2735 (equal '(0 1)
2736 (org-test-with-parsed-data "
2737 | a | b | c |
2738 |---+---+---|
2739 | d | e | f |"
2740 (org-element-map tree 'table-row
2741 (lambda (row) (org-export-table-row-number row info)) info))))
2742 ;; Number ignores special rows.
2743 (should
2744 (equal '(0 1)
2745 (org-test-with-parsed-data "
2746 | / | < | > |
2747 | | b | c |
2748 |---+-----+-----|
2749 | | <c> | <c> |
2750 | | e | f |"
2751 (org-element-map tree 'table-row
2752 (lambda (row) (org-export-table-row-number row info)) info)))))
2754 (ert-deftest test-org-export/table-cell-width ()
2755 "Test `org-export-table-cell-width' specifications."
2756 ;; 1. Width is primarily determined by width cookies. If no cookie
2757 ;; is found, cell's width is nil.
2758 (org-test-with-parsed-data "
2759 | / | <l> | <6> | <l7> |
2760 | | a | b | c |"
2761 (should
2762 (equal
2763 '(nil 6 7)
2764 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2765 (org-element-map tree 'table-cell 'identity info)))))
2766 ;; 2. The last width cookie has precedence.
2767 (org-test-with-parsed-data "
2768 | <6> |
2769 | <7> |
2770 | a |"
2771 (should
2772 (equal
2773 '(7)
2774 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2775 (org-element-map tree 'table-cell 'identity info)))))
2776 ;; 3. Valid width cookies must have a specific row.
2777 (org-test-with-parsed-data "| <6> | cell |"
2778 (should
2779 (equal
2780 '(nil nil)
2781 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2782 (org-element-map tree 'table-cell 'identity))))))
2784 (ert-deftest test-org-export/table-cell-alignment ()
2785 "Test `org-export-table-cell-alignment' specifications."
2786 ;; 1. Alignment is primarily determined by alignment cookies.
2787 (should
2788 (equal '(left center right)
2789 (let ((org-table-number-fraction 0.5)
2790 (org-table-number-regexp "^[0-9]+$"))
2791 (org-test-with-parsed-data "| <l> | <c> | <r> |"
2792 (mapcar (lambda (cell)
2793 (org-export-table-cell-alignment cell info))
2794 (org-element-map tree 'table-cell 'identity))))))
2795 ;; 2. The last alignment cookie has precedence.
2796 (should
2797 (equal '(right right right)
2798 (org-test-with-parsed-data "
2799 | <l8> |
2800 | cell |
2801 | <r9> |"
2802 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
2803 (org-element-map tree 'table-cell 'identity)))))
2804 ;; 3. If there's no cookie, cell's contents determine alignment.
2805 ;; A column mostly made of cells containing numbers will align
2806 ;; its cells to the right.
2807 (should
2808 (equal '(right right right)
2809 (let ((org-table-number-fraction 0.5)
2810 (org-table-number-regexp "^[0-9]+$"))
2811 (org-test-with-parsed-data "
2812 | 123 |
2813 | some text |
2814 | 12345 |"
2815 (mapcar (lambda (cell)
2816 (org-export-table-cell-alignment cell info))
2817 (org-element-map tree 'table-cell 'identity))))))
2818 ;; 4. Otherwise, they will be aligned to the left.
2819 (should
2820 (equal '(left left left)
2821 (org-test-with-parsed-data "
2822 | text |
2823 | some text |
2824 | \alpha |"
2825 (mapcar (lambda (cell)
2826 (org-export-table-cell-alignment cell info))
2827 (org-element-map tree 'table-cell 'identity info))))))
2829 (ert-deftest test-org-export/table-cell-borders ()
2830 "Test `org-export-table-cell-borders' specifications."
2831 ;; 1. Recognize various column groups indicators.
2832 (org-test-with-parsed-data "| / | < | > | <> |"
2833 (should
2834 (equal
2835 '((right bottom top) (left bottom top) (right bottom top)
2836 (right left bottom top))
2837 (mapcar (lambda (cell)
2838 (org-export-table-cell-borders cell info))
2839 (org-element-map tree 'table-cell 'identity)))))
2840 ;; 2. Accept shortcuts to define column groups.
2841 (org-test-with-parsed-data "| / | < | < |"
2842 (should
2843 (equal
2844 '((right bottom top) (right left bottom top) (left bottom top))
2845 (mapcar (lambda (cell)
2846 (org-export-table-cell-borders cell info))
2847 (org-element-map tree 'table-cell 'identity)))))
2848 ;; 3. A valid column groups row must start with a "/".
2849 (org-test-with-parsed-data "
2850 | | < |
2851 | a | b |"
2852 (should
2853 (equal '((top) (top) (bottom) (bottom))
2854 (mapcar (lambda (cell)
2855 (org-export-table-cell-borders cell info))
2856 (org-element-map tree 'table-cell 'identity)))))
2857 ;; 4. Take table rules into consideration.
2858 (org-test-with-parsed-data "
2859 | 1 |
2860 |---|
2861 | 2 |"
2862 (should
2863 (equal '((below top) (bottom above))
2864 (mapcar (lambda (cell)
2865 (org-export-table-cell-borders cell info))
2866 (org-element-map tree 'table-cell 'identity)))))
2867 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
2868 ;; (resp. `bottom' and `below') borders. Any special row is
2869 ;; ignored.
2870 (org-test-with-parsed-data "
2871 |---+----|
2872 | / | |
2873 | | 1 |
2874 |---+----|"
2875 (should
2876 (equal '((bottom below top above))
2877 (last
2878 (mapcar (lambda (cell)
2879 (org-export-table-cell-borders cell info))
2880 (org-element-map tree 'table-cell 'identity)))))))
2882 (ert-deftest test-org-export/table-dimensions ()
2883 "Test `org-export-table-dimensions' specifications."
2884 ;; 1. Standard test.
2885 (org-test-with-parsed-data "
2886 | 1 | 2 | 3 |
2887 | 4 | 5 | 6 |"
2888 (should
2889 (equal '(2 . 3)
2890 (org-export-table-dimensions
2891 (org-element-map tree 'table 'identity info 'first-match) info))))
2892 ;; 2. Ignore horizontal rules and special columns.
2893 (org-test-with-parsed-data "
2894 | / | < | > |
2895 | 1 | 2 | 3 |
2896 |---+---+---|
2897 | 4 | 5 | 6 |"
2898 (should
2899 (equal '(2 . 3)
2900 (org-export-table-dimensions
2901 (org-element-map tree 'table 'identity info 'first-match) info)))))
2903 (ert-deftest test-org-export/table-cell-address ()
2904 "Test `org-export-table-cell-address' specifications."
2905 ;; 1. Standard test: index is 0-based.
2906 (org-test-with-parsed-data "| a | b |"
2907 (should
2908 (equal '((0 . 0) (0 . 1))
2909 (org-element-map tree 'table-cell
2910 (lambda (cell) (org-export-table-cell-address cell info))
2911 info))))
2912 ;; 2. Special column isn't counted, nor are special rows.
2913 (org-test-with-parsed-data "
2914 | / | <> |
2915 | | c |"
2916 (should
2917 (equal '(0 . 0)
2918 (org-export-table-cell-address
2919 (car (last (org-element-map tree 'table-cell 'identity info)))
2920 info))))
2921 ;; 3. Tables rules do not count either.
2922 (org-test-with-parsed-data "
2923 | a |
2924 |---|
2925 | b |
2926 |---|
2927 | c |"
2928 (should
2929 (equal '(2 . 0)
2930 (org-export-table-cell-address
2931 (car (last (org-element-map tree 'table-cell 'identity info)))
2932 info))))
2933 ;; 4. Return nil for special cells.
2934 (org-test-with-parsed-data "| / | a |"
2935 (should-not
2936 (org-export-table-cell-address
2937 (org-element-map tree 'table-cell 'identity nil 'first-match)
2938 info))))
2940 (ert-deftest test-org-export/get-table-cell-at ()
2941 "Test `org-export-get-table-cell-at' specifications."
2942 ;; 1. Address ignores special columns, special rows and rules.
2943 (org-test-with-parsed-data "
2944 | / | <> |
2945 | | a |
2946 |---+----|
2947 | | b |"
2948 (should
2949 (equal '("b")
2950 (org-element-contents
2951 (org-export-get-table-cell-at
2952 '(1 . 0)
2953 (org-element-map tree 'table 'identity info 'first-match)
2954 info)))))
2955 ;; 2. Return value for a non-existent address is nil.
2956 (org-test-with-parsed-data "| a |"
2957 (should-not
2958 (org-export-get-table-cell-at
2959 '(2 . 2)
2960 (org-element-map tree 'table 'identity info 'first-match)
2961 info)))
2962 (org-test-with-parsed-data "| / |"
2963 (should-not
2964 (org-export-get-table-cell-at
2965 '(0 . 0)
2966 (org-element-map tree 'table 'identity info 'first-match)
2967 info))))
2969 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
2970 "Test `org-export-table-cell-starts-colgroup-p' specifications."
2971 ;; 1. A cell at a beginning of a row always starts a column group.
2972 (org-test-with-parsed-data "| a |"
2973 (should
2974 (org-export-table-cell-starts-colgroup-p
2975 (org-element-map tree 'table-cell 'identity info 'first-match)
2976 info)))
2977 ;; 2. Special column should be ignored when determining the
2978 ;; beginning of the row.
2979 (org-test-with-parsed-data "
2980 | / | |
2981 | | a |"
2982 (should
2983 (org-export-table-cell-starts-colgroup-p
2984 (org-element-map tree 'table-cell 'identity info 'first-match)
2985 info)))
2986 ;; 2. Explicit column groups.
2987 (org-test-with-parsed-data "
2988 | / | | < |
2989 | a | b | c |"
2990 (should
2991 (equal
2992 '(yes no yes)
2993 (org-element-map tree 'table-cell
2994 (lambda (cell)
2995 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
2996 info)))))
2998 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
2999 "Test `org-export-table-cell-ends-colgroup-p' specifications."
3000 ;; 1. A cell at the end of a row always ends a column group.
3001 (org-test-with-parsed-data "| a |"
3002 (should
3003 (org-export-table-cell-ends-colgroup-p
3004 (org-element-map tree 'table-cell 'identity info 'first-match)
3005 info)))
3006 ;; 2. Special column should be ignored when determining the
3007 ;; beginning of the row.
3008 (org-test-with-parsed-data "
3009 | / | |
3010 | | a |"
3011 (should
3012 (org-export-table-cell-ends-colgroup-p
3013 (org-element-map tree 'table-cell 'identity info 'first-match)
3014 info)))
3015 ;; 3. Explicit column groups.
3016 (org-test-with-parsed-data "
3017 | / | < | |
3018 | a | b | c |"
3019 (should
3020 (equal
3021 '(yes no yes)
3022 (org-element-map tree 'table-cell
3023 (lambda (cell)
3024 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
3025 info)))))
3027 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
3028 "Test `org-export-table-row-starts-rowgroup-p' specifications."
3029 ;; 1. A row at the beginning of a table always starts a row group.
3030 ;; So does a row following a table rule.
3031 (org-test-with-parsed-data "
3032 | a |
3033 |---|
3034 | b |"
3035 (should
3036 (equal
3037 '(yes no yes)
3038 (org-element-map tree 'table-row
3039 (lambda (row)
3040 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
3041 info))))
3042 ;; 2. Special rows should be ignored when determining the beginning
3043 ;; of the row.
3044 (org-test-with-parsed-data "
3045 | / | < |
3046 | | a |
3047 |---+---|
3048 | / | < |
3049 | | b |"
3050 (should
3051 (equal
3052 '(yes no yes)
3053 (org-element-map tree 'table-row
3054 (lambda (row)
3055 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
3056 info)))))
3058 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
3059 "Test `org-export-table-row-ends-rowgroup-p' specifications."
3060 ;; 1. A row at the end of a table always ends a row group. So does
3061 ;; a row preceding a table rule.
3062 (org-test-with-parsed-data "
3063 | a |
3064 |---|
3065 | b |"
3066 (should
3067 (equal
3068 '(yes no yes)
3069 (org-element-map tree 'table-row
3070 (lambda (row)
3071 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
3072 info))))
3073 ;; 2. Special rows should be ignored when determining the beginning
3074 ;; of the row.
3075 (org-test-with-parsed-data "
3076 | | a |
3077 | / | < |
3078 |---+---|
3079 | | b |
3080 | / | < |"
3081 (should
3082 (equal
3083 '(yes no yes)
3084 (org-element-map tree 'table-row
3085 (lambda (row)
3086 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
3087 info)))))
3089 (ert-deftest test-org-export/table-row-in-header-p ()
3090 "Test `org-export-table-row-in-header-p' specifications."
3091 ;; Standard test. Separators are always nil.
3092 (should
3093 (equal
3094 '(yes no no)
3095 (org-test-with-parsed-data "| a |\n|---|\n| b |"
3096 (org-element-map tree 'table-row
3097 (lambda (row)
3098 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
3099 ;; Nil when there is no header.
3100 (should
3101 (equal
3102 '(no no)
3103 (org-test-with-parsed-data "| a |\n| b |"
3104 (org-element-map tree 'table-row
3105 (lambda (row)
3106 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
3108 (ert-deftest test-org-export/table-row-starts-header-p ()
3109 "Test `org-export-table-row-starts-header-p' specifications."
3110 ;; 1. Only the row starting the first row group starts the table
3111 ;; header.
3112 (org-test-with-parsed-data "
3113 | a |
3114 | b |
3115 |---|
3116 | c |"
3117 (should
3118 (equal
3119 '(yes no no no)
3120 (org-element-map tree 'table-row
3121 (lambda (row)
3122 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
3123 info))))
3124 ;; 2. A row cannot start an header if there's no header in the
3125 ;; table.
3126 (org-test-with-parsed-data "
3127 | a |
3128 |---|"
3129 (should-not
3130 (org-export-table-row-starts-header-p
3131 (org-element-map tree 'table-row 'identity info 'first-match)
3132 info))))
3134 (ert-deftest test-org-export/table-row-ends-header-p ()
3135 "Test `org-export-table-row-ends-header-p' specifications."
3136 ;; 1. Only the row starting the first row group starts the table
3137 ;; header.
3138 (org-test-with-parsed-data "
3139 | a |
3140 | b |
3141 |---|
3142 | c |"
3143 (should
3144 (equal
3145 '(no yes no no)
3146 (org-element-map tree 'table-row
3147 (lambda (row)
3148 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
3149 info))))
3150 ;; 2. A row cannot start an header if there's no header in the
3151 ;; table.
3152 (org-test-with-parsed-data "
3153 | a |
3154 |---|"
3155 (should-not
3156 (org-export-table-row-ends-header-p
3157 (org-element-map tree 'table-row 'identity info 'first-match)
3158 info))))
3162 ;;; Tables of Contents
3164 (ert-deftest test-org-export/collect-headlines ()
3165 "Test `org-export-collect-headlines' specifications."
3166 ;; Standard test.
3167 (should
3168 (= 2
3169 (length
3170 (org-test-with-parsed-data "* H1\n** H2"
3171 (org-export-collect-headlines info)))))
3172 ;; Do not collect headlines below optional argument.
3173 (should
3174 (= 1
3175 (length
3176 (org-test-with-parsed-data "* H1\n** H2"
3177 (org-export-collect-headlines info 1)))))
3178 ;; Never collect headlines below maximum headline level.
3179 (should
3180 (= 1
3181 (length
3182 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3183 (org-export-collect-headlines info)))))
3184 (should
3185 (= 1
3186 (length
3187 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3188 (org-export-collect-headlines info 2))))))
3192 ;;; Templates
3194 (ert-deftest test-org-export/inner-template ()
3195 "Test `inner-template' translator specifications."
3196 (should
3197 (equal "Success!"
3198 (org-test-with-temp-text "* Headline"
3199 (org-export-as
3200 (org-export-create-backend
3201 :transcoders
3202 '((inner-template . (lambda (contents info) "Success!"))
3203 (headline . (lambda (h c i) "Headline"))))))))
3204 ;; Inner template is applied even in a "body-only" export.
3205 (should
3206 (equal "Success!"
3207 (org-test-with-temp-text "* Headline"
3208 (org-export-as
3209 (org-export-create-backend
3210 :transcoders '((inner-template . (lambda (c i) "Success!"))
3211 (headline . (lambda (h c i) "Headline"))))
3212 nil nil 'body-only)))))
3214 (ert-deftest test-org-export/template ()
3215 "Test `template' translator specifications."
3216 (should
3217 (equal "Success!"
3218 (org-test-with-temp-text "* Headline"
3219 (org-export-as
3220 (org-export-create-backend
3221 :transcoders '((template . (lambda (contents info) "Success!"))
3222 (headline . (lambda (h c i) "Headline"))))))))
3223 ;; Template is not applied in a "body-only" export.
3224 (should-not
3225 (equal "Success!"
3226 (org-test-with-temp-text "* Headline"
3227 (org-export-as
3228 (org-export-create-backend
3229 :transcoders '((template . (lambda (contents info) "Success!"))
3230 (headline . (lambda (h c i) "Headline"))))
3231 nil nil 'body-only)))))
3235 ;;; Topology
3237 (ert-deftest test-org-export/get-next-element ()
3238 "Test `org-export-get-next-element' specifications."
3239 ;; Standard test.
3240 (should
3241 (equal "b"
3242 (org-test-with-parsed-data "* Headline\n*a* b"
3243 (org-export-get-next-element
3244 (org-element-map tree 'bold 'identity info t) info))))
3245 ;; Return nil when no previous element.
3246 (should-not
3247 (org-test-with-parsed-data "* Headline\na *b*"
3248 (org-export-get-next-element
3249 (org-element-map tree 'bold 'identity info t) info)))
3250 ;; Non-exportable elements are ignored.
3251 (should-not
3252 (let ((org-export-with-timestamps nil))
3253 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
3254 (org-export-get-next-element
3255 (org-element-map tree 'entity 'identity info t) info))))
3256 ;; Find next element in secondary strings.
3257 (should
3258 (eq 'verbatim
3259 (org-test-with-parsed-data "* a =verb="
3260 (org-element-type
3261 (org-export-get-next-element
3262 (org-element-map tree 'plain-text 'identity info t) info)))))
3263 (should
3264 (eq 'verbatim
3265 (org-test-with-parsed-data "* /italic/ =verb="
3266 (org-element-type
3267 (org-export-get-next-element
3268 (org-element-map tree 'italic 'identity info t) info)))))
3269 ;; Find next element in document keywords.
3270 (should
3271 (eq 'verbatim
3272 (org-test-with-parsed-data "#+TITLE: a =verb="
3273 (org-element-type
3274 (org-export-get-next-element
3275 (org-element-map
3276 (plist-get info :title) 'plain-text 'identity info t) info)))))
3277 ;; Find next element in parsed affiliated keywords.
3278 (should
3279 (eq 'verbatim
3280 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
3281 (org-element-type
3282 (org-export-get-next-element
3283 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3284 ;; With optional argument N, return a list containing all the
3285 ;; following elements.
3286 (should
3287 (equal
3288 '(bold code underline)
3289 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3290 (mapcar 'car
3291 (org-export-get-next-element
3292 (org-element-map tree 'italic 'identity info t) info t)))))
3293 ;; When N is a positive integer, return a list containing up to
3294 ;; N following elements.
3295 (should
3296 (equal
3297 '(bold code)
3298 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3299 (mapcar 'car
3300 (org-export-get-next-element
3301 (org-element-map tree 'italic 'identity info t) info 2))))))
3303 (ert-deftest test-org-export/get-previous-element ()
3304 "Test `org-export-get-previous-element' specifications."
3305 ;; Standard test.
3306 (should
3307 (equal "a "
3308 (org-test-with-parsed-data "* Headline\na *b*"
3309 (org-export-get-previous-element
3310 (org-element-map tree 'bold 'identity info t) info))))
3311 ;; Return nil when no previous element.
3312 (should-not
3313 (org-test-with-parsed-data "* Headline\n*a* b"
3314 (org-export-get-previous-element
3315 (org-element-map tree 'bold 'identity info t) info)))
3316 ;; Non-exportable elements are ignored.
3317 (should-not
3318 (let ((org-export-with-timestamps nil))
3319 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
3320 (org-export-get-previous-element
3321 (org-element-map tree 'entity 'identity info t) info))))
3322 ;; Find previous element in secondary strings.
3323 (should
3324 (eq 'verbatim
3325 (org-test-with-parsed-data "* =verb= a"
3326 (org-element-type
3327 (org-export-get-previous-element
3328 (org-element-map tree 'plain-text 'identity info t) info)))))
3329 (should
3330 (eq 'verbatim
3331 (org-test-with-parsed-data "* =verb= /italic/"
3332 (org-element-type
3333 (org-export-get-previous-element
3334 (org-element-map tree 'italic 'identity info t) info)))))
3335 ;; Find previous element in document keywords.
3336 (should
3337 (eq 'verbatim
3338 (org-test-with-parsed-data "#+TITLE: =verb= a"
3339 (org-element-type
3340 (org-export-get-previous-element
3341 (org-element-map
3342 (plist-get info :title) 'plain-text 'identity info t) info)))))
3343 ;; Find previous element in parsed affiliated keywords.
3344 (should
3345 (eq 'verbatim
3346 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
3347 (org-element-type
3348 (org-export-get-previous-element
3349 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3350 ;; With optional argument N, return a list containing up to
3351 ;; N previous elements.
3352 (should
3353 (equal '(underline italic bold)
3354 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3355 (mapcar 'car
3356 (org-export-get-previous-element
3357 (org-element-map tree 'code 'identity info t) info t)))))
3358 ;; When N is a positive integer, return a list containing up to
3359 ;; N previous elements.
3360 (should
3361 (equal '(italic bold)
3362 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3363 (mapcar 'car
3364 (org-export-get-previous-element
3365 (org-element-map tree 'code 'identity info t) info 2))))))
3368 (provide 'test-ox)
3369 ;;; test-org-export.el end here