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