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