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