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