org-mtags: remove
[org-mode.git] / testing / lisp / test-ox.el
blob234032ed576255c4c47a8f1418535c8220239261
1 ;;; test-ox.el --- Tests for ox.el
3 ;; Copyright (C) 2012, 2013, 2014 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 (defun org-test-default-backend ()
28 "Return a default export back-end.
29 This back-end simply returns parsed data as Org syntax."
30 (org-export-create-backend
31 :transcoders (let (transcode-table)
32 (dolist (type (append org-element-all-elements
33 org-element-all-objects)
34 transcode-table)
35 (push
36 (cons type
37 (lambda (obj contents info)
38 (funcall
39 (intern (format "org-element-%s-interpreter"
40 type))
41 obj contents)))
42 transcode-table)))))
44 (defmacro org-test-with-parsed-data (data &rest body)
45 "Execute body with parsed data available.
47 DATA is a string containing the data to be parsed. BODY is the
48 body to execute. Parse tree is available under the `tree'
49 variable, and communication channel under `info'.
51 This function calls `org-export-collect-tree-properties'. As
52 such, `:ignore-list' (for `org-element-map') and
53 `:parse-tree' (for `org-export-get-genealogy') properties are
54 already filled in `info'."
55 (declare (debug (form body)) (indent 1))
56 `(org-test-with-temp-text ,data
57 (let* ((tree (org-element-parse-buffer))
58 (info (org-export-collect-tree-properties
59 tree (org-export-get-environment))))
60 ,@body)))
64 ;;; Internal Tests
66 (ert-deftest test-org-export/bind-keyword ()
67 "Test reading #+BIND: keywords."
68 ;; Test with `org-export-allow-bind-keywords' set to t.
69 (should
70 (org-test-with-temp-text "#+BIND: test-ox-var value"
71 (let ((org-export-allow-bind-keywords t))
72 (org-export-get-environment)
73 (eq test-ox-var 'value))))
74 ;; Test with `org-export-allow-bind-keywords' set to nil.
75 (should-not
76 (org-test-with-temp-text "#+BIND: test-ox-var value"
77 (let ((org-export-allow-bind-keywords nil))
78 (org-export-get-environment)
79 (boundp 'test-ox-var))))
80 ;; BIND keywords are case-insensitive.
81 (should
82 (org-test-with-temp-text "#+bind: test-ox-var value"
83 (let ((org-export-allow-bind-keywords t))
84 (org-export-get-environment)
85 (eq test-ox-var 'value))))
86 ;; Preserve order of BIND keywords.
87 (should
88 (org-test-with-temp-text "#+BIND: test-ox-var 1\n#+BIND: test-ox-var 2"
89 (let ((org-export-allow-bind-keywords t))
90 (org-export-get-environment)
91 (eq test-ox-var 2))))
92 ;; Read BIND keywords in setup files.
93 (should
94 (org-test-with-temp-text
95 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
96 (let ((org-export-allow-bind-keywords t))
97 (org-export-get-environment)
98 (eq variable 'value))))
99 ;; Verify that bound variables are seen during export.
100 (should
101 (equal "Yes\n"
102 (org-test-with-temp-text "#+BIND: test-ox-var value"
103 (let ((org-export-allow-bind-keywords t))
104 (org-export-as
105 (org-export-create-backend
106 :transcoders
107 '((section . (lambda (s c i)
108 (if (eq test-ox-var 'value) "Yes" "No")))))))))))
110 (ert-deftest test-org-export/parse-option-keyword ()
111 "Test reading all standard #+OPTIONS: items."
112 (should
113 (equal
114 (org-export--parse-option-keyword
115 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
116 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
117 stat:t")
118 '(:headline-levels
119 1 :preserve-breaks t :section-numbers t :time-stamp-file t
120 :with-archived-trees t :with-author t :with-creator t :with-drawers t
121 :with-email t :with-emphasize t :with-entities t :with-fixed-width t
122 :with-footnotes t :with-inlinetasks nil :with-priority t
123 :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
124 :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
125 :with-todo-keywords t)))
126 ;; Test some special values.
127 (should
128 (equal
129 (org-export--parse-option-keyword
130 "arch:headline creator:comment d:(\"TEST\")
131 ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
132 '( :section-numbers
134 :with-archived-trees headline :with-creator comment
135 :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
136 :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
138 (ert-deftest test-org-export/get-inbuffer-options ()
139 "Test reading all standard export keywords."
140 ;; Properties should follow buffer order.
141 (should
142 (equal
143 (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
144 (org-export--get-inbuffer-options))
145 '(:language "fr" :creator "Me" :email "email")))
146 ;; Parse document keywords.
147 (should
148 (equal
149 (org-test-with-temp-text "#+AUTHOR: Me"
150 (org-export--get-inbuffer-options))
151 '(:author ("Me"))))
152 ;; Test `space' behaviour.
153 (should
154 (equal
155 (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
156 (org-export--get-inbuffer-options))
157 '(:title ("Some title with spaces"))))
158 ;; Test `newline' behaviour.
159 (should
160 (equal
161 (org-test-with-temp-text "#+DESCRIPTION: With\n#+DESCRIPTION: two lines"
162 (org-export--get-inbuffer-options))
163 '(:description "With\ntwo lines")))
164 ;; Test `split' behaviour.
165 (should
166 (equal
167 (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
168 (org-export--get-inbuffer-options))
169 '(:select-tags ("a" "b"))))
170 ;; Options set through SETUPFILE.
171 (should
172 (equal
173 (org-test-with-temp-text
174 (format "#+DESCRIPTION: l1
175 #+LANGUAGE: es
176 #+SELECT_TAGS: a
177 #+TITLE: a
178 #+SETUPFILE: \"%s/examples/setupfile.org\"
179 #+DESCRIPTION: l3
180 #+LANGUAGE: fr
181 #+SELECT_TAGS: c
182 #+TITLE: c"
183 org-test-dir)
184 (org-export--get-inbuffer-options))
185 '(:description "l1\nl2\nl3":language "fr" :select-tags ("a" "b" "c")
186 :title ("a b c"))))
187 ;; More than one property can refer to the same buffer keyword.
188 (should
189 (equal '(:k2 "value" :k1 "value")
190 (let ((backend (org-export-create-backend
191 :options '((:k1 "KEYWORD")
192 (:k2 "KEYWORD")))))
193 (org-test-with-temp-text "#+KEYWORD: value"
194 (org-export--get-inbuffer-options backend))))))
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/set-title ()
272 "Test title setting."
273 ;; If no title if specified, use file name.
274 (should
275 (apply
276 'equal
277 (org-test-with-temp-text-in-file "Test"
278 (org-mode)
279 (list (org-export-as
280 (org-export-create-backend
281 :transcoders
282 '((template . (lambda (text info)
283 (org-element-interpret-data
284 (plist-get info :title)))))))
285 (file-name-nondirectory
286 (file-name-sans-extension (buffer-file-name)))))))
287 ;; If no title is specified, and no file is associated to the
288 ;; buffer, use buffer's name.
289 (should
290 (apply
291 'equal
292 (org-test-with-temp-text "Test"
293 (org-mode)
294 (list (org-export-as
295 (org-export-create-backend
296 :transcoders
297 '((template . (lambda (text info)
298 (org-element-interpret-data
299 (plist-get info :title)))))))
300 (buffer-name)))))
301 ;; If a title is specified, use it.
302 (should
303 (equal
304 "Title"
305 (org-test-with-temp-text-in-file "#+TITLE: Title\nTest"
306 (org-mode)
307 (org-export-as
308 (org-export-create-backend
309 :transcoders
310 '((template . (lambda (text info)
311 (org-element-interpret-data
312 (plist-get info :title))))))))))
313 ;; If an empty title is specified, do not set it.
314 (should
315 (equal
317 (org-test-with-temp-text-in-file "#+TITLE:\nTest"
318 (org-mode)
319 (org-export-as
320 (org-export-create-backend
321 :transcoders
322 '((template . (lambda (text info)
323 (org-element-interpret-data
324 (plist-get info :title)))))))))))
326 (ert-deftest test-org-export/handle-options ()
327 "Test if export options have an impact on output."
328 ;; Test exclude tags for headlines and inlinetasks.
329 (should
330 (equal ""
331 (org-test-with-temp-text "* Head1 :noexp:"
332 (org-export-as (org-test-default-backend)
333 nil nil nil '(:exclude-tags ("noexp"))))))
334 ;; Test include tags for headlines and inlinetasks.
335 (should
336 (equal "* H2\n** Sub :exp:\n*** Sub Sub\n"
337 (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
338 (let ((org-tags-column 0))
339 (org-export-as (org-test-default-backend)
340 nil nil nil '(:select-tags ("exp")))))))
341 ;; If there is an include tag, ignore the section before the first
342 ;; headline, if any.
343 (should
344 (equal "* H1 :exp:\nBody\n"
345 (org-test-with-temp-text "First section\n* H1 :exp:\nBody"
346 (let ((org-tags-column 0))
347 (org-export-as (org-test-default-backend)
348 nil nil nil '(:select-tags ("exp")))))))
349 (should-not
350 (equal "* H1 :exp:\n"
351 (org-test-with-temp-text "* H1 :exp:\nBody"
352 (let ((org-tags-column 0))
353 (org-export-as (org-test-default-backend)
354 nil nil nil '(:select-tags ("exp")))))))
355 ;; Test mixing include tags and exclude tags.
356 (should
357 (string-match
358 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
359 (org-test-with-temp-text "
360 * Head1 :export:
361 ** Sub-Head1 :noexport:
362 ** Sub-Head2
363 * Head2 :noexport:
364 ** Sub-Head1 :export:"
365 (org-export-as (org-test-default-backend) nil nil nil
366 '(:select-tags ("export") :exclude-tags ("noexport"))))))
367 ;; Ignore tasks.
368 (should
369 (equal ""
370 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
371 (org-test-with-temp-text "* TODO Head1"
372 (org-export-as (org-test-default-backend)
373 nil nil nil '(:with-tasks nil))))))
374 (should
375 (equal "* TODO Head1\n"
376 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
377 (org-test-with-temp-text "* TODO Head1"
378 (org-export-as (org-test-default-backend)
379 nil nil nil '(:with-tasks t))))))
380 ;; Archived tree.
381 (should
382 (equal ""
383 (org-test-with-temp-text "* Head1 :archive:"
384 (let ((org-archive-tag "archive"))
385 (org-export-as (org-test-default-backend)
386 nil nil nil '(:with-archived-trees nil))))))
387 (should
388 (string-match
389 "\\* Head1[ \t]+:archive:"
390 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
391 (let ((org-archive-tag "archive"))
392 (org-export-as (org-test-default-backend) nil nil nil
393 '(:with-archived-trees headline))))))
394 (should
395 (string-match
396 "\\`\\* Head1[ \t]+:archive:\n\\'"
397 (org-test-with-temp-text "* Head1 :archive:"
398 (let ((org-archive-tag "archive"))
399 (org-export-as (org-test-default-backend)
400 nil nil nil '(:with-archived-trees t))))))
401 ;; Clocks.
402 (should
403 (string-match "CLOCK: \\[2012-04-29 .* 10:45\\]"
404 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
405 (org-export-as (org-test-default-backend)
406 nil nil nil '(:with-clocks t)))))
407 (should
408 (equal ""
409 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
410 (org-export-as (org-test-default-backend)
411 nil nil nil '(:with-clocks nil)))))
412 ;; Drawers.
413 (should
414 (equal ""
415 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
416 (org-export-as (org-test-default-backend)
417 nil nil nil '(:with-drawers nil)))))
418 (should
419 (equal ":TEST:\ncontents\n:END:\n"
420 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
421 (org-export-as (org-test-default-backend)
422 nil nil nil '(:with-drawers t)))))
423 (should
424 (equal ":FOO:\nkeep\n:END:\n"
425 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
426 (org-export-as (org-test-default-backend)
427 nil nil nil '(:with-drawers ("FOO"))))))
428 (should
429 (equal ":FOO:\nkeep\n:END:\n"
430 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
431 (org-export-as (org-test-default-backend)
432 nil nil nil '(:with-drawers (not "BAR"))))))
433 ;; Fixed-width.
434 (should
435 (equal ": A\n"
436 (org-test-with-temp-text ": A"
437 (org-export-as (org-test-default-backend) nil nil nil
438 '(:with-fixed-width t)))))
439 (should
440 (equal ""
441 (org-test-with-temp-text ": A"
442 (org-export-as (org-test-default-backend) nil nil nil
443 '(:with-fixed-width nil)))))
444 ;; Footnotes.
445 (should
446 (equal "Footnote?"
447 (let ((org-footnote-section nil))
448 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
449 (org-trim (org-export-as (org-test-default-backend)
450 nil nil nil '(:with-footnotes nil)))))))
451 (should
452 (equal "Footnote?[fn:1]\n\n[fn:1] Def"
453 (let ((org-footnote-section nil))
454 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
455 (org-trim (org-export-as (org-test-default-backend)
456 nil nil nil '(:with-footnotes t)))))))
457 ;; Inlinetasks.
458 (when (featurep 'org-inlinetask)
459 (should
460 (equal
462 (let ((org-inlinetask-min-level 15))
463 (org-test-with-temp-text "*************** Task"
464 (org-export-as (org-test-default-backend)
465 nil nil nil '(:with-inlinetasks nil))))))
466 (should
467 (equal
469 (let ((org-inlinetask-min-level 15))
470 (org-test-with-temp-text
471 "*************** Task\nContents\n*************** END"
472 (org-export-as (org-test-default-backend)
473 nil nil nil '(:with-inlinetasks nil)))))))
474 ;; Plannings.
475 (should
476 (string-match
477 "CLOSED: \\[2012-04-29 .* 10:45\\]"
478 (let ((org-closed-string "CLOSED:"))
479 (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
480 (org-export-as (org-test-default-backend)
481 nil nil nil '(:with-planning t))))))
482 (should
483 (equal ""
484 (let ((org-closed-string "CLOSED:"))
485 (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
486 (org-export-as (org-test-default-backend)
487 nil nil nil '(:with-planning nil))))))
488 ;; Property Drawers.
489 (should
490 (equal "* H1\n"
491 (org-test-with-temp-text
492 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
493 (org-export-as (org-test-default-backend)
494 nil nil nil '(:with-properties nil)))))
495 (should
496 (equal "* H1\n:PROPERTIES:\n:PROP: value\n:END:\n"
497 (org-test-with-temp-text
498 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
499 (org-export-as (org-test-default-backend)
500 nil nil nil '(:with-properties t)))))
501 (should
502 (equal "* H1\n:PROPERTIES:\n:B: 2\n:END:\n"
503 (org-test-with-temp-text
504 "* H1\n :PROPERTIES:\n :A: 1\n :B: 2\n:END:"
505 (org-export-as (org-test-default-backend)
506 nil nil nil '(:with-properties ("B"))))))
507 ;; Statistics cookies.
508 (should
509 (equal ""
510 (org-test-with-temp-text "[0/0]"
511 (org-export-as (org-test-default-backend)
512 nil nil nil '(:with-statistics-cookies nil)))))
513 ;; Tables.
514 (should
515 (equal "| A |\n"
516 (org-test-with-temp-text "| A |"
517 (org-export-as (org-test-default-backend) nil nil nil
518 '(:with-tables t)))))
519 (should
520 (equal ""
521 (org-test-with-temp-text "| A |"
522 (org-export-as (org-test-default-backend) nil nil nil
523 '(:with-tables nil))))))
525 (ert-deftest test-org-export/with-timestamps ()
526 "Test `org-export-with-timestamps' specifications."
527 ;; t value.
528 (should
529 (string-match
530 "\\[2012-04-29 .*? 10:45\\]<2012-04-29 .*? 10:45>"
531 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
532 (org-export-as (org-test-default-backend)
533 nil nil nil '(:with-timestamps t)))))
534 ;; nil value.
535 (should
536 (equal
538 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
539 (org-export-as (org-test-default-backend)
540 nil nil nil '(:with-timestamps nil)))))
541 ;; `active' value.
542 (should
543 (string-match
544 "<2012-03-29 .*?>\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
545 (org-test-with-temp-text
546 "<2012-03-29 Thu>[2012-03-29 Thu]
548 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
549 (org-export-as (org-test-default-backend)
550 nil nil nil '(:with-timestamps active)))))
551 ;; `inactive' value.
552 (should
553 (string-match
554 "\\[2012-03-29 .*?\\]\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
555 (org-test-with-temp-text
556 "<2012-03-29 Thu>[2012-03-29 Thu]
558 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
559 (org-export-as (org-test-default-backend)
560 nil nil nil '(:with-timestamps inactive))))))
562 (ert-deftest test-org-export/comment-tree ()
563 "Test if export process ignores commented trees."
564 (should
565 (equal ""
566 (org-test-with-temp-text "* COMMENT Head1"
567 (org-export-as (org-test-default-backend))))))
569 (ert-deftest test-org-export/uninterpreted ()
570 "Test handling of uninterpreted elements."
571 ;; Entities.
572 (should
573 (equal "dummy\n"
574 (org-test-with-temp-text "\\alpha"
575 (org-export-as
576 (org-export-create-backend
577 :transcoders '((entity . (lambda (e c i) "dummy"))
578 (paragraph . (lambda (p c i) c))
579 (section . (lambda (s c i) c))))
580 nil nil nil '(:with-entities t)))))
581 (should
582 (equal "\\alpha\n"
583 (org-test-with-temp-text "\\alpha"
584 (org-export-as
585 (org-export-create-backend
586 :transcoders '((entity . (lambda (e c i) "dummy"))
587 (paragraph . (lambda (p c i) c))
588 (section . (lambda (s c i) c))))
589 nil nil nil '(:with-entities nil)))))
590 ;; Emphasis.
591 (should
592 (equal "dummy\n"
593 (org-test-with-temp-text "*bold*"
594 (org-export-as
595 (org-export-create-backend
596 :transcoders '((bold . (lambda (b c i) "dummy"))
597 (paragraph . (lambda (p c i) c))
598 (section . (lambda (s c i) c))))
599 nil nil nil '(:with-emphasize t)))))
600 (should
601 (equal "*bold*\n"
602 (org-test-with-temp-text "*bold*"
603 (org-export-as
604 (org-export-create-backend
605 :transcoders '((bold . (lambda (b c i) "dummy"))
606 (paragraph . (lambda (p c i) c))
607 (section . (lambda (s c i) c))))
608 nil nil nil '(:with-emphasize nil)))))
609 ;; LaTeX environment.
610 (should
611 (equal "dummy\n"
612 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
613 (org-export-as
614 (org-export-create-backend
615 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
616 (section . (lambda (s c i) c))))
617 nil nil nil '(:with-latex t)))))
618 (should
619 (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
620 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
621 (org-export-as
622 (org-export-create-backend
623 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
624 (section . (lambda (s c i) c))))
625 nil nil nil '(:with-latex verbatim)))))
626 ;; LaTeX fragment.
627 (should
628 (equal "dummy\n"
629 (org-test-with-temp-text "$1$"
630 (org-export-as
631 (org-export-create-backend
632 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
633 (paragraph . (lambda (p c i) c))
634 (section . (lambda (s c i) c))))
635 nil nil nil '(:with-latex t)))))
636 (should
637 (equal "$1$\n"
638 (org-test-with-temp-text "$1$"
639 (org-export-as
640 (org-export-create-backend
641 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
642 (paragraph . (lambda (p c i) c))
643 (section . (lambda (s c i) c))))
644 nil nil nil '(:with-latex verbatim)))))
645 ;; Sub/superscript.
646 (should
647 (equal "adummy\n"
648 (org-test-with-temp-text "a_b"
649 (org-export-as
650 (org-export-create-backend
651 :transcoders '((subscript . (lambda (s c i) "dummy"))
652 (paragraph . (lambda (p c i) c))
653 (section . (lambda (s c i) c))))
654 nil nil nil '(:with-sub-superscript t)))))
655 (should
656 (equal "a_b\n"
657 (org-test-with-temp-text "a_b"
658 (org-export-as
659 (org-export-create-backend
660 :transcoders '((subscript . (lambda (s c i) "dummy"))
661 (paragraph . (lambda (p c i) c))
662 (section . (lambda (s c i) c))))
663 nil nil nil '(:with-sub-superscript nil)))))
664 (should
665 (equal "a_b\n"
666 (org-test-with-temp-text "a_b"
667 (org-export-as
668 (org-export-create-backend
669 :transcoders '((subscript . (lambda (s c i) "dummy"))
670 (paragraph . (lambda (p c i) c))
671 (section . (lambda (s c i) c))))
672 nil nil nil '(:with-sub-superscript {})))))
673 (should
674 (equal "adummy\n"
675 (org-test-with-temp-text "a_{b}"
676 (org-export-as
677 (org-export-create-backend
678 :transcoders '((subscript . (lambda (s c i) "dummy"))
679 (paragraph . (lambda (p c i) c))
680 (section . (lambda (s c i) c))))
681 nil nil nil '(:with-sub-superscript {})))))
682 ;; Also handle uninterpreted objects in title.
683 (should
684 (equal "a_b"
685 (org-test-with-temp-text "#+TITLE: a_b"
686 (org-export-as
687 (org-export-create-backend
688 :transcoders
689 '((subscript . (lambda (s c i) "dummy"))
690 (template . (lambda (c i) (org-export-data
691 (plist-get i :title) i)))
692 (section . (lambda (s c i) c))))
693 nil nil nil '(:with-sub-superscript nil)))))
694 ;; Special case: multiples uninterpreted objects in a row.
695 (should
696 (equal "a_b_c_d\n"
697 (org-test-with-temp-text "a_b_c_d"
698 (org-export-as
699 (org-export-create-backend
700 :transcoders '((subscript . (lambda (s c i) "dummy"))
701 (paragraph . (lambda (p c i) c))
702 (section . (lambda (s c i) c))))
703 nil nil nil '(:with-sub-superscript {}))))))
705 (ert-deftest test-org-export/export-scope ()
706 "Test all export scopes."
707 (org-test-with-temp-text "
708 * Head1
709 ** Head2
710 text
711 *** Head3"
712 ;; Subtree.
713 (forward-line 3)
714 (should (equal (org-export-as (org-test-default-backend) 'subtree)
715 "text\n*** Head3\n"))
716 ;; Visible.
717 (goto-char (point-min))
718 (forward-line)
719 (org-cycle)
720 (should (equal (org-export-as (org-test-default-backend) nil 'visible)
721 "* Head1\n"))
722 ;; Region.
723 (goto-char (point-min))
724 (forward-line 3)
725 (transient-mark-mode 1)
726 (push-mark (point) t t)
727 (goto-char (point-at-eol))
728 (should (equal (org-export-as (org-test-default-backend)) "text\n")))
729 ;; Subtree with a code block calling another block outside.
730 (should
731 (equal ": 3\n"
732 (org-test-with-temp-text "
733 * Head1
734 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
735 <<test>>
736 #+END_SRC
737 * Head2
738 #+NAME: test
739 #+BEGIN_SRC emacs-lisp
740 \(+ 1 2)
741 #+END_SRC"
742 (forward-line 1)
743 (org-export-as (org-test-default-backend) 'subtree))))
744 ;; Body only.
745 (let ((backend (org-test-default-backend)))
746 (setf (org-export-backend-transcoders backend)
747 (cons '(template . (lambda (body i)
748 (format "BEGIN\n%sEND" body)))
749 (org-export-backend-transcoders backend)))
750 (org-test-with-temp-text "Text"
751 (should (equal (org-export-as backend nil nil 'body-only)
752 "Text\n"))
753 (should (equal (org-export-as backend) "BEGIN\nText\nEND")))))
755 (ert-deftest test-org-export/output-file-name ()
756 "Test `org-export-output-file-name' specifications."
757 ;; Export from a file: name is built from original file name.
758 (should
759 (org-test-with-temp-text-in-file "Test"
760 (equal (concat (file-name-as-directory ".")
761 (file-name-nondirectory
762 (file-name-sans-extension (buffer-file-name))))
763 (file-name-sans-extension (org-export-output-file-name ".ext")))))
764 ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
765 (should
766 (org-test-with-temp-text-in-file
767 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
768 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
769 ;; From a buffer not associated to a file, too.
770 (should
771 (org-test-with-temp-text
772 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
773 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
774 ;; When provided name is absolute, preserve it.
775 (should
776 (org-test-with-temp-text
777 (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
778 (expand-file-name "test"))
779 (file-name-absolute-p (org-export-output-file-name ".ext" t))))
780 ;; When PUB-DIR argument is provided, use it.
781 (should
782 (org-test-with-temp-text-in-file "Test"
783 (equal (file-name-directory
784 (org-export-output-file-name ".ext" nil "dir/"))
785 "dir/")))
786 ;; When returned name would overwrite original file, add EXTENSION
787 ;; another time.
788 (should
789 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
790 (equal (org-export-output-file-name ".org") "./normal.org.org"))))
792 (ert-deftest test-org-export/expand-include ()
793 "Test file inclusion in an Org buffer."
794 ;; Error when file isn't specified.
795 (should-error
796 (org-test-with-temp-text "#+INCLUDE: dummy.org"
797 (org-export-expand-include-keyword)))
798 ;; Full insertion with recursive inclusion.
799 (org-test-with-temp-text
800 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
801 (org-export-expand-include-keyword)
802 (should (equal (buffer-string)
803 "Small Org file with an include keyword.
805 #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
807 Success!
809 * Heading
810 body\n")))
811 ;; Localized insertion.
812 (org-test-with-temp-text
813 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
814 org-test-dir)
815 (org-export-expand-include-keyword)
816 (should (equal (buffer-string)
817 "Small Org file with an include keyword.\n")))
818 ;; Insertion with constraints on headlines level.
819 (should
820 (equal
821 "* Top heading\n** Heading\nbody\n"
822 (org-test-with-temp-text
823 (format
824 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
825 org-test-dir)
826 (org-export-expand-include-keyword)
827 (buffer-string))))
828 (should
829 (equal
830 "* Top heading\n* Heading\nbody\n"
831 (org-test-with-temp-text
832 (format
833 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\" :minlevel 1"
834 org-test-dir)
835 (org-export-expand-include-keyword)
836 (buffer-string))))
837 ;; Inclusion within an example block.
838 (should
839 (equal
840 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
841 (org-test-with-temp-text
842 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
843 org-test-dir)
844 (org-export-expand-include-keyword)
845 (buffer-string))))
846 ;; Inclusion within a src-block.
847 (should
848 (equal
849 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
850 (org-test-with-temp-text
851 (format
852 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
853 org-test-dir)
854 (org-export-expand-include-keyword)
855 (buffer-string))))
856 ;; Inclusion within an html export-block.
857 (should
858 (equal
859 "#+BEGIN_HTML\n<p>HTML!</p>\n#+END_HTML\n"
860 (org-test-with-temp-text
861 (format
862 "#+INCLUDE: \"%s/examples/include.html\" HTML"
863 org-test-dir)
864 (org-export-expand-include-keyword)
865 (buffer-string))))
866 ;; Inclusion within an center paragraph
867 (should
868 (equal
869 "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
870 (org-test-with-temp-text
871 (format
872 "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
873 org-test-dir)
874 (org-export-expand-include-keyword)
875 (buffer-string))))
876 ;; Footnotes labels are local to each included file.
877 (should
878 (= 6
879 (length
880 (delete-dups
881 (let ((contents "
882 Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote].
883 \[fn:1] Footnote 1
884 \[fn:test] Footnote \"test\""))
885 (org-test-with-temp-text-in-file contents
886 (let ((file1 (buffer-file-name)))
887 (org-test-with-temp-text-in-file contents
888 (let ((file2 (buffer-file-name)))
889 (org-test-with-temp-text
890 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
891 file1 file2)
892 (org-export-expand-include-keyword)
893 (org-element-map (org-element-parse-buffer)
894 'footnote-reference
895 (lambda (ref)
896 (org-element-property :label ref)))))))))))))
897 ;; Footnotes labels are not local to each include keyword.
898 (should
899 (= 3
900 (length
901 (delete-dups
902 (let ((contents "
903 Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote].
904 \[fn:1] Footnote 1
905 \[fn:test] Footnote \"test\""))
906 (org-test-with-temp-text-in-file contents
907 (let ((file (buffer-file-name)))
908 (org-test-with-temp-text
909 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
910 (org-export-expand-include-keyword)
911 (org-element-map (org-element-parse-buffer)
912 'footnote-reference
913 (lambda (ref) (org-element-property :label ref))))))))))))
915 (ert-deftest test-org-export/expand-macro ()
916 "Test macro expansion in an Org buffer."
917 ;; Standard macro expansion.
918 (should
919 (equal "#+MACRO: macro1 value\nvalue\n"
920 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
921 (org-export-as (org-test-default-backend)))))
922 ;; Expand specific macros.
923 (should
924 (equal "me 2012-03-29 me@here Title\n"
925 (org-test-with-temp-text
927 #+TITLE: Title
928 #+DATE: 2012-03-29
929 #+AUTHOR: me
930 #+EMAIL: me@here
931 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
932 (let ((output (org-export-as (org-test-default-backend))))
933 (substring output (string-match ".*\n\\'" output))))))
934 ;; Expand specific macros when property contained a regular macro
935 ;; already.
936 (should
937 (equal "value\n"
938 (org-test-with-temp-text "
939 #+MACRO: macro1 value
940 #+TITLE: {{{macro1}}}
941 {{{title}}}"
942 (let ((output (org-export-as (org-test-default-backend))))
943 (substring output (string-match ".*\n\\'" output))))))
944 ;; Expand macros with templates in included files.
945 (should
946 (equal "success\n"
947 (org-test-with-temp-text
948 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
949 {{{included-macro}}}" org-test-dir)
950 (let ((output (org-export-as (org-test-default-backend))))
951 (substring output (string-match ".*\n\\'" output)))))))
953 (ert-deftest test-org-export/user-ignore-list ()
954 "Test if `:ignore-list' accepts user input."
955 (let ((backend (org-test-default-backend)))
956 (setf (org-export-backend-transcoders backend)
957 (cons '(template . (lambda (body i)
958 (format "BEGIN\n%sEND" body)))
959 (org-export-backend-transcoders backend)))
960 (org-test-with-temp-text "Text"
961 (should (equal (org-export-as backend nil nil 'body-only)
962 "Text\n"))
963 (should (equal (org-export-as backend) "BEGIN\nText\nEND"))))
964 (should
965 (equal
966 "* Head1\n"
967 (let ((org-export-filter-parse-tree-functions
968 '((lambda (data backend info)
969 ;; Ignore headlines with the word "note" in their title.
970 (org-element-map data 'headline
971 (lambda (headline)
972 (when (string-match "\\<note\\>"
973 (org-element-property :raw-value
974 headline))
975 (org-export-ignore-element headline info)))
976 info)
977 data))))
978 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
979 (org-export-as (org-test-default-backend)))))))
981 (ert-deftest test-org-export/before-processing-hook ()
982 "Test `org-export-before-processing-hook'."
983 (should
984 (equal
985 "#+MACRO: mac val\nTest\n"
986 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
987 (let ((org-export-before-processing-hook
988 '((lambda (backend)
989 (while (re-search-forward "{{{" nil t)
990 (let ((object (org-element-context)))
991 (when (eq (org-element-type object) 'macro)
992 (delete-region
993 (org-element-property :begin object)
994 (org-element-property :end object)))))))))
995 (org-export-as (org-test-default-backend)))))))
997 (ert-deftest test-org-export/before-parsing-hook ()
998 "Test `org-export-before-parsing-hook'."
999 (should
1000 (equal "Body 1\nBody 2\n"
1001 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
1002 (let ((org-export-before-parsing-hook
1003 '((lambda (backend)
1004 (goto-char (point-min))
1005 (while (re-search-forward org-outline-regexp-bol nil t)
1006 (delete-region
1007 (point-at-bol) (progn (forward-line) (point))))))))
1008 (org-export-as (org-test-default-backend)))))))
1012 ;;; Affiliated Keywords
1014 (ert-deftest test-org-export/read-attribute ()
1015 "Test `org-export-read-attribute' specifications."
1016 ;; Standard test.
1017 (should
1018 (equal
1019 (org-export-read-attribute
1020 :attr_html
1021 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
1022 (org-element-at-point)))
1023 '(:a "1" :b "2")))
1024 ;; Return nil on empty attribute.
1025 (should-not
1026 (org-export-read-attribute
1027 :attr_html
1028 (org-test-with-temp-text "Paragraph" (org-element-at-point))))
1029 ;; Return nil on "nil" string.
1030 (should
1031 (equal '(:a nil :b nil)
1032 (org-export-read-attribute
1033 :attr_html
1034 (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
1035 (org-element-at-point)))))
1036 ;; Return nil on empty string.
1037 (should
1038 (equal '(:a nil :b nil)
1039 (org-export-read-attribute
1040 :attr_html
1041 (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
1042 (org-element-at-point)))))
1043 ;; Return empty string when value is "".
1044 (should
1045 (equal '(:a "")
1046 (org-export-read-attribute
1047 :attr_html
1048 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
1049 (org-element-at-point)))))
1050 ;; Return \"\" when value is """".
1051 (should
1052 (equal '(:a "\"\"")
1053 (org-export-read-attribute
1054 :attr_html
1055 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
1056 (org-element-at-point)))))
1057 ;; Ignore text before first property.
1058 (should-not
1059 (member "ignore"
1060 (org-export-read-attribute
1061 :attr_html
1062 (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
1063 (org-element-at-point))))))
1065 (ert-deftest test-org-export/get-caption ()
1066 "Test `org-export-get-caption' specifications."
1067 ;; Without optional argument, return long caption
1068 (should
1069 (equal
1070 '("l")
1071 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1072 (org-export-get-caption (org-element-at-point)))))
1073 ;; With optional argument, return short caption.
1074 (should
1075 (equal
1076 '("s")
1077 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1078 (org-export-get-caption (org-element-at-point) t))))
1079 ;; Multiple lines are separated by white spaces.
1080 (should
1081 (equal
1082 '("a" " " "b")
1083 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
1084 (org-export-get-caption (org-element-at-point))))))
1088 ;;; Back-End Tools
1090 (ert-deftest test-org-export/define-backend ()
1091 "Test back-end definition and accessors."
1092 ;; Translate table.
1093 (should
1094 (equal '((headline . my-headline-test))
1095 (let (org-export--registered-backends)
1096 (org-export-define-backend 'test '((headline . my-headline-test)))
1097 (org-export-get-all-transcoders 'test))))
1098 ;; Filters.
1099 (should
1100 (equal '((:filter-headline . my-filter))
1101 (let (org-export--registered-backends)
1102 (org-export-define-backend 'test
1103 '((headline . my-headline-test))
1104 :filters-alist '((:filter-headline . my-filter)))
1105 (org-export-backend-filters (org-export-get-backend 'test)))))
1106 ;; Options.
1107 (should
1108 (equal '((:prop value))
1109 (let (org-export--registered-backends)
1110 (org-export-define-backend 'test
1111 '((headline . my-headline-test))
1112 :options-alist '((:prop value)))
1113 (org-export-backend-options (org-export-get-backend 'test)))))
1114 ;; Menu.
1115 (should
1116 (equal '(?k "Test Export" test)
1117 (let (org-export--registered-backends)
1118 (org-export-define-backend 'test
1119 '((headline . my-headline-test))
1120 :menu-entry '(?k "Test Export" test))
1121 (org-export-backend-menu (org-export-get-backend 'test)))))
1122 ;; Export Blocks.
1123 (should
1124 (equal '(("TEST" . org-element-export-block-parser))
1125 (let (org-export--registered-backends org-element-block-name-alist)
1126 (org-export-define-backend 'test
1127 '((headline . my-headline-test))
1128 :export-block '("test"))
1129 org-element-block-name-alist))))
1131 (ert-deftest test-org-export/define-derived-backend ()
1132 "Test `org-export-define-derived-backend' specifications."
1133 ;; Error when parent back-end is not defined.
1134 (should-error
1135 (let (org-export--registered-backends)
1136 (org-export-define-derived-backend 'test 'parent)))
1137 ;; Append translation table to parent's.
1138 (should
1139 (equal '((:headline . test) (:headline . parent))
1140 (let (org-export--registered-backends)
1141 (org-export-define-backend 'parent '((:headline . parent)))
1142 (org-export-define-derived-backend 'test 'parent
1143 :translate-alist '((:headline . test)))
1144 (org-export-get-all-transcoders 'test))))
1145 ;; Options defined in the new back have priority over those defined
1146 ;; in parent.
1147 (should
1148 (eq 'test
1149 (let (org-export--registered-backends)
1150 (org-export-define-backend 'parent
1151 '((:headline . parent))
1152 :options-alist '((:a nil nil 'parent)))
1153 (org-export-define-derived-backend 'test 'parent
1154 :options-alist '((:a nil nil 'test)))
1155 (plist-get (org-export--get-global-options
1156 (org-export-get-backend 'test))
1157 :a)))))
1159 (ert-deftest test-org-export/derived-backend-p ()
1160 "Test `org-export-derived-backend-p' specifications."
1161 ;; Non-nil with direct match.
1162 (should
1163 (let (org-export--registered-backends)
1164 (org-export-define-backend 'test '((headline . test)))
1165 (org-export-derived-backend-p 'test 'test)))
1166 (should
1167 (let (org-export--registered-backends)
1168 (org-export-define-backend 'test '((headline . test)))
1169 (org-export-define-derived-backend 'test2 'test)
1170 (org-export-derived-backend-p 'test2 'test2)))
1171 ;; Non-nil with a direct parent.
1172 (should
1173 (let (org-export--registered-backends)
1174 (org-export-define-backend 'test '((headline . test)))
1175 (org-export-define-derived-backend 'test2 'test)
1176 (org-export-derived-backend-p 'test2 'test)))
1177 ;; Non-nil with an indirect parent.
1178 (should
1179 (let (org-export--registered-backends)
1180 (org-export-define-backend 'test '((headline . test)))
1181 (org-export-define-derived-backend 'test2 'test)
1182 (org-export-define-derived-backend 'test3 'test2)
1183 (org-export-derived-backend-p 'test3 'test)))
1184 ;; Nil otherwise.
1185 (should-not
1186 (let (org-export--registered-backends)
1187 (org-export-define-backend 'test '((headline . test)))
1188 (org-export-define-backend 'test2 '((headline . test2)))
1189 (org-export-derived-backend-p 'test2 'test)))
1190 (should-not
1191 (let (org-export--registered-backends)
1192 (org-export-define-backend 'test '((headline . test)))
1193 (org-export-define-backend 'test2 '((headline . test2)))
1194 (org-export-define-derived-backend 'test3 'test2)
1195 (org-export-derived-backend-p 'test3 'test))))
1197 (ert-deftest test-org-export/get-all-transcoders ()
1198 "Test `org-export-get-all-transcoders' specifications."
1199 ;; Return nil when back-end cannot be found.
1200 (should-not (org-export-get-all-transcoders nil))
1201 ;; Same as `org-export-transcoders' if no parent.
1202 (should
1203 (equal '((headline . ignore))
1204 (org-export-get-all-transcoders
1205 (org-export-create-backend
1206 :transcoders '((headline . ignore))))))
1207 ;; But inherit from all ancestors whenever possible.
1208 (should
1209 (equal '((section . ignore) (headline . ignore))
1210 (let (org-export--registered-backends)
1211 (org-export-define-backend 'b1 '((headline . ignore)))
1212 (org-export-get-all-transcoders
1213 (org-export-create-backend
1214 :parent 'b1 :transcoders '((section . ignore)))))))
1215 (should
1216 (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
1217 (let (org-export--registered-backends)
1218 (org-export-define-backend 'b1 '((headline . ignore)))
1219 (org-export-define-derived-backend 'b2 'b1
1220 :translate-alist '((section . ignore)))
1221 (org-export-get-all-transcoders
1222 (org-export-create-backend
1223 :parent 'b2 :transcoders '((paragraph . ignore)))))))
1224 ;; Back-end transcoders overrule inherited ones.
1225 (should
1226 (eq 'b
1227 (let (org-export--registered-backends)
1228 (org-export-define-backend 'b1 '((headline . a)))
1229 (cdr (assq 'headline
1230 (org-export-get-all-transcoders
1231 (org-export-create-backend
1232 :parent 'b1 :transcoders '((headline . b))))))))))
1234 (ert-deftest test-org-export/get-all-options ()
1235 "Test `org-export-get-all-options' specifications."
1236 ;; Return nil when back-end cannot be found.
1237 (should-not (org-export-get-all-options nil))
1238 ;; Same as `org-export-options' if no parent.
1239 (should
1240 (equal '((headline . ignore))
1241 (org-export-get-all-options
1242 (org-export-create-backend
1243 :options '((headline . ignore))))))
1244 ;; But inherit from all ancestors whenever possible.
1245 (should
1246 (equal '((:key2 value2) (:key1 value1))
1247 (let (org-export--registered-backends)
1248 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1249 (org-export-get-all-options
1250 (org-export-create-backend
1251 :parent 'b1 :options '((:key2 value2)))))))
1252 (should
1253 (equal '((:key3 value3) (:key2 value2) (:key1 value1))
1254 (let (org-export--registered-backends)
1255 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1256 (org-export-define-derived-backend 'b2 'b1
1257 :options-alist '((:key2 value2)))
1258 (org-export-get-all-options
1259 (org-export-create-backend
1260 :parent 'b2 :options '((:key3 value3)))))))
1261 ;; Back-end options overrule inherited ones.
1262 (should
1263 (eq 'b
1264 (let (org-export--registered-backends)
1265 (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
1266 (cdr (assq :key1
1267 (org-export-get-all-options
1268 (org-export-create-backend
1269 :parent 'b1 :options '((:key1 . b))))))))))
1271 (ert-deftest test-org-export/get-all-filters ()
1272 "Test `org-export-get-all-filters' specifications."
1273 ;; Return nil when back-end cannot be found.
1274 (should-not (org-export-get-all-filters nil))
1275 ;; Same as `org-export-filters' if no parent.
1276 (should
1277 (equal '((:filter-headline . ignore))
1278 (org-export-get-all-filters
1279 (org-export-create-backend
1280 :filters '((:filter-headline . ignore))))))
1281 ;; But inherit from all ancestors whenever possible.
1282 (should
1283 (equal '((:filter-section . ignore) (:filter-headline . ignore))
1284 (let (org-export--registered-backends)
1285 (org-export-define-backend 'b1
1286 nil :filters-alist '((:filter-headline . ignore)))
1287 (org-export-get-all-filters
1288 (org-export-create-backend
1289 :parent 'b1 :filters '((:filter-section . ignore)))))))
1290 (should
1291 (equal '((:filter-paragraph . ignore)
1292 (:filter-section . ignore)
1293 (:filter-headline . ignore))
1294 (let (org-export--registered-backends)
1295 (org-export-define-backend 'b1
1296 nil :filters-alist '((:filter-headline . ignore)))
1297 (org-export-define-derived-backend 'b2 'b1
1298 :filters-alist '((:filter-section . ignore)))
1299 (org-export-get-all-filters
1300 (org-export-create-backend
1301 :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
1302 ;; Back-end filters overrule inherited ones.
1303 (should
1304 (eq 'b
1305 (let (org-export--registered-backends)
1306 (org-export-define-backend 'b1 '((:filter-headline . a)))
1307 (cdr (assq :filter-headline
1308 (org-export-get-all-filters
1309 (org-export-create-backend
1310 :parent 'b1 :filters '((:filter-headline . b))))))))))
1312 (ert-deftest test-org-export/with-backend ()
1313 "Test `org-export-with-backend' definition."
1314 ;; Error when calling an undefined back-end
1315 (should-error (org-export-with-backend nil "Test"))
1316 ;; Error when called back-end doesn't have an appropriate
1317 ;; transcoder.
1318 (should-error
1319 (org-export-with-backend
1320 (org-export-create-backend :transcoders '((headline . ignore)))
1321 "Test"))
1322 ;; Otherwise, export using correct transcoder
1323 (should
1324 (equal "Success"
1325 (let (org-export--registered-backends)
1326 (org-export-define-backend 'test
1327 '((plain-text . (lambda (text contents info) "Failure"))))
1328 (org-export-define-backend 'test2
1329 '((plain-text . (lambda (text contents info) "Success"))))
1330 (org-export-with-backend 'test2 "Test"))))
1331 ;; Provide correct back-end if transcoder needs to use recursive
1332 ;; calls anyway.
1333 (should
1334 (equal "Success\n"
1335 (let ((test-back-end
1336 (org-export-create-backend
1337 :transcoders
1338 '((headline . (lambda (headline contents info)
1339 (org-export-data
1340 (org-element-property :title headline)
1341 info)))
1342 (plain-text . (lambda (text info) "Success"))))))
1343 (org-export-string-as
1344 "* Test"
1345 (org-export-create-backend
1346 :transcoders
1347 '((headline . (lambda (headline contents info)
1348 (org-export-with-backend
1349 test-back-end headline contents info))))))))))
1351 (ert-deftest test-org-export/data-with-backend ()
1352 "Test `org-export-data-with-backend' specifications."
1353 ;; Error when calling an undefined back-end.
1354 (should-error (org-export-data-with-backend nil "nil" nil))
1355 ;; Otherwise, export data recursively, using correct back-end.
1356 (should
1357 (equal
1358 "Success!"
1359 (org-export-data-with-backend
1360 '(bold nil "Test")
1361 (org-export-create-backend
1362 :transcoders
1363 '((plain-text . (lambda (text info) "Success"))
1364 (bold . (lambda (bold contents info) (concat contents "!")))))
1365 '(:with-emphasize t)))))
1369 ;;; Export Snippets
1371 (ert-deftest test-org-export/export-snippet ()
1372 "Test export snippets transcoding."
1373 ;; Standard test.
1374 (org-test-with-temp-text "@@test:A@@@@t:B@@"
1375 (let ((backend (org-test-default-backend)))
1376 (setf (org-export-backend-name backend) 'test)
1377 (setf (org-export-backend-transcoders backend)
1378 (cons (cons 'export-snippet
1379 (lambda (snippet contents info)
1380 (when (eq (org-export-snippet-backend snippet) 'test)
1381 (org-element-property :value snippet))))
1382 (org-export-backend-transcoders backend)))
1383 (let ((org-export-snippet-translation-alist nil))
1384 (should (equal (org-export-as backend) "A\n")))
1385 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
1386 (should (equal (org-export-as backend) "AB\n")))))
1387 ;; Ignored export snippets do not remove any blank.
1388 (should
1389 (equal "begin end\n"
1390 (org-test-with-parsed-data "begin @@test:A@@ end"
1391 (org-export-data-with-backend
1392 tree
1393 (org-export-create-backend
1394 :transcoders
1395 '((paragraph . (lambda (paragraph contents info) contents))
1396 (section . (lambda (section contents info) contents))))
1397 info)))))
1401 ;;; Footnotes
1403 (ert-deftest test-org-export/footnotes ()
1404 "Test footnotes specifications."
1405 (let ((org-footnote-section nil)
1406 (org-export-with-footnotes t))
1407 ;; 1. Read every type of footnote.
1408 (should
1409 (equal
1410 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
1411 (org-test-with-parsed-data
1412 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
1413 (org-element-map tree 'footnote-reference
1414 (lambda (ref)
1415 (let ((def (org-export-get-footnote-definition ref info)))
1416 (cons (org-export-get-footnote-number ref info)
1417 (if (eq (org-element-property :type ref) 'inline) (car def)
1418 (car (org-element-contents
1419 (car (org-element-contents def))))))))
1420 info))))
1421 ;; 2. Test nested footnotes order.
1422 (should
1423 (equal
1424 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
1425 (org-test-with-parsed-data
1426 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
1427 (org-element-map tree 'footnote-reference
1428 (lambda (ref)
1429 (when (org-export-footnote-first-reference-p ref info)
1430 (cons (org-export-get-footnote-number ref info)
1431 (org-element-property :label ref))))
1432 info))))
1433 ;; 3. Test nested footnote in invisible definitions.
1434 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
1435 ;; Hide definitions.
1436 (narrow-to-region (point) (point-at-eol))
1437 (let* ((tree (org-element-parse-buffer))
1438 (info (org-combine-plists
1439 `(:parse-tree ,tree)
1440 (org-export-collect-tree-properties
1441 tree (org-export-get-environment)))))
1442 ;; Both footnotes should be seen.
1443 (should
1444 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
1445 ;; 4. Test footnotes definitions collection.
1446 (should
1447 (= 4
1448 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1450 \[fn:2] B [fn:3] [fn::D].
1452 \[fn:3] C."
1453 (length (org-export-collect-footnote-definitions tree info)))))
1454 ;; 5. Test export of footnotes defined outside parsing scope.
1455 (should
1456 (equal
1457 "ParagraphOut of scope\n"
1458 (org-test-with-temp-text "[fn:1] Out of scope
1459 * Title
1460 Paragraph[fn:1]"
1461 (let ((backend (org-test-default-backend)))
1462 (setf (org-export-backend-transcoders backend)
1463 (cons (cons 'footnote-reference
1464 (lambda (fn contents info)
1465 (org-element-interpret-data
1466 (org-export-get-footnote-definition fn info))))
1467 (org-export-backend-transcoders backend)))
1468 (forward-line)
1469 (org-export-as backend 'subtree)))))
1470 ;; 6. Footnotes without a definition should be provided a fallback
1471 ;; definition.
1472 (should
1473 (org-test-with-parsed-data "[fn:1]"
1474 (org-export-get-footnote-definition
1475 (org-element-map tree 'footnote-reference 'identity info t) info)))
1476 ;; 7. Footnote section should be ignored in TOC and in headlines
1477 ;; numbering.
1478 (should
1479 (= 1 (let ((org-footnote-section "Footnotes"))
1480 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
1481 (org-export-collect-headlines info))))))
1482 (should
1483 (equal '(2)
1484 (let ((org-footnote-section "Footnotes"))
1485 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
1486 (org-element-map tree 'headline
1487 (lambda (hl)
1488 (when (equal (org-element-property :raw-value hl) "H2")
1489 (org-export-get-headline-number hl info)))
1490 info t)))))))
1494 ;;; Headlines and Inlinetasks
1496 (ert-deftest test-org-export/get-relative-level ()
1497 "Test `org-export-get-relative-level' specifications."
1498 ;; Standard test.
1499 (should
1500 (equal '(1 2)
1501 (let ((org-odd-levels-only nil))
1502 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1503 (org-element-map tree 'headline
1504 (lambda (h) (org-export-get-relative-level h info))
1505 info)))))
1506 ;; Missing levels
1507 (should
1508 (equal '(1 3)
1509 (let ((org-odd-levels-only nil))
1510 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
1511 (org-element-map tree 'headline
1512 (lambda (h) (org-export-get-relative-level h info))
1513 info))))))
1515 (ert-deftest test-org-export/low-level-p ()
1516 "Test `org-export-low-level-p' specifications."
1517 (should
1518 (equal
1519 '(no yes)
1520 (let ((org-odd-levels-only nil))
1521 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1522 (org-element-map tree 'headline
1523 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
1524 (plist-put info :headline-levels 1)))))))
1526 (ert-deftest test-org-export/get-headline-number ()
1527 "Test `org-export-get-headline-number' specifications."
1528 ;; Standard test.
1529 (should
1530 (equal
1531 '((1) (1 1))
1532 (let ((org-odd-levels-only nil))
1533 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1534 (org-element-map tree 'headline
1535 (lambda (h) (org-export-get-headline-number h info))
1536 info)))))
1537 ;; Missing levels are replaced with 0.
1538 (should
1539 (equal
1540 '((1) (1 0 1))
1541 (let ((org-odd-levels-only nil))
1542 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
1543 (org-element-map tree 'headline
1544 (lambda (h) (org-export-get-headline-number h info))
1545 info))))))
1547 (ert-deftest test-org-export/numbered-headline-p ()
1548 "Test `org-export-numbered-headline-p' specifications."
1549 ;; If `:section-numbers' is nil, never number headlines.
1550 (should-not
1551 (org-test-with-parsed-data "* Headline"
1552 (org-element-map tree 'headline
1553 (lambda (h) (org-export-numbered-headline-p h info))
1554 (plist-put info :section-numbers nil))))
1555 ;; If `:section-numbers' is a number, only number headlines with
1556 ;; a level greater that it.
1557 (should
1558 (equal
1559 '(yes no)
1560 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1561 (org-element-map tree 'headline
1562 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
1563 (plist-put info :section-numbers 1)))))
1564 ;; Otherwise, headlines are always numbered.
1565 (should
1566 (org-test-with-parsed-data "* Headline"
1567 (org-element-map tree 'headline
1568 (lambda (h) (org-export-numbered-headline-p h info))
1569 (plist-put info :section-numbers t)))))
1571 (ert-deftest test-org-export/number-to-roman ()
1572 "Test `org-export-number-to-roman' specifications."
1573 ;; If number is negative, return it as a string.
1574 (should (equal (org-export-number-to-roman -1) "-1"))
1575 ;; Otherwise, return it as a roman number.
1576 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
1578 (ert-deftest test-org-export/get-optional-title ()
1579 "Test `org-export-get-alt-title' specifications."
1580 ;; If ALT_TITLE property is defined, use it.
1581 (should
1582 (equal '("opt")
1583 (org-test-with-parsed-data
1584 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
1585 (org-export-get-alt-title
1586 (org-element-map tree 'headline 'identity info t)
1587 info))))
1588 ;; Otherwise, fall-back to regular title.
1589 (should
1590 (equal '("Headline")
1591 (org-test-with-parsed-data "* Headline"
1592 (org-export-get-alt-title
1593 (org-element-map tree 'headline 'identity info t)
1594 info)))))
1596 (ert-deftest test-org-export/get-tags ()
1597 "Test `org-export-get-tags' specifications."
1598 (let ((org-export-exclude-tags '("noexport"))
1599 (org-export-select-tags '("export")))
1600 ;; Standard test: tags which are not a select tag, an exclude tag,
1601 ;; or specified as optional argument shouldn't be ignored.
1602 (should
1603 (org-test-with-parsed-data "* Headline :tag:"
1604 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1605 info)))
1606 ;; Exclude tags are removed.
1607 (should-not
1608 (org-test-with-parsed-data "* Headline :noexport:"
1609 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1610 info)))
1611 ;; Select tags are removed.
1612 (should-not
1613 (org-test-with-parsed-data "* Headline :export:"
1614 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1615 info)))
1616 (should
1617 (equal
1618 '("tag")
1619 (org-test-with-parsed-data "* Headline :tag:export:"
1620 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1621 info))))
1622 ;; Tags provided in the optional argument are also ignored.
1623 (should-not
1624 (org-test-with-parsed-data "* Headline :ignore:"
1625 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1626 info '("ignore"))))
1627 ;; Allow tag inheritance.
1628 (should
1629 (equal
1630 '(("tag") ("tag"))
1631 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
1632 (org-element-map tree 'headline
1633 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
1634 ;; Tag inheritance checks FILETAGS keywords.
1635 (should
1636 (equal
1637 '(("a" "b" "tag"))
1638 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
1639 (org-element-map tree 'headline
1640 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
1642 (ert-deftest test-org-export/get-node-property ()
1643 "Test`org-export-get-node-property' specifications."
1644 ;; Standard test.
1645 (should
1646 (equal "value"
1647 (org-test-with-parsed-data "* Headline
1648 :PROPERTIES:
1649 :prop: value
1650 :END:"
1651 (org-export-get-node-property
1652 :PROP (org-element-map tree 'headline 'identity nil t)))))
1653 ;; Test inheritance.
1654 (should
1655 (equal "value"
1656 (org-test-with-parsed-data "* Parent
1657 :PROPERTIES:
1658 :prop: value
1659 :END:
1660 ** Headline
1661 Paragraph"
1662 (org-export-get-node-property
1663 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
1664 ;; Cannot return a value before the first headline.
1665 (should-not
1666 (org-test-with-parsed-data "Paragraph
1667 * Headline
1668 :PROPERTIES:
1669 :prop: value
1670 :END:"
1671 (org-export-get-node-property
1672 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
1674 (ert-deftest test-org-export/get-category ()
1675 "Test `org-export-get-category' specifications."
1676 ;; Standard test.
1677 (should
1678 (equal "value"
1679 (org-test-with-parsed-data "* Headline
1680 :PROPERTIES:
1681 :CATEGORY: value
1682 :END:"
1683 (org-export-get-category
1684 (org-element-map tree 'headline 'identity nil t) info))))
1685 ;; Test inheritance from a parent headline.
1686 (should
1687 (equal '("value" "value")
1688 (org-test-with-parsed-data "* Headline1
1689 :PROPERTIES:
1690 :CATEGORY: value
1691 :END:
1692 ** Headline2"
1693 (org-element-map tree 'headline
1694 (lambda (hl) (org-export-get-category hl info)) info))))
1695 ;; Test inheritance from #+CATEGORY keyword
1696 (should
1697 (equal "value"
1698 (org-test-with-parsed-data "#+CATEGORY: value
1699 * Headline"
1700 (org-export-get-category
1701 (org-element-map tree 'headline 'identity nil t) info))))
1702 ;; Test inheritance from file name.
1703 (should
1704 (equal "test"
1705 (org-test-with-parsed-data "* Headline"
1706 (let ((info (plist-put info :input-file "~/test.org")))
1707 (org-export-get-category
1708 (org-element-map tree 'headline 'identity nil t) info)))))
1709 ;; Fall-back value.
1710 (should
1711 (equal "???"
1712 (org-test-with-parsed-data "* Headline"
1713 (org-export-get-category
1714 (org-element-map tree 'headline 'identity nil t) info)))))
1716 (ert-deftest test-org-export/first-sibling-p ()
1717 "Test `org-export-first-sibling-p' specifications."
1718 ;; Standard test.
1719 (should
1720 (equal
1721 '(yes yes no)
1722 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1723 (org-element-map tree 'headline
1724 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1725 info))))
1726 ;; Ignore headlines not exported.
1727 (should
1728 (equal
1729 '(yes)
1730 (let ((org-export-exclude-tags '("ignore")))
1731 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
1732 (org-element-map tree 'headline
1733 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1734 info))))))
1736 (ert-deftest test-org-export/last-sibling-p ()
1737 "Test `org-export-last-sibling-p' specifications."
1738 ;; Standard test.
1739 (should
1740 (equal
1741 '(yes no yes)
1742 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1743 (org-element-map tree 'headline
1744 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1745 info))))
1746 ;; Ignore headlines not exported.
1747 (should
1748 (equal
1749 '(yes)
1750 (let ((org-export-exclude-tags '("ignore")))
1751 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
1752 (org-element-map tree 'headline
1753 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1754 info))))))
1756 (ert-deftest test-org-export/handle-inlinetasks ()
1757 "Test inlinetask export."
1758 ;; Inlinetask with an exclude tag.
1759 (when (featurep 'org-inlinetask)
1760 (should
1761 (equal
1763 (let ((org-inlinetask-min-level 3))
1764 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
1765 (org-export-as (org-test-default-backend)
1766 nil nil nil '(:exclude-tags ("noexp")))))))
1767 ;; Inlinetask with an include tag.
1768 (should
1769 (equal
1770 "* H2\n*** Inline :exp:\n"
1771 (let ((org-inlinetask-min-level 3)
1772 (org-tags-column 0))
1773 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
1774 (org-export-as (org-test-default-backend)
1775 nil nil nil '(:select-tags ("exp")))))))
1776 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
1777 (should
1778 (equal ""
1779 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
1780 (org-inlinetask-min-level 3))
1781 (org-test-with-temp-text "*** TODO Inline"
1782 (org-export-as (org-test-default-backend)
1783 nil nil nil '(:with-tasks nil))))))))
1787 ;;; Keywords
1789 (ert-deftest test-org-export/get-date ()
1790 "Test `org-export-get-date' specifications."
1791 ;; Return a properly formatted string when
1792 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
1793 ;; consists in a single timestamp.
1794 (should
1795 (equal "29 03 2012"
1796 (let ((org-export-date-timestamp-format "%d %m %Y"))
1797 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1798 (org-export-get-date info)))))
1799 ;; Return a secondary string otherwise.
1800 (should-not
1801 (stringp
1802 (let ((org-export-date-timestamp-format nil))
1803 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1804 (org-export-get-date info)))))
1805 (should
1806 (equal '("Date")
1807 (org-test-with-parsed-data "#+DATE: Date"
1808 (org-export-get-date info))))
1809 ;; Optional argument has precedence over
1810 ;; `org-export-date-timestamp-format'.
1811 (should
1812 (equal "29 03"
1813 (let ((org-export-date-timestamp-format "%d %m %Y"))
1814 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1815 (org-export-get-date info "%d %m"))))))
1819 ;;; Links
1821 (ert-deftest test-org-export/get-coderef-format ()
1822 "Test `org-export-get-coderef-format' specifications."
1823 ;; A link without description returns "%s"
1824 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
1825 "%s"))
1826 ;; Return "%s" when path is matched within description.
1827 (should (equal (org-export-get-coderef-format "path" "desc (path)")
1828 "desc %s"))
1829 ;; Otherwise return description.
1830 (should (equal (org-export-get-coderef-format "path" "desc")
1831 "desc")))
1833 (ert-deftest test-org-export/inline-image-p ()
1834 "Test `org-export-inline-image-p' specifications."
1835 (should
1836 (org-export-inline-image-p
1837 (org-test-with-temp-text "[[#id]]"
1838 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
1839 '(("custom-id" . "id")))))
1841 (ert-deftest test-org-export/fuzzy-link ()
1842 "Test fuzzy links specifications."
1843 ;; Link to an headline should return headline's number.
1844 (org-test-with-parsed-data
1845 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
1846 (should
1847 ;; Note: Headline's number is in fact a list of numbers.
1848 (equal '(2)
1849 (org-element-map tree 'link
1850 (lambda (link)
1851 (org-export-get-ordinal
1852 (org-export-resolve-fuzzy-link link info) info)) info t))))
1853 ;; Link to a target in an item should return item's number.
1854 (org-test-with-parsed-data
1855 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
1856 (should
1857 ;; Note: Item's number is in fact a list of numbers.
1858 (equal '(1 2)
1859 (org-element-map tree 'link
1860 (lambda (link)
1861 (org-export-get-ordinal
1862 (org-export-resolve-fuzzy-link link info) info)) info t))))
1863 ;; Link to a target in a footnote should return footnote's number.
1864 (org-test-with-parsed-data "
1865 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
1866 (should
1867 (equal '(2 3)
1868 (org-element-map tree 'link
1869 (lambda (link)
1870 (org-export-get-ordinal
1871 (org-export-resolve-fuzzy-link link info) info)) info))))
1872 ;; Link to a named element should return sequence number of that
1873 ;; element.
1874 (org-test-with-parsed-data
1875 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
1876 (should
1877 (= 2
1878 (org-element-map tree 'link
1879 (lambda (link)
1880 (org-export-get-ordinal
1881 (org-export-resolve-fuzzy-link link info) info)) info t))))
1882 ;; Link to a target not within an item, a table, a footnote
1883 ;; reference or definition should return section number.
1884 (org-test-with-parsed-data
1885 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
1886 (should
1887 (equal '(2)
1888 (org-element-map tree 'link
1889 (lambda (link)
1890 (org-export-get-ordinal
1891 (org-export-resolve-fuzzy-link link info) info)) info t))))
1892 ;; Space are not significant when matching a fuzzy link.
1893 (should
1894 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
1895 (org-element-map tree 'link
1896 (lambda (link) (org-export-resolve-fuzzy-link link info))
1897 info t)))
1898 ;; Statistics cookies are ignored for headline match.
1899 (should
1900 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
1901 (org-element-map tree 'link
1902 (lambda (link) (org-export-resolve-fuzzy-link link info))
1903 info t)))
1904 (should
1905 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
1906 (org-element-map tree 'link
1907 (lambda (link) (org-export-resolve-fuzzy-link link info))
1908 info t)))
1909 ;; Headline match is position dependent.
1910 (should-not
1911 (apply
1913 (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
1914 (org-element-map tree 'link
1915 (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
1917 (ert-deftest test-org-export/resolve-coderef ()
1918 "Test `org-export-resolve-coderef' specifications."
1919 (let ((org-coderef-label-format "(ref:%s)"))
1920 ;; 1. A link to a "-n -k -r" block returns line number.
1921 (org-test-with-parsed-data
1922 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
1923 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1924 (org-test-with-parsed-data
1925 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1926 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1927 ;; 2. A link to a "-n -r" block returns line number.
1928 (org-test-with-parsed-data
1929 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
1930 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1931 (org-test-with-parsed-data
1932 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1933 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1934 ;; 3. A link to a "-n" block returns coderef.
1935 (org-test-with-parsed-data
1936 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1937 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1938 (org-test-with-parsed-data
1939 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
1940 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1941 ;; 4. A link to a "-r" block returns line number.
1942 (org-test-with-parsed-data
1943 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1944 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1945 (org-test-with-parsed-data
1946 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
1947 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1948 ;; 5. A link to a block without a switch returns coderef.
1949 (org-test-with-parsed-data
1950 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1951 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1952 (org-test-with-parsed-data
1953 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
1954 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1955 ;; 6. Correctly handle continued line numbers. A "+n" switch
1956 ;; should resume numbering from previous block with numbered
1957 ;; lines, ignoring blocks not numbering lines in the process.
1958 ;; A "-n" switch resets count.
1959 (org-test-with-parsed-data "
1960 #+BEGIN_EXAMPLE -n
1961 Text.
1962 #+END_EXAMPLE
1964 #+BEGIN_SRC emacs-lisp
1965 \(- 1 1)
1966 #+END_SRC
1968 #+BEGIN_SRC emacs-lisp +n -r
1969 \(+ 1 1) (ref:addition)
1970 #+END_SRC
1972 #+BEGIN_EXAMPLE -n -r
1973 Another text. (ref:text)
1974 #+END_EXAMPLE"
1975 (should (= (org-export-resolve-coderef "addition" info) 2))
1976 (should (= (org-export-resolve-coderef "text" info) 1)))
1977 ;; 7. Recognize coderef with user-specified syntax.
1978 (org-test-with-parsed-data
1979 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
1980 (should (equal (org-export-resolve-coderef "text" info) "text")))))
1982 (ert-deftest test-org-export/resolve-fuzzy-link ()
1983 "Test `org-export-resolve-fuzzy-link' specifications."
1984 ;; Match target objects.
1985 (should
1986 (org-test-with-parsed-data "<<target>> [[target]]"
1987 (org-export-resolve-fuzzy-link
1988 (org-element-map tree 'link 'identity info t) info)))
1989 ;; Match named elements.
1990 (should
1991 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
1992 (org-export-resolve-fuzzy-link
1993 (org-element-map tree 'link 'identity info t) info)))
1994 ;; Match exact headline's name.
1995 (should
1996 (org-test-with-parsed-data "* My headline\n[[My headline]]"
1997 (org-export-resolve-fuzzy-link
1998 (org-element-map tree 'link 'identity info t) info)))
1999 ;; Targets objects have priority over named elements and headline
2000 ;; titles.
2001 (should
2002 (eq 'target
2003 (org-test-with-parsed-data
2004 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
2005 (org-element-type
2006 (org-export-resolve-fuzzy-link
2007 (org-element-map tree 'link 'identity info t) info)))))
2008 ;; Named elements have priority over headline titles.
2009 (should
2010 (eq 'paragraph
2011 (org-test-with-parsed-data
2012 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
2013 (org-element-type
2014 (org-export-resolve-fuzzy-link
2015 (org-element-map tree 'link 'identity info t) info)))))
2016 ;; If link's path starts with a "*", only match headline titles,
2017 ;; though.
2018 (should
2019 (eq 'headline
2020 (org-test-with-parsed-data
2021 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
2022 (org-element-type
2023 (org-export-resolve-fuzzy-link
2024 (org-element-map tree 'link 'identity info t) info)))))
2025 ;; Return nil if no match.
2026 (should-not
2027 (org-test-with-parsed-data "[[target]]"
2028 (org-export-resolve-fuzzy-link
2029 (org-element-map tree 'link 'identity info t) info)))
2030 ;; Match fuzzy link even when before first headline.
2031 (should
2032 (eq 'headline
2033 (org-test-with-parsed-data "[[hl]]\n* hl"
2034 (org-element-type
2035 (org-export-resolve-fuzzy-link
2036 (org-element-map tree 'link 'identity info t) info))))))
2038 (ert-deftest test-org-export/resolve-id-link ()
2039 "Test `org-export-resolve-id-link' specifications."
2040 ;; 1. Regular test for custom-id link.
2041 (org-test-with-parsed-data "* Headline1
2042 :PROPERTIES:
2043 :CUSTOM_ID: test
2044 :END:
2045 * Headline 2
2046 \[[#test]]"
2047 (should
2048 (org-export-resolve-id-link
2049 (org-element-map tree 'link 'identity info t) info)))
2050 ;; 2. Failing test for custom-id link.
2051 (org-test-with-parsed-data "* Headline1
2052 :PROPERTIES:
2053 :CUSTOM_ID: test
2054 :END:
2055 * Headline 2
2056 \[[#no-match]]"
2057 (should-not
2058 (org-export-resolve-id-link
2059 (org-element-map tree 'link 'identity info t) info)))
2060 ;; 3. Test for internal id target.
2061 (org-test-with-parsed-data "* Headline1
2062 :PROPERTIES:
2063 :ID: aaaa
2064 :END:
2065 * Headline 2
2066 \[[id:aaaa]]"
2067 (should
2068 (org-export-resolve-id-link
2069 (org-element-map tree 'link 'identity info t) info)))
2070 ;; 4. Test for external id target.
2071 (org-test-with-parsed-data "[[id:aaaa]]"
2072 (should
2073 (org-export-resolve-id-link
2074 (org-element-map tree 'link 'identity info t)
2075 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
2077 (ert-deftest test-org-export/resolve-radio-link ()
2078 "Test `org-export-resolve-radio-link' specifications."
2079 ;; Standard test.
2080 (should
2081 (org-test-with-temp-text "<<<radio>>> radio"
2082 (org-update-radio-target-regexp)
2083 (let* ((tree (org-element-parse-buffer))
2084 (info `(:parse-tree ,tree)))
2085 (org-export-resolve-radio-link
2086 (org-element-map tree 'link 'identity info t)
2087 info))))
2088 ;; Radio targets are case-insensitive.
2089 (should
2090 (org-test-with-temp-text "<<<RADIO>>> radio"
2091 (org-update-radio-target-regexp)
2092 (let* ((tree (org-element-parse-buffer))
2093 (info `(:parse-tree ,tree)))
2094 (org-export-resolve-radio-link
2095 (org-element-map tree 'link 'identity info t)
2096 info))))
2097 ;; Radio target with objects.
2098 (should
2099 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
2100 (org-update-radio-target-regexp)
2101 (let* ((tree (org-element-parse-buffer))
2102 (info `(:parse-tree ,tree)))
2103 (org-export-resolve-radio-link
2104 (org-element-map tree 'link 'identity info t)
2105 info))))
2106 ;; Radio target with objects at its beginning.
2107 (should
2108 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
2109 (org-update-radio-target-regexp)
2110 (let* ((tree (org-element-parse-buffer))
2111 (info `(:parse-tree ,tree)))
2112 (org-export-resolve-radio-link
2113 (org-element-map tree 'link 'identity info t)
2114 info))))
2115 ;; Radio link next to an apostrophe.
2116 (should
2117 (org-test-with-temp-text "<<<radio>>> radio's"
2118 (org-update-radio-target-regexp)
2119 (let* ((tree (org-element-parse-buffer))
2120 (info `(:parse-tree ,tree)))
2121 (org-export-resolve-radio-link
2122 (org-element-map tree 'link 'identity info t)
2123 info))))
2124 ;; Multiple radio targets.
2125 (should
2126 (equal '("radio1" "radio2")
2127 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
2128 (org-update-radio-target-regexp)
2129 (let* ((tree (org-element-parse-buffer))
2130 (info `(:parse-tree ,tree)))
2131 (org-element-map tree 'link
2132 (lambda (link)
2133 (org-element-property
2134 :value (org-export-resolve-radio-link link info)))
2135 info)))))
2136 ;; Radio target is whitespace insensitive.
2137 (should
2138 (org-test-with-temp-text "<<<a radio>>> a\n radio"
2139 (org-update-radio-target-regexp)
2140 (let* ((tree (org-element-parse-buffer))
2141 (info `(:parse-tree ,tree)))
2142 (org-element-map tree 'link
2143 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
2147 ;;; Src-block and example-block
2149 (ert-deftest test-org-export/unravel-code ()
2150 "Test `org-export-unravel-code' function."
2151 ;; Code without reference.
2152 (should
2153 (equal '("(+ 1 1)\n")
2154 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2155 (org-export-unravel-code (org-element-at-point)))))
2156 ;; Code with reference.
2157 (should
2158 (equal '("(+ 1 1)\n" (1 . "test"))
2159 (org-test-with-temp-text
2160 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
2161 (let ((org-coderef-label-format "(ref:%s)"))
2162 (org-export-unravel-code (org-element-at-point))))))
2163 ;; Code with user-defined reference.
2164 (should
2165 (equal
2166 '("(+ 1 1)\n" (1 . "test"))
2167 (org-test-with-temp-text
2168 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
2169 (let ((org-coderef-label-format "(ref:%s)"))
2170 (org-export-unravel-code (org-element-at-point))))))
2171 ;; Code references keys are relative to the current block.
2172 (should
2173 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
2174 (org-test-with-temp-text "
2175 #+BEGIN_EXAMPLE -n
2176 \(+ 1 1)
2177 #+END_EXAMPLE
2178 #+BEGIN_EXAMPLE +n
2179 \(+ 2 2)
2180 \(+ 3 3) (ref:one)
2181 #+END_EXAMPLE"
2182 (goto-line 5)
2183 (let ((org-coderef-label-format "(ref:%s)"))
2184 (org-export-unravel-code (org-element-at-point)))))))
2186 (ert-deftest test-org-export/format-code-default ()
2187 "Test `org-export-format-code-default' specifications."
2188 ;; Return the empty string when code is empty.
2189 (should
2190 (equal ""
2191 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
2192 (org-export-format-code-default
2193 (org-element-map tree 'src-block 'identity info t) info))))
2194 ;; Number lines, two whitespace characters before the actual loc.
2195 (should
2196 (equal "1 a\n2 b\n"
2197 (org-test-with-parsed-data
2198 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
2199 (org-export-format-code-default
2200 (org-element-map tree 'src-block 'identity info t) info))))
2201 ;; Put references 6 whitespace characters after the widest line,
2202 ;; wrapped within parenthesis.
2203 (should
2204 (equal "123 (a)\n1 (b)\n"
2205 (let ((org-coderef-label-format "(ref:%s)"))
2206 (org-test-with-parsed-data
2207 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
2208 (org-export-format-code-default
2209 (org-element-map tree 'src-block 'identity info t) info))))))
2213 ;;; Smart Quotes
2215 (ert-deftest test-org-export/activate-smart-quotes ()
2216 "Test `org-export-activate-smart-quotes' specifications."
2217 ;; Opening double quotes: standard test.
2218 (should
2219 (equal
2220 '("some &ldquo;paragraph")
2221 (let ((org-export-default-language "en"))
2222 (org-test-with-parsed-data "some \"paragraph"
2223 (org-element-map tree 'plain-text
2224 (lambda (s) (org-export-activate-smart-quotes s :html info))
2225 info)))))
2226 ;; Opening quotes: at the beginning of a paragraph.
2227 (should
2228 (equal
2229 '("&ldquo;begin")
2230 (let ((org-export-default-language "en"))
2231 (org-test-with-parsed-data "\"begin"
2232 (org-element-map tree 'plain-text
2233 (lambda (s) (org-export-activate-smart-quotes s :html info))
2234 info)))))
2235 ;; Opening quotes: after an object.
2236 (should
2237 (equal
2238 '("&ldquo;begin")
2239 (let ((org-export-default-language "en"))
2240 (org-test-with-parsed-data "=verb= \"begin"
2241 (org-element-map tree 'plain-text
2242 (lambda (s) (org-export-activate-smart-quotes s :html info))
2243 info)))))
2244 ;; Closing quotes: standard test.
2245 (should
2246 (equal
2247 '("some&rdquo; paragraph")
2248 (let ((org-export-default-language "en"))
2249 (org-test-with-parsed-data "some\" paragraph"
2250 (org-element-map tree 'plain-text
2251 (lambda (s) (org-export-activate-smart-quotes s :html info))
2252 info)))))
2253 ;; Closing quotes: at the end of a paragraph.
2254 (should
2255 (equal
2256 '("end&rdquo;")
2257 (let ((org-export-default-language "en"))
2258 (org-test-with-parsed-data "end\""
2259 (org-element-map tree 'plain-text
2260 (lambda (s) (org-export-activate-smart-quotes s :html info))
2261 info)))))
2262 ;; Apostrophe: standard test.
2263 (should
2264 (equal
2265 '("It shouldn&rsquo;t fail")
2266 (let ((org-export-default-language "en"))
2267 (org-test-with-parsed-data "It shouldn't fail"
2268 (org-element-map tree 'plain-text
2269 (lambda (s) (org-export-activate-smart-quotes s :html info))
2270 info)))))
2271 ;; Apostrophe: before an object.
2272 (should
2273 (equal
2274 '("a&rsquo;")
2275 (let ((org-export-default-language "en"))
2276 (org-test-with-parsed-data "a'=b="
2277 (org-element-map tree 'plain-text
2278 (lambda (s) (org-export-activate-smart-quotes s :html info))
2279 info)))))
2280 ;; Apostrophe: after an object.
2281 (should
2282 (equal
2283 '("&rsquo;s")
2284 (let ((org-export-default-language "en"))
2285 (org-test-with-parsed-data "=code='s"
2286 (org-element-map tree 'plain-text
2287 (lambda (s) (org-export-activate-smart-quotes s :html info))
2288 info)))))
2289 ;; Special case: isolated quotes.
2290 (should
2291 (equal '("&ldquo;" "&rdquo;")
2292 (let ((org-export-default-language "en"))
2293 (org-test-with-parsed-data "\"$x$\""
2294 (org-element-map tree 'plain-text
2295 (lambda (s) (org-export-activate-smart-quotes s :html info))
2296 info)))))
2297 ;; Smart quotes in secondary strings.
2298 (should
2299 (equal '("&ldquo;" "&rdquo;")
2300 (let ((org-export-default-language "en"))
2301 (org-test-with-parsed-data "* \"$x$\""
2302 (org-element-map tree 'plain-text
2303 (lambda (s) (org-export-activate-smart-quotes s :html info))
2304 info)))))
2305 ;; Smart quotes in document keywords.
2306 (should
2307 (equal '("&ldquo;" "&rdquo;")
2308 (let ((org-export-default-language "en"))
2309 (org-test-with-parsed-data "#+TITLE: \"$x$\""
2310 (org-element-map (plist-get info :title) 'plain-text
2311 (lambda (s) (org-export-activate-smart-quotes s :html info))
2312 info)))))
2313 ;; Smart quotes in parsed affiliated keywords.
2314 (should
2315 (equal '("&ldquo;" "&rdquo;" "Paragraph")
2316 (let ((org-export-default-language "en"))
2317 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
2318 (org-element-map tree 'plain-text
2319 (lambda (s) (org-export-activate-smart-quotes s :html info))
2320 info nil nil t))))))
2324 ;;; Tables
2326 (ert-deftest test-org-export/special-column ()
2327 "Test if the table's special column is properly recognized."
2328 ;; 1. First column is special if it contains only a special marking
2329 ;; characters or empty cells.
2330 (org-test-with-temp-text "
2331 | ! | 1 |
2332 | | 2 |"
2333 (should
2334 (org-export-table-has-special-column-p
2335 (org-element-map
2336 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2337 ;; 2. If the column contains anything else, it isn't special.
2338 (org-test-with-temp-text "
2339 | ! | 1 |
2340 | b | 2 |"
2341 (should-not
2342 (org-export-table-has-special-column-p
2343 (org-element-map
2344 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2345 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
2346 ;; and "!".
2347 (org-test-with-temp-text "
2348 | # | 1 |
2349 | ^ | 2 |
2350 | * | 3 |
2351 | _ | 4 |
2352 | / | 5 |
2353 | $ | 6 |
2354 | ! | 7 |"
2355 (should
2356 (org-export-table-has-special-column-p
2357 (org-element-map
2358 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2359 ;; 4. A first column with only empty cells isn't considered as
2360 ;; special.
2361 (org-test-with-temp-text "
2362 | | 1 |
2363 | | 2 |"
2364 (should-not
2365 (org-export-table-has-special-column-p
2366 (org-element-map
2367 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
2369 (ert-deftest test-org-export/table-row-is-special-p ()
2370 "Test `org-export-table-row-is-special-p' specifications."
2371 ;; 1. A row is special if it has a special marking character in the
2372 ;; special column.
2373 (org-test-with-parsed-data "| ! | 1 |"
2374 (should
2375 (org-export-table-row-is-special-p
2376 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2377 ;; 2. A row is special when its first field is "/"
2378 (org-test-with-parsed-data "
2379 | / | 1 |
2380 | a | b |"
2381 (should
2382 (org-export-table-row-is-special-p
2383 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2384 ;; 3. A row only containing alignment cookies is also considered as
2385 ;; special.
2386 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
2387 (should
2388 (org-export-table-row-is-special-p
2389 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2390 ;; 4. Everything else isn't considered as special.
2391 (org-test-with-parsed-data "| \alpha | | c |"
2392 (should-not
2393 (org-export-table-row-is-special-p
2394 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2395 ;; 5. Table's rules are never considered as special rows.
2396 (org-test-with-parsed-data "|---+---|"
2397 (should-not
2398 (org-export-table-row-is-special-p
2399 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
2401 (ert-deftest test-org-export/has-header-p ()
2402 "Test `org-export-table-has-header-p' specifications."
2403 ;; 1. With an header.
2404 (org-test-with-parsed-data "
2405 | a | b |
2406 |---+---|
2407 | c | d |"
2408 (should
2409 (org-export-table-has-header-p
2410 (org-element-map tree 'table 'identity info 'first-match)
2411 info)))
2412 ;; 2. Without an header.
2413 (org-test-with-parsed-data "
2414 | a | b |
2415 | c | d |"
2416 (should-not
2417 (org-export-table-has-header-p
2418 (org-element-map tree 'table 'identity info 'first-match)
2419 info)))
2420 ;; 3. Don't get fooled with starting and ending rules.
2421 (org-test-with-parsed-data "
2422 |---+---|
2423 | a | b |
2424 | c | d |
2425 |---+---|"
2426 (should-not
2427 (org-export-table-has-header-p
2428 (org-element-map tree 'table 'identity info 'first-match)
2429 info))))
2431 (ert-deftest test-org-export/table-row-group ()
2432 "Test `org-export-table-row-group' specifications."
2433 ;; 1. A rule creates a new group.
2434 (should
2435 (equal '(1 rule 2)
2436 (org-test-with-parsed-data "
2437 | a | b |
2438 |---+---|
2439 | 1 | 2 |"
2440 (org-element-map tree 'table-row
2441 (lambda (row)
2442 (if (eq (org-element-property :type row) 'rule) 'rule
2443 (org-export-table-row-group row info)))))))
2444 ;; 2. Special rows are ignored in count.
2445 (should
2446 (equal
2447 '(rule 1)
2448 (org-test-with-parsed-data "
2449 | / | < | > |
2450 |---|---+---|
2451 | | 1 | 2 |"
2452 (org-element-map tree 'table-row
2453 (lambda (row)
2454 (if (eq (org-element-property :type row) 'rule) 'rule
2455 (org-export-table-row-group row info)))
2456 info))))
2457 ;; 3. Double rules also are ignored in count.
2458 (should
2459 (equal '(1 rule rule 2)
2460 (org-test-with-parsed-data "
2461 | a | b |
2462 |---+---|
2463 |---+---|
2464 | 1 | 2 |"
2465 (org-element-map tree 'table-row
2466 (lambda (row)
2467 (if (eq (org-element-property :type row) 'rule) 'rule
2468 (org-export-table-row-group row info))))))))
2470 (ert-deftest test-org-export/table-row-number ()
2471 "Test `org-export-table-row-number' specifications."
2472 ;; Standard test. Number is 0-indexed.
2473 (should
2474 (equal '(0 1)
2475 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
2476 (org-element-map tree 'table-row
2477 (lambda (row) (org-export-table-row-number row info)) info))))
2478 ;; Number ignores separators.
2479 (should
2480 (equal '(0 1)
2481 (org-test-with-parsed-data "
2482 | a | b | c |
2483 |---+---+---|
2484 | d | e | f |"
2485 (org-element-map tree 'table-row
2486 (lambda (row) (org-export-table-row-number row info)) info))))
2487 ;; Number ignores special rows.
2488 (should
2489 (equal '(0 1)
2490 (org-test-with-parsed-data "
2491 | / | < | > |
2492 | | b | c |
2493 |---+-----+-----|
2494 | | <c> | <c> |
2495 | | e | f |"
2496 (org-element-map tree 'table-row
2497 (lambda (row) (org-export-table-row-number row info)) info)))))
2499 (ert-deftest test-org-export/table-cell-width ()
2500 "Test `org-export-table-cell-width' specifications."
2501 ;; 1. Width is primarily determined by width cookies. If no cookie
2502 ;; is found, cell's width is nil.
2503 (org-test-with-parsed-data "
2504 | / | <l> | <6> | <l7> |
2505 | | a | b | c |"
2506 (should
2507 (equal
2508 '(nil 6 7)
2509 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2510 (org-element-map tree 'table-cell 'identity info)))))
2511 ;; 2. The last width cookie has precedence.
2512 (org-test-with-parsed-data "
2513 | <6> |
2514 | <7> |
2515 | a |"
2516 (should
2517 (equal
2518 '(7)
2519 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2520 (org-element-map tree 'table-cell 'identity info)))))
2521 ;; 3. Valid width cookies must have a specific row.
2522 (org-test-with-parsed-data "| <6> | cell |"
2523 (should
2524 (equal
2525 '(nil nil)
2526 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2527 (org-element-map tree 'table-cell 'identity))))))
2529 (ert-deftest test-org-export/table-cell-alignment ()
2530 "Test `org-export-table-cell-alignment' specifications."
2531 ;; 1. Alignment is primarily determined by alignment cookies.
2532 (should
2533 (equal '(left center right)
2534 (let ((org-table-number-fraction 0.5)
2535 (org-table-number-regexp "^[0-9]+$"))
2536 (org-test-with-parsed-data "| <l> | <c> | <r> |"
2537 (mapcar (lambda (cell)
2538 (org-export-table-cell-alignment cell info))
2539 (org-element-map tree 'table-cell 'identity))))))
2540 ;; 2. The last alignment cookie has precedence.
2541 (should
2542 (equal '(right right right)
2543 (org-test-with-parsed-data "
2544 | <l8> |
2545 | cell |
2546 | <r9> |"
2547 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
2548 (org-element-map tree 'table-cell 'identity)))))
2549 ;; 3. If there's no cookie, cell's contents determine alignment.
2550 ;; A column mostly made of cells containing numbers will align
2551 ;; its cells to the right.
2552 (should
2553 (equal '(right right right)
2554 (let ((org-table-number-fraction 0.5)
2555 (org-table-number-regexp "^[0-9]+$"))
2556 (org-test-with-parsed-data "
2557 | 123 |
2558 | some text |
2559 | 12345 |"
2560 (mapcar (lambda (cell)
2561 (org-export-table-cell-alignment cell info))
2562 (org-element-map tree 'table-cell 'identity))))))
2563 ;; 4. Otherwise, they will be aligned to the left.
2564 (should
2565 (equal '(left left left)
2566 (org-test-with-parsed-data "
2567 | text |
2568 | some text |
2569 | \alpha |"
2570 (mapcar (lambda (cell)
2571 (org-export-table-cell-alignment cell info))
2572 (org-element-map tree 'table-cell 'identity info))))))
2574 (ert-deftest test-org-export/table-cell-borders ()
2575 "Test `org-export-table-cell-borders' specifications."
2576 ;; 1. Recognize various column groups indicators.
2577 (org-test-with-parsed-data "| / | < | > | <> |"
2578 (should
2579 (equal
2580 '((right bottom top) (left bottom top) (right bottom top)
2581 (right left bottom top))
2582 (mapcar (lambda (cell)
2583 (org-export-table-cell-borders cell info))
2584 (org-element-map tree 'table-cell 'identity)))))
2585 ;; 2. Accept shortcuts to define column groups.
2586 (org-test-with-parsed-data "| / | < | < |"
2587 (should
2588 (equal
2589 '((right bottom top) (right left bottom top) (left bottom top))
2590 (mapcar (lambda (cell)
2591 (org-export-table-cell-borders cell info))
2592 (org-element-map tree 'table-cell 'identity)))))
2593 ;; 3. A valid column groups row must start with a "/".
2594 (org-test-with-parsed-data "
2595 | | < |
2596 | a | b |"
2597 (should
2598 (equal '((top) (top) (bottom) (bottom))
2599 (mapcar (lambda (cell)
2600 (org-export-table-cell-borders cell info))
2601 (org-element-map tree 'table-cell 'identity)))))
2602 ;; 4. Take table rules into consideration.
2603 (org-test-with-parsed-data "
2604 | 1 |
2605 |---|
2606 | 2 |"
2607 (should
2608 (equal '((below top) (bottom above))
2609 (mapcar (lambda (cell)
2610 (org-export-table-cell-borders cell info))
2611 (org-element-map tree 'table-cell 'identity)))))
2612 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
2613 ;; (resp. `bottom' and `below') borders. Any special row is
2614 ;; ignored.
2615 (org-test-with-parsed-data "
2616 |---+----|
2617 | / | |
2618 | | 1 |
2619 |---+----|"
2620 (should
2621 (equal '((bottom below top above))
2622 (last
2623 (mapcar (lambda (cell)
2624 (org-export-table-cell-borders cell info))
2625 (org-element-map tree 'table-cell 'identity)))))))
2627 (ert-deftest test-org-export/table-dimensions ()
2628 "Test `org-export-table-dimensions' specifications."
2629 ;; 1. Standard test.
2630 (org-test-with-parsed-data "
2631 | 1 | 2 | 3 |
2632 | 4 | 5 | 6 |"
2633 (should
2634 (equal '(2 . 3)
2635 (org-export-table-dimensions
2636 (org-element-map tree 'table 'identity info 'first-match) info))))
2637 ;; 2. Ignore horizontal rules and special columns.
2638 (org-test-with-parsed-data "
2639 | / | < | > |
2640 | 1 | 2 | 3 |
2641 |---+---+---|
2642 | 4 | 5 | 6 |"
2643 (should
2644 (equal '(2 . 3)
2645 (org-export-table-dimensions
2646 (org-element-map tree 'table 'identity info 'first-match) info)))))
2648 (ert-deftest test-org-export/table-cell-address ()
2649 "Test `org-export-table-cell-address' specifications."
2650 ;; 1. Standard test: index is 0-based.
2651 (org-test-with-parsed-data "| a | b |"
2652 (should
2653 (equal '((0 . 0) (0 . 1))
2654 (org-element-map tree 'table-cell
2655 (lambda (cell) (org-export-table-cell-address cell info))
2656 info))))
2657 ;; 2. Special column isn't counted, nor are special rows.
2658 (org-test-with-parsed-data "
2659 | / | <> |
2660 | | c |"
2661 (should
2662 (equal '(0 . 0)
2663 (org-export-table-cell-address
2664 (car (last (org-element-map tree 'table-cell 'identity info)))
2665 info))))
2666 ;; 3. Tables rules do not count either.
2667 (org-test-with-parsed-data "
2668 | a |
2669 |---|
2670 | b |
2671 |---|
2672 | c |"
2673 (should
2674 (equal '(2 . 0)
2675 (org-export-table-cell-address
2676 (car (last (org-element-map tree 'table-cell 'identity info)))
2677 info))))
2678 ;; 4. Return nil for special cells.
2679 (org-test-with-parsed-data "| / | a |"
2680 (should-not
2681 (org-export-table-cell-address
2682 (org-element-map tree 'table-cell 'identity nil 'first-match)
2683 info))))
2685 (ert-deftest test-org-export/get-table-cell-at ()
2686 "Test `org-export-get-table-cell-at' specifications."
2687 ;; 1. Address ignores special columns, special rows and rules.
2688 (org-test-with-parsed-data "
2689 | / | <> |
2690 | | a |
2691 |---+----|
2692 | | b |"
2693 (should
2694 (equal '("b")
2695 (org-element-contents
2696 (org-export-get-table-cell-at
2697 '(1 . 0)
2698 (org-element-map tree 'table 'identity info 'first-match)
2699 info)))))
2700 ;; 2. Return value for a non-existent address is nil.
2701 (org-test-with-parsed-data "| a |"
2702 (should-not
2703 (org-export-get-table-cell-at
2704 '(2 . 2)
2705 (org-element-map tree 'table 'identity info 'first-match)
2706 info)))
2707 (org-test-with-parsed-data "| / |"
2708 (should-not
2709 (org-export-get-table-cell-at
2710 '(0 . 0)
2711 (org-element-map tree 'table 'identity info 'first-match)
2712 info))))
2714 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
2715 "Test `org-export-table-cell-starts-colgroup-p' specifications."
2716 ;; 1. A cell at a beginning of a row always starts a column group.
2717 (org-test-with-parsed-data "| a |"
2718 (should
2719 (org-export-table-cell-starts-colgroup-p
2720 (org-element-map tree 'table-cell 'identity info 'first-match)
2721 info)))
2722 ;; 2. Special column should be ignored when determining the
2723 ;; beginning of the row.
2724 (org-test-with-parsed-data "
2725 | / | |
2726 | | a |"
2727 (should
2728 (org-export-table-cell-starts-colgroup-p
2729 (org-element-map tree 'table-cell 'identity info 'first-match)
2730 info)))
2731 ;; 2. Explicit column groups.
2732 (org-test-with-parsed-data "
2733 | / | | < |
2734 | a | b | c |"
2735 (should
2736 (equal
2737 '(yes no yes)
2738 (org-element-map tree 'table-cell
2739 (lambda (cell)
2740 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
2741 info)))))
2743 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
2744 "Test `org-export-table-cell-ends-colgroup-p' specifications."
2745 ;; 1. A cell at the end of a row always ends a column group.
2746 (org-test-with-parsed-data "| a |"
2747 (should
2748 (org-export-table-cell-ends-colgroup-p
2749 (org-element-map tree 'table-cell 'identity info 'first-match)
2750 info)))
2751 ;; 2. Special column should be ignored when determining the
2752 ;; beginning of the row.
2753 (org-test-with-parsed-data "
2754 | / | |
2755 | | a |"
2756 (should
2757 (org-export-table-cell-ends-colgroup-p
2758 (org-element-map tree 'table-cell 'identity info 'first-match)
2759 info)))
2760 ;; 3. Explicit column groups.
2761 (org-test-with-parsed-data "
2762 | / | < | |
2763 | a | b | c |"
2764 (should
2765 (equal
2766 '(yes no yes)
2767 (org-element-map tree 'table-cell
2768 (lambda (cell)
2769 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
2770 info)))))
2772 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
2773 "Test `org-export-table-row-starts-rowgroup-p' specifications."
2774 ;; 1. A row at the beginning of a table always starts a row group.
2775 ;; So does a row following a table rule.
2776 (org-test-with-parsed-data "
2777 | a |
2778 |---|
2779 | b |"
2780 (should
2781 (equal
2782 '(yes no yes)
2783 (org-element-map tree 'table-row
2784 (lambda (row)
2785 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2786 info))))
2787 ;; 2. Special rows should be ignored when determining the beginning
2788 ;; of the row.
2789 (org-test-with-parsed-data "
2790 | / | < |
2791 | | a |
2792 |---+---|
2793 | / | < |
2794 | | b |"
2795 (should
2796 (equal
2797 '(yes no yes)
2798 (org-element-map tree 'table-row
2799 (lambda (row)
2800 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2801 info)))))
2803 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
2804 "Test `org-export-table-row-ends-rowgroup-p' specifications."
2805 ;; 1. A row at the end of a table always ends a row group. So does
2806 ;; a row preceding a table rule.
2807 (org-test-with-parsed-data "
2808 | a |
2809 |---|
2810 | b |"
2811 (should
2812 (equal
2813 '(yes no yes)
2814 (org-element-map tree 'table-row
2815 (lambda (row)
2816 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2817 info))))
2818 ;; 2. Special rows should be ignored when determining the beginning
2819 ;; of the row.
2820 (org-test-with-parsed-data "
2821 | | a |
2822 | / | < |
2823 |---+---|
2824 | | b |
2825 | / | < |"
2826 (should
2827 (equal
2828 '(yes no yes)
2829 (org-element-map tree 'table-row
2830 (lambda (row)
2831 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2832 info)))))
2834 (ert-deftest test-org-export/table-row-starts-header-p ()
2835 "Test `org-export-table-row-starts-header-p' specifications."
2836 ;; 1. Only the row starting the first row group starts the table
2837 ;; header.
2838 (org-test-with-parsed-data "
2839 | a |
2840 | b |
2841 |---|
2842 | c |"
2843 (should
2844 (equal
2845 '(yes no no no)
2846 (org-element-map tree 'table-row
2847 (lambda (row)
2848 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
2849 info))))
2850 ;; 2. A row cannot start an header if there's no header in the
2851 ;; table.
2852 (org-test-with-parsed-data "
2853 | a |
2854 |---|"
2855 (should-not
2856 (org-export-table-row-starts-header-p
2857 (org-element-map tree 'table-row 'identity info 'first-match)
2858 info))))
2860 (ert-deftest test-org-export/table-row-ends-header-p ()
2861 "Test `org-export-table-row-ends-header-p' specifications."
2862 ;; 1. Only the row starting the first row group starts the table
2863 ;; header.
2864 (org-test-with-parsed-data "
2865 | a |
2866 | b |
2867 |---|
2868 | c |"
2869 (should
2870 (equal
2871 '(no yes no no)
2872 (org-element-map tree 'table-row
2873 (lambda (row)
2874 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
2875 info))))
2876 ;; 2. A row cannot start an header if there's no header in the
2877 ;; table.
2878 (org-test-with-parsed-data "
2879 | a |
2880 |---|"
2881 (should-not
2882 (org-export-table-row-ends-header-p
2883 (org-element-map tree 'table-row 'identity info 'first-match)
2884 info))))
2888 ;;; Tables of Contents
2890 (ert-deftest test-org-export/collect-headlines ()
2891 "Test `org-export-collect-headlines' specifications."
2892 ;; Standard test.
2893 (should
2894 (= 2
2895 (length
2896 (org-test-with-parsed-data "* H1\n** H2"
2897 (org-export-collect-headlines info)))))
2898 ;; Do not collect headlines below optional argument.
2899 (should
2900 (= 1
2901 (length
2902 (org-test-with-parsed-data "* H1\n** H2"
2903 (org-export-collect-headlines info 1)))))
2904 ;; Never collect headlines below maximum headline level.
2905 (should
2906 (= 1
2907 (length
2908 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
2909 (org-export-collect-headlines info)))))
2910 (should
2911 (= 1
2912 (length
2913 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
2914 (org-export-collect-headlines info 2))))))
2918 ;;; Templates
2920 (ert-deftest test-org-export/inner-template ()
2921 "Test `inner-template' translator specifications."
2922 (should
2923 (equal "Success!"
2924 (org-test-with-temp-text "* Headline"
2925 (org-export-as
2926 (org-export-create-backend
2927 :transcoders
2928 '((inner-template . (lambda (contents info) "Success!"))
2929 (headline . (lambda (h c i) "Headline"))))))))
2930 ;; Inner template is applied even in a "body-only" export.
2931 (should
2932 (equal "Success!"
2933 (org-test-with-temp-text "* Headline"
2934 (org-export-as
2935 (org-export-create-backend
2936 :transcoders '((inner-template . (lambda (c i) "Success!"))
2937 (headline . (lambda (h c i) "Headline"))))
2938 nil nil 'body-only)))))
2940 (ert-deftest test-org-export/template ()
2941 "Test `template' translator specifications."
2942 (should
2943 (equal "Success!"
2944 (org-test-with-temp-text "* Headline"
2945 (org-export-as
2946 (org-export-create-backend
2947 :transcoders '((template . (lambda (contents info) "Success!"))
2948 (headline . (lambda (h c i) "Headline"))))))))
2949 ;; Template is not applied in a "body-only" export.
2950 (should-not
2951 (equal "Success!"
2952 (org-test-with-temp-text "* Headline"
2953 (org-export-as
2954 (org-export-create-backend
2955 :transcoders '((template . (lambda (contents info) "Success!"))
2956 (headline . (lambda (h c i) "Headline"))))
2957 nil nil 'body-only)))))
2961 ;;; Topology
2963 (ert-deftest test-org-export/get-next-element ()
2964 "Test `org-export-get-next-element' specifications."
2965 ;; Standard test.
2966 (should
2967 (equal "b"
2968 (org-test-with-parsed-data "* Headline\n*a* b"
2969 (org-export-get-next-element
2970 (org-element-map tree 'bold 'identity info t) info))))
2971 ;; Return nil when no previous element.
2972 (should-not
2973 (org-test-with-parsed-data "* Headline\na *b*"
2974 (org-export-get-next-element
2975 (org-element-map tree 'bold 'identity info t) info)))
2976 ;; Non-exportable elements are ignored.
2977 (should-not
2978 (let ((org-export-with-timestamps nil))
2979 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
2980 (org-export-get-next-element
2981 (org-element-map tree 'entity 'identity info t) info))))
2982 ;; Find next element in secondary strings.
2983 (should
2984 (eq 'verbatim
2985 (org-test-with-parsed-data "* a =verb="
2986 (org-element-type
2987 (org-export-get-next-element
2988 (org-element-map tree 'plain-text 'identity info t) info)))))
2989 (should
2990 (eq 'verbatim
2991 (org-test-with-parsed-data "* /italic/ =verb="
2992 (org-element-type
2993 (org-export-get-next-element
2994 (org-element-map tree 'italic 'identity info t) info)))))
2995 ;; Find next element in document keywords.
2996 (should
2997 (eq 'verbatim
2998 (org-test-with-parsed-data "#+TITLE: a =verb="
2999 (org-element-type
3000 (org-export-get-next-element
3001 (org-element-map
3002 (plist-get info :title) 'plain-text 'identity info t) info)))))
3003 ;; Find next element in parsed affiliated keywords.
3004 (should
3005 (eq 'verbatim
3006 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
3007 (org-element-type
3008 (org-export-get-next-element
3009 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3010 ;; With optional argument N, return a list containing all the
3011 ;; following elements.
3012 (should
3013 (equal
3014 '(bold code underline)
3015 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3016 (mapcar 'car
3017 (org-export-get-next-element
3018 (org-element-map tree 'italic 'identity info t) info t)))))
3019 ;; When N is a positive integer, return a list containing up to
3020 ;; N following elements.
3021 (should
3022 (equal
3023 '(bold code)
3024 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3025 (mapcar 'car
3026 (org-export-get-next-element
3027 (org-element-map tree 'italic 'identity info t) info 2))))))
3029 (ert-deftest test-org-export/get-previous-element ()
3030 "Test `org-export-get-previous-element' specifications."
3031 ;; Standard test.
3032 (should
3033 (equal "a "
3034 (org-test-with-parsed-data "* Headline\na *b*"
3035 (org-export-get-previous-element
3036 (org-element-map tree 'bold 'identity info t) info))))
3037 ;; Return nil when no previous element.
3038 (should-not
3039 (org-test-with-parsed-data "* Headline\n*a* b"
3040 (org-export-get-previous-element
3041 (org-element-map tree 'bold 'identity info t) info)))
3042 ;; Non-exportable elements are ignored.
3043 (should-not
3044 (let ((org-export-with-timestamps nil))
3045 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
3046 (org-export-get-previous-element
3047 (org-element-map tree 'entity 'identity info t) info))))
3048 ;; Find previous element in secondary strings.
3049 (should
3050 (eq 'verbatim
3051 (org-test-with-parsed-data "* =verb= a"
3052 (org-element-type
3053 (org-export-get-previous-element
3054 (org-element-map tree 'plain-text 'identity info t) info)))))
3055 (should
3056 (eq 'verbatim
3057 (org-test-with-parsed-data "* =verb= /italic/"
3058 (org-element-type
3059 (org-export-get-previous-element
3060 (org-element-map tree 'italic 'identity info t) info)))))
3061 ;; Find previous element in document keywords.
3062 (should
3063 (eq 'verbatim
3064 (org-test-with-parsed-data "#+TITLE: =verb= a"
3065 (org-element-type
3066 (org-export-get-previous-element
3067 (org-element-map
3068 (plist-get info :title) 'plain-text 'identity info t) info)))))
3069 ;; Find previous element in parsed affiliated keywords.
3070 (should
3071 (eq 'verbatim
3072 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
3073 (org-element-type
3074 (org-export-get-previous-element
3075 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3076 ;; With optional argument N, return a list containing up to
3077 ;; N previous elements.
3078 (should
3079 (equal '(underline italic bold)
3080 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3081 (mapcar 'car
3082 (org-export-get-previous-element
3083 (org-element-map tree 'code 'identity info t) info t)))))
3084 ;; When N is a positive integer, return a list containing up to
3085 ;; N previous elements.
3086 (should
3087 (equal '(italic bold)
3088 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3089 (mapcar 'car
3090 (org-export-get-previous-element
3091 (org-element-map tree 'code 'identity info t) info 2))))))
3094 (provide 'test-ox)
3095 ;;; test-org-export.el end here