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