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