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