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