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