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