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