ox: Implement `org-export-file-uri'
[org-mode.git] / testing / lisp / test-ox.el
blob386e0242222d2141125e101e031827c16e63ddaf
1 ;;; test-ox.el --- Tests for ox.el
3 ;; Copyright (C) 2012-2015 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.
46 DATA is a string containing the data to be parsed. BODY is the
47 body to execute. Parse tree is available under the `tree'
48 variable, and communication channel under `info'."
49 (declare (debug (form body)) (indent 1))
50 `(org-test-with-temp-text ,data
51 (org-export--delete-comments)
52 (let* ((tree (org-element-parse-buffer))
53 (info (org-export-get-environment)))
54 (org-export--prune-tree tree info)
55 (org-export--remove-uninterpreted-data tree info)
56 (let ((info (org-combine-plists
57 info (org-export-collect-tree-properties tree info))))
58 ,@body))))
62 ;;; Internal Tests
64 (ert-deftest test-org-export/bind-keyword ()
65 "Test reading #+BIND: keywords."
66 ;; Test with `org-export-allow-bind-keywords' set to t.
67 (should
68 (org-test-with-temp-text "#+BIND: test-ox-var value"
69 (let ((org-export-allow-bind-keywords t))
70 (org-export-get-environment)
71 (eq test-ox-var 'value))))
72 ;; Test with `org-export-allow-bind-keywords' set to nil.
73 (should-not
74 (org-test-with-temp-text "#+BIND: test-ox-var value"
75 (let ((org-export-allow-bind-keywords nil))
76 (org-export-get-environment)
77 (boundp 'test-ox-var))))
78 ;; BIND keywords are case-insensitive.
79 (should
80 (org-test-with-temp-text "#+bind: test-ox-var value"
81 (let ((org-export-allow-bind-keywords t))
82 (org-export-get-environment)
83 (eq test-ox-var 'value))))
84 ;; Preserve order of BIND keywords.
85 (should
86 (org-test-with-temp-text "#+BIND: test-ox-var 1\n#+BIND: test-ox-var 2"
87 (let ((org-export-allow-bind-keywords t))
88 (org-export-get-environment)
89 (eq test-ox-var 2))))
90 ;; Read BIND keywords in setup files.
91 (should
92 (org-test-with-temp-text
93 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
94 (let ((org-export-allow-bind-keywords t))
95 (org-export-get-environment)
96 (eq variable 'value))))
97 ;; Verify that bound variables are seen during export.
98 (should
99 (equal "Yes\n"
100 (org-test-with-temp-text "#+BIND: test-ox-var value"
101 (let ((org-export-allow-bind-keywords t))
102 (org-export-as
103 (org-export-create-backend
104 :transcoders
105 '((section . (lambda (s c i)
106 (if (eq test-ox-var 'value) "Yes" "No")))))))))))
108 (ert-deftest test-org-export/parse-option-keyword ()
109 "Test reading all standard #+OPTIONS: items."
110 (should
111 (equal
112 (org-export--parse-option-keyword
113 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
114 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
115 stat:t title:t")
116 '(:headline-levels
117 1 :preserve-breaks t :section-numbers t :time-stamp-file t
118 :with-archived-trees t :with-author t :with-creator t :with-drawers t
119 :with-email t :with-emphasize t :with-entities t :with-fixed-width t
120 :with-footnotes t :with-inlinetasks nil :with-priority t
121 :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
122 :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
123 :with-title t :with-todo-keywords t)))
124 ;; Test some special values.
125 (should
126 (equal
127 (org-export--parse-option-keyword
128 "arch:headline d:(\"TEST\") ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
129 '( :section-numbers
131 :with-archived-trees headline :with-drawers ("TEST")
132 :with-sub-superscript {} :with-toc 1 :with-tags not-in-toc
133 :with-tasks todo :with-timestamps active))))
135 (ert-deftest test-org-export/get-inbuffer-options ()
136 "Test reading all standard export keywords."
137 ;; Properties should follow buffer order.
138 (should
139 (equal
140 (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
141 (org-export--get-inbuffer-options))
142 '(:language "fr" :creator "Me" :email "email")))
143 ;; Test `space' behaviour.
144 (should
145 (equal
146 (let ((back-end (org-export-create-backend
147 :options '((:keyword "KEYWORD" nil nil space)))))
148 (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: spaces"
149 (org-export--get-inbuffer-options back-end)))
150 '(:keyword "With spaces")))
151 ;; Test `newline' behaviour.
152 (should
153 (equal
154 (let ((back-end (org-export-create-backend
155 :options '((:keyword "KEYWORD" nil nil newline)))))
156 (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: two lines"
157 (org-export--get-inbuffer-options back-end)))
158 '(:keyword "With\ntwo lines")))
159 ;; Test `split' behaviour.
160 (should
161 (equal
162 (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
163 (org-export--get-inbuffer-options))
164 '(:select-tags ("a" "b"))))
165 ;; Test `parse' behaviour. `parse' implies `space' but preserve
166 ;; line breaks. Multi-line objects are allowed.
167 (should
168 (org-element-map
169 (org-test-with-temp-text "#+TITLE: *bold*"
170 (plist-get (org-export--get-inbuffer-options) :title))
171 'bold #'identity nil t))
172 (should
173 (equal
174 (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
175 (plist-get (org-export--get-inbuffer-options) :title))
176 '("Some title with spaces")))
177 (should
178 (org-element-map
179 (org-test-with-temp-text "#+TITLE: Some title\\\\\n#+TITLE: with spaces"
180 (plist-get (org-export--get-inbuffer-options) :title))
181 'line-break #'identity nil t))
182 (should
183 (org-element-map
184 (org-test-with-temp-text "#+TITLE: *bold\n#+TITLE: sentence*"
185 (plist-get (org-export--get-inbuffer-options) :title))
186 'bold #'identity nil t))
187 ;; Options set through SETUPFILE.
188 (should
189 (equal
190 (org-test-with-temp-text
191 (format "#+DESCRIPTION: l1
192 #+LANGUAGE: es
193 #+SELECT_TAGS: a
194 #+TITLE: a
195 #+SETUPFILE: \"%s/examples/setupfile.org\"
196 #+LANGUAGE: fr
197 #+SELECT_TAGS: c
198 #+TITLE: c"
199 org-test-dir)
200 (org-export--get-inbuffer-options))
201 '(:language "fr" :select-tags ("a" "b" "c") :title ("a b c"))))
202 ;; More than one property can refer to the same buffer keyword.
203 (should
204 (equal '(:k2 "value" :k1 "value")
205 (let ((backend (org-export-create-backend
206 :options '((:k1 "KEYWORD")
207 (:k2 "KEYWORD")))))
208 (org-test-with-temp-text "#+KEYWORD: value"
209 (org-export--get-inbuffer-options backend)))))
210 ;; Keywords in commented subtrees are ignored.
211 (should-not
212 (equal "Me"
213 (org-test-with-parsed-data "* COMMENT H1\n#+AUTHOR: Me"
214 (plist-get info :author))))
215 (should-not
216 (equal "Mine"
217 (org-test-with-parsed-data "* COMMENT H1\n** H2\n#+EMAIL: Mine"
218 (plist-get info :email)))))
220 (ert-deftest test-org-export/get-subtree-options ()
221 "Test setting options from headline's properties."
222 ;; EXPORT_TITLE.
223 (org-test-with-temp-text "#+TITLE: Title
224 * Headline
225 :PROPERTIES:
226 :EXPORT_TITLE: Subtree Title
227 :END:
228 Paragraph"
229 (forward-line)
230 (should (equal (plist-get (org-export-get-environment nil t) :title)
231 '("Subtree Title"))))
232 :title
233 '("subtree-title")
234 ;; EXPORT_OPTIONS.
235 (org-test-with-temp-text "#+OPTIONS: H:1
236 * Headline
237 :PROPERTIES:
238 :EXPORT_OPTIONS: H:2
239 :END:
240 Paragraph"
241 (forward-line)
242 (should
243 (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
244 ;; EXPORT_DATE.
245 (org-test-with-temp-text "#+DATE: today
246 * Headline
247 :PROPERTIES:
248 :EXPORT_DATE: 29-03-2012
249 :END:
250 Paragraph"
251 (forward-line)
252 (should (equal (plist-get (org-export-get-environment nil t) :date)
253 '("29-03-2012"))))
254 ;; Properties with `split' behaviour are stored as a list of
255 ;; strings.
256 (should
257 (equal '("a" "b")
258 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
259 * Headline
260 :PROPERTIES:
261 :EXPORT_EXCLUDE_TAGS: a b
262 :END:
263 Paragraph"
264 (progn
265 (forward-line)
266 (plist-get (org-export-get-environment nil t) :exclude-tags)))))
267 ;; Handle :PROPERTY+: syntax.
268 (should
269 (equal '("a" "b")
270 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
271 * Headline
272 :PROPERTIES:
273 :EXPORT_EXCLUDE_TAGS: a
274 :EXPORT_EXCLUDE_TAGS+: b
275 :END:
276 Paragraph"
277 (progn
278 (forward-line)
279 (plist-get (org-export-get-environment nil t) :exclude-tags)))))
280 ;; Export properties are case-insensitive.
281 (org-test-with-temp-text "* Headline
282 :PROPERTIES:
283 :EXPORT_Date: 29-03-2012
284 :END:
285 Paragraph"
286 (should (equal (plist-get (org-export-get-environment nil t) :date)
287 '("29-03-2012"))))
288 ;; Still grab correct options when section above is empty.
289 (should
290 (equal '("H1")
291 (org-test-with-temp-text "* H1\n** H11\n** H12"
292 (progn (forward-line 2)
293 (plist-get (org-export-get-environment nil t) :title))))))
295 (ert-deftest test-org-export/set-title ()
296 "Test title setting."
297 ;; Without TITLE keyword.
298 (should
299 (equal
301 (let (org-export-filter-body-functions
302 org-export-filter-final-output-functions)
303 (org-test-with-temp-text "Test"
304 (org-export-as
305 (org-export-create-backend
306 :transcoders
307 '((template . (lambda (text info)
308 (org-element-interpret-data
309 (plist-get info :title)))))))))))
310 ;; With a blank TITLE keyword.
311 (should
312 (equal
314 (let (org-export-filter-body-functions
315 org-export-filter-final-output-functions)
316 (org-test-with-temp-text "#+TITLE:\nTest"
317 (org-export-as
318 (org-export-create-backend
319 :transcoders
320 '((template . (lambda (text info)
321 (org-element-interpret-data
322 (plist-get info :title)))))))))))
323 ;; With a non-empty TITLE keyword.
324 (should
325 (equal
326 "Title"
327 (org-test-with-temp-text "#+TITLE: Title\nTest"
328 (org-export-as
329 (org-export-create-backend
330 :transcoders
331 '((template . (lambda (text info)
332 (org-element-interpret-data
333 (plist-get info :title))))))))))
334 ;; When exporting a subtree, its heading becomes the headline of the
335 ;; document...
336 (should
337 (equal
338 "Headline"
339 (org-test-with-temp-text "* Headline\nBody"
340 (org-export-as
341 (org-export-create-backend
342 :transcoders
343 '((template . (lambda (text info)
344 (org-element-interpret-data
345 (plist-get info :title))))))
346 'subtree))))
347 ;; ... unless there is an EXPORT_TITLE property at the root of the
348 ;; subtree.
349 (should
350 (equal
352 (org-test-with-temp-text
353 "* A\n :PROPERTIES:\n :EXPORT_TITLE: B\n :END:\nBody"
354 (org-export-as
355 (org-export-create-backend
356 :transcoders
357 '((template . (lambda (text info)
358 (org-element-interpret-data
359 (plist-get info :title))))))
360 'subtree)))))
362 (ert-deftest test-org-export/handle-options ()
363 "Test if export options have an impact on output."
364 ;; Test exclude tags for headlines and inlinetasks.
365 (should
366 (equal ""
367 (let (org-export-filter-body-functions
368 org-export-filter-final-output-functions)
369 (org-test-with-temp-text "* Head1 :noexp:"
370 (org-export-as (org-test-default-backend)
371 nil nil nil '(:exclude-tags ("noexp")))))))
372 ;; Test include tags for headlines and inlinetasks.
373 (should
374 (equal "* H2\n** Sub :exp:\n*** Sub Sub\n"
375 (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
376 (let ((org-tags-column 0))
377 (org-export-as (org-test-default-backend)
378 nil nil nil '(:select-tags ("exp")))))))
379 ;; If there is an include tag, ignore the section before the first
380 ;; headline, if any.
381 (should
382 (equal "* H1 :exp:\nBody\n"
383 (org-test-with-temp-text "First section\n* H1 :exp:\nBody"
384 (let ((org-tags-column 0))
385 (org-export-as (org-test-default-backend)
386 nil nil nil '(:select-tags ("exp")))))))
387 (should-not
388 (equal "* H1 :exp:\n"
389 (org-test-with-temp-text "* H1 :exp:\nBody"
390 (let ((org-tags-column 0))
391 (org-export-as (org-test-default-backend)
392 nil nil nil '(:select-tags ("exp")))))))
393 ;; Test mixing include tags and exclude tags.
394 (should
395 (string-match
396 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
397 (org-test-with-temp-text "
398 * Head1 :export:
399 ** Sub-Head1 :noexport:
400 ** Sub-Head2
401 * Head2 :noexport:
402 ** Sub-Head1 :export:"
403 (org-export-as (org-test-default-backend) nil nil nil
404 '(:select-tags ("export") :exclude-tags ("noexport"))))))
405 ;; Ignore tasks.
406 (should
407 (equal ""
408 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
409 org-export-filter-body-functions
410 org-export-filter-final-output-functions)
411 (org-test-with-temp-text "* TODO Head1"
412 (org-export-as (org-test-default-backend)
413 nil nil nil '(:with-tasks nil))))))
414 (should
415 (equal "* TODO Head1\n"
416 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
417 (org-test-with-temp-text "* TODO Head1"
418 (org-export-as (org-test-default-backend)
419 nil nil nil '(:with-tasks t))))))
420 ;; Archived tree.
421 (should
422 (equal ""
423 (let (org-export-filter-body-functions
424 org-export-filter-final-output-functions)
425 (org-test-with-temp-text "* Head1 :archive:"
426 (let ((org-archive-tag "archive"))
427 (org-export-as (org-test-default-backend)
428 nil nil nil '(:with-archived-trees nil)))))))
429 (should
430 (string-match
431 "\\* Head1[ \t]+:archive:"
432 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
433 (let ((org-archive-tag "archive"))
434 (org-export-as (org-test-default-backend) nil nil nil
435 '(:with-archived-trees headline))))))
436 (should
437 (string-match
438 "\\`\\* Head1[ \t]+:archive:\n\\'"
439 (org-test-with-temp-text "* Head1 :archive:"
440 (let ((org-archive-tag "archive"))
441 (org-export-as (org-test-default-backend)
442 nil nil nil '(:with-archived-trees t))))))
443 ;; Clocks.
444 (should
445 (string-match "CLOCK: \\[2012-04-29 .* 10:45\\]"
446 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
447 (org-export-as (org-test-default-backend)
448 nil nil nil '(:with-clocks t)))))
449 (should
450 (equal ""
451 (let (org-export-filter-body-functions
452 org-export-filter-final-output-functions)
453 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
454 (org-export-as (org-test-default-backend)
455 nil nil nil '(:with-clocks nil))))))
456 ;; Drawers.
457 (should
458 (equal ""
459 (let (org-export-filter-body-functions
460 org-export-filter-final-output-functions)
461 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
462 (org-export-as (org-test-default-backend)
463 nil nil nil '(:with-drawers nil))))))
464 (should
465 (equal ":TEST:\ncontents\n:END:\n"
466 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
467 (org-export-as (org-test-default-backend)
468 nil nil nil '(:with-drawers t)))))
469 (should
470 (equal ":FOO:\nkeep\n:END:\n"
471 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
472 (org-export-as (org-test-default-backend)
473 nil nil nil '(:with-drawers ("FOO"))))))
474 (should
475 (equal ":FOO:\nkeep\n:END:\n"
476 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
477 (org-export-as (org-test-default-backend)
478 nil nil nil '(:with-drawers (not "BAR"))))))
479 ;; Fixed-width.
480 (should
481 (equal ": A\n"
482 (org-test-with-temp-text ": A"
483 (org-export-as (org-test-default-backend) nil nil nil
484 '(:with-fixed-width t)))))
485 (should
486 (equal ""
487 (let (org-export-filter-body-functions
488 org-export-filter-final-output-functions)
489 (org-test-with-temp-text ": A"
490 (org-export-as (org-test-default-backend) nil nil nil
491 '(:with-fixed-width nil))))))
492 ;; Footnotes.
493 (should
494 (equal "Footnote?"
495 (let ((org-footnote-section nil))
496 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
497 (org-trim (org-export-as (org-test-default-backend)
498 nil nil nil '(:with-footnotes nil)))))))
499 (should
500 (equal "Footnote?[fn:1]\n\n[fn:1] Def"
501 (let ((org-footnote-section nil))
502 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
503 (org-trim (org-export-as (org-test-default-backend)
504 nil nil nil '(:with-footnotes t)))))))
505 ;; Inlinetasks.
506 (when (featurep 'org-inlinetask)
507 (should
508 (equal
510 (let ((org-inlinetask-min-level 15)
511 org-export-filter-body-functions
512 org-export-filter-final-output-functions)
513 (org-test-with-temp-text "*************** Task"
514 (org-export-as (org-test-default-backend)
515 nil nil nil '(:with-inlinetasks nil))))))
516 (should
517 (equal
519 (let ((org-inlinetask-min-level 15)
520 org-export-filter-body-functions
521 org-export-filter-final-output-functions)
522 (org-test-with-temp-text
523 "*************** Task\nContents\n*************** END"
524 (org-export-as (org-test-default-backend)
525 nil nil nil '(:with-inlinetasks nil)))))))
526 ;; Plannings.
527 (should
528 (string-match
529 "* H\nCLOSED: \\[2012-04-29 .* 10:45\\]"
530 (let ((org-closed-string "CLOSED:"))
531 (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
532 (org-export-as (org-test-default-backend)
533 nil nil nil '(:with-planning t))))))
534 (should
535 (equal "* H\n"
536 (let ((org-closed-string "CLOSED:"))
537 (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
538 (org-export-as (org-test-default-backend)
539 nil nil nil '(:with-planning nil))))))
540 ;; Property Drawers.
541 (should
542 (equal "* H1\n"
543 (org-test-with-temp-text
544 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
545 (org-export-as (org-test-default-backend)
546 nil nil nil '(:with-properties nil)))))
547 (should
548 (equal "* H1\n:PROPERTIES:\n:PROP: value\n:END:\n"
549 (org-test-with-temp-text
550 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
551 (org-export-as (org-test-default-backend)
552 nil nil nil '(:with-properties t)))))
553 (should
554 (equal "* H1\n:PROPERTIES:\n:B: 2\n:END:\n"
555 (org-test-with-temp-text
556 "* H1\n :PROPERTIES:\n :A: 1\n :B: 2\n:END:"
557 (org-export-as (org-test-default-backend)
558 nil nil nil '(:with-properties ("B"))))))
559 ;; Statistics cookies.
560 (should
561 (equal ""
562 (let (org-export-filter-body-functions
563 org-export-filter-final-output-functions)
564 (org-trim
565 (org-test-with-temp-text "[0/0]"
566 (org-export-as (org-test-default-backend)
567 nil nil nil '(:with-statistics-cookies nil)))))))
568 ;; Tables.
569 (should
570 (equal "| A |\n"
571 (org-test-with-temp-text "| A |"
572 (org-export-as (org-test-default-backend) nil nil nil
573 '(:with-tables t)))))
574 (should
575 (equal ""
576 (let (org-export-filter-body-functions
577 org-export-filter-final-output-functions)
578 (org-test-with-temp-text "| A |"
579 (org-export-as (org-test-default-backend) nil nil nil
580 '(:with-tables nil)))))))
582 (ert-deftest test-org-export/with-timestamps ()
583 "Test `org-export-with-timestamps' specifications."
584 ;; t value.
585 (should
586 (string-match
587 "\\[2012-04-29 .*? 10:45\\]<2012-04-29 .*? 10:45>"
588 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
589 (org-export-as (org-test-default-backend)
590 nil nil nil '(:with-timestamps t)))))
591 ;; nil value.
592 (should
593 (equal
595 (let (org-export-filter-body-functions
596 org-export-filter-final-output-functions)
597 (org-trim
598 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
599 (org-export-as (org-test-default-backend)
600 nil nil nil '(:with-timestamps nil)))))))
601 ;; `active' value.
602 (should
603 (string-match
604 "<2012-03-29 .*?>\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
605 (org-test-with-temp-text
606 "<2012-03-29 Thu>[2012-03-29 Thu]
608 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
609 (org-export-as (org-test-default-backend)
610 nil nil nil '(:with-timestamps active)))))
611 ;; `inactive' value.
612 (should
613 (string-match
614 "\\[2012-03-29 .*?\\]\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
615 (org-test-with-temp-text
616 "<2012-03-29 Thu>[2012-03-29 Thu]
618 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
619 (org-export-as (org-test-default-backend)
620 nil nil nil '(:with-timestamps inactive))))))
622 (ert-deftest test-org-export/comment-tree ()
623 "Test if export process ignores commented trees."
624 (should
625 (equal ""
626 (let (org-export-filter-body-functions
627 org-export-filter-final-output-functions)
628 (org-test-with-temp-text "* COMMENT Head1"
629 (org-export-as (org-test-default-backend)))))))
631 (ert-deftest test-org-export/uninterpreted ()
632 "Test handling of uninterpreted elements."
633 ;; Entities.
634 (should
635 (equal "dummy\n"
636 (org-test-with-temp-text "\\alpha"
637 (org-export-as
638 (org-export-create-backend
639 :transcoders '((entity . (lambda (e c i) "dummy"))
640 (paragraph . (lambda (p c i) c))
641 (section . (lambda (s c i) c))))
642 nil nil nil '(:with-entities t)))))
643 (should
644 (equal "\\alpha\n"
645 (org-test-with-temp-text "\\alpha"
646 (org-export-as
647 (org-export-create-backend
648 :transcoders '((entity . (lambda (e c i) "dummy"))
649 (paragraph . (lambda (p c i) c))
650 (section . (lambda (s c i) c))))
651 nil nil nil '(:with-entities nil)))))
652 ;; Emphasis.
653 (should
654 (equal "dummy\n"
655 (org-test-with-temp-text "*bold*"
656 (org-export-as
657 (org-export-create-backend
658 :transcoders '((bold . (lambda (b c i) "dummy"))
659 (paragraph . (lambda (p c i) c))
660 (section . (lambda (s c i) c))))
661 nil nil nil '(:with-emphasize t)))))
662 (should
663 (equal "*bold*\n"
664 (org-test-with-temp-text "*bold*"
665 (org-export-as
666 (org-export-create-backend
667 :transcoders '((bold . (lambda (b c i) "dummy"))
668 (paragraph . (lambda (p c i) c))
669 (section . (lambda (s c i) c))))
670 nil nil nil '(:with-emphasize nil)))))
671 ;; LaTeX environment.
672 (should
673 (equal "dummy\n"
674 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
675 (org-export-as
676 (org-export-create-backend
677 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
678 (section . (lambda (s c i) c))))
679 nil nil nil '(:with-latex t)))))
680 (should
681 (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
682 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
683 (org-export-as
684 (org-export-create-backend
685 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
686 (section . (lambda (s c i) c))))
687 nil nil nil '(:with-latex verbatim)))))
688 ;; LaTeX fragment.
689 (should
690 (equal "dummy\n"
691 (org-test-with-temp-text "$1$"
692 (org-export-as
693 (org-export-create-backend
694 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
695 (paragraph . (lambda (p c i) c))
696 (section . (lambda (s c i) c))))
697 nil nil nil '(:with-latex t)))))
698 (should
699 (equal "$1$\n"
700 (org-test-with-temp-text "$1$"
701 (org-export-as
702 (org-export-create-backend
703 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
704 (paragraph . (lambda (p c i) c))
705 (section . (lambda (s c i) c))))
706 nil nil nil '(:with-latex verbatim)))))
707 ;; Sub/superscript.
708 (should
709 (equal "adummy\n"
710 (org-test-with-temp-text "a_b"
711 (org-export-as
712 (org-export-create-backend
713 :transcoders '((subscript . (lambda (s c i) "dummy"))
714 (paragraph . (lambda (p c i) c))
715 (section . (lambda (s c i) c))))
716 nil nil nil '(:with-sub-superscript t)))))
717 (should
718 (equal "a_b\n"
719 (org-test-with-temp-text "a_b"
720 (org-export-as
721 (org-export-create-backend
722 :transcoders '((subscript . (lambda (s c i) "dummy"))
723 (paragraph . (lambda (p c i) c))
724 (section . (lambda (s c i) c))))
725 nil nil nil '(:with-sub-superscript nil)))))
726 (should
727 (equal "a_b\n"
728 (org-test-with-temp-text "a_b"
729 (org-export-as
730 (org-export-create-backend
731 :transcoders '((subscript . (lambda (s c i) "dummy"))
732 (paragraph . (lambda (p c i) c))
733 (section . (lambda (s c i) c))))
734 nil nil nil '(:with-sub-superscript {})))))
735 (should
736 (equal "adummy\n"
737 (org-test-with-temp-text "a_{b}"
738 (org-export-as
739 (org-export-create-backend
740 :transcoders '((subscript . (lambda (s c i) "dummy"))
741 (paragraph . (lambda (p c i) c))
742 (section . (lambda (s c i) c))))
743 nil nil nil '(:with-sub-superscript {})))))
744 ;; Also handle uninterpreted objects in title.
745 (should
746 (equal "a_b"
747 (org-test-with-temp-text "#+TITLE: a_b"
748 (org-export-as
749 (org-export-create-backend
750 :transcoders
751 '((subscript . (lambda (s c i) "dummy"))
752 (template . (lambda (c i) (org-export-data
753 (plist-get i :title) i)))
754 (section . (lambda (s c i) c))))
755 nil nil nil '(:with-sub-superscript nil)))))
756 ;; Special case: multiples uninterpreted objects in a row.
757 (should
758 (equal "a_b_c_d\n"
759 (org-test-with-temp-text "a_b_c_d"
760 (org-export-as
761 (org-export-create-backend
762 :transcoders '((subscript . (lambda (s c i) "dummy"))
763 (paragraph . (lambda (p c i) c))
764 (section . (lambda (s c i) c))))
765 nil nil nil '(:with-sub-superscript {}))))))
767 (ert-deftest test-org-export/export-scope ()
768 "Test all export scopes."
769 (org-test-with-temp-text "
770 * Head1
771 ** Head2
772 text
773 *** Head3"
774 ;; Subtree.
775 (forward-line 3)
776 (should (equal (org-export-as (org-test-default-backend) 'subtree)
777 "text\n*** Head3\n"))
778 ;; Visible.
779 (goto-char (point-min))
780 (forward-line)
781 (org-cycle)
782 (should (equal (org-export-as (org-test-default-backend) nil 'visible)
783 "* Head1\n"))
784 ;; Region.
785 (goto-char (point-min))
786 (forward-line 3)
787 (transient-mark-mode 1)
788 (push-mark (point) t t)
789 (goto-char (point-at-eol))
790 (should (equal (org-export-as (org-test-default-backend)) "text\n")))
791 ;; Subtree with a code block calling another block outside.
792 (let ((org-export-babel-evaluate t))
793 (should
794 (equal ": 3\n"
795 (org-test-with-temp-text "
796 * Head1
797 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
798 <<test>>
799 #+END_SRC
800 * Head2
801 #+NAME: test
802 #+BEGIN_SRC emacs-lisp
803 \(+ 1 2)
804 #+END_SRC"
805 (forward-line 1)
806 (org-export-as (org-test-default-backend) 'subtree)))))
807 ;; Body only.
808 (let ((backend (org-test-default-backend)))
809 (setf (org-export-backend-transcoders backend)
810 (cons '(template . (lambda (body i)
811 (format "BEGIN\n%sEND" body)))
812 (org-export-backend-transcoders backend)))
813 (org-test-with-temp-text "Text"
814 (should (equal (org-export-as backend nil nil 'body-only)
815 "Text\n"))
816 (should (equal (org-export-as backend) "BEGIN\nText\nEND")))))
818 (ert-deftest test-org-export/output-file-name ()
819 "Test `org-export-output-file-name' specifications."
820 ;; Export from a file: name is built from original file name.
821 (should
822 (org-test-with-temp-text-in-file "Test"
823 (equal (concat (file-name-as-directory ".")
824 (file-name-nondirectory
825 (file-name-sans-extension (buffer-file-name))))
826 (file-name-sans-extension (org-export-output-file-name ".ext")))))
827 ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
828 (should
829 (org-test-with-temp-text-in-file
830 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
831 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
832 ;; From a buffer not associated to a file, too.
833 (should
834 (org-test-with-temp-text
835 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
836 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
837 ;; When provided name is absolute, preserve it.
838 (should
839 (org-test-with-temp-text
840 (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
841 (expand-file-name "test"))
842 (file-name-absolute-p (org-export-output-file-name ".ext" t))))
843 ;; When PUB-DIR argument is provided, use it.
844 (should
845 (org-test-with-temp-text-in-file "Test"
846 (equal (file-name-directory
847 (org-export-output-file-name ".ext" nil "dir/"))
848 "dir/")))
849 ;; When returned name would overwrite original file, add EXTENSION
850 ;; another time.
851 (should
852 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
853 (equal (org-export-output-file-name ".org") "./normal.org.org"))))
855 (ert-deftest test-org-export/expand-include ()
856 "Test file inclusion in an Org buffer."
857 ;; Error when file isn't specified.
858 (should-error
859 (org-test-with-temp-text "#+INCLUDE: dummy.org"
860 (org-export-expand-include-keyword)))
861 ;; Full insertion with recursive inclusion.
862 (should
863 (equal
864 (with-temp-buffer
865 (insert-file
866 (expand-file-name "examples/include.org" org-test-dir))
867 (replace-regexp-in-string
868 (regexp-quote "#+INCLUDE: \"include2.org\"")
869 "Success!" (buffer-string)))
870 (org-test-with-temp-text
871 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
872 (org-export-expand-include-keyword)
873 (buffer-string))))
874 ;; Localized insertion.
875 (org-test-with-temp-text
876 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
877 org-test-dir)
878 (org-export-expand-include-keyword)
879 (should (equal (buffer-string)
880 "Small Org file with an include keyword.\n")))
881 ;; Insertion with constraints on headlines level.
882 (should
883 (equal
884 "* Top heading\n** Heading\nbody\n"
885 (org-test-with-temp-text
886 (format
887 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\""
888 org-test-dir)
889 (org-export-expand-include-keyword)
890 (buffer-string))))
891 (should
892 (equal
893 "* Top heading\n* Heading\nbody\n"
894 (org-test-with-temp-text
895 (format
896 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\" :minlevel 1"
897 org-test-dir)
898 (org-export-expand-include-keyword)
899 (buffer-string))))
900 ;; Inclusion within an example block.
901 (should
902 (equal
903 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
904 (org-test-with-temp-text
905 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
906 org-test-dir)
907 (org-export-expand-include-keyword)
908 (buffer-string))))
909 ;; Inclusion within a src-block.
910 (should
911 (equal
912 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
913 (org-test-with-temp-text
914 (format
915 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
916 org-test-dir)
917 (org-export-expand-include-keyword)
918 (buffer-string))))
919 ;; Inclusion within an html export-block.
920 (should
921 (equal
922 "#+BEGIN_HTML\n<p>HTML!</p>\n#+END_HTML\n"
923 (org-test-with-temp-text
924 (format
925 "#+INCLUDE: \"%s/examples/include.html\" HTML"
926 org-test-dir)
927 (org-export-expand-include-keyword)
928 (buffer-string))))
929 ;; Inclusion within an center paragraph
930 (should
931 (equal
932 "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
933 (org-test-with-temp-text
934 (format
935 "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
936 org-test-dir)
937 (org-export-expand-include-keyword)
938 (buffer-string))))
939 ;; Footnotes labels are local to each included file.
940 (should
941 (= 6
942 (length
943 (delete-dups
944 (let ((contents "
945 Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote].
946 \[fn:1] Footnote 1
947 \[fn:test] Footnote \"test\""))
948 (org-test-with-temp-text-in-file contents
949 (let ((file1 (buffer-file-name)))
950 (org-test-with-temp-text-in-file contents
951 (let ((file2 (buffer-file-name)))
952 (org-test-with-temp-text
953 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
954 file1 file2)
955 (org-export-expand-include-keyword)
956 (org-element-map (org-element-parse-buffer)
957 'footnote-reference
958 (lambda (ref)
959 (org-element-property :label ref)))))))))))))
960 ;; Footnotes labels are not local to each include keyword.
961 (should
962 (= 4
963 (length
964 (delete-dups
965 (let ((contents "
966 Footnotes[fn:1], [fn:test], [2] and [fn:inline:anonymous footnote].
967 \[fn:1] Footnote 1
968 \[2] Footnote 2
969 \[fn:test] Footnote \"test\""))
970 (org-test-with-temp-text-in-file contents
971 (let ((file (buffer-file-name)))
972 (org-test-with-temp-text
973 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
974 (org-export-expand-include-keyword)
975 (org-element-map (org-element-parse-buffer)
976 'footnote-reference
977 (lambda (ref) (org-element-property :label ref)))))))))))
978 ;; Footnotes are supported by :lines-like elements and unnecessary
979 ;; footnotes are dropped.
980 (should
981 (= 4
982 (length
983 (delete-dups
984 (let ((contents "
985 * foo
986 Footnotes[fn:1]
987 * bar
988 Footnotes[fn:2], foot[fn:test], digit only[3], and [fn:inline:anonymous footnote]
990 \[fn:1] Footnote 1
991 \[fn:2] Footnote 1
992 * Footnotes
993 \[fn:test] Footnote \"test\"
994 \[3] Footnote 3
996 (org-test-with-temp-text-in-file contents
997 (let ((file (buffer-file-name)))
998 (org-test-with-temp-text
999 (format "#+INCLUDE: \"%s::*bar\"
1000 " file)
1001 (org-export-expand-include-keyword)
1002 (org-element-map (org-element-parse-buffer)
1003 'footnote-definition
1004 (lambda (ref) (org-element-property :label ref)))))))))))
1005 ;; If only-contents is non-nil only include contents of element.
1006 (should
1007 (equal
1008 "body\n"
1009 (org-test-with-temp-text
1010 (concat
1011 (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" " org-test-dir)
1012 ":only-contents t")
1013 (org-export-expand-include-keyword)
1014 (buffer-string))))
1015 ;; Headings can be included via CUSTOM_ID.
1016 (should
1017 (org-test-with-temp-text
1018 (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
1019 (org-export-expand-include-keyword)
1020 (goto-char (point-min))
1021 (looking-at "* Another heading")))
1022 ;; Named objects can be included.
1023 (should
1024 (equal
1025 "| 1 |\n"
1026 (org-test-with-temp-text
1027 (format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t" org-test-dir)
1028 (org-export-expand-include-keyword)
1029 (buffer-string))))
1030 ;; Including non-existing elements should result in an error.
1031 (should-error
1032 (org-test-with-temp-text
1033 (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\"" org-test-dir)
1034 (org-export-expand-include-keyword)))
1035 ;; Lines work relatively to an included element.
1036 (should
1037 (equal
1038 "2\n3\n"
1039 (org-test-with-temp-text
1040 (format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t :lines \"2-3\"" org-test-dir)
1041 (org-export-expand-include-keyword)
1042 (buffer-string))))
1043 ;; Properties should be dropped from headlines.
1044 (should
1045 (equal
1046 (org-test-with-temp-text
1047 (format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t" org-test-dir)
1048 (org-export-expand-include-keyword)
1049 (buffer-string))
1050 (org-test-with-temp-text
1051 (format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
1052 (org-export-expand-include-keyword)
1053 (buffer-string))))
1054 ;; Properties should be dropped, drawers should not be.
1055 (should
1056 (equal
1057 ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
1058 (org-test-with-temp-text
1059 (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t" org-test-dir)
1060 (org-export-expand-include-keyword)
1061 (buffer-string))))
1062 ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
1063 (should
1064 (org-every (lambda (level) (zerop (1- level)))
1065 (org-test-with-temp-text
1066 (concat
1067 (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n" org-test-dir)
1068 (format "#+INCLUDE: \"%s/examples/include.org::*Heading\"" org-test-dir))
1069 (org-export-expand-include-keyword)
1070 (org-element-map (org-element-parse-buffer) 'headline
1071 (lambda (head) (org-element-property :level head))))))
1072 ;; INCLUDE does not insert induced :minlevel for src-blocks.
1073 (should-not
1074 (equal
1075 (org-test-with-temp-text
1076 (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp" org-test-dir)
1077 (org-export-expand-include-keyword)
1078 (buffer-string))
1079 (org-test-with-temp-text
1080 (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1" org-test-dir)
1081 (org-export-expand-include-keyword)
1082 (buffer-string))))
1083 ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
1084 (should
1085 (org-test-with-temp-text-in-file
1086 (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
1087 (narrow-to-region (point) (point-max))
1088 (org-export-expand-include-keyword)
1089 (eq 1 (org-current-level))))
1090 ;; If :minlevel is present do not alter it.
1091 (should
1092 (org-test-with-temp-text
1093 (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 3" org-test-dir)
1094 (narrow-to-region (point) (point-max))
1095 (org-export-expand-include-keyword)
1096 (eq 3 (org-current-level)))))
1098 (ert-deftest test-org-export/expand-macro ()
1099 "Test macro expansion in an Org buffer."
1100 ;; Standard macro expansion.
1101 (should
1102 (equal "#+MACRO: macro1 value\nvalue\n"
1103 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
1104 (org-export-as (org-test-default-backend)))))
1105 ;; Allow macro in parsed keywords and associated properties.
1106 ;; Standard macro expansion.
1107 (should
1108 (string-match
1109 "#\\+K: value"
1110 (let ((backend (org-export-create-backend
1111 :parent 'org
1112 :options '((:k "K" nil nil parse)))))
1113 (org-test-with-temp-text "#+MACRO: macro value\n#+K: {{{macro}}}"
1114 (org-export-as backend)))))
1115 (should
1116 (string-match
1117 ":EXPORT_K: v"
1118 (let ((backend (org-export-create-backend
1119 :parent 'org
1120 :options '((:k "K" nil nil parse)))))
1121 (org-test-with-temp-text
1122 "#+MACRO: m v\n* H\n:PROPERTIES:\n:EXPORT_K: {{{m}}}\n:END:"
1123 (org-export-as backend nil nil nil '(:with-properties t))))))
1124 ;; Expand specific macros.
1125 (should
1126 (equal "me 2012-03-29 me@here Title\n"
1127 (org-test-with-temp-text
1129 #+TITLE: Title
1130 #+DATE: 2012-03-29
1131 #+AUTHOR: me
1132 #+EMAIL: me@here
1133 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
1134 (let ((output (org-export-as (org-test-default-backend))))
1135 (substring output (string-match ".*\n\\'" output))))))
1136 ;; Expand specific macros when property contained a regular macro
1137 ;; already.
1138 (should
1139 (equal "value\n"
1140 (org-test-with-temp-text "
1141 #+MACRO: macro1 value
1142 #+TITLE: {{{macro1}}}
1143 {{{title}}}"
1144 (let ((output (org-export-as (org-test-default-backend))))
1145 (substring output (string-match ".*\n\\'" output))))))
1146 ;; Expand macros with templates in included files.
1147 (should
1148 (equal "success\n"
1149 (org-test-with-temp-text
1150 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
1151 {{{included-macro}}}" org-test-dir)
1152 (let ((output (org-export-as (org-test-default-backend))))
1153 (substring output (string-match ".*\n\\'" output))))))
1154 ;; Date macro takes a optional formatting argument
1155 (should
1156 (equal "09-02-15\n"
1157 (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02-09>"
1158 (org-export-as (org-test-default-backend)))))
1159 ;; Only single timestamps are formatted
1160 (should
1161 (equal "<2015-02x-09>\n"
1162 (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02x-09>"
1163 (org-export-as (org-test-default-backend)))))
1164 ;; Throw an error when a macro definition is missing.
1165 (should-error
1166 (org-test-with-temp-text "{{{missing}}}"
1167 (org-export-as (org-test-default-backend))))
1168 ;; Macros defined in commented subtrees are ignored.
1169 (should-error
1170 (org-test-with-temp-text
1171 "* COMMENT H\n#+MACRO: macro1\n* H2\nvalue\n{{{macro1}}}"
1172 (org-export-as (org-test-default-backend))))
1173 (should-error
1174 (org-test-with-temp-text
1175 "* COMMENT H\n** H2\n#+MACRO: macro1\n* H3\nvalue\n{{{macro1}}}"
1176 (org-export-as (org-test-default-backend)))))
1178 (ert-deftest test-org-export/before-processing-hook ()
1179 "Test `org-export-before-processing-hook'."
1180 (should
1181 (equal
1182 "#+MACRO: mac val\nTest\n"
1183 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
1184 (let ((org-export-before-processing-hook
1185 '((lambda (backend)
1186 (while (re-search-forward "{{{" nil t)
1187 (let ((object (org-element-context)))
1188 (when (eq (org-element-type object) 'macro)
1189 (delete-region
1190 (org-element-property :begin object)
1191 (org-element-property :end object)))))))))
1192 (org-export-as (org-test-default-backend)))))))
1194 (ert-deftest test-org-export/before-parsing-hook ()
1195 "Test `org-export-before-parsing-hook'."
1196 (should
1197 (equal "Body 1\nBody 2\n"
1198 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
1199 (let ((org-export-before-parsing-hook
1200 '((lambda (backend)
1201 (goto-char (point-min))
1202 (while (re-search-forward org-outline-regexp-bol nil t)
1203 (delete-region
1204 (point-at-bol) (progn (forward-line) (point))))))))
1205 (org-export-as (org-test-default-backend)))))))
1209 ;;; Affiliated Keywords
1211 (ert-deftest test-org-export/read-attribute ()
1212 "Test `org-export-read-attribute' specifications."
1213 ;; Standard test.
1214 (should
1215 (equal
1216 (org-export-read-attribute
1217 :attr_html
1218 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
1219 (org-element-at-point)))
1220 '(:a "1" :b "2")))
1221 ;; Return nil on empty attribute.
1222 (should-not
1223 (org-export-read-attribute
1224 :attr_html
1225 (org-test-with-temp-text "Paragraph" (org-element-at-point))))
1226 ;; Return nil on "nil" string.
1227 (should
1228 (equal '(:a nil :b nil)
1229 (org-export-read-attribute
1230 :attr_html
1231 (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
1232 (org-element-at-point)))))
1233 ;; Return nil on empty string.
1234 (should
1235 (equal '(:a nil :b nil)
1236 (org-export-read-attribute
1237 :attr_html
1238 (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
1239 (org-element-at-point)))))
1240 ;; Return empty string when value is "".
1241 (should
1242 (equal '(:a "")
1243 (org-export-read-attribute
1244 :attr_html
1245 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
1246 (org-element-at-point)))))
1247 ;; Return \"\" when value is """".
1248 (should
1249 (equal '(:a "\"\"")
1250 (org-export-read-attribute
1251 :attr_html
1252 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
1253 (org-element-at-point)))))
1254 ;; Ignore text before first property.
1255 (should-not
1256 (member "ignore"
1257 (org-export-read-attribute
1258 :attr_html
1259 (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
1260 (org-element-at-point))))))
1262 (ert-deftest test-org-export/get-caption ()
1263 "Test `org-export-get-caption' specifications."
1264 ;; Without optional argument, return long caption
1265 (should
1266 (equal
1267 '("l")
1268 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1269 (org-export-get-caption (org-element-at-point)))))
1270 ;; With optional argument, return short caption.
1271 (should
1272 (equal
1273 '("s")
1274 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1275 (org-export-get-caption (org-element-at-point) t))))
1276 ;; Multiple lines are separated by white spaces.
1277 (should
1278 (equal
1279 '("a" " " "b")
1280 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
1281 (org-export-get-caption (org-element-at-point))))))
1285 ;;; Back-End Tools
1287 (ert-deftest test-org-export/define-backend ()
1288 "Test back-end definition and accessors."
1289 ;; Translate table.
1290 (should
1291 (equal '((headline . my-headline-test))
1292 (let (org-export--registered-backends)
1293 (org-export-define-backend 'test '((headline . my-headline-test)))
1294 (org-export-get-all-transcoders 'test))))
1295 ;; Filters.
1296 (should
1297 (equal '((:filter-headline . my-filter))
1298 (let (org-export--registered-backends)
1299 (org-export-define-backend 'test
1300 '((headline . my-headline-test))
1301 :filters-alist '((:filter-headline . my-filter)))
1302 (org-export-backend-filters (org-export-get-backend 'test)))))
1303 ;; Options.
1304 (should
1305 (equal '((:prop value))
1306 (let (org-export--registered-backends)
1307 (org-export-define-backend 'test
1308 '((headline . my-headline-test))
1309 :options-alist '((:prop value)))
1310 (org-export-backend-options (org-export-get-backend 'test)))))
1311 ;; Menu.
1312 (should
1313 (equal '(?k "Test Export" test)
1314 (let (org-export--registered-backends)
1315 (org-export-define-backend 'test
1316 '((headline . my-headline-test))
1317 :menu-entry '(?k "Test Export" test))
1318 (org-export-backend-menu (org-export-get-backend 'test)))))
1319 ;; Export Blocks.
1320 (should
1321 (equal '(("TEST" . org-element-export-block-parser))
1322 (let (org-export--registered-backends org-element-block-name-alist)
1323 (org-export-define-backend 'test
1324 '((headline . my-headline-test))
1325 :export-block '("test"))
1326 org-element-block-name-alist))))
1328 (ert-deftest test-org-export/define-derived-backend ()
1329 "Test `org-export-define-derived-backend' specifications."
1330 ;; Error when parent back-end is not defined.
1331 (should-error
1332 (let (org-export--registered-backends)
1333 (org-export-define-derived-backend 'test 'parent)))
1334 ;; Append translation table to parent's.
1335 (should
1336 (equal '((:headline . test) (:headline . parent))
1337 (let (org-export--registered-backends)
1338 (org-export-define-backend 'parent '((:headline . parent)))
1339 (org-export-define-derived-backend 'test 'parent
1340 :translate-alist '((:headline . test)))
1341 (org-export-get-all-transcoders 'test))))
1342 ;; Options defined in the new back have priority over those defined
1343 ;; in parent.
1344 (should
1345 (eq 'test
1346 (let (org-export--registered-backends)
1347 (org-export-define-backend 'parent
1348 '((:headline . parent))
1349 :options-alist '((:a nil nil 'parent)))
1350 (org-export-define-derived-backend 'test 'parent
1351 :options-alist '((:a nil nil 'test)))
1352 (plist-get (org-export--get-global-options
1353 (org-export-get-backend 'test))
1354 :a)))))
1356 (ert-deftest test-org-export/derived-backend-p ()
1357 "Test `org-export-derived-backend-p' specifications."
1358 ;; Non-nil with direct match.
1359 (should
1360 (let (org-export--registered-backends)
1361 (org-export-define-backend 'test '((headline . test)))
1362 (org-export-derived-backend-p 'test 'test)))
1363 (should
1364 (let (org-export--registered-backends)
1365 (org-export-define-backend 'test '((headline . test)))
1366 (org-export-define-derived-backend 'test2 'test)
1367 (org-export-derived-backend-p 'test2 'test2)))
1368 ;; Non-nil with a direct parent.
1369 (should
1370 (let (org-export--registered-backends)
1371 (org-export-define-backend 'test '((headline . test)))
1372 (org-export-define-derived-backend 'test2 'test)
1373 (org-export-derived-backend-p 'test2 'test)))
1374 ;; Non-nil with an indirect parent.
1375 (should
1376 (let (org-export--registered-backends)
1377 (org-export-define-backend 'test '((headline . test)))
1378 (org-export-define-derived-backend 'test2 'test)
1379 (org-export-define-derived-backend 'test3 'test2)
1380 (org-export-derived-backend-p 'test3 'test)))
1381 ;; Nil otherwise.
1382 (should-not
1383 (let (org-export--registered-backends)
1384 (org-export-define-backend 'test '((headline . test)))
1385 (org-export-define-backend 'test2 '((headline . test2)))
1386 (org-export-derived-backend-p 'test2 'test)))
1387 (should-not
1388 (let (org-export--registered-backends)
1389 (org-export-define-backend 'test '((headline . test)))
1390 (org-export-define-backend 'test2 '((headline . test2)))
1391 (org-export-define-derived-backend 'test3 'test2)
1392 (org-export-derived-backend-p 'test3 'test))))
1394 (ert-deftest test-org-export/get-all-transcoders ()
1395 "Test `org-export-get-all-transcoders' specifications."
1396 ;; Return nil when back-end cannot be found.
1397 (should-not (org-export-get-all-transcoders nil))
1398 ;; Same as `org-export-transcoders' if no parent.
1399 (should
1400 (equal '((headline . ignore))
1401 (org-export-get-all-transcoders
1402 (org-export-create-backend
1403 :transcoders '((headline . ignore))))))
1404 ;; But inherit from all ancestors whenever possible.
1405 (should
1406 (equal '((section . ignore) (headline . ignore))
1407 (let (org-export--registered-backends)
1408 (org-export-define-backend 'b1 '((headline . ignore)))
1409 (org-export-get-all-transcoders
1410 (org-export-create-backend
1411 :parent 'b1 :transcoders '((section . ignore)))))))
1412 (should
1413 (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
1414 (let (org-export--registered-backends)
1415 (org-export-define-backend 'b1 '((headline . ignore)))
1416 (org-export-define-derived-backend 'b2 'b1
1417 :translate-alist '((section . ignore)))
1418 (org-export-get-all-transcoders
1419 (org-export-create-backend
1420 :parent 'b2 :transcoders '((paragraph . ignore)))))))
1421 ;; Back-end transcoders overrule inherited ones.
1422 (should
1423 (eq 'b
1424 (let (org-export--registered-backends)
1425 (org-export-define-backend 'b1 '((headline . a)))
1426 (cdr (assq 'headline
1427 (org-export-get-all-transcoders
1428 (org-export-create-backend
1429 :parent 'b1 :transcoders '((headline . b))))))))))
1431 (ert-deftest test-org-export/get-all-options ()
1432 "Test `org-export-get-all-options' specifications."
1433 ;; Return nil when back-end cannot be found.
1434 (should-not (org-export-get-all-options nil))
1435 ;; Same as `org-export-options' if no parent.
1436 (should
1437 (equal '((headline . ignore))
1438 (org-export-get-all-options
1439 (org-export-create-backend
1440 :options '((headline . ignore))))))
1441 ;; But inherit from all ancestors whenever possible.
1442 (should
1443 (equal '((:key2 value2) (:key1 value1))
1444 (let (org-export--registered-backends)
1445 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1446 (org-export-get-all-options
1447 (org-export-create-backend
1448 :parent 'b1 :options '((:key2 value2)))))))
1449 (should
1450 (equal '((:key3 value3) (:key2 value2) (:key1 value1))
1451 (let (org-export--registered-backends)
1452 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1453 (org-export-define-derived-backend 'b2 'b1
1454 :options-alist '((:key2 value2)))
1455 (org-export-get-all-options
1456 (org-export-create-backend
1457 :parent 'b2 :options '((:key3 value3)))))))
1458 ;; Back-end options overrule inherited ones.
1459 (should
1460 (eq 'b
1461 (let (org-export--registered-backends)
1462 (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
1463 (cdr (assq :key1
1464 (org-export-get-all-options
1465 (org-export-create-backend
1466 :parent 'b1 :options '((:key1 . b))))))))))
1468 (ert-deftest test-org-export/get-all-filters ()
1469 "Test `org-export-get-all-filters' specifications."
1470 ;; Return nil when back-end cannot be found.
1471 (should-not (org-export-get-all-filters nil))
1472 ;; Same as `org-export-filters' if no parent.
1473 (should
1474 (equal '((:filter-headline . ignore))
1475 (org-export-get-all-filters
1476 (org-export-create-backend
1477 :filters '((:filter-headline . ignore))))))
1478 ;; But inherit from all ancestors whenever possible.
1479 (should
1480 (equal '((:filter-section . ignore) (:filter-headline . ignore))
1481 (let (org-export--registered-backends)
1482 (org-export-define-backend 'b1
1483 nil :filters-alist '((:filter-headline . ignore)))
1484 (org-export-get-all-filters
1485 (org-export-create-backend
1486 :parent 'b1 :filters '((:filter-section . ignore)))))))
1487 (should
1488 (equal '((:filter-paragraph . ignore)
1489 (:filter-section . ignore)
1490 (:filter-headline . ignore))
1491 (let (org-export--registered-backends)
1492 (org-export-define-backend 'b1
1493 nil :filters-alist '((:filter-headline . ignore)))
1494 (org-export-define-derived-backend 'b2 'b1
1495 :filters-alist '((:filter-section . ignore)))
1496 (org-export-get-all-filters
1497 (org-export-create-backend
1498 :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
1499 ;; Back-end filters overrule inherited ones.
1500 (should
1501 (eq 'b
1502 (let (org-export--registered-backends)
1503 (org-export-define-backend 'b1 '((:filter-headline . a)))
1504 (cdr (assq :filter-headline
1505 (org-export-get-all-filters
1506 (org-export-create-backend
1507 :parent 'b1 :filters '((:filter-headline . b))))))))))
1509 (ert-deftest test-org-export/with-backend ()
1510 "Test `org-export-with-backend' definition."
1511 ;; Error when calling an undefined back-end
1512 (should-error (org-export-with-backend nil "Test"))
1513 ;; Error when called back-end doesn't have an appropriate
1514 ;; transcoder.
1515 (should-error
1516 (org-export-with-backend
1517 (org-export-create-backend :transcoders '((headline . ignore)))
1518 "Test"))
1519 ;; Otherwise, export using correct transcoder
1520 (should
1521 (equal "Success"
1522 (let (org-export--registered-backends)
1523 (org-export-define-backend 'test
1524 '((plain-text . (lambda (text contents info) "Failure"))))
1525 (org-export-define-backend 'test2
1526 '((plain-text . (lambda (text contents info) "Success"))))
1527 (org-export-with-backend 'test2 "Test"))))
1528 ;; Provide correct back-end if transcoder needs to use recursive
1529 ;; calls anyway.
1530 (should
1531 (equal "Success\n"
1532 (let ((test-back-end
1533 (org-export-create-backend
1534 :transcoders
1535 '((headline . (lambda (headline contents info)
1536 (org-export-data
1537 (org-element-property :title headline)
1538 info)))
1539 (plain-text . (lambda (text info) "Success"))))))
1540 (org-export-string-as
1541 "* Test"
1542 (org-export-create-backend
1543 :transcoders
1544 '((headline . (lambda (headline contents info)
1545 (org-export-with-backend
1546 test-back-end headline contents info))))))))))
1548 (ert-deftest test-org-export/data-with-backend ()
1549 "Test `org-export-data-with-backend' specifications."
1550 ;; Error when calling an undefined back-end.
1551 (should-error (org-export-data-with-backend nil "nil" nil))
1552 ;; Otherwise, export data recursively, using correct back-end.
1553 (should
1554 (equal
1555 "Success!"
1556 (org-export-data-with-backend
1557 '(bold nil "Test")
1558 (org-export-create-backend
1559 :transcoders
1560 '((plain-text . (lambda (text info) "Success"))
1561 (bold . (lambda (bold contents info) (concat contents "!")))))
1562 '(:with-emphasize t)))))
1566 ;;; Export Snippets
1568 (ert-deftest test-org-export/export-snippet ()
1569 "Test export snippets transcoding."
1570 ;; Standard test.
1571 (org-test-with-temp-text "@@test:A@@@@t:B@@"
1572 (let ((backend (org-test-default-backend)))
1573 (setf (org-export-backend-name backend) 'test)
1574 (setf (org-export-backend-transcoders backend)
1575 (cons (cons 'export-snippet
1576 (lambda (snippet contents info)
1577 (when (eq (org-export-snippet-backend snippet) 'test)
1578 (org-element-property :value snippet))))
1579 (org-export-backend-transcoders backend)))
1580 (let ((org-export-snippet-translation-alist nil))
1581 (should (equal (org-export-as backend) "A\n")))
1582 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
1583 (should (equal (org-export-as backend) "AB\n")))))
1584 ;; Ignored export snippets do not remove any blank.
1585 (should
1586 (equal "begin end\n"
1587 (org-test-with-parsed-data "begin @@test:A@@ end"
1588 (org-export-data-with-backend
1589 tree
1590 (org-export-create-backend
1591 :transcoders
1592 '((paragraph . (lambda (paragraph contents info) contents))
1593 (section . (lambda (section contents info) contents))))
1594 info)))))
1598 ;;; Footnotes
1600 (ert-deftest test-org-export/footnote-first-reference-p ()
1601 "Test `org-export-footnote-first-reference-p' specifications."
1602 (should
1603 (equal
1604 '(t nil)
1605 (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
1606 (let (result)
1607 (org-export-as
1608 (org-export-create-backend
1609 :transcoders
1610 `(,(cons 'footnote-reference
1611 (lambda (f c i)
1612 (push (org-export-footnote-first-reference-p f info)
1613 result)
1614 ""))
1615 (section . (lambda (s c i) c))
1616 (paragraph . (lambda (p c i) c))))
1617 nil nil nil '(:with-footnotes t))
1618 (nreverse result)))))
1619 ;; Limit check to DATA, when non-nil.
1620 (should
1621 (equal
1622 '(nil t)
1623 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
1624 (let (result)
1625 (org-element-map tree 'footnote-reference
1626 (lambda (ref)
1627 (push
1628 (org-export-footnote-first-reference-p
1629 ref info (org-element-map tree 'headline #'identity info t))
1630 result))
1631 info)
1632 (nreverse result)))))
1633 (should
1634 (equal
1635 '(t nil)
1636 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
1637 (let (result)
1638 (org-element-map tree 'footnote-reference
1639 (lambda (ref)
1640 (push (org-export-footnote-first-reference-p ref info) result))
1641 info)
1642 (nreverse result)))))
1643 ;; If optional argument BODY-FIRST is non-nil, first find footnote
1644 ;; in the main body of the document. Otherwise, enter footnote
1645 ;; definitions when they are encountered.
1646 (should
1647 (equal
1648 '(t nil)
1649 (org-test-with-temp-text
1650 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
1651 (let (result)
1652 (org-export-as
1653 (org-export-create-backend
1654 :transcoders
1655 `(,(cons 'footnote-reference
1656 (lambda (f c i)
1657 (when (org-element-lineage f '(drawer))
1658 (push (org-export-footnote-first-reference-p f info nil)
1659 result))
1660 ""))
1661 (drawer . (lambda (d c i) c))
1662 (footnote-definition . (lambda (d c i) c))
1663 (section . (lambda (s c i) c))
1664 (paragraph . (lambda (p c i) c))))
1665 nil nil nil '(:with-footnotes t))
1666 (nreverse result)))))
1667 (should
1668 (equal
1669 '(t t)
1670 (org-test-with-temp-text
1671 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
1672 (let (result)
1673 (org-export-as
1674 (org-export-create-backend
1675 :transcoders
1676 `(,(cons 'footnote-reference
1677 (lambda (f c i)
1678 (when (org-element-lineage f '(drawer))
1679 (push (org-export-footnote-first-reference-p f info nil t)
1680 result))
1681 ""))
1682 (drawer . (lambda (d c i) c))
1683 (footnote-definition . (lambda (d c i) c))
1684 (section . (lambda (s c i) c))
1685 (paragraph . (lambda (p c i) c))))
1686 nil nil nil '(:with-footnotes t))
1687 (nreverse result))))))
1689 (ert-deftest test-org-export/get-footnote-number ()
1690 "Test `org-export-get-footnote-number' specifications."
1691 (should
1692 (equal '(1 2 1)
1693 (org-test-with-parsed-data
1694 "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
1695 (org-element-map tree 'footnote-reference
1696 (lambda (ref) (org-export-get-footnote-number ref info))
1697 info))))
1698 ;; Anonymous footnotes all get a new number.
1699 (should
1700 (equal '(1 2)
1701 (org-test-with-parsed-data
1702 "Text[fn::anon1][fn::anon2]"
1703 (org-element-map tree 'footnote-reference
1704 (lambda (ref) (org-export-get-footnote-number ref info))
1705 info))))
1706 ;; Test nested footnotes order.
1707 (should
1708 (equal
1709 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (3 . "fn:3") (4))
1710 (org-test-with-parsed-data
1711 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
1712 (org-element-map tree 'footnote-reference
1713 (lambda (ref)
1714 (cons (org-export-get-footnote-number ref info)
1715 (org-element-property :label ref)))
1716 info))))
1717 ;; Limit number to provided DATA, when non-nil.
1718 (should
1719 (equal
1720 '(1)
1721 (org-test-with-parsed-data
1722 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
1723 (org-element-map tree 'footnote-reference
1724 (lambda (ref)
1725 (org-export-get-footnote-number
1726 ref info (org-element-map tree 'headline #'identity info t)))
1727 info))))
1728 (should
1729 (equal
1730 '(1 2)
1731 (org-test-with-parsed-data
1732 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
1733 (org-element-map tree 'footnote-reference
1734 (lambda (ref) (org-export-get-footnote-number ref info))
1735 info))))
1736 ;; With a non-nil BODY-FIRST optional argument, first check body,
1737 ;; then footnote definitions.
1738 (should
1739 (equal
1740 '(("fn:1" . 1) ("fn:2" . 2) ("fn:3" . 3) ("fn:3" . 3))
1741 (org-test-with-parsed-data
1742 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
1743 (org-element-map tree 'footnote-reference
1744 (lambda (ref)
1745 (cons (org-element-property :label ref)
1746 (org-export-get-footnote-number ref info nil t)))
1747 info))))
1748 (should
1749 (equal
1750 '(("fn:1" . 1) ("fn:2" . 3) ("fn:3" . 2) ("fn:3" . 2))
1751 (org-test-with-parsed-data
1752 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
1753 (org-element-map tree 'footnote-reference
1754 (lambda (ref)
1755 (cons (org-element-property :label ref)
1756 (org-export-get-footnote-number ref info nil)))
1757 info)))))
1759 (ert-deftest test-org-export/collect-footnote-definitions ()
1760 "Test `org-export-collect-footnote-definitions' specifications."
1761 (should
1762 (= 4
1763 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1765 \[fn:2] B [fn:3] [fn::D].
1767 \[fn:3] C."
1768 (length (org-export-collect-footnote-definitions info)))))
1769 ;; Limit number to provided DATA, when non-nil.
1770 (should
1771 (equal
1772 '((1 "fn:2"))
1773 (org-test-with-parsed-data
1774 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
1775 (mapcar #'butlast
1776 (org-export-collect-footnote-definitions
1777 info (org-element-map tree 'headline #'identity info t))))))
1778 (should
1779 (equal
1780 '((1 "fn:1") (2 "fn:2"))
1781 (org-test-with-parsed-data
1782 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
1783 (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
1784 ;; With optional argument BODY-FIRST, first check body, then
1785 ;; footnote definitions.
1786 (should
1787 (equal '("fn:1" "fn:3" "fn:2" nil)
1788 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1790 \[fn:2] B [fn:3] [fn::D].
1792 \[fn:3] C."
1793 (mapcar (lambda (e) (nth 1 e))
1794 (org-export-collect-footnote-definitions info nil t)))))
1795 (should-not
1796 (equal '("fn:1" "fn:3" "fn:2" nil)
1797 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1799 \[fn:2] B [fn:3] [fn::D].
1801 \[fn:3] C."
1802 (mapcar (lambda (e) (nth 1 e))
1803 (org-export-collect-footnote-definitions info))))))
1805 (ert-deftest test-org-export/footnotes ()
1806 "Miscellaneous tests on footnotes."
1807 (let ((org-footnote-section nil)
1808 (org-export-with-footnotes t))
1809 ;; Read every type of footnote.
1810 (should
1811 (equal
1812 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
1813 (org-test-with-parsed-data
1814 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
1815 (org-element-map tree 'footnote-reference
1816 (lambda (ref)
1817 (let ((def (org-export-get-footnote-definition ref info)))
1818 (cons (org-export-get-footnote-number ref info)
1819 (if (eq (org-element-property :type ref) 'inline) (car def)
1820 (car (org-element-contents
1821 (car (org-element-contents def))))))))
1822 info))))
1823 ;; Test nested footnote in invisible definitions.
1824 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
1825 ;; Hide definitions.
1826 (narrow-to-region (point) (point-at-eol))
1827 (let* ((tree (org-element-parse-buffer))
1828 (info (org-combine-plists
1829 `(:parse-tree ,tree)
1830 (org-export-collect-tree-properties
1831 tree (org-export-get-environment)))))
1832 ;; Both footnotes should be seen.
1833 (should
1834 (= (length (org-export-collect-footnote-definitions info)) 2))))
1835 ;; Test export of footnotes defined outside parsing scope.
1836 (should
1837 (equal
1838 "ParagraphOut of scope\n"
1839 (org-test-with-temp-text "[fn:1] Out of scope
1840 * Title
1841 Paragraph[fn:1]"
1842 (let ((backend (org-test-default-backend)))
1843 (setf (org-export-backend-transcoders backend)
1844 (cons (cons 'footnote-reference
1845 (lambda (fn contents info)
1846 (org-element-interpret-data
1847 (org-export-get-footnote-definition fn info))))
1848 (org-export-backend-transcoders backend)))
1849 (forward-line)
1850 (org-export-as backend 'subtree)))))
1851 ;; Footnotes without a definition should throw an error.
1852 (should-error
1853 (org-test-with-parsed-data "Text[fn:1]"
1854 (org-export-get-footnote-definition
1855 (org-element-map tree 'footnote-reference 'identity info t) info)))
1856 ;; Footnote section should be ignored in TOC and in headlines
1857 ;; numbering.
1858 (should
1859 (= 1 (let ((org-footnote-section "Footnotes"))
1860 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
1861 (org-export-collect-headlines info))))))
1862 (should
1863 (equal '(2)
1864 (let ((org-footnote-section "Footnotes"))
1865 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
1866 (org-element-map tree 'headline
1867 (lambda (hl)
1868 (when (equal (org-element-property :raw-value hl) "H2")
1869 (org-export-get-headline-number hl info)))
1870 info t)))))))
1874 ;;; Headlines and Inlinetasks
1876 (ert-deftest test-org-export/get-relative-level ()
1877 "Test `org-export-get-relative-level' specifications."
1878 ;; Standard test.
1879 (should
1880 (equal '(1 2)
1881 (let ((org-odd-levels-only nil))
1882 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1883 (org-element-map tree 'headline
1884 (lambda (h) (org-export-get-relative-level h info))
1885 info)))))
1886 ;; Missing levels
1887 (should
1888 (equal '(1 3)
1889 (let ((org-odd-levels-only nil))
1890 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
1891 (org-element-map tree 'headline
1892 (lambda (h) (org-export-get-relative-level h info))
1893 info))))))
1895 (ert-deftest test-org-export/low-level-p ()
1896 "Test `org-export-low-level-p' specifications."
1897 (should
1898 (equal
1899 '(no yes)
1900 (let ((org-odd-levels-only nil))
1901 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1902 (org-element-map tree 'headline
1903 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
1904 (plist-put info :headline-levels 1)))))))
1906 (ert-deftest test-org-export/get-headline-number ()
1907 "Test `org-export-get-headline-number' specifications."
1908 ;; Standard test.
1909 (should
1910 (equal
1911 '((1) (1 1))
1912 (let ((org-odd-levels-only nil))
1913 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1914 (org-element-map tree 'headline
1915 (lambda (h) (org-export-get-headline-number h info))
1916 info)))))
1917 ;; Missing levels are replaced with 0.
1918 (should
1919 (equal
1920 '((1) (1 0 1))
1921 (let ((org-odd-levels-only nil))
1922 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
1923 (org-element-map tree 'headline
1924 (lambda (h) (org-export-get-headline-number h info))
1925 info))))))
1927 (ert-deftest test-org-export/numbered-headline-p ()
1928 "Test `org-export-numbered-headline-p' specifications."
1929 ;; If `:section-numbers' is nil, never number headlines.
1930 (should-not
1931 (org-test-with-parsed-data "* Headline"
1932 (org-element-map tree 'headline
1933 (lambda (h) (org-export-numbered-headline-p h info))
1934 (plist-put info :section-numbers nil))))
1935 ;; If `:section-numbers' is a number, only number headlines with
1936 ;; a level greater that it.
1937 (should
1938 (equal
1939 '(yes no)
1940 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1941 (org-element-map tree 'headline
1942 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
1943 (plist-put info :section-numbers 1)))))
1944 ;; Otherwise, headlines are always numbered.
1945 (should
1946 (org-test-with-parsed-data "* Headline"
1947 (org-element-map tree 'headline
1948 (lambda (h) (org-export-numbered-headline-p h info))
1949 (plist-put info :section-numbers t))))
1950 ;; With #+OPTIONS: num:nil all headlines are unnumbered.
1951 (should-not
1952 (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
1953 (org-export-numbered-headline-p
1954 (org-element-map tree 'headline 'identity info t)
1955 info)))
1956 ;; Headlines with a non-nil UNNUMBERED property are not numbered.
1957 (should-not
1958 (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
1959 (org-export-numbered-headline-p
1960 (org-element-map tree 'headline #'identity info t)
1961 info)))
1962 ;; UNNUMBERED ignores inheritance. Any non-nil value among
1963 ;; ancestors disables numbering.
1964 (should
1965 (org-test-with-parsed-data
1966 "* H
1967 :PROPERTIES:
1968 :UNNUMBERED: t
1969 :END:
1970 ** H2
1971 :PROPERTIES:
1972 :UNNUMBERED: nil
1973 :END:
1974 *** H3"
1975 (org-every
1976 (lambda (h) (not (org-export-numbered-headline-p h info)))
1977 (org-element-map tree 'headline #'identity info)))))
1979 (ert-deftest test-org-export/number-to-roman ()
1980 "Test `org-export-number-to-roman' specifications."
1981 ;; If number is negative, return it as a string.
1982 (should (equal (org-export-number-to-roman -1) "-1"))
1983 ;; Otherwise, return it as a roman number.
1984 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
1986 (ert-deftest test-org-export/get-optional-title ()
1987 "Test `org-export-get-alt-title' specifications."
1988 ;; If ALT_TITLE property is defined, use it.
1989 (should
1990 (equal '("opt")
1991 (org-test-with-parsed-data
1992 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
1993 (org-export-get-alt-title
1994 (org-element-map tree 'headline 'identity info t)
1995 info))))
1996 ;; Otherwise, fall-back to regular title.
1997 (should
1998 (equal '("Headline")
1999 (org-test-with-parsed-data "* Headline"
2000 (org-export-get-alt-title
2001 (org-element-map tree 'headline 'identity info t)
2002 info)))))
2004 (ert-deftest test-org-export/get-tags ()
2005 "Test `org-export-get-tags' specifications."
2006 (let ((org-export-exclude-tags '("noexport"))
2007 (org-export-select-tags '("export")))
2008 ;; Standard test: tags which are not a select tag, an exclude tag,
2009 ;; or specified as optional argument shouldn't be ignored.
2010 (should
2011 (org-test-with-parsed-data "* Headline :tag:"
2012 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2013 info)))
2014 ;; Exclude tags are removed.
2015 (should-not
2016 (org-test-with-parsed-data "* Headline :noexport:"
2017 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2018 info)))
2019 ;; Select tags are removed.
2020 (should-not
2021 (org-test-with-parsed-data "* Headline :export:"
2022 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2023 info)))
2024 (should
2025 (equal
2026 '("tag")
2027 (org-test-with-parsed-data "* Headline :tag:export:"
2028 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2029 info))))
2030 ;; Tags provided in the optional argument are also ignored.
2031 (should-not
2032 (org-test-with-parsed-data "* Headline :ignore:"
2033 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2034 info '("ignore"))))
2035 ;; Allow tag inheritance.
2036 (should
2037 (equal
2038 '(("tag") ("tag"))
2039 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
2040 (org-element-map tree 'headline
2041 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
2042 ;; Tag inheritance checks FILETAGS keywords.
2043 (should
2044 (equal
2045 '(("a" "b" "tag"))
2046 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
2047 (org-element-map tree 'headline
2048 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
2050 (ert-deftest test-org-export/get-node-property ()
2051 "Test`org-export-get-node-property' specifications."
2052 ;; Standard test.
2053 (should
2054 (equal "value"
2055 (org-test-with-parsed-data "* Headline
2056 :PROPERTIES:
2057 :prop: value
2058 :END:"
2059 (org-export-get-node-property
2060 :PROP (org-element-map tree 'headline 'identity nil t)))))
2061 ;; Test inheritance.
2062 (should
2063 (equal "value"
2064 (org-test-with-parsed-data "* Parent
2065 :PROPERTIES:
2066 :prop: value
2067 :END:
2068 ** Headline
2069 Paragraph"
2070 (org-export-get-node-property
2071 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
2072 ;; Cannot return a value before the first headline.
2073 (should-not
2074 (org-test-with-parsed-data "Paragraph
2075 * Headline
2076 :PROPERTIES:
2077 :prop: value
2078 :END:"
2079 (org-export-get-node-property
2080 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
2082 (ert-deftest test-org-export/get-category ()
2083 "Test `org-export-get-category' specifications."
2084 ;; Standard test.
2085 (should
2086 (equal "value"
2087 (org-test-with-parsed-data "* Headline
2088 :PROPERTIES:
2089 :CATEGORY: value
2090 :END:"
2091 (org-export-get-category
2092 (org-element-map tree 'headline 'identity nil t) info))))
2093 ;; Test inheritance from a parent headline.
2094 (should
2095 (equal '("value" "value")
2096 (org-test-with-parsed-data "* Headline1
2097 :PROPERTIES:
2098 :CATEGORY: value
2099 :END:
2100 ** Headline2"
2101 (org-element-map tree 'headline
2102 (lambda (hl) (org-export-get-category hl info)) info))))
2103 ;; Test inheritance from #+CATEGORY keyword
2104 (should
2105 (equal "value"
2106 (org-test-with-parsed-data "#+CATEGORY: value
2107 * Headline"
2108 (org-export-get-category
2109 (org-element-map tree 'headline 'identity nil t) info))))
2110 ;; Test inheritance from file name.
2111 (should
2112 (equal "test"
2113 (org-test-with-parsed-data "* Headline"
2114 (let ((info (plist-put info :input-file "~/test.org")))
2115 (org-export-get-category
2116 (org-element-map tree 'headline 'identity nil t) info)))))
2117 ;; Fall-back value.
2118 (should
2119 (equal "???"
2120 (org-test-with-parsed-data "* Headline"
2121 (org-export-get-category
2122 (org-element-map tree 'headline 'identity nil t) info)))))
2124 (ert-deftest test-org-export/first-sibling-p ()
2125 "Test `org-export-first-sibling-p' specifications."
2126 ;; Standard test.
2127 (should
2128 (equal
2129 '(yes yes no)
2130 (org-test-with-parsed-data "* H\n** H 2\n** H 3"
2131 (org-element-map tree 'headline
2132 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2133 info))))
2134 (should
2135 (equal '(yes no)
2136 (org-test-with-parsed-data "- item\n\n para"
2137 (org-element-map tree 'paragraph
2138 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2139 info))))
2140 ;; Ignore sections for headlines.
2141 (should
2142 (equal '(yes yes)
2143 (org-test-with-parsed-data "* H\nSection\n** H 2"
2144 (org-element-map tree 'headline
2145 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2146 info))))
2147 ;; Ignore headlines not exported.
2148 (should
2149 (equal
2150 '(yes)
2151 (let ((org-export-exclude-tags '("ignore")))
2152 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
2153 (org-element-map tree 'headline
2154 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2155 info))))))
2157 (ert-deftest test-org-export/last-sibling-p ()
2158 "Test `org-export-last-sibling-p' specifications."
2159 ;; Standard test.
2160 (should
2161 (equal
2162 '(yes no yes)
2163 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
2164 (org-element-map tree 'headline
2165 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2166 info))))
2167 (should
2168 (equal '(no yes)
2169 (org-test-with-parsed-data "- item\n\n para"
2170 (org-element-map tree 'paragraph
2171 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2172 info))))
2173 ;; Ignore headlines not exported.
2174 (should
2175 (equal
2176 '(yes)
2177 (let ((org-export-exclude-tags '("ignore")))
2178 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
2179 (org-element-map tree 'headline
2180 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2181 info))))))
2183 (ert-deftest test-org-export/handle-inlinetasks ()
2184 "Test inlinetask export."
2185 ;; Inlinetask with an exclude tag.
2186 (when (featurep 'org-inlinetask)
2187 (should
2188 (equal
2190 (let ((org-inlinetask-min-level 3)
2191 org-export-filter-body-functions
2192 org-export-filter-final-output-functions)
2193 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
2194 (org-export-as (org-test-default-backend)
2195 nil nil nil '(:exclude-tags ("noexp")))))))
2196 ;; Inlinetask with an include tag.
2197 (should
2198 (equal
2199 "* H2\n*** Inline :exp:\n"
2200 (let ((org-inlinetask-min-level 3)
2201 (org-tags-column 0))
2202 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
2203 (org-export-as (org-test-default-backend)
2204 nil nil nil '(:select-tags ("exp")))))))
2205 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
2206 (should
2207 (equal ""
2208 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
2209 (org-inlinetask-min-level 3)
2210 org-export-filter-body-functions
2211 org-export-filter-final-output-functions)
2212 (org-test-with-temp-text "*** TODO Inline"
2213 (org-export-as (org-test-default-backend)
2214 nil nil nil '(:with-tasks nil))))))))
2218 ;;; Keywords
2220 (ert-deftest test-org-export/get-date ()
2221 "Test `org-export-get-date' specifications."
2222 ;; Return a properly formatted string when
2223 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
2224 ;; consists in a single timestamp.
2225 (should
2226 (equal "29 03 2012"
2227 (let ((org-export-date-timestamp-format "%d %m %Y"))
2228 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2229 (org-export-get-date info)))))
2230 ;; Return a secondary string otherwise.
2231 (should-not
2232 (stringp
2233 (let ((org-export-date-timestamp-format nil))
2234 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2235 (org-export-get-date info)))))
2236 (should
2237 (equal '("Date")
2238 (org-test-with-parsed-data "#+DATE: Date"
2239 (org-export-get-date info))))
2240 ;; Optional argument has precedence over
2241 ;; `org-export-date-timestamp-format'.
2242 (should
2243 (equal "29 03"
2244 (let ((org-export-date-timestamp-format "%d %m %Y"))
2245 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2246 (org-export-get-date info "%d %m"))))))
2250 ;;; Links
2252 (ert-deftest test-org-export/custom-protocol-maybe ()
2253 "Test `org-export-custom-protocol-maybe' specifications."
2254 (should
2255 (string-match
2256 "success"
2257 (let ((org-link-types (copy-sequence org-link-types)))
2258 (org-add-link-type "foo" nil (lambda (p d f) "success"))
2259 (org-export-string-as
2260 "[[foo:path]]"
2261 (org-export-create-backend
2262 :name 'test
2263 :transcoders
2264 '((section . (lambda (s c i) c))
2265 (paragraph . (lambda (p c i) c))
2266 (link . (lambda (l c i)
2267 (or (org-export-custom-protocol-maybe l c 'test)
2268 "failure")))))))))
2269 (should-not
2270 (string-match
2271 "success"
2272 (let ((org-link-types (copy-sequence org-link-types)))
2273 (org-add-link-type
2274 "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
2275 (org-export-string-as
2276 "[[foo:path]]"
2277 (org-export-create-backend
2278 :name 'no-test
2279 :transcoders
2280 '((section . (lambda (s c i) c))
2281 (paragraph . (lambda (p c i) c))
2282 (link . (lambda (l c i)
2283 (or (org-export-custom-protocol-maybe l c 'no-test)
2284 "failure")))))))))
2285 ;; Ignore anonymous back-ends.
2286 (should-not
2287 (string-match
2288 "success"
2289 (let ((org-link-types (copy-sequence org-link-types)))
2290 (org-add-link-type
2291 "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
2292 (org-export-string-as
2293 "[[foo:path]]"
2294 (org-export-create-backend
2295 :transcoders
2296 '((section . (lambda (s c i) c))
2297 (paragraph . (lambda (p c i) c))
2298 (link . (lambda (l c i)
2299 (or (org-export-custom-protocol-maybe l c nil)
2300 "failure"))))))))))
2302 (ert-deftest test-org-export/get-coderef-format ()
2303 "Test `org-export-get-coderef-format' specifications."
2304 ;; A link without description returns "%s"
2305 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
2306 "%s"))
2307 ;; Return "%s" when path is matched within description.
2308 (should (equal (org-export-get-coderef-format "path" "desc (path)")
2309 "desc %s"))
2310 ;; Otherwise return description.
2311 (should (equal (org-export-get-coderef-format "path" "desc")
2312 "desc")))
2314 (ert-deftest test-org-export/inline-image-p ()
2315 "Test `org-export-inline-image-p' specifications."
2316 (should
2317 (org-export-inline-image-p
2318 (org-test-with-temp-text "[[#id]]"
2319 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
2320 '(("custom-id" . "id")))))
2322 (ert-deftest test-org-export/fuzzy-link ()
2323 "Test fuzzy links specifications."
2324 ;; Link to an headline should return headline's number.
2325 (org-test-with-parsed-data
2326 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
2327 (should
2328 ;; Note: Headline's number is in fact a list of numbers.
2329 (equal '(2)
2330 (org-element-map tree 'link
2331 (lambda (link)
2332 (org-export-get-ordinal
2333 (org-export-resolve-fuzzy-link link info) info)) info t))))
2334 ;; Link to a target in an item should return item's number.
2335 (org-test-with-parsed-data
2336 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
2337 (should
2338 ;; Note: Item's number is in fact a list of numbers.
2339 (equal '(1 2)
2340 (org-element-map tree 'link
2341 (lambda (link)
2342 (org-export-get-ordinal
2343 (org-export-resolve-fuzzy-link link info) info)) info t))))
2344 ;; Link to a target in a footnote should return footnote's number.
2345 (org-test-with-parsed-data "
2346 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
2347 (should
2348 (equal '(2 3)
2349 (org-element-map tree 'link
2350 (lambda (link)
2351 (org-export-get-ordinal
2352 (org-export-resolve-fuzzy-link link info) info)) info))))
2353 ;; Link to a named element should return sequence number of that
2354 ;; element.
2355 (org-test-with-parsed-data
2356 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
2357 (should
2358 (= 2
2359 (org-element-map tree 'link
2360 (lambda (link)
2361 (org-export-get-ordinal
2362 (org-export-resolve-fuzzy-link link info) info)) info t))))
2363 ;; Link to a target not within an item, a table, a footnote
2364 ;; reference or definition should return section number.
2365 (org-test-with-parsed-data
2366 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
2367 (should
2368 (equal '(2)
2369 (org-element-map tree 'link
2370 (lambda (link)
2371 (org-export-get-ordinal
2372 (org-export-resolve-fuzzy-link link info) info)) info t))))
2373 ;; Space are not significant when matching a fuzzy link.
2374 (should
2375 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
2376 (org-element-map tree 'link
2377 (lambda (link) (org-export-resolve-fuzzy-link link info))
2378 info t)))
2379 ;; Statistics cookies are ignored for headline match.
2380 (should
2381 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
2382 (org-element-map tree 'link
2383 (lambda (link) (org-export-resolve-fuzzy-link link info))
2384 info t)))
2385 (should
2386 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
2387 (org-element-map tree 'link
2388 (lambda (link) (org-export-resolve-fuzzy-link link info))
2389 info t)))
2390 ;; Headline match is position dependent.
2391 (should-not
2392 (apply
2394 (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
2395 (org-element-map tree 'link
2396 (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
2398 (ert-deftest test-org-export/resolve-coderef ()
2399 "Test `org-export-resolve-coderef' specifications."
2400 (let ((org-coderef-label-format "(ref:%s)"))
2401 ;; A link to a "-n -k -r" block returns line number.
2402 (should
2403 (= 1
2404 (org-test-with-parsed-data
2405 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
2406 (org-export-resolve-coderef "coderef" info))))
2407 (should
2408 (= 1
2409 (org-test-with-parsed-data
2410 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2411 (org-export-resolve-coderef "coderef" info))))
2412 ;; A link to a "-n -r" block returns line number.
2413 (should
2414 (= 1
2415 (org-test-with-parsed-data
2416 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
2417 (org-export-resolve-coderef "coderef" info))))
2418 (should
2419 (= 1
2420 (org-test-with-parsed-data
2421 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2422 (org-export-resolve-coderef "coderef" info))))
2423 ;; A link to a "-n" block returns coderef.
2424 (should
2425 (equal "coderef"
2426 (org-test-with-parsed-data
2427 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2428 (org-export-resolve-coderef "coderef" info))))
2429 (should
2430 (equal "coderef"
2431 (org-test-with-parsed-data
2432 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
2433 (org-export-resolve-coderef "coderef" info))))
2434 ;; A link to a "-r" block returns line number.
2435 (should
2436 (= 1
2437 (org-test-with-parsed-data
2438 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2439 (org-export-resolve-coderef "coderef" info))))
2440 (should
2441 (= 1
2442 (org-test-with-parsed-data
2443 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
2444 (org-export-resolve-coderef "coderef" info))))
2445 ;; A link to a block without a switch returns coderef.
2446 (should
2447 (equal "coderef"
2448 (org-test-with-parsed-data
2449 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2450 (org-export-resolve-coderef "coderef" info))))
2451 (org-test-with-parsed-data
2452 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
2453 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2454 ;; Correctly handle continued line numbers. A "+n" switch should
2455 ;; resume numbering from previous block with numbered lines,
2456 ;; ignoring blocks not numbering lines in the process. A "-n"
2457 ;; switch resets count.
2458 (should
2459 (equal '(2 1)
2460 (org-test-with-parsed-data "
2461 #+BEGIN_EXAMPLE -n
2462 Text.
2463 #+END_EXAMPLE
2465 #+BEGIN_SRC emacs-lisp
2466 \(- 1 1)
2467 #+END_SRC
2469 #+BEGIN_SRC emacs-lisp +n -r
2470 \(+ 1 1) (ref:addition)
2471 #+END_SRC
2473 #+BEGIN_EXAMPLE -n -r
2474 Another text. (ref:text)
2475 #+END_EXAMPLE"
2476 (list (org-export-resolve-coderef "addition" info)
2477 (org-export-resolve-coderef "text" info)))))
2478 ;; Recognize coderef with user-specified syntax.
2479 (should
2480 (equal "text"
2481 (org-test-with-parsed-data
2482 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
2483 (org-export-resolve-coderef "text" info))))
2484 ;; Unresolved coderefs throw an error.
2485 (should-error
2486 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2487 (org-export-resolve-coderef "unknown" info)))))
2489 (ert-deftest test-org-export/resolve-fuzzy-link ()
2490 "Test `org-export-resolve-fuzzy-link' specifications."
2491 ;; Match target objects.
2492 (should
2493 (org-test-with-parsed-data "<<target>> [[target]]"
2494 (org-export-resolve-fuzzy-link
2495 (org-element-map tree 'link 'identity info t) info)))
2496 ;; Match named elements.
2497 (should
2498 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
2499 (org-export-resolve-fuzzy-link
2500 (org-element-map tree 'link 'identity info t) info)))
2501 ;; Match exact headline's name.
2502 (should
2503 (org-test-with-parsed-data "* My headline\n[[My headline]]"
2504 (org-export-resolve-fuzzy-link
2505 (org-element-map tree 'link 'identity info t) info)))
2506 ;; Targets objects have priority over named elements and headline
2507 ;; titles.
2508 (should
2509 (eq 'target
2510 (org-test-with-parsed-data
2511 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
2512 (org-element-type
2513 (org-export-resolve-fuzzy-link
2514 (org-element-map tree 'link 'identity info t) info)))))
2515 ;; Named elements have priority over headline titles.
2516 (should
2517 (eq 'paragraph
2518 (org-test-with-parsed-data
2519 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
2520 (org-element-type
2521 (org-export-resolve-fuzzy-link
2522 (org-element-map tree 'link 'identity info t) info)))))
2523 ;; If link's path starts with a "*", only match headline titles,
2524 ;; though.
2525 (should
2526 (eq 'headline
2527 (org-test-with-parsed-data
2528 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
2529 (org-element-type
2530 (org-export-resolve-fuzzy-link
2531 (org-element-map tree 'link 'identity info t) info)))))
2532 ;; Error if no match.
2533 (should-error
2534 (org-test-with-parsed-data "[[target]]"
2535 (org-export-resolve-fuzzy-link
2536 (org-element-map tree 'link 'identity info t) info)))
2537 ;; Match fuzzy link even when before first headline.
2538 (should
2539 (eq 'headline
2540 (org-test-with-parsed-data "[[hl]]\n* hl"
2541 (org-element-type
2542 (org-export-resolve-fuzzy-link
2543 (org-element-map tree 'link 'identity info t) info))))))
2545 (ert-deftest test-org-export/resolve-id-link ()
2546 "Test `org-export-resolve-id-link' specifications."
2547 ;; Regular test for custom-id link.
2548 (should
2549 (equal '("Headline1")
2550 (org-test-with-parsed-data "* Headline1
2551 :PROPERTIES:
2552 :CUSTOM_ID: test
2553 :END:
2554 * Headline 2
2555 \[[#test]]"
2556 (org-element-property
2557 :title
2558 (org-export-resolve-id-link
2559 (org-element-map tree 'link 'identity info t) info)))))
2560 ;; Throw an error on failing searches.
2561 (should-error
2562 (org-test-with-parsed-data "* Headline1
2563 :PROPERTIES:
2564 :CUSTOM_ID: test
2565 :END:
2566 * Headline 2
2567 \[[#no-match]]"
2568 (org-export-resolve-id-link
2569 (org-element-map tree 'link 'identity info t) info)))
2570 ;; Test for internal id target.
2571 (should
2572 (equal '("Headline1")
2573 (org-test-with-parsed-data "* Headline1
2574 :PROPERTIES:
2575 :ID: aaaa
2576 :END:
2577 * Headline 2
2578 \[[id:aaaa]]"
2579 (org-element-property
2580 :title
2581 (org-export-resolve-id-link
2582 (org-element-map tree 'link 'identity info t) info)))))
2583 ;; Test for external id target.
2584 (should
2585 (equal
2586 "external-file"
2587 (org-test-with-parsed-data "[[id:aaaa]]"
2588 (org-export-resolve-id-link
2589 (org-element-map tree 'link 'identity info t)
2590 (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
2592 (ert-deftest test-org-export/resolve-radio-link ()
2593 "Test `org-export-resolve-radio-link' specifications."
2594 ;; Standard test.
2595 (should
2596 (org-test-with-temp-text "<<<radio>>> radio"
2597 (org-update-radio-target-regexp)
2598 (let* ((tree (org-element-parse-buffer))
2599 (info `(:parse-tree ,tree)))
2600 (org-export-resolve-radio-link
2601 (org-element-map tree 'link 'identity info t)
2602 info))))
2603 ;; Radio targets are case-insensitive.
2604 (should
2605 (org-test-with-temp-text "<<<RADIO>>> radio"
2606 (org-update-radio-target-regexp)
2607 (let* ((tree (org-element-parse-buffer))
2608 (info `(:parse-tree ,tree)))
2609 (org-export-resolve-radio-link
2610 (org-element-map tree 'link 'identity info t)
2611 info))))
2612 ;; Radio target with objects.
2613 (should
2614 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
2615 (org-update-radio-target-regexp)
2616 (let* ((tree (org-element-parse-buffer))
2617 (info `(:parse-tree ,tree)))
2618 (org-export-resolve-radio-link
2619 (org-element-map tree 'link 'identity info t)
2620 info))))
2621 ;; Radio target with objects at its beginning.
2622 (should
2623 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
2624 (org-update-radio-target-regexp)
2625 (let* ((tree (org-element-parse-buffer))
2626 (info `(:parse-tree ,tree)))
2627 (org-export-resolve-radio-link
2628 (org-element-map tree 'link 'identity info t)
2629 info))))
2630 ;; Radio link next to an apostrophe.
2631 (should
2632 (org-test-with-temp-text "<<<radio>>> radio's"
2633 (org-update-radio-target-regexp)
2634 (let* ((tree (org-element-parse-buffer))
2635 (info `(:parse-tree ,tree)))
2636 (org-export-resolve-radio-link
2637 (org-element-map tree 'link 'identity info t)
2638 info))))
2639 ;; Multiple radio targets.
2640 (should
2641 (equal '("radio1" "radio2")
2642 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
2643 (org-update-radio-target-regexp)
2644 (let* ((tree (org-element-parse-buffer))
2645 (info `(:parse-tree ,tree)))
2646 (org-element-map tree 'link
2647 (lambda (link)
2648 (org-element-property
2649 :value (org-export-resolve-radio-link link info)))
2650 info)))))
2651 ;; Radio target is whitespace insensitive.
2652 (should
2653 (org-test-with-temp-text "<<<a radio>>> a\n radio"
2654 (org-update-radio-target-regexp)
2655 (let* ((tree (org-element-parse-buffer))
2656 (info `(:parse-tree ,tree)))
2657 (org-element-map tree 'link
2658 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
2660 (ert-deftest test-org-export/file-uri ()
2661 "Test `org-export-file-uri' specifications."
2662 ;; Preserve relative filenames.
2663 (should (equal "relative.org" (org-export-file-uri "relative.org")))
2664 ;; Local files start with "file:///"
2665 (should (equal "file:///local.org" (org-export-file-uri "/local.org")))
2666 ;; Remote files start with "file://"
2667 (should (equal "file://myself@some.where:papers/last.pdf"
2668 (org-export-file-uri "/myself@some.where:papers/last.pdf")))
2669 ;; Expand filename starting with "~".
2670 (should (equal (org-export-file-uri "~/file.org")
2671 (concat "file://" (expand-file-name "~/file.org")))))
2675 ;;; Src-block and example-block
2677 (ert-deftest test-org-export/unravel-code ()
2678 "Test `org-export-unravel-code' function."
2679 ;; Code without reference.
2680 (should
2681 (equal '("(+ 1 1)\n")
2682 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2683 (org-export-unravel-code (org-element-at-point)))))
2684 ;; Code with reference.
2685 (should
2686 (equal '("(+ 1 1)\n" (1 . "test"))
2687 (org-test-with-temp-text
2688 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
2689 (let ((org-coderef-label-format "(ref:%s)"))
2690 (org-export-unravel-code (org-element-at-point))))))
2691 ;; Code with user-defined reference.
2692 (should
2693 (equal
2694 '("(+ 1 1)\n" (1 . "test"))
2695 (org-test-with-temp-text
2696 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
2697 (let ((org-coderef-label-format "(ref:%s)"))
2698 (org-export-unravel-code (org-element-at-point))))))
2699 ;; Code references keys are relative to the current block.
2700 (should
2701 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
2702 (org-test-with-temp-text "
2703 #+BEGIN_EXAMPLE -n
2704 \(+ 1 1)
2705 #+END_EXAMPLE
2706 #+BEGIN_EXAMPLE +n
2707 \(+ 2 2)
2708 \(+ 3 3) (ref:one)
2709 #+END_EXAMPLE"
2710 (goto-line 5)
2711 (let ((org-coderef-label-format "(ref:%s)"))
2712 (org-export-unravel-code (org-element-at-point)))))))
2714 (ert-deftest test-org-export/format-code-default ()
2715 "Test `org-export-format-code-default' specifications."
2716 ;; Return the empty string when code is empty.
2717 (should
2718 (equal ""
2719 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
2720 (org-export-format-code-default
2721 (org-element-map tree 'src-block 'identity info t) info))))
2722 ;; Number lines, two whitespace characters before the actual loc.
2723 (should
2724 (equal "1 a\n2 b\n"
2725 (org-test-with-parsed-data
2726 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
2727 (org-export-format-code-default
2728 (org-element-map tree 'src-block 'identity info t) info))))
2729 ;; Put references 6 whitespace characters after the widest line,
2730 ;; wrapped within parenthesis.
2731 (should
2732 (equal "123 (a)\n1 (b)\n"
2733 (let ((org-coderef-label-format "(ref:%s)"))
2734 (org-test-with-parsed-data
2735 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
2736 (org-export-format-code-default
2737 (org-element-map tree 'src-block 'identity info t) info))))))
2741 ;;; Smart Quotes
2743 (ert-deftest test-org-export/activate-smart-quotes ()
2744 "Test `org-export-activate-smart-quotes' specifications."
2745 ;; Double quotes: standard test.
2746 (should
2747 (equal
2748 '("some &ldquo;quoted&rdquo; text")
2749 (let ((org-export-default-language "en"))
2750 (org-test-with-parsed-data "some \"quoted\" text"
2751 (org-element-map tree 'plain-text
2752 (lambda (s) (org-export-activate-smart-quotes s :html info))
2753 info)))))
2754 ;; Opening quotes: at the beginning of a paragraph.
2755 (should
2756 (equal
2757 '("&ldquo;begin")
2758 (let ((org-export-default-language "en"))
2759 (org-test-with-parsed-data "\"begin"
2760 (org-element-map tree 'plain-text
2761 (lambda (s) (org-export-activate-smart-quotes s :html info))
2762 info)))))
2763 ;; Opening quotes: after an object.
2764 (should
2765 (equal
2766 '("&ldquo;quoted&rdquo; text")
2767 (let ((org-export-default-language "en"))
2768 (org-test-with-parsed-data "=verb= \"quoted\" text"
2769 (org-element-map tree 'plain-text
2770 (lambda (s) (org-export-activate-smart-quotes s :html info))
2771 info)))))
2772 ;; Closing quotes: at the end of a paragraph.
2773 (should
2774 (equal
2775 '("Quoted &ldquo;text&rdquo;")
2776 (let ((org-export-default-language "en"))
2777 (org-test-with-parsed-data "Quoted \"text\""
2778 (org-element-map tree 'plain-text
2779 (lambda (s) (org-export-activate-smart-quotes s :html info))
2780 info)))))
2781 ;; Inner quotes: standard test.
2782 (should
2783 (equal '("« outer « inner » outer »")
2784 (let ((org-export-default-language "fr"))
2785 (org-test-with-parsed-data "\"outer 'inner' outer\""
2786 (org-element-map tree 'plain-text
2787 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
2788 info)))))
2789 ;; Apostrophe: standard test.
2790 (should
2791 (equal '("It « shouldn’t » fail")
2792 (let ((org-export-default-language "fr"))
2793 (org-test-with-parsed-data "It \"shouldn't\" fail"
2794 (org-element-map tree 'plain-text
2795 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
2796 info)))))
2797 (should
2798 (equal '("It shouldn’t fail")
2799 (let ((org-export-default-language "fr"))
2800 (org-test-with-parsed-data "It shouldn't fail"
2801 (org-element-map tree 'plain-text
2802 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
2803 info)))))
2804 ;; Apostrophe: before an object.
2805 (should
2806 (equal
2807 '("« a’" " »")
2808 (let ((org-export-default-language "fr"))
2809 (org-test-with-parsed-data "\"a'=b=\""
2810 (org-element-map tree 'plain-text
2811 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
2812 info)))))
2813 ;; Apostrophe: after an object.
2814 (should
2815 (equal '("« " "’s »")
2816 (let ((org-export-default-language "fr"))
2817 (org-test-with-parsed-data "\"=code='s\""
2818 (org-element-map tree 'plain-text
2819 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
2820 info)))))
2821 ;; Special case: isolated quotes.
2822 (should
2823 (equal '("&ldquo;" "&rdquo;")
2824 (let ((org-export-default-language "en"))
2825 (org-test-with-parsed-data "\"$x$\""
2826 (org-element-map tree 'plain-text
2827 (lambda (s) (org-export-activate-smart-quotes s :html info))
2828 info)))))
2829 ;; Smart quotes in secondary strings.
2830 (should
2831 (equal '("&ldquo;" "&rdquo;")
2832 (let ((org-export-default-language "en"))
2833 (org-test-with-parsed-data "* \"$x$\""
2834 (org-element-map tree 'plain-text
2835 (lambda (s) (org-export-activate-smart-quotes s :html info))
2836 info)))))
2837 ;; Smart quotes in document keywords.
2838 (should
2839 (equal '("&ldquo;" "&rdquo;")
2840 (let ((org-export-default-language "en"))
2841 (org-test-with-parsed-data "#+TITLE: \"$x$\""
2842 (org-element-map (plist-get info :title) 'plain-text
2843 (lambda (s) (org-export-activate-smart-quotes s :html info))
2844 info)))))
2845 ;; Smart quotes in parsed affiliated keywords.
2846 (should
2847 (equal '("&ldquo;" "&rdquo;" "Paragraph")
2848 (let ((org-export-default-language "en"))
2849 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
2850 (org-element-map tree 'plain-text
2851 (lambda (s) (org-export-activate-smart-quotes s :html info))
2852 info nil nil t))))))
2856 ;;; Tables
2858 (ert-deftest test-org-export/special-column ()
2859 "Test if the table's special column is properly recognized."
2860 ;; 1. First column is special if it contains only a special marking
2861 ;; characters or empty cells.
2862 (org-test-with-temp-text "
2863 | ! | 1 |
2864 | | 2 |"
2865 (should
2866 (org-export-table-has-special-column-p
2867 (org-element-map
2868 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2869 ;; 2. If the column contains anything else, it isn't special.
2870 (org-test-with-temp-text "
2871 | ! | 1 |
2872 | b | 2 |"
2873 (should-not
2874 (org-export-table-has-special-column-p
2875 (org-element-map
2876 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2877 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
2878 ;; and "!".
2879 (org-test-with-temp-text "
2880 | # | 1 |
2881 | ^ | 2 |
2882 | * | 3 |
2883 | _ | 4 |
2884 | / | 5 |
2885 | $ | 6 |
2886 | ! | 7 |"
2887 (should
2888 (org-export-table-has-special-column-p
2889 (org-element-map
2890 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2891 ;; 4. A first column with only empty cells isn't considered as
2892 ;; special.
2893 (org-test-with-temp-text "
2894 | | 1 |
2895 | | 2 |"
2896 (should-not
2897 (org-export-table-has-special-column-p
2898 (org-element-map
2899 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
2901 (ert-deftest test-org-export/table-row-is-special-p ()
2902 "Test `org-export-table-row-is-special-p' specifications."
2903 ;; 1. A row is special if it has a special marking character in the
2904 ;; special column.
2905 (org-test-with-parsed-data "| ! | 1 |"
2906 (should
2907 (org-export-table-row-is-special-p
2908 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2909 ;; 2. A row is special when its first field is "/"
2910 (org-test-with-parsed-data "
2911 | / | 1 |
2912 | a | b |"
2913 (should
2914 (org-export-table-row-is-special-p
2915 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2916 ;; 3. A row only containing alignment cookies is also considered as
2917 ;; special.
2918 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
2919 (should
2920 (org-export-table-row-is-special-p
2921 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2922 ;; 4. Everything else isn't considered as special.
2923 (org-test-with-parsed-data "| \alpha | | c |"
2924 (should-not
2925 (org-export-table-row-is-special-p
2926 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2927 ;; 5. Table's rules are never considered as special rows.
2928 (org-test-with-parsed-data "|---+---|"
2929 (should-not
2930 (org-export-table-row-is-special-p
2931 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
2933 (ert-deftest test-org-export/has-header-p ()
2934 "Test `org-export-table-has-header-p' specifications."
2935 ;; 1. With an header.
2936 (org-test-with-parsed-data "
2937 | a | b |
2938 |---+---|
2939 | c | d |"
2940 (should
2941 (org-export-table-has-header-p
2942 (org-element-map tree 'table 'identity info 'first-match)
2943 info)))
2944 ;; 2. Without an header.
2945 (org-test-with-parsed-data "
2946 | a | b |
2947 | c | d |"
2948 (should-not
2949 (org-export-table-has-header-p
2950 (org-element-map tree 'table 'identity info 'first-match)
2951 info)))
2952 ;; 3. Don't get fooled with starting and ending rules.
2953 (org-test-with-parsed-data "
2954 |---+---|
2955 | a | b |
2956 | c | d |
2957 |---+---|"
2958 (should-not
2959 (org-export-table-has-header-p
2960 (org-element-map tree 'table 'identity info 'first-match)
2961 info))))
2963 (ert-deftest test-org-export/table-row-group ()
2964 "Test `org-export-table-row-group' specifications."
2965 ;; 1. A rule creates a new group.
2966 (should
2967 (equal '(1 rule 2)
2968 (org-test-with-parsed-data "
2969 | a | b |
2970 |---+---|
2971 | 1 | 2 |"
2972 (org-element-map tree 'table-row
2973 (lambda (row)
2974 (if (eq (org-element-property :type row) 'rule) 'rule
2975 (org-export-table-row-group row info)))))))
2976 ;; 2. Special rows are ignored in count.
2977 (should
2978 (equal
2979 '(rule 1)
2980 (org-test-with-parsed-data "
2981 | / | < | > |
2982 |---|---+---|
2983 | | 1 | 2 |"
2984 (org-element-map tree 'table-row
2985 (lambda (row)
2986 (if (eq (org-element-property :type row) 'rule) 'rule
2987 (org-export-table-row-group row info)))
2988 info))))
2989 ;; 3. Double rules also are ignored in count.
2990 (should
2991 (equal '(1 rule rule 2)
2992 (org-test-with-parsed-data "
2993 | a | b |
2994 |---+---|
2995 |---+---|
2996 | 1 | 2 |"
2997 (org-element-map tree 'table-row
2998 (lambda (row)
2999 (if (eq (org-element-property :type row) 'rule) 'rule
3000 (org-export-table-row-group row info))))))))
3002 (ert-deftest test-org-export/table-row-number ()
3003 "Test `org-export-table-row-number' specifications."
3004 ;; Standard test. Number is 0-indexed.
3005 (should
3006 (equal '(0 1)
3007 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
3008 (org-element-map tree 'table-row
3009 (lambda (row) (org-export-table-row-number row info)) info))))
3010 ;; Number ignores separators.
3011 (should
3012 (equal '(0 1)
3013 (org-test-with-parsed-data "
3014 | a | b | c |
3015 |---+---+---|
3016 | d | e | f |"
3017 (org-element-map tree 'table-row
3018 (lambda (row) (org-export-table-row-number row info)) info))))
3019 ;; Number ignores special rows.
3020 (should
3021 (equal '(0 1)
3022 (org-test-with-parsed-data "
3023 | / | < | > |
3024 | | b | c |
3025 |---+-----+-----|
3026 | | <c> | <c> |
3027 | | e | f |"
3028 (org-element-map tree 'table-row
3029 (lambda (row) (org-export-table-row-number row info)) info)))))
3031 (ert-deftest test-org-export/table-cell-width ()
3032 "Test `org-export-table-cell-width' specifications."
3033 ;; 1. Width is primarily determined by width cookies. If no cookie
3034 ;; is found, cell's width is nil.
3035 (org-test-with-parsed-data "
3036 | / | <l> | <6> | <l7> |
3037 | | a | b | c |"
3038 (should
3039 (equal
3040 '(nil 6 7)
3041 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3042 (org-element-map tree 'table-cell 'identity info)))))
3043 ;; 2. The last width cookie has precedence.
3044 (org-test-with-parsed-data "
3045 | <6> |
3046 | <7> |
3047 | a |"
3048 (should
3049 (equal
3050 '(7)
3051 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3052 (org-element-map tree 'table-cell 'identity info)))))
3053 ;; 3. Valid width cookies must have a specific row.
3054 (org-test-with-parsed-data "| <6> | cell |"
3055 (should
3056 (equal
3057 '(nil nil)
3058 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3059 (org-element-map tree 'table-cell 'identity))))))
3061 (ert-deftest test-org-export/table-cell-alignment ()
3062 "Test `org-export-table-cell-alignment' specifications."
3063 ;; 1. Alignment is primarily determined by alignment cookies.
3064 (should
3065 (equal '(left center right)
3066 (let ((org-table-number-fraction 0.5)
3067 (org-table-number-regexp "^[0-9]+$"))
3068 (org-test-with-parsed-data "| <l> | <c> | <r> |"
3069 (mapcar (lambda (cell)
3070 (org-export-table-cell-alignment cell info))
3071 (org-element-map tree 'table-cell 'identity))))))
3072 ;; 2. The last alignment cookie has precedence.
3073 (should
3074 (equal '(right right right)
3075 (org-test-with-parsed-data "
3076 | <l8> |
3077 | cell |
3078 | <r9> |"
3079 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
3080 (org-element-map tree 'table-cell 'identity)))))
3081 ;; 3. If there's no cookie, cell's contents determine alignment.
3082 ;; A column mostly made of cells containing numbers will align
3083 ;; its cells to the right.
3084 (should
3085 (equal '(right right right)
3086 (let ((org-table-number-fraction 0.5)
3087 (org-table-number-regexp "^[0-9]+$"))
3088 (org-test-with-parsed-data "
3089 | 123 |
3090 | some text |
3091 | 12345 |"
3092 (mapcar (lambda (cell)
3093 (org-export-table-cell-alignment cell info))
3094 (org-element-map tree 'table-cell 'identity))))))
3095 ;; 4. Otherwise, they will be aligned to the left.
3096 (should
3097 (equal '(left left left)
3098 (org-test-with-parsed-data "
3099 | text |
3100 | some text |
3101 | \alpha |"
3102 (mapcar (lambda (cell)
3103 (org-export-table-cell-alignment cell info))
3104 (org-element-map tree 'table-cell 'identity info))))))
3106 (ert-deftest test-org-export/table-cell-borders ()
3107 "Test `org-export-table-cell-borders' specifications."
3108 ;; 1. Recognize various column groups indicators.
3109 (org-test-with-parsed-data "| / | < | > | <> |"
3110 (should
3111 (equal
3112 '((right bottom top) (left bottom top) (right bottom top)
3113 (right left bottom top))
3114 (mapcar (lambda (cell)
3115 (org-export-table-cell-borders cell info))
3116 (org-element-map tree 'table-cell 'identity)))))
3117 ;; 2. Accept shortcuts to define column groups.
3118 (org-test-with-parsed-data "| / | < | < |"
3119 (should
3120 (equal
3121 '((right bottom top) (right left bottom top) (left bottom top))
3122 (mapcar (lambda (cell)
3123 (org-export-table-cell-borders cell info))
3124 (org-element-map tree 'table-cell 'identity)))))
3125 ;; 3. A valid column groups row must start with a "/".
3126 (org-test-with-parsed-data "
3127 | | < |
3128 | a | b |"
3129 (should
3130 (equal '((top) (top) (bottom) (bottom))
3131 (mapcar (lambda (cell)
3132 (org-export-table-cell-borders cell info))
3133 (org-element-map tree 'table-cell 'identity)))))
3134 ;; 4. Take table rules into consideration.
3135 (org-test-with-parsed-data "
3136 | 1 |
3137 |---|
3138 | 2 |"
3139 (should
3140 (equal '((below top) (bottom above))
3141 (mapcar (lambda (cell)
3142 (org-export-table-cell-borders cell info))
3143 (org-element-map tree 'table-cell 'identity)))))
3144 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
3145 ;; (resp. `bottom' and `below') borders. Any special row is
3146 ;; ignored.
3147 (org-test-with-parsed-data "
3148 |---+----|
3149 | / | |
3150 | | 1 |
3151 |---+----|"
3152 (should
3153 (equal '((bottom below top above))
3154 (last
3155 (mapcar (lambda (cell)
3156 (org-export-table-cell-borders cell info))
3157 (org-element-map tree 'table-cell 'identity)))))))
3159 (ert-deftest test-org-export/table-dimensions ()
3160 "Test `org-export-table-dimensions' specifications."
3161 ;; 1. Standard test.
3162 (org-test-with-parsed-data "
3163 | 1 | 2 | 3 |
3164 | 4 | 5 | 6 |"
3165 (should
3166 (equal '(2 . 3)
3167 (org-export-table-dimensions
3168 (org-element-map tree 'table 'identity info 'first-match) info))))
3169 ;; 2. Ignore horizontal rules and special columns.
3170 (org-test-with-parsed-data "
3171 | / | < | > |
3172 | 1 | 2 | 3 |
3173 |---+---+---|
3174 | 4 | 5 | 6 |"
3175 (should
3176 (equal '(2 . 3)
3177 (org-export-table-dimensions
3178 (org-element-map tree 'table 'identity info 'first-match) info)))))
3180 (ert-deftest test-org-export/table-cell-address ()
3181 "Test `org-export-table-cell-address' specifications."
3182 ;; 1. Standard test: index is 0-based.
3183 (org-test-with-parsed-data "| a | b |"
3184 (should
3185 (equal '((0 . 0) (0 . 1))
3186 (org-element-map tree 'table-cell
3187 (lambda (cell) (org-export-table-cell-address cell info))
3188 info))))
3189 ;; 2. Special column isn't counted, nor are special rows.
3190 (org-test-with-parsed-data "
3191 | / | <> |
3192 | | c |"
3193 (should
3194 (equal '(0 . 0)
3195 (org-export-table-cell-address
3196 (car (last (org-element-map tree 'table-cell 'identity info)))
3197 info))))
3198 ;; 3. Tables rules do not count either.
3199 (org-test-with-parsed-data "
3200 | a |
3201 |---|
3202 | b |
3203 |---|
3204 | c |"
3205 (should
3206 (equal '(2 . 0)
3207 (org-export-table-cell-address
3208 (car (last (org-element-map tree 'table-cell 'identity info)))
3209 info))))
3210 ;; 4. Return nil for special cells.
3211 (org-test-with-parsed-data "| / | a |"
3212 (should-not
3213 (org-export-table-cell-address
3214 (org-element-map tree 'table-cell 'identity nil 'first-match)
3215 info))))
3217 (ert-deftest test-org-export/get-table-cell-at ()
3218 "Test `org-export-get-table-cell-at' specifications."
3219 ;; 1. Address ignores special columns, special rows and rules.
3220 (org-test-with-parsed-data "
3221 | / | <> |
3222 | | a |
3223 |---+----|
3224 | | b |"
3225 (should
3226 (equal '("b")
3227 (org-element-contents
3228 (org-export-get-table-cell-at
3229 '(1 . 0)
3230 (org-element-map tree 'table 'identity info 'first-match)
3231 info)))))
3232 ;; 2. Return value for a non-existent address is nil.
3233 (org-test-with-parsed-data "| a |"
3234 (should-not
3235 (org-export-get-table-cell-at
3236 '(2 . 2)
3237 (org-element-map tree 'table 'identity info 'first-match)
3238 info)))
3239 (org-test-with-parsed-data "| / |"
3240 (should-not
3241 (org-export-get-table-cell-at
3242 '(0 . 0)
3243 (org-element-map tree 'table 'identity info 'first-match)
3244 info))))
3246 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
3247 "Test `org-export-table-cell-starts-colgroup-p' specifications."
3248 ;; 1. A cell at a beginning of a row always starts a column group.
3249 (org-test-with-parsed-data "| a |"
3250 (should
3251 (org-export-table-cell-starts-colgroup-p
3252 (org-element-map tree 'table-cell 'identity info 'first-match)
3253 info)))
3254 ;; 2. Special column should be ignored when determining the
3255 ;; beginning of the row.
3256 (org-test-with-parsed-data "
3257 | / | |
3258 | | a |"
3259 (should
3260 (org-export-table-cell-starts-colgroup-p
3261 (org-element-map tree 'table-cell 'identity info 'first-match)
3262 info)))
3263 ;; 2. Explicit column groups.
3264 (org-test-with-parsed-data "
3265 | / | | < |
3266 | a | b | c |"
3267 (should
3268 (equal
3269 '(yes no yes)
3270 (org-element-map tree 'table-cell
3271 (lambda (cell)
3272 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
3273 info)))))
3275 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
3276 "Test `org-export-table-cell-ends-colgroup-p' specifications."
3277 ;; 1. A cell at the end of a row always ends a column group.
3278 (org-test-with-parsed-data "| a |"
3279 (should
3280 (org-export-table-cell-ends-colgroup-p
3281 (org-element-map tree 'table-cell 'identity info 'first-match)
3282 info)))
3283 ;; 2. Special column should be ignored when determining the
3284 ;; beginning of the row.
3285 (org-test-with-parsed-data "
3286 | / | |
3287 | | a |"
3288 (should
3289 (org-export-table-cell-ends-colgroup-p
3290 (org-element-map tree 'table-cell 'identity info 'first-match)
3291 info)))
3292 ;; 3. Explicit column groups.
3293 (org-test-with-parsed-data "
3294 | / | < | |
3295 | a | b | c |"
3296 (should
3297 (equal
3298 '(yes no yes)
3299 (org-element-map tree 'table-cell
3300 (lambda (cell)
3301 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
3302 info)))))
3304 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
3305 "Test `org-export-table-row-starts-rowgroup-p' specifications."
3306 ;; 1. A row at the beginning of a table always starts a row group.
3307 ;; So does a row following a table rule.
3308 (org-test-with-parsed-data "
3309 | a |
3310 |---|
3311 | b |"
3312 (should
3313 (equal
3314 '(yes no yes)
3315 (org-element-map tree 'table-row
3316 (lambda (row)
3317 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
3318 info))))
3319 ;; 2. Special rows should be ignored when determining the beginning
3320 ;; of the row.
3321 (org-test-with-parsed-data "
3322 | / | < |
3323 | | a |
3324 |---+---|
3325 | / | < |
3326 | | b |"
3327 (should
3328 (equal
3329 '(yes no yes)
3330 (org-element-map tree 'table-row
3331 (lambda (row)
3332 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
3333 info)))))
3335 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
3336 "Test `org-export-table-row-ends-rowgroup-p' specifications."
3337 ;; 1. A row at the end of a table always ends a row group. So does
3338 ;; a row preceding a table rule.
3339 (org-test-with-parsed-data "
3340 | a |
3341 |---|
3342 | b |"
3343 (should
3344 (equal
3345 '(yes no yes)
3346 (org-element-map tree 'table-row
3347 (lambda (row)
3348 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
3349 info))))
3350 ;; 2. Special rows should be ignored when determining the beginning
3351 ;; of the row.
3352 (org-test-with-parsed-data "
3353 | | a |
3354 | / | < |
3355 |---+---|
3356 | | b |
3357 | / | < |"
3358 (should
3359 (equal
3360 '(yes no yes)
3361 (org-element-map tree 'table-row
3362 (lambda (row)
3363 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
3364 info)))))
3366 (ert-deftest test-org-export/table-row-in-header-p ()
3367 "Test `org-export-table-row-in-header-p' specifications."
3368 ;; Standard test. Separators are always nil.
3369 (should
3370 (equal
3371 '(yes no no)
3372 (org-test-with-parsed-data "| a |\n|---|\n| b |"
3373 (org-element-map tree 'table-row
3374 (lambda (row)
3375 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
3376 ;; Nil when there is no header.
3377 (should
3378 (equal
3379 '(no no)
3380 (org-test-with-parsed-data "| a |\n| b |"
3381 (org-element-map tree 'table-row
3382 (lambda (row)
3383 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
3385 (ert-deftest test-org-export/table-row-starts-header-p ()
3386 "Test `org-export-table-row-starts-header-p' specifications."
3387 ;; 1. Only the row starting the first row group starts the table
3388 ;; header.
3389 (org-test-with-parsed-data "
3390 | a |
3391 | b |
3392 |---|
3393 | c |"
3394 (should
3395 (equal
3396 '(yes no no no)
3397 (org-element-map tree 'table-row
3398 (lambda (row)
3399 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
3400 info))))
3401 ;; 2. A row cannot start an header if there's no header in the
3402 ;; table.
3403 (org-test-with-parsed-data "
3404 | a |
3405 |---|"
3406 (should-not
3407 (org-export-table-row-starts-header-p
3408 (org-element-map tree 'table-row 'identity info 'first-match)
3409 info))))
3411 (ert-deftest test-org-export/table-row-ends-header-p ()
3412 "Test `org-export-table-row-ends-header-p' specifications."
3413 ;; 1. Only the row starting the first row group starts the table
3414 ;; header.
3415 (org-test-with-parsed-data "
3416 | a |
3417 | b |
3418 |---|
3419 | c |"
3420 (should
3421 (equal
3422 '(no yes no no)
3423 (org-element-map tree 'table-row
3424 (lambda (row)
3425 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
3426 info))))
3427 ;; 2. A row cannot start an header if there's no header in the
3428 ;; table.
3429 (org-test-with-parsed-data "
3430 | a |
3431 |---|"
3432 (should-not
3433 (org-export-table-row-ends-header-p
3434 (org-element-map tree 'table-row 'identity info 'first-match)
3435 info))))
3439 ;;; Tables of Contents
3441 (ert-deftest test-org-export/collect-headlines ()
3442 "Test `org-export-collect-headlines' specifications."
3443 ;; Standard test.
3444 (should
3445 (= 2
3446 (length
3447 (org-test-with-parsed-data "* H1\n** H2"
3448 (org-export-collect-headlines info)))))
3449 ;; Do not collect headlines below optional argument.
3450 (should
3451 (= 1
3452 (length
3453 (org-test-with-parsed-data "* H1\n** H2"
3454 (org-export-collect-headlines info 1)))))
3455 ;; Never collect headlines below maximum headline level.
3456 (should
3457 (= 1
3458 (length
3459 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3460 (org-export-collect-headlines info)))))
3461 (should
3462 (= 1
3463 (length
3464 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3465 (org-export-collect-headlines info 2)))))
3466 ;; Collect headlines locally.
3467 (should
3468 (= 2
3469 (org-test-with-parsed-data "* H1\n** H2\n** H3"
3470 (let ((scope (org-element-map tree 'headline #'identity info t)))
3471 (length (org-export-collect-headlines info nil scope))))))
3472 ;; When collecting locally, optional level is relative.
3473 (should
3474 (= 1
3475 (org-test-with-parsed-data "* H1\n** H2\n*** H3"
3476 (let ((scope (org-element-map tree 'headline #'identity info t)))
3477 (length (org-export-collect-headlines info 1 scope)))))))
3481 ;;; Templates
3483 (ert-deftest test-org-export/inner-template ()
3484 "Test `inner-template' translator specifications."
3485 (should
3486 (equal "Success!"
3487 (org-test-with-temp-text "* Headline"
3488 (org-export-as
3489 (org-export-create-backend
3490 :transcoders
3491 '((inner-template . (lambda (contents info) "Success!"))
3492 (headline . (lambda (h c i) "Headline"))))))))
3493 ;; Inner template is applied even in a "body-only" export.
3494 (should
3495 (equal "Success!"
3496 (org-test-with-temp-text "* Headline"
3497 (org-export-as
3498 (org-export-create-backend
3499 :transcoders '((inner-template . (lambda (c i) "Success!"))
3500 (headline . (lambda (h c i) "Headline"))))
3501 nil nil 'body-only)))))
3503 (ert-deftest test-org-export/template ()
3504 "Test `template' translator specifications."
3505 (should
3506 (equal "Success!"
3507 (org-test-with-temp-text "* Headline"
3508 (org-export-as
3509 (org-export-create-backend
3510 :transcoders '((template . (lambda (contents info) "Success!"))
3511 (headline . (lambda (h c i) "Headline"))))))))
3512 ;; Template is not applied in a "body-only" export.
3513 (should-not
3514 (equal "Success!"
3515 (org-test-with-temp-text "* Headline"
3516 (org-export-as
3517 (org-export-create-backend
3518 :transcoders '((template . (lambda (contents info) "Success!"))
3519 (headline . (lambda (h c i) "Headline"))))
3520 nil nil 'body-only)))))
3524 ;;; Topology
3526 (ert-deftest test-org-export/get-next-element ()
3527 "Test `org-export-get-next-element' specifications."
3528 ;; Standard test.
3529 (should
3530 (equal "b"
3531 (org-test-with-parsed-data "* Headline\n*a* b"
3532 (org-export-get-next-element
3533 (org-element-map tree 'bold 'identity info t) info))))
3534 ;; Return nil when no previous element.
3535 (should-not
3536 (org-test-with-parsed-data "* Headline\na *b*"
3537 (org-export-get-next-element
3538 (org-element-map tree 'bold 'identity info t) info)))
3539 ;; Non-exportable elements are ignored.
3540 (should-not
3541 (let ((org-export-with-timestamps nil))
3542 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
3543 (org-export-get-next-element
3544 (org-element-map tree 'entity 'identity info t) info))))
3545 ;; Find next element in secondary strings.
3546 (should
3547 (eq 'verbatim
3548 (org-test-with-parsed-data "* a =verb="
3549 (org-element-type
3550 (org-export-get-next-element
3551 (org-element-map tree 'plain-text 'identity info t) info)))))
3552 (should
3553 (eq 'verbatim
3554 (org-test-with-parsed-data "* /italic/ =verb="
3555 (org-element-type
3556 (org-export-get-next-element
3557 (org-element-map tree 'italic 'identity info t) info)))))
3558 ;; Find next element in document keywords.
3559 (should
3560 (eq 'verbatim
3561 (org-test-with-parsed-data "#+TITLE: a =verb="
3562 (org-element-type
3563 (org-export-get-next-element
3564 (org-element-map
3565 (plist-get info :title) 'plain-text 'identity info t) info)))))
3566 ;; Find next element in parsed affiliated keywords.
3567 (should
3568 (eq 'verbatim
3569 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
3570 (org-element-type
3571 (org-export-get-next-element
3572 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3573 ;; With optional argument N, return a list containing all the
3574 ;; following elements.
3575 (should
3576 (equal
3577 '(bold code underline)
3578 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3579 (mapcar 'car
3580 (org-export-get-next-element
3581 (org-element-map tree 'italic 'identity info t) info t)))))
3582 ;; When N is a positive integer, return a list containing up to
3583 ;; N following elements.
3584 (should
3585 (equal
3586 '(bold code)
3587 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3588 (mapcar 'car
3589 (org-export-get-next-element
3590 (org-element-map tree 'italic 'identity info t) info 2))))))
3592 (ert-deftest test-org-export/get-previous-element ()
3593 "Test `org-export-get-previous-element' specifications."
3594 ;; Standard test.
3595 (should
3596 (equal "a "
3597 (org-test-with-parsed-data "* Headline\na *b*"
3598 (org-export-get-previous-element
3599 (org-element-map tree 'bold 'identity info t) info))))
3600 ;; Return nil when no previous element.
3601 (should-not
3602 (org-test-with-parsed-data "* Headline\n*a* b"
3603 (org-export-get-previous-element
3604 (org-element-map tree 'bold 'identity info t) info)))
3605 ;; Non-exportable elements are ignored.
3606 (should-not
3607 (let ((org-export-with-timestamps nil))
3608 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
3609 (org-export-get-previous-element
3610 (org-element-map tree 'entity 'identity info t) info))))
3611 ;; Find previous element in secondary strings.
3612 (should
3613 (eq 'verbatim
3614 (org-test-with-parsed-data "* =verb= a"
3615 (org-element-type
3616 (org-export-get-previous-element
3617 (org-element-map tree 'plain-text 'identity info t) info)))))
3618 (should
3619 (eq 'verbatim
3620 (org-test-with-parsed-data "* =verb= /italic/"
3621 (org-element-type
3622 (org-export-get-previous-element
3623 (org-element-map tree 'italic 'identity info t) info)))))
3624 ;; Find previous element in document keywords.
3625 (should
3626 (eq 'verbatim
3627 (org-test-with-parsed-data "#+TITLE: =verb= a"
3628 (org-element-type
3629 (org-export-get-previous-element
3630 (org-element-map
3631 (plist-get info :title) 'plain-text 'identity info t) info)))))
3632 ;; Find previous element in parsed affiliated keywords.
3633 (should
3634 (eq 'verbatim
3635 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
3636 (org-element-type
3637 (org-export-get-previous-element
3638 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3639 ;; With optional argument N, return a list containing up to
3640 ;; N previous elements.
3641 (should
3642 (equal '(underline italic bold)
3643 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3644 (mapcar 'car
3645 (org-export-get-previous-element
3646 (org-element-map tree 'code 'identity info t) info t)))))
3647 ;; When N is a positive integer, return a list containing up to
3648 ;; N previous elements.
3649 (should
3650 (equal '(italic bold)
3651 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3652 (mapcar 'car
3653 (org-export-get-previous-element
3654 (org-element-map tree 'code 'identity info t) info 2))))))
3657 (provide 'test-ox)
3658 ;;; test-org-export.el end here