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