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