1 ;;; test-org-export.el --- Tests for org-export.el
3 ;; Copyright (C) 2012 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
7 ;; Released under the GNU General Public License version 3
8 ;; see: http://www.gnu.org/licenses/gpl-3.0.html
16 (unless (featurep 'org-export
)
17 (signal 'missing-test-dependency
"org-export"))
19 (defmacro org-test-with-backend
(backend &rest body
)
20 "Execute body with an export back-end defined.
22 BACKEND is the name of the back-end. BODY is the body to
23 execute. The defined back-end simply returns parsed data as Org
25 (declare (debug (form body
)) (indent 1))
26 `(let ((,(intern (format "org-%s-translate-alist" backend
))
27 ',(let (transcode-table)
28 (dolist (type (append org-element-all-elements
29 org-element-all-objects
)
31 (push (cons type
(intern (format "org-%s-%s" backend type
)))
33 (flet ,(let (transcoders)
34 (dolist (type (append org-element-all-elements
35 org-element-all-objects
)
37 (push `(,(intern (format "org-%s-%s" backend type
))
39 (,(intern (format "org-element-%s-interpreter" type
))
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))))
66 (ert-deftest test-org-export
/parse-option-keyword
()
67 "Test reading all standard #+OPTIONS: items."
70 (org-export-parse-option-keyword
71 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
72 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t")
74 1 :preserve-breaks t
:section-numbers t
:time-stamp-file t
75 :with-archived-trees t
:with-author t
:with-creator t
:with-drawers t
76 :with-email t
:with-emphasize t
:with-entities t
:with-fixed-width t
77 :with-footnotes t
:with-priority t
:with-special-strings t
78 :with-sub-superscript t
:with-toc t
:with-tables t
:with-tags t
79 :with-tasks t
:with-timestamps t
:with-todo-keywords t
)))
80 ;; Test some special values.
83 (org-export-parse-option-keyword
84 "arch:headline creator:comment d:(\"TEST\")
85 ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
88 :with-archived-trees headline
:with-creator comment
89 :with-drawers
("TEST") :with-sub-superscript
{} :with-toc
1
90 :with-tags not-in-toc
:with-tasks todo
:with-timestamps active
))))
92 (ert-deftest test-org-export
/get-inbuffer-options
()
93 "Test reading all standard export keywords."
96 (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
99 #+DESCRIPTION: Testing
100 #+DESCRIPTION: with two lines
101 #+EMAIL: some@email.org
102 #+EXPORT_EXCLUDE_TAGS: noexport invisible
105 #+EXPORT_SELECT_TAGS: export
107 #+TITLE: with spaces"
108 (org-export-get-inbuffer-options))
110 ("Me, Myself and I") :creator
"Idem" :date
("Today")
111 :description
"Testing\nwith two lines" :email
"some@email.org"
112 :exclude-tags
("noexport" "invisible") :keywords
"test" :language
"en"
113 :select-tags
("export") :title
("Some title with spaces")))))
115 (ert-deftest test-org-export
/handle-options
()
116 "Test if export options have an impact on output."
117 ;; Test exclude tags.
118 (org-test-with-temp-text "* Head1 :noexport:"
119 (org-test-with-backend test
121 (equal (org-export-as 'test nil nil nil
'(:exclude-tags
("noexport")))
123 ;; Test include tags.
124 (org-test-with-temp-text "
127 ** Sub-Head2.1 :export:
130 (org-test-with-backend test
133 "* Head2\n** Sub-Head2.1 :export:\n*** Sub-Head2.1.1\n"
134 (let ((org-tags-column 0))
135 (org-export-as 'test nil nil nil
'(:select-tags
("export"))))))))
136 ;; Test mixing include tags and exclude tags.
137 (org-test-with-temp-text "
139 ** Sub-Head1 :noexport:
142 ** Sub-Head1 :export:"
143 (org-test-with-backend test
146 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
149 '(:select-tags
("export") :exclude-tags
("noexport")))))))
151 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
152 (org-test-with-temp-text "* TODO Head1"
153 (org-test-with-backend test
154 (should (equal (org-export-as 'test nil nil nil
'(:with-tasks nil
))
156 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
157 (org-test-with-temp-text "* TODO Head1"
158 (org-test-with-backend test
159 (should (equal (org-export-as 'test nil nil nil
'(:with-tasks t
))
160 "* TODO Head1\n")))))
162 (org-test-with-temp-text "* Head1 :archive:"
163 (let ((org-archive-tag "archive"))
164 (org-test-with-backend test
166 (equal (org-export-as 'test nil nil nil
'(:with-archived-trees nil
))
168 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
169 (let ((org-archive-tag "archive"))
170 (org-test-with-backend test
173 "\\* Head1[ \t]+:archive:"
174 (org-export-as 'test nil nil nil
175 '(:with-archived-trees headline
)))))))
176 (org-test-with-temp-text "* Head1 :archive:"
177 (let ((org-archive-tag "archive"))
178 (org-test-with-backend test
181 "\\`\\* Head1[ \t]+:archive:\n\\'"
182 (org-export-as 'test nil nil nil
'(:with-archived-trees t
)))))))
184 (let ((org-drawers '("TEST")))
185 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
186 (org-test-with-backend test
187 (should (equal (org-export-as 'test nil nil nil
'(:with-drawers nil
))
189 (should (equal (org-export-as 'test nil nil nil
'(:with-drawers t
))
190 ":TEST:\ncontents\n:END:\n")))))
191 (let ((org-drawers '("FOO" "BAR")))
192 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
193 (org-test-with-backend test
195 (equal (org-export-as 'test nil nil nil
'(:with-drawers
("FOO")))
196 ":FOO:\nkeep\n:END:\n")))))
198 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
199 (org-test-with-backend test
201 (equal (org-export-as 'test nil nil nil
'(:with-timestamps t
))
202 "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
204 (equal (org-export-as 'test nil nil nil
'(:with-timestamps nil
)) ""))
206 (equal (org-export-as 'test nil nil nil
'(:with-timestamps active
))
207 "<2012-04-29 sun. 10:45>\n"))
209 (equal (org-export-as 'test nil nil nil
'(:with-timestamps inactive
))
210 "[2012-04-29 sun. 10:45]\n"))))
212 (let ((org-clock-string "CLOCK:"))
213 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
214 (org-test-with-backend test
216 (equal (org-export-as 'test nil nil nil
'(:with-clocks t
))
217 "CLOCK: [2012-04-29 sun. 10:45]\n"))
219 (equal (org-export-as 'test nil nil nil
'(:with-clocks nil
)) "")))))
221 (let ((org-closed-string "CLOSED:"))
222 (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
223 (org-test-with-backend test
225 (equal (org-export-as 'test nil nil nil
'(:with-plannings t
))
226 "CLOSED: [2012-04-29 sun. 10:45]\n"))
228 (equal (org-export-as 'test nil nil nil
'(:with-plannings nil
))
231 (ert-deftest test-org-export
/comment-tree
()
232 "Test if export process ignores commented trees."
233 (let ((org-comment-string "COMMENT"))
234 (org-test-with-temp-text "* COMMENT Head1"
235 (org-test-with-backend test
236 (should (equal (org-export-as 'test
) ""))))))
238 (ert-deftest test-org-export
/export-scope
()
239 "Test all export scopes."
240 (org-test-with-temp-text "
245 (org-test-with-backend test
248 (should (equal (org-export-as 'test
'subtree
) "text\n*** Head3\n"))
250 (goto-char (point-min))
253 (should (equal (org-export-as 'test nil
'visible
) "* Head1\n"))
255 (flet ((org-test-template (body info
) (format "BEGIN\n%sEND" body
)))
256 (push '(template . org-test-template
) org-test-translate-alist
)
257 (should (equal (org-export-as 'test nil nil
'body-only
)
258 "* Head1\n** Head2\ntext\n*** Head3\n"))
259 (should (equal (org-export-as 'test
)
260 "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
262 (goto-char (point-min))
264 (transient-mark-mode 1)
265 (push-mark (point) t t
)
266 (goto-char (point-at-eol))
267 (should (equal (org-export-as 'test
) "text\n"))))
268 ;; Subtree with a code block calling another block outside.
269 (org-test-with-temp-text "
271 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
276 #+BEGIN_SRC emacs-lisp
279 (org-test-with-backend test
281 (should (equal (org-export-as 'test
'subtree
) ": 3\n"))))
282 ;; Subtree's EXPORT_TITLE property.
283 (org-test-with-backend test
284 (flet ((org-test-template (body info
)
285 (org-export-data (plist-get info
:title
) info
)))
286 (push '(template . org-test-template
) org-test-translate-alist
)
287 (org-test-with-temp-text "
290 :EXPORT_TITLE: subtree-title
294 (should (equal "subtree-title" (org-export-as 'test
'subtree
)))))))
296 (ert-deftest test-org-export
/export-snippet
()
297 "Test export snippets transcoding."
298 (org-test-with-temp-text "@@test:A@@@@t:B@@"
299 (org-test-with-backend test
300 (flet ((org-test-export-snippet
301 (snippet contents info
)
302 (when (eq (org-export-snippet-backend snippet
) 'test
)
303 (org-element-property :value snippet
))))
304 (let ((org-export-snippet-translation-alist nil
))
305 (should (equal (org-export-as 'test
) "A\n")))
306 (let ((org-export-snippet-translation-alist '(("t" .
"test"))))
307 (should (equal (org-export-as 'test
) "AB\n")))))))
309 (ert-deftest test-org-export
/expand-include
()
310 "Test file inclusion in an Org buffer."
311 ;; Full insertion with recursive inclusion.
312 (org-test-with-temp-text
313 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir
)
314 (org-export-expand-include-keyword)
315 (should (equal (buffer-string)
316 "Small Org file with an include keyword.
318 #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
324 ;; Localized insertion.
325 (org-test-with-temp-text
326 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
328 (org-export-expand-include-keyword)
329 (should (equal (buffer-string)
330 "Small Org file with an include keyword.\n")))
331 ;; Insertion with constraints on headlines level.
332 (org-test-with-temp-text
334 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
336 (org-export-expand-include-keyword)
337 (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
338 ;; Inclusion within an example block.
339 (org-test-with-temp-text
340 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
342 (org-export-expand-include-keyword)
346 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
347 ;; Inclusion within a src-block.
348 (org-test-with-temp-text
350 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
352 (org-export-expand-include-keyword)
353 (should (equal (buffer-string)
354 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
356 (ert-deftest test-org-export
/user-ignore-list
()
357 "Test if `:ignore-list' accepts user input."
358 (org-test-with-backend test
359 (flet ((skip-note-head
361 ;; Ignore headlines with the word "note" in their title.
365 (when (string-match "\\<note\\>"
366 (org-element-property :raw-value headline
))
367 (org-export-ignore-element headline info
)))
370 ;; Install function in parse tree filters.
371 (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
372 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
373 (should (equal (org-export-as 'test
) "* Head1\n")))))))
375 (ert-deftest test-org-export
/before-parsing-hook
()
376 "Test `org-export-before-parsing-hook'."
377 (org-test-with-backend test
378 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
379 (let ((org-export-before-parsing-hook
383 (delete-region (point) (progn (forward-line) (point)))))))))
384 (should (equal (org-export-as 'test
) "Body 1\nBody 2\n"))))))
386 (ert-deftest test-org-export
/set-element
()
387 "Test `org-export-set-element' specifications."
388 (org-test-with-parsed-data "* Headline\n*a*"
389 (org-export-set-element
390 (org-element-map tree
'bold
'identity nil t
)
392 ;; Check if object is correctly replaced.
393 (should (org-element-map tree
'italic
'identity
))
394 (should-not (org-element-map tree
'bold
'identity
))
395 ;; Check if new object's parent is correctly set.
398 (org-element-property :parent
399 (org-element-map tree
'italic
'identity nil t
))
400 (org-element-map tree
'paragraph
'identity nil t
)))))
404 ;;; Affiliated Keywords
406 (ert-deftest test-org-export
/read-attribute
()
407 "Test `org-export-read-attribute' specifications."
411 (org-export-read-attribute
413 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
414 (org-element-current-element)))
416 ;; Return nil on empty attribute.
418 (org-export-read-attribute
420 (org-test-with-temp-text "Paragraph" (org-element-current-element)))))
425 (ert-deftest test-org-export
/footnotes
()
426 "Test footnotes specifications."
427 (let ((org-footnote-section nil
)
428 (org-export-with-footnotes t
))
429 ;; 1. Read every type of footnote.
430 (org-test-with-parsed-data
431 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
434 '((1 .
"A") (2 .
"B") (3 .
"C") (4 .
"D"))
436 tree
'footnote-reference
438 (let ((def (org-export-get-footnote-definition ref info
)))
439 (cons (org-export-get-footnote-number ref info
)
440 (if (eq (org-element-property :type ref
) 'inline
) (car def
)
441 (car (org-element-contents
442 (car (org-element-contents def
))))))))
444 ;; 2. Test nested footnotes order.
445 (org-test-with-parsed-data
446 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
449 '((1 .
"fn:1") (2 .
"fn:2") (3 .
"fn:3") (4))
451 tree
'footnote-reference
453 (when (org-export-footnote-first-reference-p ref info
)
454 (cons (org-export-get-footnote-number ref info
)
455 (org-element-property :label ref
))))
457 ;; 3. Test nested footnote in invisible definitions.
458 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
460 (narrow-to-region (point) (point-at-eol))
461 (let* ((tree (org-element-parse-buffer))
462 (info (org-combine-plists
464 (org-export-collect-tree-properties
465 tree
(org-export-get-environment)))))
466 ;; Both footnotes should be seen.
468 (= (length (org-export-collect-footnote-definitions tree info
)) 2))))
469 ;; 4. Test footnotes definitions collection.
470 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
472 \[fn:2] B [fn:3] [fn::D].
475 (should (= (length (org-export-collect-footnote-definitions tree info
))
477 ;; 5. Test export of footnotes defined outside parsing scope.
478 (org-test-with-temp-text "[fn:1] Out of scope
481 (org-test-with-backend test
482 (flet ((org-test-footnote-reference
483 (fn-ref contents info
)
484 (org-element-interpret-data
485 (org-export-get-footnote-definition fn-ref info
))))
487 (should (equal "ParagraphOut of scope\n"
488 (org-export-as 'test
'subtree
))))))))
492 ;;; Headlines and Inlinetasks
494 (ert-deftest test-org-export
/get-tags
()
495 "Test `org-export-get-tags' specifications."
496 (let ((org-export-exclude-tags '("noexport"))
497 (org-export-select-tags '("export")))
498 ;; Standard test: tags which are not a select tag, an exclude tag,
499 ;; or specified as optional argument shouldn't be ignored.
501 (org-test-with-parsed-data "* Headline :tag:"
502 (org-export-get-tags (org-element-map tree
'headline
'identity info t
)
504 ;; Exclude tags are removed.
506 (org-test-with-parsed-data "* Headline :noexport:"
507 (org-export-get-tags (org-element-map tree
'headline
'identity info t
)
509 ;; Select tags are removed.
511 (org-test-with-parsed-data "* Headline :export:"
512 (org-export-get-tags (org-element-map tree
'headline
'identity info t
)
517 (org-test-with-parsed-data "* Headline :tag:export:"
518 (org-export-get-tags (org-element-map tree
'headline
'identity info t
)
520 ;; Tags provided in the optional argument are also ignored.
522 (org-test-with-parsed-data "* Headline :ignore:"
523 (org-export-get-tags (org-element-map tree
'headline
'identity info t
)
524 info
'("ignore"))))))
530 (ert-deftest test-org-export
/fuzzy-link
()
531 "Test fuzzy links specifications."
532 ;; 1. Links to invisible (keyword) targets should be ignored.
533 (org-test-with-parsed-data
534 "Paragraph.\n#+TARGET: Test\n[[Test]]"
539 (org-export-get-ordinal
540 (org-export-resolve-fuzzy-link link info
) info
)) info
)))
541 ;; 2. Link to an headline should return headline's number.
542 (org-test-with-parsed-data
543 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
545 ;; Note: Headline's number is in fact a list of numbers.
550 (org-export-get-ordinal
551 (org-export-resolve-fuzzy-link link info
) info
)) info t
))))
552 ;; 3. Link to a target in an item should return item's number.
553 (org-test-with-parsed-data
554 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
556 ;; Note: Item's number is in fact a list of numbers.
561 (org-export-get-ordinal
562 (org-export-resolve-fuzzy-link link info
) info
)) info t
))))
563 ;; 4. Link to a target in a footnote should return footnote's
565 (org-test-with-parsed-data "
566 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
572 (org-export-get-ordinal
573 (org-export-resolve-fuzzy-link link info
) info
)) info
))))
574 ;; 5. Link to a named element should return sequence number of that
576 (org-test-with-parsed-data
577 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
583 (org-export-get-ordinal
584 (org-export-resolve-fuzzy-link link info
) info
)) info t
))))
585 ;; 6. Link to a target not within an item, a table, a footnote
586 ;; reference or definition should return section number.
587 (org-test-with-parsed-data
588 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
594 (org-export-get-ordinal
595 (org-export-resolve-fuzzy-link link info
) info
)) info t
)))))
597 (ert-deftest test-org-export
/resolve-coderef
()
598 "Test `org-export-resolve-coderef' specifications."
599 (let ((org-coderef-label-format "(ref:%s)"))
600 ;; 1. A link to a "-n -k -r" block returns line number.
601 (org-test-with-parsed-data
602 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
603 (should (= (org-export-resolve-coderef "coderef" info
) 1)))
604 (org-test-with-parsed-data
605 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
606 (should (= (org-export-resolve-coderef "coderef" info
) 1)))
607 ;; 2. A link to a "-n -r" block returns line number.
608 (org-test-with-parsed-data
609 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
610 (should (= (org-export-resolve-coderef "coderef" info
) 1)))
611 (org-test-with-parsed-data
612 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
613 (should (= (org-export-resolve-coderef "coderef" info
) 1)))
614 ;; 3. A link to a "-n" block returns coderef.
615 (org-test-with-parsed-data
616 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
617 (should (equal (org-export-resolve-coderef "coderef" info
) "coderef")))
618 (org-test-with-parsed-data
619 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
620 (should (equal (org-export-resolve-coderef "coderef" info
) "coderef")))
621 ;; 4. A link to a "-r" block returns line number.
622 (org-test-with-parsed-data
623 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
624 (should (= (org-export-resolve-coderef "coderef" info
) 1)))
625 (org-test-with-parsed-data
626 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
627 (should (= (org-export-resolve-coderef "coderef" info
) 1)))
628 ;; 5. A link to a block without a switch returns coderef.
629 (org-test-with-parsed-data
630 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
631 (should (equal (org-export-resolve-coderef "coderef" info
) "coderef")))
632 (org-test-with-parsed-data
633 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
634 (should (equal (org-export-resolve-coderef "coderef" info
) "coderef")))
635 ;; 6. Correctly handle continued line numbers. A "+n" switch
636 ;; should resume numbering from previous block with numbered
637 ;; lines, ignoring blocks not numbering lines in the process.
638 ;; A "-n" switch resets count.
639 (org-test-with-parsed-data "
644 #+BEGIN_SRC emacs-lisp
648 #+BEGIN_SRC emacs-lisp +n -r
649 \(+ 1 1) (ref:addition)
652 #+BEGIN_EXAMPLE -n -r
653 Another text. (ref:text)
655 (should (= (org-export-resolve-coderef "addition" info
) 2))
656 (should (= (org-export-resolve-coderef "text" info
) 1)))
657 ;; 7. Recognize coderef with user-specified syntax.
658 (org-test-with-parsed-data
659 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
660 (should (equal (org-export-resolve-coderef "text" info
) "text")))))
662 (ert-deftest test-org-exprot
/resolve-fuzzy-link
()
663 "Test `org-export-resolve-fuzzy-link' specifications."
664 ;; 1. Match target objects.
665 (org-test-with-parsed-data "<<target>> [[target]]"
667 (org-export-resolve-fuzzy-link
668 (org-element-map tree
'link
'identity info t
) info
)))
669 ;; 2. Match target elements.
670 (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
672 (org-export-resolve-fuzzy-link
673 (org-element-map tree
'link
'identity info t
) info
)))
674 ;; 3. Match named elements.
675 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
677 (org-export-resolve-fuzzy-link
678 (org-element-map tree
'link
'identity info t
) info
)))
679 ;; 4. Match exact headline's name.
680 (org-test-with-parsed-data "* My headline\n[[My headline]]"
682 (org-export-resolve-fuzzy-link
683 (org-element-map tree
'link
'identity info t
) info
)))
684 ;; 5. Targets objects have priority over named elements and headline
686 (org-test-with-parsed-data
687 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
691 (org-export-resolve-fuzzy-link
692 (org-element-map tree
'link
'identity info t
) info
)))))
693 ;; 6. Named elements have priority over headline titles.
694 (org-test-with-parsed-data
695 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
699 (org-export-resolve-fuzzy-link
700 (org-element-map tree
'link
'identity info t
) info
)))))
701 ;; 7. If link's path starts with a "*", only match headline titles,
703 (org-test-with-parsed-data
704 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
708 (org-export-resolve-fuzzy-link
709 (org-element-map tree
'link
'identity info t
) info
)))))
710 ;; 8. Return nil if no match.
711 (org-test-with-parsed-data "[[target]]"
713 (org-export-resolve-fuzzy-link
714 (org-element-map tree
'link
'identity info t
) info
))))
716 (ert-deftest test-org-export
/resolve-id-link
()
717 "Test `org-export-resolve-id-link' specifications."
718 ;; 1. Regular test for custom-id link.
719 (org-test-with-parsed-data "* Headline1
726 (org-export-resolve-id-link
727 (org-element-map tree
'link
'identity info t
) info
)))
728 ;; 2. Failing test for custom-id link.
729 (org-test-with-parsed-data "* Headline1
736 (org-export-resolve-id-link
737 (org-element-map tree
'link
'identity info t
) info
)))
738 ;; 3. Test for internal id target.
739 (org-test-with-parsed-data "* Headline1
746 (org-export-resolve-id-link
747 (org-element-map tree
'link
'identity info t
) info
)))
748 ;; 4. Test for external id target.
749 (org-test-with-parsed-data "[[id:aaaa]]"
751 (org-export-resolve-id-link
752 (org-element-map tree
'link
'identity info t
)
753 (org-combine-plists info
'(:id-alist
(("aaaa" .
"external-file"))))))))
755 (ert-deftest test-org-export
/resolve-radio-link
()
756 "Test `org-export-resolve-radio-link' specifications."
758 (org-test-with-temp-text "<<<radio>>> radio"
759 (org-update-radio-target-regexp)
761 (let* ((tree (org-element-parse-buffer))
762 (info `(:parse-tree
,tree
)))
763 (org-export-resolve-radio-link
764 (org-element-map tree
'link
'identity info t
)
766 ;; Radio target with objects.
767 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
768 (org-update-radio-target-regexp)
770 (let* ((tree (org-element-parse-buffer))
771 (info `(:parse-tree
,tree
)))
772 (org-export-resolve-radio-link
773 (org-element-map tree
'link
'identity info t
)
780 (ert-deftest test-org-export
/define-macro
()
781 "Try defining various Org macro using in-buffer #+MACRO: keyword."
783 (should (equal (org-test-with-temp-text "#+MACRO: one 1"
784 (org-export-get-inbuffer-options))
785 '(:macro-one
("1"))))
787 (should (equal (org-test-with-temp-text "#+MACRO: two (eval (+ 1 1))"
788 (org-export-get-inbuffer-options))
789 '(:macro-two
("(eval (+ 1 1))"))))
791 (should-not (org-test-with-temp-text "#+MACRO: three"
792 (org-export-get-inbuffer-options)))
793 ;; Macro with newline character.
794 (should (equal (org-test-with-temp-text "#+MACRO: four a\\nb"
795 (org-export-get-inbuffer-options))
796 '(:macro-four
("a\nb"))))
797 ;; Macro with protected newline character.
798 (should (equal (org-test-with-temp-text "#+MACRO: five a\\\\nb"
799 (org-export-get-inbuffer-options))
800 '(:macro-five
("a\\nb"))))
802 (org-test-with-temp-text "#+MACRO: six 6\n#+MACRO: seven 1 + {{{six}}}"
805 (org-export-get-inbuffer-options)
809 ("1 + " (macro (:key
"six" :value
"{{{six}}}" :args nil
:begin
5 :end
14
810 :post-blank
0 :parent nil
))))))))
812 (ert-deftest test-org-export
/expand-macro
()
813 "Test `org-export-expand-macro' specifications."
818 (org-test-with-parsed-data "#+MACRO: macro some text\n{{{macro}}}"
819 (org-export-expand-macro
820 (org-element-map tree
'macro
'identity info t
) info
))))
821 ;; Macro with arguments.
825 (org-test-with-parsed-data "#+MACRO: macro $1 $2\n{{{macro(some,text)}}}"
826 (org-export-expand-macro
827 (org-element-map tree
'macro
'identity info t
) info
))))
832 (org-test-with-parsed-data "#+MACRO: add (eval (+ $1 $2))\n{{{add(1,2)}}}"
833 (org-export-expand-macro
834 (org-element-map tree
'macro
'identity info t
) info
))))
839 (org-test-with-parsed-data
840 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\n{{{out}}}"
841 (flet ((translate-macro (macro contents info
)
842 (org-export-expand-macro macro info
)))
843 (org-export-expand-macro
844 (org-element-map tree
'macro
'identity info t
)
846 info
`(:translate-alist
((macro . translate-macro
))))))))))
850 ;;; Src-block and example-block
852 (ert-deftest test-org-export
/unravel-code
()
853 "Test `org-export-unravel-code' function."
854 (let ((org-coderef-label-format "(ref:%s)"))
855 ;; 1. Code without reference.
856 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
857 (should (equal (org-export-unravel-code (org-element-current-element))
859 ;; 2. Code with reference.
860 (org-test-with-temp-text
861 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
862 (should (equal (org-export-unravel-code (org-element-current-element))
863 '("(+ 1 1)\n" (1 .
"test")))))
864 ;; 3. Code with user-defined reference.
865 (org-test-with-temp-text
866 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
867 (should (equal (org-export-unravel-code (org-element-current-element))
868 '("(+ 1 1)\n" (1 .
"test")))))
869 ;; 4. Code references keys are relative to the current block.
870 (org-test-with-temp-text "
879 (should (equal (org-export-unravel-code (org-element-current-element))
880 '("(+ 2 2)\n(+ 3 3)\n" (2 .
"one")))))
881 ;; 5. Free up comma-protected lines.
883 ;; 5.1. In an Org source block, every line is protected.
884 (org-test-with-temp-text
885 "#+BEGIN_SRC org\n,* Test\n,# comment\n,Text\n#+END_SRC"
886 (should (equal (org-export-unravel-code (org-element-current-element))
887 '("* Test\n# comment\nText\n"))))
888 ;; 5.2. In other blocks, only headlines, comments and keywords are
890 (org-test-with-temp-text
891 "#+BEGIN_EXAMPLE\n,* Headline\n, * Not headline\n,Keep\n#+END_EXAMPLE"
892 (should (equal (org-export-unravel-code (org-element-current-element))
893 '("* Headline\n, * Not headline\n,Keep\n"))))))
899 (ert-deftest test-org-export
/special-column
()
900 "Test if the table's special column is properly recognized."
901 ;; 1. First column is special if it contains only a special marking
902 ;; characters or empty cells.
903 (org-test-with-temp-text "
907 (org-export-table-has-special-column-p
909 (org-element-parse-buffer) 'table
'identity nil
'first-match
))))
910 ;; 2. If the column contains anything else, it isn't special.
911 (org-test-with-temp-text "
915 (org-export-table-has-special-column-p
917 (org-element-parse-buffer) 'table
'identity nil
'first-match
))))
918 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
920 (org-test-with-temp-text "
929 (org-export-table-has-special-column-p
931 (org-element-parse-buffer) 'table
'identity nil
'first-match
))))
932 ;; 4. A first column with only empty cells isn't considered as
934 (org-test-with-temp-text "
938 (org-export-table-has-special-column-p
940 (org-element-parse-buffer) 'table
'identity nil
'first-match
)))))
942 (ert-deftest test-org-export
/table-row-is-special-p
()
943 "Test `org-export-table-row-is-special-p' specifications."
944 ;; 1. A row is special if it has a special marking character in the
946 (org-test-with-parsed-data "| ! | 1 |"
948 (org-export-table-row-is-special-p
949 (org-element-map tree
'table-row
'identity nil
'first-match
) info
)))
950 ;; 2. A row is special when its first field is "/"
951 (org-test-with-parsed-data "
955 (org-export-table-row-is-special-p
956 (org-element-map tree
'table-row
'identity nil
'first-match
) info
)))
957 ;; 3. A row only containing alignment cookies is also considered as
959 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
961 (org-export-table-row-is-special-p
962 (org-element-map tree
'table-row
'identity nil
'first-match
) info
)))
963 ;; 4. Everything else isn't considered as special.
964 (org-test-with-parsed-data "| \alpha | | c |"
966 (org-export-table-row-is-special-p
967 (org-element-map tree
'table-row
'identity nil
'first-match
) info
)))
968 ;; 5. Table's rules are never considered as special rows.
969 (org-test-with-parsed-data "|---+---|"
971 (org-export-table-row-is-special-p
972 (org-element-map tree
'table-row
'identity nil
'first-match
) info
))))
974 (ert-deftest test-org-export
/has-header-p
()
975 "Test `org-export-table-has-header-p' specifications."
976 ;; 1. With an header.
977 (org-test-with-parsed-data "
982 (org-export-table-has-header-p
983 (org-element-map tree
'table
'identity info
'first-match
)
985 ;; 2. Without an header.
986 (org-test-with-parsed-data "
990 (org-export-table-has-header-p
991 (org-element-map tree
'table
'identity info
'first-match
)
993 ;; 3. Don't get fooled with starting and ending rules.
994 (org-test-with-parsed-data "
1000 (org-export-table-has-header-p
1001 (org-element-map tree
'table
'identity info
'first-match
)
1004 (ert-deftest test-org-export
/table-row-group
()
1005 "Test `org-export-table-row-group' specifications."
1006 ;; 1. A rule creates a new group.
1007 (org-test-with-parsed-data "
1014 (mapcar (lambda (row) (org-export-table-row-group row info
))
1015 (org-element-map tree
'table-row
'identity
)))))
1016 ;; 2. Special rows are ignored in count.
1017 (org-test-with-parsed-data "
1024 (mapcar (lambda (row) (org-export-table-row-group row info
))
1025 (org-element-map tree
'table-row
'identity
)))))
1026 ;; 3. Double rules also are ignored in count.
1027 (org-test-with-parsed-data "
1035 (mapcar (lambda (row) (org-export-table-row-group row info
))
1036 (org-element-map tree
'table-row
'identity
))))))
1038 (ert-deftest test-org-export
/table-cell-width
()
1039 "Test `org-export-table-cell-width' specifications."
1040 ;; 1. Width is primarily determined by width cookies. If no cookie
1041 ;; is found, cell's width is nil.
1042 (org-test-with-parsed-data "
1043 | / | <l> | <6> | <l7> |
1048 (mapcar (lambda (cell) (org-export-table-cell-width cell info
))
1049 (org-element-map tree
'table-cell
'identity info
)))))
1050 ;; 2. The last width cookie has precedence.
1051 (org-test-with-parsed-data "
1058 (mapcar (lambda (cell) (org-export-table-cell-width cell info
))
1059 (org-element-map tree
'table-cell
'identity info
)))))
1060 ;; 3. Valid width cookies must have a specific row.
1061 (org-test-with-parsed-data "| <6> | cell |"
1065 (mapcar (lambda (cell) (org-export-table-cell-width cell info
))
1066 (org-element-map tree
'table-cell
'identity
))))))
1068 (ert-deftest test-org-export
/table-cell-alignment
()
1069 "Test `org-export-table-cell-alignment' specifications."
1070 (let ((org-table-number-fraction 0.5)
1071 (org-table-number-regexp "^[0-9]+$"))
1072 ;; 1. Alignment is primarily determined by alignment cookies.
1073 (org-test-with-temp-text "| <l> | <c> | <r> |"
1074 (let* ((tree (org-element-parse-buffer))
1075 (info `(:parse-tree
,tree
)))
1078 '(left center right
)
1079 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info
))
1080 (org-element-map tree
'table-cell
'identity
))))))
1081 ;; 2. The last alignment cookie has precedence.
1082 (org-test-with-parsed-data "
1088 '(right right right
)
1089 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info
))
1090 (org-element-map tree
'table-cell
'identity
)))))
1091 ;; 3. If there's no cookie, cell's contents determine alignment.
1092 ;; A column mostly made of cells containing numbers will align
1093 ;; its cells to the right.
1094 (org-test-with-parsed-data "
1100 '(right right right
)
1101 (mapcar (lambda (cell)
1102 (org-export-table-cell-alignment cell info
))
1103 (org-element-map tree
'table-cell
'identity
)))))
1104 ;; 4. Otherwise, they will be aligned to the left.
1105 (org-test-with-parsed-data "
1112 (mapcar (lambda (cell)
1113 (org-export-table-cell-alignment cell info
))
1114 (org-element-map tree
'table-cell
'identity
)))))))
1116 (ert-deftest test-org-export
/table-cell-borders
()
1117 "Test `org-export-table-cell-borders' specifications."
1118 ;; 1. Recognize various column groups indicators.
1119 (org-test-with-parsed-data "| / | < | > | <> |"
1122 '((right bottom top
) (left bottom top
) (right bottom top
)
1123 (right left bottom top
))
1124 (mapcar (lambda (cell)
1125 (org-export-table-cell-borders cell info
))
1126 (org-element-map tree
'table-cell
'identity
)))))
1127 ;; 2. Accept shortcuts to define column groups.
1128 (org-test-with-parsed-data "| / | < | < |"
1131 '((right bottom top
) (right left bottom top
) (left bottom top
))
1132 (mapcar (lambda (cell)
1133 (org-export-table-cell-borders cell info
))
1134 (org-element-map tree
'table-cell
'identity
)))))
1135 ;; 3. A valid column groups row must start with a "/".
1136 (org-test-with-parsed-data "
1140 (equal '((top) (top) (bottom) (bottom))
1141 (mapcar (lambda (cell)
1142 (org-export-table-cell-borders cell info
))
1143 (org-element-map tree
'table-cell
'identity
)))))
1144 ;; 4. Take table rules into consideration.
1145 (org-test-with-parsed-data "
1150 (equal '((below top
) (bottom above
))
1151 (mapcar (lambda (cell)
1152 (org-export-table-cell-borders cell info
))
1153 (org-element-map tree
'table-cell
'identity
)))))
1154 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
1155 ;; (resp. `bottom' and `below') borders. Any special row is
1157 (org-test-with-parsed-data "
1163 (equal '((bottom below top above
))
1165 (mapcar (lambda (cell)
1166 (org-export-table-cell-borders cell info
))
1167 (org-element-map tree
'table-cell
'identity
)))))))
1169 (ert-deftest test-org-export
/table-dimensions
()
1170 "Test `org-export-table-dimensions' specifications."
1171 ;; 1. Standard test.
1172 (org-test-with-parsed-data "
1177 (org-export-table-dimensions
1178 (org-element-map tree
'table
'identity info
'first-match
) info
))))
1179 ;; 2. Ignore horizontal rules and special columns.
1180 (org-test-with-parsed-data "
1187 (org-export-table-dimensions
1188 (org-element-map tree
'table
'identity info
'first-match
) info
)))))
1190 (ert-deftest test-org-export
/table-cell-address
()
1191 "Test `org-export-table-cell-address' specifications."
1192 ;; 1. Standard test: index is 0-based.
1193 (org-test-with-parsed-data "| a | b |"
1195 (equal '((0 .
0) (0 .
1))
1198 (lambda (cell) (org-export-table-cell-address cell info
))
1200 ;; 2. Special column isn't counted, nor are special rows.
1201 (org-test-with-parsed-data "
1206 (org-export-table-cell-address
1207 (car (last (org-element-map tree
'table-cell
'identity info
)))
1209 ;; 3. Tables rules do not count either.
1210 (org-test-with-parsed-data "
1218 (org-export-table-cell-address
1219 (car (last (org-element-map tree
'table-cell
'identity info
)))
1221 ;; 4. Return nil for special cells.
1222 (org-test-with-parsed-data "| / | a |"
1224 (org-export-table-cell-address
1225 (org-element-map tree
'table-cell
'identity nil
'first-match
)
1228 (ert-deftest test-org-export
/get-table-cell-at
()
1229 "Test `org-export-get-table-cell-at' specifications."
1230 ;; 1. Address ignores special columns, special rows and rules.
1231 (org-test-with-parsed-data "
1238 (org-element-contents
1239 (org-export-get-table-cell-at
1241 (org-element-map tree
'table
'identity info
'first-match
)
1243 ;; 2. Return value for a non-existent address is nil.
1244 (org-test-with-parsed-data "| a |"
1246 (org-export-get-table-cell-at
1248 (org-element-map tree
'table
'identity info
'first-match
)
1250 (org-test-with-parsed-data "| / |"
1252 (org-export-get-table-cell-at
1254 (org-element-map tree
'table
'identity info
'first-match
)
1257 (ert-deftest test-org-export
/table-cell-starts-colgroup-p
()
1258 "Test `org-export-table-cell-starts-colgroup-p' specifications."
1259 ;; 1. A cell at a beginning of a row always starts a column group.
1260 (org-test-with-parsed-data "| a |"
1262 (org-export-table-cell-starts-colgroup-p
1263 (org-element-map tree
'table-cell
'identity info
'first-match
)
1265 ;; 2. Special column should be ignored when determining the
1266 ;; beginning of the row.
1267 (org-test-with-parsed-data "
1271 (org-export-table-cell-starts-colgroup-p
1272 (org-element-map tree
'table-cell
'identity info
'first-match
)
1274 ;; 2. Explicit column groups.
1275 (org-test-with-parsed-data "
1284 (if (org-export-table-cell-starts-colgroup-p cell info
) 'yes
'no
))
1287 (ert-deftest test-org-export
/table-cell-ends-colgroup-p
()
1288 "Test `org-export-table-cell-ends-colgroup-p' specifications."
1289 ;; 1. A cell at the end of a row always ends a column group.
1290 (org-test-with-parsed-data "| a |"
1292 (org-export-table-cell-ends-colgroup-p
1293 (org-element-map tree
'table-cell
'identity info
'first-match
)
1295 ;; 2. Special column should be ignored when determining the
1296 ;; beginning of the row.
1297 (org-test-with-parsed-data "
1301 (org-export-table-cell-ends-colgroup-p
1302 (org-element-map tree
'table-cell
'identity info
'first-match
)
1304 ;; 3. Explicit column groups.
1305 (org-test-with-parsed-data "
1314 (if (org-export-table-cell-ends-colgroup-p cell info
) 'yes
'no
))
1317 (ert-deftest test-org-export
/table-row-starts-rowgroup-p
()
1318 "Test `org-export-table-row-starts-rowgroup-p' specifications."
1319 ;; 1. A row at the beginning of a table always starts a row group.
1320 ;; So does a row following a table rule.
1321 (org-test-with-parsed-data "
1331 (if (org-export-table-row-starts-rowgroup-p row info
) 'yes
'no
))
1333 ;; 2. Special rows should be ignored when determining the beginning
1335 (org-test-with-parsed-data "
1347 (if (org-export-table-row-starts-rowgroup-p row info
) 'yes
'no
))
1350 (ert-deftest test-org-export
/table-row-ends-rowgroup-p
()
1351 "Test `org-export-table-row-ends-rowgroup-p' specifications."
1352 ;; 1. A row at the end of a table always ends a row group. So does
1353 ;; a row preceding a table rule.
1354 (org-test-with-parsed-data "
1364 (if (org-export-table-row-ends-rowgroup-p row info
) 'yes
'no
))
1366 ;; 2. Special rows should be ignored when determining the beginning
1368 (org-test-with-parsed-data "
1380 (if (org-export-table-row-ends-rowgroup-p row info
) 'yes
'no
))
1383 (ert-deftest test-org-export
/table-row-starts-header-p
()
1384 "Test `org-export-table-row-starts-header-p' specifications."
1385 ;; 1. Only the row starting the first row group starts the table
1387 (org-test-with-parsed-data "
1398 (if (org-export-table-row-starts-header-p row info
) 'yes
'no
))
1400 ;; 2. A row cannot start an header if there's no header in the
1402 (org-test-with-parsed-data "
1406 (org-export-table-row-starts-header-p
1407 (org-element-map tree
'table-row
'identity info
'first-match
)
1410 (ert-deftest test-org-export
/table-row-ends-header-p
()
1411 "Test `org-export-table-row-ends-header-p' specifications."
1412 ;; 1. Only the row starting the first row group starts the table
1414 (org-test-with-parsed-data "
1425 (if (org-export-table-row-ends-header-p row info
) 'yes
'no
))
1427 ;; 2. A row cannot start an header if there's no header in the
1429 (org-test-with-parsed-data "
1433 (org-export-table-row-ends-header-p
1434 (org-element-map tree
'table-row
'identity info
'first-match
)
1438 (provide 'test-org-export
)
1439 ;;; test-org-export.el end here