ox-latex: Remove unused function
[org-mode.git] / testing / lisp / test-ox.el
blobcbae08a823776e2caebde9787e86c7bbf2e45a11
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)))
1646 ;; Match fuzzy link even when before first headline.
1647 (should
1648 (eq 'headline
1649 (org-test-with-parsed-data "[[hl]]\n* hl"
1650 (org-element-type
1651 (org-export-resolve-fuzzy-link
1652 (org-element-map tree 'link 'identity info t) info))))))
1654 (ert-deftest test-org-export/resolve-id-link ()
1655 "Test `org-export-resolve-id-link' specifications."
1656 ;; 1. Regular test for custom-id link.
1657 (org-test-with-parsed-data "* Headline1
1658 :PROPERTIES:
1659 :CUSTOM_ID: test
1660 :END:
1661 * Headline 2
1662 \[[#test]]"
1663 (should
1664 (org-export-resolve-id-link
1665 (org-element-map tree 'link 'identity info t) info)))
1666 ;; 2. Failing test for custom-id link.
1667 (org-test-with-parsed-data "* Headline1
1668 :PROPERTIES:
1669 :CUSTOM_ID: test
1670 :END:
1671 * Headline 2
1672 \[[#no-match]]"
1673 (should-not
1674 (org-export-resolve-id-link
1675 (org-element-map tree 'link 'identity info t) info)))
1676 ;; 3. Test for internal id target.
1677 (org-test-with-parsed-data "* Headline1
1678 :PROPERTIES:
1679 :ID: aaaa
1680 :END:
1681 * Headline 2
1682 \[[id:aaaa]]"
1683 (should
1684 (org-export-resolve-id-link
1685 (org-element-map tree 'link 'identity info t) info)))
1686 ;; 4. Test for external id target.
1687 (org-test-with-parsed-data "[[id:aaaa]]"
1688 (should
1689 (org-export-resolve-id-link
1690 (org-element-map tree 'link 'identity info t)
1691 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
1693 (ert-deftest test-org-export/resolve-radio-link ()
1694 "Test `org-export-resolve-radio-link' specifications."
1695 ;; Standard test.
1696 (should
1697 (org-test-with-temp-text "<<<radio>>> radio"
1698 (org-update-radio-target-regexp)
1699 (let* ((tree (org-element-parse-buffer))
1700 (info `(:parse-tree ,tree)))
1701 (org-export-resolve-radio-link
1702 (org-element-map tree 'link 'identity info t)
1703 info))))
1704 ;; Radio targets are case-insensitive.
1705 (should
1706 (org-test-with-temp-text "<<<RADIO>>> radio"
1707 (org-update-radio-target-regexp)
1708 (let* ((tree (org-element-parse-buffer))
1709 (info `(:parse-tree ,tree)))
1710 (org-export-resolve-radio-link
1711 (org-element-map tree 'link 'identity info t)
1712 info))))
1713 ;; Radio target with objects.
1714 (should
1715 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
1716 (org-update-radio-target-regexp)
1717 (let* ((tree (org-element-parse-buffer))
1718 (info `(:parse-tree ,tree)))
1719 (org-export-resolve-radio-link
1720 (org-element-map tree 'link 'identity info t)
1721 info))))
1722 ;; Multiple radio targets.
1723 (should
1724 (equal '("radio1" "radio2")
1725 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
1726 (org-update-radio-target-regexp)
1727 (let* ((tree (org-element-parse-buffer))
1728 (info `(:parse-tree ,tree)))
1729 (org-element-map tree 'link
1730 (lambda (link)
1731 (org-element-property
1732 :value (org-export-resolve-radio-link link info)))
1733 info)))))
1734 ;; Radio target is whitespace insensitive.
1735 (should
1736 (org-test-with-temp-text "<<<a radio>>> a\n radio"
1737 (org-update-radio-target-regexp)
1738 (let* ((tree (org-element-parse-buffer))
1739 (info `(:parse-tree ,tree)))
1740 (org-element-map tree 'link
1741 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
1745 ;;; Src-block and example-block
1747 (ert-deftest test-org-export/unravel-code ()
1748 "Test `org-export-unravel-code' function."
1749 ;; Code without reference.
1750 (should
1751 (equal '("(+ 1 1)\n")
1752 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
1753 (org-export-unravel-code (org-element-at-point)))))
1754 ;; Code with reference.
1755 (should
1756 (equal '("(+ 1 1)\n" (1 . "test"))
1757 (org-test-with-temp-text
1758 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
1759 (let ((org-coderef-label-format "(ref:%s)"))
1760 (org-export-unravel-code (org-element-at-point))))))
1761 ;; Code with user-defined reference.
1762 (should
1763 (equal
1764 '("(+ 1 1)\n" (1 . "test"))
1765 (org-test-with-temp-text
1766 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
1767 (let ((org-coderef-label-format "(ref:%s)"))
1768 (org-export-unravel-code (org-element-at-point))))))
1769 ;; Code references keys are relative to the current block.
1770 (should
1771 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
1772 (org-test-with-temp-text "
1773 #+BEGIN_EXAMPLE -n
1774 \(+ 1 1)
1775 #+END_EXAMPLE
1776 #+BEGIN_EXAMPLE +n
1777 \(+ 2 2)
1778 \(+ 3 3) (ref:one)
1779 #+END_EXAMPLE"
1780 (goto-line 5)
1781 (let ((org-coderef-label-format "(ref:%s)"))
1782 (org-export-unravel-code (org-element-at-point)))))))
1784 (ert-deftest test-org-export/format-code-default ()
1785 "Test `org-export-format-code-default' specifications."
1786 ;; Return the empty string when code is empty.
1787 (should
1788 (equal ""
1789 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
1790 (org-export-format-code-default
1791 (org-element-map tree 'src-block 'identity info t) info))))
1792 ;; Number lines, two whitespace characters before the actual loc.
1793 (should
1794 (equal "1 a\n2 b\n"
1795 (org-test-with-parsed-data
1796 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
1797 (org-export-format-code-default
1798 (org-element-map tree 'src-block 'identity info t) info))))
1799 ;; Put references 6 whitespace characters after the widest line,
1800 ;; wrapped within parenthesis.
1801 (should
1802 (equal "123 (a)\n1 (b)\n"
1803 (let ((org-coderef-label-format "(ref:%s)"))
1804 (org-test-with-parsed-data
1805 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
1806 (org-export-format-code-default
1807 (org-element-map tree 'src-block 'identity info t) info))))))
1811 ;;; Smart Quotes
1813 (ert-deftest test-org-export/activate-smart-quotes ()
1814 "Test `org-export-activate-smart-quotes' specifications."
1815 ;; Opening double quotes: standard test.
1816 (should
1817 (equal
1818 '("some &ldquo;paragraph")
1819 (let ((org-export-default-language "en"))
1820 (org-test-with-parsed-data "some \"paragraph"
1821 (org-element-map tree 'plain-text
1822 (lambda (s) (org-export-activate-smart-quotes s :html info))
1823 info)))))
1824 ;; Opening quotes: at the beginning of a paragraph.
1825 (should
1826 (equal
1827 '("&ldquo;begin")
1828 (let ((org-export-default-language "en"))
1829 (org-test-with-parsed-data "\"begin"
1830 (org-element-map tree 'plain-text
1831 (lambda (s) (org-export-activate-smart-quotes s :html info))
1832 info)))))
1833 ;; Opening quotes: after an object.
1834 (should
1835 (equal
1836 '("&ldquo;begin")
1837 (let ((org-export-default-language "en"))
1838 (org-test-with-parsed-data "=verb= \"begin"
1839 (org-element-map tree 'plain-text
1840 (lambda (s) (org-export-activate-smart-quotes s :html info))
1841 info)))))
1842 ;; Closing quotes: standard test.
1843 (should
1844 (equal
1845 '("some&rdquo; paragraph")
1846 (let ((org-export-default-language "en"))
1847 (org-test-with-parsed-data "some\" paragraph"
1848 (org-element-map tree 'plain-text
1849 (lambda (s) (org-export-activate-smart-quotes s :html info))
1850 info)))))
1851 ;; Closing quotes: at the end of a paragraph.
1852 (should
1853 (equal
1854 '("end&rdquo;")
1855 (let ((org-export-default-language "en"))
1856 (org-test-with-parsed-data "end\""
1857 (org-element-map tree 'plain-text
1858 (lambda (s) (org-export-activate-smart-quotes s :html info))
1859 info)))))
1860 ;; Apostrophe: standard test.
1861 (should
1862 (equal
1863 '("It shouldn&rsquo;t fail")
1864 (let ((org-export-default-language "en"))
1865 (org-test-with-parsed-data "It shouldn't fail"
1866 (org-element-map tree 'plain-text
1867 (lambda (s) (org-export-activate-smart-quotes s :html info))
1868 info)))))
1869 ;; Apostrophe: before an object.
1870 (should
1871 (equal
1872 '("a&rsquo;")
1873 (let ((org-export-default-language "en"))
1874 (org-test-with-parsed-data "a'=b="
1875 (org-element-map tree 'plain-text
1876 (lambda (s) (org-export-activate-smart-quotes s :html info))
1877 info)))))
1878 ;; Apostrophe: after an object.
1879 (should
1880 (equal
1881 '("&rsquo;s")
1882 (let ((org-export-default-language "en"))
1883 (org-test-with-parsed-data "=code='s"
1884 (org-element-map tree 'plain-text
1885 (lambda (s) (org-export-activate-smart-quotes s :html info))
1886 info)))))
1887 ;; Special case: isolated quotes.
1888 (should
1889 (equal '("&ldquo;" "&rdquo;")
1890 (let ((org-export-default-language "en"))
1891 (org-test-with-parsed-data "\"$x$\""
1892 (org-element-map tree 'plain-text
1893 (lambda (s) (org-export-activate-smart-quotes s :html info))
1894 info)))))
1895 ;; Smart quotes in secondary strings.
1896 (should
1897 (equal '("&ldquo;" "&rdquo;")
1898 (let ((org-export-default-language "en"))
1899 (org-test-with-parsed-data "* \"$x$\""
1900 (org-element-map tree 'plain-text
1901 (lambda (s) (org-export-activate-smart-quotes s :html info))
1902 info)))))
1903 ;; Smart quotes in document keywords.
1904 (should
1905 (equal '("&ldquo;" "&rdquo;")
1906 (let ((org-export-default-language "en"))
1907 (org-test-with-parsed-data "#+TITLE: \"$x$\""
1908 (org-element-map (plist-get info :title) 'plain-text
1909 (lambda (s) (org-export-activate-smart-quotes s :html info))
1910 info)))))
1911 ;; Smart quotes in parsed affiliated keywords.
1912 (should
1913 (equal '("&ldquo;" "&rdquo;" "Paragraph")
1914 (let ((org-export-default-language "en"))
1915 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
1916 (org-element-map tree 'plain-text
1917 (lambda (s) (org-export-activate-smart-quotes s :html info))
1918 info nil nil t))))))
1922 ;;; Tables
1924 (ert-deftest test-org-export/special-column ()
1925 "Test if the table's special column is properly recognized."
1926 ;; 1. First column is special if it contains only a special marking
1927 ;; characters or empty cells.
1928 (org-test-with-temp-text "
1929 | ! | 1 |
1930 | | 2 |"
1931 (should
1932 (org-export-table-has-special-column-p
1933 (org-element-map
1934 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1935 ;; 2. If the column contains anything else, it isn't special.
1936 (org-test-with-temp-text "
1937 | ! | 1 |
1938 | b | 2 |"
1939 (should-not
1940 (org-export-table-has-special-column-p
1941 (org-element-map
1942 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1943 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
1944 ;; and "!".
1945 (org-test-with-temp-text "
1946 | # | 1 |
1947 | ^ | 2 |
1948 | * | 3 |
1949 | _ | 4 |
1950 | / | 5 |
1951 | $ | 6 |
1952 | ! | 7 |"
1953 (should
1954 (org-export-table-has-special-column-p
1955 (org-element-map
1956 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1957 ;; 4. A first column with only empty cells isn't considered as
1958 ;; special.
1959 (org-test-with-temp-text "
1960 | | 1 |
1961 | | 2 |"
1962 (should-not
1963 (org-export-table-has-special-column-p
1964 (org-element-map
1965 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
1967 (ert-deftest test-org-export/table-row-is-special-p ()
1968 "Test `org-export-table-row-is-special-p' specifications."
1969 ;; 1. A row is special if it has a special marking character in the
1970 ;; special column.
1971 (org-test-with-parsed-data "| ! | 1 |"
1972 (should
1973 (org-export-table-row-is-special-p
1974 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1975 ;; 2. A row is special when its first field is "/"
1976 (org-test-with-parsed-data "
1977 | / | 1 |
1978 | a | b |"
1979 (should
1980 (org-export-table-row-is-special-p
1981 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1982 ;; 3. A row only containing alignment cookies is also considered as
1983 ;; special.
1984 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
1985 (should
1986 (org-export-table-row-is-special-p
1987 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1988 ;; 4. Everything else isn't considered as special.
1989 (org-test-with-parsed-data "| \alpha | | c |"
1990 (should-not
1991 (org-export-table-row-is-special-p
1992 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1993 ;; 5. Table's rules are never considered as special rows.
1994 (org-test-with-parsed-data "|---+---|"
1995 (should-not
1996 (org-export-table-row-is-special-p
1997 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
1999 (ert-deftest test-org-export/has-header-p ()
2000 "Test `org-export-table-has-header-p' specifications."
2001 ;; 1. With an header.
2002 (org-test-with-parsed-data "
2003 | a | b |
2004 |---+---|
2005 | c | d |"
2006 (should
2007 (org-export-table-has-header-p
2008 (org-element-map tree 'table 'identity info 'first-match)
2009 info)))
2010 ;; 2. Without an header.
2011 (org-test-with-parsed-data "
2012 | a | b |
2013 | c | d |"
2014 (should-not
2015 (org-export-table-has-header-p
2016 (org-element-map tree 'table 'identity info 'first-match)
2017 info)))
2018 ;; 3. Don't get fooled with starting and ending rules.
2019 (org-test-with-parsed-data "
2020 |---+---|
2021 | a | b |
2022 | c | d |
2023 |---+---|"
2024 (should-not
2025 (org-export-table-has-header-p
2026 (org-element-map tree 'table 'identity info 'first-match)
2027 info))))
2029 (ert-deftest test-org-export/table-row-group ()
2030 "Test `org-export-table-row-group' specifications."
2031 ;; 1. A rule creates a new group.
2032 (should
2033 (equal '(1 rule 2)
2034 (org-test-with-parsed-data "
2035 | a | b |
2036 |---+---|
2037 | 1 | 2 |"
2038 (org-element-map tree 'table-row
2039 (lambda (row)
2040 (if (eq (org-element-property :type row) 'rule) 'rule
2041 (org-export-table-row-group row info)))))))
2042 ;; 2. Special rows are ignored in count.
2043 (should
2044 (equal
2045 '(rule 1)
2046 (org-test-with-parsed-data "
2047 | / | < | > |
2048 |---|---+---|
2049 | | 1 | 2 |"
2050 (org-element-map tree 'table-row
2051 (lambda (row)
2052 (if (eq (org-element-property :type row) 'rule) 'rule
2053 (org-export-table-row-group row info)))
2054 info))))
2055 ;; 3. Double rules also are ignored in count.
2056 (should
2057 (equal '(1 rule rule 2)
2058 (org-test-with-parsed-data "
2059 | a | b |
2060 |---+---|
2061 |---+---|
2062 | 1 | 2 |"
2063 (org-element-map tree 'table-row
2064 (lambda (row)
2065 (if (eq (org-element-property :type row) 'rule) 'rule
2066 (org-export-table-row-group row info))))))))
2068 (ert-deftest test-org-export/table-row-number ()
2069 "Test `org-export-table-row-number' specifications."
2070 ;; Standard test. Number is 0-indexed.
2071 (should
2072 (equal '(0 1)
2073 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
2074 (org-element-map tree 'table-row
2075 (lambda (row) (org-export-table-row-number row info)) info))))
2076 ;; Number ignores separators.
2077 (should
2078 (equal '(0 1)
2079 (org-test-with-parsed-data "
2080 | a | b | c |
2081 |---+---+---|
2082 | d | e | f |"
2083 (org-element-map tree 'table-row
2084 (lambda (row) (org-export-table-row-number row info)) info))))
2085 ;; Number ignores special rows.
2086 (should
2087 (equal '(0 1)
2088 (org-test-with-parsed-data "
2089 | / | < | > |
2090 | | b | c |
2091 |---+-----+-----|
2092 | | <c> | <c> |
2093 | | e | f |"
2094 (org-element-map tree 'table-row
2095 (lambda (row) (org-export-table-row-number row info)) info)))))
2097 (ert-deftest test-org-export/table-cell-width ()
2098 "Test `org-export-table-cell-width' specifications."
2099 ;; 1. Width is primarily determined by width cookies. If no cookie
2100 ;; is found, cell's width is nil.
2101 (org-test-with-parsed-data "
2102 | / | <l> | <6> | <l7> |
2103 | | a | b | c |"
2104 (should
2105 (equal
2106 '(nil 6 7)
2107 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2108 (org-element-map tree 'table-cell 'identity info)))))
2109 ;; 2. The last width cookie has precedence.
2110 (org-test-with-parsed-data "
2111 | <6> |
2112 | <7> |
2113 | a |"
2114 (should
2115 (equal
2116 '(7)
2117 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2118 (org-element-map tree 'table-cell 'identity info)))))
2119 ;; 3. Valid width cookies must have a specific row.
2120 (org-test-with-parsed-data "| <6> | cell |"
2121 (should
2122 (equal
2123 '(nil nil)
2124 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2125 (org-element-map tree 'table-cell 'identity))))))
2127 (ert-deftest test-org-export/table-cell-alignment ()
2128 "Test `org-export-table-cell-alignment' specifications."
2129 (let ((org-table-number-fraction 0.5)
2130 (org-table-number-regexp "^[0-9]+$"))
2131 ;; 1. Alignment is primarily determined by alignment cookies.
2132 (org-test-with-temp-text "| <l> | <c> | <r> |"
2133 (let* ((tree (org-element-parse-buffer))
2134 (info `(:parse-tree ,tree)))
2135 (should
2136 (equal
2137 '(left center right)
2138 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
2139 (org-element-map tree 'table-cell 'identity))))))
2140 ;; 2. The last alignment cookie has precedence.
2141 (org-test-with-parsed-data "
2142 | <l8> |
2143 | cell |
2144 | <r9> |"
2145 (should
2146 (equal
2147 '(right right right)
2148 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
2149 (org-element-map tree 'table-cell 'identity)))))
2150 ;; 3. If there's no cookie, cell's contents determine alignment.
2151 ;; A column mostly made of cells containing numbers will align
2152 ;; its cells to the right.
2153 (org-test-with-parsed-data "
2154 | 123 |
2155 | some text |
2156 | 12345 |"
2157 (should
2158 (equal
2159 '(right right right)
2160 (mapcar (lambda (cell)
2161 (org-export-table-cell-alignment cell info))
2162 (org-element-map tree 'table-cell 'identity)))))
2163 ;; 4. Otherwise, they will be aligned to the left.
2164 (org-test-with-parsed-data "
2165 | text |
2166 | some text |
2167 | \alpha |"
2168 (should
2169 (equal
2170 '(left left left)
2171 (mapcar (lambda (cell)
2172 (org-export-table-cell-alignment cell info))
2173 (org-element-map tree 'table-cell 'identity)))))))
2175 (ert-deftest test-org-export/table-cell-borders ()
2176 "Test `org-export-table-cell-borders' specifications."
2177 ;; 1. Recognize various column groups indicators.
2178 (org-test-with-parsed-data "| / | < | > | <> |"
2179 (should
2180 (equal
2181 '((right bottom top) (left bottom top) (right bottom top)
2182 (right left bottom top))
2183 (mapcar (lambda (cell)
2184 (org-export-table-cell-borders cell info))
2185 (org-element-map tree 'table-cell 'identity)))))
2186 ;; 2. Accept shortcuts to define column groups.
2187 (org-test-with-parsed-data "| / | < | < |"
2188 (should
2189 (equal
2190 '((right bottom top) (right left bottom top) (left bottom top))
2191 (mapcar (lambda (cell)
2192 (org-export-table-cell-borders cell info))
2193 (org-element-map tree 'table-cell 'identity)))))
2194 ;; 3. A valid column groups row must start with a "/".
2195 (org-test-with-parsed-data "
2196 | | < |
2197 | a | b |"
2198 (should
2199 (equal '((top) (top) (bottom) (bottom))
2200 (mapcar (lambda (cell)
2201 (org-export-table-cell-borders cell info))
2202 (org-element-map tree 'table-cell 'identity)))))
2203 ;; 4. Take table rules into consideration.
2204 (org-test-with-parsed-data "
2205 | 1 |
2206 |---|
2207 | 2 |"
2208 (should
2209 (equal '((below top) (bottom above))
2210 (mapcar (lambda (cell)
2211 (org-export-table-cell-borders cell info))
2212 (org-element-map tree 'table-cell 'identity)))))
2213 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
2214 ;; (resp. `bottom' and `below') borders. Any special row is
2215 ;; ignored.
2216 (org-test-with-parsed-data "
2217 |---+----|
2218 | / | |
2219 | | 1 |
2220 |---+----|"
2221 (should
2222 (equal '((bottom below top above))
2223 (last
2224 (mapcar (lambda (cell)
2225 (org-export-table-cell-borders cell info))
2226 (org-element-map tree 'table-cell 'identity)))))))
2228 (ert-deftest test-org-export/table-dimensions ()
2229 "Test `org-export-table-dimensions' specifications."
2230 ;; 1. Standard test.
2231 (org-test-with-parsed-data "
2232 | 1 | 2 | 3 |
2233 | 4 | 5 | 6 |"
2234 (should
2235 (equal '(2 . 3)
2236 (org-export-table-dimensions
2237 (org-element-map tree 'table 'identity info 'first-match) info))))
2238 ;; 2. Ignore horizontal rules and special columns.
2239 (org-test-with-parsed-data "
2240 | / | < | > |
2241 | 1 | 2 | 3 |
2242 |---+---+---|
2243 | 4 | 5 | 6 |"
2244 (should
2245 (equal '(2 . 3)
2246 (org-export-table-dimensions
2247 (org-element-map tree 'table 'identity info 'first-match) info)))))
2249 (ert-deftest test-org-export/table-cell-address ()
2250 "Test `org-export-table-cell-address' specifications."
2251 ;; 1. Standard test: index is 0-based.
2252 (org-test-with-parsed-data "| a | b |"
2253 (should
2254 (equal '((0 . 0) (0 . 1))
2255 (org-element-map tree 'table-cell
2256 (lambda (cell) (org-export-table-cell-address cell info))
2257 info))))
2258 ;; 2. Special column isn't counted, nor are special rows.
2259 (org-test-with-parsed-data "
2260 | / | <> |
2261 | | c |"
2262 (should
2263 (equal '(0 . 0)
2264 (org-export-table-cell-address
2265 (car (last (org-element-map tree 'table-cell 'identity info)))
2266 info))))
2267 ;; 3. Tables rules do not count either.
2268 (org-test-with-parsed-data "
2269 | a |
2270 |---|
2271 | b |
2272 |---|
2273 | c |"
2274 (should
2275 (equal '(2 . 0)
2276 (org-export-table-cell-address
2277 (car (last (org-element-map tree 'table-cell 'identity info)))
2278 info))))
2279 ;; 4. Return nil for special cells.
2280 (org-test-with-parsed-data "| / | a |"
2281 (should-not
2282 (org-export-table-cell-address
2283 (org-element-map tree 'table-cell 'identity nil 'first-match)
2284 info))))
2286 (ert-deftest test-org-export/get-table-cell-at ()
2287 "Test `org-export-get-table-cell-at' specifications."
2288 ;; 1. Address ignores special columns, special rows and rules.
2289 (org-test-with-parsed-data "
2290 | / | <> |
2291 | | a |
2292 |---+----|
2293 | | b |"
2294 (should
2295 (equal '("b")
2296 (org-element-contents
2297 (org-export-get-table-cell-at
2298 '(1 . 0)
2299 (org-element-map tree 'table 'identity info 'first-match)
2300 info)))))
2301 ;; 2. Return value for a non-existent address is nil.
2302 (org-test-with-parsed-data "| a |"
2303 (should-not
2304 (org-export-get-table-cell-at
2305 '(2 . 2)
2306 (org-element-map tree 'table 'identity info 'first-match)
2307 info)))
2308 (org-test-with-parsed-data "| / |"
2309 (should-not
2310 (org-export-get-table-cell-at
2311 '(0 . 0)
2312 (org-element-map tree 'table 'identity info 'first-match)
2313 info))))
2315 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
2316 "Test `org-export-table-cell-starts-colgroup-p' specifications."
2317 ;; 1. A cell at a beginning of a row always starts a column group.
2318 (org-test-with-parsed-data "| a |"
2319 (should
2320 (org-export-table-cell-starts-colgroup-p
2321 (org-element-map tree 'table-cell 'identity info 'first-match)
2322 info)))
2323 ;; 2. Special column should be ignored when determining the
2324 ;; beginning of the row.
2325 (org-test-with-parsed-data "
2326 | / | |
2327 | | a |"
2328 (should
2329 (org-export-table-cell-starts-colgroup-p
2330 (org-element-map tree 'table-cell 'identity info 'first-match)
2331 info)))
2332 ;; 2. Explicit column groups.
2333 (org-test-with-parsed-data "
2334 | / | | < |
2335 | a | b | c |"
2336 (should
2337 (equal
2338 '(yes no yes)
2339 (org-element-map tree 'table-cell
2340 (lambda (cell)
2341 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
2342 info)))))
2344 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
2345 "Test `org-export-table-cell-ends-colgroup-p' specifications."
2346 ;; 1. A cell at the end of a row always ends a column group.
2347 (org-test-with-parsed-data "| a |"
2348 (should
2349 (org-export-table-cell-ends-colgroup-p
2350 (org-element-map tree 'table-cell 'identity info 'first-match)
2351 info)))
2352 ;; 2. Special column should be ignored when determining the
2353 ;; beginning of the row.
2354 (org-test-with-parsed-data "
2355 | / | |
2356 | | a |"
2357 (should
2358 (org-export-table-cell-ends-colgroup-p
2359 (org-element-map tree 'table-cell 'identity info 'first-match)
2360 info)))
2361 ;; 3. Explicit column groups.
2362 (org-test-with-parsed-data "
2363 | / | < | |
2364 | a | b | c |"
2365 (should
2366 (equal
2367 '(yes no yes)
2368 (org-element-map tree 'table-cell
2369 (lambda (cell)
2370 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
2371 info)))))
2373 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
2374 "Test `org-export-table-row-starts-rowgroup-p' specifications."
2375 ;; 1. A row at the beginning of a table always starts a row group.
2376 ;; So does a row following a table rule.
2377 (org-test-with-parsed-data "
2378 | a |
2379 |---|
2380 | b |"
2381 (should
2382 (equal
2383 '(yes no yes)
2384 (org-element-map tree 'table-row
2385 (lambda (row)
2386 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2387 info))))
2388 ;; 2. Special rows should be ignored when determining the beginning
2389 ;; of the row.
2390 (org-test-with-parsed-data "
2391 | / | < |
2392 | | a |
2393 |---+---|
2394 | / | < |
2395 | | b |"
2396 (should
2397 (equal
2398 '(yes no yes)
2399 (org-element-map tree 'table-row
2400 (lambda (row)
2401 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2402 info)))))
2404 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
2405 "Test `org-export-table-row-ends-rowgroup-p' specifications."
2406 ;; 1. A row at the end of a table always ends a row group. So does
2407 ;; a row preceding a table rule.
2408 (org-test-with-parsed-data "
2409 | a |
2410 |---|
2411 | b |"
2412 (should
2413 (equal
2414 '(yes no yes)
2415 (org-element-map tree 'table-row
2416 (lambda (row)
2417 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2418 info))))
2419 ;; 2. Special rows should be ignored when determining the beginning
2420 ;; of the row.
2421 (org-test-with-parsed-data "
2422 | | a |
2423 | / | < |
2424 |---+---|
2425 | | b |
2426 | / | < |"
2427 (should
2428 (equal
2429 '(yes no yes)
2430 (org-element-map tree 'table-row
2431 (lambda (row)
2432 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2433 info)))))
2435 (ert-deftest test-org-export/table-row-starts-header-p ()
2436 "Test `org-export-table-row-starts-header-p' specifications."
2437 ;; 1. Only the row starting the first row group starts the table
2438 ;; header.
2439 (org-test-with-parsed-data "
2440 | a |
2441 | b |
2442 |---|
2443 | c |"
2444 (should
2445 (equal
2446 '(yes no no no)
2447 (org-element-map tree 'table-row
2448 (lambda (row)
2449 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
2450 info))))
2451 ;; 2. A row cannot start an header if there's no header in the
2452 ;; table.
2453 (org-test-with-parsed-data "
2454 | a |
2455 |---|"
2456 (should-not
2457 (org-export-table-row-starts-header-p
2458 (org-element-map tree 'table-row 'identity info 'first-match)
2459 info))))
2461 (ert-deftest test-org-export/table-row-ends-header-p ()
2462 "Test `org-export-table-row-ends-header-p' specifications."
2463 ;; 1. Only the row starting the first row group starts the table
2464 ;; header.
2465 (org-test-with-parsed-data "
2466 | a |
2467 | b |
2468 |---|
2469 | c |"
2470 (should
2471 (equal
2472 '(no yes no no)
2473 (org-element-map tree 'table-row
2474 (lambda (row)
2475 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
2476 info))))
2477 ;; 2. A row cannot start an header if there's no header in the
2478 ;; table.
2479 (org-test-with-parsed-data "
2480 | a |
2481 |---|"
2482 (should-not
2483 (org-export-table-row-ends-header-p
2484 (org-element-map tree 'table-row 'identity info 'first-match)
2485 info))))
2489 ;;; Templates
2491 (ert-deftest test-org-export/inner-template ()
2492 "Test `inner-template' translator specifications."
2493 (should
2494 (equal "Success!"
2495 (let (org-export-registered-backends)
2496 (org-export-define-backend 'test
2497 '((inner-template . (lambda (contents info) "Success!"))
2498 (headline . (lambda (h c i) "Headline"))))
2499 (org-test-with-temp-text "* Headline"
2500 (org-export-as 'test)))))
2501 ;; Inner template is applied even in a "body-only" export.
2502 (should
2503 (equal "Success!"
2504 (let (org-export-registered-backends)
2505 (org-export-define-backend 'test
2506 '((inner-template . (lambda (contents info) "Success!"))
2507 (headline . (lambda (h c i) "Headline"))))
2508 (org-test-with-temp-text "* Headline"
2509 (org-export-as 'test nil nil 'body-only))))))
2511 (ert-deftest test-org-export/template ()
2512 "Test `template' translator specifications."
2513 (should
2514 (equal "Success!"
2515 (let (org-export-registered-backends)
2516 (org-export-define-backend 'test
2517 '((template . (lambda (contents info) "Success!"))
2518 (headline . (lambda (h c i) "Headline"))))
2519 (org-test-with-temp-text "* Headline"
2520 (org-export-as 'test)))))
2521 ;; Template is not applied in a "body-only" export.
2522 (should-not
2523 (equal "Success!"
2524 (let (org-export-registered-backends)
2525 (org-export-define-backend 'test
2526 '((template . (lambda (contents info) "Success!"))
2527 (headline . (lambda (h c i) "Headline"))))
2528 (org-test-with-temp-text "* Headline"
2529 (org-export-as 'test nil nil 'body-only))))))
2533 ;;; Topology
2535 (ert-deftest test-org-export/get-next-element ()
2536 "Test `org-export-get-next-element' specifications."
2537 ;; Standard test.
2538 (should
2539 (equal "b"
2540 (org-test-with-parsed-data "* Headline\n*a* b"
2541 (org-export-get-next-element
2542 (org-element-map tree 'bold 'identity info t) info))))
2543 ;; Return nil when no previous element.
2544 (should-not
2545 (org-test-with-parsed-data "* Headline\na *b*"
2546 (org-export-get-next-element
2547 (org-element-map tree 'bold 'identity info t) info)))
2548 ;; Non-exportable elements are ignored.
2549 (should-not
2550 (let ((org-export-with-timestamps nil))
2551 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
2552 (org-export-get-next-element
2553 (org-element-map tree 'entity 'identity info t) info))))
2554 ;; Find next element in secondary strings.
2555 (should
2556 (eq 'verbatim
2557 (org-test-with-parsed-data "* a =verb="
2558 (org-element-type
2559 (org-export-get-next-element
2560 (org-element-map tree 'plain-text 'identity info t) info)))))
2561 ;; Find next element in document keywords.
2562 (should
2563 (eq 'verbatim
2564 (org-test-with-parsed-data "#+TITLE: a =verb="
2565 (org-element-type
2566 (org-export-get-next-element
2567 (org-element-map
2568 (plist-get info :title) 'plain-text 'identity info t) info)))))
2569 ;; Find next element in parsed affiliated keywords.
2570 (should
2571 (eq 'verbatim
2572 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
2573 (org-element-type
2574 (org-export-get-next-element
2575 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2576 ;; With optional argument N, return a list containing all the
2577 ;; following elements.
2578 (should
2579 (equal
2580 '(bold code underline)
2581 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2582 (mapcar 'car
2583 (org-export-get-next-element
2584 (org-element-map tree 'italic 'identity info t) info t)))))
2585 ;; When N is a positive integer, return a list containing up to
2586 ;; N following elements.
2587 (should
2588 (equal
2589 '(bold code)
2590 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2591 (mapcar 'car
2592 (org-export-get-next-element
2593 (org-element-map tree 'italic 'identity info t) info 2))))))
2595 (ert-deftest test-org-export/get-previous-element ()
2596 "Test `org-export-get-previous-element' specifications."
2597 ;; Standard test.
2598 (should
2599 (equal "a "
2600 (org-test-with-parsed-data "* Headline\na *b*"
2601 (org-export-get-previous-element
2602 (org-element-map tree 'bold 'identity info t) info))))
2603 ;; Return nil when no previous element.
2604 (should-not
2605 (org-test-with-parsed-data "* Headline\n*a* b"
2606 (org-export-get-previous-element
2607 (org-element-map tree 'bold 'identity info t) info)))
2608 ;; Non-exportable elements are ignored.
2609 (should-not
2610 (let ((org-export-with-timestamps nil))
2611 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
2612 (org-export-get-previous-element
2613 (org-element-map tree 'entity 'identity info t) info))))
2614 ;; Find previous element in secondary strings.
2615 (should
2616 (eq 'verbatim
2617 (org-test-with-parsed-data "* =verb= a"
2618 (org-element-type
2619 (org-export-get-previous-element
2620 (org-element-map tree 'plain-text 'identity info t) info)))))
2621 ;; Find previous element in document keywords.
2622 (should
2623 (eq 'verbatim
2624 (org-test-with-parsed-data "#+TITLE: =verb= a"
2625 (org-element-type
2626 (org-export-get-previous-element
2627 (org-element-map
2628 (plist-get info :title) 'plain-text 'identity info t) info)))))
2629 ;; Find previous element in parsed affiliated keywords.
2630 (should
2631 (eq 'verbatim
2632 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
2633 (org-element-type
2634 (org-export-get-previous-element
2635 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2636 ;; With optional argument N, return a list containing up to
2637 ;; N previous elements.
2638 (should
2639 (equal '(underline italic bold)
2640 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
2641 (mapcar 'car
2642 (org-export-get-previous-element
2643 (org-element-map tree 'code 'identity info t) info t)))))
2644 ;; When N is a positive integer, return a list containing up to
2645 ;; N previous elements.
2646 (should
2647 (equal '(italic bold)
2648 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
2649 (mapcar 'car
2650 (org-export-get-previous-element
2651 (org-element-map tree 'code 'identity info t) info 2))))))
2654 (provide 'test-ox)
2655 ;;; test-org-export.el end here