org-export: Fix expansion of babel calls in included files
[org-mode.git] / testing / lisp / test-org-export.el
blob74a580db032a62b369e57f92bf732db47109b0f7
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"))
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
24 syntax."
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)
30 transcode-table)
31 (push
32 (cons type
33 (lambda (obj contents info)
34 (funcall
35 (intern (format "org-element-%s-interpreter" type))
36 obj contents)))
37 transcode-table)))))
38 (progn ,@body)))
40 (defmacro org-test-with-parsed-data (data &rest body)
41 "Execute body with parsed data available.
43 DATA is a string containing the data to be parsed. BODY is the
44 body to execute. Parse tree is available under the `tree'
45 variable, and communication channel under `info'.
47 This function calls `org-export-collect-tree-properties'. As
48 such, `:ignore-list' (for `org-element-map') and
49 `:parse-tree' (for `org-export-get-genealogy') properties are
50 already filled in `info'."
51 (declare (debug (form body)) (indent 1))
52 `(org-test-with-temp-text ,data
53 (let* ((tree (org-element-parse-buffer))
54 (info (org-export-collect-tree-properties
55 tree (org-export-get-environment))))
56 ,@body)))
60 ;;; Internal Tests
62 (ert-deftest test-org-export/bind-keyword ()
63 "Test reading #+BIND: keywords."
64 ;; Test with `org-export-all-BIND' set to t.
65 (should
66 (org-test-with-temp-text "#+BIND: variable value"
67 (let ((org-export-allow-BIND t))
68 (org-export--install-letbind-maybe)
69 (eq variable 'value))))
70 ;; Test with `org-export-all-BIND' set to nil.
71 (should-not
72 (org-test-with-temp-text "#+BIND: variable value"
73 (let ((org-export-allow-BIND nil))
74 (org-export--install-letbind-maybe)
75 (boundp 'variable))))
76 ;; Test with `org-export-all-BIND' set to 'confirm and
77 ;; `org-export--allow-BIND-local' to t .
78 (should
79 (org-test-with-temp-text "#+BIND: variable value"
80 (let ((org-export-allow-BIND 'confirm))
81 (org-set-local 'org-export--allow-BIND-local t)
82 (org-export--install-letbind-maybe)
83 (eq variable 'value))))
84 ;; Test with `org-export-all-BIND' set to 'confirm and
85 ;; `org-export--allow-BIND-local' to nil.
86 (should-not
87 (org-test-with-temp-text "#+BIND: variable value"
88 (let ((org-export-allow-BIND 'confirm))
89 (org-set-local 'org-export--allow-BIND-local nil)
90 (org-export--install-letbind-maybe)
91 (boundp 'variable))))
92 ;; BIND keywords are case-insensitive.
93 (should
94 (org-test-with-temp-text "#+bind: variable value"
95 (let ((org-export-allow-BIND t))
96 (org-export--install-letbind-maybe)
97 (eq variable 'value)))))
99 (ert-deftest test-org-export/parse-option-keyword ()
100 "Test reading all standard #+OPTIONS: items."
101 (should
102 (equal
103 (org-export--parse-option-keyword
104 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
105 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
106 stat:t")
107 '(:headline-levels
108 1 :preserve-breaks t :section-numbers t :time-stamp-file t
109 :with-archived-trees t :with-author t :with-creator t :with-drawers t
110 :with-email t :with-emphasize t :with-entities t :with-fixed-width t
111 :with-footnotes t :with-inlinetasks nil :with-priority t
112 :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
113 :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
114 :with-todo-keywords t)))
115 ;; Test some special values.
116 (should
117 (equal
118 (org-export--parse-option-keyword
119 "arch:headline creator:comment d:(\"TEST\")
120 ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
121 '( :section-numbers
123 :with-archived-trees headline :with-creator comment
124 :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
125 :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
127 (ert-deftest test-org-export/get-inbuffer-options ()
128 "Test reading all standard export keywords."
129 (should
130 (equal
131 (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
132 #+CREATOR: Idem
133 #+DATE: Today
134 #+DESCRIPTION: Testing
135 #+DESCRIPTION: with two lines
136 #+EMAIL: some@email.org
137 #+EXCLUDE_TAGS: noexport invisible
138 #+KEYWORDS: test
139 #+LANGUAGE: en
140 #+SELECT_TAGS: export
141 #+TITLE: Some title
142 #+TITLE: with spaces"
143 (org-export--get-inbuffer-options))
144 '(:author
145 ("Me, Myself and I") :creator "Idem" :date ("Today")
146 :description "Testing\nwith two lines" :email "some@email.org"
147 :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
148 :select-tags ("export") :title ("Some title with spaces")))))
150 (ert-deftest test-org-export/get-subtree-options ()
151 "Test setting options from headline's properties."
152 ;; EXPORT_TITLE.
153 (org-test-with-temp-text "#+TITLE: Title
154 * Headline
155 :PROPERTIES:
156 :EXPORT_TITLE: Subtree Title
157 :END:
158 Paragraph"
159 (forward-line)
160 (should (equal (plist-get (org-export-get-environment nil t) :title)
161 '("Subtree Title"))))
162 :title
163 '("subtree-title")
164 ;; EXPORT_OPTIONS.
165 (org-test-with-temp-text "#+OPTIONS: H:1
166 * Headline
167 :PROPERTIES:
168 :EXPORT_OPTIONS: H:2
169 :END:
170 Paragraph"
171 (forward-line)
172 (should
173 (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
174 ;; EXPORT_DATE.
175 (org-test-with-temp-text "#+DATE: today
176 * Headline
177 :PROPERTIES:
178 :EXPORT_DATE: 29-03-2012
179 :END:
180 Paragraph"
181 (forward-line)
182 (should (equal (plist-get (org-export-get-environment nil t) :date)
183 '("29-03-2012"))))
184 ;; Export properties are case-insensitive.
185 (org-test-with-temp-text "* Headline
186 :PROPERTIES:
187 :EXPORT_Date: 29-03-2012
188 :END:
189 Paragraph"
190 (should (equal (plist-get (org-export-get-environment nil t) :date)
191 '("29-03-2012")))))
193 (ert-deftest test-org-export/handle-options ()
194 "Test if export options have an impact on output."
195 ;; Test exclude tags.
196 (org-test-with-temp-text "* Head1 :noexport:"
197 (org-test-with-backend test
198 (should
199 (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
200 ""))))
201 ;; Test include tags.
202 (org-test-with-temp-text "
203 * Head1
204 * Head2
205 ** Sub-Head2.1 :export:
206 *** Sub-Head2.1.1
207 * Head2"
208 (org-test-with-backend test
209 (should
210 (equal
211 "* Head2\n** Sub-Head2.1 :export:\n*** Sub-Head2.1.1\n"
212 (let ((org-tags-column 0))
213 (org-export-as 'test nil nil nil '(:select-tags ("export"))))))))
214 ;; Test mixing include tags and exclude tags.
215 (org-test-with-temp-text "
216 * Head1 :export:
217 ** Sub-Head1 :noexport:
218 ** Sub-Head2
219 * Head2 :noexport:
220 ** Sub-Head1 :export:"
221 (org-test-with-backend test
222 (should
223 (string-match
224 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
225 (org-export-as
226 'test nil nil nil
227 '(:select-tags ("export") :exclude-tags ("noexport")))))))
228 ;; Ignore tasks.
229 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
230 (org-test-with-temp-text "* TODO Head1"
231 (org-test-with-backend test
232 (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
233 "")))))
234 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
235 (org-test-with-temp-text "* TODO Head1"
236 (org-test-with-backend test
237 (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
238 "* TODO Head1\n")))))
239 ;; Archived tree.
240 (org-test-with-temp-text "* Head1 :archive:"
241 (let ((org-archive-tag "archive"))
242 (org-test-with-backend test
243 (should
244 (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
245 "")))))
246 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
247 (let ((org-archive-tag "archive"))
248 (org-test-with-backend test
249 (should
250 (string-match
251 "\\* Head1[ \t]+:archive:"
252 (org-export-as 'test nil nil nil
253 '(:with-archived-trees headline)))))))
254 (org-test-with-temp-text "* Head1 :archive:"
255 (let ((org-archive-tag "archive"))
256 (org-test-with-backend test
257 (should
258 (string-match
259 "\\`\\* Head1[ \t]+:archive:\n\\'"
260 (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
261 ;; Drawers.
262 (let ((org-drawers '("TEST")))
263 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
264 (org-test-with-backend test
265 (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
266 ""))
267 (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
268 ":TEST:\ncontents\n:END:\n")))))
269 (let ((org-drawers '("FOO" "BAR")))
270 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
271 (org-test-with-backend test
272 (should
273 (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
274 ":FOO:\nkeep\n:END:\n")))))
275 ;; Timestamps.
276 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
277 (org-test-with-backend test
278 (should
279 (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
280 "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
281 (should
282 (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
283 (should
284 (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
285 "<2012-04-29 sun. 10:45>\n"))
286 (should
287 (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
288 "[2012-04-29 sun. 10:45]\n"))))
289 ;; Clocks.
290 (let ((org-clock-string "CLOCK:"))
291 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
292 (org-test-with-backend test
293 (should
294 (equal (org-export-as 'test nil nil nil '(:with-clocks t))
295 "CLOCK: [2012-04-29 sun. 10:45]\n"))
296 (should
297 (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
298 ;; Plannings.
299 (let ((org-closed-string "CLOSED:"))
300 (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
301 (org-test-with-backend test
302 (should
303 (equal (org-export-as 'test nil nil nil '(:with-plannings t))
304 "CLOSED: [2012-04-29 sun. 10:45]\n"))
305 (should
306 (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
307 "")))))
308 ;; Inlinetasks.
309 (when (featurep 'org-inlinetask)
310 (should
311 (equal
312 (let ((org-inlinetask-min-level 15))
313 (org-test-with-temp-text "*************** Task"
314 (org-test-with-backend test
315 (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
316 ""))
317 (should
318 (equal
319 (let ((org-inlinetask-min-level 15))
320 (org-test-with-temp-text
321 "*************** Task\nContents\n*************** END"
322 (org-test-with-backend test
323 (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
324 "")))
325 ;; Statistics cookies.
326 (should
327 (equal ""
328 (org-test-with-temp-text "[0/0]"
329 (org-test-with-backend test
330 (org-export-as
331 'test nil nil nil '(:with-statistics-cookies nil)))))))
333 (ert-deftest test-org-export/comment-tree ()
334 "Test if export process ignores commented trees."
335 (let ((org-comment-string "COMMENT"))
336 (org-test-with-temp-text "* COMMENT Head1"
337 (org-test-with-backend test
338 (should (equal (org-export-as 'test) ""))))))
340 (ert-deftest test-org-export/export-scope ()
341 "Test all export scopes."
342 (org-test-with-temp-text "
343 * Head1
344 ** Head2
345 text
346 *** Head3"
347 (org-test-with-backend test
348 ;; Subtree.
349 (forward-line 3)
350 (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
351 ;; Visible.
352 (goto-char (point-min))
353 (forward-line)
354 (org-cycle)
355 (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
356 ;; Body only.
357 (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
358 (push '(template . org-test-template) org-test-translate-alist)
359 (should (equal (org-export-as 'test nil nil 'body-only)
360 "* Head1\n** Head2\ntext\n*** Head3\n"))
361 (should (equal (org-export-as 'test)
362 "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
363 ;; Region.
364 (goto-char (point-min))
365 (forward-line 3)
366 (transient-mark-mode 1)
367 (push-mark (point) t t)
368 (goto-char (point-at-eol))
369 (should (equal (org-export-as 'test) "text\n"))))
370 ;; Subtree with a code block calling another block outside.
371 (should
372 (equal ": 3\n"
373 (org-test-with-temp-text "
374 * Head1
375 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
376 <<test>>
377 #+END_SRC
378 * Head2
379 #+NAME: test
380 #+BEGIN_SRC emacs-lisp
381 \(+ 1 2)
382 #+END_SRC"
383 (org-test-with-backend test
384 (forward-line 1)
385 (org-export-as 'test 'subtree))))))
387 (ert-deftest test-org-export/expand-include ()
388 "Test file inclusion in an Org buffer."
389 ;; Full insertion with recursive inclusion.
390 (org-test-with-temp-text
391 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
392 (org-export-expand-include-keyword)
393 (should (equal (buffer-string)
394 "Small Org file with an include keyword.
396 #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
398 Success!
400 * Heading
401 body\n")))
402 ;; Localized insertion.
403 (org-test-with-temp-text
404 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
405 org-test-dir)
406 (org-export-expand-include-keyword)
407 (should (equal (buffer-string)
408 "Small Org file with an include keyword.\n")))
409 ;; Insertion with constraints on headlines level.
410 (org-test-with-temp-text
411 (format
412 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
413 org-test-dir)
414 (org-export-expand-include-keyword)
415 (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
416 ;; Inclusion within an example block.
417 (org-test-with-temp-text
418 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
419 org-test-dir)
420 (org-export-expand-include-keyword)
421 (should
422 (equal
423 (buffer-string)
424 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
425 ;; Inclusion within a src-block.
426 (org-test-with-temp-text
427 (format
428 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
429 org-test-dir)
430 (org-export-expand-include-keyword)
431 (should (equal (buffer-string)
432 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
434 (ert-deftest test-org-export/expand-macro ()
435 "Test macro expansion in an Org buffer."
436 ;; Standard macro expansion.
437 (should
438 (equal "#+MACRO: macro1 value\nvalue"
439 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
440 (let (info)
441 (org-export-expand-macro info) (buffer-string)))))
442 ;; Export specific macros.
443 (should
444 (equal "me 2012-03-29 me@here Title"
445 (org-test-with-temp-text
447 #+TITLE: Title
448 #+DATE: 2012-03-29
449 #+AUTHOR: me
450 #+EMAIL: me@here
451 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
452 (let ((info (org-export-get-environment)))
453 (org-export-expand-macro info)
454 (goto-char (point-max))
455 (buffer-substring (line-beginning-position)
456 (line-end-position))))))
457 ;; Expand macros with templates in included files.
458 (should
459 (equal "success"
460 (org-test-with-temp-text
461 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
462 {{{included-macro}}}" org-test-dir)
463 (let (info)
464 (org-export-expand-include-keyword)
465 (org-export-expand-macro info)
466 (goto-char (point-max))
467 (buffer-substring (line-beginning-position)
468 (line-end-position)))))))
470 (ert-deftest test-org-export/user-ignore-list ()
471 "Test if `:ignore-list' accepts user input."
472 (org-test-with-backend test
473 (flet ((skip-note-head
474 (data backend info)
475 ;; Ignore headlines with the word "note" in their title.
476 (org-element-map
477 data 'headline
478 (lambda (headline)
479 (when (string-match "\\<note\\>"
480 (org-element-property :raw-value headline))
481 (org-export-ignore-element headline info)))
482 info)
483 data))
484 ;; Install function in parse tree filters.
485 (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
486 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
487 (should (equal (org-export-as 'test) "* Head1\n")))))))
489 (ert-deftest test-org-export/before-parsing-hook ()
490 "Test `org-export-before-parsing-hook'."
491 (org-test-with-backend test
492 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
493 (let ((org-export-before-parsing-hook
494 '((lambda (backend)
495 (org-map-entries
496 (lambda ()
497 (delete-region (point) (progn (forward-line) (point)))))))))
498 (should (equal (org-export-as 'test) "Body 1\nBody 2\n"))))))
502 ;;; Affiliated Keywords
504 (ert-deftest test-org-export/read-attribute ()
505 "Test `org-export-read-attribute' specifications."
506 ;; Standard test.
507 (should
508 (equal
509 (org-export-read-attribute
510 :attr_html
511 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
512 (org-element-at-point)))
513 '(:a 1 :b 2)))
514 ;; Return nil on empty attribute.
515 (should-not
516 (org-export-read-attribute
517 :attr_html
518 (org-test-with-temp-text "Paragraph" (org-element-at-point)))))
520 (ert-deftest test-org-export/get-caption ()
521 "Test `org-export-get-caption' specifications."
522 ;; Without optional argument, return long caption
523 (should
524 (equal
525 '("l")
526 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
527 (org-export-get-caption (org-element-at-point)))))
528 ;; With optional argument, return short caption.
529 (should
530 (equal
531 '("s")
532 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
533 (org-export-get-caption (org-element-at-point) t))))
534 ;; Multiple lines are separated by white spaces.
535 (should
536 (equal
537 '("a" " " "b")
538 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
539 (org-export-get-caption (org-element-at-point))))))
543 ;;; Export Snippets
545 (ert-deftest test-org-export/export-snippet ()
546 "Test export snippets transcoding."
547 (org-test-with-temp-text "@@test:A@@@@t:B@@"
548 (org-test-with-backend test
549 (let ((org-test-translate-alist
550 (cons (cons 'export-snippet
551 (lambda (snippet contents info)
552 (when (eq (org-export-snippet-backend snippet) 'test)
553 (org-element-property :value snippet))))
554 org-test-translate-alist)))
555 (let ((org-export-snippet-translation-alist nil))
556 (should (equal (org-export-as 'test) "A\n")))
557 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
558 (should (equal (org-export-as 'test) "AB\n")))))))
562 ;;; Footnotes
564 (ert-deftest test-org-export/footnotes ()
565 "Test footnotes specifications."
566 (let ((org-footnote-section nil)
567 (org-export-with-footnotes t))
568 ;; 1. Read every type of footnote.
569 (should
570 (equal
571 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
572 (org-test-with-parsed-data
573 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
574 (org-element-map
575 tree 'footnote-reference
576 (lambda (ref)
577 (let ((def (org-export-get-footnote-definition ref info)))
578 (cons (org-export-get-footnote-number ref info)
579 (if (eq (org-element-property :type ref) 'inline) (car def)
580 (car (org-element-contents
581 (car (org-element-contents def))))))))
582 info))))
583 ;; 2. Test nested footnotes order.
584 (org-test-with-parsed-data
585 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
586 (should
587 (equal
588 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
589 (org-element-map
590 tree 'footnote-reference
591 (lambda (ref)
592 (when (org-export-footnote-first-reference-p ref info)
593 (cons (org-export-get-footnote-number ref info)
594 (org-element-property :label ref))))
595 info))))
596 ;; 3. Test nested footnote in invisible definitions.
597 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
598 ;; Hide definitions.
599 (narrow-to-region (point) (point-at-eol))
600 (let* ((tree (org-element-parse-buffer))
601 (info (org-combine-plists
602 `(:parse-tree ,tree)
603 (org-export-collect-tree-properties
604 tree (org-export-get-environment)))))
605 ;; Both footnotes should be seen.
606 (should
607 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
608 ;; 4. Test footnotes definitions collection.
609 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
611 \[fn:2] B [fn:3] [fn::D].
613 \[fn:3] C."
614 (should (= (length (org-export-collect-footnote-definitions tree info))
615 4)))
616 ;; 5. Test export of footnotes defined outside parsing scope.
617 (org-test-with-temp-text "[fn:1] Out of scope
618 * Title
619 Paragraph[fn:1]"
620 (org-test-with-backend test
621 (let ((org-test-translate-alist
622 (cons (cons 'footnote-reference
623 (lambda (fn contents info)
624 (org-element-interpret-data
625 (org-export-get-footnote-definition fn info))))
626 org-test-translate-alist)))
627 (forward-line)
628 (should (equal "ParagraphOut of scope\n"
629 (org-export-as 'test 'subtree))))))))
633 ;;; Headlines and Inlinetasks
635 (ert-deftest test-org-export/get-relative-level ()
636 "Test `org-export-get-relative-level' specifications."
637 ;; Standard test.
638 (should
639 (equal '(1 2)
640 (let ((org-odd-levels-only nil))
641 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
642 (org-element-map
643 tree 'headline
644 (lambda (h) (org-export-get-relative-level h info))
645 info)))))
646 ;; Missing levels
647 (should
648 (equal '(1 3)
649 (let ((org-odd-levels-only nil))
650 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
651 (org-element-map
652 tree 'headline
653 (lambda (h) (org-export-get-relative-level h info))
654 info))))))
656 (ert-deftest test-org-export/low-level-p ()
657 "Test `org-export-low-level-p' specifications."
658 (should
659 (equal
660 '(no yes)
661 (let ((org-odd-levels-only nil))
662 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
663 (org-element-map
664 tree 'headline
665 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
666 (plist-put info :headline-levels 1)))))))
668 (ert-deftest test-org-export/get-headline-number ()
669 "Test `org-export-get-headline-number' specifications."
670 ;; Standard test.
671 (should
672 (equal
673 '((1) (1 1))
674 (let ((org-odd-levels-only nil))
675 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
676 (org-element-map
677 tree 'headline
678 (lambda (h) (org-export-get-headline-number h info))
679 info)))))
680 ;; Missing levels are replaced with 0.
681 (should
682 (equal
683 '((1) (1 0 1))
684 (let ((org-odd-levels-only nil))
685 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
686 (org-element-map
687 tree 'headline
688 (lambda (h) (org-export-get-headline-number h info))
689 info))))))
691 (ert-deftest test-org-export/numbered-headline-p ()
692 "Test `org-export-numbered-headline-p' specifications."
693 ;; If `:section-numbers' is nil, never number headlines.
694 (should-not
695 (org-test-with-parsed-data "* Headline"
696 (org-element-map
697 tree 'headline
698 (lambda (h) (org-export-numbered-headline-p h info))
699 (plist-put info :section-numbers nil))))
700 ;; If `:section-numbers' is a number, only number headlines with
701 ;; a level greater that it.
702 (should
703 (equal
704 '(yes no)
705 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
706 (org-element-map
707 tree 'headline
708 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
709 (plist-put info :section-numbers 1)))))
710 ;; Otherwise, headlines are always numbered.
711 (should
712 (org-test-with-parsed-data "* Headline"
713 (org-element-map
714 tree 'headline
715 (lambda (h) (org-export-numbered-headline-p h info))
716 (plist-put info :section-numbers t)))))
718 (ert-deftest test-org-export/number-to-roman ()
719 "Test `org-export-number-to-roman' specifications."
720 ;; If number is negative, return it as a string.
721 (should (equal (org-export-number-to-roman -1) "-1"))
722 ;; Otherwise, return it as a roman number.
723 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
725 (ert-deftest test-org-export/get-tags ()
726 "Test `org-export-get-tags' specifications."
727 (let ((org-export-exclude-tags '("noexport"))
728 (org-export-select-tags '("export")))
729 ;; Standard test: tags which are not a select tag, an exclude tag,
730 ;; or specified as optional argument shouldn't be ignored.
731 (should
732 (org-test-with-parsed-data "* Headline :tag:"
733 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
734 info)))
735 ;; Exclude tags are removed.
736 (should-not
737 (org-test-with-parsed-data "* Headline :noexport:"
738 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
739 info)))
740 ;; Select tags are removed.
741 (should-not
742 (org-test-with-parsed-data "* Headline :export:"
743 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
744 info)))
745 (should
746 (equal
747 '("tag")
748 (org-test-with-parsed-data "* Headline :tag:export:"
749 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
750 info))))
751 ;; Tags provided in the optional argument are also ignored.
752 (should-not
753 (org-test-with-parsed-data "* Headline :ignore:"
754 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
755 info '("ignore"))))
756 ;; Allow tag inheritance.
757 (should
758 (equal
759 '(("tag") ("tag"))
760 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
761 (org-element-map
762 tree 'headline
763 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
765 (ert-deftest test-org-export/get-node-property ()
766 "Test`org-export-get-node-property' specifications."
767 ;; Standard test.
768 (should
769 (equal "value"
770 (org-test-with-parsed-data "* Headline
771 :PROPERTIES:
772 :prop: value
773 :END:"
774 (org-export-get-node-property
775 :prop (org-element-map tree 'headline 'identity nil t)))))
776 ;; Test inheritance.
777 (should
778 (equal "value"
779 (org-test-with-parsed-data "* Parent
780 :PROPERTIES:
781 :prop: value
782 :END:
783 ** Headline
784 Paragraph"
785 (org-export-get-node-property
786 :prop (org-element-map tree 'paragraph 'identity nil t) t))))
787 ;; Cannot return a value before the first headline.
788 (should-not
789 (org-test-with-parsed-data "Paragraph
790 * Headline
791 :PROPERTIES:
792 :prop: value
793 :END:"
794 (org-export-get-node-property
795 :prop (org-element-map tree 'paragraph 'identity nil t)))))
797 (ert-deftest test-org-export/first-sibling-p ()
798 "Test `org-export-first-sibling-p' specifications."
799 ;; Standard test.
800 (should
801 (equal
802 '(yes yes no)
803 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
804 (org-element-map
805 tree 'headline
806 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
807 info))))
808 ;; Ignore headlines not exported.
809 (should
810 (equal
811 '(yes)
812 (let ((org-export-exclude-tags '("ignore")))
813 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
814 (org-element-map
815 tree 'headline
816 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
817 info))))))
819 (ert-deftest test-org-export/last-sibling-p ()
820 "Test `org-export-last-sibling-p' specifications."
821 ;; Standard test.
822 (should
823 (equal
824 '(yes no yes)
825 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
826 (org-element-map
827 tree 'headline
828 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
829 info))))
830 ;; Ignore headlines not exported.
831 (should
832 (equal
833 '(yes)
834 (let ((org-export-exclude-tags '("ignore")))
835 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
836 (org-element-map
837 tree 'headline
838 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
839 info))))))
843 ;;; Links
845 (ert-deftest test-org-export/get-coderef-format ()
846 "Test `org-export-get-coderef-format' specifications."
847 ;; A link without description returns "%s"
848 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
849 "%s"))
850 ;; Return "%s" when path is matched within description.
851 (should (equal (org-export-get-coderef-format "path" "desc (path)")
852 "desc %s"))
853 ;; Otherwise return description.
854 (should (equal (org-export-get-coderef-format "path" "desc")
855 "desc")))
857 (ert-deftest test-org-export/inline-image-p ()
858 "Test `org-export-inline-image-p' specifications."
859 (should
860 (org-export-inline-image-p
861 (org-test-with-temp-text "[[#id]]"
862 (org-element-map
863 (org-element-parse-buffer) 'link 'identity nil t))
864 '(("custom-id" . "id")))))
866 (ert-deftest test-org-export/fuzzy-link ()
867 "Test fuzzy links specifications."
868 ;; 1. Links to invisible (keyword) targets should be ignored.
869 (org-test-with-parsed-data
870 "Paragraph.\n#+TARGET: Test\n[[Test]]"
871 (should-not
872 (org-element-map
873 tree 'link
874 (lambda (link)
875 (org-export-get-ordinal
876 (org-export-resolve-fuzzy-link link info) info)) info)))
877 ;; 2. Link to an headline should return headline's number.
878 (org-test-with-parsed-data
879 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
880 (should
881 ;; Note: Headline's number is in fact a list of numbers.
882 (equal '(2)
883 (org-element-map
884 tree 'link
885 (lambda (link)
886 (org-export-get-ordinal
887 (org-export-resolve-fuzzy-link link info) info)) info t))))
888 ;; 3. Link to a target in an item should return item's number.
889 (org-test-with-parsed-data
890 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
891 (should
892 ;; Note: Item's number is in fact a list of numbers.
893 (equal '(1 2)
894 (org-element-map
895 tree 'link
896 (lambda (link)
897 (org-export-get-ordinal
898 (org-export-resolve-fuzzy-link link info) info)) info t))))
899 ;; 4. Link to a target in a footnote should return footnote's
900 ;; number.
901 (org-test-with-parsed-data "
902 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
903 (should
904 (equal '(2 3)
905 (org-element-map
906 tree 'link
907 (lambda (link)
908 (org-export-get-ordinal
909 (org-export-resolve-fuzzy-link link info) info)) info))))
910 ;; 5. Link to a named element should return sequence number of that
911 ;; element.
912 (org-test-with-parsed-data
913 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
914 (should
915 (= 2
916 (org-element-map
917 tree 'link
918 (lambda (link)
919 (org-export-get-ordinal
920 (org-export-resolve-fuzzy-link link info) info)) info t))))
921 ;; 6. Link to a target not within an item, a table, a footnote
922 ;; reference or definition should return section number.
923 (org-test-with-parsed-data
924 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
925 (should
926 (equal '(2)
927 (org-element-map
928 tree 'link
929 (lambda (link)
930 (org-export-get-ordinal
931 (org-export-resolve-fuzzy-link link info) info)) info t)))))
933 (ert-deftest test-org-export/resolve-coderef ()
934 "Test `org-export-resolve-coderef' specifications."
935 (let ((org-coderef-label-format "(ref:%s)"))
936 ;; 1. A link to a "-n -k -r" block returns line number.
937 (org-test-with-parsed-data
938 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
939 (should (= (org-export-resolve-coderef "coderef" info) 1)))
940 (org-test-with-parsed-data
941 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
942 (should (= (org-export-resolve-coderef "coderef" info) 1)))
943 ;; 2. A link to a "-n -r" block returns line number.
944 (org-test-with-parsed-data
945 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
946 (should (= (org-export-resolve-coderef "coderef" info) 1)))
947 (org-test-with-parsed-data
948 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
949 (should (= (org-export-resolve-coderef "coderef" info) 1)))
950 ;; 3. A link to a "-n" block returns coderef.
951 (org-test-with-parsed-data
952 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
953 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
954 (org-test-with-parsed-data
955 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
956 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
957 ;; 4. A link to a "-r" block returns line number.
958 (org-test-with-parsed-data
959 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
960 (should (= (org-export-resolve-coderef "coderef" info) 1)))
961 (org-test-with-parsed-data
962 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
963 (should (= (org-export-resolve-coderef "coderef" info) 1)))
964 ;; 5. A link to a block without a switch returns coderef.
965 (org-test-with-parsed-data
966 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
967 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
968 (org-test-with-parsed-data
969 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
970 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
971 ;; 6. Correctly handle continued line numbers. A "+n" switch
972 ;; should resume numbering from previous block with numbered
973 ;; lines, ignoring blocks not numbering lines in the process.
974 ;; A "-n" switch resets count.
975 (org-test-with-parsed-data "
976 #+BEGIN_EXAMPLE -n
977 Text.
978 #+END_EXAMPLE
980 #+BEGIN_SRC emacs-lisp
981 \(- 1 1)
982 #+END_SRC
984 #+BEGIN_SRC emacs-lisp +n -r
985 \(+ 1 1) (ref:addition)
986 #+END_SRC
988 #+BEGIN_EXAMPLE -n -r
989 Another text. (ref:text)
990 #+END_EXAMPLE"
991 (should (= (org-export-resolve-coderef "addition" info) 2))
992 (should (= (org-export-resolve-coderef "text" info) 1)))
993 ;; 7. Recognize coderef with user-specified syntax.
994 (org-test-with-parsed-data
995 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
996 (should (equal (org-export-resolve-coderef "text" info) "text")))))
998 (ert-deftest test-org-export/resolve-fuzzy-link ()
999 "Test `org-export-resolve-fuzzy-link' specifications."
1000 ;; 1. Match target objects.
1001 (org-test-with-parsed-data "<<target>> [[target]]"
1002 (should
1003 (org-export-resolve-fuzzy-link
1004 (org-element-map tree 'link 'identity info t) info)))
1005 ;; 2. Match target elements.
1006 (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
1007 (should
1008 (org-export-resolve-fuzzy-link
1009 (org-element-map tree 'link 'identity info t) info)))
1010 ;; 3. Match named elements.
1011 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
1012 (should
1013 (org-export-resolve-fuzzy-link
1014 (org-element-map tree 'link 'identity info t) info)))
1015 ;; 4. Match exact headline's name.
1016 (org-test-with-parsed-data "* My headline\n[[My headline]]"
1017 (should
1018 (org-export-resolve-fuzzy-link
1019 (org-element-map tree 'link 'identity info t) info)))
1020 ;; 5. Targets objects have priority over named elements and headline
1021 ;; titles.
1022 (org-test-with-parsed-data
1023 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
1024 (should
1025 (eq 'target
1026 (org-element-type
1027 (org-export-resolve-fuzzy-link
1028 (org-element-map tree 'link 'identity info t) info)))))
1029 ;; 6. Named elements have priority over headline titles.
1030 (org-test-with-parsed-data
1031 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
1032 (should
1033 (eq 'paragraph
1034 (org-element-type
1035 (org-export-resolve-fuzzy-link
1036 (org-element-map tree 'link 'identity info t) info)))))
1037 ;; 7. If link's path starts with a "*", only match headline titles,
1038 ;; though.
1039 (org-test-with-parsed-data
1040 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
1041 (should
1042 (eq 'headline
1043 (org-element-type
1044 (org-export-resolve-fuzzy-link
1045 (org-element-map tree 'link 'identity info t) info)))))
1046 ;; 8. Return nil if no match.
1047 (org-test-with-parsed-data "[[target]]"
1048 (should-not
1049 (org-export-resolve-fuzzy-link
1050 (org-element-map tree 'link 'identity info t) info))))
1052 (ert-deftest test-org-export/resolve-id-link ()
1053 "Test `org-export-resolve-id-link' specifications."
1054 ;; 1. Regular test for custom-id link.
1055 (org-test-with-parsed-data "* Headline1
1056 :PROPERTIES:
1057 :CUSTOM-ID: test
1058 :END:
1059 * Headline 2
1060 \[[#test]]"
1061 (should
1062 (org-export-resolve-id-link
1063 (org-element-map tree 'link 'identity info t) info)))
1064 ;; 2. Failing test for custom-id link.
1065 (org-test-with-parsed-data "* Headline1
1066 :PROPERTIES:
1067 :CUSTOM-ID: test
1068 :END:
1069 * Headline 2
1070 \[[#no-match]]"
1071 (should-not
1072 (org-export-resolve-id-link
1073 (org-element-map tree 'link 'identity info t) info)))
1074 ;; 3. Test for internal id target.
1075 (org-test-with-parsed-data "* Headline1
1076 :PROPERTIES:
1077 :ID: aaaa
1078 :END:
1079 * Headline 2
1080 \[[id:aaaa]]"
1081 (should
1082 (org-export-resolve-id-link
1083 (org-element-map tree 'link 'identity info t) info)))
1084 ;; 4. Test for external id target.
1085 (org-test-with-parsed-data "[[id:aaaa]]"
1086 (should
1087 (org-export-resolve-id-link
1088 (org-element-map tree 'link 'identity info t)
1089 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
1091 (ert-deftest test-org-export/resolve-radio-link ()
1092 "Test `org-export-resolve-radio-link' specifications."
1093 ;; Standard test.
1094 (org-test-with-temp-text "<<<radio>>> radio"
1095 (org-update-radio-target-regexp)
1096 (should
1097 (let* ((tree (org-element-parse-buffer))
1098 (info `(:parse-tree ,tree)))
1099 (org-export-resolve-radio-link
1100 (org-element-map tree 'link 'identity info t)
1101 info))))
1102 ;; Radio target with objects.
1103 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
1104 (org-update-radio-target-regexp)
1105 (should
1106 (let* ((tree (org-element-parse-buffer))
1107 (info `(:parse-tree ,tree)))
1108 (org-export-resolve-radio-link
1109 (org-element-map tree 'link 'identity info t)
1110 info)))))
1114 ;;; Src-block and example-block
1116 (ert-deftest test-org-export/unravel-code ()
1117 "Test `org-export-unravel-code' function."
1118 (let ((org-coderef-label-format "(ref:%s)"))
1119 ;; 1. Code without reference.
1120 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
1121 (should (equal (org-export-unravel-code (org-element-at-point))
1122 '("(+ 1 1)\n"))))
1123 ;; 2. Code with reference.
1124 (org-test-with-temp-text
1125 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
1126 (should (equal (org-export-unravel-code (org-element-at-point))
1127 '("(+ 1 1)\n" (1 . "test")))))
1128 ;; 3. Code with user-defined reference.
1129 (org-test-with-temp-text
1130 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
1131 (should (equal (org-export-unravel-code (org-element-at-point))
1132 '("(+ 1 1)\n" (1 . "test")))))
1133 ;; 4. Code references keys are relative to the current block.
1134 (org-test-with-temp-text "
1135 #+BEGIN_EXAMPLE -n
1136 \(+ 1 1)
1137 #+END_EXAMPLE
1138 #+BEGIN_EXAMPLE +n
1139 \(+ 2 2)
1140 \(+ 3 3) (ref:one)
1141 #+END_EXAMPLE"
1142 (goto-line 5)
1143 (should (equal (org-export-unravel-code (org-element-at-point))
1144 '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
1148 ;;; Smart Quotes
1150 (ert-deftest test-org-export/activate-smart-quotes ()
1151 "Test `org-export-activate-smart-quotes' specifications."
1152 ;; Opening double quotes: standard test.
1153 (should
1154 (equal
1155 '("some &ldquo;paragraph")
1156 (let ((org-export-default-language "en"))
1157 (org-test-with-parsed-data "some \"paragraph"
1158 (org-element-map
1159 tree 'plain-text
1160 (lambda (s) (org-export-activate-smart-quotes s :html info))
1161 info)))))
1162 ;; Opening quotes: at the beginning of a paragraph.
1163 (should
1164 (equal
1165 '("&ldquo;begin")
1166 (let ((org-export-default-language "en"))
1167 (org-test-with-parsed-data "\"begin"
1168 (org-element-map
1169 tree 'plain-text
1170 (lambda (s) (org-export-activate-smart-quotes s :html info))
1171 info)))))
1172 ;; Opening quotes: after an object.
1173 (should
1174 (equal
1175 '("&ldquo;begin")
1176 (let ((org-export-default-language "en"))
1177 (org-test-with-parsed-data "=verb= \"begin"
1178 (org-element-map
1179 tree 'plain-text
1180 (lambda (s) (org-export-activate-smart-quotes s :html info))
1181 info)))))
1182 ;; Closing quotes: standard test.
1183 (should
1184 (equal
1185 '("some&rdquo; paragraph")
1186 (let ((org-export-default-language "en"))
1187 (org-test-with-parsed-data "some\" paragraph"
1188 (org-element-map
1189 tree 'plain-text
1190 (lambda (s) (org-export-activate-smart-quotes s :html info))
1191 info)))))
1192 ;; Closing quotes: at the end of a paragraph.
1193 (should
1194 (equal
1195 '("end&rdquo;")
1196 (let ((org-export-default-language "en"))
1197 (org-test-with-parsed-data "end\""
1198 (org-element-map
1199 tree 'plain-text
1200 (lambda (s) (org-export-activate-smart-quotes s :html info))
1201 info)))))
1202 ;; Apostrophe: standard test.
1203 (should
1204 (equal
1205 '("It shouldn&rsquo;t fail")
1206 (let ((org-export-default-language "en"))
1207 (org-test-with-parsed-data "It shouldn't fail"
1208 (org-element-map
1209 tree 'plain-text
1210 (lambda (s) (org-export-activate-smart-quotes s :html info))
1211 info)))))
1212 ;; Apostrophe: before an object.
1213 (should
1214 (equal
1215 '("a&rsquo;")
1216 (let ((org-export-default-language "en"))
1217 (org-test-with-parsed-data "a'=b="
1218 (org-element-map
1219 tree 'plain-text
1220 (lambda (s) (org-export-activate-smart-quotes s :html info))
1221 info)))))
1222 ;; Apostrophe: after an object.
1223 (should
1224 (equal
1225 '("&rsquo;s")
1226 (let ((org-export-default-language "en"))
1227 (org-test-with-parsed-data "=code='s"
1228 (org-element-map
1229 tree 'plain-text
1230 (lambda (s) (org-export-activate-smart-quotes s :html info))
1231 info))))))
1235 ;;; Tables
1237 (ert-deftest test-org-export/special-column ()
1238 "Test if the table's special column is properly recognized."
1239 ;; 1. First column is special if it contains only a special marking
1240 ;; characters or empty cells.
1241 (org-test-with-temp-text "
1242 | ! | 1 |
1243 | | 2 |"
1244 (should
1245 (org-export-table-has-special-column-p
1246 (org-element-map
1247 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1248 ;; 2. If the column contains anything else, it isn't special.
1249 (org-test-with-temp-text "
1250 | ! | 1 |
1251 | b | 2 |"
1252 (should-not
1253 (org-export-table-has-special-column-p
1254 (org-element-map
1255 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1256 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
1257 ;; and "!".
1258 (org-test-with-temp-text "
1259 | # | 1 |
1260 | ^ | 2 |
1261 | * | 3 |
1262 | _ | 4 |
1263 | / | 5 |
1264 | $ | 6 |
1265 | ! | 7 |"
1266 (should
1267 (org-export-table-has-special-column-p
1268 (org-element-map
1269 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1270 ;; 4. A first column with only empty cells isn't considered as
1271 ;; special.
1272 (org-test-with-temp-text "
1273 | | 1 |
1274 | | 2 |"
1275 (should-not
1276 (org-export-table-has-special-column-p
1277 (org-element-map
1278 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
1280 (ert-deftest test-org-export/table-row-is-special-p ()
1281 "Test `org-export-table-row-is-special-p' specifications."
1282 ;; 1. A row is special if it has a special marking character in the
1283 ;; special column.
1284 (org-test-with-parsed-data "| ! | 1 |"
1285 (should
1286 (org-export-table-row-is-special-p
1287 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1288 ;; 2. A row is special when its first field is "/"
1289 (org-test-with-parsed-data "
1290 | / | 1 |
1291 | a | b |"
1292 (should
1293 (org-export-table-row-is-special-p
1294 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1295 ;; 3. A row only containing alignment cookies is also considered as
1296 ;; special.
1297 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
1298 (should
1299 (org-export-table-row-is-special-p
1300 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1301 ;; 4. Everything else isn't considered as special.
1302 (org-test-with-parsed-data "| \alpha | | c |"
1303 (should-not
1304 (org-export-table-row-is-special-p
1305 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1306 ;; 5. Table's rules are never considered as special rows.
1307 (org-test-with-parsed-data "|---+---|"
1308 (should-not
1309 (org-export-table-row-is-special-p
1310 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
1312 (ert-deftest test-org-export/has-header-p ()
1313 "Test `org-export-table-has-header-p' specifications."
1314 ;; 1. With an header.
1315 (org-test-with-parsed-data "
1316 | a | b |
1317 |---+---|
1318 | c | d |"
1319 (should
1320 (org-export-table-has-header-p
1321 (org-element-map tree 'table 'identity info 'first-match)
1322 info)))
1323 ;; 2. Without an header.
1324 (org-test-with-parsed-data "
1325 | a | b |
1326 | c | d |"
1327 (should-not
1328 (org-export-table-has-header-p
1329 (org-element-map tree 'table 'identity info 'first-match)
1330 info)))
1331 ;; 3. Don't get fooled with starting and ending rules.
1332 (org-test-with-parsed-data "
1333 |---+---|
1334 | a | b |
1335 | c | d |
1336 |---+---|"
1337 (should-not
1338 (org-export-table-has-header-p
1339 (org-element-map tree 'table 'identity info 'first-match)
1340 info))))
1342 (ert-deftest test-org-export/table-row-group ()
1343 "Test `org-export-table-row-group' specifications."
1344 ;; 1. A rule creates a new group.
1345 (org-test-with-parsed-data "
1346 | a | b |
1347 |---+---|
1348 | 1 | 2 |"
1349 (should
1350 (equal
1351 '(1 nil 2)
1352 (mapcar (lambda (row) (org-export-table-row-group row info))
1353 (org-element-map tree 'table-row 'identity)))))
1354 ;; 2. Special rows are ignored in count.
1355 (org-test-with-parsed-data "
1356 | / | < | > |
1357 |---|---+---|
1358 | | 1 | 2 |"
1359 (should
1360 (equal
1361 '(nil nil 1)
1362 (mapcar (lambda (row) (org-export-table-row-group row info))
1363 (org-element-map tree 'table-row 'identity)))))
1364 ;; 3. Double rules also are ignored in count.
1365 (org-test-with-parsed-data "
1366 | a | b |
1367 |---+---|
1368 |---+---|
1369 | 1 | 2 |"
1370 (should
1371 (equal
1372 '(1 nil nil 2)
1373 (mapcar (lambda (row) (org-export-table-row-group row info))
1374 (org-element-map tree 'table-row 'identity))))))
1376 (ert-deftest test-org-export/table-cell-width ()
1377 "Test `org-export-table-cell-width' specifications."
1378 ;; 1. Width is primarily determined by width cookies. If no cookie
1379 ;; is found, cell's width is nil.
1380 (org-test-with-parsed-data "
1381 | / | <l> | <6> | <l7> |
1382 | | a | b | c |"
1383 (should
1384 (equal
1385 '(nil 6 7)
1386 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1387 (org-element-map tree 'table-cell 'identity info)))))
1388 ;; 2. The last width cookie has precedence.
1389 (org-test-with-parsed-data "
1390 | <6> |
1391 | <7> |
1392 | a |"
1393 (should
1394 (equal
1395 '(7)
1396 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1397 (org-element-map tree 'table-cell 'identity info)))))
1398 ;; 3. Valid width cookies must have a specific row.
1399 (org-test-with-parsed-data "| <6> | cell |"
1400 (should
1401 (equal
1402 '(nil nil)
1403 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1404 (org-element-map tree 'table-cell 'identity))))))
1406 (ert-deftest test-org-export/table-cell-alignment ()
1407 "Test `org-export-table-cell-alignment' specifications."
1408 (let ((org-table-number-fraction 0.5)
1409 (org-table-number-regexp "^[0-9]+$"))
1410 ;; 1. Alignment is primarily determined by alignment cookies.
1411 (org-test-with-temp-text "| <l> | <c> | <r> |"
1412 (let* ((tree (org-element-parse-buffer))
1413 (info `(:parse-tree ,tree)))
1414 (should
1415 (equal
1416 '(left center right)
1417 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
1418 (org-element-map tree 'table-cell 'identity))))))
1419 ;; 2. The last alignment cookie has precedence.
1420 (org-test-with-parsed-data "
1421 | <l8> |
1422 | cell |
1423 | <r9> |"
1424 (should
1425 (equal
1426 '(right right right)
1427 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
1428 (org-element-map tree 'table-cell 'identity)))))
1429 ;; 3. If there's no cookie, cell's contents determine alignment.
1430 ;; A column mostly made of cells containing numbers will align
1431 ;; its cells to the right.
1432 (org-test-with-parsed-data "
1433 | 123 |
1434 | some text |
1435 | 12345 |"
1436 (should
1437 (equal
1438 '(right right right)
1439 (mapcar (lambda (cell)
1440 (org-export-table-cell-alignment cell info))
1441 (org-element-map tree 'table-cell 'identity)))))
1442 ;; 4. Otherwise, they will be aligned to the left.
1443 (org-test-with-parsed-data "
1444 | text |
1445 | some text |
1446 | \alpha |"
1447 (should
1448 (equal
1449 '(left left left)
1450 (mapcar (lambda (cell)
1451 (org-export-table-cell-alignment cell info))
1452 (org-element-map tree 'table-cell 'identity)))))))
1454 (ert-deftest test-org-export/table-cell-borders ()
1455 "Test `org-export-table-cell-borders' specifications."
1456 ;; 1. Recognize various column groups indicators.
1457 (org-test-with-parsed-data "| / | < | > | <> |"
1458 (should
1459 (equal
1460 '((right bottom top) (left bottom top) (right bottom top)
1461 (right left bottom top))
1462 (mapcar (lambda (cell)
1463 (org-export-table-cell-borders cell info))
1464 (org-element-map tree 'table-cell 'identity)))))
1465 ;; 2. Accept shortcuts to define column groups.
1466 (org-test-with-parsed-data "| / | < | < |"
1467 (should
1468 (equal
1469 '((right bottom top) (right left bottom top) (left bottom top))
1470 (mapcar (lambda (cell)
1471 (org-export-table-cell-borders cell info))
1472 (org-element-map tree 'table-cell 'identity)))))
1473 ;; 3. A valid column groups row must start with a "/".
1474 (org-test-with-parsed-data "
1475 | | < |
1476 | a | b |"
1477 (should
1478 (equal '((top) (top) (bottom) (bottom))
1479 (mapcar (lambda (cell)
1480 (org-export-table-cell-borders cell info))
1481 (org-element-map tree 'table-cell 'identity)))))
1482 ;; 4. Take table rules into consideration.
1483 (org-test-with-parsed-data "
1484 | 1 |
1485 |---|
1486 | 2 |"
1487 (should
1488 (equal '((below top) (bottom above))
1489 (mapcar (lambda (cell)
1490 (org-export-table-cell-borders cell info))
1491 (org-element-map tree 'table-cell 'identity)))))
1492 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
1493 ;; (resp. `bottom' and `below') borders. Any special row is
1494 ;; ignored.
1495 (org-test-with-parsed-data "
1496 |---+----|
1497 | / | |
1498 | | 1 |
1499 |---+----|"
1500 (should
1501 (equal '((bottom below top above))
1502 (last
1503 (mapcar (lambda (cell)
1504 (org-export-table-cell-borders cell info))
1505 (org-element-map tree 'table-cell 'identity)))))))
1507 (ert-deftest test-org-export/table-dimensions ()
1508 "Test `org-export-table-dimensions' specifications."
1509 ;; 1. Standard test.
1510 (org-test-with-parsed-data "
1511 | 1 | 2 | 3 |
1512 | 4 | 5 | 6 |"
1513 (should
1514 (equal '(2 . 3)
1515 (org-export-table-dimensions
1516 (org-element-map tree 'table 'identity info 'first-match) info))))
1517 ;; 2. Ignore horizontal rules and special columns.
1518 (org-test-with-parsed-data "
1519 | / | < | > |
1520 | 1 | 2 | 3 |
1521 |---+---+---|
1522 | 4 | 5 | 6 |"
1523 (should
1524 (equal '(2 . 3)
1525 (org-export-table-dimensions
1526 (org-element-map tree 'table 'identity info 'first-match) info)))))
1528 (ert-deftest test-org-export/table-cell-address ()
1529 "Test `org-export-table-cell-address' specifications."
1530 ;; 1. Standard test: index is 0-based.
1531 (org-test-with-parsed-data "| a | b |"
1532 (should
1533 (equal '((0 . 0) (0 . 1))
1534 (org-element-map
1535 tree 'table-cell
1536 (lambda (cell) (org-export-table-cell-address cell info))
1537 info))))
1538 ;; 2. Special column isn't counted, nor are special rows.
1539 (org-test-with-parsed-data "
1540 | / | <> |
1541 | | c |"
1542 (should
1543 (equal '(0 . 0)
1544 (org-export-table-cell-address
1545 (car (last (org-element-map tree 'table-cell 'identity info)))
1546 info))))
1547 ;; 3. Tables rules do not count either.
1548 (org-test-with-parsed-data "
1549 | a |
1550 |---|
1551 | b |
1552 |---|
1553 | c |"
1554 (should
1555 (equal '(2 . 0)
1556 (org-export-table-cell-address
1557 (car (last (org-element-map tree 'table-cell 'identity info)))
1558 info))))
1559 ;; 4. Return nil for special cells.
1560 (org-test-with-parsed-data "| / | a |"
1561 (should-not
1562 (org-export-table-cell-address
1563 (org-element-map tree 'table-cell 'identity nil 'first-match)
1564 info))))
1566 (ert-deftest test-org-export/get-table-cell-at ()
1567 "Test `org-export-get-table-cell-at' specifications."
1568 ;; 1. Address ignores special columns, special rows and rules.
1569 (org-test-with-parsed-data "
1570 | / | <> |
1571 | | a |
1572 |---+----|
1573 | | b |"
1574 (should
1575 (equal '("b")
1576 (org-element-contents
1577 (org-export-get-table-cell-at
1578 '(1 . 0)
1579 (org-element-map tree 'table 'identity info 'first-match)
1580 info)))))
1581 ;; 2. Return value for a non-existent address is nil.
1582 (org-test-with-parsed-data "| a |"
1583 (should-not
1584 (org-export-get-table-cell-at
1585 '(2 . 2)
1586 (org-element-map tree 'table 'identity info 'first-match)
1587 info)))
1588 (org-test-with-parsed-data "| / |"
1589 (should-not
1590 (org-export-get-table-cell-at
1591 '(0 . 0)
1592 (org-element-map tree 'table 'identity info 'first-match)
1593 info))))
1595 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
1596 "Test `org-export-table-cell-starts-colgroup-p' specifications."
1597 ;; 1. A cell at a beginning of a row always starts a column group.
1598 (org-test-with-parsed-data "| a |"
1599 (should
1600 (org-export-table-cell-starts-colgroup-p
1601 (org-element-map tree 'table-cell 'identity info 'first-match)
1602 info)))
1603 ;; 2. Special column should be ignored when determining the
1604 ;; beginning of the row.
1605 (org-test-with-parsed-data "
1606 | / | |
1607 | | a |"
1608 (should
1609 (org-export-table-cell-starts-colgroup-p
1610 (org-element-map tree 'table-cell 'identity info 'first-match)
1611 info)))
1612 ;; 2. Explicit column groups.
1613 (org-test-with-parsed-data "
1614 | / | | < |
1615 | a | b | c |"
1616 (should
1617 (equal
1618 '(yes no yes)
1619 (org-element-map
1620 tree 'table-cell
1621 (lambda (cell)
1622 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
1623 info)))))
1625 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
1626 "Test `org-export-table-cell-ends-colgroup-p' specifications."
1627 ;; 1. A cell at the end of a row always ends a column group.
1628 (org-test-with-parsed-data "| a |"
1629 (should
1630 (org-export-table-cell-ends-colgroup-p
1631 (org-element-map tree 'table-cell 'identity info 'first-match)
1632 info)))
1633 ;; 2. Special column should be ignored when determining the
1634 ;; beginning of the row.
1635 (org-test-with-parsed-data "
1636 | / | |
1637 | | a |"
1638 (should
1639 (org-export-table-cell-ends-colgroup-p
1640 (org-element-map tree 'table-cell 'identity info 'first-match)
1641 info)))
1642 ;; 3. Explicit column groups.
1643 (org-test-with-parsed-data "
1644 | / | < | |
1645 | a | b | c |"
1646 (should
1647 (equal
1648 '(yes no yes)
1649 (org-element-map
1650 tree 'table-cell
1651 (lambda (cell)
1652 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
1653 info)))))
1655 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
1656 "Test `org-export-table-row-starts-rowgroup-p' specifications."
1657 ;; 1. A row at the beginning of a table always starts a row group.
1658 ;; So does a row following a table rule.
1659 (org-test-with-parsed-data "
1660 | a |
1661 |---|
1662 | b |"
1663 (should
1664 (equal
1665 '(yes no yes)
1666 (org-element-map
1667 tree 'table-row
1668 (lambda (row)
1669 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
1670 info))))
1671 ;; 2. Special rows should be ignored when determining the beginning
1672 ;; of the row.
1673 (org-test-with-parsed-data "
1674 | / | < |
1675 | | a |
1676 |---+---|
1677 | / | < |
1678 | | b |"
1679 (should
1680 (equal
1681 '(yes no yes)
1682 (org-element-map
1683 tree 'table-row
1684 (lambda (row)
1685 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
1686 info)))))
1688 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
1689 "Test `org-export-table-row-ends-rowgroup-p' specifications."
1690 ;; 1. A row at the end of a table always ends a row group. So does
1691 ;; a row preceding a table rule.
1692 (org-test-with-parsed-data "
1693 | a |
1694 |---|
1695 | b |"
1696 (should
1697 (equal
1698 '(yes no yes)
1699 (org-element-map
1700 tree 'table-row
1701 (lambda (row)
1702 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
1703 info))))
1704 ;; 2. Special rows should be ignored when determining the beginning
1705 ;; of the row.
1706 (org-test-with-parsed-data "
1707 | | a |
1708 | / | < |
1709 |---+---|
1710 | | b |
1711 | / | < |"
1712 (should
1713 (equal
1714 '(yes no yes)
1715 (org-element-map
1716 tree 'table-row
1717 (lambda (row)
1718 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
1719 info)))))
1721 (ert-deftest test-org-export/table-row-starts-header-p ()
1722 "Test `org-export-table-row-starts-header-p' specifications."
1723 ;; 1. Only the row starting the first row group starts the table
1724 ;; header.
1725 (org-test-with-parsed-data "
1726 | a |
1727 | b |
1728 |---|
1729 | c |"
1730 (should
1731 (equal
1732 '(yes no no no)
1733 (org-element-map
1734 tree 'table-row
1735 (lambda (row)
1736 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
1737 info))))
1738 ;; 2. A row cannot start an header if there's no header in the
1739 ;; table.
1740 (org-test-with-parsed-data "
1741 | a |
1742 |---|"
1743 (should-not
1744 (org-export-table-row-starts-header-p
1745 (org-element-map tree 'table-row 'identity info 'first-match)
1746 info))))
1748 (ert-deftest test-org-export/table-row-ends-header-p ()
1749 "Test `org-export-table-row-ends-header-p' specifications."
1750 ;; 1. Only the row starting the first row group starts the table
1751 ;; header.
1752 (org-test-with-parsed-data "
1753 | a |
1754 | b |
1755 |---|
1756 | c |"
1757 (should
1758 (equal
1759 '(no yes no no)
1760 (org-element-map
1761 tree 'table-row
1762 (lambda (row)
1763 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
1764 info))))
1765 ;; 2. A row cannot start an header if there's no header in the
1766 ;; table.
1767 (org-test-with-parsed-data "
1768 | a |
1769 |---|"
1770 (should-not
1771 (org-export-table-row-ends-header-p
1772 (org-element-map tree 'table-row 'identity info 'first-match)
1773 info))))
1777 ;;; Topology
1779 (ert-deftest test-org-export/get-next-element ()
1780 "Test `org-export-get-next-element' specifications."
1781 ;; Standard test.
1782 (should
1783 (equal "b"
1784 (org-test-with-parsed-data "* Headline\n*a* b"
1785 (org-export-get-next-element
1786 (org-element-map tree 'bold 'identity info t) info))))
1787 ;; Return nil when no previous element.
1788 (should-not
1789 (org-test-with-parsed-data "* Headline\na *b*"
1790 (org-export-get-next-element
1791 (org-element-map tree 'bold 'identity info t) info)))
1792 ;; Non-exportable elements are ignored.
1793 (should-not
1794 (let ((org-export-with-timestamps nil))
1795 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
1796 (org-export-get-next-element
1797 (org-element-map tree 'entity 'identity info t) info)))))
1799 (ert-deftest test-org-export/get-previous-element ()
1800 "Test `org-export-get-previous-element' specifications."
1801 ;; Standard test.
1802 (should
1803 (equal "a "
1804 (org-test-with-parsed-data "* Headline\na *b*"
1805 (org-export-get-previous-element
1806 (org-element-map tree 'bold 'identity info t) info))))
1807 ;; Return nil when no previous element.
1808 (should-not
1809 (org-test-with-parsed-data "* Headline\n*a* b"
1810 (org-export-get-previous-element
1811 (org-element-map tree 'bold 'identity info t) info)))
1812 ;; Non-exportable elements are ignored.
1813 (should-not
1814 (let ((org-export-with-timestamps nil))
1815 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
1816 (org-export-get-previous-element
1817 (org-element-map tree 'entity 'identity info t) info)))))
1820 (provide 'test-org-export)
1821 ;;; test-org-export.el end here