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