Merge branch 'maint'
[org-mode/org-tableheadings.git] / testing / lisp / test-ox.el
blob858d38cda8c82611d3537ec2cdbadabc77461f34
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 (org-test-with-temp-text
839 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
840 org-test-dir)
841 (org-export-expand-include-keyword)
842 (should
843 (equal
844 (buffer-string)
845 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
846 ;; Inclusion within a src-block.
847 (org-test-with-temp-text
848 (format
849 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
850 org-test-dir)
851 (org-export-expand-include-keyword)
852 (should (equal (buffer-string)
853 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n")))
854 ;; Footnotes labels are local to each included file.
855 (should
856 (= 6
857 (length
858 (delete-dups
859 (let ((contents "
860 Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote].
861 \[fn:1] Footnote 1
862 \[fn:test] Footnote \"test\""))
863 (org-test-with-temp-text-in-file contents
864 (let ((file1 (buffer-file-name)))
865 (org-test-with-temp-text-in-file contents
866 (let ((file2 (buffer-file-name)))
867 (org-test-with-temp-text
868 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
869 file1 file2)
870 (org-export-expand-include-keyword)
871 (org-element-map (org-element-parse-buffer)
872 'footnote-reference
873 (lambda (ref)
874 (org-element-property :label ref)))))))))))))
875 ;; Footnotes labels are not local to each include keyword.
876 (should
877 (= 3
878 (length
879 (delete-dups
880 (let ((contents "
881 Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote].
882 \[fn:1] Footnote 1
883 \[fn:test] Footnote \"test\""))
884 (org-test-with-temp-text-in-file contents
885 (let ((file (buffer-file-name)))
886 (org-test-with-temp-text
887 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
888 (org-export-expand-include-keyword)
889 (org-element-map (org-element-parse-buffer)
890 'footnote-reference
891 (lambda (ref) (org-element-property :label ref))))))))))))
893 (ert-deftest test-org-export/expand-macro ()
894 "Test macro expansion in an Org buffer."
895 ;; Standard macro expansion.
896 (should
897 (equal "#+MACRO: macro1 value\nvalue\n"
898 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
899 (org-export-as (org-test-default-backend)))))
900 ;; Expand specific macros.
901 (should
902 (equal "me 2012-03-29 me@here Title\n"
903 (org-test-with-temp-text
905 #+TITLE: Title
906 #+DATE: 2012-03-29
907 #+AUTHOR: me
908 #+EMAIL: me@here
909 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
910 (let ((output (org-export-as (org-test-default-backend))))
911 (substring output (string-match ".*\n\\'" output))))))
912 ;; Expand specific macros when property contained a regular macro
913 ;; already.
914 (should
915 (equal "value\n"
916 (org-test-with-temp-text "
917 #+MACRO: macro1 value
918 #+TITLE: {{{macro1}}}
919 {{{title}}}"
920 (let ((output (org-export-as (org-test-default-backend))))
921 (substring output (string-match ".*\n\\'" output))))))
922 ;; Expand macros with templates in included files.
923 (should
924 (equal "success\n"
925 (org-test-with-temp-text
926 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
927 {{{included-macro}}}" org-test-dir)
928 (let ((output (org-export-as (org-test-default-backend))))
929 (substring output (string-match ".*\n\\'" output)))))))
931 (ert-deftest test-org-export/user-ignore-list ()
932 "Test if `:ignore-list' accepts user input."
933 (let ((backend (org-test-default-backend)))
934 (setf (org-export-backend-transcoders backend)
935 (cons '(template . (lambda (body i)
936 (format "BEGIN\n%sEND" body)))
937 (org-export-backend-transcoders backend)))
938 (org-test-with-temp-text "Text"
939 (should (equal (org-export-as backend nil nil 'body-only)
940 "Text\n"))
941 (should (equal (org-export-as backend) "BEGIN\nText\nEND"))))
942 (should
943 (equal
944 "* Head1\n"
945 (let ((org-export-filter-parse-tree-functions
946 '((lambda (data backend info)
947 ;; Ignore headlines with the word "note" in their title.
948 (org-element-map data 'headline
949 (lambda (headline)
950 (when (string-match "\\<note\\>"
951 (org-element-property :raw-value
952 headline))
953 (org-export-ignore-element headline info)))
954 info)
955 data))))
956 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
957 (org-export-as (org-test-default-backend)))))))
959 (ert-deftest test-org-export/before-processing-hook ()
960 "Test `org-export-before-processing-hook'."
961 (should
962 (equal
963 "#+MACRO: mac val\nTest\n"
964 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
965 (let ((org-export-before-processing-hook
966 '((lambda (backend)
967 (while (re-search-forward "{{{" nil t)
968 (let ((object (org-element-context)))
969 (when (eq (org-element-type object) 'macro)
970 (delete-region
971 (org-element-property :begin object)
972 (org-element-property :end object)))))))))
973 (org-export-as (org-test-default-backend)))))))
975 (ert-deftest test-org-export/before-parsing-hook ()
976 "Test `org-export-before-parsing-hook'."
977 (should
978 (equal "Body 1\nBody 2\n"
979 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
980 (let ((org-export-before-parsing-hook
981 '((lambda (backend)
982 (goto-char (point-min))
983 (while (re-search-forward org-outline-regexp-bol nil t)
984 (delete-region
985 (point-at-bol) (progn (forward-line) (point))))))))
986 (org-export-as (org-test-default-backend)))))))
990 ;;; Affiliated Keywords
992 (ert-deftest test-org-export/read-attribute ()
993 "Test `org-export-read-attribute' specifications."
994 ;; Standard test.
995 (should
996 (equal
997 (org-export-read-attribute
998 :attr_html
999 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
1000 (org-element-at-point)))
1001 '(:a "1" :b "2")))
1002 ;; Return nil on empty attribute.
1003 (should-not
1004 (org-export-read-attribute
1005 :attr_html
1006 (org-test-with-temp-text "Paragraph" (org-element-at-point))))
1007 ;; Return nil on "nil" string.
1008 (should
1009 (equal '(:a nil :b nil)
1010 (org-export-read-attribute
1011 :attr_html
1012 (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
1013 (org-element-at-point)))))
1014 ;; Return nil on empty string.
1015 (should
1016 (equal '(:a nil :b nil)
1017 (org-export-read-attribute
1018 :attr_html
1019 (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
1020 (org-element-at-point)))))
1021 ;; Return empty string when value is "".
1022 (should
1023 (equal '(:a "")
1024 (org-export-read-attribute
1025 :attr_html
1026 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
1027 (org-element-at-point)))))
1028 ;; Return \"\" when value is """".
1029 (should
1030 (equal '(:a "\"\"")
1031 (org-export-read-attribute
1032 :attr_html
1033 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
1034 (org-element-at-point)))))
1035 ;; Ignore text before first property.
1036 (should-not
1037 (member "ignore"
1038 (org-export-read-attribute
1039 :attr_html
1040 (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
1041 (org-element-at-point))))))
1043 (ert-deftest test-org-export/get-caption ()
1044 "Test `org-export-get-caption' specifications."
1045 ;; Without optional argument, return long caption
1046 (should
1047 (equal
1048 '("l")
1049 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1050 (org-export-get-caption (org-element-at-point)))))
1051 ;; With optional argument, return short caption.
1052 (should
1053 (equal
1054 '("s")
1055 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1056 (org-export-get-caption (org-element-at-point) t))))
1057 ;; Multiple lines are separated by white spaces.
1058 (should
1059 (equal
1060 '("a" " " "b")
1061 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
1062 (org-export-get-caption (org-element-at-point))))))
1066 ;;; Back-End Tools
1068 (ert-deftest test-org-export/define-backend ()
1069 "Test back-end definition and accessors."
1070 ;; Translate table.
1071 (should
1072 (equal '((headline . my-headline-test))
1073 (let (org-export--registered-backends)
1074 (org-export-define-backend 'test '((headline . my-headline-test)))
1075 (org-export-get-all-transcoders 'test))))
1076 ;; Filters.
1077 (should
1078 (equal '((:filter-headline . my-filter))
1079 (let (org-export--registered-backends)
1080 (org-export-define-backend 'test
1081 '((headline . my-headline-test))
1082 :filters-alist '((:filter-headline . my-filter)))
1083 (org-export-backend-filters (org-export-get-backend 'test)))))
1084 ;; Options.
1085 (should
1086 (equal '((:prop value))
1087 (let (org-export--registered-backends)
1088 (org-export-define-backend 'test
1089 '((headline . my-headline-test))
1090 :options-alist '((:prop value)))
1091 (org-export-backend-options (org-export-get-backend 'test)))))
1092 ;; Menu.
1093 (should
1094 (equal '(?k "Test Export" test)
1095 (let (org-export--registered-backends)
1096 (org-export-define-backend 'test
1097 '((headline . my-headline-test))
1098 :menu-entry '(?k "Test Export" test))
1099 (org-export-backend-menu (org-export-get-backend 'test)))))
1100 ;; Export Blocks.
1101 (should
1102 (equal '(("TEST" . org-element-export-block-parser))
1103 (let (org-export--registered-backends org-element-block-name-alist)
1104 (org-export-define-backend 'test
1105 '((headline . my-headline-test))
1106 :export-block '("test"))
1107 org-element-block-name-alist))))
1109 (ert-deftest test-org-export/define-derived-backend ()
1110 "Test `org-export-define-derived-backend' specifications."
1111 ;; Error when parent back-end is not defined.
1112 (should-error
1113 (let (org-export--registered-backends)
1114 (org-export-define-derived-backend 'test 'parent)))
1115 ;; Append translation table to parent's.
1116 (should
1117 (equal '((:headline . test) (:headline . parent))
1118 (let (org-export--registered-backends)
1119 (org-export-define-backend 'parent '((:headline . parent)))
1120 (org-export-define-derived-backend 'test 'parent
1121 :translate-alist '((:headline . test)))
1122 (org-export-get-all-transcoders 'test))))
1123 ;; Options defined in the new back have priority over those defined
1124 ;; in parent.
1125 (should
1126 (eq 'test
1127 (let (org-export--registered-backends)
1128 (org-export-define-backend 'parent
1129 '((:headline . parent))
1130 :options-alist '((:a nil nil 'parent)))
1131 (org-export-define-derived-backend 'test 'parent
1132 :options-alist '((:a nil nil 'test)))
1133 (plist-get (org-export--get-global-options
1134 (org-export-get-backend 'test))
1135 :a)))))
1137 (ert-deftest test-org-export/derived-backend-p ()
1138 "Test `org-export-derived-backend-p' specifications."
1139 ;; Non-nil with direct match.
1140 (should
1141 (let (org-export--registered-backends)
1142 (org-export-define-backend 'test '((headline . test)))
1143 (org-export-derived-backend-p 'test 'test)))
1144 (should
1145 (let (org-export--registered-backends)
1146 (org-export-define-backend 'test '((headline . test)))
1147 (org-export-define-derived-backend 'test2 'test)
1148 (org-export-derived-backend-p 'test2 'test2)))
1149 ;; Non-nil with a direct parent.
1150 (should
1151 (let (org-export--registered-backends)
1152 (org-export-define-backend 'test '((headline . test)))
1153 (org-export-define-derived-backend 'test2 'test)
1154 (org-export-derived-backend-p 'test2 'test)))
1155 ;; Non-nil with an indirect parent.
1156 (should
1157 (let (org-export--registered-backends)
1158 (org-export-define-backend 'test '((headline . test)))
1159 (org-export-define-derived-backend 'test2 'test)
1160 (org-export-define-derived-backend 'test3 'test2)
1161 (org-export-derived-backend-p 'test3 'test)))
1162 ;; Nil otherwise.
1163 (should-not
1164 (let (org-export--registered-backends)
1165 (org-export-define-backend 'test '((headline . test)))
1166 (org-export-define-backend 'test2 '((headline . test2)))
1167 (org-export-derived-backend-p 'test2 'test)))
1168 (should-not
1169 (let (org-export--registered-backends)
1170 (org-export-define-backend 'test '((headline . test)))
1171 (org-export-define-backend 'test2 '((headline . test2)))
1172 (org-export-define-derived-backend 'test3 'test2)
1173 (org-export-derived-backend-p 'test3 'test))))
1175 (ert-deftest test-org-export/get-all-transcoders ()
1176 "Test `org-export-get-all-transcoders' specifications."
1177 ;; Return nil when back-end cannot be found.
1178 (should-not (org-export-get-all-transcoders nil))
1179 ;; Same as `org-export-transcoders' if no parent.
1180 (should
1181 (equal '((headline . ignore))
1182 (org-export-get-all-transcoders
1183 (org-export-create-backend
1184 :transcoders '((headline . ignore))))))
1185 ;; But inherit from all ancestors whenever possible.
1186 (should
1187 (equal '((section . ignore) (headline . ignore))
1188 (let (org-export--registered-backends)
1189 (org-export-define-backend 'b1 '((headline . ignore)))
1190 (org-export-get-all-transcoders
1191 (org-export-create-backend
1192 :parent 'b1 :transcoders '((section . ignore)))))))
1193 (should
1194 (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
1195 (let (org-export--registered-backends)
1196 (org-export-define-backend 'b1 '((headline . ignore)))
1197 (org-export-define-derived-backend 'b2 'b1
1198 :translate-alist '((section . ignore)))
1199 (org-export-get-all-transcoders
1200 (org-export-create-backend
1201 :parent 'b2 :transcoders '((paragraph . ignore)))))))
1202 ;; Back-end transcoders overrule inherited ones.
1203 (should
1204 (eq 'b
1205 (let (org-export--registered-backends)
1206 (org-export-define-backend 'b1 '((headline . a)))
1207 (cdr (assq 'headline
1208 (org-export-get-all-transcoders
1209 (org-export-create-backend
1210 :parent 'b1 :transcoders '((headline . b))))))))))
1212 (ert-deftest test-org-export/get-all-options ()
1213 "Test `org-export-get-all-options' specifications."
1214 ;; Return nil when back-end cannot be found.
1215 (should-not (org-export-get-all-options nil))
1216 ;; Same as `org-export-options' if no parent.
1217 (should
1218 (equal '((headline . ignore))
1219 (org-export-get-all-options
1220 (org-export-create-backend
1221 :options '((headline . ignore))))))
1222 ;; But inherit from all ancestors whenever possible.
1223 (should
1224 (equal '((:key2 value2) (:key1 value1))
1225 (let (org-export--registered-backends)
1226 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1227 (org-export-get-all-options
1228 (org-export-create-backend
1229 :parent 'b1 :options '((:key2 value2)))))))
1230 (should
1231 (equal '((:key3 value3) (:key2 value2) (:key1 value1))
1232 (let (org-export--registered-backends)
1233 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1234 (org-export-define-derived-backend 'b2 'b1
1235 :options-alist '((:key2 value2)))
1236 (org-export-get-all-options
1237 (org-export-create-backend
1238 :parent 'b2 :options '((:key3 value3)))))))
1239 ;; Back-end options overrule inherited ones.
1240 (should
1241 (eq 'b
1242 (let (org-export--registered-backends)
1243 (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
1244 (cdr (assq :key1
1245 (org-export-get-all-options
1246 (org-export-create-backend
1247 :parent 'b1 :options '((:key1 . b))))))))))
1249 (ert-deftest test-org-export/get-all-filters ()
1250 "Test `org-export-get-all-filters' specifications."
1251 ;; Return nil when back-end cannot be found.
1252 (should-not (org-export-get-all-filters nil))
1253 ;; Same as `org-export-filters' if no parent.
1254 (should
1255 (equal '((:filter-headline . ignore))
1256 (org-export-get-all-filters
1257 (org-export-create-backend
1258 :filters '((:filter-headline . ignore))))))
1259 ;; But inherit from all ancestors whenever possible.
1260 (should
1261 (equal '((:filter-section . ignore) (:filter-headline . ignore))
1262 (let (org-export--registered-backends)
1263 (org-export-define-backend 'b1
1264 nil :filters-alist '((:filter-headline . ignore)))
1265 (org-export-get-all-filters
1266 (org-export-create-backend
1267 :parent 'b1 :filters '((:filter-section . ignore)))))))
1268 (should
1269 (equal '((:filter-paragraph . ignore)
1270 (:filter-section . ignore)
1271 (:filter-headline . ignore))
1272 (let (org-export--registered-backends)
1273 (org-export-define-backend 'b1
1274 nil :filters-alist '((:filter-headline . ignore)))
1275 (org-export-define-derived-backend 'b2 'b1
1276 :filters-alist '((:filter-section . ignore)))
1277 (org-export-get-all-filters
1278 (org-export-create-backend
1279 :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
1280 ;; Back-end filters overrule inherited ones.
1281 (should
1282 (eq 'b
1283 (let (org-export--registered-backends)
1284 (org-export-define-backend 'b1 '((:filter-headline . a)))
1285 (cdr (assq :filter-headline
1286 (org-export-get-all-filters
1287 (org-export-create-backend
1288 :parent 'b1 :filters '((:filter-headline . b))))))))))
1290 (ert-deftest test-org-export/with-backend ()
1291 "Test `org-export-with-backend' definition."
1292 ;; Error when calling an undefined back-end
1293 (should-error (org-export-with-backend nil "Test"))
1294 ;; Error when called back-end doesn't have an appropriate
1295 ;; transcoder.
1296 (should-error
1297 (org-export-with-backend
1298 (org-export-create-backend :transcoders '((headline . ignore)))
1299 "Test"))
1300 ;; Otherwise, export using correct transcoder
1301 (should
1302 (equal "Success"
1303 (let (org-export--registered-backends)
1304 (org-export-define-backend 'test
1305 '((plain-text . (lambda (text contents info) "Failure"))))
1306 (org-export-define-backend 'test2
1307 '((plain-text . (lambda (text contents info) "Success"))))
1308 (org-export-with-backend 'test2 "Test"))))
1309 ;; Provide correct back-end if transcoder needs to use recursive
1310 ;; calls anyway.
1311 (should
1312 (equal "Success\n"
1313 (let ((test-back-end
1314 (org-export-create-backend
1315 :transcoders
1316 '((headline . (lambda (headline contents info)
1317 (org-export-data
1318 (org-element-property :title headline)
1319 info)))
1320 (plain-text . (lambda (text info) "Success"))))))
1321 (org-export-string-as
1322 "* Test"
1323 (org-export-create-backend
1324 :transcoders
1325 '((headline . (lambda (headline contents info)
1326 (org-export-with-backend
1327 test-back-end headline contents info))))))))))
1329 (ert-deftest test-org-export/data-with-backend ()
1330 "Test `org-export-data-with-backend' specifications."
1331 ;; Error when calling an undefined back-end.
1332 (should-error (org-export-data-with-backend nil "nil" nil))
1333 ;; Otherwise, export data recursively, using correct back-end.
1334 (should
1335 (equal
1336 "Success!"
1337 (org-export-data-with-backend
1338 '(bold nil "Test")
1339 (org-export-create-backend
1340 :transcoders
1341 '((plain-text . (lambda (text info) "Success"))
1342 (bold . (lambda (bold contents info) (concat contents "!")))))
1343 '(:with-emphasize t)))))
1347 ;;; Export Snippets
1349 (ert-deftest test-org-export/export-snippet ()
1350 "Test export snippets transcoding."
1351 ;; Standard test.
1352 (org-test-with-temp-text "@@test:A@@@@t:B@@"
1353 (let ((backend (org-test-default-backend)))
1354 (setf (org-export-backend-name backend) 'test)
1355 (setf (org-export-backend-transcoders backend)
1356 (cons (cons 'export-snippet
1357 (lambda (snippet contents info)
1358 (when (eq (org-export-snippet-backend snippet) 'test)
1359 (org-element-property :value snippet))))
1360 (org-export-backend-transcoders backend)))
1361 (let ((org-export-snippet-translation-alist nil))
1362 (should (equal (org-export-as backend) "A\n")))
1363 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
1364 (should (equal (org-export-as backend) "AB\n")))))
1365 ;; Ignored export snippets do not remove any blank.
1366 (should
1367 (equal "begin end\n"
1368 (org-test-with-parsed-data "begin @@test:A@@ end"
1369 (org-export-data-with-backend
1370 tree
1371 (org-export-create-backend
1372 :transcoders
1373 '((paragraph . (lambda (paragraph contents info) contents))
1374 (section . (lambda (section contents info) contents))))
1375 info)))))
1379 ;;; Footnotes
1381 (ert-deftest test-org-export/footnotes ()
1382 "Test footnotes specifications."
1383 (let ((org-footnote-section nil)
1384 (org-export-with-footnotes t))
1385 ;; 1. Read every type of footnote.
1386 (should
1387 (equal
1388 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
1389 (org-test-with-parsed-data
1390 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
1391 (org-element-map tree 'footnote-reference
1392 (lambda (ref)
1393 (let ((def (org-export-get-footnote-definition ref info)))
1394 (cons (org-export-get-footnote-number ref info)
1395 (if (eq (org-element-property :type ref) 'inline) (car def)
1396 (car (org-element-contents
1397 (car (org-element-contents def))))))))
1398 info))))
1399 ;; 2. Test nested footnotes order.
1400 (should
1401 (equal
1402 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
1403 (org-test-with-parsed-data
1404 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
1405 (org-element-map tree 'footnote-reference
1406 (lambda (ref)
1407 (when (org-export-footnote-first-reference-p ref info)
1408 (cons (org-export-get-footnote-number ref info)
1409 (org-element-property :label ref))))
1410 info))))
1411 ;; 3. Test nested footnote in invisible definitions.
1412 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
1413 ;; Hide definitions.
1414 (narrow-to-region (point) (point-at-eol))
1415 (let* ((tree (org-element-parse-buffer))
1416 (info (org-combine-plists
1417 `(:parse-tree ,tree)
1418 (org-export-collect-tree-properties
1419 tree (org-export-get-environment)))))
1420 ;; Both footnotes should be seen.
1421 (should
1422 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
1423 ;; 4. Test footnotes definitions collection.
1424 (should
1425 (= 4
1426 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1428 \[fn:2] B [fn:3] [fn::D].
1430 \[fn:3] C."
1431 (length (org-export-collect-footnote-definitions tree info)))))
1432 ;; 5. Test export of footnotes defined outside parsing scope.
1433 (should
1434 (equal
1435 "ParagraphOut of scope\n"
1436 (org-test-with-temp-text "[fn:1] Out of scope
1437 * Title
1438 Paragraph[fn:1]"
1439 (let ((backend (org-test-default-backend)))
1440 (setf (org-export-backend-transcoders backend)
1441 (cons (cons 'footnote-reference
1442 (lambda (fn contents info)
1443 (org-element-interpret-data
1444 (org-export-get-footnote-definition fn info))))
1445 (org-export-backend-transcoders backend)))
1446 (forward-line)
1447 (org-export-as backend 'subtree)))))
1448 ;; 6. Footnotes without a definition should be provided a fallback
1449 ;; definition.
1450 (should
1451 (org-test-with-parsed-data "[fn:1]"
1452 (org-export-get-footnote-definition
1453 (org-element-map tree 'footnote-reference 'identity info t) info)))
1454 ;; 7. Footnote section should be ignored in TOC and in headlines
1455 ;; numbering.
1456 (should
1457 (= 1 (let ((org-footnote-section "Footnotes"))
1458 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
1459 (org-export-collect-headlines info))))))
1460 (should
1461 (equal '(2)
1462 (let ((org-footnote-section "Footnotes"))
1463 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
1464 (org-element-map tree 'headline
1465 (lambda (hl)
1466 (when (equal (org-element-property :raw-value hl) "H2")
1467 (org-export-get-headline-number hl info)))
1468 info t)))))))
1472 ;;; Headlines and Inlinetasks
1474 (ert-deftest test-org-export/get-relative-level ()
1475 "Test `org-export-get-relative-level' specifications."
1476 ;; Standard test.
1477 (should
1478 (equal '(1 2)
1479 (let ((org-odd-levels-only nil))
1480 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1481 (org-element-map tree 'headline
1482 (lambda (h) (org-export-get-relative-level h info))
1483 info)))))
1484 ;; Missing levels
1485 (should
1486 (equal '(1 3)
1487 (let ((org-odd-levels-only nil))
1488 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
1489 (org-element-map tree 'headline
1490 (lambda (h) (org-export-get-relative-level h info))
1491 info))))))
1493 (ert-deftest test-org-export/low-level-p ()
1494 "Test `org-export-low-level-p' specifications."
1495 (should
1496 (equal
1497 '(no yes)
1498 (let ((org-odd-levels-only nil))
1499 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1500 (org-element-map tree 'headline
1501 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
1502 (plist-put info :headline-levels 1)))))))
1504 (ert-deftest test-org-export/get-headline-number ()
1505 "Test `org-export-get-headline-number' specifications."
1506 ;; Standard test.
1507 (should
1508 (equal
1509 '((1) (1 1))
1510 (let ((org-odd-levels-only nil))
1511 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1512 (org-element-map tree 'headline
1513 (lambda (h) (org-export-get-headline-number h info))
1514 info)))))
1515 ;; Missing levels are replaced with 0.
1516 (should
1517 (equal
1518 '((1) (1 0 1))
1519 (let ((org-odd-levels-only nil))
1520 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
1521 (org-element-map tree 'headline
1522 (lambda (h) (org-export-get-headline-number h info))
1523 info))))))
1525 (ert-deftest test-org-export/numbered-headline-p ()
1526 "Test `org-export-numbered-headline-p' specifications."
1527 ;; If `:section-numbers' is nil, never number headlines.
1528 (should-not
1529 (org-test-with-parsed-data "* Headline"
1530 (org-element-map tree 'headline
1531 (lambda (h) (org-export-numbered-headline-p h info))
1532 (plist-put info :section-numbers nil))))
1533 ;; If `:section-numbers' is a number, only number headlines with
1534 ;; a level greater that it.
1535 (should
1536 (equal
1537 '(yes no)
1538 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1539 (org-element-map tree 'headline
1540 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
1541 (plist-put info :section-numbers 1)))))
1542 ;; Otherwise, headlines are always numbered.
1543 (should
1544 (org-test-with-parsed-data "* Headline"
1545 (org-element-map tree 'headline
1546 (lambda (h) (org-export-numbered-headline-p h info))
1547 (plist-put info :section-numbers t)))))
1549 (ert-deftest test-org-export/number-to-roman ()
1550 "Test `org-export-number-to-roman' specifications."
1551 ;; If number is negative, return it as a string.
1552 (should (equal (org-export-number-to-roman -1) "-1"))
1553 ;; Otherwise, return it as a roman number.
1554 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
1556 (ert-deftest test-org-export/get-optional-title ()
1557 "Test `org-export-get-alt-title' specifications."
1558 ;; If ALT_TITLE property is defined, use it.
1559 (should
1560 (equal '("opt")
1561 (org-test-with-parsed-data
1562 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
1563 (org-export-get-alt-title
1564 (org-element-map tree 'headline 'identity info t)
1565 info))))
1566 ;; Otherwise, fall-back to regular title.
1567 (should
1568 (equal '("Headline")
1569 (org-test-with-parsed-data "* Headline"
1570 (org-export-get-alt-title
1571 (org-element-map tree 'headline 'identity info t)
1572 info)))))
1574 (ert-deftest test-org-export/get-tags ()
1575 "Test `org-export-get-tags' specifications."
1576 (let ((org-export-exclude-tags '("noexport"))
1577 (org-export-select-tags '("export")))
1578 ;; Standard test: tags which are not a select tag, an exclude tag,
1579 ;; or specified as optional argument shouldn't be ignored.
1580 (should
1581 (org-test-with-parsed-data "* Headline :tag:"
1582 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1583 info)))
1584 ;; Exclude tags are removed.
1585 (should-not
1586 (org-test-with-parsed-data "* Headline :noexport:"
1587 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1588 info)))
1589 ;; Select tags are removed.
1590 (should-not
1591 (org-test-with-parsed-data "* Headline :export:"
1592 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1593 info)))
1594 (should
1595 (equal
1596 '("tag")
1597 (org-test-with-parsed-data "* Headline :tag:export:"
1598 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1599 info))))
1600 ;; Tags provided in the optional argument are also ignored.
1601 (should-not
1602 (org-test-with-parsed-data "* Headline :ignore:"
1603 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1604 info '("ignore"))))
1605 ;; Allow tag inheritance.
1606 (should
1607 (equal
1608 '(("tag") ("tag"))
1609 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
1610 (org-element-map tree 'headline
1611 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
1612 ;; Tag inheritance checks FILETAGS keywords.
1613 (should
1614 (equal
1615 '(("a" "b" "tag"))
1616 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
1617 (org-element-map tree 'headline
1618 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
1620 (ert-deftest test-org-export/get-node-property ()
1621 "Test`org-export-get-node-property' specifications."
1622 ;; Standard test.
1623 (should
1624 (equal "value"
1625 (org-test-with-parsed-data "* Headline
1626 :PROPERTIES:
1627 :prop: value
1628 :END:"
1629 (org-export-get-node-property
1630 :PROP (org-element-map tree 'headline 'identity nil t)))))
1631 ;; Test inheritance.
1632 (should
1633 (equal "value"
1634 (org-test-with-parsed-data "* Parent
1635 :PROPERTIES:
1636 :prop: value
1637 :END:
1638 ** Headline
1639 Paragraph"
1640 (org-export-get-node-property
1641 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
1642 ;; Cannot return a value before the first headline.
1643 (should-not
1644 (org-test-with-parsed-data "Paragraph
1645 * Headline
1646 :PROPERTIES:
1647 :prop: value
1648 :END:"
1649 (org-export-get-node-property
1650 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
1652 (ert-deftest test-org-export/get-category ()
1653 "Test `org-export-get-category' specifications."
1654 ;; Standard test.
1655 (should
1656 (equal "value"
1657 (org-test-with-parsed-data "* Headline
1658 :PROPERTIES:
1659 :CATEGORY: value
1660 :END:"
1661 (org-export-get-category
1662 (org-element-map tree 'headline 'identity nil t) info))))
1663 ;; Test inheritance from a parent headline.
1664 (should
1665 (equal '("value" "value")
1666 (org-test-with-parsed-data "* Headline1
1667 :PROPERTIES:
1668 :CATEGORY: value
1669 :END:
1670 ** Headline2"
1671 (org-element-map tree 'headline
1672 (lambda (hl) (org-export-get-category hl info)) info))))
1673 ;; Test inheritance from #+CATEGORY keyword
1674 (should
1675 (equal "value"
1676 (org-test-with-parsed-data "#+CATEGORY: value
1677 * Headline"
1678 (org-export-get-category
1679 (org-element-map tree 'headline 'identity nil t) info))))
1680 ;; Test inheritance from file name.
1681 (should
1682 (equal "test"
1683 (org-test-with-parsed-data "* Headline"
1684 (let ((info (plist-put info :input-file "~/test.org")))
1685 (org-export-get-category
1686 (org-element-map tree 'headline 'identity nil t) info)))))
1687 ;; Fall-back value.
1688 (should
1689 (equal "???"
1690 (org-test-with-parsed-data "* Headline"
1691 (org-export-get-category
1692 (org-element-map tree 'headline 'identity nil t) info)))))
1694 (ert-deftest test-org-export/first-sibling-p ()
1695 "Test `org-export-first-sibling-p' specifications."
1696 ;; Standard test.
1697 (should
1698 (equal
1699 '(yes yes no)
1700 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1701 (org-element-map tree 'headline
1702 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1703 info))))
1704 ;; Ignore headlines not exported.
1705 (should
1706 (equal
1707 '(yes)
1708 (let ((org-export-exclude-tags '("ignore")))
1709 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
1710 (org-element-map tree 'headline
1711 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1712 info))))))
1714 (ert-deftest test-org-export/last-sibling-p ()
1715 "Test `org-export-last-sibling-p' specifications."
1716 ;; Standard test.
1717 (should
1718 (equal
1719 '(yes no yes)
1720 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1721 (org-element-map tree 'headline
1722 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1723 info))))
1724 ;; Ignore headlines not exported.
1725 (should
1726 (equal
1727 '(yes)
1728 (let ((org-export-exclude-tags '("ignore")))
1729 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
1730 (org-element-map tree 'headline
1731 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1732 info))))))
1734 (ert-deftest test-org-export/handle-inlinetasks ()
1735 "Test inlinetask export."
1736 ;; Inlinetask with an exclude tag.
1737 (when (featurep 'org-inlinetask)
1738 (should
1739 (equal
1741 (let ((org-inlinetask-min-level 3))
1742 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
1743 (org-export-as (org-test-default-backend)
1744 nil nil nil '(:exclude-tags ("noexp")))))))
1745 ;; Inlinetask with an include tag.
1746 (should
1747 (equal
1748 "* H2\n*** Inline :exp:\n"
1749 (let ((org-inlinetask-min-level 3)
1750 (org-tags-column 0))
1751 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
1752 (org-export-as (org-test-default-backend)
1753 nil nil nil '(:select-tags ("exp")))))))
1754 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
1755 (should
1756 (equal ""
1757 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
1758 (org-inlinetask-min-level 3))
1759 (org-test-with-temp-text "*** TODO Inline"
1760 (org-export-as (org-test-default-backend)
1761 nil nil nil '(:with-tasks nil))))))))
1765 ;;; Keywords
1767 (ert-deftest test-org-export/get-date ()
1768 "Test `org-export-get-date' specifications."
1769 ;; Return a properly formatted string when
1770 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
1771 ;; consists in a single timestamp.
1772 (should
1773 (equal "29 03 2012"
1774 (let ((org-export-date-timestamp-format "%d %m %Y"))
1775 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1776 (org-export-get-date info)))))
1777 ;; Return a secondary string otherwise.
1778 (should-not
1779 (stringp
1780 (let ((org-export-date-timestamp-format nil))
1781 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1782 (org-export-get-date info)))))
1783 (should
1784 (equal '("Date")
1785 (org-test-with-parsed-data "#+DATE: Date"
1786 (org-export-get-date info))))
1787 ;; Optional argument has precedence over
1788 ;; `org-export-date-timestamp-format'.
1789 (should
1790 (equal "29 03"
1791 (let ((org-export-date-timestamp-format "%d %m %Y"))
1792 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1793 (org-export-get-date info "%d %m"))))))
1797 ;;; Links
1799 (ert-deftest test-org-export/get-coderef-format ()
1800 "Test `org-export-get-coderef-format' specifications."
1801 ;; A link without description returns "%s"
1802 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
1803 "%s"))
1804 ;; Return "%s" when path is matched within description.
1805 (should (equal (org-export-get-coderef-format "path" "desc (path)")
1806 "desc %s"))
1807 ;; Otherwise return description.
1808 (should (equal (org-export-get-coderef-format "path" "desc")
1809 "desc")))
1811 (ert-deftest test-org-export/inline-image-p ()
1812 "Test `org-export-inline-image-p' specifications."
1813 (should
1814 (org-export-inline-image-p
1815 (org-test-with-temp-text "[[#id]]"
1816 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
1817 '(("custom-id" . "id")))))
1819 (ert-deftest test-org-export/fuzzy-link ()
1820 "Test fuzzy links specifications."
1821 ;; Link to an headline should return headline's number.
1822 (org-test-with-parsed-data
1823 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
1824 (should
1825 ;; Note: Headline's number is in fact a list of numbers.
1826 (equal '(2)
1827 (org-element-map tree 'link
1828 (lambda (link)
1829 (org-export-get-ordinal
1830 (org-export-resolve-fuzzy-link link info) info)) info t))))
1831 ;; Link to a target in an item should return item's number.
1832 (org-test-with-parsed-data
1833 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
1834 (should
1835 ;; Note: Item's number is in fact a list of numbers.
1836 (equal '(1 2)
1837 (org-element-map tree 'link
1838 (lambda (link)
1839 (org-export-get-ordinal
1840 (org-export-resolve-fuzzy-link link info) info)) info t))))
1841 ;; Link to a target in a footnote should return footnote's number.
1842 (org-test-with-parsed-data "
1843 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
1844 (should
1845 (equal '(2 3)
1846 (org-element-map tree 'link
1847 (lambda (link)
1848 (org-export-get-ordinal
1849 (org-export-resolve-fuzzy-link link info) info)) info))))
1850 ;; Link to a named element should return sequence number of that
1851 ;; element.
1852 (org-test-with-parsed-data
1853 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
1854 (should
1855 (= 2
1856 (org-element-map tree 'link
1857 (lambda (link)
1858 (org-export-get-ordinal
1859 (org-export-resolve-fuzzy-link link info) info)) info t))))
1860 ;; Link to a target not within an item, a table, a footnote
1861 ;; reference or definition should return section number.
1862 (org-test-with-parsed-data
1863 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
1864 (should
1865 (equal '(2)
1866 (org-element-map tree 'link
1867 (lambda (link)
1868 (org-export-get-ordinal
1869 (org-export-resolve-fuzzy-link link info) info)) info t))))
1870 ;; Space are not significant when matching a fuzzy link.
1871 (should
1872 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
1873 (org-element-map tree 'link
1874 (lambda (link) (org-export-resolve-fuzzy-link link info))
1875 info t)))
1876 ;; Statistics cookies are ignored for headline match.
1877 (should
1878 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
1879 (org-element-map tree 'link
1880 (lambda (link) (org-export-resolve-fuzzy-link link info))
1881 info t)))
1882 (should
1883 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
1884 (org-element-map tree 'link
1885 (lambda (link) (org-export-resolve-fuzzy-link link info))
1886 info t)))
1887 ;; Headline match is position dependent.
1888 (should-not
1889 (apply
1891 (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
1892 (org-element-map tree 'link
1893 (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
1895 (ert-deftest test-org-export/resolve-coderef ()
1896 "Test `org-export-resolve-coderef' specifications."
1897 (let ((org-coderef-label-format "(ref:%s)"))
1898 ;; 1. A link to a "-n -k -r" block returns line number.
1899 (org-test-with-parsed-data
1900 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
1901 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1902 (org-test-with-parsed-data
1903 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1904 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1905 ;; 2. A link to a "-n -r" block returns line number.
1906 (org-test-with-parsed-data
1907 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
1908 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1909 (org-test-with-parsed-data
1910 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1911 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1912 ;; 3. A link to a "-n" block returns coderef.
1913 (org-test-with-parsed-data
1914 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1915 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1916 (org-test-with-parsed-data
1917 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
1918 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1919 ;; 4. A link to a "-r" block returns line number.
1920 (org-test-with-parsed-data
1921 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1922 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1923 (org-test-with-parsed-data
1924 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
1925 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1926 ;; 5. A link to a block without a switch returns coderef.
1927 (org-test-with-parsed-data
1928 "#+BEGIN_SRC emacs-lisp\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\nText (ref:coderef)\n#+END_EXAMPLE"
1932 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1933 ;; 6. Correctly handle continued line numbers. A "+n" switch
1934 ;; should resume numbering from previous block with numbered
1935 ;; lines, ignoring blocks not numbering lines in the process.
1936 ;; A "-n" switch resets count.
1937 (org-test-with-parsed-data "
1938 #+BEGIN_EXAMPLE -n
1939 Text.
1940 #+END_EXAMPLE
1942 #+BEGIN_SRC emacs-lisp
1943 \(- 1 1)
1944 #+END_SRC
1946 #+BEGIN_SRC emacs-lisp +n -r
1947 \(+ 1 1) (ref:addition)
1948 #+END_SRC
1950 #+BEGIN_EXAMPLE -n -r
1951 Another text. (ref:text)
1952 #+END_EXAMPLE"
1953 (should (= (org-export-resolve-coderef "addition" info) 2))
1954 (should (= (org-export-resolve-coderef "text" info) 1)))
1955 ;; 7. Recognize coderef with user-specified syntax.
1956 (org-test-with-parsed-data
1957 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
1958 (should (equal (org-export-resolve-coderef "text" info) "text")))))
1960 (ert-deftest test-org-export/resolve-fuzzy-link ()
1961 "Test `org-export-resolve-fuzzy-link' specifications."
1962 ;; Match target objects.
1963 (should
1964 (org-test-with-parsed-data "<<target>> [[target]]"
1965 (org-export-resolve-fuzzy-link
1966 (org-element-map tree 'link 'identity info t) info)))
1967 ;; Match named elements.
1968 (should
1969 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
1970 (org-export-resolve-fuzzy-link
1971 (org-element-map tree 'link 'identity info t) info)))
1972 ;; Match exact headline's name.
1973 (should
1974 (org-test-with-parsed-data "* My headline\n[[My headline]]"
1975 (org-export-resolve-fuzzy-link
1976 (org-element-map tree 'link 'identity info t) info)))
1977 ;; Targets objects have priority over named elements and headline
1978 ;; titles.
1979 (should
1980 (eq 'target
1981 (org-test-with-parsed-data
1982 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
1983 (org-element-type
1984 (org-export-resolve-fuzzy-link
1985 (org-element-map tree 'link 'identity info t) info)))))
1986 ;; Named elements have priority over headline titles.
1987 (should
1988 (eq 'paragraph
1989 (org-test-with-parsed-data
1990 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
1991 (org-element-type
1992 (org-export-resolve-fuzzy-link
1993 (org-element-map tree 'link 'identity info t) info)))))
1994 ;; If link's path starts with a "*", only match headline titles,
1995 ;; though.
1996 (should
1997 (eq 'headline
1998 (org-test-with-parsed-data
1999 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
2000 (org-element-type
2001 (org-export-resolve-fuzzy-link
2002 (org-element-map tree 'link 'identity info t) info)))))
2003 ;; Return nil if no match.
2004 (should-not
2005 (org-test-with-parsed-data "[[target]]"
2006 (org-export-resolve-fuzzy-link
2007 (org-element-map tree 'link 'identity info t) info)))
2008 ;; Match fuzzy link even when before first headline.
2009 (should
2010 (eq 'headline
2011 (org-test-with-parsed-data "[[hl]]\n* hl"
2012 (org-element-type
2013 (org-export-resolve-fuzzy-link
2014 (org-element-map tree 'link 'identity info t) info))))))
2016 (ert-deftest test-org-export/resolve-id-link ()
2017 "Test `org-export-resolve-id-link' specifications."
2018 ;; 1. Regular test for custom-id link.
2019 (org-test-with-parsed-data "* Headline1
2020 :PROPERTIES:
2021 :CUSTOM_ID: test
2022 :END:
2023 * Headline 2
2024 \[[#test]]"
2025 (should
2026 (org-export-resolve-id-link
2027 (org-element-map tree 'link 'identity info t) info)))
2028 ;; 2. Failing test for custom-id link.
2029 (org-test-with-parsed-data "* Headline1
2030 :PROPERTIES:
2031 :CUSTOM_ID: test
2032 :END:
2033 * Headline 2
2034 \[[#no-match]]"
2035 (should-not
2036 (org-export-resolve-id-link
2037 (org-element-map tree 'link 'identity info t) info)))
2038 ;; 3. Test for internal id target.
2039 (org-test-with-parsed-data "* Headline1
2040 :PROPERTIES:
2041 :ID: aaaa
2042 :END:
2043 * Headline 2
2044 \[[id:aaaa]]"
2045 (should
2046 (org-export-resolve-id-link
2047 (org-element-map tree 'link 'identity info t) info)))
2048 ;; 4. Test for external id target.
2049 (org-test-with-parsed-data "[[id:aaaa]]"
2050 (should
2051 (org-export-resolve-id-link
2052 (org-element-map tree 'link 'identity info t)
2053 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
2055 (ert-deftest test-org-export/resolve-radio-link ()
2056 "Test `org-export-resolve-radio-link' specifications."
2057 ;; Standard test.
2058 (should
2059 (org-test-with-temp-text "<<<radio>>> radio"
2060 (org-update-radio-target-regexp)
2061 (let* ((tree (org-element-parse-buffer))
2062 (info `(:parse-tree ,tree)))
2063 (org-export-resolve-radio-link
2064 (org-element-map tree 'link 'identity info t)
2065 info))))
2066 ;; Radio targets are case-insensitive.
2067 (should
2068 (org-test-with-temp-text "<<<RADIO>>> radio"
2069 (org-update-radio-target-regexp)
2070 (let* ((tree (org-element-parse-buffer))
2071 (info `(:parse-tree ,tree)))
2072 (org-export-resolve-radio-link
2073 (org-element-map tree 'link 'identity info t)
2074 info))))
2075 ;; Radio target with objects.
2076 (should
2077 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
2078 (org-update-radio-target-regexp)
2079 (let* ((tree (org-element-parse-buffer))
2080 (info `(:parse-tree ,tree)))
2081 (org-export-resolve-radio-link
2082 (org-element-map tree 'link 'identity info t)
2083 info))))
2084 ;; Radio target with objects at its beginning.
2085 (should
2086 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
2087 (org-update-radio-target-regexp)
2088 (let* ((tree (org-element-parse-buffer))
2089 (info `(:parse-tree ,tree)))
2090 (org-export-resolve-radio-link
2091 (org-element-map tree 'link 'identity info t)
2092 info))))
2093 ;; Radio link next to an apostrophe.
2094 (should
2095 (org-test-with-temp-text "<<<radio>>> radio's"
2096 (org-update-radio-target-regexp)
2097 (let* ((tree (org-element-parse-buffer))
2098 (info `(:parse-tree ,tree)))
2099 (org-export-resolve-radio-link
2100 (org-element-map tree 'link 'identity info t)
2101 info))))
2102 ;; Multiple radio targets.
2103 (should
2104 (equal '("radio1" "radio2")
2105 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
2106 (org-update-radio-target-regexp)
2107 (let* ((tree (org-element-parse-buffer))
2108 (info `(:parse-tree ,tree)))
2109 (org-element-map tree 'link
2110 (lambda (link)
2111 (org-element-property
2112 :value (org-export-resolve-radio-link link info)))
2113 info)))))
2114 ;; Radio target is whitespace insensitive.
2115 (should
2116 (org-test-with-temp-text "<<<a radio>>> a\n radio"
2117 (org-update-radio-target-regexp)
2118 (let* ((tree (org-element-parse-buffer))
2119 (info `(:parse-tree ,tree)))
2120 (org-element-map tree 'link
2121 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
2125 ;;; Src-block and example-block
2127 (ert-deftest test-org-export/unravel-code ()
2128 "Test `org-export-unravel-code' function."
2129 ;; Code without reference.
2130 (should
2131 (equal '("(+ 1 1)\n")
2132 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2133 (org-export-unravel-code (org-element-at-point)))))
2134 ;; Code with reference.
2135 (should
2136 (equal '("(+ 1 1)\n" (1 . "test"))
2137 (org-test-with-temp-text
2138 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
2139 (let ((org-coderef-label-format "(ref:%s)"))
2140 (org-export-unravel-code (org-element-at-point))))))
2141 ;; Code with user-defined reference.
2142 (should
2143 (equal
2144 '("(+ 1 1)\n" (1 . "test"))
2145 (org-test-with-temp-text
2146 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
2147 (let ((org-coderef-label-format "(ref:%s)"))
2148 (org-export-unravel-code (org-element-at-point))))))
2149 ;; Code references keys are relative to the current block.
2150 (should
2151 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
2152 (org-test-with-temp-text "
2153 #+BEGIN_EXAMPLE -n
2154 \(+ 1 1)
2155 #+END_EXAMPLE
2156 #+BEGIN_EXAMPLE +n
2157 \(+ 2 2)
2158 \(+ 3 3) (ref:one)
2159 #+END_EXAMPLE"
2160 (goto-line 5)
2161 (let ((org-coderef-label-format "(ref:%s)"))
2162 (org-export-unravel-code (org-element-at-point)))))))
2164 (ert-deftest test-org-export/format-code-default ()
2165 "Test `org-export-format-code-default' specifications."
2166 ;; Return the empty string when code is empty.
2167 (should
2168 (equal ""
2169 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
2170 (org-export-format-code-default
2171 (org-element-map tree 'src-block 'identity info t) info))))
2172 ;; Number lines, two whitespace characters before the actual loc.
2173 (should
2174 (equal "1 a\n2 b\n"
2175 (org-test-with-parsed-data
2176 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
2177 (org-export-format-code-default
2178 (org-element-map tree 'src-block 'identity info t) info))))
2179 ;; Put references 6 whitespace characters after the widest line,
2180 ;; wrapped within parenthesis.
2181 (should
2182 (equal "123 (a)\n1 (b)\n"
2183 (let ((org-coderef-label-format "(ref:%s)"))
2184 (org-test-with-parsed-data
2185 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
2186 (org-export-format-code-default
2187 (org-element-map tree 'src-block 'identity info t) info))))))
2191 ;;; Smart Quotes
2193 (ert-deftest test-org-export/activate-smart-quotes ()
2194 "Test `org-export-activate-smart-quotes' specifications."
2195 ;; Opening double quotes: standard test.
2196 (should
2197 (equal
2198 '("some &ldquo;paragraph")
2199 (let ((org-export-default-language "en"))
2200 (org-test-with-parsed-data "some \"paragraph"
2201 (org-element-map tree 'plain-text
2202 (lambda (s) (org-export-activate-smart-quotes s :html info))
2203 info)))))
2204 ;; Opening quotes: at the beginning of a paragraph.
2205 (should
2206 (equal
2207 '("&ldquo;begin")
2208 (let ((org-export-default-language "en"))
2209 (org-test-with-parsed-data "\"begin"
2210 (org-element-map tree 'plain-text
2211 (lambda (s) (org-export-activate-smart-quotes s :html info))
2212 info)))))
2213 ;; Opening quotes: after an object.
2214 (should
2215 (equal
2216 '("&ldquo;begin")
2217 (let ((org-export-default-language "en"))
2218 (org-test-with-parsed-data "=verb= \"begin"
2219 (org-element-map tree 'plain-text
2220 (lambda (s) (org-export-activate-smart-quotes s :html info))
2221 info)))))
2222 ;; Closing quotes: standard test.
2223 (should
2224 (equal
2225 '("some&rdquo; paragraph")
2226 (let ((org-export-default-language "en"))
2227 (org-test-with-parsed-data "some\" paragraph"
2228 (org-element-map tree 'plain-text
2229 (lambda (s) (org-export-activate-smart-quotes s :html info))
2230 info)))))
2231 ;; Closing quotes: at the end of a paragraph.
2232 (should
2233 (equal
2234 '("end&rdquo;")
2235 (let ((org-export-default-language "en"))
2236 (org-test-with-parsed-data "end\""
2237 (org-element-map tree 'plain-text
2238 (lambda (s) (org-export-activate-smart-quotes s :html info))
2239 info)))))
2240 ;; Apostrophe: standard test.
2241 (should
2242 (equal
2243 '("It shouldn&rsquo;t fail")
2244 (let ((org-export-default-language "en"))
2245 (org-test-with-parsed-data "It shouldn't fail"
2246 (org-element-map tree 'plain-text
2247 (lambda (s) (org-export-activate-smart-quotes s :html info))
2248 info)))))
2249 ;; Apostrophe: before an object.
2250 (should
2251 (equal
2252 '("a&rsquo;")
2253 (let ((org-export-default-language "en"))
2254 (org-test-with-parsed-data "a'=b="
2255 (org-element-map tree 'plain-text
2256 (lambda (s) (org-export-activate-smart-quotes s :html info))
2257 info)))))
2258 ;; Apostrophe: after an object.
2259 (should
2260 (equal
2261 '("&rsquo;s")
2262 (let ((org-export-default-language "en"))
2263 (org-test-with-parsed-data "=code='s"
2264 (org-element-map tree 'plain-text
2265 (lambda (s) (org-export-activate-smart-quotes s :html info))
2266 info)))))
2267 ;; Special case: isolated quotes.
2268 (should
2269 (equal '("&ldquo;" "&rdquo;")
2270 (let ((org-export-default-language "en"))
2271 (org-test-with-parsed-data "\"$x$\""
2272 (org-element-map tree 'plain-text
2273 (lambda (s) (org-export-activate-smart-quotes s :html info))
2274 info)))))
2275 ;; Smart quotes in secondary strings.
2276 (should
2277 (equal '("&ldquo;" "&rdquo;")
2278 (let ((org-export-default-language "en"))
2279 (org-test-with-parsed-data "* \"$x$\""
2280 (org-element-map tree 'plain-text
2281 (lambda (s) (org-export-activate-smart-quotes s :html info))
2282 info)))))
2283 ;; Smart quotes in document keywords.
2284 (should
2285 (equal '("&ldquo;" "&rdquo;")
2286 (let ((org-export-default-language "en"))
2287 (org-test-with-parsed-data "#+TITLE: \"$x$\""
2288 (org-element-map (plist-get info :title) 'plain-text
2289 (lambda (s) (org-export-activate-smart-quotes s :html info))
2290 info)))))
2291 ;; Smart quotes in parsed affiliated keywords.
2292 (should
2293 (equal '("&ldquo;" "&rdquo;" "Paragraph")
2294 (let ((org-export-default-language "en"))
2295 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
2296 (org-element-map tree 'plain-text
2297 (lambda (s) (org-export-activate-smart-quotes s :html info))
2298 info nil nil t))))))
2302 ;;; Tables
2304 (ert-deftest test-org-export/special-column ()
2305 "Test if the table's special column is properly recognized."
2306 ;; 1. First column is special if it contains only a special marking
2307 ;; characters or empty cells.
2308 (org-test-with-temp-text "
2309 | ! | 1 |
2310 | | 2 |"
2311 (should
2312 (org-export-table-has-special-column-p
2313 (org-element-map
2314 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2315 ;; 2. If the column contains anything else, it isn't special.
2316 (org-test-with-temp-text "
2317 | ! | 1 |
2318 | b | 2 |"
2319 (should-not
2320 (org-export-table-has-special-column-p
2321 (org-element-map
2322 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2323 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
2324 ;; and "!".
2325 (org-test-with-temp-text "
2326 | # | 1 |
2327 | ^ | 2 |
2328 | * | 3 |
2329 | _ | 4 |
2330 | / | 5 |
2331 | $ | 6 |
2332 | ! | 7 |"
2333 (should
2334 (org-export-table-has-special-column-p
2335 (org-element-map
2336 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2337 ;; 4. A first column with only empty cells isn't considered as
2338 ;; special.
2339 (org-test-with-temp-text "
2340 | | 1 |
2341 | | 2 |"
2342 (should-not
2343 (org-export-table-has-special-column-p
2344 (org-element-map
2345 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
2347 (ert-deftest test-org-export/table-row-is-special-p ()
2348 "Test `org-export-table-row-is-special-p' specifications."
2349 ;; 1. A row is special if it has a special marking character in the
2350 ;; special column.
2351 (org-test-with-parsed-data "| ! | 1 |"
2352 (should
2353 (org-export-table-row-is-special-p
2354 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2355 ;; 2. A row is special when its first field is "/"
2356 (org-test-with-parsed-data "
2357 | / | 1 |
2358 | a | b |"
2359 (should
2360 (org-export-table-row-is-special-p
2361 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2362 ;; 3. A row only containing alignment cookies is also considered as
2363 ;; special.
2364 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
2365 (should
2366 (org-export-table-row-is-special-p
2367 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2368 ;; 4. Everything else isn't considered as special.
2369 (org-test-with-parsed-data "| \alpha | | c |"
2370 (should-not
2371 (org-export-table-row-is-special-p
2372 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2373 ;; 5. Table's rules are never considered as special rows.
2374 (org-test-with-parsed-data "|---+---|"
2375 (should-not
2376 (org-export-table-row-is-special-p
2377 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
2379 (ert-deftest test-org-export/has-header-p ()
2380 "Test `org-export-table-has-header-p' specifications."
2381 ;; 1. With an header.
2382 (org-test-with-parsed-data "
2383 | a | b |
2384 |---+---|
2385 | c | d |"
2386 (should
2387 (org-export-table-has-header-p
2388 (org-element-map tree 'table 'identity info 'first-match)
2389 info)))
2390 ;; 2. Without an header.
2391 (org-test-with-parsed-data "
2392 | a | b |
2393 | c | d |"
2394 (should-not
2395 (org-export-table-has-header-p
2396 (org-element-map tree 'table 'identity info 'first-match)
2397 info)))
2398 ;; 3. Don't get fooled with starting and ending rules.
2399 (org-test-with-parsed-data "
2400 |---+---|
2401 | a | b |
2402 | c | d |
2403 |---+---|"
2404 (should-not
2405 (org-export-table-has-header-p
2406 (org-element-map tree 'table 'identity info 'first-match)
2407 info))))
2409 (ert-deftest test-org-export/table-row-group ()
2410 "Test `org-export-table-row-group' specifications."
2411 ;; 1. A rule creates a new group.
2412 (should
2413 (equal '(1 rule 2)
2414 (org-test-with-parsed-data "
2415 | a | b |
2416 |---+---|
2417 | 1 | 2 |"
2418 (org-element-map tree 'table-row
2419 (lambda (row)
2420 (if (eq (org-element-property :type row) 'rule) 'rule
2421 (org-export-table-row-group row info)))))))
2422 ;; 2. Special rows are ignored in count.
2423 (should
2424 (equal
2425 '(rule 1)
2426 (org-test-with-parsed-data "
2427 | / | < | > |
2428 |---|---+---|
2429 | | 1 | 2 |"
2430 (org-element-map tree 'table-row
2431 (lambda (row)
2432 (if (eq (org-element-property :type row) 'rule) 'rule
2433 (org-export-table-row-group row info)))
2434 info))))
2435 ;; 3. Double rules also are ignored in count.
2436 (should
2437 (equal '(1 rule rule 2)
2438 (org-test-with-parsed-data "
2439 | a | b |
2440 |---+---|
2441 |---+---|
2442 | 1 | 2 |"
2443 (org-element-map tree 'table-row
2444 (lambda (row)
2445 (if (eq (org-element-property :type row) 'rule) 'rule
2446 (org-export-table-row-group row info))))))))
2448 (ert-deftest test-org-export/table-row-number ()
2449 "Test `org-export-table-row-number' specifications."
2450 ;; Standard test. Number is 0-indexed.
2451 (should
2452 (equal '(0 1)
2453 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
2454 (org-element-map tree 'table-row
2455 (lambda (row) (org-export-table-row-number row info)) info))))
2456 ;; Number ignores separators.
2457 (should
2458 (equal '(0 1)
2459 (org-test-with-parsed-data "
2460 | a | b | c |
2461 |---+---+---|
2462 | d | e | f |"
2463 (org-element-map tree 'table-row
2464 (lambda (row) (org-export-table-row-number row info)) info))))
2465 ;; Number ignores special rows.
2466 (should
2467 (equal '(0 1)
2468 (org-test-with-parsed-data "
2469 | / | < | > |
2470 | | b | c |
2471 |---+-----+-----|
2472 | | <c> | <c> |
2473 | | e | f |"
2474 (org-element-map tree 'table-row
2475 (lambda (row) (org-export-table-row-number row info)) info)))))
2477 (ert-deftest test-org-export/table-cell-width ()
2478 "Test `org-export-table-cell-width' specifications."
2479 ;; 1. Width is primarily determined by width cookies. If no cookie
2480 ;; is found, cell's width is nil.
2481 (org-test-with-parsed-data "
2482 | / | <l> | <6> | <l7> |
2483 | | a | b | c |"
2484 (should
2485 (equal
2486 '(nil 6 7)
2487 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2488 (org-element-map tree 'table-cell 'identity info)))))
2489 ;; 2. The last width cookie has precedence.
2490 (org-test-with-parsed-data "
2491 | <6> |
2492 | <7> |
2493 | a |"
2494 (should
2495 (equal
2496 '(7)
2497 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2498 (org-element-map tree 'table-cell 'identity info)))))
2499 ;; 3. Valid width cookies must have a specific row.
2500 (org-test-with-parsed-data "| <6> | cell |"
2501 (should
2502 (equal
2503 '(nil nil)
2504 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2505 (org-element-map tree 'table-cell 'identity))))))
2507 (ert-deftest test-org-export/table-cell-alignment ()
2508 "Test `org-export-table-cell-alignment' specifications."
2509 ;; 1. Alignment is primarily determined by alignment cookies.
2510 (should
2511 (equal '(left center right)
2512 (let ((org-table-number-fraction 0.5)
2513 (org-table-number-regexp "^[0-9]+$"))
2514 (org-test-with-parsed-data "| <l> | <c> | <r> |"
2515 (mapcar (lambda (cell)
2516 (org-export-table-cell-alignment cell info))
2517 (org-element-map tree 'table-cell 'identity))))))
2518 ;; 2. The last alignment cookie has precedence.
2519 (should
2520 (equal '(right right right)
2521 (org-test-with-parsed-data "
2522 | <l8> |
2523 | cell |
2524 | <r9> |"
2525 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
2526 (org-element-map tree 'table-cell 'identity)))))
2527 ;; 3. If there's no cookie, cell's contents determine alignment.
2528 ;; A column mostly made of cells containing numbers will align
2529 ;; its cells to the right.
2530 (should
2531 (equal '(right right right)
2532 (let ((org-table-number-fraction 0.5)
2533 (org-table-number-regexp "^[0-9]+$"))
2534 (org-test-with-parsed-data "
2535 | 123 |
2536 | some text |
2537 | 12345 |"
2538 (mapcar (lambda (cell)
2539 (org-export-table-cell-alignment cell info))
2540 (org-element-map tree 'table-cell 'identity))))))
2541 ;; 4. Otherwise, they will be aligned to the left.
2542 (should
2543 (equal '(left left left)
2544 (org-test-with-parsed-data "
2545 | text |
2546 | some text |
2547 | \alpha |"
2548 (mapcar (lambda (cell)
2549 (org-export-table-cell-alignment cell info))
2550 (org-element-map tree 'table-cell 'identity info))))))
2552 (ert-deftest test-org-export/table-cell-borders ()
2553 "Test `org-export-table-cell-borders' specifications."
2554 ;; 1. Recognize various column groups indicators.
2555 (org-test-with-parsed-data "| / | < | > | <> |"
2556 (should
2557 (equal
2558 '((right bottom top) (left bottom top) (right bottom top)
2559 (right left bottom top))
2560 (mapcar (lambda (cell)
2561 (org-export-table-cell-borders cell info))
2562 (org-element-map tree 'table-cell 'identity)))))
2563 ;; 2. Accept shortcuts to define column groups.
2564 (org-test-with-parsed-data "| / | < | < |"
2565 (should
2566 (equal
2567 '((right bottom top) (right left bottom top) (left bottom top))
2568 (mapcar (lambda (cell)
2569 (org-export-table-cell-borders cell info))
2570 (org-element-map tree 'table-cell 'identity)))))
2571 ;; 3. A valid column groups row must start with a "/".
2572 (org-test-with-parsed-data "
2573 | | < |
2574 | a | b |"
2575 (should
2576 (equal '((top) (top) (bottom) (bottom))
2577 (mapcar (lambda (cell)
2578 (org-export-table-cell-borders cell info))
2579 (org-element-map tree 'table-cell 'identity)))))
2580 ;; 4. Take table rules into consideration.
2581 (org-test-with-parsed-data "
2582 | 1 |
2583 |---|
2584 | 2 |"
2585 (should
2586 (equal '((below top) (bottom above))
2587 (mapcar (lambda (cell)
2588 (org-export-table-cell-borders cell info))
2589 (org-element-map tree 'table-cell 'identity)))))
2590 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
2591 ;; (resp. `bottom' and `below') borders. Any special row is
2592 ;; ignored.
2593 (org-test-with-parsed-data "
2594 |---+----|
2595 | / | |
2596 | | 1 |
2597 |---+----|"
2598 (should
2599 (equal '((bottom below top above))
2600 (last
2601 (mapcar (lambda (cell)
2602 (org-export-table-cell-borders cell info))
2603 (org-element-map tree 'table-cell 'identity)))))))
2605 (ert-deftest test-org-export/table-dimensions ()
2606 "Test `org-export-table-dimensions' specifications."
2607 ;; 1. Standard test.
2608 (org-test-with-parsed-data "
2609 | 1 | 2 | 3 |
2610 | 4 | 5 | 6 |"
2611 (should
2612 (equal '(2 . 3)
2613 (org-export-table-dimensions
2614 (org-element-map tree 'table 'identity info 'first-match) info))))
2615 ;; 2. Ignore horizontal rules and special columns.
2616 (org-test-with-parsed-data "
2617 | / | < | > |
2618 | 1 | 2 | 3 |
2619 |---+---+---|
2620 | 4 | 5 | 6 |"
2621 (should
2622 (equal '(2 . 3)
2623 (org-export-table-dimensions
2624 (org-element-map tree 'table 'identity info 'first-match) info)))))
2626 (ert-deftest test-org-export/table-cell-address ()
2627 "Test `org-export-table-cell-address' specifications."
2628 ;; 1. Standard test: index is 0-based.
2629 (org-test-with-parsed-data "| a | b |"
2630 (should
2631 (equal '((0 . 0) (0 . 1))
2632 (org-element-map tree 'table-cell
2633 (lambda (cell) (org-export-table-cell-address cell info))
2634 info))))
2635 ;; 2. Special column isn't counted, nor are special rows.
2636 (org-test-with-parsed-data "
2637 | / | <> |
2638 | | c |"
2639 (should
2640 (equal '(0 . 0)
2641 (org-export-table-cell-address
2642 (car (last (org-element-map tree 'table-cell 'identity info)))
2643 info))))
2644 ;; 3. Tables rules do not count either.
2645 (org-test-with-parsed-data "
2646 | a |
2647 |---|
2648 | b |
2649 |---|
2650 | c |"
2651 (should
2652 (equal '(2 . 0)
2653 (org-export-table-cell-address
2654 (car (last (org-element-map tree 'table-cell 'identity info)))
2655 info))))
2656 ;; 4. Return nil for special cells.
2657 (org-test-with-parsed-data "| / | a |"
2658 (should-not
2659 (org-export-table-cell-address
2660 (org-element-map tree 'table-cell 'identity nil 'first-match)
2661 info))))
2663 (ert-deftest test-org-export/get-table-cell-at ()
2664 "Test `org-export-get-table-cell-at' specifications."
2665 ;; 1. Address ignores special columns, special rows and rules.
2666 (org-test-with-parsed-data "
2667 | / | <> |
2668 | | a |
2669 |---+----|
2670 | | b |"
2671 (should
2672 (equal '("b")
2673 (org-element-contents
2674 (org-export-get-table-cell-at
2675 '(1 . 0)
2676 (org-element-map tree 'table 'identity info 'first-match)
2677 info)))))
2678 ;; 2. Return value for a non-existent address is nil.
2679 (org-test-with-parsed-data "| a |"
2680 (should-not
2681 (org-export-get-table-cell-at
2682 '(2 . 2)
2683 (org-element-map tree 'table 'identity info 'first-match)
2684 info)))
2685 (org-test-with-parsed-data "| / |"
2686 (should-not
2687 (org-export-get-table-cell-at
2688 '(0 . 0)
2689 (org-element-map tree 'table 'identity info 'first-match)
2690 info))))
2692 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
2693 "Test `org-export-table-cell-starts-colgroup-p' specifications."
2694 ;; 1. A cell at a beginning of a row always starts a column group.
2695 (org-test-with-parsed-data "| a |"
2696 (should
2697 (org-export-table-cell-starts-colgroup-p
2698 (org-element-map tree 'table-cell 'identity info 'first-match)
2699 info)))
2700 ;; 2. Special column should be ignored when determining the
2701 ;; beginning of the row.
2702 (org-test-with-parsed-data "
2703 | / | |
2704 | | a |"
2705 (should
2706 (org-export-table-cell-starts-colgroup-p
2707 (org-element-map tree 'table-cell 'identity info 'first-match)
2708 info)))
2709 ;; 2. Explicit column groups.
2710 (org-test-with-parsed-data "
2711 | / | | < |
2712 | a | b | c |"
2713 (should
2714 (equal
2715 '(yes no yes)
2716 (org-element-map tree 'table-cell
2717 (lambda (cell)
2718 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
2719 info)))))
2721 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
2722 "Test `org-export-table-cell-ends-colgroup-p' specifications."
2723 ;; 1. A cell at the end of a row always ends a column group.
2724 (org-test-with-parsed-data "| a |"
2725 (should
2726 (org-export-table-cell-ends-colgroup-p
2727 (org-element-map tree 'table-cell 'identity info 'first-match)
2728 info)))
2729 ;; 2. Special column should be ignored when determining the
2730 ;; beginning of the row.
2731 (org-test-with-parsed-data "
2732 | / | |
2733 | | a |"
2734 (should
2735 (org-export-table-cell-ends-colgroup-p
2736 (org-element-map tree 'table-cell 'identity info 'first-match)
2737 info)))
2738 ;; 3. Explicit column groups.
2739 (org-test-with-parsed-data "
2740 | / | < | |
2741 | a | b | c |"
2742 (should
2743 (equal
2744 '(yes no yes)
2745 (org-element-map tree 'table-cell
2746 (lambda (cell)
2747 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
2748 info)))))
2750 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
2751 "Test `org-export-table-row-starts-rowgroup-p' specifications."
2752 ;; 1. A row at the beginning of a table always starts a row group.
2753 ;; So does a row following a table rule.
2754 (org-test-with-parsed-data "
2755 | a |
2756 |---|
2757 | b |"
2758 (should
2759 (equal
2760 '(yes no yes)
2761 (org-element-map tree 'table-row
2762 (lambda (row)
2763 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2764 info))))
2765 ;; 2. Special rows should be ignored when determining the beginning
2766 ;; of the row.
2767 (org-test-with-parsed-data "
2768 | / | < |
2769 | | a |
2770 |---+---|
2771 | / | < |
2772 | | b |"
2773 (should
2774 (equal
2775 '(yes no yes)
2776 (org-element-map tree 'table-row
2777 (lambda (row)
2778 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2779 info)))))
2781 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
2782 "Test `org-export-table-row-ends-rowgroup-p' specifications."
2783 ;; 1. A row at the end of a table always ends a row group. So does
2784 ;; a row preceding a table rule.
2785 (org-test-with-parsed-data "
2786 | a |
2787 |---|
2788 | b |"
2789 (should
2790 (equal
2791 '(yes no yes)
2792 (org-element-map tree 'table-row
2793 (lambda (row)
2794 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2795 info))))
2796 ;; 2. Special rows should be ignored when determining the beginning
2797 ;; of the row.
2798 (org-test-with-parsed-data "
2799 | | a |
2800 | / | < |
2801 |---+---|
2802 | | b |
2803 | / | < |"
2804 (should
2805 (equal
2806 '(yes no yes)
2807 (org-element-map tree 'table-row
2808 (lambda (row)
2809 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2810 info)))))
2812 (ert-deftest test-org-export/table-row-starts-header-p ()
2813 "Test `org-export-table-row-starts-header-p' specifications."
2814 ;; 1. Only the row starting the first row group starts the table
2815 ;; header.
2816 (org-test-with-parsed-data "
2817 | a |
2818 | b |
2819 |---|
2820 | c |"
2821 (should
2822 (equal
2823 '(yes no no no)
2824 (org-element-map tree 'table-row
2825 (lambda (row)
2826 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
2827 info))))
2828 ;; 2. A row cannot start an header if there's no header in the
2829 ;; table.
2830 (org-test-with-parsed-data "
2831 | a |
2832 |---|"
2833 (should-not
2834 (org-export-table-row-starts-header-p
2835 (org-element-map tree 'table-row 'identity info 'first-match)
2836 info))))
2838 (ert-deftest test-org-export/table-row-ends-header-p ()
2839 "Test `org-export-table-row-ends-header-p' specifications."
2840 ;; 1. Only the row starting the first row group starts the table
2841 ;; header.
2842 (org-test-with-parsed-data "
2843 | a |
2844 | b |
2845 |---|
2846 | c |"
2847 (should
2848 (equal
2849 '(no yes no no)
2850 (org-element-map tree 'table-row
2851 (lambda (row)
2852 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
2853 info))))
2854 ;; 2. A row cannot start an header if there's no header in the
2855 ;; table.
2856 (org-test-with-parsed-data "
2857 | a |
2858 |---|"
2859 (should-not
2860 (org-export-table-row-ends-header-p
2861 (org-element-map tree 'table-row 'identity info 'first-match)
2862 info))))
2866 ;;; Tables of Contents
2868 (ert-deftest test-org-export/collect-headlines ()
2869 "Test `org-export-collect-headlines' specifications."
2870 ;; Standard test.
2871 (should
2872 (= 2
2873 (length
2874 (org-test-with-parsed-data "* H1\n** H2"
2875 (org-export-collect-headlines info)))))
2876 ;; Do not collect headlines below optional argument.
2877 (should
2878 (= 1
2879 (length
2880 (org-test-with-parsed-data "* H1\n** H2"
2881 (org-export-collect-headlines info 1)))))
2882 ;; Never collect headlines below maximum headline level.
2883 (should
2884 (= 1
2885 (length
2886 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
2887 (org-export-collect-headlines info)))))
2888 (should
2889 (= 1
2890 (length
2891 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
2892 (org-export-collect-headlines info 2))))))
2896 ;;; Templates
2898 (ert-deftest test-org-export/inner-template ()
2899 "Test `inner-template' translator specifications."
2900 (should
2901 (equal "Success!"
2902 (org-test-with-temp-text "* Headline"
2903 (org-export-as
2904 (org-export-create-backend
2905 :transcoders
2906 '((inner-template . (lambda (contents info) "Success!"))
2907 (headline . (lambda (h c i) "Headline"))))))))
2908 ;; Inner template is applied even in a "body-only" export.
2909 (should
2910 (equal "Success!"
2911 (org-test-with-temp-text "* Headline"
2912 (org-export-as
2913 (org-export-create-backend
2914 :transcoders '((inner-template . (lambda (c i) "Success!"))
2915 (headline . (lambda (h c i) "Headline"))))
2916 nil nil 'body-only)))))
2918 (ert-deftest test-org-export/template ()
2919 "Test `template' translator specifications."
2920 (should
2921 (equal "Success!"
2922 (org-test-with-temp-text "* Headline"
2923 (org-export-as
2924 (org-export-create-backend
2925 :transcoders '((template . (lambda (contents info) "Success!"))
2926 (headline . (lambda (h c i) "Headline"))))))))
2927 ;; Template is not applied in a "body-only" export.
2928 (should-not
2929 (equal "Success!"
2930 (org-test-with-temp-text "* Headline"
2931 (org-export-as
2932 (org-export-create-backend
2933 :transcoders '((template . (lambda (contents info) "Success!"))
2934 (headline . (lambda (h c i) "Headline"))))
2935 nil nil 'body-only)))))
2939 ;;; Topology
2941 (ert-deftest test-org-export/get-next-element ()
2942 "Test `org-export-get-next-element' specifications."
2943 ;; Standard test.
2944 (should
2945 (equal "b"
2946 (org-test-with-parsed-data "* Headline\n*a* b"
2947 (org-export-get-next-element
2948 (org-element-map tree 'bold 'identity info t) info))))
2949 ;; Return nil when no previous element.
2950 (should-not
2951 (org-test-with-parsed-data "* Headline\na *b*"
2952 (org-export-get-next-element
2953 (org-element-map tree 'bold 'identity info t) info)))
2954 ;; Non-exportable elements are ignored.
2955 (should-not
2956 (let ((org-export-with-timestamps nil))
2957 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
2958 (org-export-get-next-element
2959 (org-element-map tree 'entity 'identity info t) info))))
2960 ;; Find next element in secondary strings.
2961 (should
2962 (eq 'verbatim
2963 (org-test-with-parsed-data "* a =verb="
2964 (org-element-type
2965 (org-export-get-next-element
2966 (org-element-map tree 'plain-text 'identity info t) info)))))
2967 (should
2968 (eq 'verbatim
2969 (org-test-with-parsed-data "* /italic/ =verb="
2970 (org-element-type
2971 (org-export-get-next-element
2972 (org-element-map tree 'italic 'identity info t) info)))))
2973 ;; Find next element in document keywords.
2974 (should
2975 (eq 'verbatim
2976 (org-test-with-parsed-data "#+TITLE: a =verb="
2977 (org-element-type
2978 (org-export-get-next-element
2979 (org-element-map
2980 (plist-get info :title) 'plain-text 'identity info t) info)))))
2981 ;; Find next element in parsed affiliated keywords.
2982 (should
2983 (eq 'verbatim
2984 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
2985 (org-element-type
2986 (org-export-get-next-element
2987 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2988 ;; With optional argument N, return a list containing all the
2989 ;; following elements.
2990 (should
2991 (equal
2992 '(bold code underline)
2993 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2994 (mapcar 'car
2995 (org-export-get-next-element
2996 (org-element-map tree 'italic 'identity info t) info t)))))
2997 ;; When N is a positive integer, return a list containing up to
2998 ;; N following elements.
2999 (should
3000 (equal
3001 '(bold code)
3002 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3003 (mapcar 'car
3004 (org-export-get-next-element
3005 (org-element-map tree 'italic 'identity info t) info 2))))))
3007 (ert-deftest test-org-export/get-previous-element ()
3008 "Test `org-export-get-previous-element' specifications."
3009 ;; Standard test.
3010 (should
3011 (equal "a "
3012 (org-test-with-parsed-data "* Headline\na *b*"
3013 (org-export-get-previous-element
3014 (org-element-map tree 'bold 'identity info t) info))))
3015 ;; Return nil when no previous element.
3016 (should-not
3017 (org-test-with-parsed-data "* Headline\n*a* b"
3018 (org-export-get-previous-element
3019 (org-element-map tree 'bold 'identity info t) info)))
3020 ;; Non-exportable elements are ignored.
3021 (should-not
3022 (let ((org-export-with-timestamps nil))
3023 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
3024 (org-export-get-previous-element
3025 (org-element-map tree 'entity 'identity info t) info))))
3026 ;; Find previous element in secondary strings.
3027 (should
3028 (eq 'verbatim
3029 (org-test-with-parsed-data "* =verb= a"
3030 (org-element-type
3031 (org-export-get-previous-element
3032 (org-element-map tree 'plain-text 'identity info t) info)))))
3033 (should
3034 (eq 'verbatim
3035 (org-test-with-parsed-data "* =verb= /italic/"
3036 (org-element-type
3037 (org-export-get-previous-element
3038 (org-element-map tree 'italic 'identity info t) info)))))
3039 ;; Find previous element in document keywords.
3040 (should
3041 (eq 'verbatim
3042 (org-test-with-parsed-data "#+TITLE: =verb= a"
3043 (org-element-type
3044 (org-export-get-previous-element
3045 (org-element-map
3046 (plist-get info :title) 'plain-text 'identity info t) info)))))
3047 ;; Find previous element in parsed affiliated keywords.
3048 (should
3049 (eq 'verbatim
3050 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
3051 (org-element-type
3052 (org-export-get-previous-element
3053 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3054 ;; With optional argument N, return a list containing up to
3055 ;; N previous elements.
3056 (should
3057 (equal '(underline italic bold)
3058 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3059 (mapcar 'car
3060 (org-export-get-previous-element
3061 (org-element-map tree 'code 'identity info t) info t)))))
3062 ;; When N is a positive integer, return a list containing up to
3063 ;; N previous elements.
3064 (should
3065 (equal '(italic bold)
3066 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3067 (mapcar 'car
3068 (org-export-get-previous-element
3069 (org-element-map tree 'code 'identity info t) info 2))))))
3072 (provide 'test-ox)
3073 ;;; test-org-export.el end here