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