ox: Fix "Fix :filter-options"
[org-mode/org-tableheadings.git] / testing / lisp / test-ox.el
blob15d90450397a2ee55c185963b3d0e930a135e817
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 (should
751 (equal "/simple/ /example/\n"
752 (org-test-with-temp-text "/simple/ /example/"
753 (org-export-as
754 (org-export-create-backend
755 :transcoders '((bold . (lambda (b c i) "dummy"))
756 (paragraph . (lambda (p c i) c))
757 (section . (lambda (s c i) c))))
758 nil nil nil '(:with-emphasize nil)))))
759 ;; LaTeX environment.
760 (should
761 (equal "dummy\n"
762 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
763 (org-export-as
764 (org-export-create-backend
765 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
766 (section . (lambda (s c i) c))))
767 nil nil nil '(:with-latex t)))))
768 (should
769 (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
770 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
771 (org-export-as
772 (org-export-create-backend
773 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
774 (section . (lambda (s c i) c))))
775 nil nil nil '(:with-latex verbatim)))))
776 ;; LaTeX fragment.
777 (should
778 (equal "dummy\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 t)))))
786 (should
787 (equal "$1$\n"
788 (org-test-with-temp-text "$1$"
789 (org-export-as
790 (org-export-create-backend
791 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
792 (paragraph . (lambda (p c i) c))
793 (section . (lambda (s c i) c))))
794 nil nil nil '(:with-latex verbatim)))))
795 ;; Sub/superscript.
796 (should
797 (equal "adummy\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 t)))))
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 nil)))))
814 (should
815 (equal "a_b\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 "adummy\n"
825 (org-test-with-temp-text "a_{b}"
826 (org-export-as
827 (org-export-create-backend
828 :transcoders '((subscript . (lambda (s c i) "dummy"))
829 (paragraph . (lambda (p c i) c))
830 (section . (lambda (s c i) c))))
831 nil nil nil '(:with-sub-superscript {})))))
832 (should
833 (equal "a_entity\n"
834 (org-test-with-temp-text "a_\\alpha"
835 (org-export-as
836 (org-export-create-backend
837 :transcoders '((entity . (lambda (e c i) "entity"))
838 (subscript . (lambda (s c i) "dummy"))
839 (paragraph . (lambda (p c i) c))
840 (section . (lambda (s c i) c))))
841 nil nil nil '(:with-sub-superscript nil)))))
842 ;; Handle uninterpreted objects in parsed keywords.
843 (should
844 (equal "a_b"
845 (org-test-with-temp-text "#+TITLE: a_b"
846 (org-export-as
847 (org-export-create-backend
848 :transcoders
849 '((subscript . (lambda (s c i) "dummy"))
850 (template . (lambda (c i)
851 (org-export-data (plist-get i :title) i)))
852 (section . (lambda (s c i) c))))
853 nil nil nil '(:with-sub-superscript nil)))))
854 (should
855 (equal "a_b"
856 (org-test-with-temp-text "#+FOO: a_b"
857 (org-export-as
858 (org-export-create-backend
859 :options
860 '((:foo "FOO" nil nil parse))
861 :transcoders
862 '((subscript . (lambda (s c i) "dummy"))
863 (template . (lambda (c i)
864 (org-export-data (plist-get i :foo) i)))
865 (section . (lambda (s c i) c))))
866 nil nil nil '(:with-sub-superscript nil)))))
867 ;; Objects in parsed keywords are "uninterpreted" before filters are
868 ;; applied.
869 (should
870 (org-test-with-temp-text "#+TITLE: a_b"
871 (org-export-as
872 (org-export-create-backend
873 :filters
874 '((:filter-options
875 (lambda (i _)
876 (org-element-map (plist-get i :title) 'subscript
877 (lambda (_) (error "There should be no subscript here")))))))
878 nil nil nil '(:with-sub-superscript nil))))
879 ;; Handle uninterpreted objects in captions.
880 (should
881 (equal "adummy\n"
882 (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
883 (org-export-as
884 (org-export-create-backend
885 :transcoders
886 '((subscript . (lambda (s c i) "dummy"))
887 (paragraph . (lambda (p c i)
888 (org-export-data (org-export-get-caption p) i)))
889 (section . (lambda (s c i) c))))
890 nil nil nil '(:with-sub-superscript t)))))
891 (should
892 (equal "a_b\n"
893 (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
894 (org-export-as
895 (org-export-create-backend
896 :transcoders
897 '((subscript . (lambda (s c i) "dummy"))
898 (paragraph . (lambda (p c i)
899 (org-export-data (org-export-get-caption p) i)))
900 (section . (lambda (s c i) c))))
901 nil nil nil '(:with-sub-superscript nil)))))
902 ;; Special case: multiples uninterpreted objects in a row.
903 (should
904 (equal "a_b_c_d\n"
905 (org-test-with-temp-text "a_b_c_d"
906 (org-export-as
907 (org-export-create-backend
908 :transcoders '((subscript . (lambda (s c i) "dummy"))
909 (paragraph . (lambda (p c i) c))
910 (section . (lambda (s c i) c))))
911 nil nil nil '(:with-sub-superscript {}))))))
913 (ert-deftest test-org-export/export-scope ()
914 "Test all export scopes."
915 ;; Subtree.
916 (should
917 (equal "text\n*** H3\n"
918 (org-test-with-temp-text "* H1\n<point>** H2\ntext\n*** H3"
919 (org-export-as (org-test-default-backend) 'subtree))))
920 (should
921 (equal "text\n*** H3\n"
922 (org-test-with-temp-text "* H1\n** H2\n<point>text\n*** H3"
923 (org-export-as (org-test-default-backend) 'subtree))))
924 ;; Subtree with a code block calling another block outside.
925 (should
926 (equal ": 3\n"
927 (org-test-with-temp-text "
928 <point>* Head1
929 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
930 <<test>>
931 #+END_SRC
932 * Head2
933 #+NAME: test
934 #+BEGIN_SRC emacs-lisp
935 \(+ 1 2)
936 #+END_SRC"
937 (let ((org-export-babel-evaluate t))
938 (org-export-as (org-test-default-backend) 'subtree)))))
939 ;; Subtree export should ignore leading planning line and property
940 ;; drawer.
941 (should
942 (equal "Text\n"
943 (org-test-with-temp-text "
944 <point>* H
945 SCHEDULED: <2012-03-29 Thu>
946 :PROPERTIES:
947 :A: 1
948 :END:
949 Text"
950 (org-export-as (org-test-default-backend)
951 'subtree nil nil
952 '(:with-planning t :with-properties t)))))
953 ;; Visible.
954 (should
955 (equal "* H1\n"
956 (org-test-with-temp-text "* H1\n** H2\ntext\n*** H3"
957 (org-cycle)
958 (org-export-as (org-test-default-backend) nil 'visible))))
959 ;; Region.
960 (should
961 (equal "text\n"
962 (org-test-with-temp-text "* H1\n** H2\n<point>text\n*** H3"
963 (transient-mark-mode 1)
964 (push-mark (point) t t)
965 (end-of-line)
966 (org-export-as (org-test-default-backend)))))
967 ;; Body only.
968 (should
969 (equal "Text\n"
970 (org-test-with-temp-text "Text"
971 (org-export-as
972 (org-export-create-backend
973 :transcoders
974 '((template . (lambda (b _i) (format "BEGIN\n%sEND" b)))
975 (section . (lambda (_s c _i) c))
976 (paragraph . (lambda (_p c _i) c))))
977 nil nil 'body-only))))
978 (should
979 (equal "BEGIN\nText\nEND"
980 (org-test-with-temp-text "Text"
981 (org-export-as
982 (org-export-create-backend
983 :transcoders
984 '((template . (lambda (b _i) (format "BEGIN\n%sEND" b)))
985 (section . (lambda (_s c _i) c))
986 (paragraph . (lambda (_p c _i) c))))))))
987 ;; Pathological case: Body only on an empty buffer is expected to
988 ;; return an empty string, not nil.
989 (should
990 (org-test-with-temp-text ""
991 (org-export-as (org-test-default-backend) nil nil t))))
993 (ert-deftest test-org-export/output-file-name ()
994 "Test `org-export-output-file-name' specifications."
995 ;; Export from a file: name is built from original file name.
996 (should
997 (org-test-with-temp-text-in-file "Test"
998 (equal (concat (file-name-as-directory ".")
999 (file-name-nondirectory
1000 (file-name-sans-extension (buffer-file-name))))
1001 (file-name-sans-extension (org-export-output-file-name ".ext")))))
1002 ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
1003 (should
1004 (org-test-with-temp-text-in-file
1005 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
1006 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
1007 ;; From a buffer not associated to a file, too.
1008 (should
1009 (org-test-with-temp-text
1010 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
1011 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
1012 ;; When provided name is absolute, preserve it.
1013 (should
1014 (org-test-with-temp-text
1015 (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
1016 (expand-file-name "test"))
1017 (file-name-absolute-p (org-export-output-file-name ".ext" t))))
1018 ;; When PUB-DIR argument is provided, use it.
1019 (should
1020 (org-test-with-temp-text-in-file "Test"
1021 (equal (file-name-directory
1022 (org-export-output-file-name ".ext" nil "dir/"))
1023 "dir/")))
1024 ;; When returned name would overwrite original file, add EXTENSION
1025 ;; another time.
1026 (should
1027 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
1028 (equal (org-export-output-file-name ".org") "./normal.org.org"))))
1030 (ert-deftest test-org-export/expand-include ()
1031 "Test file inclusion in an Org buffer."
1032 ;; Error when file isn't specified.
1033 (should-error
1034 (org-test-with-temp-text "#+INCLUDE: dummy.org"
1035 (org-export-expand-include-keyword)))
1036 ;; Full insertion with recursive inclusion.
1037 (should
1038 (equal
1039 (with-temp-buffer
1040 (insert-file
1041 (expand-file-name "examples/include.org" org-test-dir))
1042 (replace-regexp-in-string
1043 (regexp-quote "#+INCLUDE: \"include2.org\"")
1044 "Success!" (buffer-string)))
1045 (org-test-with-temp-text
1046 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
1047 (org-export-expand-include-keyword)
1048 (buffer-string))))
1049 ;; Localized insertion.
1050 (org-test-with-temp-text
1051 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
1052 org-test-dir)
1053 (org-export-expand-include-keyword)
1054 (should (equal (buffer-string)
1055 "Small Org file with an include keyword.\n")))
1056 ;; Insertion with constraints on headlines level.
1057 (should
1058 (equal
1059 "* Top heading\n** Heading\nbody\n"
1060 (org-test-with-temp-text
1061 (format
1062 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\""
1063 org-test-dir)
1064 (org-export-expand-include-keyword)
1065 (buffer-string))))
1066 (should
1067 (equal
1068 "* Top heading\n* Heading\nbody\n"
1069 (org-test-with-temp-text
1070 (format
1071 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\" :minlevel 1"
1072 org-test-dir)
1073 (org-export-expand-include-keyword)
1074 (buffer-string))))
1075 ;; Inclusion within an example block.
1076 (should
1077 (equal
1078 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
1079 (org-test-with-temp-text
1080 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
1081 org-test-dir)
1082 (org-export-expand-include-keyword)
1083 (buffer-string))))
1084 ;; Inclusion within a src-block.
1085 (should
1086 (equal
1087 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
1088 (org-test-with-temp-text
1089 (format
1090 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
1091 org-test-dir)
1092 (org-export-expand-include-keyword)
1093 (buffer-string))))
1094 ;; Inclusion within an html export-block.
1095 (should
1096 (equal
1097 "#+BEGIN_EXPORT html\n<p>HTML!</p>\n#+END_EXPORT\n"
1098 (org-test-with-temp-text
1099 (format
1100 "#+INCLUDE: \"%s/examples/include.html\" EXPORT html"
1101 org-test-dir)
1102 (org-export-expand-include-keyword)
1103 (buffer-string))))
1104 ;; Inclusion within an center paragraph
1105 (should
1106 (equal
1107 "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
1108 (org-test-with-temp-text
1109 (format
1110 "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
1111 org-test-dir)
1112 (org-export-expand-include-keyword)
1113 (buffer-string))))
1114 ;; Footnotes labels are local to each included file.
1115 (should
1116 (= 6
1117 (length
1118 (delete-dups
1119 (let ((contents "
1120 Footnotes[fn:1], [fn:test], [fn:test] and [fn:inline:inline footnote].
1121 \[fn:1] Footnote 1
1122 \[fn:test] Footnote \"test\""))
1123 (org-test-with-temp-text-in-file contents
1124 (let ((file1 (buffer-file-name)))
1125 (org-test-with-temp-text-in-file contents
1126 (let ((file2 (buffer-file-name)))
1127 (org-test-with-temp-text
1128 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
1129 file1 file2)
1130 (org-export-expand-include-keyword)
1131 (org-element-map (org-element-parse-buffer)
1132 'footnote-reference
1133 (lambda (r) (org-element-property :label r)))))))))))))
1134 ;; Footnotes labels are not local to each include keyword.
1135 (should
1136 (= 3
1137 (length
1138 (delete-dups
1139 (let ((contents "
1140 Footnotes[fn:1], [fn:test] and [fn:inline:inline footnote].
1141 \[fn:1] Footnote 1
1142 \[fn:test] Footnote \"test\""))
1143 (org-test-with-temp-text-in-file contents
1144 (let ((file (buffer-file-name)))
1145 (org-test-with-temp-text
1146 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
1147 (org-export-expand-include-keyword)
1148 (org-element-map (org-element-parse-buffer)
1149 'footnote-reference
1150 (lambda (ref) (org-element-property :label ref)))))))))))
1151 ;; Footnotes are supported by :lines-like elements and unnecessary
1152 ;; footnotes are dropped.
1153 (should
1154 (= 3
1155 (length
1156 (delete-dups
1157 (let ((contents "
1158 * foo
1159 Footnotes[fn:1]
1160 * bar
1161 Footnotes[fn:2], foot[fn:test] and [fn:inline:inline footnote]
1163 \[fn:1] Footnote 1
1164 \[fn:2] Footnote 1
1165 * Footnotes
1166 \[fn:test] Footnote \"test\"
1168 (org-test-with-temp-text-in-file contents
1169 (let ((file (buffer-file-name)))
1170 (org-test-with-temp-text
1171 (format "#+INCLUDE: \"%s::*bar\"\n" file)
1172 (org-export-expand-include-keyword)
1173 (org-element-map (org-element-parse-buffer)
1174 'footnote-definition
1175 (lambda (ref) (org-element-property :label ref)))))))))))
1176 ;; If only-contents is non-nil only include contents of element.
1177 (should
1178 (equal
1179 "body\n"
1180 (org-test-with-temp-text
1181 (concat
1182 (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" "
1183 org-test-dir)
1184 ":only-contents t")
1185 (org-export-expand-include-keyword)
1186 (buffer-string))))
1187 ;; Headings can be included via CUSTOM_ID.
1188 (should
1189 (org-test-with-temp-text
1190 (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
1191 (org-export-expand-include-keyword)
1192 (goto-char (point-min))
1193 (looking-at "* Another heading")))
1194 ;; Named objects can be included.
1195 (should
1196 (equal
1197 "| 1 |\n"
1198 (org-test-with-temp-text
1199 (format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t"
1200 org-test-dir)
1201 (org-export-expand-include-keyword)
1202 (buffer-string))))
1203 ;; Including non-existing elements should result in an error.
1204 (should-error
1205 (org-test-with-temp-text
1206 (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\""
1207 org-test-dir)
1208 (org-export-expand-include-keyword)))
1209 ;; Lines work relatively to an included element.
1210 (should
1211 (equal
1212 "2\n3\n"
1213 (org-test-with-temp-text
1214 (format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t \
1215 :lines \"2-3\""
1216 org-test-dir)
1217 (org-export-expand-include-keyword)
1218 (buffer-string))))
1219 ;; Properties should be dropped from headlines.
1220 (should
1221 (equal
1222 (org-test-with-temp-text
1223 (format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t"
1224 org-test-dir)
1225 (org-export-expand-include-keyword)
1226 (buffer-string))
1227 (org-test-with-temp-text
1228 (format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
1229 (org-export-expand-include-keyword)
1230 (buffer-string))))
1231 ;; Properties should be dropped, drawers should not be.
1232 (should
1233 (equal
1234 ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
1235 (org-test-with-temp-text
1236 (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t"
1237 org-test-dir)
1238 (org-export-expand-include-keyword)
1239 (buffer-string))))
1240 ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
1241 (should
1242 (cl-every (lambda (level) (zerop (1- level)))
1243 (org-test-with-temp-text
1244 (concat
1245 (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n"
1246 org-test-dir)
1247 (format "#+INCLUDE: \"%s/examples/include.org::*Heading\""
1248 org-test-dir))
1249 (org-export-expand-include-keyword)
1250 (org-element-map (org-element-parse-buffer) 'headline
1251 (lambda (head) (org-element-property :level head))))))
1252 ;; INCLUDE does not insert induced :minlevel for src-blocks.
1253 (should-not
1254 (equal
1255 (org-test-with-temp-text
1256 (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp"
1257 org-test-dir)
1258 (org-export-expand-include-keyword)
1259 (buffer-string))
1260 (org-test-with-temp-text
1261 (format
1262 "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1"
1263 org-test-dir)
1264 (org-export-expand-include-keyword)
1265 (buffer-string))))
1266 ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
1267 (should
1268 (org-test-with-temp-text-in-file
1269 (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\""
1270 org-test-dir)
1271 (narrow-to-region (point) (point-max))
1272 (org-export-expand-include-keyword)
1273 (eq 1 (org-current-level))))
1274 ;; If :minlevel is present do not alter it.
1275 (should
1276 (org-test-with-temp-text
1277 (format
1278 "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 3"
1279 org-test-dir)
1280 (narrow-to-region (point) (point-max))
1281 (org-export-expand-include-keyword)
1282 (eq 3 (org-current-level)))))
1284 (ert-deftest test-org-export/expand-macro ()
1285 "Test macro expansion in an Org buffer."
1286 (require 'ox-org)
1287 ;; Standard macro expansion.
1288 (should
1289 (equal "#+MACRO: macro1 value\nvalue\n"
1290 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
1291 (org-export-as (org-test-default-backend)))))
1292 ;; Allow macro in parsed keywords and associated properties.
1293 ;; Standard macro expansion.
1294 (should
1295 (string-match
1296 "#\\+K: value"
1297 (let ((backend (org-export-create-backend
1298 :parent 'org
1299 :options '((:k "K" nil nil parse)))))
1300 (org-test-with-temp-text "#+MACRO: macro value\n#+K: {{{macro}}}"
1301 (org-export-as backend)))))
1302 (should
1303 (string-match
1304 ":EXPORT_K: v"
1305 (let ((backend (org-export-create-backend
1306 :parent 'org
1307 :options '((:k "K" nil nil parse)))))
1308 (org-test-with-temp-text
1309 "#+MACRO: m v\n* H\n:PROPERTIES:\n:EXPORT_K: {{{m}}}\n:END:"
1310 (org-export-as backend nil nil nil '(:with-properties t))))))
1311 ;; Expand specific macros.
1312 (should
1313 (equal "me 2012-03-29 me@here Title\n"
1314 (org-test-with-temp-text
1316 #+TITLE: Title
1317 #+DATE: 2012-03-29
1318 #+AUTHOR: me
1319 #+EMAIL: me@here
1320 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
1321 (let ((output (org-export-as (org-test-default-backend))))
1322 (substring output (string-match ".*\n\\'" output))))))
1323 ;; Expand specific macros when property contained a regular macro
1324 ;; already.
1325 (should
1326 (equal "value\n"
1327 (org-test-with-temp-text "
1328 #+MACRO: macro1 value
1329 #+TITLE: {{{macro1}}}
1330 {{{title}}}"
1331 (let ((output (org-export-as (org-test-default-backend))))
1332 (substring output (string-match ".*\n\\'" output))))))
1333 ;; Expand macros with templates in included files.
1334 (should
1335 (equal "success\n"
1336 (org-test-with-temp-text
1337 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
1338 {{{included-macro}}}" org-test-dir)
1339 (let ((output (org-export-as (org-test-default-backend))))
1340 (substring output (string-match ".*\n\\'" output))))))
1341 ;; Date macro takes a optional formatting argument
1342 (should
1343 (equal "09-02-15\n"
1344 (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02-09>"
1345 (org-export-as (org-test-default-backend)))))
1346 ;; Only single timestamps are formatted
1347 (should
1348 (equal "<2015-02x-09>\n"
1349 (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02x-09>"
1350 (org-export-as (org-test-default-backend)))))
1351 ;; Throw an error when a macro definition is missing.
1352 (should-error
1353 (org-test-with-temp-text "{{{missing}}}"
1354 (org-export-as (org-test-default-backend))))
1355 ;; Macros defined in commented subtrees are ignored.
1356 (should-error
1357 (org-test-with-temp-text
1358 "* COMMENT H\n#+MACRO: macro1\n* H2\nvalue\n{{{macro1}}}"
1359 (org-export-as (org-test-default-backend))))
1360 (should-error
1361 (org-test-with-temp-text
1362 "* COMMENT H\n** H2\n#+MACRO: macro1\n* H3\nvalue\n{{{macro1}}}"
1363 (org-export-as (org-test-default-backend)))))
1365 (ert-deftest test-org-export/before-processing-hook ()
1366 "Test `org-export-before-processing-hook'."
1367 (should
1368 (equal
1369 "#+MACRO: mac val\nTest\n"
1370 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
1371 (let ((org-export-before-processing-hook
1372 '((lambda (backend)
1373 (while (re-search-forward "{{{" nil t)
1374 (let ((object (org-element-context)))
1375 (when (eq (org-element-type object) 'macro)
1376 (delete-region
1377 (org-element-property :begin object)
1378 (org-element-property :end object)))))))))
1379 (org-export-as (org-test-default-backend)))))))
1381 (ert-deftest test-org-export/before-parsing-hook ()
1382 "Test `org-export-before-parsing-hook'."
1383 (should
1384 (equal "Body 1\nBody 2\n"
1385 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
1386 (let ((org-export-before-parsing-hook
1387 '((lambda (backend)
1388 (goto-char (point-min))
1389 (while (re-search-forward org-outline-regexp-bol nil t)
1390 (delete-region
1391 (point-at-bol) (progn (forward-line) (point))))))))
1392 (org-export-as (org-test-default-backend)))))))
1396 ;;; Affiliated Keywords
1398 (ert-deftest test-org-export/read-attribute ()
1399 "Test `org-export-read-attribute' specifications."
1400 ;; Standard test.
1401 (should
1402 (equal
1403 (org-export-read-attribute
1404 :attr_html
1405 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
1406 (org-element-at-point)))
1407 '(:a "1" :b "2")))
1408 ;; Return nil on empty attribute.
1409 (should-not
1410 (org-export-read-attribute
1411 :attr_html
1412 (org-test-with-temp-text "Paragraph" (org-element-at-point))))
1413 ;; Return nil on "nil" string.
1414 (should
1415 (equal '(:a nil :b nil)
1416 (org-export-read-attribute
1417 :attr_html
1418 (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
1419 (org-element-at-point)))))
1420 ;; Return nil on empty string.
1421 (should
1422 (equal '(:a nil :b nil)
1423 (org-export-read-attribute
1424 :attr_html
1425 (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
1426 (org-element-at-point)))))
1427 ;; Return empty string when value is "".
1428 (should
1429 (equal '(:a "")
1430 (org-export-read-attribute
1431 :attr_html
1432 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
1433 (org-element-at-point)))))
1434 ;; Return \"\" when value is """".
1435 (should
1436 (equal '(:a "\"\"")
1437 (org-export-read-attribute
1438 :attr_html
1439 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
1440 (org-element-at-point)))))
1441 ;; Ignore text before first property.
1442 (should-not
1443 (member "ignore"
1444 (org-export-read-attribute
1445 :attr_html
1446 (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
1447 (org-element-at-point))))))
1449 (ert-deftest test-org-export/get-caption ()
1450 "Test `org-export-get-caption' specifications."
1451 ;; Without optional argument, return long caption
1452 (should
1453 (equal
1454 '("l")
1455 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1456 (org-export-get-caption (org-element-at-point)))))
1457 ;; With optional argument, return short caption.
1458 (should
1459 (equal
1460 '("s")
1461 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1462 (org-export-get-caption (org-element-at-point) t))))
1463 ;; Multiple lines are separated by white spaces.
1464 (should
1465 (equal
1466 '("a" " " "b")
1467 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
1468 (org-export-get-caption (org-element-at-point))))))
1472 ;;; Back-End Tools
1474 (ert-deftest test-org-export/define-backend ()
1475 "Test back-end definition and accessors."
1476 ;; Translate table.
1477 (should
1478 (equal '((headline . my-headline-test))
1479 (let (org-export-registered-backends)
1480 (org-export-define-backend 'test '((headline . my-headline-test)))
1481 (org-export-get-all-transcoders 'test))))
1482 ;; Filters.
1483 (should
1484 (equal '((:filter-headline . my-filter))
1485 (let (org-export-registered-backends)
1486 (org-export-define-backend 'test
1487 '((headline . my-headline-test))
1488 :filters-alist '((:filter-headline . my-filter)))
1489 (org-export-backend-filters (org-export-get-backend 'test)))))
1490 ;; Options.
1491 (should
1492 (equal '((:prop value))
1493 (let (org-export-registered-backends)
1494 (org-export-define-backend 'test
1495 '((headline . my-headline-test))
1496 :options-alist '((:prop value)))
1497 (org-export-backend-options (org-export-get-backend 'test)))))
1498 ;; Menu.
1499 (should
1500 (equal '(?k "Test Export" test)
1501 (let (org-export-registered-backends)
1502 (org-export-define-backend 'test
1503 '((headline . my-headline-test))
1504 :menu-entry '(?k "Test Export" test))
1505 (org-export-backend-menu (org-export-get-backend 'test))))))
1507 (ert-deftest test-org-export/define-derived-backend ()
1508 "Test `org-export-define-derived-backend' specifications."
1509 ;; Error when parent back-end is not defined.
1510 (should-error
1511 (let (org-export-registered-backends)
1512 (org-export-define-derived-backend 'test 'parent)))
1513 ;; Append translation table to parent's.
1514 (should
1515 (equal '((:headline . test) (:headline . parent))
1516 (let (org-export-registered-backends)
1517 (org-export-define-backend 'parent '((:headline . parent)))
1518 (org-export-define-derived-backend 'test 'parent
1519 :translate-alist '((:headline . test)))
1520 (org-export-get-all-transcoders 'test))))
1521 ;; Options defined in the new back have priority over those defined
1522 ;; in parent.
1523 (should
1524 (eq 'test
1525 (let (org-export-registered-backends)
1526 (org-export-define-backend 'parent
1527 '((:headline . parent))
1528 :options-alist '((:a nil nil 'parent)))
1529 (org-export-define-derived-backend 'test 'parent
1530 :options-alist '((:a nil nil 'test)))
1531 (plist-get (org-export--get-global-options
1532 (org-export-get-backend 'test))
1533 :a)))))
1535 (ert-deftest test-org-export/derived-backend-p ()
1536 "Test `org-export-derived-backend-p' specifications."
1537 ;; Non-nil with direct match.
1538 (should
1539 (let (org-export-registered-backends)
1540 (org-export-define-backend 'test '((headline . test)))
1541 (org-export-derived-backend-p 'test 'test)))
1542 (should
1543 (let (org-export-registered-backends)
1544 (org-export-define-backend 'test '((headline . test)))
1545 (org-export-define-derived-backend 'test2 'test)
1546 (org-export-derived-backend-p 'test2 'test2)))
1547 ;; Non-nil with a direct parent.
1548 (should
1549 (let (org-export-registered-backends)
1550 (org-export-define-backend 'test '((headline . test)))
1551 (org-export-define-derived-backend 'test2 'test)
1552 (org-export-derived-backend-p 'test2 'test)))
1553 ;; Non-nil with an indirect parent.
1554 (should
1555 (let (org-export-registered-backends)
1556 (org-export-define-backend 'test '((headline . test)))
1557 (org-export-define-derived-backend 'test2 'test)
1558 (org-export-define-derived-backend 'test3 'test2)
1559 (org-export-derived-backend-p 'test3 'test)))
1560 ;; Nil otherwise.
1561 (should-not
1562 (let (org-export-registered-backends)
1563 (org-export-define-backend 'test '((headline . test)))
1564 (org-export-define-backend 'test2 '((headline . test2)))
1565 (org-export-derived-backend-p 'test2 'test)))
1566 (should-not
1567 (let (org-export-registered-backends)
1568 (org-export-define-backend 'test '((headline . test)))
1569 (org-export-define-backend 'test2 '((headline . test2)))
1570 (org-export-define-derived-backend 'test3 'test2)
1571 (org-export-derived-backend-p 'test3 'test))))
1573 (ert-deftest test-org-export/get-all-transcoders ()
1574 "Test `org-export-get-all-transcoders' specifications."
1575 ;; Return nil when back-end cannot be found.
1576 (should-not (org-export-get-all-transcoders nil))
1577 ;; Same as `org-export-transcoders' if no parent.
1578 (should
1579 (equal '((headline . ignore))
1580 (org-export-get-all-transcoders
1581 (org-export-create-backend
1582 :transcoders '((headline . ignore))))))
1583 ;; But inherit from all ancestors whenever possible.
1584 (should
1585 (equal '((section . ignore) (headline . ignore))
1586 (let (org-export-registered-backends)
1587 (org-export-define-backend 'b1 '((headline . ignore)))
1588 (org-export-get-all-transcoders
1589 (org-export-create-backend
1590 :parent 'b1 :transcoders '((section . ignore)))))))
1591 (should
1592 (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
1593 (let (org-export-registered-backends)
1594 (org-export-define-backend 'b1 '((headline . ignore)))
1595 (org-export-define-derived-backend 'b2 'b1
1596 :translate-alist '((section . ignore)))
1597 (org-export-get-all-transcoders
1598 (org-export-create-backend
1599 :parent 'b2 :transcoders '((paragraph . ignore)))))))
1600 ;; Back-end transcoders overrule inherited ones.
1601 (should
1602 (eq 'b
1603 (let (org-export-registered-backends)
1604 (org-export-define-backend 'b1 '((headline . a)))
1605 (cdr (assq 'headline
1606 (org-export-get-all-transcoders
1607 (org-export-create-backend
1608 :parent 'b1 :transcoders '((headline . b))))))))))
1610 (ert-deftest test-org-export/get-all-options ()
1611 "Test `org-export-get-all-options' specifications."
1612 ;; Return nil when back-end cannot be found.
1613 (should-not (org-export-get-all-options nil))
1614 ;; Same as `org-export-options' if no parent.
1615 (should
1616 (equal '((headline . ignore))
1617 (org-export-get-all-options
1618 (org-export-create-backend
1619 :options '((headline . ignore))))))
1620 ;; But inherit from all ancestors whenever possible.
1621 (should
1622 (equal '((:key2 value2) (:key1 value1))
1623 (let (org-export-registered-backends)
1624 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1625 (org-export-get-all-options
1626 (org-export-create-backend
1627 :parent 'b1 :options '((:key2 value2)))))))
1628 (should
1629 (equal '((:key3 value3) (:key2 value2) (:key1 value1))
1630 (let (org-export-registered-backends)
1631 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1632 (org-export-define-derived-backend 'b2 'b1
1633 :options-alist '((:key2 value2)))
1634 (org-export-get-all-options
1635 (org-export-create-backend
1636 :parent 'b2 :options '((:key3 value3)))))))
1637 ;; Back-end options overrule inherited ones.
1638 (should
1639 (eq 'b
1640 (let (org-export-registered-backends)
1641 (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
1642 (cdr (assq :key1
1643 (org-export-get-all-options
1644 (org-export-create-backend
1645 :parent 'b1 :options '((:key1 . b))))))))))
1647 (ert-deftest test-org-export/get-all-filters ()
1648 "Test `org-export-get-all-filters' specifications."
1649 ;; Return nil when back-end cannot be found.
1650 (should-not (org-export-get-all-filters nil))
1651 ;; Same as `org-export-filters' if no parent.
1652 (should
1653 (equal '((:filter-headline . ignore))
1654 (org-export-get-all-filters
1655 (org-export-create-backend
1656 :filters '((:filter-headline . ignore))))))
1657 ;; But inherit from all ancestors whenever possible.
1658 (should
1659 (equal '((:filter-section . ignore) (:filter-headline . ignore))
1660 (let (org-export-registered-backends)
1661 (org-export-define-backend 'b1
1662 nil :filters-alist '((:filter-headline . ignore)))
1663 (org-export-get-all-filters
1664 (org-export-create-backend
1665 :parent 'b1 :filters '((:filter-section . ignore)))))))
1666 (should
1667 (equal '((:filter-paragraph . ignore)
1668 (:filter-section . ignore)
1669 (:filter-headline . ignore))
1670 (let (org-export-registered-backends)
1671 (org-export-define-backend 'b1
1672 nil :filters-alist '((:filter-headline . ignore)))
1673 (org-export-define-derived-backend 'b2 'b1
1674 :filters-alist '((:filter-section . ignore)))
1675 (org-export-get-all-filters
1676 (org-export-create-backend
1677 :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
1678 ;; Back-end filters overrule inherited ones.
1679 (should
1680 (eq 'b
1681 (let (org-export-registered-backends)
1682 (org-export-define-backend 'b1 '((:filter-headline . a)))
1683 (cdr (assq :filter-headline
1684 (org-export-get-all-filters
1685 (org-export-create-backend
1686 :parent 'b1 :filters '((:filter-headline . b))))))))))
1688 (ert-deftest test-org-export/with-backend ()
1689 "Test `org-export-with-backend' definition."
1690 ;; Error when calling an undefined back-end
1691 (should-error (org-export-with-backend nil "Test"))
1692 ;; Error when called back-end doesn't have an appropriate
1693 ;; transcoder.
1694 (should-error
1695 (org-export-with-backend
1696 (org-export-create-backend :transcoders '((headline . ignore)))
1697 "Test"))
1698 ;; Otherwise, export using correct transcoder
1699 (should
1700 (equal "Success"
1701 (let (org-export-registered-backends)
1702 (org-export-define-backend 'test
1703 '((plain-text . (lambda (text contents info) "Failure"))))
1704 (org-export-define-backend 'test2
1705 '((plain-text . (lambda (text contents info) "Success"))))
1706 (org-export-with-backend 'test2 "Test"))))
1707 ;; Provide correct back-end if transcoder needs to use recursive
1708 ;; calls anyway.
1709 (should
1710 (equal "Success\n"
1711 (let ((test-back-end
1712 (org-export-create-backend
1713 :transcoders
1714 (list (cons 'headline
1715 (lambda (headline contents info)
1716 (org-export-data
1717 (org-element-property :title headline)
1718 info)))
1719 (cons 'plain-text (lambda (text info) "Success"))))))
1720 (org-export-string-as
1721 "* Test"
1722 (org-export-create-backend
1723 :transcoders
1724 (list (cons 'headline
1725 (lambda (headline contents info)
1726 (org-export-with-backend
1727 test-back-end headline contents info))))))))))
1729 (ert-deftest test-org-export/data-with-backend ()
1730 "Test `org-export-data-with-backend' specifications."
1731 ;; Error when calling an undefined back-end.
1732 (should-error (org-export-data-with-backend nil "nil" nil))
1733 ;; Otherwise, export data recursively, using correct back-end.
1734 (should
1735 (equal
1736 "Success!"
1737 (org-export-data-with-backend
1738 '(bold nil "Test")
1739 (org-export-create-backend
1740 :transcoders
1741 '((plain-text . (lambda (text info) "Success"))
1742 (bold . (lambda (bold contents info) (concat contents "!")))))
1743 '(:with-emphasize t)))))
1747 ;;; Comments
1749 (ert-deftest test-org-export/comments ()
1750 "Test comments handling during export.
1751 In particular, structure of the document mustn't be altered after
1752 comments removal."
1753 (should
1754 (equal "Para1\n\nPara2\n"
1755 (org-test-with-temp-text "
1756 Para1
1757 # Comment
1759 # Comment
1760 Para2"
1761 (org-export-as (org-test-default-backend)))))
1762 (should
1763 (equal "Para1\n\nPara2\n"
1764 (org-test-with-temp-text "
1765 Para1
1766 # Comment
1767 Para2"
1768 (org-export-as (org-test-default-backend)))))
1769 (should
1770 (equal "[fn:1] Para1\n\n\nPara2\n"
1771 (org-test-with-temp-text "
1772 \[fn:1] Para1
1773 # Inside definition
1776 # Outside definition
1777 Para2"
1778 (org-export-as (org-test-default-backend)))))
1779 (should
1780 (equal "[fn:1] Para1\n\nPara2\n"
1781 (org-test-with-temp-text "
1782 \[fn:1] Para1
1784 # Inside definition
1786 # Inside definition
1788 Para2"
1789 (org-export-as (org-test-default-backend)))))
1790 (should
1791 (equal "[fn:1] Para1\n\nPara2\n"
1792 (org-test-with-temp-text "
1793 \[fn:1] Para1
1794 # Inside definition
1796 Para2"
1797 (org-export-as (org-test-default-backend)))))
1798 (should
1799 (equal "[fn:1] Para1\n\nPara2\n"
1800 (org-test-with-temp-text "
1801 \[fn:1] Para1
1803 # Inside definition
1804 Para2"
1805 (org-export-as (org-test-default-backend)))))
1806 (should
1807 (equal "- item 1\n\n- item 2\n"
1808 (org-test-with-temp-text "
1809 - item 1
1811 # Comment
1813 - item 2"
1814 (org-export-as (org-test-default-backend))))))
1818 ;;; Export blocks
1820 (ert-deftest test-org-export/export-block ()
1821 "Test export blocks transcoding."
1822 (should
1823 (equal "Success!\n"
1824 (org-test-with-temp-text
1825 "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
1826 (org-export-as
1827 (org-export-create-backend
1828 :transcoders '((export-block . (lambda (b _c _i)
1829 (org-element-property :value b)))
1830 (section . (lambda (_s c _i) c))))))))
1831 (should
1832 (equal "Success!\n"
1833 (org-test-with-temp-text
1834 "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
1835 (org-export-as
1836 (org-export-create-backend
1837 :transcoders
1838 (list
1839 (cons 'export-block
1840 (lambda (b _c _i)
1841 (and (equal (org-element-property :type b) "BACKEND")
1842 (org-element-property :value b))))
1843 (cons 'section (lambda (_s c _i) c)))))))))
1846 ;;; Export Snippets
1848 (ert-deftest test-org-export/export-snippet ()
1849 "Test export snippets transcoding."
1850 ;; Standard test.
1851 (org-test-with-temp-text "@@test:A@@@@t:B@@"
1852 (let ((backend (org-test-default-backend)))
1853 (setf (org-export-backend-name backend) 'test)
1854 (setf (org-export-backend-transcoders backend)
1855 (cons (cons 'export-snippet
1856 (lambda (snippet contents info)
1857 (when (eq (org-export-snippet-backend snippet) 'test)
1858 (org-element-property :value snippet))))
1859 (org-export-backend-transcoders backend)))
1860 (let ((org-export-snippet-translation-alist nil))
1861 (should (equal (org-export-as backend) "A\n")))
1862 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
1863 (should (equal (org-export-as backend) "AB\n")))))
1864 ;; Ignored export snippets do not remove any blank.
1865 (should
1866 (equal "begin end\n"
1867 (org-test-with-parsed-data "begin @@test:A@@ end"
1868 (org-export-data-with-backend
1869 tree
1870 (org-export-create-backend
1871 :transcoders
1872 '((paragraph . (lambda (paragraph contents info) contents))
1873 (section . (lambda (section contents info) contents))))
1874 info)))))
1877 ;;; Filters
1879 (ert-deftest test-org-export/filter-apply-functions ()
1880 "Test `org-export-filter-apply-functions' specifications."
1881 ;; Functions are applied in order and return values are reduced.
1882 (should
1883 (equal "210"
1884 (org-export-filter-apply-functions
1885 (list (lambda (value &rest _) (concat "1" value))
1886 (lambda (value &rest _) (concat "2" value)))
1887 "0" nil)))
1888 ;; Functions returning nil are skipped.
1889 (should
1890 (equal "20"
1891 (org-export-filter-apply-functions
1892 (list #'ignore (lambda (value &rest _) (concat "2" value)))
1893 "0" nil)))
1894 ;; If all functions are skipped, return the initial value.
1895 (should
1896 (equal "0"
1897 (org-export-filter-apply-functions (list #'ignore) "0" nil)))
1898 ;; If any function returns the empty string, final value is the
1899 ;; empty string.
1900 (should
1901 (equal ""
1902 (org-export-filter-apply-functions
1903 (list (lambda (value &rest _) "")
1904 (lambda (value &rest _) (concat "2" value)))
1905 "0" nil)))
1906 ;; Any function returning the empty string short-circuits the
1907 ;; process.
1908 (should
1909 (org-export-filter-apply-functions
1910 (list (lambda (value &rest _) "")
1911 (lambda (value &rest _) (error "This shouldn't happen")))
1912 "0" nil)))
1915 ;;; Footnotes
1917 (ert-deftest test-org-export/footnote-first-reference-p ()
1918 "Test `org-export-footnote-first-reference-p' specifications."
1919 (should
1920 (equal
1921 '(t nil)
1922 (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
1923 (let (result)
1924 (org-export-as
1925 (org-export-create-backend
1926 :transcoders
1927 `(,(cons 'footnote-reference
1928 (lambda (f c i)
1929 (push (org-export-footnote-first-reference-p f i)
1930 result)
1931 ""))
1932 (section . (lambda (s c i) c))
1933 (paragraph . (lambda (p c i) c))))
1934 nil nil nil '(:with-footnotes t))
1935 (nreverse result)))))
1936 ;; Limit check to DATA, when non-nil.
1937 (should
1938 (equal
1939 '(nil t)
1940 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
1941 (let (result)
1942 (org-element-map tree 'footnote-reference
1943 (lambda (ref)
1944 (push
1945 (org-export-footnote-first-reference-p
1946 ref info (org-element-map tree 'headline #'identity info t))
1947 result))
1948 info)
1949 (nreverse result)))))
1950 (should
1951 (equal
1952 '(t nil)
1953 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
1954 (let (result)
1955 (org-element-map tree 'footnote-reference
1956 (lambda (ref)
1957 (push (org-export-footnote-first-reference-p ref info) result))
1958 info)
1959 (nreverse result)))))
1960 ;; If optional argument BODY-FIRST is non-nil, first find footnote
1961 ;; in the main body of the document. Otherwise, enter footnote
1962 ;; definitions when they are encountered.
1963 (should
1964 (equal
1965 '(t nil)
1966 (org-test-with-temp-text
1967 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
1968 (let (result)
1969 (org-export-as
1970 (org-export-create-backend
1971 :transcoders
1972 `(,(cons 'footnote-reference
1973 (lambda (f c i)
1974 (when (org-element-lineage f '(drawer))
1975 (push (org-export-footnote-first-reference-p f i nil)
1976 result))
1977 ""))
1978 (drawer . (lambda (d c i) c))
1979 (footnote-definition . (lambda (d c i) c))
1980 (section . (lambda (s c i) c))
1981 (paragraph . (lambda (p c i) c))))
1982 nil nil nil '(:with-footnotes t))
1983 (nreverse result)))))
1984 (should
1985 (equal
1986 '(t t)
1987 (org-test-with-temp-text
1988 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
1989 (let (result)
1990 (org-export-as
1991 (org-export-create-backend
1992 :transcoders
1993 `(,(cons 'footnote-reference
1994 (lambda (f c i)
1995 (when (org-element-lineage f '(drawer))
1996 (push (org-export-footnote-first-reference-p f i nil t)
1997 result))
1998 ""))
1999 (drawer . (lambda (d c i) c))
2000 (footnote-definition . (lambda (d c i) c))
2001 (section . (lambda (s c i) c))
2002 (paragraph . (lambda (p c i) c))))
2003 nil nil nil '(:with-footnotes t))
2004 (nreverse result))))))
2006 (ert-deftest test-org-export/get-footnote-number ()
2007 "Test `org-export-get-footnote-number' specifications."
2008 (should
2009 (equal '(1 2 1)
2010 (org-test-with-parsed-data
2011 "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
2012 (org-element-map tree 'footnote-reference
2013 (lambda (ref) (org-export-get-footnote-number ref info))
2014 info))))
2015 ;; Anonymous footnotes all get a new number.
2016 (should
2017 (equal '(1 2)
2018 (org-test-with-parsed-data
2019 "Text[fn::anon1][fn::anon2]"
2020 (org-element-map tree 'footnote-reference
2021 (lambda (ref) (org-export-get-footnote-number ref info))
2022 info))))
2023 ;; Test nested footnotes order.
2024 (should
2025 (equal
2026 '((1 . "1") (2 . "2") (3 . "3") (3 . "3") (4))
2027 (org-test-with-parsed-data
2028 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
2029 (org-element-map tree 'footnote-reference
2030 (lambda (ref)
2031 (cons (org-export-get-footnote-number ref info)
2032 (org-element-property :label ref)))
2033 info))))
2034 ;; Limit number to provided DATA, when non-nil.
2035 (should
2036 (equal
2037 '(1)
2038 (org-test-with-parsed-data
2039 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2040 (org-element-map tree 'footnote-reference
2041 (lambda (ref)
2042 (org-export-get-footnote-number
2043 ref info (org-element-map tree 'headline #'identity info t)))
2044 info))))
2045 (should
2046 (equal
2047 '(1 2)
2048 (org-test-with-parsed-data
2049 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
2050 (org-element-map tree 'footnote-reference
2051 (lambda (ref) (org-export-get-footnote-number ref info))
2052 info))))
2053 ;; With a non-nil BODY-FIRST optional argument, first check body,
2054 ;; then footnote definitions.
2055 (should
2056 (equal
2057 '(("1" . 1) ("2" . 2) ("3" . 3) ("3" . 3))
2058 (org-test-with-parsed-data
2059 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
2060 (org-element-map tree 'footnote-reference
2061 (lambda (ref)
2062 (cons (org-element-property :label ref)
2063 (org-export-get-footnote-number ref info nil t)))
2064 info))))
2065 (should
2066 (equal
2067 '(("1" . 1) ("2" . 3) ("3" . 2) ("3" . 2))
2068 (org-test-with-parsed-data
2069 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
2070 (org-element-map tree 'footnote-reference
2071 (lambda (ref)
2072 (cons (org-element-property :label ref)
2073 (org-export-get-footnote-number ref info nil)))
2074 info)))))
2076 (ert-deftest test-org-export/get-footnote-definition ()
2077 "Test `org-export-get-footnote-definition' specifications."
2078 ;; Standard test.
2079 (should
2080 (equal "A\n"
2081 (org-element-interpret-data
2082 (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1] A"
2083 (org-export-get-footnote-definition
2084 (org-element-map tree 'footnote-reference #'identity nil t)
2085 info)))))
2086 ;; Raise an error if no definition is found.
2087 (should-error
2088 (org-test-with-parsed-data "Text[fn:1]"
2089 (org-export-get-footnote-definition
2090 (org-element-map tree 'footnote-reference #'identity nil t)
2091 info)))
2092 ;; Find inline definitions.
2093 (should
2094 (equal "A"
2095 (org-element-interpret-data
2096 (org-test-with-parsed-data "Text[fn:1:A]"
2097 (org-export-get-footnote-definition
2098 (org-element-map tree 'footnote-reference #'identity nil t)
2099 info)))))
2100 ;; Find anonymous definitions.
2101 (should
2102 (equal "A"
2103 (org-element-interpret-data
2104 (org-test-with-parsed-data "Text[fn::A]"
2105 (org-export-get-footnote-definition
2106 (org-element-map tree 'footnote-reference #'identity nil t)
2107 info)))))
2108 ;; Find empty definitions.
2109 (should
2110 (equal ""
2111 (org-element-interpret-data
2112 (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1]"
2113 (org-export-get-footnote-definition
2114 (org-element-map tree 'footnote-reference #'identity nil t)
2115 info)))))
2116 (should
2117 (equal ""
2118 (org-element-interpret-data
2119 (org-test-with-parsed-data "Text[fn:1:]"
2120 (org-export-get-footnote-definition
2121 (org-element-map tree 'footnote-reference #'identity nil t)
2122 info)))))
2123 (should
2124 (equal ""
2125 (org-element-interpret-data
2126 (org-test-with-parsed-data "Text[fn::]"
2127 (org-export-get-footnote-definition
2128 (org-element-map tree 'footnote-reference #'identity nil t)
2129 info))))))
2131 (ert-deftest test-org-export/collect-footnote-definitions ()
2132 "Test `org-export-collect-footnote-definitions' specifications."
2133 (should
2134 (= 4
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 (length (org-export-collect-footnote-definitions info)))))
2141 ;; Limit number to provided DATA, when non-nil.
2142 (should
2143 (equal
2144 '((1 "2"))
2145 (org-test-with-parsed-data
2146 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2147 (mapcar #'butlast
2148 (org-export-collect-footnote-definitions
2149 info (org-element-map tree 'headline #'identity info t))))))
2150 (should
2151 (equal
2152 '((1 "1") (2 "2"))
2153 (org-test-with-parsed-data
2154 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2155 (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
2156 ;; With optional argument BODY-FIRST, first check body, then
2157 ;; footnote definitions.
2158 (should
2159 (equal '("1" "3" "2" nil)
2160 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2162 \[fn:2] B [fn:3] [fn::D].
2164 \[fn:3] C."
2165 (mapcar (lambda (e) (nth 1 e))
2166 (org-export-collect-footnote-definitions info nil t)))))
2167 (should-not
2168 (equal '("1" "3" "2" nil)
2169 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2171 \[fn:2] B [fn:3] [fn::D].
2173 \[fn:3] C."
2174 (mapcar (lambda (e) (nth 1 e))
2175 (org-export-collect-footnote-definitions info))))))
2177 (ert-deftest test-org-export/footnotes ()
2178 "Miscellaneous tests on footnotes."
2179 (let ((org-footnote-section nil)
2180 (org-export-with-footnotes t))
2181 ;; Read every type of footnote.
2182 (should
2183 (equal
2184 '((1 . "A\n") (2 . "C") (3 . "D"))
2185 (org-test-with-parsed-data
2186 "Text[fn:1] [fn:label:C] [fn::D]\n\n[fn:1] A\n"
2187 (org-element-map tree 'footnote-reference
2188 (lambda (ref)
2189 (let ((def (org-export-get-footnote-definition ref info)))
2190 (cons (org-export-get-footnote-number ref info)
2191 (if (eq (org-element-property :type ref) 'inline) (car def)
2192 (car (org-element-contents
2193 (car (org-element-contents def))))))))
2194 info))))
2195 ;; Export nested footnote in invisible definitions.
2196 (should
2197 (= 2
2198 (org-test-with-temp-text "Text[fn:1]\n\n[fn:1] B [fn:2]\n\n[fn:2] C."
2199 (narrow-to-region (point) (line-end-position))
2200 (catch 'exit
2201 (org-export-as
2202 (org-export-create-backend
2203 :transcoders
2204 '((section
2206 (lambda (s c i)
2207 (throw 'exit (length
2208 (org-export-collect-footnote-definitions
2209 i))))))))))))
2210 ;; Export footnotes defined outside parsing scope.
2211 (should
2212 (string-match
2213 "Out of scope"
2214 (org-test-with-temp-text "[fn:1] Out of scope
2215 * Title
2216 <point>Paragraph[fn:1]"
2217 (org-export-as (org-test-default-backend) 'subtree))))
2218 (should
2219 (string-match
2220 "Out of scope"
2221 (org-test-with-temp-text "[fn:1] Out of scope
2222 * Title
2223 <point>Paragraph[fn:1]"
2224 (narrow-to-region (point) (point-max))
2225 (org-export-as (org-test-default-backend)))))
2226 ;; Export nested footnotes defined outside parsing scope.
2227 (should
2228 (string-match
2229 "Very out of scope"
2230 (org-test-with-temp-text "
2231 \[fn:1] Out of scope[fn:2]
2233 \[fn:2] Very out of scope
2234 * Title
2235 <point>Paragraph[fn:1]"
2236 (org-export-as (org-test-default-backend) 'subtree))))
2237 (should
2238 (string-match
2239 "Very out of scope"
2240 (org-test-with-temp-text "
2241 \[fn:1] Out of scope[fn:2]
2243 \[fn:2] Very out of scope
2244 * Title
2245 <point>Paragraph[fn:1]"
2246 (narrow-to-region (point) (point-max))
2247 (org-export-as (org-test-default-backend)))))
2248 (should
2249 (string-match
2250 "D2"
2251 (org-test-with-temp-text "
2252 \[fn:1] Out of scope[fn:2:D2]
2253 * Title
2254 <point>Paragraph[fn:1]"
2255 (narrow-to-region (point) (point-max))
2256 (org-export-as (org-test-default-backend)))))
2257 ;; Export footnotes in pruned parts of tree.
2258 (should
2259 (string-match
2260 "Definition"
2261 (let ((org-export-exclude-tags '("noexport")))
2262 (org-test-with-temp-text
2263 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] Definition"
2264 (org-export-as (org-test-default-backend))))))
2265 (should
2266 (string-match
2267 "Definition"
2268 (let ((org-export-select-tags '("export")))
2269 (org-test-with-temp-text
2270 "* H :export:\nText[fn:1]\n* H2\n[fn:1] Definition"
2271 (org-export-as (org-test-default-backend))))))
2272 ;; Export nested footnotes in pruned parts of tree.
2273 (should
2274 (string-match
2275 "D2"
2276 (let ((org-export-exclude-tags '("noexport")))
2277 (org-test-with-temp-text
2278 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
2279 (org-export-as (org-test-default-backend))))))
2280 (should
2281 (string-match
2282 "D2"
2283 (let ((org-export-select-tags '("export")))
2284 (org-test-with-temp-text
2285 "* H :export:\nText[fn:1]\n* H2\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
2286 (org-export-as (org-test-default-backend))))))
2287 ;; Handle uninterpreted data in pruned footnote definitions.
2288 (should-not
2289 (string-match
2291 (let ((org-export-with-tables nil))
2292 (org-test-with-temp-text
2293 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1]\n| a |"
2294 (org-export-as (org-test-default-backend))))))
2295 ;; Footnotes without a definition should throw an error.
2296 (should-error
2297 (org-test-with-parsed-data "Text[fn:1]"
2298 (org-export-get-footnote-definition
2299 (org-element-map tree 'footnote-reference #'identity info t) info)))
2300 ;; Footnote section should be ignored in TOC and in headlines
2301 ;; numbering.
2302 (should
2303 (= 1 (let ((org-footnote-section "Footnotes"))
2304 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
2305 (org-export-collect-headlines info))))))
2306 (should
2307 (equal '(2)
2308 (let ((org-footnote-section "Footnotes"))
2309 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
2310 (org-element-map tree 'headline
2311 (lambda (hl)
2312 (when (equal (org-element-property :raw-value hl) "H2")
2313 (org-export-get-headline-number hl info)))
2314 info t)))))))
2318 ;;; Headlines and Inlinetasks
2320 (ert-deftest test-org-export/get-relative-level ()
2321 "Test `org-export-get-relative-level' specifications."
2322 ;; Standard test.
2323 (should
2324 (equal '(1 2)
2325 (let ((org-odd-levels-only nil))
2326 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2327 (org-element-map tree 'headline
2328 (lambda (h) (org-export-get-relative-level h info))
2329 info)))))
2330 ;; Missing levels
2331 (should
2332 (equal '(1 3)
2333 (let ((org-odd-levels-only nil))
2334 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
2335 (org-element-map tree 'headline
2336 (lambda (h) (org-export-get-relative-level h info))
2337 info))))))
2339 (ert-deftest test-org-export/low-level-p ()
2340 "Test `org-export-low-level-p' specifications."
2341 (should
2342 (equal
2343 '(no yes)
2344 (let ((org-odd-levels-only nil))
2345 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2346 (org-element-map tree 'headline
2347 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
2348 (plist-put info :headline-levels 1)))))))
2350 (ert-deftest test-org-export/get-headline-number ()
2351 "Test `org-export-get-headline-number' specifications."
2352 ;; Standard test.
2353 (should
2354 (equal
2355 '((1) (1 1))
2356 (let ((org-odd-levels-only nil))
2357 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2358 (org-element-map tree 'headline
2359 (lambda (h) (org-export-get-headline-number h info))
2360 info)))))
2361 ;; Missing levels are replaced with 0.
2362 (should
2363 (equal
2364 '((1) (1 0 1))
2365 (let ((org-odd-levels-only nil))
2366 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
2367 (org-element-map tree 'headline
2368 (lambda (h) (org-export-get-headline-number h info))
2369 info))))))
2371 (ert-deftest test-org-export/numbered-headline-p ()
2372 "Test `org-export-numbered-headline-p' specifications."
2373 ;; If `:section-numbers' is nil, never number headlines.
2374 (should-not
2375 (org-test-with-parsed-data "* Headline"
2376 (org-element-map tree 'headline
2377 (lambda (h) (org-export-numbered-headline-p h info))
2378 (plist-put info :section-numbers nil))))
2379 ;; If `:section-numbers' is a number, only number headlines with
2380 ;; a level greater that it.
2381 (should
2382 (equal
2383 '(yes no)
2384 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2385 (org-element-map tree 'headline
2386 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
2387 (plist-put info :section-numbers 1)))))
2388 ;; Otherwise, headlines are always numbered.
2389 (should
2390 (org-test-with-parsed-data "* Headline"
2391 (org-element-map tree 'headline
2392 (lambda (h) (org-export-numbered-headline-p h info))
2393 (plist-put info :section-numbers t))))
2394 ;; With #+OPTIONS: num:nil all headlines are unnumbered.
2395 (should-not
2396 (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
2397 (org-export-numbered-headline-p
2398 (org-element-map tree 'headline 'identity info t)
2399 info)))
2400 ;; Headlines with a non-nil UNNUMBERED property are not numbered.
2401 (should-not
2402 (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
2403 (org-export-numbered-headline-p
2404 (org-element-map tree 'headline #'identity info t)
2405 info)))
2406 ;; UNNUMBERED ignores inheritance. Any non-nil value among
2407 ;; ancestors disables numbering.
2408 (should
2409 (org-test-with-parsed-data
2410 "* H
2411 :PROPERTIES:
2412 :UNNUMBERED: t
2413 :END:
2414 ** H2
2415 :PROPERTIES:
2416 :UNNUMBERED: nil
2417 :END:
2418 *** H3"
2419 (cl-every
2420 (lambda (h) (not (org-export-numbered-headline-p h info)))
2421 (org-element-map tree 'headline #'identity info)))))
2423 (ert-deftest test-org-export/number-to-roman ()
2424 "Test `org-export-number-to-roman' specifications."
2425 ;; If number is negative, return it as a string.
2426 (should (equal (org-export-number-to-roman -1) "-1"))
2427 ;; Otherwise, return it as a roman number.
2428 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
2430 (ert-deftest test-org-export/get-optional-title ()
2431 "Test `org-export-get-alt-title' specifications."
2432 ;; If ALT_TITLE property is defined, use it.
2433 (should
2434 (equal '("opt")
2435 (org-test-with-parsed-data
2436 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
2437 (org-export-get-alt-title
2438 (org-element-map tree 'headline 'identity info t)
2439 info))))
2440 ;; Otherwise, fall-back to regular title.
2441 (should
2442 (equal '("Headline")
2443 (org-test-with-parsed-data "* Headline"
2444 (org-export-get-alt-title
2445 (org-element-map tree 'headline 'identity info t)
2446 info)))))
2448 (ert-deftest test-org-export/get-tags ()
2449 "Test `org-export-get-tags' specifications."
2450 ;; Standard test: tags which are not a select tag, an exclude tag,
2451 ;; or specified as optional argument shouldn't be ignored.
2452 (should
2453 (org-test-with-parsed-data "* Headline :tag:"
2454 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2455 info)))
2456 ;; Tags provided in the optional argument are ignored.
2457 (should-not
2458 (org-test-with-parsed-data "* Headline :ignore:"
2459 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2460 info '("ignore"))))
2461 ;; Allow tag inheritance.
2462 (should
2463 (equal
2464 '(("tag") ("tag"))
2465 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
2466 (org-element-map tree 'headline
2467 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
2468 ;; Tag inheritance checks FILETAGS keywords.
2469 (should
2470 (equal
2471 '(("a" "b" "tag"))
2472 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
2473 (org-element-map tree 'headline
2474 (lambda (hl) (org-export-get-tags hl info nil t)) info)))))
2476 (ert-deftest test-org-export/get-node-property ()
2477 "Test`org-export-get-node-property' specifications."
2478 ;; Standard test.
2479 (should
2480 (equal "value"
2481 (org-test-with-parsed-data "* Headline
2482 :PROPERTIES:
2483 :prop: value
2484 :END:"
2485 (org-export-get-node-property
2486 :PROP (org-element-map tree 'headline 'identity nil t)))))
2487 ;; Test inheritance.
2488 (should
2489 (equal "value"
2490 (org-test-with-parsed-data "* Parent
2491 :PROPERTIES:
2492 :prop: value
2493 :END:
2494 ** Headline
2495 Paragraph"
2496 (org-export-get-node-property
2497 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
2498 ;; Cannot return a value before the first headline.
2499 (should-not
2500 (org-test-with-parsed-data "Paragraph
2501 * Headline
2502 :PROPERTIES:
2503 :prop: value
2504 :END:"
2505 (org-export-get-node-property
2506 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
2508 (ert-deftest test-org-export/get-category ()
2509 "Test `org-export-get-category' specifications."
2510 ;; Standard test.
2511 (should
2512 (equal "value"
2513 (org-test-with-parsed-data "* Headline
2514 :PROPERTIES:
2515 :CATEGORY: value
2516 :END:"
2517 (org-export-get-category
2518 (org-element-map tree 'headline 'identity nil t) info))))
2519 ;; Test inheritance from a parent headline.
2520 (should
2521 (equal '("value" "value")
2522 (org-test-with-parsed-data "* Headline1
2523 :PROPERTIES:
2524 :CATEGORY: value
2525 :END:
2526 ** Headline2"
2527 (org-element-map tree 'headline
2528 (lambda (hl) (org-export-get-category hl info)) info))))
2529 ;; Test inheritance from #+CATEGORY keyword
2530 (should
2531 (equal "value"
2532 (org-test-with-parsed-data "#+CATEGORY: value
2533 * Headline"
2534 (org-export-get-category
2535 (org-element-map tree 'headline 'identity nil t) info))))
2536 ;; Test inheritance from file name.
2537 (should
2538 (equal "test"
2539 (org-test-with-parsed-data "* Headline"
2540 (let ((info (plist-put info :input-file "~/test.org")))
2541 (org-export-get-category
2542 (org-element-map tree 'headline 'identity nil t) info)))))
2543 ;; Fall-back value.
2544 (should
2545 (equal "???"
2546 (org-test-with-parsed-data "* Headline"
2547 (org-export-get-category
2548 (org-element-map tree 'headline 'identity nil t) info)))))
2550 (ert-deftest test-org-export/first-sibling-p ()
2551 "Test `org-export-first-sibling-p' specifications."
2552 ;; Standard test.
2553 (should
2554 (equal
2555 '(yes yes no)
2556 (org-test-with-parsed-data "* H\n** H 2\n** H 3"
2557 (org-element-map tree 'headline
2558 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2559 info))))
2560 (should
2561 (equal '(yes no)
2562 (org-test-with-parsed-data "- item\n\n para"
2563 (org-element-map tree 'paragraph
2564 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2565 info))))
2566 ;; Ignore sections for headlines.
2567 (should
2568 (equal '(yes yes)
2569 (org-test-with-parsed-data "* H\nSection\n** H 2"
2570 (org-element-map tree 'headline
2571 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2572 info))))
2573 ;; Ignore headlines not exported.
2574 (should
2575 (equal
2576 '(yes)
2577 (let ((org-export-exclude-tags '("ignore")))
2578 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
2579 (org-element-map tree 'headline
2580 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2581 info))))))
2583 (ert-deftest test-org-export/last-sibling-p ()
2584 "Test `org-export-last-sibling-p' specifications."
2585 ;; Standard test.
2586 (should
2587 (equal
2588 '(yes no yes)
2589 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
2590 (org-element-map tree 'headline
2591 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2592 info))))
2593 (should
2594 (equal '(no yes)
2595 (org-test-with-parsed-data "- item\n\n para"
2596 (org-element-map tree 'paragraph
2597 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2598 info))))
2599 ;; Ignore headlines not exported.
2600 (should
2601 (equal
2602 '(yes)
2603 (let ((org-export-exclude-tags '("ignore")))
2604 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
2605 (org-element-map tree 'headline
2606 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2607 info))))))
2609 (ert-deftest test-org-export/handle-inlinetasks ()
2610 "Test inlinetask export."
2611 ;; Inlinetask with an exclude tag.
2612 (when (featurep 'org-inlinetask)
2613 (should
2614 (equal
2616 (let ((org-inlinetask-min-level 3)
2617 org-export-filter-body-functions
2618 org-export-filter-final-output-functions)
2619 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
2620 (org-export-as (org-test-default-backend)
2621 nil nil nil '(:exclude-tags ("noexp")))))))
2622 ;; Inlinetask with an include tag.
2623 (should
2624 (equal
2625 "* H2\n*** Inline :exp:\n"
2626 (let ((org-inlinetask-min-level 3)
2627 (org-tags-column 0))
2628 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
2629 (org-export-as (org-test-default-backend)
2630 nil nil nil '(:select-tags ("exp")))))))
2631 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
2632 (should
2633 (equal ""
2634 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
2635 (org-inlinetask-min-level 3)
2636 org-export-filter-body-functions
2637 org-export-filter-final-output-functions)
2638 (org-test-with-temp-text "*** TODO Inline"
2639 (org-export-as (org-test-default-backend)
2640 nil nil nil '(:with-tasks nil))))))))
2644 ;;; Keywords
2646 (ert-deftest test-org-export/get-date ()
2647 "Test `org-export-get-date' specifications."
2648 ;; Return a properly formatted string when
2649 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
2650 ;; consists in a single timestamp.
2651 (should
2652 (equal "29 03 2012"
2653 (let ((org-export-date-timestamp-format "%d %m %Y"))
2654 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2655 (org-export-get-date info)))))
2656 ;; Return a secondary string otherwise.
2657 (should-not
2658 (stringp
2659 (let ((org-export-date-timestamp-format nil))
2660 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2661 (org-export-get-date info)))))
2662 (should
2663 (equal '("Date")
2664 (org-test-with-parsed-data "#+DATE: Date"
2665 (org-export-get-date info))))
2666 ;; Optional argument has precedence over
2667 ;; `org-export-date-timestamp-format'.
2668 (should
2669 (equal "29 03"
2670 (let ((org-export-date-timestamp-format "%d %m %Y"))
2671 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2672 (org-export-get-date info "%d %m"))))))
2676 ;;; Links
2678 (ert-deftest test-org-export/custom-protocol-maybe ()
2679 "Test `org-export-custom-protocol-maybe' specifications."
2680 (should
2681 (string-match
2682 "success"
2683 (progn
2684 (org-link-set-parameters "foo" :export (lambda (p d f) "success"))
2685 (org-export-string-as
2686 "[[foo:path]]"
2687 (org-export-create-backend
2688 :name 'test
2689 :transcoders
2690 '((section . (lambda (s c i) c))
2691 (paragraph . (lambda (p c i) c))
2692 (link . (lambda (l c i)
2693 (or (org-export-custom-protocol-maybe l c 'test)
2694 "failure")))))))))
2695 (should-not
2696 (string-match
2697 "success"
2698 (progn
2699 (org-link-set-parameters
2700 "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
2701 (org-export-string-as
2702 "[[foo:path]]"
2703 (org-export-create-backend
2704 :name 'no-test
2705 :transcoders
2706 '((section . (lambda (s c i) c))
2707 (paragraph . (lambda (p c i) c))
2708 (link . (lambda (l c i)
2709 (or (org-export-custom-protocol-maybe l c 'no-test)
2710 "failure")))))))))
2711 ;; Ignore anonymous back-ends.
2712 (should-not
2713 (string-match
2714 "success"
2715 (progn
2716 (org-link-set-parameters
2717 "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
2718 (org-export-string-as
2719 "[[foo:path]]"
2720 (org-export-create-backend
2721 :transcoders
2722 '((section . (lambda (s c i) c))
2723 (paragraph . (lambda (p c i) c))
2724 (link . (lambda (l c i)
2725 (or (org-export-custom-protocol-maybe l c nil)
2726 "failure"))))))))))
2728 (ert-deftest test-org-export/get-coderef-format ()
2729 "Test `org-export-get-coderef-format' specifications."
2730 ;; A link without description returns "%s"
2731 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
2732 "%s"))
2733 ;; Return "%s" when path is matched within description.
2734 (should (equal (org-export-get-coderef-format "path" "desc (path)")
2735 "desc %s"))
2736 ;; Otherwise return description.
2737 (should (equal (org-export-get-coderef-format "path" "desc")
2738 "desc")))
2740 (ert-deftest test-org-export/inline-image-p ()
2741 "Test `org-export-inline-image-p' specifications."
2742 (should
2743 (org-export-inline-image-p
2744 (org-test-with-temp-text "[[#id]]"
2745 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
2746 '(("custom-id" . "id")))))
2748 (ert-deftest test-org-export/fuzzy-link ()
2749 "Test fuzzy links specifications."
2750 ;; Link to an headline should return headline's number.
2751 (should
2752 ;; Note: Headline's number is in fact a list of numbers.
2753 (equal '(2)
2754 (org-test-with-parsed-data
2755 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
2756 (org-element-map tree 'link
2757 (lambda (link)
2758 (org-export-get-ordinal
2759 (org-export-resolve-fuzzy-link link info) info)) info t))))
2760 ;; Link to a target in an item should return item's number.
2761 (should
2762 ;; Note: Item's number is in fact a list of numbers.
2763 (equal '(1 2)
2764 (org-test-with-parsed-data
2765 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
2766 (org-element-map tree 'link
2767 (lambda (link)
2768 (org-export-get-ordinal
2769 (org-export-resolve-fuzzy-link link info) info)) info t))))
2770 ;; Link to a target in a footnote should return footnote's number.
2771 (should
2772 (equal '(2 3)
2773 (org-test-with-parsed-data "
2774 Paragraph[fn:1][fn:2][fn:lbl3:C<<target>>][[test]][[target]]
2775 \[fn:1] A
2777 \[fn:2] <<test>>B"
2778 (org-element-map tree 'link
2779 (lambda (link)
2780 (org-export-get-ordinal
2781 (org-export-resolve-fuzzy-link link info) info)) info))))
2782 ;; Link to a named element should return sequence number of that
2783 ;; element.
2784 (should
2785 (= 2
2786 (org-test-with-parsed-data
2787 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
2788 (org-element-map tree 'link
2789 (lambda (link)
2790 (org-export-get-ordinal
2791 (org-export-resolve-fuzzy-link link info) info)) info t))))
2792 ;; Link to a target not within an item, a table, a footnote
2793 ;; reference or definition should return section number.
2794 (should
2795 (equal '(2)
2796 (org-test-with-parsed-data
2797 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
2798 (org-element-map tree 'link
2799 (lambda (link)
2800 (org-export-get-ordinal
2801 (org-export-resolve-fuzzy-link link info) info)) info t))))
2802 ;; Space are not significant when matching a fuzzy link.
2803 (should
2804 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
2805 (org-element-map tree 'link
2806 (lambda (link) (org-export-resolve-fuzzy-link link info))
2807 info t)))
2808 ;; Statistics cookies are ignored for headline match.
2809 (should
2810 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
2811 (org-element-map tree 'link
2812 (lambda (link) (org-export-resolve-fuzzy-link link info))
2813 info t)))
2814 (should
2815 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
2816 (org-element-map tree 'link
2817 (lambda (link) (org-export-resolve-fuzzy-link link info))
2818 info t))))
2820 (defun test-org-gen-loc-list(text type)
2821 (org-test-with-parsed-data text
2822 (org-element-map tree type
2823 (lambda (el) (or (org-export-get-loc el info) 'no-loc)))))
2825 (ert-deftest test-org-export/get-loc ()
2826 "Test `org-export-get-loc' specifications."
2827 (should
2828 ;; "-n" resets line number.
2829 (equal '(0)
2830 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n\n Text\n#+END_EXAMPLE"
2831 'example-block)))
2832 ;; The first "+n" has 0 lines before it
2833 (should
2834 (equal '(0)
2835 (test-org-gen-loc-list "#+BEGIN_EXAMPLE +n\n Text\n#+END_EXAMPLE"
2836 'example-block)))
2837 ;; "-n 10" resets line number but has "9 lines" before it.
2838 (should
2839 (equal '(9)
2840 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n 10\n Text\n#+END_EXAMPLE"
2841 'example-block)))
2842 ;; -n10 with two lines then +n 15
2843 (should
2844 (equal '(9 25)
2845 (test-org-gen-loc-list "
2846 #+BEGIN_EXAMPLE -n 10
2847 Text_10
2848 Second line(11)
2849 #+END_EXAMPLE
2850 #+BEGIN_EXAMPLE +n 15
2851 Text line (11 + 15)
2852 #+END_EXAMPLE"
2853 'example-block)))
2854 (should
2855 (equal '(9 19 0)
2856 (test-org-gen-loc-list "
2857 #+BEGIN_EXAMPLE -n 10
2858 Text
2859 #+END_EXAMPLE
2860 #+BEGIN_EXAMPLE +n 10
2861 Text
2862 #+END_EXAMPLE
2864 #+BEGIN_EXAMPLE -n
2865 Text
2866 #+END_EXAMPLE"
2867 'example-block)))
2868 ;; an Example Block without -n does not add to the line count.
2869 (should
2870 (equal '(9 no-loc 19)
2871 (test-org-gen-loc-list "
2872 #+BEGIN_EXAMPLE -n 10
2873 Text
2874 #+END_EXAMPLE
2875 #+BEGIN_EXAMPLE
2876 Text
2877 #+END_EXAMPLE
2878 #+BEGIN_EXAMPLE +n 10
2879 Text
2880 #+END_EXAMPLE"
2881 'example-block)))
2882 ;; "-n" resets line number.
2883 (should
2884 (equal
2885 '(0)
2886 (test-org-gen-loc-list "#+BEGIN_SRC emacs-lisp -n \n (- 1 1) \n#+END_SRC"
2887 'src-block)))
2888 ;; The first "+n" has 0 lines before it.
2889 (should
2890 (equal '(0)
2891 (test-org-gen-loc-list
2892 "#+BEGIN_SRC emacs-lisp +n \n (+ 0 (- 1 1))\n#+END_SRC"
2893 'src-block)))
2894 ;; "-n 10" resets line number but has "9 lines" before it.
2895 (should
2896 (equal '(9)
2897 (test-org-gen-loc-list
2898 "#+BEGIN_SRC emacs-lisp -n 10\n (- 10 1)\n#+END_SRC"
2899 'src-block)))
2900 (should
2901 (equal '(9 25)
2902 (test-org-gen-loc-list "
2903 #+BEGIN_SRC emacs-lisp -n 10
2904 (- 10 1)
2905 (+ (- 10 1) 1)
2906 #+END_SRC
2907 #+BEGIN_SRC emacs-lisp +n 15
2908 (+ (- 10 1) 2 (- 15 1))
2909 #+END_SRC"
2910 'src-block)))
2911 (should
2912 (equal '(9 19 0)
2913 (test-org-gen-loc-list "
2914 #+BEGIN_SRC emacs-lisp -n 10
2915 (- 10 1)
2916 #+END_SRC
2917 #+BEGIN_SRC emacs-lisp +n 10
2918 (+ (- 10 1) 1 (- 10 1))
2919 #+END_SRC
2920 #+BEGIN_SRC emacs-lisp -n
2921 (- 1 1)
2922 #+END_SRC"
2923 'src-block)))
2924 ;; A SRC Block without -n does not add to the line count.
2925 (should
2926 (equal '(9 no-loc 19)
2927 (test-org-gen-loc-list
2928 "#+BEGIN_SRC emacs-lisp -n 10
2929 (+ (-10 1) 1)
2930 #+END_SRC
2931 #+BEGIN_SRC emacs-lisp
2932 (+ 2 2)
2933 #+END_SRC
2934 #+BEGIN_SRC emacs-lisp +n 10
2935 (+ (- 10 1) 1 (- 10 1))
2936 #+END_SRC"
2937 'src-block))))
2939 (ert-deftest test-org-export/resolve-coderef ()
2940 "Test `org-export-resolve-coderef' specifications."
2941 (let ((org-coderef-label-format "(ref:%s)"))
2942 ;; A link to a "-n -k -r" block returns line number.
2943 (should
2944 (= 1
2945 (org-test-with-parsed-data
2946 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
2947 (org-export-resolve-coderef "coderef" info))))
2948 (should
2949 (= 10
2950 (org-test-with-parsed-data
2951 "#+BEGIN_EXAMPLE -n 10 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
2952 (org-export-resolve-coderef "coderef" info))))
2953 (should
2954 (= 135
2955 (org-test-with-parsed-data
2956 "#+BEGIN_EXAMPLE -n 10 -k -r\nText \n#+END_EXAMPLE\n
2957 #+BEGIN_EXAMPLE +n 125 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
2958 (org-export-resolve-coderef "coderef" info))))
2959 (should
2960 (= 1
2961 (org-test-with-parsed-data
2962 "#+BEGIN_SRC emacs-lisp -n -k -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 -n 10 -k -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 -n 10 -k -r\n(+ 1 1) \n#+END_SRC\n
2973 #+BEGIN_SRC emacs-lisp +n 125 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2974 (org-export-resolve-coderef "coderef" info))))
2975 ;; A link to a "-n -r" block returns line number.
2976 (should
2977 (= 1
2978 (org-test-with-parsed-data
2979 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
2980 (org-export-resolve-coderef "coderef" info))))
2981 (should
2982 (= 10
2983 (org-test-with-parsed-data
2984 "#+BEGIN_EXAMPLE -n 10 -r\nText (ref:coderef)\n#+END_EXAMPLE"
2985 (org-export-resolve-coderef "coderef" info))))
2986 (should
2987 (= 135
2988 (org-test-with-parsed-data
2989 "#+BEGIN_EXAMPLE +n 10 -r\nText \n#+END_EXAMPLE
2990 #+BEGIN_EXAMPLE +n 125 -r\nText (ref:coderef)\n#+END_EXAMPLE"
2991 (org-export-resolve-coderef "coderef" info))))
2993 (should
2994 (= 1
2995 (org-test-with-parsed-data
2996 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2997 (org-export-resolve-coderef "coderef" info))))
2998 (should
2999 (= 10
3000 (org-test-with-parsed-data
3001 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3002 (org-export-resolve-coderef "coderef" info))))
3003 (should
3004 (= 135
3005 (org-test-with-parsed-data
3006 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) \n#+END_SRC
3007 #+BEGIN_SRC emacs-lisp +n125 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3008 (org-export-resolve-coderef "coderef" info))))
3009 ;; A link to a "-n" block returns coderef.
3010 (should
3011 (equal "coderef"
3012 (org-test-with-parsed-data
3013 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3014 (org-export-resolve-coderef "coderef" info))))
3015 (should
3016 (equal "coderef"
3017 (org-test-with-parsed-data
3018 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
3019 (org-export-resolve-coderef "coderef" info))))
3020 ;; A link to a "-r" block returns line number.
3021 (should
3022 (= 1
3023 (org-test-with-parsed-data
3024 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3025 (org-export-resolve-coderef "coderef" info))))
3026 (should
3027 (= 1
3028 (org-test-with-parsed-data
3029 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
3030 (org-export-resolve-coderef "coderef" info))))
3031 ;; A link to a block without a switch returns coderef.
3032 (should
3033 (equal "coderef"
3034 (org-test-with-parsed-data
3035 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3036 (org-export-resolve-coderef "coderef" info))))
3037 (org-test-with-parsed-data
3038 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
3039 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
3040 ;; Correctly handle continued line numbers. A "+n" switch should
3041 ;; resume numbering from previous block with numbered lines,
3042 ;; ignoring blocks not numbering lines in the process. A "-n"
3043 ;; switch resets count.
3044 (should
3045 (equal '(2 1)
3046 (org-test-with-parsed-data "
3047 #+BEGIN_EXAMPLE -n
3048 Text.
3049 #+END_EXAMPLE
3051 #+BEGIN_SRC emacs-lisp
3052 \(- 1 1)
3053 #+END_SRC
3055 #+BEGIN_SRC emacs-lisp +n -r
3056 \(+ 1 1) (ref:addition)
3057 #+END_SRC
3059 #+BEGIN_EXAMPLE -n -r
3060 Another text. (ref:text)
3061 #+END_EXAMPLE"
3062 (list (org-export-resolve-coderef "addition" info)
3063 (org-export-resolve-coderef "text" info)))))
3064 ;; Recognize coderef with user-specified syntax.
3065 (should
3066 (equal
3067 "text"
3068 (org-test-with-parsed-data
3069 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
3070 (org-export-resolve-coderef "text" info))))
3071 ;; Unresolved coderefs raise a `org-link-broken' signal.
3072 (should
3073 (condition-case nil
3074 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
3075 (org-export-resolve-coderef "unknown" info))
3076 (org-link-broken t)))))
3078 (ert-deftest test-org-export/resolve-fuzzy-link ()
3079 "Test `org-export-resolve-fuzzy-link' specifications."
3080 ;; Match target objects.
3081 (should
3082 (org-test-with-parsed-data "<<target>> [[target]]"
3083 (org-export-resolve-fuzzy-link
3084 (org-element-map tree 'link 'identity info t) info)))
3085 ;; Match named elements.
3086 (should
3087 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
3088 (org-export-resolve-fuzzy-link
3089 (org-element-map tree 'link 'identity info t) info)))
3090 ;; Match exact headline's name.
3091 (should
3092 (org-test-with-parsed-data "* My headline\n[[My headline]]"
3093 (org-export-resolve-fuzzy-link
3094 (org-element-map tree 'link 'identity info t) info)))
3095 ;; Targets objects have priority over headline titles.
3096 (should
3097 (eq 'target
3098 (org-test-with-parsed-data "* target\n<<target>>[[target]]"
3099 (org-element-type
3100 (org-export-resolve-fuzzy-link
3101 (org-element-map tree 'link 'identity info t) info)))))
3102 ;; Named elements have priority over headline titles.
3103 (should
3104 (eq 'paragraph
3105 (org-test-with-parsed-data
3106 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
3107 (org-element-type
3108 (org-export-resolve-fuzzy-link
3109 (org-element-map tree 'link 'identity info t) info)))))
3110 ;; If link's path starts with a "*", only match headline titles,
3111 ;; though.
3112 (should
3113 (eq 'headline
3114 (org-test-with-parsed-data
3115 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
3116 (org-element-type
3117 (org-export-resolve-fuzzy-link
3118 (org-element-map tree 'link 'identity info t) info)))))
3119 ;; Raise a `org-link-broken' signal if no match.
3120 (should
3121 (org-test-with-parsed-data "[[target]]"
3122 (condition-case nil
3123 (org-export-resolve-fuzzy-link
3124 (org-element-map tree 'link #'identity info t) info)
3125 (org-link-broken t))))
3126 ;; Match fuzzy link even when before first headline.
3127 (should
3128 (eq 'headline
3129 (org-test-with-parsed-data "[[hl]]\n* hl"
3130 (org-element-type
3131 (org-export-resolve-fuzzy-link
3132 (org-element-map tree 'link 'identity info t) info)))))
3133 ;; Handle url-encoded fuzzy links.
3134 (should
3135 (org-test-with-parsed-data "* A B\n[[A%20B]]"
3136 (org-export-resolve-fuzzy-link
3137 (org-element-map tree 'link #'identity info t) info))))
3139 (ert-deftest test-org-export/resolve-id-link ()
3140 "Test `org-export-resolve-id-link' specifications."
3141 ;; Regular test for custom-id link.
3142 (should
3143 (equal '("Headline1")
3144 (org-test-with-parsed-data "* Headline1
3145 :PROPERTIES:
3146 :CUSTOM_ID: test
3147 :END:
3148 * Headline 2
3149 \[[#test]]"
3150 (org-element-property
3151 :title
3152 (org-export-resolve-id-link
3153 (org-element-map tree 'link 'identity info t) info)))))
3154 ;; Raise a `org-link-broken' signal on failing searches.
3155 (should
3156 (org-test-with-parsed-data "* Headline1
3157 :PROPERTIES:
3158 :CUSTOM_ID: test
3159 :END:
3160 * Headline 2
3161 \[[#no-match]]"
3162 (condition-case nil
3163 (org-export-resolve-id-link
3164 (org-element-map tree 'link #'identity info t) info)
3165 (org-link-broken t))))
3166 ;; Test for internal id target.
3167 (should
3168 (equal '("Headline1")
3169 (org-test-with-parsed-data "* Headline1
3170 :PROPERTIES:
3171 :ID: aaaa
3172 :END:
3173 * Headline 2
3174 \[[id:aaaa]]"
3175 (org-element-property
3176 :title
3177 (org-export-resolve-id-link
3178 (org-element-map tree 'link 'identity info t) info)))))
3179 ;; Test for external id target.
3180 (should
3181 (equal
3182 "external-file"
3183 (org-test-with-parsed-data "[[id:aaaa]]"
3184 (org-export-resolve-id-link
3185 (org-element-map tree 'link 'identity info t)
3186 (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
3188 (ert-deftest test-org-export/resolve-radio-link ()
3189 "Test `org-export-resolve-radio-link' specifications."
3190 ;; Standard test.
3191 (should
3192 (org-test-with-temp-text "<<<radio>>> radio"
3193 (org-update-radio-target-regexp)
3194 (let* ((tree (org-element-parse-buffer))
3195 (info `(:parse-tree ,tree)))
3196 (org-export-resolve-radio-link
3197 (org-element-map tree 'link 'identity info t)
3198 info))))
3199 ;; Radio targets are case-insensitive.
3200 (should
3201 (org-test-with-temp-text "<<<RADIO>>> radio"
3202 (org-update-radio-target-regexp)
3203 (let* ((tree (org-element-parse-buffer))
3204 (info `(:parse-tree ,tree)))
3205 (org-export-resolve-radio-link
3206 (org-element-map tree 'link 'identity info t)
3207 info))))
3208 ;; Radio target with objects.
3209 (should
3210 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
3211 (org-update-radio-target-regexp)
3212 (let* ((tree (org-element-parse-buffer))
3213 (info `(:parse-tree ,tree)))
3214 (org-export-resolve-radio-link
3215 (org-element-map tree 'link 'identity info t)
3216 info))))
3217 ;; Radio target with objects at its beginning.
3218 (should
3219 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
3220 (org-update-radio-target-regexp)
3221 (let* ((tree (org-element-parse-buffer))
3222 (info `(:parse-tree ,tree)))
3223 (org-export-resolve-radio-link
3224 (org-element-map tree 'link 'identity info t)
3225 info))))
3226 ;; Radio link next to an apostrophe.
3227 (should
3228 (org-test-with-temp-text "<<<radio>>> radio's"
3229 (org-update-radio-target-regexp)
3230 (let* ((tree (org-element-parse-buffer))
3231 (info `(:parse-tree ,tree)))
3232 (org-export-resolve-radio-link
3233 (org-element-map tree 'link 'identity info t)
3234 info))))
3235 ;; Multiple radio targets.
3236 (should
3237 (equal '("radio1" "radio2")
3238 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
3239 (org-update-radio-target-regexp)
3240 (let* ((tree (org-element-parse-buffer))
3241 (info `(:parse-tree ,tree)))
3242 (org-element-map tree 'link
3243 (lambda (link)
3244 (org-element-property
3245 :value (org-export-resolve-radio-link link info)))
3246 info)))))
3247 ;; Radio target is whitespace insensitive.
3248 (should
3249 (org-test-with-temp-text "<<<a radio>>> a\n radio"
3250 (org-update-radio-target-regexp)
3251 (let* ((tree (org-element-parse-buffer))
3252 (info `(:parse-tree ,tree)))
3253 (org-element-map tree 'link
3254 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
3256 (ert-deftest test-org-export/file-uri ()
3257 "Test `org-export-file-uri' specifications."
3258 ;; Preserve relative filenames.
3259 (should (equal "relative.org" (org-export-file-uri "relative.org")))
3260 ;; Local files start with "file://"
3261 (should (equal (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "/local.org"))
3262 (org-export-file-uri "/local.org")))
3263 ;; Remote files start with "file://"
3264 (should (equal "file://myself@some.where:papers/last.pdf"
3265 (org-export-file-uri "/myself@some.where:papers/last.pdf")))
3266 (should (equal "file://localhost/etc/fstab"
3267 (org-export-file-uri "//localhost/etc/fstab")))
3268 ;; Expand filename starting with "~".
3269 (should (equal (org-export-file-uri "~/file.org")
3270 (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "~/file.org")))))
3272 (ert-deftest test-org-export/get-reference ()
3273 "Test `org-export-get-reference' specifications."
3274 (should
3275 (org-test-with-parsed-data "* Headline"
3276 (org-export-get-reference (org-element-map tree 'headline #'identity nil t)
3277 info)))
3278 ;; For a given element always return the same reference.
3279 (should
3280 (org-test-with-parsed-data "* Headline"
3281 (let ((headline (org-element-map tree 'headline #'identity nil t)))
3282 (equal (org-export-get-reference headline info)
3283 (org-export-get-reference headline info)))))
3284 ;; References get through local export back-ends.
3285 (should
3286 (org-test-with-parsed-data "* Headline"
3287 (let ((headline (org-element-map tree 'headline #'identity nil t))
3288 (backend
3289 (org-export-create-backend
3290 :transcoders
3291 '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
3292 (equal (org-trim (org-export-data-with-backend headline backend info))
3293 (org-export-get-reference headline info)))))
3294 (should
3295 (org-test-with-parsed-data "* Headline"
3296 (let ((headline (org-element-map tree 'headline #'identity nil t))
3297 (backend
3298 (org-export-create-backend
3299 :transcoders
3300 '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
3301 (equal (org-export-with-backend backend headline nil info)
3302 (org-export-get-reference headline info)))))
3303 ;; Use search cells defined in `:crossrefs'. However, handle
3304 ;; duplicate search cells.
3305 (should
3306 (equal "org0000001"
3307 (org-test-with-parsed-data "* Headline"
3308 (let* ((headline (org-element-map tree 'headline #'identity nil t))
3309 (search-cell (car (org-export-search-cells headline))))
3310 (setq info
3311 (plist-put info :crossrefs (list (cons search-cell 1))))
3312 (org-export-get-reference headline info)))))
3313 (should-not
3314 (equal '("org0000001" "org0000001")
3315 (org-test-with-parsed-data "* H\n** H"
3316 (org-element-map tree 'headline
3317 (lambda (h)
3318 (let* ((search-cell (car (org-export-search-cells h)))
3319 (info (plist-put info :crossrefs
3320 (list (cons search-cell 1)))))
3321 (org-export-get-reference h info))))))))
3324 ;;; Pseudo objects and pseudo elements
3326 (ert-deftest test-org-export/pseudo-elements ()
3327 "Test exporting pseudo-elements."
3328 ;; Handle blank lines after pseudo-elements. In particular, do not
3329 ;; replace them with white spaces.
3330 (should
3331 (equal "contents\n\nparagraph\n"
3332 (let ((backend (org-export-create-backend
3333 :transcoders
3334 '((pseudo-element . (lambda (_p c _i) c))
3335 (paragraph . (lambda (_p c _i) c))
3336 (plain-text . (lambda (c _i) c)))))
3337 (element '(pseudo-element (:post-blank 1) "contents"))
3338 (paragraph '(paragraph nil "paragraph"))
3339 (data '(org-data nil)))
3340 (org-element-adopt-elements data element paragraph)
3341 (org-export-data-with-backend data backend nil)))))
3343 (ert-deftest test-org-export/pseudo-objects ()
3344 "Test exporting pseudo-objects."
3345 ;; Handle blank spaces after pseudo-objects. In particular, do not
3346 ;; replace them with newlines.
3347 (should
3348 (equal "begin x end\n"
3349 (let ((backend (org-export-create-backend
3350 :transcoders
3351 '((pseudo-object . (lambda (_p c _i) c))
3352 (paragraph . (lambda (_p c _i) c))
3353 (plain-text . (lambda (c _i) c)))))
3354 (object '(pseudo-object (:post-blank 1) "x"))
3355 (paragraph '(paragraph nil)))
3356 (org-element-adopt-elements paragraph "begin " object "end")
3357 (org-export-data-with-backend paragraph backend nil)))))
3360 ;;; Src-block and example-block
3362 (ert-deftest test-org-export/unravel-code ()
3363 "Test `org-export-unravel-code' function."
3364 ;; Code without reference.
3365 (should
3366 (equal '("(+ 1 1)\n")
3367 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
3368 (org-export-unravel-code (org-element-at-point)))))
3369 ;; Code with reference.
3370 (should
3371 (equal '("(+ 1 1)\n" (1 . "test"))
3372 (org-test-with-temp-text
3373 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
3374 (let ((org-coderef-label-format "(ref:%s)"))
3375 (org-export-unravel-code (org-element-at-point))))))
3376 ;; Code with user-defined reference.
3377 (should
3378 (equal
3379 '("(+ 1 1)\n" (1 . "test"))
3380 (org-test-with-temp-text
3381 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
3382 (let ((org-coderef-label-format "(ref:%s)"))
3383 (org-export-unravel-code (org-element-at-point))))))
3384 ;; Code references keys are relative to the current block.
3385 (should
3386 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
3387 (org-test-with-temp-text "
3388 #+BEGIN_EXAMPLE -n
3389 \(+ 1 1)
3390 #+END_EXAMPLE
3391 #+BEGIN_EXAMPLE +n
3392 \(+ 2 2)
3393 \(+ 3 3) (ref:one)
3394 #+END_EXAMPLE"
3395 (goto-line 5)
3396 (let ((org-coderef-label-format "(ref:%s)"))
3397 (org-export-unravel-code (org-element-at-point)))))))
3399 (ert-deftest test-org-export/format-code-default ()
3400 "Test `org-export-format-code-default' specifications."
3401 ;; Return the empty string when code is empty.
3402 (should
3403 (equal ""
3404 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
3405 (org-export-format-code-default
3406 (org-element-map tree 'src-block 'identity info t) info))))
3407 ;; Number lines, two whitespace characters before the actual loc.
3408 (should
3409 (equal "1 a\n2 b\n"
3410 (org-test-with-parsed-data
3411 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
3412 (org-export-format-code-default
3413 (org-element-map tree 'src-block 'identity info t) info))))
3414 ;; Put references 6 whitespace characters after the widest line,
3415 ;; wrapped within parenthesis.
3416 (should
3417 (equal "123 (a)\n1 (b)\n"
3418 (let ((org-coderef-label-format "(ref:%s)"))
3419 (org-test-with-parsed-data
3420 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
3421 (org-export-format-code-default
3422 (org-element-map tree 'src-block 'identity info t) info))))))
3426 ;;; Smart Quotes
3428 (ert-deftest test-org-export/activate-smart-quotes ()
3429 "Test `org-export-activate-smart-quotes' specifications."
3430 ;; Double quotes: standard test.
3431 (should
3432 (equal
3433 '("some &ldquo;quoted&rdquo; text")
3434 (let ((org-export-default-language "en"))
3435 (org-test-with-parsed-data "some \"quoted\" text"
3436 (org-element-map tree 'plain-text
3437 (lambda (s) (org-export-activate-smart-quotes s :html info))
3438 info)))))
3439 ;; Opening quotes: at the beginning of a paragraph.
3440 (should
3441 (equal
3442 '("&ldquo;begin")
3443 (let ((org-export-default-language "en"))
3444 (org-test-with-parsed-data "\"begin"
3445 (org-element-map tree 'plain-text
3446 (lambda (s) (org-export-activate-smart-quotes s :html info))
3447 info)))))
3448 ;; Opening quotes: after an object.
3449 (should
3450 (equal
3451 '("&ldquo;quoted&rdquo; text")
3452 (let ((org-export-default-language "en"))
3453 (org-test-with-parsed-data "=verb= \"quoted\" text"
3454 (org-element-map tree 'plain-text
3455 (lambda (s) (org-export-activate-smart-quotes s :html info))
3456 info)))))
3457 ;; Closing quotes: at the end of a paragraph.
3458 (should
3459 (equal
3460 '("Quoted &ldquo;text&rdquo;")
3461 (let ((org-export-default-language "en"))
3462 (org-test-with-parsed-data "Quoted \"text\""
3463 (org-element-map tree 'plain-text
3464 (lambda (s) (org-export-activate-smart-quotes s :html info))
3465 info)))))
3466 ;; Inner quotes: standard test.
3467 (should
3468 (equal '("« outer « inner » outer »")
3469 (let ((org-export-default-language "fr"))
3470 (org-test-with-parsed-data "\"outer 'inner' outer\""
3471 (org-element-map tree 'plain-text
3472 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3473 info)))))
3474 ;; Inner quotes: close to special symbols.
3475 (should
3476 (equal '("« outer (« inner ») outer »")
3477 (let ((org-export-default-language "fr"))
3478 (org-test-with-parsed-data "\"outer ('inner') outer\""
3479 (org-element-map tree 'plain-text
3480 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3481 info)))))
3482 (should
3483 (equal '("« « inner » »")
3484 (let ((org-export-default-language "fr"))
3485 (org-test-with-parsed-data "\"'inner'\""
3486 (org-element-map tree 'plain-text
3487 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3488 info)))))
3489 ;; Apostrophe: standard test.
3490 (should
3491 (equal '("It « shouldn’t » fail")
3492 (let ((org-export-default-language "fr"))
3493 (org-test-with-parsed-data "It \"shouldn't\" fail"
3494 (org-element-map tree 'plain-text
3495 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3496 info)))))
3497 (should
3498 (equal '("It shouldn’t fail")
3499 (let ((org-export-default-language "fr"))
3500 (org-test-with-parsed-data "It shouldn't fail"
3501 (org-element-map tree 'plain-text
3502 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3503 info)))))
3504 ;; Apostrophe: before an object.
3505 (should
3506 (equal
3507 '("« a’" " »")
3508 (let ((org-export-default-language "fr"))
3509 (org-test-with-parsed-data "\"a'=b=\""
3510 (org-element-map tree 'plain-text
3511 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3512 info)))))
3513 ;; Apostrophe: after an object.
3514 (should
3515 (equal '("« " "’s »")
3516 (let ((org-export-default-language "fr"))
3517 (org-test-with-parsed-data "\"=code='s\""
3518 (org-element-map tree 'plain-text
3519 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3520 info)))))
3521 ;; Special case: isolated quotes.
3522 (should
3523 (equal '("&ldquo;" "&rdquo;")
3524 (let ((org-export-default-language "en"))
3525 (org-test-with-parsed-data "\"$x$\""
3526 (org-element-map tree 'plain-text
3527 (lambda (s) (org-export-activate-smart-quotes s :html info))
3528 info)))))
3529 ;; Smart quotes in secondary strings.
3530 (should
3531 (equal '("&ldquo;" "&rdquo;")
3532 (let ((org-export-default-language "en"))
3533 (org-test-with-parsed-data "* \"$x$\""
3534 (org-element-map tree 'plain-text
3535 (lambda (s) (org-export-activate-smart-quotes s :html info))
3536 info)))))
3537 ;; Smart quotes in document keywords.
3538 (should
3539 (equal '("&ldquo;" "&rdquo;")
3540 (let ((org-export-default-language "en"))
3541 (org-test-with-parsed-data "#+TITLE: \"$x$\""
3542 (org-element-map (plist-get info :title) 'plain-text
3543 (lambda (s) (org-export-activate-smart-quotes s :html info))
3544 info)))))
3545 ;; Smart quotes in parsed affiliated keywords.
3546 (should
3547 (equal '("&ldquo;" "&rdquo;" "Paragraph")
3548 (let ((org-export-default-language "en"))
3549 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
3550 (org-element-map tree 'plain-text
3551 (lambda (s) (org-export-activate-smart-quotes s :html info))
3552 info nil nil t)))))
3553 ;; Smart quotes within objects.
3554 (should
3555 (equal '("&ldquo;foo&rdquo;")
3556 (let ((org-export-default-language "en"))
3557 (org-test-with-parsed-data "| \"foo\" |"
3558 (org-element-map tree 'plain-text
3559 (lambda (s) (org-export-activate-smart-quotes s :html info))
3560 info nil nil t)))))
3561 ;; FIXME: Test failing non-interactively.
3563 ;; (should
3564 ;; (equal '("&ldquo;foo&rdquo;")
3565 ;; (let ((org-export-default-language "en"))
3566 ;; (org-test-with-parsed-data "*\"foo\"*"
3567 ;; (org-element-map tree 'plain-text
3568 ;; (lambda (s) (org-export-activate-smart-quotes s :html info))
3569 ;; info nil nil t)))))
3574 ;;; Tables
3576 (ert-deftest test-org-export/special-column ()
3577 "Test if the table's special column is properly recognized."
3578 ;; 1. First column is special if it contains only a special marking
3579 ;; characters or empty cells.
3580 (org-test-with-temp-text "
3581 | ! | 1 |
3582 | | 2 |"
3583 (should
3584 (org-export-table-has-special-column-p
3585 (org-element-map
3586 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3587 ;; 2. If the column contains anything else, it isn't special.
3588 (org-test-with-temp-text "
3589 | ! | 1 |
3590 | b | 2 |"
3591 (should-not
3592 (org-export-table-has-special-column-p
3593 (org-element-map
3594 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3595 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
3596 ;; and "!".
3597 (org-test-with-temp-text "
3598 | # | 1 |
3599 | ^ | 2 |
3600 | * | 3 |
3601 | _ | 4 |
3602 | / | 5 |
3603 | $ | 6 |
3604 | ! | 7 |"
3605 (should
3606 (org-export-table-has-special-column-p
3607 (org-element-map
3608 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3609 ;; 4. A first column with only empty cells isn't considered as
3610 ;; special.
3611 (org-test-with-temp-text "
3612 | | 1 |
3613 | | 2 |"
3614 (should-not
3615 (org-export-table-has-special-column-p
3616 (org-element-map
3617 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
3619 (ert-deftest test-org-export/table-row-is-special-p ()
3620 "Test `org-export-table-row-is-special-p' specifications."
3621 ;; 1. A row is special if it has a special marking character in the
3622 ;; special column.
3623 (org-test-with-parsed-data "| ! | 1 |"
3624 (should
3625 (org-export-table-row-is-special-p
3626 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3627 ;; 2. A row is special when its first field is "/"
3628 (org-test-with-parsed-data "
3629 | / | 1 |
3630 | a | b |"
3631 (should
3632 (org-export-table-row-is-special-p
3633 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3634 ;; 3. A row only containing alignment cookies is also considered as
3635 ;; special.
3636 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
3637 (should
3638 (org-export-table-row-is-special-p
3639 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3640 ;; 4. Everything else isn't considered as special.
3641 (org-test-with-parsed-data "| \alpha | | c |"
3642 (should-not
3643 (org-export-table-row-is-special-p
3644 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3645 ;; 5. Table's rules are never considered as special rows.
3646 (org-test-with-parsed-data "|---+---|"
3647 (should-not
3648 (org-export-table-row-is-special-p
3649 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
3651 (ert-deftest test-org-export/has-header-p ()
3652 "Test `org-export-table-has-header-p' specifications."
3653 ;; 1. With an header.
3654 (org-test-with-parsed-data "
3655 | a | b |
3656 |---+---|
3657 | c | d |"
3658 (should
3659 (org-export-table-has-header-p
3660 (org-element-map tree 'table 'identity info 'first-match)
3661 info)))
3662 ;; 2. Without an header.
3663 (org-test-with-parsed-data "
3664 | a | b |
3665 | c | d |"
3666 (should-not
3667 (org-export-table-has-header-p
3668 (org-element-map tree 'table 'identity info 'first-match)
3669 info)))
3670 ;; 3. Don't get fooled with starting and ending rules.
3671 (org-test-with-parsed-data "
3672 |---+---|
3673 | a | b |
3674 | c | d |
3675 |---+---|"
3676 (should-not
3677 (org-export-table-has-header-p
3678 (org-element-map tree 'table 'identity info 'first-match)
3679 info))))
3681 (ert-deftest test-org-export/table-row-group ()
3682 "Test `org-export-table-row-group' specifications."
3683 ;; 1. A rule creates a new group.
3684 (should
3685 (equal '(1 rule 2)
3686 (org-test-with-parsed-data "
3687 | a | b |
3688 |---+---|
3689 | 1 | 2 |"
3690 (org-element-map tree 'table-row
3691 (lambda (row)
3692 (if (eq (org-element-property :type row) 'rule) 'rule
3693 (org-export-table-row-group row info)))))))
3694 ;; 2. Special rows are ignored in count.
3695 (should
3696 (equal
3697 '(rule 1)
3698 (org-test-with-parsed-data "
3699 | / | < | > |
3700 |---|---+---|
3701 | | 1 | 2 |"
3702 (org-element-map tree 'table-row
3703 (lambda (row)
3704 (if (eq (org-element-property :type row) 'rule) 'rule
3705 (org-export-table-row-group row info)))
3706 info))))
3707 ;; 3. Double rules also are ignored in count.
3708 (should
3709 (equal '(1 rule rule 2)
3710 (org-test-with-parsed-data "
3711 | a | b |
3712 |---+---|
3713 |---+---|
3714 | 1 | 2 |"
3715 (org-element-map tree 'table-row
3716 (lambda (row)
3717 (if (eq (org-element-property :type row) 'rule) 'rule
3718 (org-export-table-row-group row info))))))))
3720 (ert-deftest test-org-export/table-row-number ()
3721 "Test `org-export-table-row-number' specifications."
3722 ;; Standard test. Number is 0-indexed.
3723 (should
3724 (equal '(0 1)
3725 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
3726 (org-element-map tree 'table-row
3727 (lambda (row) (org-export-table-row-number row info)) info))))
3728 ;; Number ignores separators.
3729 (should
3730 (equal '(0 1)
3731 (org-test-with-parsed-data "
3732 | a | b | c |
3733 |---+---+---|
3734 | d | e | f |"
3735 (org-element-map tree 'table-row
3736 (lambda (row) (org-export-table-row-number row info)) info))))
3737 ;; Number ignores special rows.
3738 (should
3739 (equal '(0 1)
3740 (org-test-with-parsed-data "
3741 | / | < | > |
3742 | | b | c |
3743 |---+-----+-----|
3744 | | <c> | <c> |
3745 | | e | f |"
3746 (org-element-map tree 'table-row
3747 (lambda (row) (org-export-table-row-number row info)) info)))))
3749 (ert-deftest test-org-export/table-cell-width ()
3750 "Test `org-export-table-cell-width' specifications."
3751 ;; 1. Width is primarily determined by width cookies. If no cookie
3752 ;; is found, cell's width is nil.
3753 (org-test-with-parsed-data "
3754 | / | <l> | <6> | <l7> |
3755 | | a | b | c |"
3756 (should
3757 (equal
3758 '(nil 6 7)
3759 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3760 (org-element-map tree 'table-cell 'identity info)))))
3761 ;; 2. The last width cookie has precedence.
3762 (org-test-with-parsed-data "
3763 | <6> |
3764 | <7> |
3765 | a |"
3766 (should
3767 (equal
3768 '(7)
3769 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3770 (org-element-map tree 'table-cell 'identity info)))))
3771 ;; 3. Valid width cookies must have a specific row.
3772 (org-test-with-parsed-data "| <6> | cell |"
3773 (should
3774 (equal
3775 '(nil nil)
3776 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3777 (org-element-map tree 'table-cell 'identity))))))
3779 (ert-deftest test-org-export/table-cell-alignment ()
3780 "Test `org-export-table-cell-alignment' specifications."
3781 ;; 1. Alignment is primarily determined by alignment cookies.
3782 (should
3783 (equal '(left center right)
3784 (let ((org-table-number-fraction 0.5)
3785 (org-table-number-regexp "^[0-9]+$"))
3786 (org-test-with-parsed-data "| <l> | <c> | <r> |"
3787 (mapcar (lambda (cell)
3788 (org-export-table-cell-alignment cell info))
3789 (org-element-map tree 'table-cell 'identity))))))
3790 ;; 2. The last alignment cookie has precedence.
3791 (should
3792 (equal '(right right right)
3793 (org-test-with-parsed-data "
3794 | <l8> |
3795 | cell |
3796 | <r9> |"
3797 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
3798 (org-element-map tree 'table-cell 'identity)))))
3799 ;; 3. If there's no cookie, cell's contents determine alignment.
3800 ;; A column mostly made of cells containing numbers will align
3801 ;; its cells to the right.
3802 (should
3803 (equal '(right right right)
3804 (let ((org-table-number-fraction 0.5)
3805 (org-table-number-regexp "^[0-9]+$"))
3806 (org-test-with-parsed-data "
3807 | 123 |
3808 | some text |
3809 | 12345 |"
3810 (mapcar (lambda (cell)
3811 (org-export-table-cell-alignment cell info))
3812 (org-element-map tree 'table-cell 'identity))))))
3813 ;; 4. Otherwise, they will be aligned to the left.
3814 (should
3815 (equal '(left left left)
3816 (org-test-with-parsed-data "
3817 | text |
3818 | some text |
3819 | \alpha |"
3820 (mapcar (lambda (cell)
3821 (org-export-table-cell-alignment cell info))
3822 (org-element-map tree 'table-cell 'identity info))))))
3824 (ert-deftest test-org-export/table-cell-borders ()
3825 "Test `org-export-table-cell-borders' specifications."
3826 ;; 1. Recognize various column groups indicators.
3827 (org-test-with-parsed-data "| / | < | > | <> |"
3828 (should
3829 (equal
3830 '((right bottom top) (left bottom top) (right bottom top)
3831 (right left bottom top))
3832 (mapcar (lambda (cell)
3833 (org-export-table-cell-borders cell info))
3834 (org-element-map tree 'table-cell 'identity)))))
3835 ;; 2. Accept shortcuts to define column groups.
3836 (org-test-with-parsed-data "| / | < | < |"
3837 (should
3838 (equal
3839 '((right bottom top) (right left bottom top) (left bottom top))
3840 (mapcar (lambda (cell)
3841 (org-export-table-cell-borders cell info))
3842 (org-element-map tree 'table-cell 'identity)))))
3843 ;; 3. A valid column groups row must start with a "/".
3844 (org-test-with-parsed-data "
3845 | | < |
3846 | a | b |"
3847 (should
3848 (equal '((top) (top) (bottom) (bottom))
3849 (mapcar (lambda (cell)
3850 (org-export-table-cell-borders cell info))
3851 (org-element-map tree 'table-cell 'identity)))))
3852 ;; 4. Take table rules into consideration.
3853 (org-test-with-parsed-data "
3854 | 1 |
3855 |---|
3856 | 2 |"
3857 (should
3858 (equal '((below top) (bottom above))
3859 (mapcar (lambda (cell)
3860 (org-export-table-cell-borders cell info))
3861 (org-element-map tree 'table-cell 'identity)))))
3862 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
3863 ;; (resp. `bottom' and `below') borders. Any special row is
3864 ;; ignored.
3865 (org-test-with-parsed-data "
3866 |---+----|
3867 | / | |
3868 | | 1 |
3869 |---+----|"
3870 (should
3871 (equal '((bottom below top above))
3872 (last
3873 (mapcar (lambda (cell)
3874 (org-export-table-cell-borders cell info))
3875 (org-element-map tree 'table-cell 'identity)))))))
3877 (ert-deftest test-org-export/table-dimensions ()
3878 "Test `org-export-table-dimensions' specifications."
3879 ;; 1. Standard test.
3880 (org-test-with-parsed-data "
3881 | 1 | 2 | 3 |
3882 | 4 | 5 | 6 |"
3883 (should
3884 (equal '(2 . 3)
3885 (org-export-table-dimensions
3886 (org-element-map tree 'table 'identity info 'first-match) info))))
3887 ;; 2. Ignore horizontal rules and special columns.
3888 (org-test-with-parsed-data "
3889 | / | < | > |
3890 | 1 | 2 | 3 |
3891 |---+---+---|
3892 | 4 | 5 | 6 |"
3893 (should
3894 (equal '(2 . 3)
3895 (org-export-table-dimensions
3896 (org-element-map tree 'table 'identity info 'first-match) info)))))
3898 (ert-deftest test-org-export/table-cell-address ()
3899 "Test `org-export-table-cell-address' specifications."
3900 ;; 1. Standard test: index is 0-based.
3901 (org-test-with-parsed-data "| a | b |"
3902 (should
3903 (equal '((0 . 0) (0 . 1))
3904 (org-element-map tree 'table-cell
3905 (lambda (cell) (org-export-table-cell-address cell info))
3906 info))))
3907 ;; 2. Special column isn't counted, nor are special rows.
3908 (org-test-with-parsed-data "
3909 | / | <> |
3910 | | c |"
3911 (should
3912 (equal '(0 . 0)
3913 (org-export-table-cell-address
3914 (car (last (org-element-map tree 'table-cell 'identity info)))
3915 info))))
3916 ;; 3. Tables rules do not count either.
3917 (org-test-with-parsed-data "
3918 | a |
3919 |---|
3920 | b |
3921 |---|
3922 | c |"
3923 (should
3924 (equal '(2 . 0)
3925 (org-export-table-cell-address
3926 (car (last (org-element-map tree 'table-cell 'identity info)))
3927 info))))
3928 ;; 4. Return nil for special cells.
3929 (org-test-with-parsed-data "| / | a |"
3930 (should-not
3931 (org-export-table-cell-address
3932 (org-element-map tree 'table-cell 'identity nil 'first-match)
3933 info))))
3935 (ert-deftest test-org-export/get-table-cell-at ()
3936 "Test `org-export-get-table-cell-at' specifications."
3937 ;; 1. Address ignores special columns, special rows and rules.
3938 (org-test-with-parsed-data "
3939 | / | <> |
3940 | | a |
3941 |---+----|
3942 | | b |"
3943 (should
3944 (equal '("b")
3945 (org-element-contents
3946 (org-export-get-table-cell-at
3947 '(1 . 0)
3948 (org-element-map tree 'table 'identity info 'first-match)
3949 info)))))
3950 ;; 2. Return value for a non-existent address is nil.
3951 (org-test-with-parsed-data "| a |"
3952 (should-not
3953 (org-export-get-table-cell-at
3954 '(2 . 2)
3955 (org-element-map tree 'table 'identity info 'first-match)
3956 info)))
3957 (org-test-with-parsed-data "| / |"
3958 (should-not
3959 (org-export-get-table-cell-at
3960 '(0 . 0)
3961 (org-element-map tree 'table 'identity info 'first-match)
3962 info))))
3964 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
3965 "Test `org-export-table-cell-starts-colgroup-p' specifications."
3966 ;; 1. A cell at a beginning of a row always starts a column group.
3967 (org-test-with-parsed-data "| a |"
3968 (should
3969 (org-export-table-cell-starts-colgroup-p
3970 (org-element-map tree 'table-cell 'identity info 'first-match)
3971 info)))
3972 ;; 2. Special column should be ignored when determining the
3973 ;; beginning of the row.
3974 (org-test-with-parsed-data "
3975 | / | |
3976 | | a |"
3977 (should
3978 (org-export-table-cell-starts-colgroup-p
3979 (org-element-map tree 'table-cell 'identity info 'first-match)
3980 info)))
3981 ;; 2. Explicit column groups.
3982 (org-test-with-parsed-data "
3983 | / | | < |
3984 | a | b | c |"
3985 (should
3986 (equal
3987 '(yes no yes)
3988 (org-element-map tree 'table-cell
3989 (lambda (cell)
3990 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
3991 info)))))
3993 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
3994 "Test `org-export-table-cell-ends-colgroup-p' specifications."
3995 ;; 1. A cell at the end of a row always ends a column group.
3996 (org-test-with-parsed-data "| a |"
3997 (should
3998 (org-export-table-cell-ends-colgroup-p
3999 (org-element-map tree 'table-cell 'identity info 'first-match)
4000 info)))
4001 ;; 2. Special column should be ignored when determining the
4002 ;; beginning of the row.
4003 (org-test-with-parsed-data "
4004 | / | |
4005 | | a |"
4006 (should
4007 (org-export-table-cell-ends-colgroup-p
4008 (org-element-map tree 'table-cell 'identity info 'first-match)
4009 info)))
4010 ;; 3. Explicit column groups.
4011 (org-test-with-parsed-data "
4012 | / | < | |
4013 | a | b | c |"
4014 (should
4015 (equal
4016 '(yes no yes)
4017 (org-element-map tree 'table-cell
4018 (lambda (cell)
4019 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
4020 info)))))
4022 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
4023 "Test `org-export-table-row-starts-rowgroup-p' specifications."
4024 ;; 1. A row at the beginning of a table always starts a row group.
4025 ;; So does a row following a table rule.
4026 (org-test-with-parsed-data "
4027 | a |
4028 |---|
4029 | b |"
4030 (should
4031 (equal
4032 '(yes no yes)
4033 (org-element-map tree 'table-row
4034 (lambda (row)
4035 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
4036 info))))
4037 ;; 2. Special rows should be ignored when determining the beginning
4038 ;; of the row.
4039 (org-test-with-parsed-data "
4040 | / | < |
4041 | | a |
4042 |---+---|
4043 | / | < |
4044 | | b |"
4045 (should
4046 (equal
4047 '(yes no yes)
4048 (org-element-map tree 'table-row
4049 (lambda (row)
4050 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
4051 info)))))
4053 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
4054 "Test `org-export-table-row-ends-rowgroup-p' specifications."
4055 ;; 1. A row at the end of a table always ends a row group. So does
4056 ;; a row preceding a table rule.
4057 (org-test-with-parsed-data "
4058 | a |
4059 |---|
4060 | b |"
4061 (should
4062 (equal
4063 '(yes no yes)
4064 (org-element-map tree 'table-row
4065 (lambda (row)
4066 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
4067 info))))
4068 ;; 2. Special rows should be ignored when determining the beginning
4069 ;; of the row.
4070 (org-test-with-parsed-data "
4071 | | a |
4072 | / | < |
4073 |---+---|
4074 | | b |
4075 | / | < |"
4076 (should
4077 (equal
4078 '(yes no yes)
4079 (org-element-map tree 'table-row
4080 (lambda (row)
4081 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
4082 info)))))
4084 (ert-deftest test-org-export/table-row-in-header-p ()
4085 "Test `org-export-table-row-in-header-p' specifications."
4086 ;; Standard test. Separators are always nil.
4087 (should
4088 (equal
4089 '(yes no no)
4090 (org-test-with-parsed-data "| a |\n|---|\n| b |"
4091 (org-element-map tree 'table-row
4092 (lambda (row)
4093 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
4094 ;; Nil when there is no header.
4095 (should
4096 (equal
4097 '(no no)
4098 (org-test-with-parsed-data "| a |\n| b |"
4099 (org-element-map tree 'table-row
4100 (lambda (row)
4101 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
4103 (ert-deftest test-org-export/table-row-starts-header-p ()
4104 "Test `org-export-table-row-starts-header-p' specifications."
4105 ;; 1. Only the row starting the first row group starts the table
4106 ;; header.
4107 (org-test-with-parsed-data "
4108 | a |
4109 | b |
4110 |---|
4111 | c |"
4112 (should
4113 (equal
4114 '(yes no no no)
4115 (org-element-map tree 'table-row
4116 (lambda (row)
4117 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
4118 info))))
4119 ;; 2. A row cannot start an header if there's no header in the
4120 ;; table.
4121 (org-test-with-parsed-data "
4122 | a |
4123 |---|"
4124 (should-not
4125 (org-export-table-row-starts-header-p
4126 (org-element-map tree 'table-row 'identity info 'first-match)
4127 info))))
4129 (ert-deftest test-org-export/table-row-ends-header-p ()
4130 "Test `org-export-table-row-ends-header-p' specifications."
4131 ;; 1. Only the row starting the first row group starts the table
4132 ;; header.
4133 (org-test-with-parsed-data "
4134 | a |
4135 | b |
4136 |---|
4137 | c |"
4138 (should
4139 (equal
4140 '(no yes no no)
4141 (org-element-map tree 'table-row
4142 (lambda (row)
4143 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
4144 info))))
4145 ;; 2. A row cannot start an header if there's no header in the
4146 ;; table.
4147 (org-test-with-parsed-data "
4148 | a |
4149 |---|"
4150 (should-not
4151 (org-export-table-row-ends-header-p
4152 (org-element-map tree 'table-row 'identity info 'first-match)
4153 info))))
4157 ;;; Tables of Contents
4159 (ert-deftest test-org-export/collect-headlines ()
4160 "Test `org-export-collect-headlines' specifications."
4161 ;; Standard test.
4162 (should
4163 (= 2
4164 (length
4165 (org-test-with-parsed-data "* H1\n** H2"
4166 (org-export-collect-headlines info)))))
4167 ;; Do not collect headlines below optional argument.
4168 (should
4169 (= 1
4170 (length
4171 (org-test-with-parsed-data "* H1\n** H2"
4172 (org-export-collect-headlines info 1)))))
4173 ;; Never collect headlines below maximum headline level.
4174 (should
4175 (= 1
4176 (length
4177 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4178 (org-export-collect-headlines info)))))
4179 (should
4180 (= 1
4181 (length
4182 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4183 (org-export-collect-headlines info 2)))))
4184 ;; Collect headlines locally.
4185 (should
4186 (= 2
4187 (org-test-with-parsed-data "* H1\n** H2\n** H3"
4188 (let ((scope (org-element-map tree 'headline #'identity info t)))
4189 (length (org-export-collect-headlines info nil scope))))))
4190 ;; When collecting locally, optional level is relative.
4191 (should
4192 (= 1
4193 (org-test-with-parsed-data "* H1\n** H2\n*** H3"
4194 (let ((scope (org-element-map tree 'headline #'identity info t)))
4195 (length (org-export-collect-headlines info 1 scope)))))))
4199 ;;; Templates
4201 (ert-deftest test-org-export/inner-template ()
4202 "Test `inner-template' translator specifications."
4203 (should
4204 (equal "Success!"
4205 (org-test-with-temp-text "* Headline"
4206 (org-export-as
4207 (org-export-create-backend
4208 :transcoders
4209 '((inner-template . (lambda (contents info) "Success!"))
4210 (headline . (lambda (h c i) "Headline"))))))))
4211 ;; Inner template is applied even in a "body-only" export.
4212 (should
4213 (equal "Success!"
4214 (org-test-with-temp-text "* Headline"
4215 (org-export-as
4216 (org-export-create-backend
4217 :transcoders '((inner-template . (lambda (c i) "Success!"))
4218 (headline . (lambda (h c i) "Headline"))))
4219 nil nil 'body-only)))))
4221 (ert-deftest test-org-export/template ()
4222 "Test `template' translator specifications."
4223 (should
4224 (equal "Success!"
4225 (org-test-with-temp-text "* Headline"
4226 (org-export-as
4227 (org-export-create-backend
4228 :transcoders '((template . (lambda (contents info) "Success!"))
4229 (headline . (lambda (h c i) "Headline"))))))))
4230 ;; Template is not applied in a "body-only" export.
4231 (should-not
4232 (equal "Success!"
4233 (org-test-with-temp-text "* Headline"
4234 (org-export-as
4235 (org-export-create-backend
4236 :transcoders '((template . (lambda (contents info) "Success!"))
4237 (headline . (lambda (h c i) "Headline"))))
4238 nil nil 'body-only)))))
4242 ;;; Topology
4244 (ert-deftest test-org-export/get-next-element ()
4245 "Test `org-export-get-next-element' specifications."
4246 ;; Standard test.
4247 (should
4248 (equal "b"
4249 (org-test-with-parsed-data "* Headline\n*a* b"
4250 (org-export-get-next-element
4251 (org-element-map tree 'bold 'identity info t) info))))
4252 ;; Return nil when no previous element.
4253 (should-not
4254 (org-test-with-parsed-data "* Headline\na *b*"
4255 (org-export-get-next-element
4256 (org-element-map tree 'bold 'identity info t) info)))
4257 ;; Non-exportable elements are ignored.
4258 (should-not
4259 (let ((org-export-with-timestamps nil))
4260 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
4261 (org-export-get-next-element
4262 (org-element-map tree 'entity 'identity info t) info))))
4263 ;; Find next element in secondary strings.
4264 (should
4265 (eq 'verbatim
4266 (org-test-with-parsed-data "* a =verb="
4267 (org-element-type
4268 (org-export-get-next-element
4269 (org-element-map tree 'plain-text 'identity info t) info)))))
4270 (should
4271 (eq 'verbatim
4272 (org-test-with-parsed-data "* /italic/ =verb="
4273 (org-element-type
4274 (org-export-get-next-element
4275 (org-element-map tree 'italic 'identity info t) info)))))
4276 ;; Find next element in document keywords.
4277 (should
4278 (eq 'verbatim
4279 (org-test-with-parsed-data "#+TITLE: a =verb="
4280 (org-element-type
4281 (org-export-get-next-element
4282 (org-element-map
4283 (plist-get info :title) 'plain-text 'identity info t) info)))))
4284 ;; Find next element in parsed affiliated keywords.
4285 (should
4286 (eq 'verbatim
4287 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
4288 (org-element-type
4289 (org-export-get-next-element
4290 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4291 ;; With optional argument N, return a list containing all the
4292 ;; following elements.
4293 (should
4294 (equal
4295 '(bold code underline)
4296 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4297 (mapcar 'car
4298 (org-export-get-next-element
4299 (org-element-map tree 'italic 'identity info t) info t)))))
4300 ;; When N is a positive integer, return a list containing up to
4301 ;; N following elements.
4302 (should
4303 (equal
4304 '(bold code)
4305 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4306 (mapcar 'car
4307 (org-export-get-next-element
4308 (org-element-map tree 'italic 'identity info t) info 2))))))
4310 (ert-deftest test-org-export/get-previous-element ()
4311 "Test `org-export-get-previous-element' specifications."
4312 ;; Standard test.
4313 (should
4314 (equal "a "
4315 (org-test-with-parsed-data "* Headline\na *b*"
4316 (org-export-get-previous-element
4317 (org-element-map tree 'bold 'identity info t) info))))
4318 ;; Return nil when no previous element.
4319 (should-not
4320 (org-test-with-parsed-data "* Headline\n*a* b"
4321 (org-export-get-previous-element
4322 (org-element-map tree 'bold 'identity info t) info)))
4323 ;; Non-exportable elements are ignored.
4324 (should-not
4325 (let ((org-export-with-timestamps nil))
4326 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
4327 (org-export-get-previous-element
4328 (org-element-map tree 'entity 'identity info t) info))))
4329 ;; Find previous element in secondary strings.
4330 (should
4331 (eq 'verbatim
4332 (org-test-with-parsed-data "* =verb= a"
4333 (org-element-type
4334 (org-export-get-previous-element
4335 (org-element-map tree 'plain-text 'identity info t) info)))))
4336 (should
4337 (eq 'verbatim
4338 (org-test-with-parsed-data "* =verb= /italic/"
4339 (org-element-type
4340 (org-export-get-previous-element
4341 (org-element-map tree 'italic 'identity info t) info)))))
4342 ;; Find previous element in document keywords.
4343 (should
4344 (eq 'verbatim
4345 (org-test-with-parsed-data "#+TITLE: =verb= a"
4346 (org-element-type
4347 (org-export-get-previous-element
4348 (org-element-map
4349 (plist-get info :title) 'plain-text 'identity info t) info)))))
4350 ;; Find previous element in parsed affiliated keywords.
4351 (should
4352 (eq 'verbatim
4353 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
4354 (org-element-type
4355 (org-export-get-previous-element
4356 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4357 ;; With optional argument N, return a list containing up to
4358 ;; N previous elements.
4359 (should
4360 (equal '(underline italic bold)
4361 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4362 (mapcar 'car
4363 (org-export-get-previous-element
4364 (org-element-map tree 'code 'identity info t) info t)))))
4365 ;; When N is a positive integer, return a list containing up to
4366 ;; N previous elements.
4367 (should
4368 (equal '(italic bold)
4369 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4370 (mapcar 'car
4371 (org-export-get-previous-element
4372 (org-element-map tree 'code 'identity info t) info 2))))))
4375 (provide 'test-ox)
4376 ;;; test-org-export.el end here