Fix commit 128e14b
[org-mode.git] / testing / lisp / test-ox.el
blob6fa1f25fa0921f5d98a7bce87a05be88363ea821
1 ;;; test-ox.el --- Tests for ox.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 'ox)
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 ((org-export-registered-backends
35 ',(list
36 (list backend
37 :translate-alist
38 (let (transcode-table)
39 (dolist (type (append org-element-all-elements
40 org-element-all-objects)
41 transcode-table)
42 (push
43 (cons type
44 (lambda (obj contents info)
45 (funcall
46 (intern (format "org-element-%s-interpreter"
47 type))
48 obj contents)))
49 transcode-table)))))))
50 (progn ,@body)))
52 (defmacro org-test-with-parsed-data (data &rest body)
53 "Execute body with parsed data available.
55 DATA is a string containing the data to be parsed. BODY is the
56 body to execute. Parse tree is available under the `tree'
57 variable, and communication channel under `info'.
59 This function calls `org-export-collect-tree-properties'. As
60 such, `:ignore-list' (for `org-element-map') and
61 `:parse-tree' (for `org-export-get-genealogy') properties are
62 already filled in `info'."
63 (declare (debug (form body)) (indent 1))
64 `(org-test-with-temp-text ,data
65 (let* ((tree (org-element-parse-buffer))
66 (info (org-export-collect-tree-properties
67 tree (org-export-get-environment))))
68 ,@body)))
72 ;;; Internal Tests
74 (ert-deftest test-org-export/bind-keyword ()
75 "Test reading #+BIND: keywords."
76 ;; Test with `org-export-allow-bind-keywords' set to t.
77 (should
78 (org-test-with-temp-text "#+BIND: variable value"
79 (let ((org-export-allow-bind-keywords t))
80 (org-export--install-letbind-maybe)
81 (eq variable 'value))))
82 ;; Test with `org-export-allow-bind-keywords' set to nil.
83 (should-not
84 (org-test-with-temp-text "#+BIND: variable value"
85 (let ((org-export-allow-bind-keywords nil))
86 (org-export--install-letbind-maybe)
87 (boundp 'variable))))
88 ;; BIND keywords are case-insensitive.
89 (should
90 (org-test-with-temp-text "#+bind: variable value"
91 (let ((org-export-allow-bind-keywords t))
92 (org-export--install-letbind-maybe)
93 (eq variable 'value)))))
95 (ert-deftest test-org-export/parse-option-keyword ()
96 "Test reading all standard #+OPTIONS: items."
97 (should
98 (equal
99 (org-export--parse-option-keyword
100 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
101 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
102 stat:t")
103 '(:headline-levels
104 1 :preserve-breaks t :section-numbers t :time-stamp-file t
105 :with-archived-trees t :with-author t :with-creator t :with-drawers t
106 :with-email t :with-emphasize t :with-entities t :with-fixed-width t
107 :with-footnotes t :with-inlinetasks nil :with-priority t
108 :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
109 :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
110 :with-todo-keywords t)))
111 ;; Test some special values.
112 (should
113 (equal
114 (org-export--parse-option-keyword
115 "arch:headline creator:comment d:(\"TEST\")
116 ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
117 '( :section-numbers
119 :with-archived-trees headline :with-creator comment
120 :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
121 :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
123 (ert-deftest test-org-export/get-inbuffer-options ()
124 "Test reading all standard export keywords."
125 ;; Properties should follow buffer order.
126 (should
127 (equal
128 (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
129 (org-export--get-inbuffer-options))
130 '(:language "fr" :creator "Me" :email "email")))
131 ;; Parse document keywords.
132 (should
133 (equal
134 (org-test-with-temp-text "#+AUTHOR: Me"
135 (org-export--get-inbuffer-options))
136 '(:author ("Me"))))
137 ;; Test `space' behaviour.
138 (should
139 (equal
140 (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
141 (org-export--get-inbuffer-options))
142 '(:title ("Some title with spaces"))))
143 ;; Test `newline' behaviour.
144 (should
145 (equal
146 (org-test-with-temp-text "#+DESCRIPTION: With\n#+DESCRIPTION: two lines"
147 (org-export--get-inbuffer-options))
148 '(:description "With\ntwo lines")))
149 ;; Test `split' behaviour.
150 (should
151 (equal
152 (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
153 (org-export--get-inbuffer-options))
154 '(:select-tags ("a" "b"))))
155 ;; Options set through SETUPFILE.
156 (should
157 (equal
158 (org-test-with-temp-text
159 (format "#+DESCRIPTION: l1
160 #+LANGUAGE: es
161 #+SELECT_TAGS: a
162 #+TITLE: a
163 #+SETUPFILE: \"%s/examples/setupfile.org\"
164 #+DESCRIPTION: l3
165 #+LANGUAGE: fr
166 #+SELECT_TAGS: c
167 #+TITLE: c"
168 org-test-dir)
169 (org-export--get-inbuffer-options))
170 '(:description "l1\nl2\nl3":language "fr" :select-tags ("a" "b" "c")
171 :title ("a b c")))))
173 (ert-deftest test-org-export/get-subtree-options ()
174 "Test setting options from headline's properties."
175 ;; EXPORT_TITLE.
176 (org-test-with-temp-text "#+TITLE: Title
177 * Headline
178 :PROPERTIES:
179 :EXPORT_TITLE: Subtree Title
180 :END:
181 Paragraph"
182 (forward-line)
183 (should (equal (plist-get (org-export-get-environment nil t) :title)
184 '("Subtree Title"))))
185 :title
186 '("subtree-title")
187 ;; EXPORT_OPTIONS.
188 (org-test-with-temp-text "#+OPTIONS: H:1
189 * Headline
190 :PROPERTIES:
191 :EXPORT_OPTIONS: H:2
192 :END:
193 Paragraph"
194 (forward-line)
195 (should
196 (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
197 ;; EXPORT_DATE.
198 (org-test-with-temp-text "#+DATE: today
199 * Headline
200 :PROPERTIES:
201 :EXPORT_DATE: 29-03-2012
202 :END:
203 Paragraph"
204 (forward-line)
205 (should (equal (plist-get (org-export-get-environment nil t) :date)
206 '("29-03-2012"))))
207 ;; Properties with `split' behaviour are stored as a list of
208 ;; strings.
209 (should
210 (equal '("a" "b")
211 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
212 * Headline
213 :PROPERTIES:
214 :EXPORT_EXCLUDE_TAGS: a b
215 :END:
216 Paragraph"
217 (progn
218 (forward-line)
219 (plist-get (org-export-get-environment nil t) :exclude-tags)))))
220 ;; Handle :PROPERTY+: syntax.
221 (should
222 (equal '("a" "b")
223 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
224 * Headline
225 :PROPERTIES:
226 :EXPORT_EXCLUDE_TAGS: a
227 :EXPORT_EXCLUDE_TAGS+: b
228 :END:
229 Paragraph"
230 (progn
231 (forward-line)
232 (plist-get (org-export-get-environment nil t) :exclude-tags)))))
233 ;; Export properties are case-insensitive.
234 (org-test-with-temp-text "* Headline
235 :PROPERTIES:
236 :EXPORT_Date: 29-03-2012
237 :END:
238 Paragraph"
239 (should (equal (plist-get (org-export-get-environment nil t) :date)
240 '("29-03-2012"))))
241 ;; Still grab correct options when section above is empty.
242 (should
243 (equal '("H1")
244 (org-test-with-temp-text "* H1\n** H11\n** H12"
245 (progn (forward-line 2)
246 (plist-get (org-export-get-environment nil t) :title))))))
248 (ert-deftest test-org-export/handle-options ()
249 "Test if export options have an impact on output."
250 ;; Test exclude tags for headlines and inlinetasks.
251 (should
252 (equal ""
253 (org-test-with-temp-text "* Head1 :noexp:"
254 (org-test-with-backend test
255 (org-export-as 'test nil nil nil '(:exclude-tags ("noexp")))))))
256 ;; Test include tags for headlines and inlinetasks.
257 (should
258 (equal "* H2\n** Sub :exp:\n*** Sub Sub\n"
259 (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
260 (let ((org-tags-column 0))
261 (org-test-with-backend test
262 (org-export-as 'test nil nil nil '(:select-tags ("exp"))))))))
263 ;; Test mixing include tags and exclude tags.
264 (org-test-with-temp-text "
265 * Head1 :export:
266 ** Sub-Head1 :noexport:
267 ** Sub-Head2
268 * Head2 :noexport:
269 ** Sub-Head1 :export:"
270 (org-test-with-backend test
271 (should
272 (string-match
273 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
274 (org-export-as
275 'test nil nil nil
276 '(:select-tags ("export") :exclude-tags ("noexport")))))))
277 ;; Ignore tasks.
278 (should
279 (equal ""
280 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
281 (org-test-with-temp-text "* TODO Head1"
282 (org-test-with-backend test
283 (org-export-as 'test nil nil nil '(:with-tasks nil)))))))
284 (should
285 (equal "* TODO Head1\n"
286 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
287 (org-test-with-temp-text "* TODO Head1"
288 (org-test-with-backend test
289 (org-export-as 'test nil nil nil '(:with-tasks t)))))))
290 ;; Archived tree.
291 (org-test-with-temp-text "* Head1 :archive:"
292 (let ((org-archive-tag "archive"))
293 (org-test-with-backend test
294 (should
295 (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
296 "")))))
297 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
298 (let ((org-archive-tag "archive"))
299 (org-test-with-backend test
300 (should
301 (string-match
302 "\\* Head1[ \t]+:archive:"
303 (org-export-as 'test nil nil nil
304 '(:with-archived-trees headline)))))))
305 (org-test-with-temp-text "* Head1 :archive:"
306 (let ((org-archive-tag "archive"))
307 (org-test-with-backend test
308 (should
309 (string-match
310 "\\`\\* Head1[ \t]+:archive:\n\\'"
311 (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
312 ;; Clocks.
313 (let ((org-clock-string "CLOCK:"))
314 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
315 (org-test-with-backend test
316 (should
317 (equal (org-export-as 'test nil nil nil '(:with-clocks t))
318 "CLOCK: [2012-04-29 sun. 10:45]\n"))
319 (should
320 (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
321 ;; Drawers.
322 (let ((org-drawers '("TEST")))
323 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
324 (org-test-with-backend test
325 (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
326 ""))
327 (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
328 ":TEST:\ncontents\n:END:\n")))))
329 (let ((org-drawers '("FOO" "BAR")))
330 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
331 (org-test-with-backend test
332 (should
333 (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
334 ":FOO:\nkeep\n:END:\n")))))
335 (let ((org-drawers '("FOO" "BAR")))
336 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
337 (org-test-with-backend test
338 (should
339 (equal (org-export-as 'test nil nil nil '(:with-drawers (not "BAR")))
340 ":FOO:\nkeep\n:END:\n")))))
341 ;; Inlinetasks.
342 (when (featurep 'org-inlinetask)
343 (should
344 (equal
345 (let ((org-inlinetask-min-level 15))
346 (org-test-with-temp-text "*************** Task"
347 (org-test-with-backend test
348 (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
349 ""))
350 (should
351 (equal
352 (let ((org-inlinetask-min-level 15))
353 (org-test-with-temp-text
354 "*************** Task\nContents\n*************** END"
355 (org-test-with-backend test
356 (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
357 "")))
358 ;; Plannings.
359 (let ((org-closed-string "CLOSED:"))
360 (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
361 (org-test-with-backend test
362 (should
363 (equal (org-export-as 'test nil nil nil '(:with-plannings t))
364 "CLOSED: [2012-04-29 sun. 10:45]\n"))
365 (should
366 (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
367 "")))))
368 ;; Statistics cookies.
369 (should
370 (equal ""
371 (org-test-with-temp-text "[0/0]"
372 (org-test-with-backend test
373 (org-export-as
374 'test nil nil nil '(:with-statistics-cookies nil))))))
375 ;; Timestamps.
376 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
377 (org-test-with-backend test
378 (should
379 (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
380 "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
381 (should
382 (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
383 (should
384 (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
385 "<2012-04-29 sun. 10:45>\n"))
386 (should
387 (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
388 "[2012-04-29 sun. 10:45]\n")))))
390 (ert-deftest test-org-export/comment-tree ()
391 "Test if export process ignores commented trees."
392 (let ((org-comment-string "COMMENT"))
393 (org-test-with-temp-text "* COMMENT Head1"
394 (org-test-with-backend test
395 (should (equal (org-export-as 'test) ""))))))
397 (ert-deftest test-org-export/export-scope ()
398 "Test all export scopes."
399 (org-test-with-temp-text "
400 * Head1
401 ** Head2
402 text
403 *** Head3"
404 (org-test-with-backend test
405 ;; Subtree.
406 (forward-line 3)
407 (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
408 ;; Visible.
409 (goto-char (point-min))
410 (forward-line)
411 (org-cycle)
412 (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
413 ;; Region.
414 (goto-char (point-min))
415 (forward-line 3)
416 (transient-mark-mode 1)
417 (push-mark (point) t t)
418 (goto-char (point-at-eol))
419 (should (equal (org-export-as 'test) "text\n"))))
420 ;; Subtree with a code block calling another block outside.
421 (should
422 (equal ": 3\n"
423 (org-test-with-temp-text "
424 * Head1
425 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
426 <<test>>
427 #+END_SRC
428 * Head2
429 #+NAME: test
430 #+BEGIN_SRC emacs-lisp
431 \(+ 1 2)
432 #+END_SRC"
433 (org-test-with-backend test
434 (forward-line 1)
435 (org-export-as 'test 'subtree)))))
436 ;; Body only.
437 (org-test-with-temp-text "Text"
438 (org-test-with-backend test
439 (plist-put
440 (cdr (assq 'test org-export-registered-backends))
441 :translate-alist
442 (cons (cons 'template (lambda (body info) (format "BEGIN\n%sEND" body)))
443 (org-export-backend-translate-table 'test)))
444 (should (equal (org-export-as 'test nil nil 'body-only) "Text\n"))
445 (should (equal (org-export-as 'test) "BEGIN\nText\nEND")))))
447 (ert-deftest test-org-export/output-file-name ()
448 "Test `org-export-output-file-name' specifications."
449 ;; Export from a file: name is built from original file name.
450 (should
451 (org-test-with-temp-text-in-file "Test"
452 (equal (concat (file-name-as-directory ".")
453 (file-name-nondirectory
454 (file-name-sans-extension (buffer-file-name))))
455 (file-name-sans-extension (org-export-output-file-name ".ext")))))
456 ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
457 (should
458 (org-test-with-temp-text-in-file
459 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
460 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
461 ;; From a buffer not associated to a file, too.
462 (should
463 (org-test-with-temp-text
464 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
465 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
466 ;; When provided name is absolute, preserve it.
467 (should
468 (org-test-with-temp-text
469 (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
470 (expand-file-name "test"))
471 (file-name-absolute-p (org-export-output-file-name ".ext" t))))
472 ;; When PUB-DIR argument is provided, use it.
473 (should
474 (org-test-with-temp-text-in-file "Test"
475 (equal (file-name-directory
476 (org-export-output-file-name ".ext" nil "dir/"))
477 "dir/")))
478 ;; When returned name would overwrite original file, add EXTENSION
479 ;; another time.
480 (should
481 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
482 (equal (org-export-output-file-name ".org") "./normal.org.org"))))
484 (ert-deftest test-org-export/expand-include ()
485 "Test file inclusion in an Org buffer."
486 ;; Error when file isn't specified.
487 (should-error
488 (org-test-with-temp-text "#+INCLUDE: dummy.org"
489 (org-export-expand-include-keyword)))
490 ;; Full insertion with recursive inclusion.
491 (org-test-with-temp-text
492 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
493 (org-export-expand-include-keyword)
494 (should (equal (buffer-string)
495 "Small Org file with an include keyword.
497 #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
499 Success!
501 * Heading
502 body\n")))
503 ;; Localized insertion.
504 (org-test-with-temp-text
505 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
506 org-test-dir)
507 (org-export-expand-include-keyword)
508 (should (equal (buffer-string)
509 "Small Org file with an include keyword.\n")))
510 ;; Insertion with constraints on headlines level.
511 (should
512 (equal
513 "* Top heading\n** Heading\nbody\n"
514 (org-test-with-temp-text
515 (format
516 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
517 org-test-dir)
518 (org-export-expand-include-keyword)
519 (buffer-string))))
520 (should
521 (equal
522 "* Top heading\n* Heading\nbody\n"
523 (org-test-with-temp-text
524 (format
525 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\" :minlevel 1"
526 org-test-dir)
527 (org-export-expand-include-keyword)
528 (buffer-string))))
529 ;; Inclusion within an example block.
530 (org-test-with-temp-text
531 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
532 org-test-dir)
533 (org-export-expand-include-keyword)
534 (should
535 (equal
536 (buffer-string)
537 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
538 ;; Inclusion within a src-block.
539 (org-test-with-temp-text
540 (format
541 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
542 org-test-dir)
543 (org-export-expand-include-keyword)
544 (should (equal (buffer-string)
545 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
547 (ert-deftest test-org-export/expand-macro ()
548 "Test macro expansion in an Org buffer."
549 ;; Standard macro expansion.
550 (should
551 (equal "#+MACRO: macro1 value\nvalue\n"
552 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
553 (org-test-with-backend test (org-export-as 'test)))))
554 ;; Expand specific macros.
555 (should
556 (equal "me 2012-03-29 me@here Title\n"
557 (org-test-with-temp-text
559 #+TITLE: Title
560 #+DATE: 2012-03-29
561 #+AUTHOR: me
562 #+EMAIL: me@here
563 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
564 (let ((output (org-test-with-backend test (org-export-as 'test))))
565 (substring output (string-match ".*\n\\'" output))))))
566 ;; Expand specific macros when property contained a regular macro
567 ;; already.
568 (should
569 (equal "value\n"
570 (org-test-with-temp-text "
571 #+MACRO: macro1 value
572 #+TITLE: {{{macro1}}}
573 {{{title}}}"
574 (let ((output (org-test-with-backend test (org-export-as 'test))))
575 (substring output (string-match ".*\n\\'" output))))))
576 ;; Expand macros with templates in included files.
577 (should
578 (equal "success\n"
579 (org-test-with-temp-text
580 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
581 {{{included-macro}}}" org-test-dir)
582 (let ((output (org-test-with-backend test (org-export-as 'test))))
583 (substring output (string-match ".*\n\\'" output)))))))
585 (ert-deftest test-org-export/user-ignore-list ()
586 "Test if `:ignore-list' accepts user input."
587 (org-test-with-backend test
588 (flet ((skip-note-head
589 (data backend info)
590 ;; Ignore headlines with the word "note" in their title.
591 (org-element-map
592 data 'headline
593 (lambda (headline)
594 (when (string-match "\\<note\\>"
595 (org-element-property :raw-value headline))
596 (org-export-ignore-element headline info)))
597 info)
598 data))
599 ;; Install function in parse tree filters.
600 (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
601 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
602 (should (equal (org-export-as 'test) "* Head1\n")))))))
604 (ert-deftest test-org-export/before-processing-hook ()
605 "Test `org-export-before-processing-hook'."
606 (should
607 (equal
608 "#+MACRO: mac val\nTest\n"
609 (org-test-with-backend test
610 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
611 (let ((org-export-before-processing-hook
612 '((lambda (backend)
613 (while (re-search-forward "{{{" nil t)
614 (let ((object (org-element-context)))
615 (when (eq (org-element-type object) 'macro)
616 (delete-region
617 (org-element-property :begin object)
618 (org-element-property :end object)))))))))
619 (org-export-as 'test)))))))
621 (ert-deftest test-org-export/before-parsing-hook ()
622 "Test `org-export-before-parsing-hook'."
623 (should
624 (equal "Body 1\nBody 2\n"
625 (org-test-with-backend test
626 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
627 (let ((org-export-before-parsing-hook
628 '((lambda (backend)
629 (goto-char (point-min))
630 (while (re-search-forward org-outline-regexp-bol nil t)
631 (delete-region
632 (point-at-bol) (progn (forward-line) (point))))))))
633 (org-export-as 'test)))))))
637 ;;; Affiliated Keywords
639 (ert-deftest test-org-export/read-attribute ()
640 "Test `org-export-read-attribute' specifications."
641 ;; Standard test.
642 (should
643 (equal
644 (org-export-read-attribute
645 :attr_html
646 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
647 (org-element-at-point)))
648 '(:a 1 :b 2)))
649 ;; Return nil on empty attribute.
650 (should-not
651 (org-export-read-attribute
652 :attr_html
653 (org-test-with-temp-text "Paragraph" (org-element-at-point)))))
655 (ert-deftest test-org-export/get-caption ()
656 "Test `org-export-get-caption' specifications."
657 ;; Without optional argument, return long caption
658 (should
659 (equal
660 '("l")
661 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
662 (org-export-get-caption (org-element-at-point)))))
663 ;; With optional argument, return short caption.
664 (should
665 (equal
666 '("s")
667 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
668 (org-export-get-caption (org-element-at-point) t))))
669 ;; Multiple lines are separated by white spaces.
670 (should
671 (equal
672 '("a" " " "b")
673 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
674 (org-export-get-caption (org-element-at-point))))))
678 ;;; Back-End Tools
680 (ert-deftest test-org-export/define-backend ()
681 "Test back-end definition and accessors."
682 ;; Translate table.
683 (should
684 (equal '((headline . my-headline-test))
685 (let (org-export-registered-backends)
686 (org-export-define-backend test ((headline . my-headline-test)))
687 (org-export-backend-translate-table 'test))))
688 ;; Filters.
689 (should
690 (equal '((:filter-headline . my-filter))
691 (let (org-export-registered-backends)
692 (org-export-define-backend test
693 ((headline . my-headline-test))
694 :filters-alist ((:filter-headline . my-filter)))
695 (org-export-backend-filters 'test))))
696 ;; Options.
697 (should
698 (equal '((:prop value))
699 (let (org-export-registered-backends)
700 (org-export-define-backend test
701 ((headline . my-headline-test))
702 :options-alist ((:prop value)))
703 (org-export-backend-options 'test))))
704 ;; Menu.
705 (should
706 (equal '(?k "Test Export" test)
707 (let (org-export-registered-backends)
708 (org-export-define-backend test
709 ((headline . my-headline-test))
710 :menu-entry (?k "Test Export" test))
711 (org-export-backend-menu 'test))))
712 ;; Export Blocks.
713 (should
714 (equal '(("TEST" . org-element-export-block-parser))
715 (let (org-export-registered-backends org-element-block-name-alist)
716 (org-export-define-backend test
717 ((headline . my-headline-test))
718 :export-block ("test"))
719 org-element-block-name-alist))))
721 (ert-deftest test-org-export/define-derived-backend ()
722 "Test `org-export-define-derived-backend' specifications."
723 ;; Error when parent back-end is not defined.
724 (should-error
725 (let (org-export-registered-backends)
726 (org-export-define-derived-backend test parent)))
727 ;; Append translation table to parent's.
728 (should
729 (equal '((:headline . test) (:headline . parent))
730 (let (org-export-registered-backends)
731 (org-export-define-backend parent ((:headline . parent)))
732 (org-export-define-derived-backend test parent
733 :translate-alist ((:headline . test)))
734 (org-export-backend-translate-table 'test))))
735 ;; Options defined in the new back have priority over those defined
736 ;; in parent.
737 (should
738 (eq 'test
739 (let (org-export-registered-backends)
740 (org-export-define-backend parent
741 ((:headline . parent))
742 :options-alist ((:a nil nil 'parent)))
743 (org-export-define-derived-backend test parent
744 :options-alist ((:a nil nil 'test)))
745 (plist-get (org-export--get-global-options 'test) :a)))))
747 (ert-deftest test-org-export/derived-backend-p ()
748 "Test `org-export-derived-backend-p' specifications."
749 ;; Non-nil with direct match.
750 (should
751 (let (org-export-registered-backends)
752 (org-export-define-backend test ((headline . test)))
753 (org-export-derived-backend-p 'test 'test)))
754 (should
755 (let (org-export-registered-backends)
756 (org-export-define-backend test ((headline . test)))
757 (org-export-define-derived-backend test2 test)
758 (org-export-derived-backend-p 'test2 'test2)))
759 ;; Non-nil with a direct parent.
760 (should
761 (let (org-export-registered-backends)
762 (org-export-define-backend test ((headline . test)))
763 (org-export-define-derived-backend test2 test)
764 (org-export-derived-backend-p 'test2 'test)))
765 ;; Non-nil with an indirect parent.
766 (should
767 (let (org-export-registered-backends)
768 (org-export-define-backend test ((headline . test)))
769 (org-export-define-derived-backend test2 test)
770 (org-export-define-derived-backend test3 test2)
771 (org-export-derived-backend-p 'test3 'test)))
772 ;; Nil otherwise.
773 (should-not
774 (let (org-export-registered-backends)
775 (org-export-define-backend test ((headline . test)))
776 (org-export-define-backend test2 ((headline . test2)))
777 (org-export-derived-backend-p 'test2 'test)))
778 (should-not
779 (let (org-export-registered-backends)
780 (org-export-define-backend test ((headline . test)))
781 (org-export-define-backend test2 ((headline . test2)))
782 (org-export-define-derived-backend test3 test2)
783 (org-export-derived-backend-p 'test3 'test))))
785 (ert-deftest test-org-export/with-backend ()
786 "Test `org-export-with-backend' definition."
787 ;; Error when calling an undefined back-end
788 (should-error
789 (let (org-export-registered-backends)
790 (org-export-with-backend 'test "Test")))
791 ;; Error when called back-end doesn't have an appropriate
792 ;; transcoder.
793 (should-error
794 (let (org-export-registered-backends)
795 (org-export-define-backend test ((headline . ignore)))
796 (org-export-with-backend 'test "Test")))
797 ;; Otherwise, export using correct transcoder
798 (should
799 (equal "Success"
800 (let (org-export-registered-backends)
801 (org-export-define-backend test
802 ((plain-text . (lambda (text contents info) "Failure"))))
803 (org-export-define-backend test2
804 ((plain-text . (lambda (text contents info) "Success"))))
805 (org-export-with-backend 'test2 "Test")))))
807 (ert-deftest test-org-export/data-with-translations ()
808 "Test `org-export-data-with-translations' specifications."
809 (should
810 (equal
811 "Success!"
812 (org-export-data-with-translations
813 '(bold nil "Test")
814 '((plain-text . (lambda (text info) "Success"))
815 (bold . (lambda (bold contents info) (concat contents "!"))))
816 '(:with-emphasize t)))))
818 (ert-deftest test-org-export/data-with-backend ()
819 "Test `org-export-data-with-backend' specifications."
820 ;; Error when calling an undefined back-end.
821 (should-error
822 (let (org-export-registered-backends)
823 (org-export-data-with-backend 'test "Test" nil)))
824 ;; Otherwise, export data recursively, using correct back-end.
825 (should
826 (equal
827 "Success!"
828 (let (org-export-registered-backends)
829 (org-export-define-backend test
830 ((plain-text . (lambda (text info) "Success"))
831 (bold . (lambda (bold contents info) (concat contents "!")))))
832 (org-export-data-with-backend
833 '(bold nil "Test") 'test '(:with-emphasize t))))))
837 ;;; Export Snippets
839 (ert-deftest test-org-export/export-snippet ()
840 "Test export snippets transcoding."
841 (org-test-with-temp-text "@@test:A@@@@t:B@@"
842 (org-test-with-backend test
843 (plist-put
844 (cdr (assq 'test org-export-registered-backends))
845 :translate-alist
846 (cons (cons 'export-snippet
847 (lambda (snippet contents info)
848 (when (eq (org-export-snippet-backend snippet) 'test)
849 (org-element-property :value snippet))))
850 (org-export-backend-translate-table 'test)))
851 (let ((org-export-snippet-translation-alist nil))
852 (should (equal (org-export-as 'test) "A\n")))
853 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
854 (should (equal (org-export-as 'test) "AB\n"))))))
858 ;;; Footnotes
860 (ert-deftest test-org-export/footnotes ()
861 "Test footnotes specifications."
862 (let ((org-footnote-section nil)
863 (org-export-with-footnotes t))
864 ;; 1. Read every type of footnote.
865 (should
866 (equal
867 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
868 (org-test-with-parsed-data
869 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
870 (org-element-map
871 tree 'footnote-reference
872 (lambda (ref)
873 (let ((def (org-export-get-footnote-definition ref info)))
874 (cons (org-export-get-footnote-number ref info)
875 (if (eq (org-element-property :type ref) 'inline) (car def)
876 (car (org-element-contents
877 (car (org-element-contents def))))))))
878 info))))
879 ;; 2. Test nested footnotes order.
880 (org-test-with-parsed-data
881 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
882 (should
883 (equal
884 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
885 (org-element-map
886 tree 'footnote-reference
887 (lambda (ref)
888 (when (org-export-footnote-first-reference-p ref info)
889 (cons (org-export-get-footnote-number ref info)
890 (org-element-property :label ref))))
891 info))))
892 ;; 3. Test nested footnote in invisible definitions.
893 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
894 ;; Hide definitions.
895 (narrow-to-region (point) (point-at-eol))
896 (let* ((tree (org-element-parse-buffer))
897 (info (org-combine-plists
898 `(:parse-tree ,tree)
899 (org-export-collect-tree-properties
900 tree (org-export-get-environment)))))
901 ;; Both footnotes should be seen.
902 (should
903 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
904 ;; 4. Test footnotes definitions collection.
905 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
907 \[fn:2] B [fn:3] [fn::D].
909 \[fn:3] C."
910 (should (= (length (org-export-collect-footnote-definitions tree info))
911 4)))
912 ;; 5. Test export of footnotes defined outside parsing scope.
913 (org-test-with-temp-text "[fn:1] Out of scope
914 * Title
915 Paragraph[fn:1]"
916 (org-test-with-backend test
917 (plist-put
918 (cdr (assq 'test org-export-registered-backends))
919 :translate-alist
920 (cons (cons 'footnote-reference
921 (lambda (fn contents info)
922 (org-element-interpret-data
923 (org-export-get-footnote-definition fn info))))
924 (org-export-backend-translate-table 'test)))
925 (forward-line)
926 (should (equal "ParagraphOut of scope\n"
927 (org-export-as 'test 'subtree)))))
928 ;; 6. Footnotes without a definition should be provided a fallback
929 ;; definition.
930 (should
931 (org-test-with-parsed-data "[fn:1]"
932 (org-export-get-footnote-definition
933 (org-element-map tree 'footnote-reference 'identity info t) info)))
934 ;; 7. Footnote section should be ignored in TOC and in headlines
935 ;; numbering.
936 (should
937 (= 1 (let ((org-footnote-section "Footnotes"))
938 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
939 (org-export-collect-headlines info))))))
940 (should
941 (equal '(2)
942 (let ((org-footnote-section "Footnotes"))
943 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
944 (org-element-map tree 'headline
945 (lambda (hl)
946 (when (equal (org-element-property :raw-value hl) "H2")
947 (org-export-get-headline-number hl info)))
948 info t)))))))
952 ;;; Headlines and Inlinetasks
954 (ert-deftest test-org-export/get-relative-level ()
955 "Test `org-export-get-relative-level' specifications."
956 ;; Standard test.
957 (should
958 (equal '(1 2)
959 (let ((org-odd-levels-only nil))
960 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
961 (org-element-map
962 tree 'headline
963 (lambda (h) (org-export-get-relative-level h info))
964 info)))))
965 ;; Missing levels
966 (should
967 (equal '(1 3)
968 (let ((org-odd-levels-only nil))
969 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
970 (org-element-map
971 tree 'headline
972 (lambda (h) (org-export-get-relative-level h info))
973 info))))))
975 (ert-deftest test-org-export/low-level-p ()
976 "Test `org-export-low-level-p' specifications."
977 (should
978 (equal
979 '(no yes)
980 (let ((org-odd-levels-only nil))
981 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
982 (org-element-map
983 tree 'headline
984 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
985 (plist-put info :headline-levels 1)))))))
987 (ert-deftest test-org-export/get-headline-number ()
988 "Test `org-export-get-headline-number' specifications."
989 ;; Standard test.
990 (should
991 (equal
992 '((1) (1 1))
993 (let ((org-odd-levels-only nil))
994 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
995 (org-element-map
996 tree 'headline
997 (lambda (h) (org-export-get-headline-number h info))
998 info)))))
999 ;; Missing levels are replaced with 0.
1000 (should
1001 (equal
1002 '((1) (1 0 1))
1003 (let ((org-odd-levels-only nil))
1004 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
1005 (org-element-map
1006 tree 'headline
1007 (lambda (h) (org-export-get-headline-number h info))
1008 info))))))
1010 (ert-deftest test-org-export/numbered-headline-p ()
1011 "Test `org-export-numbered-headline-p' specifications."
1012 ;; If `:section-numbers' is nil, never number headlines.
1013 (should-not
1014 (org-test-with-parsed-data "* Headline"
1015 (org-element-map
1016 tree 'headline
1017 (lambda (h) (org-export-numbered-headline-p h info))
1018 (plist-put info :section-numbers nil))))
1019 ;; If `:section-numbers' is a number, only number headlines with
1020 ;; a level greater that it.
1021 (should
1022 (equal
1023 '(yes no)
1024 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1025 (org-element-map
1026 tree 'headline
1027 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
1028 (plist-put info :section-numbers 1)))))
1029 ;; Otherwise, headlines are always numbered.
1030 (should
1031 (org-test-with-parsed-data "* Headline"
1032 (org-element-map
1033 tree 'headline
1034 (lambda (h) (org-export-numbered-headline-p h info))
1035 (plist-put info :section-numbers t)))))
1037 (ert-deftest test-org-export/number-to-roman ()
1038 "Test `org-export-number-to-roman' specifications."
1039 ;; If number is negative, return it as a string.
1040 (should (equal (org-export-number-to-roman -1) "-1"))
1041 ;; Otherwise, return it as a roman number.
1042 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
1044 (ert-deftest test-org-export/get-optional-title ()
1045 "Test `org-export-get-alt-title' specifications."
1046 ;; If ALT_TITLE property is defined, use it.
1047 (should
1048 (equal '("opt")
1049 (org-test-with-parsed-data
1050 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
1051 (org-export-get-alt-title
1052 (org-element-map tree 'headline 'identity info t)
1053 info))))
1054 ;; Otherwise, fall-back to regular title.
1055 (should
1056 (equal '("Headline")
1057 (org-test-with-parsed-data "* Headline"
1058 (org-export-get-alt-title
1059 (org-element-map tree 'headline 'identity info t)
1060 info)))))
1062 (ert-deftest test-org-export/get-tags ()
1063 "Test `org-export-get-tags' specifications."
1064 (let ((org-export-exclude-tags '("noexport"))
1065 (org-export-select-tags '("export")))
1066 ;; Standard test: tags which are not a select tag, an exclude tag,
1067 ;; or specified as optional argument shouldn't be ignored.
1068 (should
1069 (org-test-with-parsed-data "* Headline :tag:"
1070 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1071 info)))
1072 ;; Exclude tags are removed.
1073 (should-not
1074 (org-test-with-parsed-data "* Headline :noexport:"
1075 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1076 info)))
1077 ;; Select tags are removed.
1078 (should-not
1079 (org-test-with-parsed-data "* Headline :export:"
1080 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1081 info)))
1082 (should
1083 (equal
1084 '("tag")
1085 (org-test-with-parsed-data "* Headline :tag:export:"
1086 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1087 info))))
1088 ;; Tags provided in the optional argument are also ignored.
1089 (should-not
1090 (org-test-with-parsed-data "* Headline :ignore:"
1091 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1092 info '("ignore"))))
1093 ;; Allow tag inheritance.
1094 (should
1095 (equal
1096 '(("tag") ("tag"))
1097 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
1098 (org-element-map
1099 tree 'headline
1100 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
1101 ;; Tag inheritance checks FILETAGS keywords.
1102 (should
1103 (equal
1104 '(("a" "b" "tag"))
1105 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
1106 (org-element-map
1107 tree 'headline
1108 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
1110 (ert-deftest test-org-export/get-node-property ()
1111 "Test`org-export-get-node-property' specifications."
1112 ;; Standard test.
1113 (should
1114 (equal "value"
1115 (org-test-with-parsed-data "* Headline
1116 :PROPERTIES:
1117 :prop: value
1118 :END:"
1119 (org-export-get-node-property
1120 :PROP (org-element-map tree 'headline 'identity nil t)))))
1121 ;; Test inheritance.
1122 (should
1123 (equal "value"
1124 (org-test-with-parsed-data "* Parent
1125 :PROPERTIES:
1126 :prop: value
1127 :END:
1128 ** Headline
1129 Paragraph"
1130 (org-export-get-node-property
1131 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
1132 ;; Cannot return a value before the first headline.
1133 (should-not
1134 (org-test-with-parsed-data "Paragraph
1135 * Headline
1136 :PROPERTIES:
1137 :prop: value
1138 :END:"
1139 (org-export-get-node-property
1140 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
1142 (ert-deftest test-org-export/get-category ()
1143 "Test `org-export-get-category' specifications."
1144 ;; Standard test.
1145 (should
1146 (equal "value"
1147 (org-test-with-parsed-data "* Headline
1148 :PROPERTIES:
1149 :CATEGORY: value
1150 :END:"
1151 (org-export-get-category
1152 (org-element-map tree 'headline 'identity nil t) info))))
1153 ;; Test inheritance from a parent headline.
1154 (should
1155 (equal '("value" "value")
1156 (org-test-with-parsed-data "* Headline1
1157 :PROPERTIES:
1158 :CATEGORY: value
1159 :END:
1160 ** Headline2"
1161 (org-element-map
1162 tree 'headline
1163 (lambda (hl) (org-export-get-category hl info)) info))))
1164 ;; Test inheritance from #+CATEGORY keyword
1165 (should
1166 (equal "value"
1167 (org-test-with-parsed-data "#+CATEGORY: value
1168 * Headline"
1169 (org-export-get-category
1170 (org-element-map tree 'headline 'identity nil t) info))))
1171 ;; Test inheritance from file name.
1172 (should
1173 (equal "test"
1174 (org-test-with-parsed-data "* Headline"
1175 (let ((info (plist-put info :input-file "~/test.org")))
1176 (org-export-get-category
1177 (org-element-map tree 'headline 'identity nil t) info)))))
1178 ;; Fall-back value.
1179 (should
1180 (equal "???"
1181 (org-test-with-parsed-data "* Headline"
1182 (org-export-get-category
1183 (org-element-map tree 'headline 'identity nil t) info)))))
1185 (ert-deftest test-org-export/first-sibling-p ()
1186 "Test `org-export-first-sibling-p' specifications."
1187 ;; Standard test.
1188 (should
1189 (equal
1190 '(yes yes no)
1191 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1192 (org-element-map
1193 tree 'headline
1194 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1195 info))))
1196 ;; Ignore headlines not exported.
1197 (should
1198 (equal
1199 '(yes)
1200 (let ((org-export-exclude-tags '("ignore")))
1201 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
1202 (org-element-map
1203 tree 'headline
1204 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1205 info))))))
1207 (ert-deftest test-org-export/last-sibling-p ()
1208 "Test `org-export-last-sibling-p' specifications."
1209 ;; Standard test.
1210 (should
1211 (equal
1212 '(yes no yes)
1213 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1214 (org-element-map
1215 tree 'headline
1216 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1217 info))))
1218 ;; Ignore headlines not exported.
1219 (should
1220 (equal
1221 '(yes)
1222 (let ((org-export-exclude-tags '("ignore")))
1223 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
1224 (org-element-map
1225 tree 'headline
1226 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1227 info))))))
1229 (ert-deftest test-org-export/handle-inlinetasks ()
1230 "Test inlinetask export."
1231 ;; Inlinetask with an exclude tag.
1232 (when (featurep 'org-inlinetask)
1233 (should
1234 (equal
1236 (let ((org-inlinetask-min-level 3))
1237 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
1238 (org-test-with-backend test
1239 (org-export-as 'test nil nil nil '(:exclude-tags ("noexp"))))))))
1240 ;; Inlinetask with an include tag.
1241 (should
1242 (equal
1243 "* H2\n*** Inline :exp:\n"
1244 (let ((org-inlinetask-min-level 3)
1245 (org-tags-column 0))
1246 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
1247 (org-test-with-backend test
1248 (org-export-as 'test nil nil nil '(:select-tags ("exp"))))))))
1249 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
1250 (should
1251 (equal ""
1252 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
1253 (org-inlinetask-min-level 3))
1254 (org-test-with-temp-text "*** TODO Inline"
1255 (org-test-with-backend test
1256 (org-export-as 'test nil nil nil '(:with-tasks nil)))))))))
1260 ;;; Links
1262 (ert-deftest test-org-export/get-coderef-format ()
1263 "Test `org-export-get-coderef-format' specifications."
1264 ;; A link without description returns "%s"
1265 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
1266 "%s"))
1267 ;; Return "%s" when path is matched within description.
1268 (should (equal (org-export-get-coderef-format "path" "desc (path)")
1269 "desc %s"))
1270 ;; Otherwise return description.
1271 (should (equal (org-export-get-coderef-format "path" "desc")
1272 "desc")))
1274 (ert-deftest test-org-export/inline-image-p ()
1275 "Test `org-export-inline-image-p' specifications."
1276 (should
1277 (org-export-inline-image-p
1278 (org-test-with-temp-text "[[#id]]"
1279 (org-element-map
1280 (org-element-parse-buffer) 'link 'identity nil t))
1281 '(("custom-id" . "id")))))
1283 (ert-deftest test-org-export/fuzzy-link ()
1284 "Test fuzzy links specifications."
1285 ;; 1. Links to invisible (keyword) targets should be ignored.
1286 (org-test-with-parsed-data
1287 "Paragraph.\n#+TARGET: Test\n[[Test]]"
1288 (should-not
1289 (org-element-map
1290 tree 'link
1291 (lambda (link)
1292 (org-export-get-ordinal
1293 (org-export-resolve-fuzzy-link link info) info)) info)))
1294 ;; 2. Link to an headline should return headline's number.
1295 (org-test-with-parsed-data
1296 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
1297 (should
1298 ;; Note: Headline's number is in fact a list of numbers.
1299 (equal '(2)
1300 (org-element-map
1301 tree 'link
1302 (lambda (link)
1303 (org-export-get-ordinal
1304 (org-export-resolve-fuzzy-link link info) info)) info t))))
1305 ;; 3. Link to a target in an item should return item's number.
1306 (org-test-with-parsed-data
1307 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
1308 (should
1309 ;; Note: Item's number is in fact a list of numbers.
1310 (equal '(1 2)
1311 (org-element-map
1312 tree 'link
1313 (lambda (link)
1314 (org-export-get-ordinal
1315 (org-export-resolve-fuzzy-link link info) info)) info t))))
1316 ;; 4. Link to a target in a footnote should return footnote's
1317 ;; number.
1318 (org-test-with-parsed-data "
1319 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
1320 (should
1321 (equal '(2 3)
1322 (org-element-map
1323 tree 'link
1324 (lambda (link)
1325 (org-export-get-ordinal
1326 (org-export-resolve-fuzzy-link link info) info)) info))))
1327 ;; 5. Link to a named element should return sequence number of that
1328 ;; element.
1329 (org-test-with-parsed-data
1330 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
1331 (should
1332 (= 2
1333 (org-element-map
1334 tree 'link
1335 (lambda (link)
1336 (org-export-get-ordinal
1337 (org-export-resolve-fuzzy-link link info) info)) info t))))
1338 ;; 6. Link to a target not within an item, a table, a footnote
1339 ;; reference or definition should return section number.
1340 (org-test-with-parsed-data
1341 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
1342 (should
1343 (equal '(2)
1344 (org-element-map
1345 tree 'link
1346 (lambda (link)
1347 (org-export-get-ordinal
1348 (org-export-resolve-fuzzy-link link info) info)) info t))))
1349 ;; 7. Space are not significant when matching a fuzzy link.
1350 (should
1351 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
1352 (org-element-map tree 'link
1353 (lambda (link) (org-export-resolve-fuzzy-link link info))
1354 info t)))
1355 ;; 8. Statistics cookies are ignored for headline match.
1356 (should
1357 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
1358 (org-element-map tree 'link
1359 (lambda (link) (org-export-resolve-fuzzy-link link info))
1360 info t)))
1361 (should
1362 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
1363 (org-element-map tree 'link
1364 (lambda (link) (org-export-resolve-fuzzy-link link info))
1365 info t))))
1367 (ert-deftest test-org-export/resolve-coderef ()
1368 "Test `org-export-resolve-coderef' specifications."
1369 (let ((org-coderef-label-format "(ref:%s)"))
1370 ;; 1. A link to a "-n -k -r" block returns line number.
1371 (org-test-with-parsed-data
1372 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
1373 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1374 (org-test-with-parsed-data
1375 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1376 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1377 ;; 2. A link to a "-n -r" block returns line number.
1378 (org-test-with-parsed-data
1379 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
1380 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1381 (org-test-with-parsed-data
1382 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1383 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1384 ;; 3. A link to a "-n" block returns coderef.
1385 (org-test-with-parsed-data
1386 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1387 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1388 (org-test-with-parsed-data
1389 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
1390 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1391 ;; 4. A link to a "-r" block returns line number.
1392 (org-test-with-parsed-data
1393 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1394 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1395 (org-test-with-parsed-data
1396 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
1397 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1398 ;; 5. A link to a block without a switch returns coderef.
1399 (org-test-with-parsed-data
1400 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1401 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1402 (org-test-with-parsed-data
1403 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
1404 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1405 ;; 6. Correctly handle continued line numbers. A "+n" switch
1406 ;; should resume numbering from previous block with numbered
1407 ;; lines, ignoring blocks not numbering lines in the process.
1408 ;; A "-n" switch resets count.
1409 (org-test-with-parsed-data "
1410 #+BEGIN_EXAMPLE -n
1411 Text.
1412 #+END_EXAMPLE
1414 #+BEGIN_SRC emacs-lisp
1415 \(- 1 1)
1416 #+END_SRC
1418 #+BEGIN_SRC emacs-lisp +n -r
1419 \(+ 1 1) (ref:addition)
1420 #+END_SRC
1422 #+BEGIN_EXAMPLE -n -r
1423 Another text. (ref:text)
1424 #+END_EXAMPLE"
1425 (should (= (org-export-resolve-coderef "addition" info) 2))
1426 (should (= (org-export-resolve-coderef "text" info) 1)))
1427 ;; 7. Recognize coderef with user-specified syntax.
1428 (org-test-with-parsed-data
1429 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
1430 (should (equal (org-export-resolve-coderef "text" info) "text")))))
1432 (ert-deftest test-org-export/resolve-fuzzy-link ()
1433 "Test `org-export-resolve-fuzzy-link' specifications."
1434 ;; 1. Match target objects.
1435 (org-test-with-parsed-data "<<target>> [[target]]"
1436 (should
1437 (org-export-resolve-fuzzy-link
1438 (org-element-map tree 'link 'identity info t) info)))
1439 ;; 2. Match target elements.
1440 (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
1441 (should
1442 (org-export-resolve-fuzzy-link
1443 (org-element-map tree 'link 'identity info t) info)))
1444 ;; 3. Match named elements.
1445 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
1446 (should
1447 (org-export-resolve-fuzzy-link
1448 (org-element-map tree 'link 'identity info t) info)))
1449 ;; 4. Match exact headline's name.
1450 (org-test-with-parsed-data "* My headline\n[[My headline]]"
1451 (should
1452 (org-export-resolve-fuzzy-link
1453 (org-element-map tree 'link 'identity info t) info)))
1454 ;; 5. Targets objects have priority over named elements and headline
1455 ;; titles.
1456 (org-test-with-parsed-data
1457 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
1458 (should
1459 (eq 'target
1460 (org-element-type
1461 (org-export-resolve-fuzzy-link
1462 (org-element-map tree 'link 'identity info t) info)))))
1463 ;; 6. Named elements have priority over headline titles.
1464 (org-test-with-parsed-data
1465 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
1466 (should
1467 (eq 'paragraph
1468 (org-element-type
1469 (org-export-resolve-fuzzy-link
1470 (org-element-map tree 'link 'identity info t) info)))))
1471 ;; 7. If link's path starts with a "*", only match headline titles,
1472 ;; though.
1473 (org-test-with-parsed-data
1474 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
1475 (should
1476 (eq 'headline
1477 (org-element-type
1478 (org-export-resolve-fuzzy-link
1479 (org-element-map tree 'link 'identity info t) info)))))
1480 ;; 8. Return nil if no match.
1481 (org-test-with-parsed-data "[[target]]"
1482 (should-not
1483 (org-export-resolve-fuzzy-link
1484 (org-element-map tree 'link 'identity info t) info))))
1486 (ert-deftest test-org-export/resolve-id-link ()
1487 "Test `org-export-resolve-id-link' specifications."
1488 ;; 1. Regular test for custom-id link.
1489 (org-test-with-parsed-data "* Headline1
1490 :PROPERTIES:
1491 :CUSTOM_ID: test
1492 :END:
1493 * Headline 2
1494 \[[#test]]"
1495 (should
1496 (org-export-resolve-id-link
1497 (org-element-map tree 'link 'identity info t) info)))
1498 ;; 2. Failing test for custom-id link.
1499 (org-test-with-parsed-data "* Headline1
1500 :PROPERTIES:
1501 :CUSTOM_ID: test
1502 :END:
1503 * Headline 2
1504 \[[#no-match]]"
1505 (should-not
1506 (org-export-resolve-id-link
1507 (org-element-map tree 'link 'identity info t) info)))
1508 ;; 3. Test for internal id target.
1509 (org-test-with-parsed-data "* Headline1
1510 :PROPERTIES:
1511 :ID: aaaa
1512 :END:
1513 * Headline 2
1514 \[[id:aaaa]]"
1515 (should
1516 (org-export-resolve-id-link
1517 (org-element-map tree 'link 'identity info t) info)))
1518 ;; 4. Test for external id target.
1519 (org-test-with-parsed-data "[[id:aaaa]]"
1520 (should
1521 (org-export-resolve-id-link
1522 (org-element-map tree 'link 'identity info t)
1523 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
1525 (ert-deftest test-org-export/resolve-radio-link ()
1526 "Test `org-export-resolve-radio-link' specifications."
1527 ;; Standard test.
1528 (should
1529 (org-test-with-temp-text "<<<radio>>> radio"
1530 (org-update-radio-target-regexp)
1531 (let* ((tree (org-element-parse-buffer))
1532 (info `(:parse-tree ,tree)))
1533 (org-export-resolve-radio-link
1534 (org-element-map tree 'link 'identity info t)
1535 info))))
1536 ;; Radio targets are case-insensitive.
1537 (should
1538 (org-test-with-temp-text "<<<RADIO>>> radio"
1539 (org-update-radio-target-regexp)
1540 (let* ((tree (org-element-parse-buffer))
1541 (info `(:parse-tree ,tree)))
1542 (org-export-resolve-radio-link
1543 (org-element-map tree 'link 'identity info t)
1544 info))))
1545 ;; Radio target with objects.
1546 (should
1547 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
1548 (org-update-radio-target-regexp)
1549 (let* ((tree (org-element-parse-buffer))
1550 (info `(:parse-tree ,tree)))
1551 (org-export-resolve-radio-link
1552 (org-element-map tree 'link 'identity info t)
1553 info)))))
1557 ;;; Src-block and example-block
1559 (ert-deftest test-org-export/unravel-code ()
1560 "Test `org-export-unravel-code' function."
1561 (let ((org-coderef-label-format "(ref:%s)"))
1562 ;; 1. Code without reference.
1563 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
1564 (should (equal (org-export-unravel-code (org-element-at-point))
1565 '("(+ 1 1)\n"))))
1566 ;; 2. Code with reference.
1567 (org-test-with-temp-text
1568 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
1569 (should (equal (org-export-unravel-code (org-element-at-point))
1570 '("(+ 1 1)\n" (1 . "test")))))
1571 ;; 3. Code with user-defined reference.
1572 (org-test-with-temp-text
1573 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
1574 (should (equal (org-export-unravel-code (org-element-at-point))
1575 '("(+ 1 1)\n" (1 . "test")))))
1576 ;; 4. Code references keys are relative to the current block.
1577 (org-test-with-temp-text "
1578 #+BEGIN_EXAMPLE -n
1579 \(+ 1 1)
1580 #+END_EXAMPLE
1581 #+BEGIN_EXAMPLE +n
1582 \(+ 2 2)
1583 \(+ 3 3) (ref:one)
1584 #+END_EXAMPLE"
1585 (goto-line 5)
1586 (should (equal (org-export-unravel-code (org-element-at-point))
1587 '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
1589 (ert-deftest test-org-export/format-code-default ()
1590 "Test `org-export-format-code-default' specifications."
1591 ;; Return the empty string when code is empty.
1592 (should
1593 (equal ""
1594 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
1595 (org-export-format-code-default
1596 (org-element-map tree 'src-block 'identity info t) info))))
1597 ;; Number lines, two whitespace characters before the actual loc.
1598 (should
1599 (equal "1 a\n2 b\n"
1600 (org-test-with-parsed-data
1601 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
1602 (org-export-format-code-default
1603 (org-element-map tree 'src-block 'identity info t) info))))
1604 ;; Put references 6 whitespace characters after the widest line,
1605 ;; wrapped within parenthesis.
1606 (should
1607 (equal "123 (a)\n1 (b)\n"
1608 (let ((org-coderef-label-format "(ref:%s)"))
1609 (org-test-with-parsed-data
1610 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
1611 (org-export-format-code-default
1612 (org-element-map tree 'src-block 'identity info t) info))))))
1616 ;;; Smart Quotes
1618 (ert-deftest test-org-export/activate-smart-quotes ()
1619 "Test `org-export-activate-smart-quotes' specifications."
1620 ;; Opening double quotes: standard test.
1621 (should
1622 (equal
1623 '("some &ldquo;paragraph")
1624 (let ((org-export-default-language "en"))
1625 (org-test-with-parsed-data "some \"paragraph"
1626 (org-element-map
1627 tree 'plain-text
1628 (lambda (s) (org-export-activate-smart-quotes s :html info))
1629 info)))))
1630 ;; Opening quotes: at the beginning of a paragraph.
1631 (should
1632 (equal
1633 '("&ldquo;begin")
1634 (let ((org-export-default-language "en"))
1635 (org-test-with-parsed-data "\"begin"
1636 (org-element-map
1637 tree 'plain-text
1638 (lambda (s) (org-export-activate-smart-quotes s :html info))
1639 info)))))
1640 ;; Opening quotes: after an object.
1641 (should
1642 (equal
1643 '("&ldquo;begin")
1644 (let ((org-export-default-language "en"))
1645 (org-test-with-parsed-data "=verb= \"begin"
1646 (org-element-map
1647 tree 'plain-text
1648 (lambda (s) (org-export-activate-smart-quotes s :html info))
1649 info)))))
1650 ;; Closing quotes: standard test.
1651 (should
1652 (equal
1653 '("some&rdquo; paragraph")
1654 (let ((org-export-default-language "en"))
1655 (org-test-with-parsed-data "some\" paragraph"
1656 (org-element-map
1657 tree 'plain-text
1658 (lambda (s) (org-export-activate-smart-quotes s :html info))
1659 info)))))
1660 ;; Closing quotes: at the end of a paragraph.
1661 (should
1662 (equal
1663 '("end&rdquo;")
1664 (let ((org-export-default-language "en"))
1665 (org-test-with-parsed-data "end\""
1666 (org-element-map
1667 tree 'plain-text
1668 (lambda (s) (org-export-activate-smart-quotes s :html info))
1669 info)))))
1670 ;; Apostrophe: standard test.
1671 (should
1672 (equal
1673 '("It shouldn&rsquo;t fail")
1674 (let ((org-export-default-language "en"))
1675 (org-test-with-parsed-data "It shouldn't fail"
1676 (org-element-map
1677 tree 'plain-text
1678 (lambda (s) (org-export-activate-smart-quotes s :html info))
1679 info)))))
1680 ;; Apostrophe: before an object.
1681 (should
1682 (equal
1683 '("a&rsquo;")
1684 (let ((org-export-default-language "en"))
1685 (org-test-with-parsed-data "a'=b="
1686 (org-element-map
1687 tree 'plain-text
1688 (lambda (s) (org-export-activate-smart-quotes s :html info))
1689 info)))))
1690 ;; Apostrophe: after an object.
1691 (should
1692 (equal
1693 '("&rsquo;s")
1694 (let ((org-export-default-language "en"))
1695 (org-test-with-parsed-data "=code='s"
1696 (org-element-map
1697 tree 'plain-text
1698 (lambda (s) (org-export-activate-smart-quotes s :html info))
1699 info)))))
1700 ;; Special case: isolated quotes.
1701 (should
1702 (equal '("&ldquo;" "&rdquo;")
1703 (let ((org-export-default-language "en"))
1704 (org-test-with-parsed-data "\"$x$\""
1705 (org-element-map
1706 tree 'plain-text
1707 (lambda (s) (org-export-activate-smart-quotes s :html info))
1708 info)))))
1709 ;; Smart quotes in secondary strings.
1710 (should
1711 (equal '("&ldquo;" "&rdquo;")
1712 (let ((org-export-default-language "en"))
1713 (org-test-with-parsed-data "* \"$x$\""
1714 (org-element-map
1715 tree 'plain-text
1716 (lambda (s) (org-export-activate-smart-quotes s :html info))
1717 info)))))
1718 ;; Smart quotes in document keywords.
1719 (should
1720 (equal '("&ldquo;" "&rdquo;")
1721 (let ((org-export-default-language "en"))
1722 (org-test-with-parsed-data "#+TITLE: \"$x$\""
1723 (org-element-map
1724 (plist-get info :title) 'plain-text
1725 (lambda (s) (org-export-activate-smart-quotes s :html info))
1726 info)))))
1727 ;; Smart quotes in parsed affiliated keywords.
1728 (should
1729 (equal '("&ldquo;" "&rdquo;" "Paragraph")
1730 (let ((org-export-default-language "en"))
1731 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
1732 (org-element-map
1733 tree 'plain-text
1734 (lambda (s) (org-export-activate-smart-quotes s :html info))
1735 info nil nil t))))))
1739 ;;; Tables
1741 (ert-deftest test-org-export/special-column ()
1742 "Test if the table's special column is properly recognized."
1743 ;; 1. First column is special if it contains only a special marking
1744 ;; characters or empty cells.
1745 (org-test-with-temp-text "
1746 | ! | 1 |
1747 | | 2 |"
1748 (should
1749 (org-export-table-has-special-column-p
1750 (org-element-map
1751 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1752 ;; 2. If the column contains anything else, it isn't special.
1753 (org-test-with-temp-text "
1754 | ! | 1 |
1755 | b | 2 |"
1756 (should-not
1757 (org-export-table-has-special-column-p
1758 (org-element-map
1759 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1760 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
1761 ;; and "!".
1762 (org-test-with-temp-text "
1763 | # | 1 |
1764 | ^ | 2 |
1765 | * | 3 |
1766 | _ | 4 |
1767 | / | 5 |
1768 | $ | 6 |
1769 | ! | 7 |"
1770 (should
1771 (org-export-table-has-special-column-p
1772 (org-element-map
1773 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1774 ;; 4. A first column with only empty cells isn't considered as
1775 ;; special.
1776 (org-test-with-temp-text "
1777 | | 1 |
1778 | | 2 |"
1779 (should-not
1780 (org-export-table-has-special-column-p
1781 (org-element-map
1782 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
1784 (ert-deftest test-org-export/table-row-is-special-p ()
1785 "Test `org-export-table-row-is-special-p' specifications."
1786 ;; 1. A row is special if it has a special marking character in the
1787 ;; special column.
1788 (org-test-with-parsed-data "| ! | 1 |"
1789 (should
1790 (org-export-table-row-is-special-p
1791 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1792 ;; 2. A row is special when its first field is "/"
1793 (org-test-with-parsed-data "
1794 | / | 1 |
1795 | a | b |"
1796 (should
1797 (org-export-table-row-is-special-p
1798 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1799 ;; 3. A row only containing alignment cookies is also considered as
1800 ;; special.
1801 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
1802 (should
1803 (org-export-table-row-is-special-p
1804 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1805 ;; 4. Everything else isn't considered as special.
1806 (org-test-with-parsed-data "| \alpha | | c |"
1807 (should-not
1808 (org-export-table-row-is-special-p
1809 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1810 ;; 5. Table's rules are never considered as special rows.
1811 (org-test-with-parsed-data "|---+---|"
1812 (should-not
1813 (org-export-table-row-is-special-p
1814 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
1816 (ert-deftest test-org-export/has-header-p ()
1817 "Test `org-export-table-has-header-p' specifications."
1818 ;; 1. With an header.
1819 (org-test-with-parsed-data "
1820 | a | b |
1821 |---+---|
1822 | c | d |"
1823 (should
1824 (org-export-table-has-header-p
1825 (org-element-map tree 'table 'identity info 'first-match)
1826 info)))
1827 ;; 2. Without an header.
1828 (org-test-with-parsed-data "
1829 | a | b |
1830 | c | d |"
1831 (should-not
1832 (org-export-table-has-header-p
1833 (org-element-map tree 'table 'identity info 'first-match)
1834 info)))
1835 ;; 3. Don't get fooled with starting and ending rules.
1836 (org-test-with-parsed-data "
1837 |---+---|
1838 | a | b |
1839 | c | d |
1840 |---+---|"
1841 (should-not
1842 (org-export-table-has-header-p
1843 (org-element-map tree 'table 'identity info 'first-match)
1844 info))))
1846 (ert-deftest test-org-export/table-row-group ()
1847 "Test `org-export-table-row-group' specifications."
1848 ;; 1. A rule creates a new group.
1849 (org-test-with-parsed-data "
1850 | a | b |
1851 |---+---|
1852 | 1 | 2 |"
1853 (should
1854 (equal
1855 '(1 nil 2)
1856 (mapcar (lambda (row) (org-export-table-row-group row info))
1857 (org-element-map tree 'table-row 'identity)))))
1858 ;; 2. Special rows are ignored in count.
1859 (org-test-with-parsed-data "
1860 | / | < | > |
1861 |---|---+---|
1862 | | 1 | 2 |"
1863 (should
1864 (equal
1865 '(nil nil 1)
1866 (mapcar (lambda (row) (org-export-table-row-group row info))
1867 (org-element-map tree 'table-row 'identity)))))
1868 ;; 3. Double rules also are ignored in count.
1869 (org-test-with-parsed-data "
1870 | a | b |
1871 |---+---|
1872 |---+---|
1873 | 1 | 2 |"
1874 (should
1875 (equal
1876 '(1 nil nil 2)
1877 (mapcar (lambda (row) (org-export-table-row-group row info))
1878 (org-element-map tree 'table-row 'identity))))))
1880 (ert-deftest test-org-export/table-cell-width ()
1881 "Test `org-export-table-cell-width' specifications."
1882 ;; 1. Width is primarily determined by width cookies. If no cookie
1883 ;; is found, cell's width is nil.
1884 (org-test-with-parsed-data "
1885 | / | <l> | <6> | <l7> |
1886 | | a | b | c |"
1887 (should
1888 (equal
1889 '(nil 6 7)
1890 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1891 (org-element-map tree 'table-cell 'identity info)))))
1892 ;; 2. The last width cookie has precedence.
1893 (org-test-with-parsed-data "
1894 | <6> |
1895 | <7> |
1896 | a |"
1897 (should
1898 (equal
1899 '(7)
1900 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1901 (org-element-map tree 'table-cell 'identity info)))))
1902 ;; 3. Valid width cookies must have a specific row.
1903 (org-test-with-parsed-data "| <6> | cell |"
1904 (should
1905 (equal
1906 '(nil nil)
1907 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1908 (org-element-map tree 'table-cell 'identity))))))
1910 (ert-deftest test-org-export/table-cell-alignment ()
1911 "Test `org-export-table-cell-alignment' specifications."
1912 (let ((org-table-number-fraction 0.5)
1913 (org-table-number-regexp "^[0-9]+$"))
1914 ;; 1. Alignment is primarily determined by alignment cookies.
1915 (org-test-with-temp-text "| <l> | <c> | <r> |"
1916 (let* ((tree (org-element-parse-buffer))
1917 (info `(:parse-tree ,tree)))
1918 (should
1919 (equal
1920 '(left center right)
1921 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
1922 (org-element-map tree 'table-cell 'identity))))))
1923 ;; 2. The last alignment cookie has precedence.
1924 (org-test-with-parsed-data "
1925 | <l8> |
1926 | cell |
1927 | <r9> |"
1928 (should
1929 (equal
1930 '(right right right)
1931 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
1932 (org-element-map tree 'table-cell 'identity)))))
1933 ;; 3. If there's no cookie, cell's contents determine alignment.
1934 ;; A column mostly made of cells containing numbers will align
1935 ;; its cells to the right.
1936 (org-test-with-parsed-data "
1937 | 123 |
1938 | some text |
1939 | 12345 |"
1940 (should
1941 (equal
1942 '(right right right)
1943 (mapcar (lambda (cell)
1944 (org-export-table-cell-alignment cell info))
1945 (org-element-map tree 'table-cell 'identity)))))
1946 ;; 4. Otherwise, they will be aligned to the left.
1947 (org-test-with-parsed-data "
1948 | text |
1949 | some text |
1950 | \alpha |"
1951 (should
1952 (equal
1953 '(left left left)
1954 (mapcar (lambda (cell)
1955 (org-export-table-cell-alignment cell info))
1956 (org-element-map tree 'table-cell 'identity)))))))
1958 (ert-deftest test-org-export/table-cell-borders ()
1959 "Test `org-export-table-cell-borders' specifications."
1960 ;; 1. Recognize various column groups indicators.
1961 (org-test-with-parsed-data "| / | < | > | <> |"
1962 (should
1963 (equal
1964 '((right bottom top) (left bottom top) (right bottom top)
1965 (right left bottom top))
1966 (mapcar (lambda (cell)
1967 (org-export-table-cell-borders cell info))
1968 (org-element-map tree 'table-cell 'identity)))))
1969 ;; 2. Accept shortcuts to define column groups.
1970 (org-test-with-parsed-data "| / | < | < |"
1971 (should
1972 (equal
1973 '((right bottom top) (right left bottom top) (left bottom top))
1974 (mapcar (lambda (cell)
1975 (org-export-table-cell-borders cell info))
1976 (org-element-map tree 'table-cell 'identity)))))
1977 ;; 3. A valid column groups row must start with a "/".
1978 (org-test-with-parsed-data "
1979 | | < |
1980 | a | b |"
1981 (should
1982 (equal '((top) (top) (bottom) (bottom))
1983 (mapcar (lambda (cell)
1984 (org-export-table-cell-borders cell info))
1985 (org-element-map tree 'table-cell 'identity)))))
1986 ;; 4. Take table rules into consideration.
1987 (org-test-with-parsed-data "
1988 | 1 |
1989 |---|
1990 | 2 |"
1991 (should
1992 (equal '((below top) (bottom above))
1993 (mapcar (lambda (cell)
1994 (org-export-table-cell-borders cell info))
1995 (org-element-map tree 'table-cell 'identity)))))
1996 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
1997 ;; (resp. `bottom' and `below') borders. Any special row is
1998 ;; ignored.
1999 (org-test-with-parsed-data "
2000 |---+----|
2001 | / | |
2002 | | 1 |
2003 |---+----|"
2004 (should
2005 (equal '((bottom below top above))
2006 (last
2007 (mapcar (lambda (cell)
2008 (org-export-table-cell-borders cell info))
2009 (org-element-map tree 'table-cell 'identity)))))))
2011 (ert-deftest test-org-export/table-dimensions ()
2012 "Test `org-export-table-dimensions' specifications."
2013 ;; 1. Standard test.
2014 (org-test-with-parsed-data "
2015 | 1 | 2 | 3 |
2016 | 4 | 5 | 6 |"
2017 (should
2018 (equal '(2 . 3)
2019 (org-export-table-dimensions
2020 (org-element-map tree 'table 'identity info 'first-match) info))))
2021 ;; 2. Ignore horizontal rules and special columns.
2022 (org-test-with-parsed-data "
2023 | / | < | > |
2024 | 1 | 2 | 3 |
2025 |---+---+---|
2026 | 4 | 5 | 6 |"
2027 (should
2028 (equal '(2 . 3)
2029 (org-export-table-dimensions
2030 (org-element-map tree 'table 'identity info 'first-match) info)))))
2032 (ert-deftest test-org-export/table-cell-address ()
2033 "Test `org-export-table-cell-address' specifications."
2034 ;; 1. Standard test: index is 0-based.
2035 (org-test-with-parsed-data "| a | b |"
2036 (should
2037 (equal '((0 . 0) (0 . 1))
2038 (org-element-map
2039 tree 'table-cell
2040 (lambda (cell) (org-export-table-cell-address cell info))
2041 info))))
2042 ;; 2. Special column isn't counted, nor are special rows.
2043 (org-test-with-parsed-data "
2044 | / | <> |
2045 | | c |"
2046 (should
2047 (equal '(0 . 0)
2048 (org-export-table-cell-address
2049 (car (last (org-element-map tree 'table-cell 'identity info)))
2050 info))))
2051 ;; 3. Tables rules do not count either.
2052 (org-test-with-parsed-data "
2053 | a |
2054 |---|
2055 | b |
2056 |---|
2057 | c |"
2058 (should
2059 (equal '(2 . 0)
2060 (org-export-table-cell-address
2061 (car (last (org-element-map tree 'table-cell 'identity info)))
2062 info))))
2063 ;; 4. Return nil for special cells.
2064 (org-test-with-parsed-data "| / | a |"
2065 (should-not
2066 (org-export-table-cell-address
2067 (org-element-map tree 'table-cell 'identity nil 'first-match)
2068 info))))
2070 (ert-deftest test-org-export/get-table-cell-at ()
2071 "Test `org-export-get-table-cell-at' specifications."
2072 ;; 1. Address ignores special columns, special rows and rules.
2073 (org-test-with-parsed-data "
2074 | / | <> |
2075 | | a |
2076 |---+----|
2077 | | b |"
2078 (should
2079 (equal '("b")
2080 (org-element-contents
2081 (org-export-get-table-cell-at
2082 '(1 . 0)
2083 (org-element-map tree 'table 'identity info 'first-match)
2084 info)))))
2085 ;; 2. Return value for a non-existent address is nil.
2086 (org-test-with-parsed-data "| a |"
2087 (should-not
2088 (org-export-get-table-cell-at
2089 '(2 . 2)
2090 (org-element-map tree 'table 'identity info 'first-match)
2091 info)))
2092 (org-test-with-parsed-data "| / |"
2093 (should-not
2094 (org-export-get-table-cell-at
2095 '(0 . 0)
2096 (org-element-map tree 'table 'identity info 'first-match)
2097 info))))
2099 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
2100 "Test `org-export-table-cell-starts-colgroup-p' specifications."
2101 ;; 1. A cell at a beginning of a row always starts a column group.
2102 (org-test-with-parsed-data "| a |"
2103 (should
2104 (org-export-table-cell-starts-colgroup-p
2105 (org-element-map tree 'table-cell 'identity info 'first-match)
2106 info)))
2107 ;; 2. Special column should be ignored when determining the
2108 ;; beginning of the row.
2109 (org-test-with-parsed-data "
2110 | / | |
2111 | | a |"
2112 (should
2113 (org-export-table-cell-starts-colgroup-p
2114 (org-element-map tree 'table-cell 'identity info 'first-match)
2115 info)))
2116 ;; 2. Explicit column groups.
2117 (org-test-with-parsed-data "
2118 | / | | < |
2119 | a | b | c |"
2120 (should
2121 (equal
2122 '(yes no yes)
2123 (org-element-map
2124 tree 'table-cell
2125 (lambda (cell)
2126 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
2127 info)))))
2129 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
2130 "Test `org-export-table-cell-ends-colgroup-p' specifications."
2131 ;; 1. A cell at the end of a row always ends a column group.
2132 (org-test-with-parsed-data "| a |"
2133 (should
2134 (org-export-table-cell-ends-colgroup-p
2135 (org-element-map tree 'table-cell 'identity info 'first-match)
2136 info)))
2137 ;; 2. Special column should be ignored when determining the
2138 ;; beginning of the row.
2139 (org-test-with-parsed-data "
2140 | / | |
2141 | | a |"
2142 (should
2143 (org-export-table-cell-ends-colgroup-p
2144 (org-element-map tree 'table-cell 'identity info 'first-match)
2145 info)))
2146 ;; 3. Explicit column groups.
2147 (org-test-with-parsed-data "
2148 | / | < | |
2149 | a | b | c |"
2150 (should
2151 (equal
2152 '(yes no yes)
2153 (org-element-map
2154 tree 'table-cell
2155 (lambda (cell)
2156 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
2157 info)))))
2159 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
2160 "Test `org-export-table-row-starts-rowgroup-p' specifications."
2161 ;; 1. A row at the beginning of a table always starts a row group.
2162 ;; So does a row following a table rule.
2163 (org-test-with-parsed-data "
2164 | a |
2165 |---|
2166 | b |"
2167 (should
2168 (equal
2169 '(yes no yes)
2170 (org-element-map
2171 tree 'table-row
2172 (lambda (row)
2173 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2174 info))))
2175 ;; 2. Special rows should be ignored when determining the beginning
2176 ;; of the row.
2177 (org-test-with-parsed-data "
2178 | / | < |
2179 | | a |
2180 |---+---|
2181 | / | < |
2182 | | b |"
2183 (should
2184 (equal
2185 '(yes no yes)
2186 (org-element-map
2187 tree 'table-row
2188 (lambda (row)
2189 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2190 info)))))
2192 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
2193 "Test `org-export-table-row-ends-rowgroup-p' specifications."
2194 ;; 1. A row at the end of a table always ends a row group. So does
2195 ;; a row preceding a table rule.
2196 (org-test-with-parsed-data "
2197 | a |
2198 |---|
2199 | b |"
2200 (should
2201 (equal
2202 '(yes no yes)
2203 (org-element-map
2204 tree 'table-row
2205 (lambda (row)
2206 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2207 info))))
2208 ;; 2. Special rows should be ignored when determining the beginning
2209 ;; of the row.
2210 (org-test-with-parsed-data "
2211 | | a |
2212 | / | < |
2213 |---+---|
2214 | | b |
2215 | / | < |"
2216 (should
2217 (equal
2218 '(yes no yes)
2219 (org-element-map
2220 tree 'table-row
2221 (lambda (row)
2222 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2223 info)))))
2225 (ert-deftest test-org-export/table-row-starts-header-p ()
2226 "Test `org-export-table-row-starts-header-p' specifications."
2227 ;; 1. Only the row starting the first row group starts the table
2228 ;; header.
2229 (org-test-with-parsed-data "
2230 | a |
2231 | b |
2232 |---|
2233 | c |"
2234 (should
2235 (equal
2236 '(yes no no no)
2237 (org-element-map
2238 tree 'table-row
2239 (lambda (row)
2240 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
2241 info))))
2242 ;; 2. A row cannot start an header if there's no header in the
2243 ;; table.
2244 (org-test-with-parsed-data "
2245 | a |
2246 |---|"
2247 (should-not
2248 (org-export-table-row-starts-header-p
2249 (org-element-map tree 'table-row 'identity info 'first-match)
2250 info))))
2252 (ert-deftest test-org-export/table-row-ends-header-p ()
2253 "Test `org-export-table-row-ends-header-p' specifications."
2254 ;; 1. Only the row starting the first row group starts the table
2255 ;; header.
2256 (org-test-with-parsed-data "
2257 | a |
2258 | b |
2259 |---|
2260 | c |"
2261 (should
2262 (equal
2263 '(no yes no no)
2264 (org-element-map
2265 tree 'table-row
2266 (lambda (row)
2267 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
2268 info))))
2269 ;; 2. A row cannot start an header if there's no header in the
2270 ;; table.
2271 (org-test-with-parsed-data "
2272 | a |
2273 |---|"
2274 (should-not
2275 (org-export-table-row-ends-header-p
2276 (org-element-map tree 'table-row 'identity info 'first-match)
2277 info))))
2281 ;;; Templates
2283 (ert-deftest test-org-export/inner-template ()
2284 "Test `inner-template' translator specifications."
2285 (should
2286 (equal "Success!"
2287 (let (org-export-registered-backends)
2288 (org-export-define-backend test
2289 ((inner-template . (lambda (contents info) "Success!"))
2290 (headline . (lambda (h c i) "Headline"))))
2291 (org-test-with-temp-text "* Headline"
2292 (org-export-as 'test)))))
2293 ;; Inner template is applied even in a "body-only" export.
2294 (should
2295 (equal "Success!"
2296 (let (org-export-registered-backends)
2297 (org-export-define-backend test
2298 ((inner-template . (lambda (contents info) "Success!"))
2299 (headline . (lambda (h c i) "Headline"))))
2300 (org-test-with-temp-text "* Headline"
2301 (org-export-as 'test nil nil 'body-only))))))
2303 (ert-deftest test-org-export/template ()
2304 "Test `template' translator specifications."
2305 (should
2306 (equal "Success!"
2307 (let (org-export-registered-backends)
2308 (org-export-define-backend test
2309 ((template . (lambda (contents info) "Success!"))
2310 (headline . (lambda (h c i) "Headline"))))
2311 (org-test-with-temp-text "* Headline"
2312 (org-export-as 'test)))))
2313 ;; Template is not applied in a "body-only" export.
2314 (should-not
2315 (equal "Success!"
2316 (let (org-export-registered-backends)
2317 (org-export-define-backend test
2318 ((template . (lambda (contents info) "Success!"))
2319 (headline . (lambda (h c i) "Headline"))))
2320 (org-test-with-temp-text "* Headline"
2321 (org-export-as 'test nil nil 'body-only))))))
2325 ;;; Topology
2327 (ert-deftest test-org-export/get-next-element ()
2328 "Test `org-export-get-next-element' specifications."
2329 ;; Standard test.
2330 (should
2331 (equal "b"
2332 (org-test-with-parsed-data "* Headline\n*a* b"
2333 (org-export-get-next-element
2334 (org-element-map tree 'bold 'identity info t) info))))
2335 ;; Return nil when no previous element.
2336 (should-not
2337 (org-test-with-parsed-data "* Headline\na *b*"
2338 (org-export-get-next-element
2339 (org-element-map tree 'bold 'identity info t) info)))
2340 ;; Non-exportable elements are ignored.
2341 (should-not
2342 (let ((org-export-with-timestamps nil))
2343 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
2344 (org-export-get-next-element
2345 (org-element-map tree 'entity 'identity info t) info))))
2346 ;; Find next element in secondary strings.
2347 (should
2348 (eq 'verbatim
2349 (org-test-with-parsed-data "* a =verb="
2350 (org-element-type
2351 (org-export-get-next-element
2352 (org-element-map tree 'plain-text 'identity info t) info)))))
2353 ;; Find next element in document keywords.
2354 (should
2355 (eq 'verbatim
2356 (org-test-with-parsed-data "#+TITLE: a =verb="
2357 (org-element-type
2358 (org-export-get-next-element
2359 (org-element-map
2360 (plist-get info :title) 'plain-text 'identity info t) info)))))
2361 ;; Find next element in parsed affiliated keywords.
2362 (should
2363 (eq 'verbatim
2364 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
2365 (org-element-type
2366 (org-export-get-next-element
2367 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2368 ;; With optional argument N, return a list containing all the
2369 ;; following elements.
2370 (should
2371 (equal
2372 '(bold code underline)
2373 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2374 (mapcar 'car
2375 (org-export-get-next-element
2376 (org-element-map tree 'italic 'identity info t) info t)))))
2377 ;; When N is a positive integer, return a list containing up to
2378 ;; N following elements.
2379 (should
2380 (equal
2381 '(bold code)
2382 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2383 (mapcar 'car
2384 (org-export-get-next-element
2385 (org-element-map tree 'italic 'identity info t) info 2))))))
2387 (ert-deftest test-org-export/get-previous-element ()
2388 "Test `org-export-get-previous-element' specifications."
2389 ;; Standard test.
2390 (should
2391 (equal "a "
2392 (org-test-with-parsed-data "* Headline\na *b*"
2393 (org-export-get-previous-element
2394 (org-element-map tree 'bold 'identity info t) info))))
2395 ;; Return nil when no previous element.
2396 (should-not
2397 (org-test-with-parsed-data "* Headline\n*a* b"
2398 (org-export-get-previous-element
2399 (org-element-map tree 'bold 'identity info t) info)))
2400 ;; Non-exportable elements are ignored.
2401 (should-not
2402 (let ((org-export-with-timestamps nil))
2403 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
2404 (org-export-get-previous-element
2405 (org-element-map tree 'entity 'identity info t) info))))
2406 ;; Find previous element in secondary strings.
2407 (should
2408 (eq 'verbatim
2409 (org-test-with-parsed-data "* =verb= a"
2410 (org-element-type
2411 (org-export-get-previous-element
2412 (org-element-map tree 'plain-text 'identity info t) info)))))
2413 ;; Find previous element in document keywords.
2414 (should
2415 (eq 'verbatim
2416 (org-test-with-parsed-data "#+TITLE: =verb= a"
2417 (org-element-type
2418 (org-export-get-previous-element
2419 (org-element-map
2420 (plist-get info :title) 'plain-text 'identity info t) info)))))
2421 ;; Find previous element in parsed affiliated keywords.
2422 (should
2423 (eq 'verbatim
2424 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
2425 (org-element-type
2426 (org-export-get-previous-element
2427 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2428 ;; With optional argument N, return a list containing up to
2429 ;; N previous elements.
2430 (should
2431 (equal '(underline italic bold)
2432 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
2433 (mapcar 'car
2434 (org-export-get-previous-element
2435 (org-element-map tree 'code 'identity info t) info t)))))
2436 ;; When N is a positive integer, return a list containing up to
2437 ;; N previous elements.
2438 (should
2439 (equal '(italic bold)
2440 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
2441 (mapcar 'car
2442 (org-export-get-previous-element
2443 (org-element-map tree 'code 'identity info t) info 2))))))
2446 (provide 'test-ox)
2447 ;;; test-org-export.el end here