ox: Support unnumbered headlines via property
[org-mode/org-tableheadings.git] / testing / lisp / test-ox.el
blob7139dd490fdbfeba1d6c535d733c860a49b6e641
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 be provided a fallback
1536 ;; definition.
1537 (should
1538 (org-test-with-parsed-data "[fn:1]"
1539 (org-export-get-footnote-definition
1540 (org-element-map tree 'footnote-reference 'identity info t) info)))
1541 ;; 7. Footnote section should be ignored in TOC and in headlines
1542 ;; numbering.
1543 (should
1544 (= 1 (let ((org-footnote-section "Footnotes"))
1545 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
1546 (org-export-collect-headlines info))))))
1547 (should
1548 (equal '(2)
1549 (let ((org-footnote-section "Footnotes"))
1550 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
1551 (org-element-map tree 'headline
1552 (lambda (hl)
1553 (when (equal (org-element-property :raw-value hl) "H2")
1554 (org-export-get-headline-number hl info)))
1555 info t)))))))
1559 ;;; Headlines and Inlinetasks
1561 (ert-deftest test-org-export/get-relative-level ()
1562 "Test `org-export-get-relative-level' specifications."
1563 ;; Standard test.
1564 (should
1565 (equal '(1 2)
1566 (let ((org-odd-levels-only nil))
1567 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1568 (org-element-map tree 'headline
1569 (lambda (h) (org-export-get-relative-level h info))
1570 info)))))
1571 ;; Missing levels
1572 (should
1573 (equal '(1 3)
1574 (let ((org-odd-levels-only nil))
1575 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
1576 (org-element-map tree 'headline
1577 (lambda (h) (org-export-get-relative-level h info))
1578 info))))))
1580 (ert-deftest test-org-export/low-level-p ()
1581 "Test `org-export-low-level-p' specifications."
1582 (should
1583 (equal
1584 '(no yes)
1585 (let ((org-odd-levels-only nil))
1586 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1587 (org-element-map tree 'headline
1588 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
1589 (plist-put info :headline-levels 1)))))))
1591 (ert-deftest test-org-export/get-headline-number ()
1592 "Test `org-export-get-headline-number' specifications."
1593 ;; Standard test.
1594 (should
1595 (equal
1596 '((1) (1 1))
1597 (let ((org-odd-levels-only nil))
1598 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1599 (org-element-map tree 'headline
1600 (lambda (h) (org-export-get-headline-number h info))
1601 info)))))
1602 ;; Missing levels are replaced with 0.
1603 (should
1604 (equal
1605 '((1) (1 0 1))
1606 (let ((org-odd-levels-only nil))
1607 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
1608 (org-element-map tree 'headline
1609 (lambda (h) (org-export-get-headline-number h info))
1610 info))))))
1612 (ert-deftest test-org-export/numbered-headline-p ()
1613 "Test `org-export-numbered-headline-p' specifications."
1614 ;; If `:section-numbers' is nil, never number headlines.
1615 (should-not
1616 (org-test-with-parsed-data "* Headline"
1617 (org-element-map tree 'headline
1618 (lambda (h) (org-export-numbered-headline-p h info))
1619 (plist-put info :section-numbers nil))))
1620 ;; If `:section-numbers' is a number, only number headlines with
1621 ;; a level greater that it.
1622 (should
1623 (equal
1624 '(yes no)
1625 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1626 (org-element-map tree 'headline
1627 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
1628 (plist-put info :section-numbers 1)))))
1629 ;; Otherwise, headlines are always numbered.
1630 (should
1631 (org-test-with-parsed-data "* Headline"
1632 (org-element-map tree 'headline
1633 (lambda (h) (org-export-numbered-headline-p h info))
1634 (plist-put info :section-numbers t)))))
1636 (ert-deftest test-org-export/org-export-get-headline-id ()
1637 "Test `org-export-get-headline-id' specifications."
1638 ;; Numbered headlines have IDs akin to "sec-N".
1639 (should
1640 (equal "sec-1"
1641 (org-test-with-parsed-data "* H"
1642 (org-export-get-headline-id
1643 (org-element-map tree 'headline #'identity info t)
1644 info))))
1645 ;; The ID of numbered headlines reflect the hierarchy.
1646 (should
1647 (equal "sec-1-1"
1648 (org-test-with-parsed-data "* H1\n** H2"
1649 (org-export-get-headline-id
1650 (org-element-map tree 'headline
1651 (lambda (h)
1652 (and (equal "H2" (org-element-property :raw-value h)) h))
1653 info t)
1654 info))))
1655 ;; Unnumbered headlines have IDs akin to "unnumbered-N".
1656 (should
1657 (equal "unnumbered-1"
1658 (org-test-with-parsed-data
1659 "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
1660 (org-export-get-headline-id
1661 (org-element-map tree 'headline #'identity info t)
1662 info))))
1663 ;; The ID of Unnumbered headlines do not reflect the hierarchy.
1664 (should
1665 (equal "unnumbered-2"
1666 (org-test-with-parsed-data
1667 "* H1\n:PROPERTIES:\n:UNNUMBERED: t\n:END:\n** H2"
1668 (org-export-get-headline-id
1669 (org-element-map tree 'headline
1670 (lambda (h)
1671 (and (equal "H2" (org-element-property :raw-value h)) h))
1672 info t)
1673 info))))
1674 ;; When #+OPTIONS: num:nil all headlines are unnumbered.
1675 (should
1676 (equal "unnumbered-1"
1677 (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
1678 (org-export-get-headline-id
1679 (org-element-map tree 'headline 'identity info t)
1680 info))))
1681 ;; UNNUMBERED ignores inheritance. Any non-nil value among
1682 ;; ancestors disables numbering.
1683 (should
1684 (org-test-with-parsed-data
1685 "* H
1686 :PROPERTIES:
1687 :UNNUMBERED: t
1688 :END:
1689 ** H2
1690 :PROPERTIES:
1691 :UNNUMBERED: nil
1692 :END:
1693 *** H3"
1694 (org-every
1695 (lambda (h) (not (org-export-numbered-headline-p h info)))
1696 (org-element-map tree 'headline #'identity info)))))
1698 (ert-deftest test-org-export/number-to-roman ()
1699 "Test `org-export-number-to-roman' specifications."
1700 ;; If number is negative, return it as a string.
1701 (should (equal (org-export-number-to-roman -1) "-1"))
1702 ;; Otherwise, return it as a roman number.
1703 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
1705 (ert-deftest test-org-export/get-optional-title ()
1706 "Test `org-export-get-alt-title' specifications."
1707 ;; If ALT_TITLE property is defined, use it.
1708 (should
1709 (equal '("opt")
1710 (org-test-with-parsed-data
1711 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
1712 (org-export-get-alt-title
1713 (org-element-map tree 'headline 'identity info t)
1714 info))))
1715 ;; Otherwise, fall-back to regular title.
1716 (should
1717 (equal '("Headline")
1718 (org-test-with-parsed-data "* Headline"
1719 (org-export-get-alt-title
1720 (org-element-map tree 'headline 'identity info t)
1721 info)))))
1723 (ert-deftest test-org-export/get-tags ()
1724 "Test `org-export-get-tags' specifications."
1725 (let ((org-export-exclude-tags '("noexport"))
1726 (org-export-select-tags '("export")))
1727 ;; Standard test: tags which are not a select tag, an exclude tag,
1728 ;; or specified as optional argument shouldn't be ignored.
1729 (should
1730 (org-test-with-parsed-data "* Headline :tag:"
1731 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1732 info)))
1733 ;; Exclude tags are removed.
1734 (should-not
1735 (org-test-with-parsed-data "* Headline :noexport:"
1736 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1737 info)))
1738 ;; Select tags are removed.
1739 (should-not
1740 (org-test-with-parsed-data "* Headline :export:"
1741 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1742 info)))
1743 (should
1744 (equal
1745 '("tag")
1746 (org-test-with-parsed-data "* Headline :tag:export:"
1747 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1748 info))))
1749 ;; Tags provided in the optional argument are also ignored.
1750 (should-not
1751 (org-test-with-parsed-data "* Headline :ignore:"
1752 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1753 info '("ignore"))))
1754 ;; Allow tag inheritance.
1755 (should
1756 (equal
1757 '(("tag") ("tag"))
1758 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
1759 (org-element-map tree 'headline
1760 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
1761 ;; Tag inheritance checks FILETAGS keywords.
1762 (should
1763 (equal
1764 '(("a" "b" "tag"))
1765 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
1766 (org-element-map tree 'headline
1767 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
1769 (ert-deftest test-org-export/get-node-property ()
1770 "Test`org-export-get-node-property' specifications."
1771 ;; Standard test.
1772 (should
1773 (equal "value"
1774 (org-test-with-parsed-data "* Headline
1775 :PROPERTIES:
1776 :prop: value
1777 :END:"
1778 (org-export-get-node-property
1779 :PROP (org-element-map tree 'headline 'identity nil t)))))
1780 ;; Test inheritance.
1781 (should
1782 (equal "value"
1783 (org-test-with-parsed-data "* Parent
1784 :PROPERTIES:
1785 :prop: value
1786 :END:
1787 ** Headline
1788 Paragraph"
1789 (org-export-get-node-property
1790 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
1791 ;; Cannot return a value before the first headline.
1792 (should-not
1793 (org-test-with-parsed-data "Paragraph
1794 * Headline
1795 :PROPERTIES:
1796 :prop: value
1797 :END:"
1798 (org-export-get-node-property
1799 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
1801 (ert-deftest test-org-export/get-category ()
1802 "Test `org-export-get-category' specifications."
1803 ;; Standard test.
1804 (should
1805 (equal "value"
1806 (org-test-with-parsed-data "* Headline
1807 :PROPERTIES:
1808 :CATEGORY: value
1809 :END:"
1810 (org-export-get-category
1811 (org-element-map tree 'headline 'identity nil t) info))))
1812 ;; Test inheritance from a parent headline.
1813 (should
1814 (equal '("value" "value")
1815 (org-test-with-parsed-data "* Headline1
1816 :PROPERTIES:
1817 :CATEGORY: value
1818 :END:
1819 ** Headline2"
1820 (org-element-map tree 'headline
1821 (lambda (hl) (org-export-get-category hl info)) info))))
1822 ;; Test inheritance from #+CATEGORY keyword
1823 (should
1824 (equal "value"
1825 (org-test-with-parsed-data "#+CATEGORY: value
1826 * Headline"
1827 (org-export-get-category
1828 (org-element-map tree 'headline 'identity nil t) info))))
1829 ;; Test inheritance from file name.
1830 (should
1831 (equal "test"
1832 (org-test-with-parsed-data "* Headline"
1833 (let ((info (plist-put info :input-file "~/test.org")))
1834 (org-export-get-category
1835 (org-element-map tree 'headline 'identity nil t) info)))))
1836 ;; Fall-back value.
1837 (should
1838 (equal "???"
1839 (org-test-with-parsed-data "* Headline"
1840 (org-export-get-category
1841 (org-element-map tree 'headline 'identity nil t) info)))))
1843 (ert-deftest test-org-export/first-sibling-p ()
1844 "Test `org-export-first-sibling-p' specifications."
1845 ;; Standard test.
1846 (should
1847 (equal
1848 '(yes yes no)
1849 (org-test-with-parsed-data "* H\n** H 2\n** H 3"
1850 (org-element-map tree 'headline
1851 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1852 info))))
1853 (should
1854 (equal '(yes no)
1855 (org-test-with-parsed-data "- item\n\n para"
1856 (org-element-map tree 'paragraph
1857 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1858 info))))
1859 ;; Ignore sections for headlines.
1860 (should
1861 (equal '(yes yes)
1862 (org-test-with-parsed-data "* H\nSection\n** H 2"
1863 (org-element-map tree 'headline
1864 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1865 info))))
1866 ;; Ignore headlines not exported.
1867 (should
1868 (equal
1869 '(yes)
1870 (let ((org-export-exclude-tags '("ignore")))
1871 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
1872 (org-element-map tree 'headline
1873 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1874 info))))))
1876 (ert-deftest test-org-export/last-sibling-p ()
1877 "Test `org-export-last-sibling-p' specifications."
1878 ;; Standard test.
1879 (should
1880 (equal
1881 '(yes no yes)
1882 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1883 (org-element-map tree 'headline
1884 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1885 info))))
1886 (should
1887 (equal '(no yes)
1888 (org-test-with-parsed-data "- item\n\n para"
1889 (org-element-map tree 'paragraph
1890 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1891 info))))
1892 ;; Ignore headlines not exported.
1893 (should
1894 (equal
1895 '(yes)
1896 (let ((org-export-exclude-tags '("ignore")))
1897 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
1898 (org-element-map tree 'headline
1899 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1900 info))))))
1902 (ert-deftest test-org-export/handle-inlinetasks ()
1903 "Test inlinetask export."
1904 ;; Inlinetask with an exclude tag.
1905 (when (featurep 'org-inlinetask)
1906 (should
1907 (equal
1909 (let ((org-inlinetask-min-level 3))
1910 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
1911 (org-export-as (org-test-default-backend)
1912 nil nil nil '(:exclude-tags ("noexp")))))))
1913 ;; Inlinetask with an include tag.
1914 (should
1915 (equal
1916 "* H2\n*** Inline :exp:\n"
1917 (let ((org-inlinetask-min-level 3)
1918 (org-tags-column 0))
1919 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
1920 (org-export-as (org-test-default-backend)
1921 nil nil nil '(:select-tags ("exp")))))))
1922 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
1923 (should
1924 (equal ""
1925 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
1926 (org-inlinetask-min-level 3))
1927 (org-test-with-temp-text "*** TODO Inline"
1928 (org-export-as (org-test-default-backend)
1929 nil nil nil '(:with-tasks nil))))))))
1933 ;;; Keywords
1935 (ert-deftest test-org-export/get-date ()
1936 "Test `org-export-get-date' specifications."
1937 ;; Return a properly formatted string when
1938 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
1939 ;; consists in a single timestamp.
1940 (should
1941 (equal "29 03 2012"
1942 (let ((org-export-date-timestamp-format "%d %m %Y"))
1943 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1944 (org-export-get-date info)))))
1945 ;; Return a secondary string otherwise.
1946 (should-not
1947 (stringp
1948 (let ((org-export-date-timestamp-format nil))
1949 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1950 (org-export-get-date info)))))
1951 (should
1952 (equal '("Date")
1953 (org-test-with-parsed-data "#+DATE: Date"
1954 (org-export-get-date info))))
1955 ;; Optional argument has precedence over
1956 ;; `org-export-date-timestamp-format'.
1957 (should
1958 (equal "29 03"
1959 (let ((org-export-date-timestamp-format "%d %m %Y"))
1960 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1961 (org-export-get-date info "%d %m"))))))
1965 ;;; Links
1967 (ert-deftest test-org-export/get-coderef-format ()
1968 "Test `org-export-get-coderef-format' specifications."
1969 ;; A link without description returns "%s"
1970 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
1971 "%s"))
1972 ;; Return "%s" when path is matched within description.
1973 (should (equal (org-export-get-coderef-format "path" "desc (path)")
1974 "desc %s"))
1975 ;; Otherwise return description.
1976 (should (equal (org-export-get-coderef-format "path" "desc")
1977 "desc")))
1979 (ert-deftest test-org-export/inline-image-p ()
1980 "Test `org-export-inline-image-p' specifications."
1981 (should
1982 (org-export-inline-image-p
1983 (org-test-with-temp-text "[[#id]]"
1984 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
1985 '(("custom-id" . "id")))))
1987 (ert-deftest test-org-export/fuzzy-link ()
1988 "Test fuzzy links specifications."
1989 ;; Link to an headline should return headline's number.
1990 (org-test-with-parsed-data
1991 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
1992 (should
1993 ;; Note: Headline's number is in fact a list of numbers.
1994 (equal '(2)
1995 (org-element-map tree 'link
1996 (lambda (link)
1997 (org-export-get-ordinal
1998 (org-export-resolve-fuzzy-link link info) info)) info t))))
1999 ;; Link to a target in an item should return item's number.
2000 (org-test-with-parsed-data
2001 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
2002 (should
2003 ;; Note: Item's number is in fact a list of numbers.
2004 (equal '(1 2)
2005 (org-element-map tree 'link
2006 (lambda (link)
2007 (org-export-get-ordinal
2008 (org-export-resolve-fuzzy-link link info) info)) info t))))
2009 ;; Link to a target in a footnote should return footnote's number.
2010 (org-test-with-parsed-data "
2011 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
2012 (should
2013 (equal '(2 3)
2014 (org-element-map tree 'link
2015 (lambda (link)
2016 (org-export-get-ordinal
2017 (org-export-resolve-fuzzy-link link info) info)) info))))
2018 ;; Link to a named element should return sequence number of that
2019 ;; element.
2020 (org-test-with-parsed-data
2021 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
2022 (should
2023 (= 2
2024 (org-element-map tree 'link
2025 (lambda (link)
2026 (org-export-get-ordinal
2027 (org-export-resolve-fuzzy-link link info) info)) info t))))
2028 ;; Link to a target not within an item, a table, a footnote
2029 ;; reference or definition should return section number.
2030 (org-test-with-parsed-data
2031 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
2032 (should
2033 (equal '(2)
2034 (org-element-map tree 'link
2035 (lambda (link)
2036 (org-export-get-ordinal
2037 (org-export-resolve-fuzzy-link link info) info)) info t))))
2038 ;; Space are not significant when matching a fuzzy link.
2039 (should
2040 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
2041 (org-element-map tree 'link
2042 (lambda (link) (org-export-resolve-fuzzy-link link info))
2043 info t)))
2044 ;; Statistics cookies are ignored for headline match.
2045 (should
2046 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
2047 (org-element-map tree 'link
2048 (lambda (link) (org-export-resolve-fuzzy-link link info))
2049 info t)))
2050 (should
2051 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
2052 (org-element-map tree 'link
2053 (lambda (link) (org-export-resolve-fuzzy-link link info))
2054 info t)))
2055 ;; Headline match is position dependent.
2056 (should-not
2057 (apply
2059 (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
2060 (org-element-map tree 'link
2061 (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
2063 (ert-deftest test-org-export/resolve-coderef ()
2064 "Test `org-export-resolve-coderef' specifications."
2065 (let ((org-coderef-label-format "(ref:%s)"))
2066 ;; 1. A link to a "-n -k -r" block returns line number.
2067 (org-test-with-parsed-data
2068 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
2069 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2070 (org-test-with-parsed-data
2071 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2072 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2073 ;; 2. A link to a "-n -r" block returns line number.
2074 (org-test-with-parsed-data
2075 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
2076 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2077 (org-test-with-parsed-data
2078 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2079 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2080 ;; 3. A link to a "-n" block returns coderef.
2081 (org-test-with-parsed-data
2082 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2083 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2084 (org-test-with-parsed-data
2085 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
2086 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2087 ;; 4. A link to a "-r" block returns line number.
2088 (org-test-with-parsed-data
2089 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2090 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2091 (org-test-with-parsed-data
2092 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
2093 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2094 ;; 5. A link to a block without a switch returns coderef.
2095 (org-test-with-parsed-data
2096 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2097 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2098 (org-test-with-parsed-data
2099 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
2100 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2101 ;; 6. Correctly handle continued line numbers. A "+n" switch
2102 ;; should resume numbering from previous block with numbered
2103 ;; lines, ignoring blocks not numbering lines in the process.
2104 ;; A "-n" switch resets count.
2105 (org-test-with-parsed-data "
2106 #+BEGIN_EXAMPLE -n
2107 Text.
2108 #+END_EXAMPLE
2110 #+BEGIN_SRC emacs-lisp
2111 \(- 1 1)
2112 #+END_SRC
2114 #+BEGIN_SRC emacs-lisp +n -r
2115 \(+ 1 1) (ref:addition)
2116 #+END_SRC
2118 #+BEGIN_EXAMPLE -n -r
2119 Another text. (ref:text)
2120 #+END_EXAMPLE"
2121 (should (= (org-export-resolve-coderef "addition" info) 2))
2122 (should (= (org-export-resolve-coderef "text" info) 1)))
2123 ;; 7. Recognize coderef with user-specified syntax.
2124 (org-test-with-parsed-data
2125 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
2126 (should (equal (org-export-resolve-coderef "text" info) "text")))))
2128 (ert-deftest test-org-export/resolve-fuzzy-link ()
2129 "Test `org-export-resolve-fuzzy-link' specifications."
2130 ;; Match target objects.
2131 (should
2132 (org-test-with-parsed-data "<<target>> [[target]]"
2133 (org-export-resolve-fuzzy-link
2134 (org-element-map tree 'link 'identity info t) info)))
2135 ;; Match named elements.
2136 (should
2137 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
2138 (org-export-resolve-fuzzy-link
2139 (org-element-map tree 'link 'identity info t) info)))
2140 ;; Match exact headline's name.
2141 (should
2142 (org-test-with-parsed-data "* My headline\n[[My headline]]"
2143 (org-export-resolve-fuzzy-link
2144 (org-element-map tree 'link 'identity info t) info)))
2145 ;; Targets objects have priority over named elements and headline
2146 ;; titles.
2147 (should
2148 (eq 'target
2149 (org-test-with-parsed-data
2150 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
2151 (org-element-type
2152 (org-export-resolve-fuzzy-link
2153 (org-element-map tree 'link 'identity info t) info)))))
2154 ;; Named elements have priority over headline titles.
2155 (should
2156 (eq 'paragraph
2157 (org-test-with-parsed-data
2158 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
2159 (org-element-type
2160 (org-export-resolve-fuzzy-link
2161 (org-element-map tree 'link 'identity info t) info)))))
2162 ;; If link's path starts with a "*", only match headline titles,
2163 ;; though.
2164 (should
2165 (eq 'headline
2166 (org-test-with-parsed-data
2167 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
2168 (org-element-type
2169 (org-export-resolve-fuzzy-link
2170 (org-element-map tree 'link 'identity info t) info)))))
2171 ;; Return nil if no match.
2172 (should-not
2173 (org-test-with-parsed-data "[[target]]"
2174 (org-export-resolve-fuzzy-link
2175 (org-element-map tree 'link 'identity info t) info)))
2176 ;; Match fuzzy link even when before first headline.
2177 (should
2178 (eq 'headline
2179 (org-test-with-parsed-data "[[hl]]\n* hl"
2180 (org-element-type
2181 (org-export-resolve-fuzzy-link
2182 (org-element-map tree 'link 'identity info t) info))))))
2184 (ert-deftest test-org-export/resolve-id-link ()
2185 "Test `org-export-resolve-id-link' specifications."
2186 ;; 1. Regular test for custom-id link.
2187 (org-test-with-parsed-data "* Headline1
2188 :PROPERTIES:
2189 :CUSTOM_ID: test
2190 :END:
2191 * Headline 2
2192 \[[#test]]"
2193 (should
2194 (org-export-resolve-id-link
2195 (org-element-map tree 'link 'identity info t) info)))
2196 ;; 2. Failing test for custom-id link.
2197 (org-test-with-parsed-data "* Headline1
2198 :PROPERTIES:
2199 :CUSTOM_ID: test
2200 :END:
2201 * Headline 2
2202 \[[#no-match]]"
2203 (should-not
2204 (org-export-resolve-id-link
2205 (org-element-map tree 'link 'identity info t) info)))
2206 ;; 3. Test for internal id target.
2207 (org-test-with-parsed-data "* Headline1
2208 :PROPERTIES:
2209 :ID: aaaa
2210 :END:
2211 * Headline 2
2212 \[[id:aaaa]]"
2213 (should
2214 (org-export-resolve-id-link
2215 (org-element-map tree 'link 'identity info t) info)))
2216 ;; 4. Test for external id target.
2217 (org-test-with-parsed-data "[[id:aaaa]]"
2218 (should
2219 (org-export-resolve-id-link
2220 (org-element-map tree 'link 'identity info t)
2221 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
2223 (ert-deftest test-org-export/resolve-radio-link ()
2224 "Test `org-export-resolve-radio-link' specifications."
2225 ;; Standard test.
2226 (should
2227 (org-test-with-temp-text "<<<radio>>> radio"
2228 (org-update-radio-target-regexp)
2229 (let* ((tree (org-element-parse-buffer))
2230 (info `(:parse-tree ,tree)))
2231 (org-export-resolve-radio-link
2232 (org-element-map tree 'link 'identity info t)
2233 info))))
2234 ;; Radio targets are case-insensitive.
2235 (should
2236 (org-test-with-temp-text "<<<RADIO>>> radio"
2237 (org-update-radio-target-regexp)
2238 (let* ((tree (org-element-parse-buffer))
2239 (info `(:parse-tree ,tree)))
2240 (org-export-resolve-radio-link
2241 (org-element-map tree 'link 'identity info t)
2242 info))))
2243 ;; Radio target with objects.
2244 (should
2245 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
2246 (org-update-radio-target-regexp)
2247 (let* ((tree (org-element-parse-buffer))
2248 (info `(:parse-tree ,tree)))
2249 (org-export-resolve-radio-link
2250 (org-element-map tree 'link 'identity info t)
2251 info))))
2252 ;; Radio target with objects at its beginning.
2253 (should
2254 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
2255 (org-update-radio-target-regexp)
2256 (let* ((tree (org-element-parse-buffer))
2257 (info `(:parse-tree ,tree)))
2258 (org-export-resolve-radio-link
2259 (org-element-map tree 'link 'identity info t)
2260 info))))
2261 ;; Radio link next to an apostrophe.
2262 (should
2263 (org-test-with-temp-text "<<<radio>>> radio's"
2264 (org-update-radio-target-regexp)
2265 (let* ((tree (org-element-parse-buffer))
2266 (info `(:parse-tree ,tree)))
2267 (org-export-resolve-radio-link
2268 (org-element-map tree 'link 'identity info t)
2269 info))))
2270 ;; Multiple radio targets.
2271 (should
2272 (equal '("radio1" "radio2")
2273 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
2274 (org-update-radio-target-regexp)
2275 (let* ((tree (org-element-parse-buffer))
2276 (info `(:parse-tree ,tree)))
2277 (org-element-map tree 'link
2278 (lambda (link)
2279 (org-element-property
2280 :value (org-export-resolve-radio-link link info)))
2281 info)))))
2282 ;; Radio target is whitespace insensitive.
2283 (should
2284 (org-test-with-temp-text "<<<a radio>>> a\n radio"
2285 (org-update-radio-target-regexp)
2286 (let* ((tree (org-element-parse-buffer))
2287 (info `(:parse-tree ,tree)))
2288 (org-element-map tree 'link
2289 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
2293 ;;; Src-block and example-block
2295 (ert-deftest test-org-export/unravel-code ()
2296 "Test `org-export-unravel-code' function."
2297 ;; Code without reference.
2298 (should
2299 (equal '("(+ 1 1)\n")
2300 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2301 (org-export-unravel-code (org-element-at-point)))))
2302 ;; Code with reference.
2303 (should
2304 (equal '("(+ 1 1)\n" (1 . "test"))
2305 (org-test-with-temp-text
2306 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
2307 (let ((org-coderef-label-format "(ref:%s)"))
2308 (org-export-unravel-code (org-element-at-point))))))
2309 ;; Code with user-defined reference.
2310 (should
2311 (equal
2312 '("(+ 1 1)\n" (1 . "test"))
2313 (org-test-with-temp-text
2314 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
2315 (let ((org-coderef-label-format "(ref:%s)"))
2316 (org-export-unravel-code (org-element-at-point))))))
2317 ;; Code references keys are relative to the current block.
2318 (should
2319 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
2320 (org-test-with-temp-text "
2321 #+BEGIN_EXAMPLE -n
2322 \(+ 1 1)
2323 #+END_EXAMPLE
2324 #+BEGIN_EXAMPLE +n
2325 \(+ 2 2)
2326 \(+ 3 3) (ref:one)
2327 #+END_EXAMPLE"
2328 (goto-line 5)
2329 (let ((org-coderef-label-format "(ref:%s)"))
2330 (org-export-unravel-code (org-element-at-point)))))))
2332 (ert-deftest test-org-export/format-code-default ()
2333 "Test `org-export-format-code-default' specifications."
2334 ;; Return the empty string when code is empty.
2335 (should
2336 (equal ""
2337 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
2338 (org-export-format-code-default
2339 (org-element-map tree 'src-block 'identity info t) info))))
2340 ;; Number lines, two whitespace characters before the actual loc.
2341 (should
2342 (equal "1 a\n2 b\n"
2343 (org-test-with-parsed-data
2344 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
2345 (org-export-format-code-default
2346 (org-element-map tree 'src-block 'identity info t) info))))
2347 ;; Put references 6 whitespace characters after the widest line,
2348 ;; wrapped within parenthesis.
2349 (should
2350 (equal "123 (a)\n1 (b)\n"
2351 (let ((org-coderef-label-format "(ref:%s)"))
2352 (org-test-with-parsed-data
2353 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
2354 (org-export-format-code-default
2355 (org-element-map tree 'src-block 'identity info t) info))))))
2359 ;;; Smart Quotes
2361 (ert-deftest test-org-export/activate-smart-quotes ()
2362 "Test `org-export-activate-smart-quotes' specifications."
2363 ;; Opening double quotes: standard test.
2364 (should
2365 (equal
2366 '("some &ldquo;paragraph")
2367 (let ((org-export-default-language "en"))
2368 (org-test-with-parsed-data "some \"paragraph"
2369 (org-element-map tree 'plain-text
2370 (lambda (s) (org-export-activate-smart-quotes s :html info))
2371 info)))))
2372 ;; Opening quotes: at the beginning of a paragraph.
2373 (should
2374 (equal
2375 '("&ldquo;begin")
2376 (let ((org-export-default-language "en"))
2377 (org-test-with-parsed-data "\"begin"
2378 (org-element-map tree 'plain-text
2379 (lambda (s) (org-export-activate-smart-quotes s :html info))
2380 info)))))
2381 ;; Opening quotes: after an object.
2382 (should
2383 (equal
2384 '("&ldquo;begin")
2385 (let ((org-export-default-language "en"))
2386 (org-test-with-parsed-data "=verb= \"begin"
2387 (org-element-map tree 'plain-text
2388 (lambda (s) (org-export-activate-smart-quotes s :html info))
2389 info)))))
2390 ;; Closing quotes: standard test.
2391 (should
2392 (equal
2393 '("some&rdquo; paragraph")
2394 (let ((org-export-default-language "en"))
2395 (org-test-with-parsed-data "some\" paragraph"
2396 (org-element-map tree 'plain-text
2397 (lambda (s) (org-export-activate-smart-quotes s :html info))
2398 info)))))
2399 ;; Closing quotes: at the end of a paragraph.
2400 (should
2401 (equal
2402 '("end&rdquo;")
2403 (let ((org-export-default-language "en"))
2404 (org-test-with-parsed-data "end\""
2405 (org-element-map tree 'plain-text
2406 (lambda (s) (org-export-activate-smart-quotes s :html info))
2407 info)))))
2408 ;; Apostrophe: standard test.
2409 (should
2410 (equal
2411 '("It shouldn&rsquo;t fail")
2412 (let ((org-export-default-language "en"))
2413 (org-test-with-parsed-data "It shouldn't fail"
2414 (org-element-map tree 'plain-text
2415 (lambda (s) (org-export-activate-smart-quotes s :html info))
2416 info)))))
2417 ;; Apostrophe: before an object.
2418 (should
2419 (equal
2420 '("a&rsquo;")
2421 (let ((org-export-default-language "en"))
2422 (org-test-with-parsed-data "a'=b="
2423 (org-element-map tree 'plain-text
2424 (lambda (s) (org-export-activate-smart-quotes s :html info))
2425 info)))))
2426 ;; Apostrophe: after an object.
2427 (should
2428 (equal
2429 '("&rsquo;s")
2430 (let ((org-export-default-language "en"))
2431 (org-test-with-parsed-data "=code='s"
2432 (org-element-map tree 'plain-text
2433 (lambda (s) (org-export-activate-smart-quotes s :html info))
2434 info)))))
2435 ;; Special case: isolated quotes.
2436 (should
2437 (equal '("&ldquo;" "&rdquo;")
2438 (let ((org-export-default-language "en"))
2439 (org-test-with-parsed-data "\"$x$\""
2440 (org-element-map tree 'plain-text
2441 (lambda (s) (org-export-activate-smart-quotes s :html info))
2442 info)))))
2443 ;; Smart quotes in secondary strings.
2444 (should
2445 (equal '("&ldquo;" "&rdquo;")
2446 (let ((org-export-default-language "en"))
2447 (org-test-with-parsed-data "* \"$x$\""
2448 (org-element-map tree 'plain-text
2449 (lambda (s) (org-export-activate-smart-quotes s :html info))
2450 info)))))
2451 ;; Smart quotes in document keywords.
2452 (should
2453 (equal '("&ldquo;" "&rdquo;")
2454 (let ((org-export-default-language "en"))
2455 (org-test-with-parsed-data "#+TITLE: \"$x$\""
2456 (org-element-map (plist-get info :title) 'plain-text
2457 (lambda (s) (org-export-activate-smart-quotes s :html info))
2458 info)))))
2459 ;; Smart quotes in parsed affiliated keywords.
2460 (should
2461 (equal '("&ldquo;" "&rdquo;" "Paragraph")
2462 (let ((org-export-default-language "en"))
2463 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
2464 (org-element-map tree 'plain-text
2465 (lambda (s) (org-export-activate-smart-quotes s :html info))
2466 info nil nil t))))))
2470 ;;; Tables
2472 (ert-deftest test-org-export/special-column ()
2473 "Test if the table's special column is properly recognized."
2474 ;; 1. First column is special if it contains only a special marking
2475 ;; characters or empty cells.
2476 (org-test-with-temp-text "
2477 | ! | 1 |
2478 | | 2 |"
2479 (should
2480 (org-export-table-has-special-column-p
2481 (org-element-map
2482 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2483 ;; 2. If the column contains anything else, it isn't special.
2484 (org-test-with-temp-text "
2485 | ! | 1 |
2486 | b | 2 |"
2487 (should-not
2488 (org-export-table-has-special-column-p
2489 (org-element-map
2490 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2491 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
2492 ;; and "!".
2493 (org-test-with-temp-text "
2494 | # | 1 |
2495 | ^ | 2 |
2496 | * | 3 |
2497 | _ | 4 |
2498 | / | 5 |
2499 | $ | 6 |
2500 | ! | 7 |"
2501 (should
2502 (org-export-table-has-special-column-p
2503 (org-element-map
2504 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2505 ;; 4. A first column with only empty cells isn't considered as
2506 ;; special.
2507 (org-test-with-temp-text "
2508 | | 1 |
2509 | | 2 |"
2510 (should-not
2511 (org-export-table-has-special-column-p
2512 (org-element-map
2513 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
2515 (ert-deftest test-org-export/table-row-is-special-p ()
2516 "Test `org-export-table-row-is-special-p' specifications."
2517 ;; 1. A row is special if it has a special marking character in the
2518 ;; special column.
2519 (org-test-with-parsed-data "| ! | 1 |"
2520 (should
2521 (org-export-table-row-is-special-p
2522 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2523 ;; 2. A row is special when its first field is "/"
2524 (org-test-with-parsed-data "
2525 | / | 1 |
2526 | a | b |"
2527 (should
2528 (org-export-table-row-is-special-p
2529 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2530 ;; 3. A row only containing alignment cookies is also considered as
2531 ;; special.
2532 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
2533 (should
2534 (org-export-table-row-is-special-p
2535 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2536 ;; 4. Everything else isn't considered as special.
2537 (org-test-with-parsed-data "| \alpha | | c |"
2538 (should-not
2539 (org-export-table-row-is-special-p
2540 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2541 ;; 5. Table's rules are never considered as special rows.
2542 (org-test-with-parsed-data "|---+---|"
2543 (should-not
2544 (org-export-table-row-is-special-p
2545 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
2547 (ert-deftest test-org-export/has-header-p ()
2548 "Test `org-export-table-has-header-p' specifications."
2549 ;; 1. With an header.
2550 (org-test-with-parsed-data "
2551 | a | b |
2552 |---+---|
2553 | c | d |"
2554 (should
2555 (org-export-table-has-header-p
2556 (org-element-map tree 'table 'identity info 'first-match)
2557 info)))
2558 ;; 2. Without an header.
2559 (org-test-with-parsed-data "
2560 | a | b |
2561 | c | d |"
2562 (should-not
2563 (org-export-table-has-header-p
2564 (org-element-map tree 'table 'identity info 'first-match)
2565 info)))
2566 ;; 3. Don't get fooled with starting and ending rules.
2567 (org-test-with-parsed-data "
2568 |---+---|
2569 | a | b |
2570 | c | d |
2571 |---+---|"
2572 (should-not
2573 (org-export-table-has-header-p
2574 (org-element-map tree 'table 'identity info 'first-match)
2575 info))))
2577 (ert-deftest test-org-export/table-row-group ()
2578 "Test `org-export-table-row-group' specifications."
2579 ;; 1. A rule creates a new group.
2580 (should
2581 (equal '(1 rule 2)
2582 (org-test-with-parsed-data "
2583 | a | b |
2584 |---+---|
2585 | 1 | 2 |"
2586 (org-element-map tree 'table-row
2587 (lambda (row)
2588 (if (eq (org-element-property :type row) 'rule) 'rule
2589 (org-export-table-row-group row info)))))))
2590 ;; 2. Special rows are ignored in count.
2591 (should
2592 (equal
2593 '(rule 1)
2594 (org-test-with-parsed-data "
2595 | / | < | > |
2596 |---|---+---|
2597 | | 1 | 2 |"
2598 (org-element-map tree 'table-row
2599 (lambda (row)
2600 (if (eq (org-element-property :type row) 'rule) 'rule
2601 (org-export-table-row-group row info)))
2602 info))))
2603 ;; 3. Double rules also are ignored in count.
2604 (should
2605 (equal '(1 rule rule 2)
2606 (org-test-with-parsed-data "
2607 | a | b |
2608 |---+---|
2609 |---+---|
2610 | 1 | 2 |"
2611 (org-element-map tree 'table-row
2612 (lambda (row)
2613 (if (eq (org-element-property :type row) 'rule) 'rule
2614 (org-export-table-row-group row info))))))))
2616 (ert-deftest test-org-export/table-row-number ()
2617 "Test `org-export-table-row-number' specifications."
2618 ;; Standard test. Number is 0-indexed.
2619 (should
2620 (equal '(0 1)
2621 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
2622 (org-element-map tree 'table-row
2623 (lambda (row) (org-export-table-row-number row info)) info))))
2624 ;; Number ignores separators.
2625 (should
2626 (equal '(0 1)
2627 (org-test-with-parsed-data "
2628 | a | b | c |
2629 |---+---+---|
2630 | d | e | f |"
2631 (org-element-map tree 'table-row
2632 (lambda (row) (org-export-table-row-number row info)) info))))
2633 ;; Number ignores special rows.
2634 (should
2635 (equal '(0 1)
2636 (org-test-with-parsed-data "
2637 | / | < | > |
2638 | | b | c |
2639 |---+-----+-----|
2640 | | <c> | <c> |
2641 | | e | f |"
2642 (org-element-map tree 'table-row
2643 (lambda (row) (org-export-table-row-number row info)) info)))))
2645 (ert-deftest test-org-export/table-cell-width ()
2646 "Test `org-export-table-cell-width' specifications."
2647 ;; 1. Width is primarily determined by width cookies. If no cookie
2648 ;; is found, cell's width is nil.
2649 (org-test-with-parsed-data "
2650 | / | <l> | <6> | <l7> |
2651 | | a | b | c |"
2652 (should
2653 (equal
2654 '(nil 6 7)
2655 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2656 (org-element-map tree 'table-cell 'identity info)))))
2657 ;; 2. The last width cookie has precedence.
2658 (org-test-with-parsed-data "
2659 | <6> |
2660 | <7> |
2661 | a |"
2662 (should
2663 (equal
2664 '(7)
2665 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2666 (org-element-map tree 'table-cell 'identity info)))))
2667 ;; 3. Valid width cookies must have a specific row.
2668 (org-test-with-parsed-data "| <6> | cell |"
2669 (should
2670 (equal
2671 '(nil nil)
2672 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2673 (org-element-map tree 'table-cell 'identity))))))
2675 (ert-deftest test-org-export/table-cell-alignment ()
2676 "Test `org-export-table-cell-alignment' specifications."
2677 ;; 1. Alignment is primarily determined by alignment cookies.
2678 (should
2679 (equal '(left center right)
2680 (let ((org-table-number-fraction 0.5)
2681 (org-table-number-regexp "^[0-9]+$"))
2682 (org-test-with-parsed-data "| <l> | <c> | <r> |"
2683 (mapcar (lambda (cell)
2684 (org-export-table-cell-alignment cell info))
2685 (org-element-map tree 'table-cell 'identity))))))
2686 ;; 2. The last alignment cookie has precedence.
2687 (should
2688 (equal '(right right right)
2689 (org-test-with-parsed-data "
2690 | <l8> |
2691 | cell |
2692 | <r9> |"
2693 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
2694 (org-element-map tree 'table-cell 'identity)))))
2695 ;; 3. If there's no cookie, cell's contents determine alignment.
2696 ;; A column mostly made of cells containing numbers will align
2697 ;; its cells to the right.
2698 (should
2699 (equal '(right right right)
2700 (let ((org-table-number-fraction 0.5)
2701 (org-table-number-regexp "^[0-9]+$"))
2702 (org-test-with-parsed-data "
2703 | 123 |
2704 | some text |
2705 | 12345 |"
2706 (mapcar (lambda (cell)
2707 (org-export-table-cell-alignment cell info))
2708 (org-element-map tree 'table-cell 'identity))))))
2709 ;; 4. Otherwise, they will be aligned to the left.
2710 (should
2711 (equal '(left left left)
2712 (org-test-with-parsed-data "
2713 | text |
2714 | some text |
2715 | \alpha |"
2716 (mapcar (lambda (cell)
2717 (org-export-table-cell-alignment cell info))
2718 (org-element-map tree 'table-cell 'identity info))))))
2720 (ert-deftest test-org-export/table-cell-borders ()
2721 "Test `org-export-table-cell-borders' specifications."
2722 ;; 1. Recognize various column groups indicators.
2723 (org-test-with-parsed-data "| / | < | > | <> |"
2724 (should
2725 (equal
2726 '((right bottom top) (left bottom top) (right bottom top)
2727 (right left bottom top))
2728 (mapcar (lambda (cell)
2729 (org-export-table-cell-borders cell info))
2730 (org-element-map tree 'table-cell 'identity)))))
2731 ;; 2. Accept shortcuts to define column groups.
2732 (org-test-with-parsed-data "| / | < | < |"
2733 (should
2734 (equal
2735 '((right bottom top) (right left bottom top) (left bottom top))
2736 (mapcar (lambda (cell)
2737 (org-export-table-cell-borders cell info))
2738 (org-element-map tree 'table-cell 'identity)))))
2739 ;; 3. A valid column groups row must start with a "/".
2740 (org-test-with-parsed-data "
2741 | | < |
2742 | a | b |"
2743 (should
2744 (equal '((top) (top) (bottom) (bottom))
2745 (mapcar (lambda (cell)
2746 (org-export-table-cell-borders cell info))
2747 (org-element-map tree 'table-cell 'identity)))))
2748 ;; 4. Take table rules into consideration.
2749 (org-test-with-parsed-data "
2750 | 1 |
2751 |---|
2752 | 2 |"
2753 (should
2754 (equal '((below top) (bottom above))
2755 (mapcar (lambda (cell)
2756 (org-export-table-cell-borders cell info))
2757 (org-element-map tree 'table-cell 'identity)))))
2758 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
2759 ;; (resp. `bottom' and `below') borders. Any special row is
2760 ;; ignored.
2761 (org-test-with-parsed-data "
2762 |---+----|
2763 | / | |
2764 | | 1 |
2765 |---+----|"
2766 (should
2767 (equal '((bottom below top above))
2768 (last
2769 (mapcar (lambda (cell)
2770 (org-export-table-cell-borders cell info))
2771 (org-element-map tree 'table-cell 'identity)))))))
2773 (ert-deftest test-org-export/table-dimensions ()
2774 "Test `org-export-table-dimensions' specifications."
2775 ;; 1. Standard test.
2776 (org-test-with-parsed-data "
2777 | 1 | 2 | 3 |
2778 | 4 | 5 | 6 |"
2779 (should
2780 (equal '(2 . 3)
2781 (org-export-table-dimensions
2782 (org-element-map tree 'table 'identity info 'first-match) info))))
2783 ;; 2. Ignore horizontal rules and special columns.
2784 (org-test-with-parsed-data "
2785 | / | < | > |
2786 | 1 | 2 | 3 |
2787 |---+---+---|
2788 | 4 | 5 | 6 |"
2789 (should
2790 (equal '(2 . 3)
2791 (org-export-table-dimensions
2792 (org-element-map tree 'table 'identity info 'first-match) info)))))
2794 (ert-deftest test-org-export/table-cell-address ()
2795 "Test `org-export-table-cell-address' specifications."
2796 ;; 1. Standard test: index is 0-based.
2797 (org-test-with-parsed-data "| a | b |"
2798 (should
2799 (equal '((0 . 0) (0 . 1))
2800 (org-element-map tree 'table-cell
2801 (lambda (cell) (org-export-table-cell-address cell info))
2802 info))))
2803 ;; 2. Special column isn't counted, nor are special rows.
2804 (org-test-with-parsed-data "
2805 | / | <> |
2806 | | c |"
2807 (should
2808 (equal '(0 . 0)
2809 (org-export-table-cell-address
2810 (car (last (org-element-map tree 'table-cell 'identity info)))
2811 info))))
2812 ;; 3. Tables rules do not count either.
2813 (org-test-with-parsed-data "
2814 | a |
2815 |---|
2816 | b |
2817 |---|
2818 | c |"
2819 (should
2820 (equal '(2 . 0)
2821 (org-export-table-cell-address
2822 (car (last (org-element-map tree 'table-cell 'identity info)))
2823 info))))
2824 ;; 4. Return nil for special cells.
2825 (org-test-with-parsed-data "| / | a |"
2826 (should-not
2827 (org-export-table-cell-address
2828 (org-element-map tree 'table-cell 'identity nil 'first-match)
2829 info))))
2831 (ert-deftest test-org-export/get-table-cell-at ()
2832 "Test `org-export-get-table-cell-at' specifications."
2833 ;; 1. Address ignores special columns, special rows and rules.
2834 (org-test-with-parsed-data "
2835 | / | <> |
2836 | | a |
2837 |---+----|
2838 | | b |"
2839 (should
2840 (equal '("b")
2841 (org-element-contents
2842 (org-export-get-table-cell-at
2843 '(1 . 0)
2844 (org-element-map tree 'table 'identity info 'first-match)
2845 info)))))
2846 ;; 2. Return value for a non-existent address is nil.
2847 (org-test-with-parsed-data "| a |"
2848 (should-not
2849 (org-export-get-table-cell-at
2850 '(2 . 2)
2851 (org-element-map tree 'table 'identity info 'first-match)
2852 info)))
2853 (org-test-with-parsed-data "| / |"
2854 (should-not
2855 (org-export-get-table-cell-at
2856 '(0 . 0)
2857 (org-element-map tree 'table 'identity info 'first-match)
2858 info))))
2860 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
2861 "Test `org-export-table-cell-starts-colgroup-p' specifications."
2862 ;; 1. A cell at a beginning of a row always starts a column group.
2863 (org-test-with-parsed-data "| a |"
2864 (should
2865 (org-export-table-cell-starts-colgroup-p
2866 (org-element-map tree 'table-cell 'identity info 'first-match)
2867 info)))
2868 ;; 2. Special column should be ignored when determining the
2869 ;; beginning of the row.
2870 (org-test-with-parsed-data "
2871 | / | |
2872 | | a |"
2873 (should
2874 (org-export-table-cell-starts-colgroup-p
2875 (org-element-map tree 'table-cell 'identity info 'first-match)
2876 info)))
2877 ;; 2. Explicit column groups.
2878 (org-test-with-parsed-data "
2879 | / | | < |
2880 | a | b | c |"
2881 (should
2882 (equal
2883 '(yes no yes)
2884 (org-element-map tree 'table-cell
2885 (lambda (cell)
2886 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
2887 info)))))
2889 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
2890 "Test `org-export-table-cell-ends-colgroup-p' specifications."
2891 ;; 1. A cell at the end of a row always ends a column group.
2892 (org-test-with-parsed-data "| a |"
2893 (should
2894 (org-export-table-cell-ends-colgroup-p
2895 (org-element-map tree 'table-cell 'identity info 'first-match)
2896 info)))
2897 ;; 2. Special column should be ignored when determining the
2898 ;; beginning of the row.
2899 (org-test-with-parsed-data "
2900 | / | |
2901 | | a |"
2902 (should
2903 (org-export-table-cell-ends-colgroup-p
2904 (org-element-map tree 'table-cell 'identity info 'first-match)
2905 info)))
2906 ;; 3. Explicit column groups.
2907 (org-test-with-parsed-data "
2908 | / | < | |
2909 | a | b | c |"
2910 (should
2911 (equal
2912 '(yes no yes)
2913 (org-element-map tree 'table-cell
2914 (lambda (cell)
2915 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
2916 info)))))
2918 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
2919 "Test `org-export-table-row-starts-rowgroup-p' specifications."
2920 ;; 1. A row at the beginning of a table always starts a row group.
2921 ;; So does a row following a table rule.
2922 (org-test-with-parsed-data "
2923 | a |
2924 |---|
2925 | b |"
2926 (should
2927 (equal
2928 '(yes no yes)
2929 (org-element-map tree 'table-row
2930 (lambda (row)
2931 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2932 info))))
2933 ;; 2. Special rows should be ignored when determining the beginning
2934 ;; of the row.
2935 (org-test-with-parsed-data "
2936 | / | < |
2937 | | a |
2938 |---+---|
2939 | / | < |
2940 | | b |"
2941 (should
2942 (equal
2943 '(yes no yes)
2944 (org-element-map tree 'table-row
2945 (lambda (row)
2946 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2947 info)))))
2949 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
2950 "Test `org-export-table-row-ends-rowgroup-p' specifications."
2951 ;; 1. A row at the end of a table always ends a row group. So does
2952 ;; a row preceding a table rule.
2953 (org-test-with-parsed-data "
2954 | a |
2955 |---|
2956 | b |"
2957 (should
2958 (equal
2959 '(yes no yes)
2960 (org-element-map tree 'table-row
2961 (lambda (row)
2962 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2963 info))))
2964 ;; 2. Special rows should be ignored when determining the beginning
2965 ;; of the row.
2966 (org-test-with-parsed-data "
2967 | | a |
2968 | / | < |
2969 |---+---|
2970 | | b |
2971 | / | < |"
2972 (should
2973 (equal
2974 '(yes no yes)
2975 (org-element-map tree 'table-row
2976 (lambda (row)
2977 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2978 info)))))
2980 (ert-deftest test-org-export/table-row-in-header-p ()
2981 "Test `org-export-table-row-in-header-p' specifications."
2982 ;; Standard test. Separators are always nil.
2983 (should
2984 (equal
2985 '(yes no no)
2986 (org-test-with-parsed-data "| a |\n|---|\n| b |"
2987 (org-element-map tree 'table-row
2988 (lambda (row)
2989 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
2990 ;; Nil when there is no header.
2991 (should
2992 (equal
2993 '(no no)
2994 (org-test-with-parsed-data "| a |\n| b |"
2995 (org-element-map tree 'table-row
2996 (lambda (row)
2997 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
2999 (ert-deftest test-org-export/table-row-starts-header-p ()
3000 "Test `org-export-table-row-starts-header-p' specifications."
3001 ;; 1. Only the row starting the first row group starts the table
3002 ;; header.
3003 (org-test-with-parsed-data "
3004 | a |
3005 | b |
3006 |---|
3007 | c |"
3008 (should
3009 (equal
3010 '(yes no no no)
3011 (org-element-map tree 'table-row
3012 (lambda (row)
3013 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
3014 info))))
3015 ;; 2. A row cannot start an header if there's no header in the
3016 ;; table.
3017 (org-test-with-parsed-data "
3018 | a |
3019 |---|"
3020 (should-not
3021 (org-export-table-row-starts-header-p
3022 (org-element-map tree 'table-row 'identity info 'first-match)
3023 info))))
3025 (ert-deftest test-org-export/table-row-ends-header-p ()
3026 "Test `org-export-table-row-ends-header-p' specifications."
3027 ;; 1. Only the row starting the first row group starts the table
3028 ;; header.
3029 (org-test-with-parsed-data "
3030 | a |
3031 | b |
3032 |---|
3033 | c |"
3034 (should
3035 (equal
3036 '(no yes no no)
3037 (org-element-map tree 'table-row
3038 (lambda (row)
3039 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
3040 info))))
3041 ;; 2. A row cannot start an header if there's no header in the
3042 ;; table.
3043 (org-test-with-parsed-data "
3044 | a |
3045 |---|"
3046 (should-not
3047 (org-export-table-row-ends-header-p
3048 (org-element-map tree 'table-row 'identity info 'first-match)
3049 info))))
3053 ;;; Tables of Contents
3055 (ert-deftest test-org-export/collect-headlines ()
3056 "Test `org-export-collect-headlines' specifications."
3057 ;; Standard test.
3058 (should
3059 (= 2
3060 (length
3061 (org-test-with-parsed-data "* H1\n** H2"
3062 (org-export-collect-headlines info)))))
3063 ;; Do not collect headlines below optional argument.
3064 (should
3065 (= 1
3066 (length
3067 (org-test-with-parsed-data "* H1\n** H2"
3068 (org-export-collect-headlines info 1)))))
3069 ;; Never collect headlines below maximum headline level.
3070 (should
3071 (= 1
3072 (length
3073 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3074 (org-export-collect-headlines info)))))
3075 (should
3076 (= 1
3077 (length
3078 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3079 (org-export-collect-headlines info 2))))))
3083 ;;; Templates
3085 (ert-deftest test-org-export/inner-template ()
3086 "Test `inner-template' translator specifications."
3087 (should
3088 (equal "Success!"
3089 (org-test-with-temp-text "* Headline"
3090 (org-export-as
3091 (org-export-create-backend
3092 :transcoders
3093 '((inner-template . (lambda (contents info) "Success!"))
3094 (headline . (lambda (h c i) "Headline"))))))))
3095 ;; Inner template is applied even in a "body-only" export.
3096 (should
3097 (equal "Success!"
3098 (org-test-with-temp-text "* Headline"
3099 (org-export-as
3100 (org-export-create-backend
3101 :transcoders '((inner-template . (lambda (c i) "Success!"))
3102 (headline . (lambda (h c i) "Headline"))))
3103 nil nil 'body-only)))))
3105 (ert-deftest test-org-export/template ()
3106 "Test `template' translator specifications."
3107 (should
3108 (equal "Success!"
3109 (org-test-with-temp-text "* Headline"
3110 (org-export-as
3111 (org-export-create-backend
3112 :transcoders '((template . (lambda (contents info) "Success!"))
3113 (headline . (lambda (h c i) "Headline"))))))))
3114 ;; Template is not applied in a "body-only" export.
3115 (should-not
3116 (equal "Success!"
3117 (org-test-with-temp-text "* Headline"
3118 (org-export-as
3119 (org-export-create-backend
3120 :transcoders '((template . (lambda (contents info) "Success!"))
3121 (headline . (lambda (h c i) "Headline"))))
3122 nil nil 'body-only)))))
3126 ;;; Topology
3128 (ert-deftest test-org-export/get-next-element ()
3129 "Test `org-export-get-next-element' specifications."
3130 ;; Standard test.
3131 (should
3132 (equal "b"
3133 (org-test-with-parsed-data "* Headline\n*a* b"
3134 (org-export-get-next-element
3135 (org-element-map tree 'bold 'identity info t) info))))
3136 ;; Return nil when no previous element.
3137 (should-not
3138 (org-test-with-parsed-data "* Headline\na *b*"
3139 (org-export-get-next-element
3140 (org-element-map tree 'bold 'identity info t) info)))
3141 ;; Non-exportable elements are ignored.
3142 (should-not
3143 (let ((org-export-with-timestamps nil))
3144 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
3145 (org-export-get-next-element
3146 (org-element-map tree 'entity 'identity info t) info))))
3147 ;; Find next element in secondary strings.
3148 (should
3149 (eq 'verbatim
3150 (org-test-with-parsed-data "* a =verb="
3151 (org-element-type
3152 (org-export-get-next-element
3153 (org-element-map tree 'plain-text 'identity info t) info)))))
3154 (should
3155 (eq 'verbatim
3156 (org-test-with-parsed-data "* /italic/ =verb="
3157 (org-element-type
3158 (org-export-get-next-element
3159 (org-element-map tree 'italic 'identity info t) info)))))
3160 ;; Find next element in document keywords.
3161 (should
3162 (eq 'verbatim
3163 (org-test-with-parsed-data "#+TITLE: a =verb="
3164 (org-element-type
3165 (org-export-get-next-element
3166 (org-element-map
3167 (plist-get info :title) 'plain-text 'identity info t) info)))))
3168 ;; Find next element in parsed affiliated keywords.
3169 (should
3170 (eq 'verbatim
3171 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
3172 (org-element-type
3173 (org-export-get-next-element
3174 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3175 ;; With optional argument N, return a list containing all the
3176 ;; following elements.
3177 (should
3178 (equal
3179 '(bold code underline)
3180 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3181 (mapcar 'car
3182 (org-export-get-next-element
3183 (org-element-map tree 'italic 'identity info t) info t)))))
3184 ;; When N is a positive integer, return a list containing up to
3185 ;; N following elements.
3186 (should
3187 (equal
3188 '(bold code)
3189 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3190 (mapcar 'car
3191 (org-export-get-next-element
3192 (org-element-map tree 'italic 'identity info t) info 2))))))
3194 (ert-deftest test-org-export/get-previous-element ()
3195 "Test `org-export-get-previous-element' specifications."
3196 ;; Standard test.
3197 (should
3198 (equal "a "
3199 (org-test-with-parsed-data "* Headline\na *b*"
3200 (org-export-get-previous-element
3201 (org-element-map tree 'bold 'identity info t) info))))
3202 ;; Return nil when no previous element.
3203 (should-not
3204 (org-test-with-parsed-data "* Headline\n*a* b"
3205 (org-export-get-previous-element
3206 (org-element-map tree 'bold 'identity info t) info)))
3207 ;; Non-exportable elements are ignored.
3208 (should-not
3209 (let ((org-export-with-timestamps nil))
3210 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
3211 (org-export-get-previous-element
3212 (org-element-map tree 'entity 'identity info t) info))))
3213 ;; Find previous element in secondary strings.
3214 (should
3215 (eq 'verbatim
3216 (org-test-with-parsed-data "* =verb= a"
3217 (org-element-type
3218 (org-export-get-previous-element
3219 (org-element-map tree 'plain-text 'identity info t) info)))))
3220 (should
3221 (eq 'verbatim
3222 (org-test-with-parsed-data "* =verb= /italic/"
3223 (org-element-type
3224 (org-export-get-previous-element
3225 (org-element-map tree 'italic 'identity info t) info)))))
3226 ;; Find previous element in document keywords.
3227 (should
3228 (eq 'verbatim
3229 (org-test-with-parsed-data "#+TITLE: =verb= a"
3230 (org-element-type
3231 (org-export-get-previous-element
3232 (org-element-map
3233 (plist-get info :title) 'plain-text 'identity info t) info)))))
3234 ;; Find previous element in parsed affiliated keywords.
3235 (should
3236 (eq 'verbatim
3237 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
3238 (org-element-type
3239 (org-export-get-previous-element
3240 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3241 ;; With optional argument N, return a list containing up to
3242 ;; N previous elements.
3243 (should
3244 (equal '(underline italic bold)
3245 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3246 (mapcar 'car
3247 (org-export-get-previous-element
3248 (org-element-map tree 'code 'identity info t) info t)))))
3249 ;; When N is a positive integer, return a list containing up to
3250 ;; N previous elements.
3251 (should
3252 (equal '(italic bold)
3253 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3254 (mapcar 'car
3255 (org-export-get-previous-element
3256 (org-element-map tree 'code 'identity info t) info 2))))))
3259 (provide 'test-ox)
3260 ;;; test-org-export.el end here