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