Move two test files into common test directory
[org-mode.git] / testing / lisp / test-org-export.el
blobfaa277375f0795076b33403bff4cdb4849ccf660
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
10 ;;;; Comments
14 ;;; Code:
16 (unless (featurep 'org-export)
17 (signal 'missing-test-dependency "org-export"))
21 ;;; Tests
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
28 as Org syntax."
29 (declare (debug (form body)) (indent 1))
30 `(flet ,(let (transcoders)
31 (dolist (type (append org-element-all-elements
32 org-element-all-objects)
33 transcoders)
34 (push `(,(intern (format "org-%s-%s" backend type))
35 (obj contents info)
36 (,(intern (format "org-element-%s-interpreter" type))
37 obj contents))
38 transcoders)))
39 ,@body))
41 (ert-deftest test-org-export/parse-option-keyword ()
42 "Test reading all standard #+OPTIONS: items."
43 (should
44 (equal
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")
48 '(:headline-levels
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.
56 (should
57 (equal
58 (org-export-parse-option-keyword
59 "arch:headline creator:comment d:(\"TEST\")
60 ^:{} toc:1 tags:not-in-toc tasks:todo")
61 '(:with-archived-trees
62 headline :with-creator comment :with-drawers ("TEST")
63 :with-sub-superscript {} :with-toc 1 :with-tags not-in-toc
64 :with-tasks todo))))
66 (ert-deftest test-org-export/get-inbuffer-options ()
67 "Test reading all standard export keywords."
68 (should
69 (equal
70 (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
71 #+CREATOR: Idem
72 #+DATE: Today
73 #+DESCRIPTION: Testing
74 #+DESCRIPTION: with two lines
75 #+EMAIL: some@email.org
76 #+EXPORT_EXCLUDE_TAGS: noexport invisible
77 #+KEYWORDS: test
78 #+LANGUAGE: en
79 #+EXPORT_SELECT_TAGS: export
80 #+TITLE: Some title
81 #+TITLE: with spaces"
82 (org-export-get-inbuffer-options))
83 '(:author
84 "Me, Myself and I" :creator "Idem" :date "Today"
85 :description "Testing\nwith two lines" :email "some@email.org"
86 :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
87 :select-tags ("export") :title "Some title with spaces"))))
89 (ert-deftest test-org-export/define-macro ()
90 "Try defining various Org macro using in-buffer #+MACRO: keyword."
91 ;; Parsed macro.
92 (should (equal (org-test-with-temp-text "#+MACRO: one 1"
93 (org-export-get-inbuffer-options))
94 '(:macro-one ("1"))))
95 ;; Evaled macro.
96 (should (equal (org-test-with-temp-text "#+MACRO: two (eval (+ 1 1))"
97 (org-export-get-inbuffer-options))
98 '(:macro-two "(eval (+ 1 1))")))
99 ;; Incomplete macro.
100 (should-not (org-test-with-temp-text "#+MACRO: three"
101 (org-export-get-inbuffer-options)))
102 ;; Macro with newline character.
103 (should (equal (org-test-with-temp-text "#+MACRO: four a\\nb"
104 (org-export-get-inbuffer-options))
105 '(:macro-four ("a\nb"))))
106 ;; Macro with protected newline character.
107 (should (equal (org-test-with-temp-text "#+MACRO: five a\\\\nb"
108 (org-export-get-inbuffer-options))
109 '(:macro-five ("a\\nb"))))
110 ;; Recursive macro.
111 (org-test-with-temp-text "#+MACRO: six 6\n#+MACRO: seven 1 + {{{six}}}"
112 (should
113 (equal
114 (org-export-get-inbuffer-options)
115 '(:macro-six
116 ("6")
117 :macro-seven
118 ("1 + " (macro (:key "six" :value "{{{six}}}" :args nil :begin 5 :end 14
119 :post-blank 0))))))))
121 (ert-deftest test-org-export/handle-options ()
122 "Test if export options have an impact on output."
123 ;; Test exclude tags.
124 (org-test-with-temp-text "* Head1 :noexport:"
125 (org-test-with-backend "test"
126 (should
127 (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
128 ""))))
129 ;; Test include tags.
130 (org-test-with-temp-text "
131 * Head1
132 ** Sub-Head1.1 :export:
133 *** Sub-Head1.1.1
134 * Head2"
135 (org-test-with-backend "test"
136 (should
137 (string-match
138 "\\* Head1\n\\*\\* Sub-Head1.1[ \t]+:export:\n\\*\\*\\* Sub-Head1.1.1\n"
139 (org-export-as 'test nil nil nil '(:select-tags ("export")))))))
140 ;; Test mixing include tags and exclude tags.
141 (org-test-with-temp-text "
142 * Head1 :export:
143 ** Sub-Head1 :noexport:
144 ** Sub-Head2
145 * Head2 :noexport:
146 ** Sub-Head1 :export:"
147 (org-test-with-backend "test"
148 (should
149 (string-match
150 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
151 (org-export-as
152 'test nil nil nil
153 '(:select-tags ("export") :exclude-tags ("noexport")))))))
154 ;; Ignore tasks.
155 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
156 (org-test-with-temp-text "* TODO Head1"
157 (org-test-with-backend "test"
158 (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
159 "")))))
160 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
161 (org-test-with-temp-text "* TODO Head1"
162 (org-test-with-backend "test"
163 (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
164 "* TODO Head1\n")))))
165 ;; Archived tree.
166 (org-test-with-temp-text "* Head1 :archive:"
167 (let ((org-archive-tag "archive"))
168 (org-test-with-backend "test"
169 (should
170 (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
171 "")))))
172 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
173 (let ((org-archive-tag "archive"))
174 (org-test-with-backend "test"
175 (should
176 (string-match
177 "\\* Head1[ \t]+:archive:"
178 (org-export-as 'test nil nil nil
179 '(:with-archived-trees headline)))))))
180 (org-test-with-temp-text "* Head1 :archive:"
181 (let ((org-archive-tag "archive"))
182 (org-test-with-backend "test"
183 (should
184 (string-match
185 "\\`\\* Head1[ \t]+:archive:\n\\'"
186 (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
187 ;; Drawers.
188 (let ((org-drawers '("TEST")))
189 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
190 (org-test-with-backend "test"
191 (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
192 "")))))
193 (let ((org-drawers '("TEST")))
194 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
195 (org-test-with-backend "test"
196 (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
197 ":TEST:\ncontents\n:END:\n"))))))
199 (ert-deftest test-org-export/comment-tree ()
200 "Test if export process ignores commented trees."
201 (let ((org-comment-string "COMMENT"))
202 (org-test-with-temp-text "* COMMENT Head1"
203 (org-test-with-backend "test"
204 (should (equal (org-export-as 'test) ""))))))
206 (ert-deftest test-org-export/export-scope ()
207 "Test all export scopes."
208 (org-test-with-temp-text "
209 * Head1
210 ** Head2
211 text
212 *** Head3"
213 (org-test-with-backend "test"
214 ;; Subtree.
215 (forward-line 3)
216 (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
217 ;; Visible.
218 (goto-char (point-min))
219 (forward-line)
220 (org-cycle)
221 (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
222 ;; Body only.
223 (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
224 (should (equal (org-export-as 'test nil nil 'body-only)
225 "* Head1\n** Head2\ntext\n*** Head3\n"))
226 (should (equal (org-export-as 'test)
227 "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
228 ;; Region.
229 (goto-char (point-min))
230 (forward-line 3)
231 (mark-paragraph)
232 (should (equal (org-export-as 'test) "text\n")))))
234 (ert-deftest test-org-export/export-snippet ()
235 "Test export snippets transcoding."
236 (org-test-with-temp-text "@test{A}@t{B}"
237 (org-test-with-backend "test"
238 (flet ((org-test-export-snippet
239 (snippet contents info)
240 (when (eq (org-export-snippet-backend snippet) 'test)
241 (org-element-property :value snippet))))
242 (let ((org-export-snippet-translation-alist nil))
243 (should (equal (org-export-as 'test) "A\n")))
244 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
245 (should (equal (org-export-as 'test) "AB\n")))))))
247 (ert-deftest test-org-export/expand-include ()
248 "Test file inclusion in an Org buffer."
249 ;; Full insertion with recursive inclusion.
250 (org-test-with-temp-text
251 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
252 (org-export-expand-include-keyword)
253 (should (equal (buffer-string)
254 "Small Org file with an include keyword.
256 #+BEGIN_SRC emacs-lisp :exports results
257 (+ 2 1)
258 #+END_SRC
260 Success!
262 * Heading
263 body\n")))
264 ;; Localized insertion.
265 (org-test-with-temp-text
266 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
267 org-test-dir)
268 (org-export-expand-include-keyword)
269 (should (equal (buffer-string)
270 "Small Org file with an include keyword.\n")))
271 ;; Insertion with constraints on headlines level.
272 (org-test-with-temp-text
273 (format
274 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
275 org-test-dir)
276 (org-export-expand-include-keyword)
277 (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
278 ;; Inclusion within an example block.
279 (org-test-with-temp-text
280 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
281 org-test-dir)
282 (org-export-expand-include-keyword)
283 (should
284 (equal
285 (buffer-string)
286 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
287 ;; Inclusion within a src-block.
288 (org-test-with-temp-text
289 (format
290 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
291 org-test-dir)
292 (org-export-expand-include-keyword)
293 (should (equal (buffer-string)
294 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
296 (ert-deftest test-org-export/user-ignore-list ()
297 "Test if `:ignore-list' accepts user input."
298 (org-test-with-backend "test"
299 (flet ((skip-note-head
300 (data backend info)
301 ;; Ignore headlines with the word "note" in their title.
302 (org-element-map
303 data 'headline
304 (lambda (headline)
305 (when (string-match "\\<note\\>"
306 (org-element-property :raw-value headline))
307 (org-export-ignore-element headline info)))
308 info)
309 data))
310 ;; Install function in parse tree filters.
311 (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
312 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
313 (should (equal (org-export-as 'test) "* Head1\n")))))))
315 (ert-deftest test-org-export/footnotes ()
316 "Test footnotes specifications."
317 (let ((org-footnote-section nil))
318 ;; 1. Read every type of footnote.
319 (org-test-with-temp-text
320 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
321 (let* ((tree (org-element-parse-buffer))
322 (info (org-combine-plists
323 (org-export-initial-options) '(:with-footnotes t))))
324 (setq info (org-combine-plists
325 info (org-export-collect-tree-properties tree info 'test)))
326 (should
327 (equal
328 '((1 . "A") (2 . "B") (3 . "C") (4 . "D"))
329 (org-element-map
330 tree 'footnote-reference
331 (lambda (ref)
332 (let ((def (org-export-get-footnote-definition ref info)))
333 (cons (org-export-get-footnote-number ref info)
334 (if (eq (org-element-property :type ref) 'inline) (car def)
335 (car (org-element-contents
336 (car (org-element-contents def))))))))
337 info)))))
338 ;; 2. Test nested footnotes order.
339 (org-test-with-temp-text
340 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
341 (let* ((tree (org-element-parse-buffer))
342 (info (org-combine-plists
343 (org-export-initial-options) '(:with-footnotes t))))
344 (setq info (org-combine-plists
345 info (org-export-collect-tree-properties tree info 'test)))
346 (should
347 (equal
348 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
349 (org-element-map
350 tree 'footnote-reference
351 (lambda (ref)
352 (when (org-export-footnote-first-reference-p ref info)
353 (cons (org-export-get-footnote-number ref info)
354 (org-element-property :label ref))))
355 info)))))
356 ;; 3. Test nested footnote in invisible definitions.
357 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
358 ;; Hide definitions.
359 (narrow-to-region (point) (point-at-eol))
360 (let* ((tree (org-element-parse-buffer))
361 (info (org-combine-plists
362 (org-export-initial-options) '(:with-footnotes t))))
363 (setq info (org-combine-plists
364 info (org-export-collect-tree-properties tree info 'test)))
365 ;; Both footnotes should be seen.
366 (should
367 (= (length (org-export-collect-footnote-definitions tree info)) 2))))))