Revert "Merge export and special blocks within back-ends"
[org-mode/org-tableheadings.git] / testing / lisp / test-ox.el
blob9cb5bb06c692c821b076f88cc635ce7a37b136ed
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 (org-test-with-temp-text
808 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
809 (org-export-expand-include-keyword)
810 (should (equal (buffer-string)
811 "Small Org file with an include keyword.
813 #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
815 Success!
817 * Heading
818 body\n")))
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-\""
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-\" :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))))))))))))
923 (ert-deftest test-org-export/expand-macro ()
924 "Test macro expansion in an Org buffer."
925 ;; Standard macro expansion.
926 (should
927 (equal "#+MACRO: macro1 value\nvalue\n"
928 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
929 (org-export-as (org-test-default-backend)))))
930 ;; Expand specific macros.
931 (should
932 (equal "me 2012-03-29 me@here Title\n"
933 (org-test-with-temp-text
935 #+TITLE: Title
936 #+DATE: 2012-03-29
937 #+AUTHOR: me
938 #+EMAIL: me@here
939 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
940 (let ((output (org-export-as (org-test-default-backend))))
941 (substring output (string-match ".*\n\\'" output))))))
942 ;; Expand specific macros when property contained a regular macro
943 ;; already.
944 (should
945 (equal "value\n"
946 (org-test-with-temp-text "
947 #+MACRO: macro1 value
948 #+TITLE: {{{macro1}}}
949 {{{title}}}"
950 (let ((output (org-export-as (org-test-default-backend))))
951 (substring output (string-match ".*\n\\'" output))))))
952 ;; Expand macros with templates in included files.
953 (should
954 (equal "success\n"
955 (org-test-with-temp-text
956 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
957 {{{included-macro}}}" org-test-dir)
958 (let ((output (org-export-as (org-test-default-backend))))
959 (substring output (string-match ".*\n\\'" output)))))))
961 (ert-deftest test-org-export/user-ignore-list ()
962 "Test if `:ignore-list' accepts user input."
963 (let ((backend (org-test-default-backend)))
964 (setf (org-export-backend-transcoders backend)
965 (cons '(template . (lambda (body i)
966 (format "BEGIN\n%sEND" body)))
967 (org-export-backend-transcoders backend)))
968 (org-test-with-temp-text "Text"
969 (should (equal (org-export-as backend nil nil 'body-only)
970 "Text\n"))
971 (should (equal (org-export-as backend) "BEGIN\nText\nEND"))))
972 (should
973 (equal
974 "* Head1\n"
975 (let ((org-export-filter-parse-tree-functions
976 '((lambda (data backend info)
977 ;; Ignore headlines with the word "note" in their title.
978 (org-element-map data 'headline
979 (lambda (headline)
980 (when (string-match "\\<note\\>"
981 (org-element-property :raw-value
982 headline))
983 (org-export-ignore-element headline info)))
984 info)
985 data))))
986 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
987 (org-export-as (org-test-default-backend)))))))
989 (ert-deftest test-org-export/before-processing-hook ()
990 "Test `org-export-before-processing-hook'."
991 (should
992 (equal
993 "#+MACRO: mac val\nTest\n"
994 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
995 (let ((org-export-before-processing-hook
996 '((lambda (backend)
997 (while (re-search-forward "{{{" nil t)
998 (let ((object (org-element-context)))
999 (when (eq (org-element-type object) 'macro)
1000 (delete-region
1001 (org-element-property :begin object)
1002 (org-element-property :end object)))))))))
1003 (org-export-as (org-test-default-backend)))))))
1005 (ert-deftest test-org-export/before-parsing-hook ()
1006 "Test `org-export-before-parsing-hook'."
1007 (should
1008 (equal "Body 1\nBody 2\n"
1009 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
1010 (let ((org-export-before-parsing-hook
1011 '((lambda (backend)
1012 (goto-char (point-min))
1013 (while (re-search-forward org-outline-regexp-bol nil t)
1014 (delete-region
1015 (point-at-bol) (progn (forward-line) (point))))))))
1016 (org-export-as (org-test-default-backend)))))))
1020 ;;; Affiliated Keywords
1022 (ert-deftest test-org-export/read-attribute ()
1023 "Test `org-export-read-attribute' specifications."
1024 ;; Standard test.
1025 (should
1026 (equal
1027 (org-export-read-attribute
1028 :attr_html
1029 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
1030 (org-element-at-point)))
1031 '(:a "1" :b "2")))
1032 ;; Return nil on empty attribute.
1033 (should-not
1034 (org-export-read-attribute
1035 :attr_html
1036 (org-test-with-temp-text "Paragraph" (org-element-at-point))))
1037 ;; Return nil on "nil" string.
1038 (should
1039 (equal '(:a nil :b nil)
1040 (org-export-read-attribute
1041 :attr_html
1042 (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
1043 (org-element-at-point)))))
1044 ;; Return nil on empty string.
1045 (should
1046 (equal '(:a nil :b nil)
1047 (org-export-read-attribute
1048 :attr_html
1049 (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
1050 (org-element-at-point)))))
1051 ;; Return empty string when value is "".
1052 (should
1053 (equal '(:a "")
1054 (org-export-read-attribute
1055 :attr_html
1056 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
1057 (org-element-at-point)))))
1058 ;; Return \"\" when value is """".
1059 (should
1060 (equal '(:a "\"\"")
1061 (org-export-read-attribute
1062 :attr_html
1063 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
1064 (org-element-at-point)))))
1065 ;; Ignore text before first property.
1066 (should-not
1067 (member "ignore"
1068 (org-export-read-attribute
1069 :attr_html
1070 (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
1071 (org-element-at-point))))))
1073 (ert-deftest test-org-export/get-caption ()
1074 "Test `org-export-get-caption' specifications."
1075 ;; Without optional argument, return long caption
1076 (should
1077 (equal
1078 '("l")
1079 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1080 (org-export-get-caption (org-element-at-point)))))
1081 ;; With optional argument, return short caption.
1082 (should
1083 (equal
1084 '("s")
1085 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1086 (org-export-get-caption (org-element-at-point) t))))
1087 ;; Multiple lines are separated by white spaces.
1088 (should
1089 (equal
1090 '("a" " " "b")
1091 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
1092 (org-export-get-caption (org-element-at-point))))))
1096 ;;; Back-End Tools
1098 (ert-deftest test-org-export/define-backend ()
1099 "Test back-end definition and accessors."
1100 ;; Translate table.
1101 (should
1102 (equal '((headline . my-headline-test))
1103 (let (org-export--registered-backends)
1104 (org-export-define-backend 'test '((headline . my-headline-test)))
1105 (org-export-get-all-transcoders 'test))))
1106 ;; Filters.
1107 (should
1108 (equal '((:filter-headline . my-filter))
1109 (let (org-export--registered-backends)
1110 (org-export-define-backend 'test
1111 '((headline . my-headline-test))
1112 :filters-alist '((:filter-headline . my-filter)))
1113 (org-export-backend-filters (org-export-get-backend 'test)))))
1114 ;; Options.
1115 (should
1116 (equal '((:prop value))
1117 (let (org-export--registered-backends)
1118 (org-export-define-backend 'test
1119 '((headline . my-headline-test))
1120 :options-alist '((:prop value)))
1121 (org-export-backend-options (org-export-get-backend 'test)))))
1122 ;; Menu.
1123 (should
1124 (equal '(?k "Test Export" test)
1125 (let (org-export--registered-backends)
1126 (org-export-define-backend 'test
1127 '((headline . my-headline-test))
1128 :menu-entry '(?k "Test Export" test))
1129 (org-export-backend-menu (org-export-get-backend 'test)))))
1130 ;; Export Blocks.
1131 (should
1132 (equal '(("TEST" . org-element-export-block-parser))
1133 (let (org-export--registered-backends org-element-block-name-alist)
1134 (org-export-define-backend 'test
1135 '((headline . my-headline-test))
1136 :export-block '("test"))
1137 org-element-block-name-alist))))
1139 (ert-deftest test-org-export/define-derived-backend ()
1140 "Test `org-export-define-derived-backend' specifications."
1141 ;; Error when parent back-end is not defined.
1142 (should-error
1143 (let (org-export--registered-backends)
1144 (org-export-define-derived-backend 'test 'parent)))
1145 ;; Append translation table to parent's.
1146 (should
1147 (equal '((:headline . test) (:headline . parent))
1148 (let (org-export--registered-backends)
1149 (org-export-define-backend 'parent '((:headline . parent)))
1150 (org-export-define-derived-backend 'test 'parent
1151 :translate-alist '((:headline . test)))
1152 (org-export-get-all-transcoders 'test))))
1153 ;; Options defined in the new back have priority over those defined
1154 ;; in parent.
1155 (should
1156 (eq 'test
1157 (let (org-export--registered-backends)
1158 (org-export-define-backend 'parent
1159 '((:headline . parent))
1160 :options-alist '((:a nil nil 'parent)))
1161 (org-export-define-derived-backend 'test 'parent
1162 :options-alist '((:a nil nil 'test)))
1163 (plist-get (org-export--get-global-options
1164 (org-export-get-backend 'test))
1165 :a)))))
1167 (ert-deftest test-org-export/derived-backend-p ()
1168 "Test `org-export-derived-backend-p' specifications."
1169 ;; Non-nil with direct match.
1170 (should
1171 (let (org-export--registered-backends)
1172 (org-export-define-backend 'test '((headline . test)))
1173 (org-export-derived-backend-p 'test 'test)))
1174 (should
1175 (let (org-export--registered-backends)
1176 (org-export-define-backend 'test '((headline . test)))
1177 (org-export-define-derived-backend 'test2 'test)
1178 (org-export-derived-backend-p 'test2 'test2)))
1179 ;; Non-nil with a direct parent.
1180 (should
1181 (let (org-export--registered-backends)
1182 (org-export-define-backend 'test '((headline . test)))
1183 (org-export-define-derived-backend 'test2 'test)
1184 (org-export-derived-backend-p 'test2 'test)))
1185 ;; Non-nil with an indirect parent.
1186 (should
1187 (let (org-export--registered-backends)
1188 (org-export-define-backend 'test '((headline . test)))
1189 (org-export-define-derived-backend 'test2 'test)
1190 (org-export-define-derived-backend 'test3 'test2)
1191 (org-export-derived-backend-p 'test3 'test)))
1192 ;; Nil otherwise.
1193 (should-not
1194 (let (org-export--registered-backends)
1195 (org-export-define-backend 'test '((headline . test)))
1196 (org-export-define-backend 'test2 '((headline . test2)))
1197 (org-export-derived-backend-p 'test2 'test)))
1198 (should-not
1199 (let (org-export--registered-backends)
1200 (org-export-define-backend 'test '((headline . test)))
1201 (org-export-define-backend 'test2 '((headline . test2)))
1202 (org-export-define-derived-backend 'test3 'test2)
1203 (org-export-derived-backend-p 'test3 'test))))
1205 (ert-deftest test-org-export/get-all-transcoders ()
1206 "Test `org-export-get-all-transcoders' specifications."
1207 ;; Return nil when back-end cannot be found.
1208 (should-not (org-export-get-all-transcoders nil))
1209 ;; Same as `org-export-transcoders' if no parent.
1210 (should
1211 (equal '((headline . ignore))
1212 (org-export-get-all-transcoders
1213 (org-export-create-backend
1214 :transcoders '((headline . ignore))))))
1215 ;; But inherit from all ancestors whenever possible.
1216 (should
1217 (equal '((section . ignore) (headline . ignore))
1218 (let (org-export--registered-backends)
1219 (org-export-define-backend 'b1 '((headline . ignore)))
1220 (org-export-get-all-transcoders
1221 (org-export-create-backend
1222 :parent 'b1 :transcoders '((section . ignore)))))))
1223 (should
1224 (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
1225 (let (org-export--registered-backends)
1226 (org-export-define-backend 'b1 '((headline . ignore)))
1227 (org-export-define-derived-backend 'b2 'b1
1228 :translate-alist '((section . ignore)))
1229 (org-export-get-all-transcoders
1230 (org-export-create-backend
1231 :parent 'b2 :transcoders '((paragraph . ignore)))))))
1232 ;; Back-end transcoders overrule inherited ones.
1233 (should
1234 (eq 'b
1235 (let (org-export--registered-backends)
1236 (org-export-define-backend 'b1 '((headline . a)))
1237 (cdr (assq 'headline
1238 (org-export-get-all-transcoders
1239 (org-export-create-backend
1240 :parent 'b1 :transcoders '((headline . b))))))))))
1242 (ert-deftest test-org-export/get-all-options ()
1243 "Test `org-export-get-all-options' specifications."
1244 ;; Return nil when back-end cannot be found.
1245 (should-not (org-export-get-all-options nil))
1246 ;; Same as `org-export-options' if no parent.
1247 (should
1248 (equal '((headline . ignore))
1249 (org-export-get-all-options
1250 (org-export-create-backend
1251 :options '((headline . ignore))))))
1252 ;; But inherit from all ancestors whenever possible.
1253 (should
1254 (equal '((:key2 value2) (:key1 value1))
1255 (let (org-export--registered-backends)
1256 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1257 (org-export-get-all-options
1258 (org-export-create-backend
1259 :parent 'b1 :options '((:key2 value2)))))))
1260 (should
1261 (equal '((:key3 value3) (:key2 value2) (:key1 value1))
1262 (let (org-export--registered-backends)
1263 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1264 (org-export-define-derived-backend 'b2 'b1
1265 :options-alist '((:key2 value2)))
1266 (org-export-get-all-options
1267 (org-export-create-backend
1268 :parent 'b2 :options '((:key3 value3)))))))
1269 ;; Back-end options overrule inherited ones.
1270 (should
1271 (eq 'b
1272 (let (org-export--registered-backends)
1273 (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
1274 (cdr (assq :key1
1275 (org-export-get-all-options
1276 (org-export-create-backend
1277 :parent 'b1 :options '((:key1 . b))))))))))
1279 (ert-deftest test-org-export/get-all-filters ()
1280 "Test `org-export-get-all-filters' specifications."
1281 ;; Return nil when back-end cannot be found.
1282 (should-not (org-export-get-all-filters nil))
1283 ;; Same as `org-export-filters' if no parent.
1284 (should
1285 (equal '((:filter-headline . ignore))
1286 (org-export-get-all-filters
1287 (org-export-create-backend
1288 :filters '((:filter-headline . ignore))))))
1289 ;; But inherit from all ancestors whenever possible.
1290 (should
1291 (equal '((:filter-section . ignore) (:filter-headline . ignore))
1292 (let (org-export--registered-backends)
1293 (org-export-define-backend 'b1
1294 nil :filters-alist '((:filter-headline . ignore)))
1295 (org-export-get-all-filters
1296 (org-export-create-backend
1297 :parent 'b1 :filters '((:filter-section . ignore)))))))
1298 (should
1299 (equal '((:filter-paragraph . ignore)
1300 (:filter-section . ignore)
1301 (:filter-headline . ignore))
1302 (let (org-export--registered-backends)
1303 (org-export-define-backend 'b1
1304 nil :filters-alist '((:filter-headline . ignore)))
1305 (org-export-define-derived-backend 'b2 'b1
1306 :filters-alist '((:filter-section . ignore)))
1307 (org-export-get-all-filters
1308 (org-export-create-backend
1309 :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
1310 ;; Back-end filters overrule inherited ones.
1311 (should
1312 (eq 'b
1313 (let (org-export--registered-backends)
1314 (org-export-define-backend 'b1 '((:filter-headline . a)))
1315 (cdr (assq :filter-headline
1316 (org-export-get-all-filters
1317 (org-export-create-backend
1318 :parent 'b1 :filters '((:filter-headline . b))))))))))
1320 (ert-deftest test-org-export/with-backend ()
1321 "Test `org-export-with-backend' definition."
1322 ;; Error when calling an undefined back-end
1323 (should-error (org-export-with-backend nil "Test"))
1324 ;; Error when called back-end doesn't have an appropriate
1325 ;; transcoder.
1326 (should-error
1327 (org-export-with-backend
1328 (org-export-create-backend :transcoders '((headline . ignore)))
1329 "Test"))
1330 ;; Otherwise, export using correct transcoder
1331 (should
1332 (equal "Success"
1333 (let (org-export--registered-backends)
1334 (org-export-define-backend 'test
1335 '((plain-text . (lambda (text contents info) "Failure"))))
1336 (org-export-define-backend 'test2
1337 '((plain-text . (lambda (text contents info) "Success"))))
1338 (org-export-with-backend 'test2 "Test"))))
1339 ;; Provide correct back-end if transcoder needs to use recursive
1340 ;; calls anyway.
1341 (should
1342 (equal "Success\n"
1343 (let ((test-back-end
1344 (org-export-create-backend
1345 :transcoders
1346 '((headline . (lambda (headline contents info)
1347 (org-export-data
1348 (org-element-property :title headline)
1349 info)))
1350 (plain-text . (lambda (text info) "Success"))))))
1351 (org-export-string-as
1352 "* Test"
1353 (org-export-create-backend
1354 :transcoders
1355 '((headline . (lambda (headline contents info)
1356 (org-export-with-backend
1357 test-back-end headline contents info))))))))))
1359 (ert-deftest test-org-export/data-with-backend ()
1360 "Test `org-export-data-with-backend' specifications."
1361 ;; Error when calling an undefined back-end.
1362 (should-error (org-export-data-with-backend nil "nil" nil))
1363 ;; Otherwise, export data recursively, using correct back-end.
1364 (should
1365 (equal
1366 "Success!"
1367 (org-export-data-with-backend
1368 '(bold nil "Test")
1369 (org-export-create-backend
1370 :transcoders
1371 '((plain-text . (lambda (text info) "Success"))
1372 (bold . (lambda (bold contents info) (concat contents "!")))))
1373 '(:with-emphasize t)))))
1377 ;;; Export Snippets
1379 (ert-deftest test-org-export/export-snippet ()
1380 "Test export snippets transcoding."
1381 ;; Standard test.
1382 (org-test-with-temp-text "@@test:A@@@@t:B@@"
1383 (let ((backend (org-test-default-backend)))
1384 (setf (org-export-backend-name backend) 'test)
1385 (setf (org-export-backend-transcoders backend)
1386 (cons (cons 'export-snippet
1387 (lambda (snippet contents info)
1388 (when (eq (org-export-snippet-backend snippet) 'test)
1389 (org-element-property :value snippet))))
1390 (org-export-backend-transcoders backend)))
1391 (let ((org-export-snippet-translation-alist nil))
1392 (should (equal (org-export-as backend) "A\n")))
1393 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
1394 (should (equal (org-export-as backend) "AB\n")))))
1395 ;; Ignored export snippets do not remove any blank.
1396 (should
1397 (equal "begin end\n"
1398 (org-test-with-parsed-data "begin @@test:A@@ end"
1399 (org-export-data-with-backend
1400 tree
1401 (org-export-create-backend
1402 :transcoders
1403 '((paragraph . (lambda (paragraph contents info) contents))
1404 (section . (lambda (section contents info) contents))))
1405 info)))))
1409 ;;; Footnotes
1411 (ert-deftest test-org-export/footnotes ()
1412 "Test footnotes specifications."
1413 (let ((org-footnote-section nil)
1414 (org-export-with-footnotes t))
1415 ;; 1. Read every type of footnote.
1416 (should
1417 (equal
1418 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
1419 (org-test-with-parsed-data
1420 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
1421 (org-element-map tree 'footnote-reference
1422 (lambda (ref)
1423 (let ((def (org-export-get-footnote-definition ref info)))
1424 (cons (org-export-get-footnote-number ref info)
1425 (if (eq (org-element-property :type ref) 'inline) (car def)
1426 (car (org-element-contents
1427 (car (org-element-contents def))))))))
1428 info))))
1429 ;; 2. Test nested footnotes order.
1430 (should
1431 (equal
1432 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
1433 (org-test-with-parsed-data
1434 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
1435 (org-element-map tree 'footnote-reference
1436 (lambda (ref)
1437 (when (org-export-footnote-first-reference-p ref info)
1438 (cons (org-export-get-footnote-number ref info)
1439 (org-element-property :label ref))))
1440 info))))
1441 ;; 3. Test nested footnote in invisible definitions.
1442 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
1443 ;; Hide definitions.
1444 (narrow-to-region (point) (point-at-eol))
1445 (let* ((tree (org-element-parse-buffer))
1446 (info (org-combine-plists
1447 `(:parse-tree ,tree)
1448 (org-export-collect-tree-properties
1449 tree (org-export-get-environment)))))
1450 ;; Both footnotes should be seen.
1451 (should
1452 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
1453 ;; 4. Test footnotes definitions collection.
1454 (should
1455 (= 4
1456 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1458 \[fn:2] B [fn:3] [fn::D].
1460 \[fn:3] C."
1461 (length (org-export-collect-footnote-definitions tree info)))))
1462 ;; 5. Test export of footnotes defined outside parsing scope.
1463 (should
1464 (equal
1465 "ParagraphOut of scope\n"
1466 (org-test-with-temp-text "[fn:1] Out of scope
1467 * Title
1468 Paragraph[fn:1]"
1469 (let ((backend (org-test-default-backend)))
1470 (setf (org-export-backend-transcoders backend)
1471 (cons (cons 'footnote-reference
1472 (lambda (fn contents info)
1473 (org-element-interpret-data
1474 (org-export-get-footnote-definition fn info))))
1475 (org-export-backend-transcoders backend)))
1476 (forward-line)
1477 (org-export-as backend 'subtree)))))
1478 ;; 6. Footnotes without a definition should be provided a fallback
1479 ;; definition.
1480 (should
1481 (org-test-with-parsed-data "[fn:1]"
1482 (org-export-get-footnote-definition
1483 (org-element-map tree 'footnote-reference 'identity info t) info)))
1484 ;; 7. Footnote section should be ignored in TOC and in headlines
1485 ;; numbering.
1486 (should
1487 (= 1 (let ((org-footnote-section "Footnotes"))
1488 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
1489 (org-export-collect-headlines info))))))
1490 (should
1491 (equal '(2)
1492 (let ((org-footnote-section "Footnotes"))
1493 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
1494 (org-element-map tree 'headline
1495 (lambda (hl)
1496 (when (equal (org-element-property :raw-value hl) "H2")
1497 (org-export-get-headline-number hl info)))
1498 info t)))))))
1502 ;;; Headlines and Inlinetasks
1504 (ert-deftest test-org-export/get-relative-level ()
1505 "Test `org-export-get-relative-level' specifications."
1506 ;; Standard test.
1507 (should
1508 (equal '(1 2)
1509 (let ((org-odd-levels-only nil))
1510 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1511 (org-element-map tree 'headline
1512 (lambda (h) (org-export-get-relative-level h info))
1513 info)))))
1514 ;; Missing levels
1515 (should
1516 (equal '(1 3)
1517 (let ((org-odd-levels-only nil))
1518 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
1519 (org-element-map tree 'headline
1520 (lambda (h) (org-export-get-relative-level h info))
1521 info))))))
1523 (ert-deftest test-org-export/low-level-p ()
1524 "Test `org-export-low-level-p' specifications."
1525 (should
1526 (equal
1527 '(no yes)
1528 (let ((org-odd-levels-only nil))
1529 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1530 (org-element-map tree 'headline
1531 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
1532 (plist-put info :headline-levels 1)))))))
1534 (ert-deftest test-org-export/get-headline-number ()
1535 "Test `org-export-get-headline-number' specifications."
1536 ;; Standard test.
1537 (should
1538 (equal
1539 '((1) (1 1))
1540 (let ((org-odd-levels-only nil))
1541 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1542 (org-element-map tree 'headline
1543 (lambda (h) (org-export-get-headline-number h info))
1544 info)))))
1545 ;; Missing levels are replaced with 0.
1546 (should
1547 (equal
1548 '((1) (1 0 1))
1549 (let ((org-odd-levels-only nil))
1550 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
1551 (org-element-map tree 'headline
1552 (lambda (h) (org-export-get-headline-number h info))
1553 info))))))
1555 (ert-deftest test-org-export/numbered-headline-p ()
1556 "Test `org-export-numbered-headline-p' specifications."
1557 ;; If `:section-numbers' is nil, never number headlines.
1558 (should-not
1559 (org-test-with-parsed-data "* Headline"
1560 (org-element-map tree 'headline
1561 (lambda (h) (org-export-numbered-headline-p h info))
1562 (plist-put info :section-numbers nil))))
1563 ;; If `:section-numbers' is a number, only number headlines with
1564 ;; a level greater that it.
1565 (should
1566 (equal
1567 '(yes no)
1568 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1569 (org-element-map tree 'headline
1570 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
1571 (plist-put info :section-numbers 1)))))
1572 ;; Otherwise, headlines are always numbered.
1573 (should
1574 (org-test-with-parsed-data "* Headline"
1575 (org-element-map tree 'headline
1576 (lambda (h) (org-export-numbered-headline-p h info))
1577 (plist-put info :section-numbers t)))))
1579 (ert-deftest test-org-export/number-to-roman ()
1580 "Test `org-export-number-to-roman' specifications."
1581 ;; If number is negative, return it as a string.
1582 (should (equal (org-export-number-to-roman -1) "-1"))
1583 ;; Otherwise, return it as a roman number.
1584 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
1586 (ert-deftest test-org-export/get-optional-title ()
1587 "Test `org-export-get-alt-title' specifications."
1588 ;; If ALT_TITLE property is defined, use it.
1589 (should
1590 (equal '("opt")
1591 (org-test-with-parsed-data
1592 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
1593 (org-export-get-alt-title
1594 (org-element-map tree 'headline 'identity info t)
1595 info))))
1596 ;; Otherwise, fall-back to regular title.
1597 (should
1598 (equal '("Headline")
1599 (org-test-with-parsed-data "* Headline"
1600 (org-export-get-alt-title
1601 (org-element-map tree 'headline 'identity info t)
1602 info)))))
1604 (ert-deftest test-org-export/get-tags ()
1605 "Test `org-export-get-tags' specifications."
1606 (let ((org-export-exclude-tags '("noexport"))
1607 (org-export-select-tags '("export")))
1608 ;; Standard test: tags which are not a select tag, an exclude tag,
1609 ;; or specified as optional argument shouldn't be ignored.
1610 (should
1611 (org-test-with-parsed-data "* Headline :tag:"
1612 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1613 info)))
1614 ;; Exclude tags are removed.
1615 (should-not
1616 (org-test-with-parsed-data "* Headline :noexport:"
1617 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1618 info)))
1619 ;; Select tags are removed.
1620 (should-not
1621 (org-test-with-parsed-data "* Headline :export:"
1622 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1623 info)))
1624 (should
1625 (equal
1626 '("tag")
1627 (org-test-with-parsed-data "* Headline :tag:export:"
1628 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1629 info))))
1630 ;; Tags provided in the optional argument are also ignored.
1631 (should-not
1632 (org-test-with-parsed-data "* Headline :ignore:"
1633 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1634 info '("ignore"))))
1635 ;; Allow tag inheritance.
1636 (should
1637 (equal
1638 '(("tag") ("tag"))
1639 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
1640 (org-element-map tree 'headline
1641 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
1642 ;; Tag inheritance checks FILETAGS keywords.
1643 (should
1644 (equal
1645 '(("a" "b" "tag"))
1646 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
1647 (org-element-map tree 'headline
1648 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
1650 (ert-deftest test-org-export/get-node-property ()
1651 "Test`org-export-get-node-property' specifications."
1652 ;; Standard test.
1653 (should
1654 (equal "value"
1655 (org-test-with-parsed-data "* Headline
1656 :PROPERTIES:
1657 :prop: value
1658 :END:"
1659 (org-export-get-node-property
1660 :PROP (org-element-map tree 'headline 'identity nil t)))))
1661 ;; Test inheritance.
1662 (should
1663 (equal "value"
1664 (org-test-with-parsed-data "* Parent
1665 :PROPERTIES:
1666 :prop: value
1667 :END:
1668 ** Headline
1669 Paragraph"
1670 (org-export-get-node-property
1671 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
1672 ;; Cannot return a value before the first headline.
1673 (should-not
1674 (org-test-with-parsed-data "Paragraph
1675 * Headline
1676 :PROPERTIES:
1677 :prop: value
1678 :END:"
1679 (org-export-get-node-property
1680 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
1682 (ert-deftest test-org-export/get-category ()
1683 "Test `org-export-get-category' specifications."
1684 ;; Standard test.
1685 (should
1686 (equal "value"
1687 (org-test-with-parsed-data "* Headline
1688 :PROPERTIES:
1689 :CATEGORY: value
1690 :END:"
1691 (org-export-get-category
1692 (org-element-map tree 'headline 'identity nil t) info))))
1693 ;; Test inheritance from a parent headline.
1694 (should
1695 (equal '("value" "value")
1696 (org-test-with-parsed-data "* Headline1
1697 :PROPERTIES:
1698 :CATEGORY: value
1699 :END:
1700 ** Headline2"
1701 (org-element-map tree 'headline
1702 (lambda (hl) (org-export-get-category hl info)) info))))
1703 ;; Test inheritance from #+CATEGORY keyword
1704 (should
1705 (equal "value"
1706 (org-test-with-parsed-data "#+CATEGORY: value
1707 * Headline"
1708 (org-export-get-category
1709 (org-element-map tree 'headline 'identity nil t) info))))
1710 ;; Test inheritance from file name.
1711 (should
1712 (equal "test"
1713 (org-test-with-parsed-data "* Headline"
1714 (let ((info (plist-put info :input-file "~/test.org")))
1715 (org-export-get-category
1716 (org-element-map tree 'headline 'identity nil t) info)))))
1717 ;; Fall-back value.
1718 (should
1719 (equal "???"
1720 (org-test-with-parsed-data "* Headline"
1721 (org-export-get-category
1722 (org-element-map tree 'headline 'identity nil t) info)))))
1724 (ert-deftest test-org-export/first-sibling-p ()
1725 "Test `org-export-first-sibling-p' specifications."
1726 ;; Standard test.
1727 (should
1728 (equal
1729 '(yes yes no)
1730 (org-test-with-parsed-data "* H\n** H 2\n** H 3"
1731 (org-element-map tree 'headline
1732 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1733 info))))
1734 (should
1735 (equal '(yes no)
1736 (org-test-with-parsed-data "- item\n\n para"
1737 (org-element-map tree 'paragraph
1738 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1739 info))))
1740 ;; Ignore sections for headlines.
1741 (should
1742 (equal '(yes yes)
1743 (org-test-with-parsed-data "* H\nSection\n** H 2"
1744 (org-element-map tree 'headline
1745 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1746 info))))
1747 ;; Ignore headlines not exported.
1748 (should
1749 (equal
1750 '(yes)
1751 (let ((org-export-exclude-tags '("ignore")))
1752 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
1753 (org-element-map tree 'headline
1754 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1755 info))))))
1757 (ert-deftest test-org-export/last-sibling-p ()
1758 "Test `org-export-last-sibling-p' specifications."
1759 ;; Standard test.
1760 (should
1761 (equal
1762 '(yes no yes)
1763 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1764 (org-element-map tree 'headline
1765 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1766 info))))
1767 (should
1768 (equal '(no yes)
1769 (org-test-with-parsed-data "- item\n\n para"
1770 (org-element-map tree 'paragraph
1771 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1772 info))))
1773 ;; Ignore headlines not exported.
1774 (should
1775 (equal
1776 '(yes)
1777 (let ((org-export-exclude-tags '("ignore")))
1778 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
1779 (org-element-map tree 'headline
1780 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1781 info))))))
1783 (ert-deftest test-org-export/handle-inlinetasks ()
1784 "Test inlinetask export."
1785 ;; Inlinetask with an exclude tag.
1786 (when (featurep 'org-inlinetask)
1787 (should
1788 (equal
1790 (let ((org-inlinetask-min-level 3))
1791 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
1792 (org-export-as (org-test-default-backend)
1793 nil nil nil '(:exclude-tags ("noexp")))))))
1794 ;; Inlinetask with an include tag.
1795 (should
1796 (equal
1797 "* H2\n*** Inline :exp:\n"
1798 (let ((org-inlinetask-min-level 3)
1799 (org-tags-column 0))
1800 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
1801 (org-export-as (org-test-default-backend)
1802 nil nil nil '(:select-tags ("exp")))))))
1803 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
1804 (should
1805 (equal ""
1806 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
1807 (org-inlinetask-min-level 3))
1808 (org-test-with-temp-text "*** TODO Inline"
1809 (org-export-as (org-test-default-backend)
1810 nil nil nil '(:with-tasks nil))))))))
1814 ;;; Keywords
1816 (ert-deftest test-org-export/get-date ()
1817 "Test `org-export-get-date' specifications."
1818 ;; Return a properly formatted string when
1819 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
1820 ;; consists in a single timestamp.
1821 (should
1822 (equal "29 03 2012"
1823 (let ((org-export-date-timestamp-format "%d %m %Y"))
1824 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1825 (org-export-get-date info)))))
1826 ;; Return a secondary string otherwise.
1827 (should-not
1828 (stringp
1829 (let ((org-export-date-timestamp-format nil))
1830 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1831 (org-export-get-date info)))))
1832 (should
1833 (equal '("Date")
1834 (org-test-with-parsed-data "#+DATE: Date"
1835 (org-export-get-date info))))
1836 ;; Optional argument has precedence over
1837 ;; `org-export-date-timestamp-format'.
1838 (should
1839 (equal "29 03"
1840 (let ((org-export-date-timestamp-format "%d %m %Y"))
1841 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1842 (org-export-get-date info "%d %m"))))))
1846 ;;; Links
1848 (ert-deftest test-org-export/get-coderef-format ()
1849 "Test `org-export-get-coderef-format' specifications."
1850 ;; A link without description returns "%s"
1851 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
1852 "%s"))
1853 ;; Return "%s" when path is matched within description.
1854 (should (equal (org-export-get-coderef-format "path" "desc (path)")
1855 "desc %s"))
1856 ;; Otherwise return description.
1857 (should (equal (org-export-get-coderef-format "path" "desc")
1858 "desc")))
1860 (ert-deftest test-org-export/inline-image-p ()
1861 "Test `org-export-inline-image-p' specifications."
1862 (should
1863 (org-export-inline-image-p
1864 (org-test-with-temp-text "[[#id]]"
1865 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
1866 '(("custom-id" . "id")))))
1868 (ert-deftest test-org-export/fuzzy-link ()
1869 "Test fuzzy links specifications."
1870 ;; Link to an headline should return headline's number.
1871 (org-test-with-parsed-data
1872 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
1873 (should
1874 ;; Note: Headline's number is in fact a list of numbers.
1875 (equal '(2)
1876 (org-element-map tree 'link
1877 (lambda (link)
1878 (org-export-get-ordinal
1879 (org-export-resolve-fuzzy-link link info) info)) info t))))
1880 ;; Link to a target in an item should return item's number.
1881 (org-test-with-parsed-data
1882 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
1883 (should
1884 ;; Note: Item's number is in fact a list of numbers.
1885 (equal '(1 2)
1886 (org-element-map tree 'link
1887 (lambda (link)
1888 (org-export-get-ordinal
1889 (org-export-resolve-fuzzy-link link info) info)) info t))))
1890 ;; Link to a target in a footnote should return footnote's number.
1891 (org-test-with-parsed-data "
1892 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
1893 (should
1894 (equal '(2 3)
1895 (org-element-map tree 'link
1896 (lambda (link)
1897 (org-export-get-ordinal
1898 (org-export-resolve-fuzzy-link link info) info)) info))))
1899 ;; Link to a named element should return sequence number of that
1900 ;; element.
1901 (org-test-with-parsed-data
1902 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
1903 (should
1904 (= 2
1905 (org-element-map tree 'link
1906 (lambda (link)
1907 (org-export-get-ordinal
1908 (org-export-resolve-fuzzy-link link info) info)) info t))))
1909 ;; Link to a target not within an item, a table, a footnote
1910 ;; reference or definition should return section number.
1911 (org-test-with-parsed-data
1912 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
1913 (should
1914 (equal '(2)
1915 (org-element-map tree 'link
1916 (lambda (link)
1917 (org-export-get-ordinal
1918 (org-export-resolve-fuzzy-link link info) info)) info t))))
1919 ;; Space are not significant when matching a fuzzy link.
1920 (should
1921 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
1922 (org-element-map tree 'link
1923 (lambda (link) (org-export-resolve-fuzzy-link link info))
1924 info t)))
1925 ;; Statistics cookies are ignored for headline match.
1926 (should
1927 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
1928 (org-element-map tree 'link
1929 (lambda (link) (org-export-resolve-fuzzy-link link info))
1930 info t)))
1931 (should
1932 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
1933 (org-element-map tree 'link
1934 (lambda (link) (org-export-resolve-fuzzy-link link info))
1935 info t)))
1936 ;; Headline match is position dependent.
1937 (should-not
1938 (apply
1940 (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
1941 (org-element-map tree 'link
1942 (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
1944 (ert-deftest test-org-export/resolve-coderef ()
1945 "Test `org-export-resolve-coderef' specifications."
1946 (let ((org-coderef-label-format "(ref:%s)"))
1947 ;; 1. A link to a "-n -k -r" block returns line number.
1948 (org-test-with-parsed-data
1949 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
1950 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1951 (org-test-with-parsed-data
1952 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1953 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1954 ;; 2. A link to a "-n -r" block returns line number.
1955 (org-test-with-parsed-data
1956 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
1957 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1958 (org-test-with-parsed-data
1959 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1960 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1961 ;; 3. A link to a "-n" block returns coderef.
1962 (org-test-with-parsed-data
1963 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1964 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1965 (org-test-with-parsed-data
1966 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
1967 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1968 ;; 4. A link to a "-r" block returns line number.
1969 (org-test-with-parsed-data
1970 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1971 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1972 (org-test-with-parsed-data
1973 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
1974 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1975 ;; 5. A link to a block without a switch returns coderef.
1976 (org-test-with-parsed-data
1977 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1978 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1979 (org-test-with-parsed-data
1980 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
1981 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1982 ;; 6. Correctly handle continued line numbers. A "+n" switch
1983 ;; should resume numbering from previous block with numbered
1984 ;; lines, ignoring blocks not numbering lines in the process.
1985 ;; A "-n" switch resets count.
1986 (org-test-with-parsed-data "
1987 #+BEGIN_EXAMPLE -n
1988 Text.
1989 #+END_EXAMPLE
1991 #+BEGIN_SRC emacs-lisp
1992 \(- 1 1)
1993 #+END_SRC
1995 #+BEGIN_SRC emacs-lisp +n -r
1996 \(+ 1 1) (ref:addition)
1997 #+END_SRC
1999 #+BEGIN_EXAMPLE -n -r
2000 Another text. (ref:text)
2001 #+END_EXAMPLE"
2002 (should (= (org-export-resolve-coderef "addition" info) 2))
2003 (should (= (org-export-resolve-coderef "text" info) 1)))
2004 ;; 7. Recognize coderef with user-specified syntax.
2005 (org-test-with-parsed-data
2006 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
2007 (should (equal (org-export-resolve-coderef "text" info) "text")))))
2009 (ert-deftest test-org-export/resolve-fuzzy-link ()
2010 "Test `org-export-resolve-fuzzy-link' specifications."
2011 ;; Match target objects.
2012 (should
2013 (org-test-with-parsed-data "<<target>> [[target]]"
2014 (org-export-resolve-fuzzy-link
2015 (org-element-map tree 'link 'identity info t) info)))
2016 ;; Match named elements.
2017 (should
2018 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
2019 (org-export-resolve-fuzzy-link
2020 (org-element-map tree 'link 'identity info t) info)))
2021 ;; Match exact headline's name.
2022 (should
2023 (org-test-with-parsed-data "* My headline\n[[My headline]]"
2024 (org-export-resolve-fuzzy-link
2025 (org-element-map tree 'link 'identity info t) info)))
2026 ;; Targets objects have priority over named elements and headline
2027 ;; titles.
2028 (should
2029 (eq 'target
2030 (org-test-with-parsed-data
2031 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
2032 (org-element-type
2033 (org-export-resolve-fuzzy-link
2034 (org-element-map tree 'link 'identity info t) info)))))
2035 ;; Named elements have priority over headline titles.
2036 (should
2037 (eq 'paragraph
2038 (org-test-with-parsed-data
2039 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
2040 (org-element-type
2041 (org-export-resolve-fuzzy-link
2042 (org-element-map tree 'link 'identity info t) info)))))
2043 ;; If link's path starts with a "*", only match headline titles,
2044 ;; though.
2045 (should
2046 (eq 'headline
2047 (org-test-with-parsed-data
2048 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
2049 (org-element-type
2050 (org-export-resolve-fuzzy-link
2051 (org-element-map tree 'link 'identity info t) info)))))
2052 ;; Return nil if no match.
2053 (should-not
2054 (org-test-with-parsed-data "[[target]]"
2055 (org-export-resolve-fuzzy-link
2056 (org-element-map tree 'link 'identity info t) info)))
2057 ;; Match fuzzy link even when before first headline.
2058 (should
2059 (eq 'headline
2060 (org-test-with-parsed-data "[[hl]]\n* hl"
2061 (org-element-type
2062 (org-export-resolve-fuzzy-link
2063 (org-element-map tree 'link 'identity info t) info))))))
2065 (ert-deftest test-org-export/resolve-id-link ()
2066 "Test `org-export-resolve-id-link' specifications."
2067 ;; 1. Regular test for custom-id link.
2068 (org-test-with-parsed-data "* Headline1
2069 :PROPERTIES:
2070 :CUSTOM_ID: test
2071 :END:
2072 * Headline 2
2073 \[[#test]]"
2074 (should
2075 (org-export-resolve-id-link
2076 (org-element-map tree 'link 'identity info t) info)))
2077 ;; 2. Failing test for custom-id link.
2078 (org-test-with-parsed-data "* Headline1
2079 :PROPERTIES:
2080 :CUSTOM_ID: test
2081 :END:
2082 * Headline 2
2083 \[[#no-match]]"
2084 (should-not
2085 (org-export-resolve-id-link
2086 (org-element-map tree 'link 'identity info t) info)))
2087 ;; 3. Test for internal id target.
2088 (org-test-with-parsed-data "* Headline1
2089 :PROPERTIES:
2090 :ID: aaaa
2091 :END:
2092 * Headline 2
2093 \[[id:aaaa]]"
2094 (should
2095 (org-export-resolve-id-link
2096 (org-element-map tree 'link 'identity info t) info)))
2097 ;; 4. Test for external id target.
2098 (org-test-with-parsed-data "[[id:aaaa]]"
2099 (should
2100 (org-export-resolve-id-link
2101 (org-element-map tree 'link 'identity info t)
2102 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
2104 (ert-deftest test-org-export/resolve-radio-link ()
2105 "Test `org-export-resolve-radio-link' specifications."
2106 ;; Standard test.
2107 (should
2108 (org-test-with-temp-text "<<<radio>>> radio"
2109 (org-update-radio-target-regexp)
2110 (let* ((tree (org-element-parse-buffer))
2111 (info `(:parse-tree ,tree)))
2112 (org-export-resolve-radio-link
2113 (org-element-map tree 'link 'identity info t)
2114 info))))
2115 ;; Radio targets are case-insensitive.
2116 (should
2117 (org-test-with-temp-text "<<<RADIO>>> radio"
2118 (org-update-radio-target-regexp)
2119 (let* ((tree (org-element-parse-buffer))
2120 (info `(:parse-tree ,tree)))
2121 (org-export-resolve-radio-link
2122 (org-element-map tree 'link 'identity info t)
2123 info))))
2124 ;; Radio target with objects.
2125 (should
2126 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
2127 (org-update-radio-target-regexp)
2128 (let* ((tree (org-element-parse-buffer))
2129 (info `(:parse-tree ,tree)))
2130 (org-export-resolve-radio-link
2131 (org-element-map tree 'link 'identity info t)
2132 info))))
2133 ;; Radio target with objects at its beginning.
2134 (should
2135 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
2136 (org-update-radio-target-regexp)
2137 (let* ((tree (org-element-parse-buffer))
2138 (info `(:parse-tree ,tree)))
2139 (org-export-resolve-radio-link
2140 (org-element-map tree 'link 'identity info t)
2141 info))))
2142 ;; Radio link next to an apostrophe.
2143 (should
2144 (org-test-with-temp-text "<<<radio>>> radio's"
2145 (org-update-radio-target-regexp)
2146 (let* ((tree (org-element-parse-buffer))
2147 (info `(:parse-tree ,tree)))
2148 (org-export-resolve-radio-link
2149 (org-element-map tree 'link 'identity info t)
2150 info))))
2151 ;; Multiple radio targets.
2152 (should
2153 (equal '("radio1" "radio2")
2154 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
2155 (org-update-radio-target-regexp)
2156 (let* ((tree (org-element-parse-buffer))
2157 (info `(:parse-tree ,tree)))
2158 (org-element-map tree 'link
2159 (lambda (link)
2160 (org-element-property
2161 :value (org-export-resolve-radio-link link info)))
2162 info)))))
2163 ;; Radio target is whitespace insensitive.
2164 (should
2165 (org-test-with-temp-text "<<<a radio>>> a\n radio"
2166 (org-update-radio-target-regexp)
2167 (let* ((tree (org-element-parse-buffer))
2168 (info `(:parse-tree ,tree)))
2169 (org-element-map tree 'link
2170 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
2174 ;;; Special blocks
2176 (ert-deftest test-org-export/raw-special-block-p ()
2177 "Test `org-export-raw-special-block-p' specifications."
2178 ;; Standard test.
2179 (should
2180 (org-test-with-parsed-data "#+BEGIN_FOO\nContents\n#+END_FOO"
2181 (let ((info (org-combine-plists
2182 info (list :back-end
2183 (org-export-create-backend :blocks '("FOO"))))))
2184 (org-export-raw-special-block-p
2185 (org-element-map tree 'special-block #'identity info t) info))))
2186 (should-not
2187 (org-test-with-parsed-data "#+BEGIN_BAR\nContents\n#+END_BAR"
2188 (let ((info (org-combine-plists
2189 info (list :back-end
2190 (org-export-create-backend :blocks '("FOO"))))))
2191 (org-export-raw-special-block-p
2192 (org-element-map tree 'special-block #'identity info t) info))))
2193 ;; Check is not case-sensitive.
2194 (should
2195 (org-test-with-parsed-data "#+begin_foo\nContents\n#+end_foo"
2196 (let ((info (org-combine-plists
2197 info (list :back-end
2198 (org-export-create-backend :blocks '("FOO"))))))
2199 (org-export-raw-special-block-p
2200 (org-element-map tree 'special-block #'identity info t) info))))
2201 ;; Test inheritance.
2202 (should
2203 (org-test-with-parsed-data "#+BEGIN_FOO\nContents\n#+END_FOO"
2204 (let* ((org-export--registered-backends
2205 (list (org-export-create-backend :name 'b1 :blocks '("FOO"))))
2206 (info (org-combine-plists
2207 info (list :back-end
2208 (org-export-create-backend :parent 'b1
2209 :blocks '("BAR"))))))
2210 (org-export-raw-special-block-p
2211 (org-element-map tree 'special-block #'identity info t) info))))
2212 (should-not
2213 (org-test-with-parsed-data "#+BEGIN_BAZ\nContents\n#+END_BAZ"
2214 (let* ((org-export--registered-backends
2215 (list (org-export-create-backend :name 'b1 :blocks '("FOO"))))
2216 (info (org-combine-plists
2217 info (list :back-end
2218 (org-export-create-backend :parent 'b1
2219 :blocks '("BAR"))))))
2220 (org-export-raw-special-block-p
2221 (org-element-map tree 'special-block #'identity info t) info))))
2222 ;; With optional argument, ignore inheritance.
2223 (should-not
2224 (org-test-with-parsed-data "#+BEGIN_FOO\nContents\n#+END_FOO"
2225 (let* ((org-export--registered-backends
2226 (list (org-export-create-backend :name 'b1 :blocks '("FOO"))))
2227 (info (org-combine-plists
2228 info (list :back-end
2229 (org-export-create-backend :parent 'b1
2230 :blocks '("BAR"))))))
2231 (org-export-raw-special-block-p
2232 (org-element-map tree 'special-block #'identity info t) info t)))))
2236 ;;; Src-block and example-block
2238 (ert-deftest test-org-export/unravel-code ()
2239 "Test `org-export-unravel-code' function."
2240 ;; Code without reference.
2241 (should
2242 (equal '("(+ 1 1)\n")
2243 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2244 (org-export-unravel-code (org-element-at-point)))))
2245 ;; Code with reference.
2246 (should
2247 (equal '("(+ 1 1)\n" (1 . "test"))
2248 (org-test-with-temp-text
2249 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
2250 (let ((org-coderef-label-format "(ref:%s)"))
2251 (org-export-unravel-code (org-element-at-point))))))
2252 ;; Code with user-defined reference.
2253 (should
2254 (equal
2255 '("(+ 1 1)\n" (1 . "test"))
2256 (org-test-with-temp-text
2257 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
2258 (let ((org-coderef-label-format "(ref:%s)"))
2259 (org-export-unravel-code (org-element-at-point))))))
2260 ;; Code references keys are relative to the current block.
2261 (should
2262 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
2263 (org-test-with-temp-text "
2264 #+BEGIN_EXAMPLE -n
2265 \(+ 1 1)
2266 #+END_EXAMPLE
2267 #+BEGIN_EXAMPLE +n
2268 \(+ 2 2)
2269 \(+ 3 3) (ref:one)
2270 #+END_EXAMPLE"
2271 (goto-line 5)
2272 (let ((org-coderef-label-format "(ref:%s)"))
2273 (org-export-unravel-code (org-element-at-point)))))))
2275 (ert-deftest test-org-export/format-code-default ()
2276 "Test `org-export-format-code-default' specifications."
2277 ;; Return the empty string when code is empty.
2278 (should
2279 (equal ""
2280 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
2281 (org-export-format-code-default
2282 (org-element-map tree 'src-block 'identity info t) info))))
2283 ;; Number lines, two whitespace characters before the actual loc.
2284 (should
2285 (equal "1 a\n2 b\n"
2286 (org-test-with-parsed-data
2287 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
2288 (org-export-format-code-default
2289 (org-element-map tree 'src-block 'identity info t) info))))
2290 ;; Put references 6 whitespace characters after the widest line,
2291 ;; wrapped within parenthesis.
2292 (should
2293 (equal "123 (a)\n1 (b)\n"
2294 (let ((org-coderef-label-format "(ref:%s)"))
2295 (org-test-with-parsed-data
2296 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
2297 (org-export-format-code-default
2298 (org-element-map tree 'src-block 'identity info t) info))))))
2302 ;;; Smart Quotes
2304 (ert-deftest test-org-export/activate-smart-quotes ()
2305 "Test `org-export-activate-smart-quotes' specifications."
2306 ;; Opening double quotes: standard test.
2307 (should
2308 (equal
2309 '("some &ldquo;paragraph")
2310 (let ((org-export-default-language "en"))
2311 (org-test-with-parsed-data "some \"paragraph"
2312 (org-element-map tree 'plain-text
2313 (lambda (s) (org-export-activate-smart-quotes s :html info))
2314 info)))))
2315 ;; Opening quotes: at the beginning of a paragraph.
2316 (should
2317 (equal
2318 '("&ldquo;begin")
2319 (let ((org-export-default-language "en"))
2320 (org-test-with-parsed-data "\"begin"
2321 (org-element-map tree 'plain-text
2322 (lambda (s) (org-export-activate-smart-quotes s :html info))
2323 info)))))
2324 ;; Opening quotes: after an object.
2325 (should
2326 (equal
2327 '("&ldquo;begin")
2328 (let ((org-export-default-language "en"))
2329 (org-test-with-parsed-data "=verb= \"begin"
2330 (org-element-map tree 'plain-text
2331 (lambda (s) (org-export-activate-smart-quotes s :html info))
2332 info)))))
2333 ;; Closing quotes: standard test.
2334 (should
2335 (equal
2336 '("some&rdquo; paragraph")
2337 (let ((org-export-default-language "en"))
2338 (org-test-with-parsed-data "some\" paragraph"
2339 (org-element-map tree 'plain-text
2340 (lambda (s) (org-export-activate-smart-quotes s :html info))
2341 info)))))
2342 ;; Closing quotes: at the end of a paragraph.
2343 (should
2344 (equal
2345 '("end&rdquo;")
2346 (let ((org-export-default-language "en"))
2347 (org-test-with-parsed-data "end\""
2348 (org-element-map tree 'plain-text
2349 (lambda (s) (org-export-activate-smart-quotes s :html info))
2350 info)))))
2351 ;; Apostrophe: standard test.
2352 (should
2353 (equal
2354 '("It shouldn&rsquo;t fail")
2355 (let ((org-export-default-language "en"))
2356 (org-test-with-parsed-data "It shouldn't fail"
2357 (org-element-map tree 'plain-text
2358 (lambda (s) (org-export-activate-smart-quotes s :html info))
2359 info)))))
2360 ;; Apostrophe: before an object.
2361 (should
2362 (equal
2363 '("a&rsquo;")
2364 (let ((org-export-default-language "en"))
2365 (org-test-with-parsed-data "a'=b="
2366 (org-element-map tree 'plain-text
2367 (lambda (s) (org-export-activate-smart-quotes s :html info))
2368 info)))))
2369 ;; Apostrophe: after an object.
2370 (should
2371 (equal
2372 '("&rsquo;s")
2373 (let ((org-export-default-language "en"))
2374 (org-test-with-parsed-data "=code='s"
2375 (org-element-map tree 'plain-text
2376 (lambda (s) (org-export-activate-smart-quotes s :html info))
2377 info)))))
2378 ;; Special case: isolated quotes.
2379 (should
2380 (equal '("&ldquo;" "&rdquo;")
2381 (let ((org-export-default-language "en"))
2382 (org-test-with-parsed-data "\"$x$\""
2383 (org-element-map tree 'plain-text
2384 (lambda (s) (org-export-activate-smart-quotes s :html info))
2385 info)))))
2386 ;; Smart quotes in secondary strings.
2387 (should
2388 (equal '("&ldquo;" "&rdquo;")
2389 (let ((org-export-default-language "en"))
2390 (org-test-with-parsed-data "* \"$x$\""
2391 (org-element-map tree 'plain-text
2392 (lambda (s) (org-export-activate-smart-quotes s :html info))
2393 info)))))
2394 ;; Smart quotes in document keywords.
2395 (should
2396 (equal '("&ldquo;" "&rdquo;")
2397 (let ((org-export-default-language "en"))
2398 (org-test-with-parsed-data "#+TITLE: \"$x$\""
2399 (org-element-map (plist-get info :title) 'plain-text
2400 (lambda (s) (org-export-activate-smart-quotes s :html info))
2401 info)))))
2402 ;; Smart quotes in parsed affiliated keywords.
2403 (should
2404 (equal '("&ldquo;" "&rdquo;" "Paragraph")
2405 (let ((org-export-default-language "en"))
2406 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
2407 (org-element-map tree 'plain-text
2408 (lambda (s) (org-export-activate-smart-quotes s :html info))
2409 info nil nil t))))))
2413 ;;; Tables
2415 (ert-deftest test-org-export/special-column ()
2416 "Test if the table's special column is properly recognized."
2417 ;; 1. First column is special if it contains only a special marking
2418 ;; characters or empty cells.
2419 (org-test-with-temp-text "
2420 | ! | 1 |
2421 | | 2 |"
2422 (should
2423 (org-export-table-has-special-column-p
2424 (org-element-map
2425 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2426 ;; 2. If the column contains anything else, it isn't special.
2427 (org-test-with-temp-text "
2428 | ! | 1 |
2429 | b | 2 |"
2430 (should-not
2431 (org-export-table-has-special-column-p
2432 (org-element-map
2433 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2434 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
2435 ;; and "!".
2436 (org-test-with-temp-text "
2437 | # | 1 |
2438 | ^ | 2 |
2439 | * | 3 |
2440 | _ | 4 |
2441 | / | 5 |
2442 | $ | 6 |
2443 | ! | 7 |"
2444 (should
2445 (org-export-table-has-special-column-p
2446 (org-element-map
2447 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2448 ;; 4. A first column with only empty cells isn't considered as
2449 ;; special.
2450 (org-test-with-temp-text "
2451 | | 1 |
2452 | | 2 |"
2453 (should-not
2454 (org-export-table-has-special-column-p
2455 (org-element-map
2456 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
2458 (ert-deftest test-org-export/table-row-is-special-p ()
2459 "Test `org-export-table-row-is-special-p' specifications."
2460 ;; 1. A row is special if it has a special marking character in the
2461 ;; special column.
2462 (org-test-with-parsed-data "| ! | 1 |"
2463 (should
2464 (org-export-table-row-is-special-p
2465 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2466 ;; 2. A row is special when its first field is "/"
2467 (org-test-with-parsed-data "
2468 | / | 1 |
2469 | a | b |"
2470 (should
2471 (org-export-table-row-is-special-p
2472 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2473 ;; 3. A row only containing alignment cookies is also considered as
2474 ;; special.
2475 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
2476 (should
2477 (org-export-table-row-is-special-p
2478 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2479 ;; 4. Everything else isn't considered as special.
2480 (org-test-with-parsed-data "| \alpha | | c |"
2481 (should-not
2482 (org-export-table-row-is-special-p
2483 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2484 ;; 5. Table's rules are never considered as special rows.
2485 (org-test-with-parsed-data "|---+---|"
2486 (should-not
2487 (org-export-table-row-is-special-p
2488 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
2490 (ert-deftest test-org-export/has-header-p ()
2491 "Test `org-export-table-has-header-p' specifications."
2492 ;; 1. With an header.
2493 (org-test-with-parsed-data "
2494 | a | b |
2495 |---+---|
2496 | c | d |"
2497 (should
2498 (org-export-table-has-header-p
2499 (org-element-map tree 'table 'identity info 'first-match)
2500 info)))
2501 ;; 2. Without an header.
2502 (org-test-with-parsed-data "
2503 | a | b |
2504 | c | d |"
2505 (should-not
2506 (org-export-table-has-header-p
2507 (org-element-map tree 'table 'identity info 'first-match)
2508 info)))
2509 ;; 3. Don't get fooled with starting and ending rules.
2510 (org-test-with-parsed-data "
2511 |---+---|
2512 | a | b |
2513 | c | d |
2514 |---+---|"
2515 (should-not
2516 (org-export-table-has-header-p
2517 (org-element-map tree 'table 'identity info 'first-match)
2518 info))))
2520 (ert-deftest test-org-export/table-row-group ()
2521 "Test `org-export-table-row-group' specifications."
2522 ;; 1. A rule creates a new group.
2523 (should
2524 (equal '(1 rule 2)
2525 (org-test-with-parsed-data "
2526 | a | b |
2527 |---+---|
2528 | 1 | 2 |"
2529 (org-element-map tree 'table-row
2530 (lambda (row)
2531 (if (eq (org-element-property :type row) 'rule) 'rule
2532 (org-export-table-row-group row info)))))))
2533 ;; 2. Special rows are ignored in count.
2534 (should
2535 (equal
2536 '(rule 1)
2537 (org-test-with-parsed-data "
2538 | / | < | > |
2539 |---|---+---|
2540 | | 1 | 2 |"
2541 (org-element-map tree 'table-row
2542 (lambda (row)
2543 (if (eq (org-element-property :type row) 'rule) 'rule
2544 (org-export-table-row-group row info)))
2545 info))))
2546 ;; 3. Double rules also are ignored in count.
2547 (should
2548 (equal '(1 rule rule 2)
2549 (org-test-with-parsed-data "
2550 | a | b |
2551 |---+---|
2552 |---+---|
2553 | 1 | 2 |"
2554 (org-element-map tree 'table-row
2555 (lambda (row)
2556 (if (eq (org-element-property :type row) 'rule) 'rule
2557 (org-export-table-row-group row info))))))))
2559 (ert-deftest test-org-export/table-row-number ()
2560 "Test `org-export-table-row-number' specifications."
2561 ;; Standard test. Number is 0-indexed.
2562 (should
2563 (equal '(0 1)
2564 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
2565 (org-element-map tree 'table-row
2566 (lambda (row) (org-export-table-row-number row info)) info))))
2567 ;; Number ignores separators.
2568 (should
2569 (equal '(0 1)
2570 (org-test-with-parsed-data "
2571 | a | b | c |
2572 |---+---+---|
2573 | d | e | f |"
2574 (org-element-map tree 'table-row
2575 (lambda (row) (org-export-table-row-number row info)) info))))
2576 ;; Number ignores special rows.
2577 (should
2578 (equal '(0 1)
2579 (org-test-with-parsed-data "
2580 | / | < | > |
2581 | | b | c |
2582 |---+-----+-----|
2583 | | <c> | <c> |
2584 | | e | f |"
2585 (org-element-map tree 'table-row
2586 (lambda (row) (org-export-table-row-number row info)) info)))))
2588 (ert-deftest test-org-export/table-cell-width ()
2589 "Test `org-export-table-cell-width' specifications."
2590 ;; 1. Width is primarily determined by width cookies. If no cookie
2591 ;; is found, cell's width is nil.
2592 (org-test-with-parsed-data "
2593 | / | <l> | <6> | <l7> |
2594 | | a | b | c |"
2595 (should
2596 (equal
2597 '(nil 6 7)
2598 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2599 (org-element-map tree 'table-cell 'identity info)))))
2600 ;; 2. The last width cookie has precedence.
2601 (org-test-with-parsed-data "
2602 | <6> |
2603 | <7> |
2604 | a |"
2605 (should
2606 (equal
2607 '(7)
2608 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2609 (org-element-map tree 'table-cell 'identity info)))))
2610 ;; 3. Valid width cookies must have a specific row.
2611 (org-test-with-parsed-data "| <6> | cell |"
2612 (should
2613 (equal
2614 '(nil nil)
2615 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2616 (org-element-map tree 'table-cell 'identity))))))
2618 (ert-deftest test-org-export/table-cell-alignment ()
2619 "Test `org-export-table-cell-alignment' specifications."
2620 ;; 1. Alignment is primarily determined by alignment cookies.
2621 (should
2622 (equal '(left center right)
2623 (let ((org-table-number-fraction 0.5)
2624 (org-table-number-regexp "^[0-9]+$"))
2625 (org-test-with-parsed-data "| <l> | <c> | <r> |"
2626 (mapcar (lambda (cell)
2627 (org-export-table-cell-alignment cell info))
2628 (org-element-map tree 'table-cell 'identity))))))
2629 ;; 2. The last alignment cookie has precedence.
2630 (should
2631 (equal '(right right right)
2632 (org-test-with-parsed-data "
2633 | <l8> |
2634 | cell |
2635 | <r9> |"
2636 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
2637 (org-element-map tree 'table-cell 'identity)))))
2638 ;; 3. If there's no cookie, cell's contents determine alignment.
2639 ;; A column mostly made of cells containing numbers will align
2640 ;; its cells to the right.
2641 (should
2642 (equal '(right right right)
2643 (let ((org-table-number-fraction 0.5)
2644 (org-table-number-regexp "^[0-9]+$"))
2645 (org-test-with-parsed-data "
2646 | 123 |
2647 | some text |
2648 | 12345 |"
2649 (mapcar (lambda (cell)
2650 (org-export-table-cell-alignment cell info))
2651 (org-element-map tree 'table-cell 'identity))))))
2652 ;; 4. Otherwise, they will be aligned to the left.
2653 (should
2654 (equal '(left left left)
2655 (org-test-with-parsed-data "
2656 | text |
2657 | some text |
2658 | \alpha |"
2659 (mapcar (lambda (cell)
2660 (org-export-table-cell-alignment cell info))
2661 (org-element-map tree 'table-cell 'identity info))))))
2663 (ert-deftest test-org-export/table-cell-borders ()
2664 "Test `org-export-table-cell-borders' specifications."
2665 ;; 1. Recognize various column groups indicators.
2666 (org-test-with-parsed-data "| / | < | > | <> |"
2667 (should
2668 (equal
2669 '((right bottom top) (left bottom top) (right bottom top)
2670 (right left bottom top))
2671 (mapcar (lambda (cell)
2672 (org-export-table-cell-borders cell info))
2673 (org-element-map tree 'table-cell 'identity)))))
2674 ;; 2. Accept shortcuts to define column groups.
2675 (org-test-with-parsed-data "| / | < | < |"
2676 (should
2677 (equal
2678 '((right bottom top) (right left bottom top) (left bottom top))
2679 (mapcar (lambda (cell)
2680 (org-export-table-cell-borders cell info))
2681 (org-element-map tree 'table-cell 'identity)))))
2682 ;; 3. A valid column groups row must start with a "/".
2683 (org-test-with-parsed-data "
2684 | | < |
2685 | a | b |"
2686 (should
2687 (equal '((top) (top) (bottom) (bottom))
2688 (mapcar (lambda (cell)
2689 (org-export-table-cell-borders cell info))
2690 (org-element-map tree 'table-cell 'identity)))))
2691 ;; 4. Take table rules into consideration.
2692 (org-test-with-parsed-data "
2693 | 1 |
2694 |---|
2695 | 2 |"
2696 (should
2697 (equal '((below top) (bottom above))
2698 (mapcar (lambda (cell)
2699 (org-export-table-cell-borders cell info))
2700 (org-element-map tree 'table-cell 'identity)))))
2701 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
2702 ;; (resp. `bottom' and `below') borders. Any special row is
2703 ;; ignored.
2704 (org-test-with-parsed-data "
2705 |---+----|
2706 | / | |
2707 | | 1 |
2708 |---+----|"
2709 (should
2710 (equal '((bottom below top above))
2711 (last
2712 (mapcar (lambda (cell)
2713 (org-export-table-cell-borders cell info))
2714 (org-element-map tree 'table-cell 'identity)))))))
2716 (ert-deftest test-org-export/table-dimensions ()
2717 "Test `org-export-table-dimensions' specifications."
2718 ;; 1. Standard test.
2719 (org-test-with-parsed-data "
2720 | 1 | 2 | 3 |
2721 | 4 | 5 | 6 |"
2722 (should
2723 (equal '(2 . 3)
2724 (org-export-table-dimensions
2725 (org-element-map tree 'table 'identity info 'first-match) info))))
2726 ;; 2. Ignore horizontal rules and special columns.
2727 (org-test-with-parsed-data "
2728 | / | < | > |
2729 | 1 | 2 | 3 |
2730 |---+---+---|
2731 | 4 | 5 | 6 |"
2732 (should
2733 (equal '(2 . 3)
2734 (org-export-table-dimensions
2735 (org-element-map tree 'table 'identity info 'first-match) info)))))
2737 (ert-deftest test-org-export/table-cell-address ()
2738 "Test `org-export-table-cell-address' specifications."
2739 ;; 1. Standard test: index is 0-based.
2740 (org-test-with-parsed-data "| a | b |"
2741 (should
2742 (equal '((0 . 0) (0 . 1))
2743 (org-element-map tree 'table-cell
2744 (lambda (cell) (org-export-table-cell-address cell info))
2745 info))))
2746 ;; 2. Special column isn't counted, nor are special rows.
2747 (org-test-with-parsed-data "
2748 | / | <> |
2749 | | c |"
2750 (should
2751 (equal '(0 . 0)
2752 (org-export-table-cell-address
2753 (car (last (org-element-map tree 'table-cell 'identity info)))
2754 info))))
2755 ;; 3. Tables rules do not count either.
2756 (org-test-with-parsed-data "
2757 | a |
2758 |---|
2759 | b |
2760 |---|
2761 | c |"
2762 (should
2763 (equal '(2 . 0)
2764 (org-export-table-cell-address
2765 (car (last (org-element-map tree 'table-cell 'identity info)))
2766 info))))
2767 ;; 4. Return nil for special cells.
2768 (org-test-with-parsed-data "| / | a |"
2769 (should-not
2770 (org-export-table-cell-address
2771 (org-element-map tree 'table-cell 'identity nil 'first-match)
2772 info))))
2774 (ert-deftest test-org-export/get-table-cell-at ()
2775 "Test `org-export-get-table-cell-at' specifications."
2776 ;; 1. Address ignores special columns, special rows and rules.
2777 (org-test-with-parsed-data "
2778 | / | <> |
2779 | | a |
2780 |---+----|
2781 | | b |"
2782 (should
2783 (equal '("b")
2784 (org-element-contents
2785 (org-export-get-table-cell-at
2786 '(1 . 0)
2787 (org-element-map tree 'table 'identity info 'first-match)
2788 info)))))
2789 ;; 2. Return value for a non-existent address is nil.
2790 (org-test-with-parsed-data "| a |"
2791 (should-not
2792 (org-export-get-table-cell-at
2793 '(2 . 2)
2794 (org-element-map tree 'table 'identity info 'first-match)
2795 info)))
2796 (org-test-with-parsed-data "| / |"
2797 (should-not
2798 (org-export-get-table-cell-at
2799 '(0 . 0)
2800 (org-element-map tree 'table 'identity info 'first-match)
2801 info))))
2803 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
2804 "Test `org-export-table-cell-starts-colgroup-p' specifications."
2805 ;; 1. A cell at a beginning of a row always starts a column group.
2806 (org-test-with-parsed-data "| a |"
2807 (should
2808 (org-export-table-cell-starts-colgroup-p
2809 (org-element-map tree 'table-cell 'identity info 'first-match)
2810 info)))
2811 ;; 2. Special column should be ignored when determining the
2812 ;; beginning of the row.
2813 (org-test-with-parsed-data "
2814 | / | |
2815 | | a |"
2816 (should
2817 (org-export-table-cell-starts-colgroup-p
2818 (org-element-map tree 'table-cell 'identity info 'first-match)
2819 info)))
2820 ;; 2. Explicit column groups.
2821 (org-test-with-parsed-data "
2822 | / | | < |
2823 | a | b | c |"
2824 (should
2825 (equal
2826 '(yes no yes)
2827 (org-element-map tree 'table-cell
2828 (lambda (cell)
2829 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
2830 info)))))
2832 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
2833 "Test `org-export-table-cell-ends-colgroup-p' specifications."
2834 ;; 1. A cell at the end of a row always ends a column group.
2835 (org-test-with-parsed-data "| a |"
2836 (should
2837 (org-export-table-cell-ends-colgroup-p
2838 (org-element-map tree 'table-cell 'identity info 'first-match)
2839 info)))
2840 ;; 2. Special column should be ignored when determining the
2841 ;; beginning of the row.
2842 (org-test-with-parsed-data "
2843 | / | |
2844 | | a |"
2845 (should
2846 (org-export-table-cell-ends-colgroup-p
2847 (org-element-map tree 'table-cell 'identity info 'first-match)
2848 info)))
2849 ;; 3. Explicit column groups.
2850 (org-test-with-parsed-data "
2851 | / | < | |
2852 | a | b | c |"
2853 (should
2854 (equal
2855 '(yes no yes)
2856 (org-element-map tree 'table-cell
2857 (lambda (cell)
2858 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
2859 info)))))
2861 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
2862 "Test `org-export-table-row-starts-rowgroup-p' specifications."
2863 ;; 1. A row at the beginning of a table always starts a row group.
2864 ;; So does a row following a table rule.
2865 (org-test-with-parsed-data "
2866 | a |
2867 |---|
2868 | b |"
2869 (should
2870 (equal
2871 '(yes no yes)
2872 (org-element-map tree 'table-row
2873 (lambda (row)
2874 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2875 info))))
2876 ;; 2. Special rows should be ignored when determining the beginning
2877 ;; of the row.
2878 (org-test-with-parsed-data "
2879 | / | < |
2880 | | a |
2881 |---+---|
2882 | / | < |
2883 | | b |"
2884 (should
2885 (equal
2886 '(yes no yes)
2887 (org-element-map tree 'table-row
2888 (lambda (row)
2889 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2890 info)))))
2892 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
2893 "Test `org-export-table-row-ends-rowgroup-p' specifications."
2894 ;; 1. A row at the end of a table always ends a row group. So does
2895 ;; a row preceding a table rule.
2896 (org-test-with-parsed-data "
2897 | a |
2898 |---|
2899 | b |"
2900 (should
2901 (equal
2902 '(yes no yes)
2903 (org-element-map tree 'table-row
2904 (lambda (row)
2905 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2906 info))))
2907 ;; 2. Special rows should be ignored when determining the beginning
2908 ;; of the row.
2909 (org-test-with-parsed-data "
2910 | | a |
2911 | / | < |
2912 |---+---|
2913 | | b |
2914 | / | < |"
2915 (should
2916 (equal
2917 '(yes no yes)
2918 (org-element-map tree 'table-row
2919 (lambda (row)
2920 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2921 info)))))
2923 (ert-deftest test-org-export/table-row-in-header-p ()
2924 "Test `org-export-table-row-in-header-p' specifications."
2925 ;; Standard test. Separators are always nil.
2926 (should
2927 (equal
2928 '(yes no no)
2929 (org-test-with-parsed-data "| a |\n|---|\n| b |"
2930 (org-element-map tree 'table-row
2931 (lambda (row)
2932 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
2933 ;; Nil when there is no header.
2934 (should
2935 (equal
2936 '(no no)
2937 (org-test-with-parsed-data "| a |\n| b |"
2938 (org-element-map tree 'table-row
2939 (lambda (row)
2940 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
2942 (ert-deftest test-org-export/table-row-starts-header-p ()
2943 "Test `org-export-table-row-starts-header-p' specifications."
2944 ;; 1. Only the row starting the first row group starts the table
2945 ;; header.
2946 (org-test-with-parsed-data "
2947 | a |
2948 | b |
2949 |---|
2950 | c |"
2951 (should
2952 (equal
2953 '(yes no no no)
2954 (org-element-map tree 'table-row
2955 (lambda (row)
2956 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
2957 info))))
2958 ;; 2. A row cannot start an header if there's no header in the
2959 ;; table.
2960 (org-test-with-parsed-data "
2961 | a |
2962 |---|"
2963 (should-not
2964 (org-export-table-row-starts-header-p
2965 (org-element-map tree 'table-row 'identity info 'first-match)
2966 info))))
2968 (ert-deftest test-org-export/table-row-ends-header-p ()
2969 "Test `org-export-table-row-ends-header-p' specifications."
2970 ;; 1. Only the row starting the first row group starts the table
2971 ;; header.
2972 (org-test-with-parsed-data "
2973 | a |
2974 | b |
2975 |---|
2976 | c |"
2977 (should
2978 (equal
2979 '(no yes no no)
2980 (org-element-map tree 'table-row
2981 (lambda (row)
2982 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
2983 info))))
2984 ;; 2. A row cannot start an header if there's no header in the
2985 ;; table.
2986 (org-test-with-parsed-data "
2987 | a |
2988 |---|"
2989 (should-not
2990 (org-export-table-row-ends-header-p
2991 (org-element-map tree 'table-row 'identity info 'first-match)
2992 info))))
2996 ;;; Tables of Contents
2998 (ert-deftest test-org-export/collect-headlines ()
2999 "Test `org-export-collect-headlines' specifications."
3000 ;; Standard test.
3001 (should
3002 (= 2
3003 (length
3004 (org-test-with-parsed-data "* H1\n** H2"
3005 (org-export-collect-headlines info)))))
3006 ;; Do not collect headlines below optional argument.
3007 (should
3008 (= 1
3009 (length
3010 (org-test-with-parsed-data "* H1\n** H2"
3011 (org-export-collect-headlines info 1)))))
3012 ;; Never collect headlines below maximum headline level.
3013 (should
3014 (= 1
3015 (length
3016 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3017 (org-export-collect-headlines info)))))
3018 (should
3019 (= 1
3020 (length
3021 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3022 (org-export-collect-headlines info 2))))))
3026 ;;; Templates
3028 (ert-deftest test-org-export/inner-template ()
3029 "Test `inner-template' translator specifications."
3030 (should
3031 (equal "Success!"
3032 (org-test-with-temp-text "* Headline"
3033 (org-export-as
3034 (org-export-create-backend
3035 :transcoders
3036 '((inner-template . (lambda (contents info) "Success!"))
3037 (headline . (lambda (h c i) "Headline"))))))))
3038 ;; Inner template is applied even in a "body-only" export.
3039 (should
3040 (equal "Success!"
3041 (org-test-with-temp-text "* Headline"
3042 (org-export-as
3043 (org-export-create-backend
3044 :transcoders '((inner-template . (lambda (c i) "Success!"))
3045 (headline . (lambda (h c i) "Headline"))))
3046 nil nil 'body-only)))))
3048 (ert-deftest test-org-export/template ()
3049 "Test `template' translator specifications."
3050 (should
3051 (equal "Success!"
3052 (org-test-with-temp-text "* Headline"
3053 (org-export-as
3054 (org-export-create-backend
3055 :transcoders '((template . (lambda (contents info) "Success!"))
3056 (headline . (lambda (h c i) "Headline"))))))))
3057 ;; Template is not applied in a "body-only" export.
3058 (should-not
3059 (equal "Success!"
3060 (org-test-with-temp-text "* Headline"
3061 (org-export-as
3062 (org-export-create-backend
3063 :transcoders '((template . (lambda (contents info) "Success!"))
3064 (headline . (lambda (h c i) "Headline"))))
3065 nil nil 'body-only)))))
3069 ;;; Topology
3071 (ert-deftest test-org-export/get-next-element ()
3072 "Test `org-export-get-next-element' specifications."
3073 ;; Standard test.
3074 (should
3075 (equal "b"
3076 (org-test-with-parsed-data "* Headline\n*a* b"
3077 (org-export-get-next-element
3078 (org-element-map tree 'bold 'identity info t) info))))
3079 ;; Return nil when no previous element.
3080 (should-not
3081 (org-test-with-parsed-data "* Headline\na *b*"
3082 (org-export-get-next-element
3083 (org-element-map tree 'bold 'identity info t) info)))
3084 ;; Non-exportable elements are ignored.
3085 (should-not
3086 (let ((org-export-with-timestamps nil))
3087 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
3088 (org-export-get-next-element
3089 (org-element-map tree 'entity 'identity info t) info))))
3090 ;; Find next element in secondary strings.
3091 (should
3092 (eq 'verbatim
3093 (org-test-with-parsed-data "* a =verb="
3094 (org-element-type
3095 (org-export-get-next-element
3096 (org-element-map tree 'plain-text 'identity info t) info)))))
3097 (should
3098 (eq 'verbatim
3099 (org-test-with-parsed-data "* /italic/ =verb="
3100 (org-element-type
3101 (org-export-get-next-element
3102 (org-element-map tree 'italic 'identity info t) info)))))
3103 ;; Find next element in document keywords.
3104 (should
3105 (eq 'verbatim
3106 (org-test-with-parsed-data "#+TITLE: a =verb="
3107 (org-element-type
3108 (org-export-get-next-element
3109 (org-element-map
3110 (plist-get info :title) 'plain-text 'identity info t) info)))))
3111 ;; Find next element in parsed affiliated keywords.
3112 (should
3113 (eq 'verbatim
3114 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
3115 (org-element-type
3116 (org-export-get-next-element
3117 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3118 ;; With optional argument N, return a list containing all the
3119 ;; following elements.
3120 (should
3121 (equal
3122 '(bold code underline)
3123 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3124 (mapcar 'car
3125 (org-export-get-next-element
3126 (org-element-map tree 'italic 'identity info t) info t)))))
3127 ;; When N is a positive integer, return a list containing up to
3128 ;; N following elements.
3129 (should
3130 (equal
3131 '(bold code)
3132 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3133 (mapcar 'car
3134 (org-export-get-next-element
3135 (org-element-map tree 'italic 'identity info t) info 2))))))
3137 (ert-deftest test-org-export/get-previous-element ()
3138 "Test `org-export-get-previous-element' specifications."
3139 ;; Standard test.
3140 (should
3141 (equal "a "
3142 (org-test-with-parsed-data "* Headline\na *b*"
3143 (org-export-get-previous-element
3144 (org-element-map tree 'bold 'identity info t) info))))
3145 ;; Return nil when no previous element.
3146 (should-not
3147 (org-test-with-parsed-data "* Headline\n*a* b"
3148 (org-export-get-previous-element
3149 (org-element-map tree 'bold 'identity info t) info)))
3150 ;; Non-exportable elements are ignored.
3151 (should-not
3152 (let ((org-export-with-timestamps nil))
3153 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
3154 (org-export-get-previous-element
3155 (org-element-map tree 'entity 'identity info t) info))))
3156 ;; Find previous element in secondary strings.
3157 (should
3158 (eq 'verbatim
3159 (org-test-with-parsed-data "* =verb= a"
3160 (org-element-type
3161 (org-export-get-previous-element
3162 (org-element-map tree 'plain-text 'identity info t) info)))))
3163 (should
3164 (eq 'verbatim
3165 (org-test-with-parsed-data "* =verb= /italic/"
3166 (org-element-type
3167 (org-export-get-previous-element
3168 (org-element-map tree 'italic 'identity info t) info)))))
3169 ;; Find previous element in document keywords.
3170 (should
3171 (eq 'verbatim
3172 (org-test-with-parsed-data "#+TITLE: =verb= a"
3173 (org-element-type
3174 (org-export-get-previous-element
3175 (org-element-map
3176 (plist-get info :title) 'plain-text 'identity info t) info)))))
3177 ;; Find previous element in parsed affiliated keywords.
3178 (should
3179 (eq 'verbatim
3180 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
3181 (org-element-type
3182 (org-export-get-previous-element
3183 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3184 ;; With optional argument N, return a list containing up to
3185 ;; N previous elements.
3186 (should
3187 (equal '(underline italic bold)
3188 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3189 (mapcar 'car
3190 (org-export-get-previous-element
3191 (org-element-map tree 'code 'identity info t) info t)))))
3192 ;; When N is a positive integer, return a list containing up to
3193 ;; N previous elements.
3194 (should
3195 (equal '(italic bold)
3196 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3197 (mapcar 'car
3198 (org-export-get-previous-element
3199 (org-element-map tree 'code 'identity info t) info 2))))))
3202 (provide 'test-ox)
3203 ;;; test-org-export.el end here