org-export: Add tag inheritance to `org-export-get-tags'
[org-mode.git] / testing / lisp / test-org-export.el
blobc8a1cd70f1683702aac68ee3a2391cfd5f81db7a
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 (org-test-with-temp-text "
372 * Head1
373 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
374 <<test>>
375 #+END_SRC
376 * Head2
377 #+NAME: test
378 #+BEGIN_SRC emacs-lisp
379 \(+ 1 2)
380 #+END_SRC"
381 (org-test-with-backend test
382 (forward-line 1)
383 (should (equal (org-export-as 'test 'subtree) ": 3\n")))))
385 (ert-deftest test-org-export/expand-include ()
386 "Test file inclusion in an Org buffer."
387 ;; Full insertion with recursive inclusion.
388 (org-test-with-temp-text
389 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
390 (org-export-expand-include-keyword)
391 (should (equal (buffer-string)
392 "Small Org file with an include keyword.
394 #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
396 Success!
398 * Heading
399 body\n")))
400 ;; Localized insertion.
401 (org-test-with-temp-text
402 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
403 org-test-dir)
404 (org-export-expand-include-keyword)
405 (should (equal (buffer-string)
406 "Small Org file with an include keyword.\n")))
407 ;; Insertion with constraints on headlines level.
408 (org-test-with-temp-text
409 (format
410 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
411 org-test-dir)
412 (org-export-expand-include-keyword)
413 (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
414 ;; Inclusion within an example block.
415 (org-test-with-temp-text
416 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
417 org-test-dir)
418 (org-export-expand-include-keyword)
419 (should
420 (equal
421 (buffer-string)
422 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
423 ;; Inclusion within a src-block.
424 (org-test-with-temp-text
425 (format
426 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
427 org-test-dir)
428 (org-export-expand-include-keyword)
429 (should (equal (buffer-string)
430 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
432 (ert-deftest test-org-export/expand-macro ()
433 "Test macro expansion in an Org buffer."
434 ;; Standard macro expansion.
435 (should
436 (equal "#+MACRO: macro1 value\nvalue"
437 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
438 (let (info)
439 (org-export-expand-macro info) (buffer-string)))))
440 ;; Export specific macros.
441 (should
442 (equal "me 2012-03-29 me@here Title"
443 (org-test-with-temp-text
445 #+TITLE: Title
446 #+DATE: 2012-03-29
447 #+AUTHOR: me
448 #+EMAIL: me@here
449 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
450 (let ((info (org-export-get-environment)))
451 (org-export-expand-macro info)
452 (goto-char (point-max))
453 (buffer-substring (line-beginning-position)
454 (line-end-position))))))
455 ;; Expand macros with templates in included files.
456 (should
457 (equal "success"
458 (org-test-with-temp-text
459 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
460 {{{included-macro}}}" org-test-dir)
461 (let (info)
462 (org-export-expand-include-keyword)
463 (org-export-expand-macro info)
464 (goto-char (point-max))
465 (buffer-substring (line-beginning-position)
466 (line-end-position)))))))
468 (ert-deftest test-org-export/user-ignore-list ()
469 "Test if `:ignore-list' accepts user input."
470 (org-test-with-backend test
471 (flet ((skip-note-head
472 (data backend info)
473 ;; Ignore headlines with the word "note" in their title.
474 (org-element-map
475 data 'headline
476 (lambda (headline)
477 (when (string-match "\\<note\\>"
478 (org-element-property :raw-value headline))
479 (org-export-ignore-element headline info)))
480 info)
481 data))
482 ;; Install function in parse tree filters.
483 (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
484 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
485 (should (equal (org-export-as 'test) "* Head1\n")))))))
487 (ert-deftest test-org-export/before-parsing-hook ()
488 "Test `org-export-before-parsing-hook'."
489 (org-test-with-backend test
490 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
491 (let ((org-export-before-parsing-hook
492 '((lambda (backend)
493 (org-map-entries
494 (lambda ()
495 (delete-region (point) (progn (forward-line) (point)))))))))
496 (should (equal (org-export-as 'test) "Body 1\nBody 2\n"))))))
500 ;;; Affiliated Keywords
502 (ert-deftest test-org-export/read-attribute ()
503 "Test `org-export-read-attribute' specifications."
504 ;; Standard test.
505 (should
506 (equal
507 (org-export-read-attribute
508 :attr_html
509 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
510 (org-element-at-point)))
511 '(:a 1 :b 2)))
512 ;; Return nil on empty attribute.
513 (should-not
514 (org-export-read-attribute
515 :attr_html
516 (org-test-with-temp-text "Paragraph" (org-element-at-point)))))
518 (ert-deftest test-org-export/get-caption ()
519 "Test `org-export-get-caption' specifications."
520 ;; Without optional argument, return long caption
521 (should
522 (equal
523 '("l")
524 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
525 (org-export-get-caption (org-element-at-point)))))
526 ;; With optional argument, return short caption.
527 (should
528 (equal
529 '("s")
530 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
531 (org-export-get-caption (org-element-at-point) t))))
532 ;; Multiple lines are separated by white spaces.
533 (should
534 (equal
535 '("a" " " "b")
536 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
537 (org-export-get-caption (org-element-at-point))))))
541 ;;; Export Snippets
543 (ert-deftest test-org-export/export-snippet ()
544 "Test export snippets transcoding."
545 (org-test-with-temp-text "@@test:A@@@@t:B@@"
546 (org-test-with-backend test
547 (let ((org-test-translate-alist
548 (cons (cons 'export-snippet
549 (lambda (snippet contents info)
550 (when (eq (org-export-snippet-backend snippet) 'test)
551 (org-element-property :value snippet))))
552 org-test-translate-alist)))
553 (let ((org-export-snippet-translation-alist nil))
554 (should (equal (org-export-as 'test) "A\n")))
555 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
556 (should (equal (org-export-as 'test) "AB\n")))))))
560 ;;; Footnotes
562 (ert-deftest test-org-export/footnotes ()
563 "Test footnotes specifications."
564 (let ((org-footnote-section nil)
565 (org-export-with-footnotes t))
566 ;; 1. Read every type of footnote.
567 (should
568 (equal
569 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
570 (org-test-with-parsed-data
571 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
572 (org-element-map
573 tree 'footnote-reference
574 (lambda (ref)
575 (let ((def (org-export-get-footnote-definition ref info)))
576 (cons (org-export-get-footnote-number ref info)
577 (if (eq (org-element-property :type ref) 'inline) (car def)
578 (car (org-element-contents
579 (car (org-element-contents def))))))))
580 info))))
581 ;; 2. Test nested footnotes order.
582 (org-test-with-parsed-data
583 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
584 (should
585 (equal
586 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
587 (org-element-map
588 tree 'footnote-reference
589 (lambda (ref)
590 (when (org-export-footnote-first-reference-p ref info)
591 (cons (org-export-get-footnote-number ref info)
592 (org-element-property :label ref))))
593 info))))
594 ;; 3. Test nested footnote in invisible definitions.
595 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
596 ;; Hide definitions.
597 (narrow-to-region (point) (point-at-eol))
598 (let* ((tree (org-element-parse-buffer))
599 (info (org-combine-plists
600 `(:parse-tree ,tree)
601 (org-export-collect-tree-properties
602 tree (org-export-get-environment)))))
603 ;; Both footnotes should be seen.
604 (should
605 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
606 ;; 4. Test footnotes definitions collection.
607 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
609 \[fn:2] B [fn:3] [fn::D].
611 \[fn:3] C."
612 (should (= (length (org-export-collect-footnote-definitions tree info))
613 4)))
614 ;; 5. Test export of footnotes defined outside parsing scope.
615 (org-test-with-temp-text "[fn:1] Out of scope
616 * Title
617 Paragraph[fn:1]"
618 (org-test-with-backend test
619 (let ((org-test-translate-alist
620 (cons (cons 'footnote-reference
621 (lambda (fn contents info)
622 (org-element-interpret-data
623 (org-export-get-footnote-definition fn info))))
624 org-test-translate-alist)))
625 (forward-line)
626 (should (equal "ParagraphOut of scope\n"
627 (org-export-as 'test 'subtree))))))))
631 ;;; Headlines and Inlinetasks
633 (ert-deftest test-org-export/get-relative-level ()
634 "Test `org-export-get-relative-level' specifications."
635 ;; Standard test.
636 (should
637 (equal '(1 2)
638 (let ((org-odd-levels-only nil))
639 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
640 (org-element-map
641 tree 'headline
642 (lambda (h) (org-export-get-relative-level h info))
643 info)))))
644 ;; Missing levels
645 (should
646 (equal '(1 3)
647 (let ((org-odd-levels-only nil))
648 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
649 (org-element-map
650 tree 'headline
651 (lambda (h) (org-export-get-relative-level h info))
652 info))))))
654 (ert-deftest test-org-export/low-level-p ()
655 "Test `org-export-low-level-p' specifications."
656 (should
657 (equal
658 '(no yes)
659 (let ((org-odd-levels-only nil))
660 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
661 (org-element-map
662 tree 'headline
663 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
664 (plist-put info :headline-levels 1)))))))
666 (ert-deftest test-org-export/get-headline-number ()
667 "Test `org-export-get-headline-number' specifications."
668 ;; Standard test.
669 (should
670 (equal
671 '((1) (1 1))
672 (let ((org-odd-levels-only nil))
673 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
674 (org-element-map
675 tree 'headline
676 (lambda (h) (org-export-get-headline-number h info))
677 info)))))
678 ;; Missing levels are replaced with 0.
679 (should
680 (equal
681 '((1) (1 0 1))
682 (let ((org-odd-levels-only nil))
683 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
684 (org-element-map
685 tree 'headline
686 (lambda (h) (org-export-get-headline-number h info))
687 info))))))
689 (ert-deftest test-org-export/numbered-headline-p ()
690 "Test `org-export-numbered-headline-p' specifications."
691 ;; If `:section-numbers' is nil, never number headlines.
692 (should-not
693 (org-test-with-parsed-data "* Headline"
694 (org-element-map
695 tree 'headline
696 (lambda (h) (org-export-numbered-headline-p h info))
697 (plist-put info :section-numbers nil))))
698 ;; If `:section-numbers' is a number, only number headlines with
699 ;; a level greater that it.
700 (should
701 (equal
702 '(yes no)
703 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
704 (org-element-map
705 tree 'headline
706 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
707 (plist-put info :section-numbers 1)))))
708 ;; Otherwise, headlines are always numbered.
709 (should
710 (org-test-with-parsed-data "* Headline"
711 (org-element-map
712 tree 'headline
713 (lambda (h) (org-export-numbered-headline-p h info))
714 (plist-put info :section-numbers t)))))
716 (ert-deftest test-org-export/number-to-roman ()
717 "Test `org-export-number-to-roman' specifications."
718 ;; If number is negative, return it as a string.
719 (should (equal (org-export-number-to-roman -1) "-1"))
720 ;; Otherwise, return it as a roman number.
721 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
723 (ert-deftest test-org-export/get-tags ()
724 "Test `org-export-get-tags' specifications."
725 (let ((org-export-exclude-tags '("noexport"))
726 (org-export-select-tags '("export")))
727 ;; Standard test: tags which are not a select tag, an exclude tag,
728 ;; or specified as optional argument shouldn't be ignored.
729 (should
730 (org-test-with-parsed-data "* Headline :tag:"
731 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
732 info)))
733 ;; Exclude tags are removed.
734 (should-not
735 (org-test-with-parsed-data "* Headline :noexport:"
736 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
737 info)))
738 ;; Select tags are removed.
739 (should-not
740 (org-test-with-parsed-data "* Headline :export:"
741 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
742 info)))
743 (should
744 (equal
745 '("tag")
746 (org-test-with-parsed-data "* Headline :tag:export:"
747 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
748 info))))
749 ;; Tags provided in the optional argument are also ignored.
750 (should-not
751 (org-test-with-parsed-data "* Headline :ignore:"
752 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
753 info '("ignore"))))
754 ;; Allow tag inheritance.
755 (should
756 (equal
757 '(("tag") ("tag"))
758 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
759 (org-element-map
760 tree 'headline
761 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
763 (ert-deftest test-org-export/get-node-property ()
764 "Test`org-export-get-node-property' specifications."
765 ;; Standard test.
766 (should
767 (equal "value"
768 (org-test-with-parsed-data "* Headline
769 :PROPERTIES:
770 :prop: value
771 :END:"
772 (org-export-get-node-property
773 :prop (org-element-map tree 'headline 'identity nil t)))))
774 ;; Test inheritance.
775 (should
776 (equal "value"
777 (org-test-with-parsed-data "* Parent
778 :PROPERTIES:
779 :prop: value
780 :END:
781 ** Headline
782 Paragraph"
783 (org-export-get-node-property
784 :prop (org-element-map tree 'paragraph 'identity nil t) t))))
785 ;; Cannot return a value before the first headline.
786 (should-not
787 (org-test-with-parsed-data "Paragraph
788 * Headline
789 :PROPERTIES:
790 :prop: value
791 :END:"
792 (org-export-get-node-property
793 :prop (org-element-map tree 'paragraph 'identity nil t)))))
795 (ert-deftest test-org-export/first-sibling-p ()
796 "Test `org-export-first-sibling-p' specifications."
797 ;; Standard test.
798 (should
799 (equal
800 '(yes yes no)
801 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
802 (org-element-map
803 tree 'headline
804 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
805 info))))
806 ;; Ignore headlines not exported.
807 (should
808 (equal
809 '(yes)
810 (let ((org-export-exclude-tags '("ignore")))
811 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
812 (org-element-map
813 tree 'headline
814 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
815 info))))))
817 (ert-deftest test-org-export/last-sibling-p ()
818 "Test `org-export-last-sibling-p' specifications."
819 ;; Standard test.
820 (should
821 (equal
822 '(yes no yes)
823 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
824 (org-element-map
825 tree 'headline
826 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
827 info))))
828 ;; Ignore headlines not exported.
829 (should
830 (equal
831 '(yes)
832 (let ((org-export-exclude-tags '("ignore")))
833 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
834 (org-element-map
835 tree 'headline
836 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
837 info))))))
841 ;;; Links
843 (ert-deftest test-org-export/get-coderef-format ()
844 "Test `org-export-get-coderef-format' specifications."
845 ;; A link without description returns "%s"
846 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
847 "%s"))
848 ;; Return "%s" when path is matched within description.
849 (should (equal (org-export-get-coderef-format "path" "desc (path)")
850 "desc %s"))
851 ;; Otherwise return description.
852 (should (equal (org-export-get-coderef-format "path" "desc")
853 "desc")))
855 (ert-deftest test-org-export/inline-image-p ()
856 "Test `org-export-inline-image-p' specifications."
857 (should
858 (org-export-inline-image-p
859 (org-test-with-temp-text "[[#id]]"
860 (org-element-map
861 (org-element-parse-buffer) 'link 'identity nil t))
862 '(("custom-id" . "id")))))
864 (ert-deftest test-org-export/fuzzy-link ()
865 "Test fuzzy links specifications."
866 ;; 1. Links to invisible (keyword) targets should be ignored.
867 (org-test-with-parsed-data
868 "Paragraph.\n#+TARGET: Test\n[[Test]]"
869 (should-not
870 (org-element-map
871 tree 'link
872 (lambda (link)
873 (org-export-get-ordinal
874 (org-export-resolve-fuzzy-link link info) info)) info)))
875 ;; 2. Link to an headline should return headline's number.
876 (org-test-with-parsed-data
877 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
878 (should
879 ;; Note: Headline's number is in fact a list of numbers.
880 (equal '(2)
881 (org-element-map
882 tree 'link
883 (lambda (link)
884 (org-export-get-ordinal
885 (org-export-resolve-fuzzy-link link info) info)) info t))))
886 ;; 3. Link to a target in an item should return item's number.
887 (org-test-with-parsed-data
888 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
889 (should
890 ;; Note: Item's number is in fact a list of numbers.
891 (equal '(1 2)
892 (org-element-map
893 tree 'link
894 (lambda (link)
895 (org-export-get-ordinal
896 (org-export-resolve-fuzzy-link link info) info)) info t))))
897 ;; 4. Link to a target in a footnote should return footnote's
898 ;; number.
899 (org-test-with-parsed-data "
900 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
901 (should
902 (equal '(2 3)
903 (org-element-map
904 tree 'link
905 (lambda (link)
906 (org-export-get-ordinal
907 (org-export-resolve-fuzzy-link link info) info)) info))))
908 ;; 5. Link to a named element should return sequence number of that
909 ;; element.
910 (org-test-with-parsed-data
911 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
912 (should
913 (= 2
914 (org-element-map
915 tree 'link
916 (lambda (link)
917 (org-export-get-ordinal
918 (org-export-resolve-fuzzy-link link info) info)) info t))))
919 ;; 6. Link to a target not within an item, a table, a footnote
920 ;; reference or definition should return section number.
921 (org-test-with-parsed-data
922 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
923 (should
924 (equal '(2)
925 (org-element-map
926 tree 'link
927 (lambda (link)
928 (org-export-get-ordinal
929 (org-export-resolve-fuzzy-link link info) info)) info t)))))
931 (ert-deftest test-org-export/resolve-coderef ()
932 "Test `org-export-resolve-coderef' specifications."
933 (let ((org-coderef-label-format "(ref:%s)"))
934 ;; 1. A link to a "-n -k -r" block returns line number.
935 (org-test-with-parsed-data
936 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
937 (should (= (org-export-resolve-coderef "coderef" info) 1)))
938 (org-test-with-parsed-data
939 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
940 (should (= (org-export-resolve-coderef "coderef" info) 1)))
941 ;; 2. A link to a "-n -r" block returns line number.
942 (org-test-with-parsed-data
943 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
944 (should (= (org-export-resolve-coderef "coderef" info) 1)))
945 (org-test-with-parsed-data
946 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
947 (should (= (org-export-resolve-coderef "coderef" info) 1)))
948 ;; 3. A link to a "-n" block returns coderef.
949 (org-test-with-parsed-data
950 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
951 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
952 (org-test-with-parsed-data
953 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
954 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
955 ;; 4. A link to a "-r" block returns line number.
956 (org-test-with-parsed-data
957 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
958 (should (= (org-export-resolve-coderef "coderef" info) 1)))
959 (org-test-with-parsed-data
960 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
961 (should (= (org-export-resolve-coderef "coderef" info) 1)))
962 ;; 5. A link to a block without a switch returns coderef.
963 (org-test-with-parsed-data
964 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
965 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
966 (org-test-with-parsed-data
967 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
968 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
969 ;; 6. Correctly handle continued line numbers. A "+n" switch
970 ;; should resume numbering from previous block with numbered
971 ;; lines, ignoring blocks not numbering lines in the process.
972 ;; A "-n" switch resets count.
973 (org-test-with-parsed-data "
974 #+BEGIN_EXAMPLE -n
975 Text.
976 #+END_EXAMPLE
978 #+BEGIN_SRC emacs-lisp
979 \(- 1 1)
980 #+END_SRC
982 #+BEGIN_SRC emacs-lisp +n -r
983 \(+ 1 1) (ref:addition)
984 #+END_SRC
986 #+BEGIN_EXAMPLE -n -r
987 Another text. (ref:text)
988 #+END_EXAMPLE"
989 (should (= (org-export-resolve-coderef "addition" info) 2))
990 (should (= (org-export-resolve-coderef "text" info) 1)))
991 ;; 7. Recognize coderef with user-specified syntax.
992 (org-test-with-parsed-data
993 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
994 (should (equal (org-export-resolve-coderef "text" info) "text")))))
996 (ert-deftest test-org-export/resolve-fuzzy-link ()
997 "Test `org-export-resolve-fuzzy-link' specifications."
998 ;; 1. Match target objects.
999 (org-test-with-parsed-data "<<target>> [[target]]"
1000 (should
1001 (org-export-resolve-fuzzy-link
1002 (org-element-map tree 'link 'identity info t) info)))
1003 ;; 2. Match target elements.
1004 (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
1005 (should
1006 (org-export-resolve-fuzzy-link
1007 (org-element-map tree 'link 'identity info t) info)))
1008 ;; 3. Match named elements.
1009 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
1010 (should
1011 (org-export-resolve-fuzzy-link
1012 (org-element-map tree 'link 'identity info t) info)))
1013 ;; 4. Match exact headline's name.
1014 (org-test-with-parsed-data "* My headline\n[[My headline]]"
1015 (should
1016 (org-export-resolve-fuzzy-link
1017 (org-element-map tree 'link 'identity info t) info)))
1018 ;; 5. Targets objects have priority over named elements and headline
1019 ;; titles.
1020 (org-test-with-parsed-data
1021 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
1022 (should
1023 (eq 'target
1024 (org-element-type
1025 (org-export-resolve-fuzzy-link
1026 (org-element-map tree 'link 'identity info t) info)))))
1027 ;; 6. Named elements have priority over headline titles.
1028 (org-test-with-parsed-data
1029 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
1030 (should
1031 (eq 'paragraph
1032 (org-element-type
1033 (org-export-resolve-fuzzy-link
1034 (org-element-map tree 'link 'identity info t) info)))))
1035 ;; 7. If link's path starts with a "*", only match headline titles,
1036 ;; though.
1037 (org-test-with-parsed-data
1038 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
1039 (should
1040 (eq 'headline
1041 (org-element-type
1042 (org-export-resolve-fuzzy-link
1043 (org-element-map tree 'link 'identity info t) info)))))
1044 ;; 8. Return nil if no match.
1045 (org-test-with-parsed-data "[[target]]"
1046 (should-not
1047 (org-export-resolve-fuzzy-link
1048 (org-element-map tree 'link 'identity info t) info))))
1050 (ert-deftest test-org-export/resolve-id-link ()
1051 "Test `org-export-resolve-id-link' specifications."
1052 ;; 1. Regular test for custom-id link.
1053 (org-test-with-parsed-data "* Headline1
1054 :PROPERTIES:
1055 :CUSTOM-ID: test
1056 :END:
1057 * Headline 2
1058 \[[#test]]"
1059 (should
1060 (org-export-resolve-id-link
1061 (org-element-map tree 'link 'identity info t) info)))
1062 ;; 2. Failing test for custom-id link.
1063 (org-test-with-parsed-data "* Headline1
1064 :PROPERTIES:
1065 :CUSTOM-ID: test
1066 :END:
1067 * Headline 2
1068 \[[#no-match]]"
1069 (should-not
1070 (org-export-resolve-id-link
1071 (org-element-map tree 'link 'identity info t) info)))
1072 ;; 3. Test for internal id target.
1073 (org-test-with-parsed-data "* Headline1
1074 :PROPERTIES:
1075 :ID: aaaa
1076 :END:
1077 * Headline 2
1078 \[[id:aaaa]]"
1079 (should
1080 (org-export-resolve-id-link
1081 (org-element-map tree 'link 'identity info t) info)))
1082 ;; 4. Test for external id target.
1083 (org-test-with-parsed-data "[[id:aaaa]]"
1084 (should
1085 (org-export-resolve-id-link
1086 (org-element-map tree 'link 'identity info t)
1087 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
1089 (ert-deftest test-org-export/resolve-radio-link ()
1090 "Test `org-export-resolve-radio-link' specifications."
1091 ;; Standard test.
1092 (org-test-with-temp-text "<<<radio>>> radio"
1093 (org-update-radio-target-regexp)
1094 (should
1095 (let* ((tree (org-element-parse-buffer))
1096 (info `(:parse-tree ,tree)))
1097 (org-export-resolve-radio-link
1098 (org-element-map tree 'link 'identity info t)
1099 info))))
1100 ;; Radio target with objects.
1101 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
1102 (org-update-radio-target-regexp)
1103 (should
1104 (let* ((tree (org-element-parse-buffer))
1105 (info `(:parse-tree ,tree)))
1106 (org-export-resolve-radio-link
1107 (org-element-map tree 'link 'identity info t)
1108 info)))))
1112 ;;; Src-block and example-block
1114 (ert-deftest test-org-export/unravel-code ()
1115 "Test `org-export-unravel-code' function."
1116 (let ((org-coderef-label-format "(ref:%s)"))
1117 ;; 1. Code without reference.
1118 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
1119 (should (equal (org-export-unravel-code (org-element-at-point))
1120 '("(+ 1 1)\n"))))
1121 ;; 2. Code with reference.
1122 (org-test-with-temp-text
1123 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
1124 (should (equal (org-export-unravel-code (org-element-at-point))
1125 '("(+ 1 1)\n" (1 . "test")))))
1126 ;; 3. Code with user-defined reference.
1127 (org-test-with-temp-text
1128 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
1129 (should (equal (org-export-unravel-code (org-element-at-point))
1130 '("(+ 1 1)\n" (1 . "test")))))
1131 ;; 4. Code references keys are relative to the current block.
1132 (org-test-with-temp-text "
1133 #+BEGIN_EXAMPLE -n
1134 \(+ 1 1)
1135 #+END_EXAMPLE
1136 #+BEGIN_EXAMPLE +n
1137 \(+ 2 2)
1138 \(+ 3 3) (ref:one)
1139 #+END_EXAMPLE"
1140 (goto-line 5)
1141 (should (equal (org-export-unravel-code (org-element-at-point))
1142 '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
1146 ;;; Tables
1148 (ert-deftest test-org-export/special-column ()
1149 "Test if the table's special column is properly recognized."
1150 ;; 1. First column is special if it contains only a special marking
1151 ;; characters or empty cells.
1152 (org-test-with-temp-text "
1153 | ! | 1 |
1154 | | 2 |"
1155 (should
1156 (org-export-table-has-special-column-p
1157 (org-element-map
1158 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1159 ;; 2. If the column contains anything else, it isn't special.
1160 (org-test-with-temp-text "
1161 | ! | 1 |
1162 | b | 2 |"
1163 (should-not
1164 (org-export-table-has-special-column-p
1165 (org-element-map
1166 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1167 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
1168 ;; and "!".
1169 (org-test-with-temp-text "
1170 | # | 1 |
1171 | ^ | 2 |
1172 | * | 3 |
1173 | _ | 4 |
1174 | / | 5 |
1175 | $ | 6 |
1176 | ! | 7 |"
1177 (should
1178 (org-export-table-has-special-column-p
1179 (org-element-map
1180 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1181 ;; 4. A first column with only empty cells isn't considered as
1182 ;; special.
1183 (org-test-with-temp-text "
1184 | | 1 |
1185 | | 2 |"
1186 (should-not
1187 (org-export-table-has-special-column-p
1188 (org-element-map
1189 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
1191 (ert-deftest test-org-export/table-row-is-special-p ()
1192 "Test `org-export-table-row-is-special-p' specifications."
1193 ;; 1. A row is special if it has a special marking character in the
1194 ;; special column.
1195 (org-test-with-parsed-data "| ! | 1 |"
1196 (should
1197 (org-export-table-row-is-special-p
1198 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1199 ;; 2. A row is special when its first field is "/"
1200 (org-test-with-parsed-data "
1201 | / | 1 |
1202 | a | b |"
1203 (should
1204 (org-export-table-row-is-special-p
1205 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1206 ;; 3. A row only containing alignment cookies is also considered as
1207 ;; special.
1208 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
1209 (should
1210 (org-export-table-row-is-special-p
1211 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1212 ;; 4. Everything else isn't considered as special.
1213 (org-test-with-parsed-data "| \alpha | | c |"
1214 (should-not
1215 (org-export-table-row-is-special-p
1216 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1217 ;; 5. Table's rules are never considered as special rows.
1218 (org-test-with-parsed-data "|---+---|"
1219 (should-not
1220 (org-export-table-row-is-special-p
1221 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
1223 (ert-deftest test-org-export/has-header-p ()
1224 "Test `org-export-table-has-header-p' specifications."
1225 ;; 1. With an header.
1226 (org-test-with-parsed-data "
1227 | a | b |
1228 |---+---|
1229 | c | d |"
1230 (should
1231 (org-export-table-has-header-p
1232 (org-element-map tree 'table 'identity info 'first-match)
1233 info)))
1234 ;; 2. Without an header.
1235 (org-test-with-parsed-data "
1236 | a | b |
1237 | c | d |"
1238 (should-not
1239 (org-export-table-has-header-p
1240 (org-element-map tree 'table 'identity info 'first-match)
1241 info)))
1242 ;; 3. Don't get fooled with starting and ending rules.
1243 (org-test-with-parsed-data "
1244 |---+---|
1245 | a | b |
1246 | c | d |
1247 |---+---|"
1248 (should-not
1249 (org-export-table-has-header-p
1250 (org-element-map tree 'table 'identity info 'first-match)
1251 info))))
1253 (ert-deftest test-org-export/table-row-group ()
1254 "Test `org-export-table-row-group' specifications."
1255 ;; 1. A rule creates a new group.
1256 (org-test-with-parsed-data "
1257 | a | b |
1258 |---+---|
1259 | 1 | 2 |"
1260 (should
1261 (equal
1262 '(1 nil 2)
1263 (mapcar (lambda (row) (org-export-table-row-group row info))
1264 (org-element-map tree 'table-row 'identity)))))
1265 ;; 2. Special rows are ignored in count.
1266 (org-test-with-parsed-data "
1267 | / | < | > |
1268 |---|---+---|
1269 | | 1 | 2 |"
1270 (should
1271 (equal
1272 '(nil nil 1)
1273 (mapcar (lambda (row) (org-export-table-row-group row info))
1274 (org-element-map tree 'table-row 'identity)))))
1275 ;; 3. Double rules also are ignored in count.
1276 (org-test-with-parsed-data "
1277 | a | b |
1278 |---+---|
1279 |---+---|
1280 | 1 | 2 |"
1281 (should
1282 (equal
1283 '(1 nil nil 2)
1284 (mapcar (lambda (row) (org-export-table-row-group row info))
1285 (org-element-map tree 'table-row 'identity))))))
1287 (ert-deftest test-org-export/table-cell-width ()
1288 "Test `org-export-table-cell-width' specifications."
1289 ;; 1. Width is primarily determined by width cookies. If no cookie
1290 ;; is found, cell's width is nil.
1291 (org-test-with-parsed-data "
1292 | / | <l> | <6> | <l7> |
1293 | | a | b | c |"
1294 (should
1295 (equal
1296 '(nil 6 7)
1297 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1298 (org-element-map tree 'table-cell 'identity info)))))
1299 ;; 2. The last width cookie has precedence.
1300 (org-test-with-parsed-data "
1301 | <6> |
1302 | <7> |
1303 | a |"
1304 (should
1305 (equal
1306 '(7)
1307 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1308 (org-element-map tree 'table-cell 'identity info)))))
1309 ;; 3. Valid width cookies must have a specific row.
1310 (org-test-with-parsed-data "| <6> | cell |"
1311 (should
1312 (equal
1313 '(nil nil)
1314 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1315 (org-element-map tree 'table-cell 'identity))))))
1317 (ert-deftest test-org-export/table-cell-alignment ()
1318 "Test `org-export-table-cell-alignment' specifications."
1319 (let ((org-table-number-fraction 0.5)
1320 (org-table-number-regexp "^[0-9]+$"))
1321 ;; 1. Alignment is primarily determined by alignment cookies.
1322 (org-test-with-temp-text "| <l> | <c> | <r> |"
1323 (let* ((tree (org-element-parse-buffer))
1324 (info `(:parse-tree ,tree)))
1325 (should
1326 (equal
1327 '(left center right)
1328 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
1329 (org-element-map tree 'table-cell 'identity))))))
1330 ;; 2. The last alignment cookie has precedence.
1331 (org-test-with-parsed-data "
1332 | <l8> |
1333 | cell |
1334 | <r9> |"
1335 (should
1336 (equal
1337 '(right right right)
1338 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
1339 (org-element-map tree 'table-cell 'identity)))))
1340 ;; 3. If there's no cookie, cell's contents determine alignment.
1341 ;; A column mostly made of cells containing numbers will align
1342 ;; its cells to the right.
1343 (org-test-with-parsed-data "
1344 | 123 |
1345 | some text |
1346 | 12345 |"
1347 (should
1348 (equal
1349 '(right right right)
1350 (mapcar (lambda (cell)
1351 (org-export-table-cell-alignment cell info))
1352 (org-element-map tree 'table-cell 'identity)))))
1353 ;; 4. Otherwise, they will be aligned to the left.
1354 (org-test-with-parsed-data "
1355 | text |
1356 | some text |
1357 | \alpha |"
1358 (should
1359 (equal
1360 '(left left left)
1361 (mapcar (lambda (cell)
1362 (org-export-table-cell-alignment cell info))
1363 (org-element-map tree 'table-cell 'identity)))))))
1365 (ert-deftest test-org-export/table-cell-borders ()
1366 "Test `org-export-table-cell-borders' specifications."
1367 ;; 1. Recognize various column groups indicators.
1368 (org-test-with-parsed-data "| / | < | > | <> |"
1369 (should
1370 (equal
1371 '((right bottom top) (left bottom top) (right bottom top)
1372 (right left bottom top))
1373 (mapcar (lambda (cell)
1374 (org-export-table-cell-borders cell info))
1375 (org-element-map tree 'table-cell 'identity)))))
1376 ;; 2. Accept shortcuts to define column groups.
1377 (org-test-with-parsed-data "| / | < | < |"
1378 (should
1379 (equal
1380 '((right bottom top) (right left bottom top) (left bottom top))
1381 (mapcar (lambda (cell)
1382 (org-export-table-cell-borders cell info))
1383 (org-element-map tree 'table-cell 'identity)))))
1384 ;; 3. A valid column groups row must start with a "/".
1385 (org-test-with-parsed-data "
1386 | | < |
1387 | a | b |"
1388 (should
1389 (equal '((top) (top) (bottom) (bottom))
1390 (mapcar (lambda (cell)
1391 (org-export-table-cell-borders cell info))
1392 (org-element-map tree 'table-cell 'identity)))))
1393 ;; 4. Take table rules into consideration.
1394 (org-test-with-parsed-data "
1395 | 1 |
1396 |---|
1397 | 2 |"
1398 (should
1399 (equal '((below top) (bottom above))
1400 (mapcar (lambda (cell)
1401 (org-export-table-cell-borders cell info))
1402 (org-element-map tree 'table-cell 'identity)))))
1403 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
1404 ;; (resp. `bottom' and `below') borders. Any special row is
1405 ;; ignored.
1406 (org-test-with-parsed-data "
1407 |---+----|
1408 | / | |
1409 | | 1 |
1410 |---+----|"
1411 (should
1412 (equal '((bottom below top above))
1413 (last
1414 (mapcar (lambda (cell)
1415 (org-export-table-cell-borders cell info))
1416 (org-element-map tree 'table-cell 'identity)))))))
1418 (ert-deftest test-org-export/table-dimensions ()
1419 "Test `org-export-table-dimensions' specifications."
1420 ;; 1. Standard test.
1421 (org-test-with-parsed-data "
1422 | 1 | 2 | 3 |
1423 | 4 | 5 | 6 |"
1424 (should
1425 (equal '(2 . 3)
1426 (org-export-table-dimensions
1427 (org-element-map tree 'table 'identity info 'first-match) info))))
1428 ;; 2. Ignore horizontal rules and special columns.
1429 (org-test-with-parsed-data "
1430 | / | < | > |
1431 | 1 | 2 | 3 |
1432 |---+---+---|
1433 | 4 | 5 | 6 |"
1434 (should
1435 (equal '(2 . 3)
1436 (org-export-table-dimensions
1437 (org-element-map tree 'table 'identity info 'first-match) info)))))
1439 (ert-deftest test-org-export/table-cell-address ()
1440 "Test `org-export-table-cell-address' specifications."
1441 ;; 1. Standard test: index is 0-based.
1442 (org-test-with-parsed-data "| a | b |"
1443 (should
1444 (equal '((0 . 0) (0 . 1))
1445 (org-element-map
1446 tree 'table-cell
1447 (lambda (cell) (org-export-table-cell-address cell info))
1448 info))))
1449 ;; 2. Special column isn't counted, nor are special rows.
1450 (org-test-with-parsed-data "
1451 | / | <> |
1452 | | c |"
1453 (should
1454 (equal '(0 . 0)
1455 (org-export-table-cell-address
1456 (car (last (org-element-map tree 'table-cell 'identity info)))
1457 info))))
1458 ;; 3. Tables rules do not count either.
1459 (org-test-with-parsed-data "
1460 | a |
1461 |---|
1462 | b |
1463 |---|
1464 | c |"
1465 (should
1466 (equal '(2 . 0)
1467 (org-export-table-cell-address
1468 (car (last (org-element-map tree 'table-cell 'identity info)))
1469 info))))
1470 ;; 4. Return nil for special cells.
1471 (org-test-with-parsed-data "| / | a |"
1472 (should-not
1473 (org-export-table-cell-address
1474 (org-element-map tree 'table-cell 'identity nil 'first-match)
1475 info))))
1477 (ert-deftest test-org-export/get-table-cell-at ()
1478 "Test `org-export-get-table-cell-at' specifications."
1479 ;; 1. Address ignores special columns, special rows and rules.
1480 (org-test-with-parsed-data "
1481 | / | <> |
1482 | | a |
1483 |---+----|
1484 | | b |"
1485 (should
1486 (equal '("b")
1487 (org-element-contents
1488 (org-export-get-table-cell-at
1489 '(1 . 0)
1490 (org-element-map tree 'table 'identity info 'first-match)
1491 info)))))
1492 ;; 2. Return value for a non-existent address is nil.
1493 (org-test-with-parsed-data "| a |"
1494 (should-not
1495 (org-export-get-table-cell-at
1496 '(2 . 2)
1497 (org-element-map tree 'table 'identity info 'first-match)
1498 info)))
1499 (org-test-with-parsed-data "| / |"
1500 (should-not
1501 (org-export-get-table-cell-at
1502 '(0 . 0)
1503 (org-element-map tree 'table 'identity info 'first-match)
1504 info))))
1506 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
1507 "Test `org-export-table-cell-starts-colgroup-p' specifications."
1508 ;; 1. A cell at a beginning of a row always starts a column group.
1509 (org-test-with-parsed-data "| a |"
1510 (should
1511 (org-export-table-cell-starts-colgroup-p
1512 (org-element-map tree 'table-cell 'identity info 'first-match)
1513 info)))
1514 ;; 2. Special column should be ignored when determining the
1515 ;; beginning of the row.
1516 (org-test-with-parsed-data "
1517 | / | |
1518 | | a |"
1519 (should
1520 (org-export-table-cell-starts-colgroup-p
1521 (org-element-map tree 'table-cell 'identity info 'first-match)
1522 info)))
1523 ;; 2. Explicit column groups.
1524 (org-test-with-parsed-data "
1525 | / | | < |
1526 | a | b | c |"
1527 (should
1528 (equal
1529 '(yes no yes)
1530 (org-element-map
1531 tree 'table-cell
1532 (lambda (cell)
1533 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
1534 info)))))
1536 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
1537 "Test `org-export-table-cell-ends-colgroup-p' specifications."
1538 ;; 1. A cell at the end of a row always ends a column group.
1539 (org-test-with-parsed-data "| a |"
1540 (should
1541 (org-export-table-cell-ends-colgroup-p
1542 (org-element-map tree 'table-cell 'identity info 'first-match)
1543 info)))
1544 ;; 2. Special column should be ignored when determining the
1545 ;; beginning of the row.
1546 (org-test-with-parsed-data "
1547 | / | |
1548 | | a |"
1549 (should
1550 (org-export-table-cell-ends-colgroup-p
1551 (org-element-map tree 'table-cell 'identity info 'first-match)
1552 info)))
1553 ;; 3. Explicit column groups.
1554 (org-test-with-parsed-data "
1555 | / | < | |
1556 | a | b | c |"
1557 (should
1558 (equal
1559 '(yes no yes)
1560 (org-element-map
1561 tree 'table-cell
1562 (lambda (cell)
1563 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
1564 info)))))
1566 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
1567 "Test `org-export-table-row-starts-rowgroup-p' specifications."
1568 ;; 1. A row at the beginning of a table always starts a row group.
1569 ;; So does a row following a table rule.
1570 (org-test-with-parsed-data "
1571 | a |
1572 |---|
1573 | b |"
1574 (should
1575 (equal
1576 '(yes no yes)
1577 (org-element-map
1578 tree 'table-row
1579 (lambda (row)
1580 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
1581 info))))
1582 ;; 2. Special rows should be ignored when determining the beginning
1583 ;; of the row.
1584 (org-test-with-parsed-data "
1585 | / | < |
1586 | | a |
1587 |---+---|
1588 | / | < |
1589 | | b |"
1590 (should
1591 (equal
1592 '(yes no yes)
1593 (org-element-map
1594 tree 'table-row
1595 (lambda (row)
1596 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
1597 info)))))
1599 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
1600 "Test `org-export-table-row-ends-rowgroup-p' specifications."
1601 ;; 1. A row at the end of a table always ends a row group. So does
1602 ;; a row preceding a table rule.
1603 (org-test-with-parsed-data "
1604 | a |
1605 |---|
1606 | b |"
1607 (should
1608 (equal
1609 '(yes no yes)
1610 (org-element-map
1611 tree 'table-row
1612 (lambda (row)
1613 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
1614 info))))
1615 ;; 2. Special rows should be ignored when determining the beginning
1616 ;; of the row.
1617 (org-test-with-parsed-data "
1618 | | a |
1619 | / | < |
1620 |---+---|
1621 | | b |
1622 | / | < |"
1623 (should
1624 (equal
1625 '(yes no yes)
1626 (org-element-map
1627 tree 'table-row
1628 (lambda (row)
1629 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
1630 info)))))
1632 (ert-deftest test-org-export/table-row-starts-header-p ()
1633 "Test `org-export-table-row-starts-header-p' specifications."
1634 ;; 1. Only the row starting the first row group starts the table
1635 ;; header.
1636 (org-test-with-parsed-data "
1637 | a |
1638 | b |
1639 |---|
1640 | c |"
1641 (should
1642 (equal
1643 '(yes no no no)
1644 (org-element-map
1645 tree 'table-row
1646 (lambda (row)
1647 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
1648 info))))
1649 ;; 2. A row cannot start an header if there's no header in the
1650 ;; table.
1651 (org-test-with-parsed-data "
1652 | a |
1653 |---|"
1654 (should-not
1655 (org-export-table-row-starts-header-p
1656 (org-element-map tree 'table-row 'identity info 'first-match)
1657 info))))
1659 (ert-deftest test-org-export/table-row-ends-header-p ()
1660 "Test `org-export-table-row-ends-header-p' specifications."
1661 ;; 1. Only the row starting the first row group starts the table
1662 ;; header.
1663 (org-test-with-parsed-data "
1664 | a |
1665 | b |
1666 |---|
1667 | c |"
1668 (should
1669 (equal
1670 '(no yes no no)
1671 (org-element-map
1672 tree 'table-row
1673 (lambda (row)
1674 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
1675 info))))
1676 ;; 2. A row cannot start an header if there's no header in the
1677 ;; table.
1678 (org-test-with-parsed-data "
1679 | a |
1680 |---|"
1681 (should-not
1682 (org-export-table-row-ends-header-p
1683 (org-element-map tree 'table-row 'identity info 'first-match)
1684 info))))
1688 ;;; Topology
1690 (ert-deftest test-org-export/get-next-element ()
1691 "Test `org-export-get-next-element' specifications."
1692 ;; Standard test.
1693 (should
1694 (equal "b"
1695 (org-test-with-parsed-data "* Headline\n*a* b"
1696 (org-export-get-next-element
1697 (org-element-map tree 'bold 'identity info t) info))))
1698 ;; Return nil when no previous element.
1699 (should-not
1700 (org-test-with-parsed-data "* Headline\na *b*"
1701 (org-export-get-next-element
1702 (org-element-map tree 'bold 'identity info t) info)))
1703 ;; Non-exportable elements are ignored.
1704 (should-not
1705 (let ((org-export-with-timestamps nil))
1706 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
1707 (org-export-get-next-element
1708 (org-element-map tree 'entity 'identity info t) info)))))
1710 (ert-deftest test-org-export/get-previous-element ()
1711 "Test `org-export-get-previous-element' specifications."
1712 ;; Standard test.
1713 (should
1714 (equal "a "
1715 (org-test-with-parsed-data "* Headline\na *b*"
1716 (org-export-get-previous-element
1717 (org-element-map tree 'bold 'identity info t) info))))
1718 ;; Return nil when no previous element.
1719 (should-not
1720 (org-test-with-parsed-data "* Headline\n*a* b"
1721 (org-export-get-previous-element
1722 (org-element-map tree 'bold 'identity info t) info)))
1723 ;; Non-exportable elements are ignored.
1724 (should-not
1725 (let ((org-export-with-timestamps nil))
1726 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
1727 (org-export-get-previous-element
1728 (org-element-map tree 'entity 'identity info t) info)))))
1731 (provide 'test-org-export)
1732 ;;; test-org-export.el end here