Fix opening custom ID links with percent escaped syntax
[org-mode/org-tableheadings.git] / testing / lisp / test-ox.el
blob245cb7e218f6681299a29229d4a82272a5835f18
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))))))
1599 ;; If inline source block is already associated to a "results"
1600 ;; macro, do not duplicate it.
1601 (should
1602 (equal "src_emacs-lisp{(+ 1 1)} {{{results(=2=)}}}"
1603 (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)} {{{results(=2=)}}}"
1604 (let ((org-export-use-babel t)
1605 (org-babel-inline-result-wrap "=%s="))
1606 (org-export-as (org-test-default-backend)))
1607 (buffer-string)))))
1609 (ert-deftest test-org-export/before-processing-hook ()
1610 "Test `org-export-before-processing-hook'."
1611 (should
1612 (equal
1613 "#+macro: mac val\nTest\n"
1614 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
1615 (let ((org-export-before-processing-hook
1616 '((lambda (backend)
1617 (while (re-search-forward "{{{" nil t)
1618 (let ((object (org-element-context)))
1619 (when (eq (org-element-type object) 'macro)
1620 (delete-region
1621 (org-element-property :begin object)
1622 (org-element-property :end object)))))))))
1623 (org-export-as (org-test-default-backend)))))))
1625 (ert-deftest test-org-export/before-parsing-hook ()
1626 "Test `org-export-before-parsing-hook'."
1627 (should
1628 (equal "Body 1\nBody 2\n"
1629 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
1630 (let ((org-export-before-parsing-hook
1631 '((lambda (backend)
1632 (goto-char (point-min))
1633 (while (re-search-forward org-outline-regexp-bol nil t)
1634 (delete-region
1635 (point-at-bol) (progn (forward-line) (point))))))))
1636 (org-export-as (org-test-default-backend)))))))
1640 ;;; Affiliated Keywords
1642 (ert-deftest test-org-export/read-attribute ()
1643 "Test `org-export-read-attribute' specifications."
1644 ;; Standard test.
1645 (should
1646 (equal
1647 (org-export-read-attribute
1648 :attr_html
1649 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
1650 (org-element-at-point)))
1651 '(:a "1" :b "2")))
1652 ;; Return nil on empty attribute.
1653 (should-not
1654 (org-export-read-attribute
1655 :attr_html
1656 (org-test-with-temp-text "Paragraph" (org-element-at-point))))
1657 ;; Return nil on "nil" string.
1658 (should
1659 (equal '(:a nil :b nil)
1660 (org-export-read-attribute
1661 :attr_html
1662 (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
1663 (org-element-at-point)))))
1664 ;; Return nil on empty string.
1665 (should
1666 (equal '(:a nil :b nil)
1667 (org-export-read-attribute
1668 :attr_html
1669 (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
1670 (org-element-at-point)))))
1671 ;; Return empty string when value is "".
1672 (should
1673 (equal '(:a "")
1674 (org-export-read-attribute
1675 :attr_html
1676 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
1677 (org-element-at-point)))))
1678 ;; Return \"\" when value is """".
1679 (should
1680 (equal '(:a "\"\"")
1681 (org-export-read-attribute
1682 :attr_html
1683 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
1684 (org-element-at-point)))))
1685 ;; Ignore text before first property.
1686 (should-not
1687 (member "ignore"
1688 (org-export-read-attribute
1689 :attr_html
1690 (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
1691 (org-element-at-point))))))
1693 (ert-deftest test-org-export/get-caption ()
1694 "Test `org-export-get-caption' specifications."
1695 ;; Without optional argument, return long caption
1696 (should
1697 (equal
1698 '("l")
1699 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1700 (org-export-get-caption (org-element-at-point)))))
1701 ;; With optional argument, return short caption.
1702 (should
1703 (equal
1704 '("s")
1705 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1706 (org-export-get-caption (org-element-at-point) t))))
1707 ;; Multiple lines are separated by white spaces.
1708 (should
1709 (equal
1710 '("a" " " "b")
1711 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
1712 (org-export-get-caption (org-element-at-point))))))
1716 ;;; Back-End Tools
1718 (ert-deftest test-org-export/define-backend ()
1719 "Test back-end definition and accessors."
1720 ;; Translate table.
1721 (should
1722 (equal '((headline . my-headline-test))
1723 (let (org-export-registered-backends)
1724 (org-export-define-backend 'test '((headline . my-headline-test)))
1725 (org-export-get-all-transcoders 'test))))
1726 ;; Filters.
1727 (should
1728 (equal '((:filter-headline . my-filter))
1729 (let (org-export-registered-backends)
1730 (org-export-define-backend 'test
1731 '((headline . my-headline-test))
1732 :filters-alist '((:filter-headline . my-filter)))
1733 (org-export-backend-filters (org-export-get-backend 'test)))))
1734 ;; Options.
1735 (should
1736 (equal '((:prop value))
1737 (let (org-export-registered-backends)
1738 (org-export-define-backend 'test
1739 '((headline . my-headline-test))
1740 :options-alist '((:prop value)))
1741 (org-export-backend-options (org-export-get-backend 'test)))))
1742 ;; Menu.
1743 (should
1744 (equal '(?k "Test Export" test)
1745 (let (org-export-registered-backends)
1746 (org-export-define-backend 'test
1747 '((headline . my-headline-test))
1748 :menu-entry '(?k "Test Export" test))
1749 (org-export-backend-menu (org-export-get-backend 'test))))))
1751 (ert-deftest test-org-export/define-derived-backend ()
1752 "Test `org-export-define-derived-backend' specifications."
1753 ;; Error when parent back-end is not defined.
1754 (should-error
1755 (let (org-export-registered-backends)
1756 (org-export-define-derived-backend 'test 'parent)))
1757 ;; Append translation table to parent's.
1758 (should
1759 (equal '((:headline . test) (:headline . parent))
1760 (let (org-export-registered-backends)
1761 (org-export-define-backend 'parent '((:headline . parent)))
1762 (org-export-define-derived-backend 'test 'parent
1763 :translate-alist '((:headline . test)))
1764 (org-export-get-all-transcoders 'test))))
1765 ;; Options defined in the new back have priority over those defined
1766 ;; in parent.
1767 (should
1768 (eq 'test
1769 (let (org-export-registered-backends)
1770 (org-export-define-backend 'parent
1771 '((:headline . parent))
1772 :options-alist '((:a nil nil 'parent)))
1773 (org-export-define-derived-backend 'test 'parent
1774 :options-alist '((:a nil nil 'test)))
1775 (plist-get (org-export--get-global-options
1776 (org-export-get-backend 'test))
1777 :a)))))
1779 (ert-deftest test-org-export/derived-backend-p ()
1780 "Test `org-export-derived-backend-p' specifications."
1781 ;; Non-nil with direct match.
1782 (should
1783 (let (org-export-registered-backends)
1784 (org-export-define-backend 'test '((headline . test)))
1785 (org-export-derived-backend-p 'test 'test)))
1786 (should
1787 (let (org-export-registered-backends)
1788 (org-export-define-backend 'test '((headline . test)))
1789 (org-export-define-derived-backend 'test2 'test)
1790 (org-export-derived-backend-p 'test2 'test2)))
1791 ;; Non-nil with a direct parent.
1792 (should
1793 (let (org-export-registered-backends)
1794 (org-export-define-backend 'test '((headline . test)))
1795 (org-export-define-derived-backend 'test2 'test)
1796 (org-export-derived-backend-p 'test2 'test)))
1797 ;; Non-nil with an indirect parent.
1798 (should
1799 (let (org-export-registered-backends)
1800 (org-export-define-backend 'test '((headline . test)))
1801 (org-export-define-derived-backend 'test2 'test)
1802 (org-export-define-derived-backend 'test3 'test2)
1803 (org-export-derived-backend-p 'test3 'test)))
1804 ;; Nil otherwise.
1805 (should-not
1806 (let (org-export-registered-backends)
1807 (org-export-define-backend 'test '((headline . test)))
1808 (org-export-define-backend 'test2 '((headline . test2)))
1809 (org-export-derived-backend-p 'test2 'test)))
1810 (should-not
1811 (let (org-export-registered-backends)
1812 (org-export-define-backend 'test '((headline . test)))
1813 (org-export-define-backend 'test2 '((headline . test2)))
1814 (org-export-define-derived-backend 'test3 'test2)
1815 (org-export-derived-backend-p 'test3 'test))))
1817 (ert-deftest test-org-export/get-all-transcoders ()
1818 "Test `org-export-get-all-transcoders' specifications."
1819 ;; Return nil when back-end cannot be found.
1820 (should-not (org-export-get-all-transcoders nil))
1821 ;; Same as `org-export-transcoders' if no parent.
1822 (should
1823 (equal '((headline . ignore))
1824 (org-export-get-all-transcoders
1825 (org-export-create-backend
1826 :transcoders '((headline . ignore))))))
1827 ;; But inherit from all ancestors whenever possible.
1828 (should
1829 (equal '((section . ignore) (headline . ignore))
1830 (let (org-export-registered-backends)
1831 (org-export-define-backend 'b1 '((headline . ignore)))
1832 (org-export-get-all-transcoders
1833 (org-export-create-backend
1834 :parent 'b1 :transcoders '((section . ignore)))))))
1835 (should
1836 (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
1837 (let (org-export-registered-backends)
1838 (org-export-define-backend 'b1 '((headline . ignore)))
1839 (org-export-define-derived-backend 'b2 'b1
1840 :translate-alist '((section . ignore)))
1841 (org-export-get-all-transcoders
1842 (org-export-create-backend
1843 :parent 'b2 :transcoders '((paragraph . ignore)))))))
1844 ;; Back-end transcoders overrule inherited ones.
1845 (should
1846 (eq 'b
1847 (let (org-export-registered-backends)
1848 (org-export-define-backend 'b1 '((headline . a)))
1849 (cdr (assq 'headline
1850 (org-export-get-all-transcoders
1851 (org-export-create-backend
1852 :parent 'b1 :transcoders '((headline . b))))))))))
1854 (ert-deftest test-org-export/get-all-options ()
1855 "Test `org-export-get-all-options' specifications."
1856 ;; Return nil when back-end cannot be found.
1857 (should-not (org-export-get-all-options nil))
1858 ;; Same as `org-export-options' if no parent.
1859 (should
1860 (equal '((headline . ignore))
1861 (org-export-get-all-options
1862 (org-export-create-backend
1863 :options '((headline . ignore))))))
1864 ;; But inherit from all ancestors whenever possible.
1865 (should
1866 (equal '((:key2 value2) (:key1 value1))
1867 (let (org-export-registered-backends)
1868 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1869 (org-export-get-all-options
1870 (org-export-create-backend
1871 :parent 'b1 :options '((:key2 value2)))))))
1872 (should
1873 (equal '((:key3 value3) (:key2 value2) (:key1 value1))
1874 (let (org-export-registered-backends)
1875 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1876 (org-export-define-derived-backend 'b2 'b1
1877 :options-alist '((:key2 value2)))
1878 (org-export-get-all-options
1879 (org-export-create-backend
1880 :parent 'b2 :options '((:key3 value3)))))))
1881 ;; Back-end options overrule inherited ones.
1882 (should
1883 (eq 'b
1884 (let (org-export-registered-backends)
1885 (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
1886 (cdr (assq :key1
1887 (org-export-get-all-options
1888 (org-export-create-backend
1889 :parent 'b1 :options '((:key1 . b))))))))))
1891 (ert-deftest test-org-export/get-all-filters ()
1892 "Test `org-export-get-all-filters' specifications."
1893 ;; Return nil when back-end cannot be found.
1894 (should-not (org-export-get-all-filters nil))
1895 ;; Same as `org-export-filters' if no parent.
1896 (should
1897 (equal '((:filter-headline . ignore))
1898 (org-export-get-all-filters
1899 (org-export-create-backend
1900 :filters '((:filter-headline . ignore))))))
1901 ;; But inherit from all ancestors whenever possible.
1902 (should
1903 (equal '((:filter-section . ignore) (:filter-headline . ignore))
1904 (let (org-export-registered-backends)
1905 (org-export-define-backend 'b1
1906 nil :filters-alist '((:filter-headline . ignore)))
1907 (org-export-get-all-filters
1908 (org-export-create-backend
1909 :parent 'b1 :filters '((:filter-section . ignore)))))))
1910 (should
1911 (equal '((:filter-paragraph . ignore)
1912 (:filter-section . ignore)
1913 (:filter-headline . ignore))
1914 (let (org-export-registered-backends)
1915 (org-export-define-backend 'b1
1916 nil :filters-alist '((:filter-headline . ignore)))
1917 (org-export-define-derived-backend 'b2 'b1
1918 :filters-alist '((:filter-section . ignore)))
1919 (org-export-get-all-filters
1920 (org-export-create-backend
1921 :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
1922 ;; Back-end filters overrule inherited ones.
1923 (should
1924 (eq 'b
1925 (let (org-export-registered-backends)
1926 (org-export-define-backend 'b1 '((:filter-headline . a)))
1927 (cdr (assq :filter-headline
1928 (org-export-get-all-filters
1929 (org-export-create-backend
1930 :parent 'b1 :filters '((:filter-headline . b))))))))))
1932 (ert-deftest test-org-export/with-backend ()
1933 "Test `org-export-with-backend' definition."
1934 ;; Error when calling an undefined back-end
1935 (should-error (org-export-with-backend nil "Test"))
1936 ;; Error when called back-end doesn't have an appropriate
1937 ;; transcoder.
1938 (should-error
1939 (org-export-with-backend
1940 (org-export-create-backend :transcoders '((headline . ignore)))
1941 "Test"))
1942 ;; Otherwise, export using correct transcoder
1943 (should
1944 (equal "Success"
1945 (let (org-export-registered-backends)
1946 (org-export-define-backend 'test
1947 '((verbatim . (lambda (text contents info) "Failure"))))
1948 (org-export-define-backend 'test2
1949 '((verbatim . (lambda (text contents info) "Success"))))
1950 (org-export-with-backend 'test2 '(verbatim (:value "=Test="))))))
1951 ;; Corner case: plain-text transcoders have a different arity.
1952 (should
1953 (equal "Success"
1954 (org-export-with-backend
1955 (org-export-create-backend
1956 :transcoders '((plain-text . (lambda (text info) "Success"))))
1957 "Test")))
1958 ;; Provide correct back-end if transcoder needs to use recursive
1959 ;; calls.
1960 (should
1961 (equal "Success\n"
1962 (let ((test-back-end
1963 (org-export-create-backend
1964 :transcoders
1965 (list (cons 'headline
1966 (lambda (headline contents info)
1967 (org-export-data
1968 (org-element-property :title headline)
1969 info)))
1970 (cons 'plain-text (lambda (text info) "Success"))))))
1971 (org-export-string-as
1972 "* Test"
1973 (org-export-create-backend
1974 :transcoders
1975 (list (cons 'headline
1976 (lambda (headline contents info)
1977 (org-export-with-backend
1978 test-back-end headline contents info))))))))))
1980 (ert-deftest test-org-export/data-with-backend ()
1981 "Test `org-export-data-with-backend' specifications."
1982 ;; Error when calling an undefined back-end.
1983 (should-error (org-export-data-with-backend nil "nil" nil))
1984 ;; Otherwise, export data recursively, using correct back-end.
1985 (should
1986 (equal
1987 "Success!"
1988 (org-export-data-with-backend
1989 '(bold nil "Test")
1990 (org-export-create-backend
1991 :transcoders
1992 '((plain-text . (lambda (text info) "Success"))
1993 (bold . (lambda (bold contents info) (concat contents "!")))))
1994 '(:with-emphasize t)))))
1998 ;;; Comments
2000 (ert-deftest test-org-export/comments ()
2001 "Test comments handling during export.
2002 In particular, structure of the document mustn't be altered after
2003 comments removal."
2004 (should
2005 (equal "Para1\n\nPara2\n"
2006 (org-test-with-temp-text "
2007 Para1
2008 # Comment
2010 # Comment
2011 Para2"
2012 (org-export-as (org-test-default-backend)))))
2013 (should
2014 (equal "Para1\n\nPara2\n"
2015 (org-test-with-temp-text "
2016 Para1
2017 # Comment
2018 Para2"
2019 (org-export-as (org-test-default-backend)))))
2020 (should
2021 (equal "[fn:1] Para1\n\n\nPara2\n"
2022 (org-test-with-temp-text "
2023 \[fn:1] Para1
2024 # Inside definition
2027 # Outside definition
2028 Para2"
2029 (org-export-as (org-test-default-backend)))))
2030 (should
2031 (equal "[fn:1] Para1\n\nPara2\n"
2032 (org-test-with-temp-text "
2033 \[fn:1] Para1
2035 # Inside definition
2037 # Inside definition
2039 Para2"
2040 (org-export-as (org-test-default-backend)))))
2041 (should
2042 (equal "[fn:1] Para1\n\nPara2\n"
2043 (org-test-with-temp-text "
2044 \[fn:1] Para1
2045 # Inside definition
2047 Para2"
2048 (org-export-as (org-test-default-backend)))))
2049 (should
2050 (equal "[fn:1] Para1\n\nPara2\n"
2051 (org-test-with-temp-text "
2052 \[fn:1] Para1
2054 # Inside definition
2055 Para2"
2056 (org-export-as (org-test-default-backend)))))
2057 (should
2058 (equal "- item 1\n\n- item 2\n"
2059 (org-test-with-temp-text "
2060 - item 1
2062 # Comment
2064 - item 2"
2065 (org-export-as (org-test-default-backend))))))
2069 ;;; Export blocks
2071 (ert-deftest test-org-export/export-block ()
2072 "Test export blocks transcoding."
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 '((export-block . (lambda (b _c _i)
2080 (org-element-property :value b)))
2081 (section . (lambda (_s c _i) c))))))))
2082 (should
2083 (equal "Success!\n"
2084 (org-test-with-temp-text
2085 "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
2086 (org-export-as
2087 (org-export-create-backend
2088 :transcoders
2089 (list
2090 (cons 'export-block
2091 (lambda (b _c _i)
2092 (and (equal (org-element-property :type b) "BACKEND")
2093 (org-element-property :value b))))
2094 (cons 'section (lambda (_s c _i) c)))))))))
2097 ;;; Export Snippets
2099 (ert-deftest test-org-export/export-snippet ()
2100 "Test export snippets transcoding."
2101 ;; Standard test.
2102 (org-test-with-temp-text "@@test:A@@@@t:B@@"
2103 (let ((backend (org-test-default-backend)))
2104 (setf (org-export-backend-name backend) 'test)
2105 (setf (org-export-backend-transcoders backend)
2106 (cons (cons 'export-snippet
2107 (lambda (snippet contents info)
2108 (when (eq (org-export-snippet-backend snippet) 'test)
2109 (org-element-property :value snippet))))
2110 (org-export-backend-transcoders backend)))
2111 (let ((org-export-snippet-translation-alist nil))
2112 (should (equal (org-export-as backend) "A\n")))
2113 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
2114 (should (equal (org-export-as backend) "AB\n")))))
2115 ;; Ignored export snippets do not remove any blank.
2116 (should
2117 (equal "begin end\n"
2118 (org-test-with-parsed-data "begin @@test:A@@ end"
2119 (org-export-data-with-backend
2120 tree
2121 (org-export-create-backend
2122 :transcoders
2123 '((paragraph . (lambda (paragraph contents info) contents))
2124 (section . (lambda (section contents info) contents))))
2125 info)))))
2128 ;;; Filters
2130 (ert-deftest test-org-export/filter-apply-functions ()
2131 "Test `org-export-filter-apply-functions' specifications."
2132 ;; Functions are applied in order and return values are reduced.
2133 (should
2134 (equal "210"
2135 (org-export-filter-apply-functions
2136 (list (lambda (value &rest _) (concat "1" value))
2137 (lambda (value &rest _) (concat "2" value)))
2138 "0" nil)))
2139 ;; Functions returning nil are skipped.
2140 (should
2141 (equal "20"
2142 (org-export-filter-apply-functions
2143 (list #'ignore (lambda (value &rest _) (concat "2" value)))
2144 "0" nil)))
2145 ;; If all functions are skipped, return the initial value.
2146 (should
2147 (equal "0"
2148 (org-export-filter-apply-functions (list #'ignore) "0" nil)))
2149 ;; If any function returns the empty string, final value is the
2150 ;; empty string.
2151 (should
2152 (equal ""
2153 (org-export-filter-apply-functions
2154 (list (lambda (value &rest _) "")
2155 (lambda (value &rest _) (concat "2" value)))
2156 "0" nil)))
2157 ;; Any function returning the empty string short-circuits the
2158 ;; process.
2159 (should
2160 (org-export-filter-apply-functions
2161 (list (lambda (value &rest _) "")
2162 (lambda (value &rest _) (error "This shouldn't happen")))
2163 "0" nil)))
2166 ;;; Footnotes
2168 (ert-deftest test-org-export/footnote-first-reference-p ()
2169 "Test `org-export-footnote-first-reference-p' specifications."
2170 (should
2171 (equal
2172 '(t nil)
2173 (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
2174 (let (result)
2175 (org-export-as
2176 (org-export-create-backend
2177 :transcoders
2178 `(,(cons 'footnote-reference
2179 (lambda (f c i)
2180 (push (org-export-footnote-first-reference-p f i)
2181 result)
2182 ""))
2183 (section . (lambda (s c i) c))
2184 (paragraph . (lambda (p c i) c))))
2185 nil nil nil '(:with-footnotes t))
2186 (nreverse result)))))
2187 ;; Limit check to DATA, when non-nil.
2188 (should
2189 (equal
2190 '(nil t)
2191 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
2192 (let (result)
2193 (org-element-map tree 'footnote-reference
2194 (lambda (ref)
2195 (push
2196 (org-export-footnote-first-reference-p
2197 ref info (org-element-map tree 'headline #'identity info t))
2198 result))
2199 info)
2200 (nreverse result)))))
2201 (should
2202 (equal
2203 '(t nil)
2204 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
2205 (let (result)
2206 (org-element-map tree 'footnote-reference
2207 (lambda (ref)
2208 (push (org-export-footnote-first-reference-p ref info) result))
2209 info)
2210 (nreverse result)))))
2211 ;; If optional argument BODY-FIRST is non-nil, first find footnote
2212 ;; in the main body of the document. Otherwise, enter footnote
2213 ;; definitions when they are encountered.
2214 (should
2215 (equal
2216 '(t nil)
2217 (org-test-with-temp-text
2218 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
2219 (let (result)
2220 (org-export-as
2221 (org-export-create-backend
2222 :transcoders
2223 `(,(cons 'footnote-reference
2224 (lambda (f c i)
2225 (when (org-element-lineage f '(drawer))
2226 (push (org-export-footnote-first-reference-p f i nil)
2227 result))
2228 ""))
2229 (drawer . (lambda (d c i) c))
2230 (footnote-definition . (lambda (d c i) c))
2231 (section . (lambda (s c i) c))
2232 (paragraph . (lambda (p c i) c))))
2233 nil nil nil '(:with-footnotes t))
2234 (nreverse result)))))
2235 (should
2236 (equal
2237 '(t t)
2238 (org-test-with-temp-text
2239 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
2240 (let (result)
2241 (org-export-as
2242 (org-export-create-backend
2243 :transcoders
2244 `(,(cons 'footnote-reference
2245 (lambda (f c i)
2246 (when (org-element-lineage f '(drawer))
2247 (push (org-export-footnote-first-reference-p f i nil t)
2248 result))
2249 ""))
2250 (drawer . (lambda (d c i) c))
2251 (footnote-definition . (lambda (d c i) c))
2252 (section . (lambda (s c i) c))
2253 (paragraph . (lambda (p c i) c))))
2254 nil nil nil '(:with-footnotes t))
2255 (nreverse result))))))
2257 (ert-deftest test-org-export/get-footnote-number ()
2258 "Test `org-export-get-footnote-number' specifications."
2259 (should
2260 (equal '(1 2 1)
2261 (org-test-with-parsed-data
2262 "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
2263 (org-element-map tree 'footnote-reference
2264 (lambda (ref) (org-export-get-footnote-number ref info))
2265 info))))
2266 ;; Anonymous footnotes all get a new number.
2267 (should
2268 (equal '(1 2)
2269 (org-test-with-parsed-data
2270 "Text[fn::anon1][fn::anon2]"
2271 (org-element-map tree 'footnote-reference
2272 (lambda (ref) (org-export-get-footnote-number ref info))
2273 info))))
2274 ;; Test nested footnotes order.
2275 (should
2276 (equal
2277 '((1 . "1") (2 . "2") (3 . "3") (3 . "3") (4))
2278 (org-test-with-parsed-data
2279 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
2280 (org-element-map tree 'footnote-reference
2281 (lambda (ref)
2282 (cons (org-export-get-footnote-number ref info)
2283 (org-element-property :label ref)))
2284 info))))
2285 ;; Limit number to provided DATA, when non-nil.
2286 (should
2287 (equal
2288 '(1)
2289 (org-test-with-parsed-data
2290 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2291 (org-element-map tree 'footnote-reference
2292 (lambda (ref)
2293 (org-export-get-footnote-number
2294 ref info (org-element-map tree 'headline #'identity info t)))
2295 info))))
2296 (should
2297 (equal
2298 '(1 2)
2299 (org-test-with-parsed-data
2300 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
2301 (org-element-map tree 'footnote-reference
2302 (lambda (ref) (org-export-get-footnote-number ref info))
2303 info))))
2304 ;; With a non-nil BODY-FIRST optional argument, first check body,
2305 ;; then footnote definitions.
2306 (should
2307 (equal
2308 '(("1" . 1) ("2" . 2) ("3" . 3) ("3" . 3))
2309 (org-test-with-parsed-data
2310 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
2311 (org-element-map tree 'footnote-reference
2312 (lambda (ref)
2313 (cons (org-element-property :label ref)
2314 (org-export-get-footnote-number ref info nil t)))
2315 info))))
2316 (should
2317 (equal
2318 '(("1" . 1) ("2" . 3) ("3" . 2) ("3" . 2))
2319 (org-test-with-parsed-data
2320 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
2321 (org-element-map tree 'footnote-reference
2322 (lambda (ref)
2323 (cons (org-element-property :label ref)
2324 (org-export-get-footnote-number ref info nil)))
2325 info)))))
2327 (ert-deftest test-org-export/get-footnote-definition ()
2328 "Test `org-export-get-footnote-definition' specifications."
2329 ;; Standard test.
2330 (should
2331 (equal "A\n"
2332 (org-element-interpret-data
2333 (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1] A"
2334 (org-export-get-footnote-definition
2335 (org-element-map tree 'footnote-reference #'identity nil t)
2336 info)))))
2337 ;; Raise an error if no definition is found.
2338 (should-error
2339 (org-test-with-parsed-data "Text[fn:1]"
2340 (org-export-get-footnote-definition
2341 (org-element-map tree 'footnote-reference #'identity nil t)
2342 info)))
2343 ;; Find inline definitions.
2344 (should
2345 (equal "A"
2346 (org-element-interpret-data
2347 (org-test-with-parsed-data "Text[fn:1:A]"
2348 (org-export-get-footnote-definition
2349 (org-element-map tree 'footnote-reference #'identity nil t)
2350 info)))))
2351 ;; Find anonymous definitions.
2352 (should
2353 (equal "A"
2354 (org-element-interpret-data
2355 (org-test-with-parsed-data "Text[fn::A]"
2356 (org-export-get-footnote-definition
2357 (org-element-map tree 'footnote-reference #'identity nil t)
2358 info)))))
2359 ;; Find empty definitions.
2360 (should
2361 (equal ""
2362 (org-element-interpret-data
2363 (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1]"
2364 (org-export-get-footnote-definition
2365 (org-element-map tree 'footnote-reference #'identity nil t)
2366 info)))))
2367 (should
2368 (equal ""
2369 (org-element-interpret-data
2370 (org-test-with-parsed-data "Text[fn:1:]"
2371 (org-export-get-footnote-definition
2372 (org-element-map tree 'footnote-reference #'identity nil t)
2373 info)))))
2374 (should
2375 (equal ""
2376 (org-element-interpret-data
2377 (org-test-with-parsed-data "Text[fn::]"
2378 (org-export-get-footnote-definition
2379 (org-element-map tree 'footnote-reference #'identity nil t)
2380 info))))))
2382 (ert-deftest test-org-export/collect-footnote-definitions ()
2383 "Test `org-export-collect-footnote-definitions' specifications."
2384 (should
2385 (= 4
2386 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2388 \[fn:2] B [fn:3] [fn::D].
2390 \[fn:3] C."
2391 (length (org-export-collect-footnote-definitions info)))))
2392 ;; Limit number to provided DATA, when non-nil.
2393 (should
2394 (equal
2395 '((1 "2"))
2396 (org-test-with-parsed-data
2397 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2398 (mapcar #'butlast
2399 (org-export-collect-footnote-definitions
2400 info (org-element-map tree 'headline #'identity info t))))))
2401 (should
2402 (equal
2403 '((1 "1") (2 "2"))
2404 (org-test-with-parsed-data
2405 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2406 (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
2407 ;; With optional argument BODY-FIRST, first check body, then
2408 ;; footnote definitions.
2409 (should
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 nil t)))))
2418 (should-not
2419 (equal '("1" "3" "2" nil)
2420 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2422 \[fn:2] B [fn:3] [fn::D].
2424 \[fn:3] C."
2425 (mapcar (lambda (e) (nth 1 e))
2426 (org-export-collect-footnote-definitions info))))))
2428 (ert-deftest test-org-export/footnotes ()
2429 "Miscellaneous tests on footnotes."
2430 (let ((org-footnote-section nil)
2431 (org-export-with-footnotes t))
2432 ;; Read every type of footnote.
2433 (should
2434 (equal
2435 '((1 . "A\n") (2 . "C") (3 . "D"))
2436 (org-test-with-parsed-data
2437 "Text[fn:1] [fn:label:C] [fn::D]\n\n[fn:1] A\n"
2438 (org-element-map tree 'footnote-reference
2439 (lambda (ref)
2440 (let ((def (org-export-get-footnote-definition ref info)))
2441 (cons (org-export-get-footnote-number ref info)
2442 (if (eq (org-element-property :type ref) 'inline) (car def)
2443 (car (org-element-contents
2444 (car (org-element-contents def))))))))
2445 info))))
2446 ;; Export nested footnote in invisible definitions.
2447 (should
2448 (= 2
2449 (org-test-with-temp-text "Text[fn:1]\n\n[fn:1] B [fn:2]\n\n[fn:2] C."
2450 (narrow-to-region (point) (line-end-position))
2451 (catch 'exit
2452 (org-export-as
2453 (org-export-create-backend
2454 :transcoders
2455 '((section
2457 (lambda (s c i)
2458 (throw 'exit (length
2459 (org-export-collect-footnote-definitions
2460 i))))))))))))
2461 ;; Export footnotes defined outside parsing scope.
2462 (should
2463 (string-match
2464 "Out of scope"
2465 (org-test-with-temp-text "[fn:1] Out of scope
2466 * Title
2467 <point>Paragraph[fn:1]"
2468 (org-export-as (org-test-default-backend) 'subtree))))
2469 (should
2470 (string-match
2471 "Out of scope"
2472 (org-test-with-temp-text "[fn:1] Out of scope
2473 * Title
2474 <point>Paragraph[fn:1]"
2475 (narrow-to-region (point) (point-max))
2476 (org-export-as (org-test-default-backend)))))
2477 ;; Export nested footnotes defined outside parsing scope.
2478 (should
2479 (string-match
2480 "Very out of scope"
2481 (org-test-with-temp-text "
2482 \[fn:1] Out of scope[fn:2]
2484 \[fn:2] Very out of scope
2485 * Title
2486 <point>Paragraph[fn:1]"
2487 (org-export-as (org-test-default-backend) 'subtree))))
2488 (should
2489 (string-match
2490 "Very out of scope"
2491 (org-test-with-temp-text "
2492 \[fn:1] Out of scope[fn:2]
2494 \[fn:2] Very out of scope
2495 * Title
2496 <point>Paragraph[fn:1]"
2497 (narrow-to-region (point) (point-max))
2498 (org-export-as (org-test-default-backend)))))
2499 (should
2500 (string-match
2501 "D2"
2502 (org-test-with-temp-text "
2503 \[fn:1] Out of scope[fn:2:D2]
2504 * Title
2505 <point>Paragraph[fn:1]"
2506 (narrow-to-region (point) (point-max))
2507 (org-export-as (org-test-default-backend)))))
2508 ;; Export footnotes in pruned parts of tree.
2509 (should
2510 (string-match
2511 "Definition"
2512 (let ((org-export-exclude-tags '("noexport")))
2513 (org-test-with-temp-text
2514 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] Definition"
2515 (org-export-as (org-test-default-backend))))))
2516 (should
2517 (string-match
2518 "Definition"
2519 (let ((org-export-select-tags '("export")))
2520 (org-test-with-temp-text
2521 "* H :export:\nText[fn:1]\n* H2\n[fn:1] Definition"
2522 (org-export-as (org-test-default-backend))))))
2523 ;; Export nested footnotes in pruned parts of tree.
2524 (should
2525 (string-match
2526 "D2"
2527 (let ((org-export-exclude-tags '("noexport")))
2528 (org-test-with-temp-text
2529 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
2530 (org-export-as (org-test-default-backend))))))
2531 (should
2532 (string-match
2533 "D2"
2534 (let ((org-export-select-tags '("export")))
2535 (org-test-with-temp-text
2536 "* H :export:\nText[fn:1]\n* H2\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
2537 (org-export-as (org-test-default-backend))))))
2538 ;; Handle uninterpreted data in pruned footnote definitions.
2539 (should-not
2540 (string-match
2542 (let ((org-export-with-tables nil))
2543 (org-test-with-temp-text
2544 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1]\n| a |"
2545 (org-export-as (org-test-default-backend))))))
2546 ;; Footnotes without a definition should throw an error.
2547 (should-error
2548 (org-test-with-parsed-data "Text[fn:1]"
2549 (org-export-get-footnote-definition
2550 (org-element-map tree 'footnote-reference #'identity info t) info)))
2551 ;; Footnote section should be ignored in TOC and in headlines
2552 ;; numbering.
2553 (should
2554 (= 1 (let ((org-footnote-section "Footnotes"))
2555 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
2556 (org-export-collect-headlines info))))))
2557 (should
2558 (equal '(2)
2559 (let ((org-footnote-section "Footnotes"))
2560 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
2561 (org-element-map tree 'headline
2562 (lambda (hl)
2563 (when (equal (org-element-property :raw-value hl) "H2")
2564 (org-export-get-headline-number hl info)))
2565 info t)))))))
2569 ;;; Headlines and Inlinetasks
2571 (ert-deftest test-org-export/get-relative-level ()
2572 "Test `org-export-get-relative-level' specifications."
2573 ;; Standard test.
2574 (should
2575 (equal '(1 2)
2576 (let ((org-odd-levels-only nil))
2577 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2578 (org-element-map tree 'headline
2579 (lambda (h) (org-export-get-relative-level h info))
2580 info)))))
2581 ;; Missing levels
2582 (should
2583 (equal '(1 3)
2584 (let ((org-odd-levels-only nil))
2585 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
2586 (org-element-map tree 'headline
2587 (lambda (h) (org-export-get-relative-level h info))
2588 info))))))
2590 (ert-deftest test-org-export/low-level-p ()
2591 "Test `org-export-low-level-p' specifications."
2592 (should
2593 (equal
2594 '(no yes)
2595 (let ((org-odd-levels-only nil))
2596 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2597 (org-element-map tree 'headline
2598 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
2599 (plist-put info :headline-levels 1)))))))
2601 (ert-deftest test-org-export/get-headline-number ()
2602 "Test `org-export-get-headline-number' specifications."
2603 ;; Standard test.
2604 (should
2605 (equal
2606 '((1) (1 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)))))
2612 ;; Missing levels are replaced with 0.
2613 (should
2614 (equal
2615 '((1) (1 0 1))
2616 (let ((org-odd-levels-only nil))
2617 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
2618 (org-element-map tree 'headline
2619 (lambda (h) (org-export-get-headline-number h info))
2620 info))))))
2622 (ert-deftest test-org-export/numbered-headline-p ()
2623 "Test `org-export-numbered-headline-p' specifications."
2624 ;; If `:section-numbers' is nil, never number headlines.
2625 (should-not
2626 (org-test-with-parsed-data "* Headline"
2627 (org-element-map tree 'headline
2628 (lambda (h) (org-export-numbered-headline-p h info))
2629 (plist-put info :section-numbers nil))))
2630 ;; If `:section-numbers' is a number, only number headlines with
2631 ;; a level greater that it.
2632 (should
2633 (equal
2634 '(yes no)
2635 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2636 (org-element-map tree 'headline
2637 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
2638 (plist-put info :section-numbers 1)))))
2639 ;; Otherwise, headlines are always numbered.
2640 (should
2641 (org-test-with-parsed-data "* Headline"
2642 (org-element-map tree 'headline
2643 (lambda (h) (org-export-numbered-headline-p h info))
2644 (plist-put info :section-numbers t))))
2645 ;; With #+OPTIONS: num:nil all headlines are unnumbered.
2646 (should-not
2647 (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
2648 (org-export-numbered-headline-p
2649 (org-element-map tree 'headline 'identity info t)
2650 info)))
2651 ;; Headlines with a non-nil UNNUMBERED property are not numbered.
2652 (should-not
2653 (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
2654 (org-export-numbered-headline-p
2655 (org-element-map tree 'headline #'identity info t)
2656 info)))
2657 ;; UNNUMBERED is inherited.
2658 (should
2659 (equal '(unnumbered numbered unnumbered)
2660 (org-test-with-parsed-data
2661 "* H
2662 :PROPERTIES:
2663 :UNNUMBERED: t
2664 :END:
2665 ** H2
2666 :PROPERTIES:
2667 :UNNUMBERED: nil
2668 :END:
2669 ** H3"
2670 (org-element-map tree 'headline
2671 (lambda (h)
2672 (if (org-export-numbered-headline-p h info) 'numbered
2673 'unnumbered))
2674 info)))))
2676 (ert-deftest test-org-export/number-to-roman ()
2677 "Test `org-export-number-to-roman' specifications."
2678 ;; If number is negative, return it as a string.
2679 (should (equal (org-export-number-to-roman -1) "-1"))
2680 ;; Otherwise, return it as a roman number.
2681 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
2683 (ert-deftest test-org-export/get-optional-title ()
2684 "Test `org-export-get-alt-title' specifications."
2685 ;; If ALT_TITLE property is defined, use it.
2686 (should
2687 (equal '("opt")
2688 (org-test-with-parsed-data
2689 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
2690 (org-export-get-alt-title
2691 (org-element-map tree 'headline 'identity info t)
2692 info))))
2693 ;; Otherwise, fall-back to regular title.
2694 (should
2695 (equal '("Headline")
2696 (org-test-with-parsed-data "* Headline"
2697 (org-export-get-alt-title
2698 (org-element-map tree 'headline 'identity info t)
2699 info)))))
2701 (ert-deftest test-org-export/get-tags ()
2702 "Test `org-export-get-tags' specifications."
2703 ;; Standard test: tags which are not a select tag, an exclude tag,
2704 ;; or specified as optional argument shouldn't be ignored.
2705 (should
2706 (org-test-with-parsed-data "* Headline :tag:"
2707 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2708 info)))
2709 ;; Tags provided in the optional argument are ignored.
2710 (should-not
2711 (org-test-with-parsed-data "* Headline :ignore:"
2712 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2713 info '("ignore"))))
2714 ;; Allow tag inheritance.
2715 (should
2716 (equal
2717 '(("tag") ("tag"))
2718 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
2719 (org-element-map tree 'headline
2720 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
2721 ;; Tag inheritance checks FILETAGS keywords.
2722 (should
2723 (equal
2724 '(("a" "b" "tag"))
2725 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
2726 (org-element-map tree 'headline
2727 (lambda (hl) (org-export-get-tags hl info nil t)) info)))))
2729 (ert-deftest test-org-export/get-node-property ()
2730 "Test`org-export-get-node-property' specifications."
2731 ;; Standard test.
2732 (should
2733 (equal "value"
2734 (org-test-with-parsed-data "* Headline
2735 :PROPERTIES:
2736 :prop: value
2737 :END:"
2738 (org-export-get-node-property
2739 :PROP (org-element-map tree 'headline 'identity nil t)))))
2740 ;; Test inheritance.
2741 (should
2742 (equal "value"
2743 (org-test-with-parsed-data "* Parent
2744 :PROPERTIES:
2745 :prop: value
2746 :END:
2747 ** Headline
2748 Paragraph"
2749 (org-export-get-node-property
2750 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
2751 ;; Cannot return a value before the first headline.
2752 (should-not
2753 (org-test-with-parsed-data "Paragraph
2754 * Headline
2755 :PROPERTIES:
2756 :prop: value
2757 :END:"
2758 (org-export-get-node-property
2759 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
2761 (ert-deftest test-org-export/get-category ()
2762 "Test `org-export-get-category' specifications."
2763 ;; Standard test.
2764 (should
2765 (equal "value"
2766 (org-test-with-parsed-data "* Headline
2767 :PROPERTIES:
2768 :CATEGORY: value
2769 :END:"
2770 (org-export-get-category
2771 (org-element-map tree 'headline 'identity nil t) info))))
2772 ;; Test inheritance from a parent headline.
2773 (should
2774 (equal '("value" "value")
2775 (org-test-with-parsed-data "* Headline1
2776 :PROPERTIES:
2777 :CATEGORY: value
2778 :END:
2779 ** Headline2"
2780 (org-element-map tree 'headline
2781 (lambda (hl) (org-export-get-category hl info)) info))))
2782 ;; Test inheritance from #+CATEGORY keyword
2783 (should
2784 (equal "value"
2785 (org-test-with-parsed-data "#+CATEGORY: value
2786 * Headline"
2787 (org-export-get-category
2788 (org-element-map tree 'headline 'identity nil t) info))))
2789 ;; Test inheritance from file name.
2790 (should
2791 (equal "test"
2792 (org-test-with-parsed-data "* Headline"
2793 (let ((info (plist-put info :input-file "~/test.org")))
2794 (org-export-get-category
2795 (org-element-map tree 'headline 'identity nil t) info)))))
2796 ;; Fall-back value.
2797 (should
2798 (equal "???"
2799 (org-test-with-parsed-data "* Headline"
2800 (org-export-get-category
2801 (org-element-map tree 'headline 'identity nil t) info)))))
2803 (ert-deftest test-org-export/first-sibling-p ()
2804 "Test `org-export-first-sibling-p' specifications."
2805 ;; Standard test.
2806 (should
2807 (equal
2808 '(yes yes no)
2809 (org-test-with-parsed-data "* H\n** H 2\n** H 3"
2810 (org-element-map tree 'headline
2811 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2812 info))))
2813 (should
2814 (equal '(yes no)
2815 (org-test-with-parsed-data "- item\n\n para"
2816 (org-element-map tree 'paragraph
2817 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2818 info))))
2819 ;; Ignore sections for headlines.
2820 (should
2821 (equal '(yes yes)
2822 (org-test-with-parsed-data "* H\nSection\n** H 2"
2823 (org-element-map tree 'headline
2824 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2825 info))))
2826 ;; Ignore headlines not exported.
2827 (should
2828 (equal
2829 '(yes)
2830 (let ((org-export-exclude-tags '("ignore")))
2831 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
2832 (org-element-map tree 'headline
2833 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2834 info))))))
2836 (ert-deftest test-org-export/last-sibling-p ()
2837 "Test `org-export-last-sibling-p' specifications."
2838 ;; Standard test.
2839 (should
2840 (equal
2841 '(yes no yes)
2842 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
2843 (org-element-map tree 'headline
2844 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2845 info))))
2846 (should
2847 (equal '(no yes)
2848 (org-test-with-parsed-data "- item\n\n para"
2849 (org-element-map tree 'paragraph
2850 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2851 info))))
2852 ;; Ignore headlines not exported.
2853 (should
2854 (equal
2855 '(yes)
2856 (let ((org-export-exclude-tags '("ignore")))
2857 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
2858 (org-element-map tree 'headline
2859 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2860 info)))))
2861 ;; Handle gracefully discontinuous headings.
2862 (should
2863 (equal '(yes yes)
2864 (org-test-with-parsed-data "** S\n* H"
2865 (org-element-map tree 'headline
2866 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no)))))))
2868 (ert-deftest test-org-export/handle-inlinetasks ()
2869 "Test inlinetask export."
2870 ;; Inlinetask with an exclude tag.
2871 (when (featurep 'org-inlinetask)
2872 (should
2873 (equal
2875 (let ((org-inlinetask-min-level 3)
2876 org-export-filter-body-functions
2877 org-export-filter-final-output-functions)
2878 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
2879 (org-export-as (org-test-default-backend)
2880 nil nil nil '(:exclude-tags ("noexp")))))))
2881 ;; Inlinetask with an include tag.
2882 (should
2883 (equal
2884 "* H2\n*** Inline :exp:\n"
2885 (let ((org-inlinetask-min-level 3)
2886 (org-tags-column 0))
2887 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
2888 (org-export-as (org-test-default-backend)
2889 nil nil nil '(:select-tags ("exp")))))))
2890 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
2891 (should
2892 (equal ""
2893 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
2894 (org-inlinetask-min-level 3)
2895 org-export-filter-body-functions
2896 org-export-filter-final-output-functions)
2897 (org-test-with-temp-text "*** TODO Inline"
2898 (org-export-as (org-test-default-backend)
2899 nil nil nil '(:with-tasks nil))))))))
2903 ;;; Keywords
2905 (ert-deftest test-org-export/get-date ()
2906 "Test `org-export-get-date' specifications."
2907 ;; Return a properly formatted string when
2908 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
2909 ;; consists in a single timestamp.
2910 (should
2911 (equal "29 03 2012"
2912 (let ((org-export-date-timestamp-format "%d %m %Y"))
2913 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2914 (org-export-get-date info)))))
2915 ;; Return a secondary string otherwise.
2916 (should-not
2917 (stringp
2918 (let ((org-export-date-timestamp-format nil))
2919 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2920 (org-export-get-date info)))))
2921 (should
2922 (equal '("Date")
2923 (org-test-with-parsed-data "#+DATE: Date"
2924 (org-export-get-date info))))
2925 ;; Optional argument has precedence over
2926 ;; `org-export-date-timestamp-format'.
2927 (should
2928 (equal "29 03"
2929 (let ((org-export-date-timestamp-format "%d %m %Y"))
2930 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2931 (org-export-get-date info "%d %m"))))))
2935 ;;; Links
2937 (ert-deftest test-org-export/custom-protocol-maybe ()
2938 "Test `org-export-custom-protocol-maybe' specifications."
2939 (should
2940 (string-match
2941 "success"
2942 (progn
2943 (org-link-set-parameters "foo" :export (lambda (p d f) "success"))
2944 (org-export-string-as
2945 "[[foo:path]]"
2946 (org-export-create-backend
2947 :name 'test
2948 :transcoders
2949 '((section . (lambda (s c i) c))
2950 (paragraph . (lambda (p c i) c))
2951 (link . (lambda (l c i)
2952 (or (org-export-custom-protocol-maybe l c 'test)
2953 "failure")))))))))
2954 (should-not
2955 (string-match
2956 "success"
2957 (progn
2958 (org-link-set-parameters
2959 "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
2960 (org-export-string-as
2961 "[[foo:path]]"
2962 (org-export-create-backend
2963 :name 'no-test
2964 :transcoders
2965 '((section . (lambda (s c i) c))
2966 (paragraph . (lambda (p c i) c))
2967 (link . (lambda (l c i)
2968 (or (org-export-custom-protocol-maybe l c 'no-test)
2969 "failure")))))))))
2970 ;; Ignore anonymous back-ends.
2971 (should-not
2972 (string-match
2973 "success"
2974 (progn
2975 (org-link-set-parameters
2976 "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
2977 (org-export-string-as
2978 "[[foo:path]]"
2979 (org-export-create-backend
2980 :transcoders
2981 '((section . (lambda (s c i) c))
2982 (paragraph . (lambda (p c i) c))
2983 (link . (lambda (l c i)
2984 (or (org-export-custom-protocol-maybe l c nil)
2985 "failure"))))))))))
2987 (ert-deftest test-org-export/get-coderef-format ()
2988 "Test `org-export-get-coderef-format' specifications."
2989 ;; A link without description returns "%s"
2990 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
2991 "%s"))
2992 ;; Return "%s" when path is matched within description.
2993 (should (equal (org-export-get-coderef-format "path" "desc (path)")
2994 "desc %s"))
2995 ;; Otherwise return description.
2996 (should (equal (org-export-get-coderef-format "path" "desc")
2997 "desc")))
2999 (ert-deftest test-org-export/inline-image-p ()
3000 "Test `org-export-inline-image-p' specifications."
3001 (should
3002 (org-export-inline-image-p
3003 (org-test-with-temp-text "[[#id]]"
3004 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
3005 '(("custom-id" . "id")))))
3007 (ert-deftest test-org-export/insert-image-links ()
3008 "Test `org-export-insert-image-links' specifications."
3009 (should-not
3010 (member "file"
3011 (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
3012 (org-element-map tree 'link
3013 (lambda (l) (org-element-property :type l))))))
3014 (should
3015 (member "file"
3016 (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
3017 (org-element-map (org-export-insert-image-links tree info) 'link
3018 (lambda (l) (org-element-property :type l))))))
3019 ;; Properly set `:parent' property when replace contents with image
3020 ;; link.
3021 (should
3022 (memq 'link
3023 (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
3024 (org-element-map (org-export-insert-image-links tree info) 'link
3025 (lambda (l)
3026 (org-element-type (org-element-property :parent l)))))))
3027 ;; With optional argument RULES, recognize different links as
3028 ;; images.
3029 (should-not
3030 (member "file"
3031 (org-test-with-parsed-data "[[https://orgmode.org][file:image.xxx]]"
3032 (org-element-map (org-export-insert-image-links tree info) 'link
3033 (lambda (l) (org-element-property :type l))))))
3034 (should
3035 (member "file"
3036 (org-test-with-parsed-data "[[https://orgmode.org][file:image.xxx]]"
3037 (org-element-map
3038 (org-export-insert-image-links tree info '(("file" . "xxx")))
3039 'link
3040 (lambda (l) (org-element-property :type l)))))))
3042 (ert-deftest test-org-export/fuzzy-link ()
3043 "Test fuzzy links specifications."
3044 ;; Link to an headline should return headline's number.
3045 (should
3046 ;; Note: Headline's number is in fact a list of numbers.
3047 (equal '(2)
3048 (org-test-with-parsed-data
3049 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
3050 (org-element-map tree 'link
3051 (lambda (link)
3052 (org-export-get-ordinal
3053 (org-export-resolve-fuzzy-link link info) info)) info t))))
3054 ;; Link to a target in an item should return item's number.
3055 (should
3056 ;; Note: Item's number is in fact a list of numbers.
3057 (equal '(1 2)
3058 (org-test-with-parsed-data
3059 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
3060 (org-element-map tree 'link
3061 (lambda (link)
3062 (org-export-get-ordinal
3063 (org-export-resolve-fuzzy-link link info) info)) info t))))
3064 ;; Link to a target in a footnote should return footnote's number.
3065 (should
3066 (equal '(2 3)
3067 (org-test-with-parsed-data "
3068 Paragraph[fn:1][fn:2][fn:lbl3:C<<target>>][[test]][[target]]
3069 \[fn:1] A
3071 \[fn:2] <<test>>B"
3072 (org-element-map tree 'link
3073 (lambda (link)
3074 (org-export-get-ordinal
3075 (org-export-resolve-fuzzy-link link info) info)) info))))
3076 ;; Link to a named element should return sequence number of that
3077 ;; element.
3078 (should
3079 (= 2
3080 (org-test-with-parsed-data
3081 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
3082 (org-element-map tree 'link
3083 (lambda (link)
3084 (org-export-get-ordinal
3085 (org-export-resolve-fuzzy-link link info) info)) info t))))
3086 ;; Link to a target not within an item, a table, a footnote
3087 ;; reference or definition should return section number.
3088 (should
3089 (equal '(2)
3090 (org-test-with-parsed-data
3091 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
3092 (org-element-map tree 'link
3093 (lambda (link)
3094 (org-export-get-ordinal
3095 (org-export-resolve-fuzzy-link link info) info)) info t))))
3096 ;; Space are not significant when matching a fuzzy link.
3097 (should
3098 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
3099 (org-element-map tree 'link
3100 (lambda (link) (org-export-resolve-fuzzy-link link info))
3101 info t)))
3102 ;; Statistics cookies are ignored for headline match.
3103 (should
3104 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
3105 (org-element-map tree 'link
3106 (lambda (link) (org-export-resolve-fuzzy-link link info))
3107 info t)))
3108 (should
3109 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
3110 (org-element-map tree 'link
3111 (lambda (link) (org-export-resolve-fuzzy-link link info))
3112 info t))))
3114 (defun test-org-gen-loc-list(text type)
3115 (org-test-with-parsed-data text
3116 (org-element-map tree type
3117 (lambda (el) (or (org-export-get-loc el info) 'no-loc)))))
3119 (ert-deftest test-org-export/get-loc ()
3120 "Test `org-export-get-loc' specifications."
3121 (should
3122 ;; "-n" resets line number.
3123 (equal '(0)
3124 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n\n Text\n#+END_EXAMPLE"
3125 'example-block)))
3126 ;; The first "+n" has 0 lines before it
3127 (should
3128 (equal '(0)
3129 (test-org-gen-loc-list "#+BEGIN_EXAMPLE +n\n Text\n#+END_EXAMPLE"
3130 'example-block)))
3131 ;; "-n 10" resets line number but has "9 lines" before it.
3132 (should
3133 (equal '(9)
3134 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n 10\n Text\n#+END_EXAMPLE"
3135 'example-block)))
3136 ;; -n10 with two lines then +n 15
3137 (should
3138 (equal '(9 25)
3139 (test-org-gen-loc-list "
3140 #+BEGIN_EXAMPLE -n 10
3141 Text_10
3142 Second line(11)
3143 #+END_EXAMPLE
3144 #+BEGIN_EXAMPLE +n 15
3145 Text line (11 + 15)
3146 #+END_EXAMPLE"
3147 'example-block)))
3148 (should
3149 (equal '(9 19 0)
3150 (test-org-gen-loc-list "
3151 #+BEGIN_EXAMPLE -n 10
3152 Text
3153 #+END_EXAMPLE
3154 #+BEGIN_EXAMPLE +n 10
3155 Text
3156 #+END_EXAMPLE
3158 #+BEGIN_EXAMPLE -n
3159 Text
3160 #+END_EXAMPLE"
3161 'example-block)))
3162 ;; an Example Block without -n does not add to the line count.
3163 (should
3164 (equal '(9 no-loc 19)
3165 (test-org-gen-loc-list "
3166 #+BEGIN_EXAMPLE -n 10
3167 Text
3168 #+END_EXAMPLE
3169 #+BEGIN_EXAMPLE
3170 Text
3171 #+END_EXAMPLE
3172 #+BEGIN_EXAMPLE +n 10
3173 Text
3174 #+END_EXAMPLE"
3175 'example-block)))
3176 ;; "-n" resets line number.
3177 (should
3178 (equal
3179 '(0)
3180 (test-org-gen-loc-list "#+BEGIN_SRC emacs-lisp -n \n (- 1 1) \n#+END_SRC"
3181 'src-block)))
3182 ;; The first "+n" has 0 lines before it.
3183 (should
3184 (equal '(0)
3185 (test-org-gen-loc-list
3186 "#+BEGIN_SRC emacs-lisp +n \n (+ 0 (- 1 1))\n#+END_SRC"
3187 'src-block)))
3188 ;; "-n 10" resets line number but has "9 lines" before it.
3189 (should
3190 (equal '(9)
3191 (test-org-gen-loc-list
3192 "#+BEGIN_SRC emacs-lisp -n 10\n (- 10 1)\n#+END_SRC"
3193 'src-block)))
3194 (should
3195 (equal '(9 25)
3196 (test-org-gen-loc-list "
3197 #+BEGIN_SRC emacs-lisp -n 10
3198 (- 10 1)
3199 (+ (- 10 1) 1)
3200 #+END_SRC
3201 #+BEGIN_SRC emacs-lisp +n 15
3202 (+ (- 10 1) 2 (- 15 1))
3203 #+END_SRC"
3204 'src-block)))
3205 (should
3206 (equal '(9 19 0)
3207 (test-org-gen-loc-list "
3208 #+BEGIN_SRC emacs-lisp -n 10
3209 (- 10 1)
3210 #+END_SRC
3211 #+BEGIN_SRC emacs-lisp +n 10
3212 (+ (- 10 1) 1 (- 10 1))
3213 #+END_SRC
3214 #+BEGIN_SRC emacs-lisp -n
3215 (- 1 1)
3216 #+END_SRC"
3217 'src-block)))
3218 ;; A SRC Block without -n does not add to the line count.
3219 (should
3220 (equal '(9 no-loc 19)
3221 (test-org-gen-loc-list
3222 "#+BEGIN_SRC emacs-lisp -n 10
3223 (+ (-10 1) 1)
3224 #+END_SRC
3225 #+BEGIN_SRC emacs-lisp
3226 (+ 2 2)
3227 #+END_SRC
3228 #+BEGIN_SRC emacs-lisp +n 10
3229 (+ (- 10 1) 1 (- 10 1))
3230 #+END_SRC"
3231 'src-block))))
3233 (ert-deftest test-org-export/resolve-coderef ()
3234 "Test `org-export-resolve-coderef' specifications."
3235 (let ((org-coderef-label-format "(ref:%s)"))
3236 ;; A link to a "-n -k -r" block returns line number.
3237 (should
3238 (= 1
3239 (org-test-with-parsed-data
3240 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3241 (org-export-resolve-coderef "coderef" info))))
3242 (should
3243 (= 10
3244 (org-test-with-parsed-data
3245 "#+BEGIN_EXAMPLE -n 10 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3246 (org-export-resolve-coderef "coderef" info))))
3247 (should
3248 (= 135
3249 (org-test-with-parsed-data
3250 "#+BEGIN_EXAMPLE -n 10 -k -r\nText \n#+END_EXAMPLE\n
3251 #+BEGIN_EXAMPLE +n 125 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3252 (org-export-resolve-coderef "coderef" info))))
3253 (should
3254 (= 1
3255 (org-test-with-parsed-data
3256 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3257 (org-export-resolve-coderef "coderef" info))))
3258 (should
3259 (= 10
3260 (org-test-with-parsed-data
3261 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3262 (org-export-resolve-coderef "coderef" info))))
3263 (should
3264 (= 135
3265 (org-test-with-parsed-data
3266 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) \n#+END_SRC\n
3267 #+BEGIN_SRC emacs-lisp +n 125 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3268 (org-export-resolve-coderef "coderef" info))))
3269 ;; A link to a "-n -r" block returns line number.
3270 (should
3271 (= 1
3272 (org-test-with-parsed-data
3273 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
3274 (org-export-resolve-coderef "coderef" info))))
3275 (should
3276 (= 10
3277 (org-test-with-parsed-data
3278 "#+BEGIN_EXAMPLE -n 10 -r\nText (ref:coderef)\n#+END_EXAMPLE"
3279 (org-export-resolve-coderef "coderef" info))))
3280 (should
3281 (= 135
3282 (org-test-with-parsed-data
3283 "#+BEGIN_EXAMPLE +n 10 -r\nText \n#+END_EXAMPLE
3284 #+BEGIN_EXAMPLE +n 125 -r\nText (ref:coderef)\n#+END_EXAMPLE"
3285 (org-export-resolve-coderef "coderef" info))))
3287 (should
3288 (= 1
3289 (org-test-with-parsed-data
3290 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3291 (org-export-resolve-coderef "coderef" info))))
3292 (should
3293 (= 10
3294 (org-test-with-parsed-data
3295 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3296 (org-export-resolve-coderef "coderef" info))))
3297 (should
3298 (= 135
3299 (org-test-with-parsed-data
3300 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) \n#+END_SRC
3301 #+BEGIN_SRC emacs-lisp +n125 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3302 (org-export-resolve-coderef "coderef" info))))
3303 ;; A link to a "-n" block returns coderef.
3304 (should
3305 (equal "coderef"
3306 (org-test-with-parsed-data
3307 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3308 (org-export-resolve-coderef "coderef" info))))
3309 (should
3310 (equal "coderef"
3311 (org-test-with-parsed-data
3312 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
3313 (org-export-resolve-coderef "coderef" info))))
3314 ;; A link to a "-r" block returns line number.
3315 (should
3316 (= 1
3317 (org-test-with-parsed-data
3318 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3319 (org-export-resolve-coderef "coderef" info))))
3320 (should
3321 (= 1
3322 (org-test-with-parsed-data
3323 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
3324 (org-export-resolve-coderef "coderef" info))))
3325 ;; A link to a block without a switch returns coderef.
3326 (should
3327 (equal "coderef"
3328 (org-test-with-parsed-data
3329 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3330 (org-export-resolve-coderef "coderef" info))))
3331 (org-test-with-parsed-data
3332 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
3333 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
3334 ;; Correctly handle continued line numbers. A "+n" switch should
3335 ;; resume numbering from previous block with numbered lines,
3336 ;; ignoring blocks not numbering lines in the process. A "-n"
3337 ;; switch resets count.
3338 (should
3339 (equal '(2 1)
3340 (org-test-with-parsed-data "
3341 #+BEGIN_EXAMPLE -n
3342 Text.
3343 #+END_EXAMPLE
3345 #+BEGIN_SRC emacs-lisp
3346 \(- 1 1)
3347 #+END_SRC
3349 #+BEGIN_SRC emacs-lisp +n -r
3350 \(+ 1 1) (ref:addition)
3351 #+END_SRC
3353 #+BEGIN_EXAMPLE -n -r
3354 Another text. (ref:text)
3355 #+END_EXAMPLE"
3356 (list (org-export-resolve-coderef "addition" info)
3357 (org-export-resolve-coderef "text" info)))))
3358 ;; Recognize coderef with user-specified syntax.
3359 (should
3360 (equal
3361 "text"
3362 (org-test-with-parsed-data
3363 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
3364 (org-export-resolve-coderef "text" info))))
3365 ;; Unresolved coderefs raise a `org-link-broken' signal.
3366 (should
3367 (condition-case nil
3368 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
3369 (org-export-resolve-coderef "unknown" info))
3370 (org-link-broken t)))))
3372 (ert-deftest test-org-export/resolve-fuzzy-link ()
3373 "Test `org-export-resolve-fuzzy-link' specifications."
3374 ;; Match target objects.
3375 (should
3376 (org-test-with-parsed-data "<<target>> [[target]]"
3377 (org-export-resolve-fuzzy-link
3378 (org-element-map tree 'link 'identity info t) info)))
3379 ;; Match named elements.
3380 (should
3381 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
3382 (org-export-resolve-fuzzy-link
3383 (org-element-map tree 'link 'identity info t) info)))
3384 ;; Match exact headline's name.
3385 (should
3386 (org-test-with-parsed-data "* My headline\n[[My headline]]"
3387 (org-export-resolve-fuzzy-link
3388 (org-element-map tree 'link 'identity info t) info)))
3389 ;; Targets objects have priority over headline titles.
3390 (should
3391 (eq 'target
3392 (org-test-with-parsed-data "* target\n<<target>>[[target]]"
3393 (org-element-type
3394 (org-export-resolve-fuzzy-link
3395 (org-element-map tree 'link 'identity info t) info)))))
3396 ;; Named elements have priority over headline titles.
3397 (should
3398 (eq 'paragraph
3399 (org-test-with-parsed-data
3400 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
3401 (org-element-type
3402 (org-export-resolve-fuzzy-link
3403 (org-element-map tree 'link 'identity info t) info)))))
3404 ;; If link's path starts with a "*", only match headline titles,
3405 ;; though.
3406 (should
3407 (eq 'headline
3408 (org-test-with-parsed-data
3409 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
3410 (org-element-type
3411 (org-export-resolve-fuzzy-link
3412 (org-element-map tree 'link 'identity info t) info)))))
3413 ;; Raise a `org-link-broken' signal if no match.
3414 (should
3415 (org-test-with-parsed-data "[[target]]"
3416 (condition-case nil
3417 (org-export-resolve-fuzzy-link
3418 (org-element-map tree 'link #'identity info t) info)
3419 (org-link-broken t))))
3420 ;; Match fuzzy link even when before first headline.
3421 (should
3422 (eq 'headline
3423 (org-test-with-parsed-data "[[hl]]\n* hl"
3424 (org-element-type
3425 (org-export-resolve-fuzzy-link
3426 (org-element-map tree 'link 'identity info t) info)))))
3427 ;; Handle url-encoded fuzzy links.
3428 (should
3429 (org-test-with-parsed-data "* A B\n[[A%20B]]"
3430 (org-export-resolve-fuzzy-link
3431 (org-element-map tree 'link #'identity info t) info))))
3433 (ert-deftest test-org-export/resolve-id-link ()
3434 "Test `org-export-resolve-id-link' specifications."
3435 ;; Regular test for custom-id link.
3436 (should
3437 (equal '("Headline1")
3438 (org-test-with-parsed-data "* Headline1
3439 :PROPERTIES:
3440 :CUSTOM_ID: test
3441 :END:
3442 * Headline 2
3443 \[[#test]]"
3444 (org-element-property
3445 :title
3446 (org-export-resolve-id-link
3447 (org-element-map tree 'link 'identity info t) info)))))
3448 ;; Raise a `org-link-broken' signal on failing searches.
3449 (should
3450 (org-test-with-parsed-data "* Headline1
3451 :PROPERTIES:
3452 :CUSTOM_ID: test
3453 :END:
3454 * Headline 2
3455 \[[#no-match]]"
3456 (condition-case nil
3457 (org-export-resolve-id-link
3458 (org-element-map tree 'link #'identity info t) info)
3459 (org-link-broken t))))
3460 ;; Test for internal id target.
3461 (should
3462 (equal '("Headline1")
3463 (org-test-with-parsed-data "* Headline1
3464 :PROPERTIES:
3465 :ID: aaaa
3466 :END:
3467 * Headline 2
3468 \[[id:aaaa]]"
3469 (org-element-property
3470 :title
3471 (org-export-resolve-id-link
3472 (org-element-map tree 'link 'identity info t) info)))))
3473 ;; Test for external id target.
3474 (should
3475 (equal
3476 "external-file"
3477 (org-test-with-parsed-data "[[id:aaaa]]"
3478 (org-export-resolve-id-link
3479 (org-element-map tree 'link 'identity info t)
3480 (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
3482 (ert-deftest test-org-export/resolve-radio-link ()
3483 "Test `org-export-resolve-radio-link' specifications."
3484 ;; Standard test.
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 targets are case-insensitive.
3494 (should
3495 (org-test-with-temp-text "<<<RADIO>>> radio"
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.
3503 (should
3504 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
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 target with objects at its beginning.
3512 (should
3513 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
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 ;; Radio link next to an apostrophe.
3521 (should
3522 (org-test-with-temp-text "<<<radio>>> radio's"
3523 (org-update-radio-target-regexp)
3524 (let* ((tree (org-element-parse-buffer))
3525 (info `(:parse-tree ,tree)))
3526 (org-export-resolve-radio-link
3527 (org-element-map tree 'link 'identity info t)
3528 info))))
3529 ;; Multiple radio targets.
3530 (should
3531 (equal '("radio1" "radio2")
3532 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
3533 (org-update-radio-target-regexp)
3534 (let* ((tree (org-element-parse-buffer))
3535 (info `(:parse-tree ,tree)))
3536 (org-element-map tree 'link
3537 (lambda (link)
3538 (org-element-property
3539 :value (org-export-resolve-radio-link link info)))
3540 info)))))
3541 ;; Radio target is whitespace insensitive.
3542 (should
3543 (org-test-with-temp-text "<<<a radio>>> a\n radio"
3544 (org-update-radio-target-regexp)
3545 (let* ((tree (org-element-parse-buffer))
3546 (info `(:parse-tree ,tree)))
3547 (org-element-map tree 'link
3548 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
3550 (ert-deftest test-org-export/file-uri ()
3551 "Test `org-export-file-uri' specifications."
3552 ;; Preserve relative filenames.
3553 (should (equal "relative.org" (org-export-file-uri "relative.org")))
3554 ;; Local files start with "file://"
3555 (should (equal (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "/local.org"))
3556 (org-export-file-uri "/local.org")))
3557 ;; Remote files start with "file://"
3558 (should (equal "file://ssh:myself@some.where:papers/last.pdf"
3559 (org-export-file-uri "/ssh:myself@some.where:papers/last.pdf")))
3560 (should (equal "file://localhost/etc/fstab"
3561 (org-export-file-uri "//localhost/etc/fstab")))
3562 ;; Expand filename starting with "~".
3563 (should (equal (org-export-file-uri "~/file.org")
3564 (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "~/file.org")))))
3566 (ert-deftest test-org-export/get-reference ()
3567 "Test `org-export-get-reference' specifications."
3568 (should
3569 (org-test-with-parsed-data "* Headline"
3570 (org-export-get-reference (org-element-map tree 'headline #'identity nil t)
3571 info)))
3572 ;; For a given element always return the same reference.
3573 (should
3574 (org-test-with-parsed-data "* Headline"
3575 (let ((headline (org-element-map tree 'headline #'identity nil t)))
3576 (equal (org-export-get-reference headline info)
3577 (org-export-get-reference headline info)))))
3578 ;; References get through local export back-ends.
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-trim (org-export-data-with-backend headline backend info))
3587 (org-export-get-reference headline info)))))
3588 (should
3589 (org-test-with-parsed-data "* Headline"
3590 (let ((headline (org-element-map tree 'headline #'identity nil t))
3591 (backend
3592 (org-export-create-backend
3593 :transcoders
3594 '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
3595 (equal (org-export-with-backend backend headline nil info)
3596 (org-export-get-reference headline info)))))
3597 ;; Use search cells defined in `:crossrefs'. However, handle
3598 ;; duplicate search cells.
3599 (should
3600 (equal "org0000001"
3601 (org-test-with-parsed-data "* Headline"
3602 (let* ((headline (org-element-map tree 'headline #'identity nil t))
3603 (search-cell (car (org-export-search-cells headline))))
3604 (setq info
3605 (plist-put info :crossrefs (list (cons search-cell 1))))
3606 (org-export-get-reference headline info)))))
3607 (should-not
3608 (equal '("org0000001" "org0000001")
3609 (org-test-with-parsed-data "* H\n** H"
3610 (org-element-map tree 'headline
3611 (lambda (h)
3612 (let* ((search-cell (car (org-export-search-cells h)))
3613 (info (plist-put info :crossrefs
3614 (list (cons search-cell 1)))))
3615 (org-export-get-reference h info))))))))
3618 ;;; Pseudo objects and pseudo elements
3620 (ert-deftest test-org-export/pseudo-elements ()
3621 "Test exporting pseudo-elements."
3622 ;; Handle blank lines after pseudo-elements. In particular, do not
3623 ;; replace them with white spaces.
3624 (should
3625 (equal "contents\n\nparagraph\n"
3626 (let ((backend (org-export-create-backend
3627 :transcoders
3628 '((pseudo-element . (lambda (_p c _i) c))
3629 (paragraph . (lambda (_p c _i) c))
3630 (plain-text . (lambda (c _i) c)))))
3631 (element '(pseudo-element (:post-blank 1) "contents"))
3632 (paragraph '(paragraph nil "paragraph"))
3633 (data '(org-data nil)))
3634 (org-element-adopt-elements data element paragraph)
3635 (org-export-data-with-backend data backend nil)))))
3637 (ert-deftest test-org-export/pseudo-objects ()
3638 "Test exporting pseudo-objects."
3639 ;; Handle blank spaces after pseudo-objects. In particular, do not
3640 ;; replace them with newlines.
3641 (should
3642 (equal "begin x end\n"
3643 (let ((backend (org-export-create-backend
3644 :transcoders
3645 '((pseudo-object . (lambda (_p c _i) c))
3646 (paragraph . (lambda (_p c _i) c))
3647 (plain-text . (lambda (c _i) c)))))
3648 (object '(pseudo-object (:post-blank 1) "x"))
3649 (paragraph '(paragraph nil)))
3650 (org-element-adopt-elements paragraph "begin " object "end")
3651 (org-export-data-with-backend paragraph backend nil)))))
3654 ;;; Src-block and example-block
3656 (ert-deftest test-org-export/unravel-code ()
3657 "Test `org-export-unravel-code' function."
3658 ;; Code without reference.
3659 (should
3660 (equal '("(+ 1 1)")
3661 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
3662 (org-export-unravel-code (org-element-at-point)))))
3663 ;; Code with reference.
3664 (should
3665 (equal '("(+ 1 1)" (1 . "test"))
3666 (org-test-with-temp-text
3667 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
3668 (let ((org-coderef-label-format "(ref:%s)"))
3669 (org-export-unravel-code (org-element-at-point))))))
3670 ;; Code with user-defined reference.
3671 (should
3672 (equal
3673 '("(+ 1 1)" (1 . "test"))
3674 (org-test-with-temp-text
3675 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
3676 (let ((org-coderef-label-format "(ref:%s)"))
3677 (org-export-unravel-code (org-element-at-point))))))
3678 ;; Code references keys are relative to the current block.
3679 (should
3680 (equal '("(+ 2 2)\n(+ 3 3)" (2 . "one"))
3681 (org-test-with-temp-text "
3682 #+BEGIN_EXAMPLE -n
3683 \(+ 1 1)
3684 #+END_EXAMPLE
3685 #+BEGIN_EXAMPLE +n
3686 \(+ 2 2)
3687 \(+ 3 3) (ref:one)
3688 #+END_EXAMPLE"
3689 (goto-line 5)
3690 (let ((org-coderef-label-format "(ref:%s)"))
3691 (org-export-unravel-code (org-element-at-point)))))\x14))
3693 (ert-deftest test-org-export/format-code-default ()
3694 "Test `org-export-format-code-default' specifications."
3695 ;; Preserve blank lines, even when code is empty.
3696 (should
3697 (equal "\n\n"
3698 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
3699 (org-export-format-code-default
3700 (org-element-map tree 'src-block #'identity info t) info))))
3701 ;; Likewise, preserve leading and trailing blank lines in the code.
3702 (should
3703 (equal "\n(+ 1 1)\n"
3704 (org-test-with-parsed-data
3705 "#+BEGIN_SRC emacs-lisp\n\n(+ 1 1)\n#+END_SRC"
3706 (org-export-format-code-default
3707 (org-element-map tree 'src-block #'identity info t) info))))
3708 (should
3709 (equal "(+ 1 1)\n\n"
3710 (org-test-with-parsed-data
3711 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n\n#+END_SRC"
3712 (org-export-format-code-default
3713 (org-element-map tree 'src-block #'identity info t) info))))
3714 ;; Number lines, two whitespace characters before the actual loc.
3715 (should
3716 (equal "1 a\n2 b\n"
3717 (org-test-with-parsed-data
3718 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
3719 (org-export-format-code-default
3720 (org-element-map tree 'src-block #'identity info t) info))))
3721 ;; Numbering includes blank lines.
3722 (should
3723 (equal "1 \n2 a\n3 \n4 b\n5 \n"
3724 (org-test-with-parsed-data
3725 "#+BEGIN_SRC emacs-lisp +n\n\na\n\nb\n\n#+END_SRC"
3726 (org-export-format-code-default
3727 (org-element-map tree 'src-block #'identity info t) info))))
3728 ;; Put references 6 whitespace characters after the widest line,
3729 ;; wrapped within parenthesis.
3730 (should
3731 (equal "123 (a)\n1 (b)\n"
3732 (let ((org-coderef-label-format "(ref:%s)"))
3733 (org-test-with-parsed-data
3734 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
3735 (org-export-format-code-default
3736 (org-element-map tree 'src-block #'identity info t) info))))))
3740 ;;; Smart Quotes
3742 (ert-deftest test-org-export/activate-smart-quotes ()
3743 "Test `org-export-activate-smart-quotes' specifications."
3744 ;; Double quotes: standard test.
3745 (should
3746 (equal
3747 '("some &ldquo;quoted&rdquo; text")
3748 (let ((org-export-default-language "en"))
3749 (org-test-with-parsed-data "some \"quoted\" text"
3750 (org-element-map tree 'plain-text
3751 (lambda (s) (org-export-activate-smart-quotes s :html info))
3752 info)))))
3753 ;; Opening quotes: at the beginning of a paragraph.
3754 (should
3755 (equal
3756 '("&ldquo;begin")
3757 (let ((org-export-default-language "en"))
3758 (org-test-with-parsed-data "\"begin"
3759 (org-element-map tree 'plain-text
3760 (lambda (s) (org-export-activate-smart-quotes s :html info))
3761 info)))))
3762 ;; Opening quotes: after an object.
3763 (should
3764 (equal
3765 '("&ldquo;quoted&rdquo; text")
3766 (let ((org-export-default-language "en"))
3767 (org-test-with-parsed-data "=verb= \"quoted\" text"
3768 (org-element-map tree 'plain-text
3769 (lambda (s) (org-export-activate-smart-quotes s :html info))
3770 info)))))
3771 ;; Closing quotes: at the end of a paragraph.
3772 (should
3773 (equal
3774 '("Quoted &ldquo;text&rdquo;")
3775 (let ((org-export-default-language "en"))
3776 (org-test-with-parsed-data "Quoted \"text\""
3777 (org-element-map tree 'plain-text
3778 (lambda (s) (org-export-activate-smart-quotes s :html info))
3779 info)))))
3780 ;; Inner quotes: standard test.
3781 (should
3782 (equal '("« outer « inner » outer »")
3783 (let ((org-export-default-language "fr"))
3784 (org-test-with-parsed-data "\"outer 'inner' outer\""
3785 (org-element-map tree 'plain-text
3786 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3787 info)))))
3788 ;; Inner quotes: close to special symbols.
3789 (should
3790 (equal '("« outer (« inner ») outer »")
3791 (let ((org-export-default-language "fr"))
3792 (org-test-with-parsed-data "\"outer ('inner') outer\""
3793 (org-element-map tree 'plain-text
3794 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3795 info)))))
3796 (should
3797 (equal '("« « inner » »")
3798 (let ((org-export-default-language "fr"))
3799 (org-test-with-parsed-data "\"'inner'\""
3800 (org-element-map tree 'plain-text
3801 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3802 info)))))
3803 ;; Apostrophe: standard test.
3804 (should
3805 (equal '("It « shouldn’t » fail")
3806 (let ((org-export-default-language "fr"))
3807 (org-test-with-parsed-data "It \"shouldn't\" fail"
3808 (org-element-map tree 'plain-text
3809 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3810 info)))))
3811 (should
3812 (equal '("It shouldn’t fail")
3813 (let ((org-export-default-language "fr"))
3814 (org-test-with-parsed-data "It shouldn't fail"
3815 (org-element-map tree 'plain-text
3816 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3817 info)))))
3818 ;; Apostrophe: before an object.
3819 (should
3820 (equal
3821 '("« a’" " »")
3822 (let ((org-export-default-language "fr"))
3823 (org-test-with-parsed-data "\"a'=b=\""
3824 (org-element-map tree 'plain-text
3825 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3826 info)))))
3827 ;; Apostrophe: after an object.
3828 (should
3829 (equal '("« " "’s »")
3830 (let ((org-export-default-language "fr"))
3831 (org-test-with-parsed-data "\"=code='s\""
3832 (org-element-map tree 'plain-text
3833 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3834 info)))))
3835 ;; Special case: isolated quotes.
3836 (should
3837 (equal '("&ldquo;" "&rdquo;")
3838 (let ((org-export-default-language "en"))
3839 (org-test-with-parsed-data "\"$x$\""
3840 (org-element-map tree 'plain-text
3841 (lambda (s) (org-export-activate-smart-quotes s :html info))
3842 info)))))
3843 ;; Smart quotes in secondary strings.
3844 (should
3845 (equal '("&ldquo;" "&rdquo;")
3846 (let ((org-export-default-language "en"))
3847 (org-test-with-parsed-data "* \"$x$\""
3848 (org-element-map tree 'plain-text
3849 (lambda (s) (org-export-activate-smart-quotes s :html info))
3850 info)))))
3851 ;; Smart quotes in document keywords.
3852 (should
3853 (equal '("&ldquo;" "&rdquo;")
3854 (let ((org-export-default-language "en"))
3855 (org-test-with-parsed-data "#+TITLE: \"$x$\""
3856 (org-element-map (plist-get info :title) 'plain-text
3857 (lambda (s) (org-export-activate-smart-quotes s :html info))
3858 info)))))
3859 ;; Smart quotes in parsed affiliated keywords.
3860 (should
3861 (equal '("&ldquo;" "&rdquo;" "Paragraph")
3862 (let ((org-export-default-language "en"))
3863 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
3864 (org-element-map tree 'plain-text
3865 (lambda (s) (org-export-activate-smart-quotes s :html info))
3866 info nil nil t)))))
3867 ;; Smart quotes within objects.
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)))))
3875 ;; FIXME: Test failing non-interactively.
3877 ;; (should
3878 ;; (equal '("&ldquo;foo&rdquo;")
3879 ;; (let ((org-export-default-language "en"))
3880 ;; (org-test-with-parsed-data "*\"foo\"*"
3881 ;; (org-element-map tree 'plain-text
3882 ;; (lambda (s) (org-export-activate-smart-quotes s :html info))
3883 ;; info nil nil t)))))
3888 ;;; Tables
3890 (ert-deftest test-org-export/special-column ()
3891 "Test if the table's special column is properly recognized."
3892 ;; 1. First column is special if it contains only a special marking
3893 ;; characters or empty cells.
3894 (org-test-with-temp-text "
3895 | ! | 1 |
3896 | | 2 |"
3897 (should
3898 (org-export-table-has-special-column-p
3899 (org-element-map
3900 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3901 ;; 2. If the column contains anything else, it isn't special.
3902 (org-test-with-temp-text "
3903 | ! | 1 |
3904 | b | 2 |"
3905 (should-not
3906 (org-export-table-has-special-column-p
3907 (org-element-map
3908 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3909 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
3910 ;; and "!".
3911 (org-test-with-temp-text "
3912 | # | 1 |
3913 | ^ | 2 |
3914 | * | 3 |
3915 | _ | 4 |
3916 | / | 5 |
3917 | $ | 6 |
3918 | ! | 7 |"
3919 (should
3920 (org-export-table-has-special-column-p
3921 (org-element-map
3922 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3923 ;; 4. A first column with only empty cells isn't considered as
3924 ;; special.
3925 (org-test-with-temp-text "
3926 | | 1 |
3927 | | 2 |"
3928 (should-not
3929 (org-export-table-has-special-column-p
3930 (org-element-map
3931 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
3933 (ert-deftest test-org-export/table-row-is-special-p ()
3934 "Test `org-export-table-row-is-special-p' specifications."
3935 ;; 1. A row is special if it has a special marking character in the
3936 ;; special column.
3937 (org-test-with-parsed-data "| ! | 1 |"
3938 (should
3939 (org-export-table-row-is-special-p
3940 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3941 ;; 2. A row is special when its first field is "/"
3942 (org-test-with-parsed-data "
3943 | / | 1 |
3944 | a | b |"
3945 (should
3946 (org-export-table-row-is-special-p
3947 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3948 ;; 3. A row only containing alignment cookies is also considered as
3949 ;; special.
3950 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
3951 (should
3952 (org-export-table-row-is-special-p
3953 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3954 ;; 4. Everything else isn't considered as special.
3955 (org-test-with-parsed-data "| \alpha | | c |"
3956 (should-not
3957 (org-export-table-row-is-special-p
3958 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3959 ;; 5. Table's rules are never considered as special rows.
3960 (org-test-with-parsed-data "|---+---|"
3961 (should-not
3962 (org-export-table-row-is-special-p
3963 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
3965 (ert-deftest test-org-export/has-header-p ()
3966 "Test `org-export-table-has-header-p' specifications."
3967 ;; With an header.
3968 (should
3969 (org-test-with-parsed-data "
3970 | a | b |
3971 |---+---|
3972 | c | d |"
3973 (org-export-table-has-header-p
3974 (org-element-map tree 'table 'identity info 'first-match)
3975 info)))
3976 ;; Without an header.
3977 (should-not
3978 (org-test-with-parsed-data "
3979 | a | b |
3980 | c | d |"
3981 (org-export-table-has-header-p
3982 (org-element-map tree 'table 'identity info 'first-match)
3983 info)))
3984 ;; Don't get fooled with starting and ending rules.
3985 (should-not
3986 (org-test-with-parsed-data "
3987 |---+---|
3988 | a | b |
3989 | c | d |
3990 |---+---|"
3991 (org-export-table-has-header-p
3992 (org-element-map tree 'table 'identity info 'first-match)
3993 info))))
3995 (ert-deftest test-org-export/table-row-group ()
3996 "Test `org-export-table-row-group' specifications."
3997 ;; A rule creates a new group.
3998 (should
3999 (equal '(1 rule 2)
4000 (org-test-with-parsed-data "
4001 | a | b |
4002 |---+---|
4003 | 1 | 2 |"
4004 (org-element-map tree 'table-row
4005 (lambda (row)
4006 (if (eq (org-element-property :type row) 'rule) 'rule
4007 (org-export-table-row-group row info)))))))
4008 ;; Special rows are ignored in count.
4009 (should
4010 (equal
4011 '(rule 1)
4012 (org-test-with-parsed-data "
4013 | / | < | > |
4014 |---|---+---|
4015 | | 1 | 2 |"
4016 (org-element-map tree 'table-row
4017 (lambda (row)
4018 (if (eq (org-element-property :type row) 'rule) 'rule
4019 (org-export-table-row-group row info)))
4020 info))))
4021 ;; Double rules also are ignored in count.
4022 (should
4023 (equal '(1 rule rule 2)
4024 (org-test-with-parsed-data "
4025 | a | b |
4026 |---+---|
4027 |---+---|
4028 | 1 | 2 |"
4029 (org-element-map tree 'table-row
4030 (lambda (row)
4031 (if (eq (org-element-property :type row) 'rule) 'rule
4032 (org-export-table-row-group row info))))))))
4034 (ert-deftest test-org-export/table-row-number ()
4035 "Test `org-export-table-row-number' specifications."
4036 ;; Standard test. Number is 0-indexed.
4037 (should
4038 (equal '(0 1)
4039 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
4040 (org-element-map tree 'table-row
4041 (lambda (row) (org-export-table-row-number row info)) info))))
4042 ;; Number ignores separators.
4043 (should
4044 (equal '(0 1)
4045 (org-test-with-parsed-data "
4046 | a | b | c |
4047 |---+---+---|
4048 | d | e | f |"
4049 (org-element-map tree 'table-row
4050 (lambda (row) (org-export-table-row-number row info)) info))))
4051 ;; Number ignores special rows.
4052 (should
4053 (equal '(0 1)
4054 (org-test-with-parsed-data "
4055 | / | < | > |
4056 | | b | c |
4057 |---+-----+-----|
4058 | | <c> | <c> |
4059 | | e | f |"
4060 (org-element-map tree 'table-row
4061 (lambda (row) (org-export-table-row-number row info)) info)))))
4063 (ert-deftest test-org-export/table-cell-width ()
4064 "Test `org-export-table-cell-width' specifications."
4065 ;; 1. Width is primarily determined by width cookies. If no cookie
4066 ;; is found, cell's width is nil.
4067 (org-test-with-parsed-data "
4068 | / | <l> | <6> | <l7> |
4069 | | a | b | c |"
4070 (should
4071 (equal
4072 '(nil 6 7)
4073 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
4074 (org-element-map tree 'table-cell 'identity info)))))
4075 ;; 2. The last width cookie has precedence.
4076 (org-test-with-parsed-data "
4077 | <6> |
4078 | <7> |
4079 | a |"
4080 (should
4081 (equal
4082 '(7)
4083 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
4084 (org-element-map tree 'table-cell 'identity info)))))
4085 ;; 3. Valid width cookies must have a specific row.
4086 (org-test-with-parsed-data "| <6> | cell |"
4087 (should
4088 (equal
4089 '(nil nil)
4090 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
4091 (org-element-map tree 'table-cell 'identity))))))
4093 (ert-deftest test-org-export/table-cell-alignment ()
4094 "Test `org-export-table-cell-alignment' specifications."
4095 ;; 1. Alignment is primarily determined by alignment cookies.
4096 (should
4097 (equal '(left center right)
4098 (let ((org-table-number-fraction 0.5)
4099 (org-table-number-regexp "^[0-9]+$"))
4100 (org-test-with-parsed-data "| <l> | <c> | <r> |"
4101 (mapcar (lambda (cell)
4102 (org-export-table-cell-alignment cell info))
4103 (org-element-map tree 'table-cell 'identity))))))
4104 ;; 2. The last alignment cookie has precedence.
4105 (should
4106 (equal '(right right right)
4107 (org-test-with-parsed-data "
4108 | <l8> |
4109 | cell |
4110 | <r9> |"
4111 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
4112 (org-element-map tree 'table-cell 'identity)))))
4113 ;; 3. If there's no cookie, cell's contents determine alignment.
4114 ;; A column mostly made of cells containing numbers will align
4115 ;; its cells to the right.
4116 (should
4117 (equal '(right right right)
4118 (let ((org-table-number-fraction 0.5)
4119 (org-table-number-regexp "^[0-9]+$"))
4120 (org-test-with-parsed-data "
4121 | 123 |
4122 | some text |
4123 | 12345 |"
4124 (mapcar (lambda (cell)
4125 (org-export-table-cell-alignment cell info))
4126 (org-element-map tree 'table-cell 'identity))))))
4127 ;; 4. Otherwise, they will be aligned to the left.
4128 (should
4129 (equal '(left left left)
4130 (org-test-with-parsed-data "
4131 | text |
4132 | some text |
4133 | \alpha |"
4134 (mapcar (lambda (cell)
4135 (org-export-table-cell-alignment cell info))
4136 (org-element-map tree 'table-cell 'identity info))))))
4138 (ert-deftest test-org-export/table-cell-borders ()
4139 "Test `org-export-table-cell-borders' specifications."
4140 ;; 1. Recognize various column groups indicators.
4141 (org-test-with-parsed-data "| / | < | > | <> |"
4142 (should
4143 (equal
4144 '((right bottom top) (left bottom top) (right bottom top)
4145 (right left bottom top))
4146 (mapcar (lambda (cell)
4147 (org-export-table-cell-borders cell info))
4148 (org-element-map tree 'table-cell 'identity)))))
4149 ;; 2. Accept shortcuts to define column groups.
4150 (org-test-with-parsed-data "| / | < | < |"
4151 (should
4152 (equal
4153 '((right bottom top) (right left bottom top) (left bottom top))
4154 (mapcar (lambda (cell)
4155 (org-export-table-cell-borders cell info))
4156 (org-element-map tree 'table-cell 'identity)))))
4157 ;; 3. A valid column groups row must start with a "/".
4158 (org-test-with-parsed-data "
4159 | | < |
4160 | a | b |"
4161 (should
4162 (equal '((top) (top) (bottom) (bottom))
4163 (mapcar (lambda (cell)
4164 (org-export-table-cell-borders cell info))
4165 (org-element-map tree 'table-cell 'identity)))))
4166 ;; 4. Take table rules into consideration.
4167 (org-test-with-parsed-data "
4168 | 1 |
4169 |---|
4170 | 2 |"
4171 (should
4172 (equal '((below top) (bottom above))
4173 (mapcar (lambda (cell)
4174 (org-export-table-cell-borders cell info))
4175 (org-element-map tree 'table-cell 'identity)))))
4176 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
4177 ;; (resp. `bottom' and `below') borders. Any special row is
4178 ;; ignored.
4179 (org-test-with-parsed-data "
4180 |---+----|
4181 | / | |
4182 | | 1 |
4183 |---+----|"
4184 (should
4185 (equal '((bottom below top above))
4186 (last
4187 (mapcar (lambda (cell)
4188 (org-export-table-cell-borders cell info))
4189 (org-element-map tree 'table-cell 'identity)))))))
4191 (ert-deftest test-org-export/table-dimensions ()
4192 "Test `org-export-table-dimensions' specifications."
4193 ;; 1. Standard test.
4194 (org-test-with-parsed-data "
4195 | 1 | 2 | 3 |
4196 | 4 | 5 | 6 |"
4197 (should
4198 (equal '(2 . 3)
4199 (org-export-table-dimensions
4200 (org-element-map tree 'table 'identity info 'first-match) info))))
4201 ;; 2. Ignore horizontal rules and special columns.
4202 (org-test-with-parsed-data "
4203 | / | < | > |
4204 | 1 | 2 | 3 |
4205 |---+---+---|
4206 | 4 | 5 | 6 |"
4207 (should
4208 (equal '(2 . 3)
4209 (org-export-table-dimensions
4210 (org-element-map tree 'table 'identity info 'first-match) info)))))
4212 (ert-deftest test-org-export/table-cell-address ()
4213 "Test `org-export-table-cell-address' specifications."
4214 ;; 1. Standard test: index is 0-based.
4215 (org-test-with-parsed-data "| a | b |"
4216 (should
4217 (equal '((0 . 0) (0 . 1))
4218 (org-element-map tree 'table-cell
4219 (lambda (cell) (org-export-table-cell-address cell info))
4220 info))))
4221 ;; 2. Special column isn't counted, nor are special rows.
4222 (org-test-with-parsed-data "
4223 | / | <> |
4224 | | c |"
4225 (should
4226 (equal '(0 . 0)
4227 (org-export-table-cell-address
4228 (car (last (org-element-map tree 'table-cell 'identity info)))
4229 info))))
4230 ;; 3. Tables rules do not count either.
4231 (org-test-with-parsed-data "
4232 | a |
4233 |---|
4234 | b |
4235 |---|
4236 | c |"
4237 (should
4238 (equal '(2 . 0)
4239 (org-export-table-cell-address
4240 (car (last (org-element-map tree 'table-cell 'identity info)))
4241 info))))
4242 ;; 4. Return nil for special cells.
4243 (org-test-with-parsed-data "| / | a |"
4244 (should-not
4245 (org-export-table-cell-address
4246 (org-element-map tree 'table-cell 'identity nil 'first-match)
4247 info))))
4249 (ert-deftest test-org-export/get-table-cell-at ()
4250 "Test `org-export-get-table-cell-at' specifications."
4251 ;; 1. Address ignores special columns, special rows and rules.
4252 (org-test-with-parsed-data "
4253 | / | <> |
4254 | | a |
4255 |---+----|
4256 | | b |"
4257 (should
4258 (equal '("b")
4259 (org-element-contents
4260 (org-export-get-table-cell-at
4261 '(1 . 0)
4262 (org-element-map tree 'table 'identity info 'first-match)
4263 info)))))
4264 ;; 2. Return value for a non-existent address is nil.
4265 (org-test-with-parsed-data "| a |"
4266 (should-not
4267 (org-export-get-table-cell-at
4268 '(2 . 2)
4269 (org-element-map tree 'table 'identity info 'first-match)
4270 info)))
4271 (org-test-with-parsed-data "| / |"
4272 (should-not
4273 (org-export-get-table-cell-at
4274 '(0 . 0)
4275 (org-element-map tree 'table 'identity info 'first-match)
4276 info))))
4278 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
4279 "Test `org-export-table-cell-starts-colgroup-p' specifications."
4280 ;; 1. A cell at a beginning of a row always starts a column group.
4281 (org-test-with-parsed-data "| 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. Special column should be ignored when determining the
4287 ;; beginning of the row.
4288 (org-test-with-parsed-data "
4289 | / | |
4290 | | a |"
4291 (should
4292 (org-export-table-cell-starts-colgroup-p
4293 (org-element-map tree 'table-cell 'identity info 'first-match)
4294 info)))
4295 ;; 2. Explicit column groups.
4296 (org-test-with-parsed-data "
4297 | / | | < |
4298 | a | b | c |"
4299 (should
4300 (equal
4301 '(yes no yes)
4302 (org-element-map tree 'table-cell
4303 (lambda (cell)
4304 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
4305 info)))))
4307 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
4308 "Test `org-export-table-cell-ends-colgroup-p' specifications."
4309 ;; 1. A cell at the end of a row always ends a column group.
4310 (org-test-with-parsed-data "| 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 ;; 2. Special column should be ignored when determining the
4316 ;; beginning of the row.
4317 (org-test-with-parsed-data "
4318 | / | |
4319 | | a |"
4320 (should
4321 (org-export-table-cell-ends-colgroup-p
4322 (org-element-map tree 'table-cell 'identity info 'first-match)
4323 info)))
4324 ;; 3. Explicit column groups.
4325 (org-test-with-parsed-data "
4326 | / | < | |
4327 | a | b | c |"
4328 (should
4329 (equal
4330 '(yes no yes)
4331 (org-element-map tree 'table-cell
4332 (lambda (cell)
4333 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
4334 info)))))
4336 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
4337 "Test `org-export-table-row-starts-rowgroup-p' specifications."
4338 ;; 1. A row at the beginning of a table always starts a row group.
4339 ;; So does a row following a table rule.
4340 (org-test-with-parsed-data "
4341 | a |
4342 |---|
4343 | b |"
4344 (should
4345 (equal
4346 '(yes no yes)
4347 (org-element-map tree 'table-row
4348 (lambda (row)
4349 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
4350 info))))
4351 ;; 2. Special rows should be ignored when determining the beginning
4352 ;; of the row.
4353 (org-test-with-parsed-data "
4354 | / | < |
4355 | | a |
4356 |---+---|
4357 | / | < |
4358 | | b |"
4359 (should
4360 (equal
4361 '(yes no yes)
4362 (org-element-map tree 'table-row
4363 (lambda (row)
4364 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
4365 info)))))
4367 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
4368 "Test `org-export-table-row-ends-rowgroup-p' specifications."
4369 ;; 1. A row at the end of a table always ends a row group. So does
4370 ;; a row preceding a table rule.
4371 (org-test-with-parsed-data "
4372 | a |
4373 |---|
4374 | b |"
4375 (should
4376 (equal
4377 '(yes no yes)
4378 (org-element-map tree 'table-row
4379 (lambda (row)
4380 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
4381 info))))
4382 ;; 2. Special rows should be ignored when determining the beginning
4383 ;; of the row.
4384 (org-test-with-parsed-data "
4385 | | a |
4386 | / | < |
4387 |---+---|
4388 | | b |
4389 | / | < |"
4390 (should
4391 (equal
4392 '(yes no yes)
4393 (org-element-map tree 'table-row
4394 (lambda (row)
4395 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
4396 info)))))
4398 (ert-deftest test-org-export/table-row-in-header-p ()
4399 "Test `org-export-table-row-in-header-p' specifications."
4400 ;; Standard test. Separators are always nil.
4401 (should
4402 (equal
4403 '(yes no no)
4404 (org-test-with-parsed-data "| a |\n|---|\n| b |"
4405 (org-element-map tree 'table-row
4406 (lambda (row)
4407 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
4408 ;; Nil when there is no header.
4409 (should
4410 (equal
4411 '(no no)
4412 (org-test-with-parsed-data "| a |\n| b |"
4413 (org-element-map tree 'table-row
4414 (lambda (row)
4415 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
4417 (ert-deftest test-org-export/table-row-starts-header-p ()
4418 "Test `org-export-table-row-starts-header-p' specifications."
4419 ;; 1. Only the row starting the first row group starts the table
4420 ;; header.
4421 (org-test-with-parsed-data "
4422 | a |
4423 | b |
4424 |---|
4425 | c |"
4426 (should
4427 (equal
4428 '(yes no no no)
4429 (org-element-map tree 'table-row
4430 (lambda (row)
4431 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
4432 info))))
4433 ;; 2. A row cannot start an header if there's no header in the
4434 ;; table.
4435 (org-test-with-parsed-data "
4436 | a |
4437 |---|"
4438 (should-not
4439 (org-export-table-row-starts-header-p
4440 (org-element-map tree 'table-row 'identity info 'first-match)
4441 info))))
4443 (ert-deftest test-org-export/table-row-ends-header-p ()
4444 "Test `org-export-table-row-ends-header-p' specifications."
4445 ;; 1. Only the row starting the first row group starts the table
4446 ;; header.
4447 (org-test-with-parsed-data "
4448 | a |
4449 | b |
4450 |---|
4451 | c |"
4452 (should
4453 (equal
4454 '(no yes no no)
4455 (org-element-map tree 'table-row
4456 (lambda (row)
4457 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
4458 info))))
4459 ;; 2. A row cannot start an header if there's no header in the
4460 ;; table.
4461 (org-test-with-parsed-data "
4462 | a |
4463 |---|"
4464 (should-not
4465 (org-export-table-row-ends-header-p
4466 (org-element-map tree 'table-row 'identity info 'first-match)
4467 info))))
4471 ;;; Tables of Contents
4473 (ert-deftest test-org-export/collect-headlines ()
4474 "Test `org-export-collect-headlines' specifications."
4475 ;; Standard test.
4476 (should
4477 (equal '("H1" "H2")
4478 (org-test-with-parsed-data "* H1\n** H2"
4479 (mapcar (lambda (h) (org-element-property :raw-value h))
4480 (org-export-collect-headlines info)))))
4481 ;; Do not collect headlines below optional argument.
4482 (should
4483 (equal '("H1")
4484 (org-test-with-parsed-data "* H1\n** H2"
4485 (mapcar (lambda (h) (org-element-property :raw-value h))
4486 (org-export-collect-headlines info 1)))))
4487 ;; Never collect headlines below maximum headline level.
4488 (should
4489 (equal '("H1")
4490 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4491 (mapcar (lambda (h) (org-element-property :raw-value h))
4492 (org-export-collect-headlines info)))))
4493 (should
4494 (equal '("H1")
4495 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4496 (mapcar (lambda (h) (org-element-property :raw-value h))
4497 (org-export-collect-headlines info 2)))))
4498 ;; Do not collect footnote section.
4499 (should
4500 (equal '("H1")
4501 (let ((org-footnote-section "Footnotes"))
4502 (org-test-with-parsed-data "* H1\n** Footnotes"
4503 (mapcar (lambda (h) (org-element-property :raw-value h))
4504 (org-export-collect-headlines info))))))
4505 ;; Do not collect headlines with UNNUMBERED property set to "notoc".
4506 ;; Headlines with another value for the property are still
4507 ;; collected. UNNUMBERED property is inherited.
4508 (should
4509 (equal '("H1")
4510 (org-test-with-parsed-data
4511 "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:"
4512 (mapcar (lambda (h) (org-element-property :raw-value h))
4513 (org-export-collect-headlines info)))))
4514 (should-not
4515 (org-test-with-parsed-data
4516 "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:\n** H2"
4517 (mapcar (lambda (h) (org-element-property :raw-value h))
4518 (org-export-collect-headlines info))))
4519 (should
4520 (equal '("H1" "H2")
4521 (org-test-with-parsed-data
4522 "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
4523 (mapcar (lambda (h) (org-element-property :raw-value h))
4524 (org-export-collect-headlines info)))))
4525 ;; Collect headlines locally.
4526 (should
4527 (equal '("H2" "H3")
4528 (org-test-with-parsed-data "* H1\n** H2\n** H3"
4529 (let ((scope (org-element-map tree 'headline #'identity info t)))
4530 (mapcar (lambda (h) (org-element-property :raw-value h))
4531 (org-export-collect-headlines info nil scope))))))
4532 ;; When collecting locally, optional level is relative.
4533 (should
4534 (equal '("H2")
4535 (org-test-with-parsed-data "* H1\n** H2\n*** H3"
4536 (let ((scope (org-element-map tree 'headline #'identity info t)))
4537 (mapcar (lambda (h) (org-element-property :raw-value h))
4538 (org-export-collect-headlines info 1 scope)))))))
4540 (ert-deftest test-org-export/excluded-from-toc-p ()
4541 "Test `org-export-excluded-from-toc-p' specifications."
4542 ;; By default, headlines are not excluded.
4543 (should-not
4544 (org-test-with-parsed-data "* H1"
4545 (org-element-map tree 'headline
4546 (lambda (h) (org-export-excluded-from-toc-p h info)) info t)))
4547 ;; Exclude according to a maximum level.
4548 (should
4549 (equal '(in out)
4550 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4551 (org-element-map tree 'headline
4552 (lambda (h) (if (org-export-excluded-from-toc-p h info) 'out 'in))
4553 info))))
4554 ;; Exclude according to UNNUMBERED property.
4555 (should
4556 (org-test-with-parsed-data "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:"
4557 (org-element-map tree 'headline
4558 (lambda (h) (org-export-excluded-from-toc-p h info)) info t)))
4559 ;; UNNUMBERED property is inherited, so is "notoc" value.
4560 (should
4561 (equal '(out out)
4562 (org-test-with-parsed-data
4563 "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:\n** H2"
4564 (org-element-map tree 'headline
4565 (lambda (h) (if (org-export-excluded-from-toc-p h info) 'out 'in))
4566 info)))))
4568 (ert-deftest test-org-export/toc-entry-backend ()
4569 "Test `org-export-toc-entry-backend' specifications."
4570 ;; Ignore targets.
4571 (should
4572 (equal "H \n"
4573 (org-test-with-temp-text "* H <<target>>"
4574 (let (org-export-registered-backends)
4575 (org-export-define-backend 'test
4576 '((headline . (lambda (h _c i) (org-export-data-with-backend
4577 (org-element-property :title h)
4578 (org-export-toc-entry-backend 'test)
4579 i)))))
4580 (org-export-as 'test)))))
4581 ;; Ignore footnote references.
4582 (should
4583 (equal "H \n"
4584 (org-test-with-temp-text "[fn:1] Definition\n* H [fn:1]"
4585 (let (org-export-registered-backends)
4586 (org-export-define-backend 'test
4587 '((headline . (lambda (h _c i) (org-export-data-with-backend
4588 (org-element-property :title h)
4589 (org-export-toc-entry-backend 'test)
4590 i)))))
4591 (org-export-as 'test)))))
4592 ;; Replace plain links with contents, or with path.
4593 (should
4594 (equal "H Org mode\n"
4595 (org-test-with-temp-text "* H [[https://orgmode.org][Org mode]]"
4596 (let (org-export-registered-backends)
4597 (org-export-define-backend 'test
4598 '((headline . (lambda (h _c i) (org-export-data-with-backend
4599 (org-element-property :title h)
4600 (org-export-toc-entry-backend 'test)
4601 i)))))
4602 (org-export-as 'test)))))
4603 (should
4604 (equal "H https://orgmode.org\n"
4605 (org-test-with-temp-text "* H [[https://orgmode.org]]"
4606 (let (org-export-registered-backends)
4607 (org-export-define-backend 'test
4608 '((headline . (lambda (h _c i) (org-export-data-with-backend
4609 (org-element-property :title h)
4610 (org-export-toc-entry-backend 'test)
4611 i)))))
4612 (org-export-as 'test)))))
4613 ;; Replace radio targets with contents.
4614 (should
4615 (equal "H radio\n"
4616 (org-test-with-temp-text "* H <<<radio>>>"
4617 (let (org-export-registered-backends)
4618 (org-export-define-backend 'test
4619 '((headline . (lambda (h _c i) (org-export-data-with-backend
4620 (org-element-property :title h)
4621 (org-export-toc-entry-backend 'test)
4622 i)))))
4623 (org-export-as 'test)))))
4624 ;; With optional argument TRANSCODERS, specify other
4625 ;; transformations.
4626 (should
4627 (equal "H bold\n"
4628 (org-test-with-temp-text "* H *bold*"
4629 (let (org-export-registered-backends)
4630 (org-export-define-backend 'test
4631 '((headline . (lambda (h _c i) (org-export-data-with-backend
4632 (org-element-property :title h)
4633 (org-export-toc-entry-backend 'test
4634 '(bold . (lambda (_b c _i) c)))
4635 i)))))
4636 (org-export-as 'test))))))
4640 ;;; Templates
4642 (ert-deftest test-org-export/inner-template ()
4643 "Test `inner-template' translator specifications."
4644 (should
4645 (equal "Success!"
4646 (org-test-with-temp-text "* Headline"
4647 (org-export-as
4648 (org-export-create-backend
4649 :transcoders
4650 '((inner-template . (lambda (contents info) "Success!"))
4651 (headline . (lambda (h c i) "Headline"))))))))
4652 ;; Inner template is applied even in a "body-only" export.
4653 (should
4654 (equal "Success!"
4655 (org-test-with-temp-text "* Headline"
4656 (org-export-as
4657 (org-export-create-backend
4658 :transcoders '((inner-template . (lambda (c i) "Success!"))
4659 (headline . (lambda (h c i) "Headline"))))
4660 nil nil 'body-only)))))
4662 (ert-deftest test-org-export/template ()
4663 "Test `template' translator specifications."
4664 (should
4665 (equal "Success!"
4666 (org-test-with-temp-text "* Headline"
4667 (org-export-as
4668 (org-export-create-backend
4669 :transcoders '((template . (lambda (contents info) "Success!"))
4670 (headline . (lambda (h c i) "Headline"))))))))
4671 ;; Template is not applied in a "body-only" export.
4672 (should-not
4673 (equal "Success!"
4674 (org-test-with-temp-text "* Headline"
4675 (org-export-as
4676 (org-export-create-backend
4677 :transcoders '((template . (lambda (contents info) "Success!"))
4678 (headline . (lambda (h c i) "Headline"))))
4679 nil nil 'body-only)))))
4683 ;;; Topology
4685 (ert-deftest test-org-export/get-next-element ()
4686 "Test `org-export-get-next-element' specifications."
4687 ;; Standard test.
4688 (should
4689 (equal "b"
4690 (org-test-with-parsed-data "* Headline\n*a* b"
4691 (org-export-get-next-element
4692 (org-element-map tree 'bold 'identity info t) info))))
4693 ;; Return nil when no previous element.
4694 (should-not
4695 (org-test-with-parsed-data "* Headline\na *b*"
4696 (org-export-get-next-element
4697 (org-element-map tree 'bold 'identity info t) info)))
4698 ;; Non-exportable elements are ignored.
4699 (should-not
4700 (let ((org-export-with-timestamps nil))
4701 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
4702 (org-export-get-next-element
4703 (org-element-map tree 'entity 'identity info t) info))))
4704 ;; Find next element in secondary strings.
4705 (should
4706 (eq 'verbatim
4707 (org-test-with-parsed-data "* a =verb="
4708 (org-element-type
4709 (org-export-get-next-element
4710 (org-element-map tree 'plain-text 'identity info t) info)))))
4711 (should
4712 (eq 'verbatim
4713 (org-test-with-parsed-data "* /italic/ =verb="
4714 (org-element-type
4715 (org-export-get-next-element
4716 (org-element-map tree 'italic 'identity info t) info)))))
4717 ;; Find next element in document keywords.
4718 (should
4719 (eq 'verbatim
4720 (org-test-with-parsed-data "#+TITLE: a =verb="
4721 (org-element-type
4722 (org-export-get-next-element
4723 (org-element-map
4724 (plist-get info :title) 'plain-text 'identity info t) info)))))
4725 ;; Find next element in parsed affiliated keywords.
4726 (should
4727 (eq 'verbatim
4728 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
4729 (org-element-type
4730 (org-export-get-next-element
4731 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4732 ;; With optional argument N, return a list containing all the
4733 ;; following elements.
4734 (should
4735 (equal
4736 '(bold code underline)
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 t)))))
4741 ;; When N is a positive integer, return a list containing up to
4742 ;; N following elements.
4743 (should
4744 (equal
4745 '(bold code)
4746 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4747 (mapcar 'car
4748 (org-export-get-next-element
4749 (org-element-map tree 'italic 'identity info t) info 2))))))
4751 (ert-deftest test-org-export/get-previous-element ()
4752 "Test `org-export-get-previous-element' specifications."
4753 ;; Standard test.
4754 (should
4755 (equal "a "
4756 (org-test-with-parsed-data "* Headline\na *b*"
4757 (org-export-get-previous-element
4758 (org-element-map tree 'bold 'identity info t) info))))
4759 ;; Return nil when no previous element.
4760 (should-not
4761 (org-test-with-parsed-data "* Headline\n*a* b"
4762 (org-export-get-previous-element
4763 (org-element-map tree 'bold 'identity info t) info)))
4764 ;; Non-exportable elements are ignored.
4765 (should-not
4766 (let ((org-export-with-timestamps nil))
4767 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
4768 (org-export-get-previous-element
4769 (org-element-map tree 'entity 'identity info t) info))))
4770 ;; Find previous element in secondary strings.
4771 (should
4772 (eq 'verbatim
4773 (org-test-with-parsed-data "* =verb= a"
4774 (org-element-type
4775 (org-export-get-previous-element
4776 (org-element-map tree 'plain-text 'identity info t) info)))))
4777 (should
4778 (eq 'verbatim
4779 (org-test-with-parsed-data "* =verb= /italic/"
4780 (org-element-type
4781 (org-export-get-previous-element
4782 (org-element-map tree 'italic 'identity info t) info)))))
4783 ;; Find previous element in document keywords.
4784 (should
4785 (eq 'verbatim
4786 (org-test-with-parsed-data "#+TITLE: =verb= a"
4787 (org-element-type
4788 (org-export-get-previous-element
4789 (org-element-map
4790 (plist-get info :title) 'plain-text 'identity info t) info)))))
4791 ;; Find previous element in parsed affiliated keywords.
4792 (should
4793 (eq 'verbatim
4794 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
4795 (org-element-type
4796 (org-export-get-previous-element
4797 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4798 ;; With optional argument N, return a list containing up to
4799 ;; N previous elements.
4800 (should
4801 (equal '(underline italic bold)
4802 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4803 (mapcar 'car
4804 (org-export-get-previous-element
4805 (org-element-map tree 'code 'identity info t) info t)))))
4806 ;; When N is a positive integer, return a list containing up to
4807 ;; N previous elements.
4808 (should
4809 (equal '(italic bold)
4810 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4811 (mapcar 'car
4812 (org-export-get-previous-element
4813 (org-element-map tree 'code 'identity info t) info 2))))))
4816 (provide 'test-ox)
4817 ;;; test-org-export.el end here