Merge branch 'maint'
[org-mode.git] / testing / lisp / test-ox.el
blobb5a4d3a5e8d33c01814fbdc3c648092aec505225
1 ;;; test-ox.el --- Tests for ox.el
3 ;; Copyright (C) 2012, 2013 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
7 ;; This file is not part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Code:
24 (unless (featurep 'ox)
25 (signal 'missing-test-dependency "org-export"))
27 (defun org-test-default-backend ()
28 "Return a default export back-end.
29 This back-end simply returns parsed data as Org syntax."
30 (org-export-create-backend
31 :transcoders (let (transcode-table)
32 (dolist (type (append org-element-all-elements
33 org-element-all-objects)
34 transcode-table)
35 (push
36 (cons type
37 (lambda (obj contents info)
38 (funcall
39 (intern (format "org-element-%s-interpreter"
40 type))
41 obj contents)))
42 transcode-table)))))
44 (defmacro org-test-with-parsed-data (data &rest body)
45 "Execute body with parsed data available.
47 DATA is a string containing the data to be parsed. BODY is the
48 body to execute. Parse tree is available under the `tree'
49 variable, and communication channel under `info'.
51 This function calls `org-export-collect-tree-properties'. As
52 such, `:ignore-list' (for `org-element-map') and
53 `:parse-tree' (for `org-export-get-genealogy') properties are
54 already filled in `info'."
55 (declare (debug (form body)) (indent 1))
56 `(org-test-with-temp-text ,data
57 (let* ((tree (org-element-parse-buffer))
58 (info (org-export-collect-tree-properties
59 tree (org-export-get-environment))))
60 ,@body)))
64 ;;; Internal Tests
66 (ert-deftest test-org-export/bind-keyword ()
67 "Test reading #+BIND: keywords."
68 ;; Test with `org-export-allow-bind-keywords' set to t.
69 (should
70 (org-test-with-temp-text "#+BIND: test-ox-var value"
71 (let ((org-export-allow-bind-keywords t))
72 (org-export-get-environment)
73 (eq test-ox-var 'value))))
74 ;; Test with `org-export-allow-bind-keywords' set to nil.
75 (should-not
76 (org-test-with-temp-text "#+BIND: test-ox-var value"
77 (let ((org-export-allow-bind-keywords nil))
78 (org-export-get-environment)
79 (boundp 'test-ox-var))))
80 ;; BIND keywords are case-insensitive.
81 (should
82 (org-test-with-temp-text "#+bind: test-ox-var value"
83 (let ((org-export-allow-bind-keywords t))
84 (org-export-get-environment)
85 (eq test-ox-var 'value))))
86 ;; Preserve order of BIND keywords.
87 (should
88 (org-test-with-temp-text "#+BIND: test-ox-var 1\n#+BIND: test-ox-var 2"
89 (let ((org-export-allow-bind-keywords t))
90 (org-export-get-environment)
91 (eq test-ox-var 2))))
92 ;; Read BIND keywords in setup files.
93 (should
94 (org-test-with-temp-text
95 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
96 (let ((org-export-allow-bind-keywords t))
97 (org-export-get-environment)
98 (eq variable 'value))))
99 ;; Verify that bound variables are seen during export.
100 (should
101 (equal "Yes\n"
102 (org-test-with-temp-text "#+BIND: test-ox-var value"
103 (let ((org-export-allow-bind-keywords t))
104 (org-export-as
105 (org-export-create-backend
106 :transcoders
107 '((section . (lambda (s c i)
108 (if (eq test-ox-var 'value) "Yes" "No")))))))))))
110 (ert-deftest test-org-export/parse-option-keyword ()
111 "Test reading all standard #+OPTIONS: items."
112 (should
113 (equal
114 (org-export--parse-option-keyword
115 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
116 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
117 stat:t")
118 '(:headline-levels
119 1 :preserve-breaks t :section-numbers t :time-stamp-file t
120 :with-archived-trees t :with-author t :with-creator t :with-drawers t
121 :with-email t :with-emphasize t :with-entities t :with-fixed-width t
122 :with-footnotes t :with-inlinetasks nil :with-priority t
123 :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
124 :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
125 :with-todo-keywords t)))
126 ;; Test some special values.
127 (should
128 (equal
129 (org-export--parse-option-keyword
130 "arch:headline creator:comment d:(\"TEST\")
131 ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
132 '( :section-numbers
134 :with-archived-trees headline :with-creator comment
135 :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
136 :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
138 (ert-deftest test-org-export/get-inbuffer-options ()
139 "Test reading all standard export keywords."
140 ;; Properties should follow buffer order.
141 (should
142 (equal
143 (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
144 (org-export--get-inbuffer-options))
145 '(:language "fr" :creator "Me" :email "email")))
146 ;; Parse document keywords.
147 (should
148 (equal
149 (org-test-with-temp-text "#+AUTHOR: Me"
150 (org-export--get-inbuffer-options))
151 '(:author ("Me"))))
152 ;; Test `space' behaviour.
153 (should
154 (equal
155 (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
156 (org-export--get-inbuffer-options))
157 '(:title ("Some title with spaces"))))
158 ;; Test `newline' behaviour.
159 (should
160 (equal
161 (org-test-with-temp-text "#+DESCRIPTION: With\n#+DESCRIPTION: two lines"
162 (org-export--get-inbuffer-options))
163 '(:description "With\ntwo lines")))
164 ;; Test `split' behaviour.
165 (should
166 (equal
167 (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
168 (org-export--get-inbuffer-options))
169 '(:select-tags ("a" "b"))))
170 ;; Options set through SETUPFILE.
171 (should
172 (equal
173 (org-test-with-temp-text
174 (format "#+DESCRIPTION: l1
175 #+LANGUAGE: es
176 #+SELECT_TAGS: a
177 #+TITLE: a
178 #+SETUPFILE: \"%s/examples/setupfile.org\"
179 #+DESCRIPTION: l3
180 #+LANGUAGE: fr
181 #+SELECT_TAGS: c
182 #+TITLE: c"
183 org-test-dir)
184 (org-export--get-inbuffer-options))
185 '(:description "l1\nl2\nl3":language "fr" :select-tags ("a" "b" "c")
186 :title ("a b c"))))
187 ;; More than one property can refer to the same buffer keyword.
188 (should
189 (equal '(:k2 "value" :k1 "value")
190 (let ((backend (org-export-create-backend
191 :options '((:k1 "KEYWORD")
192 (:k2 "KEYWORD")))))
193 (org-test-with-temp-text "#+KEYWORD: value"
194 (org-export--get-inbuffer-options backend))))))
196 (ert-deftest test-org-export/get-subtree-options ()
197 "Test setting options from headline's properties."
198 ;; EXPORT_TITLE.
199 (org-test-with-temp-text "#+TITLE: Title
200 * Headline
201 :PROPERTIES:
202 :EXPORT_TITLE: Subtree Title
203 :END:
204 Paragraph"
205 (forward-line)
206 (should (equal (plist-get (org-export-get-environment nil t) :title)
207 '("Subtree Title"))))
208 :title
209 '("subtree-title")
210 ;; EXPORT_OPTIONS.
211 (org-test-with-temp-text "#+OPTIONS: H:1
212 * Headline
213 :PROPERTIES:
214 :EXPORT_OPTIONS: H:2
215 :END:
216 Paragraph"
217 (forward-line)
218 (should
219 (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
220 ;; EXPORT_DATE.
221 (org-test-with-temp-text "#+DATE: today
222 * Headline
223 :PROPERTIES:
224 :EXPORT_DATE: 29-03-2012
225 :END:
226 Paragraph"
227 (forward-line)
228 (should (equal (plist-get (org-export-get-environment nil t) :date)
229 '("29-03-2012"))))
230 ;; Properties with `split' behaviour are stored as a list of
231 ;; strings.
232 (should
233 (equal '("a" "b")
234 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
235 * Headline
236 :PROPERTIES:
237 :EXPORT_EXCLUDE_TAGS: a b
238 :END:
239 Paragraph"
240 (progn
241 (forward-line)
242 (plist-get (org-export-get-environment nil t) :exclude-tags)))))
243 ;; Handle :PROPERTY+: syntax.
244 (should
245 (equal '("a" "b")
246 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
247 * Headline
248 :PROPERTIES:
249 :EXPORT_EXCLUDE_TAGS: a
250 :EXPORT_EXCLUDE_TAGS+: b
251 :END:
252 Paragraph"
253 (progn
254 (forward-line)
255 (plist-get (org-export-get-environment nil t) :exclude-tags)))))
256 ;; Export properties are case-insensitive.
257 (org-test-with-temp-text "* Headline
258 :PROPERTIES:
259 :EXPORT_Date: 29-03-2012
260 :END:
261 Paragraph"
262 (should (equal (plist-get (org-export-get-environment nil t) :date)
263 '("29-03-2012"))))
264 ;; Still grab correct options when section above is empty.
265 (should
266 (equal '("H1")
267 (org-test-with-temp-text "* H1\n** H11\n** H12"
268 (progn (forward-line 2)
269 (plist-get (org-export-get-environment nil t) :title))))))
271 (ert-deftest test-org-export/set-title ()
272 "Test title setting."
273 ;; If no title if specified, use file name.
274 (should
275 (apply
276 'equal
277 (org-test-with-temp-text-in-file "Test"
278 (org-mode)
279 (list (org-export-as
280 (org-export-create-backend
281 :transcoders
282 '((template . (lambda (text info)
283 (org-element-interpret-data
284 (plist-get info :title) 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")))))
1087 (ert-deftest test-org-export/data-with-backend ()
1088 "Test `org-export-data-with-backend' specifications."
1089 ;; Error when calling an undefined back-end.
1090 (should-error (org-export-data-with-backend nil "nil" nil))
1091 ;; Otherwise, export data recursively, using correct back-end.
1092 (should
1093 (equal
1094 "Success!"
1095 (org-export-data-with-backend
1096 '(bold nil "Test")
1097 (org-export-create-backend
1098 :transcoders
1099 '((plain-text . (lambda (text info) "Success"))
1100 (bold . (lambda (bold contents info) (concat contents "!")))))
1101 '(:with-emphasize t)))))
1105 ;;; Export Snippets
1107 (ert-deftest test-org-export/export-snippet ()
1108 "Test export snippets transcoding."
1109 ;; Standard test.
1110 (org-test-with-temp-text "@@test:A@@@@t:B@@"
1111 (let ((backend (org-test-default-backend)))
1112 (setf (org-export-backend-name backend) 'test)
1113 (setf (org-export-backend-transcoders backend)
1114 (cons (cons 'export-snippet
1115 (lambda (snippet contents info)
1116 (when (eq (org-export-snippet-backend snippet) 'test)
1117 (org-element-property :value snippet))))
1118 (org-export-backend-transcoders backend)))
1119 (let ((org-export-snippet-translation-alist nil))
1120 (should (equal (org-export-as backend) "A\n")))
1121 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
1122 (should (equal (org-export-as backend) "AB\n")))))
1123 ;; Ignored export snippets do not remove any blank.
1124 (should
1125 (equal "begin end\n"
1126 (org-test-with-parsed-data "begin @@test:A@@ end"
1127 (org-export-data-with-backend
1128 tree
1129 (org-export-create-backend
1130 :transcoders
1131 '((paragraph . (lambda (paragraph contents info) contents))
1132 (section . (lambda (section contents info) contents))))
1133 info)))))
1137 ;;; Footnotes
1139 (ert-deftest test-org-export/footnotes ()
1140 "Test footnotes specifications."
1141 (let ((org-footnote-section nil)
1142 (org-export-with-footnotes t))
1143 ;; 1. Read every type of footnote.
1144 (should
1145 (equal
1146 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
1147 (org-test-with-parsed-data
1148 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
1149 (org-element-map tree 'footnote-reference
1150 (lambda (ref)
1151 (let ((def (org-export-get-footnote-definition ref info)))
1152 (cons (org-export-get-footnote-number ref info)
1153 (if (eq (org-element-property :type ref) 'inline) (car def)
1154 (car (org-element-contents
1155 (car (org-element-contents def))))))))
1156 info))))
1157 ;; 2. Test nested footnotes order.
1158 (should
1159 (equal
1160 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
1161 (org-test-with-parsed-data
1162 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
1163 (org-element-map tree 'footnote-reference
1164 (lambda (ref)
1165 (when (org-export-footnote-first-reference-p ref info)
1166 (cons (org-export-get-footnote-number ref info)
1167 (org-element-property :label ref))))
1168 info))))
1169 ;; 3. Test nested footnote in invisible definitions.
1170 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
1171 ;; Hide definitions.
1172 (narrow-to-region (point) (point-at-eol))
1173 (let* ((tree (org-element-parse-buffer))
1174 (info (org-combine-plists
1175 `(:parse-tree ,tree)
1176 (org-export-collect-tree-properties
1177 tree (org-export-get-environment)))))
1178 ;; Both footnotes should be seen.
1179 (should
1180 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
1181 ;; 4. Test footnotes definitions collection.
1182 (should
1183 (= 4
1184 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1186 \[fn:2] B [fn:3] [fn::D].
1188 \[fn:3] C."
1189 (length (org-export-collect-footnote-definitions tree info)))))
1190 ;; 5. Test export of footnotes defined outside parsing scope.
1191 (should
1192 (equal
1193 "ParagraphOut of scope\n"
1194 (org-test-with-temp-text "[fn:1] Out of scope
1195 * Title
1196 Paragraph[fn:1]"
1197 (let ((backend (org-test-default-backend)))
1198 (setf (org-export-backend-transcoders backend)
1199 (cons (cons 'footnote-reference
1200 (lambda (fn contents info)
1201 (org-element-interpret-data
1202 (org-export-get-footnote-definition fn info))))
1203 (org-export-backend-transcoders backend)))
1204 (forward-line)
1205 (org-export-as backend 'subtree)))))
1206 ;; 6. Footnotes without a definition should be provided a fallback
1207 ;; definition.
1208 (should
1209 (org-test-with-parsed-data "[fn:1]"
1210 (org-export-get-footnote-definition
1211 (org-element-map tree 'footnote-reference 'identity info t) info)))
1212 ;; 7. Footnote section should be ignored in TOC and in headlines
1213 ;; numbering.
1214 (should
1215 (= 1 (let ((org-footnote-section "Footnotes"))
1216 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
1217 (org-export-collect-headlines info))))))
1218 (should
1219 (equal '(2)
1220 (let ((org-footnote-section "Footnotes"))
1221 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
1222 (org-element-map tree 'headline
1223 (lambda (hl)
1224 (when (equal (org-element-property :raw-value hl) "H2")
1225 (org-export-get-headline-number hl info)))
1226 info t)))))))
1230 ;;; Headlines and Inlinetasks
1232 (ert-deftest test-org-export/get-relative-level ()
1233 "Test `org-export-get-relative-level' specifications."
1234 ;; Standard test.
1235 (should
1236 (equal '(1 2)
1237 (let ((org-odd-levels-only nil))
1238 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1239 (org-element-map tree 'headline
1240 (lambda (h) (org-export-get-relative-level h info))
1241 info)))))
1242 ;; Missing levels
1243 (should
1244 (equal '(1 3)
1245 (let ((org-odd-levels-only nil))
1246 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
1247 (org-element-map tree 'headline
1248 (lambda (h) (org-export-get-relative-level h info))
1249 info))))))
1251 (ert-deftest test-org-export/low-level-p ()
1252 "Test `org-export-low-level-p' specifications."
1253 (should
1254 (equal
1255 '(no yes)
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) (if (org-export-low-level-p h info) 'yes 'no))
1260 (plist-put info :headline-levels 1)))))))
1262 (ert-deftest test-org-export/get-headline-number ()
1263 "Test `org-export-get-headline-number' specifications."
1264 ;; Standard test.
1265 (should
1266 (equal
1267 '((1) (1 1))
1268 (let ((org-odd-levels-only nil))
1269 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1270 (org-element-map tree 'headline
1271 (lambda (h) (org-export-get-headline-number h info))
1272 info)))))
1273 ;; Missing levels are replaced with 0.
1274 (should
1275 (equal
1276 '((1) (1 0 1))
1277 (let ((org-odd-levels-only nil))
1278 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
1279 (org-element-map tree 'headline
1280 (lambda (h) (org-export-get-headline-number h info))
1281 info))))))
1283 (ert-deftest test-org-export/numbered-headline-p ()
1284 "Test `org-export-numbered-headline-p' specifications."
1285 ;; If `:section-numbers' is nil, never number headlines.
1286 (should-not
1287 (org-test-with-parsed-data "* Headline"
1288 (org-element-map tree 'headline
1289 (lambda (h) (org-export-numbered-headline-p h info))
1290 (plist-put info :section-numbers nil))))
1291 ;; If `:section-numbers' is a number, only number headlines with
1292 ;; a level greater that it.
1293 (should
1294 (equal
1295 '(yes no)
1296 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1297 (org-element-map tree 'headline
1298 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
1299 (plist-put info :section-numbers 1)))))
1300 ;; Otherwise, headlines are always numbered.
1301 (should
1302 (org-test-with-parsed-data "* Headline"
1303 (org-element-map tree 'headline
1304 (lambda (h) (org-export-numbered-headline-p h info))
1305 (plist-put info :section-numbers t)))))
1307 (ert-deftest test-org-export/number-to-roman ()
1308 "Test `org-export-number-to-roman' specifications."
1309 ;; If number is negative, return it as a string.
1310 (should (equal (org-export-number-to-roman -1) "-1"))
1311 ;; Otherwise, return it as a roman number.
1312 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
1314 (ert-deftest test-org-export/get-optional-title ()
1315 "Test `org-export-get-alt-title' specifications."
1316 ;; If ALT_TITLE property is defined, use it.
1317 (should
1318 (equal '("opt")
1319 (org-test-with-parsed-data
1320 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
1321 (org-export-get-alt-title
1322 (org-element-map tree 'headline 'identity info t)
1323 info))))
1324 ;; Otherwise, fall-back to regular title.
1325 (should
1326 (equal '("Headline")
1327 (org-test-with-parsed-data "* Headline"
1328 (org-export-get-alt-title
1329 (org-element-map tree 'headline 'identity info t)
1330 info)))))
1332 (ert-deftest test-org-export/get-tags ()
1333 "Test `org-export-get-tags' specifications."
1334 (let ((org-export-exclude-tags '("noexport"))
1335 (org-export-select-tags '("export")))
1336 ;; Standard test: tags which are not a select tag, an exclude tag,
1337 ;; or specified as optional argument shouldn't be ignored.
1338 (should
1339 (org-test-with-parsed-data "* Headline :tag:"
1340 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1341 info)))
1342 ;; Exclude tags are removed.
1343 (should-not
1344 (org-test-with-parsed-data "* Headline :noexport:"
1345 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1346 info)))
1347 ;; Select tags are removed.
1348 (should-not
1349 (org-test-with-parsed-data "* Headline :export:"
1350 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1351 info)))
1352 (should
1353 (equal
1354 '("tag")
1355 (org-test-with-parsed-data "* Headline :tag:export:"
1356 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1357 info))))
1358 ;; Tags provided in the optional argument are also ignored.
1359 (should-not
1360 (org-test-with-parsed-data "* Headline :ignore:"
1361 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1362 info '("ignore"))))
1363 ;; Allow tag inheritance.
1364 (should
1365 (equal
1366 '(("tag") ("tag"))
1367 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
1368 (org-element-map tree 'headline
1369 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
1370 ;; Tag inheritance checks FILETAGS keywords.
1371 (should
1372 (equal
1373 '(("a" "b" "tag"))
1374 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
1375 (org-element-map tree 'headline
1376 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
1378 (ert-deftest test-org-export/get-node-property ()
1379 "Test`org-export-get-node-property' specifications."
1380 ;; Standard test.
1381 (should
1382 (equal "value"
1383 (org-test-with-parsed-data "* Headline
1384 :PROPERTIES:
1385 :prop: value
1386 :END:"
1387 (org-export-get-node-property
1388 :PROP (org-element-map tree 'headline 'identity nil t)))))
1389 ;; Test inheritance.
1390 (should
1391 (equal "value"
1392 (org-test-with-parsed-data "* Parent
1393 :PROPERTIES:
1394 :prop: value
1395 :END:
1396 ** Headline
1397 Paragraph"
1398 (org-export-get-node-property
1399 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
1400 ;; Cannot return a value before the first headline.
1401 (should-not
1402 (org-test-with-parsed-data "Paragraph
1403 * Headline
1404 :PROPERTIES:
1405 :prop: value
1406 :END:"
1407 (org-export-get-node-property
1408 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
1410 (ert-deftest test-org-export/get-category ()
1411 "Test `org-export-get-category' specifications."
1412 ;; Standard test.
1413 (should
1414 (equal "value"
1415 (org-test-with-parsed-data "* Headline
1416 :PROPERTIES:
1417 :CATEGORY: value
1418 :END:"
1419 (org-export-get-category
1420 (org-element-map tree 'headline 'identity nil t) info))))
1421 ;; Test inheritance from a parent headline.
1422 (should
1423 (equal '("value" "value")
1424 (org-test-with-parsed-data "* Headline1
1425 :PROPERTIES:
1426 :CATEGORY: value
1427 :END:
1428 ** Headline2"
1429 (org-element-map tree 'headline
1430 (lambda (hl) (org-export-get-category hl info)) info))))
1431 ;; Test inheritance from #+CATEGORY keyword
1432 (should
1433 (equal "value"
1434 (org-test-with-parsed-data "#+CATEGORY: value
1435 * Headline"
1436 (org-export-get-category
1437 (org-element-map tree 'headline 'identity nil t) info))))
1438 ;; Test inheritance from file name.
1439 (should
1440 (equal "test"
1441 (org-test-with-parsed-data "* Headline"
1442 (let ((info (plist-put info :input-file "~/test.org")))
1443 (org-export-get-category
1444 (org-element-map tree 'headline 'identity nil t) info)))))
1445 ;; Fall-back value.
1446 (should
1447 (equal "???"
1448 (org-test-with-parsed-data "* Headline"
1449 (org-export-get-category
1450 (org-element-map tree 'headline 'identity nil t) info)))))
1452 (ert-deftest test-org-export/first-sibling-p ()
1453 "Test `org-export-first-sibling-p' specifications."
1454 ;; Standard test.
1455 (should
1456 (equal
1457 '(yes yes no)
1458 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1459 (org-element-map tree 'headline
1460 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1461 info))))
1462 ;; Ignore headlines not exported.
1463 (should
1464 (equal
1465 '(yes)
1466 (let ((org-export-exclude-tags '("ignore")))
1467 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
1468 (org-element-map tree 'headline
1469 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1470 info))))))
1472 (ert-deftest test-org-export/last-sibling-p ()
1473 "Test `org-export-last-sibling-p' specifications."
1474 ;; Standard test.
1475 (should
1476 (equal
1477 '(yes no yes)
1478 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1479 (org-element-map tree 'headline
1480 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1481 info))))
1482 ;; Ignore headlines not exported.
1483 (should
1484 (equal
1485 '(yes)
1486 (let ((org-export-exclude-tags '("ignore")))
1487 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
1488 (org-element-map tree 'headline
1489 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1490 info))))))
1492 (ert-deftest test-org-export/handle-inlinetasks ()
1493 "Test inlinetask export."
1494 ;; Inlinetask with an exclude tag.
1495 (when (featurep 'org-inlinetask)
1496 (should
1497 (equal
1499 (let ((org-inlinetask-min-level 3))
1500 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
1501 (org-export-as (org-test-default-backend)
1502 nil nil nil '(:exclude-tags ("noexp")))))))
1503 ;; Inlinetask with an include tag.
1504 (should
1505 (equal
1506 "* H2\n*** Inline :exp:\n"
1507 (let ((org-inlinetask-min-level 3)
1508 (org-tags-column 0))
1509 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
1510 (org-export-as (org-test-default-backend)
1511 nil nil nil '(:select-tags ("exp")))))))
1512 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
1513 (should
1514 (equal ""
1515 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
1516 (org-inlinetask-min-level 3))
1517 (org-test-with-temp-text "*** TODO Inline"
1518 (org-export-as (org-test-default-backend)
1519 nil nil nil '(:with-tasks nil))))))))
1523 ;;; Keywords
1525 (ert-deftest test-org-export/get-date ()
1526 "Test `org-export-get-date' specifications."
1527 ;; Return a properly formatted string when
1528 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
1529 ;; consists in a single timestamp.
1530 (should
1531 (equal "29 03 2012"
1532 (let ((org-export-date-timestamp-format "%d %m %Y"))
1533 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1534 (org-export-get-date info)))))
1535 ;; Return a secondary string otherwise.
1536 (should-not
1537 (stringp
1538 (let ((org-export-date-timestamp-format nil))
1539 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1540 (org-export-get-date info)))))
1541 (should
1542 (equal '("Date")
1543 (org-test-with-parsed-data "#+DATE: Date"
1544 (org-export-get-date info))))
1545 ;; Optional argument has precedence over
1546 ;; `org-export-date-timestamp-format'.
1547 (should
1548 (equal "29 03"
1549 (let ((org-export-date-timestamp-format "%d %m %Y"))
1550 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1551 (org-export-get-date info "%d %m"))))))
1555 ;;; Links
1557 (ert-deftest test-org-export/get-coderef-format ()
1558 "Test `org-export-get-coderef-format' specifications."
1559 ;; A link without description returns "%s"
1560 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
1561 "%s"))
1562 ;; Return "%s" when path is matched within description.
1563 (should (equal (org-export-get-coderef-format "path" "desc (path)")
1564 "desc %s"))
1565 ;; Otherwise return description.
1566 (should (equal (org-export-get-coderef-format "path" "desc")
1567 "desc")))
1569 (ert-deftest test-org-export/inline-image-p ()
1570 "Test `org-export-inline-image-p' specifications."
1571 (should
1572 (org-export-inline-image-p
1573 (org-test-with-temp-text "[[#id]]"
1574 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
1575 '(("custom-id" . "id")))))
1577 (ert-deftest test-org-export/fuzzy-link ()
1578 "Test fuzzy links specifications."
1579 ;; Link to an headline should return headline's number.
1580 (org-test-with-parsed-data
1581 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
1582 (should
1583 ;; Note: Headline's number is in fact a list of numbers.
1584 (equal '(2)
1585 (org-element-map tree 'link
1586 (lambda (link)
1587 (org-export-get-ordinal
1588 (org-export-resolve-fuzzy-link link info) info)) info t))))
1589 ;; Link to a target in an item should return item's number.
1590 (org-test-with-parsed-data
1591 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
1592 (should
1593 ;; Note: Item's number is in fact a list of numbers.
1594 (equal '(1 2)
1595 (org-element-map tree 'link
1596 (lambda (link)
1597 (org-export-get-ordinal
1598 (org-export-resolve-fuzzy-link link info) info)) info t))))
1599 ;; Link to a target in a footnote should return footnote's number.
1600 (org-test-with-parsed-data "
1601 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
1602 (should
1603 (equal '(2 3)
1604 (org-element-map tree 'link
1605 (lambda (link)
1606 (org-export-get-ordinal
1607 (org-export-resolve-fuzzy-link link info) info)) info))))
1608 ;; Link to a named element should return sequence number of that
1609 ;; element.
1610 (org-test-with-parsed-data
1611 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
1612 (should
1613 (= 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 not within an item, a table, a footnote
1619 ;; reference or definition should return section number.
1620 (org-test-with-parsed-data
1621 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
1622 (should
1623 (equal '(2)
1624 (org-element-map tree 'link
1625 (lambda (link)
1626 (org-export-get-ordinal
1627 (org-export-resolve-fuzzy-link link info) info)) info t))))
1628 ;; Space are not significant when matching a fuzzy link.
1629 (should
1630 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
1631 (org-element-map tree 'link
1632 (lambda (link) (org-export-resolve-fuzzy-link link info))
1633 info t)))
1634 ;; Statistics cookies are ignored for headline match.
1635 (should
1636 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
1637 (org-element-map tree 'link
1638 (lambda (link) (org-export-resolve-fuzzy-link link info))
1639 info t)))
1640 (should
1641 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
1642 (org-element-map tree 'link
1643 (lambda (link) (org-export-resolve-fuzzy-link link info))
1644 info t)))
1645 ;; Headline match is position dependent.
1646 (should-not
1647 (apply
1649 (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
1650 (org-element-map tree 'link
1651 (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
1653 (ert-deftest test-org-export/resolve-coderef ()
1654 "Test `org-export-resolve-coderef' specifications."
1655 (let ((org-coderef-label-format "(ref:%s)"))
1656 ;; 1. A link to a "-n -k -r" block returns line number.
1657 (org-test-with-parsed-data
1658 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
1659 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1660 (org-test-with-parsed-data
1661 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1662 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1663 ;; 2. A link to a "-n -r" block returns line number.
1664 (org-test-with-parsed-data
1665 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
1666 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1667 (org-test-with-parsed-data
1668 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1669 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1670 ;; 3. A link to a "-n" block returns coderef.
1671 (org-test-with-parsed-data
1672 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1673 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1674 (org-test-with-parsed-data
1675 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
1676 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1677 ;; 4. A link to a "-r" block returns line number.
1678 (org-test-with-parsed-data
1679 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1680 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1681 (org-test-with-parsed-data
1682 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
1683 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1684 ;; 5. A link to a block without a switch returns coderef.
1685 (org-test-with-parsed-data
1686 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1687 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1688 (org-test-with-parsed-data
1689 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
1690 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1691 ;; 6. Correctly handle continued line numbers. A "+n" switch
1692 ;; should resume numbering from previous block with numbered
1693 ;; lines, ignoring blocks not numbering lines in the process.
1694 ;; A "-n" switch resets count.
1695 (org-test-with-parsed-data "
1696 #+BEGIN_EXAMPLE -n
1697 Text.
1698 #+END_EXAMPLE
1700 #+BEGIN_SRC emacs-lisp
1701 \(- 1 1)
1702 #+END_SRC
1704 #+BEGIN_SRC emacs-lisp +n -r
1705 \(+ 1 1) (ref:addition)
1706 #+END_SRC
1708 #+BEGIN_EXAMPLE -n -r
1709 Another text. (ref:text)
1710 #+END_EXAMPLE"
1711 (should (= (org-export-resolve-coderef "addition" info) 2))
1712 (should (= (org-export-resolve-coderef "text" info) 1)))
1713 ;; 7. Recognize coderef with user-specified syntax.
1714 (org-test-with-parsed-data
1715 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
1716 (should (equal (org-export-resolve-coderef "text" info) "text")))))
1718 (ert-deftest test-org-export/resolve-fuzzy-link ()
1719 "Test `org-export-resolve-fuzzy-link' specifications."
1720 ;; Match target objects.
1721 (should
1722 (org-test-with-parsed-data "<<target>> [[target]]"
1723 (org-export-resolve-fuzzy-link
1724 (org-element-map tree 'link 'identity info t) info)))
1725 ;; Match named elements.
1726 (should
1727 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
1728 (org-export-resolve-fuzzy-link
1729 (org-element-map tree 'link 'identity info t) info)))
1730 ;; Match exact headline's name.
1731 (should
1732 (org-test-with-parsed-data "* My headline\n[[My headline]]"
1733 (org-export-resolve-fuzzy-link
1734 (org-element-map tree 'link 'identity info t) info)))
1735 ;; Targets objects have priority over named elements and headline
1736 ;; titles.
1737 (should
1738 (eq 'target
1739 (org-test-with-parsed-data
1740 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
1741 (org-element-type
1742 (org-export-resolve-fuzzy-link
1743 (org-element-map tree 'link 'identity info t) info)))))
1744 ;; Named elements have priority over headline titles.
1745 (should
1746 (eq 'paragraph
1747 (org-test-with-parsed-data
1748 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
1749 (org-element-type
1750 (org-export-resolve-fuzzy-link
1751 (org-element-map tree 'link 'identity info t) info)))))
1752 ;; If link's path starts with a "*", only match headline titles,
1753 ;; though.
1754 (should
1755 (eq 'headline
1756 (org-test-with-parsed-data
1757 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
1758 (org-element-type
1759 (org-export-resolve-fuzzy-link
1760 (org-element-map tree 'link 'identity info t) info)))))
1761 ;; Return nil if no match.
1762 (should-not
1763 (org-test-with-parsed-data "[[target]]"
1764 (org-export-resolve-fuzzy-link
1765 (org-element-map tree 'link 'identity info t) info)))
1766 ;; Match fuzzy link even when before first headline.
1767 (should
1768 (eq 'headline
1769 (org-test-with-parsed-data "[[hl]]\n* hl"
1770 (org-element-type
1771 (org-export-resolve-fuzzy-link
1772 (org-element-map tree 'link 'identity info t) info))))))
1774 (ert-deftest test-org-export/resolve-id-link ()
1775 "Test `org-export-resolve-id-link' specifications."
1776 ;; 1. Regular test for custom-id link.
1777 (org-test-with-parsed-data "* Headline1
1778 :PROPERTIES:
1779 :CUSTOM_ID: test
1780 :END:
1781 * Headline 2
1782 \[[#test]]"
1783 (should
1784 (org-export-resolve-id-link
1785 (org-element-map tree 'link 'identity info t) info)))
1786 ;; 2. Failing test for custom-id link.
1787 (org-test-with-parsed-data "* Headline1
1788 :PROPERTIES:
1789 :CUSTOM_ID: test
1790 :END:
1791 * Headline 2
1792 \[[#no-match]]"
1793 (should-not
1794 (org-export-resolve-id-link
1795 (org-element-map tree 'link 'identity info t) info)))
1796 ;; 3. Test for internal id target.
1797 (org-test-with-parsed-data "* Headline1
1798 :PROPERTIES:
1799 :ID: aaaa
1800 :END:
1801 * Headline 2
1802 \[[id:aaaa]]"
1803 (should
1804 (org-export-resolve-id-link
1805 (org-element-map tree 'link 'identity info t) info)))
1806 ;; 4. Test for external id target.
1807 (org-test-with-parsed-data "[[id:aaaa]]"
1808 (should
1809 (org-export-resolve-id-link
1810 (org-element-map tree 'link 'identity info t)
1811 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
1813 (ert-deftest test-org-export/resolve-radio-link ()
1814 "Test `org-export-resolve-radio-link' specifications."
1815 ;; Standard test.
1816 (should
1817 (org-test-with-temp-text "<<<radio>>> radio"
1818 (org-update-radio-target-regexp)
1819 (let* ((tree (org-element-parse-buffer))
1820 (info `(:parse-tree ,tree)))
1821 (org-export-resolve-radio-link
1822 (org-element-map tree 'link 'identity info t)
1823 info))))
1824 ;; Radio targets are case-insensitive.
1825 (should
1826 (org-test-with-temp-text "<<<RADIO>>> radio"
1827 (org-update-radio-target-regexp)
1828 (let* ((tree (org-element-parse-buffer))
1829 (info `(:parse-tree ,tree)))
1830 (org-export-resolve-radio-link
1831 (org-element-map tree 'link 'identity info t)
1832 info))))
1833 ;; Radio target with objects.
1834 (should
1835 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
1836 (org-update-radio-target-regexp)
1837 (let* ((tree (org-element-parse-buffer))
1838 (info `(:parse-tree ,tree)))
1839 (org-export-resolve-radio-link
1840 (org-element-map tree 'link 'identity info t)
1841 info))))
1842 ;; Multiple radio targets.
1843 (should
1844 (equal '("radio1" "radio2")
1845 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
1846 (org-update-radio-target-regexp)
1847 (let* ((tree (org-element-parse-buffer))
1848 (info `(:parse-tree ,tree)))
1849 (org-element-map tree 'link
1850 (lambda (link)
1851 (org-element-property
1852 :value (org-export-resolve-radio-link link info)))
1853 info)))))
1854 ;; Radio target is whitespace insensitive.
1855 (should
1856 (org-test-with-temp-text "<<<a radio>>> a\n radio"
1857 (org-update-radio-target-regexp)
1858 (let* ((tree (org-element-parse-buffer))
1859 (info `(:parse-tree ,tree)))
1860 (org-element-map tree 'link
1861 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
1865 ;;; Src-block and example-block
1867 (ert-deftest test-org-export/unravel-code ()
1868 "Test `org-export-unravel-code' function."
1869 ;; Code without reference.
1870 (should
1871 (equal '("(+ 1 1)\n")
1872 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
1873 (org-export-unravel-code (org-element-at-point)))))
1874 ;; Code with reference.
1875 (should
1876 (equal '("(+ 1 1)\n" (1 . "test"))
1877 (org-test-with-temp-text
1878 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
1879 (let ((org-coderef-label-format "(ref:%s)"))
1880 (org-export-unravel-code (org-element-at-point))))))
1881 ;; Code with user-defined reference.
1882 (should
1883 (equal
1884 '("(+ 1 1)\n" (1 . "test"))
1885 (org-test-with-temp-text
1886 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
1887 (let ((org-coderef-label-format "(ref:%s)"))
1888 (org-export-unravel-code (org-element-at-point))))))
1889 ;; Code references keys are relative to the current block.
1890 (should
1891 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
1892 (org-test-with-temp-text "
1893 #+BEGIN_EXAMPLE -n
1894 \(+ 1 1)
1895 #+END_EXAMPLE
1896 #+BEGIN_EXAMPLE +n
1897 \(+ 2 2)
1898 \(+ 3 3) (ref:one)
1899 #+END_EXAMPLE"
1900 (goto-line 5)
1901 (let ((org-coderef-label-format "(ref:%s)"))
1902 (org-export-unravel-code (org-element-at-point)))))))
1904 (ert-deftest test-org-export/format-code-default ()
1905 "Test `org-export-format-code-default' specifications."
1906 ;; Return the empty string when code is empty.
1907 (should
1908 (equal ""
1909 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
1910 (org-export-format-code-default
1911 (org-element-map tree 'src-block 'identity info t) info))))
1912 ;; Number lines, two whitespace characters before the actual loc.
1913 (should
1914 (equal "1 a\n2 b\n"
1915 (org-test-with-parsed-data
1916 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
1917 (org-export-format-code-default
1918 (org-element-map tree 'src-block 'identity info t) info))))
1919 ;; Put references 6 whitespace characters after the widest line,
1920 ;; wrapped within parenthesis.
1921 (should
1922 (equal "123 (a)\n1 (b)\n"
1923 (let ((org-coderef-label-format "(ref:%s)"))
1924 (org-test-with-parsed-data
1925 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
1926 (org-export-format-code-default
1927 (org-element-map tree 'src-block 'identity info t) info))))))
1931 ;;; Smart Quotes
1933 (ert-deftest test-org-export/activate-smart-quotes ()
1934 "Test `org-export-activate-smart-quotes' specifications."
1935 ;; Opening double quotes: standard test.
1936 (should
1937 (equal
1938 '("some &ldquo;paragraph")
1939 (let ((org-export-default-language "en"))
1940 (org-test-with-parsed-data "some \"paragraph"
1941 (org-element-map tree 'plain-text
1942 (lambda (s) (org-export-activate-smart-quotes s :html info))
1943 info)))))
1944 ;; Opening quotes: at the beginning of a paragraph.
1945 (should
1946 (equal
1947 '("&ldquo;begin")
1948 (let ((org-export-default-language "en"))
1949 (org-test-with-parsed-data "\"begin"
1950 (org-element-map tree 'plain-text
1951 (lambda (s) (org-export-activate-smart-quotes s :html info))
1952 info)))))
1953 ;; Opening quotes: after an object.
1954 (should
1955 (equal
1956 '("&ldquo;begin")
1957 (let ((org-export-default-language "en"))
1958 (org-test-with-parsed-data "=verb= \"begin"
1959 (org-element-map tree 'plain-text
1960 (lambda (s) (org-export-activate-smart-quotes s :html info))
1961 info)))))
1962 ;; Closing quotes: standard test.
1963 (should
1964 (equal
1965 '("some&rdquo; paragraph")
1966 (let ((org-export-default-language "en"))
1967 (org-test-with-parsed-data "some\" paragraph"
1968 (org-element-map tree 'plain-text
1969 (lambda (s) (org-export-activate-smart-quotes s :html info))
1970 info)))))
1971 ;; Closing quotes: at the end of a paragraph.
1972 (should
1973 (equal
1974 '("end&rdquo;")
1975 (let ((org-export-default-language "en"))
1976 (org-test-with-parsed-data "end\""
1977 (org-element-map tree 'plain-text
1978 (lambda (s) (org-export-activate-smart-quotes s :html info))
1979 info)))))
1980 ;; Apostrophe: standard test.
1981 (should
1982 (equal
1983 '("It shouldn&rsquo;t fail")
1984 (let ((org-export-default-language "en"))
1985 (org-test-with-parsed-data "It shouldn't fail"
1986 (org-element-map tree 'plain-text
1987 (lambda (s) (org-export-activate-smart-quotes s :html info))
1988 info)))))
1989 ;; Apostrophe: before an object.
1990 (should
1991 (equal
1992 '("a&rsquo;")
1993 (let ((org-export-default-language "en"))
1994 (org-test-with-parsed-data "a'=b="
1995 (org-element-map tree 'plain-text
1996 (lambda (s) (org-export-activate-smart-quotes s :html info))
1997 info)))))
1998 ;; Apostrophe: after an object.
1999 (should
2000 (equal
2001 '("&rsquo;s")
2002 (let ((org-export-default-language "en"))
2003 (org-test-with-parsed-data "=code='s"
2004 (org-element-map tree 'plain-text
2005 (lambda (s) (org-export-activate-smart-quotes s :html info))
2006 info)))))
2007 ;; Special case: isolated quotes.
2008 (should
2009 (equal '("&ldquo;" "&rdquo;")
2010 (let ((org-export-default-language "en"))
2011 (org-test-with-parsed-data "\"$x$\""
2012 (org-element-map tree 'plain-text
2013 (lambda (s) (org-export-activate-smart-quotes s :html info))
2014 info)))))
2015 ;; Smart quotes in secondary strings.
2016 (should
2017 (equal '("&ldquo;" "&rdquo;")
2018 (let ((org-export-default-language "en"))
2019 (org-test-with-parsed-data "* \"$x$\""
2020 (org-element-map tree 'plain-text
2021 (lambda (s) (org-export-activate-smart-quotes s :html info))
2022 info)))))
2023 ;; Smart quotes in document keywords.
2024 (should
2025 (equal '("&ldquo;" "&rdquo;")
2026 (let ((org-export-default-language "en"))
2027 (org-test-with-parsed-data "#+TITLE: \"$x$\""
2028 (org-element-map (plist-get info :title) 'plain-text
2029 (lambda (s) (org-export-activate-smart-quotes s :html info))
2030 info)))))
2031 ;; Smart quotes in parsed affiliated keywords.
2032 (should
2033 (equal '("&ldquo;" "&rdquo;" "Paragraph")
2034 (let ((org-export-default-language "en"))
2035 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
2036 (org-element-map tree 'plain-text
2037 (lambda (s) (org-export-activate-smart-quotes s :html info))
2038 info nil nil t))))))
2042 ;;; Tables
2044 (ert-deftest test-org-export/special-column ()
2045 "Test if the table's special column is properly recognized."
2046 ;; 1. First column is special if it contains only a special marking
2047 ;; characters or empty cells.
2048 (org-test-with-temp-text "
2049 | ! | 1 |
2050 | | 2 |"
2051 (should
2052 (org-export-table-has-special-column-p
2053 (org-element-map
2054 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2055 ;; 2. If the column contains anything else, it isn't special.
2056 (org-test-with-temp-text "
2057 | ! | 1 |
2058 | b | 2 |"
2059 (should-not
2060 (org-export-table-has-special-column-p
2061 (org-element-map
2062 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2063 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
2064 ;; and "!".
2065 (org-test-with-temp-text "
2066 | # | 1 |
2067 | ^ | 2 |
2068 | * | 3 |
2069 | _ | 4 |
2070 | / | 5 |
2071 | $ | 6 |
2072 | ! | 7 |"
2073 (should
2074 (org-export-table-has-special-column-p
2075 (org-element-map
2076 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2077 ;; 4. A first column with only empty cells isn't considered as
2078 ;; special.
2079 (org-test-with-temp-text "
2080 | | 1 |
2081 | | 2 |"
2082 (should-not
2083 (org-export-table-has-special-column-p
2084 (org-element-map
2085 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
2087 (ert-deftest test-org-export/table-row-is-special-p ()
2088 "Test `org-export-table-row-is-special-p' specifications."
2089 ;; 1. A row is special if it has a special marking character in the
2090 ;; special column.
2091 (org-test-with-parsed-data "| ! | 1 |"
2092 (should
2093 (org-export-table-row-is-special-p
2094 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2095 ;; 2. A row is special when its first field is "/"
2096 (org-test-with-parsed-data "
2097 | / | 1 |
2098 | a | b |"
2099 (should
2100 (org-export-table-row-is-special-p
2101 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2102 ;; 3. A row only containing alignment cookies is also considered as
2103 ;; special.
2104 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
2105 (should
2106 (org-export-table-row-is-special-p
2107 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2108 ;; 4. Everything else isn't considered as special.
2109 (org-test-with-parsed-data "| \alpha | | c |"
2110 (should-not
2111 (org-export-table-row-is-special-p
2112 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2113 ;; 5. Table's rules are never considered as special rows.
2114 (org-test-with-parsed-data "|---+---|"
2115 (should-not
2116 (org-export-table-row-is-special-p
2117 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
2119 (ert-deftest test-org-export/has-header-p ()
2120 "Test `org-export-table-has-header-p' specifications."
2121 ;; 1. With an header.
2122 (org-test-with-parsed-data "
2123 | a | b |
2124 |---+---|
2125 | c | d |"
2126 (should
2127 (org-export-table-has-header-p
2128 (org-element-map tree 'table 'identity info 'first-match)
2129 info)))
2130 ;; 2. Without an header.
2131 (org-test-with-parsed-data "
2132 | a | b |
2133 | c | d |"
2134 (should-not
2135 (org-export-table-has-header-p
2136 (org-element-map tree 'table 'identity info 'first-match)
2137 info)))
2138 ;; 3. Don't get fooled with starting and ending rules.
2139 (org-test-with-parsed-data "
2140 |---+---|
2141 | a | b |
2142 | c | d |
2143 |---+---|"
2144 (should-not
2145 (org-export-table-has-header-p
2146 (org-element-map tree 'table 'identity info 'first-match)
2147 info))))
2149 (ert-deftest test-org-export/table-row-group ()
2150 "Test `org-export-table-row-group' specifications."
2151 ;; 1. A rule creates a new group.
2152 (should
2153 (equal '(1 rule 2)
2154 (org-test-with-parsed-data "
2155 | a | b |
2156 |---+---|
2157 | 1 | 2 |"
2158 (org-element-map tree 'table-row
2159 (lambda (row)
2160 (if (eq (org-element-property :type row) 'rule) 'rule
2161 (org-export-table-row-group row info)))))))
2162 ;; 2. Special rows are ignored in count.
2163 (should
2164 (equal
2165 '(rule 1)
2166 (org-test-with-parsed-data "
2167 | / | < | > |
2168 |---|---+---|
2169 | | 1 | 2 |"
2170 (org-element-map tree 'table-row
2171 (lambda (row)
2172 (if (eq (org-element-property :type row) 'rule) 'rule
2173 (org-export-table-row-group row info)))
2174 info))))
2175 ;; 3. Double rules also are ignored in count.
2176 (should
2177 (equal '(1 rule rule 2)
2178 (org-test-with-parsed-data "
2179 | a | b |
2180 |---+---|
2181 |---+---|
2182 | 1 | 2 |"
2183 (org-element-map tree 'table-row
2184 (lambda (row)
2185 (if (eq (org-element-property :type row) 'rule) 'rule
2186 (org-export-table-row-group row info))))))))
2188 (ert-deftest test-org-export/table-row-number ()
2189 "Test `org-export-table-row-number' specifications."
2190 ;; Standard test. Number is 0-indexed.
2191 (should
2192 (equal '(0 1)
2193 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
2194 (org-element-map tree 'table-row
2195 (lambda (row) (org-export-table-row-number row info)) info))))
2196 ;; Number ignores separators.
2197 (should
2198 (equal '(0 1)
2199 (org-test-with-parsed-data "
2200 | a | b | c |
2201 |---+---+---|
2202 | d | e | f |"
2203 (org-element-map tree 'table-row
2204 (lambda (row) (org-export-table-row-number row info)) info))))
2205 ;; Number ignores special rows.
2206 (should
2207 (equal '(0 1)
2208 (org-test-with-parsed-data "
2209 | / | < | > |
2210 | | b | c |
2211 |---+-----+-----|
2212 | | <c> | <c> |
2213 | | e | f |"
2214 (org-element-map tree 'table-row
2215 (lambda (row) (org-export-table-row-number row info)) info)))))
2217 (ert-deftest test-org-export/table-cell-width ()
2218 "Test `org-export-table-cell-width' specifications."
2219 ;; 1. Width is primarily determined by width cookies. If no cookie
2220 ;; is found, cell's width is nil.
2221 (org-test-with-parsed-data "
2222 | / | <l> | <6> | <l7> |
2223 | | a | b | c |"
2224 (should
2225 (equal
2226 '(nil 6 7)
2227 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2228 (org-element-map tree 'table-cell 'identity info)))))
2229 ;; 2. The last width cookie has precedence.
2230 (org-test-with-parsed-data "
2231 | <6> |
2232 | <7> |
2233 | a |"
2234 (should
2235 (equal
2236 '(7)
2237 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2238 (org-element-map tree 'table-cell 'identity info)))))
2239 ;; 3. Valid width cookies must have a specific row.
2240 (org-test-with-parsed-data "| <6> | cell |"
2241 (should
2242 (equal
2243 '(nil nil)
2244 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2245 (org-element-map tree 'table-cell 'identity))))))
2247 (ert-deftest test-org-export/table-cell-alignment ()
2248 "Test `org-export-table-cell-alignment' specifications."
2249 ;; 1. Alignment is primarily determined by alignment cookies.
2250 (should
2251 (equal '(left center right)
2252 (let ((org-table-number-fraction 0.5)
2253 (org-table-number-regexp "^[0-9]+$"))
2254 (org-test-with-parsed-data "| <l> | <c> | <r> |"
2255 (mapcar (lambda (cell)
2256 (org-export-table-cell-alignment cell info))
2257 (org-element-map tree 'table-cell 'identity))))))
2258 ;; 2. The last alignment cookie has precedence.
2259 (should
2260 (equal '(right right right)
2261 (org-test-with-parsed-data "
2262 | <l8> |
2263 | cell |
2264 | <r9> |"
2265 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
2266 (org-element-map tree 'table-cell 'identity)))))
2267 ;; 3. If there's no cookie, cell's contents determine alignment.
2268 ;; A column mostly made of cells containing numbers will align
2269 ;; its cells to the right.
2270 (should
2271 (equal '(right right right)
2272 (let ((org-table-number-fraction 0.5)
2273 (org-table-number-regexp "^[0-9]+$"))
2274 (org-test-with-parsed-data "
2275 | 123 |
2276 | some text |
2277 | 12345 |"
2278 (mapcar (lambda (cell)
2279 (org-export-table-cell-alignment cell info))
2280 (org-element-map tree 'table-cell 'identity))))))
2281 ;; 4. Otherwise, they will be aligned to the left.
2282 (should
2283 (equal '(left left left)
2284 (org-test-with-parsed-data "
2285 | text |
2286 | some text |
2287 | \alpha |"
2288 (mapcar (lambda (cell)
2289 (org-export-table-cell-alignment cell info))
2290 (org-element-map tree 'table-cell 'identity info))))))
2292 (ert-deftest test-org-export/table-cell-borders ()
2293 "Test `org-export-table-cell-borders' specifications."
2294 ;; 1. Recognize various column groups indicators.
2295 (org-test-with-parsed-data "| / | < | > | <> |"
2296 (should
2297 (equal
2298 '((right bottom top) (left bottom top) (right bottom top)
2299 (right left bottom top))
2300 (mapcar (lambda (cell)
2301 (org-export-table-cell-borders cell info))
2302 (org-element-map tree 'table-cell 'identity)))))
2303 ;; 2. Accept shortcuts to define column groups.
2304 (org-test-with-parsed-data "| / | < | < |"
2305 (should
2306 (equal
2307 '((right bottom top) (right left bottom top) (left bottom top))
2308 (mapcar (lambda (cell)
2309 (org-export-table-cell-borders cell info))
2310 (org-element-map tree 'table-cell 'identity)))))
2311 ;; 3. A valid column groups row must start with a "/".
2312 (org-test-with-parsed-data "
2313 | | < |
2314 | a | b |"
2315 (should
2316 (equal '((top) (top) (bottom) (bottom))
2317 (mapcar (lambda (cell)
2318 (org-export-table-cell-borders cell info))
2319 (org-element-map tree 'table-cell 'identity)))))
2320 ;; 4. Take table rules into consideration.
2321 (org-test-with-parsed-data "
2322 | 1 |
2323 |---|
2324 | 2 |"
2325 (should
2326 (equal '((below top) (bottom above))
2327 (mapcar (lambda (cell)
2328 (org-export-table-cell-borders cell info))
2329 (org-element-map tree 'table-cell 'identity)))))
2330 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
2331 ;; (resp. `bottom' and `below') borders. Any special row is
2332 ;; ignored.
2333 (org-test-with-parsed-data "
2334 |---+----|
2335 | / | |
2336 | | 1 |
2337 |---+----|"
2338 (should
2339 (equal '((bottom below top above))
2340 (last
2341 (mapcar (lambda (cell)
2342 (org-export-table-cell-borders cell info))
2343 (org-element-map tree 'table-cell 'identity)))))))
2345 (ert-deftest test-org-export/table-dimensions ()
2346 "Test `org-export-table-dimensions' specifications."
2347 ;; 1. Standard test.
2348 (org-test-with-parsed-data "
2349 | 1 | 2 | 3 |
2350 | 4 | 5 | 6 |"
2351 (should
2352 (equal '(2 . 3)
2353 (org-export-table-dimensions
2354 (org-element-map tree 'table 'identity info 'first-match) info))))
2355 ;; 2. Ignore horizontal rules and special columns.
2356 (org-test-with-parsed-data "
2357 | / | < | > |
2358 | 1 | 2 | 3 |
2359 |---+---+---|
2360 | 4 | 5 | 6 |"
2361 (should
2362 (equal '(2 . 3)
2363 (org-export-table-dimensions
2364 (org-element-map tree 'table 'identity info 'first-match) info)))))
2366 (ert-deftest test-org-export/table-cell-address ()
2367 "Test `org-export-table-cell-address' specifications."
2368 ;; 1. Standard test: index is 0-based.
2369 (org-test-with-parsed-data "| a | b |"
2370 (should
2371 (equal '((0 . 0) (0 . 1))
2372 (org-element-map tree 'table-cell
2373 (lambda (cell) (org-export-table-cell-address cell info))
2374 info))))
2375 ;; 2. Special column isn't counted, nor are special rows.
2376 (org-test-with-parsed-data "
2377 | / | <> |
2378 | | c |"
2379 (should
2380 (equal '(0 . 0)
2381 (org-export-table-cell-address
2382 (car (last (org-element-map tree 'table-cell 'identity info)))
2383 info))))
2384 ;; 3. Tables rules do not count either.
2385 (org-test-with-parsed-data "
2386 | a |
2387 |---|
2388 | b |
2389 |---|
2390 | c |"
2391 (should
2392 (equal '(2 . 0)
2393 (org-export-table-cell-address
2394 (car (last (org-element-map tree 'table-cell 'identity info)))
2395 info))))
2396 ;; 4. Return nil for special cells.
2397 (org-test-with-parsed-data "| / | a |"
2398 (should-not
2399 (org-export-table-cell-address
2400 (org-element-map tree 'table-cell 'identity nil 'first-match)
2401 info))))
2403 (ert-deftest test-org-export/get-table-cell-at ()
2404 "Test `org-export-get-table-cell-at' specifications."
2405 ;; 1. Address ignores special columns, special rows and rules.
2406 (org-test-with-parsed-data "
2407 | / | <> |
2408 | | a |
2409 |---+----|
2410 | | b |"
2411 (should
2412 (equal '("b")
2413 (org-element-contents
2414 (org-export-get-table-cell-at
2415 '(1 . 0)
2416 (org-element-map tree 'table 'identity info 'first-match)
2417 info)))))
2418 ;; 2. Return value for a non-existent address is nil.
2419 (org-test-with-parsed-data "| a |"
2420 (should-not
2421 (org-export-get-table-cell-at
2422 '(2 . 2)
2423 (org-element-map tree 'table 'identity info 'first-match)
2424 info)))
2425 (org-test-with-parsed-data "| / |"
2426 (should-not
2427 (org-export-get-table-cell-at
2428 '(0 . 0)
2429 (org-element-map tree 'table 'identity info 'first-match)
2430 info))))
2432 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
2433 "Test `org-export-table-cell-starts-colgroup-p' specifications."
2434 ;; 1. A cell at a beginning of a row always starts a column group.
2435 (org-test-with-parsed-data "| a |"
2436 (should
2437 (org-export-table-cell-starts-colgroup-p
2438 (org-element-map tree 'table-cell 'identity info 'first-match)
2439 info)))
2440 ;; 2. Special column should be ignored when determining the
2441 ;; beginning of the row.
2442 (org-test-with-parsed-data "
2443 | / | |
2444 | | a |"
2445 (should
2446 (org-export-table-cell-starts-colgroup-p
2447 (org-element-map tree 'table-cell 'identity info 'first-match)
2448 info)))
2449 ;; 2. Explicit column groups.
2450 (org-test-with-parsed-data "
2451 | / | | < |
2452 | a | b | c |"
2453 (should
2454 (equal
2455 '(yes no yes)
2456 (org-element-map tree 'table-cell
2457 (lambda (cell)
2458 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
2459 info)))))
2461 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
2462 "Test `org-export-table-cell-ends-colgroup-p' specifications."
2463 ;; 1. A cell at the end of a row always ends a column group.
2464 (org-test-with-parsed-data "| a |"
2465 (should
2466 (org-export-table-cell-ends-colgroup-p
2467 (org-element-map tree 'table-cell 'identity info 'first-match)
2468 info)))
2469 ;; 2. Special column should be ignored when determining the
2470 ;; beginning of the row.
2471 (org-test-with-parsed-data "
2472 | / | |
2473 | | a |"
2474 (should
2475 (org-export-table-cell-ends-colgroup-p
2476 (org-element-map tree 'table-cell 'identity info 'first-match)
2477 info)))
2478 ;; 3. Explicit column groups.
2479 (org-test-with-parsed-data "
2480 | / | < | |
2481 | a | b | c |"
2482 (should
2483 (equal
2484 '(yes no yes)
2485 (org-element-map tree 'table-cell
2486 (lambda (cell)
2487 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
2488 info)))))
2490 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
2491 "Test `org-export-table-row-starts-rowgroup-p' specifications."
2492 ;; 1. A row at the beginning of a table always starts a row group.
2493 ;; So does a row following a table rule.
2494 (org-test-with-parsed-data "
2495 | a |
2496 |---|
2497 | b |"
2498 (should
2499 (equal
2500 '(yes no yes)
2501 (org-element-map tree 'table-row
2502 (lambda (row)
2503 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2504 info))))
2505 ;; 2. Special rows should be ignored when determining the beginning
2506 ;; of the row.
2507 (org-test-with-parsed-data "
2508 | / | < |
2509 | | a |
2510 |---+---|
2511 | / | < |
2512 | | b |"
2513 (should
2514 (equal
2515 '(yes no yes)
2516 (org-element-map tree 'table-row
2517 (lambda (row)
2518 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2519 info)))))
2521 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
2522 "Test `org-export-table-row-ends-rowgroup-p' specifications."
2523 ;; 1. A row at the end of a table always ends a row group. So does
2524 ;; a row preceding a table rule.
2525 (org-test-with-parsed-data "
2526 | a |
2527 |---|
2528 | b |"
2529 (should
2530 (equal
2531 '(yes no yes)
2532 (org-element-map tree 'table-row
2533 (lambda (row)
2534 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2535 info))))
2536 ;; 2. Special rows should be ignored when determining the beginning
2537 ;; of the row.
2538 (org-test-with-parsed-data "
2539 | | a |
2540 | / | < |
2541 |---+---|
2542 | | b |
2543 | / | < |"
2544 (should
2545 (equal
2546 '(yes no yes)
2547 (org-element-map tree 'table-row
2548 (lambda (row)
2549 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2550 info)))))
2552 (ert-deftest test-org-export/table-row-starts-header-p ()
2553 "Test `org-export-table-row-starts-header-p' specifications."
2554 ;; 1. Only the row starting the first row group starts the table
2555 ;; header.
2556 (org-test-with-parsed-data "
2557 | a |
2558 | b |
2559 |---|
2560 | c |"
2561 (should
2562 (equal
2563 '(yes no no no)
2564 (org-element-map tree 'table-row
2565 (lambda (row)
2566 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
2567 info))))
2568 ;; 2. A row cannot start an header if there's no header in the
2569 ;; table.
2570 (org-test-with-parsed-data "
2571 | a |
2572 |---|"
2573 (should-not
2574 (org-export-table-row-starts-header-p
2575 (org-element-map tree 'table-row 'identity info 'first-match)
2576 info))))
2578 (ert-deftest test-org-export/table-row-ends-header-p ()
2579 "Test `org-export-table-row-ends-header-p' specifications."
2580 ;; 1. Only the row starting the first row group starts the table
2581 ;; header.
2582 (org-test-with-parsed-data "
2583 | a |
2584 | b |
2585 |---|
2586 | c |"
2587 (should
2588 (equal
2589 '(no yes no no)
2590 (org-element-map tree 'table-row
2591 (lambda (row)
2592 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
2593 info))))
2594 ;; 2. A row cannot start an header if there's no header in the
2595 ;; table.
2596 (org-test-with-parsed-data "
2597 | a |
2598 |---|"
2599 (should-not
2600 (org-export-table-row-ends-header-p
2601 (org-element-map tree 'table-row 'identity info 'first-match)
2602 info))))
2606 ;;; Tables of Contents
2608 (ert-deftest test-org-export/collect-headlines ()
2609 "Test `org-export-collect-headlines' specifications."
2610 ;; Standard test.
2611 (should
2612 (= 2
2613 (length
2614 (org-test-with-parsed-data "* H1\n** H2"
2615 (org-export-collect-headlines info)))))
2616 ;; Do not collect headlines below optional argument.
2617 (should
2618 (= 1
2619 (length
2620 (org-test-with-parsed-data "* H1\n** H2"
2621 (org-export-collect-headlines info 1)))))
2622 ;; Never collect headlines below maximum headline level.
2623 (should
2624 (= 1
2625 (length
2626 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
2627 (org-export-collect-headlines info)))))
2628 (should
2629 (= 1
2630 (length
2631 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
2632 (org-export-collect-headlines info 2))))))
2636 ;;; Templates
2638 (ert-deftest test-org-export/inner-template ()
2639 "Test `inner-template' translator specifications."
2640 (should
2641 (equal "Success!"
2642 (org-test-with-temp-text "* Headline"
2643 (org-export-as
2644 (org-export-create-backend
2645 :transcoders
2646 '((inner-template . (lambda (contents info) "Success!"))
2647 (headline . (lambda (h c i) "Headline"))))))))
2648 ;; Inner template is applied even in a "body-only" export.
2649 (should
2650 (equal "Success!"
2651 (org-test-with-temp-text "* Headline"
2652 (org-export-as
2653 (org-export-create-backend
2654 :transcoders '((inner-template . (lambda (c i) "Success!"))
2655 (headline . (lambda (h c i) "Headline"))))
2656 nil nil 'body-only)))))
2658 (ert-deftest test-org-export/template ()
2659 "Test `template' translator specifications."
2660 (should
2661 (equal "Success!"
2662 (org-test-with-temp-text "* Headline"
2663 (org-export-as
2664 (org-export-create-backend
2665 :transcoders '((template . (lambda (contents info) "Success!"))
2666 (headline . (lambda (h c i) "Headline"))))))))
2667 ;; Template is not applied in a "body-only" export.
2668 (should-not
2669 (equal "Success!"
2670 (org-test-with-temp-text "* Headline"
2671 (org-export-as
2672 (org-export-create-backend
2673 :transcoders '((template . (lambda (contents info) "Success!"))
2674 (headline . (lambda (h c i) "Headline"))))
2675 nil nil 'body-only)))))
2679 ;;; Topology
2681 (ert-deftest test-org-export/get-next-element ()
2682 "Test `org-export-get-next-element' specifications."
2683 ;; Standard test.
2684 (should
2685 (equal "b"
2686 (org-test-with-parsed-data "* Headline\n*a* b"
2687 (org-export-get-next-element
2688 (org-element-map tree 'bold 'identity info t) info))))
2689 ;; Return nil when no previous element.
2690 (should-not
2691 (org-test-with-parsed-data "* Headline\na *b*"
2692 (org-export-get-next-element
2693 (org-element-map tree 'bold 'identity info t) info)))
2694 ;; Non-exportable elements are ignored.
2695 (should-not
2696 (let ((org-export-with-timestamps nil))
2697 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
2698 (org-export-get-next-element
2699 (org-element-map tree 'entity 'identity info t) info))))
2700 ;; Find next element in secondary strings.
2701 (should
2702 (eq 'verbatim
2703 (org-test-with-parsed-data "* a =verb="
2704 (org-element-type
2705 (org-export-get-next-element
2706 (org-element-map tree 'plain-text 'identity info t) info)))))
2707 ;; Find next element in document keywords.
2708 (should
2709 (eq 'verbatim
2710 (org-test-with-parsed-data "#+TITLE: a =verb="
2711 (org-element-type
2712 (org-export-get-next-element
2713 (org-element-map
2714 (plist-get info :title) 'plain-text 'identity info t) info)))))
2715 ;; Find next element in parsed affiliated keywords.
2716 (should
2717 (eq 'verbatim
2718 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
2719 (org-element-type
2720 (org-export-get-next-element
2721 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2722 ;; With optional argument N, return a list containing all the
2723 ;; following elements.
2724 (should
2725 (equal
2726 '(bold code underline)
2727 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2728 (mapcar 'car
2729 (org-export-get-next-element
2730 (org-element-map tree 'italic 'identity info t) info t)))))
2731 ;; When N is a positive integer, return a list containing up to
2732 ;; N following elements.
2733 (should
2734 (equal
2735 '(bold code)
2736 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2737 (mapcar 'car
2738 (org-export-get-next-element
2739 (org-element-map tree 'italic 'identity info t) info 2))))))
2741 (ert-deftest test-org-export/get-previous-element ()
2742 "Test `org-export-get-previous-element' specifications."
2743 ;; Standard test.
2744 (should
2745 (equal "a "
2746 (org-test-with-parsed-data "* Headline\na *b*"
2747 (org-export-get-previous-element
2748 (org-element-map tree 'bold 'identity info t) info))))
2749 ;; Return nil when no previous element.
2750 (should-not
2751 (org-test-with-parsed-data "* Headline\n*a* b"
2752 (org-export-get-previous-element
2753 (org-element-map tree 'bold 'identity info t) info)))
2754 ;; Non-exportable elements are ignored.
2755 (should-not
2756 (let ((org-export-with-timestamps nil))
2757 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
2758 (org-export-get-previous-element
2759 (org-element-map tree 'entity 'identity info t) info))))
2760 ;; Find previous element in secondary strings.
2761 (should
2762 (eq 'verbatim
2763 (org-test-with-parsed-data "* =verb= a"
2764 (org-element-type
2765 (org-export-get-previous-element
2766 (org-element-map tree 'plain-text 'identity info t) info)))))
2767 ;; Find previous element in document keywords.
2768 (should
2769 (eq 'verbatim
2770 (org-test-with-parsed-data "#+TITLE: =verb= a"
2771 (org-element-type
2772 (org-export-get-previous-element
2773 (org-element-map
2774 (plist-get info :title) 'plain-text 'identity info t) info)))))
2775 ;; Find previous element in parsed affiliated keywords.
2776 (should
2777 (eq 'verbatim
2778 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
2779 (org-element-type
2780 (org-export-get-previous-element
2781 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2782 ;; With optional argument N, return a list containing up to
2783 ;; N previous elements.
2784 (should
2785 (equal '(underline italic bold)
2786 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
2787 (mapcar 'car
2788 (org-export-get-previous-element
2789 (org-element-map tree 'code 'identity info t) info t)))))
2790 ;; When N is a positive integer, return a list containing up to
2791 ;; N previous elements.
2792 (should
2793 (equal '(italic bold)
2794 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
2795 (mapcar 'car
2796 (org-export-get-previous-element
2797 (org-element-map tree 'code 'identity info t) info 2))))))
2800 (provide 'test-ox)
2801 ;;; test-org-export.el end here