org-clone-subtree-with-time-shift: Fix SHIFT check
[org-mode.git] / testing / lisp / test-ox.el
blob48544906b2926fb7082b8dac313a72193f1e10fc
1 ;;; test-ox.el --- Tests for ox.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2016 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
7 ;; This file is not part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Code:
24 (require 'cl-lib)
26 (unless (featurep 'ox)
27 (signal 'missing-test-dependency "org-export"))
29 (defun org-test-default-backend ()
30 "Return a default export back-end.
31 This back-end simply returns parsed data as Org syntax."
32 (org-export-create-backend
33 :transcoders
34 (mapcar (lambda (type)
35 (cons type
36 (lambda (o c _)
37 (funcall
38 (intern (format "org-element-%s-interpreter" type))
39 o c))))
40 (append org-element-all-elements org-element-all-objects))))
42 (defmacro org-test-with-parsed-data (data &rest body)
43 "Execute body with parsed data available.
44 DATA is a string containing the data to be parsed. BODY is the
45 body to execute. Parse tree is available under the `tree'
46 variable, and communication channel under `info'."
47 (declare (debug (form body)) (indent 1))
48 `(org-test-with-temp-text ,data
49 (org-export--delete-comment-trees)
50 (let* ((tree (org-element-parse-buffer))
51 (info (org-combine-plists
52 (org-export--get-export-attributes)
53 (org-export-get-environment))))
54 (org-export--prune-tree tree info)
55 (org-export--remove-uninterpreted-data tree info)
56 (let ((info (org-combine-plists
57 info (org-export--collect-tree-properties tree info))))
58 ,@body))))
62 ;;; Internal Tests
64 (ert-deftest test-org-export/bind-keyword ()
65 "Test reading #+BIND: keywords."
66 ;; Test with `org-export-allow-bind-keywords' set to t.
67 (should
68 (org-test-with-temp-text "#+BIND: test-ox-var value"
69 (let ((org-export-allow-bind-keywords t))
70 (org-export-get-environment)
71 (eq test-ox-var 'value))))
72 ;; Test with `org-export-allow-bind-keywords' set to nil.
73 (should-not
74 (org-test-with-temp-text "#+BIND: test-ox-var value"
75 (let ((org-export-allow-bind-keywords nil))
76 (org-export-get-environment)
77 (boundp 'test-ox-var))))
78 ;; BIND keywords are case-insensitive.
79 (should
80 (org-test-with-temp-text "#+bind: test-ox-var value"
81 (let ((org-export-allow-bind-keywords t))
82 (org-export-get-environment)
83 (eq test-ox-var 'value))))
84 ;; Preserve order of BIND keywords.
85 (should
86 (org-test-with-temp-text "#+BIND: test-ox-var 1\n#+BIND: test-ox-var 2"
87 (let ((org-export-allow-bind-keywords t))
88 (org-export-get-environment)
89 (eq test-ox-var 2))))
90 ;; Read BIND keywords in setup files.
91 (should
92 (org-test-with-temp-text
93 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
94 (let ((org-export-allow-bind-keywords t))
95 (org-export-get-environment)
96 (eq variable 'value))))
97 ;; Verify that bound variables are seen during export.
98 (should
99 (equal "Yes\n"
100 (org-test-with-temp-text "#+BIND: test-ox-var value"
101 (let ((org-export-allow-bind-keywords t))
102 (org-export-as
103 (org-export-create-backend
104 :transcoders
105 '((section . (lambda (s c i)
106 (if (eq test-ox-var 'value) "Yes" "No")))))))))))
108 (ert-deftest test-org-export/parse-option-keyword ()
109 "Test reading all standard #+OPTIONS: items."
110 (should
111 (let ((options
112 (org-export--parse-option-keyword
113 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t \
114 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil \
115 stat:t title:t")))
116 (and (eq (plist-get options :headline-levels) 1)
117 (eq (plist-get options :section-numbers) t)
118 (eq (plist-get options :preserve-breaks) t)
119 (eq (plist-get options :time-stamp-file) t)
120 (eq (plist-get options :with-archived-trees) t)
121 (eq (plist-get options :with-author) t)
122 (eq (plist-get options :with-drawers) t)
123 (eq (plist-get options :with-email) t)
124 (eq (plist-get options :with-emphasize) t)
125 (eq (plist-get options :with-entities) t)
126 (eq (plist-get options :with-fixed-width) t)
127 (eq (plist-get options :with-footnotes) t)
128 (eq (plist-get options :with-priority) t)
129 (eq (plist-get options :with-special-strings) t)
130 (eq (plist-get options :with-sub-superscript) t)
131 (eq (plist-get options :with-toc) t)
132 (eq (plist-get options :with-tables) t)
133 (eq (plist-get options :with-tags) t)
134 (eq (plist-get options :with-tasks) t)
135 (eq (plist-get options :with-timestamps) t)
136 (eq (plist-get options :with-todo-keywords) t)
137 (eq (plist-get options :with-inlinetasks) nil)
138 (eq (plist-get options :with-statistics-cookies) t)
139 (eq (plist-get options :with-title) t))))
140 ;; Test some special values.
141 (should
142 (let ((options
143 (org-export--parse-option-keyword
144 "arch:headline d:(\"TEST\") ^:{} toc:1 tags:not-in-toc tasks:todo \
145 num:2 <:active")))
146 (and (eq (plist-get options :with-archived-trees) 'headline)
147 (eq (plist-get options :with-sub-superscript) '{})
148 (eq (plist-get options :with-toc) 1)
149 (eq (plist-get options :with-tags) 'not-in-toc)
150 (eq (plist-get options :with-tasks) 'todo)
151 (eq (plist-get options :section-numbers) 2)
152 (eq (plist-get options :with-timestamps) 'active)
153 (equal (plist-get options :with-drawers) '("TEST")))))
154 ;; Test back-end specific values.
155 (should
156 (equal
157 (org-export--parse-option-keyword
158 "opt:t" (org-export-create-backend :options '((:option nil "opt"))))
159 '(:option t)))
160 ;; More than one property can refer to the same option item.
161 (should
162 (equal '(:opt1 t :opt2 t)
163 (org-export--parse-option-keyword
164 "opt:t"
165 (org-export-create-backend
166 :options '((:opt1 nil "opt") (:opt2 nil "opt")))))))
168 (ert-deftest test-org-export/get-inbuffer-options ()
169 "Test reading all standard export keywords."
170 ;; Properties should follow buffer order.
171 (should
172 (equal
173 (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
174 (org-export--get-inbuffer-options))
175 '(:language "fr" :creator "Me" :email "email")))
176 ;; Test `space' behaviour.
177 (should
178 (equal
179 (let ((back-end (org-export-create-backend
180 :options '((:keyword "KEYWORD" nil nil space)))))
181 (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: spaces"
182 (org-export--get-inbuffer-options back-end)))
183 '(:keyword "With spaces")))
184 ;; Test `newline' behaviour.
185 (should
186 (equal
187 (let ((back-end (org-export-create-backend
188 :options '((:keyword "KEYWORD" nil nil newline)))))
189 (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: two lines"
190 (org-export--get-inbuffer-options back-end)))
191 '(:keyword "With\ntwo lines")))
192 ;; Test `split' behaviour.
193 (should
194 (equal
195 (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
196 (org-export--get-inbuffer-options))
197 '(:select-tags ("a" "b"))))
198 ;; Test `parse' behaviour. `parse' implies `space' but preserve
199 ;; line breaks. Multi-line objects are allowed.
200 (should
201 (org-element-map
202 (org-test-with-temp-text "#+TITLE: *bold*"
203 (plist-get (org-export--get-inbuffer-options) :title))
204 'bold #'identity nil t))
205 (should
206 (equal
207 (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
208 (plist-get (org-export--get-inbuffer-options) :title))
209 '("Some title with spaces")))
210 (should
211 (org-element-map
212 (org-test-with-temp-text "#+TITLE: Some title\\\\\n#+TITLE: with spaces"
213 (plist-get (org-export--get-inbuffer-options) :title))
214 'line-break #'identity nil t))
215 (should
216 (org-element-map
217 (org-test-with-temp-text "#+TITLE: *bold\n#+TITLE: sentence*"
218 (plist-get (org-export--get-inbuffer-options) :title))
219 'bold #'identity nil t))
220 ;; Options set through SETUPFILE.
221 (should
222 (equal
223 (org-test-with-temp-text
224 (format "#+DESCRIPTION: l1
225 #+LANGUAGE: es
226 #+SELECT_TAGS: a
227 #+TITLE: a
228 #+SETUPFILE: \"%s/examples/setupfile.org\"
229 #+LANGUAGE: fr
230 #+SELECT_TAGS: c
231 #+TITLE: c"
232 org-test-dir)
233 (org-export--get-inbuffer-options))
234 '(:language "fr" :select-tags ("a" "b" "c") :title ("a b c"))))
235 ;; 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 "Para1\n\nPara2\n"
1721 (org-test-with-temp-text "
1722 Para1
1723 # Comment
1725 # Comment
1726 Para2"
1727 (org-export-as (org-test-default-backend)))))
1728 (should
1729 (equal "Para1\n\nPara2\n"
1730 (org-test-with-temp-text "
1731 Para1
1732 # Comment
1733 Para2"
1734 (org-export-as (org-test-default-backend)))))
1735 (should
1736 (equal "[fn:1] Para1\n\n\nPara2\n"
1737 (org-test-with-temp-text "
1738 \[fn:1] Para1
1739 # Inside definition
1742 # Outside definition
1743 Para2"
1744 (org-export-as (org-test-default-backend)))))
1745 (should
1746 (equal "[fn:1] Para1\n\nPara2\n"
1747 (org-test-with-temp-text "
1748 \[fn:1] Para1
1750 # Inside definition
1752 # Inside definition
1754 Para2"
1755 (org-export-as (org-test-default-backend)))))
1756 (should
1757 (equal "[fn:1] Para1\n\nPara2\n"
1758 (org-test-with-temp-text "
1759 \[fn:1] Para1
1760 # Inside definition
1762 Para2"
1763 (org-export-as (org-test-default-backend)))))
1764 (should
1765 (equal "[fn:1] Para1\n\nPara2\n"
1766 (org-test-with-temp-text "
1767 \[fn:1] Para1
1769 # Inside definition
1770 Para2"
1771 (org-export-as (org-test-default-backend)))))
1772 (should
1773 (equal "- item 1\n\n- item 2\n"
1774 (org-test-with-temp-text "
1775 - item 1
1777 # Comment
1779 - item 2"
1780 (org-export-as (org-test-default-backend))))))
1784 ;;; Export blocks
1786 (ert-deftest test-org-export/export-block ()
1787 "Test export blocks transcoding."
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 '((export-block . (lambda (b _c _i)
1795 (org-element-property :value b)))
1796 (section . (lambda (_s c _i) c))))))))
1797 (should
1798 (equal "Success!\n"
1799 (org-test-with-temp-text
1800 "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
1801 (org-export-as
1802 (org-export-create-backend
1803 :transcoders
1804 (list
1805 (cons 'export-block
1806 (lambda (b _c _i)
1807 (and (equal (org-element-property :type b) "BACKEND")
1808 (org-element-property :value b))))
1809 (cons 'section (lambda (_s c _i) c)))))))))
1812 ;;; Export Snippets
1814 (ert-deftest test-org-export/export-snippet ()
1815 "Test export snippets transcoding."
1816 ;; Standard test.
1817 (org-test-with-temp-text "@@test:A@@@@t:B@@"
1818 (let ((backend (org-test-default-backend)))
1819 (setf (org-export-backend-name backend) 'test)
1820 (setf (org-export-backend-transcoders backend)
1821 (cons (cons 'export-snippet
1822 (lambda (snippet contents info)
1823 (when (eq (org-export-snippet-backend snippet) 'test)
1824 (org-element-property :value snippet))))
1825 (org-export-backend-transcoders backend)))
1826 (let ((org-export-snippet-translation-alist nil))
1827 (should (equal (org-export-as backend) "A\n")))
1828 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
1829 (should (equal (org-export-as backend) "AB\n")))))
1830 ;; Ignored export snippets do not remove any blank.
1831 (should
1832 (equal "begin end\n"
1833 (org-test-with-parsed-data "begin @@test:A@@ end"
1834 (org-export-data-with-backend
1835 tree
1836 (org-export-create-backend
1837 :transcoders
1838 '((paragraph . (lambda (paragraph contents info) contents))
1839 (section . (lambda (section contents info) contents))))
1840 info)))))
1843 ;;; Filters
1845 (ert-deftest test-org-export/filter-apply-functions ()
1846 "Test `org-export-filter-apply-functions' specifications."
1847 ;; Functions are applied in order and return values are reduced.
1848 (should
1849 (equal "210"
1850 (org-export-filter-apply-functions
1851 (list (lambda (value &rest _) (concat "1" value))
1852 (lambda (value &rest _) (concat "2" value)))
1853 "0" nil)))
1854 ;; Functions returning nil are skipped.
1855 (should
1856 (equal "20"
1857 (org-export-filter-apply-functions
1858 (list #'ignore (lambda (value &rest _) (concat "2" value)))
1859 "0" nil)))
1860 ;; If all functions are skipped, return the initial value.
1861 (should
1862 (equal "0"
1863 (org-export-filter-apply-functions (list #'ignore) "0" nil)))
1864 ;; If any function returns the empty string, final value is the
1865 ;; empty string.
1866 (should
1867 (equal ""
1868 (org-export-filter-apply-functions
1869 (list (lambda (value &rest _) "")
1870 (lambda (value &rest _) (concat "2" value)))
1871 "0" nil)))
1872 ;; Any function returning the empty string short-circuits the
1873 ;; process.
1874 (should
1875 (org-export-filter-apply-functions
1876 (list (lambda (value &rest _) "")
1877 (lambda (value &rest _) (error "This shouldn't happen")))
1878 "0" nil)))
1881 ;;; Footnotes
1883 (ert-deftest test-org-export/footnote-first-reference-p ()
1884 "Test `org-export-footnote-first-reference-p' specifications."
1885 (should
1886 (equal
1887 '(t nil)
1888 (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
1889 (let (result)
1890 (org-export-as
1891 (org-export-create-backend
1892 :transcoders
1893 `(,(cons 'footnote-reference
1894 (lambda (f c i)
1895 (push (org-export-footnote-first-reference-p f i)
1896 result)
1897 ""))
1898 (section . (lambda (s c i) c))
1899 (paragraph . (lambda (p c i) c))))
1900 nil nil nil '(:with-footnotes t))
1901 (nreverse result)))))
1902 ;; Limit check to DATA, when non-nil.
1903 (should
1904 (equal
1905 '(nil t)
1906 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
1907 (let (result)
1908 (org-element-map tree 'footnote-reference
1909 (lambda (ref)
1910 (push
1911 (org-export-footnote-first-reference-p
1912 ref info (org-element-map tree 'headline #'identity info t))
1913 result))
1914 info)
1915 (nreverse result)))))
1916 (should
1917 (equal
1918 '(t nil)
1919 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
1920 (let (result)
1921 (org-element-map tree 'footnote-reference
1922 (lambda (ref)
1923 (push (org-export-footnote-first-reference-p ref info) result))
1924 info)
1925 (nreverse result)))))
1926 ;; If optional argument BODY-FIRST is non-nil, first find footnote
1927 ;; in the main body of the document. Otherwise, enter footnote
1928 ;; definitions when they are encountered.
1929 (should
1930 (equal
1931 '(t nil)
1932 (org-test-with-temp-text
1933 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
1934 (let (result)
1935 (org-export-as
1936 (org-export-create-backend
1937 :transcoders
1938 `(,(cons 'footnote-reference
1939 (lambda (f c i)
1940 (when (org-element-lineage f '(drawer))
1941 (push (org-export-footnote-first-reference-p f i nil)
1942 result))
1943 ""))
1944 (drawer . (lambda (d c i) c))
1945 (footnote-definition . (lambda (d c i) c))
1946 (section . (lambda (s c i) c))
1947 (paragraph . (lambda (p c i) c))))
1948 nil nil nil '(:with-footnotes t))
1949 (nreverse result)))))
1950 (should
1951 (equal
1952 '(t t)
1953 (org-test-with-temp-text
1954 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
1955 (let (result)
1956 (org-export-as
1957 (org-export-create-backend
1958 :transcoders
1959 `(,(cons 'footnote-reference
1960 (lambda (f c i)
1961 (when (org-element-lineage f '(drawer))
1962 (push (org-export-footnote-first-reference-p f i nil t)
1963 result))
1964 ""))
1965 (drawer . (lambda (d c i) c))
1966 (footnote-definition . (lambda (d c i) c))
1967 (section . (lambda (s c i) c))
1968 (paragraph . (lambda (p c i) c))))
1969 nil nil nil '(:with-footnotes t))
1970 (nreverse result))))))
1972 (ert-deftest test-org-export/get-footnote-number ()
1973 "Test `org-export-get-footnote-number' specifications."
1974 (should
1975 (equal '(1 2 1)
1976 (org-test-with-parsed-data
1977 "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
1978 (org-element-map tree 'footnote-reference
1979 (lambda (ref) (org-export-get-footnote-number ref info))
1980 info))))
1981 ;; Anonymous footnotes all get a new number.
1982 (should
1983 (equal '(1 2)
1984 (org-test-with-parsed-data
1985 "Text[fn::anon1][fn::anon2]"
1986 (org-element-map tree 'footnote-reference
1987 (lambda (ref) (org-export-get-footnote-number ref info))
1988 info))))
1989 ;; Test nested footnotes order.
1990 (should
1991 (equal
1992 '((1 . "1") (2 . "2") (3 . "3") (3 . "3") (4))
1993 (org-test-with-parsed-data
1994 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
1995 (org-element-map tree 'footnote-reference
1996 (lambda (ref)
1997 (cons (org-export-get-footnote-number ref info)
1998 (org-element-property :label ref)))
1999 info))))
2000 ;; Limit number to provided DATA, when non-nil.
2001 (should
2002 (equal
2003 '(1)
2004 (org-test-with-parsed-data
2005 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2006 (org-element-map tree 'footnote-reference
2007 (lambda (ref)
2008 (org-export-get-footnote-number
2009 ref info (org-element-map tree 'headline #'identity info t)))
2010 info))))
2011 (should
2012 (equal
2013 '(1 2)
2014 (org-test-with-parsed-data
2015 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
2016 (org-element-map tree 'footnote-reference
2017 (lambda (ref) (org-export-get-footnote-number ref info))
2018 info))))
2019 ;; With a non-nil BODY-FIRST optional argument, first check body,
2020 ;; then footnote definitions.
2021 (should
2022 (equal
2023 '(("1" . 1) ("2" . 2) ("3" . 3) ("3" . 3))
2024 (org-test-with-parsed-data
2025 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
2026 (org-element-map tree 'footnote-reference
2027 (lambda (ref)
2028 (cons (org-element-property :label ref)
2029 (org-export-get-footnote-number ref info nil t)))
2030 info))))
2031 (should
2032 (equal
2033 '(("1" . 1) ("2" . 3) ("3" . 2) ("3" . 2))
2034 (org-test-with-parsed-data
2035 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
2036 (org-element-map tree 'footnote-reference
2037 (lambda (ref)
2038 (cons (org-element-property :label ref)
2039 (org-export-get-footnote-number ref info nil)))
2040 info)))))
2042 (ert-deftest test-org-export/get-footnote-definition ()
2043 "Test `org-export-get-footnote-definition' specifications."
2044 ;; Standard test.
2045 (should
2046 (equal "A\n"
2047 (org-element-interpret-data
2048 (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1] A"
2049 (org-export-get-footnote-definition
2050 (org-element-map tree 'footnote-reference #'identity nil t)
2051 info)))))
2052 ;; Raise an error if no definition is found.
2053 (should-error
2054 (org-test-with-parsed-data "Text[fn:1]"
2055 (org-export-get-footnote-definition
2056 (org-element-map tree 'footnote-reference #'identity nil t)
2057 info)))
2058 ;; Find inline definitions.
2059 (should
2060 (equal "A"
2061 (org-element-interpret-data
2062 (org-test-with-parsed-data "Text[fn:1:A]"
2063 (org-export-get-footnote-definition
2064 (org-element-map tree 'footnote-reference #'identity nil t)
2065 info)))))
2066 ;; Find anonymous definitions.
2067 (should
2068 (equal "A"
2069 (org-element-interpret-data
2070 (org-test-with-parsed-data "Text[fn::A]"
2071 (org-export-get-footnote-definition
2072 (org-element-map tree 'footnote-reference #'identity nil t)
2073 info)))))
2074 ;; Find empty definitions.
2075 (should
2076 (equal ""
2077 (org-element-interpret-data
2078 (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1]"
2079 (org-export-get-footnote-definition
2080 (org-element-map tree 'footnote-reference #'identity nil t)
2081 info)))))
2082 (should
2083 (equal ""
2084 (org-element-interpret-data
2085 (org-test-with-parsed-data "Text[fn:1:]"
2086 (org-export-get-footnote-definition
2087 (org-element-map tree 'footnote-reference #'identity nil t)
2088 info)))))
2089 (should
2090 (equal ""
2091 (org-element-interpret-data
2092 (org-test-with-parsed-data "Text[fn::]"
2093 (org-export-get-footnote-definition
2094 (org-element-map tree 'footnote-reference #'identity nil t)
2095 info))))))
2097 (ert-deftest test-org-export/collect-footnote-definitions ()
2098 "Test `org-export-collect-footnote-definitions' specifications."
2099 (should
2100 (= 4
2101 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2103 \[fn:2] B [fn:3] [fn::D].
2105 \[fn:3] C."
2106 (length (org-export-collect-footnote-definitions info)))))
2107 ;; Limit number to provided DATA, when non-nil.
2108 (should
2109 (equal
2110 '((1 "2"))
2111 (org-test-with-parsed-data
2112 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2113 (mapcar #'butlast
2114 (org-export-collect-footnote-definitions
2115 info (org-element-map tree 'headline #'identity info t))))))
2116 (should
2117 (equal
2118 '((1 "1") (2 "2"))
2119 (org-test-with-parsed-data
2120 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2121 (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
2122 ;; With optional argument BODY-FIRST, first check body, then
2123 ;; footnote definitions.
2124 (should
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 nil t)))))
2133 (should-not
2134 (equal '("1" "3" "2" nil)
2135 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2137 \[fn:2] B [fn:3] [fn::D].
2139 \[fn:3] C."
2140 (mapcar (lambda (e) (nth 1 e))
2141 (org-export-collect-footnote-definitions info))))))
2143 (ert-deftest test-org-export/footnotes ()
2144 "Miscellaneous tests on footnotes."
2145 (let ((org-footnote-section nil)
2146 (org-export-with-footnotes t))
2147 ;; Read every type of footnote.
2148 (should
2149 (equal
2150 '((1 . "A\n") (2 . "C") (3 . "D"))
2151 (org-test-with-parsed-data
2152 "Text[fn:1] [fn:label:C] [fn::D]\n\n[fn:1] A\n"
2153 (org-element-map tree 'footnote-reference
2154 (lambda (ref)
2155 (let ((def (org-export-get-footnote-definition ref info)))
2156 (cons (org-export-get-footnote-number ref info)
2157 (if (eq (org-element-property :type ref) 'inline) (car def)
2158 (car (org-element-contents
2159 (car (org-element-contents def))))))))
2160 info))))
2161 ;; Export nested footnote in invisible definitions.
2162 (should
2163 (= 2
2164 (org-test-with-temp-text "Text[fn:1]\n\n[fn:1] B [fn:2]\n\n[fn:2] C."
2165 (narrow-to-region (point) (line-end-position))
2166 (catch 'exit
2167 (org-export-as
2168 (org-export-create-backend
2169 :transcoders
2170 '((section
2172 (lambda (s c i)
2173 (throw 'exit (length
2174 (org-export-collect-footnote-definitions
2175 i))))))))))))
2176 ;; Export footnotes defined outside parsing scope.
2177 (should
2178 (string-match
2179 "Out of scope"
2180 (org-test-with-temp-text "[fn:1] Out of scope
2181 * Title
2182 <point>Paragraph[fn:1]"
2183 (org-export-as (org-test-default-backend) 'subtree))))
2184 (should
2185 (string-match
2186 "Out of scope"
2187 (org-test-with-temp-text "[fn:1] Out of scope
2188 * Title
2189 <point>Paragraph[fn:1]"
2190 (narrow-to-region (point) (point-max))
2191 (org-export-as (org-test-default-backend)))))
2192 ;; Export nested footnotes defined outside parsing scope.
2193 (should
2194 (string-match
2195 "Very out of scope"
2196 (org-test-with-temp-text "
2197 \[fn:1] Out of scope[fn:2]
2199 \[fn:2] Very out of scope
2200 * Title
2201 <point>Paragraph[fn:1]"
2202 (org-export-as (org-test-default-backend) 'subtree))))
2203 (should
2204 (string-match
2205 "Very out of scope"
2206 (org-test-with-temp-text "
2207 \[fn:1] Out of scope[fn:2]
2209 \[fn:2] Very out of scope
2210 * Title
2211 <point>Paragraph[fn:1]"
2212 (narrow-to-region (point) (point-max))
2213 (org-export-as (org-test-default-backend)))))
2214 (should
2215 (string-match
2216 "D2"
2217 (org-test-with-temp-text "
2218 \[fn:1] Out of scope[fn:2:D2]
2219 * Title
2220 <point>Paragraph[fn:1]"
2221 (narrow-to-region (point) (point-max))
2222 (org-export-as (org-test-default-backend)))))
2223 ;; Export footnotes in pruned parts of tree.
2224 (should
2225 (string-match
2226 "Definition"
2227 (let ((org-export-exclude-tags '("noexport")))
2228 (org-test-with-temp-text
2229 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] Definition"
2230 (org-export-as (org-test-default-backend))))))
2231 (should
2232 (string-match
2233 "Definition"
2234 (let ((org-export-select-tags '("export")))
2235 (org-test-with-temp-text
2236 "* H :export:\nText[fn:1]\n* H2\n[fn:1] Definition"
2237 (org-export-as (org-test-default-backend))))))
2238 ;; Export nested footnotes in pruned parts of tree.
2239 (should
2240 (string-match
2241 "D2"
2242 (let ((org-export-exclude-tags '("noexport")))
2243 (org-test-with-temp-text
2244 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
2245 (org-export-as (org-test-default-backend))))))
2246 (should
2247 (string-match
2248 "D2"
2249 (let ((org-export-select-tags '("export")))
2250 (org-test-with-temp-text
2251 "* H :export:\nText[fn:1]\n* H2\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
2252 (org-export-as (org-test-default-backend))))))
2253 ;; Handle uninterpreted data in pruned footnote definitions.
2254 (should-not
2255 (string-match
2257 (let ((org-export-with-tables nil))
2258 (org-test-with-temp-text
2259 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1]\n| a |"
2260 (org-export-as (org-test-default-backend))))))
2261 ;; Footnotes without a definition should throw an error.
2262 (should-error
2263 (org-test-with-parsed-data "Text[fn:1]"
2264 (org-export-get-footnote-definition
2265 (org-element-map tree 'footnote-reference #'identity info t) info)))
2266 ;; Footnote section should be ignored in TOC and in headlines
2267 ;; numbering.
2268 (should
2269 (= 1 (let ((org-footnote-section "Footnotes"))
2270 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
2271 (org-export-collect-headlines info))))))
2272 (should
2273 (equal '(2)
2274 (let ((org-footnote-section "Footnotes"))
2275 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
2276 (org-element-map tree 'headline
2277 (lambda (hl)
2278 (when (equal (org-element-property :raw-value hl) "H2")
2279 (org-export-get-headline-number hl info)))
2280 info t)))))))
2284 ;;; Headlines and Inlinetasks
2286 (ert-deftest test-org-export/get-relative-level ()
2287 "Test `org-export-get-relative-level' specifications."
2288 ;; Standard test.
2289 (should
2290 (equal '(1 2)
2291 (let ((org-odd-levels-only nil))
2292 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2293 (org-element-map tree 'headline
2294 (lambda (h) (org-export-get-relative-level h info))
2295 info)))))
2296 ;; Missing levels
2297 (should
2298 (equal '(1 3)
2299 (let ((org-odd-levels-only nil))
2300 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
2301 (org-element-map tree 'headline
2302 (lambda (h) (org-export-get-relative-level h info))
2303 info))))))
2305 (ert-deftest test-org-export/low-level-p ()
2306 "Test `org-export-low-level-p' specifications."
2307 (should
2308 (equal
2309 '(no yes)
2310 (let ((org-odd-levels-only nil))
2311 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2312 (org-element-map tree 'headline
2313 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
2314 (plist-put info :headline-levels 1)))))))
2316 (ert-deftest test-org-export/get-headline-number ()
2317 "Test `org-export-get-headline-number' specifications."
2318 ;; Standard test.
2319 (should
2320 (equal
2321 '((1) (1 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)))))
2327 ;; Missing levels are replaced with 0.
2328 (should
2329 (equal
2330 '((1) (1 0 1))
2331 (let ((org-odd-levels-only nil))
2332 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
2333 (org-element-map tree 'headline
2334 (lambda (h) (org-export-get-headline-number h info))
2335 info))))))
2337 (ert-deftest test-org-export/numbered-headline-p ()
2338 "Test `org-export-numbered-headline-p' specifications."
2339 ;; If `:section-numbers' is nil, never number headlines.
2340 (should-not
2341 (org-test-with-parsed-data "* Headline"
2342 (org-element-map tree 'headline
2343 (lambda (h) (org-export-numbered-headline-p h info))
2344 (plist-put info :section-numbers nil))))
2345 ;; If `:section-numbers' is a number, only number headlines with
2346 ;; a level greater that it.
2347 (should
2348 (equal
2349 '(yes no)
2350 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2351 (org-element-map tree 'headline
2352 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
2353 (plist-put info :section-numbers 1)))))
2354 ;; Otherwise, headlines are always numbered.
2355 (should
2356 (org-test-with-parsed-data "* Headline"
2357 (org-element-map tree 'headline
2358 (lambda (h) (org-export-numbered-headline-p h info))
2359 (plist-put info :section-numbers t))))
2360 ;; With #+OPTIONS: num:nil all headlines are unnumbered.
2361 (should-not
2362 (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
2363 (org-export-numbered-headline-p
2364 (org-element-map tree 'headline 'identity info t)
2365 info)))
2366 ;; Headlines with a non-nil UNNUMBERED property are not numbered.
2367 (should-not
2368 (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
2369 (org-export-numbered-headline-p
2370 (org-element-map tree 'headline #'identity info t)
2371 info)))
2372 ;; UNNUMBERED ignores inheritance. Any non-nil value among
2373 ;; ancestors disables numbering.
2374 (should
2375 (org-test-with-parsed-data
2376 "* H
2377 :PROPERTIES:
2378 :UNNUMBERED: t
2379 :END:
2380 ** H2
2381 :PROPERTIES:
2382 :UNNUMBERED: nil
2383 :END:
2384 *** H3"
2385 (cl-every
2386 (lambda (h) (not (org-export-numbered-headline-p h info)))
2387 (org-element-map tree 'headline #'identity info)))))
2389 (ert-deftest test-org-export/number-to-roman ()
2390 "Test `org-export-number-to-roman' specifications."
2391 ;; If number is negative, return it as a string.
2392 (should (equal (org-export-number-to-roman -1) "-1"))
2393 ;; Otherwise, return it as a roman number.
2394 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
2396 (ert-deftest test-org-export/get-optional-title ()
2397 "Test `org-export-get-alt-title' specifications."
2398 ;; If ALT_TITLE property is defined, use it.
2399 (should
2400 (equal '("opt")
2401 (org-test-with-parsed-data
2402 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
2403 (org-export-get-alt-title
2404 (org-element-map tree 'headline 'identity info t)
2405 info))))
2406 ;; Otherwise, fall-back to regular title.
2407 (should
2408 (equal '("Headline")
2409 (org-test-with-parsed-data "* Headline"
2410 (org-export-get-alt-title
2411 (org-element-map tree 'headline 'identity info t)
2412 info)))))
2414 (ert-deftest test-org-export/get-tags ()
2415 "Test `org-export-get-tags' specifications."
2416 ;; Standard test: tags which are not a select tag, an exclude tag,
2417 ;; or specified as optional argument shouldn't be ignored.
2418 (should
2419 (org-test-with-parsed-data "* Headline :tag:"
2420 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2421 info)))
2422 ;; Tags provided in the optional argument are ignored.
2423 (should-not
2424 (org-test-with-parsed-data "* Headline :ignore:"
2425 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2426 info '("ignore"))))
2427 ;; Allow tag inheritance.
2428 (should
2429 (equal
2430 '(("tag") ("tag"))
2431 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
2432 (org-element-map tree 'headline
2433 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
2434 ;; Tag inheritance checks FILETAGS keywords.
2435 (should
2436 (equal
2437 '(("a" "b" "tag"))
2438 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
2439 (org-element-map tree 'headline
2440 (lambda (hl) (org-export-get-tags hl info nil t)) info)))))
2442 (ert-deftest test-org-export/get-node-property ()
2443 "Test`org-export-get-node-property' specifications."
2444 ;; Standard test.
2445 (should
2446 (equal "value"
2447 (org-test-with-parsed-data "* Headline
2448 :PROPERTIES:
2449 :prop: value
2450 :END:"
2451 (org-export-get-node-property
2452 :PROP (org-element-map tree 'headline 'identity nil t)))))
2453 ;; Test inheritance.
2454 (should
2455 (equal "value"
2456 (org-test-with-parsed-data "* Parent
2457 :PROPERTIES:
2458 :prop: value
2459 :END:
2460 ** Headline
2461 Paragraph"
2462 (org-export-get-node-property
2463 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
2464 ;; Cannot return a value before the first headline.
2465 (should-not
2466 (org-test-with-parsed-data "Paragraph
2467 * Headline
2468 :PROPERTIES:
2469 :prop: value
2470 :END:"
2471 (org-export-get-node-property
2472 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
2474 (ert-deftest test-org-export/get-category ()
2475 "Test `org-export-get-category' specifications."
2476 ;; Standard test.
2477 (should
2478 (equal "value"
2479 (org-test-with-parsed-data "* Headline
2480 :PROPERTIES:
2481 :CATEGORY: value
2482 :END:"
2483 (org-export-get-category
2484 (org-element-map tree 'headline 'identity nil t) info))))
2485 ;; Test inheritance from a parent headline.
2486 (should
2487 (equal '("value" "value")
2488 (org-test-with-parsed-data "* Headline1
2489 :PROPERTIES:
2490 :CATEGORY: value
2491 :END:
2492 ** Headline2"
2493 (org-element-map tree 'headline
2494 (lambda (hl) (org-export-get-category hl info)) info))))
2495 ;; Test inheritance from #+CATEGORY keyword
2496 (should
2497 (equal "value"
2498 (org-test-with-parsed-data "#+CATEGORY: value
2499 * Headline"
2500 (org-export-get-category
2501 (org-element-map tree 'headline 'identity nil t) info))))
2502 ;; Test inheritance from file name.
2503 (should
2504 (equal "test"
2505 (org-test-with-parsed-data "* Headline"
2506 (let ((info (plist-put info :input-file "~/test.org")))
2507 (org-export-get-category
2508 (org-element-map tree 'headline 'identity nil t) info)))))
2509 ;; Fall-back value.
2510 (should
2511 (equal "???"
2512 (org-test-with-parsed-data "* Headline"
2513 (org-export-get-category
2514 (org-element-map tree 'headline 'identity nil t) info)))))
2516 (ert-deftest test-org-export/first-sibling-p ()
2517 "Test `org-export-first-sibling-p' specifications."
2518 ;; Standard test.
2519 (should
2520 (equal
2521 '(yes yes no)
2522 (org-test-with-parsed-data "* H\n** H 2\n** H 3"
2523 (org-element-map tree 'headline
2524 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2525 info))))
2526 (should
2527 (equal '(yes no)
2528 (org-test-with-parsed-data "- item\n\n para"
2529 (org-element-map tree 'paragraph
2530 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2531 info))))
2532 ;; Ignore sections for headlines.
2533 (should
2534 (equal '(yes yes)
2535 (org-test-with-parsed-data "* H\nSection\n** H 2"
2536 (org-element-map tree 'headline
2537 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2538 info))))
2539 ;; Ignore headlines not exported.
2540 (should
2541 (equal
2542 '(yes)
2543 (let ((org-export-exclude-tags '("ignore")))
2544 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
2545 (org-element-map tree 'headline
2546 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2547 info))))))
2549 (ert-deftest test-org-export/last-sibling-p ()
2550 "Test `org-export-last-sibling-p' specifications."
2551 ;; Standard test.
2552 (should
2553 (equal
2554 '(yes no yes)
2555 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
2556 (org-element-map tree 'headline
2557 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2558 info))))
2559 (should
2560 (equal '(no yes)
2561 (org-test-with-parsed-data "- item\n\n para"
2562 (org-element-map tree 'paragraph
2563 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2564 info))))
2565 ;; Ignore headlines not exported.
2566 (should
2567 (equal
2568 '(yes)
2569 (let ((org-export-exclude-tags '("ignore")))
2570 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
2571 (org-element-map tree 'headline
2572 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2573 info))))))
2575 (ert-deftest test-org-export/handle-inlinetasks ()
2576 "Test inlinetask export."
2577 ;; Inlinetask with an exclude tag.
2578 (when (featurep 'org-inlinetask)
2579 (should
2580 (equal
2582 (let ((org-inlinetask-min-level 3)
2583 org-export-filter-body-functions
2584 org-export-filter-final-output-functions)
2585 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
2586 (org-export-as (org-test-default-backend)
2587 nil nil nil '(:exclude-tags ("noexp")))))))
2588 ;; Inlinetask with an include tag.
2589 (should
2590 (equal
2591 "* H2\n*** Inline :exp:\n"
2592 (let ((org-inlinetask-min-level 3)
2593 (org-tags-column 0))
2594 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
2595 (org-export-as (org-test-default-backend)
2596 nil nil nil '(:select-tags ("exp")))))))
2597 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
2598 (should
2599 (equal ""
2600 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
2601 (org-inlinetask-min-level 3)
2602 org-export-filter-body-functions
2603 org-export-filter-final-output-functions)
2604 (org-test-with-temp-text "*** TODO Inline"
2605 (org-export-as (org-test-default-backend)
2606 nil nil nil '(:with-tasks nil))))))))
2610 ;;; Keywords
2612 (ert-deftest test-org-export/get-date ()
2613 "Test `org-export-get-date' specifications."
2614 ;; Return a properly formatted string when
2615 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
2616 ;; consists in a single timestamp.
2617 (should
2618 (equal "29 03 2012"
2619 (let ((org-export-date-timestamp-format "%d %m %Y"))
2620 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2621 (org-export-get-date info)))))
2622 ;; Return a secondary string otherwise.
2623 (should-not
2624 (stringp
2625 (let ((org-export-date-timestamp-format nil))
2626 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2627 (org-export-get-date info)))))
2628 (should
2629 (equal '("Date")
2630 (org-test-with-parsed-data "#+DATE: Date"
2631 (org-export-get-date info))))
2632 ;; Optional argument has precedence over
2633 ;; `org-export-date-timestamp-format'.
2634 (should
2635 (equal "29 03"
2636 (let ((org-export-date-timestamp-format "%d %m %Y"))
2637 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2638 (org-export-get-date info "%d %m"))))))
2642 ;;; Links
2644 (ert-deftest test-org-export/custom-protocol-maybe ()
2645 "Test `org-export-custom-protocol-maybe' specifications."
2646 (should
2647 (string-match
2648 "success"
2649 (progn
2650 (org-link-set-parameters "foo" :export (lambda (p d f) "success"))
2651 (org-export-string-as
2652 "[[foo:path]]"
2653 (org-export-create-backend
2654 :name 'test
2655 :transcoders
2656 '((section . (lambda (s c i) c))
2657 (paragraph . (lambda (p c i) c))
2658 (link . (lambda (l c i)
2659 (or (org-export-custom-protocol-maybe l c 'test)
2660 "failure")))))))))
2661 (should-not
2662 (string-match
2663 "success"
2664 (progn
2665 (org-link-set-parameters
2666 "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
2667 (org-export-string-as
2668 "[[foo:path]]"
2669 (org-export-create-backend
2670 :name 'no-test
2671 :transcoders
2672 '((section . (lambda (s c i) c))
2673 (paragraph . (lambda (p c i) c))
2674 (link . (lambda (l c i)
2675 (or (org-export-custom-protocol-maybe l c 'no-test)
2676 "failure")))))))))
2677 ;; Ignore anonymous back-ends.
2678 (should-not
2679 (string-match
2680 "success"
2681 (progn
2682 (org-link-set-parameters
2683 "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
2684 (org-export-string-as
2685 "[[foo:path]]"
2686 (org-export-create-backend
2687 :transcoders
2688 '((section . (lambda (s c i) c))
2689 (paragraph . (lambda (p c i) c))
2690 (link . (lambda (l c i)
2691 (or (org-export-custom-protocol-maybe l c nil)
2692 "failure"))))))))))
2694 (ert-deftest test-org-export/get-coderef-format ()
2695 "Test `org-export-get-coderef-format' specifications."
2696 ;; A link without description returns "%s"
2697 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
2698 "%s"))
2699 ;; Return "%s" when path is matched within description.
2700 (should (equal (org-export-get-coderef-format "path" "desc (path)")
2701 "desc %s"))
2702 ;; Otherwise return description.
2703 (should (equal (org-export-get-coderef-format "path" "desc")
2704 "desc")))
2706 (ert-deftest test-org-export/inline-image-p ()
2707 "Test `org-export-inline-image-p' specifications."
2708 (should
2709 (org-export-inline-image-p
2710 (org-test-with-temp-text "[[#id]]"
2711 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
2712 '(("custom-id" . "id")))))
2714 (ert-deftest test-org-export/fuzzy-link ()
2715 "Test fuzzy links specifications."
2716 ;; Link to an headline should return headline's number.
2717 (should
2718 ;; Note: Headline's number is in fact a list of numbers.
2719 (equal '(2)
2720 (org-test-with-parsed-data
2721 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
2722 (org-element-map tree 'link
2723 (lambda (link)
2724 (org-export-get-ordinal
2725 (org-export-resolve-fuzzy-link link info) info)) info t))))
2726 ;; Link to a target in an item should return item's number.
2727 (should
2728 ;; Note: Item's number is in fact a list of numbers.
2729 (equal '(1 2)
2730 (org-test-with-parsed-data
2731 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
2732 (org-element-map tree 'link
2733 (lambda (link)
2734 (org-export-get-ordinal
2735 (org-export-resolve-fuzzy-link link info) info)) info t))))
2736 ;; Link to a target in a footnote should return footnote's number.
2737 (should
2738 (equal '(2 3)
2739 (org-test-with-parsed-data "
2740 Paragraph[fn:1][fn:2][fn:lbl3:C<<target>>][[test]][[target]]
2741 \[fn:1] A
2743 \[fn:2] <<test>>B"
2744 (org-element-map tree 'link
2745 (lambda (link)
2746 (org-export-get-ordinal
2747 (org-export-resolve-fuzzy-link link info) info)) info))))
2748 ;; Link to a named element should return sequence number of that
2749 ;; element.
2750 (should
2751 (= 2
2752 (org-test-with-parsed-data
2753 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
2754 (org-element-map tree 'link
2755 (lambda (link)
2756 (org-export-get-ordinal
2757 (org-export-resolve-fuzzy-link link info) info)) info t))))
2758 ;; Link to a target not within an item, a table, a footnote
2759 ;; reference or definition should return section number.
2760 (should
2761 (equal '(2)
2762 (org-test-with-parsed-data
2763 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
2764 (org-element-map tree 'link
2765 (lambda (link)
2766 (org-export-get-ordinal
2767 (org-export-resolve-fuzzy-link link info) info)) info t))))
2768 ;; Space are not significant when matching a fuzzy link.
2769 (should
2770 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
2771 (org-element-map tree 'link
2772 (lambda (link) (org-export-resolve-fuzzy-link link info))
2773 info t)))
2774 ;; Statistics cookies are ignored for headline match.
2775 (should
2776 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
2777 (org-element-map tree 'link
2778 (lambda (link) (org-export-resolve-fuzzy-link link info))
2779 info t)))
2780 (should
2781 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
2782 (org-element-map tree 'link
2783 (lambda (link) (org-export-resolve-fuzzy-link link info))
2784 info t))))
2786 (defun test-org-gen-loc-list(text type)
2787 (org-test-with-parsed-data text
2788 (org-element-map tree type
2789 (lambda (el) (or (org-export-get-loc el info) 'no-loc)))))
2791 (ert-deftest test-org-export/get-loc ()
2792 "Test `org-export-get-loc' specifications."
2793 (should
2794 ;; "-n" resets line number.
2795 (equal '(0)
2796 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n\n Text\n#+END_EXAMPLE"
2797 'example-block)))
2798 ;; The first "+n" has 0 lines before it
2799 (should
2800 (equal '(0)
2801 (test-org-gen-loc-list "#+BEGIN_EXAMPLE +n\n Text\n#+END_EXAMPLE"
2802 'example-block)))
2803 ;; "-n 10" resets line number but has "9 lines" before it.
2804 (should
2805 (equal '(9)
2806 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n 10\n Text\n#+END_EXAMPLE"
2807 'example-block)))
2808 ;; -n10 with two lines then +n 15
2809 (should
2810 (equal '(9 25)
2811 (test-org-gen-loc-list "
2812 #+BEGIN_EXAMPLE -n 10
2813 Text_10
2814 Second line(11)
2815 #+END_EXAMPLE
2816 #+BEGIN_EXAMPLE +n 15
2817 Text line (11 + 15)
2818 #+END_EXAMPLE"
2819 'example-block)))
2820 (should
2821 (equal '(9 19 0)
2822 (test-org-gen-loc-list "
2823 #+BEGIN_EXAMPLE -n 10
2824 Text
2825 #+END_EXAMPLE
2826 #+BEGIN_EXAMPLE +n 10
2827 Text
2828 #+END_EXAMPLE
2830 #+BEGIN_EXAMPLE -n
2831 Text
2832 #+END_EXAMPLE"
2833 'example-block)))
2834 ;; an Example Block without -n does not add to the line count.
2835 (should
2836 (equal '(9 no-loc 19)
2837 (test-org-gen-loc-list "
2838 #+BEGIN_EXAMPLE -n 10
2839 Text
2840 #+END_EXAMPLE
2841 #+BEGIN_EXAMPLE
2842 Text
2843 #+END_EXAMPLE
2844 #+BEGIN_EXAMPLE +n 10
2845 Text
2846 #+END_EXAMPLE"
2847 'example-block)))
2848 ;; "-n" resets line number.
2849 (should
2850 (equal
2851 '(0)
2852 (test-org-gen-loc-list "#+BEGIN_SRC emacs-lisp -n \n (- 1 1) \n#+END_SRC"
2853 'src-block)))
2854 ;; The first "+n" has 0 lines before it.
2855 (should
2856 (equal '(0)
2857 (test-org-gen-loc-list
2858 "#+BEGIN_SRC emacs-lisp +n \n (+ 0 (- 1 1))\n#+END_SRC"
2859 'src-block)))
2860 ;; "-n 10" resets line number but has "9 lines" before it.
2861 (should
2862 (equal '(9)
2863 (test-org-gen-loc-list
2864 "#+BEGIN_SRC emacs-lisp -n 10\n (- 10 1)\n#+END_SRC"
2865 'src-block)))
2866 (should
2867 (equal '(9 25)
2868 (test-org-gen-loc-list "
2869 #+BEGIN_SRC emacs-lisp -n 10
2870 (- 10 1)
2871 (+ (- 10 1) 1)
2872 #+END_SRC
2873 #+BEGIN_SRC emacs-lisp +n 15
2874 (+ (- 10 1) 2 (- 15 1))
2875 #+END_SRC"
2876 'src-block)))
2877 (should
2878 (equal '(9 19 0)
2879 (test-org-gen-loc-list "
2880 #+BEGIN_SRC emacs-lisp -n 10
2881 (- 10 1)
2882 #+END_SRC
2883 #+BEGIN_SRC emacs-lisp +n 10
2884 (+ (- 10 1) 1 (- 10 1))
2885 #+END_SRC
2886 #+BEGIN_SRC emacs-lisp -n
2887 (- 1 1)
2888 #+END_SRC"
2889 'src-block)))
2890 ;; A SRC Block without -n does not add to the line count.
2891 (should
2892 (equal '(9 no-loc 19)
2893 (test-org-gen-loc-list
2894 "#+BEGIN_SRC emacs-lisp -n 10
2895 (+ (-10 1) 1)
2896 #+END_SRC
2897 #+BEGIN_SRC emacs-lisp
2898 (+ 2 2)
2899 #+END_SRC
2900 #+BEGIN_SRC emacs-lisp +n 10
2901 (+ (- 10 1) 1 (- 10 1))
2902 #+END_SRC"
2903 'src-block))))
2905 (ert-deftest test-org-export/resolve-coderef ()
2906 "Test `org-export-resolve-coderef' specifications."
2907 (let ((org-coderef-label-format "(ref:%s)"))
2908 ;; A link to a "-n -k -r" block returns line number.
2909 (should
2910 (= 1
2911 (org-test-with-parsed-data
2912 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
2913 (org-export-resolve-coderef "coderef" info))))
2914 (should
2915 (= 10
2916 (org-test-with-parsed-data
2917 "#+BEGIN_EXAMPLE -n 10 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
2918 (org-export-resolve-coderef "coderef" info))))
2919 (should
2920 (= 135
2921 (org-test-with-parsed-data
2922 "#+BEGIN_EXAMPLE -n 10 -k -r\nText \n#+END_EXAMPLE\n
2923 #+BEGIN_EXAMPLE +n 125 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
2924 (org-export-resolve-coderef "coderef" info))))
2925 (should
2926 (= 1
2927 (org-test-with-parsed-data
2928 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2929 (org-export-resolve-coderef "coderef" info))))
2930 (should
2931 (= 10
2932 (org-test-with-parsed-data
2933 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2934 (org-export-resolve-coderef "coderef" info))))
2935 (should
2936 (= 135
2937 (org-test-with-parsed-data
2938 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) \n#+END_SRC\n
2939 #+BEGIN_SRC emacs-lisp +n 125 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2940 (org-export-resolve-coderef "coderef" info))))
2941 ;; A link to a "-n -r" block returns line number.
2942 (should
2943 (= 1
2944 (org-test-with-parsed-data
2945 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
2946 (org-export-resolve-coderef "coderef" info))))
2947 (should
2948 (= 10
2949 (org-test-with-parsed-data
2950 "#+BEGIN_EXAMPLE -n 10 -r\nText (ref:coderef)\n#+END_EXAMPLE"
2951 (org-export-resolve-coderef "coderef" info))))
2952 (should
2953 (= 135
2954 (org-test-with-parsed-data
2955 "#+BEGIN_EXAMPLE +n 10 -r\nText \n#+END_EXAMPLE
2956 #+BEGIN_EXAMPLE +n 125 -r\nText (ref:coderef)\n#+END_EXAMPLE"
2957 (org-export-resolve-coderef "coderef" info))))
2959 (should
2960 (= 1
2961 (org-test-with-parsed-data
2962 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2963 (org-export-resolve-coderef "coderef" info))))
2964 (should
2965 (= 10
2966 (org-test-with-parsed-data
2967 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2968 (org-export-resolve-coderef "coderef" info))))
2969 (should
2970 (= 135
2971 (org-test-with-parsed-data
2972 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) \n#+END_SRC
2973 #+BEGIN_SRC emacs-lisp +n125 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2974 (org-export-resolve-coderef "coderef" info))))
2975 ;; A link to a "-n" block returns coderef.
2976 (should
2977 (equal "coderef"
2978 (org-test-with-parsed-data
2979 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2980 (org-export-resolve-coderef "coderef" info))))
2981 (should
2982 (equal "coderef"
2983 (org-test-with-parsed-data
2984 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
2985 (org-export-resolve-coderef "coderef" info))))
2986 ;; A link to a "-r" block returns line number.
2987 (should
2988 (= 1
2989 (org-test-with-parsed-data
2990 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2991 (org-export-resolve-coderef "coderef" info))))
2992 (should
2993 (= 1
2994 (org-test-with-parsed-data
2995 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
2996 (org-export-resolve-coderef "coderef" info))))
2997 ;; A link to a block without a switch returns coderef.
2998 (should
2999 (equal "coderef"
3000 (org-test-with-parsed-data
3001 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3002 (org-export-resolve-coderef "coderef" info))))
3003 (org-test-with-parsed-data
3004 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
3005 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
3006 ;; Correctly handle continued line numbers. A "+n" switch should
3007 ;; resume numbering from previous block with numbered lines,
3008 ;; ignoring blocks not numbering lines in the process. A "-n"
3009 ;; switch resets count.
3010 (should
3011 (equal '(2 1)
3012 (org-test-with-parsed-data "
3013 #+BEGIN_EXAMPLE -n
3014 Text.
3015 #+END_EXAMPLE
3017 #+BEGIN_SRC emacs-lisp
3018 \(- 1 1)
3019 #+END_SRC
3021 #+BEGIN_SRC emacs-lisp +n -r
3022 \(+ 1 1) (ref:addition)
3023 #+END_SRC
3025 #+BEGIN_EXAMPLE -n -r
3026 Another text. (ref:text)
3027 #+END_EXAMPLE"
3028 (list (org-export-resolve-coderef "addition" info)
3029 (org-export-resolve-coderef "text" info)))))
3030 ;; Recognize coderef with user-specified syntax.
3031 (should
3032 (equal
3033 "text"
3034 (org-test-with-parsed-data
3035 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
3036 (org-export-resolve-coderef "text" info))))
3037 ;; Unresolved coderefs raise a `org-link-broken' signal.
3038 (should
3039 (condition-case nil
3040 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
3041 (org-export-resolve-coderef "unknown" info))
3042 (org-link-broken t)))))
3044 (ert-deftest test-org-export/resolve-fuzzy-link ()
3045 "Test `org-export-resolve-fuzzy-link' specifications."
3046 ;; Match target objects.
3047 (should
3048 (org-test-with-parsed-data "<<target>> [[target]]"
3049 (org-export-resolve-fuzzy-link
3050 (org-element-map tree 'link 'identity info t) info)))
3051 ;; Match named elements.
3052 (should
3053 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
3054 (org-export-resolve-fuzzy-link
3055 (org-element-map tree 'link 'identity info t) info)))
3056 ;; Match exact headline's name.
3057 (should
3058 (org-test-with-parsed-data "* My headline\n[[My headline]]"
3059 (org-export-resolve-fuzzy-link
3060 (org-element-map tree 'link 'identity info t) info)))
3061 ;; Targets objects have priority over headline titles.
3062 (should
3063 (eq 'target
3064 (org-test-with-parsed-data "* target\n<<target>>[[target]]"
3065 (org-element-type
3066 (org-export-resolve-fuzzy-link
3067 (org-element-map tree 'link 'identity info t) info)))))
3068 ;; Named elements have priority over headline titles.
3069 (should
3070 (eq 'paragraph
3071 (org-test-with-parsed-data
3072 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
3073 (org-element-type
3074 (org-export-resolve-fuzzy-link
3075 (org-element-map tree 'link 'identity info t) info)))))
3076 ;; If link's path starts with a "*", only match headline titles,
3077 ;; though.
3078 (should
3079 (eq 'headline
3080 (org-test-with-parsed-data
3081 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
3082 (org-element-type
3083 (org-export-resolve-fuzzy-link
3084 (org-element-map tree 'link 'identity info t) info)))))
3085 ;; Raise a `org-link-broken' signal if no match.
3086 (should
3087 (org-test-with-parsed-data "[[target]]"
3088 (condition-case nil
3089 (org-export-resolve-fuzzy-link
3090 (org-element-map tree 'link #'identity info t) info)
3091 (org-link-broken t))))
3092 ;; Match fuzzy link even when before first headline.
3093 (should
3094 (eq 'headline
3095 (org-test-with-parsed-data "[[hl]]\n* hl"
3096 (org-element-type
3097 (org-export-resolve-fuzzy-link
3098 (org-element-map tree 'link 'identity info t) info)))))
3099 ;; Handle url-encoded fuzzy links.
3100 (should
3101 (org-test-with-parsed-data "* A B\n[[A%20B]]"
3102 (org-export-resolve-fuzzy-link
3103 (org-element-map tree 'link #'identity info t) info))))
3105 (ert-deftest test-org-export/resolve-id-link ()
3106 "Test `org-export-resolve-id-link' specifications."
3107 ;; Regular test for custom-id link.
3108 (should
3109 (equal '("Headline1")
3110 (org-test-with-parsed-data "* Headline1
3111 :PROPERTIES:
3112 :CUSTOM_ID: test
3113 :END:
3114 * Headline 2
3115 \[[#test]]"
3116 (org-element-property
3117 :title
3118 (org-export-resolve-id-link
3119 (org-element-map tree 'link 'identity info t) info)))))
3120 ;; Raise a `org-link-broken' signal on failing searches.
3121 (should
3122 (org-test-with-parsed-data "* Headline1
3123 :PROPERTIES:
3124 :CUSTOM_ID: test
3125 :END:
3126 * Headline 2
3127 \[[#no-match]]"
3128 (condition-case nil
3129 (org-export-resolve-id-link
3130 (org-element-map tree 'link #'identity info t) info)
3131 (org-link-broken t))))
3132 ;; Test for internal id target.
3133 (should
3134 (equal '("Headline1")
3135 (org-test-with-parsed-data "* Headline1
3136 :PROPERTIES:
3137 :ID: aaaa
3138 :END:
3139 * Headline 2
3140 \[[id:aaaa]]"
3141 (org-element-property
3142 :title
3143 (org-export-resolve-id-link
3144 (org-element-map tree 'link 'identity info t) info)))))
3145 ;; Test for external id target.
3146 (should
3147 (equal
3148 "external-file"
3149 (org-test-with-parsed-data "[[id:aaaa]]"
3150 (org-export-resolve-id-link
3151 (org-element-map tree 'link 'identity info t)
3152 (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
3154 (ert-deftest test-org-export/resolve-radio-link ()
3155 "Test `org-export-resolve-radio-link' specifications."
3156 ;; Standard test.
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 targets are case-insensitive.
3166 (should
3167 (org-test-with-temp-text "<<<RADIO>>> radio"
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.
3175 (should
3176 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
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 target with objects at its beginning.
3184 (should
3185 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
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 ;; Radio link next to an apostrophe.
3193 (should
3194 (org-test-with-temp-text "<<<radio>>> radio's"
3195 (org-update-radio-target-regexp)
3196 (let* ((tree (org-element-parse-buffer))
3197 (info `(:parse-tree ,tree)))
3198 (org-export-resolve-radio-link
3199 (org-element-map tree 'link 'identity info t)
3200 info))))
3201 ;; Multiple radio targets.
3202 (should
3203 (equal '("radio1" "radio2")
3204 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
3205 (org-update-radio-target-regexp)
3206 (let* ((tree (org-element-parse-buffer))
3207 (info `(:parse-tree ,tree)))
3208 (org-element-map tree 'link
3209 (lambda (link)
3210 (org-element-property
3211 :value (org-export-resolve-radio-link link info)))
3212 info)))))
3213 ;; Radio target is whitespace insensitive.
3214 (should
3215 (org-test-with-temp-text "<<<a radio>>> a\n radio"
3216 (org-update-radio-target-regexp)
3217 (let* ((tree (org-element-parse-buffer))
3218 (info `(:parse-tree ,tree)))
3219 (org-element-map tree 'link
3220 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
3222 (ert-deftest test-org-export/file-uri ()
3223 "Test `org-export-file-uri' specifications."
3224 ;; Preserve relative filenames.
3225 (should (equal "relative.org" (org-export-file-uri "relative.org")))
3226 ;; Local files start with "file:///"
3227 (should (equal "file:///local.org" (org-export-file-uri "/local.org")))
3228 ;; Remote files start with "file://"
3229 (should (equal "file://myself@some.where:papers/last.pdf"
3230 (org-export-file-uri "/myself@some.where:papers/last.pdf")))
3231 (should (equal "file://localhost/etc/fstab"
3232 (org-export-file-uri "//localhost/etc/fstab")))
3233 ;; Expand filename starting with "~".
3234 (should (equal (org-export-file-uri "~/file.org")
3235 (concat "file://" (expand-file-name "~/file.org")))))
3237 (ert-deftest test-org-export/get-reference ()
3238 "Test `org-export-get-reference' specifications."
3239 (should
3240 (org-test-with-parsed-data "* Headline"
3241 (org-export-get-reference (org-element-map tree 'headline #'identity nil t)
3242 info)))
3243 ;; For a given element always return the same reference.
3244 (should
3245 (org-test-with-parsed-data "* Headline"
3246 (let ((headline (org-element-map tree 'headline #'identity nil t)))
3247 (equal (org-export-get-reference headline info)
3248 (org-export-get-reference headline info)))))
3249 ;; References get through local export back-ends.
3250 (should
3251 (org-test-with-parsed-data "* Headline"
3252 (let ((headline (org-element-map tree 'headline #'identity nil t))
3253 (backend
3254 (org-export-create-backend
3255 :transcoders
3256 '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
3257 (equal (org-trim (org-export-data-with-backend headline backend info))
3258 (org-export-get-reference headline info)))))
3259 (should
3260 (org-test-with-parsed-data "* Headline"
3261 (let ((headline (org-element-map tree 'headline #'identity nil t))
3262 (backend
3263 (org-export-create-backend
3264 :transcoders
3265 '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
3266 (equal (org-export-with-backend backend headline nil info)
3267 (org-export-get-reference headline info)))))
3268 ;; Use search cells defined in `:crossrefs'. However, handle
3269 ;; duplicate search cells.
3270 (should
3271 (equal "org0000001"
3272 (org-test-with-parsed-data "* Headline"
3273 (let* ((headline (org-element-map tree 'headline #'identity nil t))
3274 (search-cell (car (org-export-search-cells headline))))
3275 (setq info
3276 (plist-put info :crossrefs (list (cons search-cell 1))))
3277 (org-export-get-reference headline info)))))
3278 (should-not
3279 (equal '("org0000001" "org0000001")
3280 (org-test-with-parsed-data "* H\n** H"
3281 (org-element-map tree 'headline
3282 (lambda (h)
3283 (let* ((search-cell (car (org-export-search-cells h)))
3284 (info (plist-put info :crossrefs
3285 (list (cons search-cell 1)))))
3286 (org-export-get-reference h info))))))))
3289 ;;; Pseudo objects and pseudo elements
3291 (ert-deftest test-org-export/pseudo-elements ()
3292 "Test exporting pseudo-elements."
3293 ;; Handle blank lines after pseudo-elements. In particular, do not
3294 ;; replace them with white spaces.
3295 (should
3296 (equal "contents\n\nparagraph\n"
3297 (let ((backend (org-export-create-backend
3298 :transcoders
3299 '((pseudo-element . (lambda (_p c _i) c))
3300 (paragraph . (lambda (_p c _i) c))
3301 (plain-text . (lambda (c _i) c)))))
3302 (element '(pseudo-element (:post-blank 1) "contents"))
3303 (paragraph '(paragraph nil "paragraph"))
3304 (data '(org-data nil)))
3305 (org-element-adopt-elements data element paragraph)
3306 (org-export-data-with-backend data backend nil)))))
3308 (ert-deftest test-org-export/pseudo-objects ()
3309 "Test exporting pseudo-objects."
3310 ;; Handle blank spaces after pseudo-objects. In particular, do not
3311 ;; replace them with newlines.
3312 (should
3313 (equal "begin x end\n"
3314 (let ((backend (org-export-create-backend
3315 :transcoders
3316 '((pseudo-object . (lambda (_p c _i) c))
3317 (paragraph . (lambda (_p c _i) c))
3318 (plain-text . (lambda (c _i) c)))))
3319 (object '(pseudo-object (:post-blank 1) "x"))
3320 (paragraph '(paragraph nil)))
3321 (org-element-adopt-elements paragraph "begin " object "end")
3322 (org-export-data-with-backend paragraph backend nil)))))
3325 ;;; Src-block and example-block
3327 (ert-deftest test-org-export/unravel-code ()
3328 "Test `org-export-unravel-code' function."
3329 ;; Code without reference.
3330 (should
3331 (equal '("(+ 1 1)\n")
3332 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
3333 (org-export-unravel-code (org-element-at-point)))))
3334 ;; Code with reference.
3335 (should
3336 (equal '("(+ 1 1)\n" (1 . "test"))
3337 (org-test-with-temp-text
3338 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
3339 (let ((org-coderef-label-format "(ref:%s)"))
3340 (org-export-unravel-code (org-element-at-point))))))
3341 ;; Code with user-defined reference.
3342 (should
3343 (equal
3344 '("(+ 1 1)\n" (1 . "test"))
3345 (org-test-with-temp-text
3346 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
3347 (let ((org-coderef-label-format "(ref:%s)"))
3348 (org-export-unravel-code (org-element-at-point))))))
3349 ;; Code references keys are relative to the current block.
3350 (should
3351 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
3352 (org-test-with-temp-text "
3353 #+BEGIN_EXAMPLE -n
3354 \(+ 1 1)
3355 #+END_EXAMPLE
3356 #+BEGIN_EXAMPLE +n
3357 \(+ 2 2)
3358 \(+ 3 3) (ref:one)
3359 #+END_EXAMPLE"
3360 (goto-line 5)
3361 (let ((org-coderef-label-format "(ref:%s)"))
3362 (org-export-unravel-code (org-element-at-point)))))))
3364 (ert-deftest test-org-export/format-code-default ()
3365 "Test `org-export-format-code-default' specifications."
3366 ;; Return the empty string when code is empty.
3367 (should
3368 (equal ""
3369 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
3370 (org-export-format-code-default
3371 (org-element-map tree 'src-block 'identity info t) info))))
3372 ;; Number lines, two whitespace characters before the actual loc.
3373 (should
3374 (equal "1 a\n2 b\n"
3375 (org-test-with-parsed-data
3376 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
3377 (org-export-format-code-default
3378 (org-element-map tree 'src-block 'identity info t) info))))
3379 ;; Put references 6 whitespace characters after the widest line,
3380 ;; wrapped within parenthesis.
3381 (should
3382 (equal "123 (a)\n1 (b)\n"
3383 (let ((org-coderef-label-format "(ref:%s)"))
3384 (org-test-with-parsed-data
3385 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
3386 (org-export-format-code-default
3387 (org-element-map tree 'src-block 'identity info t) info))))))
3391 ;;; Smart Quotes
3393 (ert-deftest test-org-export/activate-smart-quotes ()
3394 "Test `org-export-activate-smart-quotes' specifications."
3395 ;; Double quotes: standard test.
3396 (should
3397 (equal
3398 '("some &ldquo;quoted&rdquo; text")
3399 (let ((org-export-default-language "en"))
3400 (org-test-with-parsed-data "some \"quoted\" text"
3401 (org-element-map tree 'plain-text
3402 (lambda (s) (org-export-activate-smart-quotes s :html info))
3403 info)))))
3404 ;; Opening quotes: at the beginning of a paragraph.
3405 (should
3406 (equal
3407 '("&ldquo;begin")
3408 (let ((org-export-default-language "en"))
3409 (org-test-with-parsed-data "\"begin"
3410 (org-element-map tree 'plain-text
3411 (lambda (s) (org-export-activate-smart-quotes s :html info))
3412 info)))))
3413 ;; Opening quotes: after an object.
3414 (should
3415 (equal
3416 '("&ldquo;quoted&rdquo; text")
3417 (let ((org-export-default-language "en"))
3418 (org-test-with-parsed-data "=verb= \"quoted\" text"
3419 (org-element-map tree 'plain-text
3420 (lambda (s) (org-export-activate-smart-quotes s :html info))
3421 info)))))
3422 ;; Closing quotes: at the end of a paragraph.
3423 (should
3424 (equal
3425 '("Quoted &ldquo;text&rdquo;")
3426 (let ((org-export-default-language "en"))
3427 (org-test-with-parsed-data "Quoted \"text\""
3428 (org-element-map tree 'plain-text
3429 (lambda (s) (org-export-activate-smart-quotes s :html info))
3430 info)))))
3431 ;; Inner quotes: standard test.
3432 (should
3433 (equal '("« outer « inner » outer »")
3434 (let ((org-export-default-language "fr"))
3435 (org-test-with-parsed-data "\"outer 'inner' outer\""
3436 (org-element-map tree 'plain-text
3437 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3438 info)))))
3439 ;; Inner quotes: close to special symbols.
3440 (should
3441 (equal '("« outer (« inner ») outer »")
3442 (let ((org-export-default-language "fr"))
3443 (org-test-with-parsed-data "\"outer ('inner') outer\""
3444 (org-element-map tree 'plain-text
3445 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3446 info)))))
3447 (should
3448 (equal '("« « inner » »")
3449 (let ((org-export-default-language "fr"))
3450 (org-test-with-parsed-data "\"'inner'\""
3451 (org-element-map tree 'plain-text
3452 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3453 info)))))
3454 ;; Apostrophe: standard test.
3455 (should
3456 (equal '("It « shouldn’t » fail")
3457 (let ((org-export-default-language "fr"))
3458 (org-test-with-parsed-data "It \"shouldn't\" fail"
3459 (org-element-map tree 'plain-text
3460 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3461 info)))))
3462 (should
3463 (equal '("It shouldn’t fail")
3464 (let ((org-export-default-language "fr"))
3465 (org-test-with-parsed-data "It shouldn't fail"
3466 (org-element-map tree 'plain-text
3467 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3468 info)))))
3469 ;; Apostrophe: before an object.
3470 (should
3471 (equal
3472 '("« a’" " »")
3473 (let ((org-export-default-language "fr"))
3474 (org-test-with-parsed-data "\"a'=b=\""
3475 (org-element-map tree 'plain-text
3476 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3477 info)))))
3478 ;; Apostrophe: after an object.
3479 (should
3480 (equal '("« " "’s »")
3481 (let ((org-export-default-language "fr"))
3482 (org-test-with-parsed-data "\"=code='s\""
3483 (org-element-map tree 'plain-text
3484 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3485 info)))))
3486 ;; Special case: isolated quotes.
3487 (should
3488 (equal '("&ldquo;" "&rdquo;")
3489 (let ((org-export-default-language "en"))
3490 (org-test-with-parsed-data "\"$x$\""
3491 (org-element-map tree 'plain-text
3492 (lambda (s) (org-export-activate-smart-quotes s :html info))
3493 info)))))
3494 ;; Smart quotes in secondary strings.
3495 (should
3496 (equal '("&ldquo;" "&rdquo;")
3497 (let ((org-export-default-language "en"))
3498 (org-test-with-parsed-data "* \"$x$\""
3499 (org-element-map tree 'plain-text
3500 (lambda (s) (org-export-activate-smart-quotes s :html info))
3501 info)))))
3502 ;; Smart quotes in document keywords.
3503 (should
3504 (equal '("&ldquo;" "&rdquo;")
3505 (let ((org-export-default-language "en"))
3506 (org-test-with-parsed-data "#+TITLE: \"$x$\""
3507 (org-element-map (plist-get info :title) 'plain-text
3508 (lambda (s) (org-export-activate-smart-quotes s :html info))
3509 info)))))
3510 ;; Smart quotes in parsed affiliated keywords.
3511 (should
3512 (equal '("&ldquo;" "&rdquo;" "Paragraph")
3513 (let ((org-export-default-language "en"))
3514 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
3515 (org-element-map tree 'plain-text
3516 (lambda (s) (org-export-activate-smart-quotes s :html info))
3517 info nil nil t)))))
3518 ;; Smart quotes within objects.
3519 (should
3520 (equal '("&ldquo;foo&rdquo;")
3521 (let ((org-export-default-language "en"))
3522 (org-test-with-parsed-data "| \"foo\" |"
3523 (org-element-map tree 'plain-text
3524 (lambda (s) (org-export-activate-smart-quotes s :html info))
3525 info nil nil t)))))
3526 ;; FIXME: Test failing non-interactively.
3528 ;; (should
3529 ;; (equal '("&ldquo;foo&rdquo;")
3530 ;; (let ((org-export-default-language "en"))
3531 ;; (org-test-with-parsed-data "*\"foo\"*"
3532 ;; (org-element-map tree 'plain-text
3533 ;; (lambda (s) (org-export-activate-smart-quotes s :html info))
3534 ;; info nil nil t)))))
3539 ;;; Tables
3541 (ert-deftest test-org-export/special-column ()
3542 "Test if the table's special column is properly recognized."
3543 ;; 1. First column is special if it contains only a special marking
3544 ;; characters or empty cells.
3545 (org-test-with-temp-text "
3546 | ! | 1 |
3547 | | 2 |"
3548 (should
3549 (org-export-table-has-special-column-p
3550 (org-element-map
3551 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3552 ;; 2. If the column contains anything else, it isn't special.
3553 (org-test-with-temp-text "
3554 | ! | 1 |
3555 | b | 2 |"
3556 (should-not
3557 (org-export-table-has-special-column-p
3558 (org-element-map
3559 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3560 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
3561 ;; and "!".
3562 (org-test-with-temp-text "
3563 | # | 1 |
3564 | ^ | 2 |
3565 | * | 3 |
3566 | _ | 4 |
3567 | / | 5 |
3568 | $ | 6 |
3569 | ! | 7 |"
3570 (should
3571 (org-export-table-has-special-column-p
3572 (org-element-map
3573 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3574 ;; 4. A first column with only empty cells isn't considered as
3575 ;; special.
3576 (org-test-with-temp-text "
3577 | | 1 |
3578 | | 2 |"
3579 (should-not
3580 (org-export-table-has-special-column-p
3581 (org-element-map
3582 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
3584 (ert-deftest test-org-export/table-row-is-special-p ()
3585 "Test `org-export-table-row-is-special-p' specifications."
3586 ;; 1. A row is special if it has a special marking character in the
3587 ;; special column.
3588 (org-test-with-parsed-data "| ! | 1 |"
3589 (should
3590 (org-export-table-row-is-special-p
3591 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3592 ;; 2. A row is special when its first field is "/"
3593 (org-test-with-parsed-data "
3594 | / | 1 |
3595 | a | b |"
3596 (should
3597 (org-export-table-row-is-special-p
3598 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3599 ;; 3. A row only containing alignment cookies is also considered as
3600 ;; special.
3601 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
3602 (should
3603 (org-export-table-row-is-special-p
3604 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3605 ;; 4. Everything else isn't considered as special.
3606 (org-test-with-parsed-data "| \alpha | | c |"
3607 (should-not
3608 (org-export-table-row-is-special-p
3609 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3610 ;; 5. Table's rules are never considered as special rows.
3611 (org-test-with-parsed-data "|---+---|"
3612 (should-not
3613 (org-export-table-row-is-special-p
3614 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
3616 (ert-deftest test-org-export/has-header-p ()
3617 "Test `org-export-table-has-header-p' specifications."
3618 ;; 1. With an header.
3619 (org-test-with-parsed-data "
3620 | a | b |
3621 |---+---|
3622 | c | d |"
3623 (should
3624 (org-export-table-has-header-p
3625 (org-element-map tree 'table 'identity info 'first-match)
3626 info)))
3627 ;; 2. Without an header.
3628 (org-test-with-parsed-data "
3629 | a | b |
3630 | c | d |"
3631 (should-not
3632 (org-export-table-has-header-p
3633 (org-element-map tree 'table 'identity info 'first-match)
3634 info)))
3635 ;; 3. Don't get fooled with starting and ending rules.
3636 (org-test-with-parsed-data "
3637 |---+---|
3638 | a | b |
3639 | c | d |
3640 |---+---|"
3641 (should-not
3642 (org-export-table-has-header-p
3643 (org-element-map tree 'table 'identity info 'first-match)
3644 info))))
3646 (ert-deftest test-org-export/table-row-group ()
3647 "Test `org-export-table-row-group' specifications."
3648 ;; 1. A rule creates a new group.
3649 (should
3650 (equal '(1 rule 2)
3651 (org-test-with-parsed-data "
3652 | a | b |
3653 |---+---|
3654 | 1 | 2 |"
3655 (org-element-map tree 'table-row
3656 (lambda (row)
3657 (if (eq (org-element-property :type row) 'rule) 'rule
3658 (org-export-table-row-group row info)))))))
3659 ;; 2. Special rows are ignored in count.
3660 (should
3661 (equal
3662 '(rule 1)
3663 (org-test-with-parsed-data "
3664 | / | < | > |
3665 |---|---+---|
3666 | | 1 | 2 |"
3667 (org-element-map tree 'table-row
3668 (lambda (row)
3669 (if (eq (org-element-property :type row) 'rule) 'rule
3670 (org-export-table-row-group row info)))
3671 info))))
3672 ;; 3. Double rules also are ignored in count.
3673 (should
3674 (equal '(1 rule rule 2)
3675 (org-test-with-parsed-data "
3676 | a | b |
3677 |---+---|
3678 |---+---|
3679 | 1 | 2 |"
3680 (org-element-map tree 'table-row
3681 (lambda (row)
3682 (if (eq (org-element-property :type row) 'rule) 'rule
3683 (org-export-table-row-group row info))))))))
3685 (ert-deftest test-org-export/table-row-number ()
3686 "Test `org-export-table-row-number' specifications."
3687 ;; Standard test. Number is 0-indexed.
3688 (should
3689 (equal '(0 1)
3690 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
3691 (org-element-map tree 'table-row
3692 (lambda (row) (org-export-table-row-number row info)) info))))
3693 ;; Number ignores separators.
3694 (should
3695 (equal '(0 1)
3696 (org-test-with-parsed-data "
3697 | a | b | c |
3698 |---+---+---|
3699 | d | e | f |"
3700 (org-element-map tree 'table-row
3701 (lambda (row) (org-export-table-row-number row info)) info))))
3702 ;; Number ignores special rows.
3703 (should
3704 (equal '(0 1)
3705 (org-test-with-parsed-data "
3706 | / | < | > |
3707 | | b | c |
3708 |---+-----+-----|
3709 | | <c> | <c> |
3710 | | e | f |"
3711 (org-element-map tree 'table-row
3712 (lambda (row) (org-export-table-row-number row info)) info)))))
3714 (ert-deftest test-org-export/table-cell-width ()
3715 "Test `org-export-table-cell-width' specifications."
3716 ;; 1. Width is primarily determined by width cookies. If no cookie
3717 ;; is found, cell's width is nil.
3718 (org-test-with-parsed-data "
3719 | / | <l> | <6> | <l7> |
3720 | | a | b | c |"
3721 (should
3722 (equal
3723 '(nil 6 7)
3724 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3725 (org-element-map tree 'table-cell 'identity info)))))
3726 ;; 2. The last width cookie has precedence.
3727 (org-test-with-parsed-data "
3728 | <6> |
3729 | <7> |
3730 | a |"
3731 (should
3732 (equal
3733 '(7)
3734 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3735 (org-element-map tree 'table-cell 'identity info)))))
3736 ;; 3. Valid width cookies must have a specific row.
3737 (org-test-with-parsed-data "| <6> | cell |"
3738 (should
3739 (equal
3740 '(nil nil)
3741 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3742 (org-element-map tree 'table-cell 'identity))))))
3744 (ert-deftest test-org-export/table-cell-alignment ()
3745 "Test `org-export-table-cell-alignment' specifications."
3746 ;; 1. Alignment is primarily determined by alignment cookies.
3747 (should
3748 (equal '(left center right)
3749 (let ((org-table-number-fraction 0.5)
3750 (org-table-number-regexp "^[0-9]+$"))
3751 (org-test-with-parsed-data "| <l> | <c> | <r> |"
3752 (mapcar (lambda (cell)
3753 (org-export-table-cell-alignment cell info))
3754 (org-element-map tree 'table-cell 'identity))))))
3755 ;; 2. The last alignment cookie has precedence.
3756 (should
3757 (equal '(right right right)
3758 (org-test-with-parsed-data "
3759 | <l8> |
3760 | cell |
3761 | <r9> |"
3762 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
3763 (org-element-map tree 'table-cell 'identity)))))
3764 ;; 3. If there's no cookie, cell's contents determine alignment.
3765 ;; A column mostly made of cells containing numbers will align
3766 ;; its cells to the right.
3767 (should
3768 (equal '(right right right)
3769 (let ((org-table-number-fraction 0.5)
3770 (org-table-number-regexp "^[0-9]+$"))
3771 (org-test-with-parsed-data "
3772 | 123 |
3773 | some text |
3774 | 12345 |"
3775 (mapcar (lambda (cell)
3776 (org-export-table-cell-alignment cell info))
3777 (org-element-map tree 'table-cell 'identity))))))
3778 ;; 4. Otherwise, they will be aligned to the left.
3779 (should
3780 (equal '(left left left)
3781 (org-test-with-parsed-data "
3782 | text |
3783 | some text |
3784 | \alpha |"
3785 (mapcar (lambda (cell)
3786 (org-export-table-cell-alignment cell info))
3787 (org-element-map tree 'table-cell 'identity info))))))
3789 (ert-deftest test-org-export/table-cell-borders ()
3790 "Test `org-export-table-cell-borders' specifications."
3791 ;; 1. Recognize various column groups indicators.
3792 (org-test-with-parsed-data "| / | < | > | <> |"
3793 (should
3794 (equal
3795 '((right bottom top) (left bottom top) (right bottom top)
3796 (right left bottom top))
3797 (mapcar (lambda (cell)
3798 (org-export-table-cell-borders cell info))
3799 (org-element-map tree 'table-cell 'identity)))))
3800 ;; 2. Accept shortcuts to define column groups.
3801 (org-test-with-parsed-data "| / | < | < |"
3802 (should
3803 (equal
3804 '((right bottom top) (right left bottom top) (left bottom top))
3805 (mapcar (lambda (cell)
3806 (org-export-table-cell-borders cell info))
3807 (org-element-map tree 'table-cell 'identity)))))
3808 ;; 3. A valid column groups row must start with a "/".
3809 (org-test-with-parsed-data "
3810 | | < |
3811 | a | b |"
3812 (should
3813 (equal '((top) (top) (bottom) (bottom))
3814 (mapcar (lambda (cell)
3815 (org-export-table-cell-borders cell info))
3816 (org-element-map tree 'table-cell 'identity)))))
3817 ;; 4. Take table rules into consideration.
3818 (org-test-with-parsed-data "
3819 | 1 |
3820 |---|
3821 | 2 |"
3822 (should
3823 (equal '((below top) (bottom above))
3824 (mapcar (lambda (cell)
3825 (org-export-table-cell-borders cell info))
3826 (org-element-map tree 'table-cell 'identity)))))
3827 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
3828 ;; (resp. `bottom' and `below') borders. Any special row is
3829 ;; ignored.
3830 (org-test-with-parsed-data "
3831 |---+----|
3832 | / | |
3833 | | 1 |
3834 |---+----|"
3835 (should
3836 (equal '((bottom below top above))
3837 (last
3838 (mapcar (lambda (cell)
3839 (org-export-table-cell-borders cell info))
3840 (org-element-map tree 'table-cell 'identity)))))))
3842 (ert-deftest test-org-export/table-dimensions ()
3843 "Test `org-export-table-dimensions' specifications."
3844 ;; 1. Standard test.
3845 (org-test-with-parsed-data "
3846 | 1 | 2 | 3 |
3847 | 4 | 5 | 6 |"
3848 (should
3849 (equal '(2 . 3)
3850 (org-export-table-dimensions
3851 (org-element-map tree 'table 'identity info 'first-match) info))))
3852 ;; 2. Ignore horizontal rules and special columns.
3853 (org-test-with-parsed-data "
3854 | / | < | > |
3855 | 1 | 2 | 3 |
3856 |---+---+---|
3857 | 4 | 5 | 6 |"
3858 (should
3859 (equal '(2 . 3)
3860 (org-export-table-dimensions
3861 (org-element-map tree 'table 'identity info 'first-match) info)))))
3863 (ert-deftest test-org-export/table-cell-address ()
3864 "Test `org-export-table-cell-address' specifications."
3865 ;; 1. Standard test: index is 0-based.
3866 (org-test-with-parsed-data "| a | b |"
3867 (should
3868 (equal '((0 . 0) (0 . 1))
3869 (org-element-map tree 'table-cell
3870 (lambda (cell) (org-export-table-cell-address cell info))
3871 info))))
3872 ;; 2. Special column isn't counted, nor are special rows.
3873 (org-test-with-parsed-data "
3874 | / | <> |
3875 | | c |"
3876 (should
3877 (equal '(0 . 0)
3878 (org-export-table-cell-address
3879 (car (last (org-element-map tree 'table-cell 'identity info)))
3880 info))))
3881 ;; 3. Tables rules do not count either.
3882 (org-test-with-parsed-data "
3883 | a |
3884 |---|
3885 | b |
3886 |---|
3887 | c |"
3888 (should
3889 (equal '(2 . 0)
3890 (org-export-table-cell-address
3891 (car (last (org-element-map tree 'table-cell 'identity info)))
3892 info))))
3893 ;; 4. Return nil for special cells.
3894 (org-test-with-parsed-data "| / | a |"
3895 (should-not
3896 (org-export-table-cell-address
3897 (org-element-map tree 'table-cell 'identity nil 'first-match)
3898 info))))
3900 (ert-deftest test-org-export/get-table-cell-at ()
3901 "Test `org-export-get-table-cell-at' specifications."
3902 ;; 1. Address ignores special columns, special rows and rules.
3903 (org-test-with-parsed-data "
3904 | / | <> |
3905 | | a |
3906 |---+----|
3907 | | b |"
3908 (should
3909 (equal '("b")
3910 (org-element-contents
3911 (org-export-get-table-cell-at
3912 '(1 . 0)
3913 (org-element-map tree 'table 'identity info 'first-match)
3914 info)))))
3915 ;; 2. Return value for a non-existent address is nil.
3916 (org-test-with-parsed-data "| a |"
3917 (should-not
3918 (org-export-get-table-cell-at
3919 '(2 . 2)
3920 (org-element-map tree 'table 'identity info 'first-match)
3921 info)))
3922 (org-test-with-parsed-data "| / |"
3923 (should-not
3924 (org-export-get-table-cell-at
3925 '(0 . 0)
3926 (org-element-map tree 'table 'identity info 'first-match)
3927 info))))
3929 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
3930 "Test `org-export-table-cell-starts-colgroup-p' specifications."
3931 ;; 1. A cell at a beginning of a row always starts a column group.
3932 (org-test-with-parsed-data "| a |"
3933 (should
3934 (org-export-table-cell-starts-colgroup-p
3935 (org-element-map tree 'table-cell 'identity info 'first-match)
3936 info)))
3937 ;; 2. Special column should be ignored when determining the
3938 ;; beginning of the row.
3939 (org-test-with-parsed-data "
3940 | / | |
3941 | | a |"
3942 (should
3943 (org-export-table-cell-starts-colgroup-p
3944 (org-element-map tree 'table-cell 'identity info 'first-match)
3945 info)))
3946 ;; 2. Explicit column groups.
3947 (org-test-with-parsed-data "
3948 | / | | < |
3949 | a | b | c |"
3950 (should
3951 (equal
3952 '(yes no yes)
3953 (org-element-map tree 'table-cell
3954 (lambda (cell)
3955 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
3956 info)))))
3958 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
3959 "Test `org-export-table-cell-ends-colgroup-p' specifications."
3960 ;; 1. A cell at the end of a row always ends a column group.
3961 (org-test-with-parsed-data "| a |"
3962 (should
3963 (org-export-table-cell-ends-colgroup-p
3964 (org-element-map tree 'table-cell 'identity info 'first-match)
3965 info)))
3966 ;; 2. Special column should be ignored when determining the
3967 ;; beginning of the row.
3968 (org-test-with-parsed-data "
3969 | / | |
3970 | | a |"
3971 (should
3972 (org-export-table-cell-ends-colgroup-p
3973 (org-element-map tree 'table-cell 'identity info 'first-match)
3974 info)))
3975 ;; 3. Explicit column groups.
3976 (org-test-with-parsed-data "
3977 | / | < | |
3978 | a | b | c |"
3979 (should
3980 (equal
3981 '(yes no yes)
3982 (org-element-map tree 'table-cell
3983 (lambda (cell)
3984 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
3985 info)))))
3987 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
3988 "Test `org-export-table-row-starts-rowgroup-p' specifications."
3989 ;; 1. A row at the beginning of a table always starts a row group.
3990 ;; So does a row following a table rule.
3991 (org-test-with-parsed-data "
3992 | a |
3993 |---|
3994 | b |"
3995 (should
3996 (equal
3997 '(yes no yes)
3998 (org-element-map tree 'table-row
3999 (lambda (row)
4000 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
4001 info))))
4002 ;; 2. Special rows should be ignored when determining the beginning
4003 ;; of the row.
4004 (org-test-with-parsed-data "
4005 | / | < |
4006 | | a |
4007 |---+---|
4008 | / | < |
4009 | | b |"
4010 (should
4011 (equal
4012 '(yes no yes)
4013 (org-element-map tree 'table-row
4014 (lambda (row)
4015 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
4016 info)))))
4018 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
4019 "Test `org-export-table-row-ends-rowgroup-p' specifications."
4020 ;; 1. A row at the end of a table always ends a row group. So does
4021 ;; a row preceding a table rule.
4022 (org-test-with-parsed-data "
4023 | a |
4024 |---|
4025 | b |"
4026 (should
4027 (equal
4028 '(yes no yes)
4029 (org-element-map tree 'table-row
4030 (lambda (row)
4031 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
4032 info))))
4033 ;; 2. Special rows should be ignored when determining the beginning
4034 ;; of the row.
4035 (org-test-with-parsed-data "
4036 | | a |
4037 | / | < |
4038 |---+---|
4039 | | b |
4040 | / | < |"
4041 (should
4042 (equal
4043 '(yes no yes)
4044 (org-element-map tree 'table-row
4045 (lambda (row)
4046 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
4047 info)))))
4049 (ert-deftest test-org-export/table-row-in-header-p ()
4050 "Test `org-export-table-row-in-header-p' specifications."
4051 ;; Standard test. Separators are always nil.
4052 (should
4053 (equal
4054 '(yes no no)
4055 (org-test-with-parsed-data "| a |\n|---|\n| b |"
4056 (org-element-map tree 'table-row
4057 (lambda (row)
4058 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
4059 ;; Nil when there is no header.
4060 (should
4061 (equal
4062 '(no no)
4063 (org-test-with-parsed-data "| a |\n| b |"
4064 (org-element-map tree 'table-row
4065 (lambda (row)
4066 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
4068 (ert-deftest test-org-export/table-row-starts-header-p ()
4069 "Test `org-export-table-row-starts-header-p' specifications."
4070 ;; 1. Only the row starting the first row group starts the table
4071 ;; header.
4072 (org-test-with-parsed-data "
4073 | a |
4074 | b |
4075 |---|
4076 | c |"
4077 (should
4078 (equal
4079 '(yes no no no)
4080 (org-element-map tree 'table-row
4081 (lambda (row)
4082 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
4083 info))))
4084 ;; 2. A row cannot start an header if there's no header in the
4085 ;; table.
4086 (org-test-with-parsed-data "
4087 | a |
4088 |---|"
4089 (should-not
4090 (org-export-table-row-starts-header-p
4091 (org-element-map tree 'table-row 'identity info 'first-match)
4092 info))))
4094 (ert-deftest test-org-export/table-row-ends-header-p ()
4095 "Test `org-export-table-row-ends-header-p' specifications."
4096 ;; 1. Only the row starting the first row group starts the table
4097 ;; header.
4098 (org-test-with-parsed-data "
4099 | a |
4100 | b |
4101 |---|
4102 | c |"
4103 (should
4104 (equal
4105 '(no yes no no)
4106 (org-element-map tree 'table-row
4107 (lambda (row)
4108 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
4109 info))))
4110 ;; 2. A row cannot start an header if there's no header in the
4111 ;; table.
4112 (org-test-with-parsed-data "
4113 | a |
4114 |---|"
4115 (should-not
4116 (org-export-table-row-ends-header-p
4117 (org-element-map tree 'table-row 'identity info 'first-match)
4118 info))))
4122 ;;; Tables of Contents
4124 (ert-deftest test-org-export/collect-headlines ()
4125 "Test `org-export-collect-headlines' specifications."
4126 ;; Standard test.
4127 (should
4128 (= 2
4129 (length
4130 (org-test-with-parsed-data "* H1\n** H2"
4131 (org-export-collect-headlines info)))))
4132 ;; Do not collect headlines below optional argument.
4133 (should
4134 (= 1
4135 (length
4136 (org-test-with-parsed-data "* H1\n** H2"
4137 (org-export-collect-headlines info 1)))))
4138 ;; Never collect headlines below maximum headline level.
4139 (should
4140 (= 1
4141 (length
4142 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4143 (org-export-collect-headlines info)))))
4144 (should
4145 (= 1
4146 (length
4147 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4148 (org-export-collect-headlines info 2)))))
4149 ;; Collect headlines locally.
4150 (should
4151 (= 2
4152 (org-test-with-parsed-data "* H1\n** H2\n** H3"
4153 (let ((scope (org-element-map tree 'headline #'identity info t)))
4154 (length (org-export-collect-headlines info nil scope))))))
4155 ;; When collecting locally, optional level is relative.
4156 (should
4157 (= 1
4158 (org-test-with-parsed-data "* H1\n** H2\n*** H3"
4159 (let ((scope (org-element-map tree 'headline #'identity info t)))
4160 (length (org-export-collect-headlines info 1 scope)))))))
4164 ;;; Templates
4166 (ert-deftest test-org-export/inner-template ()
4167 "Test `inner-template' translator specifications."
4168 (should
4169 (equal "Success!"
4170 (org-test-with-temp-text "* Headline"
4171 (org-export-as
4172 (org-export-create-backend
4173 :transcoders
4174 '((inner-template . (lambda (contents info) "Success!"))
4175 (headline . (lambda (h c i) "Headline"))))))))
4176 ;; Inner template is applied even in a "body-only" export.
4177 (should
4178 (equal "Success!"
4179 (org-test-with-temp-text "* Headline"
4180 (org-export-as
4181 (org-export-create-backend
4182 :transcoders '((inner-template . (lambda (c i) "Success!"))
4183 (headline . (lambda (h c i) "Headline"))))
4184 nil nil 'body-only)))))
4186 (ert-deftest test-org-export/template ()
4187 "Test `template' translator specifications."
4188 (should
4189 (equal "Success!"
4190 (org-test-with-temp-text "* Headline"
4191 (org-export-as
4192 (org-export-create-backend
4193 :transcoders '((template . (lambda (contents info) "Success!"))
4194 (headline . (lambda (h c i) "Headline"))))))))
4195 ;; Template is not applied in a "body-only" export.
4196 (should-not
4197 (equal "Success!"
4198 (org-test-with-temp-text "* Headline"
4199 (org-export-as
4200 (org-export-create-backend
4201 :transcoders '((template . (lambda (contents info) "Success!"))
4202 (headline . (lambda (h c i) "Headline"))))
4203 nil nil 'body-only)))))
4207 ;;; Topology
4209 (ert-deftest test-org-export/get-next-element ()
4210 "Test `org-export-get-next-element' specifications."
4211 ;; Standard test.
4212 (should
4213 (equal "b"
4214 (org-test-with-parsed-data "* Headline\n*a* b"
4215 (org-export-get-next-element
4216 (org-element-map tree 'bold 'identity info t) info))))
4217 ;; Return nil when no previous element.
4218 (should-not
4219 (org-test-with-parsed-data "* Headline\na *b*"
4220 (org-export-get-next-element
4221 (org-element-map tree 'bold 'identity info t) info)))
4222 ;; Non-exportable elements are ignored.
4223 (should-not
4224 (let ((org-export-with-timestamps nil))
4225 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
4226 (org-export-get-next-element
4227 (org-element-map tree 'entity 'identity info t) info))))
4228 ;; Find next element in secondary strings.
4229 (should
4230 (eq 'verbatim
4231 (org-test-with-parsed-data "* a =verb="
4232 (org-element-type
4233 (org-export-get-next-element
4234 (org-element-map tree 'plain-text 'identity info t) info)))))
4235 (should
4236 (eq 'verbatim
4237 (org-test-with-parsed-data "* /italic/ =verb="
4238 (org-element-type
4239 (org-export-get-next-element
4240 (org-element-map tree 'italic 'identity info t) info)))))
4241 ;; Find next element in document keywords.
4242 (should
4243 (eq 'verbatim
4244 (org-test-with-parsed-data "#+TITLE: a =verb="
4245 (org-element-type
4246 (org-export-get-next-element
4247 (org-element-map
4248 (plist-get info :title) 'plain-text 'identity info t) info)))))
4249 ;; Find next element in parsed affiliated keywords.
4250 (should
4251 (eq 'verbatim
4252 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
4253 (org-element-type
4254 (org-export-get-next-element
4255 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4256 ;; With optional argument N, return a list containing all the
4257 ;; following elements.
4258 (should
4259 (equal
4260 '(bold code underline)
4261 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4262 (mapcar 'car
4263 (org-export-get-next-element
4264 (org-element-map tree 'italic 'identity info t) info t)))))
4265 ;; When N is a positive integer, return a list containing up to
4266 ;; N following elements.
4267 (should
4268 (equal
4269 '(bold code)
4270 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4271 (mapcar 'car
4272 (org-export-get-next-element
4273 (org-element-map tree 'italic 'identity info t) info 2))))))
4275 (ert-deftest test-org-export/get-previous-element ()
4276 "Test `org-export-get-previous-element' specifications."
4277 ;; Standard test.
4278 (should
4279 (equal "a "
4280 (org-test-with-parsed-data "* Headline\na *b*"
4281 (org-export-get-previous-element
4282 (org-element-map tree 'bold 'identity info t) info))))
4283 ;; Return nil when no previous element.
4284 (should-not
4285 (org-test-with-parsed-data "* Headline\n*a* b"
4286 (org-export-get-previous-element
4287 (org-element-map tree 'bold 'identity info t) info)))
4288 ;; Non-exportable elements are ignored.
4289 (should-not
4290 (let ((org-export-with-timestamps nil))
4291 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
4292 (org-export-get-previous-element
4293 (org-element-map tree 'entity 'identity info t) info))))
4294 ;; Find previous element in secondary strings.
4295 (should
4296 (eq 'verbatim
4297 (org-test-with-parsed-data "* =verb= a"
4298 (org-element-type
4299 (org-export-get-previous-element
4300 (org-element-map tree 'plain-text 'identity info t) info)))))
4301 (should
4302 (eq 'verbatim
4303 (org-test-with-parsed-data "* =verb= /italic/"
4304 (org-element-type
4305 (org-export-get-previous-element
4306 (org-element-map tree 'italic 'identity info t) info)))))
4307 ;; Find previous element in document keywords.
4308 (should
4309 (eq 'verbatim
4310 (org-test-with-parsed-data "#+TITLE: =verb= a"
4311 (org-element-type
4312 (org-export-get-previous-element
4313 (org-element-map
4314 (plist-get info :title) 'plain-text 'identity info t) info)))))
4315 ;; Find previous element in parsed affiliated keywords.
4316 (should
4317 (eq 'verbatim
4318 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
4319 (org-element-type
4320 (org-export-get-previous-element
4321 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4322 ;; With optional argument N, return a list containing up to
4323 ;; N previous elements.
4324 (should
4325 (equal '(underline italic bold)
4326 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4327 (mapcar 'car
4328 (org-export-get-previous-element
4329 (org-element-map tree 'code 'identity info t) info t)))))
4330 ;; When N is a positive integer, return a list containing up to
4331 ;; N previous elements.
4332 (should
4333 (equal '(italic bold)
4334 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4335 (mapcar 'car
4336 (org-export-get-previous-element
4337 (org-element-map tree 'code 'identity info t) info 2))))))
4340 (provide 'test-ox)
4341 ;;; test-org-export.el end here