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