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"))
23 (defmacro org-test-with-backend
(backend &rest body
)
24 "Execute body with an export back-end defined.
26 BACKEND is the name, as a string, of the back-end. BODY is the
27 body to execute. The defined back-end simply returns parsed data
29 (declare (debug (form body
)) (indent 1))
30 `(flet ,(let (transcoders)
31 (dolist (type (append org-element-all-elements
32 org-element-all-objects
)
34 (push `(,(intern (format "org-%s-%s" backend type
))
36 (,(intern (format "org-element-%s-interpreter" type
))
41 (ert-deftest test-org-export
/parse-option-keyword
()
42 "Test reading all standard #+OPTIONS: items."
45 (org-export-parse-option-keyword
46 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
47 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t")
49 1 :preserve-breaks t
:section-numbers t
:time-stamp-file t
50 :with-archived-trees t
:with-author t
:with-creator t
:with-drawers t
51 :with-email t
:with-emphasize t
:with-entities t
:with-fixed-width t
52 :with-footnotes t
:with-priority t
:with-special-strings t
53 :with-sub-superscript t
:with-toc t
:with-tables t
:with-tags t
54 :with-tasks t
:with-timestamps t
:with-todo-keywords t
)))
55 ;; Test some special values.
58 (org-export-parse-option-keyword
59 "arch:headline creator:comment d:(\"TEST\")
60 ^:{} toc:1 tags:not-in-toc tasks:todo num:2")
63 :with-archived-trees headline
:with-creator comment
64 :with-drawers
("TEST") :with-sub-superscript
{} :with-toc
1
65 :with-tags not-in-toc
:with-tasks todo
))))
67 (ert-deftest test-org-export
/get-inbuffer-options
()
68 "Test reading all standard export keywords."
71 (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
74 #+DESCRIPTION: Testing
75 #+DESCRIPTION: with two lines
76 #+EMAIL: some@email.org
77 #+EXPORT_EXCLUDE_TAGS: noexport invisible
80 #+EXPORT_SELECT_TAGS: export
83 (org-export-get-inbuffer-options))
85 "Me, Myself and I" :creator
"Idem" :date
"Today"
86 :description
"Testing\nwith two lines" :email
"some@email.org"
87 :exclude-tags
("noexport" "invisible") :keywords
"test" :language
"en"
88 :select-tags
("export") :title
"Some title with spaces"))))
90 (ert-deftest test-org-export
/define-macro
()
91 "Try defining various Org macro using in-buffer #+MACRO: keyword."
93 (should (equal (org-test-with-temp-text "#+MACRO: one 1"
94 (org-export-get-inbuffer-options))
97 (should (equal (org-test-with-temp-text "#+MACRO: two (eval (+ 1 1))"
98 (org-export-get-inbuffer-options))
99 '(:macro-two
"(eval (+ 1 1))")))
101 (should-not (org-test-with-temp-text "#+MACRO: three"
102 (org-export-get-inbuffer-options)))
103 ;; Macro with newline character.
104 (should (equal (org-test-with-temp-text "#+MACRO: four a\\nb"
105 (org-export-get-inbuffer-options))
106 '(:macro-four
("a\nb"))))
107 ;; Macro with protected newline character.
108 (should (equal (org-test-with-temp-text "#+MACRO: five a\\\\nb"
109 (org-export-get-inbuffer-options))
110 '(:macro-five
("a\\nb"))))
112 (org-test-with-temp-text "#+MACRO: six 6\n#+MACRO: seven 1 + {{{six}}}"
115 (org-export-get-inbuffer-options)
119 ("1 + " (macro (:key
"six" :value
"{{{six}}}" :args nil
:begin
5 :end
14
120 :post-blank
0))))))))
122 (ert-deftest test-org-export
/handle-options
()
123 "Test if export options have an impact on output."
124 ;; Test exclude tags.
125 (org-test-with-temp-text "* Head1 :noexport:"
126 (org-test-with-backend "test"
128 (equal (org-export-as 'test nil nil nil
'(:exclude-tags
("noexport")))
130 ;; Test include tags.
131 (org-test-with-temp-text "
133 ** Sub-Head1.1 :export:
136 (org-test-with-backend "test"
139 "\\* Head1\n\\*\\* Sub-Head1.1[ \t]+:export:\n\\*\\*\\* Sub-Head1.1.1\n"
140 (org-export-as 'test nil nil nil
'(:select-tags
("export")))))))
141 ;; Test mixing include tags and exclude tags.
142 (org-test-with-temp-text "
144 ** Sub-Head1 :noexport:
147 ** Sub-Head1 :export:"
148 (org-test-with-backend "test"
151 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
154 '(:select-tags
("export") :exclude-tags
("noexport")))))))
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 nil
))
161 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
162 (org-test-with-temp-text "* TODO Head1"
163 (org-test-with-backend "test"
164 (should (equal (org-export-as 'test nil nil nil
'(:with-tasks t
))
165 "* TODO Head1\n")))))
167 (org-test-with-temp-text "* Head1 :archive:"
168 (let ((org-archive-tag "archive"))
169 (org-test-with-backend "test"
171 (equal (org-export-as 'test nil nil nil
'(:with-archived-trees nil
))
173 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
174 (let ((org-archive-tag "archive"))
175 (org-test-with-backend "test"
178 "\\* Head1[ \t]+:archive:"
179 (org-export-as 'test nil nil nil
180 '(:with-archived-trees headline
)))))))
181 (org-test-with-temp-text "* Head1 :archive:"
182 (let ((org-archive-tag "archive"))
183 (org-test-with-backend "test"
186 "\\`\\* Head1[ \t]+:archive:\n\\'"
187 (org-export-as 'test nil nil nil
'(:with-archived-trees t
)))))))
189 (let ((org-drawers '("TEST")))
190 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
191 (org-test-with-backend "test"
192 (should (equal (org-export-as 'test nil nil nil
'(:with-drawers nil
))
194 (let ((org-drawers '("TEST")))
195 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
196 (org-test-with-backend "test"
197 (should (equal (org-export-as 'test nil nil nil
'(:with-drawers t
))
198 ":TEST:\ncontents\n:END:\n"))))))
200 (ert-deftest test-org-export
/comment-tree
()
201 "Test if export process ignores commented trees."
202 (let ((org-comment-string "COMMENT"))
203 (org-test-with-temp-text "* COMMENT Head1"
204 (org-test-with-backend "test"
205 (should (equal (org-export-as 'test
) ""))))))
207 (ert-deftest test-org-export
/export-scope
()
208 "Test all export scopes."
209 (org-test-with-temp-text "
214 (org-test-with-backend "test"
217 (should (equal (org-export-as 'test
'subtree
) "text\n*** Head3\n"))
219 (goto-char (point-min))
222 (should (equal (org-export-as 'test nil
'visible
) "* Head1\n"))
224 (flet ((org-test-template (body info
) (format "BEGIN\n%sEND" body
)))
225 (should (equal (org-export-as 'test nil nil
'body-only
)
226 "* Head1\n** Head2\ntext\n*** Head3\n"))
227 (should (equal (org-export-as 'test
)
228 "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
230 (goto-char (point-min))
232 (transient-mark-mode 1)
233 (push-mark (point) t t
)
234 (goto-char (point-at-eol))
235 (should (equal (org-export-as 'test
) "text\n")))))
237 (ert-deftest test-org-export
/export-snippet
()
238 "Test export snippets transcoding."
239 (org-test-with-temp-text "@test{A}@t{B}"
240 (org-test-with-backend "test"
241 (flet ((org-test-export-snippet
242 (snippet contents info
)
243 (when (eq (org-export-snippet-backend snippet
) 'test
)
244 (org-element-property :value snippet
))))
245 (let ((org-export-snippet-translation-alist nil
))
246 (should (equal (org-export-as 'test
) "A\n")))
247 (let ((org-export-snippet-translation-alist '(("t" .
"test"))))
248 (should (equal (org-export-as 'test
) "AB\n")))))))
250 (ert-deftest test-org-export
/expand-include
()
251 "Test file inclusion in an Org buffer."
252 ;; Full insertion with recursive inclusion.
253 (org-test-with-temp-text
254 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir
)
255 (org-export-expand-include-keyword)
256 (should (equal (buffer-string)
257 "Small Org file with an include keyword.
259 #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
265 ;; Localized insertion.
266 (org-test-with-temp-text
267 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
269 (org-export-expand-include-keyword)
270 (should (equal (buffer-string)
271 "Small Org file with an include keyword.\n")))
272 ;; Insertion with constraints on headlines level.
273 (org-test-with-temp-text
275 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
277 (org-export-expand-include-keyword)
278 (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
279 ;; Inclusion within an example block.
280 (org-test-with-temp-text
281 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
283 (org-export-expand-include-keyword)
287 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
288 ;; Inclusion within a src-block.
289 (org-test-with-temp-text
291 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
293 (org-export-expand-include-keyword)
294 (should (equal (buffer-string)
295 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
297 (ert-deftest test-org-export
/user-ignore-list
()
298 "Test if `:ignore-list' accepts user input."
299 (org-test-with-backend "test"
300 (flet ((skip-note-head
302 ;; Ignore headlines with the word "note" in their title.
306 (when (string-match "\\<note\\>"
307 (org-element-property :raw-value headline
))
308 (org-export-ignore-element headline info
)))
311 ;; Install function in parse tree filters.
312 (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
313 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
314 (should (equal (org-export-as 'test
) "* Head1\n")))))))
316 (ert-deftest test-org-export
/footnotes
()
317 "Test footnotes specifications."
318 (let ((org-footnote-section nil
))
319 ;; 1. Read every type of footnote.
320 (org-test-with-temp-text
321 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
322 (let* ((tree (org-element-parse-buffer))
323 (info (org-combine-plists
324 (org-export-initial-options) '(:with-footnotes t
))))
325 (setq info
(org-combine-plists
326 info
(org-export-collect-tree-properties tree info
'test
)))
329 '((1 .
"A") (2 .
"B") (3 .
"C") (4 .
"D"))
331 tree
'footnote-reference
333 (let ((def (org-export-get-footnote-definition ref info
)))
334 (cons (org-export-get-footnote-number ref info
)
335 (if (eq (org-element-property :type ref
) 'inline
) (car def
)
336 (car (org-element-contents
337 (car (org-element-contents def
))))))))
339 ;; 2. Test nested footnotes order.
340 (org-test-with-temp-text
341 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
342 (let* ((tree (org-element-parse-buffer))
343 (info (org-combine-plists
344 (org-export-initial-options) '(:with-footnotes t
))))
345 (setq info
(org-combine-plists
346 info
(org-export-collect-tree-properties tree info
'test
)))
349 '((1 .
"fn:1") (2 .
"fn:2") (3 .
"fn:3") (4))
351 tree
'footnote-reference
353 (when (org-export-footnote-first-reference-p ref info
)
354 (cons (org-export-get-footnote-number ref info
)
355 (org-element-property :label ref
))))
357 ;; 3. Test nested footnote in invisible definitions.
358 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
360 (narrow-to-region (point) (point-at-eol))
361 (let* ((tree (org-element-parse-buffer))
362 (info (org-combine-plists
363 (org-export-initial-options) '(:with-footnotes t
))))
364 (setq info
(org-combine-plists
365 info
(org-export-collect-tree-properties tree info
'test
)))
366 ;; Both footnotes should be seen.
368 (= (length (org-export-collect-footnote-definitions tree info
)) 2))))
369 ;; 4. Test footnotes definitions collection.
370 (org-test-with-temp-text "Text[fn:1:A[fn:2]] [fn:3].
372 \[fn:2] B [fn:3] [fn::D].
375 (let ((tree (org-element-parse-buffer))
376 (info (org-combine-plists
377 (org-export-initial-options) '(:with-footnotes t
))))
378 (setq info
(org-combine-plists
379 info
(org-export-collect-tree-properties tree info
'test
)))
380 (should (= (length (org-export-collect-footnote-definitions tree info
))
383 (ert-deftest test-org-export
/fuzzy-links
()
384 "Test fuzz link export specifications."
385 ;; 1. Links to invisible (keyword) targets should be ignored.
386 (org-test-with-temp-text
387 "Paragraph.\n#+TARGET: Test\n[[Test]]"
388 (let* ((tree (org-element-parse-buffer))
389 (info (org-combine-plists (org-export-initial-options))))
390 (setq info
(org-combine-plists
391 info
(org-export-collect-tree-properties tree info
'test
)))
396 (org-export-get-ordinal
397 (org-export-resolve-fuzzy-link link info
) info
)) info
))))
398 ;; 2. Link to an headline should return headline's number.
399 (org-test-with-temp-text
400 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
401 (let* ((tree (org-element-parse-buffer))
402 (info (org-combine-plists (org-export-initial-options))))
403 (setq info
(org-combine-plists
404 info
(org-export-collect-tree-properties tree info
'test
)))
406 ;; Note: Headline's number is in fact a list of numbers.
411 (org-export-get-ordinal
412 (org-export-resolve-fuzzy-link link info
) info
)) info t
)))))
413 ;; 3. Link to a target in an item should return item's number.
414 (org-test-with-temp-text
415 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
416 (let* ((tree (org-element-parse-buffer))
417 (info (org-combine-plists (org-export-initial-options))))
418 (setq info
(org-combine-plists
419 info
(org-export-collect-tree-properties tree info
'test
)))
421 ;; Note: Item's number is in fact a list of numbers.
426 (org-export-get-ordinal
427 (org-export-resolve-fuzzy-link link info
) info
)) info t
)))))
428 ;; 4. Link to a target in a footnote should return footnote's
430 (org-test-with-temp-text
431 "Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
432 (let* ((tree (org-element-parse-buffer))
433 (info (org-combine-plists (org-export-initial-options))))
434 (setq info
(org-combine-plists
435 info
(org-export-collect-tree-properties tree info
'test
)))
441 (org-export-get-ordinal
442 (org-export-resolve-fuzzy-link link info
) info
)) info
)))))
443 ;; 5. Link to a named element should return sequence number of that
445 (org-test-with-temp-text
446 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
447 (let* ((tree (org-element-parse-buffer))
448 (info (org-combine-plists (org-export-initial-options))))
449 (setq info
(org-combine-plists
450 info
(org-export-collect-tree-properties tree info
'test
)))
456 (org-export-get-ordinal
457 (org-export-resolve-fuzzy-link link info
) info
)) info t
)))))
458 ;; 6. Link to a target not within an item, a table, a footnote
459 ;; reference or definition should return section number.
460 (org-test-with-temp-text
461 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
462 (let* ((tree (org-element-parse-buffer))
463 (info (org-combine-plists (org-export-initial-options))))
464 (setq info
(org-combine-plists
465 info
(org-export-collect-tree-properties tree info
'test
)))
471 (org-export-get-ordinal
472 (org-export-resolve-fuzzy-link link info
) info
)) info t
))))))