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