org.texi: Overwrite with the one generated from "org-manual.org"
[org-mode/org-tableheadings.git] / testing / lisp / test-ox.el
blob1c429c83c30a6f885d7bda41719a2cf4f72a9a8a
1 ;;; test-ox.el --- Tests for ox.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2016 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 (require 'cl-lib)
26 (unless (featurep 'ox)
27 (signal 'missing-test-dependency "org-export"))
29 (defun org-test-default-backend ()
30 "Return a default export back-end.
31 This back-end simply returns parsed data as Org syntax."
32 (org-export-create-backend
33 :transcoders
34 (mapcar (lambda (type)
35 (cons type
36 (lambda (o c _)
37 (funcall
38 (intern (format "org-element-%s-interpreter" type))
39 o c))))
40 (append org-element-all-elements org-element-all-objects))))
42 (defmacro org-test-with-parsed-data (data &rest body)
43 "Execute body with parsed data available.
44 DATA is a string containing the data to be parsed. BODY is the
45 body to execute. Parse tree is available under the `tree'
46 variable, and communication channel under `info'."
47 (declare (debug (form body)) (indent 1))
48 `(org-test-with-temp-text ,data
49 (org-export--delete-comment-trees)
50 (let* ((tree (org-element-parse-buffer))
51 (info (org-combine-plists
52 (org-export--get-export-attributes)
53 (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 (let ((options
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 (and (eq (plist-get options :headline-levels) 1)
117 (eq (plist-get options :section-numbers) t)
118 (eq (plist-get options :preserve-breaks) t)
119 (eq (plist-get options :time-stamp-file) t)
120 (eq (plist-get options :with-archived-trees) t)
121 (eq (plist-get options :with-author) t)
122 (eq (plist-get options :with-drawers) t)
123 (eq (plist-get options :with-email) t)
124 (eq (plist-get options :with-emphasize) t)
125 (eq (plist-get options :with-entities) t)
126 (eq (plist-get options :with-fixed-width) t)
127 (eq (plist-get options :with-footnotes) t)
128 (eq (plist-get options :with-priority) t)
129 (eq (plist-get options :with-special-strings) t)
130 (eq (plist-get options :with-sub-superscript) t)
131 (eq (plist-get options :with-toc) t)
132 (eq (plist-get options :with-tables) t)
133 (eq (plist-get options :with-tags) t)
134 (eq (plist-get options :with-tasks) t)
135 (eq (plist-get options :with-timestamps) t)
136 (eq (plist-get options :with-todo-keywords) t)
137 (eq (plist-get options :with-inlinetasks) nil)
138 (eq (plist-get options :with-statistics-cookies) t)
139 (eq (plist-get options :with-title) t))))
140 ;; Test some special values.
141 (should
142 (let ((options
143 (org-export--parse-option-keyword
144 "arch:headline d:(\"TEST\") ^:{} toc:1 tags:not-in-toc tasks:todo \
145 num:2 <:active")))
146 (and (eq (plist-get options :with-archived-trees) 'headline)
147 (eq (plist-get options :with-sub-superscript) '{})
148 (eq (plist-get options :with-toc) 1)
149 (eq (plist-get options :with-tags) 'not-in-toc)
150 (eq (plist-get options :with-tasks) 'todo)
151 (eq (plist-get options :section-numbers) 2)
152 (eq (plist-get options :with-timestamps) 'active)
153 (equal (plist-get options :with-drawers) '("TEST")))))
154 ;; Test back-end specific values.
155 (should
156 (equal
157 (org-export--parse-option-keyword
158 "opt:t" (org-export-create-backend :options '((:option nil "opt"))))
159 '(:option t)))
160 ;; More than one property can refer to the same option item.
161 (should
162 (equal '(:opt1 t :opt2 t)
163 (org-export--parse-option-keyword
164 "opt:t"
165 (org-export-create-backend
166 :options '((:opt1 nil "opt") (:opt2 nil "opt")))))))
168 (ert-deftest test-org-export/get-inbuffer-options ()
169 "Test reading all standard export keywords."
170 ;; Properties should follow buffer order.
171 (should
172 (equal
173 (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
174 (org-export--get-inbuffer-options))
175 '(:language "fr" :creator "Me" :email "email")))
176 ;; Test `space' behaviour.
177 (should
178 (equal
179 (let ((back-end (org-export-create-backend
180 :options '((:keyword "KEYWORD" nil nil space)))))
181 (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: spaces"
182 (org-export--get-inbuffer-options back-end)))
183 '(:keyword "With spaces")))
184 ;; Test `newline' behaviour.
185 (should
186 (equal
187 (let ((back-end (org-export-create-backend
188 :options '((:keyword "KEYWORD" nil nil newline)))))
189 (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: two lines"
190 (org-export--get-inbuffer-options back-end)))
191 '(:keyword "With\ntwo lines")))
192 ;; Test `split' behaviour.
193 (should
194 (equal
195 (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
196 (org-export--get-inbuffer-options))
197 '(:select-tags ("a" "b"))))
198 ;; Test `parse' behaviour. `parse' implies `space' but preserve
199 ;; line breaks. Multi-line objects are allowed.
200 (should
201 (org-element-map
202 (org-test-with-temp-text "#+TITLE: *bold*"
203 (plist-get (org-export--get-inbuffer-options) :title))
204 'bold #'identity nil t))
205 (should
206 (equal
207 (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
208 (plist-get (org-export--get-inbuffer-options) :title))
209 '("Some title with spaces")))
210 (should
211 (org-element-map
212 (org-test-with-temp-text "#+TITLE: Some title\\\\\n#+TITLE: with spaces"
213 (plist-get (org-export--get-inbuffer-options) :title))
214 'line-break #'identity nil t))
215 (should
216 (org-element-map
217 (org-test-with-temp-text "#+TITLE: *bold\n#+TITLE: sentence*"
218 (plist-get (org-export--get-inbuffer-options) :title))
219 'bold #'identity nil t))
220 ;; Options set through SETUPFILE.
221 (should
222 (equal
223 (org-test-with-temp-text
224 (format "#+DESCRIPTION: l1
225 #+LANGUAGE: es
226 #+SELECT_TAGS: a
227 #+TITLE: a
228 #+SETUPFILE: \"%s/examples/setupfile.org\"
229 #+LANGUAGE: fr
230 #+SELECT_TAGS: c
231 #+TITLE: c"
232 org-test-dir)
233 (org-export--get-inbuffer-options))
234 '(:language "fr" :select-tags ("a" "b" "c") :title ("a b c"))))
235 ;; Options set through SETUPFILE specified using a URL.
236 (let ((buffer (generate-new-buffer "url-retrieve-output")))
237 (unwind-protect
238 ;; Simulate successful retrieval of a setupfile from URL.
239 (cl-letf (((symbol-function 'url-retrieve-synchronously)
240 (lambda (&rest_)
241 (with-current-buffer buffer
242 (insert "HTTP/1.1 200 OK
244 # Contents of http://link-to-my-setupfile.org
245 #+BIND: variable value
246 #+DESCRIPTION: l2
247 #+LANGUAGE: en
248 #+SELECT_TAGS: b
249 #+TITLE: b
250 #+PROPERTY: a 1
252 buffer)))
253 (should
254 (equal
255 (org-test-with-temp-text
256 "#+DESCRIPTION: l1
257 #+LANGUAGE: es
258 #+SELECT_TAGS: a
259 #+TITLE: a
260 #+SETUPFILE: \"http://link-to-my-setupfile.org\"
261 #+LANGUAGE: fr
262 #+SELECT_TAGS: c
263 #+TITLE: c"
264 (org-export--get-inbuffer-options))
265 '(:language "fr" :select-tags ("a" "b" "c") :title ("a b c")))))
266 (kill-buffer buffer)))
267 ;; More than one property can refer to the same buffer keyword.
268 (should
269 (equal '(:k2 "value" :k1 "value")
270 (let ((backend (org-export-create-backend
271 :options '((:k1 "KEYWORD")
272 (:k2 "KEYWORD")))))
273 (org-test-with-temp-text "#+KEYWORD: value"
274 (org-export--get-inbuffer-options backend)))))
275 ;; Keywords in commented subtrees are ignored.
276 (should-not
277 (equal "Me"
278 (org-test-with-parsed-data "* COMMENT H1\n#+AUTHOR: Me"
279 (plist-get info :author))))
280 (should-not
281 (equal "Mine"
282 (org-test-with-parsed-data "* COMMENT H1\n** H2\n#+EMAIL: Mine"
283 (plist-get info :email)))))
285 (ert-deftest test-org-export/get-subtree-options ()
286 "Test setting options from headline's properties."
287 ;; EXPORT_TITLE.
288 (should
289 (equal '("Subtree Title")
290 (org-test-with-temp-text "#+TITLE: Title
291 * Headline<point>
292 :PROPERTIES:
293 :EXPORT_TITLE: Subtree Title
294 :END:
295 Paragraph"
296 (plist-get (org-export-get-environment nil t) :title))))
297 ;; EXPORT_OPTIONS.
298 (should
299 (= 2
300 (org-test-with-temp-text "#+OPTIONS: H:1
301 * Headline<point>
302 :PROPERTIES:
303 :EXPORT_OPTIONS: H:2
304 :END:
305 Paragraph"
306 (plist-get (org-export-get-environment nil t) :headline-levels))))
307 ;; EXPORT_DATE.
308 (should
309 (equal '("29-03-2012")
310 (org-test-with-temp-text "#+DATE: today
311 * Headline<point>
312 :PROPERTIES:
313 :EXPORT_DATE: 29-03-2012
314 :END:
315 Paragraph"
316 (plist-get (org-export-get-environment nil t) :date))))
317 ;; Properties with `split' behaviour are stored as a list of
318 ;; strings.
319 (should
320 (equal '("a" "b")
321 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
322 * Headline<point>
323 :PROPERTIES:
324 :EXPORT_EXCLUDE_TAGS: a b
325 :END:
326 Paragraph"
327 (plist-get (org-export-get-environment nil t) :exclude-tags))))
328 ;; Handle :PROPERTY+: syntax.
329 (should
330 (equal '("a" "b")
331 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
332 * Headline<point>
333 :PROPERTIES:
334 :EXPORT_EXCLUDE_TAGS: a
335 :EXPORT_EXCLUDE_TAGS+: b
336 :END:
337 Paragraph"
338 (plist-get (org-export-get-environment nil t) :exclude-tags))))
339 ;; Export properties are case-insensitive.
340 (should
341 (equal '("29-03-2012")
342 (org-test-with-temp-text "* Headline
343 :PROPERTIES:
344 :EXPORT_Date: 29-03-2012
345 :END:
346 Paragraph"
347 (plist-get (org-export-get-environment nil t) :date))))
348 ;; Still grab correct options when section above is empty.
349 (should
350 (equal '("H1")
351 (org-test-with-temp-text "* H1\n** H11\n** H12<point>"
352 (plist-get (org-export-get-environment nil t) :title))))
353 ;; More than one property can refer to the same node property.
354 (should
355 (equal '("1" "1")
356 (org-test-with-temp-text
357 "* H\n:PROPERTIES:\n:EXPORT_A: 1\n:END:\n<point>"
358 (let* ((backend (org-export-create-backend
359 :options '((:k1 "A")
360 (:k2 "A"))))
361 (options (org-export-get-environment backend t)))
362 (list (plist-get options :k1) (plist-get options :k2)))))))
364 (ert-deftest test-org-export/set-title ()
365 "Test title setting."
366 ;; Without TITLE keyword.
367 (should
368 (equal
370 (let (org-export-filter-body-functions
371 org-export-filter-final-output-functions)
372 (org-test-with-temp-text "Test"
373 (org-export-as
374 (org-export-create-backend
375 :transcoders
376 '((template . (lambda (text info)
377 (org-element-interpret-data
378 (plist-get info :title)))))))))))
379 ;; With a blank TITLE keyword.
380 (should
381 (equal
383 (let (org-export-filter-body-functions
384 org-export-filter-final-output-functions)
385 (org-test-with-temp-text "#+TITLE:\nTest"
386 (org-export-as
387 (org-export-create-backend
388 :transcoders
389 '((template . (lambda (text info)
390 (org-element-interpret-data
391 (plist-get info :title)))))))))))
392 ;; With a non-empty TITLE keyword.
393 (should
394 (equal
395 "Title"
396 (org-test-with-temp-text "#+TITLE: Title\nTest"
397 (org-export-as
398 (org-export-create-backend
399 :transcoders
400 '((template . (lambda (text info)
401 (org-element-interpret-data
402 (plist-get info :title))))))))))
403 ;; When exporting a subtree, its heading becomes the headline of the
404 ;; document...
405 (should
406 (equal
407 "Headline"
408 (org-test-with-temp-text "* Headline\nBody"
409 (org-export-as
410 (org-export-create-backend
411 :transcoders
412 '((template . (lambda (text info)
413 (org-element-interpret-data
414 (plist-get info :title))))))
415 'subtree))))
416 ;; ... unless there is an EXPORT_TITLE property at the root of the
417 ;; subtree.
418 (should
419 (equal
421 (org-test-with-temp-text
422 "* A\n :PROPERTIES:\n :EXPORT_TITLE: B\n :END:\nBody"
423 (org-export-as
424 (org-export-create-backend
425 :transcoders
426 '((template . (lambda (text info)
427 (org-element-interpret-data
428 (plist-get info :title))))))
429 'subtree)))))
431 (ert-deftest test-org-export/handle-options ()
432 "Test if export options have an impact on output."
433 ;; Test exclude tags for headlines and inlinetasks.
434 (should
435 (equal ""
436 (let (org-export-filter-body-functions
437 org-export-filter-final-output-functions)
438 (org-test-with-temp-text "* Head1 :noexp:"
439 (org-export-as (org-test-default-backend)
440 nil nil nil '(:exclude-tags ("noexp")))))))
441 (should
442 (equal "#+filetags: noexp\n"
443 (let (org-export-filter-body-functions
444 org-export-filter-final-output-functions)
445 (org-test-with-temp-text "#+FILETAGS: noexp\n* Head1"
446 (org-export-as (org-test-default-backend)
447 nil nil nil '(:exclude-tags ("noexp")))))))
448 ;; Excluding a tag excludes its whole group.
449 (should
450 (equal ""
451 (let (org-export-filter-body-functions
452 org-export-filter-final-output-functions)
453 (org-test-with-temp-text "* Head1 :baz:"
454 (let ((org-tag-alist '((:startgrouptag)
455 ("foo") (:grouptags) ("bar") ("baz")
456 (:endgrouptag))))
457 (org-export-as (org-test-default-backend)
458 nil nil nil '(:exclude-tags ("foo"))))))))
459 ;; Test include tags for headlines and inlinetasks.
460 (should
461 (equal (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
462 (let ((org-tags-column 0))
463 (org-export-as (org-test-default-backend)
464 nil nil nil '(:select-tags ("exp")))))
465 "* H2\n** Sub :exp:\n*** Sub Sub\n"))
466 ;; Including a tag includes its whole group.
467 (should
468 (string-match-p
469 "\\`\\* H2"
470 (let (org-export-filter-body-functions
471 org-export-filter-final-output-functions)
472 (org-test-with-temp-text "* H1\n* H2 :bar:"
473 (let ((org-tag-alist '((:startgrouptag)
474 ("foo") (:grouptags) ("bar") ("baz")
475 (:endgrouptag))))
476 (org-export-as (org-test-default-backend)
477 nil nil nil '(:select-tags ("foo"))))))))
478 ;; If there is an include tag, ignore the section before the first
479 ;; headline, if any.
480 (should
481 (equal (org-test-with-temp-text "First section\n* H1 :exp:\nBody"
482 (let ((org-tags-column 0))
483 (org-export-as (org-test-default-backend)
484 nil nil nil '(:select-tags ("exp")))))
485 "* H1 :exp:\nBody\n"))
486 (should
487 (equal (org-test-with-temp-text "#+FILETAGS: exp\nFirst section\n* H1\nBody"
488 (org-export-as (org-test-default-backend)
489 nil nil nil '(:select-tags ("exp"))))
490 "* H1\nBody\n"))
491 (should-not
492 (equal (org-test-with-temp-text "* H1 :exp:\nBody"
493 (let ((org-tags-column 0))
494 (org-export-as (org-test-default-backend)
495 nil nil nil '(:select-tags ("exp")))))
496 "* H1 :exp:\n"))
497 ;; Test mixing include tags and exclude tags.
498 (should
499 (string-match
500 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
501 (org-test-with-temp-text "
502 * Head1 :export:
503 ** Sub-Head1 :noexport:
504 ** Sub-Head2
505 * Head2 :noexport:
506 ** Sub-Head1 :export:"
507 (org-export-as (org-test-default-backend) nil nil nil
508 '(:select-tags ("export") :exclude-tags ("noexport"))))))
509 ;; Ignore tasks.
510 (should
511 (equal ""
512 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
513 org-export-filter-body-functions
514 org-export-filter-final-output-functions)
515 (org-test-with-temp-text "* TODO Head1"
516 (org-export-as (org-test-default-backend)
517 nil nil nil '(:with-tasks nil))))))
518 (should
519 (equal "* TODO Head1\n"
520 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
521 (org-test-with-temp-text "* TODO Head1"
522 (org-export-as (org-test-default-backend)
523 nil nil nil '(:with-tasks t))))))
524 ;; Archived tree.
525 (should
526 (equal ""
527 (let (org-export-filter-body-functions
528 org-export-filter-final-output-functions)
529 (org-test-with-temp-text "* Head1 :archive:"
530 (let ((org-archive-tag "archive"))
531 (org-export-as (org-test-default-backend)
532 nil nil nil '(:with-archived-trees nil)))))))
533 (should
534 (string-match
535 "\\* Head1[ \t]+:archive:"
536 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
537 (let ((org-archive-tag "archive"))
538 (org-export-as (org-test-default-backend) nil nil nil
539 '(:with-archived-trees headline))))))
540 (should
541 (string-match
542 "\\`\\* Head1[ \t]+:archive:\n\\'"
543 (org-test-with-temp-text "* Head1 :archive:"
544 (let ((org-archive-tag "archive"))
545 (org-export-as (org-test-default-backend)
546 nil nil nil '(:with-archived-trees t))))))
547 ;; Broken links. Depending on `org-export-with-broken-links', raise
548 ;; an error, ignore link or mark is as broken in output.
549 (should-error
550 (org-test-with-temp-text "[[#broken][link]]"
551 (let ((backend
552 (org-export-create-backend
553 :transcoders
554 '((section . (lambda (_e c _i) c))
555 (paragraph . (lambda (_e c _i) c))
556 (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
557 (org-export-as backend nil nil nil '(:with-broken-links nil)))))
558 (should
559 (org-test-with-temp-text "[[#broken][link]]"
560 (let ((backend
561 (org-export-create-backend
562 :transcoders
563 '((section . (lambda (_e c _i) c))
564 (paragraph . (lambda (_e c _i) c))
565 (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
566 (org-export-as backend nil nil nil '(:with-broken-links t)))))
567 (should
568 (org-test-with-temp-text "[[#broken][link]]"
569 (let ((backend
570 (org-export-create-backend
571 :transcoders
572 '((section . (lambda (_e c _i) c))
573 (paragraph . (lambda (_e c _i) c))
574 (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
575 (org-string-nw-p
576 (org-export-as backend nil nil nil '(:with-broken-links mark))))))
577 ;; Clocks.
578 (should
579 (string-match "CLOCK: \\[2012-04-29 .* 10:45\\]"
580 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
581 (org-export-as (org-test-default-backend)
582 nil nil nil '(:with-clocks t)))))
583 (should
584 (equal ""
585 (let (org-export-filter-body-functions
586 org-export-filter-final-output-functions)
587 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
588 (org-export-as (org-test-default-backend)
589 nil nil nil '(:with-clocks nil))))))
590 ;; Drawers.
591 (should
592 (equal ""
593 (let (org-export-filter-body-functions
594 org-export-filter-final-output-functions)
595 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
596 (org-export-as (org-test-default-backend)
597 nil nil nil '(:with-drawers nil))))))
598 (should
599 (equal ":TEST:\ncontents\n:END:\n"
600 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
601 (org-export-as (org-test-default-backend)
602 nil nil nil '(:with-drawers t)))))
603 (should
604 (equal ":FOO:\nkeep\n:END:\n"
605 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
606 (org-export-as (org-test-default-backend)
607 nil nil nil '(:with-drawers ("FOO"))))))
608 (should
609 (equal ":FOO:\nkeep\n:END:\n"
610 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
611 (org-export-as (org-test-default-backend)
612 nil nil nil '(:with-drawers (not "BAR"))))))
613 ;; Fixed-width.
614 (should
615 (equal ": A\n"
616 (org-test-with-temp-text ": A"
617 (org-export-as (org-test-default-backend) nil nil nil
618 '(:with-fixed-width t)))))
619 (should
620 (equal ""
621 (let (org-export-filter-body-functions
622 org-export-filter-final-output-functions)
623 (org-test-with-temp-text ": A"
624 (org-export-as (org-test-default-backend) nil nil nil
625 '(:with-fixed-width nil))))))
626 ;; Footnotes.
627 (should
628 (equal "Footnote?"
629 (let ((org-footnote-section nil))
630 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
631 (org-trim (org-export-as (org-test-default-backend)
632 nil nil nil '(:with-footnotes nil)))))))
633 (should
634 (equal "Footnote?[fn:1]\n\n[fn:1] Def"
635 (let ((org-footnote-section nil))
636 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
637 (org-trim (org-export-as (org-test-default-backend)
638 nil nil nil '(:with-footnotes t)))))))
639 ;; Inlinetasks.
640 (when (featurep 'org-inlinetask)
641 (should
642 (equal
644 (let ((org-inlinetask-min-level 15)
645 org-export-filter-body-functions
646 org-export-filter-final-output-functions)
647 (org-test-with-temp-text "*************** Task"
648 (org-export-as (org-test-default-backend)
649 nil nil nil '(:with-inlinetasks nil))))))
650 (should
651 (equal
653 (let ((org-inlinetask-min-level 15)
654 org-export-filter-body-functions
655 org-export-filter-final-output-functions)
656 (org-test-with-temp-text
657 "*************** Task\nContents\n*************** END"
658 (org-export-as (org-test-default-backend)
659 nil nil nil '(:with-inlinetasks nil)))))))
660 ;; Plannings.
661 (should
662 (string-match
663 "* H\nCLOSED: \\[2012-04-29 .* 10:45\\]"
664 (let ((org-closed-string "CLOSED:"))
665 (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
666 (org-export-as (org-test-default-backend)
667 nil nil nil '(:with-planning t))))))
668 (should
669 (equal "* H\n"
670 (let ((org-closed-string "CLOSED:"))
671 (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
672 (org-export-as (org-test-default-backend)
673 nil nil nil '(:with-planning nil))))))
674 ;; Property Drawers.
675 (should
676 (equal "* H1\n"
677 (org-test-with-temp-text
678 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
679 (org-export-as (org-test-default-backend)
680 nil nil nil '(:with-properties nil)))))
681 (should
682 (equal "* H1\n:PROPERTIES:\n:PROP: value\n:END:\n"
683 (org-test-with-temp-text
684 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
685 (org-export-as (org-test-default-backend)
686 nil nil nil '(:with-properties t)))))
687 (should
688 (equal "* H1\n:PROPERTIES:\n:B: 2\n:END:\n"
689 (org-test-with-temp-text
690 "* H1\n :PROPERTIES:\n :A: 1\n :B: 2\n:END:"
691 (org-export-as (org-test-default-backend)
692 nil nil nil '(:with-properties ("B"))))))
693 ;; Statistics cookies.
694 (should
695 (equal "* Stats"
696 (let (org-export-filter-body-functions
697 org-export-filter-final-output-functions)
698 (org-trim
699 (org-test-with-temp-text "* Stats [0/0]"
700 (org-export-as (org-test-default-backend)
701 nil nil nil '(:with-statistics-cookies nil)))))))
702 ;; Tables.
703 (should
704 (equal "| A |\n"
705 (org-test-with-temp-text "| A |"
706 (org-export-as (org-test-default-backend) nil nil nil
707 '(:with-tables t)))))
708 (should
709 (equal ""
710 (let (org-export-filter-body-functions
711 org-export-filter-final-output-functions)
712 (org-test-with-temp-text "| A |"
713 (org-export-as (org-test-default-backend) nil nil nil
714 '(:with-tables nil)))))))
716 (ert-deftest test-org-export/with-timestamps ()
717 "Test `org-export-with-timestamps' specifications."
718 ;; t value.
719 (should
720 (string-match
721 "\\[2012-04-29 .*? 10:45\\]<2012-04-29 .*? 10:45>"
722 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
723 (org-export-as (org-test-default-backend)
724 nil nil nil '(:with-timestamps t)))))
725 ;; nil value.
726 (should
727 (equal
729 (let (org-export-filter-body-functions
730 org-export-filter-final-output-functions)
731 (org-trim
732 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
733 (org-export-as (org-test-default-backend)
734 nil nil nil '(:with-timestamps nil)))))))
735 ;; `active' value.
736 (should
737 (string-match
738 "<2012-03-29 .*?>\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
739 (org-test-with-temp-text
740 "<2012-03-29 Thu>[2012-03-29 Thu]
742 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
743 (org-export-as (org-test-default-backend)
744 nil nil nil '(:with-timestamps active)))))
745 ;; `inactive' value.
746 (should
747 (string-match
748 "\\[2012-03-29 .*?\\]\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
749 (org-test-with-temp-text
750 "<2012-03-29 Thu>[2012-03-29 Thu]
752 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
753 (org-export-as (org-test-default-backend)
754 nil nil nil '(:with-timestamps inactive))))))
756 (ert-deftest test-org-export/comment-tree ()
757 "Test if export process ignores commented trees."
758 (should
759 (equal ""
760 (let (org-export-filter-body-functions
761 org-export-filter-final-output-functions)
762 (org-test-with-temp-text "* COMMENT Head1"
763 (org-export-as (org-test-default-backend)))))))
765 (ert-deftest test-org-export/uninterpreted ()
766 "Test handling of uninterpreted elements."
767 ;; Entities.
768 (should
769 (equal "dummy\n"
770 (org-test-with-temp-text "\\alpha"
771 (org-export-as
772 (org-export-create-backend
773 :transcoders '((entity . (lambda (e c i) "dummy"))
774 (paragraph . (lambda (p c i) c))
775 (section . (lambda (s c i) c))))
776 nil nil nil '(:with-entities t)))))
777 (should
778 (equal "\\alpha\n"
779 (org-test-with-temp-text "\\alpha"
780 (org-export-as
781 (org-export-create-backend
782 :transcoders '((entity . (lambda (e c i) "dummy"))
783 (paragraph . (lambda (p c i) c))
784 (section . (lambda (s c i) c))))
785 nil nil nil '(:with-entities nil)))))
786 ;; Emphasis.
787 (should
788 (equal "dummy\n"
789 (org-test-with-temp-text "*bold*"
790 (org-export-as
791 (org-export-create-backend
792 :transcoders '((bold . (lambda (b c i) "dummy"))
793 (paragraph . (lambda (p c i) c))
794 (section . (lambda (s c i) c))))
795 nil nil nil '(:with-emphasize t)))))
796 (should
797 (equal "*bold*\n"
798 (org-test-with-temp-text "*bold*"
799 (org-export-as
800 (org-export-create-backend
801 :transcoders '((bold . (lambda (b c i) "dummy"))
802 (paragraph . (lambda (p c i) c))
803 (section . (lambda (s c i) c))))
804 nil nil nil '(:with-emphasize nil)))))
805 (should
806 (equal "/simple/ /example/\n"
807 (org-test-with-temp-text "/simple/ /example/"
808 (org-export-as
809 (org-export-create-backend
810 :transcoders '((bold . (lambda (b c i) "dummy"))
811 (paragraph . (lambda (p c i) c))
812 (section . (lambda (s c i) c))))
813 nil nil nil '(:with-emphasize nil)))))
814 ;; LaTeX environment.
815 (should
816 (equal "dummy\n"
817 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
818 (org-export-as
819 (org-export-create-backend
820 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
821 (section . (lambda (s c i) c))))
822 nil nil nil '(:with-latex t)))))
823 (should
824 (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
825 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
826 (org-export-as
827 (org-export-create-backend
828 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
829 (section . (lambda (s c i) c))))
830 nil nil nil '(:with-latex verbatim)))))
831 ;; LaTeX fragment.
832 (should
833 (equal "dummy\n"
834 (org-test-with-temp-text "$1$"
835 (org-export-as
836 (org-export-create-backend
837 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
838 (paragraph . (lambda (p c i) c))
839 (section . (lambda (s c i) c))))
840 nil nil nil '(:with-latex t)))))
841 (should
842 (equal "$1$\n"
843 (org-test-with-temp-text "$1$"
844 (org-export-as
845 (org-export-create-backend
846 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
847 (paragraph . (lambda (p c i) c))
848 (section . (lambda (s c i) c))))
849 nil nil nil '(:with-latex verbatim)))))
850 ;; Sub/superscript.
851 (should
852 (equal "adummy\n"
853 (org-test-with-temp-text "a_b"
854 (org-export-as
855 (org-export-create-backend
856 :transcoders '((subscript . (lambda (s c i) "dummy"))
857 (paragraph . (lambda (p c i) c))
858 (section . (lambda (s c i) c))))
859 nil nil nil '(:with-sub-superscript t)))))
860 (should
861 (equal "a_b\n"
862 (org-test-with-temp-text "a_b"
863 (org-export-as
864 (org-export-create-backend
865 :transcoders '((subscript . (lambda (s c i) "dummy"))
866 (paragraph . (lambda (p c i) c))
867 (section . (lambda (s c i) c))))
868 nil nil nil '(:with-sub-superscript nil)))))
869 (should
870 (equal "a_b\n"
871 (org-test-with-temp-text "a_b"
872 (org-export-as
873 (org-export-create-backend
874 :transcoders '((subscript . (lambda (s c i) "dummy"))
875 (paragraph . (lambda (p c i) c))
876 (section . (lambda (s c i) c))))
877 nil nil nil '(:with-sub-superscript {})))))
878 (should
879 (equal "adummy\n"
880 (org-test-with-temp-text "a_{b}"
881 (org-export-as
882 (org-export-create-backend
883 :transcoders '((subscript . (lambda (s c i) "dummy"))
884 (paragraph . (lambda (p c i) c))
885 (section . (lambda (s c i) c))))
886 nil nil nil '(:with-sub-superscript {})))))
887 (should
888 (equal "a_entity\n"
889 (org-test-with-temp-text "a_\\alpha"
890 (org-export-as
891 (org-export-create-backend
892 :transcoders '((entity . (lambda (e c i) "entity"))
893 (subscript . (lambda (s c i) "dummy"))
894 (paragraph . (lambda (p c i) c))
895 (section . (lambda (s c i) c))))
896 nil nil nil '(:with-sub-superscript nil)))))
897 ;; Handle uninterpreted objects in parsed keywords.
898 (should
899 (equal "a_b"
900 (org-test-with-temp-text "#+TITLE: a_b"
901 (org-export-as
902 (org-export-create-backend
903 :transcoders
904 '((subscript . (lambda (s c i) "dummy"))
905 (template . (lambda (c i)
906 (org-export-data (plist-get i :title) i)))
907 (section . (lambda (s c i) c))))
908 nil nil nil '(:with-sub-superscript nil)))))
909 (should
910 (equal "a_b"
911 (org-test-with-temp-text "#+FOO: a_b"
912 (org-export-as
913 (org-export-create-backend
914 :options
915 '((:foo "FOO" nil nil parse))
916 :transcoders
917 '((subscript . (lambda (s c i) "dummy"))
918 (template . (lambda (c i)
919 (org-export-data (plist-get i :foo) i)))
920 (section . (lambda (s c i) c))))
921 nil nil nil '(:with-sub-superscript nil)))))
922 ;; Objects in parsed keywords are "uninterpreted" before filters are
923 ;; applied.
924 (should
925 (org-test-with-temp-text "#+TITLE: a_b"
926 (org-export-as
927 (org-export-create-backend
928 :filters
929 '((:filter-options
930 (lambda (i _)
931 (org-element-map (plist-get i :title) 'subscript
932 (lambda (_) (error "There should be no subscript here")))))))
933 nil nil nil '(:with-sub-superscript nil))))
934 ;; Handle uninterpreted objects in captions.
935 (should
936 (equal "adummy\n"
937 (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
938 (org-export-as
939 (org-export-create-backend
940 :transcoders
941 '((subscript . (lambda (s c i) "dummy"))
942 (paragraph . (lambda (p c i)
943 (org-export-data (org-export-get-caption p) i)))
944 (section . (lambda (s c i) c))))
945 nil nil nil '(:with-sub-superscript t)))))
946 (should
947 (equal "a_b\n"
948 (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
949 (org-export-as
950 (org-export-create-backend
951 :transcoders
952 '((subscript . (lambda (s c i) "dummy"))
953 (paragraph . (lambda (p c i)
954 (org-export-data (org-export-get-caption p) i)))
955 (section . (lambda (s c i) c))))
956 nil nil nil '(:with-sub-superscript nil)))))
957 ;; Special case: multiples uninterpreted objects in a row.
958 (should
959 (equal "a_b_c_d\n"
960 (org-test-with-temp-text "a_b_c_d"
961 (org-export-as
962 (org-export-create-backend
963 :transcoders '((subscript . (lambda (s c i) "dummy"))
964 (paragraph . (lambda (p c i) c))
965 (section . (lambda (s c i) c))))
966 nil nil nil '(:with-sub-superscript {}))))))
968 (ert-deftest test-org-export/export-scope ()
969 "Test all export scopes."
970 ;; Subtree.
971 (should
972 (equal "text\n*** H3\n"
973 (org-test-with-temp-text "* H1\n<point>** H2\ntext\n*** H3"
974 (org-export-as (org-test-default-backend) 'subtree))))
975 (should
976 (equal "text\n*** H3\n"
977 (org-test-with-temp-text "* H1\n** H2\n<point>text\n*** H3"
978 (org-export-as (org-test-default-backend) 'subtree))))
979 ;; Subtree with a code block calling another block outside.
980 (should
981 (equal ": 3\n"
982 (org-test-with-temp-text "
983 <point>* Head1
984 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
985 <<test>>
986 #+END_SRC
987 * Head2
988 #+NAME: test
989 #+BEGIN_SRC emacs-lisp
990 \(+ 1 2)
991 #+END_SRC"
992 (let ((org-export-use-babel t))
993 (org-export-as (org-test-default-backend) 'subtree)))))
994 ;; Subtree export should ignore leading planning line and property
995 ;; drawer.
996 (should
997 (equal "Text\n"
998 (org-test-with-temp-text "
999 <point>* H
1000 SCHEDULED: <2012-03-29 Thu>
1001 :PROPERTIES:
1002 :A: 1
1003 :END:
1004 Text"
1005 (org-export-as (org-test-default-backend)
1006 'subtree nil nil
1007 '(:with-planning t :with-properties t)))))
1008 ;; Visible.
1009 (should
1010 (equal "* H1\n"
1011 (org-test-with-temp-text "* H1\n** H2\ntext\n*** H3"
1012 (org-cycle)
1013 (org-export-as (org-test-default-backend) nil 'visible))))
1014 ;; Region.
1015 (should
1016 (equal "text\n"
1017 (org-test-with-temp-text "* H1\n** H2\n<point>text\n*** H3"
1018 (transient-mark-mode 1)
1019 (push-mark (point) t t)
1020 (end-of-line)
1021 (org-export-as (org-test-default-backend)))))
1022 ;; Body only.
1023 (should
1024 (equal "Text\n"
1025 (org-test-with-temp-text "Text"
1026 (org-export-as
1027 (org-export-create-backend
1028 :transcoders
1029 '((template . (lambda (b _i) (format "BEGIN\n%sEND" b)))
1030 (section . (lambda (_s c _i) c))
1031 (paragraph . (lambda (_p c _i) c))))
1032 nil nil 'body-only))))
1033 (should
1034 (equal "BEGIN\nText\nEND"
1035 (org-test-with-temp-text "Text"
1036 (org-export-as
1037 (org-export-create-backend
1038 :transcoders
1039 '((template . (lambda (b _i) (format "BEGIN\n%sEND" b)))
1040 (section . (lambda (_s c _i) c))
1041 (paragraph . (lambda (_p c _i) c))))))))
1042 ;; Pathological case: Body only on an empty buffer is expected to
1043 ;; return an empty string, not nil.
1044 (should
1045 (org-test-with-temp-text ""
1046 (org-export-as (org-test-default-backend) nil nil t))))
1048 (ert-deftest test-org-export/output-file-name ()
1049 "Test `org-export-output-file-name' specifications."
1050 ;; Export from a file: name is built from original file name.
1051 (should
1052 (org-test-with-temp-text-in-file "Test"
1053 (equal (file-name-base (buffer-file-name))
1054 (file-name-base (org-export-output-file-name ".ext")))))
1055 ;; When #+EXPORT_FILE_NAME is defined, use it.
1056 (should
1057 (equal "test.ext"
1058 (org-test-with-temp-text-in-file "#+EXPORT_FILE_NAME: test"
1059 (org-export-output-file-name ".ext" t))))
1060 ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
1061 (should
1062 (equal "test.ext"
1063 (org-test-with-temp-text-in-file
1064 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
1065 (org-export-output-file-name ".ext" t))))
1066 (should
1067 (equal "property.ext"
1068 (org-test-with-temp-text
1069 "#+EXPORT_FILE_NAME: keyword
1070 * Test<point>
1071 :PROPERTIES:
1072 :EXPORT_FILE_NAME: property
1073 :END:"
1074 (org-export-output-file-name ".ext" t))))
1075 ;; From a buffer not associated to a file, too.
1076 (should
1077 (equal "test.ext"
1078 (org-test-with-temp-text
1079 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
1080 (org-export-output-file-name ".ext" t))))
1081 ;; When provided name is absolute, preserve it.
1082 (should
1083 (org-test-with-temp-text
1084 (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
1085 (expand-file-name "test"))
1086 (file-name-absolute-p (org-export-output-file-name ".ext" t))))
1087 ;; When PUB-DIR argument is provided, use it.
1088 (should
1089 (equal "dir/"
1090 (org-test-with-temp-text-in-file "Test"
1091 (file-name-directory
1092 (org-export-output-file-name ".ext" nil "dir/")))))
1093 ;; PUB-DIR has precedence over EXPORT_FILE_NAME keyword or property.
1094 (should
1095 (equal "pub-dir/"
1096 (org-test-with-temp-text-in-file
1097 "#+EXPORT_FILE_NAME: /dir/keyword\nTest"
1098 (file-name-directory
1099 (org-export-output-file-name ".ext" nil "pub-dir/")))))
1100 ;; When returned name would overwrite original file, add EXTENSION
1101 ;; another time.
1102 (should
1103 (equal "normal.org.org"
1104 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
1105 (org-export-output-file-name ".org")))))
1107 (ert-deftest test-org-export/expand-include ()
1108 "Test file inclusion in an Org buffer."
1109 ;; Error when file isn't specified.
1110 (should-error
1111 (org-test-with-temp-text "#+INCLUDE: dummy.org"
1112 (org-export-expand-include-keyword)))
1113 ;; Refuse to expand keywords in commented headings.
1114 (should
1115 (org-test-with-temp-text "* COMMENT H1\n#+INCLUDE: dummy.org"
1116 (org-export-expand-include-keyword)
1118 ;; Full insertion with recursive inclusion.
1119 (should
1120 (equal
1121 (with-temp-buffer
1122 (insert-file
1123 (expand-file-name "examples/include.org" org-test-dir))
1124 (replace-regexp-in-string
1125 (regexp-quote "#+INCLUDE: \"include2.org\"")
1126 "Success!" (buffer-string)))
1127 (org-test-with-temp-text
1128 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
1129 (org-export-expand-include-keyword)
1130 (buffer-string))))
1131 ;; Localized insertion.
1132 (org-test-with-temp-text
1133 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
1134 org-test-dir)
1135 (org-export-expand-include-keyword)
1136 (should (equal (buffer-string)
1137 "Small Org file with an include keyword.\n")))
1138 ;; Insertion with constraints on headlines level.
1139 (should
1140 (equal
1141 "* Top heading\n** Heading\nbody\n"
1142 (org-test-with-temp-text
1143 (format
1144 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\""
1145 org-test-dir)
1146 (org-export-expand-include-keyword)
1147 (buffer-string))))
1148 (should
1149 (equal
1150 "* Top heading\n* Heading\nbody\n"
1151 (org-test-with-temp-text
1152 (format
1153 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\" :minlevel 1"
1154 org-test-dir)
1155 (org-export-expand-include-keyword)
1156 (buffer-string))))
1157 ;; Inclusion within an example block.
1158 (should
1159 (equal
1160 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
1161 (org-test-with-temp-text
1162 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
1163 org-test-dir)
1164 (org-export-expand-include-keyword)
1165 (buffer-string))))
1166 ;; Inclusion within a src-block.
1167 (should
1168 (equal
1169 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
1170 (org-test-with-temp-text
1171 (format
1172 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
1173 org-test-dir)
1174 (org-export-expand-include-keyword)
1175 (buffer-string))))
1176 ;; Inclusion within an html export-block.
1177 (should
1178 (equal
1179 "#+BEGIN_EXPORT html\n<p>HTML!</p>\n#+END_EXPORT\n"
1180 (org-test-with-temp-text
1181 (format
1182 "#+INCLUDE: \"%s/examples/include.html\" EXPORT html"
1183 org-test-dir)
1184 (org-export-expand-include-keyword)
1185 (buffer-string))))
1186 ;; Inclusion within an center paragraph
1187 (should
1188 (equal
1189 "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
1190 (org-test-with-temp-text
1191 (format
1192 "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
1193 org-test-dir)
1194 (org-export-expand-include-keyword)
1195 (buffer-string))))
1196 ;; Footnotes labels are local to each included file.
1197 (should
1198 (= 6
1199 (length
1200 (delete-dups
1201 (let ((contents "
1202 Footnotes[fn:1], [fn:test], [fn:test] and [fn:inline:inline footnote].
1203 \[fn:1] Footnote 1
1204 \[fn:test] Footnote \"test\""))
1205 (org-test-with-temp-text-in-file contents
1206 (let ((file1 (buffer-file-name)))
1207 (org-test-with-temp-text-in-file contents
1208 (let ((file2 (buffer-file-name)))
1209 (org-test-with-temp-text
1210 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
1211 file1 file2)
1212 (org-export-expand-include-keyword)
1213 (org-element-map (org-element-parse-buffer)
1214 'footnote-reference
1215 (lambda (r) (org-element-property :label r)))))))))))))
1216 ;; Footnotes labels are not local to each include keyword.
1217 (should
1218 (= 3
1219 (length
1220 (delete-dups
1221 (let ((contents "
1222 Footnotes[fn:1], [fn:test] and [fn:inline:inline footnote].
1223 \[fn:1] Footnote 1
1224 \[fn:test] Footnote \"test\""))
1225 (org-test-with-temp-text-in-file contents
1226 (let ((file (buffer-file-name)))
1227 (org-test-with-temp-text
1228 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
1229 (org-export-expand-include-keyword)
1230 (org-element-map (org-element-parse-buffer)
1231 'footnote-reference
1232 (lambda (ref) (org-element-property :label ref)))))))))))
1233 ;; Footnotes are supported by :lines-like elements and unnecessary
1234 ;; footnotes are dropped.
1235 (should
1236 (= 3
1237 (length
1238 (delete-dups
1239 (let ((contents "
1240 * foo
1241 Footnotes[fn:1]
1242 * bar
1243 Footnotes[fn:2], foot[fn:test] and [fn:inline:inline footnote]
1245 \[fn:1] Footnote 1
1246 \[fn:2] Footnote 1
1247 * Footnotes
1248 \[fn:test] Footnote \"test\"
1250 (org-test-with-temp-text-in-file contents
1251 (let ((file (buffer-file-name)))
1252 (org-test-with-temp-text
1253 (format "#+INCLUDE: \"%s::*bar\"\n" file)
1254 (org-export-expand-include-keyword)
1255 (org-element-map (org-element-parse-buffer)
1256 'footnote-definition
1257 (lambda (ref) (org-element-property :label ref)))))))))))
1258 ;; If only-contents is non-nil only include contents of element.
1259 (should
1260 (equal
1261 "body\n"
1262 (org-test-with-temp-text
1263 (concat
1264 (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" "
1265 org-test-dir)
1266 ":only-contents t")
1267 (org-export-expand-include-keyword)
1268 (buffer-string))))
1269 ;; Headings can be included via CUSTOM_ID.
1270 (should
1271 (org-test-with-temp-text
1272 (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
1273 (org-export-expand-include-keyword)
1274 (goto-char (point-min))
1275 (looking-at "* Another heading")))
1276 ;; Named objects can be included.
1277 (should
1278 (equal
1279 "| 1 |\n"
1280 (org-test-with-temp-text
1281 (format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t"
1282 org-test-dir)
1283 (org-export-expand-include-keyword)
1284 (buffer-string))))
1285 ;; Including non-existing elements should result in an error.
1286 (should-error
1287 (org-test-with-temp-text
1288 (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\""
1289 org-test-dir)
1290 (org-export-expand-include-keyword)))
1291 ;; Lines work relatively to an included element.
1292 (should
1293 (equal
1294 "2\n3\n"
1295 (org-test-with-temp-text
1296 (format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t \
1297 :lines \"2-3\""
1298 org-test-dir)
1299 (org-export-expand-include-keyword)
1300 (buffer-string))))
1301 ;; Properties should be dropped from headlines.
1302 (should
1303 (equal
1304 (org-test-with-temp-text
1305 (format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t"
1306 org-test-dir)
1307 (org-export-expand-include-keyword)
1308 (buffer-string))
1309 (org-test-with-temp-text
1310 (format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
1311 (org-export-expand-include-keyword)
1312 (buffer-string))))
1313 ;; Properties should be dropped, drawers should not be.
1314 (should
1315 (equal
1316 ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
1317 (org-test-with-temp-text
1318 (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t"
1319 org-test-dir)
1320 (org-export-expand-include-keyword)
1321 (buffer-string))))
1322 ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
1323 (should
1324 (cl-every (lambda (level) (zerop (1- level)))
1325 (org-test-with-temp-text
1326 (concat
1327 (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n"
1328 org-test-dir)
1329 (format "#+INCLUDE: \"%s/examples/include.org::*Heading\""
1330 org-test-dir))
1331 (org-export-expand-include-keyword)
1332 (org-element-map (org-element-parse-buffer) 'headline
1333 (lambda (head) (org-element-property :level head))))))
1334 ;; INCLUDE does not insert induced :minlevel for src-blocks.
1335 (should-not
1336 (equal
1337 (org-test-with-temp-text
1338 (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp"
1339 org-test-dir)
1340 (org-export-expand-include-keyword)
1341 (buffer-string))
1342 (org-test-with-temp-text
1343 (format
1344 "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1"
1345 org-test-dir)
1346 (org-export-expand-include-keyword)
1347 (buffer-string))))
1348 ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
1349 (should
1350 (org-test-with-temp-text-in-file
1351 (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\""
1352 org-test-dir)
1353 (narrow-to-region (point) (point-max))
1354 (org-export-expand-include-keyword)
1355 (eq 1 (org-current-level))))
1356 ;; If :minlevel is present do not alter it.
1357 (should
1358 (org-test-with-temp-text
1359 (format
1360 "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 3"
1361 org-test-dir)
1362 (narrow-to-region (point) (point-max))
1363 (org-export-expand-include-keyword)
1364 (eq 3 (org-current-level)))))
1366 (ert-deftest test-org/expand-include/links ()
1367 "Test links modifications when including files."
1368 ;; Preserve relative plain links.
1369 (should
1370 (string-prefix-p
1371 "file:org-includee-"
1372 (let* ((subdir (make-temp-file "org-includee-" t))
1373 (includee (expand-file-name "includee.org" subdir))
1374 (includer (make-temp-file "org-includer-")))
1375 (write-region "file:foo.org" nil includee)
1376 (write-region (format "#+INCLUDE: %S"
1377 (file-relative-name includee
1378 temporary-file-directory))
1379 nil includer)
1380 (let ((buffer (find-file-noselect includer t)))
1381 (unwind-protect
1382 (with-current-buffer buffer
1383 (org-export-expand-include-keyword)
1384 (org-trim (buffer-string)))
1385 (when (buffer-live-p buffer)
1386 (with-current-buffer buffer (set-buffer-modified-p nil))
1387 (kill-buffer buffer))
1388 (when (file-exists-p subdir) (delete-directory subdir t))
1389 (when (file-exists-p includer) (delete-file includer)))))))
1390 ;; Preserve relative angular links.
1391 (should
1392 (string-prefix-p
1393 "<file:org-includee-"
1394 (let* ((subdir (make-temp-file "org-includee-" t))
1395 (includee (expand-file-name "includee.org" subdir))
1396 (includer (make-temp-file "org-includer-")))
1397 (write-region "<file:foo.org>" nil includee)
1398 (write-region (format "#+INCLUDE: %S"
1399 (file-relative-name includee
1400 temporary-file-directory))
1401 nil includer)
1402 (let ((buffer (find-file-noselect includer t)))
1403 (unwind-protect
1404 (with-current-buffer buffer
1405 (org-export-expand-include-keyword)
1406 (org-trim (buffer-string)))
1407 (when (buffer-live-p buffer)
1408 (with-current-buffer buffer (set-buffer-modified-p nil))
1409 (kill-buffer buffer))
1410 (when (file-exists-p subdir) (delete-directory subdir t))
1411 (when (file-exists-p includer) (delete-file includer)))))))
1412 ;; Preserve relative bracket links without description.
1413 (should
1414 (string-prefix-p
1415 "[[file:org-includee-"
1416 (let* ((subdir (make-temp-file "org-includee-" t))
1417 (includee (expand-file-name "includee.org" subdir))
1418 (includer (make-temp-file "org-includer-")))
1419 (write-region "[[file:foo.org]]" nil includee)
1420 (write-region (format "#+INCLUDE: %S"
1421 (file-relative-name includee
1422 temporary-file-directory))
1423 nil includer)
1424 (let ((buffer (find-file-noselect includer t)))
1425 (unwind-protect
1426 (with-current-buffer buffer
1427 (org-export-expand-include-keyword)
1428 (org-trim (buffer-string)))
1429 (when (buffer-live-p buffer)
1430 (with-current-buffer buffer (set-buffer-modified-p nil))
1431 (kill-buffer buffer))
1432 (when (file-exists-p subdir) (delete-directory subdir t))
1433 (when (file-exists-p includer) (delete-file includer)))))))
1434 ;; Preserve relative bracket links with description.
1435 (should
1436 (string-prefix-p
1437 "[[file:org-includee-"
1438 (let* ((subdir (make-temp-file "org-includee-" t))
1439 (includee (expand-file-name "includee.org" subdir))
1440 (includer (make-temp-file "org-includer-")))
1441 (write-region "[[file:foo.org][description]]" nil includee)
1442 (write-region (format "#+INCLUDE: %S"
1443 (file-relative-name includee
1444 temporary-file-directory))
1445 nil includer)
1446 (let ((buffer (find-file-noselect includer t)))
1447 (unwind-protect
1448 (with-current-buffer buffer
1449 (org-export-expand-include-keyword)
1450 (org-trim (buffer-string)))
1451 (when (buffer-live-p buffer)
1452 (with-current-buffer buffer (set-buffer-modified-p nil))
1453 (kill-buffer buffer))
1454 (when (file-exists-p subdir) (delete-directory subdir t))
1455 (when (file-exists-p includer) (delete-file includer)))))))
1456 ;; Preserve absolute links.
1457 (should
1458 (string=
1459 "[[file:/foo/bar.org]]"
1460 (let* ((subdir (make-temp-file "org-includee-" t))
1461 (includee (expand-file-name "includee.org" subdir))
1462 (includer (make-temp-file "org-includer-")))
1463 (write-region "[[file:/foo/bar.org]]" nil includee)
1464 (write-region (format "#+INCLUDE: %S"
1465 (file-relative-name includee
1466 temporary-file-directory))
1467 nil includer)
1468 (let ((buffer (find-file-noselect includer t)))
1469 (unwind-protect
1470 (with-current-buffer buffer
1471 (org-export-expand-include-keyword)
1472 (org-trim (buffer-string)))
1473 (when (buffer-live-p buffer)
1474 (with-current-buffer buffer (set-buffer-modified-p nil))
1475 (kill-buffer buffer))
1476 (when (file-exists-p subdir) (delete-directory subdir t))
1477 (when (file-exists-p includer) (delete-file includer)))))))
1478 ;; Pathological case: Do not error when fixing a path in a headline.
1479 (should
1480 (let* ((subdir (make-temp-file "org-includee-" t))
1481 (includee (expand-file-name "includee.org" subdir))
1482 (includer (make-temp-file "org-includer-")))
1483 (write-region "* [[file:foo.org]]" nil includee)
1484 (write-region (format "#+INCLUDE: %S"
1485 (file-relative-name includee
1486 temporary-file-directory))
1487 nil includer)
1488 (let ((buffer (find-file-noselect includer t)))
1489 (unwind-protect
1490 (with-current-buffer buffer
1491 (org-export-expand-include-keyword)
1492 (org-trim (buffer-string)))
1493 (when (buffer-live-p buffer)
1494 (with-current-buffer buffer (set-buffer-modified-p nil))
1495 (kill-buffer buffer))
1496 (when (file-exists-p subdir) (delete-directory subdir t))
1497 (when (file-exists-p includer) (delete-file includer)))))))
1499 (ert-deftest test-org-export/expand-macro ()
1500 "Test macro expansion in an Org buffer."
1501 (require 'ox-org)
1502 ;; Standard macro expansion.
1503 (should
1504 (equal "#+macro: macro1 value\nvalue\n"
1505 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
1506 (org-export-as (org-test-default-backend)))))
1507 ;; Include global macros. However, local macros override them.
1508 (should
1509 (equal "global\n"
1510 (org-test-with-temp-text "{{{M}}}"
1511 (let ((org-export-global-macros '(("M" . "global"))))
1512 (org-export-as (org-test-default-backend))))))
1513 (should
1514 (equal "global arg\n"
1515 (org-test-with-temp-text "{{{M(arg)}}}"
1516 (let ((org-export-global-macros '(("M" . "global $1"))))
1517 (org-export-as (org-test-default-backend))))))
1518 (should
1519 (equal "2\n"
1520 (org-test-with-temp-text "{{{M}}}"
1521 (let ((org-export-global-macros '(("M" . "(eval (+ 1 1))"))))
1522 (org-export-as (org-test-default-backend))))))
1523 (should
1524 (equal "#+macro: M local\nlocal\n"
1525 (org-test-with-temp-text "#+macro: M local\n{{{M}}}"
1526 (let ((org-export-global-macros '(("M" . "global"))))
1527 (org-export-as (org-test-default-backend))))))
1528 ;; Allow macro in parsed keywords and associated properties.
1529 ;; Standard macro expansion.
1530 (should
1531 (string-match
1532 "#\\+k: value"
1533 (let ((backend (org-export-create-backend
1534 :parent 'org
1535 :options '((:k "K" nil nil parse)))))
1536 (org-test-with-temp-text "#+MACRO: macro value\n#+K: {{{macro}}}"
1537 (org-export-as backend)))))
1538 (should
1539 (string-match
1540 ":EXPORT_K: v"
1541 (let ((backend (org-export-create-backend
1542 :parent 'org
1543 :options '((:k "K" nil nil parse)))))
1544 (org-test-with-temp-text
1545 "#+macro: m v\n* H\n:PROPERTIES:\n:EXPORT_K: {{{m}}}\n:END:"
1546 (org-export-as backend nil nil nil '(:with-properties t))))))
1547 ;; Expand specific macros.
1548 (should
1549 (equal "me 2012-03-29 me@here Title\n"
1550 (org-test-with-temp-text
1552 #+TITLE: Title
1553 #+DATE: 2012-03-29
1554 #+AUTHOR: me
1555 #+EMAIL: me@here
1556 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
1557 (let ((output (org-export-as (org-test-default-backend))))
1558 (substring output (string-match ".*\n\\'" output))))))
1559 ;; Expand specific macros when property contained a regular macro
1560 ;; already.
1561 (should
1562 (equal "value\n"
1563 (org-test-with-temp-text "
1564 #+MACRO: macro1 value
1565 #+TITLE: {{{macro1}}}
1566 {{{title}}}"
1567 (let ((output (org-export-as (org-test-default-backend))))
1568 (substring output (string-match ".*\n\\'" output))))))
1569 ;; Expand macros with templates in included files.
1570 (should
1571 (equal "success\n"
1572 (org-test-with-temp-text
1573 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
1574 {{{included-macro}}}" org-test-dir)
1575 (let ((output (org-export-as (org-test-default-backend))))
1576 (substring output (string-match ".*\n\\'" output))))))
1577 ;; Date macro takes a optional formatting argument
1578 (should
1579 (equal "09-02-15\n"
1580 (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02-09>"
1581 (org-export-as (org-test-default-backend)))))
1582 ;; Only single timestamps are formatted
1583 (should
1584 (equal "<2015-02x-09>\n"
1585 (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02x-09>"
1586 (org-export-as (org-test-default-backend)))))
1587 ;; Throw an error when a macro definition is missing.
1588 (should-error
1589 (org-test-with-temp-text "{{{missing}}}"
1590 (org-export-as (org-test-default-backend))))
1591 ;; Inline source blocks generate {{{results}}} macros. Evaluate
1592 ;; those.
1593 (should
1594 (equal "=2=\n"
1595 (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
1596 (let ((org-export-use-babel t)
1597 (org-babel-inline-result-wrap "=%s="))
1598 (org-export-as (org-test-default-backend)))))))
1600 (ert-deftest test-org-export/before-processing-hook ()
1601 "Test `org-export-before-processing-hook'."
1602 (should
1603 (equal
1604 "#+macro: mac val\nTest\n"
1605 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
1606 (let ((org-export-before-processing-hook
1607 '((lambda (backend)
1608 (while (re-search-forward "{{{" nil t)
1609 (let ((object (org-element-context)))
1610 (when (eq (org-element-type object) 'macro)
1611 (delete-region
1612 (org-element-property :begin object)
1613 (org-element-property :end object)))))))))
1614 (org-export-as (org-test-default-backend)))))))
1616 (ert-deftest test-org-export/before-parsing-hook ()
1617 "Test `org-export-before-parsing-hook'."
1618 (should
1619 (equal "Body 1\nBody 2\n"
1620 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
1621 (let ((org-export-before-parsing-hook
1622 '((lambda (backend)
1623 (goto-char (point-min))
1624 (while (re-search-forward org-outline-regexp-bol nil t)
1625 (delete-region
1626 (point-at-bol) (progn (forward-line) (point))))))))
1627 (org-export-as (org-test-default-backend)))))))
1631 ;;; Affiliated Keywords
1633 (ert-deftest test-org-export/read-attribute ()
1634 "Test `org-export-read-attribute' specifications."
1635 ;; Standard test.
1636 (should
1637 (equal
1638 (org-export-read-attribute
1639 :attr_html
1640 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
1641 (org-element-at-point)))
1642 '(:a "1" :b "2")))
1643 ;; Return nil on empty attribute.
1644 (should-not
1645 (org-export-read-attribute
1646 :attr_html
1647 (org-test-with-temp-text "Paragraph" (org-element-at-point))))
1648 ;; Return nil on "nil" string.
1649 (should
1650 (equal '(:a nil :b nil)
1651 (org-export-read-attribute
1652 :attr_html
1653 (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
1654 (org-element-at-point)))))
1655 ;; Return nil on empty string.
1656 (should
1657 (equal '(:a nil :b nil)
1658 (org-export-read-attribute
1659 :attr_html
1660 (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
1661 (org-element-at-point)))))
1662 ;; Return empty string when value is "".
1663 (should
1664 (equal '(:a "")
1665 (org-export-read-attribute
1666 :attr_html
1667 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
1668 (org-element-at-point)))))
1669 ;; Return \"\" when value is """".
1670 (should
1671 (equal '(:a "\"\"")
1672 (org-export-read-attribute
1673 :attr_html
1674 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
1675 (org-element-at-point)))))
1676 ;; Ignore text before first property.
1677 (should-not
1678 (member "ignore"
1679 (org-export-read-attribute
1680 :attr_html
1681 (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
1682 (org-element-at-point))))))
1684 (ert-deftest test-org-export/get-caption ()
1685 "Test `org-export-get-caption' specifications."
1686 ;; Without optional argument, return long caption
1687 (should
1688 (equal
1689 '("l")
1690 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1691 (org-export-get-caption (org-element-at-point)))))
1692 ;; With optional argument, return short caption.
1693 (should
1694 (equal
1695 '("s")
1696 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1697 (org-export-get-caption (org-element-at-point) t))))
1698 ;; Multiple lines are separated by white spaces.
1699 (should
1700 (equal
1701 '("a" " " "b")
1702 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
1703 (org-export-get-caption (org-element-at-point))))))
1707 ;;; Back-End Tools
1709 (ert-deftest test-org-export/define-backend ()
1710 "Test back-end definition and accessors."
1711 ;; Translate table.
1712 (should
1713 (equal '((headline . my-headline-test))
1714 (let (org-export-registered-backends)
1715 (org-export-define-backend 'test '((headline . my-headline-test)))
1716 (org-export-get-all-transcoders 'test))))
1717 ;; Filters.
1718 (should
1719 (equal '((:filter-headline . my-filter))
1720 (let (org-export-registered-backends)
1721 (org-export-define-backend 'test
1722 '((headline . my-headline-test))
1723 :filters-alist '((:filter-headline . my-filter)))
1724 (org-export-backend-filters (org-export-get-backend 'test)))))
1725 ;; Options.
1726 (should
1727 (equal '((:prop value))
1728 (let (org-export-registered-backends)
1729 (org-export-define-backend 'test
1730 '((headline . my-headline-test))
1731 :options-alist '((:prop value)))
1732 (org-export-backend-options (org-export-get-backend 'test)))))
1733 ;; Menu.
1734 (should
1735 (equal '(?k "Test Export" test)
1736 (let (org-export-registered-backends)
1737 (org-export-define-backend 'test
1738 '((headline . my-headline-test))
1739 :menu-entry '(?k "Test Export" test))
1740 (org-export-backend-menu (org-export-get-backend 'test))))))
1742 (ert-deftest test-org-export/define-derived-backend ()
1743 "Test `org-export-define-derived-backend' specifications."
1744 ;; Error when parent back-end is not defined.
1745 (should-error
1746 (let (org-export-registered-backends)
1747 (org-export-define-derived-backend 'test 'parent)))
1748 ;; Append translation table to parent's.
1749 (should
1750 (equal '((:headline . test) (:headline . parent))
1751 (let (org-export-registered-backends)
1752 (org-export-define-backend 'parent '((:headline . parent)))
1753 (org-export-define-derived-backend 'test 'parent
1754 :translate-alist '((:headline . test)))
1755 (org-export-get-all-transcoders 'test))))
1756 ;; Options defined in the new back have priority over those defined
1757 ;; in parent.
1758 (should
1759 (eq 'test
1760 (let (org-export-registered-backends)
1761 (org-export-define-backend 'parent
1762 '((:headline . parent))
1763 :options-alist '((:a nil nil 'parent)))
1764 (org-export-define-derived-backend 'test 'parent
1765 :options-alist '((:a nil nil 'test)))
1766 (plist-get (org-export--get-global-options
1767 (org-export-get-backend 'test))
1768 :a)))))
1770 (ert-deftest test-org-export/derived-backend-p ()
1771 "Test `org-export-derived-backend-p' specifications."
1772 ;; Non-nil with direct match.
1773 (should
1774 (let (org-export-registered-backends)
1775 (org-export-define-backend 'test '((headline . test)))
1776 (org-export-derived-backend-p 'test 'test)))
1777 (should
1778 (let (org-export-registered-backends)
1779 (org-export-define-backend 'test '((headline . test)))
1780 (org-export-define-derived-backend 'test2 'test)
1781 (org-export-derived-backend-p 'test2 'test2)))
1782 ;; Non-nil with a direct parent.
1783 (should
1784 (let (org-export-registered-backends)
1785 (org-export-define-backend 'test '((headline . test)))
1786 (org-export-define-derived-backend 'test2 'test)
1787 (org-export-derived-backend-p 'test2 'test)))
1788 ;; Non-nil with an indirect parent.
1789 (should
1790 (let (org-export-registered-backends)
1791 (org-export-define-backend 'test '((headline . test)))
1792 (org-export-define-derived-backend 'test2 'test)
1793 (org-export-define-derived-backend 'test3 'test2)
1794 (org-export-derived-backend-p 'test3 'test)))
1795 ;; Nil otherwise.
1796 (should-not
1797 (let (org-export-registered-backends)
1798 (org-export-define-backend 'test '((headline . test)))
1799 (org-export-define-backend 'test2 '((headline . test2)))
1800 (org-export-derived-backend-p 'test2 'test)))
1801 (should-not
1802 (let (org-export-registered-backends)
1803 (org-export-define-backend 'test '((headline . test)))
1804 (org-export-define-backend 'test2 '((headline . test2)))
1805 (org-export-define-derived-backend 'test3 'test2)
1806 (org-export-derived-backend-p 'test3 'test))))
1808 (ert-deftest test-org-export/get-all-transcoders ()
1809 "Test `org-export-get-all-transcoders' specifications."
1810 ;; Return nil when back-end cannot be found.
1811 (should-not (org-export-get-all-transcoders nil))
1812 ;; Same as `org-export-transcoders' if no parent.
1813 (should
1814 (equal '((headline . ignore))
1815 (org-export-get-all-transcoders
1816 (org-export-create-backend
1817 :transcoders '((headline . ignore))))))
1818 ;; But inherit from all ancestors whenever possible.
1819 (should
1820 (equal '((section . ignore) (headline . ignore))
1821 (let (org-export-registered-backends)
1822 (org-export-define-backend 'b1 '((headline . ignore)))
1823 (org-export-get-all-transcoders
1824 (org-export-create-backend
1825 :parent 'b1 :transcoders '((section . ignore)))))))
1826 (should
1827 (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
1828 (let (org-export-registered-backends)
1829 (org-export-define-backend 'b1 '((headline . ignore)))
1830 (org-export-define-derived-backend 'b2 'b1
1831 :translate-alist '((section . ignore)))
1832 (org-export-get-all-transcoders
1833 (org-export-create-backend
1834 :parent 'b2 :transcoders '((paragraph . ignore)))))))
1835 ;; Back-end transcoders overrule inherited ones.
1836 (should
1837 (eq 'b
1838 (let (org-export-registered-backends)
1839 (org-export-define-backend 'b1 '((headline . a)))
1840 (cdr (assq 'headline
1841 (org-export-get-all-transcoders
1842 (org-export-create-backend
1843 :parent 'b1 :transcoders '((headline . b))))))))))
1845 (ert-deftest test-org-export/get-all-options ()
1846 "Test `org-export-get-all-options' specifications."
1847 ;; Return nil when back-end cannot be found.
1848 (should-not (org-export-get-all-options nil))
1849 ;; Same as `org-export-options' if no parent.
1850 (should
1851 (equal '((headline . ignore))
1852 (org-export-get-all-options
1853 (org-export-create-backend
1854 :options '((headline . ignore))))))
1855 ;; But inherit from all ancestors whenever possible.
1856 (should
1857 (equal '((:key2 value2) (:key1 value1))
1858 (let (org-export-registered-backends)
1859 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1860 (org-export-get-all-options
1861 (org-export-create-backend
1862 :parent 'b1 :options '((:key2 value2)))))))
1863 (should
1864 (equal '((:key3 value3) (:key2 value2) (:key1 value1))
1865 (let (org-export-registered-backends)
1866 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1867 (org-export-define-derived-backend 'b2 'b1
1868 :options-alist '((:key2 value2)))
1869 (org-export-get-all-options
1870 (org-export-create-backend
1871 :parent 'b2 :options '((:key3 value3)))))))
1872 ;; Back-end options overrule inherited ones.
1873 (should
1874 (eq 'b
1875 (let (org-export-registered-backends)
1876 (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
1877 (cdr (assq :key1
1878 (org-export-get-all-options
1879 (org-export-create-backend
1880 :parent 'b1 :options '((:key1 . b))))))))))
1882 (ert-deftest test-org-export/get-all-filters ()
1883 "Test `org-export-get-all-filters' specifications."
1884 ;; Return nil when back-end cannot be found.
1885 (should-not (org-export-get-all-filters nil))
1886 ;; Same as `org-export-filters' if no parent.
1887 (should
1888 (equal '((:filter-headline . ignore))
1889 (org-export-get-all-filters
1890 (org-export-create-backend
1891 :filters '((:filter-headline . ignore))))))
1892 ;; But inherit from all ancestors whenever possible.
1893 (should
1894 (equal '((:filter-section . ignore) (:filter-headline . ignore))
1895 (let (org-export-registered-backends)
1896 (org-export-define-backend 'b1
1897 nil :filters-alist '((:filter-headline . ignore)))
1898 (org-export-get-all-filters
1899 (org-export-create-backend
1900 :parent 'b1 :filters '((:filter-section . ignore)))))))
1901 (should
1902 (equal '((:filter-paragraph . ignore)
1903 (:filter-section . ignore)
1904 (:filter-headline . ignore))
1905 (let (org-export-registered-backends)
1906 (org-export-define-backend 'b1
1907 nil :filters-alist '((:filter-headline . ignore)))
1908 (org-export-define-derived-backend 'b2 'b1
1909 :filters-alist '((:filter-section . ignore)))
1910 (org-export-get-all-filters
1911 (org-export-create-backend
1912 :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
1913 ;; Back-end filters overrule inherited ones.
1914 (should
1915 (eq 'b
1916 (let (org-export-registered-backends)
1917 (org-export-define-backend 'b1 '((:filter-headline . a)))
1918 (cdr (assq :filter-headline
1919 (org-export-get-all-filters
1920 (org-export-create-backend
1921 :parent 'b1 :filters '((:filter-headline . b))))))))))
1923 (ert-deftest test-org-export/with-backend ()
1924 "Test `org-export-with-backend' definition."
1925 ;; Error when calling an undefined back-end
1926 (should-error (org-export-with-backend nil "Test"))
1927 ;; Error when called back-end doesn't have an appropriate
1928 ;; transcoder.
1929 (should-error
1930 (org-export-with-backend
1931 (org-export-create-backend :transcoders '((headline . ignore)))
1932 "Test"))
1933 ;; Otherwise, export using correct transcoder
1934 (should
1935 (equal "Success"
1936 (let (org-export-registered-backends)
1937 (org-export-define-backend 'test
1938 '((verbatim . (lambda (text contents info) "Failure"))))
1939 (org-export-define-backend 'test2
1940 '((verbatim . (lambda (text contents info) "Success"))))
1941 (org-export-with-backend 'test2 '(verbatim (:value "=Test="))))))
1942 ;; Corner case: plain-text transcoders have a different arity.
1943 (should
1944 (equal "Success"
1945 (org-export-with-backend
1946 (org-export-create-backend
1947 :transcoders '((plain-text . (lambda (text info) "Success"))))
1948 "Test")))
1949 ;; Provide correct back-end if transcoder needs to use recursive
1950 ;; calls.
1951 (should
1952 (equal "Success\n"
1953 (let ((test-back-end
1954 (org-export-create-backend
1955 :transcoders
1956 (list (cons 'headline
1957 (lambda (headline contents info)
1958 (org-export-data
1959 (org-element-property :title headline)
1960 info)))
1961 (cons 'plain-text (lambda (text info) "Success"))))))
1962 (org-export-string-as
1963 "* Test"
1964 (org-export-create-backend
1965 :transcoders
1966 (list (cons 'headline
1967 (lambda (headline contents info)
1968 (org-export-with-backend
1969 test-back-end headline contents info))))))))))
1971 (ert-deftest test-org-export/data-with-backend ()
1972 "Test `org-export-data-with-backend' specifications."
1973 ;; Error when calling an undefined back-end.
1974 (should-error (org-export-data-with-backend nil "nil" nil))
1975 ;; Otherwise, export data recursively, using correct back-end.
1976 (should
1977 (equal
1978 "Success!"
1979 (org-export-data-with-backend
1980 '(bold nil "Test")
1981 (org-export-create-backend
1982 :transcoders
1983 '((plain-text . (lambda (text info) "Success"))
1984 (bold . (lambda (bold contents info) (concat contents "!")))))
1985 '(:with-emphasize t)))))
1989 ;;; Comments
1991 (ert-deftest test-org-export/comments ()
1992 "Test comments handling during export.
1993 In particular, structure of the document mustn't be altered after
1994 comments removal."
1995 (should
1996 (equal "Para1\n\nPara2\n"
1997 (org-test-with-temp-text "
1998 Para1
1999 # Comment
2001 # Comment
2002 Para2"
2003 (org-export-as (org-test-default-backend)))))
2004 (should
2005 (equal "Para1\n\nPara2\n"
2006 (org-test-with-temp-text "
2007 Para1
2008 # Comment
2009 Para2"
2010 (org-export-as (org-test-default-backend)))))
2011 (should
2012 (equal "[fn:1] Para1\n\n\nPara2\n"
2013 (org-test-with-temp-text "
2014 \[fn:1] Para1
2015 # Inside definition
2018 # Outside definition
2019 Para2"
2020 (org-export-as (org-test-default-backend)))))
2021 (should
2022 (equal "[fn:1] Para1\n\nPara2\n"
2023 (org-test-with-temp-text "
2024 \[fn:1] Para1
2026 # Inside definition
2028 # Inside definition
2030 Para2"
2031 (org-export-as (org-test-default-backend)))))
2032 (should
2033 (equal "[fn:1] Para1\n\nPara2\n"
2034 (org-test-with-temp-text "
2035 \[fn:1] Para1
2036 # Inside definition
2038 Para2"
2039 (org-export-as (org-test-default-backend)))))
2040 (should
2041 (equal "[fn:1] Para1\n\nPara2\n"
2042 (org-test-with-temp-text "
2043 \[fn:1] Para1
2045 # Inside definition
2046 Para2"
2047 (org-export-as (org-test-default-backend)))))
2048 (should
2049 (equal "- item 1\n\n- item 2\n"
2050 (org-test-with-temp-text "
2051 - item 1
2053 # Comment
2055 - item 2"
2056 (org-export-as (org-test-default-backend))))))
2060 ;;; Export blocks
2062 (ert-deftest test-org-export/export-block ()
2063 "Test export blocks transcoding."
2064 (should
2065 (equal "Success!\n"
2066 (org-test-with-temp-text
2067 "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
2068 (org-export-as
2069 (org-export-create-backend
2070 :transcoders '((export-block . (lambda (b _c _i)
2071 (org-element-property :value b)))
2072 (section . (lambda (_s c _i) c))))))))
2073 (should
2074 (equal "Success!\n"
2075 (org-test-with-temp-text
2076 "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
2077 (org-export-as
2078 (org-export-create-backend
2079 :transcoders
2080 (list
2081 (cons 'export-block
2082 (lambda (b _c _i)
2083 (and (equal (org-element-property :type b) "BACKEND")
2084 (org-element-property :value b))))
2085 (cons 'section (lambda (_s c _i) c)))))))))
2088 ;;; Export Snippets
2090 (ert-deftest test-org-export/export-snippet ()
2091 "Test export snippets transcoding."
2092 ;; Standard test.
2093 (org-test-with-temp-text "@@test:A@@@@t:B@@"
2094 (let ((backend (org-test-default-backend)))
2095 (setf (org-export-backend-name backend) 'test)
2096 (setf (org-export-backend-transcoders backend)
2097 (cons (cons 'export-snippet
2098 (lambda (snippet contents info)
2099 (when (eq (org-export-snippet-backend snippet) 'test)
2100 (org-element-property :value snippet))))
2101 (org-export-backend-transcoders backend)))
2102 (let ((org-export-snippet-translation-alist nil))
2103 (should (equal (org-export-as backend) "A\n")))
2104 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
2105 (should (equal (org-export-as backend) "AB\n")))))
2106 ;; Ignored export snippets do not remove any blank.
2107 (should
2108 (equal "begin end\n"
2109 (org-test-with-parsed-data "begin @@test:A@@ end"
2110 (org-export-data-with-backend
2111 tree
2112 (org-export-create-backend
2113 :transcoders
2114 '((paragraph . (lambda (paragraph contents info) contents))
2115 (section . (lambda (section contents info) contents))))
2116 info)))))
2119 ;;; Filters
2121 (ert-deftest test-org-export/filter-apply-functions ()
2122 "Test `org-export-filter-apply-functions' specifications."
2123 ;; Functions are applied in order and return values are reduced.
2124 (should
2125 (equal "210"
2126 (org-export-filter-apply-functions
2127 (list (lambda (value &rest _) (concat "1" value))
2128 (lambda (value &rest _) (concat "2" value)))
2129 "0" nil)))
2130 ;; Functions returning nil are skipped.
2131 (should
2132 (equal "20"
2133 (org-export-filter-apply-functions
2134 (list #'ignore (lambda (value &rest _) (concat "2" value)))
2135 "0" nil)))
2136 ;; If all functions are skipped, return the initial value.
2137 (should
2138 (equal "0"
2139 (org-export-filter-apply-functions (list #'ignore) "0" nil)))
2140 ;; If any function returns the empty string, final value is the
2141 ;; empty string.
2142 (should
2143 (equal ""
2144 (org-export-filter-apply-functions
2145 (list (lambda (value &rest _) "")
2146 (lambda (value &rest _) (concat "2" value)))
2147 "0" nil)))
2148 ;; Any function returning the empty string short-circuits the
2149 ;; process.
2150 (should
2151 (org-export-filter-apply-functions
2152 (list (lambda (value &rest _) "")
2153 (lambda (value &rest _) (error "This shouldn't happen")))
2154 "0" nil)))
2157 ;;; Footnotes
2159 (ert-deftest test-org-export/footnote-first-reference-p ()
2160 "Test `org-export-footnote-first-reference-p' specifications."
2161 (should
2162 (equal
2163 '(t nil)
2164 (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
2165 (let (result)
2166 (org-export-as
2167 (org-export-create-backend
2168 :transcoders
2169 `(,(cons 'footnote-reference
2170 (lambda (f c i)
2171 (push (org-export-footnote-first-reference-p f i)
2172 result)
2173 ""))
2174 (section . (lambda (s c i) c))
2175 (paragraph . (lambda (p c i) c))))
2176 nil nil nil '(:with-footnotes t))
2177 (nreverse result)))))
2178 ;; Limit check to DATA, when non-nil.
2179 (should
2180 (equal
2181 '(nil t)
2182 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
2183 (let (result)
2184 (org-element-map tree 'footnote-reference
2185 (lambda (ref)
2186 (push
2187 (org-export-footnote-first-reference-p
2188 ref info (org-element-map tree 'headline #'identity info t))
2189 result))
2190 info)
2191 (nreverse result)))))
2192 (should
2193 (equal
2194 '(t nil)
2195 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
2196 (let (result)
2197 (org-element-map tree 'footnote-reference
2198 (lambda (ref)
2199 (push (org-export-footnote-first-reference-p ref info) result))
2200 info)
2201 (nreverse result)))))
2202 ;; If optional argument BODY-FIRST is non-nil, first find footnote
2203 ;; in the main body of the document. Otherwise, enter footnote
2204 ;; definitions when they are encountered.
2205 (should
2206 (equal
2207 '(t nil)
2208 (org-test-with-temp-text
2209 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
2210 (let (result)
2211 (org-export-as
2212 (org-export-create-backend
2213 :transcoders
2214 `(,(cons 'footnote-reference
2215 (lambda (f c i)
2216 (when (org-element-lineage f '(drawer))
2217 (push (org-export-footnote-first-reference-p f i nil)
2218 result))
2219 ""))
2220 (drawer . (lambda (d c i) c))
2221 (footnote-definition . (lambda (d c i) c))
2222 (section . (lambda (s c i) c))
2223 (paragraph . (lambda (p c i) c))))
2224 nil nil nil '(:with-footnotes t))
2225 (nreverse result)))))
2226 (should
2227 (equal
2228 '(t t)
2229 (org-test-with-temp-text
2230 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
2231 (let (result)
2232 (org-export-as
2233 (org-export-create-backend
2234 :transcoders
2235 `(,(cons 'footnote-reference
2236 (lambda (f c i)
2237 (when (org-element-lineage f '(drawer))
2238 (push (org-export-footnote-first-reference-p f i nil t)
2239 result))
2240 ""))
2241 (drawer . (lambda (d c i) c))
2242 (footnote-definition . (lambda (d c i) c))
2243 (section . (lambda (s c i) c))
2244 (paragraph . (lambda (p c i) c))))
2245 nil nil nil '(:with-footnotes t))
2246 (nreverse result))))))
2248 (ert-deftest test-org-export/get-footnote-number ()
2249 "Test `org-export-get-footnote-number' specifications."
2250 (should
2251 (equal '(1 2 1)
2252 (org-test-with-parsed-data
2253 "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
2254 (org-element-map tree 'footnote-reference
2255 (lambda (ref) (org-export-get-footnote-number ref info))
2256 info))))
2257 ;; Anonymous footnotes all get a new number.
2258 (should
2259 (equal '(1 2)
2260 (org-test-with-parsed-data
2261 "Text[fn::anon1][fn::anon2]"
2262 (org-element-map tree 'footnote-reference
2263 (lambda (ref) (org-export-get-footnote-number ref info))
2264 info))))
2265 ;; Test nested footnotes order.
2266 (should
2267 (equal
2268 '((1 . "1") (2 . "2") (3 . "3") (3 . "3") (4))
2269 (org-test-with-parsed-data
2270 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
2271 (org-element-map tree 'footnote-reference
2272 (lambda (ref)
2273 (cons (org-export-get-footnote-number ref info)
2274 (org-element-property :label ref)))
2275 info))))
2276 ;; Limit number to provided DATA, when non-nil.
2277 (should
2278 (equal
2279 '(1)
2280 (org-test-with-parsed-data
2281 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2282 (org-element-map tree 'footnote-reference
2283 (lambda (ref)
2284 (org-export-get-footnote-number
2285 ref info (org-element-map tree 'headline #'identity info t)))
2286 info))))
2287 (should
2288 (equal
2289 '(1 2)
2290 (org-test-with-parsed-data
2291 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
2292 (org-element-map tree 'footnote-reference
2293 (lambda (ref) (org-export-get-footnote-number ref info))
2294 info))))
2295 ;; With a non-nil BODY-FIRST optional argument, first check body,
2296 ;; then footnote definitions.
2297 (should
2298 (equal
2299 '(("1" . 1) ("2" . 2) ("3" . 3) ("3" . 3))
2300 (org-test-with-parsed-data
2301 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
2302 (org-element-map tree 'footnote-reference
2303 (lambda (ref)
2304 (cons (org-element-property :label ref)
2305 (org-export-get-footnote-number ref info nil t)))
2306 info))))
2307 (should
2308 (equal
2309 '(("1" . 1) ("2" . 3) ("3" . 2) ("3" . 2))
2310 (org-test-with-parsed-data
2311 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
2312 (org-element-map tree 'footnote-reference
2313 (lambda (ref)
2314 (cons (org-element-property :label ref)
2315 (org-export-get-footnote-number ref info nil)))
2316 info)))))
2318 (ert-deftest test-org-export/get-footnote-definition ()
2319 "Test `org-export-get-footnote-definition' specifications."
2320 ;; Standard test.
2321 (should
2322 (equal "A\n"
2323 (org-element-interpret-data
2324 (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1] A"
2325 (org-export-get-footnote-definition
2326 (org-element-map tree 'footnote-reference #'identity nil t)
2327 info)))))
2328 ;; Raise an error if no definition is found.
2329 (should-error
2330 (org-test-with-parsed-data "Text[fn:1]"
2331 (org-export-get-footnote-definition
2332 (org-element-map tree 'footnote-reference #'identity nil t)
2333 info)))
2334 ;; Find inline definitions.
2335 (should
2336 (equal "A"
2337 (org-element-interpret-data
2338 (org-test-with-parsed-data "Text[fn:1:A]"
2339 (org-export-get-footnote-definition
2340 (org-element-map tree 'footnote-reference #'identity nil t)
2341 info)))))
2342 ;; Find anonymous definitions.
2343 (should
2344 (equal "A"
2345 (org-element-interpret-data
2346 (org-test-with-parsed-data "Text[fn::A]"
2347 (org-export-get-footnote-definition
2348 (org-element-map tree 'footnote-reference #'identity nil t)
2349 info)))))
2350 ;; Find empty definitions.
2351 (should
2352 (equal ""
2353 (org-element-interpret-data
2354 (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1]"
2355 (org-export-get-footnote-definition
2356 (org-element-map tree 'footnote-reference #'identity nil t)
2357 info)))))
2358 (should
2359 (equal ""
2360 (org-element-interpret-data
2361 (org-test-with-parsed-data "Text[fn:1:]"
2362 (org-export-get-footnote-definition
2363 (org-element-map tree 'footnote-reference #'identity nil t)
2364 info)))))
2365 (should
2366 (equal ""
2367 (org-element-interpret-data
2368 (org-test-with-parsed-data "Text[fn::]"
2369 (org-export-get-footnote-definition
2370 (org-element-map tree 'footnote-reference #'identity nil t)
2371 info))))))
2373 (ert-deftest test-org-export/collect-footnote-definitions ()
2374 "Test `org-export-collect-footnote-definitions' specifications."
2375 (should
2376 (= 4
2377 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2379 \[fn:2] B [fn:3] [fn::D].
2381 \[fn:3] C."
2382 (length (org-export-collect-footnote-definitions info)))))
2383 ;; Limit number to provided DATA, when non-nil.
2384 (should
2385 (equal
2386 '((1 "2"))
2387 (org-test-with-parsed-data
2388 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2389 (mapcar #'butlast
2390 (org-export-collect-footnote-definitions
2391 info (org-element-map tree 'headline #'identity info t))))))
2392 (should
2393 (equal
2394 '((1 "1") (2 "2"))
2395 (org-test-with-parsed-data
2396 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2397 (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
2398 ;; With optional argument BODY-FIRST, first check body, then
2399 ;; footnote definitions.
2400 (should
2401 (equal '("1" "3" "2" nil)
2402 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2404 \[fn:2] B [fn:3] [fn::D].
2406 \[fn:3] C."
2407 (mapcar (lambda (e) (nth 1 e))
2408 (org-export-collect-footnote-definitions info nil t)))))
2409 (should-not
2410 (equal '("1" "3" "2" nil)
2411 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2413 \[fn:2] B [fn:3] [fn::D].
2415 \[fn:3] C."
2416 (mapcar (lambda (e) (nth 1 e))
2417 (org-export-collect-footnote-definitions info))))))
2419 (ert-deftest test-org-export/footnotes ()
2420 "Miscellaneous tests on footnotes."
2421 (let ((org-footnote-section nil)
2422 (org-export-with-footnotes t))
2423 ;; Read every type of footnote.
2424 (should
2425 (equal
2426 '((1 . "A\n") (2 . "C") (3 . "D"))
2427 (org-test-with-parsed-data
2428 "Text[fn:1] [fn:label:C] [fn::D]\n\n[fn:1] A\n"
2429 (org-element-map tree 'footnote-reference
2430 (lambda (ref)
2431 (let ((def (org-export-get-footnote-definition ref info)))
2432 (cons (org-export-get-footnote-number ref info)
2433 (if (eq (org-element-property :type ref) 'inline) (car def)
2434 (car (org-element-contents
2435 (car (org-element-contents def))))))))
2436 info))))
2437 ;; Export nested footnote in invisible definitions.
2438 (should
2439 (= 2
2440 (org-test-with-temp-text "Text[fn:1]\n\n[fn:1] B [fn:2]\n\n[fn:2] C."
2441 (narrow-to-region (point) (line-end-position))
2442 (catch 'exit
2443 (org-export-as
2444 (org-export-create-backend
2445 :transcoders
2446 '((section
2448 (lambda (s c i)
2449 (throw 'exit (length
2450 (org-export-collect-footnote-definitions
2451 i))))))))))))
2452 ;; Export footnotes defined outside parsing scope.
2453 (should
2454 (string-match
2455 "Out of scope"
2456 (org-test-with-temp-text "[fn:1] Out of scope
2457 * Title
2458 <point>Paragraph[fn:1]"
2459 (org-export-as (org-test-default-backend) 'subtree))))
2460 (should
2461 (string-match
2462 "Out of scope"
2463 (org-test-with-temp-text "[fn:1] Out of scope
2464 * Title
2465 <point>Paragraph[fn:1]"
2466 (narrow-to-region (point) (point-max))
2467 (org-export-as (org-test-default-backend)))))
2468 ;; Export nested footnotes defined outside parsing scope.
2469 (should
2470 (string-match
2471 "Very out of scope"
2472 (org-test-with-temp-text "
2473 \[fn:1] Out of scope[fn:2]
2475 \[fn:2] Very out of scope
2476 * Title
2477 <point>Paragraph[fn:1]"
2478 (org-export-as (org-test-default-backend) 'subtree))))
2479 (should
2480 (string-match
2481 "Very out of scope"
2482 (org-test-with-temp-text "
2483 \[fn:1] Out of scope[fn:2]
2485 \[fn:2] Very out of scope
2486 * Title
2487 <point>Paragraph[fn:1]"
2488 (narrow-to-region (point) (point-max))
2489 (org-export-as (org-test-default-backend)))))
2490 (should
2491 (string-match
2492 "D2"
2493 (org-test-with-temp-text "
2494 \[fn:1] Out of scope[fn:2:D2]
2495 * Title
2496 <point>Paragraph[fn:1]"
2497 (narrow-to-region (point) (point-max))
2498 (org-export-as (org-test-default-backend)))))
2499 ;; Export footnotes in pruned parts of tree.
2500 (should
2501 (string-match
2502 "Definition"
2503 (let ((org-export-exclude-tags '("noexport")))
2504 (org-test-with-temp-text
2505 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] Definition"
2506 (org-export-as (org-test-default-backend))))))
2507 (should
2508 (string-match
2509 "Definition"
2510 (let ((org-export-select-tags '("export")))
2511 (org-test-with-temp-text
2512 "* H :export:\nText[fn:1]\n* H2\n[fn:1] Definition"
2513 (org-export-as (org-test-default-backend))))))
2514 ;; Export nested footnotes in pruned parts of tree.
2515 (should
2516 (string-match
2517 "D2"
2518 (let ((org-export-exclude-tags '("noexport")))
2519 (org-test-with-temp-text
2520 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
2521 (org-export-as (org-test-default-backend))))))
2522 (should
2523 (string-match
2524 "D2"
2525 (let ((org-export-select-tags '("export")))
2526 (org-test-with-temp-text
2527 "* H :export:\nText[fn:1]\n* H2\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
2528 (org-export-as (org-test-default-backend))))))
2529 ;; Handle uninterpreted data in pruned footnote definitions.
2530 (should-not
2531 (string-match
2533 (let ((org-export-with-tables nil))
2534 (org-test-with-temp-text
2535 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1]\n| a |"
2536 (org-export-as (org-test-default-backend))))))
2537 ;; Footnotes without a definition should throw an error.
2538 (should-error
2539 (org-test-with-parsed-data "Text[fn:1]"
2540 (org-export-get-footnote-definition
2541 (org-element-map tree 'footnote-reference #'identity info t) info)))
2542 ;; Footnote section should be ignored in TOC and in headlines
2543 ;; numbering.
2544 (should
2545 (= 1 (let ((org-footnote-section "Footnotes"))
2546 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
2547 (org-export-collect-headlines info))))))
2548 (should
2549 (equal '(2)
2550 (let ((org-footnote-section "Footnotes"))
2551 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
2552 (org-element-map tree 'headline
2553 (lambda (hl)
2554 (when (equal (org-element-property :raw-value hl) "H2")
2555 (org-export-get-headline-number hl info)))
2556 info t)))))))
2560 ;;; Headlines and Inlinetasks
2562 (ert-deftest test-org-export/get-relative-level ()
2563 "Test `org-export-get-relative-level' specifications."
2564 ;; Standard test.
2565 (should
2566 (equal '(1 2)
2567 (let ((org-odd-levels-only nil))
2568 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2569 (org-element-map tree 'headline
2570 (lambda (h) (org-export-get-relative-level h info))
2571 info)))))
2572 ;; Missing levels
2573 (should
2574 (equal '(1 3)
2575 (let ((org-odd-levels-only nil))
2576 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
2577 (org-element-map tree 'headline
2578 (lambda (h) (org-export-get-relative-level h info))
2579 info))))))
2581 (ert-deftest test-org-export/low-level-p ()
2582 "Test `org-export-low-level-p' specifications."
2583 (should
2584 (equal
2585 '(no yes)
2586 (let ((org-odd-levels-only nil))
2587 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2588 (org-element-map tree 'headline
2589 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
2590 (plist-put info :headline-levels 1)))))))
2592 (ert-deftest test-org-export/get-headline-number ()
2593 "Test `org-export-get-headline-number' specifications."
2594 ;; Standard test.
2595 (should
2596 (equal
2597 '((1) (1 1))
2598 (let ((org-odd-levels-only nil))
2599 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2600 (org-element-map tree 'headline
2601 (lambda (h) (org-export-get-headline-number h info))
2602 info)))))
2603 ;; Missing levels are replaced with 0.
2604 (should
2605 (equal
2606 '((1) (1 0 1))
2607 (let ((org-odd-levels-only nil))
2608 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
2609 (org-element-map tree 'headline
2610 (lambda (h) (org-export-get-headline-number h info))
2611 info))))))
2613 (ert-deftest test-org-export/numbered-headline-p ()
2614 "Test `org-export-numbered-headline-p' specifications."
2615 ;; If `:section-numbers' is nil, never number headlines.
2616 (should-not
2617 (org-test-with-parsed-data "* Headline"
2618 (org-element-map tree 'headline
2619 (lambda (h) (org-export-numbered-headline-p h info))
2620 (plist-put info :section-numbers nil))))
2621 ;; If `:section-numbers' is a number, only number headlines with
2622 ;; a level greater that it.
2623 (should
2624 (equal
2625 '(yes no)
2626 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2627 (org-element-map tree 'headline
2628 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
2629 (plist-put info :section-numbers 1)))))
2630 ;; Otherwise, headlines are always numbered.
2631 (should
2632 (org-test-with-parsed-data "* Headline"
2633 (org-element-map tree 'headline
2634 (lambda (h) (org-export-numbered-headline-p h info))
2635 (plist-put info :section-numbers t))))
2636 ;; With #+OPTIONS: num:nil all headlines are unnumbered.
2637 (should-not
2638 (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
2639 (org-export-numbered-headline-p
2640 (org-element-map tree 'headline 'identity info t)
2641 info)))
2642 ;; Headlines with a non-nil UNNUMBERED property are not numbered.
2643 (should-not
2644 (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
2645 (org-export-numbered-headline-p
2646 (org-element-map tree 'headline #'identity info t)
2647 info)))
2648 ;; UNNUMBERED is inherited.
2649 (should
2650 (equal '(unnumbered numbered unnumbered)
2651 (org-test-with-parsed-data
2652 "* H
2653 :PROPERTIES:
2654 :UNNUMBERED: t
2655 :END:
2656 ** H2
2657 :PROPERTIES:
2658 :UNNUMBERED: nil
2659 :END:
2660 ** H3"
2661 (org-element-map tree 'headline
2662 (lambda (h)
2663 (if (org-export-numbered-headline-p h info) 'numbered
2664 'unnumbered))
2665 info)))))
2667 (ert-deftest test-org-export/number-to-roman ()
2668 "Test `org-export-number-to-roman' specifications."
2669 ;; If number is negative, return it as a string.
2670 (should (equal (org-export-number-to-roman -1) "-1"))
2671 ;; Otherwise, return it as a roman number.
2672 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
2674 (ert-deftest test-org-export/get-optional-title ()
2675 "Test `org-export-get-alt-title' specifications."
2676 ;; If ALT_TITLE property is defined, use it.
2677 (should
2678 (equal '("opt")
2679 (org-test-with-parsed-data
2680 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
2681 (org-export-get-alt-title
2682 (org-element-map tree 'headline 'identity info t)
2683 info))))
2684 ;; Otherwise, fall-back to regular title.
2685 (should
2686 (equal '("Headline")
2687 (org-test-with-parsed-data "* Headline"
2688 (org-export-get-alt-title
2689 (org-element-map tree 'headline 'identity info t)
2690 info)))))
2692 (ert-deftest test-org-export/get-tags ()
2693 "Test `org-export-get-tags' specifications."
2694 ;; Standard test: tags which are not a select tag, an exclude tag,
2695 ;; or specified as optional argument shouldn't be ignored.
2696 (should
2697 (org-test-with-parsed-data "* Headline :tag:"
2698 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2699 info)))
2700 ;; Tags provided in the optional argument are ignored.
2701 (should-not
2702 (org-test-with-parsed-data "* Headline :ignore:"
2703 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2704 info '("ignore"))))
2705 ;; Allow tag inheritance.
2706 (should
2707 (equal
2708 '(("tag") ("tag"))
2709 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
2710 (org-element-map tree 'headline
2711 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
2712 ;; Tag inheritance checks FILETAGS keywords.
2713 (should
2714 (equal
2715 '(("a" "b" "tag"))
2716 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
2717 (org-element-map tree 'headline
2718 (lambda (hl) (org-export-get-tags hl info nil t)) info)))))
2720 (ert-deftest test-org-export/get-node-property ()
2721 "Test`org-export-get-node-property' specifications."
2722 ;; Standard test.
2723 (should
2724 (equal "value"
2725 (org-test-with-parsed-data "* Headline
2726 :PROPERTIES:
2727 :prop: value
2728 :END:"
2729 (org-export-get-node-property
2730 :PROP (org-element-map tree 'headline 'identity nil t)))))
2731 ;; Test inheritance.
2732 (should
2733 (equal "value"
2734 (org-test-with-parsed-data "* Parent
2735 :PROPERTIES:
2736 :prop: value
2737 :END:
2738 ** Headline
2739 Paragraph"
2740 (org-export-get-node-property
2741 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
2742 ;; Cannot return a value before the first headline.
2743 (should-not
2744 (org-test-with-parsed-data "Paragraph
2745 * Headline
2746 :PROPERTIES:
2747 :prop: value
2748 :END:"
2749 (org-export-get-node-property
2750 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
2752 (ert-deftest test-org-export/get-category ()
2753 "Test `org-export-get-category' specifications."
2754 ;; Standard test.
2755 (should
2756 (equal "value"
2757 (org-test-with-parsed-data "* Headline
2758 :PROPERTIES:
2759 :CATEGORY: value
2760 :END:"
2761 (org-export-get-category
2762 (org-element-map tree 'headline 'identity nil t) info))))
2763 ;; Test inheritance from a parent headline.
2764 (should
2765 (equal '("value" "value")
2766 (org-test-with-parsed-data "* Headline1
2767 :PROPERTIES:
2768 :CATEGORY: value
2769 :END:
2770 ** Headline2"
2771 (org-element-map tree 'headline
2772 (lambda (hl) (org-export-get-category hl info)) info))))
2773 ;; Test inheritance from #+CATEGORY keyword
2774 (should
2775 (equal "value"
2776 (org-test-with-parsed-data "#+CATEGORY: value
2777 * Headline"
2778 (org-export-get-category
2779 (org-element-map tree 'headline 'identity nil t) info))))
2780 ;; Test inheritance from file name.
2781 (should
2782 (equal "test"
2783 (org-test-with-parsed-data "* Headline"
2784 (let ((info (plist-put info :input-file "~/test.org")))
2785 (org-export-get-category
2786 (org-element-map tree 'headline 'identity nil t) info)))))
2787 ;; Fall-back value.
2788 (should
2789 (equal "???"
2790 (org-test-with-parsed-data "* Headline"
2791 (org-export-get-category
2792 (org-element-map tree 'headline 'identity nil t) info)))))
2794 (ert-deftest test-org-export/first-sibling-p ()
2795 "Test `org-export-first-sibling-p' specifications."
2796 ;; Standard test.
2797 (should
2798 (equal
2799 '(yes yes no)
2800 (org-test-with-parsed-data "* H\n** H 2\n** H 3"
2801 (org-element-map tree 'headline
2802 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2803 info))))
2804 (should
2805 (equal '(yes no)
2806 (org-test-with-parsed-data "- item\n\n para"
2807 (org-element-map tree 'paragraph
2808 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2809 info))))
2810 ;; Ignore sections for headlines.
2811 (should
2812 (equal '(yes yes)
2813 (org-test-with-parsed-data "* H\nSection\n** H 2"
2814 (org-element-map tree 'headline
2815 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2816 info))))
2817 ;; Ignore headlines not exported.
2818 (should
2819 (equal
2820 '(yes)
2821 (let ((org-export-exclude-tags '("ignore")))
2822 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
2823 (org-element-map tree 'headline
2824 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2825 info))))))
2827 (ert-deftest test-org-export/last-sibling-p ()
2828 "Test `org-export-last-sibling-p' specifications."
2829 ;; Standard test.
2830 (should
2831 (equal
2832 '(yes no yes)
2833 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
2834 (org-element-map tree 'headline
2835 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2836 info))))
2837 (should
2838 (equal '(no yes)
2839 (org-test-with-parsed-data "- item\n\n para"
2840 (org-element-map tree 'paragraph
2841 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2842 info))))
2843 ;; Ignore headlines not exported.
2844 (should
2845 (equal
2846 '(yes)
2847 (let ((org-export-exclude-tags '("ignore")))
2848 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
2849 (org-element-map tree 'headline
2850 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2851 info)))))
2852 ;; Handle gracefully discontinuous headings.
2853 (should
2854 (equal '(yes yes)
2855 (org-test-with-parsed-data "** S\n* H"
2856 (org-element-map tree 'headline
2857 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no)))))))
2859 (ert-deftest test-org-export/handle-inlinetasks ()
2860 "Test inlinetask export."
2861 ;; Inlinetask with an exclude tag.
2862 (when (featurep 'org-inlinetask)
2863 (should
2864 (equal
2866 (let ((org-inlinetask-min-level 3)
2867 org-export-filter-body-functions
2868 org-export-filter-final-output-functions)
2869 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
2870 (org-export-as (org-test-default-backend)
2871 nil nil nil '(:exclude-tags ("noexp")))))))
2872 ;; Inlinetask with an include tag.
2873 (should
2874 (equal
2875 "* H2\n*** Inline :exp:\n"
2876 (let ((org-inlinetask-min-level 3)
2877 (org-tags-column 0))
2878 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
2879 (org-export-as (org-test-default-backend)
2880 nil nil nil '(:select-tags ("exp")))))))
2881 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
2882 (should
2883 (equal ""
2884 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
2885 (org-inlinetask-min-level 3)
2886 org-export-filter-body-functions
2887 org-export-filter-final-output-functions)
2888 (org-test-with-temp-text "*** TODO Inline"
2889 (org-export-as (org-test-default-backend)
2890 nil nil nil '(:with-tasks nil))))))))
2894 ;;; Keywords
2896 (ert-deftest test-org-export/get-date ()
2897 "Test `org-export-get-date' specifications."
2898 ;; Return a properly formatted string when
2899 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
2900 ;; consists in a single timestamp.
2901 (should
2902 (equal "29 03 2012"
2903 (let ((org-export-date-timestamp-format "%d %m %Y"))
2904 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2905 (org-export-get-date info)))))
2906 ;; Return a secondary string otherwise.
2907 (should-not
2908 (stringp
2909 (let ((org-export-date-timestamp-format nil))
2910 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2911 (org-export-get-date info)))))
2912 (should
2913 (equal '("Date")
2914 (org-test-with-parsed-data "#+DATE: Date"
2915 (org-export-get-date info))))
2916 ;; Optional argument has precedence over
2917 ;; `org-export-date-timestamp-format'.
2918 (should
2919 (equal "29 03"
2920 (let ((org-export-date-timestamp-format "%d %m %Y"))
2921 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2922 (org-export-get-date info "%d %m"))))))
2926 ;;; Links
2928 (ert-deftest test-org-export/custom-protocol-maybe ()
2929 "Test `org-export-custom-protocol-maybe' specifications."
2930 (should
2931 (string-match
2932 "success"
2933 (progn
2934 (org-link-set-parameters "foo" :export (lambda (p d f) "success"))
2935 (org-export-string-as
2936 "[[foo:path]]"
2937 (org-export-create-backend
2938 :name 'test
2939 :transcoders
2940 '((section . (lambda (s c i) c))
2941 (paragraph . (lambda (p c i) c))
2942 (link . (lambda (l c i)
2943 (or (org-export-custom-protocol-maybe l c 'test)
2944 "failure")))))))))
2945 (should-not
2946 (string-match
2947 "success"
2948 (progn
2949 (org-link-set-parameters
2950 "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
2951 (org-export-string-as
2952 "[[foo:path]]"
2953 (org-export-create-backend
2954 :name 'no-test
2955 :transcoders
2956 '((section . (lambda (s c i) c))
2957 (paragraph . (lambda (p c i) c))
2958 (link . (lambda (l c i)
2959 (or (org-export-custom-protocol-maybe l c 'no-test)
2960 "failure")))))))))
2961 ;; Ignore anonymous back-ends.
2962 (should-not
2963 (string-match
2964 "success"
2965 (progn
2966 (org-link-set-parameters
2967 "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
2968 (org-export-string-as
2969 "[[foo:path]]"
2970 (org-export-create-backend
2971 :transcoders
2972 '((section . (lambda (s c i) c))
2973 (paragraph . (lambda (p c i) c))
2974 (link . (lambda (l c i)
2975 (or (org-export-custom-protocol-maybe l c nil)
2976 "failure"))))))))))
2978 (ert-deftest test-org-export/get-coderef-format ()
2979 "Test `org-export-get-coderef-format' specifications."
2980 ;; A link without description returns "%s"
2981 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
2982 "%s"))
2983 ;; Return "%s" when path is matched within description.
2984 (should (equal (org-export-get-coderef-format "path" "desc (path)")
2985 "desc %s"))
2986 ;; Otherwise return description.
2987 (should (equal (org-export-get-coderef-format "path" "desc")
2988 "desc")))
2990 (ert-deftest test-org-export/inline-image-p ()
2991 "Test `org-export-inline-image-p' specifications."
2992 (should
2993 (org-export-inline-image-p
2994 (org-test-with-temp-text "[[#id]]"
2995 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
2996 '(("custom-id" . "id")))))
2998 (ert-deftest test-org-export/insert-image-links ()
2999 "Test `org-export-insert-image-links' specifications."
3000 (should-not
3001 (member "file"
3002 (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
3003 (org-element-map tree 'link
3004 (lambda (l) (org-element-property :type l))))))
3005 (should
3006 (member "file"
3007 (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
3008 (org-element-map (org-export-insert-image-links tree info) 'link
3009 (lambda (l) (org-element-property :type l))))))
3010 ;; Properly set `:parent' property when replace contents with image
3011 ;; link.
3012 (should
3013 (memq 'link
3014 (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
3015 (org-element-map (org-export-insert-image-links tree info) 'link
3016 (lambda (l)
3017 (org-element-type (org-element-property :parent l)))))))
3018 ;; With optional argument RULES, recognize different links as
3019 ;; images.
3020 (should-not
3021 (member "file"
3022 (org-test-with-parsed-data "[[https://orgmode.org][file:image.xxx]]"
3023 (org-element-map (org-export-insert-image-links tree info) 'link
3024 (lambda (l) (org-element-property :type l))))))
3025 (should
3026 (member "file"
3027 (org-test-with-parsed-data "[[https://orgmode.org][file:image.xxx]]"
3028 (org-element-map
3029 (org-export-insert-image-links tree info '(("file" . "xxx")))
3030 'link
3031 (lambda (l) (org-element-property :type l)))))))
3033 (ert-deftest test-org-export/fuzzy-link ()
3034 "Test fuzzy links specifications."
3035 ;; Link to an headline should return headline's number.
3036 (should
3037 ;; Note: Headline's number is in fact a list of numbers.
3038 (equal '(2)
3039 (org-test-with-parsed-data
3040 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
3041 (org-element-map tree 'link
3042 (lambda (link)
3043 (org-export-get-ordinal
3044 (org-export-resolve-fuzzy-link link info) info)) info t))))
3045 ;; Link to a target in an item should return item's number.
3046 (should
3047 ;; Note: Item's number is in fact a list of numbers.
3048 (equal '(1 2)
3049 (org-test-with-parsed-data
3050 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
3051 (org-element-map tree 'link
3052 (lambda (link)
3053 (org-export-get-ordinal
3054 (org-export-resolve-fuzzy-link link info) info)) info t))))
3055 ;; Link to a target in a footnote should return footnote's number.
3056 (should
3057 (equal '(2 3)
3058 (org-test-with-parsed-data "
3059 Paragraph[fn:1][fn:2][fn:lbl3:C<<target>>][[test]][[target]]
3060 \[fn:1] A
3062 \[fn:2] <<test>>B"
3063 (org-element-map tree 'link
3064 (lambda (link)
3065 (org-export-get-ordinal
3066 (org-export-resolve-fuzzy-link link info) info)) info))))
3067 ;; Link to a named element should return sequence number of that
3068 ;; element.
3069 (should
3070 (= 2
3071 (org-test-with-parsed-data
3072 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
3073 (org-element-map tree 'link
3074 (lambda (link)
3075 (org-export-get-ordinal
3076 (org-export-resolve-fuzzy-link link info) info)) info t))))
3077 ;; Link to a target not within an item, a table, a footnote
3078 ;; reference or definition should return section number.
3079 (should
3080 (equal '(2)
3081 (org-test-with-parsed-data
3082 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
3083 (org-element-map tree 'link
3084 (lambda (link)
3085 (org-export-get-ordinal
3086 (org-export-resolve-fuzzy-link link info) info)) info t))))
3087 ;; Space are not significant when matching a fuzzy link.
3088 (should
3089 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
3090 (org-element-map tree 'link
3091 (lambda (link) (org-export-resolve-fuzzy-link link info))
3092 info t)))
3093 ;; Statistics cookies are ignored for headline match.
3094 (should
3095 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
3096 (org-element-map tree 'link
3097 (lambda (link) (org-export-resolve-fuzzy-link link info))
3098 info t)))
3099 (should
3100 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
3101 (org-element-map tree 'link
3102 (lambda (link) (org-export-resolve-fuzzy-link link info))
3103 info t))))
3105 (defun test-org-gen-loc-list(text type)
3106 (org-test-with-parsed-data text
3107 (org-element-map tree type
3108 (lambda (el) (or (org-export-get-loc el info) 'no-loc)))))
3110 (ert-deftest test-org-export/get-loc ()
3111 "Test `org-export-get-loc' specifications."
3112 (should
3113 ;; "-n" resets line number.
3114 (equal '(0)
3115 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n\n Text\n#+END_EXAMPLE"
3116 'example-block)))
3117 ;; The first "+n" has 0 lines before it
3118 (should
3119 (equal '(0)
3120 (test-org-gen-loc-list "#+BEGIN_EXAMPLE +n\n Text\n#+END_EXAMPLE"
3121 'example-block)))
3122 ;; "-n 10" resets line number but has "9 lines" before it.
3123 (should
3124 (equal '(9)
3125 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n 10\n Text\n#+END_EXAMPLE"
3126 'example-block)))
3127 ;; -n10 with two lines then +n 15
3128 (should
3129 (equal '(9 25)
3130 (test-org-gen-loc-list "
3131 #+BEGIN_EXAMPLE -n 10
3132 Text_10
3133 Second line(11)
3134 #+END_EXAMPLE
3135 #+BEGIN_EXAMPLE +n 15
3136 Text line (11 + 15)
3137 #+END_EXAMPLE"
3138 'example-block)))
3139 (should
3140 (equal '(9 19 0)
3141 (test-org-gen-loc-list "
3142 #+BEGIN_EXAMPLE -n 10
3143 Text
3144 #+END_EXAMPLE
3145 #+BEGIN_EXAMPLE +n 10
3146 Text
3147 #+END_EXAMPLE
3149 #+BEGIN_EXAMPLE -n
3150 Text
3151 #+END_EXAMPLE"
3152 'example-block)))
3153 ;; an Example Block without -n does not add to the line count.
3154 (should
3155 (equal '(9 no-loc 19)
3156 (test-org-gen-loc-list "
3157 #+BEGIN_EXAMPLE -n 10
3158 Text
3159 #+END_EXAMPLE
3160 #+BEGIN_EXAMPLE
3161 Text
3162 #+END_EXAMPLE
3163 #+BEGIN_EXAMPLE +n 10
3164 Text
3165 #+END_EXAMPLE"
3166 'example-block)))
3167 ;; "-n" resets line number.
3168 (should
3169 (equal
3170 '(0)
3171 (test-org-gen-loc-list "#+BEGIN_SRC emacs-lisp -n \n (- 1 1) \n#+END_SRC"
3172 'src-block)))
3173 ;; The first "+n" has 0 lines before it.
3174 (should
3175 (equal '(0)
3176 (test-org-gen-loc-list
3177 "#+BEGIN_SRC emacs-lisp +n \n (+ 0 (- 1 1))\n#+END_SRC"
3178 'src-block)))
3179 ;; "-n 10" resets line number but has "9 lines" before it.
3180 (should
3181 (equal '(9)
3182 (test-org-gen-loc-list
3183 "#+BEGIN_SRC emacs-lisp -n 10\n (- 10 1)\n#+END_SRC"
3184 'src-block)))
3185 (should
3186 (equal '(9 25)
3187 (test-org-gen-loc-list "
3188 #+BEGIN_SRC emacs-lisp -n 10
3189 (- 10 1)
3190 (+ (- 10 1) 1)
3191 #+END_SRC
3192 #+BEGIN_SRC emacs-lisp +n 15
3193 (+ (- 10 1) 2 (- 15 1))
3194 #+END_SRC"
3195 'src-block)))
3196 (should
3197 (equal '(9 19 0)
3198 (test-org-gen-loc-list "
3199 #+BEGIN_SRC emacs-lisp -n 10
3200 (- 10 1)
3201 #+END_SRC
3202 #+BEGIN_SRC emacs-lisp +n 10
3203 (+ (- 10 1) 1 (- 10 1))
3204 #+END_SRC
3205 #+BEGIN_SRC emacs-lisp -n
3206 (- 1 1)
3207 #+END_SRC"
3208 'src-block)))
3209 ;; A SRC Block without -n does not add to the line count.
3210 (should
3211 (equal '(9 no-loc 19)
3212 (test-org-gen-loc-list
3213 "#+BEGIN_SRC emacs-lisp -n 10
3214 (+ (-10 1) 1)
3215 #+END_SRC
3216 #+BEGIN_SRC emacs-lisp
3217 (+ 2 2)
3218 #+END_SRC
3219 #+BEGIN_SRC emacs-lisp +n 10
3220 (+ (- 10 1) 1 (- 10 1))
3221 #+END_SRC"
3222 'src-block))))
3224 (ert-deftest test-org-export/resolve-coderef ()
3225 "Test `org-export-resolve-coderef' specifications."
3226 (let ((org-coderef-label-format "(ref:%s)"))
3227 ;; A link to a "-n -k -r" block returns line number.
3228 (should
3229 (= 1
3230 (org-test-with-parsed-data
3231 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3232 (org-export-resolve-coderef "coderef" info))))
3233 (should
3234 (= 10
3235 (org-test-with-parsed-data
3236 "#+BEGIN_EXAMPLE -n 10 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3237 (org-export-resolve-coderef "coderef" info))))
3238 (should
3239 (= 135
3240 (org-test-with-parsed-data
3241 "#+BEGIN_EXAMPLE -n 10 -k -r\nText \n#+END_EXAMPLE\n
3242 #+BEGIN_EXAMPLE +n 125 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3243 (org-export-resolve-coderef "coderef" info))))
3244 (should
3245 (= 1
3246 (org-test-with-parsed-data
3247 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3248 (org-export-resolve-coderef "coderef" info))))
3249 (should
3250 (= 10
3251 (org-test-with-parsed-data
3252 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3253 (org-export-resolve-coderef "coderef" info))))
3254 (should
3255 (= 135
3256 (org-test-with-parsed-data
3257 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) \n#+END_SRC\n
3258 #+BEGIN_SRC emacs-lisp +n 125 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3259 (org-export-resolve-coderef "coderef" info))))
3260 ;; A link to a "-n -r" block returns line number.
3261 (should
3262 (= 1
3263 (org-test-with-parsed-data
3264 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
3265 (org-export-resolve-coderef "coderef" info))))
3266 (should
3267 (= 10
3268 (org-test-with-parsed-data
3269 "#+BEGIN_EXAMPLE -n 10 -r\nText (ref:coderef)\n#+END_EXAMPLE"
3270 (org-export-resolve-coderef "coderef" info))))
3271 (should
3272 (= 135
3273 (org-test-with-parsed-data
3274 "#+BEGIN_EXAMPLE +n 10 -r\nText \n#+END_EXAMPLE
3275 #+BEGIN_EXAMPLE +n 125 -r\nText (ref:coderef)\n#+END_EXAMPLE"
3276 (org-export-resolve-coderef "coderef" info))))
3278 (should
3279 (= 1
3280 (org-test-with-parsed-data
3281 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3282 (org-export-resolve-coderef "coderef" info))))
3283 (should
3284 (= 10
3285 (org-test-with-parsed-data
3286 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3287 (org-export-resolve-coderef "coderef" info))))
3288 (should
3289 (= 135
3290 (org-test-with-parsed-data
3291 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) \n#+END_SRC
3292 #+BEGIN_SRC emacs-lisp +n125 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3293 (org-export-resolve-coderef "coderef" info))))
3294 ;; A link to a "-n" block returns coderef.
3295 (should
3296 (equal "coderef"
3297 (org-test-with-parsed-data
3298 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3299 (org-export-resolve-coderef "coderef" info))))
3300 (should
3301 (equal "coderef"
3302 (org-test-with-parsed-data
3303 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
3304 (org-export-resolve-coderef "coderef" info))))
3305 ;; A link to a "-r" block returns line number.
3306 (should
3307 (= 1
3308 (org-test-with-parsed-data
3309 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3310 (org-export-resolve-coderef "coderef" info))))
3311 (should
3312 (= 1
3313 (org-test-with-parsed-data
3314 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
3315 (org-export-resolve-coderef "coderef" info))))
3316 ;; A link to a block without a switch returns coderef.
3317 (should
3318 (equal "coderef"
3319 (org-test-with-parsed-data
3320 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3321 (org-export-resolve-coderef "coderef" info))))
3322 (org-test-with-parsed-data
3323 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
3324 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
3325 ;; Correctly handle continued line numbers. A "+n" switch should
3326 ;; resume numbering from previous block with numbered lines,
3327 ;; ignoring blocks not numbering lines in the process. A "-n"
3328 ;; switch resets count.
3329 (should
3330 (equal '(2 1)
3331 (org-test-with-parsed-data "
3332 #+BEGIN_EXAMPLE -n
3333 Text.
3334 #+END_EXAMPLE
3336 #+BEGIN_SRC emacs-lisp
3337 \(- 1 1)
3338 #+END_SRC
3340 #+BEGIN_SRC emacs-lisp +n -r
3341 \(+ 1 1) (ref:addition)
3342 #+END_SRC
3344 #+BEGIN_EXAMPLE -n -r
3345 Another text. (ref:text)
3346 #+END_EXAMPLE"
3347 (list (org-export-resolve-coderef "addition" info)
3348 (org-export-resolve-coderef "text" info)))))
3349 ;; Recognize coderef with user-specified syntax.
3350 (should
3351 (equal
3352 "text"
3353 (org-test-with-parsed-data
3354 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
3355 (org-export-resolve-coderef "text" info))))
3356 ;; Unresolved coderefs raise a `org-link-broken' signal.
3357 (should
3358 (condition-case nil
3359 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
3360 (org-export-resolve-coderef "unknown" info))
3361 (org-link-broken t)))))
3363 (ert-deftest test-org-export/resolve-fuzzy-link ()
3364 "Test `org-export-resolve-fuzzy-link' specifications."
3365 ;; Match target objects.
3366 (should
3367 (org-test-with-parsed-data "<<target>> [[target]]"
3368 (org-export-resolve-fuzzy-link
3369 (org-element-map tree 'link 'identity info t) info)))
3370 ;; Match named elements.
3371 (should
3372 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
3373 (org-export-resolve-fuzzy-link
3374 (org-element-map tree 'link 'identity info t) info)))
3375 ;; Match exact headline's name.
3376 (should
3377 (org-test-with-parsed-data "* My headline\n[[My headline]]"
3378 (org-export-resolve-fuzzy-link
3379 (org-element-map tree 'link 'identity info t) info)))
3380 ;; Targets objects have priority over headline titles.
3381 (should
3382 (eq 'target
3383 (org-test-with-parsed-data "* target\n<<target>>[[target]]"
3384 (org-element-type
3385 (org-export-resolve-fuzzy-link
3386 (org-element-map tree 'link 'identity info t) info)))))
3387 ;; Named elements have priority over headline titles.
3388 (should
3389 (eq 'paragraph
3390 (org-test-with-parsed-data
3391 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
3392 (org-element-type
3393 (org-export-resolve-fuzzy-link
3394 (org-element-map tree 'link 'identity info t) info)))))
3395 ;; If link's path starts with a "*", only match headline titles,
3396 ;; though.
3397 (should
3398 (eq 'headline
3399 (org-test-with-parsed-data
3400 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
3401 (org-element-type
3402 (org-export-resolve-fuzzy-link
3403 (org-element-map tree 'link 'identity info t) info)))))
3404 ;; Raise a `org-link-broken' signal if no match.
3405 (should
3406 (org-test-with-parsed-data "[[target]]"
3407 (condition-case nil
3408 (org-export-resolve-fuzzy-link
3409 (org-element-map tree 'link #'identity info t) info)
3410 (org-link-broken t))))
3411 ;; Match fuzzy link even when before first headline.
3412 (should
3413 (eq 'headline
3414 (org-test-with-parsed-data "[[hl]]\n* hl"
3415 (org-element-type
3416 (org-export-resolve-fuzzy-link
3417 (org-element-map tree 'link 'identity info t) info)))))
3418 ;; Handle url-encoded fuzzy links.
3419 (should
3420 (org-test-with-parsed-data "* A B\n[[A%20B]]"
3421 (org-export-resolve-fuzzy-link
3422 (org-element-map tree 'link #'identity info t) info))))
3424 (ert-deftest test-org-export/resolve-id-link ()
3425 "Test `org-export-resolve-id-link' specifications."
3426 ;; Regular test for custom-id link.
3427 (should
3428 (equal '("Headline1")
3429 (org-test-with-parsed-data "* Headline1
3430 :PROPERTIES:
3431 :CUSTOM_ID: test
3432 :END:
3433 * Headline 2
3434 \[[#test]]"
3435 (org-element-property
3436 :title
3437 (org-export-resolve-id-link
3438 (org-element-map tree 'link 'identity info t) info)))))
3439 ;; Raise a `org-link-broken' signal on failing searches.
3440 (should
3441 (org-test-with-parsed-data "* Headline1
3442 :PROPERTIES:
3443 :CUSTOM_ID: test
3444 :END:
3445 * Headline 2
3446 \[[#no-match]]"
3447 (condition-case nil
3448 (org-export-resolve-id-link
3449 (org-element-map tree 'link #'identity info t) info)
3450 (org-link-broken t))))
3451 ;; Test for internal id target.
3452 (should
3453 (equal '("Headline1")
3454 (org-test-with-parsed-data "* Headline1
3455 :PROPERTIES:
3456 :ID: aaaa
3457 :END:
3458 * Headline 2
3459 \[[id:aaaa]]"
3460 (org-element-property
3461 :title
3462 (org-export-resolve-id-link
3463 (org-element-map tree 'link 'identity info t) info)))))
3464 ;; Test for external id target.
3465 (should
3466 (equal
3467 "external-file"
3468 (org-test-with-parsed-data "[[id:aaaa]]"
3469 (org-export-resolve-id-link
3470 (org-element-map tree 'link 'identity info t)
3471 (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
3473 (ert-deftest test-org-export/resolve-radio-link ()
3474 "Test `org-export-resolve-radio-link' specifications."
3475 ;; Standard test.
3476 (should
3477 (org-test-with-temp-text "<<<radio>>> radio"
3478 (org-update-radio-target-regexp)
3479 (let* ((tree (org-element-parse-buffer))
3480 (info `(:parse-tree ,tree)))
3481 (org-export-resolve-radio-link
3482 (org-element-map tree 'link 'identity info t)
3483 info))))
3484 ;; Radio targets are case-insensitive.
3485 (should
3486 (org-test-with-temp-text "<<<RADIO>>> radio"
3487 (org-update-radio-target-regexp)
3488 (let* ((tree (org-element-parse-buffer))
3489 (info `(:parse-tree ,tree)))
3490 (org-export-resolve-radio-link
3491 (org-element-map tree 'link 'identity info t)
3492 info))))
3493 ;; Radio target with objects.
3494 (should
3495 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
3496 (org-update-radio-target-regexp)
3497 (let* ((tree (org-element-parse-buffer))
3498 (info `(:parse-tree ,tree)))
3499 (org-export-resolve-radio-link
3500 (org-element-map tree 'link 'identity info t)
3501 info))))
3502 ;; Radio target with objects at its beginning.
3503 (should
3504 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
3505 (org-update-radio-target-regexp)
3506 (let* ((tree (org-element-parse-buffer))
3507 (info `(:parse-tree ,tree)))
3508 (org-export-resolve-radio-link
3509 (org-element-map tree 'link 'identity info t)
3510 info))))
3511 ;; Radio link next to an apostrophe.
3512 (should
3513 (org-test-with-temp-text "<<<radio>>> radio's"
3514 (org-update-radio-target-regexp)
3515 (let* ((tree (org-element-parse-buffer))
3516 (info `(:parse-tree ,tree)))
3517 (org-export-resolve-radio-link
3518 (org-element-map tree 'link 'identity info t)
3519 info))))
3520 ;; Multiple radio targets.
3521 (should
3522 (equal '("radio1" "radio2")
3523 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
3524 (org-update-radio-target-regexp)
3525 (let* ((tree (org-element-parse-buffer))
3526 (info `(:parse-tree ,tree)))
3527 (org-element-map tree 'link
3528 (lambda (link)
3529 (org-element-property
3530 :value (org-export-resolve-radio-link link info)))
3531 info)))))
3532 ;; Radio target is whitespace insensitive.
3533 (should
3534 (org-test-with-temp-text "<<<a radio>>> a\n radio"
3535 (org-update-radio-target-regexp)
3536 (let* ((tree (org-element-parse-buffer))
3537 (info `(:parse-tree ,tree)))
3538 (org-element-map tree 'link
3539 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
3541 (ert-deftest test-org-export/file-uri ()
3542 "Test `org-export-file-uri' specifications."
3543 ;; Preserve relative filenames.
3544 (should (equal "relative.org" (org-export-file-uri "relative.org")))
3545 ;; Local files start with "file://"
3546 (should (equal (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "/local.org"))
3547 (org-export-file-uri "/local.org")))
3548 ;; Remote files start with "file://"
3549 (should (equal "file://ssh:myself@some.where:papers/last.pdf"
3550 (org-export-file-uri "/ssh:myself@some.where:papers/last.pdf")))
3551 (should (equal "file://localhost/etc/fstab"
3552 (org-export-file-uri "//localhost/etc/fstab")))
3553 ;; Expand filename starting with "~".
3554 (should (equal (org-export-file-uri "~/file.org")
3555 (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "~/file.org")))))
3557 (ert-deftest test-org-export/get-reference ()
3558 "Test `org-export-get-reference' specifications."
3559 (should
3560 (org-test-with-parsed-data "* Headline"
3561 (org-export-get-reference (org-element-map tree 'headline #'identity nil t)
3562 info)))
3563 ;; For a given element always return the same reference.
3564 (should
3565 (org-test-with-parsed-data "* Headline"
3566 (let ((headline (org-element-map tree 'headline #'identity nil t)))
3567 (equal (org-export-get-reference headline info)
3568 (org-export-get-reference headline info)))))
3569 ;; References get through local export back-ends.
3570 (should
3571 (org-test-with-parsed-data "* Headline"
3572 (let ((headline (org-element-map tree 'headline #'identity nil t))
3573 (backend
3574 (org-export-create-backend
3575 :transcoders
3576 '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
3577 (equal (org-trim (org-export-data-with-backend headline backend info))
3578 (org-export-get-reference headline info)))))
3579 (should
3580 (org-test-with-parsed-data "* Headline"
3581 (let ((headline (org-element-map tree 'headline #'identity nil t))
3582 (backend
3583 (org-export-create-backend
3584 :transcoders
3585 '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
3586 (equal (org-export-with-backend backend headline nil info)
3587 (org-export-get-reference headline info)))))
3588 ;; Use search cells defined in `:crossrefs'. However, handle
3589 ;; duplicate search cells.
3590 (should
3591 (equal "org0000001"
3592 (org-test-with-parsed-data "* Headline"
3593 (let* ((headline (org-element-map tree 'headline #'identity nil t))
3594 (search-cell (car (org-export-search-cells headline))))
3595 (setq info
3596 (plist-put info :crossrefs (list (cons search-cell 1))))
3597 (org-export-get-reference headline info)))))
3598 (should-not
3599 (equal '("org0000001" "org0000001")
3600 (org-test-with-parsed-data "* H\n** H"
3601 (org-element-map tree 'headline
3602 (lambda (h)
3603 (let* ((search-cell (car (org-export-search-cells h)))
3604 (info (plist-put info :crossrefs
3605 (list (cons search-cell 1)))))
3606 (org-export-get-reference h info))))))))
3609 ;;; Pseudo objects and pseudo elements
3611 (ert-deftest test-org-export/pseudo-elements ()
3612 "Test exporting pseudo-elements."
3613 ;; Handle blank lines after pseudo-elements. In particular, do not
3614 ;; replace them with white spaces.
3615 (should
3616 (equal "contents\n\nparagraph\n"
3617 (let ((backend (org-export-create-backend
3618 :transcoders
3619 '((pseudo-element . (lambda (_p c _i) c))
3620 (paragraph . (lambda (_p c _i) c))
3621 (plain-text . (lambda (c _i) c)))))
3622 (element '(pseudo-element (:post-blank 1) "contents"))
3623 (paragraph '(paragraph nil "paragraph"))
3624 (data '(org-data nil)))
3625 (org-element-adopt-elements data element paragraph)
3626 (org-export-data-with-backend data backend nil)))))
3628 (ert-deftest test-org-export/pseudo-objects ()
3629 "Test exporting pseudo-objects."
3630 ;; Handle blank spaces after pseudo-objects. In particular, do not
3631 ;; replace them with newlines.
3632 (should
3633 (equal "begin x end\n"
3634 (let ((backend (org-export-create-backend
3635 :transcoders
3636 '((pseudo-object . (lambda (_p c _i) c))
3637 (paragraph . (lambda (_p c _i) c))
3638 (plain-text . (lambda (c _i) c)))))
3639 (object '(pseudo-object (:post-blank 1) "x"))
3640 (paragraph '(paragraph nil)))
3641 (org-element-adopt-elements paragraph "begin " object "end")
3642 (org-export-data-with-backend paragraph backend nil)))))
3645 ;;; Src-block and example-block
3647 (ert-deftest test-org-export/unravel-code ()
3648 "Test `org-export-unravel-code' function."
3649 ;; Code without reference.
3650 (should
3651 (equal '("(+ 1 1)")
3652 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
3653 (org-export-unravel-code (org-element-at-point)))))
3654 ;; Code with reference.
3655 (should
3656 (equal '("(+ 1 1)" (1 . "test"))
3657 (org-test-with-temp-text
3658 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
3659 (let ((org-coderef-label-format "(ref:%s)"))
3660 (org-export-unravel-code (org-element-at-point))))))
3661 ;; Code with user-defined reference.
3662 (should
3663 (equal
3664 '("(+ 1 1)" (1 . "test"))
3665 (org-test-with-temp-text
3666 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
3667 (let ((org-coderef-label-format "(ref:%s)"))
3668 (org-export-unravel-code (org-element-at-point))))))
3669 ;; Code references keys are relative to the current block.
3670 (should
3671 (equal '("(+ 2 2)\n(+ 3 3)" (2 . "one"))
3672 (org-test-with-temp-text "
3673 #+BEGIN_EXAMPLE -n
3674 \(+ 1 1)
3675 #+END_EXAMPLE
3676 #+BEGIN_EXAMPLE +n
3677 \(+ 2 2)
3678 \(+ 3 3) (ref:one)
3679 #+END_EXAMPLE"
3680 (goto-line 5)
3681 (let ((org-coderef-label-format "(ref:%s)"))
3682 (org-export-unravel-code (org-element-at-point)))))\x14))
3684 (ert-deftest test-org-export/format-code-default ()
3685 "Test `org-export-format-code-default' specifications."
3686 ;; Preserve blank lines, even when code is empty.
3687 (should
3688 (equal "\n\n"
3689 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
3690 (org-export-format-code-default
3691 (org-element-map tree 'src-block #'identity info t) info))))
3692 ;; Likewise, preserve leading and trailing blank lines in the code.
3693 (should
3694 (equal "\n(+ 1 1)\n"
3695 (org-test-with-parsed-data
3696 "#+BEGIN_SRC emacs-lisp\n\n(+ 1 1)\n#+END_SRC"
3697 (org-export-format-code-default
3698 (org-element-map tree 'src-block #'identity info t) info))))
3699 (should
3700 (equal "(+ 1 1)\n\n"
3701 (org-test-with-parsed-data
3702 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n\n#+END_SRC"
3703 (org-export-format-code-default
3704 (org-element-map tree 'src-block #'identity info t) info))))
3705 ;; Number lines, two whitespace characters before the actual loc.
3706 (should
3707 (equal "1 a\n2 b\n"
3708 (org-test-with-parsed-data
3709 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
3710 (org-export-format-code-default
3711 (org-element-map tree 'src-block #'identity info t) info))))
3712 ;; Numbering includes blank lines.
3713 (should
3714 (equal "1 \n2 a\n3 \n4 b\n5 \n"
3715 (org-test-with-parsed-data
3716 "#+BEGIN_SRC emacs-lisp +n\n\na\n\nb\n\n#+END_SRC"
3717 (org-export-format-code-default
3718 (org-element-map tree 'src-block #'identity info t) info))))
3719 ;; Put references 6 whitespace characters after the widest line,
3720 ;; wrapped within parenthesis.
3721 (should
3722 (equal "123 (a)\n1 (b)\n"
3723 (let ((org-coderef-label-format "(ref:%s)"))
3724 (org-test-with-parsed-data
3725 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
3726 (org-export-format-code-default
3727 (org-element-map tree 'src-block #'identity info t) info))))))
3731 ;;; Smart Quotes
3733 (ert-deftest test-org-export/activate-smart-quotes ()
3734 "Test `org-export-activate-smart-quotes' specifications."
3735 ;; Double quotes: standard test.
3736 (should
3737 (equal
3738 '("some &ldquo;quoted&rdquo; text")
3739 (let ((org-export-default-language "en"))
3740 (org-test-with-parsed-data "some \"quoted\" text"
3741 (org-element-map tree 'plain-text
3742 (lambda (s) (org-export-activate-smart-quotes s :html info))
3743 info)))))
3744 ;; Opening quotes: at the beginning of a paragraph.
3745 (should
3746 (equal
3747 '("&ldquo;begin")
3748 (let ((org-export-default-language "en"))
3749 (org-test-with-parsed-data "\"begin"
3750 (org-element-map tree 'plain-text
3751 (lambda (s) (org-export-activate-smart-quotes s :html info))
3752 info)))))
3753 ;; Opening quotes: after an object.
3754 (should
3755 (equal
3756 '("&ldquo;quoted&rdquo; text")
3757 (let ((org-export-default-language "en"))
3758 (org-test-with-parsed-data "=verb= \"quoted\" text"
3759 (org-element-map tree 'plain-text
3760 (lambda (s) (org-export-activate-smart-quotes s :html info))
3761 info)))))
3762 ;; Closing quotes: at the end of a paragraph.
3763 (should
3764 (equal
3765 '("Quoted &ldquo;text&rdquo;")
3766 (let ((org-export-default-language "en"))
3767 (org-test-with-parsed-data "Quoted \"text\""
3768 (org-element-map tree 'plain-text
3769 (lambda (s) (org-export-activate-smart-quotes s :html info))
3770 info)))))
3771 ;; Inner quotes: standard test.
3772 (should
3773 (equal '("« outer « inner » outer »")
3774 (let ((org-export-default-language "fr"))
3775 (org-test-with-parsed-data "\"outer 'inner' outer\""
3776 (org-element-map tree 'plain-text
3777 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3778 info)))))
3779 ;; Inner quotes: close to special symbols.
3780 (should
3781 (equal '("« outer (« inner ») outer »")
3782 (let ((org-export-default-language "fr"))
3783 (org-test-with-parsed-data "\"outer ('inner') outer\""
3784 (org-element-map tree 'plain-text
3785 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3786 info)))))
3787 (should
3788 (equal '("« « inner » »")
3789 (let ((org-export-default-language "fr"))
3790 (org-test-with-parsed-data "\"'inner'\""
3791 (org-element-map tree 'plain-text
3792 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3793 info)))))
3794 ;; Apostrophe: standard test.
3795 (should
3796 (equal '("It « shouldn’t » fail")
3797 (let ((org-export-default-language "fr"))
3798 (org-test-with-parsed-data "It \"shouldn't\" fail"
3799 (org-element-map tree 'plain-text
3800 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3801 info)))))
3802 (should
3803 (equal '("It shouldn’t fail")
3804 (let ((org-export-default-language "fr"))
3805 (org-test-with-parsed-data "It shouldn't fail"
3806 (org-element-map tree 'plain-text
3807 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3808 info)))))
3809 ;; Apostrophe: before an object.
3810 (should
3811 (equal
3812 '("« a’" " »")
3813 (let ((org-export-default-language "fr"))
3814 (org-test-with-parsed-data "\"a'=b=\""
3815 (org-element-map tree 'plain-text
3816 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3817 info)))))
3818 ;; Apostrophe: after an object.
3819 (should
3820 (equal '("« " "’s »")
3821 (let ((org-export-default-language "fr"))
3822 (org-test-with-parsed-data "\"=code='s\""
3823 (org-element-map tree 'plain-text
3824 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3825 info)))))
3826 ;; Special case: isolated quotes.
3827 (should
3828 (equal '("&ldquo;" "&rdquo;")
3829 (let ((org-export-default-language "en"))
3830 (org-test-with-parsed-data "\"$x$\""
3831 (org-element-map tree 'plain-text
3832 (lambda (s) (org-export-activate-smart-quotes s :html info))
3833 info)))))
3834 ;; Smart quotes in secondary strings.
3835 (should
3836 (equal '("&ldquo;" "&rdquo;")
3837 (let ((org-export-default-language "en"))
3838 (org-test-with-parsed-data "* \"$x$\""
3839 (org-element-map tree 'plain-text
3840 (lambda (s) (org-export-activate-smart-quotes s :html info))
3841 info)))))
3842 ;; Smart quotes in document keywords.
3843 (should
3844 (equal '("&ldquo;" "&rdquo;")
3845 (let ((org-export-default-language "en"))
3846 (org-test-with-parsed-data "#+TITLE: \"$x$\""
3847 (org-element-map (plist-get info :title) 'plain-text
3848 (lambda (s) (org-export-activate-smart-quotes s :html info))
3849 info)))))
3850 ;; Smart quotes in parsed affiliated keywords.
3851 (should
3852 (equal '("&ldquo;" "&rdquo;" "Paragraph")
3853 (let ((org-export-default-language "en"))
3854 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
3855 (org-element-map tree 'plain-text
3856 (lambda (s) (org-export-activate-smart-quotes s :html info))
3857 info nil nil t)))))
3858 ;; Smart quotes within objects.
3859 (should
3860 (equal '("&ldquo;foo&rdquo;")
3861 (let ((org-export-default-language "en"))
3862 (org-test-with-parsed-data "| \"foo\" |"
3863 (org-element-map tree 'plain-text
3864 (lambda (s) (org-export-activate-smart-quotes s :html info))
3865 info nil nil t)))))
3866 ;; FIXME: Test failing non-interactively.
3868 ;; (should
3869 ;; (equal '("&ldquo;foo&rdquo;")
3870 ;; (let ((org-export-default-language "en"))
3871 ;; (org-test-with-parsed-data "*\"foo\"*"
3872 ;; (org-element-map tree 'plain-text
3873 ;; (lambda (s) (org-export-activate-smart-quotes s :html info))
3874 ;; info nil nil t)))))
3879 ;;; Tables
3881 (ert-deftest test-org-export/special-column ()
3882 "Test if the table's special column is properly recognized."
3883 ;; 1. First column is special if it contains only a special marking
3884 ;; characters or empty cells.
3885 (org-test-with-temp-text "
3886 | ! | 1 |
3887 | | 2 |"
3888 (should
3889 (org-export-table-has-special-column-p
3890 (org-element-map
3891 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3892 ;; 2. If the column contains anything else, it isn't special.
3893 (org-test-with-temp-text "
3894 | ! | 1 |
3895 | b | 2 |"
3896 (should-not
3897 (org-export-table-has-special-column-p
3898 (org-element-map
3899 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3900 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
3901 ;; and "!".
3902 (org-test-with-temp-text "
3903 | # | 1 |
3904 | ^ | 2 |
3905 | * | 3 |
3906 | _ | 4 |
3907 | / | 5 |
3908 | $ | 6 |
3909 | ! | 7 |"
3910 (should
3911 (org-export-table-has-special-column-p
3912 (org-element-map
3913 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3914 ;; 4. A first column with only empty cells isn't considered as
3915 ;; special.
3916 (org-test-with-temp-text "
3917 | | 1 |
3918 | | 2 |"
3919 (should-not
3920 (org-export-table-has-special-column-p
3921 (org-element-map
3922 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
3924 (ert-deftest test-org-export/table-row-is-special-p ()
3925 "Test `org-export-table-row-is-special-p' specifications."
3926 ;; 1. A row is special if it has a special marking character in the
3927 ;; special column.
3928 (org-test-with-parsed-data "| ! | 1 |"
3929 (should
3930 (org-export-table-row-is-special-p
3931 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3932 ;; 2. A row is special when its first field is "/"
3933 (org-test-with-parsed-data "
3934 | / | 1 |
3935 | a | b |"
3936 (should
3937 (org-export-table-row-is-special-p
3938 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3939 ;; 3. A row only containing alignment cookies is also considered as
3940 ;; special.
3941 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
3942 (should
3943 (org-export-table-row-is-special-p
3944 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3945 ;; 4. Everything else isn't considered as special.
3946 (org-test-with-parsed-data "| \alpha | | c |"
3947 (should-not
3948 (org-export-table-row-is-special-p
3949 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3950 ;; 5. Table's rules are never considered as special rows.
3951 (org-test-with-parsed-data "|---+---|"
3952 (should-not
3953 (org-export-table-row-is-special-p
3954 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
3956 (ert-deftest test-org-export/has-header-p ()
3957 "Test `org-export-table-has-header-p' specifications."
3958 ;; With an header.
3959 (should
3960 (org-test-with-parsed-data "
3961 | a | b |
3962 |---+---|
3963 | c | d |"
3964 (org-export-table-has-header-p
3965 (org-element-map tree 'table 'identity info 'first-match)
3966 info)))
3967 ;; Without an header.
3968 (should-not
3969 (org-test-with-parsed-data "
3970 | a | b |
3971 | c | d |"
3972 (org-export-table-has-header-p
3973 (org-element-map tree 'table 'identity info 'first-match)
3974 info)))
3975 ;; Don't get fooled with starting and ending rules.
3976 (should-not
3977 (org-test-with-parsed-data "
3978 |---+---|
3979 | a | b |
3980 | c | d |
3981 |---+---|"
3982 (org-export-table-has-header-p
3983 (org-element-map tree 'table 'identity info 'first-match)
3984 info))))
3986 (ert-deftest test-org-export/table-row-group ()
3987 "Test `org-export-table-row-group' specifications."
3988 ;; A rule creates a new group.
3989 (should
3990 (equal '(1 rule 2)
3991 (org-test-with-parsed-data "
3992 | a | b |
3993 |---+---|
3994 | 1 | 2 |"
3995 (org-element-map tree 'table-row
3996 (lambda (row)
3997 (if (eq (org-element-property :type row) 'rule) 'rule
3998 (org-export-table-row-group row info)))))))
3999 ;; Special rows are ignored in count.
4000 (should
4001 (equal
4002 '(rule 1)
4003 (org-test-with-parsed-data "
4004 | / | < | > |
4005 |---|---+---|
4006 | | 1 | 2 |"
4007 (org-element-map tree 'table-row
4008 (lambda (row)
4009 (if (eq (org-element-property :type row) 'rule) 'rule
4010 (org-export-table-row-group row info)))
4011 info))))
4012 ;; Double rules also are ignored in count.
4013 (should
4014 (equal '(1 rule rule 2)
4015 (org-test-with-parsed-data "
4016 | a | b |
4017 |---+---|
4018 |---+---|
4019 | 1 | 2 |"
4020 (org-element-map tree 'table-row
4021 (lambda (row)
4022 (if (eq (org-element-property :type row) 'rule) 'rule
4023 (org-export-table-row-group row info))))))))
4025 (ert-deftest test-org-export/table-row-number ()
4026 "Test `org-export-table-row-number' specifications."
4027 ;; Standard test. Number is 0-indexed.
4028 (should
4029 (equal '(0 1)
4030 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
4031 (org-element-map tree 'table-row
4032 (lambda (row) (org-export-table-row-number row info)) info))))
4033 ;; Number ignores separators.
4034 (should
4035 (equal '(0 1)
4036 (org-test-with-parsed-data "
4037 | a | b | c |
4038 |---+---+---|
4039 | d | e | f |"
4040 (org-element-map tree 'table-row
4041 (lambda (row) (org-export-table-row-number row info)) info))))
4042 ;; Number ignores special rows.
4043 (should
4044 (equal '(0 1)
4045 (org-test-with-parsed-data "
4046 | / | < | > |
4047 | | b | c |
4048 |---+-----+-----|
4049 | | <c> | <c> |
4050 | | e | f |"
4051 (org-element-map tree 'table-row
4052 (lambda (row) (org-export-table-row-number row info)) info)))))
4054 (ert-deftest test-org-export/table-cell-width ()
4055 "Test `org-export-table-cell-width' specifications."
4056 ;; 1. Width is primarily determined by width cookies. If no cookie
4057 ;; is found, cell's width is nil.
4058 (org-test-with-parsed-data "
4059 | / | <l> | <6> | <l7> |
4060 | | a | b | c |"
4061 (should
4062 (equal
4063 '(nil 6 7)
4064 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
4065 (org-element-map tree 'table-cell 'identity info)))))
4066 ;; 2. The last width cookie has precedence.
4067 (org-test-with-parsed-data "
4068 | <6> |
4069 | <7> |
4070 | a |"
4071 (should
4072 (equal
4073 '(7)
4074 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
4075 (org-element-map tree 'table-cell 'identity info)))))
4076 ;; 3. Valid width cookies must have a specific row.
4077 (org-test-with-parsed-data "| <6> | cell |"
4078 (should
4079 (equal
4080 '(nil nil)
4081 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
4082 (org-element-map tree 'table-cell 'identity))))))
4084 (ert-deftest test-org-export/table-cell-alignment ()
4085 "Test `org-export-table-cell-alignment' specifications."
4086 ;; 1. Alignment is primarily determined by alignment cookies.
4087 (should
4088 (equal '(left center right)
4089 (let ((org-table-number-fraction 0.5)
4090 (org-table-number-regexp "^[0-9]+$"))
4091 (org-test-with-parsed-data "| <l> | <c> | <r> |"
4092 (mapcar (lambda (cell)
4093 (org-export-table-cell-alignment cell info))
4094 (org-element-map tree 'table-cell 'identity))))))
4095 ;; 2. The last alignment cookie has precedence.
4096 (should
4097 (equal '(right right right)
4098 (org-test-with-parsed-data "
4099 | <l8> |
4100 | cell |
4101 | <r9> |"
4102 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
4103 (org-element-map tree 'table-cell 'identity)))))
4104 ;; 3. If there's no cookie, cell's contents determine alignment.
4105 ;; A column mostly made of cells containing numbers will align
4106 ;; its cells to the right.
4107 (should
4108 (equal '(right right right)
4109 (let ((org-table-number-fraction 0.5)
4110 (org-table-number-regexp "^[0-9]+$"))
4111 (org-test-with-parsed-data "
4112 | 123 |
4113 | some text |
4114 | 12345 |"
4115 (mapcar (lambda (cell)
4116 (org-export-table-cell-alignment cell info))
4117 (org-element-map tree 'table-cell 'identity))))))
4118 ;; 4. Otherwise, they will be aligned to the left.
4119 (should
4120 (equal '(left left left)
4121 (org-test-with-parsed-data "
4122 | text |
4123 | some text |
4124 | \alpha |"
4125 (mapcar (lambda (cell)
4126 (org-export-table-cell-alignment cell info))
4127 (org-element-map tree 'table-cell 'identity info))))))
4129 (ert-deftest test-org-export/table-cell-borders ()
4130 "Test `org-export-table-cell-borders' specifications."
4131 ;; 1. Recognize various column groups indicators.
4132 (org-test-with-parsed-data "| / | < | > | <> |"
4133 (should
4134 (equal
4135 '((right bottom top) (left bottom top) (right bottom top)
4136 (right left bottom top))
4137 (mapcar (lambda (cell)
4138 (org-export-table-cell-borders cell info))
4139 (org-element-map tree 'table-cell 'identity)))))
4140 ;; 2. Accept shortcuts to define column groups.
4141 (org-test-with-parsed-data "| / | < | < |"
4142 (should
4143 (equal
4144 '((right bottom top) (right left bottom top) (left bottom top))
4145 (mapcar (lambda (cell)
4146 (org-export-table-cell-borders cell info))
4147 (org-element-map tree 'table-cell 'identity)))))
4148 ;; 3. A valid column groups row must start with a "/".
4149 (org-test-with-parsed-data "
4150 | | < |
4151 | a | b |"
4152 (should
4153 (equal '((top) (top) (bottom) (bottom))
4154 (mapcar (lambda (cell)
4155 (org-export-table-cell-borders cell info))
4156 (org-element-map tree 'table-cell 'identity)))))
4157 ;; 4. Take table rules into consideration.
4158 (org-test-with-parsed-data "
4159 | 1 |
4160 |---|
4161 | 2 |"
4162 (should
4163 (equal '((below top) (bottom above))
4164 (mapcar (lambda (cell)
4165 (org-export-table-cell-borders cell info))
4166 (org-element-map tree 'table-cell 'identity)))))
4167 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
4168 ;; (resp. `bottom' and `below') borders. Any special row is
4169 ;; ignored.
4170 (org-test-with-parsed-data "
4171 |---+----|
4172 | / | |
4173 | | 1 |
4174 |---+----|"
4175 (should
4176 (equal '((bottom below top above))
4177 (last
4178 (mapcar (lambda (cell)
4179 (org-export-table-cell-borders cell info))
4180 (org-element-map tree 'table-cell 'identity)))))))
4182 (ert-deftest test-org-export/table-dimensions ()
4183 "Test `org-export-table-dimensions' specifications."
4184 ;; 1. Standard test.
4185 (org-test-with-parsed-data "
4186 | 1 | 2 | 3 |
4187 | 4 | 5 | 6 |"
4188 (should
4189 (equal '(2 . 3)
4190 (org-export-table-dimensions
4191 (org-element-map tree 'table 'identity info 'first-match) info))))
4192 ;; 2. Ignore horizontal rules and special columns.
4193 (org-test-with-parsed-data "
4194 | / | < | > |
4195 | 1 | 2 | 3 |
4196 |---+---+---|
4197 | 4 | 5 | 6 |"
4198 (should
4199 (equal '(2 . 3)
4200 (org-export-table-dimensions
4201 (org-element-map tree 'table 'identity info 'first-match) info)))))
4203 (ert-deftest test-org-export/table-cell-address ()
4204 "Test `org-export-table-cell-address' specifications."
4205 ;; 1. Standard test: index is 0-based.
4206 (org-test-with-parsed-data "| a | b |"
4207 (should
4208 (equal '((0 . 0) (0 . 1))
4209 (org-element-map tree 'table-cell
4210 (lambda (cell) (org-export-table-cell-address cell info))
4211 info))))
4212 ;; 2. Special column isn't counted, nor are special rows.
4213 (org-test-with-parsed-data "
4214 | / | <> |
4215 | | c |"
4216 (should
4217 (equal '(0 . 0)
4218 (org-export-table-cell-address
4219 (car (last (org-element-map tree 'table-cell 'identity info)))
4220 info))))
4221 ;; 3. Tables rules do not count either.
4222 (org-test-with-parsed-data "
4223 | a |
4224 |---|
4225 | b |
4226 |---|
4227 | c |"
4228 (should
4229 (equal '(2 . 0)
4230 (org-export-table-cell-address
4231 (car (last (org-element-map tree 'table-cell 'identity info)))
4232 info))))
4233 ;; 4. Return nil for special cells.
4234 (org-test-with-parsed-data "| / | a |"
4235 (should-not
4236 (org-export-table-cell-address
4237 (org-element-map tree 'table-cell 'identity nil 'first-match)
4238 info))))
4240 (ert-deftest test-org-export/get-table-cell-at ()
4241 "Test `org-export-get-table-cell-at' specifications."
4242 ;; 1. Address ignores special columns, special rows and rules.
4243 (org-test-with-parsed-data "
4244 | / | <> |
4245 | | a |
4246 |---+----|
4247 | | b |"
4248 (should
4249 (equal '("b")
4250 (org-element-contents
4251 (org-export-get-table-cell-at
4252 '(1 . 0)
4253 (org-element-map tree 'table 'identity info 'first-match)
4254 info)))))
4255 ;; 2. Return value for a non-existent address is nil.
4256 (org-test-with-parsed-data "| a |"
4257 (should-not
4258 (org-export-get-table-cell-at
4259 '(2 . 2)
4260 (org-element-map tree 'table 'identity info 'first-match)
4261 info)))
4262 (org-test-with-parsed-data "| / |"
4263 (should-not
4264 (org-export-get-table-cell-at
4265 '(0 . 0)
4266 (org-element-map tree 'table 'identity info 'first-match)
4267 info))))
4269 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
4270 "Test `org-export-table-cell-starts-colgroup-p' specifications."
4271 ;; 1. A cell at a beginning of a row always starts a column group.
4272 (org-test-with-parsed-data "| a |"
4273 (should
4274 (org-export-table-cell-starts-colgroup-p
4275 (org-element-map tree 'table-cell 'identity info 'first-match)
4276 info)))
4277 ;; 2. Special column should be ignored when determining the
4278 ;; beginning of the row.
4279 (org-test-with-parsed-data "
4280 | / | |
4281 | | a |"
4282 (should
4283 (org-export-table-cell-starts-colgroup-p
4284 (org-element-map tree 'table-cell 'identity info 'first-match)
4285 info)))
4286 ;; 2. Explicit column groups.
4287 (org-test-with-parsed-data "
4288 | / | | < |
4289 | a | b | c |"
4290 (should
4291 (equal
4292 '(yes no yes)
4293 (org-element-map tree 'table-cell
4294 (lambda (cell)
4295 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
4296 info)))))
4298 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
4299 "Test `org-export-table-cell-ends-colgroup-p' specifications."
4300 ;; 1. A cell at the end of a row always ends a column group.
4301 (org-test-with-parsed-data "| a |"
4302 (should
4303 (org-export-table-cell-ends-colgroup-p
4304 (org-element-map tree 'table-cell 'identity info 'first-match)
4305 info)))
4306 ;; 2. Special column should be ignored when determining the
4307 ;; beginning of the row.
4308 (org-test-with-parsed-data "
4309 | / | |
4310 | | a |"
4311 (should
4312 (org-export-table-cell-ends-colgroup-p
4313 (org-element-map tree 'table-cell 'identity info 'first-match)
4314 info)))
4315 ;; 3. Explicit column groups.
4316 (org-test-with-parsed-data "
4317 | / | < | |
4318 | a | b | c |"
4319 (should
4320 (equal
4321 '(yes no yes)
4322 (org-element-map tree 'table-cell
4323 (lambda (cell)
4324 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
4325 info)))))
4327 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
4328 "Test `org-export-table-row-starts-rowgroup-p' specifications."
4329 ;; 1. A row at the beginning of a table always starts a row group.
4330 ;; So does a row following a table rule.
4331 (org-test-with-parsed-data "
4332 | a |
4333 |---|
4334 | b |"
4335 (should
4336 (equal
4337 '(yes no yes)
4338 (org-element-map tree 'table-row
4339 (lambda (row)
4340 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
4341 info))))
4342 ;; 2. Special rows should be ignored when determining the beginning
4343 ;; of the row.
4344 (org-test-with-parsed-data "
4345 | / | < |
4346 | | a |
4347 |---+---|
4348 | / | < |
4349 | | b |"
4350 (should
4351 (equal
4352 '(yes no yes)
4353 (org-element-map tree 'table-row
4354 (lambda (row)
4355 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
4356 info)))))
4358 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
4359 "Test `org-export-table-row-ends-rowgroup-p' specifications."
4360 ;; 1. A row at the end of a table always ends a row group. So does
4361 ;; a row preceding a table rule.
4362 (org-test-with-parsed-data "
4363 | a |
4364 |---|
4365 | b |"
4366 (should
4367 (equal
4368 '(yes no yes)
4369 (org-element-map tree 'table-row
4370 (lambda (row)
4371 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
4372 info))))
4373 ;; 2. Special rows should be ignored when determining the beginning
4374 ;; of the row.
4375 (org-test-with-parsed-data "
4376 | | a |
4377 | / | < |
4378 |---+---|
4379 | | b |
4380 | / | < |"
4381 (should
4382 (equal
4383 '(yes no yes)
4384 (org-element-map tree 'table-row
4385 (lambda (row)
4386 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
4387 info)))))
4389 (ert-deftest test-org-export/table-row-in-header-p ()
4390 "Test `org-export-table-row-in-header-p' specifications."
4391 ;; Standard test. Separators are always nil.
4392 (should
4393 (equal
4394 '(yes no no)
4395 (org-test-with-parsed-data "| a |\n|---|\n| b |"
4396 (org-element-map tree 'table-row
4397 (lambda (row)
4398 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
4399 ;; Nil when there is no header.
4400 (should
4401 (equal
4402 '(no no)
4403 (org-test-with-parsed-data "| a |\n| b |"
4404 (org-element-map tree 'table-row
4405 (lambda (row)
4406 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
4408 (ert-deftest test-org-export/table-row-starts-header-p ()
4409 "Test `org-export-table-row-starts-header-p' specifications."
4410 ;; 1. Only the row starting the first row group starts the table
4411 ;; header.
4412 (org-test-with-parsed-data "
4413 | a |
4414 | b |
4415 |---|
4416 | c |"
4417 (should
4418 (equal
4419 '(yes no no no)
4420 (org-element-map tree 'table-row
4421 (lambda (row)
4422 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
4423 info))))
4424 ;; 2. A row cannot start an header if there's no header in the
4425 ;; table.
4426 (org-test-with-parsed-data "
4427 | a |
4428 |---|"
4429 (should-not
4430 (org-export-table-row-starts-header-p
4431 (org-element-map tree 'table-row 'identity info 'first-match)
4432 info))))
4434 (ert-deftest test-org-export/table-row-ends-header-p ()
4435 "Test `org-export-table-row-ends-header-p' specifications."
4436 ;; 1. Only the row starting the first row group starts the table
4437 ;; header.
4438 (org-test-with-parsed-data "
4439 | a |
4440 | b |
4441 |---|
4442 | c |"
4443 (should
4444 (equal
4445 '(no yes no no)
4446 (org-element-map tree 'table-row
4447 (lambda (row)
4448 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
4449 info))))
4450 ;; 2. A row cannot start an header if there's no header in the
4451 ;; table.
4452 (org-test-with-parsed-data "
4453 | a |
4454 |---|"
4455 (should-not
4456 (org-export-table-row-ends-header-p
4457 (org-element-map tree 'table-row 'identity info 'first-match)
4458 info))))
4462 ;;; Tables of Contents
4464 (ert-deftest test-org-export/collect-headlines ()
4465 "Test `org-export-collect-headlines' specifications."
4466 ;; Standard test.
4467 (should
4468 (equal '("H1" "H2")
4469 (org-test-with-parsed-data "* H1\n** H2"
4470 (mapcar (lambda (h) (org-element-property :raw-value h))
4471 (org-export-collect-headlines info)))))
4472 ;; Do not collect headlines below optional argument.
4473 (should
4474 (equal '("H1")
4475 (org-test-with-parsed-data "* H1\n** H2"
4476 (mapcar (lambda (h) (org-element-property :raw-value h))
4477 (org-export-collect-headlines info 1)))))
4478 ;; Never collect headlines below maximum headline level.
4479 (should
4480 (equal '("H1")
4481 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4482 (mapcar (lambda (h) (org-element-property :raw-value h))
4483 (org-export-collect-headlines info)))))
4484 (should
4485 (equal '("H1")
4486 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4487 (mapcar (lambda (h) (org-element-property :raw-value h))
4488 (org-export-collect-headlines info 2)))))
4489 ;; Do not collect footnote section.
4490 (should
4491 (equal '("H1")
4492 (let ((org-footnote-section "Footnotes"))
4493 (org-test-with-parsed-data "* H1\n** Footnotes"
4494 (mapcar (lambda (h) (org-element-property :raw-value h))
4495 (org-export-collect-headlines info))))))
4496 ;; Do not collect headlines with UNNUMBERED property set to "notoc".
4497 ;; Headlines with another value for the property are still
4498 ;; collected. UNNUMBERED property is inherited.
4499 (should
4500 (equal '("H1")
4501 (org-test-with-parsed-data
4502 "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:"
4503 (mapcar (lambda (h) (org-element-property :raw-value h))
4504 (org-export-collect-headlines info)))))
4505 (should-not
4506 (org-test-with-parsed-data
4507 "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:\n** H2"
4508 (mapcar (lambda (h) (org-element-property :raw-value h))
4509 (org-export-collect-headlines info))))
4510 (should
4511 (equal '("H1" "H2")
4512 (org-test-with-parsed-data
4513 "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
4514 (mapcar (lambda (h) (org-element-property :raw-value h))
4515 (org-export-collect-headlines info)))))
4516 ;; Collect headlines locally.
4517 (should
4518 (equal '("H2" "H3")
4519 (org-test-with-parsed-data "* H1\n** H2\n** H3"
4520 (let ((scope (org-element-map tree 'headline #'identity info t)))
4521 (mapcar (lambda (h) (org-element-property :raw-value h))
4522 (org-export-collect-headlines info nil scope))))))
4523 ;; When collecting locally, optional level is relative.
4524 (should
4525 (equal '("H2")
4526 (org-test-with-parsed-data "* H1\n** H2\n*** H3"
4527 (let ((scope (org-element-map tree 'headline #'identity info t)))
4528 (mapcar (lambda (h) (org-element-property :raw-value h))
4529 (org-export-collect-headlines info 1 scope)))))))
4531 (ert-deftest test-org-export/excluded-from-toc-p ()
4532 "Test `org-export-excluded-from-toc-p' specifications."
4533 ;; By default, headlines are not excluded.
4534 (should-not
4535 (org-test-with-parsed-data "* H1"
4536 (org-element-map tree 'headline
4537 (lambda (h) (org-export-excluded-from-toc-p h info)) info t)))
4538 ;; Exclude according to a maximum level.
4539 (should
4540 (equal '(in out)
4541 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4542 (org-element-map tree 'headline
4543 (lambda (h) (if (org-export-excluded-from-toc-p h info) 'out 'in))
4544 info))))
4545 ;; Exclude according to UNNUMBERED property.
4546 (should
4547 (org-test-with-parsed-data "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:"
4548 (org-element-map tree 'headline
4549 (lambda (h) (org-export-excluded-from-toc-p h info)) info t)))
4550 ;; UNNUMBERED property is inherited, so is "notoc" value.
4551 (should
4552 (equal '(out out)
4553 (org-test-with-parsed-data
4554 "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:\n** H2"
4555 (org-element-map tree 'headline
4556 (lambda (h) (if (org-export-excluded-from-toc-p h info) 'out 'in))
4557 info)))))
4559 (ert-deftest test-org-export/toc-entry-backend ()
4560 "Test `org-export-toc-entry-backend' specifications."
4561 ;; Ignore targets.
4562 (should
4563 (equal "H \n"
4564 (org-test-with-temp-text "* H <<target>>"
4565 (let (org-export-registered-backends)
4566 (org-export-define-backend 'test
4567 '((headline . (lambda (h _c i) (org-export-data-with-backend
4568 (org-element-property :title h)
4569 (org-export-toc-entry-backend 'test)
4570 i)))))
4571 (org-export-as 'test)))))
4572 ;; Ignore footnote references.
4573 (should
4574 (equal "H \n"
4575 (org-test-with-temp-text "[fn:1] Definition\n* H [fn:1]"
4576 (let (org-export-registered-backends)
4577 (org-export-define-backend 'test
4578 '((headline . (lambda (h _c i) (org-export-data-with-backend
4579 (org-element-property :title h)
4580 (org-export-toc-entry-backend 'test)
4581 i)))))
4582 (org-export-as 'test)))))
4583 ;; Replace plain links with contents, or with path.
4584 (should
4585 (equal "H Org mode\n"
4586 (org-test-with-temp-text "* H [[https://orgmode.org][Org mode]]"
4587 (let (org-export-registered-backends)
4588 (org-export-define-backend 'test
4589 '((headline . (lambda (h _c i) (org-export-data-with-backend
4590 (org-element-property :title h)
4591 (org-export-toc-entry-backend 'test)
4592 i)))))
4593 (org-export-as 'test)))))
4594 (should
4595 (equal "H https://orgmode.org\n"
4596 (org-test-with-temp-text "* H [[https://orgmode.org]]"
4597 (let (org-export-registered-backends)
4598 (org-export-define-backend 'test
4599 '((headline . (lambda (h _c i) (org-export-data-with-backend
4600 (org-element-property :title h)
4601 (org-export-toc-entry-backend 'test)
4602 i)))))
4603 (org-export-as 'test)))))
4604 ;; Replace radio targets with contents.
4605 (should
4606 (equal "H radio\n"
4607 (org-test-with-temp-text "* H <<<radio>>>"
4608 (let (org-export-registered-backends)
4609 (org-export-define-backend 'test
4610 '((headline . (lambda (h _c i) (org-export-data-with-backend
4611 (org-element-property :title h)
4612 (org-export-toc-entry-backend 'test)
4613 i)))))
4614 (org-export-as 'test)))))
4615 ;; With optional argument TRANSCODERS, specify other
4616 ;; transformations.
4617 (should
4618 (equal "H bold\n"
4619 (org-test-with-temp-text "* H *bold*"
4620 (let (org-export-registered-backends)
4621 (org-export-define-backend 'test
4622 '((headline . (lambda (h _c i) (org-export-data-with-backend
4623 (org-element-property :title h)
4624 (org-export-toc-entry-backend 'test
4625 '(bold . (lambda (_b c _i) c)))
4626 i)))))
4627 (org-export-as 'test))))))
4631 ;;; Templates
4633 (ert-deftest test-org-export/inner-template ()
4634 "Test `inner-template' translator specifications."
4635 (should
4636 (equal "Success!"
4637 (org-test-with-temp-text "* Headline"
4638 (org-export-as
4639 (org-export-create-backend
4640 :transcoders
4641 '((inner-template . (lambda (contents info) "Success!"))
4642 (headline . (lambda (h c i) "Headline"))))))))
4643 ;; Inner template is applied even in a "body-only" export.
4644 (should
4645 (equal "Success!"
4646 (org-test-with-temp-text "* Headline"
4647 (org-export-as
4648 (org-export-create-backend
4649 :transcoders '((inner-template . (lambda (c i) "Success!"))
4650 (headline . (lambda (h c i) "Headline"))))
4651 nil nil 'body-only)))))
4653 (ert-deftest test-org-export/template ()
4654 "Test `template' translator specifications."
4655 (should
4656 (equal "Success!"
4657 (org-test-with-temp-text "* Headline"
4658 (org-export-as
4659 (org-export-create-backend
4660 :transcoders '((template . (lambda (contents info) "Success!"))
4661 (headline . (lambda (h c i) "Headline"))))))))
4662 ;; Template is not applied in a "body-only" export.
4663 (should-not
4664 (equal "Success!"
4665 (org-test-with-temp-text "* Headline"
4666 (org-export-as
4667 (org-export-create-backend
4668 :transcoders '((template . (lambda (contents info) "Success!"))
4669 (headline . (lambda (h c i) "Headline"))))
4670 nil nil 'body-only)))))
4674 ;;; Topology
4676 (ert-deftest test-org-export/get-next-element ()
4677 "Test `org-export-get-next-element' specifications."
4678 ;; Standard test.
4679 (should
4680 (equal "b"
4681 (org-test-with-parsed-data "* Headline\n*a* b"
4682 (org-export-get-next-element
4683 (org-element-map tree 'bold 'identity info t) info))))
4684 ;; Return nil when no previous element.
4685 (should-not
4686 (org-test-with-parsed-data "* Headline\na *b*"
4687 (org-export-get-next-element
4688 (org-element-map tree 'bold 'identity info t) info)))
4689 ;; Non-exportable elements are ignored.
4690 (should-not
4691 (let ((org-export-with-timestamps nil))
4692 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
4693 (org-export-get-next-element
4694 (org-element-map tree 'entity 'identity info t) info))))
4695 ;; Find next element in secondary strings.
4696 (should
4697 (eq 'verbatim
4698 (org-test-with-parsed-data "* a =verb="
4699 (org-element-type
4700 (org-export-get-next-element
4701 (org-element-map tree 'plain-text 'identity info t) info)))))
4702 (should
4703 (eq 'verbatim
4704 (org-test-with-parsed-data "* /italic/ =verb="
4705 (org-element-type
4706 (org-export-get-next-element
4707 (org-element-map tree 'italic 'identity info t) info)))))
4708 ;; Find next element in document keywords.
4709 (should
4710 (eq 'verbatim
4711 (org-test-with-parsed-data "#+TITLE: a =verb="
4712 (org-element-type
4713 (org-export-get-next-element
4714 (org-element-map
4715 (plist-get info :title) 'plain-text 'identity info t) info)))))
4716 ;; Find next element in parsed affiliated keywords.
4717 (should
4718 (eq 'verbatim
4719 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
4720 (org-element-type
4721 (org-export-get-next-element
4722 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4723 ;; With optional argument N, return a list containing all the
4724 ;; following elements.
4725 (should
4726 (equal
4727 '(bold code underline)
4728 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4729 (mapcar 'car
4730 (org-export-get-next-element
4731 (org-element-map tree 'italic 'identity info t) info t)))))
4732 ;; When N is a positive integer, return a list containing up to
4733 ;; N following elements.
4734 (should
4735 (equal
4736 '(bold code)
4737 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4738 (mapcar 'car
4739 (org-export-get-next-element
4740 (org-element-map tree 'italic 'identity info t) info 2))))))
4742 (ert-deftest test-org-export/get-previous-element ()
4743 "Test `org-export-get-previous-element' specifications."
4744 ;; Standard test.
4745 (should
4746 (equal "a "
4747 (org-test-with-parsed-data "* Headline\na *b*"
4748 (org-export-get-previous-element
4749 (org-element-map tree 'bold 'identity info t) info))))
4750 ;; Return nil when no previous element.
4751 (should-not
4752 (org-test-with-parsed-data "* Headline\n*a* b"
4753 (org-export-get-previous-element
4754 (org-element-map tree 'bold 'identity info t) info)))
4755 ;; Non-exportable elements are ignored.
4756 (should-not
4757 (let ((org-export-with-timestamps nil))
4758 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
4759 (org-export-get-previous-element
4760 (org-element-map tree 'entity 'identity info t) info))))
4761 ;; Find previous element in secondary strings.
4762 (should
4763 (eq 'verbatim
4764 (org-test-with-parsed-data "* =verb= a"
4765 (org-element-type
4766 (org-export-get-previous-element
4767 (org-element-map tree 'plain-text 'identity info t) info)))))
4768 (should
4769 (eq 'verbatim
4770 (org-test-with-parsed-data "* =verb= /italic/"
4771 (org-element-type
4772 (org-export-get-previous-element
4773 (org-element-map tree 'italic 'identity info t) info)))))
4774 ;; Find previous element in document keywords.
4775 (should
4776 (eq 'verbatim
4777 (org-test-with-parsed-data "#+TITLE: =verb= a"
4778 (org-element-type
4779 (org-export-get-previous-element
4780 (org-element-map
4781 (plist-get info :title) 'plain-text 'identity info t) info)))))
4782 ;; Find previous element in parsed affiliated keywords.
4783 (should
4784 (eq 'verbatim
4785 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
4786 (org-element-type
4787 (org-export-get-previous-element
4788 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4789 ;; With optional argument N, return a list containing up to
4790 ;; N previous elements.
4791 (should
4792 (equal '(underline italic bold)
4793 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4794 (mapcar 'car
4795 (org-export-get-previous-element
4796 (org-element-map tree 'code 'identity info t) info t)))))
4797 ;; When N is a positive integer, return a list containing up to
4798 ;; N previous elements.
4799 (should
4800 (equal '(italic bold)
4801 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4802 (mapcar 'car
4803 (org-export-get-previous-element
4804 (org-element-map tree 'code 'identity info t) info 2))))))
4807 (provide 'test-ox)
4808 ;;; test-org-export.el end here