Merge branch 'maint'
[org-mode/org-tableheadings.git] / testing / lisp / test-ox.el
blob13f406d7981f021b5677d1692bcee2df74b608e5
1 ;;; test-ox.el --- Tests for ox.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2016 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
7 ;; This file is not part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Code:
24 (require 'cl-lib)
26 (unless (featurep 'ox)
27 (signal 'missing-test-dependency "org-export"))
29 (defun org-test-default-backend ()
30 "Return a default export back-end.
31 This back-end simply returns parsed data as Org syntax."
32 (org-export-create-backend
33 :transcoders
34 (mapcar (lambda (type)
35 (cons type
36 (lambda (o c _)
37 (funcall
38 (intern (format "org-element-%s-interpreter" type))
39 o c))))
40 (append org-element-all-elements org-element-all-objects))))
42 (defmacro org-test-with-parsed-data (data &rest body)
43 "Execute body with parsed data available.
44 DATA is a string containing the data to be parsed. BODY is the
45 body to execute. Parse tree is available under the `tree'
46 variable, and communication channel under `info'."
47 (declare (debug (form body)) (indent 1))
48 `(org-test-with-temp-text ,data
49 (org-export--delete-comments)
50 (let* ((tree (org-element-parse-buffer))
51 (info (org-combine-plists
52 (org-export--get-export-attributes)
53 (org-export-get-environment))))
54 (org-export--prune-tree tree info)
55 (org-export--remove-uninterpreted-data tree info)
56 (let ((info (org-combine-plists
57 info (org-export--collect-tree-properties tree info))))
58 ,@body))))
62 ;;; Internal Tests
64 (ert-deftest test-org-export/bind-keyword ()
65 "Test reading #+BIND: keywords."
66 ;; Test with `org-export-allow-bind-keywords' set to t.
67 (should
68 (org-test-with-temp-text "#+BIND: test-ox-var value"
69 (let ((org-export-allow-bind-keywords t))
70 (org-export-get-environment)
71 (eq test-ox-var 'value))))
72 ;; Test with `org-export-allow-bind-keywords' set to nil.
73 (should-not
74 (org-test-with-temp-text "#+BIND: test-ox-var value"
75 (let ((org-export-allow-bind-keywords nil))
76 (org-export-get-environment)
77 (boundp 'test-ox-var))))
78 ;; BIND keywords are case-insensitive.
79 (should
80 (org-test-with-temp-text "#+bind: test-ox-var value"
81 (let ((org-export-allow-bind-keywords t))
82 (org-export-get-environment)
83 (eq test-ox-var 'value))))
84 ;; Preserve order of BIND keywords.
85 (should
86 (org-test-with-temp-text "#+BIND: test-ox-var 1\n#+BIND: test-ox-var 2"
87 (let ((org-export-allow-bind-keywords t))
88 (org-export-get-environment)
89 (eq test-ox-var 2))))
90 ;; Read BIND keywords in setup files.
91 (should
92 (org-test-with-temp-text
93 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
94 (let ((org-export-allow-bind-keywords t))
95 (org-export-get-environment)
96 (eq variable 'value))))
97 ;; Verify that bound variables are seen during export.
98 (should
99 (equal "Yes\n"
100 (org-test-with-temp-text "#+BIND: test-ox-var value"
101 (let ((org-export-allow-bind-keywords t))
102 (org-export-as
103 (org-export-create-backend
104 :transcoders
105 '((section . (lambda (s c i)
106 (if (eq test-ox-var 'value) "Yes" "No")))))))))))
108 (ert-deftest test-org-export/parse-option-keyword ()
109 "Test reading all standard #+OPTIONS: items."
110 (should
111 (let ((options
112 (org-export--parse-option-keyword
113 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t \
114 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil \
115 stat:t title:t")))
116 (and (eq (plist-get options :headline-levels) 1)
117 (eq (plist-get options :section-numbers) t)
118 (eq (plist-get options :preserve-breaks) t)
119 (eq (plist-get options :time-stamp-file) t)
120 (eq (plist-get options :with-archived-trees) t)
121 (eq (plist-get options :with-author) t)
122 (eq (plist-get options :with-drawers) t)
123 (eq (plist-get options :with-email) t)
124 (eq (plist-get options :with-emphasize) t)
125 (eq (plist-get options :with-entities) t)
126 (eq (plist-get options :with-fixed-width) t)
127 (eq (plist-get options :with-footnotes) t)
128 (eq (plist-get options :with-priority) t)
129 (eq (plist-get options :with-special-strings) t)
130 (eq (plist-get options :with-sub-superscript) t)
131 (eq (plist-get options :with-toc) t)
132 (eq (plist-get options :with-tables) t)
133 (eq (plist-get options :with-tags) t)
134 (eq (plist-get options :with-tasks) t)
135 (eq (plist-get options :with-timestamps) t)
136 (eq (plist-get options :with-todo-keywords) t)
137 (eq (plist-get options :with-inlinetasks) nil)
138 (eq (plist-get options :with-statistics-cookies) t)
139 (eq (plist-get options :with-title) t))))
140 ;; Test some special values.
141 (should
142 (let ((options
143 (org-export--parse-option-keyword
144 "arch:headline d:(\"TEST\") ^:{} toc:1 tags:not-in-toc tasks:todo \
145 num:2 <:active")))
146 (and (eq (plist-get options :with-archived-trees) 'headline)
147 (eq (plist-get options :with-sub-superscript) '{})
148 (eq (plist-get options :with-toc) 1)
149 (eq (plist-get options :with-tags) 'not-in-toc)
150 (eq (plist-get options :with-tasks) 'todo)
151 (eq (plist-get options :section-numbers) 2)
152 (eq (plist-get options :with-timestamps) 'active)
153 (equal (plist-get options :with-drawers) '("TEST")))))
154 ;; Test back-end specific values.
155 (should
156 (equal
157 (org-export--parse-option-keyword
158 "opt:t" (org-export-create-backend :options '((:option nil "opt"))))
159 '(:option t)))
160 ;; More than one property can refer to the same option item.
161 (should
162 (equal '(:opt1 t :opt2 t)
163 (org-export--parse-option-keyword
164 "opt:t"
165 (org-export-create-backend
166 :options '((:opt1 nil "opt") (:opt2 nil "opt")))))))
168 (ert-deftest test-org-export/get-inbuffer-options ()
169 "Test reading all standard export keywords."
170 ;; Properties should follow buffer order.
171 (should
172 (equal
173 (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
174 (org-export--get-inbuffer-options))
175 '(:language "fr" :creator "Me" :email "email")))
176 ;; Test `space' behaviour.
177 (should
178 (equal
179 (let ((back-end (org-export-create-backend
180 :options '((:keyword "KEYWORD" nil nil space)))))
181 (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: spaces"
182 (org-export--get-inbuffer-options back-end)))
183 '(:keyword "With spaces")))
184 ;; Test `newline' behaviour.
185 (should
186 (equal
187 (let ((back-end (org-export-create-backend
188 :options '((:keyword "KEYWORD" nil nil newline)))))
189 (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: two lines"
190 (org-export--get-inbuffer-options back-end)))
191 '(:keyword "With\ntwo lines")))
192 ;; Test `split' behaviour.
193 (should
194 (equal
195 (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
196 (org-export--get-inbuffer-options))
197 '(:select-tags ("a" "b"))))
198 ;; Test `parse' behaviour. `parse' implies `space' but preserve
199 ;; line breaks. Multi-line objects are allowed.
200 (should
201 (org-element-map
202 (org-test-with-temp-text "#+TITLE: *bold*"
203 (plist-get (org-export--get-inbuffer-options) :title))
204 'bold #'identity nil t))
205 (should
206 (equal
207 (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
208 (plist-get (org-export--get-inbuffer-options) :title))
209 '("Some title with spaces")))
210 (should
211 (org-element-map
212 (org-test-with-temp-text "#+TITLE: Some title\\\\\n#+TITLE: with spaces"
213 (plist-get (org-export--get-inbuffer-options) :title))
214 'line-break #'identity nil t))
215 (should
216 (org-element-map
217 (org-test-with-temp-text "#+TITLE: *bold\n#+TITLE: sentence*"
218 (plist-get (org-export--get-inbuffer-options) :title))
219 'bold #'identity nil t))
220 ;; Options set through SETUPFILE.
221 (should
222 (equal
223 (org-test-with-temp-text
224 (format "#+DESCRIPTION: l1
225 #+LANGUAGE: es
226 #+SELECT_TAGS: a
227 #+TITLE: a
228 #+SETUPFILE: \"%s/examples/setupfile.org\"
229 #+LANGUAGE: fr
230 #+SELECT_TAGS: c
231 #+TITLE: c"
232 org-test-dir)
233 (org-export--get-inbuffer-options))
234 '(:language "fr" :select-tags ("a" "b" "c") :title ("a b c"))))
235 ;; More than one property can refer to the same buffer keyword.
236 (should
237 (equal '(:k2 "value" :k1 "value")
238 (let ((backend (org-export-create-backend
239 :options '((:k1 "KEYWORD")
240 (:k2 "KEYWORD")))))
241 (org-test-with-temp-text "#+KEYWORD: value"
242 (org-export--get-inbuffer-options backend)))))
243 ;; Keywords in commented subtrees are ignored.
244 (should-not
245 (equal "Me"
246 (org-test-with-parsed-data "* COMMENT H1\n#+AUTHOR: Me"
247 (plist-get info :author))))
248 (should-not
249 (equal "Mine"
250 (org-test-with-parsed-data "* COMMENT H1\n** H2\n#+EMAIL: Mine"
251 (plist-get info :email)))))
253 (ert-deftest test-org-export/get-subtree-options ()
254 "Test setting options from headline's properties."
255 ;; EXPORT_TITLE.
256 (should
257 (equal '("Subtree Title")
258 (org-test-with-temp-text "#+TITLE: Title
259 * Headline<point>
260 :PROPERTIES:
261 :EXPORT_TITLE: Subtree Title
262 :END:
263 Paragraph"
264 (plist-get (org-export-get-environment nil t) :title))))
265 ;; EXPORT_OPTIONS.
266 (should
267 (= 2
268 (org-test-with-temp-text "#+OPTIONS: H:1
269 * Headline<point>
270 :PROPERTIES:
271 :EXPORT_OPTIONS: H:2
272 :END:
273 Paragraph"
274 (plist-get (org-export-get-environment nil t) :headline-levels))))
275 ;; EXPORT_DATE.
276 (should
277 (equal '("29-03-2012")
278 (org-test-with-temp-text "#+DATE: today
279 * Headline<point>
280 :PROPERTIES:
281 :EXPORT_DATE: 29-03-2012
282 :END:
283 Paragraph"
284 (plist-get (org-export-get-environment nil t) :date))))
285 ;; Properties with `split' behaviour are stored as a list of
286 ;; strings.
287 (should
288 (equal '("a" "b")
289 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
290 * Headline<point>
291 :PROPERTIES:
292 :EXPORT_EXCLUDE_TAGS: a b
293 :END:
294 Paragraph"
295 (plist-get (org-export-get-environment nil t) :exclude-tags))))
296 ;; Handle :PROPERTY+: syntax.
297 (should
298 (equal '("a" "b")
299 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
300 * Headline<point>
301 :PROPERTIES:
302 :EXPORT_EXCLUDE_TAGS: a
303 :EXPORT_EXCLUDE_TAGS+: b
304 :END:
305 Paragraph"
306 (plist-get (org-export-get-environment nil t) :exclude-tags))))
307 ;; Export properties are case-insensitive.
308 (should
309 (equal '("29-03-2012")
310 (org-test-with-temp-text "* Headline
311 :PROPERTIES:
312 :EXPORT_Date: 29-03-2012
313 :END:
314 Paragraph"
315 (plist-get (org-export-get-environment nil t) :date))))
316 ;; Still grab correct options when section above is empty.
317 (should
318 (equal '("H1")
319 (org-test-with-temp-text "* H1\n** H11\n** H12<point>"
320 (plist-get (org-export-get-environment nil t) :title))))
321 ;; More than one property can refer to the same node property.
322 (should
323 (equal '("1" "1")
324 (org-test-with-temp-text
325 "* H\n:PROPERTIES:\n:EXPORT_A: 1\n:END:\n<point>"
326 (let* ((backend (org-export-create-backend
327 :options '((:k1 "A")
328 (:k2 "A"))))
329 (options (org-export-get-environment backend t)))
330 (list (plist-get options :k1) (plist-get options :k2)))))))
332 (ert-deftest test-org-export/set-title ()
333 "Test title setting."
334 ;; Without TITLE keyword.
335 (should
336 (equal
338 (let (org-export-filter-body-functions
339 org-export-filter-final-output-functions)
340 (org-test-with-temp-text "Test"
341 (org-export-as
342 (org-export-create-backend
343 :transcoders
344 '((template . (lambda (text info)
345 (org-element-interpret-data
346 (plist-get info :title)))))))))))
347 ;; With a blank TITLE keyword.
348 (should
349 (equal
351 (let (org-export-filter-body-functions
352 org-export-filter-final-output-functions)
353 (org-test-with-temp-text "#+TITLE:\nTest"
354 (org-export-as
355 (org-export-create-backend
356 :transcoders
357 '((template . (lambda (text info)
358 (org-element-interpret-data
359 (plist-get info :title)))))))))))
360 ;; With a non-empty TITLE keyword.
361 (should
362 (equal
363 "Title"
364 (org-test-with-temp-text "#+TITLE: Title\nTest"
365 (org-export-as
366 (org-export-create-backend
367 :transcoders
368 '((template . (lambda (text info)
369 (org-element-interpret-data
370 (plist-get info :title))))))))))
371 ;; When exporting a subtree, its heading becomes the headline of the
372 ;; document...
373 (should
374 (equal
375 "Headline"
376 (org-test-with-temp-text "* Headline\nBody"
377 (org-export-as
378 (org-export-create-backend
379 :transcoders
380 '((template . (lambda (text info)
381 (org-element-interpret-data
382 (plist-get info :title))))))
383 'subtree))))
384 ;; ... unless there is an EXPORT_TITLE property at the root of the
385 ;; subtree.
386 (should
387 (equal
389 (org-test-with-temp-text
390 "* A\n :PROPERTIES:\n :EXPORT_TITLE: B\n :END:\nBody"
391 (org-export-as
392 (org-export-create-backend
393 :transcoders
394 '((template . (lambda (text info)
395 (org-element-interpret-data
396 (plist-get info :title))))))
397 'subtree)))))
399 (ert-deftest test-org-export/handle-options ()
400 "Test if export options have an impact on output."
401 ;; Test exclude tags for headlines and inlinetasks.
402 (should
403 (equal ""
404 (let (org-export-filter-body-functions
405 org-export-filter-final-output-functions)
406 (org-test-with-temp-text "* Head1 :noexp:"
407 (org-export-as (org-test-default-backend)
408 nil nil nil '(:exclude-tags ("noexp")))))))
409 (should
410 (equal "#+FILETAGS: noexp\n"
411 (let (org-export-filter-body-functions
412 org-export-filter-final-output-functions)
413 (org-test-with-temp-text "#+FILETAGS: noexp\n* Head1"
414 (org-export-as (org-test-default-backend)
415 nil nil nil '(:exclude-tags ("noexp")))))))
416 ;; Test include tags for headlines and inlinetasks.
417 (should
418 (equal (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
419 (let ((org-tags-column 0))
420 (org-export-as (org-test-default-backend)
421 nil nil nil '(:select-tags ("exp")))))
422 "* H2\n** Sub :exp:\n*** Sub Sub\n"))
423 ;; If there is an include tag, ignore the section before the first
424 ;; headline, if any.
425 (should
426 (equal (org-test-with-temp-text "First section\n* H1 :exp:\nBody"
427 (let ((org-tags-column 0))
428 (org-export-as (org-test-default-backend)
429 nil nil nil '(:select-tags ("exp")))))
430 "* H1 :exp:\nBody\n"))
431 (should
432 (equal (org-test-with-temp-text "#+FILETAGS: exp\nFirst section\n* H1\nBody"
433 (org-export-as (org-test-default-backend)
434 nil nil nil '(:select-tags ("exp"))))
435 "* H1\nBody\n"))
436 (should-not
437 (equal (org-test-with-temp-text "* H1 :exp:\nBody"
438 (let ((org-tags-column 0))
439 (org-export-as (org-test-default-backend)
440 nil nil nil '(:select-tags ("exp")))))
441 "* H1 :exp:\n"))
442 ;; Test mixing include tags and exclude tags.
443 (should
444 (string-match
445 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
446 (org-test-with-temp-text "
447 * Head1 :export:
448 ** Sub-Head1 :noexport:
449 ** Sub-Head2
450 * Head2 :noexport:
451 ** Sub-Head1 :export:"
452 (org-export-as (org-test-default-backend) nil nil nil
453 '(:select-tags ("export") :exclude-tags ("noexport"))))))
454 ;; Ignore tasks.
455 (should
456 (equal ""
457 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
458 org-export-filter-body-functions
459 org-export-filter-final-output-functions)
460 (org-test-with-temp-text "* TODO Head1"
461 (org-export-as (org-test-default-backend)
462 nil nil nil '(:with-tasks nil))))))
463 (should
464 (equal "* TODO Head1\n"
465 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
466 (org-test-with-temp-text "* TODO Head1"
467 (org-export-as (org-test-default-backend)
468 nil nil nil '(:with-tasks t))))))
469 ;; Archived tree.
470 (should
471 (equal ""
472 (let (org-export-filter-body-functions
473 org-export-filter-final-output-functions)
474 (org-test-with-temp-text "* Head1 :archive:"
475 (let ((org-archive-tag "archive"))
476 (org-export-as (org-test-default-backend)
477 nil nil nil '(:with-archived-trees nil)))))))
478 (should
479 (string-match
480 "\\* Head1[ \t]+:archive:"
481 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
482 (let ((org-archive-tag "archive"))
483 (org-export-as (org-test-default-backend) nil nil nil
484 '(:with-archived-trees headline))))))
485 (should
486 (string-match
487 "\\`\\* Head1[ \t]+:archive:\n\\'"
488 (org-test-with-temp-text "* Head1 :archive:"
489 (let ((org-archive-tag "archive"))
490 (org-export-as (org-test-default-backend)
491 nil nil nil '(:with-archived-trees t))))))
492 ;; Broken links. Depending on `org-export-with-broken-links', raise
493 ;; an error, ignore link or mark is as broken in output.
494 (should-error
495 (org-test-with-temp-text "[[#broken][link]]"
496 (let ((backend
497 (org-export-create-backend
498 :transcoders
499 '((section . (lambda (_e c _i) c))
500 (paragraph . (lambda (_e c _i) c))
501 (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
502 (org-export-as backend nil nil nil '(:with-broken-links nil)))))
503 (should
504 (org-test-with-temp-text "[[#broken][link]]"
505 (let ((backend
506 (org-export-create-backend
507 :transcoders
508 '((section . (lambda (_e c _i) c))
509 (paragraph . (lambda (_e c _i) c))
510 (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
511 (org-export-as backend nil nil nil '(:with-broken-links t)))))
512 (should
513 (org-test-with-temp-text "[[#broken][link]]"
514 (let ((backend
515 (org-export-create-backend
516 :transcoders
517 '((section . (lambda (_e c _i) c))
518 (paragraph . (lambda (_e c _i) c))
519 (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
520 (org-string-nw-p
521 (org-export-as backend nil nil nil '(:with-broken-links mark))))))
522 ;; Clocks.
523 (should
524 (string-match "CLOCK: \\[2012-04-29 .* 10:45\\]"
525 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
526 (org-export-as (org-test-default-backend)
527 nil nil nil '(:with-clocks t)))))
528 (should
529 (equal ""
530 (let (org-export-filter-body-functions
531 org-export-filter-final-output-functions)
532 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
533 (org-export-as (org-test-default-backend)
534 nil nil nil '(:with-clocks nil))))))
535 ;; Drawers.
536 (should
537 (equal ""
538 (let (org-export-filter-body-functions
539 org-export-filter-final-output-functions)
540 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
541 (org-export-as (org-test-default-backend)
542 nil nil nil '(:with-drawers nil))))))
543 (should
544 (equal ":TEST:\ncontents\n:END:\n"
545 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
546 (org-export-as (org-test-default-backend)
547 nil nil nil '(:with-drawers t)))))
548 (should
549 (equal ":FOO:\nkeep\n:END:\n"
550 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
551 (org-export-as (org-test-default-backend)
552 nil nil nil '(:with-drawers ("FOO"))))))
553 (should
554 (equal ":FOO:\nkeep\n:END:\n"
555 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
556 (org-export-as (org-test-default-backend)
557 nil nil nil '(:with-drawers (not "BAR"))))))
558 ;; Fixed-width.
559 (should
560 (equal ": A\n"
561 (org-test-with-temp-text ": A"
562 (org-export-as (org-test-default-backend) nil nil nil
563 '(:with-fixed-width t)))))
564 (should
565 (equal ""
566 (let (org-export-filter-body-functions
567 org-export-filter-final-output-functions)
568 (org-test-with-temp-text ": A"
569 (org-export-as (org-test-default-backend) nil nil nil
570 '(:with-fixed-width nil))))))
571 ;; Footnotes.
572 (should
573 (equal "Footnote?"
574 (let ((org-footnote-section nil))
575 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
576 (org-trim (org-export-as (org-test-default-backend)
577 nil nil nil '(:with-footnotes nil)))))))
578 (should
579 (equal "Footnote?[fn:1]\n\n[fn:1] Def"
580 (let ((org-footnote-section nil))
581 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
582 (org-trim (org-export-as (org-test-default-backend)
583 nil nil nil '(:with-footnotes t)))))))
584 ;; Inlinetasks.
585 (when (featurep 'org-inlinetask)
586 (should
587 (equal
589 (let ((org-inlinetask-min-level 15)
590 org-export-filter-body-functions
591 org-export-filter-final-output-functions)
592 (org-test-with-temp-text "*************** Task"
593 (org-export-as (org-test-default-backend)
594 nil nil nil '(:with-inlinetasks nil))))))
595 (should
596 (equal
598 (let ((org-inlinetask-min-level 15)
599 org-export-filter-body-functions
600 org-export-filter-final-output-functions)
601 (org-test-with-temp-text
602 "*************** Task\nContents\n*************** END"
603 (org-export-as (org-test-default-backend)
604 nil nil nil '(:with-inlinetasks nil)))))))
605 ;; Plannings.
606 (should
607 (string-match
608 "* H\nCLOSED: \\[2012-04-29 .* 10:45\\]"
609 (let ((org-closed-string "CLOSED:"))
610 (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
611 (org-export-as (org-test-default-backend)
612 nil nil nil '(:with-planning t))))))
613 (should
614 (equal "* H\n"
615 (let ((org-closed-string "CLOSED:"))
616 (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
617 (org-export-as (org-test-default-backend)
618 nil nil nil '(:with-planning nil))))))
619 ;; Property Drawers.
620 (should
621 (equal "* H1\n"
622 (org-test-with-temp-text
623 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
624 (org-export-as (org-test-default-backend)
625 nil nil nil '(:with-properties nil)))))
626 (should
627 (equal "* H1\n:PROPERTIES:\n:PROP: value\n:END:\n"
628 (org-test-with-temp-text
629 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
630 (org-export-as (org-test-default-backend)
631 nil nil nil '(:with-properties t)))))
632 (should
633 (equal "* H1\n:PROPERTIES:\n:B: 2\n:END:\n"
634 (org-test-with-temp-text
635 "* H1\n :PROPERTIES:\n :A: 1\n :B: 2\n:END:"
636 (org-export-as (org-test-default-backend)
637 nil nil nil '(:with-properties ("B"))))))
638 ;; Statistics cookies.
639 (should
640 (equal "* Stats"
641 (let (org-export-filter-body-functions
642 org-export-filter-final-output-functions)
643 (org-trim
644 (org-test-with-temp-text "* Stats [0/0]"
645 (org-export-as (org-test-default-backend)
646 nil nil nil '(:with-statistics-cookies nil)))))))
647 ;; Tables.
648 (should
649 (equal "| A |\n"
650 (org-test-with-temp-text "| A |"
651 (org-export-as (org-test-default-backend) nil nil nil
652 '(:with-tables t)))))
653 (should
654 (equal ""
655 (let (org-export-filter-body-functions
656 org-export-filter-final-output-functions)
657 (org-test-with-temp-text "| A |"
658 (org-export-as (org-test-default-backend) nil nil nil
659 '(:with-tables nil)))))))
661 (ert-deftest test-org-export/with-timestamps ()
662 "Test `org-export-with-timestamps' specifications."
663 ;; t value.
664 (should
665 (string-match
666 "\\[2012-04-29 .*? 10:45\\]<2012-04-29 .*? 10:45>"
667 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
668 (org-export-as (org-test-default-backend)
669 nil nil nil '(:with-timestamps t)))))
670 ;; nil value.
671 (should
672 (equal
674 (let (org-export-filter-body-functions
675 org-export-filter-final-output-functions)
676 (org-trim
677 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
678 (org-export-as (org-test-default-backend)
679 nil nil nil '(:with-timestamps nil)))))))
680 ;; `active' value.
681 (should
682 (string-match
683 "<2012-03-29 .*?>\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
684 (org-test-with-temp-text
685 "<2012-03-29 Thu>[2012-03-29 Thu]
687 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
688 (org-export-as (org-test-default-backend)
689 nil nil nil '(:with-timestamps active)))))
690 ;; `inactive' value.
691 (should
692 (string-match
693 "\\[2012-03-29 .*?\\]\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
694 (org-test-with-temp-text
695 "<2012-03-29 Thu>[2012-03-29 Thu]
697 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
698 (org-export-as (org-test-default-backend)
699 nil nil nil '(:with-timestamps inactive))))))
701 (ert-deftest test-org-export/comment-tree ()
702 "Test if export process ignores commented trees."
703 (should
704 (equal ""
705 (let (org-export-filter-body-functions
706 org-export-filter-final-output-functions)
707 (org-test-with-temp-text "* COMMENT Head1"
708 (org-export-as (org-test-default-backend)))))))
710 (ert-deftest test-org-export/uninterpreted ()
711 "Test handling of uninterpreted elements."
712 ;; Entities.
713 (should
714 (equal "dummy\n"
715 (org-test-with-temp-text "\\alpha"
716 (org-export-as
717 (org-export-create-backend
718 :transcoders '((entity . (lambda (e c i) "dummy"))
719 (paragraph . (lambda (p c i) c))
720 (section . (lambda (s c i) c))))
721 nil nil nil '(:with-entities t)))))
722 (should
723 (equal "\\alpha\n"
724 (org-test-with-temp-text "\\alpha"
725 (org-export-as
726 (org-export-create-backend
727 :transcoders '((entity . (lambda (e c i) "dummy"))
728 (paragraph . (lambda (p c i) c))
729 (section . (lambda (s c i) c))))
730 nil nil nil '(:with-entities nil)))))
731 ;; Emphasis.
732 (should
733 (equal "dummy\n"
734 (org-test-with-temp-text "*bold*"
735 (org-export-as
736 (org-export-create-backend
737 :transcoders '((bold . (lambda (b c i) "dummy"))
738 (paragraph . (lambda (p c i) c))
739 (section . (lambda (s c i) c))))
740 nil nil nil '(:with-emphasize t)))))
741 (should
742 (equal "*bold*\n"
743 (org-test-with-temp-text "*bold*"
744 (org-export-as
745 (org-export-create-backend
746 :transcoders '((bold . (lambda (b c i) "dummy"))
747 (paragraph . (lambda (p c i) c))
748 (section . (lambda (s c i) c))))
749 nil nil nil '(:with-emphasize nil)))))
750 ;; LaTeX environment.
751 (should
752 (equal "dummy\n"
753 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
754 (org-export-as
755 (org-export-create-backend
756 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
757 (section . (lambda (s c i) c))))
758 nil nil nil '(:with-latex t)))))
759 (should
760 (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
761 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
762 (org-export-as
763 (org-export-create-backend
764 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
765 (section . (lambda (s c i) c))))
766 nil nil nil '(:with-latex verbatim)))))
767 ;; LaTeX fragment.
768 (should
769 (equal "dummy\n"
770 (org-test-with-temp-text "$1$"
771 (org-export-as
772 (org-export-create-backend
773 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
774 (paragraph . (lambda (p c i) c))
775 (section . (lambda (s c i) c))))
776 nil nil nil '(:with-latex t)))))
777 (should
778 (equal "$1$\n"
779 (org-test-with-temp-text "$1$"
780 (org-export-as
781 (org-export-create-backend
782 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
783 (paragraph . (lambda (p c i) c))
784 (section . (lambda (s c i) c))))
785 nil nil nil '(:with-latex verbatim)))))
786 ;; Sub/superscript.
787 (should
788 (equal "adummy\n"
789 (org-test-with-temp-text "a_b"
790 (org-export-as
791 (org-export-create-backend
792 :transcoders '((subscript . (lambda (s c i) "dummy"))
793 (paragraph . (lambda (p c i) c))
794 (section . (lambda (s c i) c))))
795 nil nil nil '(:with-sub-superscript t)))))
796 (should
797 (equal "a_b\n"
798 (org-test-with-temp-text "a_b"
799 (org-export-as
800 (org-export-create-backend
801 :transcoders '((subscript . (lambda (s c i) "dummy"))
802 (paragraph . (lambda (p c i) c))
803 (section . (lambda (s c i) c))))
804 nil nil nil '(:with-sub-superscript nil)))))
805 (should
806 (equal "a_b\n"
807 (org-test-with-temp-text "a_b"
808 (org-export-as
809 (org-export-create-backend
810 :transcoders '((subscript . (lambda (s c i) "dummy"))
811 (paragraph . (lambda (p c i) c))
812 (section . (lambda (s c i) c))))
813 nil nil nil '(:with-sub-superscript {})))))
814 (should
815 (equal "adummy\n"
816 (org-test-with-temp-text "a_{b}"
817 (org-export-as
818 (org-export-create-backend
819 :transcoders '((subscript . (lambda (s c i) "dummy"))
820 (paragraph . (lambda (p c i) c))
821 (section . (lambda (s c i) c))))
822 nil nil nil '(:with-sub-superscript {})))))
823 (should
824 (equal "a_entity\n"
825 (org-test-with-temp-text "a_\\alpha"
826 (org-export-as
827 (org-export-create-backend
828 :transcoders '((entity . (lambda (e c i) "entity"))
829 (subscript . (lambda (s c i) "dummy"))
830 (paragraph . (lambda (p c i) c))
831 (section . (lambda (s c i) c))))
832 nil nil nil '(:with-sub-superscript nil)))))
833 ;; Also handle uninterpreted objects in title.
834 (should
835 (equal "a_b"
836 (org-test-with-temp-text "#+TITLE: a_b"
837 (org-export-as
838 (org-export-create-backend
839 :transcoders
840 '((subscript . (lambda (s c i) "dummy"))
841 (template . (lambda (c i) (org-export-data
842 (plist-get i :title) i)))
843 (section . (lambda (s c i) c))))
844 nil nil nil '(:with-sub-superscript nil)))))
845 ;; Handle uninterpreted objects in captions.
846 (should
847 (equal "adummy\n"
848 (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
849 (org-export-as
850 (org-export-create-backend
851 :transcoders
852 '((subscript . (lambda (s c i) "dummy"))
853 (paragraph . (lambda (p c i)
854 (org-export-data (org-export-get-caption p) i)))
855 (section . (lambda (s c i) c))))
856 nil nil nil '(:with-sub-superscript t)))))
857 (should
858 (equal "a_b\n"
859 (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
860 (org-export-as
861 (org-export-create-backend
862 :transcoders
863 '((subscript . (lambda (s c i) "dummy"))
864 (paragraph . (lambda (p c i)
865 (org-export-data (org-export-get-caption p) i)))
866 (section . (lambda (s c i) c))))
867 nil nil nil '(:with-sub-superscript nil)))))
868 ;; Special case: multiples uninterpreted objects in a row.
869 (should
870 (equal "a_b_c_d\n"
871 (org-test-with-temp-text "a_b_c_d"
872 (org-export-as
873 (org-export-create-backend
874 :transcoders '((subscript . (lambda (s c i) "dummy"))
875 (paragraph . (lambda (p c i) c))
876 (section . (lambda (s c i) c))))
877 nil nil nil '(:with-sub-superscript {}))))))
879 (ert-deftest test-org-export/export-scope ()
880 "Test all export scopes."
881 (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 (should
2521 ;; Note: Headline's number is in fact a list of numbers.
2522 (equal '(2)
2523 (org-test-with-parsed-data
2524 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
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 (should
2531 ;; Note: Item's number is in fact a list of numbers.
2532 (equal '(1 2)
2533 (org-test-with-parsed-data
2534 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
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 (should
2541 (equal '(2 3)
2542 (org-test-with-parsed-data "
2543 Paragraph[fn:1][fn:2][fn:lbl3:C<<target>>][[test]][[target]]
2544 \[fn:1] A
2546 \[fn:2] <<test>>B"
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 (should
2554 (= 2
2555 (org-test-with-parsed-data
2556 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
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 (should
2564 (equal '(2)
2565 (org-test-with-parsed-data
2566 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
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 (defun test-org-gen-loc-list(text type)
2590 (org-test-with-parsed-data text
2591 (org-element-map tree type
2592 (lambda (el) (or (org-export-get-loc el info) 'no-loc)))))
2594 (ert-deftest test-org-export/get-loc ()
2595 "Test `org-export-get-loc' specifications."
2596 (should
2597 ;; "-n" resets line number.
2598 (equal '(0)
2599 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n\n Text\n#+END_EXAMPLE"
2600 'example-block)))
2601 ;; The first "+n" has 0 lines before it
2602 (should
2603 (equal '(0)
2604 (test-org-gen-loc-list "#+BEGIN_EXAMPLE +n\n Text\n#+END_EXAMPLE"
2605 'example-block)))
2606 ;; "-n 10" resets line number but has "9 lines" before it.
2607 (should
2608 (equal '(9)
2609 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n 10\n Text\n#+END_EXAMPLE"
2610 'example-block)))
2611 ;; -n10 with two lines then +n 15
2612 (should
2613 (equal '(9 25)
2614 (test-org-gen-loc-list "
2615 #+BEGIN_EXAMPLE -n 10
2616 Text_10
2617 Second line(11)
2618 #+END_EXAMPLE
2619 #+BEGIN_EXAMPLE +n 15
2620 Text line (11 + 15)
2621 #+END_EXAMPLE"
2622 'example-block)))
2623 (should
2624 (equal '(9 19 0)
2625 (test-org-gen-loc-list "
2626 #+BEGIN_EXAMPLE -n 10
2627 Text
2628 #+END_EXAMPLE
2629 #+BEGIN_EXAMPLE +n 10
2630 Text
2631 #+END_EXAMPLE
2633 #+BEGIN_EXAMPLE -n
2634 Text
2635 #+END_EXAMPLE"
2636 'example-block)))
2637 ;; an Example Block without -n does not add to the line count.
2638 (should
2639 (equal '(9 no-loc 19)
2640 (test-org-gen-loc-list "
2641 #+BEGIN_EXAMPLE -n 10
2642 Text
2643 #+END_EXAMPLE
2644 #+BEGIN_EXAMPLE
2645 Text
2646 #+END_EXAMPLE
2647 #+BEGIN_EXAMPLE +n 10
2648 Text
2649 #+END_EXAMPLE"
2650 'example-block)))
2651 ;; "-n" resets line number.
2652 (should
2653 (equal
2654 '(0)
2655 (test-org-gen-loc-list "#+BEGIN_SRC emacs-lisp -n \n (- 1 1) \n#+END_SRC"
2656 'src-block)))
2657 ;; The first "+n" has 0 lines before it.
2658 (should
2659 (equal '(0)
2660 (test-org-gen-loc-list
2661 "#+BEGIN_SRC emacs-lisp +n \n (+ 0 (- 1 1))\n#+END_SRC"
2662 'src-block)))
2663 ;; "-n 10" resets line number but has "9 lines" before it.
2664 (should
2665 (equal '(9)
2666 (test-org-gen-loc-list
2667 "#+BEGIN_SRC emacs-lisp -n 10\n (- 10 1)\n#+END_SRC"
2668 'src-block)))
2669 (should
2670 (equal '(9 25)
2671 (test-org-gen-loc-list "
2672 #+BEGIN_SRC emacs-lisp -n 10
2673 (- 10 1)
2674 (+ (- 10 1) 1)
2675 #+END_SRC
2676 #+BEGIN_SRC emacs-lisp +n 15
2677 (+ (- 10 1) 2 (- 15 1))
2678 #+END_SRC"
2679 'src-block)))
2680 (should
2681 (equal '(9 19 0)
2682 (test-org-gen-loc-list "
2683 #+BEGIN_SRC emacs-lisp -n 10
2684 (- 10 1)
2685 #+END_SRC
2686 #+BEGIN_SRC emacs-lisp +n 10
2687 (+ (- 10 1) 1 (- 10 1))
2688 #+END_SRC
2689 #+BEGIN_SRC emacs-lisp -n
2690 (- 1 1)
2691 #+END_SRC"
2692 'src-block)))
2693 ;; A SRC Block without -n does not add to the line count.
2694 (should
2695 (equal '(9 no-loc 19)
2696 (test-org-gen-loc-list
2697 "#+BEGIN_SRC emacs-lisp -n 10
2698 (+ (-10 1) 1)
2699 #+END_SRC
2700 #+BEGIN_SRC emacs-lisp
2701 (+ 2 2)
2702 #+END_SRC
2703 #+BEGIN_SRC emacs-lisp +n 10
2704 (+ (- 10 1) 1 (- 10 1))
2705 #+END_SRC"
2706 'src-block))))
2708 (ert-deftest test-org-export/resolve-coderef ()
2709 "Test `org-export-resolve-coderef' specifications."
2710 (let ((org-coderef-label-format "(ref:%s)"))
2711 ;; A link to a "-n -k -r" block returns line number.
2712 (should
2713 (= 1
2714 (org-test-with-parsed-data
2715 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
2716 (org-export-resolve-coderef "coderef" info))))
2717 (should
2718 (= 10
2719 (org-test-with-parsed-data
2720 "#+BEGIN_EXAMPLE -n 10 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
2721 (org-export-resolve-coderef "coderef" info))))
2722 (should
2723 (= 135
2724 (org-test-with-parsed-data
2725 "#+BEGIN_EXAMPLE -n 10 -k -r\nText \n#+END_EXAMPLE\n
2726 #+BEGIN_EXAMPLE +n 125 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
2727 (org-export-resolve-coderef "coderef" info))))
2728 (should
2729 (= 1
2730 (org-test-with-parsed-data
2731 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2732 (org-export-resolve-coderef "coderef" info))))
2733 (should
2734 (= 10
2735 (org-test-with-parsed-data
2736 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2737 (org-export-resolve-coderef "coderef" info))))
2738 (should
2739 (= 135
2740 (org-test-with-parsed-data
2741 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) \n#+END_SRC\n
2742 #+BEGIN_SRC emacs-lisp +n 125 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2743 (org-export-resolve-coderef "coderef" info))))
2744 ;; A link to a "-n -r" block returns line number.
2745 (should
2746 (= 1
2747 (org-test-with-parsed-data
2748 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
2749 (org-export-resolve-coderef "coderef" info))))
2750 (should
2751 (= 10
2752 (org-test-with-parsed-data
2753 "#+BEGIN_EXAMPLE -n 10 -r\nText (ref:coderef)\n#+END_EXAMPLE"
2754 (org-export-resolve-coderef "coderef" info))))
2755 (should
2756 (= 135
2757 (org-test-with-parsed-data
2758 "#+BEGIN_EXAMPLE +n 10 -r\nText \n#+END_EXAMPLE
2759 #+BEGIN_EXAMPLE +n 125 -r\nText (ref:coderef)\n#+END_EXAMPLE"
2760 (org-export-resolve-coderef "coderef" info))))
2762 (should
2763 (= 1
2764 (org-test-with-parsed-data
2765 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2766 (org-export-resolve-coderef "coderef" info))))
2767 (should
2768 (= 10
2769 (org-test-with-parsed-data
2770 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2771 (org-export-resolve-coderef "coderef" info))))
2772 (should
2773 (= 135
2774 (org-test-with-parsed-data
2775 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) \n#+END_SRC
2776 #+BEGIN_SRC emacs-lisp +n125 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2777 (org-export-resolve-coderef "coderef" info))))
2778 ;; A link to a "-n" block returns coderef.
2779 (should
2780 (equal "coderef"
2781 (org-test-with-parsed-data
2782 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2783 (org-export-resolve-coderef "coderef" info))))
2784 (should
2785 (equal "coderef"
2786 (org-test-with-parsed-data
2787 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
2788 (org-export-resolve-coderef "coderef" info))))
2789 ;; A link to a "-r" block returns line number.
2790 (should
2791 (= 1
2792 (org-test-with-parsed-data
2793 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2794 (org-export-resolve-coderef "coderef" info))))
2795 (should
2796 (= 1
2797 (org-test-with-parsed-data
2798 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
2799 (org-export-resolve-coderef "coderef" info))))
2800 ;; A link to a block without a switch returns coderef.
2801 (should
2802 (equal "coderef"
2803 (org-test-with-parsed-data
2804 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2805 (org-export-resolve-coderef "coderef" info))))
2806 (org-test-with-parsed-data
2807 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
2808 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2809 ;; Correctly handle continued line numbers. A "+n" switch should
2810 ;; resume numbering from previous block with numbered lines,
2811 ;; ignoring blocks not numbering lines in the process. A "-n"
2812 ;; switch resets count.
2813 (should
2814 (equal '(2 1)
2815 (org-test-with-parsed-data "
2816 #+BEGIN_EXAMPLE -n
2817 Text.
2818 #+END_EXAMPLE
2820 #+BEGIN_SRC emacs-lisp
2821 \(- 1 1)
2822 #+END_SRC
2824 #+BEGIN_SRC emacs-lisp +n -r
2825 \(+ 1 1) (ref:addition)
2826 #+END_SRC
2828 #+BEGIN_EXAMPLE -n -r
2829 Another text. (ref:text)
2830 #+END_EXAMPLE"
2831 (list (org-export-resolve-coderef "addition" info)
2832 (org-export-resolve-coderef "text" info)))))
2833 ;; Recognize coderef with user-specified syntax.
2834 (should
2835 (equal
2836 "text"
2837 (org-test-with-parsed-data
2838 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
2839 (org-export-resolve-coderef "text" info))))
2840 ;; Unresolved coderefs raise a `org-link-broken' signal.
2841 (should
2842 (condition-case nil
2843 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2844 (org-export-resolve-coderef "unknown" info))
2845 (org-link-broken t)))))
2847 (ert-deftest test-org-export/resolve-fuzzy-link ()
2848 "Test `org-export-resolve-fuzzy-link' specifications."
2849 ;; Match target objects.
2850 (should
2851 (org-test-with-parsed-data "<<target>> [[target]]"
2852 (org-export-resolve-fuzzy-link
2853 (org-element-map tree 'link 'identity info t) info)))
2854 ;; Match named elements.
2855 (should
2856 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
2857 (org-export-resolve-fuzzy-link
2858 (org-element-map tree 'link 'identity info t) info)))
2859 ;; Match exact headline's name.
2860 (should
2861 (org-test-with-parsed-data "* My headline\n[[My headline]]"
2862 (org-export-resolve-fuzzy-link
2863 (org-element-map tree 'link 'identity info t) info)))
2864 ;; Targets objects have priority over headline titles.
2865 (should
2866 (eq 'target
2867 (org-test-with-parsed-data "* target\n<<target>>[[target]]"
2868 (org-element-type
2869 (org-export-resolve-fuzzy-link
2870 (org-element-map tree 'link 'identity info t) info)))))
2871 ;; Named elements have priority over headline titles.
2872 (should
2873 (eq 'paragraph
2874 (org-test-with-parsed-data
2875 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
2876 (org-element-type
2877 (org-export-resolve-fuzzy-link
2878 (org-element-map tree 'link 'identity info t) info)))))
2879 ;; If link's path starts with a "*", only match headline titles,
2880 ;; though.
2881 (should
2882 (eq 'headline
2883 (org-test-with-parsed-data
2884 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
2885 (org-element-type
2886 (org-export-resolve-fuzzy-link
2887 (org-element-map tree 'link 'identity info t) info)))))
2888 ;; Raise a `org-link-broken' signal if no match.
2889 (should
2890 (org-test-with-parsed-data "[[target]]"
2891 (condition-case nil
2892 (org-export-resolve-fuzzy-link
2893 (org-element-map tree 'link #'identity info t) info)
2894 (org-link-broken t))))
2895 ;; Match fuzzy link even when before first headline.
2896 (should
2897 (eq 'headline
2898 (org-test-with-parsed-data "[[hl]]\n* hl"
2899 (org-element-type
2900 (org-export-resolve-fuzzy-link
2901 (org-element-map tree 'link 'identity info t) info)))))
2902 ;; Handle url-encoded fuzzy links.
2903 (should
2904 (org-test-with-parsed-data "* A B\n[[A%20B]]"
2905 (org-export-resolve-fuzzy-link
2906 (org-element-map tree 'link #'identity info t) info))))
2908 (ert-deftest test-org-export/resolve-id-link ()
2909 "Test `org-export-resolve-id-link' specifications."
2910 ;; Regular test for custom-id link.
2911 (should
2912 (equal '("Headline1")
2913 (org-test-with-parsed-data "* Headline1
2914 :PROPERTIES:
2915 :CUSTOM_ID: test
2916 :END:
2917 * Headline 2
2918 \[[#test]]"
2919 (org-element-property
2920 :title
2921 (org-export-resolve-id-link
2922 (org-element-map tree 'link 'identity info t) info)))))
2923 ;; Raise a `org-link-broken' signal on failing searches.
2924 (should
2925 (org-test-with-parsed-data "* Headline1
2926 :PROPERTIES:
2927 :CUSTOM_ID: test
2928 :END:
2929 * Headline 2
2930 \[[#no-match]]"
2931 (condition-case nil
2932 (org-export-resolve-id-link
2933 (org-element-map tree 'link #'identity info t) info)
2934 (org-link-broken t))))
2935 ;; Test for internal id target.
2936 (should
2937 (equal '("Headline1")
2938 (org-test-with-parsed-data "* Headline1
2939 :PROPERTIES:
2940 :ID: aaaa
2941 :END:
2942 * Headline 2
2943 \[[id:aaaa]]"
2944 (org-element-property
2945 :title
2946 (org-export-resolve-id-link
2947 (org-element-map tree 'link 'identity info t) info)))))
2948 ;; Test for external id target.
2949 (should
2950 (equal
2951 "external-file"
2952 (org-test-with-parsed-data "[[id:aaaa]]"
2953 (org-export-resolve-id-link
2954 (org-element-map tree 'link 'identity info t)
2955 (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
2957 (ert-deftest test-org-export/resolve-radio-link ()
2958 "Test `org-export-resolve-radio-link' specifications."
2959 ;; Standard test.
2960 (should
2961 (org-test-with-temp-text "<<<radio>>> radio"
2962 (org-update-radio-target-regexp)
2963 (let* ((tree (org-element-parse-buffer))
2964 (info `(:parse-tree ,tree)))
2965 (org-export-resolve-radio-link
2966 (org-element-map tree 'link 'identity info t)
2967 info))))
2968 ;; Radio targets are case-insensitive.
2969 (should
2970 (org-test-with-temp-text "<<<RADIO>>> radio"
2971 (org-update-radio-target-regexp)
2972 (let* ((tree (org-element-parse-buffer))
2973 (info `(:parse-tree ,tree)))
2974 (org-export-resolve-radio-link
2975 (org-element-map tree 'link 'identity info t)
2976 info))))
2977 ;; Radio target with objects.
2978 (should
2979 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
2980 (org-update-radio-target-regexp)
2981 (let* ((tree (org-element-parse-buffer))
2982 (info `(:parse-tree ,tree)))
2983 (org-export-resolve-radio-link
2984 (org-element-map tree 'link 'identity info t)
2985 info))))
2986 ;; Radio target with objects at its beginning.
2987 (should
2988 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
2989 (org-update-radio-target-regexp)
2990 (let* ((tree (org-element-parse-buffer))
2991 (info `(:parse-tree ,tree)))
2992 (org-export-resolve-radio-link
2993 (org-element-map tree 'link 'identity info t)
2994 info))))
2995 ;; Radio link next to an apostrophe.
2996 (should
2997 (org-test-with-temp-text "<<<radio>>> radio's"
2998 (org-update-radio-target-regexp)
2999 (let* ((tree (org-element-parse-buffer))
3000 (info `(:parse-tree ,tree)))
3001 (org-export-resolve-radio-link
3002 (org-element-map tree 'link 'identity info t)
3003 info))))
3004 ;; Multiple radio targets.
3005 (should
3006 (equal '("radio1" "radio2")
3007 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
3008 (org-update-radio-target-regexp)
3009 (let* ((tree (org-element-parse-buffer))
3010 (info `(:parse-tree ,tree)))
3011 (org-element-map tree 'link
3012 (lambda (link)
3013 (org-element-property
3014 :value (org-export-resolve-radio-link link info)))
3015 info)))))
3016 ;; Radio target is whitespace insensitive.
3017 (should
3018 (org-test-with-temp-text "<<<a radio>>> a\n radio"
3019 (org-update-radio-target-regexp)
3020 (let* ((tree (org-element-parse-buffer))
3021 (info `(:parse-tree ,tree)))
3022 (org-element-map tree 'link
3023 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
3025 (ert-deftest test-org-export/file-uri ()
3026 "Test `org-export-file-uri' specifications."
3027 ;; Preserve relative filenames.
3028 (should (equal "relative.org" (org-export-file-uri "relative.org")))
3029 ;; Local files start with "file:///"
3030 (should (equal "file:///local.org" (org-export-file-uri "/local.org")))
3031 ;; Remote files start with "file://"
3032 (should (equal "file://myself@some.where:papers/last.pdf"
3033 (org-export-file-uri "/myself@some.where:papers/last.pdf")))
3034 (should (equal "file://localhost/etc/fstab"
3035 (org-export-file-uri "//localhost/etc/fstab")))
3036 ;; Expand filename starting with "~".
3037 (should (equal (org-export-file-uri "~/file.org")
3038 (concat "file://" (expand-file-name "~/file.org")))))
3040 (ert-deftest test-org-export/get-reference ()
3041 "Test `org-export-get-reference' specifications."
3042 (should
3043 (org-test-with-parsed-data "* Headline"
3044 (org-export-get-reference (org-element-map tree 'headline #'identity nil t)
3045 info)))
3046 ;; For a given element always return the same reference.
3047 (should
3048 (org-test-with-parsed-data "* Headline"
3049 (let ((headline (org-element-map tree 'headline #'identity nil t)))
3050 (equal (org-export-get-reference headline info)
3051 (org-export-get-reference headline info)))))
3052 ;; Use search cells defined in `:crossrefs'.
3053 (should
3054 (equal "org0000001"
3055 (org-test-with-parsed-data "* Headline"
3056 (let* ((headline (org-element-map tree 'headline #'identity nil t))
3057 (search-cell (car (org-export-search-cells headline))))
3058 (setq info
3059 (plist-put info :crossrefs (list (cons search-cell 1))))
3060 (org-export-get-reference headline info))))))
3064 ;;; Src-block and example-block
3066 (ert-deftest test-org-export/unravel-code ()
3067 "Test `org-export-unravel-code' function."
3068 ;; Code without reference.
3069 (should
3070 (equal '("(+ 1 1)\n")
3071 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
3072 (org-export-unravel-code (org-element-at-point)))))
3073 ;; Code with reference.
3074 (should
3075 (equal '("(+ 1 1)\n" (1 . "test"))
3076 (org-test-with-temp-text
3077 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
3078 (let ((org-coderef-label-format "(ref:%s)"))
3079 (org-export-unravel-code (org-element-at-point))))))
3080 ;; Code with user-defined reference.
3081 (should
3082 (equal
3083 '("(+ 1 1)\n" (1 . "test"))
3084 (org-test-with-temp-text
3085 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
3086 (let ((org-coderef-label-format "(ref:%s)"))
3087 (org-export-unravel-code (org-element-at-point))))))
3088 ;; Code references keys are relative to the current block.
3089 (should
3090 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
3091 (org-test-with-temp-text "
3092 #+BEGIN_EXAMPLE -n
3093 \(+ 1 1)
3094 #+END_EXAMPLE
3095 #+BEGIN_EXAMPLE +n
3096 \(+ 2 2)
3097 \(+ 3 3) (ref:one)
3098 #+END_EXAMPLE"
3099 (goto-line 5)
3100 (let ((org-coderef-label-format "(ref:%s)"))
3101 (org-export-unravel-code (org-element-at-point)))))))
3103 (ert-deftest test-org-export/format-code-default ()
3104 "Test `org-export-format-code-default' specifications."
3105 ;; Return the empty string when code is empty.
3106 (should
3107 (equal ""
3108 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
3109 (org-export-format-code-default
3110 (org-element-map tree 'src-block 'identity info t) info))))
3111 ;; Number lines, two whitespace characters before the actual loc.
3112 (should
3113 (equal "1 a\n2 b\n"
3114 (org-test-with-parsed-data
3115 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
3116 (org-export-format-code-default
3117 (org-element-map tree 'src-block 'identity info t) info))))
3118 ;; Put references 6 whitespace characters after the widest line,
3119 ;; wrapped within parenthesis.
3120 (should
3121 (equal "123 (a)\n1 (b)\n"
3122 (let ((org-coderef-label-format "(ref:%s)"))
3123 (org-test-with-parsed-data
3124 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
3125 (org-export-format-code-default
3126 (org-element-map tree 'src-block 'identity info t) info))))))
3130 ;;; Smart Quotes
3132 (ert-deftest test-org-export/activate-smart-quotes ()
3133 "Test `org-export-activate-smart-quotes' specifications."
3134 ;; Double quotes: standard test.
3135 (should
3136 (equal
3137 '("some &ldquo;quoted&rdquo; text")
3138 (let ((org-export-default-language "en"))
3139 (org-test-with-parsed-data "some \"quoted\" text"
3140 (org-element-map tree 'plain-text
3141 (lambda (s) (org-export-activate-smart-quotes s :html info))
3142 info)))))
3143 ;; Opening quotes: at the beginning of a paragraph.
3144 (should
3145 (equal
3146 '("&ldquo;begin")
3147 (let ((org-export-default-language "en"))
3148 (org-test-with-parsed-data "\"begin"
3149 (org-element-map tree 'plain-text
3150 (lambda (s) (org-export-activate-smart-quotes s :html info))
3151 info)))))
3152 ;; Opening quotes: after an object.
3153 (should
3154 (equal
3155 '("&ldquo;quoted&rdquo; text")
3156 (let ((org-export-default-language "en"))
3157 (org-test-with-parsed-data "=verb= \"quoted\" text"
3158 (org-element-map tree 'plain-text
3159 (lambda (s) (org-export-activate-smart-quotes s :html info))
3160 info)))))
3161 ;; Closing quotes: at the end of a paragraph.
3162 (should
3163 (equal
3164 '("Quoted &ldquo;text&rdquo;")
3165 (let ((org-export-default-language "en"))
3166 (org-test-with-parsed-data "Quoted \"text\""
3167 (org-element-map tree 'plain-text
3168 (lambda (s) (org-export-activate-smart-quotes s :html info))
3169 info)))))
3170 ;; Inner quotes: standard test.
3171 (should
3172 (equal '("« outer « inner » outer »")
3173 (let ((org-export-default-language "fr"))
3174 (org-test-with-parsed-data "\"outer 'inner' outer\""
3175 (org-element-map tree 'plain-text
3176 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3177 info)))))
3178 ;; Inner quotes: close to special symbols.
3179 (should
3180 (equal '("« outer (« inner ») outer »")
3181 (let ((org-export-default-language "fr"))
3182 (org-test-with-parsed-data "\"outer ('inner') outer\""
3183 (org-element-map tree 'plain-text
3184 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3185 info)))))
3186 (should
3187 (equal '("« « inner » »")
3188 (let ((org-export-default-language "fr"))
3189 (org-test-with-parsed-data "\"'inner'\""
3190 (org-element-map tree 'plain-text
3191 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3192 info)))))
3193 ;; Apostrophe: standard test.
3194 (should
3195 (equal '("It « shouldn’t » fail")
3196 (let ((org-export-default-language "fr"))
3197 (org-test-with-parsed-data "It \"shouldn't\" fail"
3198 (org-element-map tree 'plain-text
3199 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3200 info)))))
3201 (should
3202 (equal '("It shouldn’t fail")
3203 (let ((org-export-default-language "fr"))
3204 (org-test-with-parsed-data "It shouldn't fail"
3205 (org-element-map tree 'plain-text
3206 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3207 info)))))
3208 ;; Apostrophe: before an object.
3209 (should
3210 (equal
3211 '("« a’" " »")
3212 (let ((org-export-default-language "fr"))
3213 (org-test-with-parsed-data "\"a'=b=\""
3214 (org-element-map tree 'plain-text
3215 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3216 info)))))
3217 ;; Apostrophe: after an object.
3218 (should
3219 (equal '("« " "’s »")
3220 (let ((org-export-default-language "fr"))
3221 (org-test-with-parsed-data "\"=code='s\""
3222 (org-element-map tree 'plain-text
3223 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3224 info)))))
3225 ;; Special case: isolated quotes.
3226 (should
3227 (equal '("&ldquo;" "&rdquo;")
3228 (let ((org-export-default-language "en"))
3229 (org-test-with-parsed-data "\"$x$\""
3230 (org-element-map tree 'plain-text
3231 (lambda (s) (org-export-activate-smart-quotes s :html info))
3232 info)))))
3233 ;; Smart quotes in secondary strings.
3234 (should
3235 (equal '("&ldquo;" "&rdquo;")
3236 (let ((org-export-default-language "en"))
3237 (org-test-with-parsed-data "* \"$x$\""
3238 (org-element-map tree 'plain-text
3239 (lambda (s) (org-export-activate-smart-quotes s :html info))
3240 info)))))
3241 ;; Smart quotes in document keywords.
3242 (should
3243 (equal '("&ldquo;" "&rdquo;")
3244 (let ((org-export-default-language "en"))
3245 (org-test-with-parsed-data "#+TITLE: \"$x$\""
3246 (org-element-map (plist-get info :title) 'plain-text
3247 (lambda (s) (org-export-activate-smart-quotes s :html info))
3248 info)))))
3249 ;; Smart quotes in parsed affiliated keywords.
3250 (should
3251 (equal '("&ldquo;" "&rdquo;" "Paragraph")
3252 (let ((org-export-default-language "en"))
3253 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
3254 (org-element-map tree 'plain-text
3255 (lambda (s) (org-export-activate-smart-quotes s :html info))
3256 info nil nil t)))))
3257 ;; Smart quotes within objects.
3258 (should
3259 (equal '("&ldquo;foo&rdquo;")
3260 (let ((org-export-default-language "en"))
3261 (org-test-with-parsed-data "| \"foo\" |"
3262 (org-element-map tree 'plain-text
3263 (lambda (s) (org-export-activate-smart-quotes s :html info))
3264 info nil nil t)))))
3265 ;; FIXME: Test failing non-interactively.
3267 ;; (should
3268 ;; (equal '("&ldquo;foo&rdquo;")
3269 ;; (let ((org-export-default-language "en"))
3270 ;; (org-test-with-parsed-data "*\"foo\"*"
3271 ;; (org-element-map tree 'plain-text
3272 ;; (lambda (s) (org-export-activate-smart-quotes s :html info))
3273 ;; info nil nil t)))))
3278 ;;; Tables
3280 (ert-deftest test-org-export/special-column ()
3281 "Test if the table's special column is properly recognized."
3282 ;; 1. First column is special if it contains only a special marking
3283 ;; characters or empty cells.
3284 (org-test-with-temp-text "
3285 | ! | 1 |
3286 | | 2 |"
3287 (should
3288 (org-export-table-has-special-column-p
3289 (org-element-map
3290 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3291 ;; 2. If the column contains anything else, it isn't special.
3292 (org-test-with-temp-text "
3293 | ! | 1 |
3294 | b | 2 |"
3295 (should-not
3296 (org-export-table-has-special-column-p
3297 (org-element-map
3298 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3299 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
3300 ;; and "!".
3301 (org-test-with-temp-text "
3302 | # | 1 |
3303 | ^ | 2 |
3304 | * | 3 |
3305 | _ | 4 |
3306 | / | 5 |
3307 | $ | 6 |
3308 | ! | 7 |"
3309 (should
3310 (org-export-table-has-special-column-p
3311 (org-element-map
3312 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3313 ;; 4. A first column with only empty cells isn't considered as
3314 ;; special.
3315 (org-test-with-temp-text "
3316 | | 1 |
3317 | | 2 |"
3318 (should-not
3319 (org-export-table-has-special-column-p
3320 (org-element-map
3321 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
3323 (ert-deftest test-org-export/table-row-is-special-p ()
3324 "Test `org-export-table-row-is-special-p' specifications."
3325 ;; 1. A row is special if it has a special marking character in the
3326 ;; special column.
3327 (org-test-with-parsed-data "| ! | 1 |"
3328 (should
3329 (org-export-table-row-is-special-p
3330 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3331 ;; 2. A row is special when its first field is "/"
3332 (org-test-with-parsed-data "
3333 | / | 1 |
3334 | a | b |"
3335 (should
3336 (org-export-table-row-is-special-p
3337 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3338 ;; 3. A row only containing alignment cookies is also considered as
3339 ;; special.
3340 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
3341 (should
3342 (org-export-table-row-is-special-p
3343 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3344 ;; 4. Everything else isn't considered as special.
3345 (org-test-with-parsed-data "| \alpha | | c |"
3346 (should-not
3347 (org-export-table-row-is-special-p
3348 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3349 ;; 5. Table's rules are never considered as special rows.
3350 (org-test-with-parsed-data "|---+---|"
3351 (should-not
3352 (org-export-table-row-is-special-p
3353 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
3355 (ert-deftest test-org-export/has-header-p ()
3356 "Test `org-export-table-has-header-p' specifications."
3357 ;; 1. With an header.
3358 (org-test-with-parsed-data "
3359 | a | b |
3360 |---+---|
3361 | c | d |"
3362 (should
3363 (org-export-table-has-header-p
3364 (org-element-map tree 'table 'identity info 'first-match)
3365 info)))
3366 ;; 2. Without an header.
3367 (org-test-with-parsed-data "
3368 | a | b |
3369 | c | d |"
3370 (should-not
3371 (org-export-table-has-header-p
3372 (org-element-map tree 'table 'identity info 'first-match)
3373 info)))
3374 ;; 3. Don't get fooled with starting and ending rules.
3375 (org-test-with-parsed-data "
3376 |---+---|
3377 | a | b |
3378 | c | d |
3379 |---+---|"
3380 (should-not
3381 (org-export-table-has-header-p
3382 (org-element-map tree 'table 'identity info 'first-match)
3383 info))))
3385 (ert-deftest test-org-export/table-row-group ()
3386 "Test `org-export-table-row-group' specifications."
3387 ;; 1. A rule creates a new group.
3388 (should
3389 (equal '(1 rule 2)
3390 (org-test-with-parsed-data "
3391 | a | b |
3392 |---+---|
3393 | 1 | 2 |"
3394 (org-element-map tree 'table-row
3395 (lambda (row)
3396 (if (eq (org-element-property :type row) 'rule) 'rule
3397 (org-export-table-row-group row info)))))))
3398 ;; 2. Special rows are ignored in count.
3399 (should
3400 (equal
3401 '(rule 1)
3402 (org-test-with-parsed-data "
3403 | / | < | > |
3404 |---|---+---|
3405 | | 1 | 2 |"
3406 (org-element-map tree 'table-row
3407 (lambda (row)
3408 (if (eq (org-element-property :type row) 'rule) 'rule
3409 (org-export-table-row-group row info)))
3410 info))))
3411 ;; 3. Double rules also are ignored in count.
3412 (should
3413 (equal '(1 rule rule 2)
3414 (org-test-with-parsed-data "
3415 | a | b |
3416 |---+---|
3417 |---+---|
3418 | 1 | 2 |"
3419 (org-element-map tree 'table-row
3420 (lambda (row)
3421 (if (eq (org-element-property :type row) 'rule) 'rule
3422 (org-export-table-row-group row info))))))))
3424 (ert-deftest test-org-export/table-row-number ()
3425 "Test `org-export-table-row-number' specifications."
3426 ;; Standard test. Number is 0-indexed.
3427 (should
3428 (equal '(0 1)
3429 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
3430 (org-element-map tree 'table-row
3431 (lambda (row) (org-export-table-row-number row info)) info))))
3432 ;; Number ignores separators.
3433 (should
3434 (equal '(0 1)
3435 (org-test-with-parsed-data "
3436 | a | b | c |
3437 |---+---+---|
3438 | d | e | f |"
3439 (org-element-map tree 'table-row
3440 (lambda (row) (org-export-table-row-number row info)) info))))
3441 ;; Number ignores special rows.
3442 (should
3443 (equal '(0 1)
3444 (org-test-with-parsed-data "
3445 | / | < | > |
3446 | | b | c |
3447 |---+-----+-----|
3448 | | <c> | <c> |
3449 | | e | f |"
3450 (org-element-map tree 'table-row
3451 (lambda (row) (org-export-table-row-number row info)) info)))))
3453 (ert-deftest test-org-export/table-cell-width ()
3454 "Test `org-export-table-cell-width' specifications."
3455 ;; 1. Width is primarily determined by width cookies. If no cookie
3456 ;; is found, cell's width is nil.
3457 (org-test-with-parsed-data "
3458 | / | <l> | <6> | <l7> |
3459 | | a | b | c |"
3460 (should
3461 (equal
3462 '(nil 6 7)
3463 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3464 (org-element-map tree 'table-cell 'identity info)))))
3465 ;; 2. The last width cookie has precedence.
3466 (org-test-with-parsed-data "
3467 | <6> |
3468 | <7> |
3469 | a |"
3470 (should
3471 (equal
3472 '(7)
3473 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3474 (org-element-map tree 'table-cell 'identity info)))))
3475 ;; 3. Valid width cookies must have a specific row.
3476 (org-test-with-parsed-data "| <6> | cell |"
3477 (should
3478 (equal
3479 '(nil nil)
3480 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3481 (org-element-map tree 'table-cell 'identity))))))
3483 (ert-deftest test-org-export/table-cell-alignment ()
3484 "Test `org-export-table-cell-alignment' specifications."
3485 ;; 1. Alignment is primarily determined by alignment cookies.
3486 (should
3487 (equal '(left center right)
3488 (let ((org-table-number-fraction 0.5)
3489 (org-table-number-regexp "^[0-9]+$"))
3490 (org-test-with-parsed-data "| <l> | <c> | <r> |"
3491 (mapcar (lambda (cell)
3492 (org-export-table-cell-alignment cell info))
3493 (org-element-map tree 'table-cell 'identity))))))
3494 ;; 2. The last alignment cookie has precedence.
3495 (should
3496 (equal '(right right right)
3497 (org-test-with-parsed-data "
3498 | <l8> |
3499 | cell |
3500 | <r9> |"
3501 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
3502 (org-element-map tree 'table-cell 'identity)))))
3503 ;; 3. If there's no cookie, cell's contents determine alignment.
3504 ;; A column mostly made of cells containing numbers will align
3505 ;; its cells to the right.
3506 (should
3507 (equal '(right right right)
3508 (let ((org-table-number-fraction 0.5)
3509 (org-table-number-regexp "^[0-9]+$"))
3510 (org-test-with-parsed-data "
3511 | 123 |
3512 | some text |
3513 | 12345 |"
3514 (mapcar (lambda (cell)
3515 (org-export-table-cell-alignment cell info))
3516 (org-element-map tree 'table-cell 'identity))))))
3517 ;; 4. Otherwise, they will be aligned to the left.
3518 (should
3519 (equal '(left left left)
3520 (org-test-with-parsed-data "
3521 | text |
3522 | some text |
3523 | \alpha |"
3524 (mapcar (lambda (cell)
3525 (org-export-table-cell-alignment cell info))
3526 (org-element-map tree 'table-cell 'identity info))))))
3528 (ert-deftest test-org-export/table-cell-borders ()
3529 "Test `org-export-table-cell-borders' specifications."
3530 ;; 1. Recognize various column groups indicators.
3531 (org-test-with-parsed-data "| / | < | > | <> |"
3532 (should
3533 (equal
3534 '((right bottom top) (left bottom top) (right bottom top)
3535 (right left bottom top))
3536 (mapcar (lambda (cell)
3537 (org-export-table-cell-borders cell info))
3538 (org-element-map tree 'table-cell 'identity)))))
3539 ;; 2. Accept shortcuts to define column groups.
3540 (org-test-with-parsed-data "| / | < | < |"
3541 (should
3542 (equal
3543 '((right bottom top) (right left bottom top) (left bottom top))
3544 (mapcar (lambda (cell)
3545 (org-export-table-cell-borders cell info))
3546 (org-element-map tree 'table-cell 'identity)))))
3547 ;; 3. A valid column groups row must start with a "/".
3548 (org-test-with-parsed-data "
3549 | | < |
3550 | a | b |"
3551 (should
3552 (equal '((top) (top) (bottom) (bottom))
3553 (mapcar (lambda (cell)
3554 (org-export-table-cell-borders cell info))
3555 (org-element-map tree 'table-cell 'identity)))))
3556 ;; 4. Take table rules into consideration.
3557 (org-test-with-parsed-data "
3558 | 1 |
3559 |---|
3560 | 2 |"
3561 (should
3562 (equal '((below top) (bottom above))
3563 (mapcar (lambda (cell)
3564 (org-export-table-cell-borders cell info))
3565 (org-element-map tree 'table-cell 'identity)))))
3566 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
3567 ;; (resp. `bottom' and `below') borders. Any special row is
3568 ;; ignored.
3569 (org-test-with-parsed-data "
3570 |---+----|
3571 | / | |
3572 | | 1 |
3573 |---+----|"
3574 (should
3575 (equal '((bottom below top above))
3576 (last
3577 (mapcar (lambda (cell)
3578 (org-export-table-cell-borders cell info))
3579 (org-element-map tree 'table-cell 'identity)))))))
3581 (ert-deftest test-org-export/table-dimensions ()
3582 "Test `org-export-table-dimensions' specifications."
3583 ;; 1. Standard test.
3584 (org-test-with-parsed-data "
3585 | 1 | 2 | 3 |
3586 | 4 | 5 | 6 |"
3587 (should
3588 (equal '(2 . 3)
3589 (org-export-table-dimensions
3590 (org-element-map tree 'table 'identity info 'first-match) info))))
3591 ;; 2. Ignore horizontal rules and special columns.
3592 (org-test-with-parsed-data "
3593 | / | < | > |
3594 | 1 | 2 | 3 |
3595 |---+---+---|
3596 | 4 | 5 | 6 |"
3597 (should
3598 (equal '(2 . 3)
3599 (org-export-table-dimensions
3600 (org-element-map tree 'table 'identity info 'first-match) info)))))
3602 (ert-deftest test-org-export/table-cell-address ()
3603 "Test `org-export-table-cell-address' specifications."
3604 ;; 1. Standard test: index is 0-based.
3605 (org-test-with-parsed-data "| a | b |"
3606 (should
3607 (equal '((0 . 0) (0 . 1))
3608 (org-element-map tree 'table-cell
3609 (lambda (cell) (org-export-table-cell-address cell info))
3610 info))))
3611 ;; 2. Special column isn't counted, nor are special rows.
3612 (org-test-with-parsed-data "
3613 | / | <> |
3614 | | c |"
3615 (should
3616 (equal '(0 . 0)
3617 (org-export-table-cell-address
3618 (car (last (org-element-map tree 'table-cell 'identity info)))
3619 info))))
3620 ;; 3. Tables rules do not count either.
3621 (org-test-with-parsed-data "
3622 | a |
3623 |---|
3624 | b |
3625 |---|
3626 | c |"
3627 (should
3628 (equal '(2 . 0)
3629 (org-export-table-cell-address
3630 (car (last (org-element-map tree 'table-cell 'identity info)))
3631 info))))
3632 ;; 4. Return nil for special cells.
3633 (org-test-with-parsed-data "| / | a |"
3634 (should-not
3635 (org-export-table-cell-address
3636 (org-element-map tree 'table-cell 'identity nil 'first-match)
3637 info))))
3639 (ert-deftest test-org-export/get-table-cell-at ()
3640 "Test `org-export-get-table-cell-at' specifications."
3641 ;; 1. Address ignores special columns, special rows and rules.
3642 (org-test-with-parsed-data "
3643 | / | <> |
3644 | | a |
3645 |---+----|
3646 | | b |"
3647 (should
3648 (equal '("b")
3649 (org-element-contents
3650 (org-export-get-table-cell-at
3651 '(1 . 0)
3652 (org-element-map tree 'table 'identity info 'first-match)
3653 info)))))
3654 ;; 2. Return value for a non-existent address is nil.
3655 (org-test-with-parsed-data "| a |"
3656 (should-not
3657 (org-export-get-table-cell-at
3658 '(2 . 2)
3659 (org-element-map tree 'table 'identity info 'first-match)
3660 info)))
3661 (org-test-with-parsed-data "| / |"
3662 (should-not
3663 (org-export-get-table-cell-at
3664 '(0 . 0)
3665 (org-element-map tree 'table 'identity info 'first-match)
3666 info))))
3668 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
3669 "Test `org-export-table-cell-starts-colgroup-p' specifications."
3670 ;; 1. A cell at a beginning of a row always starts a column group.
3671 (org-test-with-parsed-data "| a |"
3672 (should
3673 (org-export-table-cell-starts-colgroup-p
3674 (org-element-map tree 'table-cell 'identity info 'first-match)
3675 info)))
3676 ;; 2. Special column should be ignored when determining the
3677 ;; beginning of the row.
3678 (org-test-with-parsed-data "
3679 | / | |
3680 | | a |"
3681 (should
3682 (org-export-table-cell-starts-colgroup-p
3683 (org-element-map tree 'table-cell 'identity info 'first-match)
3684 info)))
3685 ;; 2. Explicit column groups.
3686 (org-test-with-parsed-data "
3687 | / | | < |
3688 | a | b | c |"
3689 (should
3690 (equal
3691 '(yes no yes)
3692 (org-element-map tree 'table-cell
3693 (lambda (cell)
3694 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
3695 info)))))
3697 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
3698 "Test `org-export-table-cell-ends-colgroup-p' specifications."
3699 ;; 1. A cell at the end of a row always ends a column group.
3700 (org-test-with-parsed-data "| a |"
3701 (should
3702 (org-export-table-cell-ends-colgroup-p
3703 (org-element-map tree 'table-cell 'identity info 'first-match)
3704 info)))
3705 ;; 2. Special column should be ignored when determining the
3706 ;; beginning of the row.
3707 (org-test-with-parsed-data "
3708 | / | |
3709 | | a |"
3710 (should
3711 (org-export-table-cell-ends-colgroup-p
3712 (org-element-map tree 'table-cell 'identity info 'first-match)
3713 info)))
3714 ;; 3. Explicit column groups.
3715 (org-test-with-parsed-data "
3716 | / | < | |
3717 | a | b | c |"
3718 (should
3719 (equal
3720 '(yes no yes)
3721 (org-element-map tree 'table-cell
3722 (lambda (cell)
3723 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
3724 info)))))
3726 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
3727 "Test `org-export-table-row-starts-rowgroup-p' specifications."
3728 ;; 1. A row at the beginning of a table always starts a row group.
3729 ;; So does a row following a table rule.
3730 (org-test-with-parsed-data "
3731 | a |
3732 |---|
3733 | b |"
3734 (should
3735 (equal
3736 '(yes no yes)
3737 (org-element-map tree 'table-row
3738 (lambda (row)
3739 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
3740 info))))
3741 ;; 2. Special rows should be ignored when determining the beginning
3742 ;; of the row.
3743 (org-test-with-parsed-data "
3744 | / | < |
3745 | | a |
3746 |---+---|
3747 | / | < |
3748 | | b |"
3749 (should
3750 (equal
3751 '(yes no yes)
3752 (org-element-map tree 'table-row
3753 (lambda (row)
3754 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
3755 info)))))
3757 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
3758 "Test `org-export-table-row-ends-rowgroup-p' specifications."
3759 ;; 1. A row at the end of a table always ends a row group. So does
3760 ;; a row preceding a table rule.
3761 (org-test-with-parsed-data "
3762 | a |
3763 |---|
3764 | b |"
3765 (should
3766 (equal
3767 '(yes no yes)
3768 (org-element-map tree 'table-row
3769 (lambda (row)
3770 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
3771 info))))
3772 ;; 2. Special rows should be ignored when determining the beginning
3773 ;; of the row.
3774 (org-test-with-parsed-data "
3775 | | a |
3776 | / | < |
3777 |---+---|
3778 | | b |
3779 | / | < |"
3780 (should
3781 (equal
3782 '(yes no yes)
3783 (org-element-map tree 'table-row
3784 (lambda (row)
3785 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
3786 info)))))
3788 (ert-deftest test-org-export/table-row-in-header-p ()
3789 "Test `org-export-table-row-in-header-p' specifications."
3790 ;; Standard test. Separators are always nil.
3791 (should
3792 (equal
3793 '(yes no no)
3794 (org-test-with-parsed-data "| a |\n|---|\n| b |"
3795 (org-element-map tree 'table-row
3796 (lambda (row)
3797 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
3798 ;; Nil when there is no header.
3799 (should
3800 (equal
3801 '(no no)
3802 (org-test-with-parsed-data "| a |\n| b |"
3803 (org-element-map tree 'table-row
3804 (lambda (row)
3805 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
3807 (ert-deftest test-org-export/table-row-starts-header-p ()
3808 "Test `org-export-table-row-starts-header-p' specifications."
3809 ;; 1. Only the row starting the first row group starts the table
3810 ;; header.
3811 (org-test-with-parsed-data "
3812 | a |
3813 | b |
3814 |---|
3815 | c |"
3816 (should
3817 (equal
3818 '(yes no no no)
3819 (org-element-map tree 'table-row
3820 (lambda (row)
3821 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
3822 info))))
3823 ;; 2. A row cannot start an header if there's no header in the
3824 ;; table.
3825 (org-test-with-parsed-data "
3826 | a |
3827 |---|"
3828 (should-not
3829 (org-export-table-row-starts-header-p
3830 (org-element-map tree 'table-row 'identity info 'first-match)
3831 info))))
3833 (ert-deftest test-org-export/table-row-ends-header-p ()
3834 "Test `org-export-table-row-ends-header-p' specifications."
3835 ;; 1. Only the row starting the first row group starts the table
3836 ;; header.
3837 (org-test-with-parsed-data "
3838 | a |
3839 | b |
3840 |---|
3841 | c |"
3842 (should
3843 (equal
3844 '(no yes no no)
3845 (org-element-map tree 'table-row
3846 (lambda (row)
3847 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
3848 info))))
3849 ;; 2. A row cannot start an header if there's no header in the
3850 ;; table.
3851 (org-test-with-parsed-data "
3852 | a |
3853 |---|"
3854 (should-not
3855 (org-export-table-row-ends-header-p
3856 (org-element-map tree 'table-row 'identity info 'first-match)
3857 info))))
3861 ;;; Tables of Contents
3863 (ert-deftest test-org-export/collect-headlines ()
3864 "Test `org-export-collect-headlines' specifications."
3865 ;; Standard test.
3866 (should
3867 (= 2
3868 (length
3869 (org-test-with-parsed-data "* H1\n** H2"
3870 (org-export-collect-headlines info)))))
3871 ;; Do not collect headlines below optional argument.
3872 (should
3873 (= 1
3874 (length
3875 (org-test-with-parsed-data "* H1\n** H2"
3876 (org-export-collect-headlines info 1)))))
3877 ;; Never collect headlines below maximum headline level.
3878 (should
3879 (= 1
3880 (length
3881 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3882 (org-export-collect-headlines info)))))
3883 (should
3884 (= 1
3885 (length
3886 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3887 (org-export-collect-headlines info 2)))))
3888 ;; Collect headlines locally.
3889 (should
3890 (= 2
3891 (org-test-with-parsed-data "* H1\n** H2\n** H3"
3892 (let ((scope (org-element-map tree 'headline #'identity info t)))
3893 (length (org-export-collect-headlines info nil scope))))))
3894 ;; When collecting locally, optional level is relative.
3895 (should
3896 (= 1
3897 (org-test-with-parsed-data "* H1\n** H2\n*** H3"
3898 (let ((scope (org-element-map tree 'headline #'identity info t)))
3899 (length (org-export-collect-headlines info 1 scope)))))))
3903 ;;; Templates
3905 (ert-deftest test-org-export/inner-template ()
3906 "Test `inner-template' translator specifications."
3907 (should
3908 (equal "Success!"
3909 (org-test-with-temp-text "* Headline"
3910 (org-export-as
3911 (org-export-create-backend
3912 :transcoders
3913 '((inner-template . (lambda (contents info) "Success!"))
3914 (headline . (lambda (h c i) "Headline"))))))))
3915 ;; Inner template is applied even in a "body-only" export.
3916 (should
3917 (equal "Success!"
3918 (org-test-with-temp-text "* Headline"
3919 (org-export-as
3920 (org-export-create-backend
3921 :transcoders '((inner-template . (lambda (c i) "Success!"))
3922 (headline . (lambda (h c i) "Headline"))))
3923 nil nil 'body-only)))))
3925 (ert-deftest test-org-export/template ()
3926 "Test `template' translator specifications."
3927 (should
3928 (equal "Success!"
3929 (org-test-with-temp-text "* Headline"
3930 (org-export-as
3931 (org-export-create-backend
3932 :transcoders '((template . (lambda (contents info) "Success!"))
3933 (headline . (lambda (h c i) "Headline"))))))))
3934 ;; Template is not applied in a "body-only" export.
3935 (should-not
3936 (equal "Success!"
3937 (org-test-with-temp-text "* Headline"
3938 (org-export-as
3939 (org-export-create-backend
3940 :transcoders '((template . (lambda (contents info) "Success!"))
3941 (headline . (lambda (h c i) "Headline"))))
3942 nil nil 'body-only)))))
3946 ;;; Topology
3948 (ert-deftest test-org-export/get-next-element ()
3949 "Test `org-export-get-next-element' specifications."
3950 ;; Standard test.
3951 (should
3952 (equal "b"
3953 (org-test-with-parsed-data "* Headline\n*a* b"
3954 (org-export-get-next-element
3955 (org-element-map tree 'bold 'identity info t) info))))
3956 ;; Return nil when no previous element.
3957 (should-not
3958 (org-test-with-parsed-data "* Headline\na *b*"
3959 (org-export-get-next-element
3960 (org-element-map tree 'bold 'identity info t) info)))
3961 ;; Non-exportable elements are ignored.
3962 (should-not
3963 (let ((org-export-with-timestamps nil))
3964 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
3965 (org-export-get-next-element
3966 (org-element-map tree 'entity 'identity info t) info))))
3967 ;; Find next element in secondary strings.
3968 (should
3969 (eq 'verbatim
3970 (org-test-with-parsed-data "* a =verb="
3971 (org-element-type
3972 (org-export-get-next-element
3973 (org-element-map tree 'plain-text 'identity info t) info)))))
3974 (should
3975 (eq 'verbatim
3976 (org-test-with-parsed-data "* /italic/ =verb="
3977 (org-element-type
3978 (org-export-get-next-element
3979 (org-element-map tree 'italic 'identity info t) info)))))
3980 ;; Find next element in document keywords.
3981 (should
3982 (eq 'verbatim
3983 (org-test-with-parsed-data "#+TITLE: a =verb="
3984 (org-element-type
3985 (org-export-get-next-element
3986 (org-element-map
3987 (plist-get info :title) 'plain-text 'identity info t) info)))))
3988 ;; Find next element in parsed affiliated keywords.
3989 (should
3990 (eq 'verbatim
3991 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
3992 (org-element-type
3993 (org-export-get-next-element
3994 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3995 ;; With optional argument N, return a list containing all the
3996 ;; following elements.
3997 (should
3998 (equal
3999 '(bold code underline)
4000 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4001 (mapcar 'car
4002 (org-export-get-next-element
4003 (org-element-map tree 'italic 'identity info t) info t)))))
4004 ;; When N is a positive integer, return a list containing up to
4005 ;; N following elements.
4006 (should
4007 (equal
4008 '(bold code)
4009 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4010 (mapcar 'car
4011 (org-export-get-next-element
4012 (org-element-map tree 'italic 'identity info t) info 2))))))
4014 (ert-deftest test-org-export/get-previous-element ()
4015 "Test `org-export-get-previous-element' specifications."
4016 ;; Standard test.
4017 (should
4018 (equal "a "
4019 (org-test-with-parsed-data "* Headline\na *b*"
4020 (org-export-get-previous-element
4021 (org-element-map tree 'bold 'identity info t) info))))
4022 ;; Return nil when no previous element.
4023 (should-not
4024 (org-test-with-parsed-data "* Headline\n*a* b"
4025 (org-export-get-previous-element
4026 (org-element-map tree 'bold 'identity info t) info)))
4027 ;; Non-exportable elements are ignored.
4028 (should-not
4029 (let ((org-export-with-timestamps nil))
4030 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
4031 (org-export-get-previous-element
4032 (org-element-map tree 'entity 'identity info t) info))))
4033 ;; Find previous element in secondary strings.
4034 (should
4035 (eq 'verbatim
4036 (org-test-with-parsed-data "* =verb= a"
4037 (org-element-type
4038 (org-export-get-previous-element
4039 (org-element-map tree 'plain-text 'identity info t) info)))))
4040 (should
4041 (eq 'verbatim
4042 (org-test-with-parsed-data "* =verb= /italic/"
4043 (org-element-type
4044 (org-export-get-previous-element
4045 (org-element-map tree 'italic 'identity info t) info)))))
4046 ;; Find previous element in document keywords.
4047 (should
4048 (eq 'verbatim
4049 (org-test-with-parsed-data "#+TITLE: =verb= a"
4050 (org-element-type
4051 (org-export-get-previous-element
4052 (org-element-map
4053 (plist-get info :title) 'plain-text 'identity info t) info)))))
4054 ;; Find previous element in parsed affiliated keywords.
4055 (should
4056 (eq 'verbatim
4057 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
4058 (org-element-type
4059 (org-export-get-previous-element
4060 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4061 ;; With optional argument N, return a list containing up to
4062 ;; N previous elements.
4063 (should
4064 (equal '(underline italic bold)
4065 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4066 (mapcar 'car
4067 (org-export-get-previous-element
4068 (org-element-map tree 'code 'identity info t) info t)))))
4069 ;; When N is a positive integer, return a list containing up to
4070 ;; N previous elements.
4071 (should
4072 (equal '(italic bold)
4073 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4074 (mapcar 'car
4075 (org-export-get-previous-element
4076 (org-element-map tree 'code 'identity info t) info 2))))))
4079 (provide 'test-ox)
4080 ;;; test-org-export.el end here