ox: Activate lexical binding
[org-mode.git] / testing / lisp / test-ox.el
blob29171277ac7f19f2f73247f886516595e4cc44a5
1 ;;; test-ox.el --- Tests for ox.el -*- lexical-binding: t; -*-
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
32 (mapcar (lambda (type)
33 (cons type
34 (lambda (o c _)
35 (funcall
36 (intern (format "org-element-%s-interpreter" type))
37 o c))))
38 (append org-element-all-elements org-element-all-objects))))
40 (defmacro org-test-with-parsed-data (data &rest body)
41 "Execute body with parsed data available.
42 DATA is a string containing the data to be parsed. BODY is the
43 body to execute. Parse tree is available under the `tree'
44 variable, and communication channel under `info'."
45 (declare (debug (form body)) (indent 1))
46 `(org-test-with-temp-text ,data
47 (org-export--delete-comments)
48 (let* ((tree (org-element-parse-buffer))
49 (info (org-export-get-environment)))
50 (org-export--prune-tree tree info)
51 (org-export--remove-uninterpreted-data tree info)
52 (let ((info (org-combine-plists
53 info (org-export-collect-tree-properties tree info))))
54 ,@body))))
58 ;;; Internal Tests
60 (ert-deftest test-org-export/bind-keyword ()
61 "Test reading #+BIND: keywords."
62 ;; Test with `org-export-allow-bind-keywords' set to t.
63 (should
64 (org-test-with-temp-text "#+BIND: test-ox-var value"
65 (let ((org-export-allow-bind-keywords t))
66 (org-export-get-environment)
67 (eq test-ox-var 'value))))
68 ;; Test with `org-export-allow-bind-keywords' set to nil.
69 (should-not
70 (org-test-with-temp-text "#+BIND: test-ox-var value"
71 (let ((org-export-allow-bind-keywords nil))
72 (org-export-get-environment)
73 (boundp 'test-ox-var))))
74 ;; BIND keywords are case-insensitive.
75 (should
76 (org-test-with-temp-text "#+bind: test-ox-var value"
77 (let ((org-export-allow-bind-keywords t))
78 (org-export-get-environment)
79 (eq test-ox-var 'value))))
80 ;; Preserve order of BIND keywords.
81 (should
82 (org-test-with-temp-text "#+BIND: test-ox-var 1\n#+BIND: test-ox-var 2"
83 (let ((org-export-allow-bind-keywords t))
84 (org-export-get-environment)
85 (eq test-ox-var 2))))
86 ;; Read BIND keywords in setup files.
87 (should
88 (org-test-with-temp-text
89 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
90 (let ((org-export-allow-bind-keywords t))
91 (org-export-get-environment)
92 (eq variable 'value))))
93 ;; Verify that bound variables are seen during export.
94 (should
95 (equal "Yes\n"
96 (org-test-with-temp-text "#+BIND: test-ox-var value"
97 (let ((org-export-allow-bind-keywords t))
98 (org-export-as
99 (org-export-create-backend
100 :transcoders
101 '((section . (lambda (s c i)
102 (if (eq test-ox-var 'value) "Yes" "No")))))))))))
104 (ert-deftest test-org-export/parse-option-keyword ()
105 "Test reading all standard #+OPTIONS: items."
106 (should
107 (equal
108 (org-export--parse-option-keyword
109 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
110 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
111 stat:t title:t")
112 '(:headline-levels
113 1 :section-numbers t :preserve-breaks t :time-stamp-file t
114 :with-archived-trees t :with-author t :with-creator t :with-drawers t
115 :with-email t :with-emphasize t :with-entities t :with-fixed-width t
116 :with-footnotes t :with-priority t :with-special-strings t
117 :with-sub-superscript t :with-toc t :with-tables t :with-tags t
118 :with-tasks t :with-timestamps t :with-todo-keywords t
119 :with-inlinetasks nil :with-statistics-cookies t :with-title t)))
120 ;; Test some special values.
121 (should
122 (equal
123 (org-export--parse-option-keyword
124 "arch:headline d:(\"TEST\") ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
125 '(:with-archived-trees
126 headline :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
127 :with-tags not-in-toc :with-tasks todo :section-numbers 2
128 :with-timestamps active)))
129 ;; Test back-end specific values.
130 (should
131 (equal
132 (org-export--parse-option-keyword
133 "opt:t" (org-export-create-backend :options '((:option nil "opt"))))
134 '(:option t))))
136 (ert-deftest test-org-export/get-inbuffer-options ()
137 "Test reading all standard export keywords."
138 ;; Properties should follow buffer order.
139 (should
140 (equal
141 (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
142 (org-export--get-inbuffer-options))
143 '(:language "fr" :creator "Me" :email "email")))
144 ;; Test `space' behaviour.
145 (should
146 (equal
147 (let ((back-end (org-export-create-backend
148 :options '((:keyword "KEYWORD" nil nil space)))))
149 (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: spaces"
150 (org-export--get-inbuffer-options back-end)))
151 '(:keyword "With spaces")))
152 ;; Test `newline' behaviour.
153 (should
154 (equal
155 (let ((back-end (org-export-create-backend
156 :options '((:keyword "KEYWORD" nil nil newline)))))
157 (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: two lines"
158 (org-export--get-inbuffer-options back-end)))
159 '(:keyword "With\ntwo lines")))
160 ;; Test `split' behaviour.
161 (should
162 (equal
163 (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
164 (org-export--get-inbuffer-options))
165 '(:select-tags ("a" "b"))))
166 ;; Test `parse' behaviour. `parse' implies `space' but preserve
167 ;; line breaks. Multi-line objects are allowed.
168 (should
169 (org-element-map
170 (org-test-with-temp-text "#+TITLE: *bold*"
171 (plist-get (org-export--get-inbuffer-options) :title))
172 'bold #'identity nil t))
173 (should
174 (equal
175 (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
176 (plist-get (org-export--get-inbuffer-options) :title))
177 '("Some title with spaces")))
178 (should
179 (org-element-map
180 (org-test-with-temp-text "#+TITLE: Some title\\\\\n#+TITLE: with spaces"
181 (plist-get (org-export--get-inbuffer-options) :title))
182 'line-break #'identity nil t))
183 (should
184 (org-element-map
185 (org-test-with-temp-text "#+TITLE: *bold\n#+TITLE: sentence*"
186 (plist-get (org-export--get-inbuffer-options) :title))
187 'bold #'identity nil t))
188 ;; Options set through SETUPFILE.
189 (should
190 (equal
191 (org-test-with-temp-text
192 (format "#+DESCRIPTION: l1
193 #+LANGUAGE: es
194 #+SELECT_TAGS: a
195 #+TITLE: a
196 #+SETUPFILE: \"%s/examples/setupfile.org\"
197 #+LANGUAGE: fr
198 #+SELECT_TAGS: c
199 #+TITLE: c"
200 org-test-dir)
201 (org-export--get-inbuffer-options))
202 '(:language "fr" :select-tags ("a" "b" "c") :title ("a b c"))))
203 ;; More than one property can refer to the same buffer keyword.
204 (should
205 (equal '(:k2 "value" :k1 "value")
206 (let ((backend (org-export-create-backend
207 :options '((:k1 "KEYWORD")
208 (:k2 "KEYWORD")))))
209 (org-test-with-temp-text "#+KEYWORD: value"
210 (org-export--get-inbuffer-options backend)))))
211 ;; Keywords in commented subtrees are ignored.
212 (should-not
213 (equal "Me"
214 (org-test-with-parsed-data "* COMMENT H1\n#+AUTHOR: Me"
215 (plist-get info :author))))
216 (should-not
217 (equal "Mine"
218 (org-test-with-parsed-data "* COMMENT H1\n** H2\n#+EMAIL: Mine"
219 (plist-get info :email)))))
221 (ert-deftest test-org-export/get-subtree-options ()
222 "Test setting options from headline's properties."
223 ;; EXPORT_TITLE.
224 (should
225 (equal '("Subtree Title")
226 (org-test-with-temp-text "#+TITLE: Title
227 * Headline<point>
228 :PROPERTIES:
229 :EXPORT_TITLE: Subtree Title
230 :END:
231 Paragraph"
232 (plist-get (org-export-get-environment nil t) :title))))
233 ;; EXPORT_OPTIONS.
234 (should
235 (= 2
236 (org-test-with-temp-text "#+OPTIONS: H:1
237 * Headline<point>
238 :PROPERTIES:
239 :EXPORT_OPTIONS: H:2
240 :END:
241 Paragraph"
242 (plist-get (org-export-get-environment nil t) :headline-levels))))
243 ;; EXPORT_DATE.
244 (should
245 (equal '("29-03-2012")
246 (org-test-with-temp-text "#+DATE: today
247 * Headline<point>
248 :PROPERTIES:
249 :EXPORT_DATE: 29-03-2012
250 :END:
251 Paragraph"
252 (plist-get (org-export-get-environment nil t) :date))))
253 ;; Properties with `split' behaviour are stored as a list of
254 ;; strings.
255 (should
256 (equal '("a" "b")
257 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
258 * Headline<point>
259 :PROPERTIES:
260 :EXPORT_EXCLUDE_TAGS: a b
261 :END:
262 Paragraph"
263 (plist-get (org-export-get-environment nil t) :exclude-tags))))
264 ;; Handle :PROPERTY+: syntax.
265 (should
266 (equal '("a" "b")
267 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
268 * Headline<point>
269 :PROPERTIES:
270 :EXPORT_EXCLUDE_TAGS: a
271 :EXPORT_EXCLUDE_TAGS+: b
272 :END:
273 Paragraph"
274 (plist-get (org-export-get-environment nil t) :exclude-tags))))
275 ;; Export properties are case-insensitive.
276 (should
277 (equal '("29-03-2012")
278 (org-test-with-temp-text "* Headline
279 :PROPERTIES:
280 :EXPORT_Date: 29-03-2012
281 :END:
282 Paragraph"
283 (plist-get (org-export-get-environment nil t) :date))))
284 ;; Still grab correct options when section above is empty.
285 (should
286 (equal '("H1")
287 (org-test-with-temp-text "* H1\n** H11\n** H12<point>"
288 (plist-get (org-export-get-environment nil t) :title))))
289 ;; More than one property can refer to the same node property.
290 (should
291 (equal '("1" "1")
292 (org-test-with-temp-text
293 "* H\n:PROPERTIES:\n:EXPORT_A: 1\n:END:\n<point>"
294 (let* ((backend (org-export-create-backend
295 :options '((:k1 "A")
296 (:k2 "A"))))
297 (options (org-export-get-environment backend t)))
298 (list (plist-get options :k1) (plist-get options :k2)))))))
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 (list (cons 'headline
1541 (lambda (headline contents info)
1542 (org-export-data
1543 (org-element-property :title headline)
1544 info)))
1545 (cons 'plain-text (lambda (text info) "Success"))))))
1546 (org-export-string-as
1547 "* Test"
1548 (org-export-create-backend
1549 :transcoders
1550 (list (cons 'headline
1551 (lambda (headline contents info)
1552 (org-export-with-backend
1553 test-back-end headline contents info))))))))))
1555 (ert-deftest test-org-export/data-with-backend ()
1556 "Test `org-export-data-with-backend' specifications."
1557 ;; Error when calling an undefined back-end.
1558 (should-error (org-export-data-with-backend nil "nil" nil))
1559 ;; Otherwise, export data recursively, using correct back-end.
1560 (should
1561 (equal
1562 "Success!"
1563 (org-export-data-with-backend
1564 '(bold nil "Test")
1565 (org-export-create-backend
1566 :transcoders
1567 '((plain-text . (lambda (text info) "Success"))
1568 (bold . (lambda (bold contents info) (concat contents "!")))))
1569 '(:with-emphasize t)))))
1573 ;;; Export Snippets
1575 (ert-deftest test-org-export/export-snippet ()
1576 "Test export snippets transcoding."
1577 ;; Standard test.
1578 (org-test-with-temp-text "@@test:A@@@@t:B@@"
1579 (let ((backend (org-test-default-backend)))
1580 (setf (org-export-backend-name backend) 'test)
1581 (setf (org-export-backend-transcoders backend)
1582 (cons (cons 'export-snippet
1583 (lambda (snippet contents info)
1584 (when (eq (org-export-snippet-backend snippet) 'test)
1585 (org-element-property :value snippet))))
1586 (org-export-backend-transcoders backend)))
1587 (let ((org-export-snippet-translation-alist nil))
1588 (should (equal (org-export-as backend) "A\n")))
1589 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
1590 (should (equal (org-export-as backend) "AB\n")))))
1591 ;; Ignored export snippets do not remove any blank.
1592 (should
1593 (equal "begin end\n"
1594 (org-test-with-parsed-data "begin @@test:A@@ end"
1595 (org-export-data-with-backend
1596 tree
1597 (org-export-create-backend
1598 :transcoders
1599 '((paragraph . (lambda (paragraph contents info) contents))
1600 (section . (lambda (section contents info) contents))))
1601 info)))))
1605 ;;; Footnotes
1607 (ert-deftest test-org-export/footnote-first-reference-p ()
1608 "Test `org-export-footnote-first-reference-p' specifications."
1609 (should
1610 (equal
1611 '(t nil)
1612 (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
1613 (let (result)
1614 (org-export-as
1615 (org-export-create-backend
1616 :transcoders
1617 `(,(cons 'footnote-reference
1618 (lambda (f c i)
1619 (push (org-export-footnote-first-reference-p f i)
1620 result)
1621 ""))
1622 (section . (lambda (s c i) c))
1623 (paragraph . (lambda (p c i) c))))
1624 nil nil nil '(:with-footnotes t))
1625 (nreverse result)))))
1626 ;; Limit check to DATA, when non-nil.
1627 (should
1628 (equal
1629 '(nil t)
1630 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
1631 (let (result)
1632 (org-element-map tree 'footnote-reference
1633 (lambda (ref)
1634 (push
1635 (org-export-footnote-first-reference-p
1636 ref info (org-element-map tree 'headline #'identity info t))
1637 result))
1638 info)
1639 (nreverse result)))))
1640 (should
1641 (equal
1642 '(t nil)
1643 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
1644 (let (result)
1645 (org-element-map tree 'footnote-reference
1646 (lambda (ref)
1647 (push (org-export-footnote-first-reference-p ref info) result))
1648 info)
1649 (nreverse result)))))
1650 ;; If optional argument BODY-FIRST is non-nil, first find footnote
1651 ;; in the main body of the document. Otherwise, enter footnote
1652 ;; definitions when they are encountered.
1653 (should
1654 (equal
1655 '(t nil)
1656 (org-test-with-temp-text
1657 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
1658 (let (result)
1659 (org-export-as
1660 (org-export-create-backend
1661 :transcoders
1662 `(,(cons 'footnote-reference
1663 (lambda (f c i)
1664 (when (org-element-lineage f '(drawer))
1665 (push (org-export-footnote-first-reference-p f i nil)
1666 result))
1667 ""))
1668 (drawer . (lambda (d c i) c))
1669 (footnote-definition . (lambda (d c i) c))
1670 (section . (lambda (s c i) c))
1671 (paragraph . (lambda (p c i) c))))
1672 nil nil nil '(:with-footnotes t))
1673 (nreverse result)))))
1674 (should
1675 (equal
1676 '(t t)
1677 (org-test-with-temp-text
1678 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
1679 (let (result)
1680 (org-export-as
1681 (org-export-create-backend
1682 :transcoders
1683 `(,(cons 'footnote-reference
1684 (lambda (f c i)
1685 (when (org-element-lineage f '(drawer))
1686 (push (org-export-footnote-first-reference-p f i nil t)
1687 result))
1688 ""))
1689 (drawer . (lambda (d c i) c))
1690 (footnote-definition . (lambda (d c i) c))
1691 (section . (lambda (s c i) c))
1692 (paragraph . (lambda (p c i) c))))
1693 nil nil nil '(:with-footnotes t))
1694 (nreverse result))))))
1696 (ert-deftest test-org-export/get-footnote-number ()
1697 "Test `org-export-get-footnote-number' specifications."
1698 (should
1699 (equal '(1 2 1)
1700 (org-test-with-parsed-data
1701 "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
1702 (org-element-map tree 'footnote-reference
1703 (lambda (ref) (org-export-get-footnote-number ref info))
1704 info))))
1705 ;; Anonymous footnotes all get a new number.
1706 (should
1707 (equal '(1 2)
1708 (org-test-with-parsed-data
1709 "Text[fn::anon1][fn::anon2]"
1710 (org-element-map tree 'footnote-reference
1711 (lambda (ref) (org-export-get-footnote-number ref info))
1712 info))))
1713 ;; Test nested footnotes order.
1714 (should
1715 (equal
1716 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (3 . "fn:3") (4))
1717 (org-test-with-parsed-data
1718 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
1719 (org-element-map tree 'footnote-reference
1720 (lambda (ref)
1721 (cons (org-export-get-footnote-number ref info)
1722 (org-element-property :label ref)))
1723 info))))
1724 ;; Limit number to provided DATA, when non-nil.
1725 (should
1726 (equal
1727 '(1)
1728 (org-test-with-parsed-data
1729 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
1730 (org-element-map tree 'footnote-reference
1731 (lambda (ref)
1732 (org-export-get-footnote-number
1733 ref info (org-element-map tree 'headline #'identity info t)))
1734 info))))
1735 (should
1736 (equal
1737 '(1 2)
1738 (org-test-with-parsed-data
1739 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
1740 (org-element-map tree 'footnote-reference
1741 (lambda (ref) (org-export-get-footnote-number ref info))
1742 info))))
1743 ;; With a non-nil BODY-FIRST optional argument, first check body,
1744 ;; then footnote definitions.
1745 (should
1746 (equal
1747 '(("fn:1" . 1) ("fn:2" . 2) ("fn:3" . 3) ("fn:3" . 3))
1748 (org-test-with-parsed-data
1749 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
1750 (org-element-map tree 'footnote-reference
1751 (lambda (ref)
1752 (cons (org-element-property :label ref)
1753 (org-export-get-footnote-number ref info nil t)))
1754 info))))
1755 (should
1756 (equal
1757 '(("fn:1" . 1) ("fn:2" . 3) ("fn:3" . 2) ("fn:3" . 2))
1758 (org-test-with-parsed-data
1759 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
1760 (org-element-map tree 'footnote-reference
1761 (lambda (ref)
1762 (cons (org-element-property :label ref)
1763 (org-export-get-footnote-number ref info nil)))
1764 info)))))
1766 (ert-deftest test-org-export/collect-footnote-definitions ()
1767 "Test `org-export-collect-footnote-definitions' specifications."
1768 (should
1769 (= 4
1770 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1772 \[fn:2] B [fn:3] [fn::D].
1774 \[fn:3] C."
1775 (length (org-export-collect-footnote-definitions info)))))
1776 ;; Limit number to provided DATA, when non-nil.
1777 (should
1778 (equal
1779 '((1 "fn:2"))
1780 (org-test-with-parsed-data
1781 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
1782 (mapcar #'butlast
1783 (org-export-collect-footnote-definitions
1784 info (org-element-map tree 'headline #'identity info t))))))
1785 (should
1786 (equal
1787 '((1 "fn:1") (2 "fn:2"))
1788 (org-test-with-parsed-data
1789 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
1790 (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
1791 ;; With optional argument BODY-FIRST, first check body, then
1792 ;; footnote definitions.
1793 (should
1794 (equal '("fn:1" "fn:3" "fn:2" nil)
1795 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1797 \[fn:2] B [fn:3] [fn::D].
1799 \[fn:3] C."
1800 (mapcar (lambda (e) (nth 1 e))
1801 (org-export-collect-footnote-definitions info nil t)))))
1802 (should-not
1803 (equal '("fn:1" "fn:3" "fn:2" nil)
1804 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1806 \[fn:2] B [fn:3] [fn::D].
1808 \[fn:3] C."
1809 (mapcar (lambda (e) (nth 1 e))
1810 (org-export-collect-footnote-definitions info))))))
1812 (ert-deftest test-org-export/footnotes ()
1813 "Miscellaneous tests on footnotes."
1814 (let ((org-footnote-section nil)
1815 (org-export-with-footnotes t))
1816 ;; Read every type of footnote.
1817 (should
1818 (equal
1819 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
1820 (org-test-with-parsed-data
1821 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
1822 (org-element-map tree 'footnote-reference
1823 (lambda (ref)
1824 (let ((def (org-export-get-footnote-definition ref info)))
1825 (cons (org-export-get-footnote-number ref info)
1826 (if (eq (org-element-property :type ref) 'inline) (car def)
1827 (car (org-element-contents
1828 (car (org-element-contents def))))))))
1829 info))))
1830 ;; Test nested footnote in invisible definitions.
1831 (should
1832 (= 2
1833 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
1834 (narrow-to-region (point) (line-end-position))
1835 (catch 'exit
1836 (org-export-as
1837 (org-export-create-backend
1838 :transcoders
1839 '((section
1841 (lambda (s c i)
1842 (throw 'exit (length
1843 (org-export-collect-footnote-definitions
1844 i))))))))))))
1845 ;; Test export of footnotes defined outside parsing scope.
1846 (should
1847 (equal
1848 "ParagraphOut of scope\n"
1849 (org-test-with-temp-text "[fn:1] Out of scope
1850 * Title
1851 <point>Paragraph[fn:1]"
1852 (let ((backend (org-test-default-backend)))
1853 (setf (org-export-backend-transcoders backend)
1854 (append
1855 (list (cons 'footnote-reference
1856 (lambda (fn contents info)
1857 (org-element-interpret-data
1858 (org-export-get-footnote-definition fn info))))
1859 (cons 'footnote-definition #'ignore)
1860 (cons 'headline #'ignore))
1861 (org-export-backend-transcoders backend)))
1862 (org-export-as backend 'subtree)))))
1863 ;; Footnotes without a definition should throw an error.
1864 (should-error
1865 (org-test-with-parsed-data "Text[fn:1]"
1866 (org-export-get-footnote-definition
1867 (org-element-map tree 'footnote-reference #'identity info t) info)))
1868 ;; Footnote section should be ignored in TOC and in headlines
1869 ;; numbering.
1870 (should
1871 (= 1 (let ((org-footnote-section "Footnotes"))
1872 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
1873 (org-export-collect-headlines info))))))
1874 (should
1875 (equal '(2)
1876 (let ((org-footnote-section "Footnotes"))
1877 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
1878 (org-element-map tree 'headline
1879 (lambda (hl)
1880 (when (equal (org-element-property :raw-value hl) "H2")
1881 (org-export-get-headline-number hl info)))
1882 info t)))))))
1886 ;;; Headlines and Inlinetasks
1888 (ert-deftest test-org-export/get-relative-level ()
1889 "Test `org-export-get-relative-level' specifications."
1890 ;; Standard test.
1891 (should
1892 (equal '(1 2)
1893 (let ((org-odd-levels-only nil))
1894 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1895 (org-element-map tree 'headline
1896 (lambda (h) (org-export-get-relative-level h info))
1897 info)))))
1898 ;; Missing levels
1899 (should
1900 (equal '(1 3)
1901 (let ((org-odd-levels-only nil))
1902 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
1903 (org-element-map tree 'headline
1904 (lambda (h) (org-export-get-relative-level h info))
1905 info))))))
1907 (ert-deftest test-org-export/low-level-p ()
1908 "Test `org-export-low-level-p' specifications."
1909 (should
1910 (equal
1911 '(no yes)
1912 (let ((org-odd-levels-only nil))
1913 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1914 (org-element-map tree 'headline
1915 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
1916 (plist-put info :headline-levels 1)))))))
1918 (ert-deftest test-org-export/get-headline-number ()
1919 "Test `org-export-get-headline-number' specifications."
1920 ;; Standard test.
1921 (should
1922 (equal
1923 '((1) (1 1))
1924 (let ((org-odd-levels-only nil))
1925 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1926 (org-element-map tree 'headline
1927 (lambda (h) (org-export-get-headline-number h info))
1928 info)))))
1929 ;; Missing levels are replaced with 0.
1930 (should
1931 (equal
1932 '((1) (1 0 1))
1933 (let ((org-odd-levels-only nil))
1934 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
1935 (org-element-map tree 'headline
1936 (lambda (h) (org-export-get-headline-number h info))
1937 info))))))
1939 (ert-deftest test-org-export/numbered-headline-p ()
1940 "Test `org-export-numbered-headline-p' specifications."
1941 ;; If `:section-numbers' is nil, never number headlines.
1942 (should-not
1943 (org-test-with-parsed-data "* Headline"
1944 (org-element-map tree 'headline
1945 (lambda (h) (org-export-numbered-headline-p h info))
1946 (plist-put info :section-numbers nil))))
1947 ;; If `:section-numbers' is a number, only number headlines with
1948 ;; a level greater that it.
1949 (should
1950 (equal
1951 '(yes no)
1952 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1953 (org-element-map tree 'headline
1954 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
1955 (plist-put info :section-numbers 1)))))
1956 ;; Otherwise, headlines are always numbered.
1957 (should
1958 (org-test-with-parsed-data "* Headline"
1959 (org-element-map tree 'headline
1960 (lambda (h) (org-export-numbered-headline-p h info))
1961 (plist-put info :section-numbers t))))
1962 ;; With #+OPTIONS: num:nil all headlines are unnumbered.
1963 (should-not
1964 (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
1965 (org-export-numbered-headline-p
1966 (org-element-map tree 'headline 'identity info t)
1967 info)))
1968 ;; Headlines with a non-nil UNNUMBERED property are not numbered.
1969 (should-not
1970 (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
1971 (org-export-numbered-headline-p
1972 (org-element-map tree 'headline #'identity info t)
1973 info)))
1974 ;; UNNUMBERED ignores inheritance. Any non-nil value among
1975 ;; ancestors disables numbering.
1976 (should
1977 (org-test-with-parsed-data
1978 "* H
1979 :PROPERTIES:
1980 :UNNUMBERED: t
1981 :END:
1982 ** H2
1983 :PROPERTIES:
1984 :UNNUMBERED: nil
1985 :END:
1986 *** H3"
1987 (org-every
1988 (lambda (h) (not (org-export-numbered-headline-p h info)))
1989 (org-element-map tree 'headline #'identity info)))))
1991 (ert-deftest test-org-export/number-to-roman ()
1992 "Test `org-export-number-to-roman' specifications."
1993 ;; If number is negative, return it as a string.
1994 (should (equal (org-export-number-to-roman -1) "-1"))
1995 ;; Otherwise, return it as a roman number.
1996 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
1998 (ert-deftest test-org-export/get-optional-title ()
1999 "Test `org-export-get-alt-title' specifications."
2000 ;; If ALT_TITLE property is defined, use it.
2001 (should
2002 (equal '("opt")
2003 (org-test-with-parsed-data
2004 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
2005 (org-export-get-alt-title
2006 (org-element-map tree 'headline 'identity info t)
2007 info))))
2008 ;; Otherwise, fall-back to regular title.
2009 (should
2010 (equal '("Headline")
2011 (org-test-with-parsed-data "* Headline"
2012 (org-export-get-alt-title
2013 (org-element-map tree 'headline 'identity info t)
2014 info)))))
2016 (ert-deftest test-org-export/get-tags ()
2017 "Test `org-export-get-tags' specifications."
2018 (let ((org-export-exclude-tags '("noexport"))
2019 (org-export-select-tags '("export")))
2020 ;; Standard test: tags which are not a select tag, an exclude tag,
2021 ;; or specified as optional argument shouldn't be ignored.
2022 (should
2023 (org-test-with-parsed-data "* Headline :tag:"
2024 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2025 info)))
2026 ;; Exclude tags are removed.
2027 (should-not
2028 (org-test-with-parsed-data "* Headline :noexport:"
2029 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2030 info)))
2031 ;; Select tags are removed.
2032 (should-not
2033 (org-test-with-parsed-data "* Headline :export:"
2034 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2035 info)))
2036 (should
2037 (equal
2038 '("tag")
2039 (org-test-with-parsed-data "* Headline :tag:export:"
2040 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2041 info))))
2042 ;; Tags provided in the optional argument are also ignored.
2043 (should-not
2044 (org-test-with-parsed-data "* Headline :ignore:"
2045 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2046 info '("ignore"))))
2047 ;; Allow tag inheritance.
2048 (should
2049 (equal
2050 '(("tag") ("tag"))
2051 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
2052 (org-element-map tree 'headline
2053 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
2054 ;; Tag inheritance checks FILETAGS keywords.
2055 (should
2056 (equal
2057 '(("a" "b" "tag"))
2058 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
2059 (org-element-map tree 'headline
2060 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
2062 (ert-deftest test-org-export/get-node-property ()
2063 "Test`org-export-get-node-property' specifications."
2064 ;; Standard test.
2065 (should
2066 (equal "value"
2067 (org-test-with-parsed-data "* Headline
2068 :PROPERTIES:
2069 :prop: value
2070 :END:"
2071 (org-export-get-node-property
2072 :PROP (org-element-map tree 'headline 'identity nil t)))))
2073 ;; Test inheritance.
2074 (should
2075 (equal "value"
2076 (org-test-with-parsed-data "* Parent
2077 :PROPERTIES:
2078 :prop: value
2079 :END:
2080 ** Headline
2081 Paragraph"
2082 (org-export-get-node-property
2083 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
2084 ;; Cannot return a value before the first headline.
2085 (should-not
2086 (org-test-with-parsed-data "Paragraph
2087 * Headline
2088 :PROPERTIES:
2089 :prop: value
2090 :END:"
2091 (org-export-get-node-property
2092 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
2094 (ert-deftest test-org-export/get-category ()
2095 "Test `org-export-get-category' specifications."
2096 ;; Standard test.
2097 (should
2098 (equal "value"
2099 (org-test-with-parsed-data "* Headline
2100 :PROPERTIES:
2101 :CATEGORY: value
2102 :END:"
2103 (org-export-get-category
2104 (org-element-map tree 'headline 'identity nil t) info))))
2105 ;; Test inheritance from a parent headline.
2106 (should
2107 (equal '("value" "value")
2108 (org-test-with-parsed-data "* Headline1
2109 :PROPERTIES:
2110 :CATEGORY: value
2111 :END:
2112 ** Headline2"
2113 (org-element-map tree 'headline
2114 (lambda (hl) (org-export-get-category hl info)) info))))
2115 ;; Test inheritance from #+CATEGORY keyword
2116 (should
2117 (equal "value"
2118 (org-test-with-parsed-data "#+CATEGORY: value
2119 * Headline"
2120 (org-export-get-category
2121 (org-element-map tree 'headline 'identity nil t) info))))
2122 ;; Test inheritance from file name.
2123 (should
2124 (equal "test"
2125 (org-test-with-parsed-data "* Headline"
2126 (let ((info (plist-put info :input-file "~/test.org")))
2127 (org-export-get-category
2128 (org-element-map tree 'headline 'identity nil t) info)))))
2129 ;; Fall-back value.
2130 (should
2131 (equal "???"
2132 (org-test-with-parsed-data "* Headline"
2133 (org-export-get-category
2134 (org-element-map tree 'headline 'identity nil t) info)))))
2136 (ert-deftest test-org-export/first-sibling-p ()
2137 "Test `org-export-first-sibling-p' specifications."
2138 ;; Standard test.
2139 (should
2140 (equal
2141 '(yes yes no)
2142 (org-test-with-parsed-data "* H\n** H 2\n** H 3"
2143 (org-element-map tree 'headline
2144 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2145 info))))
2146 (should
2147 (equal '(yes no)
2148 (org-test-with-parsed-data "- item\n\n para"
2149 (org-element-map tree 'paragraph
2150 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2151 info))))
2152 ;; Ignore sections for headlines.
2153 (should
2154 (equal '(yes yes)
2155 (org-test-with-parsed-data "* H\nSection\n** H 2"
2156 (org-element-map tree 'headline
2157 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2158 info))))
2159 ;; Ignore headlines not exported.
2160 (should
2161 (equal
2162 '(yes)
2163 (let ((org-export-exclude-tags '("ignore")))
2164 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
2165 (org-element-map tree 'headline
2166 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2167 info))))))
2169 (ert-deftest test-org-export/last-sibling-p ()
2170 "Test `org-export-last-sibling-p' specifications."
2171 ;; Standard test.
2172 (should
2173 (equal
2174 '(yes no yes)
2175 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
2176 (org-element-map tree 'headline
2177 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2178 info))))
2179 (should
2180 (equal '(no yes)
2181 (org-test-with-parsed-data "- item\n\n para"
2182 (org-element-map tree 'paragraph
2183 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2184 info))))
2185 ;; Ignore headlines not exported.
2186 (should
2187 (equal
2188 '(yes)
2189 (let ((org-export-exclude-tags '("ignore")))
2190 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
2191 (org-element-map tree 'headline
2192 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2193 info))))))
2195 (ert-deftest test-org-export/handle-inlinetasks ()
2196 "Test inlinetask export."
2197 ;; Inlinetask with an exclude tag.
2198 (when (featurep 'org-inlinetask)
2199 (should
2200 (equal
2202 (let ((org-inlinetask-min-level 3)
2203 org-export-filter-body-functions
2204 org-export-filter-final-output-functions)
2205 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
2206 (org-export-as (org-test-default-backend)
2207 nil nil nil '(:exclude-tags ("noexp")))))))
2208 ;; Inlinetask with an include tag.
2209 (should
2210 (equal
2211 "* H2\n*** Inline :exp:\n"
2212 (let ((org-inlinetask-min-level 3)
2213 (org-tags-column 0))
2214 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
2215 (org-export-as (org-test-default-backend)
2216 nil nil nil '(:select-tags ("exp")))))))
2217 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
2218 (should
2219 (equal ""
2220 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
2221 (org-inlinetask-min-level 3)
2222 org-export-filter-body-functions
2223 org-export-filter-final-output-functions)
2224 (org-test-with-temp-text "*** TODO Inline"
2225 (org-export-as (org-test-default-backend)
2226 nil nil nil '(:with-tasks nil))))))))
2230 ;;; Keywords
2232 (ert-deftest test-org-export/get-date ()
2233 "Test `org-export-get-date' specifications."
2234 ;; Return a properly formatted string when
2235 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
2236 ;; consists in a single timestamp.
2237 (should
2238 (equal "29 03 2012"
2239 (let ((org-export-date-timestamp-format "%d %m %Y"))
2240 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2241 (org-export-get-date info)))))
2242 ;; Return a secondary string otherwise.
2243 (should-not
2244 (stringp
2245 (let ((org-export-date-timestamp-format nil))
2246 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2247 (org-export-get-date info)))))
2248 (should
2249 (equal '("Date")
2250 (org-test-with-parsed-data "#+DATE: Date"
2251 (org-export-get-date info))))
2252 ;; Optional argument has precedence over
2253 ;; `org-export-date-timestamp-format'.
2254 (should
2255 (equal "29 03"
2256 (let ((org-export-date-timestamp-format "%d %m %Y"))
2257 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2258 (org-export-get-date info "%d %m"))))))
2262 ;;; Links
2264 (ert-deftest test-org-export/custom-protocol-maybe ()
2265 "Test `org-export-custom-protocol-maybe' specifications."
2266 (should
2267 (string-match
2268 "success"
2269 (let ((org-link-types (copy-sequence org-link-types)))
2270 (org-add-link-type "foo" nil (lambda (p d f) "success"))
2271 (org-export-string-as
2272 "[[foo:path]]"
2273 (org-export-create-backend
2274 :name 'test
2275 :transcoders
2276 '((section . (lambda (s c i) c))
2277 (paragraph . (lambda (p c i) c))
2278 (link . (lambda (l c i)
2279 (or (org-export-custom-protocol-maybe l c 'test)
2280 "failure")))))))))
2281 (should-not
2282 (string-match
2283 "success"
2284 (let ((org-link-types (copy-sequence org-link-types)))
2285 (org-add-link-type
2286 "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
2287 (org-export-string-as
2288 "[[foo:path]]"
2289 (org-export-create-backend
2290 :name 'no-test
2291 :transcoders
2292 '((section . (lambda (s c i) c))
2293 (paragraph . (lambda (p c i) c))
2294 (link . (lambda (l c i)
2295 (or (org-export-custom-protocol-maybe l c 'no-test)
2296 "failure")))))))))
2297 ;; Ignore anonymous back-ends.
2298 (should-not
2299 (string-match
2300 "success"
2301 (let ((org-link-types (copy-sequence org-link-types)))
2302 (org-add-link-type
2303 "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
2304 (org-export-string-as
2305 "[[foo:path]]"
2306 (org-export-create-backend
2307 :transcoders
2308 '((section . (lambda (s c i) c))
2309 (paragraph . (lambda (p c i) c))
2310 (link . (lambda (l c i)
2311 (or (org-export-custom-protocol-maybe l c nil)
2312 "failure"))))))))))
2314 (ert-deftest test-org-export/get-coderef-format ()
2315 "Test `org-export-get-coderef-format' specifications."
2316 ;; A link without description returns "%s"
2317 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
2318 "%s"))
2319 ;; Return "%s" when path is matched within description.
2320 (should (equal (org-export-get-coderef-format "path" "desc (path)")
2321 "desc %s"))
2322 ;; Otherwise return description.
2323 (should (equal (org-export-get-coderef-format "path" "desc")
2324 "desc")))
2326 (ert-deftest test-org-export/inline-image-p ()
2327 "Test `org-export-inline-image-p' specifications."
2328 (should
2329 (org-export-inline-image-p
2330 (org-test-with-temp-text "[[#id]]"
2331 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
2332 '(("custom-id" . "id")))))
2334 (ert-deftest test-org-export/fuzzy-link ()
2335 "Test fuzzy links specifications."
2336 ;; Link to an headline should return headline's number.
2337 (org-test-with-parsed-data
2338 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
2339 (should
2340 ;; Note: Headline's number is in fact a list of numbers.
2341 (equal '(2)
2342 (org-element-map tree 'link
2343 (lambda (link)
2344 (org-export-get-ordinal
2345 (org-export-resolve-fuzzy-link link info) info)) info t))))
2346 ;; Link to a target in an item should return item's number.
2347 (org-test-with-parsed-data
2348 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
2349 (should
2350 ;; Note: Item's number is in fact a list of numbers.
2351 (equal '(1 2)
2352 (org-element-map tree 'link
2353 (lambda (link)
2354 (org-export-get-ordinal
2355 (org-export-resolve-fuzzy-link link info) info)) info t))))
2356 ;; Link to a target in a footnote should return footnote's number.
2357 (org-test-with-parsed-data "
2358 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
2359 (should
2360 (equal '(2 3)
2361 (org-element-map tree 'link
2362 (lambda (link)
2363 (org-export-get-ordinal
2364 (org-export-resolve-fuzzy-link link info) info)) info))))
2365 ;; Link to a named element should return sequence number of that
2366 ;; element.
2367 (org-test-with-parsed-data
2368 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
2369 (should
2370 (= 2
2371 (org-element-map tree 'link
2372 (lambda (link)
2373 (org-export-get-ordinal
2374 (org-export-resolve-fuzzy-link link info) info)) info t))))
2375 ;; Link to a target not within an item, a table, a footnote
2376 ;; reference or definition should return section number.
2377 (org-test-with-parsed-data
2378 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
2379 (should
2380 (equal '(2)
2381 (org-element-map tree 'link
2382 (lambda (link)
2383 (org-export-get-ordinal
2384 (org-export-resolve-fuzzy-link link info) info)) info t))))
2385 ;; Space are not significant when matching a fuzzy link.
2386 (should
2387 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
2388 (org-element-map tree 'link
2389 (lambda (link) (org-export-resolve-fuzzy-link link info))
2390 info t)))
2391 ;; Statistics cookies are ignored for headline match.
2392 (should
2393 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
2394 (org-element-map tree 'link
2395 (lambda (link) (org-export-resolve-fuzzy-link link info))
2396 info t)))
2397 (should
2398 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
2399 (org-element-map tree 'link
2400 (lambda (link) (org-export-resolve-fuzzy-link link info))
2401 info t))))
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)))))
2549 ;; Handle url-encoded fuzzy links.
2550 (should
2551 (org-test-with-parsed-data "* A B\n[[A%20B]]"
2552 (org-export-resolve-fuzzy-link
2553 (org-element-map tree 'link #'identity info t) info))))
2555 (ert-deftest test-org-export/resolve-id-link ()
2556 "Test `org-export-resolve-id-link' specifications."
2557 ;; Regular test for custom-id link.
2558 (should
2559 (equal '("Headline1")
2560 (org-test-with-parsed-data "* Headline1
2561 :PROPERTIES:
2562 :CUSTOM_ID: test
2563 :END:
2564 * Headline 2
2565 \[[#test]]"
2566 (org-element-property
2567 :title
2568 (org-export-resolve-id-link
2569 (org-element-map tree 'link 'identity info t) info)))))
2570 ;; Throw an error on failing searches.
2571 (should-error
2572 (org-test-with-parsed-data "* Headline1
2573 :PROPERTIES:
2574 :CUSTOM_ID: test
2575 :END:
2576 * Headline 2
2577 \[[#no-match]]"
2578 (org-export-resolve-id-link
2579 (org-element-map tree 'link 'identity info t) info)))
2580 ;; Test for internal id target.
2581 (should
2582 (equal '("Headline1")
2583 (org-test-with-parsed-data "* Headline1
2584 :PROPERTIES:
2585 :ID: aaaa
2586 :END:
2587 * Headline 2
2588 \[[id:aaaa]]"
2589 (org-element-property
2590 :title
2591 (org-export-resolve-id-link
2592 (org-element-map tree 'link 'identity info t) info)))))
2593 ;; Test for external id target.
2594 (should
2595 (equal
2596 "external-file"
2597 (org-test-with-parsed-data "[[id:aaaa]]"
2598 (org-export-resolve-id-link
2599 (org-element-map tree 'link 'identity info t)
2600 (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
2602 (ert-deftest test-org-export/resolve-radio-link ()
2603 "Test `org-export-resolve-radio-link' specifications."
2604 ;; Standard test.
2605 (should
2606 (org-test-with-temp-text "<<<radio>>> radio"
2607 (org-update-radio-target-regexp)
2608 (let* ((tree (org-element-parse-buffer))
2609 (info `(:parse-tree ,tree)))
2610 (org-export-resolve-radio-link
2611 (org-element-map tree 'link 'identity info t)
2612 info))))
2613 ;; Radio targets are case-insensitive.
2614 (should
2615 (org-test-with-temp-text "<<<RADIO>>> radio"
2616 (org-update-radio-target-regexp)
2617 (let* ((tree (org-element-parse-buffer))
2618 (info `(:parse-tree ,tree)))
2619 (org-export-resolve-radio-link
2620 (org-element-map tree 'link 'identity info t)
2621 info))))
2622 ;; Radio target with objects.
2623 (should
2624 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
2625 (org-update-radio-target-regexp)
2626 (let* ((tree (org-element-parse-buffer))
2627 (info `(:parse-tree ,tree)))
2628 (org-export-resolve-radio-link
2629 (org-element-map tree 'link 'identity info t)
2630 info))))
2631 ;; Radio target with objects at its beginning.
2632 (should
2633 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
2634 (org-update-radio-target-regexp)
2635 (let* ((tree (org-element-parse-buffer))
2636 (info `(:parse-tree ,tree)))
2637 (org-export-resolve-radio-link
2638 (org-element-map tree 'link 'identity info t)
2639 info))))
2640 ;; Radio link next to an apostrophe.
2641 (should
2642 (org-test-with-temp-text "<<<radio>>> radio's"
2643 (org-update-radio-target-regexp)
2644 (let* ((tree (org-element-parse-buffer))
2645 (info `(:parse-tree ,tree)))
2646 (org-export-resolve-radio-link
2647 (org-element-map tree 'link 'identity info t)
2648 info))))
2649 ;; Multiple radio targets.
2650 (should
2651 (equal '("radio1" "radio2")
2652 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
2653 (org-update-radio-target-regexp)
2654 (let* ((tree (org-element-parse-buffer))
2655 (info `(:parse-tree ,tree)))
2656 (org-element-map tree 'link
2657 (lambda (link)
2658 (org-element-property
2659 :value (org-export-resolve-radio-link link info)))
2660 info)))))
2661 ;; Radio target is whitespace insensitive.
2662 (should
2663 (org-test-with-temp-text "<<<a radio>>> a\n radio"
2664 (org-update-radio-target-regexp)
2665 (let* ((tree (org-element-parse-buffer))
2666 (info `(:parse-tree ,tree)))
2667 (org-element-map tree 'link
2668 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
2670 (ert-deftest test-org-export/file-uri ()
2671 "Test `org-export-file-uri' specifications."
2672 ;; Preserve relative filenames.
2673 (should (equal "relative.org" (org-export-file-uri "relative.org")))
2674 ;; Local files start with "file:///"
2675 (should (equal "file:///local.org" (org-export-file-uri "/local.org")))
2676 ;; Remote files start with "file://"
2677 (should (equal "file://myself@some.where:papers/last.pdf"
2678 (org-export-file-uri "/myself@some.where:papers/last.pdf")))
2679 ;; Expand filename starting with "~".
2680 (should (equal (org-export-file-uri "~/file.org")
2681 (concat "file://" (expand-file-name "~/file.org")))))
2685 ;;; Src-block and example-block
2687 (ert-deftest test-org-export/unravel-code ()
2688 "Test `org-export-unravel-code' function."
2689 ;; Code without reference.
2690 (should
2691 (equal '("(+ 1 1)\n")
2692 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2693 (org-export-unravel-code (org-element-at-point)))))
2694 ;; Code with reference.
2695 (should
2696 (equal '("(+ 1 1)\n" (1 . "test"))
2697 (org-test-with-temp-text
2698 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
2699 (let ((org-coderef-label-format "(ref:%s)"))
2700 (org-export-unravel-code (org-element-at-point))))))
2701 ;; Code with user-defined reference.
2702 (should
2703 (equal
2704 '("(+ 1 1)\n" (1 . "test"))
2705 (org-test-with-temp-text
2706 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
2707 (let ((org-coderef-label-format "(ref:%s)"))
2708 (org-export-unravel-code (org-element-at-point))))))
2709 ;; Code references keys are relative to the current block.
2710 (should
2711 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
2712 (org-test-with-temp-text "
2713 #+BEGIN_EXAMPLE -n
2714 \(+ 1 1)
2715 #+END_EXAMPLE
2716 #+BEGIN_EXAMPLE +n
2717 \(+ 2 2)
2718 \(+ 3 3) (ref:one)
2719 #+END_EXAMPLE"
2720 (goto-line 5)
2721 (let ((org-coderef-label-format "(ref:%s)"))
2722 (org-export-unravel-code (org-element-at-point)))))))
2724 (ert-deftest test-org-export/format-code-default ()
2725 "Test `org-export-format-code-default' specifications."
2726 ;; Return the empty string when code is empty.
2727 (should
2728 (equal ""
2729 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
2730 (org-export-format-code-default
2731 (org-element-map tree 'src-block 'identity info t) info))))
2732 ;; Number lines, two whitespace characters before the actual loc.
2733 (should
2734 (equal "1 a\n2 b\n"
2735 (org-test-with-parsed-data
2736 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
2737 (org-export-format-code-default
2738 (org-element-map tree 'src-block 'identity info t) info))))
2739 ;; Put references 6 whitespace characters after the widest line,
2740 ;; wrapped within parenthesis.
2741 (should
2742 (equal "123 (a)\n1 (b)\n"
2743 (let ((org-coderef-label-format "(ref:%s)"))
2744 (org-test-with-parsed-data
2745 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
2746 (org-export-format-code-default
2747 (org-element-map tree 'src-block 'identity info t) info))))))
2751 ;;; Smart Quotes
2753 (ert-deftest test-org-export/activate-smart-quotes ()
2754 "Test `org-export-activate-smart-quotes' specifications."
2755 ;; Double quotes: standard test.
2756 (should
2757 (equal
2758 '("some &ldquo;quoted&rdquo; text")
2759 (let ((org-export-default-language "en"))
2760 (org-test-with-parsed-data "some \"quoted\" text"
2761 (org-element-map tree 'plain-text
2762 (lambda (s) (org-export-activate-smart-quotes s :html info))
2763 info)))))
2764 ;; Opening quotes: at the beginning of a paragraph.
2765 (should
2766 (equal
2767 '("&ldquo;begin")
2768 (let ((org-export-default-language "en"))
2769 (org-test-with-parsed-data "\"begin"
2770 (org-element-map tree 'plain-text
2771 (lambda (s) (org-export-activate-smart-quotes s :html info))
2772 info)))))
2773 ;; Opening quotes: after an object.
2774 (should
2775 (equal
2776 '("&ldquo;quoted&rdquo; text")
2777 (let ((org-export-default-language "en"))
2778 (org-test-with-parsed-data "=verb= \"quoted\" text"
2779 (org-element-map tree 'plain-text
2780 (lambda (s) (org-export-activate-smart-quotes s :html info))
2781 info)))))
2782 ;; Closing quotes: at the end of a paragraph.
2783 (should
2784 (equal
2785 '("Quoted &ldquo;text&rdquo;")
2786 (let ((org-export-default-language "en"))
2787 (org-test-with-parsed-data "Quoted \"text\""
2788 (org-element-map tree 'plain-text
2789 (lambda (s) (org-export-activate-smart-quotes s :html info))
2790 info)))))
2791 ;; Inner quotes: standard test.
2792 (should
2793 (equal '("« outer « inner » outer »")
2794 (let ((org-export-default-language "fr"))
2795 (org-test-with-parsed-data "\"outer 'inner' outer\""
2796 (org-element-map tree 'plain-text
2797 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
2798 info)))))
2799 ;; Apostrophe: standard test.
2800 (should
2801 (equal '("It « shouldn’t » fail")
2802 (let ((org-export-default-language "fr"))
2803 (org-test-with-parsed-data "It \"shouldn't\" fail"
2804 (org-element-map tree 'plain-text
2805 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
2806 info)))))
2807 (should
2808 (equal '("It shouldn’t fail")
2809 (let ((org-export-default-language "fr"))
2810 (org-test-with-parsed-data "It shouldn't fail"
2811 (org-element-map tree 'plain-text
2812 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
2813 info)))))
2814 ;; Apostrophe: before an object.
2815 (should
2816 (equal
2817 '("« a’" " »")
2818 (let ((org-export-default-language "fr"))
2819 (org-test-with-parsed-data "\"a'=b=\""
2820 (org-element-map tree 'plain-text
2821 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
2822 info)))))
2823 ;; Apostrophe: after an object.
2824 (should
2825 (equal '("« " "’s »")
2826 (let ((org-export-default-language "fr"))
2827 (org-test-with-parsed-data "\"=code='s\""
2828 (org-element-map tree 'plain-text
2829 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
2830 info)))))
2831 ;; Special case: isolated quotes.
2832 (should
2833 (equal '("&ldquo;" "&rdquo;")
2834 (let ((org-export-default-language "en"))
2835 (org-test-with-parsed-data "\"$x$\""
2836 (org-element-map tree 'plain-text
2837 (lambda (s) (org-export-activate-smart-quotes s :html info))
2838 info)))))
2839 ;; Smart quotes in secondary strings.
2840 (should
2841 (equal '("&ldquo;" "&rdquo;")
2842 (let ((org-export-default-language "en"))
2843 (org-test-with-parsed-data "* \"$x$\""
2844 (org-element-map tree 'plain-text
2845 (lambda (s) (org-export-activate-smart-quotes s :html info))
2846 info)))))
2847 ;; Smart quotes in document keywords.
2848 (should
2849 (equal '("&ldquo;" "&rdquo;")
2850 (let ((org-export-default-language "en"))
2851 (org-test-with-parsed-data "#+TITLE: \"$x$\""
2852 (org-element-map (plist-get info :title) 'plain-text
2853 (lambda (s) (org-export-activate-smart-quotes s :html info))
2854 info)))))
2855 ;; Smart quotes in parsed affiliated keywords.
2856 (should
2857 (equal '("&ldquo;" "&rdquo;" "Paragraph")
2858 (let ((org-export-default-language "en"))
2859 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
2860 (org-element-map tree 'plain-text
2861 (lambda (s) (org-export-activate-smart-quotes s :html info))
2862 info nil nil t))))))
2866 ;;; Tables
2868 (ert-deftest test-org-export/special-column ()
2869 "Test if the table's special column is properly recognized."
2870 ;; 1. First column is special if it contains only a special marking
2871 ;; characters or empty cells.
2872 (org-test-with-temp-text "
2873 | ! | 1 |
2874 | | 2 |"
2875 (should
2876 (org-export-table-has-special-column-p
2877 (org-element-map
2878 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2879 ;; 2. If the column contains anything else, it isn't special.
2880 (org-test-with-temp-text "
2881 | ! | 1 |
2882 | b | 2 |"
2883 (should-not
2884 (org-export-table-has-special-column-p
2885 (org-element-map
2886 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2887 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
2888 ;; and "!".
2889 (org-test-with-temp-text "
2890 | # | 1 |
2891 | ^ | 2 |
2892 | * | 3 |
2893 | _ | 4 |
2894 | / | 5 |
2895 | $ | 6 |
2896 | ! | 7 |"
2897 (should
2898 (org-export-table-has-special-column-p
2899 (org-element-map
2900 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2901 ;; 4. A first column with only empty cells isn't considered as
2902 ;; special.
2903 (org-test-with-temp-text "
2904 | | 1 |
2905 | | 2 |"
2906 (should-not
2907 (org-export-table-has-special-column-p
2908 (org-element-map
2909 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
2911 (ert-deftest test-org-export/table-row-is-special-p ()
2912 "Test `org-export-table-row-is-special-p' specifications."
2913 ;; 1. A row is special if it has a special marking character in the
2914 ;; special column.
2915 (org-test-with-parsed-data "| ! | 1 |"
2916 (should
2917 (org-export-table-row-is-special-p
2918 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2919 ;; 2. A row is special when its first field is "/"
2920 (org-test-with-parsed-data "
2921 | / | 1 |
2922 | a | b |"
2923 (should
2924 (org-export-table-row-is-special-p
2925 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2926 ;; 3. A row only containing alignment cookies is also considered as
2927 ;; special.
2928 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
2929 (should
2930 (org-export-table-row-is-special-p
2931 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2932 ;; 4. Everything else isn't considered as special.
2933 (org-test-with-parsed-data "| \alpha | | c |"
2934 (should-not
2935 (org-export-table-row-is-special-p
2936 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2937 ;; 5. Table's rules are never considered as special rows.
2938 (org-test-with-parsed-data "|---+---|"
2939 (should-not
2940 (org-export-table-row-is-special-p
2941 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
2943 (ert-deftest test-org-export/has-header-p ()
2944 "Test `org-export-table-has-header-p' specifications."
2945 ;; 1. With an header.
2946 (org-test-with-parsed-data "
2947 | a | b |
2948 |---+---|
2949 | c | d |"
2950 (should
2951 (org-export-table-has-header-p
2952 (org-element-map tree 'table 'identity info 'first-match)
2953 info)))
2954 ;; 2. Without an header.
2955 (org-test-with-parsed-data "
2956 | a | b |
2957 | c | d |"
2958 (should-not
2959 (org-export-table-has-header-p
2960 (org-element-map tree 'table 'identity info 'first-match)
2961 info)))
2962 ;; 3. Don't get fooled with starting and ending rules.
2963 (org-test-with-parsed-data "
2964 |---+---|
2965 | a | b |
2966 | c | d |
2967 |---+---|"
2968 (should-not
2969 (org-export-table-has-header-p
2970 (org-element-map tree 'table 'identity info 'first-match)
2971 info))))
2973 (ert-deftest test-org-export/table-row-group ()
2974 "Test `org-export-table-row-group' specifications."
2975 ;; 1. A rule creates a new group.
2976 (should
2977 (equal '(1 rule 2)
2978 (org-test-with-parsed-data "
2979 | a | b |
2980 |---+---|
2981 | 1 | 2 |"
2982 (org-element-map tree 'table-row
2983 (lambda (row)
2984 (if (eq (org-element-property :type row) 'rule) 'rule
2985 (org-export-table-row-group row info)))))))
2986 ;; 2. Special rows are ignored in count.
2987 (should
2988 (equal
2989 '(rule 1)
2990 (org-test-with-parsed-data "
2991 | / | < | > |
2992 |---|---+---|
2993 | | 1 | 2 |"
2994 (org-element-map tree 'table-row
2995 (lambda (row)
2996 (if (eq (org-element-property :type row) 'rule) 'rule
2997 (org-export-table-row-group row info)))
2998 info))))
2999 ;; 3. Double rules also are ignored in count.
3000 (should
3001 (equal '(1 rule rule 2)
3002 (org-test-with-parsed-data "
3003 | a | b |
3004 |---+---|
3005 |---+---|
3006 | 1 | 2 |"
3007 (org-element-map tree 'table-row
3008 (lambda (row)
3009 (if (eq (org-element-property :type row) 'rule) 'rule
3010 (org-export-table-row-group row info))))))))
3012 (ert-deftest test-org-export/table-row-number ()
3013 "Test `org-export-table-row-number' specifications."
3014 ;; Standard test. Number is 0-indexed.
3015 (should
3016 (equal '(0 1)
3017 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
3018 (org-element-map tree 'table-row
3019 (lambda (row) (org-export-table-row-number row info)) info))))
3020 ;; Number ignores separators.
3021 (should
3022 (equal '(0 1)
3023 (org-test-with-parsed-data "
3024 | a | b | c |
3025 |---+---+---|
3026 | d | e | f |"
3027 (org-element-map tree 'table-row
3028 (lambda (row) (org-export-table-row-number row info)) info))))
3029 ;; Number ignores special rows.
3030 (should
3031 (equal '(0 1)
3032 (org-test-with-parsed-data "
3033 | / | < | > |
3034 | | b | c |
3035 |---+-----+-----|
3036 | | <c> | <c> |
3037 | | e | f |"
3038 (org-element-map tree 'table-row
3039 (lambda (row) (org-export-table-row-number row info)) info)))))
3041 (ert-deftest test-org-export/table-cell-width ()
3042 "Test `org-export-table-cell-width' specifications."
3043 ;; 1. Width is primarily determined by width cookies. If no cookie
3044 ;; is found, cell's width is nil.
3045 (org-test-with-parsed-data "
3046 | / | <l> | <6> | <l7> |
3047 | | a | b | c |"
3048 (should
3049 (equal
3050 '(nil 6 7)
3051 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3052 (org-element-map tree 'table-cell 'identity info)))))
3053 ;; 2. The last width cookie has precedence.
3054 (org-test-with-parsed-data "
3055 | <6> |
3056 | <7> |
3057 | a |"
3058 (should
3059 (equal
3060 '(7)
3061 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3062 (org-element-map tree 'table-cell 'identity info)))))
3063 ;; 3. Valid width cookies must have a specific row.
3064 (org-test-with-parsed-data "| <6> | cell |"
3065 (should
3066 (equal
3067 '(nil nil)
3068 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3069 (org-element-map tree 'table-cell 'identity))))))
3071 (ert-deftest test-org-export/table-cell-alignment ()
3072 "Test `org-export-table-cell-alignment' specifications."
3073 ;; 1. Alignment is primarily determined by alignment cookies.
3074 (should
3075 (equal '(left center right)
3076 (let ((org-table-number-fraction 0.5)
3077 (org-table-number-regexp "^[0-9]+$"))
3078 (org-test-with-parsed-data "| <l> | <c> | <r> |"
3079 (mapcar (lambda (cell)
3080 (org-export-table-cell-alignment cell info))
3081 (org-element-map tree 'table-cell 'identity))))))
3082 ;; 2. The last alignment cookie has precedence.
3083 (should
3084 (equal '(right right right)
3085 (org-test-with-parsed-data "
3086 | <l8> |
3087 | cell |
3088 | <r9> |"
3089 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
3090 (org-element-map tree 'table-cell 'identity)))))
3091 ;; 3. If there's no cookie, cell's contents determine alignment.
3092 ;; A column mostly made of cells containing numbers will align
3093 ;; its cells to the right.
3094 (should
3095 (equal '(right right right)
3096 (let ((org-table-number-fraction 0.5)
3097 (org-table-number-regexp "^[0-9]+$"))
3098 (org-test-with-parsed-data "
3099 | 123 |
3100 | some text |
3101 | 12345 |"
3102 (mapcar (lambda (cell)
3103 (org-export-table-cell-alignment cell info))
3104 (org-element-map tree 'table-cell 'identity))))))
3105 ;; 4. Otherwise, they will be aligned to the left.
3106 (should
3107 (equal '(left left left)
3108 (org-test-with-parsed-data "
3109 | text |
3110 | some text |
3111 | \alpha |"
3112 (mapcar (lambda (cell)
3113 (org-export-table-cell-alignment cell info))
3114 (org-element-map tree 'table-cell 'identity info))))))
3116 (ert-deftest test-org-export/table-cell-borders ()
3117 "Test `org-export-table-cell-borders' specifications."
3118 ;; 1. Recognize various column groups indicators.
3119 (org-test-with-parsed-data "| / | < | > | <> |"
3120 (should
3121 (equal
3122 '((right bottom top) (left bottom top) (right bottom top)
3123 (right left bottom top))
3124 (mapcar (lambda (cell)
3125 (org-export-table-cell-borders cell info))
3126 (org-element-map tree 'table-cell 'identity)))))
3127 ;; 2. Accept shortcuts to define column groups.
3128 (org-test-with-parsed-data "| / | < | < |"
3129 (should
3130 (equal
3131 '((right bottom top) (right left bottom top) (left bottom top))
3132 (mapcar (lambda (cell)
3133 (org-export-table-cell-borders cell info))
3134 (org-element-map tree 'table-cell 'identity)))))
3135 ;; 3. A valid column groups row must start with a "/".
3136 (org-test-with-parsed-data "
3137 | | < |
3138 | a | b |"
3139 (should
3140 (equal '((top) (top) (bottom) (bottom))
3141 (mapcar (lambda (cell)
3142 (org-export-table-cell-borders cell info))
3143 (org-element-map tree 'table-cell 'identity)))))
3144 ;; 4. Take table rules into consideration.
3145 (org-test-with-parsed-data "
3146 | 1 |
3147 |---|
3148 | 2 |"
3149 (should
3150 (equal '((below top) (bottom above))
3151 (mapcar (lambda (cell)
3152 (org-export-table-cell-borders cell info))
3153 (org-element-map tree 'table-cell 'identity)))))
3154 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
3155 ;; (resp. `bottom' and `below') borders. Any special row is
3156 ;; ignored.
3157 (org-test-with-parsed-data "
3158 |---+----|
3159 | / | |
3160 | | 1 |
3161 |---+----|"
3162 (should
3163 (equal '((bottom below top above))
3164 (last
3165 (mapcar (lambda (cell)
3166 (org-export-table-cell-borders cell info))
3167 (org-element-map tree 'table-cell 'identity)))))))
3169 (ert-deftest test-org-export/table-dimensions ()
3170 "Test `org-export-table-dimensions' specifications."
3171 ;; 1. Standard test.
3172 (org-test-with-parsed-data "
3173 | 1 | 2 | 3 |
3174 | 4 | 5 | 6 |"
3175 (should
3176 (equal '(2 . 3)
3177 (org-export-table-dimensions
3178 (org-element-map tree 'table 'identity info 'first-match) info))))
3179 ;; 2. Ignore horizontal rules and special columns.
3180 (org-test-with-parsed-data "
3181 | / | < | > |
3182 | 1 | 2 | 3 |
3183 |---+---+---|
3184 | 4 | 5 | 6 |"
3185 (should
3186 (equal '(2 . 3)
3187 (org-export-table-dimensions
3188 (org-element-map tree 'table 'identity info 'first-match) info)))))
3190 (ert-deftest test-org-export/table-cell-address ()
3191 "Test `org-export-table-cell-address' specifications."
3192 ;; 1. Standard test: index is 0-based.
3193 (org-test-with-parsed-data "| a | b |"
3194 (should
3195 (equal '((0 . 0) (0 . 1))
3196 (org-element-map tree 'table-cell
3197 (lambda (cell) (org-export-table-cell-address cell info))
3198 info))))
3199 ;; 2. Special column isn't counted, nor are special rows.
3200 (org-test-with-parsed-data "
3201 | / | <> |
3202 | | c |"
3203 (should
3204 (equal '(0 . 0)
3205 (org-export-table-cell-address
3206 (car (last (org-element-map tree 'table-cell 'identity info)))
3207 info))))
3208 ;; 3. Tables rules do not count either.
3209 (org-test-with-parsed-data "
3210 | a |
3211 |---|
3212 | b |
3213 |---|
3214 | c |"
3215 (should
3216 (equal '(2 . 0)
3217 (org-export-table-cell-address
3218 (car (last (org-element-map tree 'table-cell 'identity info)))
3219 info))))
3220 ;; 4. Return nil for special cells.
3221 (org-test-with-parsed-data "| / | a |"
3222 (should-not
3223 (org-export-table-cell-address
3224 (org-element-map tree 'table-cell 'identity nil 'first-match)
3225 info))))
3227 (ert-deftest test-org-export/get-table-cell-at ()
3228 "Test `org-export-get-table-cell-at' specifications."
3229 ;; 1. Address ignores special columns, special rows and rules.
3230 (org-test-with-parsed-data "
3231 | / | <> |
3232 | | a |
3233 |---+----|
3234 | | b |"
3235 (should
3236 (equal '("b")
3237 (org-element-contents
3238 (org-export-get-table-cell-at
3239 '(1 . 0)
3240 (org-element-map tree 'table 'identity info 'first-match)
3241 info)))))
3242 ;; 2. Return value for a non-existent address is nil.
3243 (org-test-with-parsed-data "| a |"
3244 (should-not
3245 (org-export-get-table-cell-at
3246 '(2 . 2)
3247 (org-element-map tree 'table 'identity info 'first-match)
3248 info)))
3249 (org-test-with-parsed-data "| / |"
3250 (should-not
3251 (org-export-get-table-cell-at
3252 '(0 . 0)
3253 (org-element-map tree 'table 'identity info 'first-match)
3254 info))))
3256 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
3257 "Test `org-export-table-cell-starts-colgroup-p' specifications."
3258 ;; 1. A cell at a beginning of a row always starts a column group.
3259 (org-test-with-parsed-data "| a |"
3260 (should
3261 (org-export-table-cell-starts-colgroup-p
3262 (org-element-map tree 'table-cell 'identity info 'first-match)
3263 info)))
3264 ;; 2. Special column should be ignored when determining the
3265 ;; beginning of the row.
3266 (org-test-with-parsed-data "
3267 | / | |
3268 | | a |"
3269 (should
3270 (org-export-table-cell-starts-colgroup-p
3271 (org-element-map tree 'table-cell 'identity info 'first-match)
3272 info)))
3273 ;; 2. Explicit column groups.
3274 (org-test-with-parsed-data "
3275 | / | | < |
3276 | a | b | c |"
3277 (should
3278 (equal
3279 '(yes no yes)
3280 (org-element-map tree 'table-cell
3281 (lambda (cell)
3282 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
3283 info)))))
3285 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
3286 "Test `org-export-table-cell-ends-colgroup-p' specifications."
3287 ;; 1. A cell at the end of a row always ends a column group.
3288 (org-test-with-parsed-data "| a |"
3289 (should
3290 (org-export-table-cell-ends-colgroup-p
3291 (org-element-map tree 'table-cell 'identity info 'first-match)
3292 info)))
3293 ;; 2. Special column should be ignored when determining the
3294 ;; beginning of the row.
3295 (org-test-with-parsed-data "
3296 | / | |
3297 | | a |"
3298 (should
3299 (org-export-table-cell-ends-colgroup-p
3300 (org-element-map tree 'table-cell 'identity info 'first-match)
3301 info)))
3302 ;; 3. Explicit column groups.
3303 (org-test-with-parsed-data "
3304 | / | < | |
3305 | a | b | c |"
3306 (should
3307 (equal
3308 '(yes no yes)
3309 (org-element-map tree 'table-cell
3310 (lambda (cell)
3311 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
3312 info)))))
3314 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
3315 "Test `org-export-table-row-starts-rowgroup-p' specifications."
3316 ;; 1. A row at the beginning of a table always starts a row group.
3317 ;; So does a row following a table rule.
3318 (org-test-with-parsed-data "
3319 | a |
3320 |---|
3321 | b |"
3322 (should
3323 (equal
3324 '(yes no yes)
3325 (org-element-map tree 'table-row
3326 (lambda (row)
3327 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
3328 info))))
3329 ;; 2. Special rows should be ignored when determining the beginning
3330 ;; of the row.
3331 (org-test-with-parsed-data "
3332 | / | < |
3333 | | a |
3334 |---+---|
3335 | / | < |
3336 | | b |"
3337 (should
3338 (equal
3339 '(yes no yes)
3340 (org-element-map tree 'table-row
3341 (lambda (row)
3342 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
3343 info)))))
3345 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
3346 "Test `org-export-table-row-ends-rowgroup-p' specifications."
3347 ;; 1. A row at the end of a table always ends a row group. So does
3348 ;; a row preceding a table rule.
3349 (org-test-with-parsed-data "
3350 | a |
3351 |---|
3352 | b |"
3353 (should
3354 (equal
3355 '(yes no yes)
3356 (org-element-map tree 'table-row
3357 (lambda (row)
3358 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
3359 info))))
3360 ;; 2. Special rows should be ignored when determining the beginning
3361 ;; of the row.
3362 (org-test-with-parsed-data "
3363 | | a |
3364 | / | < |
3365 |---+---|
3366 | | b |
3367 | / | < |"
3368 (should
3369 (equal
3370 '(yes no yes)
3371 (org-element-map tree 'table-row
3372 (lambda (row)
3373 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
3374 info)))))
3376 (ert-deftest test-org-export/table-row-in-header-p ()
3377 "Test `org-export-table-row-in-header-p' specifications."
3378 ;; Standard test. Separators are always nil.
3379 (should
3380 (equal
3381 '(yes no no)
3382 (org-test-with-parsed-data "| a |\n|---|\n| b |"
3383 (org-element-map tree 'table-row
3384 (lambda (row)
3385 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
3386 ;; Nil when there is no header.
3387 (should
3388 (equal
3389 '(no no)
3390 (org-test-with-parsed-data "| a |\n| b |"
3391 (org-element-map tree 'table-row
3392 (lambda (row)
3393 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
3395 (ert-deftest test-org-export/table-row-starts-header-p ()
3396 "Test `org-export-table-row-starts-header-p' specifications."
3397 ;; 1. Only the row starting the first row group starts the table
3398 ;; header.
3399 (org-test-with-parsed-data "
3400 | a |
3401 | b |
3402 |---|
3403 | c |"
3404 (should
3405 (equal
3406 '(yes no no no)
3407 (org-element-map tree 'table-row
3408 (lambda (row)
3409 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
3410 info))))
3411 ;; 2. A row cannot start an header if there's no header in the
3412 ;; table.
3413 (org-test-with-parsed-data "
3414 | a |
3415 |---|"
3416 (should-not
3417 (org-export-table-row-starts-header-p
3418 (org-element-map tree 'table-row 'identity info 'first-match)
3419 info))))
3421 (ert-deftest test-org-export/table-row-ends-header-p ()
3422 "Test `org-export-table-row-ends-header-p' specifications."
3423 ;; 1. Only the row starting the first row group starts the table
3424 ;; header.
3425 (org-test-with-parsed-data "
3426 | a |
3427 | b |
3428 |---|
3429 | c |"
3430 (should
3431 (equal
3432 '(no yes no no)
3433 (org-element-map tree 'table-row
3434 (lambda (row)
3435 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
3436 info))))
3437 ;; 2. A row cannot start an header if there's no header in the
3438 ;; table.
3439 (org-test-with-parsed-data "
3440 | a |
3441 |---|"
3442 (should-not
3443 (org-export-table-row-ends-header-p
3444 (org-element-map tree 'table-row 'identity info 'first-match)
3445 info))))
3449 ;;; Tables of Contents
3451 (ert-deftest test-org-export/collect-headlines ()
3452 "Test `org-export-collect-headlines' specifications."
3453 ;; Standard test.
3454 (should
3455 (= 2
3456 (length
3457 (org-test-with-parsed-data "* H1\n** H2"
3458 (org-export-collect-headlines info)))))
3459 ;; Do not collect headlines below optional argument.
3460 (should
3461 (= 1
3462 (length
3463 (org-test-with-parsed-data "* H1\n** H2"
3464 (org-export-collect-headlines info 1)))))
3465 ;; Never collect headlines below maximum headline level.
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)))))
3471 (should
3472 (= 1
3473 (length
3474 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3475 (org-export-collect-headlines info 2)))))
3476 ;; Collect headlines locally.
3477 (should
3478 (= 2
3479 (org-test-with-parsed-data "* H1\n** H2\n** H3"
3480 (let ((scope (org-element-map tree 'headline #'identity info t)))
3481 (length (org-export-collect-headlines info nil scope))))))
3482 ;; When collecting locally, optional level is relative.
3483 (should
3484 (= 1
3485 (org-test-with-parsed-data "* H1\n** H2\n*** H3"
3486 (let ((scope (org-element-map tree 'headline #'identity info t)))
3487 (length (org-export-collect-headlines info 1 scope)))))))
3491 ;;; Templates
3493 (ert-deftest test-org-export/inner-template ()
3494 "Test `inner-template' translator specifications."
3495 (should
3496 (equal "Success!"
3497 (org-test-with-temp-text "* Headline"
3498 (org-export-as
3499 (org-export-create-backend
3500 :transcoders
3501 '((inner-template . (lambda (contents info) "Success!"))
3502 (headline . (lambda (h c i) "Headline"))))))))
3503 ;; Inner template is applied even in a "body-only" export.
3504 (should
3505 (equal "Success!"
3506 (org-test-with-temp-text "* Headline"
3507 (org-export-as
3508 (org-export-create-backend
3509 :transcoders '((inner-template . (lambda (c i) "Success!"))
3510 (headline . (lambda (h c i) "Headline"))))
3511 nil nil 'body-only)))))
3513 (ert-deftest test-org-export/template ()
3514 "Test `template' translator specifications."
3515 (should
3516 (equal "Success!"
3517 (org-test-with-temp-text "* Headline"
3518 (org-export-as
3519 (org-export-create-backend
3520 :transcoders '((template . (lambda (contents info) "Success!"))
3521 (headline . (lambda (h c i) "Headline"))))))))
3522 ;; Template is not applied in a "body-only" export.
3523 (should-not
3524 (equal "Success!"
3525 (org-test-with-temp-text "* Headline"
3526 (org-export-as
3527 (org-export-create-backend
3528 :transcoders '((template . (lambda (contents info) "Success!"))
3529 (headline . (lambda (h c i) "Headline"))))
3530 nil nil 'body-only)))))
3534 ;;; Topology
3536 (ert-deftest test-org-export/get-next-element ()
3537 "Test `org-export-get-next-element' specifications."
3538 ;; Standard test.
3539 (should
3540 (equal "b"
3541 (org-test-with-parsed-data "* Headline\n*a* b"
3542 (org-export-get-next-element
3543 (org-element-map tree 'bold 'identity info t) info))))
3544 ;; Return nil when no previous element.
3545 (should-not
3546 (org-test-with-parsed-data "* Headline\na *b*"
3547 (org-export-get-next-element
3548 (org-element-map tree 'bold 'identity info t) info)))
3549 ;; Non-exportable elements are ignored.
3550 (should-not
3551 (let ((org-export-with-timestamps nil))
3552 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
3553 (org-export-get-next-element
3554 (org-element-map tree 'entity 'identity info t) info))))
3555 ;; Find next element in secondary strings.
3556 (should
3557 (eq 'verbatim
3558 (org-test-with-parsed-data "* a =verb="
3559 (org-element-type
3560 (org-export-get-next-element
3561 (org-element-map tree 'plain-text 'identity info t) info)))))
3562 (should
3563 (eq 'verbatim
3564 (org-test-with-parsed-data "* /italic/ =verb="
3565 (org-element-type
3566 (org-export-get-next-element
3567 (org-element-map tree 'italic 'identity info t) info)))))
3568 ;; Find next element in document keywords.
3569 (should
3570 (eq 'verbatim
3571 (org-test-with-parsed-data "#+TITLE: a =verb="
3572 (org-element-type
3573 (org-export-get-next-element
3574 (org-element-map
3575 (plist-get info :title) 'plain-text 'identity info t) info)))))
3576 ;; Find next element in parsed affiliated keywords.
3577 (should
3578 (eq 'verbatim
3579 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
3580 (org-element-type
3581 (org-export-get-next-element
3582 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3583 ;; With optional argument N, return a list containing all the
3584 ;; following elements.
3585 (should
3586 (equal
3587 '(bold code underline)
3588 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3589 (mapcar 'car
3590 (org-export-get-next-element
3591 (org-element-map tree 'italic 'identity info t) info t)))))
3592 ;; When N is a positive integer, return a list containing up to
3593 ;; N following elements.
3594 (should
3595 (equal
3596 '(bold code)
3597 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3598 (mapcar 'car
3599 (org-export-get-next-element
3600 (org-element-map tree 'italic 'identity info t) info 2))))))
3602 (ert-deftest test-org-export/get-previous-element ()
3603 "Test `org-export-get-previous-element' specifications."
3604 ;; Standard test.
3605 (should
3606 (equal "a "
3607 (org-test-with-parsed-data "* Headline\na *b*"
3608 (org-export-get-previous-element
3609 (org-element-map tree 'bold 'identity info t) info))))
3610 ;; Return nil when no previous element.
3611 (should-not
3612 (org-test-with-parsed-data "* Headline\n*a* b"
3613 (org-export-get-previous-element
3614 (org-element-map tree 'bold 'identity info t) info)))
3615 ;; Non-exportable elements are ignored.
3616 (should-not
3617 (let ((org-export-with-timestamps nil))
3618 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
3619 (org-export-get-previous-element
3620 (org-element-map tree 'entity 'identity info t) info))))
3621 ;; Find previous element in secondary strings.
3622 (should
3623 (eq 'verbatim
3624 (org-test-with-parsed-data "* =verb= a"
3625 (org-element-type
3626 (org-export-get-previous-element
3627 (org-element-map tree 'plain-text 'identity info t) info)))))
3628 (should
3629 (eq 'verbatim
3630 (org-test-with-parsed-data "* =verb= /italic/"
3631 (org-element-type
3632 (org-export-get-previous-element
3633 (org-element-map tree 'italic 'identity info t) info)))))
3634 ;; Find previous element in document keywords.
3635 (should
3636 (eq 'verbatim
3637 (org-test-with-parsed-data "#+TITLE: =verb= a"
3638 (org-element-type
3639 (org-export-get-previous-element
3640 (org-element-map
3641 (plist-get info :title) 'plain-text 'identity info t) info)))))
3642 ;; Find previous element in parsed affiliated keywords.
3643 (should
3644 (eq 'verbatim
3645 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
3646 (org-element-type
3647 (org-export-get-previous-element
3648 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3649 ;; With optional argument N, return a list containing up to
3650 ;; N previous elements.
3651 (should
3652 (equal '(underline italic bold)
3653 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3654 (mapcar 'car
3655 (org-export-get-previous-element
3656 (org-element-map tree 'code 'identity info t) info t)))))
3657 ;; When N is a positive integer, return a list containing up to
3658 ;; N previous elements.
3659 (should
3660 (equal '(italic bold)
3661 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3662 (mapcar 'car
3663 (org-export-get-previous-element
3664 (org-element-map tree 'code 'identity info t) info 2))))))
3667 (provide 'test-ox)
3668 ;;; test-org-export.el end here