org-element: Rename `org-export-set-element' into `org-element-set-element'
[org-mode.git] / testing / lisp / test-org-export.el
blob9c773cead6924c5c5e3f39e952a8209efa94f664
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 (cons type (intern (format "org-%s-%s" backend type)))
32 transcode-table)))))
33 (flet ,(let (transcoders)
34 (dolist (type (append org-element-all-elements
35 org-element-all-objects)
36 transcoders)
37 (push `(,(intern (format "org-%s-%s" backend type))
38 (obj contents info)
39 (,(intern (format "org-element-%s-interpreter" type))
40 obj contents))
41 transcoders)))
42 ,@body)))
44 (defmacro org-test-with-parsed-data (data &rest body)
45 "Execute body with parsed data available.
47 DATA is a string containing the data to be parsed. BODY is the
48 body to execute. Parse tree is available under the `tree'
49 variable, and communication channel under `info'.
51 This function calls `org-export-collect-tree-properties'. As
52 such, `:ignore-list' (for `org-element-map') and
53 `:parse-tree' (for `org-export-get-genealogy') properties are
54 already filled in `info'."
55 (declare (debug (form body)) (indent 1))
56 `(org-test-with-temp-text ,data
57 (let* ((tree (org-element-parse-buffer))
58 (info (org-export-collect-tree-properties
59 tree (org-export-get-environment))))
60 ,@body)))
64 ;;; Tests
66 (ert-deftest test-org-export/parse-option-keyword ()
67 "Test reading all standard #+OPTIONS: items."
68 (should
69 (equal
70 (org-export-parse-option-keyword
71 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
72 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t")
73 '(:headline-levels
74 1 :preserve-breaks t :section-numbers t :time-stamp-file t
75 :with-archived-trees t :with-author t :with-creator t :with-drawers t
76 :with-email t :with-emphasize t :with-entities t :with-fixed-width t
77 :with-footnotes t :with-priority t :with-special-strings t
78 :with-sub-superscript t :with-toc t :with-tables t :with-tags t
79 :with-tasks t :with-timestamps t :with-todo-keywords t)))
80 ;; Test some special values.
81 (should
82 (equal
83 (org-export-parse-option-keyword
84 "arch:headline creator:comment d:(\"TEST\")
85 ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
86 '( :section-numbers
88 :with-archived-trees headline :with-creator comment
89 :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
90 :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
92 (ert-deftest test-org-export/get-inbuffer-options ()
93 "Test reading all standard export keywords."
94 (should
95 (equal
96 (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
97 #+CREATOR: Idem
98 #+DATE: Today
99 #+DESCRIPTION: Testing
100 #+DESCRIPTION: with two lines
101 #+EMAIL: some@email.org
102 #+EXCLUDE_TAGS: noexport invisible
103 #+KEYWORDS: test
104 #+LANGUAGE: en
105 #+SELECT_TAGS: export
106 #+TITLE: Some title
107 #+TITLE: with spaces"
108 (org-export-get-inbuffer-options))
109 '(:author
110 ("Me, Myself and I") :creator "Idem" :date ("Today")
111 :description "Testing\nwith two lines" :email "some@email.org"
112 :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
113 :select-tags ("export") :title ("Some title with spaces")))))
115 (ert-deftest test-org-export/get-subtree-options ()
116 "Test setting options from headline's properties."
117 ;; EXPORT_TITLE.
118 (org-test-with-temp-text "#+TITLE: Title
119 * Headline
120 :PROPERTIES:
121 :EXPORT_TITLE: Subtree Title
122 :END:
123 Paragraph"
124 (forward-line)
125 (should (equal (plist-get (org-export-get-environment nil t) :title)
126 '("Subtree Title"))))
127 :title
128 '("subtree-title")
129 ;; EXPORT_OPTIONS.
130 (org-test-with-temp-text "#+OPTIONS: H:1
131 * Headline
132 :PROPERTIES:
133 :EXPORT_OPTIONS: H:2
134 :END:
135 Paragraph"
136 (forward-line)
137 (should
138 (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
139 ;; EXPORT DATE.
140 (org-test-with-temp-text "#+DATE: today
141 * Headline
142 :PROPERTIES:
143 :EXPORT_DATE: 29-03-2012
144 :END:
145 Paragraph"
146 (forward-line)
147 (should (equal (plist-get (org-export-get-environment nil t) :date)
148 '("29-03-2012")))))
150 (ert-deftest test-org-export/handle-options ()
151 "Test if export options have an impact on output."
152 ;; Test exclude tags.
153 (org-test-with-temp-text "* Head1 :noexport:"
154 (org-test-with-backend test
155 (should
156 (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
157 ""))))
158 ;; Test include tags.
159 (org-test-with-temp-text "
160 * Head1
161 * Head2
162 ** Sub-Head2.1 :export:
163 *** Sub-Head2.1.1
164 * Head2"
165 (org-test-with-backend test
166 (should
167 (equal
168 "* Head2\n** Sub-Head2.1 :export:\n*** Sub-Head2.1.1\n"
169 (let ((org-tags-column 0))
170 (org-export-as 'test nil nil nil '(:select-tags ("export"))))))))
171 ;; Test mixing include tags and exclude tags.
172 (org-test-with-temp-text "
173 * Head1 :export:
174 ** Sub-Head1 :noexport:
175 ** Sub-Head2
176 * Head2 :noexport:
177 ** Sub-Head1 :export:"
178 (org-test-with-backend test
179 (should
180 (string-match
181 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
182 (org-export-as
183 'test nil nil nil
184 '(:select-tags ("export") :exclude-tags ("noexport")))))))
185 ;; Ignore tasks.
186 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
187 (org-test-with-temp-text "* TODO Head1"
188 (org-test-with-backend test
189 (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
190 "")))))
191 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
192 (org-test-with-temp-text "* TODO Head1"
193 (org-test-with-backend test
194 (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
195 "* TODO Head1\n")))))
196 ;; Archived tree.
197 (org-test-with-temp-text "* Head1 :archive:"
198 (let ((org-archive-tag "archive"))
199 (org-test-with-backend test
200 (should
201 (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
202 "")))))
203 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
204 (let ((org-archive-tag "archive"))
205 (org-test-with-backend test
206 (should
207 (string-match
208 "\\* Head1[ \t]+:archive:"
209 (org-export-as 'test nil nil nil
210 '(:with-archived-trees headline)))))))
211 (org-test-with-temp-text "* Head1 :archive:"
212 (let ((org-archive-tag "archive"))
213 (org-test-with-backend test
214 (should
215 (string-match
216 "\\`\\* Head1[ \t]+:archive:\n\\'"
217 (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
218 ;; Drawers.
219 (let ((org-drawers '("TEST")))
220 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
221 (org-test-with-backend test
222 (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
223 ""))
224 (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
225 ":TEST:\ncontents\n:END:\n")))))
226 (let ((org-drawers '("FOO" "BAR")))
227 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
228 (org-test-with-backend test
229 (should
230 (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
231 ":FOO:\nkeep\n:END:\n")))))
232 ;; Timestamps.
233 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
234 (org-test-with-backend test
235 (should
236 (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
237 "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
238 (should
239 (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
240 (should
241 (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
242 "<2012-04-29 sun. 10:45>\n"))
243 (should
244 (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
245 "[2012-04-29 sun. 10:45]\n"))))
246 ;; Clocks.
247 (let ((org-clock-string "CLOCK:"))
248 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
249 (org-test-with-backend test
250 (should
251 (equal (org-export-as 'test nil nil nil '(:with-clocks t))
252 "CLOCK: [2012-04-29 sun. 10:45]\n"))
253 (should
254 (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
255 ;; Plannings.
256 (let ((org-closed-string "CLOSED:"))
257 (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
258 (org-test-with-backend test
259 (should
260 (equal (org-export-as 'test nil nil nil '(:with-plannings t))
261 "CLOSED: [2012-04-29 sun. 10:45]\n"))
262 (should
263 (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
264 ""))))))
266 (ert-deftest test-org-export/comment-tree ()
267 "Test if export process ignores commented trees."
268 (let ((org-comment-string "COMMENT"))
269 (org-test-with-temp-text "* COMMENT Head1"
270 (org-test-with-backend test
271 (should (equal (org-export-as 'test) ""))))))
273 (ert-deftest test-org-export/export-scope ()
274 "Test all export scopes."
275 (org-test-with-temp-text "
276 * Head1
277 ** Head2
278 text
279 *** Head3"
280 (org-test-with-backend test
281 ;; Subtree.
282 (forward-line 3)
283 (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
284 ;; Visible.
285 (goto-char (point-min))
286 (forward-line)
287 (org-cycle)
288 (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
289 ;; Body only.
290 (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
291 (push '(template . org-test-template) org-test-translate-alist)
292 (should (equal (org-export-as 'test nil nil 'body-only)
293 "* Head1\n** Head2\ntext\n*** Head3\n"))
294 (should (equal (org-export-as 'test)
295 "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
296 ;; Region.
297 (goto-char (point-min))
298 (forward-line 3)
299 (transient-mark-mode 1)
300 (push-mark (point) t t)
301 (goto-char (point-at-eol))
302 (should (equal (org-export-as 'test) "text\n"))))
303 ;; Subtree with a code block calling another block outside.
304 (org-test-with-temp-text "
305 * Head1
306 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
307 <<test>>
308 #+END_SRC
309 * Head2
310 #+NAME: test
311 #+BEGIN_SRC emacs-lisp
312 \(+ 1 2)
313 #+END_SRC"
314 (org-test-with-backend test
315 (forward-line 1)
316 (should (equal (org-export-as 'test 'subtree) ": 3\n")))))
318 (ert-deftest test-org-export/expand-include ()
319 "Test file inclusion in an Org buffer."
320 ;; Full insertion with recursive inclusion.
321 (org-test-with-temp-text
322 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
323 (org-export-expand-include-keyword)
324 (should (equal (buffer-string)
325 "Small Org file with an include keyword.
327 #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
329 Success!
331 * Heading
332 body\n")))
333 ;; Localized insertion.
334 (org-test-with-temp-text
335 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
336 org-test-dir)
337 (org-export-expand-include-keyword)
338 (should (equal (buffer-string)
339 "Small Org file with an include keyword.\n")))
340 ;; Insertion with constraints on headlines level.
341 (org-test-with-temp-text
342 (format
343 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
344 org-test-dir)
345 (org-export-expand-include-keyword)
346 (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
347 ;; Inclusion within an example block.
348 (org-test-with-temp-text
349 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
350 org-test-dir)
351 (org-export-expand-include-keyword)
352 (should
353 (equal
354 (buffer-string)
355 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
356 ;; Inclusion within a src-block.
357 (org-test-with-temp-text
358 (format
359 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
360 org-test-dir)
361 (org-export-expand-include-keyword)
362 (should (equal (buffer-string)
363 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
365 (ert-deftest test-org-export/user-ignore-list ()
366 "Test if `:ignore-list' accepts user input."
367 (org-test-with-backend test
368 (flet ((skip-note-head
369 (data backend info)
370 ;; Ignore headlines with the word "note" in their title.
371 (org-element-map
372 data 'headline
373 (lambda (headline)
374 (when (string-match "\\<note\\>"
375 (org-element-property :raw-value headline))
376 (org-export-ignore-element headline info)))
377 info)
378 data))
379 ;; Install function in parse tree filters.
380 (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
381 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
382 (should (equal (org-export-as 'test) "* Head1\n")))))))
384 (ert-deftest test-org-export/before-parsing-hook ()
385 "Test `org-export-before-parsing-hook'."
386 (org-test-with-backend test
387 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
388 (let ((org-export-before-parsing-hook
389 '((lambda ()
390 (org-map-entries
391 (lambda ()
392 (delete-region (point) (progn (forward-line) (point)))))))))
393 (should (equal (org-export-as 'test) "Body 1\nBody 2\n"))))))
397 ;;; Affiliated Keywords
399 (ert-deftest test-org-export/read-attribute ()
400 "Test `org-export-read-attribute' specifications."
401 ;; Standard test.
402 (should
403 (equal
404 (org-export-read-attribute
405 :attr_html
406 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
407 (org-element-current-element)))
408 '(:a 1 :b 2)))
409 ;; Return nil on empty attribute.
410 (should-not
411 (org-export-read-attribute
412 :attr_html
413 (org-test-with-temp-text "Paragraph" (org-element-current-element)))))
417 ;;; Export Snippets
419 (ert-deftest test-org-export/export-snippet ()
420 "Test export snippets transcoding."
421 (org-test-with-temp-text "@@test:A@@@@t:B@@"
422 (org-test-with-backend test
423 (flet ((org-test-export-snippet
424 (snippet contents info)
425 (when (eq (org-export-snippet-backend snippet) 'test)
426 (org-element-property :value snippet))))
427 (let ((org-export-snippet-translation-alist nil))
428 (should (equal (org-export-as 'test) "A\n")))
429 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
430 (should (equal (org-export-as 'test) "AB\n")))))))
434 ;;; Footnotes
436 (ert-deftest test-org-export/footnotes ()
437 "Test footnotes specifications."
438 (let ((org-footnote-section nil)
439 (org-export-with-footnotes t))
440 ;; 1. Read every type of footnote.
441 (org-test-with-parsed-data
442 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
443 (should
444 (equal
445 '((1 . "A") (2 . "B") (3 . "C") (4 . "D"))
446 (org-element-map
447 tree 'footnote-reference
448 (lambda (ref)
449 (let ((def (org-export-get-footnote-definition ref info)))
450 (cons (org-export-get-footnote-number ref info)
451 (if (eq (org-element-property :type ref) 'inline) (car def)
452 (car (org-element-contents
453 (car (org-element-contents def))))))))
454 info))))
455 ;; 2. Test nested footnotes order.
456 (org-test-with-parsed-data
457 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
458 (should
459 (equal
460 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
461 (org-element-map
462 tree 'footnote-reference
463 (lambda (ref)
464 (when (org-export-footnote-first-reference-p ref info)
465 (cons (org-export-get-footnote-number ref info)
466 (org-element-property :label ref))))
467 info))))
468 ;; 3. Test nested footnote in invisible definitions.
469 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
470 ;; Hide definitions.
471 (narrow-to-region (point) (point-at-eol))
472 (let* ((tree (org-element-parse-buffer))
473 (info (org-combine-plists
474 `(:parse-tree ,tree)
475 (org-export-collect-tree-properties
476 tree (org-export-get-environment)))))
477 ;; Both footnotes should be seen.
478 (should
479 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
480 ;; 4. Test footnotes definitions collection.
481 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
483 \[fn:2] B [fn:3] [fn::D].
485 \[fn:3] C."
486 (should (= (length (org-export-collect-footnote-definitions tree info))
487 4)))
488 ;; 5. Test export of footnotes defined outside parsing scope.
489 (org-test-with-temp-text "[fn:1] Out of scope
490 * Title
491 Paragraph[fn:1]"
492 (org-test-with-backend test
493 (flet ((org-test-footnote-reference
494 (fn-ref contents info)
495 (org-element-interpret-data
496 (org-export-get-footnote-definition fn-ref info))))
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-current-element))
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-current-element))
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-current-element))
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-current-element))
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-current-element))
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-current-element))
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))))
1580 (provide 'test-org-export)
1581 ;;; test-org-export.el end here