ox: Title fallbacks to nil instead of file name
[org-mode/org-tableheadings.git] / testing / lisp / test-ox.el
blob9bde04ef738268d0f6d5263fd68ab556b356eceb
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 ;; Without TITLE keyword.
274 (should
275 (equal
277 (org-test-with-temp-text "Test"
278 (org-export-as
279 (org-export-create-backend
280 :transcoders
281 '((template . (lambda (text info)
282 (org-element-interpret-data
283 (plist-get info :title))))))))))
284 ;; With a blank TITLE keyword.
285 (should
286 (equal
288 (org-test-with-temp-text "#+TITLE:\nTest"
289 (org-export-as
290 (org-export-create-backend
291 :transcoders
292 '((template . (lambda (text info)
293 (org-element-interpret-data
294 (plist-get info :title))))))))))
295 ;; With a non-empty TITLE keyword.
296 (should
297 (equal
298 "Title"
299 (org-test-with-temp-text "#+TITLE: Title\nTest"
300 (org-export-as
301 (org-export-create-backend
302 :transcoders
303 '((template . (lambda (text info)
304 (org-element-interpret-data
305 (plist-get info :title))))))))))
306 ;; When exporting a subtree, its heading becomes the headline of the
307 ;; document...
308 (should
309 (equal
310 "Headline"
311 (org-test-with-temp-text "* Headline\nBody"
312 (org-export-as
313 (org-export-create-backend
314 :transcoders
315 '((template . (lambda (text info)
316 (org-element-interpret-data
317 (plist-get info :title))))))
318 'subtree))))
319 ;; ... unless there is an EXPORT_TITLE property at the root of the
320 ;; subtree.
321 (should
322 (equal
324 (org-test-with-temp-text
325 "* A\n :PROPERTIES:\n :EXPORT_TITLE: B\n :END:\nBody"
326 (org-export-as
327 (org-export-create-backend
328 :transcoders
329 '((template . (lambda (text info)
330 (org-element-interpret-data
331 (plist-get info :title))))))
332 'subtree)))))
334 (ert-deftest test-org-export/handle-options ()
335 "Test if export options have an impact on output."
336 ;; Test exclude tags for headlines and inlinetasks.
337 (should
338 (equal ""
339 (org-test-with-temp-text "* Head1 :noexp:"
340 (org-export-as (org-test-default-backend)
341 nil nil nil '(:exclude-tags ("noexp"))))))
342 ;; Test include tags for headlines and inlinetasks.
343 (should
344 (equal "* H2\n** Sub :exp:\n*** Sub Sub\n"
345 (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
346 (let ((org-tags-column 0))
347 (org-export-as (org-test-default-backend)
348 nil nil nil '(:select-tags ("exp")))))))
349 ;; If there is an include tag, ignore the section before the first
350 ;; headline, if any.
351 (should
352 (equal "* H1 :exp:\nBody\n"
353 (org-test-with-temp-text "First section\n* H1 :exp:\nBody"
354 (let ((org-tags-column 0))
355 (org-export-as (org-test-default-backend)
356 nil nil nil '(:select-tags ("exp")))))))
357 (should-not
358 (equal "* H1 :exp:\n"
359 (org-test-with-temp-text "* H1 :exp:\nBody"
360 (let ((org-tags-column 0))
361 (org-export-as (org-test-default-backend)
362 nil nil nil '(:select-tags ("exp")))))))
363 ;; Test mixing include tags and exclude tags.
364 (should
365 (string-match
366 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
367 (org-test-with-temp-text "
368 * Head1 :export:
369 ** Sub-Head1 :noexport:
370 ** Sub-Head2
371 * Head2 :noexport:
372 ** Sub-Head1 :export:"
373 (org-export-as (org-test-default-backend) nil nil nil
374 '(:select-tags ("export") :exclude-tags ("noexport"))))))
375 ;; Ignore tasks.
376 (should
377 (equal ""
378 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
379 (org-test-with-temp-text "* TODO Head1"
380 (org-export-as (org-test-default-backend)
381 nil nil nil '(:with-tasks nil))))))
382 (should
383 (equal "* TODO Head1\n"
384 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
385 (org-test-with-temp-text "* TODO Head1"
386 (org-export-as (org-test-default-backend)
387 nil nil nil '(:with-tasks t))))))
388 ;; Archived tree.
389 (should
390 (equal ""
391 (org-test-with-temp-text "* Head1 :archive:"
392 (let ((org-archive-tag "archive"))
393 (org-export-as (org-test-default-backend)
394 nil nil nil '(:with-archived-trees nil))))))
395 (should
396 (string-match
397 "\\* Head1[ \t]+:archive:"
398 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
399 (let ((org-archive-tag "archive"))
400 (org-export-as (org-test-default-backend) nil nil nil
401 '(:with-archived-trees headline))))))
402 (should
403 (string-match
404 "\\`\\* Head1[ \t]+:archive:\n\\'"
405 (org-test-with-temp-text "* Head1 :archive:"
406 (let ((org-archive-tag "archive"))
407 (org-export-as (org-test-default-backend)
408 nil nil nil '(:with-archived-trees t))))))
409 ;; Clocks.
410 (should
411 (string-match "CLOCK: \\[2012-04-29 .* 10:45\\]"
412 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
413 (org-export-as (org-test-default-backend)
414 nil nil nil '(:with-clocks t)))))
415 (should
416 (equal ""
417 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
418 (org-export-as (org-test-default-backend)
419 nil nil nil '(:with-clocks nil)))))
420 ;; Drawers.
421 (should
422 (equal ""
423 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
424 (org-export-as (org-test-default-backend)
425 nil nil nil '(:with-drawers nil)))))
426 (should
427 (equal ":TEST:\ncontents\n:END:\n"
428 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
429 (org-export-as (org-test-default-backend)
430 nil nil nil '(:with-drawers t)))))
431 (should
432 (equal ":FOO:\nkeep\n:END:\n"
433 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
434 (org-export-as (org-test-default-backend)
435 nil nil nil '(:with-drawers ("FOO"))))))
436 (should
437 (equal ":FOO:\nkeep\n:END:\n"
438 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
439 (org-export-as (org-test-default-backend)
440 nil nil nil '(:with-drawers (not "BAR"))))))
441 ;; Fixed-width.
442 (should
443 (equal ": A\n"
444 (org-test-with-temp-text ": A"
445 (org-export-as (org-test-default-backend) nil nil nil
446 '(:with-fixed-width t)))))
447 (should
448 (equal ""
449 (org-test-with-temp-text ": A"
450 (org-export-as (org-test-default-backend) nil nil nil
451 '(:with-fixed-width nil)))))
452 ;; Footnotes.
453 (should
454 (equal "Footnote?"
455 (let ((org-footnote-section nil))
456 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
457 (org-trim (org-export-as (org-test-default-backend)
458 nil nil nil '(:with-footnotes nil)))))))
459 (should
460 (equal "Footnote?[fn:1]\n\n[fn:1] Def"
461 (let ((org-footnote-section nil))
462 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
463 (org-trim (org-export-as (org-test-default-backend)
464 nil nil nil '(:with-footnotes t)))))))
465 ;; Inlinetasks.
466 (when (featurep 'org-inlinetask)
467 (should
468 (equal
470 (let ((org-inlinetask-min-level 15))
471 (org-test-with-temp-text "*************** Task"
472 (org-export-as (org-test-default-backend)
473 nil nil nil '(:with-inlinetasks nil))))))
474 (should
475 (equal
477 (let ((org-inlinetask-min-level 15))
478 (org-test-with-temp-text
479 "*************** Task\nContents\n*************** END"
480 (org-export-as (org-test-default-backend)
481 nil nil nil '(:with-inlinetasks nil)))))))
482 ;; Plannings.
483 (should
484 (string-match
485 "CLOSED: \\[2012-04-29 .* 10:45\\]"
486 (let ((org-closed-string "CLOSED:"))
487 (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
488 (org-export-as (org-test-default-backend)
489 nil nil nil '(:with-planning t))))))
490 (should
491 (equal ""
492 (let ((org-closed-string "CLOSED:"))
493 (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
494 (org-export-as (org-test-default-backend)
495 nil nil nil '(:with-planning nil))))))
496 ;; Property Drawers.
497 (should
498 (equal "* H1\n"
499 (org-test-with-temp-text
500 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
501 (org-export-as (org-test-default-backend)
502 nil nil nil '(:with-properties nil)))))
503 (should
504 (equal "* H1\n:PROPERTIES:\n:PROP: value\n:END:\n"
505 (org-test-with-temp-text
506 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
507 (org-export-as (org-test-default-backend)
508 nil nil nil '(:with-properties t)))))
509 (should
510 (equal "* H1\n:PROPERTIES:\n:B: 2\n:END:\n"
511 (org-test-with-temp-text
512 "* H1\n :PROPERTIES:\n :A: 1\n :B: 2\n:END:"
513 (org-export-as (org-test-default-backend)
514 nil nil nil '(:with-properties ("B"))))))
515 ;; Statistics cookies.
516 (should
517 (equal ""
518 (org-test-with-temp-text "[0/0]"
519 (org-export-as (org-test-default-backend)
520 nil nil nil '(:with-statistics-cookies nil)))))
521 ;; Tables.
522 (should
523 (equal "| A |\n"
524 (org-test-with-temp-text "| A |"
525 (org-export-as (org-test-default-backend) nil nil nil
526 '(:with-tables t)))))
527 (should
528 (equal ""
529 (org-test-with-temp-text "| A |"
530 (org-export-as (org-test-default-backend) nil nil nil
531 '(:with-tables nil))))))
533 (ert-deftest test-org-export/with-timestamps ()
534 "Test `org-export-with-timestamps' specifications."
535 ;; t value.
536 (should
537 (string-match
538 "\\[2012-04-29 .*? 10:45\\]<2012-04-29 .*? 10:45>"
539 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
540 (org-export-as (org-test-default-backend)
541 nil nil nil '(:with-timestamps t)))))
542 ;; nil value.
543 (should
544 (equal
546 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
547 (org-export-as (org-test-default-backend)
548 nil nil nil '(:with-timestamps nil)))))
549 ;; `active' value.
550 (should
551 (string-match
552 "<2012-03-29 .*?>\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
553 (org-test-with-temp-text
554 "<2012-03-29 Thu>[2012-03-29 Thu]
556 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
557 (org-export-as (org-test-default-backend)
558 nil nil nil '(:with-timestamps active)))))
559 ;; `inactive' value.
560 (should
561 (string-match
562 "\\[2012-03-29 .*?\\]\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
563 (org-test-with-temp-text
564 "<2012-03-29 Thu>[2012-03-29 Thu]
566 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
567 (org-export-as (org-test-default-backend)
568 nil nil nil '(:with-timestamps inactive))))))
570 (ert-deftest test-org-export/comment-tree ()
571 "Test if export process ignores commented trees."
572 (should
573 (equal ""
574 (org-test-with-temp-text "* COMMENT Head1"
575 (org-export-as (org-test-default-backend))))))
577 (ert-deftest test-org-export/uninterpreted ()
578 "Test handling of uninterpreted elements."
579 ;; Entities.
580 (should
581 (equal "dummy\n"
582 (org-test-with-temp-text "\\alpha"
583 (org-export-as
584 (org-export-create-backend
585 :transcoders '((entity . (lambda (e c i) "dummy"))
586 (paragraph . (lambda (p c i) c))
587 (section . (lambda (s c i) c))))
588 nil nil nil '(:with-entities t)))))
589 (should
590 (equal "\\alpha\n"
591 (org-test-with-temp-text "\\alpha"
592 (org-export-as
593 (org-export-create-backend
594 :transcoders '((entity . (lambda (e c i) "dummy"))
595 (paragraph . (lambda (p c i) c))
596 (section . (lambda (s c i) c))))
597 nil nil nil '(:with-entities nil)))))
598 ;; Emphasis.
599 (should
600 (equal "dummy\n"
601 (org-test-with-temp-text "*bold*"
602 (org-export-as
603 (org-export-create-backend
604 :transcoders '((bold . (lambda (b c i) "dummy"))
605 (paragraph . (lambda (p c i) c))
606 (section . (lambda (s c i) c))))
607 nil nil nil '(:with-emphasize t)))))
608 (should
609 (equal "*bold*\n"
610 (org-test-with-temp-text "*bold*"
611 (org-export-as
612 (org-export-create-backend
613 :transcoders '((bold . (lambda (b c i) "dummy"))
614 (paragraph . (lambda (p c i) c))
615 (section . (lambda (s c i) c))))
616 nil nil nil '(:with-emphasize nil)))))
617 ;; LaTeX environment.
618 (should
619 (equal "dummy\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 t)))))
626 (should
627 (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
628 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
629 (org-export-as
630 (org-export-create-backend
631 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
632 (section . (lambda (s c i) c))))
633 nil nil nil '(:with-latex verbatim)))))
634 ;; LaTeX fragment.
635 (should
636 (equal "dummy\n"
637 (org-test-with-temp-text "$1$"
638 (org-export-as
639 (org-export-create-backend
640 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
641 (paragraph . (lambda (p c i) c))
642 (section . (lambda (s c i) c))))
643 nil nil nil '(:with-latex t)))))
644 (should
645 (equal "$1$\n"
646 (org-test-with-temp-text "$1$"
647 (org-export-as
648 (org-export-create-backend
649 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
650 (paragraph . (lambda (p c i) c))
651 (section . (lambda (s c i) c))))
652 nil nil nil '(:with-latex verbatim)))))
653 ;; Sub/superscript.
654 (should
655 (equal "adummy\n"
656 (org-test-with-temp-text "a_b"
657 (org-export-as
658 (org-export-create-backend
659 :transcoders '((subscript . (lambda (s c i) "dummy"))
660 (paragraph . (lambda (p c i) c))
661 (section . (lambda (s c i) c))))
662 nil nil nil '(:with-sub-superscript t)))))
663 (should
664 (equal "a_b\n"
665 (org-test-with-temp-text "a_b"
666 (org-export-as
667 (org-export-create-backend
668 :transcoders '((subscript . (lambda (s c i) "dummy"))
669 (paragraph . (lambda (p c i) c))
670 (section . (lambda (s c i) c))))
671 nil nil nil '(:with-sub-superscript nil)))))
672 (should
673 (equal "a_b\n"
674 (org-test-with-temp-text "a_b"
675 (org-export-as
676 (org-export-create-backend
677 :transcoders '((subscript . (lambda (s c i) "dummy"))
678 (paragraph . (lambda (p c i) c))
679 (section . (lambda (s c i) c))))
680 nil nil nil '(:with-sub-superscript {})))))
681 (should
682 (equal "adummy\n"
683 (org-test-with-temp-text "a_{b}"
684 (org-export-as
685 (org-export-create-backend
686 :transcoders '((subscript . (lambda (s c i) "dummy"))
687 (paragraph . (lambda (p c i) c))
688 (section . (lambda (s c i) c))))
689 nil nil nil '(:with-sub-superscript {})))))
690 ;; Also handle uninterpreted objects in title.
691 (should
692 (equal "a_b"
693 (org-test-with-temp-text "#+TITLE: a_b"
694 (org-export-as
695 (org-export-create-backend
696 :transcoders
697 '((subscript . (lambda (s c i) "dummy"))
698 (template . (lambda (c i) (org-export-data
699 (plist-get i :title) i)))
700 (section . (lambda (s c i) c))))
701 nil nil nil '(:with-sub-superscript nil)))))
702 ;; Special case: multiples uninterpreted objects in a row.
703 (should
704 (equal "a_b_c_d\n"
705 (org-test-with-temp-text "a_b_c_d"
706 (org-export-as
707 (org-export-create-backend
708 :transcoders '((subscript . (lambda (s c i) "dummy"))
709 (paragraph . (lambda (p c i) c))
710 (section . (lambda (s c i) c))))
711 nil nil nil '(:with-sub-superscript {}))))))
713 (ert-deftest test-org-export/export-scope ()
714 "Test all export scopes."
715 (org-test-with-temp-text "
716 * Head1
717 ** Head2
718 text
719 *** Head3"
720 ;; Subtree.
721 (forward-line 3)
722 (should (equal (org-export-as (org-test-default-backend) 'subtree)
723 "text\n*** Head3\n"))
724 ;; Visible.
725 (goto-char (point-min))
726 (forward-line)
727 (org-cycle)
728 (should (equal (org-export-as (org-test-default-backend) nil 'visible)
729 "* Head1\n"))
730 ;; Region.
731 (goto-char (point-min))
732 (forward-line 3)
733 (transient-mark-mode 1)
734 (push-mark (point) t t)
735 (goto-char (point-at-eol))
736 (should (equal (org-export-as (org-test-default-backend)) "text\n")))
737 ;; Subtree with a code block calling another block outside.
738 (should
739 (equal ": 3\n"
740 (org-test-with-temp-text "
741 * Head1
742 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
743 <<test>>
744 #+END_SRC
745 * Head2
746 #+NAME: test
747 #+BEGIN_SRC emacs-lisp
748 \(+ 1 2)
749 #+END_SRC"
750 (forward-line 1)
751 (org-export-as (org-test-default-backend) 'subtree))))
752 ;; Body only.
753 (let ((backend (org-test-default-backend)))
754 (setf (org-export-backend-transcoders backend)
755 (cons '(template . (lambda (body i)
756 (format "BEGIN\n%sEND" body)))
757 (org-export-backend-transcoders backend)))
758 (org-test-with-temp-text "Text"
759 (should (equal (org-export-as backend nil nil 'body-only)
760 "Text\n"))
761 (should (equal (org-export-as backend) "BEGIN\nText\nEND")))))
763 (ert-deftest test-org-export/output-file-name ()
764 "Test `org-export-output-file-name' specifications."
765 ;; Export from a file: name is built from original file name.
766 (should
767 (org-test-with-temp-text-in-file "Test"
768 (equal (concat (file-name-as-directory ".")
769 (file-name-nondirectory
770 (file-name-sans-extension (buffer-file-name))))
771 (file-name-sans-extension (org-export-output-file-name ".ext")))))
772 ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
773 (should
774 (org-test-with-temp-text-in-file
775 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
776 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
777 ;; From a buffer not associated to a file, too.
778 (should
779 (org-test-with-temp-text
780 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
781 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
782 ;; When provided name is absolute, preserve it.
783 (should
784 (org-test-with-temp-text
785 (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
786 (expand-file-name "test"))
787 (file-name-absolute-p (org-export-output-file-name ".ext" t))))
788 ;; When PUB-DIR argument is provided, use it.
789 (should
790 (org-test-with-temp-text-in-file "Test"
791 (equal (file-name-directory
792 (org-export-output-file-name ".ext" nil "dir/"))
793 "dir/")))
794 ;; When returned name would overwrite original file, add EXTENSION
795 ;; another time.
796 (should
797 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
798 (equal (org-export-output-file-name ".org") "./normal.org.org"))))
800 (ert-deftest test-org-export/expand-include ()
801 "Test file inclusion in an Org buffer."
802 ;; Error when file isn't specified.
803 (should-error
804 (org-test-with-temp-text "#+INCLUDE: dummy.org"
805 (org-export-expand-include-keyword)))
806 ;; Full insertion with recursive inclusion.
807 (org-test-with-temp-text
808 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
809 (org-export-expand-include-keyword)
810 (should (equal (buffer-string)
811 "Small Org file with an include keyword.
813 #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
815 Success!
817 * Heading
818 body\n")))
819 ;; Localized insertion.
820 (org-test-with-temp-text
821 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
822 org-test-dir)
823 (org-export-expand-include-keyword)
824 (should (equal (buffer-string)
825 "Small Org file with an include keyword.\n")))
826 ;; Insertion with constraints on headlines level.
827 (should
828 (equal
829 "* Top heading\n** Heading\nbody\n"
830 (org-test-with-temp-text
831 (format
832 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
833 org-test-dir)
834 (org-export-expand-include-keyword)
835 (buffer-string))))
836 (should
837 (equal
838 "* Top heading\n* Heading\nbody\n"
839 (org-test-with-temp-text
840 (format
841 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\" :minlevel 1"
842 org-test-dir)
843 (org-export-expand-include-keyword)
844 (buffer-string))))
845 ;; Inclusion within an example block.
846 (should
847 (equal
848 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
849 (org-test-with-temp-text
850 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
851 org-test-dir)
852 (org-export-expand-include-keyword)
853 (buffer-string))))
854 ;; Inclusion within a src-block.
855 (should
856 (equal
857 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
858 (org-test-with-temp-text
859 (format
860 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
861 org-test-dir)
862 (org-export-expand-include-keyword)
863 (buffer-string))))
864 ;; Inclusion within an html export-block.
865 (should
866 (equal
867 "#+BEGIN_HTML\n<p>HTML!</p>\n#+END_HTML\n"
868 (org-test-with-temp-text
869 (format
870 "#+INCLUDE: \"%s/examples/include.html\" HTML"
871 org-test-dir)
872 (org-export-expand-include-keyword)
873 (buffer-string))))
874 ;; Inclusion within an center paragraph
875 (should
876 (equal
877 "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
878 (org-test-with-temp-text
879 (format
880 "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
881 org-test-dir)
882 (org-export-expand-include-keyword)
883 (buffer-string))))
884 ;; Footnotes labels are local to each included file.
885 (should
886 (= 6
887 (length
888 (delete-dups
889 (let ((contents "
890 Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote].
891 \[fn:1] Footnote 1
892 \[fn:test] Footnote \"test\""))
893 (org-test-with-temp-text-in-file contents
894 (let ((file1 (buffer-file-name)))
895 (org-test-with-temp-text-in-file contents
896 (let ((file2 (buffer-file-name)))
897 (org-test-with-temp-text
898 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
899 file1 file2)
900 (org-export-expand-include-keyword)
901 (org-element-map (org-element-parse-buffer)
902 'footnote-reference
903 (lambda (ref)
904 (org-element-property :label ref)))))))))))))
905 ;; Footnotes labels are not local to each include keyword.
906 (should
907 (= 3
908 (length
909 (delete-dups
910 (let ((contents "
911 Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote].
912 \[fn:1] Footnote 1
913 \[fn:test] Footnote \"test\""))
914 (org-test-with-temp-text-in-file contents
915 (let ((file (buffer-file-name)))
916 (org-test-with-temp-text
917 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
918 (org-export-expand-include-keyword)
919 (org-element-map (org-element-parse-buffer)
920 'footnote-reference
921 (lambda (ref) (org-element-property :label ref))))))))))))
923 (ert-deftest test-org-export/expand-macro ()
924 "Test macro expansion in an Org buffer."
925 ;; Standard macro expansion.
926 (should
927 (equal "#+MACRO: macro1 value\nvalue\n"
928 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
929 (org-export-as (org-test-default-backend)))))
930 ;; Expand specific macros.
931 (should
932 (equal "me 2012-03-29 me@here Title\n"
933 (org-test-with-temp-text
935 #+TITLE: Title
936 #+DATE: 2012-03-29
937 #+AUTHOR: me
938 #+EMAIL: me@here
939 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
940 (let ((output (org-export-as (org-test-default-backend))))
941 (substring output (string-match ".*\n\\'" output))))))
942 ;; Expand specific macros when property contained a regular macro
943 ;; already.
944 (should
945 (equal "value\n"
946 (org-test-with-temp-text "
947 #+MACRO: macro1 value
948 #+TITLE: {{{macro1}}}
949 {{{title}}}"
950 (let ((output (org-export-as (org-test-default-backend))))
951 (substring output (string-match ".*\n\\'" output))))))
952 ;; Expand macros with templates in included files.
953 (should
954 (equal "success\n"
955 (org-test-with-temp-text
956 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
957 {{{included-macro}}}" org-test-dir)
958 (let ((output (org-export-as (org-test-default-backend))))
959 (substring output (string-match ".*\n\\'" output)))))))
961 (ert-deftest test-org-export/user-ignore-list ()
962 "Test if `:ignore-list' accepts user input."
963 (let ((backend (org-test-default-backend)))
964 (setf (org-export-backend-transcoders backend)
965 (cons '(template . (lambda (body i)
966 (format "BEGIN\n%sEND" body)))
967 (org-export-backend-transcoders backend)))
968 (org-test-with-temp-text "Text"
969 (should (equal (org-export-as backend nil nil 'body-only)
970 "Text\n"))
971 (should (equal (org-export-as backend) "BEGIN\nText\nEND"))))
972 (should
973 (equal
974 "* Head1\n"
975 (let ((org-export-filter-parse-tree-functions
976 '((lambda (data backend info)
977 ;; Ignore headlines with the word "note" in their title.
978 (org-element-map data 'headline
979 (lambda (headline)
980 (when (string-match "\\<note\\>"
981 (org-element-property :raw-value
982 headline))
983 (org-export-ignore-element headline info)))
984 info)
985 data))))
986 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
987 (org-export-as (org-test-default-backend)))))))
989 (ert-deftest test-org-export/before-processing-hook ()
990 "Test `org-export-before-processing-hook'."
991 (should
992 (equal
993 "#+MACRO: mac val\nTest\n"
994 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
995 (let ((org-export-before-processing-hook
996 '((lambda (backend)
997 (while (re-search-forward "{{{" nil t)
998 (let ((object (org-element-context)))
999 (when (eq (org-element-type object) 'macro)
1000 (delete-region
1001 (org-element-property :begin object)
1002 (org-element-property :end object)))))))))
1003 (org-export-as (org-test-default-backend)))))))
1005 (ert-deftest test-org-export/before-parsing-hook ()
1006 "Test `org-export-before-parsing-hook'."
1007 (should
1008 (equal "Body 1\nBody 2\n"
1009 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
1010 (let ((org-export-before-parsing-hook
1011 '((lambda (backend)
1012 (goto-char (point-min))
1013 (while (re-search-forward org-outline-regexp-bol nil t)
1014 (delete-region
1015 (point-at-bol) (progn (forward-line) (point))))))))
1016 (org-export-as (org-test-default-backend)))))))
1020 ;;; Affiliated Keywords
1022 (ert-deftest test-org-export/read-attribute ()
1023 "Test `org-export-read-attribute' specifications."
1024 ;; Standard test.
1025 (should
1026 (equal
1027 (org-export-read-attribute
1028 :attr_html
1029 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
1030 (org-element-at-point)))
1031 '(:a "1" :b "2")))
1032 ;; Return nil on empty attribute.
1033 (should-not
1034 (org-export-read-attribute
1035 :attr_html
1036 (org-test-with-temp-text "Paragraph" (org-element-at-point))))
1037 ;; Return nil on "nil" string.
1038 (should
1039 (equal '(:a nil :b nil)
1040 (org-export-read-attribute
1041 :attr_html
1042 (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
1043 (org-element-at-point)))))
1044 ;; Return nil on empty string.
1045 (should
1046 (equal '(:a nil :b nil)
1047 (org-export-read-attribute
1048 :attr_html
1049 (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
1050 (org-element-at-point)))))
1051 ;; Return empty string when value is "".
1052 (should
1053 (equal '(:a "")
1054 (org-export-read-attribute
1055 :attr_html
1056 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
1057 (org-element-at-point)))))
1058 ;; Return \"\" when value is """".
1059 (should
1060 (equal '(:a "\"\"")
1061 (org-export-read-attribute
1062 :attr_html
1063 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
1064 (org-element-at-point)))))
1065 ;; Ignore text before first property.
1066 (should-not
1067 (member "ignore"
1068 (org-export-read-attribute
1069 :attr_html
1070 (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
1071 (org-element-at-point))))))
1073 (ert-deftest test-org-export/get-caption ()
1074 "Test `org-export-get-caption' specifications."
1075 ;; Without optional argument, return long caption
1076 (should
1077 (equal
1078 '("l")
1079 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1080 (org-export-get-caption (org-element-at-point)))))
1081 ;; With optional argument, return short caption.
1082 (should
1083 (equal
1084 '("s")
1085 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1086 (org-export-get-caption (org-element-at-point) t))))
1087 ;; Multiple lines are separated by white spaces.
1088 (should
1089 (equal
1090 '("a" " " "b")
1091 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
1092 (org-export-get-caption (org-element-at-point))))))
1096 ;;; Back-End Tools
1098 (ert-deftest test-org-export/define-backend ()
1099 "Test back-end definition and accessors."
1100 ;; Translate table.
1101 (should
1102 (equal '((headline . my-headline-test))
1103 (let (org-export--registered-backends)
1104 (org-export-define-backend 'test '((headline . my-headline-test)))
1105 (org-export-get-all-transcoders 'test))))
1106 ;; Filters.
1107 (should
1108 (equal '((:filter-headline . my-filter))
1109 (let (org-export--registered-backends)
1110 (org-export-define-backend 'test
1111 '((headline . my-headline-test))
1112 :filters-alist '((:filter-headline . my-filter)))
1113 (org-export-backend-filters (org-export-get-backend 'test)))))
1114 ;; Options.
1115 (should
1116 (equal '((:prop value))
1117 (let (org-export--registered-backends)
1118 (org-export-define-backend 'test
1119 '((headline . my-headline-test))
1120 :options-alist '((:prop value)))
1121 (org-export-backend-options (org-export-get-backend 'test)))))
1122 ;; Menu.
1123 (should
1124 (equal '(?k "Test Export" test)
1125 (let (org-export--registered-backends)
1126 (org-export-define-backend 'test
1127 '((headline . my-headline-test))
1128 :menu-entry '(?k "Test Export" test))
1129 (org-export-backend-menu (org-export-get-backend 'test))))))
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 ;;; Special blocks
2149 (ert-deftest test-org-export/raw-special-block-p ()
2150 "Test `org-export-raw-special-block-p' specifications."
2151 ;; Standard test.
2152 (should
2153 (org-test-with-parsed-data "#+BEGIN_FOO\nContents\n#+END_FOO"
2154 (let ((info (org-combine-plists
2155 info (list :back-end
2156 (org-export-create-backend :blocks '("FOO"))))))
2157 (org-export-raw-special-block-p
2158 (org-element-map tree 'special-block #'identity info t) info))))
2159 (should-not
2160 (org-test-with-parsed-data "#+BEGIN_BAR\nContents\n#+END_BAR"
2161 (let ((info (org-combine-plists
2162 info (list :back-end
2163 (org-export-create-backend :blocks '("FOO"))))))
2164 (org-export-raw-special-block-p
2165 (org-element-map tree 'special-block #'identity info t) info))))
2166 ;; Check is not case-sensitive.
2167 (should
2168 (org-test-with-parsed-data "#+begin_foo\nContents\n#+end_foo"
2169 (let ((info (org-combine-plists
2170 info (list :back-end
2171 (org-export-create-backend :blocks '("FOO"))))))
2172 (org-export-raw-special-block-p
2173 (org-element-map tree 'special-block #'identity info t) info))))
2174 ;; Test inheritance.
2175 (should
2176 (org-test-with-parsed-data "#+BEGIN_FOO\nContents\n#+END_FOO"
2177 (let* ((org-export--registered-backends
2178 (list (org-export-create-backend :name 'b1 :blocks '("FOO"))))
2179 (info (org-combine-plists
2180 info (list :back-end
2181 (org-export-create-backend :parent 'b1
2182 :blocks '("BAR"))))))
2183 (org-export-raw-special-block-p
2184 (org-element-map tree 'special-block #'identity info t) info))))
2185 (should-not
2186 (org-test-with-parsed-data "#+BEGIN_BAZ\nContents\n#+END_BAZ"
2187 (let* ((org-export--registered-backends
2188 (list (org-export-create-backend :name 'b1 :blocks '("FOO"))))
2189 (info (org-combine-plists
2190 info (list :back-end
2191 (org-export-create-backend :parent 'b1
2192 :blocks '("BAR"))))))
2193 (org-export-raw-special-block-p
2194 (org-element-map tree 'special-block #'identity info t) info))))
2195 ;; With optional argument, ignore inheritance.
2196 (should-not
2197 (org-test-with-parsed-data "#+BEGIN_FOO\nContents\n#+END_FOO"
2198 (let* ((org-export--registered-backends
2199 (list (org-export-create-backend :name 'b1 :blocks '("FOO"))))
2200 (info (org-combine-plists
2201 info (list :back-end
2202 (org-export-create-backend :parent 'b1
2203 :blocks '("BAR"))))))
2204 (org-export-raw-special-block-p
2205 (org-element-map tree 'special-block #'identity info t) info t)))))
2209 ;;; Src-block and example-block
2211 (ert-deftest test-org-export/unravel-code ()
2212 "Test `org-export-unravel-code' function."
2213 ;; Code without reference.
2214 (should
2215 (equal '("(+ 1 1)\n")
2216 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2217 (org-export-unravel-code (org-element-at-point)))))
2218 ;; Code with reference.
2219 (should
2220 (equal '("(+ 1 1)\n" (1 . "test"))
2221 (org-test-with-temp-text
2222 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
2223 (let ((org-coderef-label-format "(ref:%s)"))
2224 (org-export-unravel-code (org-element-at-point))))))
2225 ;; Code with user-defined reference.
2226 (should
2227 (equal
2228 '("(+ 1 1)\n" (1 . "test"))
2229 (org-test-with-temp-text
2230 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
2231 (let ((org-coderef-label-format "(ref:%s)"))
2232 (org-export-unravel-code (org-element-at-point))))))
2233 ;; Code references keys are relative to the current block.
2234 (should
2235 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
2236 (org-test-with-temp-text "
2237 #+BEGIN_EXAMPLE -n
2238 \(+ 1 1)
2239 #+END_EXAMPLE
2240 #+BEGIN_EXAMPLE +n
2241 \(+ 2 2)
2242 \(+ 3 3) (ref:one)
2243 #+END_EXAMPLE"
2244 (goto-line 5)
2245 (let ((org-coderef-label-format "(ref:%s)"))
2246 (org-export-unravel-code (org-element-at-point)))))))
2248 (ert-deftest test-org-export/format-code-default ()
2249 "Test `org-export-format-code-default' specifications."
2250 ;; Return the empty string when code is empty.
2251 (should
2252 (equal ""
2253 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
2254 (org-export-format-code-default
2255 (org-element-map tree 'src-block 'identity info t) info))))
2256 ;; Number lines, two whitespace characters before the actual loc.
2257 (should
2258 (equal "1 a\n2 b\n"
2259 (org-test-with-parsed-data
2260 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
2261 (org-export-format-code-default
2262 (org-element-map tree 'src-block 'identity info t) info))))
2263 ;; Put references 6 whitespace characters after the widest line,
2264 ;; wrapped within parenthesis.
2265 (should
2266 (equal "123 (a)\n1 (b)\n"
2267 (let ((org-coderef-label-format "(ref:%s)"))
2268 (org-test-with-parsed-data
2269 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
2270 (org-export-format-code-default
2271 (org-element-map tree 'src-block 'identity info t) info))))))
2275 ;;; Smart Quotes
2277 (ert-deftest test-org-export/activate-smart-quotes ()
2278 "Test `org-export-activate-smart-quotes' specifications."
2279 ;; Opening double quotes: standard test.
2280 (should
2281 (equal
2282 '("some &ldquo;paragraph")
2283 (let ((org-export-default-language "en"))
2284 (org-test-with-parsed-data "some \"paragraph"
2285 (org-element-map tree 'plain-text
2286 (lambda (s) (org-export-activate-smart-quotes s :html info))
2287 info)))))
2288 ;; Opening quotes: at the beginning of a paragraph.
2289 (should
2290 (equal
2291 '("&ldquo;begin")
2292 (let ((org-export-default-language "en"))
2293 (org-test-with-parsed-data "\"begin"
2294 (org-element-map tree 'plain-text
2295 (lambda (s) (org-export-activate-smart-quotes s :html info))
2296 info)))))
2297 ;; Opening quotes: after an object.
2298 (should
2299 (equal
2300 '("&ldquo;begin")
2301 (let ((org-export-default-language "en"))
2302 (org-test-with-parsed-data "=verb= \"begin"
2303 (org-element-map tree 'plain-text
2304 (lambda (s) (org-export-activate-smart-quotes s :html info))
2305 info)))))
2306 ;; Closing quotes: standard test.
2307 (should
2308 (equal
2309 '("some&rdquo; paragraph")
2310 (let ((org-export-default-language "en"))
2311 (org-test-with-parsed-data "some\" paragraph"
2312 (org-element-map tree 'plain-text
2313 (lambda (s) (org-export-activate-smart-quotes s :html info))
2314 info)))))
2315 ;; Closing quotes: at the end of a paragraph.
2316 (should
2317 (equal
2318 '("end&rdquo;")
2319 (let ((org-export-default-language "en"))
2320 (org-test-with-parsed-data "end\""
2321 (org-element-map tree 'plain-text
2322 (lambda (s) (org-export-activate-smart-quotes s :html info))
2323 info)))))
2324 ;; Apostrophe: standard test.
2325 (should
2326 (equal
2327 '("It shouldn&rsquo;t fail")
2328 (let ((org-export-default-language "en"))
2329 (org-test-with-parsed-data "It shouldn't fail"
2330 (org-element-map tree 'plain-text
2331 (lambda (s) (org-export-activate-smart-quotes s :html info))
2332 info)))))
2333 ;; Apostrophe: before an object.
2334 (should
2335 (equal
2336 '("a&rsquo;")
2337 (let ((org-export-default-language "en"))
2338 (org-test-with-parsed-data "a'=b="
2339 (org-element-map tree 'plain-text
2340 (lambda (s) (org-export-activate-smart-quotes s :html info))
2341 info)))))
2342 ;; Apostrophe: after an object.
2343 (should
2344 (equal
2345 '("&rsquo;s")
2346 (let ((org-export-default-language "en"))
2347 (org-test-with-parsed-data "=code='s"
2348 (org-element-map tree 'plain-text
2349 (lambda (s) (org-export-activate-smart-quotes s :html info))
2350 info)))))
2351 ;; Special case: isolated quotes.
2352 (should
2353 (equal '("&ldquo;" "&rdquo;")
2354 (let ((org-export-default-language "en"))
2355 (org-test-with-parsed-data "\"$x$\""
2356 (org-element-map tree 'plain-text
2357 (lambda (s) (org-export-activate-smart-quotes s :html info))
2358 info)))))
2359 ;; Smart quotes in secondary strings.
2360 (should
2361 (equal '("&ldquo;" "&rdquo;")
2362 (let ((org-export-default-language "en"))
2363 (org-test-with-parsed-data "* \"$x$\""
2364 (org-element-map tree 'plain-text
2365 (lambda (s) (org-export-activate-smart-quotes s :html info))
2366 info)))))
2367 ;; Smart quotes in document keywords.
2368 (should
2369 (equal '("&ldquo;" "&rdquo;")
2370 (let ((org-export-default-language "en"))
2371 (org-test-with-parsed-data "#+TITLE: \"$x$\""
2372 (org-element-map (plist-get info :title) 'plain-text
2373 (lambda (s) (org-export-activate-smart-quotes s :html info))
2374 info)))))
2375 ;; Smart quotes in parsed affiliated keywords.
2376 (should
2377 (equal '("&ldquo;" "&rdquo;" "Paragraph")
2378 (let ((org-export-default-language "en"))
2379 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
2380 (org-element-map tree 'plain-text
2381 (lambda (s) (org-export-activate-smart-quotes s :html info))
2382 info nil nil t))))))
2386 ;;; Tables
2388 (ert-deftest test-org-export/special-column ()
2389 "Test if the table's special column is properly recognized."
2390 ;; 1. First column is special if it contains only a special marking
2391 ;; characters or empty cells.
2392 (org-test-with-temp-text "
2393 | ! | 1 |
2394 | | 2 |"
2395 (should
2396 (org-export-table-has-special-column-p
2397 (org-element-map
2398 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2399 ;; 2. If the column contains anything else, it isn't special.
2400 (org-test-with-temp-text "
2401 | ! | 1 |
2402 | b | 2 |"
2403 (should-not
2404 (org-export-table-has-special-column-p
2405 (org-element-map
2406 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2407 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
2408 ;; and "!".
2409 (org-test-with-temp-text "
2410 | # | 1 |
2411 | ^ | 2 |
2412 | * | 3 |
2413 | _ | 4 |
2414 | / | 5 |
2415 | $ | 6 |
2416 | ! | 7 |"
2417 (should
2418 (org-export-table-has-special-column-p
2419 (org-element-map
2420 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2421 ;; 4. A first column with only empty cells isn't considered as
2422 ;; special.
2423 (org-test-with-temp-text "
2424 | | 1 |
2425 | | 2 |"
2426 (should-not
2427 (org-export-table-has-special-column-p
2428 (org-element-map
2429 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
2431 (ert-deftest test-org-export/table-row-is-special-p ()
2432 "Test `org-export-table-row-is-special-p' specifications."
2433 ;; 1. A row is special if it has a special marking character in the
2434 ;; special column.
2435 (org-test-with-parsed-data "| ! | 1 |"
2436 (should
2437 (org-export-table-row-is-special-p
2438 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2439 ;; 2. A row is special when its first field is "/"
2440 (org-test-with-parsed-data "
2441 | / | 1 |
2442 | a | b |"
2443 (should
2444 (org-export-table-row-is-special-p
2445 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2446 ;; 3. A row only containing alignment cookies is also considered as
2447 ;; special.
2448 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
2449 (should
2450 (org-export-table-row-is-special-p
2451 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2452 ;; 4. Everything else isn't considered as special.
2453 (org-test-with-parsed-data "| \alpha | | c |"
2454 (should-not
2455 (org-export-table-row-is-special-p
2456 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2457 ;; 5. Table's rules are never considered as special rows.
2458 (org-test-with-parsed-data "|---+---|"
2459 (should-not
2460 (org-export-table-row-is-special-p
2461 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
2463 (ert-deftest test-org-export/has-header-p ()
2464 "Test `org-export-table-has-header-p' specifications."
2465 ;; 1. With an header.
2466 (org-test-with-parsed-data "
2467 | a | b |
2468 |---+---|
2469 | c | d |"
2470 (should
2471 (org-export-table-has-header-p
2472 (org-element-map tree 'table 'identity info 'first-match)
2473 info)))
2474 ;; 2. Without an header.
2475 (org-test-with-parsed-data "
2476 | a | b |
2477 | c | d |"
2478 (should-not
2479 (org-export-table-has-header-p
2480 (org-element-map tree 'table 'identity info 'first-match)
2481 info)))
2482 ;; 3. Don't get fooled with starting and ending rules.
2483 (org-test-with-parsed-data "
2484 |---+---|
2485 | a | b |
2486 | c | d |
2487 |---+---|"
2488 (should-not
2489 (org-export-table-has-header-p
2490 (org-element-map tree 'table 'identity info 'first-match)
2491 info))))
2493 (ert-deftest test-org-export/table-row-group ()
2494 "Test `org-export-table-row-group' specifications."
2495 ;; 1. A rule creates a new group.
2496 (should
2497 (equal '(1 rule 2)
2498 (org-test-with-parsed-data "
2499 | a | b |
2500 |---+---|
2501 | 1 | 2 |"
2502 (org-element-map tree 'table-row
2503 (lambda (row)
2504 (if (eq (org-element-property :type row) 'rule) 'rule
2505 (org-export-table-row-group row info)))))))
2506 ;; 2. Special rows are ignored in count.
2507 (should
2508 (equal
2509 '(rule 1)
2510 (org-test-with-parsed-data "
2511 | / | < | > |
2512 |---|---+---|
2513 | | 1 | 2 |"
2514 (org-element-map tree 'table-row
2515 (lambda (row)
2516 (if (eq (org-element-property :type row) 'rule) 'rule
2517 (org-export-table-row-group row info)))
2518 info))))
2519 ;; 3. Double rules also are ignored in count.
2520 (should
2521 (equal '(1 rule rule 2)
2522 (org-test-with-parsed-data "
2523 | a | b |
2524 |---+---|
2525 |---+---|
2526 | 1 | 2 |"
2527 (org-element-map tree 'table-row
2528 (lambda (row)
2529 (if (eq (org-element-property :type row) 'rule) 'rule
2530 (org-export-table-row-group row info))))))))
2532 (ert-deftest test-org-export/table-row-number ()
2533 "Test `org-export-table-row-number' specifications."
2534 ;; Standard test. Number is 0-indexed.
2535 (should
2536 (equal '(0 1)
2537 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
2538 (org-element-map tree 'table-row
2539 (lambda (row) (org-export-table-row-number row info)) info))))
2540 ;; Number ignores separators.
2541 (should
2542 (equal '(0 1)
2543 (org-test-with-parsed-data "
2544 | a | b | c |
2545 |---+---+---|
2546 | d | e | f |"
2547 (org-element-map tree 'table-row
2548 (lambda (row) (org-export-table-row-number row info)) info))))
2549 ;; Number ignores special rows.
2550 (should
2551 (equal '(0 1)
2552 (org-test-with-parsed-data "
2553 | / | < | > |
2554 | | b | c |
2555 |---+-----+-----|
2556 | | <c> | <c> |
2557 | | e | f |"
2558 (org-element-map tree 'table-row
2559 (lambda (row) (org-export-table-row-number row info)) info)))))
2561 (ert-deftest test-org-export/table-cell-width ()
2562 "Test `org-export-table-cell-width' specifications."
2563 ;; 1. Width is primarily determined by width cookies. If no cookie
2564 ;; is found, cell's width is nil.
2565 (org-test-with-parsed-data "
2566 | / | <l> | <6> | <l7> |
2567 | | a | b | c |"
2568 (should
2569 (equal
2570 '(nil 6 7)
2571 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2572 (org-element-map tree 'table-cell 'identity info)))))
2573 ;; 2. The last width cookie has precedence.
2574 (org-test-with-parsed-data "
2575 | <6> |
2576 | <7> |
2577 | a |"
2578 (should
2579 (equal
2580 '(7)
2581 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2582 (org-element-map tree 'table-cell 'identity info)))))
2583 ;; 3. Valid width cookies must have a specific row.
2584 (org-test-with-parsed-data "| <6> | cell |"
2585 (should
2586 (equal
2587 '(nil nil)
2588 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2589 (org-element-map tree 'table-cell 'identity))))))
2591 (ert-deftest test-org-export/table-cell-alignment ()
2592 "Test `org-export-table-cell-alignment' specifications."
2593 ;; 1. Alignment is primarily determined by alignment cookies.
2594 (should
2595 (equal '(left center right)
2596 (let ((org-table-number-fraction 0.5)
2597 (org-table-number-regexp "^[0-9]+$"))
2598 (org-test-with-parsed-data "| <l> | <c> | <r> |"
2599 (mapcar (lambda (cell)
2600 (org-export-table-cell-alignment cell info))
2601 (org-element-map tree 'table-cell 'identity))))))
2602 ;; 2. The last alignment cookie has precedence.
2603 (should
2604 (equal '(right right right)
2605 (org-test-with-parsed-data "
2606 | <l8> |
2607 | cell |
2608 | <r9> |"
2609 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
2610 (org-element-map tree 'table-cell 'identity)))))
2611 ;; 3. If there's no cookie, cell's contents determine alignment.
2612 ;; A column mostly made of cells containing numbers will align
2613 ;; its cells to the right.
2614 (should
2615 (equal '(right right right)
2616 (let ((org-table-number-fraction 0.5)
2617 (org-table-number-regexp "^[0-9]+$"))
2618 (org-test-with-parsed-data "
2619 | 123 |
2620 | some text |
2621 | 12345 |"
2622 (mapcar (lambda (cell)
2623 (org-export-table-cell-alignment cell info))
2624 (org-element-map tree 'table-cell 'identity))))))
2625 ;; 4. Otherwise, they will be aligned to the left.
2626 (should
2627 (equal '(left left left)
2628 (org-test-with-parsed-data "
2629 | text |
2630 | some text |
2631 | \alpha |"
2632 (mapcar (lambda (cell)
2633 (org-export-table-cell-alignment cell info))
2634 (org-element-map tree 'table-cell 'identity info))))))
2636 (ert-deftest test-org-export/table-cell-borders ()
2637 "Test `org-export-table-cell-borders' specifications."
2638 ;; 1. Recognize various column groups indicators.
2639 (org-test-with-parsed-data "| / | < | > | <> |"
2640 (should
2641 (equal
2642 '((right bottom top) (left bottom top) (right bottom top)
2643 (right left bottom top))
2644 (mapcar (lambda (cell)
2645 (org-export-table-cell-borders cell info))
2646 (org-element-map tree 'table-cell 'identity)))))
2647 ;; 2. Accept shortcuts to define column groups.
2648 (org-test-with-parsed-data "| / | < | < |"
2649 (should
2650 (equal
2651 '((right bottom top) (right left bottom top) (left bottom top))
2652 (mapcar (lambda (cell)
2653 (org-export-table-cell-borders cell info))
2654 (org-element-map tree 'table-cell 'identity)))))
2655 ;; 3. A valid column groups row must start with a "/".
2656 (org-test-with-parsed-data "
2657 | | < |
2658 | a | b |"
2659 (should
2660 (equal '((top) (top) (bottom) (bottom))
2661 (mapcar (lambda (cell)
2662 (org-export-table-cell-borders cell info))
2663 (org-element-map tree 'table-cell 'identity)))))
2664 ;; 4. Take table rules into consideration.
2665 (org-test-with-parsed-data "
2666 | 1 |
2667 |---|
2668 | 2 |"
2669 (should
2670 (equal '((below top) (bottom above))
2671 (mapcar (lambda (cell)
2672 (org-export-table-cell-borders cell info))
2673 (org-element-map tree 'table-cell 'identity)))))
2674 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
2675 ;; (resp. `bottom' and `below') borders. Any special row is
2676 ;; ignored.
2677 (org-test-with-parsed-data "
2678 |---+----|
2679 | / | |
2680 | | 1 |
2681 |---+----|"
2682 (should
2683 (equal '((bottom below top above))
2684 (last
2685 (mapcar (lambda (cell)
2686 (org-export-table-cell-borders cell info))
2687 (org-element-map tree 'table-cell 'identity)))))))
2689 (ert-deftest test-org-export/table-dimensions ()
2690 "Test `org-export-table-dimensions' specifications."
2691 ;; 1. Standard test.
2692 (org-test-with-parsed-data "
2693 | 1 | 2 | 3 |
2694 | 4 | 5 | 6 |"
2695 (should
2696 (equal '(2 . 3)
2697 (org-export-table-dimensions
2698 (org-element-map tree 'table 'identity info 'first-match) info))))
2699 ;; 2. Ignore horizontal rules and special columns.
2700 (org-test-with-parsed-data "
2701 | / | < | > |
2702 | 1 | 2 | 3 |
2703 |---+---+---|
2704 | 4 | 5 | 6 |"
2705 (should
2706 (equal '(2 . 3)
2707 (org-export-table-dimensions
2708 (org-element-map tree 'table 'identity info 'first-match) info)))))
2710 (ert-deftest test-org-export/table-cell-address ()
2711 "Test `org-export-table-cell-address' specifications."
2712 ;; 1. Standard test: index is 0-based.
2713 (org-test-with-parsed-data "| a | b |"
2714 (should
2715 (equal '((0 . 0) (0 . 1))
2716 (org-element-map tree 'table-cell
2717 (lambda (cell) (org-export-table-cell-address cell info))
2718 info))))
2719 ;; 2. Special column isn't counted, nor are special rows.
2720 (org-test-with-parsed-data "
2721 | / | <> |
2722 | | c |"
2723 (should
2724 (equal '(0 . 0)
2725 (org-export-table-cell-address
2726 (car (last (org-element-map tree 'table-cell 'identity info)))
2727 info))))
2728 ;; 3. Tables rules do not count either.
2729 (org-test-with-parsed-data "
2730 | a |
2731 |---|
2732 | b |
2733 |---|
2734 | c |"
2735 (should
2736 (equal '(2 . 0)
2737 (org-export-table-cell-address
2738 (car (last (org-element-map tree 'table-cell 'identity info)))
2739 info))))
2740 ;; 4. Return nil for special cells.
2741 (org-test-with-parsed-data "| / | a |"
2742 (should-not
2743 (org-export-table-cell-address
2744 (org-element-map tree 'table-cell 'identity nil 'first-match)
2745 info))))
2747 (ert-deftest test-org-export/get-table-cell-at ()
2748 "Test `org-export-get-table-cell-at' specifications."
2749 ;; 1. Address ignores special columns, special rows and rules.
2750 (org-test-with-parsed-data "
2751 | / | <> |
2752 | | a |
2753 |---+----|
2754 | | b |"
2755 (should
2756 (equal '("b")
2757 (org-element-contents
2758 (org-export-get-table-cell-at
2759 '(1 . 0)
2760 (org-element-map tree 'table 'identity info 'first-match)
2761 info)))))
2762 ;; 2. Return value for a non-existent address is nil.
2763 (org-test-with-parsed-data "| a |"
2764 (should-not
2765 (org-export-get-table-cell-at
2766 '(2 . 2)
2767 (org-element-map tree 'table 'identity info 'first-match)
2768 info)))
2769 (org-test-with-parsed-data "| / |"
2770 (should-not
2771 (org-export-get-table-cell-at
2772 '(0 . 0)
2773 (org-element-map tree 'table 'identity info 'first-match)
2774 info))))
2776 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
2777 "Test `org-export-table-cell-starts-colgroup-p' specifications."
2778 ;; 1. A cell at a beginning of a row always starts a column group.
2779 (org-test-with-parsed-data "| a |"
2780 (should
2781 (org-export-table-cell-starts-colgroup-p
2782 (org-element-map tree 'table-cell 'identity info 'first-match)
2783 info)))
2784 ;; 2. Special column should be ignored when determining the
2785 ;; beginning of the row.
2786 (org-test-with-parsed-data "
2787 | / | |
2788 | | a |"
2789 (should
2790 (org-export-table-cell-starts-colgroup-p
2791 (org-element-map tree 'table-cell 'identity info 'first-match)
2792 info)))
2793 ;; 2. Explicit column groups.
2794 (org-test-with-parsed-data "
2795 | / | | < |
2796 | a | b | c |"
2797 (should
2798 (equal
2799 '(yes no yes)
2800 (org-element-map tree 'table-cell
2801 (lambda (cell)
2802 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
2803 info)))))
2805 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
2806 "Test `org-export-table-cell-ends-colgroup-p' specifications."
2807 ;; 1. A cell at the end of a row always ends a column group.
2808 (org-test-with-parsed-data "| a |"
2809 (should
2810 (org-export-table-cell-ends-colgroup-p
2811 (org-element-map tree 'table-cell 'identity info 'first-match)
2812 info)))
2813 ;; 2. Special column should be ignored when determining the
2814 ;; beginning of the row.
2815 (org-test-with-parsed-data "
2816 | / | |
2817 | | a |"
2818 (should
2819 (org-export-table-cell-ends-colgroup-p
2820 (org-element-map tree 'table-cell 'identity info 'first-match)
2821 info)))
2822 ;; 3. Explicit column groups.
2823 (org-test-with-parsed-data "
2824 | / | < | |
2825 | a | b | c |"
2826 (should
2827 (equal
2828 '(yes no yes)
2829 (org-element-map tree 'table-cell
2830 (lambda (cell)
2831 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
2832 info)))))
2834 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
2835 "Test `org-export-table-row-starts-rowgroup-p' specifications."
2836 ;; 1. A row at the beginning of a table always starts a row group.
2837 ;; So does a row following a table rule.
2838 (org-test-with-parsed-data "
2839 | a |
2840 |---|
2841 | b |"
2842 (should
2843 (equal
2844 '(yes no yes)
2845 (org-element-map tree 'table-row
2846 (lambda (row)
2847 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2848 info))))
2849 ;; 2. Special rows should be ignored when determining the beginning
2850 ;; of the row.
2851 (org-test-with-parsed-data "
2852 | / | < |
2853 | | a |
2854 |---+---|
2855 | / | < |
2856 | | b |"
2857 (should
2858 (equal
2859 '(yes no yes)
2860 (org-element-map tree 'table-row
2861 (lambda (row)
2862 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2863 info)))))
2865 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
2866 "Test `org-export-table-row-ends-rowgroup-p' specifications."
2867 ;; 1. A row at the end of a table always ends a row group. So does
2868 ;; a row preceding a table rule.
2869 (org-test-with-parsed-data "
2870 | a |
2871 |---|
2872 | b |"
2873 (should
2874 (equal
2875 '(yes no yes)
2876 (org-element-map tree 'table-row
2877 (lambda (row)
2878 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2879 info))))
2880 ;; 2. Special rows should be ignored when determining the beginning
2881 ;; of the row.
2882 (org-test-with-parsed-data "
2883 | | a |
2884 | / | < |
2885 |---+---|
2886 | | b |
2887 | / | < |"
2888 (should
2889 (equal
2890 '(yes no yes)
2891 (org-element-map tree 'table-row
2892 (lambda (row)
2893 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2894 info)))))
2896 (ert-deftest test-org-export/table-row-starts-header-p ()
2897 "Test `org-export-table-row-starts-header-p' specifications."
2898 ;; 1. Only the row starting the first row group starts the table
2899 ;; header.
2900 (org-test-with-parsed-data "
2901 | a |
2902 | b |
2903 |---|
2904 | c |"
2905 (should
2906 (equal
2907 '(yes no no no)
2908 (org-element-map tree 'table-row
2909 (lambda (row)
2910 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
2911 info))))
2912 ;; 2. A row cannot start an header if there's no header in the
2913 ;; table.
2914 (org-test-with-parsed-data "
2915 | a |
2916 |---|"
2917 (should-not
2918 (org-export-table-row-starts-header-p
2919 (org-element-map tree 'table-row 'identity info 'first-match)
2920 info))))
2922 (ert-deftest test-org-export/table-row-ends-header-p ()
2923 "Test `org-export-table-row-ends-header-p' specifications."
2924 ;; 1. Only the row starting the first row group starts the table
2925 ;; header.
2926 (org-test-with-parsed-data "
2927 | a |
2928 | b |
2929 |---|
2930 | c |"
2931 (should
2932 (equal
2933 '(no yes no no)
2934 (org-element-map tree 'table-row
2935 (lambda (row)
2936 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
2937 info))))
2938 ;; 2. A row cannot start an header if there's no header in the
2939 ;; table.
2940 (org-test-with-parsed-data "
2941 | a |
2942 |---|"
2943 (should-not
2944 (org-export-table-row-ends-header-p
2945 (org-element-map tree 'table-row 'identity info 'first-match)
2946 info))))
2950 ;;; Tables of Contents
2952 (ert-deftest test-org-export/collect-headlines ()
2953 "Test `org-export-collect-headlines' specifications."
2954 ;; Standard test.
2955 (should
2956 (= 2
2957 (length
2958 (org-test-with-parsed-data "* H1\n** H2"
2959 (org-export-collect-headlines info)))))
2960 ;; Do not collect headlines below optional argument.
2961 (should
2962 (= 1
2963 (length
2964 (org-test-with-parsed-data "* H1\n** H2"
2965 (org-export-collect-headlines info 1)))))
2966 ;; Never collect headlines below maximum headline level.
2967 (should
2968 (= 1
2969 (length
2970 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
2971 (org-export-collect-headlines info)))))
2972 (should
2973 (= 1
2974 (length
2975 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
2976 (org-export-collect-headlines info 2))))))
2980 ;;; Templates
2982 (ert-deftest test-org-export/inner-template ()
2983 "Test `inner-template' translator specifications."
2984 (should
2985 (equal "Success!"
2986 (org-test-with-temp-text "* Headline"
2987 (org-export-as
2988 (org-export-create-backend
2989 :transcoders
2990 '((inner-template . (lambda (contents info) "Success!"))
2991 (headline . (lambda (h c i) "Headline"))))))))
2992 ;; Inner template is applied even in a "body-only" export.
2993 (should
2994 (equal "Success!"
2995 (org-test-with-temp-text "* Headline"
2996 (org-export-as
2997 (org-export-create-backend
2998 :transcoders '((inner-template . (lambda (c i) "Success!"))
2999 (headline . (lambda (h c i) "Headline"))))
3000 nil nil 'body-only)))))
3002 (ert-deftest test-org-export/template ()
3003 "Test `template' translator specifications."
3004 (should
3005 (equal "Success!"
3006 (org-test-with-temp-text "* Headline"
3007 (org-export-as
3008 (org-export-create-backend
3009 :transcoders '((template . (lambda (contents info) "Success!"))
3010 (headline . (lambda (h c i) "Headline"))))))))
3011 ;; Template is not applied in a "body-only" export.
3012 (should-not
3013 (equal "Success!"
3014 (org-test-with-temp-text "* Headline"
3015 (org-export-as
3016 (org-export-create-backend
3017 :transcoders '((template . (lambda (contents info) "Success!"))
3018 (headline . (lambda (h c i) "Headline"))))
3019 nil nil 'body-only)))))
3023 ;;; Topology
3025 (ert-deftest test-org-export/get-next-element ()
3026 "Test `org-export-get-next-element' specifications."
3027 ;; Standard test.
3028 (should
3029 (equal "b"
3030 (org-test-with-parsed-data "* Headline\n*a* b"
3031 (org-export-get-next-element
3032 (org-element-map tree 'bold 'identity info t) info))))
3033 ;; Return nil when no previous element.
3034 (should-not
3035 (org-test-with-parsed-data "* Headline\na *b*"
3036 (org-export-get-next-element
3037 (org-element-map tree 'bold 'identity info t) info)))
3038 ;; Non-exportable elements are ignored.
3039 (should-not
3040 (let ((org-export-with-timestamps nil))
3041 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
3042 (org-export-get-next-element
3043 (org-element-map tree 'entity 'identity info t) info))))
3044 ;; Find next element in secondary strings.
3045 (should
3046 (eq 'verbatim
3047 (org-test-with-parsed-data "* a =verb="
3048 (org-element-type
3049 (org-export-get-next-element
3050 (org-element-map tree 'plain-text 'identity info t) info)))))
3051 (should
3052 (eq 'verbatim
3053 (org-test-with-parsed-data "* /italic/ =verb="
3054 (org-element-type
3055 (org-export-get-next-element
3056 (org-element-map tree 'italic 'identity info t) info)))))
3057 ;; Find next element in document keywords.
3058 (should
3059 (eq 'verbatim
3060 (org-test-with-parsed-data "#+TITLE: a =verb="
3061 (org-element-type
3062 (org-export-get-next-element
3063 (org-element-map
3064 (plist-get info :title) 'plain-text 'identity info t) info)))))
3065 ;; Find next element in parsed affiliated keywords.
3066 (should
3067 (eq 'verbatim
3068 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
3069 (org-element-type
3070 (org-export-get-next-element
3071 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3072 ;; With optional argument N, return a list containing all the
3073 ;; following elements.
3074 (should
3075 (equal
3076 '(bold code underline)
3077 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3078 (mapcar 'car
3079 (org-export-get-next-element
3080 (org-element-map tree 'italic 'identity info t) info t)))))
3081 ;; When N is a positive integer, return a list containing up to
3082 ;; N following elements.
3083 (should
3084 (equal
3085 '(bold code)
3086 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3087 (mapcar 'car
3088 (org-export-get-next-element
3089 (org-element-map tree 'italic 'identity info t) info 2))))))
3091 (ert-deftest test-org-export/get-previous-element ()
3092 "Test `org-export-get-previous-element' specifications."
3093 ;; Standard test.
3094 (should
3095 (equal "a "
3096 (org-test-with-parsed-data "* Headline\na *b*"
3097 (org-export-get-previous-element
3098 (org-element-map tree 'bold 'identity info t) info))))
3099 ;; Return nil when no previous element.
3100 (should-not
3101 (org-test-with-parsed-data "* Headline\n*a* b"
3102 (org-export-get-previous-element
3103 (org-element-map tree 'bold 'identity info t) info)))
3104 ;; Non-exportable elements are ignored.
3105 (should-not
3106 (let ((org-export-with-timestamps nil))
3107 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
3108 (org-export-get-previous-element
3109 (org-element-map tree 'entity 'identity info t) info))))
3110 ;; Find previous element in secondary strings.
3111 (should
3112 (eq 'verbatim
3113 (org-test-with-parsed-data "* =verb= a"
3114 (org-element-type
3115 (org-export-get-previous-element
3116 (org-element-map tree 'plain-text 'identity info t) info)))))
3117 (should
3118 (eq 'verbatim
3119 (org-test-with-parsed-data "* =verb= /italic/"
3120 (org-element-type
3121 (org-export-get-previous-element
3122 (org-element-map tree 'italic 'identity info t) info)))))
3123 ;; Find previous element in document keywords.
3124 (should
3125 (eq 'verbatim
3126 (org-test-with-parsed-data "#+TITLE: =verb= a"
3127 (org-element-type
3128 (org-export-get-previous-element
3129 (org-element-map
3130 (plist-get info :title) 'plain-text 'identity info t) info)))))
3131 ;; Find previous element in parsed affiliated keywords.
3132 (should
3133 (eq 'verbatim
3134 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
3135 (org-element-type
3136 (org-export-get-previous-element
3137 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3138 ;; With optional argument N, return a list containing up to
3139 ;; N previous elements.
3140 (should
3141 (equal '(underline italic bold)
3142 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3143 (mapcar 'car
3144 (org-export-get-previous-element
3145 (org-element-map tree 'code 'identity info t) info t)))))
3146 ;; When N is a positive integer, return a list containing up to
3147 ;; N previous elements.
3148 (should
3149 (equal '(italic bold)
3150 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3151 (mapcar 'car
3152 (org-export-get-previous-element
3153 (org-element-map tree 'code 'identity info t) info 2))))))
3156 (provide 'test-ox)
3157 ;;; test-org-export.el end here