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