lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / testing / lisp / test-ox.el
blob43637b92648cc6cf53d9fa3406a218e903bc9d71
1 ;;; test-ox.el --- Tests for ox.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2016, 2019 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-export/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))))))
3041 ;; If an image link was included from another file, make sure to
3042 ;; shift any relative path accordingly.
3043 (should
3044 (string-prefix-p
3045 "file:org-includee-"
3046 (let* ((subdir (make-temp-file "org-includee-" t))
3047 (includee (expand-file-name "includee.org" subdir))
3048 (includer (make-temp-file "org-includer-")))
3049 (write-region "file:foo.png" nil includee)
3050 (write-region (format "#+INCLUDE: %S"
3051 (file-relative-name includee
3052 temporary-file-directory))
3053 nil includer)
3054 (let ((buffer (find-file-noselect includer t)))
3055 (unwind-protect
3056 (with-current-buffer buffer
3057 (org-export-as
3058 (org-export-create-backend
3059 :transcoders
3060 '((section . (lambda (_s c _i) c))
3061 (paragraph . (lambda (_p c _i) c))
3062 (link . (lambda (l c _i) (org-element-link-interpreter l c))))
3063 :filters
3064 '((:filter-parse-tree
3065 (lambda (d _b i) (org-export-insert-image-links d i)))))))
3066 (when (buffer-live-p buffer)
3067 (with-current-buffer buffer (set-buffer-modified-p nil))
3068 (kill-buffer buffer))
3069 (when (file-exists-p subdir) (delete-directory subdir t))
3070 (when (file-exists-p includer) (delete-file includer)))))))
3071 (should
3072 (string-match-p
3073 "file:org-includee-.+?foo\\.png"
3074 (let* ((subdir (make-temp-file "org-includee-" t))
3075 (includee (expand-file-name "includee.org" subdir))
3076 (includer (make-temp-file "org-includer-")))
3077 (write-region "[[https://orgmode.org][file:foo.png]]" nil includee)
3078 (write-region (format "#+INCLUDE: %S"
3079 (file-relative-name includee
3080 temporary-file-directory))
3081 nil includer)
3082 (let ((buffer (find-file-noselect includer t)))
3083 (unwind-protect
3084 (with-current-buffer buffer
3085 (org-export-as
3086 (org-export-create-backend
3087 :transcoders
3088 '((section . (lambda (_s c _i) c))
3089 (paragraph . (lambda (_p c _i) c))
3090 (link . (lambda (l c _i) (org-element-link-interpreter l c))))
3091 :filters
3092 '((:filter-parse-tree
3093 (lambda (d _b i) (org-export-insert-image-links d i)))))))
3094 (when (buffer-live-p buffer)
3095 (with-current-buffer buffer (set-buffer-modified-p nil))
3096 (kill-buffer buffer))
3097 (when (file-exists-p subdir) (delete-directory subdir t))
3098 (when (file-exists-p includer) (delete-file includer)))))))
3099 (should
3100 (string-match-p
3101 "file:org-includee.+?file:org-includee"
3102 (let* ((subdir (make-temp-file "org-includee-" t))
3103 (includee (expand-file-name "includee.org" subdir))
3104 (includer (make-temp-file "org-includer-")))
3105 (write-region "[[file:bar.png][file:foo.png]]" nil includee)
3106 (write-region (format "#+INCLUDE: %S"
3107 (file-relative-name includee
3108 temporary-file-directory))
3109 nil includer)
3110 (let ((buffer (find-file-noselect includer t)))
3111 (unwind-protect
3112 (with-current-buffer buffer
3113 (org-export-as
3114 (org-export-create-backend
3115 :transcoders
3116 '((section . (lambda (_s c _i) c))
3117 (paragraph . (lambda (_p c _i) c))
3118 (link . (lambda (l c _i) (org-element-link-interpreter l c))))
3119 :filters
3120 '((:filter-parse-tree
3121 (lambda (d _b i) (org-export-insert-image-links d i)))))))
3122 (when (buffer-live-p buffer)
3123 (with-current-buffer buffer (set-buffer-modified-p nil))
3124 (kill-buffer buffer))
3125 (when (file-exists-p subdir) (delete-directory subdir t))
3126 (when (file-exists-p includer) (delete-file includer))))))))
3128 (ert-deftest test-org-export/fuzzy-link ()
3129 "Test fuzzy links specifications."
3130 ;; Link to an headline should return headline's number.
3131 (should
3132 ;; Note: Headline's number is in fact a list of numbers.
3133 (equal '(2)
3134 (org-test-with-parsed-data
3135 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
3136 (org-element-map tree 'link
3137 (lambda (link)
3138 (org-export-get-ordinal
3139 (org-export-resolve-fuzzy-link link info) info)) info t))))
3140 ;; Link to a target in an item should return item's number.
3141 (should
3142 ;; Note: Item's number is in fact a list of numbers.
3143 (equal '(1 2)
3144 (org-test-with-parsed-data
3145 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
3146 (org-element-map tree 'link
3147 (lambda (link)
3148 (org-export-get-ordinal
3149 (org-export-resolve-fuzzy-link link info) info)) info t))))
3150 ;; Link to a target in a footnote should return footnote's number.
3151 (should
3152 (equal '(2 3)
3153 (org-test-with-parsed-data "
3154 Paragraph[fn:1][fn:2][fn:lbl3:C<<target>>][[test]][[target]]
3155 \[fn:1] A
3157 \[fn:2] <<test>>B"
3158 (org-element-map tree 'link
3159 (lambda (link)
3160 (org-export-get-ordinal
3161 (org-export-resolve-fuzzy-link link info) info)) info))))
3162 ;; Link to a named element should return sequence number of that
3163 ;; element.
3164 (should
3165 (= 2
3166 (org-test-with-parsed-data
3167 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
3168 (org-element-map tree 'link
3169 (lambda (link)
3170 (org-export-get-ordinal
3171 (org-export-resolve-fuzzy-link link info) info)) info t))))
3172 ;; Link to a target not within an item, a table, a footnote
3173 ;; reference or definition should return section number.
3174 (should
3175 (equal '(2)
3176 (org-test-with-parsed-data
3177 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
3178 (org-element-map tree 'link
3179 (lambda (link)
3180 (org-export-get-ordinal
3181 (org-export-resolve-fuzzy-link link info) info)) info t))))
3182 ;; Space are not significant when matching a fuzzy link.
3183 (should
3184 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
3185 (org-element-map tree 'link
3186 (lambda (link) (org-export-resolve-fuzzy-link link info))
3187 info t)))
3188 ;; Statistics cookies are ignored for headline match.
3189 (should
3190 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
3191 (org-element-map tree 'link
3192 (lambda (link) (org-export-resolve-fuzzy-link link info))
3193 info t)))
3194 (should
3195 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
3196 (org-element-map tree 'link
3197 (lambda (link) (org-export-resolve-fuzzy-link link info))
3198 info t))))
3200 (defun test-org-gen-loc-list(text type)
3201 (org-test-with-parsed-data text
3202 (org-element-map tree type
3203 (lambda (el) (or (org-export-get-loc el info) 'no-loc)))))
3205 (ert-deftest test-org-export/get-loc ()
3206 "Test `org-export-get-loc' specifications."
3207 (should
3208 ;; "-n" resets line number.
3209 (equal '(0)
3210 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n\n Text\n#+END_EXAMPLE"
3211 'example-block)))
3212 ;; The first "+n" has 0 lines before it
3213 (should
3214 (equal '(0)
3215 (test-org-gen-loc-list "#+BEGIN_EXAMPLE +n\n Text\n#+END_EXAMPLE"
3216 'example-block)))
3217 ;; "-n 10" resets line number but has "9 lines" before it.
3218 (should
3219 (equal '(9)
3220 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n 10\n Text\n#+END_EXAMPLE"
3221 'example-block)))
3222 ;; -n10 with two lines then +n 15
3223 (should
3224 (equal '(9 25)
3225 (test-org-gen-loc-list "
3226 #+BEGIN_EXAMPLE -n 10
3227 Text_10
3228 Second line(11)
3229 #+END_EXAMPLE
3230 #+BEGIN_EXAMPLE +n 15
3231 Text line (11 + 15)
3232 #+END_EXAMPLE"
3233 'example-block)))
3234 (should
3235 (equal '(9 19 0)
3236 (test-org-gen-loc-list "
3237 #+BEGIN_EXAMPLE -n 10
3238 Text
3239 #+END_EXAMPLE
3240 #+BEGIN_EXAMPLE +n 10
3241 Text
3242 #+END_EXAMPLE
3244 #+BEGIN_EXAMPLE -n
3245 Text
3246 #+END_EXAMPLE"
3247 'example-block)))
3248 ;; an Example Block without -n does not add to the line count.
3249 (should
3250 (equal '(9 no-loc 19)
3251 (test-org-gen-loc-list "
3252 #+BEGIN_EXAMPLE -n 10
3253 Text
3254 #+END_EXAMPLE
3255 #+BEGIN_EXAMPLE
3256 Text
3257 #+END_EXAMPLE
3258 #+BEGIN_EXAMPLE +n 10
3259 Text
3260 #+END_EXAMPLE"
3261 'example-block)))
3262 ;; "-n" resets line number.
3263 (should
3264 (equal
3265 '(0)
3266 (test-org-gen-loc-list "#+BEGIN_SRC emacs-lisp -n \n (- 1 1) \n#+END_SRC"
3267 'src-block)))
3268 ;; The first "+n" has 0 lines before it.
3269 (should
3270 (equal '(0)
3271 (test-org-gen-loc-list
3272 "#+BEGIN_SRC emacs-lisp +n \n (+ 0 (- 1 1))\n#+END_SRC"
3273 'src-block)))
3274 ;; "-n 10" resets line number but has "9 lines" before it.
3275 (should
3276 (equal '(9)
3277 (test-org-gen-loc-list
3278 "#+BEGIN_SRC emacs-lisp -n 10\n (- 10 1)\n#+END_SRC"
3279 'src-block)))
3280 (should
3281 (equal '(9 25)
3282 (test-org-gen-loc-list "
3283 #+BEGIN_SRC emacs-lisp -n 10
3284 (- 10 1)
3285 (+ (- 10 1) 1)
3286 #+END_SRC
3287 #+BEGIN_SRC emacs-lisp +n 15
3288 (+ (- 10 1) 2 (- 15 1))
3289 #+END_SRC"
3290 'src-block)))
3291 (should
3292 (equal '(9 19 0)
3293 (test-org-gen-loc-list "
3294 #+BEGIN_SRC emacs-lisp -n 10
3295 (- 10 1)
3296 #+END_SRC
3297 #+BEGIN_SRC emacs-lisp +n 10
3298 (+ (- 10 1) 1 (- 10 1))
3299 #+END_SRC
3300 #+BEGIN_SRC emacs-lisp -n
3301 (- 1 1)
3302 #+END_SRC"
3303 'src-block)))
3304 ;; A SRC Block without -n does not add to the line count.
3305 (should
3306 (equal '(9 no-loc 19)
3307 (test-org-gen-loc-list
3308 "#+BEGIN_SRC emacs-lisp -n 10
3309 (+ (-10 1) 1)
3310 #+END_SRC
3311 #+BEGIN_SRC emacs-lisp
3312 (+ 2 2)
3313 #+END_SRC
3314 #+BEGIN_SRC emacs-lisp +n 10
3315 (+ (- 10 1) 1 (- 10 1))
3316 #+END_SRC"
3317 'src-block))))
3319 (ert-deftest test-org-export/resolve-coderef ()
3320 "Test `org-export-resolve-coderef' specifications."
3321 (let ((org-coderef-label-format "(ref:%s)"))
3322 ;; A link to a "-n -k -r" block returns line number.
3323 (should
3324 (= 1
3325 (org-test-with-parsed-data
3326 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3327 (org-export-resolve-coderef "coderef" info))))
3328 (should
3329 (= 10
3330 (org-test-with-parsed-data
3331 "#+BEGIN_EXAMPLE -n 10 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3332 (org-export-resolve-coderef "coderef" info))))
3333 (should
3334 (= 135
3335 (org-test-with-parsed-data
3336 "#+BEGIN_EXAMPLE -n 10 -k -r\nText \n#+END_EXAMPLE\n
3337 #+BEGIN_EXAMPLE +n 125 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3338 (org-export-resolve-coderef "coderef" info))))
3339 (should
3340 (= 1
3341 (org-test-with-parsed-data
3342 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3343 (org-export-resolve-coderef "coderef" info))))
3344 (should
3345 (= 10
3346 (org-test-with-parsed-data
3347 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3348 (org-export-resolve-coderef "coderef" info))))
3349 (should
3350 (= 135
3351 (org-test-with-parsed-data
3352 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) \n#+END_SRC\n
3353 #+BEGIN_SRC emacs-lisp +n 125 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3354 (org-export-resolve-coderef "coderef" info))))
3355 ;; A link to a "-n -r" block returns line number.
3356 (should
3357 (= 1
3358 (org-test-with-parsed-data
3359 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
3360 (org-export-resolve-coderef "coderef" info))))
3361 (should
3362 (= 10
3363 (org-test-with-parsed-data
3364 "#+BEGIN_EXAMPLE -n 10 -r\nText (ref:coderef)\n#+END_EXAMPLE"
3365 (org-export-resolve-coderef "coderef" info))))
3366 (should
3367 (= 135
3368 (org-test-with-parsed-data
3369 "#+BEGIN_EXAMPLE +n 10 -r\nText \n#+END_EXAMPLE
3370 #+BEGIN_EXAMPLE +n 125 -r\nText (ref:coderef)\n#+END_EXAMPLE"
3371 (org-export-resolve-coderef "coderef" info))))
3373 (should
3374 (= 1
3375 (org-test-with-parsed-data
3376 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3377 (org-export-resolve-coderef "coderef" info))))
3378 (should
3379 (= 10
3380 (org-test-with-parsed-data
3381 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3382 (org-export-resolve-coderef "coderef" info))))
3383 (should
3384 (= 135
3385 (org-test-with-parsed-data
3386 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) \n#+END_SRC
3387 #+BEGIN_SRC emacs-lisp +n125 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3388 (org-export-resolve-coderef "coderef" info))))
3389 ;; A link to a "-n" block returns coderef.
3390 (should
3391 (equal "coderef"
3392 (org-test-with-parsed-data
3393 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3394 (org-export-resolve-coderef "coderef" info))))
3395 (should
3396 (equal "coderef"
3397 (org-test-with-parsed-data
3398 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
3399 (org-export-resolve-coderef "coderef" info))))
3400 ;; A link to a "-r" block returns line number.
3401 (should
3402 (= 1
3403 (org-test-with-parsed-data
3404 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3405 (org-export-resolve-coderef "coderef" info))))
3406 (should
3407 (= 1
3408 (org-test-with-parsed-data
3409 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
3410 (org-export-resolve-coderef "coderef" info))))
3411 ;; A link to a block without a switch returns coderef.
3412 (should
3413 (equal "coderef"
3414 (org-test-with-parsed-data
3415 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3416 (org-export-resolve-coderef "coderef" info))))
3417 (org-test-with-parsed-data
3418 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
3419 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
3420 ;; Correctly handle continued line numbers. A "+n" switch should
3421 ;; resume numbering from previous block with numbered lines,
3422 ;; ignoring blocks not numbering lines in the process. A "-n"
3423 ;; switch resets count.
3424 (should
3425 (equal '(2 1)
3426 (org-test-with-parsed-data "
3427 #+BEGIN_EXAMPLE -n
3428 Text.
3429 #+END_EXAMPLE
3431 #+BEGIN_SRC emacs-lisp
3432 \(- 1 1)
3433 #+END_SRC
3435 #+BEGIN_SRC emacs-lisp +n -r
3436 \(+ 1 1) (ref:addition)
3437 #+END_SRC
3439 #+BEGIN_EXAMPLE -n -r
3440 Another text. (ref:text)
3441 #+END_EXAMPLE"
3442 (list (org-export-resolve-coderef "addition" info)
3443 (org-export-resolve-coderef "text" info)))))
3444 ;; Recognize coderef with user-specified syntax.
3445 (should
3446 (equal
3447 "text"
3448 (org-test-with-parsed-data
3449 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
3450 (org-export-resolve-coderef "text" info))))
3451 ;; Unresolved coderefs raise a `org-link-broken' signal.
3452 (should
3453 (condition-case nil
3454 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
3455 (org-export-resolve-coderef "unknown" info))
3456 (org-link-broken t)))))
3458 (ert-deftest test-org-export/resolve-fuzzy-link ()
3459 "Test `org-export-resolve-fuzzy-link' specifications."
3460 ;; Match target objects.
3461 (should
3462 (org-test-with-parsed-data "<<target>> [[target]]"
3463 (org-export-resolve-fuzzy-link
3464 (org-element-map tree 'link 'identity info t) info)))
3465 ;; Match named elements.
3466 (should
3467 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
3468 (org-export-resolve-fuzzy-link
3469 (org-element-map tree 'link 'identity info t) info)))
3470 ;; Match exact headline's name.
3471 (should
3472 (org-test-with-parsed-data "* My headline\n[[My headline]]"
3473 (org-export-resolve-fuzzy-link
3474 (org-element-map tree 'link 'identity info t) info)))
3475 ;; Targets objects have priority over headline titles.
3476 (should
3477 (eq 'target
3478 (org-test-with-parsed-data "* target\n<<target>>[[target]]"
3479 (org-element-type
3480 (org-export-resolve-fuzzy-link
3481 (org-element-map tree 'link 'identity info t) info)))))
3482 ;; Named elements have priority over headline titles.
3483 (should
3484 (eq 'paragraph
3485 (org-test-with-parsed-data
3486 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
3487 (org-element-type
3488 (org-export-resolve-fuzzy-link
3489 (org-element-map tree 'link 'identity info t) info)))))
3490 ;; If link's path starts with a "*", only match headline titles,
3491 ;; though.
3492 (should
3493 (eq 'headline
3494 (org-test-with-parsed-data
3495 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
3496 (org-element-type
3497 (org-export-resolve-fuzzy-link
3498 (org-element-map tree 'link 'identity info t) info)))))
3499 ;; Raise a `org-link-broken' signal if no match.
3500 (should
3501 (org-test-with-parsed-data "[[target]]"
3502 (condition-case nil
3503 (org-export-resolve-fuzzy-link
3504 (org-element-map tree 'link #'identity info t) info)
3505 (org-link-broken t))))
3506 ;; Match fuzzy link even when before first headline.
3507 (should
3508 (eq 'headline
3509 (org-test-with-parsed-data "[[hl]]\n* hl"
3510 (org-element-type
3511 (org-export-resolve-fuzzy-link
3512 (org-element-map tree 'link 'identity info t) info)))))
3513 ;; Handle escaped fuzzy links.
3514 (should
3515 (org-test-with-parsed-data "* [foo]\n[[[foo\\]]]"
3516 (org-export-resolve-fuzzy-link
3517 (org-element-map tree 'link #'identity info t) info))))
3519 (ert-deftest test-org-export/resolve-id-link ()
3520 "Test `org-export-resolve-id-link' specifications."
3521 ;; Regular test for custom-id link.
3522 (should
3523 (equal '("Headline1")
3524 (org-test-with-parsed-data "* Headline1
3525 :PROPERTIES:
3526 :CUSTOM_ID: test
3527 :END:
3528 * Headline 2
3529 \[[#test]]"
3530 (org-element-property
3531 :title
3532 (org-export-resolve-id-link
3533 (org-element-map tree 'link 'identity info t) info)))))
3534 ;; Raise a `org-link-broken' signal on failing searches.
3535 (should
3536 (org-test-with-parsed-data "* Headline1
3537 :PROPERTIES:
3538 :CUSTOM_ID: test
3539 :END:
3540 * Headline 2
3541 \[[#no-match]]"
3542 (condition-case nil
3543 (org-export-resolve-id-link
3544 (org-element-map tree 'link #'identity info t) info)
3545 (org-link-broken t))))
3546 ;; Test for internal id target.
3547 (should
3548 (equal '("Headline1")
3549 (org-test-with-parsed-data "* Headline1
3550 :PROPERTIES:
3551 :ID: aaaa
3552 :END:
3553 * Headline 2
3554 \[[id:aaaa]]"
3555 (org-element-property
3556 :title
3557 (org-export-resolve-id-link
3558 (org-element-map tree 'link 'identity info t) info)))))
3559 ;; Test for external id target.
3560 (should
3561 (equal
3562 "external-file"
3563 (org-test-with-parsed-data "[[id:aaaa]]"
3564 (org-export-resolve-id-link
3565 (org-element-map tree 'link 'identity info t)
3566 (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
3568 (ert-deftest test-org-export/resolve-radio-link ()
3569 "Test `org-export-resolve-radio-link' specifications."
3570 ;; Standard test.
3571 (should
3572 (org-test-with-temp-text "<<<radio>>> radio"
3573 (org-update-radio-target-regexp)
3574 (let* ((tree (org-element-parse-buffer))
3575 (info `(:parse-tree ,tree)))
3576 (org-export-resolve-radio-link
3577 (org-element-map tree 'link 'identity info t)
3578 info))))
3579 ;; Radio targets are case-insensitive.
3580 (should
3581 (org-test-with-temp-text "<<<RADIO>>> radio"
3582 (org-update-radio-target-regexp)
3583 (let* ((tree (org-element-parse-buffer))
3584 (info `(:parse-tree ,tree)))
3585 (org-export-resolve-radio-link
3586 (org-element-map tree 'link 'identity info t)
3587 info))))
3588 ;; Radio target with objects.
3589 (should
3590 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
3591 (org-update-radio-target-regexp)
3592 (let* ((tree (org-element-parse-buffer))
3593 (info `(:parse-tree ,tree)))
3594 (org-export-resolve-radio-link
3595 (org-element-map tree 'link 'identity info t)
3596 info))))
3597 ;; Radio target with objects at its beginning.
3598 (should
3599 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
3600 (org-update-radio-target-regexp)
3601 (let* ((tree (org-element-parse-buffer))
3602 (info `(:parse-tree ,tree)))
3603 (org-export-resolve-radio-link
3604 (org-element-map tree 'link 'identity info t)
3605 info))))
3606 ;; Radio link next to an apostrophe.
3607 (should
3608 (org-test-with-temp-text "<<<radio>>> radio's"
3609 (org-update-radio-target-regexp)
3610 (let* ((tree (org-element-parse-buffer))
3611 (info `(:parse-tree ,tree)))
3612 (org-export-resolve-radio-link
3613 (org-element-map tree 'link 'identity info t)
3614 info))))
3615 ;; Multiple radio targets.
3616 (should
3617 (equal '("radio1" "radio2")
3618 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
3619 (org-update-radio-target-regexp)
3620 (let* ((tree (org-element-parse-buffer))
3621 (info `(:parse-tree ,tree)))
3622 (org-element-map tree 'link
3623 (lambda (link)
3624 (org-element-property
3625 :value (org-export-resolve-radio-link link info)))
3626 info)))))
3627 ;; Radio target is whitespace insensitive.
3628 (should
3629 (org-test-with-temp-text "<<<a radio>>> a\n radio"
3630 (org-update-radio-target-regexp)
3631 (let* ((tree (org-element-parse-buffer))
3632 (info `(:parse-tree ,tree)))
3633 (org-element-map tree 'link
3634 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
3636 (ert-deftest test-org-export/file-uri ()
3637 "Test `org-export-file-uri' specifications."
3638 ;; Preserve relative filenames.
3639 (should (equal "relative.org" (org-export-file-uri "relative.org")))
3640 ;; Local files start with "file://"
3641 (should (equal (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "/local.org"))
3642 (org-export-file-uri "/local.org")))
3643 ;; Remote files start with "file://"
3644 (should (equal "file://ssh:myself@some.where:papers/last.pdf"
3645 (org-export-file-uri "/ssh:myself@some.where:papers/last.pdf")))
3646 (should (equal "file://localhost/etc/fstab"
3647 (org-export-file-uri "//localhost/etc/fstab")))
3648 ;; Expand filename starting with "~".
3649 (should (equal (org-export-file-uri "~/file.org")
3650 (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "~/file.org")))))
3652 (ert-deftest test-org-export/get-reference ()
3653 "Test `org-export-get-reference' specifications."
3654 (should
3655 (org-test-with-parsed-data "* Headline"
3656 (org-export-get-reference (org-element-map tree 'headline #'identity nil t)
3657 info)))
3658 ;; For a given element always return the same reference.
3659 (should
3660 (org-test-with-parsed-data "* Headline"
3661 (let ((headline (org-element-map tree 'headline #'identity nil t)))
3662 (equal (org-export-get-reference headline info)
3663 (org-export-get-reference headline info)))))
3664 ;; References get through local export back-ends.
3665 (should
3666 (org-test-with-parsed-data "* Headline"
3667 (let ((headline (org-element-map tree 'headline #'identity nil t))
3668 (backend
3669 (org-export-create-backend
3670 :transcoders
3671 '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
3672 (equal (org-trim (org-export-data-with-backend headline backend info))
3673 (org-export-get-reference headline info)))))
3674 (should
3675 (org-test-with-parsed-data "* Headline"
3676 (let ((headline (org-element-map tree 'headline #'identity nil t))
3677 (backend
3678 (org-export-create-backend
3679 :transcoders
3680 '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
3681 (equal (org-export-with-backend backend headline nil info)
3682 (org-export-get-reference headline info)))))
3683 ;; Use search cells defined in `:crossrefs'. However, handle
3684 ;; duplicate search cells.
3685 (should
3686 (equal "org0000001"
3687 (org-test-with-parsed-data "* Headline"
3688 (let* ((headline (org-element-map tree 'headline #'identity nil t))
3689 (search-cell (car (org-export-search-cells headline))))
3690 (setq info
3691 (plist-put info :crossrefs (list (cons search-cell 1))))
3692 (org-export-get-reference headline info)))))
3693 (should-not
3694 (equal '("org0000001" "org0000001")
3695 (org-test-with-parsed-data "* H\n** H"
3696 (org-element-map tree 'headline
3697 (lambda (h)
3698 (let* ((search-cell (car (org-export-search-cells h)))
3699 (info (plist-put info :crossrefs
3700 (list (cons search-cell 1)))))
3701 (org-export-get-reference h info))))))))
3704 ;;; Pseudo objects and pseudo elements
3706 (ert-deftest test-org-export/pseudo-elements ()
3707 "Test exporting pseudo-elements."
3708 ;; Handle blank lines after pseudo-elements. In particular, do not
3709 ;; replace them with white spaces.
3710 (should
3711 (equal "contents\n\nparagraph\n"
3712 (let ((backend (org-export-create-backend
3713 :transcoders
3714 '((pseudo-element . (lambda (_p c _i) c))
3715 (paragraph . (lambda (_p c _i) c))
3716 (plain-text . (lambda (c _i) c)))))
3717 (element '(pseudo-element (:post-blank 1) "contents"))
3718 (paragraph '(paragraph nil "paragraph"))
3719 (data '(org-data nil)))
3720 (org-element-adopt-elements data element paragraph)
3721 (org-export-data-with-backend data backend nil)))))
3723 (ert-deftest test-org-export/pseudo-objects ()
3724 "Test exporting pseudo-objects."
3725 ;; Handle blank spaces after pseudo-objects. In particular, do not
3726 ;; replace them with newlines.
3727 (should
3728 (equal "begin x end\n"
3729 (let ((backend (org-export-create-backend
3730 :transcoders
3731 '((pseudo-object . (lambda (_p c _i) c))
3732 (paragraph . (lambda (_p c _i) c))
3733 (plain-text . (lambda (c _i) c)))))
3734 (object '(pseudo-object (:post-blank 1) "x"))
3735 (paragraph '(paragraph nil)))
3736 (org-element-adopt-elements paragraph "begin " object "end")
3737 (org-export-data-with-backend paragraph backend nil)))))
3740 ;;; Src-block and example-block
3742 (ert-deftest test-org-export/unravel-code ()
3743 "Test `org-export-unravel-code' function."
3744 ;; Code without reference.
3745 (should
3746 (equal '("(+ 1 1)")
3747 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
3748 (org-export-unravel-code (org-element-at-point)))))
3749 ;; Code with reference.
3750 (should
3751 (equal '("(+ 1 1)" (1 . "test"))
3752 (org-test-with-temp-text
3753 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
3754 (let ((org-coderef-label-format "(ref:%s)"))
3755 (org-export-unravel-code (org-element-at-point))))))
3756 ;; Code with user-defined reference.
3757 (should
3758 (equal
3759 '("(+ 1 1)" (1 . "test"))
3760 (org-test-with-temp-text
3761 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
3762 (let ((org-coderef-label-format "(ref:%s)"))
3763 (org-export-unravel-code (org-element-at-point))))))
3764 ;; Code references keys are relative to the current block.
3765 (should
3766 (equal '("(+ 2 2)\n(+ 3 3)" (2 . "one"))
3767 (org-test-with-temp-text "
3768 #+BEGIN_EXAMPLE -n
3769 \(+ 1 1)
3770 #+END_EXAMPLE
3771 #+BEGIN_EXAMPLE +n
3772 \(+ 2 2)
3773 \(+ 3 3) (ref:one)
3774 #+END_EXAMPLE"
3775 (goto-line 5)
3776 (let ((org-coderef-label-format "(ref:%s)"))
3777 (org-export-unravel-code (org-element-at-point)))))\x14))
3779 (ert-deftest test-org-export/format-code-default ()
3780 "Test `org-export-format-code-default' specifications."
3781 ;; Preserve blank lines, even when code is empty.
3782 (should
3783 (equal "\n\n"
3784 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
3785 (org-export-format-code-default
3786 (org-element-map tree 'src-block #'identity info t) info))))
3787 ;; Likewise, preserve leading and trailing blank lines in the code.
3788 (should
3789 (equal "\n(+ 1 1)\n"
3790 (org-test-with-parsed-data
3791 "#+BEGIN_SRC emacs-lisp\n\n(+ 1 1)\n#+END_SRC"
3792 (org-export-format-code-default
3793 (org-element-map tree 'src-block #'identity info t) info))))
3794 (should
3795 (equal "(+ 1 1)\n\n"
3796 (org-test-with-parsed-data
3797 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n\n#+END_SRC"
3798 (org-export-format-code-default
3799 (org-element-map tree 'src-block #'identity info t) info))))
3800 ;; Number lines, two whitespace characters before the actual loc.
3801 (should
3802 (equal "1 a\n2 b\n"
3803 (org-test-with-parsed-data
3804 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
3805 (org-export-format-code-default
3806 (org-element-map tree 'src-block #'identity info t) info))))
3807 ;; Numbering includes blank lines.
3808 (should
3809 (equal "1 \n2 a\n3 \n4 b\n5 \n"
3810 (org-test-with-parsed-data
3811 "#+BEGIN_SRC emacs-lisp +n\n\na\n\nb\n\n#+END_SRC"
3812 (org-export-format-code-default
3813 (org-element-map tree 'src-block #'identity info t) info))))
3814 ;; Put references 6 whitespace characters after the widest line,
3815 ;; wrapped within parenthesis.
3816 (should
3817 (equal "123 (a)\n1 (b)\n"
3818 (let ((org-coderef-label-format "(ref:%s)"))
3819 (org-test-with-parsed-data
3820 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
3821 (org-export-format-code-default
3822 (org-element-map tree 'src-block #'identity info t) info))))))
3826 ;;; Smart Quotes
3828 (ert-deftest test-org-export/activate-smart-quotes ()
3829 "Test `org-export-activate-smart-quotes' specifications."
3830 ;; Double quotes: standard test.
3831 (should
3832 (equal
3833 '("some &ldquo;quoted&rdquo; text")
3834 (let ((org-export-default-language "en"))
3835 (org-test-with-parsed-data "some \"quoted\" text"
3836 (org-element-map tree 'plain-text
3837 (lambda (s) (org-export-activate-smart-quotes s :html info))
3838 info)))))
3839 ;; Opening quotes: at the beginning of a paragraph.
3840 (should
3841 (equal
3842 '("&ldquo;begin")
3843 (let ((org-export-default-language "en"))
3844 (org-test-with-parsed-data "\"begin"
3845 (org-element-map tree 'plain-text
3846 (lambda (s) (org-export-activate-smart-quotes s :html info))
3847 info)))))
3848 ;; Opening quotes: after an object.
3849 (should
3850 (equal
3851 '("&ldquo;quoted&rdquo; text")
3852 (let ((org-export-default-language "en"))
3853 (org-test-with-parsed-data "=verb= \"quoted\" text"
3854 (org-element-map tree 'plain-text
3855 (lambda (s) (org-export-activate-smart-quotes s :html info))
3856 info)))))
3857 ;; Closing quotes: at the end of a paragraph.
3858 (should
3859 (equal
3860 '("Quoted &ldquo;text&rdquo;")
3861 (let ((org-export-default-language "en"))
3862 (org-test-with-parsed-data "Quoted \"text\""
3863 (org-element-map tree 'plain-text
3864 (lambda (s) (org-export-activate-smart-quotes s :html info))
3865 info)))))
3866 ;; Inner quotes: standard test.
3867 (should
3868 (equal '("« outer « inner » outer »")
3869 (let ((org-export-default-language "fr"))
3870 (org-test-with-parsed-data "\"outer 'inner' outer\""
3871 (org-element-map tree 'plain-text
3872 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3873 info)))))
3874 ;; Inner quotes: close to special symbols.
3875 (should
3876 (equal '("« outer (« inner ») outer »")
3877 (let ((org-export-default-language "fr"))
3878 (org-test-with-parsed-data "\"outer ('inner') outer\""
3879 (org-element-map tree 'plain-text
3880 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3881 info)))))
3882 (should
3883 (equal '("« « inner » »")
3884 (let ((org-export-default-language "fr"))
3885 (org-test-with-parsed-data "\"'inner'\""
3886 (org-element-map tree 'plain-text
3887 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3888 info)))))
3889 ;; Apostrophe: standard test.
3890 (should
3891 (equal '("It « shouldn’t » fail")
3892 (let ((org-export-default-language "fr"))
3893 (org-test-with-parsed-data "It \"shouldn't\" fail"
3894 (org-element-map tree 'plain-text
3895 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3896 info)))))
3897 (should
3898 (equal '("It shouldn’t fail")
3899 (let ((org-export-default-language "fr"))
3900 (org-test-with-parsed-data "It shouldn't fail"
3901 (org-element-map tree 'plain-text
3902 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3903 info)))))
3904 ;; Apostrophe: before an object.
3905 (should
3906 (equal
3907 '("« a’" " »")
3908 (let ((org-export-default-language "fr"))
3909 (org-test-with-parsed-data "\"a'=b=\""
3910 (org-element-map tree 'plain-text
3911 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3912 info)))))
3913 ;; Apostrophe: after an object.
3914 (should
3915 (equal '("« " "’s »")
3916 (let ((org-export-default-language "fr"))
3917 (org-test-with-parsed-data "\"=code='s\""
3918 (org-element-map tree 'plain-text
3919 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3920 info)))))
3921 ;; Special case: isolated quotes.
3922 (should
3923 (equal '("&ldquo;" "&rdquo;")
3924 (let ((org-export-default-language "en"))
3925 (org-test-with-parsed-data "\"$x$\""
3926 (org-element-map tree 'plain-text
3927 (lambda (s) (org-export-activate-smart-quotes s :html info))
3928 info)))))
3929 ;; Smart quotes in secondary strings.
3930 (should
3931 (equal '("&ldquo;" "&rdquo;")
3932 (let ((org-export-default-language "en"))
3933 (org-test-with-parsed-data "* \"$x$\""
3934 (org-element-map tree 'plain-text
3935 (lambda (s) (org-export-activate-smart-quotes s :html info))
3936 info)))))
3937 ;; Smart quotes in document keywords.
3938 (should
3939 (equal '("&ldquo;" "&rdquo;")
3940 (let ((org-export-default-language "en"))
3941 (org-test-with-parsed-data "#+TITLE: \"$x$\""
3942 (org-element-map (plist-get info :title) 'plain-text
3943 (lambda (s) (org-export-activate-smart-quotes s :html info))
3944 info)))))
3945 ;; Smart quotes in parsed affiliated keywords.
3946 (should
3947 (equal '("&ldquo;" "&rdquo;" "Paragraph")
3948 (let ((org-export-default-language "en"))
3949 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
3950 (org-element-map tree 'plain-text
3951 (lambda (s) (org-export-activate-smart-quotes s :html info))
3952 info nil nil t)))))
3953 ;; Smart quotes within objects.
3954 (should
3955 (equal '("&ldquo;foo&rdquo;")
3956 (let ((org-export-default-language "en"))
3957 (org-test-with-parsed-data "| \"foo\" |"
3958 (org-element-map tree 'plain-text
3959 (lambda (s) (org-export-activate-smart-quotes s :html info))
3960 info nil nil t)))))
3961 ;; FIXME: Test failing non-interactively.
3963 ;; (should
3964 ;; (equal '("&ldquo;foo&rdquo;")
3965 ;; (let ((org-export-default-language "en"))
3966 ;; (org-test-with-parsed-data "*\"foo\"*"
3967 ;; (org-element-map tree 'plain-text
3968 ;; (lambda (s) (org-export-activate-smart-quotes s :html info))
3969 ;; info nil nil t)))))
3974 ;;; Tables
3976 (ert-deftest test-org-export/special-column ()
3977 "Test if the table's special column is properly recognized."
3978 ;; 1. First column is special if it contains only a special marking
3979 ;; characters or empty cells.
3980 (org-test-with-temp-text "
3981 | ! | 1 |
3982 | | 2 |"
3983 (should
3984 (org-export-table-has-special-column-p
3985 (org-element-map
3986 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3987 ;; 2. If the column contains anything else, it isn't special.
3988 (org-test-with-temp-text "
3989 | ! | 1 |
3990 | b | 2 |"
3991 (should-not
3992 (org-export-table-has-special-column-p
3993 (org-element-map
3994 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3995 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
3996 ;; and "!".
3997 (org-test-with-temp-text "
3998 | # | 1 |
3999 | ^ | 2 |
4000 | * | 3 |
4001 | _ | 4 |
4002 | / | 5 |
4003 | $ | 6 |
4004 | ! | 7 |"
4005 (should
4006 (org-export-table-has-special-column-p
4007 (org-element-map
4008 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
4009 ;; 4. A first column with only empty cells isn't considered as
4010 ;; special.
4011 (org-test-with-temp-text "
4012 | | 1 |
4013 | | 2 |"
4014 (should-not
4015 (org-export-table-has-special-column-p
4016 (org-element-map
4017 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
4019 (ert-deftest test-org-export/table-row-is-special-p ()
4020 "Test `org-export-table-row-is-special-p' specifications."
4021 ;; 1. A row is special if it has a special marking character in the
4022 ;; special column.
4023 (org-test-with-parsed-data "| ! | 1 |"
4024 (should
4025 (org-export-table-row-is-special-p
4026 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
4027 ;; 2. A row is special when its first field is "/"
4028 (org-test-with-parsed-data "
4029 | / | 1 |
4030 | a | b |"
4031 (should
4032 (org-export-table-row-is-special-p
4033 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
4034 ;; 3. A row only containing alignment cookies is also considered as
4035 ;; special.
4036 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
4037 (should
4038 (org-export-table-row-is-special-p
4039 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
4040 ;; 4. Everything else isn't considered as special.
4041 (org-test-with-parsed-data "| \alpha | | c |"
4042 (should-not
4043 (org-export-table-row-is-special-p
4044 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
4045 ;; 5. Table's rules are never considered as special rows.
4046 (org-test-with-parsed-data "|---+---|"
4047 (should-not
4048 (org-export-table-row-is-special-p
4049 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
4051 (ert-deftest test-org-export/has-header-p ()
4052 "Test `org-export-table-has-header-p' specifications."
4053 ;; With an header.
4054 (should
4055 (org-test-with-parsed-data "
4056 | a | b |
4057 |---+---|
4058 | c | d |"
4059 (org-export-table-has-header-p
4060 (org-element-map tree 'table 'identity info 'first-match)
4061 info)))
4062 ;; Without an header.
4063 (should-not
4064 (org-test-with-parsed-data "
4065 | a | b |
4066 | c | d |"
4067 (org-export-table-has-header-p
4068 (org-element-map tree 'table 'identity info 'first-match)
4069 info)))
4070 ;; Don't get fooled with starting and ending rules.
4071 (should-not
4072 (org-test-with-parsed-data "
4073 |---+---|
4074 | a | b |
4075 | c | d |
4076 |---+---|"
4077 (org-export-table-has-header-p
4078 (org-element-map tree 'table 'identity info 'first-match)
4079 info))))
4081 (ert-deftest test-org-export/table-row-group ()
4082 "Test `org-export-table-row-group' specifications."
4083 ;; A rule creates a new group.
4084 (should
4085 (equal '(1 rule 2)
4086 (org-test-with-parsed-data "
4087 | a | b |
4088 |---+---|
4089 | 1 | 2 |"
4090 (org-element-map tree 'table-row
4091 (lambda (row)
4092 (if (eq (org-element-property :type row) 'rule) 'rule
4093 (org-export-table-row-group row info)))))))
4094 ;; Special rows are ignored in count.
4095 (should
4096 (equal
4097 '(rule 1)
4098 (org-test-with-parsed-data "
4099 | / | < | > |
4100 |---|---+---|
4101 | | 1 | 2 |"
4102 (org-element-map tree 'table-row
4103 (lambda (row)
4104 (if (eq (org-element-property :type row) 'rule) 'rule
4105 (org-export-table-row-group row info)))
4106 info))))
4107 ;; Double rules also are ignored in count.
4108 (should
4109 (equal '(1 rule rule 2)
4110 (org-test-with-parsed-data "
4111 | a | b |
4112 |---+---|
4113 |---+---|
4114 | 1 | 2 |"
4115 (org-element-map tree 'table-row
4116 (lambda (row)
4117 (if (eq (org-element-property :type row) 'rule) 'rule
4118 (org-export-table-row-group row info))))))))
4120 (ert-deftest test-org-export/table-row-number ()
4121 "Test `org-export-table-row-number' specifications."
4122 ;; Standard test. Number is 0-indexed.
4123 (should
4124 (equal '(0 1)
4125 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
4126 (org-element-map tree 'table-row
4127 (lambda (row) (org-export-table-row-number row info)) info))))
4128 ;; Number ignores separators.
4129 (should
4130 (equal '(0 1)
4131 (org-test-with-parsed-data "
4132 | a | b | c |
4133 |---+---+---|
4134 | d | e | f |"
4135 (org-element-map tree 'table-row
4136 (lambda (row) (org-export-table-row-number row info)) info))))
4137 ;; Number ignores special rows.
4138 (should
4139 (equal '(0 1)
4140 (org-test-with-parsed-data "
4141 | / | < | > |
4142 | | b | c |
4143 |---+-----+-----|
4144 | | <c> | <c> |
4145 | | e | f |"
4146 (org-element-map tree 'table-row
4147 (lambda (row) (org-export-table-row-number row info)) info)))))
4149 (ert-deftest test-org-export/table-cell-width ()
4150 "Test `org-export-table-cell-width' specifications."
4151 ;; 1. Width is primarily determined by width cookies. If no cookie
4152 ;; is found, cell's width is nil.
4153 (org-test-with-parsed-data "
4154 | / | <l> | <6> | <l7> |
4155 | | a | b | c |"
4156 (should
4157 (equal
4158 '(nil 6 7)
4159 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
4160 (org-element-map tree 'table-cell 'identity info)))))
4161 ;; 2. The last width cookie has precedence.
4162 (org-test-with-parsed-data "
4163 | <6> |
4164 | <7> |
4165 | a |"
4166 (should
4167 (equal
4168 '(7)
4169 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
4170 (org-element-map tree 'table-cell 'identity info)))))
4171 ;; 3. Valid width cookies must have a specific row.
4172 (org-test-with-parsed-data "| <6> | cell |"
4173 (should
4174 (equal
4175 '(nil nil)
4176 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
4177 (org-element-map tree 'table-cell 'identity))))))
4179 (ert-deftest test-org-export/table-cell-alignment ()
4180 "Test `org-export-table-cell-alignment' specifications."
4181 ;; 1. Alignment is primarily determined by alignment cookies.
4182 (should
4183 (equal '(left center right)
4184 (let ((org-table-number-fraction 0.5)
4185 (org-table-number-regexp "^[0-9]+$"))
4186 (org-test-with-parsed-data "| <l> | <c> | <r> |"
4187 (mapcar (lambda (cell)
4188 (org-export-table-cell-alignment cell info))
4189 (org-element-map tree 'table-cell 'identity))))))
4190 ;; 2. The last alignment cookie has precedence.
4191 (should
4192 (equal '(right right right)
4193 (org-test-with-parsed-data "
4194 | <l8> |
4195 | cell |
4196 | <r9> |"
4197 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
4198 (org-element-map tree 'table-cell 'identity)))))
4199 ;; 3. If there's no cookie, cell's contents determine alignment.
4200 ;; A column mostly made of cells containing numbers will align
4201 ;; its cells to the right.
4202 (should
4203 (equal '(right right right)
4204 (let ((org-table-number-fraction 0.5)
4205 (org-table-number-regexp "^[0-9]+$"))
4206 (org-test-with-parsed-data "
4207 | 123 |
4208 | some text |
4209 | 12345 |"
4210 (mapcar (lambda (cell)
4211 (org-export-table-cell-alignment cell info))
4212 (org-element-map tree 'table-cell 'identity))))))
4213 ;; 4. Otherwise, they will be aligned to the left.
4214 (should
4215 (equal '(left left left)
4216 (org-test-with-parsed-data "
4217 | text |
4218 | some text |
4219 | \alpha |"
4220 (mapcar (lambda (cell)
4221 (org-export-table-cell-alignment cell info))
4222 (org-element-map tree 'table-cell 'identity info))))))
4224 (ert-deftest test-org-export/table-cell-borders ()
4225 "Test `org-export-table-cell-borders' specifications."
4226 ;; 1. Recognize various column groups indicators.
4227 (org-test-with-parsed-data "| / | < | > | <> |"
4228 (should
4229 (equal
4230 '((right bottom top) (left bottom top) (right bottom top)
4231 (right left bottom top))
4232 (mapcar (lambda (cell)
4233 (org-export-table-cell-borders cell info))
4234 (org-element-map tree 'table-cell 'identity)))))
4235 ;; 2. Accept shortcuts to define column groups.
4236 (org-test-with-parsed-data "| / | < | < |"
4237 (should
4238 (equal
4239 '((right bottom top) (right left bottom top) (left bottom top))
4240 (mapcar (lambda (cell)
4241 (org-export-table-cell-borders cell info))
4242 (org-element-map tree 'table-cell 'identity)))))
4243 ;; 3. A valid column groups row must start with a "/".
4244 (org-test-with-parsed-data "
4245 | | < |
4246 | a | b |"
4247 (should
4248 (equal '((top) (top) (bottom) (bottom))
4249 (mapcar (lambda (cell)
4250 (org-export-table-cell-borders cell info))
4251 (org-element-map tree 'table-cell 'identity)))))
4252 ;; 4. Take table rules into consideration.
4253 (org-test-with-parsed-data "
4254 | 1 |
4255 |---|
4256 | 2 |"
4257 (should
4258 (equal '((below top) (bottom above))
4259 (mapcar (lambda (cell)
4260 (org-export-table-cell-borders cell info))
4261 (org-element-map tree 'table-cell 'identity)))))
4262 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
4263 ;; (resp. `bottom' and `below') borders. Any special row is
4264 ;; ignored.
4265 (org-test-with-parsed-data "
4266 |---+----|
4267 | / | |
4268 | | 1 |
4269 |---+----|"
4270 (should
4271 (equal '((bottom below top above))
4272 (last
4273 (mapcar (lambda (cell)
4274 (org-export-table-cell-borders cell info))
4275 (org-element-map tree 'table-cell 'identity)))))))
4277 (ert-deftest test-org-export/table-dimensions ()
4278 "Test `org-export-table-dimensions' specifications."
4279 ;; 1. Standard test.
4280 (org-test-with-parsed-data "
4281 | 1 | 2 | 3 |
4282 | 4 | 5 | 6 |"
4283 (should
4284 (equal '(2 . 3)
4285 (org-export-table-dimensions
4286 (org-element-map tree 'table 'identity info 'first-match) info))))
4287 ;; 2. Ignore horizontal rules and special columns.
4288 (org-test-with-parsed-data "
4289 | / | < | > |
4290 | 1 | 2 | 3 |
4291 |---+---+---|
4292 | 4 | 5 | 6 |"
4293 (should
4294 (equal '(2 . 3)
4295 (org-export-table-dimensions
4296 (org-element-map tree 'table 'identity info 'first-match) info)))))
4298 (ert-deftest test-org-export/table-cell-address ()
4299 "Test `org-export-table-cell-address' specifications."
4300 ;; 1. Standard test: index is 0-based.
4301 (org-test-with-parsed-data "| a | b |"
4302 (should
4303 (equal '((0 . 0) (0 . 1))
4304 (org-element-map tree 'table-cell
4305 (lambda (cell) (org-export-table-cell-address cell info))
4306 info))))
4307 ;; 2. Special column isn't counted, nor are special rows.
4308 (org-test-with-parsed-data "
4309 | / | <> |
4310 | | c |"
4311 (should
4312 (equal '(0 . 0)
4313 (org-export-table-cell-address
4314 (car (last (org-element-map tree 'table-cell 'identity info)))
4315 info))))
4316 ;; 3. Tables rules do not count either.
4317 (org-test-with-parsed-data "
4318 | a |
4319 |---|
4320 | b |
4321 |---|
4322 | c |"
4323 (should
4324 (equal '(2 . 0)
4325 (org-export-table-cell-address
4326 (car (last (org-element-map tree 'table-cell 'identity info)))
4327 info))))
4328 ;; 4. Return nil for special cells.
4329 (org-test-with-parsed-data "| / | a |"
4330 (should-not
4331 (org-export-table-cell-address
4332 (org-element-map tree 'table-cell 'identity nil 'first-match)
4333 info))))
4335 (ert-deftest test-org-export/get-table-cell-at ()
4336 "Test `org-export-get-table-cell-at' specifications."
4337 ;; 1. Address ignores special columns, special rows and rules.
4338 (org-test-with-parsed-data "
4339 | / | <> |
4340 | | a |
4341 |---+----|
4342 | | b |"
4343 (should
4344 (equal '("b")
4345 (org-element-contents
4346 (org-export-get-table-cell-at
4347 '(1 . 0)
4348 (org-element-map tree 'table 'identity info 'first-match)
4349 info)))))
4350 ;; 2. Return value for a non-existent address is nil.
4351 (org-test-with-parsed-data "| a |"
4352 (should-not
4353 (org-export-get-table-cell-at
4354 '(2 . 2)
4355 (org-element-map tree 'table 'identity info 'first-match)
4356 info)))
4357 (org-test-with-parsed-data "| / |"
4358 (should-not
4359 (org-export-get-table-cell-at
4360 '(0 . 0)
4361 (org-element-map tree 'table 'identity info 'first-match)
4362 info))))
4364 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
4365 "Test `org-export-table-cell-starts-colgroup-p' specifications."
4366 ;; 1. A cell at a beginning of a row always starts a column group.
4367 (org-test-with-parsed-data "| a |"
4368 (should
4369 (org-export-table-cell-starts-colgroup-p
4370 (org-element-map tree 'table-cell 'identity info 'first-match)
4371 info)))
4372 ;; 2. Special column should be ignored when determining the
4373 ;; beginning of the row.
4374 (org-test-with-parsed-data "
4375 | / | |
4376 | | a |"
4377 (should
4378 (org-export-table-cell-starts-colgroup-p
4379 (org-element-map tree 'table-cell 'identity info 'first-match)
4380 info)))
4381 ;; 2. Explicit column groups.
4382 (org-test-with-parsed-data "
4383 | / | | < |
4384 | a | b | c |"
4385 (should
4386 (equal
4387 '(yes no yes)
4388 (org-element-map tree 'table-cell
4389 (lambda (cell)
4390 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
4391 info)))))
4393 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
4394 "Test `org-export-table-cell-ends-colgroup-p' specifications."
4395 ;; 1. A cell at the end of a row always ends a column group.
4396 (org-test-with-parsed-data "| a |"
4397 (should
4398 (org-export-table-cell-ends-colgroup-p
4399 (org-element-map tree 'table-cell 'identity info 'first-match)
4400 info)))
4401 ;; 2. Special column should be ignored when determining the
4402 ;; beginning of the row.
4403 (org-test-with-parsed-data "
4404 | / | |
4405 | | a |"
4406 (should
4407 (org-export-table-cell-ends-colgroup-p
4408 (org-element-map tree 'table-cell 'identity info 'first-match)
4409 info)))
4410 ;; 3. Explicit column groups.
4411 (org-test-with-parsed-data "
4412 | / | < | |
4413 | a | b | c |"
4414 (should
4415 (equal
4416 '(yes no yes)
4417 (org-element-map tree 'table-cell
4418 (lambda (cell)
4419 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
4420 info)))))
4422 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
4423 "Test `org-export-table-row-starts-rowgroup-p' specifications."
4424 ;; 1. A row at the beginning of a table always starts a row group.
4425 ;; So does a row following a table rule.
4426 (org-test-with-parsed-data "
4427 | a |
4428 |---|
4429 | b |"
4430 (should
4431 (equal
4432 '(yes no yes)
4433 (org-element-map tree 'table-row
4434 (lambda (row)
4435 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
4436 info))))
4437 ;; 2. Special rows should be ignored when determining the beginning
4438 ;; of the row.
4439 (org-test-with-parsed-data "
4440 | / | < |
4441 | | a |
4442 |---+---|
4443 | / | < |
4444 | | b |"
4445 (should
4446 (equal
4447 '(yes no yes)
4448 (org-element-map tree 'table-row
4449 (lambda (row)
4450 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
4451 info)))))
4453 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
4454 "Test `org-export-table-row-ends-rowgroup-p' specifications."
4455 ;; 1. A row at the end of a table always ends a row group. So does
4456 ;; a row preceding a table rule.
4457 (org-test-with-parsed-data "
4458 | a |
4459 |---|
4460 | b |"
4461 (should
4462 (equal
4463 '(yes no yes)
4464 (org-element-map tree 'table-row
4465 (lambda (row)
4466 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
4467 info))))
4468 ;; 2. Special rows should be ignored when determining the beginning
4469 ;; of the row.
4470 (org-test-with-parsed-data "
4471 | | a |
4472 | / | < |
4473 |---+---|
4474 | | b |
4475 | / | < |"
4476 (should
4477 (equal
4478 '(yes no yes)
4479 (org-element-map tree 'table-row
4480 (lambda (row)
4481 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
4482 info)))))
4484 (ert-deftest test-org-export/table-row-in-header-p ()
4485 "Test `org-export-table-row-in-header-p' specifications."
4486 ;; Standard test. Separators are always nil.
4487 (should
4488 (equal
4489 '(yes no no)
4490 (org-test-with-parsed-data "| a |\n|---|\n| b |"
4491 (org-element-map tree 'table-row
4492 (lambda (row)
4493 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
4494 ;; Nil when there is no header.
4495 (should
4496 (equal
4497 '(no no)
4498 (org-test-with-parsed-data "| a |\n| b |"
4499 (org-element-map tree 'table-row
4500 (lambda (row)
4501 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
4503 (ert-deftest test-org-export/table-row-starts-header-p ()
4504 "Test `org-export-table-row-starts-header-p' specifications."
4505 ;; 1. Only the row starting the first row group starts the table
4506 ;; header.
4507 (org-test-with-parsed-data "
4508 | a |
4509 | b |
4510 |---|
4511 | c |"
4512 (should
4513 (equal
4514 '(yes no no no)
4515 (org-element-map tree 'table-row
4516 (lambda (row)
4517 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
4518 info))))
4519 ;; 2. A row cannot start an header if there's no header in the
4520 ;; table.
4521 (org-test-with-parsed-data "
4522 | a |
4523 |---|"
4524 (should-not
4525 (org-export-table-row-starts-header-p
4526 (org-element-map tree 'table-row 'identity info 'first-match)
4527 info))))
4529 (ert-deftest test-org-export/table-row-ends-header-p ()
4530 "Test `org-export-table-row-ends-header-p' specifications."
4531 ;; 1. Only the row starting the first row group starts the table
4532 ;; header.
4533 (org-test-with-parsed-data "
4534 | a |
4535 | b |
4536 |---|
4537 | c |"
4538 (should
4539 (equal
4540 '(no yes no no)
4541 (org-element-map tree 'table-row
4542 (lambda (row)
4543 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
4544 info))))
4545 ;; 2. A row cannot start an header if there's no header in the
4546 ;; table.
4547 (org-test-with-parsed-data "
4548 | a |
4549 |---|"
4550 (should-not
4551 (org-export-table-row-ends-header-p
4552 (org-element-map tree 'table-row 'identity info 'first-match)
4553 info))))
4557 ;;; Tables of Contents
4559 (ert-deftest test-org-export/collect-headlines ()
4560 "Test `org-export-collect-headlines' specifications."
4561 ;; Standard test.
4562 (should
4563 (equal '("H1" "H2")
4564 (org-test-with-parsed-data "* H1\n** H2"
4565 (mapcar (lambda (h) (org-element-property :raw-value h))
4566 (org-export-collect-headlines info)))))
4567 ;; Do not collect headlines below optional argument.
4568 (should
4569 (equal '("H1")
4570 (org-test-with-parsed-data "* H1\n** H2"
4571 (mapcar (lambda (h) (org-element-property :raw-value h))
4572 (org-export-collect-headlines info 1)))))
4573 ;; Never collect headlines below maximum headline level.
4574 (should
4575 (equal '("H1")
4576 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4577 (mapcar (lambda (h) (org-element-property :raw-value h))
4578 (org-export-collect-headlines info)))))
4579 (should
4580 (equal '("H1")
4581 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4582 (mapcar (lambda (h) (org-element-property :raw-value h))
4583 (org-export-collect-headlines info 2)))))
4584 ;; Do not collect footnote section.
4585 (should
4586 (equal '("H1")
4587 (let ((org-footnote-section "Footnotes"))
4588 (org-test-with-parsed-data "* H1\n** Footnotes"
4589 (mapcar (lambda (h) (org-element-property :raw-value h))
4590 (org-export-collect-headlines info))))))
4591 ;; Do not collect headlines with UNNUMBERED property set to "notoc".
4592 ;; Headlines with another value for the property are still
4593 ;; collected. UNNUMBERED property is inherited.
4594 (should
4595 (equal '("H1")
4596 (org-test-with-parsed-data
4597 "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:"
4598 (mapcar (lambda (h) (org-element-property :raw-value h))
4599 (org-export-collect-headlines info)))))
4600 (should-not
4601 (org-test-with-parsed-data
4602 "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:\n** H2"
4603 (mapcar (lambda (h) (org-element-property :raw-value h))
4604 (org-export-collect-headlines info))))
4605 (should
4606 (equal '("H1" "H2")
4607 (org-test-with-parsed-data
4608 "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
4609 (mapcar (lambda (h) (org-element-property :raw-value h))
4610 (org-export-collect-headlines info)))))
4611 ;; Collect headlines locally.
4612 (should
4613 (equal '("H2" "H3")
4614 (org-test-with-parsed-data "* H1\n** H2\n** H3"
4615 (let ((scope (org-element-map tree 'headline #'identity info t)))
4616 (mapcar (lambda (h) (org-element-property :raw-value h))
4617 (org-export-collect-headlines info nil scope))))))
4618 ;; When collecting locally, optional level is relative.
4619 (should
4620 (equal '("H2")
4621 (org-test-with-parsed-data "* H1\n** H2\n*** H3"
4622 (let ((scope (org-element-map tree 'headline #'identity info t)))
4623 (mapcar (lambda (h) (org-element-property :raw-value h))
4624 (org-export-collect-headlines info 1 scope)))))))
4626 (ert-deftest test-org-export/excluded-from-toc-p ()
4627 "Test `org-export-excluded-from-toc-p' specifications."
4628 ;; By default, headlines are not excluded.
4629 (should-not
4630 (org-test-with-parsed-data "* H1"
4631 (org-element-map tree 'headline
4632 (lambda (h) (org-export-excluded-from-toc-p h info)) info t)))
4633 ;; Exclude according to a maximum level.
4634 (should
4635 (equal '(in out)
4636 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4637 (org-element-map tree 'headline
4638 (lambda (h) (if (org-export-excluded-from-toc-p h info) 'out 'in))
4639 info))))
4640 ;; Exclude according to UNNUMBERED property.
4641 (should
4642 (org-test-with-parsed-data "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:"
4643 (org-element-map tree 'headline
4644 (lambda (h) (org-export-excluded-from-toc-p h info)) info t)))
4645 ;; UNNUMBERED property is inherited, so is "notoc" value.
4646 (should
4647 (equal '(out out)
4648 (org-test-with-parsed-data
4649 "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:\n** H2"
4650 (org-element-map tree 'headline
4651 (lambda (h) (if (org-export-excluded-from-toc-p h info) 'out 'in))
4652 info)))))
4654 (ert-deftest test-org-export/toc-entry-backend ()
4655 "Test `org-export-toc-entry-backend' specifications."
4656 ;; Ignore targets.
4657 (should
4658 (equal "H \n"
4659 (org-test-with-temp-text "* H <<target>>"
4660 (let (org-export-registered-backends)
4661 (org-export-define-backend 'test
4662 '((headline . (lambda (h _c i) (org-export-data-with-backend
4663 (org-element-property :title h)
4664 (org-export-toc-entry-backend 'test)
4665 i)))))
4666 (org-export-as 'test)))))
4667 ;; Ignore footnote references.
4668 (should
4669 (equal "H \n"
4670 (org-test-with-temp-text "[fn:1] Definition\n* H [fn:1]"
4671 (let (org-export-registered-backends)
4672 (org-export-define-backend 'test
4673 '((headline . (lambda (h _c i) (org-export-data-with-backend
4674 (org-element-property :title h)
4675 (org-export-toc-entry-backend 'test)
4676 i)))))
4677 (org-export-as 'test)))))
4678 ;; Replace plain links with contents, or with path.
4679 (should
4680 (equal "H Org mode\n"
4681 (org-test-with-temp-text "* H [[https://orgmode.org][Org mode]]"
4682 (let (org-export-registered-backends)
4683 (org-export-define-backend 'test
4684 '((headline . (lambda (h _c i) (org-export-data-with-backend
4685 (org-element-property :title h)
4686 (org-export-toc-entry-backend 'test)
4687 i)))))
4688 (org-export-as 'test)))))
4689 (should
4690 (equal "H https://orgmode.org\n"
4691 (org-test-with-temp-text "* H [[https://orgmode.org]]"
4692 (let (org-export-registered-backends)
4693 (org-export-define-backend 'test
4694 '((headline . (lambda (h _c i) (org-export-data-with-backend
4695 (org-element-property :title h)
4696 (org-export-toc-entry-backend 'test)
4697 i)))))
4698 (org-export-as 'test)))))
4699 ;; Replace radio targets with contents.
4700 (should
4701 (equal "H radio\n"
4702 (org-test-with-temp-text "* H <<<radio>>>"
4703 (let (org-export-registered-backends)
4704 (org-export-define-backend 'test
4705 '((headline . (lambda (h _c i) (org-export-data-with-backend
4706 (org-element-property :title h)
4707 (org-export-toc-entry-backend 'test)
4708 i)))))
4709 (org-export-as 'test)))))
4710 ;; With optional argument TRANSCODERS, specify other
4711 ;; transformations.
4712 (should
4713 (equal "H bold\n"
4714 (org-test-with-temp-text "* H *bold*"
4715 (let (org-export-registered-backends)
4716 (org-export-define-backend 'test
4717 '((headline . (lambda (h _c i) (org-export-data-with-backend
4718 (org-element-property :title h)
4719 (org-export-toc-entry-backend 'test
4720 '(bold . (lambda (_b c _i) c)))
4721 i)))))
4722 (org-export-as 'test))))))
4726 ;;; Templates
4728 (ert-deftest test-org-export/inner-template ()
4729 "Test `inner-template' translator specifications."
4730 (should
4731 (equal "Success!"
4732 (org-test-with-temp-text "* Headline"
4733 (org-export-as
4734 (org-export-create-backend
4735 :transcoders
4736 '((inner-template . (lambda (contents info) "Success!"))
4737 (headline . (lambda (h c i) "Headline"))))))))
4738 ;; Inner template is applied even in a "body-only" export.
4739 (should
4740 (equal "Success!"
4741 (org-test-with-temp-text "* Headline"
4742 (org-export-as
4743 (org-export-create-backend
4744 :transcoders '((inner-template . (lambda (c i) "Success!"))
4745 (headline . (lambda (h c i) "Headline"))))
4746 nil nil 'body-only)))))
4748 (ert-deftest test-org-export/template ()
4749 "Test `template' translator specifications."
4750 (should
4751 (equal "Success!"
4752 (org-test-with-temp-text "* Headline"
4753 (org-export-as
4754 (org-export-create-backend
4755 :transcoders '((template . (lambda (contents info) "Success!"))
4756 (headline . (lambda (h c i) "Headline"))))))))
4757 ;; Template is not applied in a "body-only" export.
4758 (should-not
4759 (equal "Success!"
4760 (org-test-with-temp-text "* Headline"
4761 (org-export-as
4762 (org-export-create-backend
4763 :transcoders '((template . (lambda (contents info) "Success!"))
4764 (headline . (lambda (h c i) "Headline"))))
4765 nil nil 'body-only)))))
4769 ;;; Topology
4771 (ert-deftest test-org-export/get-next-element ()
4772 "Test `org-export-get-next-element' specifications."
4773 ;; Standard test.
4774 (should
4775 (equal "b"
4776 (org-test-with-parsed-data "* Headline\n*a* b"
4777 (org-export-get-next-element
4778 (org-element-map tree 'bold 'identity info t) info))))
4779 ;; Return nil when no previous element.
4780 (should-not
4781 (org-test-with-parsed-data "* Headline\na *b*"
4782 (org-export-get-next-element
4783 (org-element-map tree 'bold 'identity info t) info)))
4784 ;; Non-exportable elements are ignored.
4785 (should-not
4786 (let ((org-export-with-timestamps nil))
4787 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
4788 (org-export-get-next-element
4789 (org-element-map tree 'entity 'identity info t) info))))
4790 ;; Find next element in secondary strings.
4791 (should
4792 (eq 'verbatim
4793 (org-test-with-parsed-data "* a =verb="
4794 (org-element-type
4795 (org-export-get-next-element
4796 (org-element-map tree 'plain-text 'identity info t) info)))))
4797 (should
4798 (eq 'verbatim
4799 (org-test-with-parsed-data "* /italic/ =verb="
4800 (org-element-type
4801 (org-export-get-next-element
4802 (org-element-map tree 'italic 'identity info t) info)))))
4803 ;; Find next element in document keywords.
4804 (should
4805 (eq 'verbatim
4806 (org-test-with-parsed-data "#+TITLE: a =verb="
4807 (org-element-type
4808 (org-export-get-next-element
4809 (org-element-map
4810 (plist-get info :title) 'plain-text 'identity info t) info)))))
4811 ;; Find next element in parsed affiliated keywords.
4812 (should
4813 (eq 'verbatim
4814 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
4815 (org-element-type
4816 (org-export-get-next-element
4817 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4818 ;; With optional argument N, return a list containing all the
4819 ;; following elements.
4820 (should
4821 (equal
4822 '(bold code underline)
4823 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4824 (mapcar 'car
4825 (org-export-get-next-element
4826 (org-element-map tree 'italic 'identity info t) info t)))))
4827 ;; When N is a positive integer, return a list containing up to
4828 ;; N following elements.
4829 (should
4830 (equal
4831 '(bold code)
4832 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4833 (mapcar 'car
4834 (org-export-get-next-element
4835 (org-element-map tree 'italic 'identity info t) info 2))))))
4837 (ert-deftest test-org-export/get-previous-element ()
4838 "Test `org-export-get-previous-element' specifications."
4839 ;; Standard test.
4840 (should
4841 (equal "a "
4842 (org-test-with-parsed-data "* Headline\na *b*"
4843 (org-export-get-previous-element
4844 (org-element-map tree 'bold 'identity info t) info))))
4845 ;; Return nil when no previous element.
4846 (should-not
4847 (org-test-with-parsed-data "* Headline\n*a* b"
4848 (org-export-get-previous-element
4849 (org-element-map tree 'bold 'identity info t) info)))
4850 ;; Non-exportable elements are ignored.
4851 (should-not
4852 (let ((org-export-with-timestamps nil))
4853 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
4854 (org-export-get-previous-element
4855 (org-element-map tree 'entity 'identity info t) info))))
4856 ;; Find previous element in secondary strings.
4857 (should
4858 (eq 'verbatim
4859 (org-test-with-parsed-data "* =verb= a"
4860 (org-element-type
4861 (org-export-get-previous-element
4862 (org-element-map tree 'plain-text 'identity info t) info)))))
4863 (should
4864 (eq 'verbatim
4865 (org-test-with-parsed-data "* =verb= /italic/"
4866 (org-element-type
4867 (org-export-get-previous-element
4868 (org-element-map tree 'italic 'identity info t) info)))))
4869 ;; Find previous element in document keywords.
4870 (should
4871 (eq 'verbatim
4872 (org-test-with-parsed-data "#+TITLE: =verb= a"
4873 (org-element-type
4874 (org-export-get-previous-element
4875 (org-element-map
4876 (plist-get info :title) 'plain-text 'identity info t) info)))))
4877 ;; Find previous element in parsed affiliated keywords.
4878 (should
4879 (eq 'verbatim
4880 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
4881 (org-element-type
4882 (org-export-get-previous-element
4883 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4884 ;; With optional argument N, return a list containing up to
4885 ;; N previous elements.
4886 (should
4887 (equal '(underline italic bold)
4888 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4889 (mapcar 'car
4890 (org-export-get-previous-element
4891 (org-element-map tree 'code 'identity info t) info t)))))
4892 ;; When N is a positive integer, return a list containing up to
4893 ;; N previous elements.
4894 (should
4895 (equal '(italic bold)
4896 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4897 (mapcar 'car
4898 (org-export-get-previous-element
4899 (org-element-map tree 'code 'identity info t) info 2))))))
4902 (provide 'test-ox)
4903 ;;; test-org-export.el end here