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