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