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