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