ox: Change meaning of `org-export-with-\(table\|fixed-width\)
[org-mode.git] / testing / lisp / test-ox.el
blob7a918c985e68fea53e169deee990c9235a50fefc
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)))))))
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)))))))
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))))))))))
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)))))))))))
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 (string-match "CLOCK: \\[2012-04-29 .* 10:45\\]"
404 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
405 (org-export-as (org-test-default-backend)
406 nil nil nil '(:with-clocks t)))))
407 (should
408 (equal ""
409 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
410 (org-export-as (org-test-default-backend)
411 nil nil nil '(:with-clocks nil)))))
412 ;; Drawers.
413 (should
414 (equal ""
415 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
416 (org-export-as (org-test-default-backend)
417 nil nil nil '(:with-drawers nil)))))
418 (should
419 (equal ":TEST:\ncontents\n:END:\n"
420 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
421 (org-export-as (org-test-default-backend)
422 nil nil nil '(:with-drawers t)))))
423 (should
424 (equal ":FOO:\nkeep\n:END:\n"
425 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
426 (org-export-as (org-test-default-backend)
427 nil nil nil '(:with-drawers ("FOO"))))))
428 (should
429 (equal ":FOO:\nkeep\n:END:\n"
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 (not "BAR"))))))
433 ;; Fixed-width.
434 (should
435 (equal ": A\n"
436 (org-test-with-temp-text ": A"
437 (org-export-as (org-test-default-backend) nil nil nil
438 '(:with-fixed-width t)))))
439 (should
440 (equal ""
441 (org-test-with-temp-text ": A"
442 (org-export-as (org-test-default-backend) nil nil nil
443 '(:with-fixed-width nil)))))
444 ;; Footnotes.
445 (should
446 (equal "Footnote?"
447 (let ((org-footnote-section nil))
448 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
449 (org-trim (org-export-as (org-test-default-backend)
450 nil nil nil '(:with-footnotes nil)))))))
451 (should
452 (equal "Footnote?[fn:1]\n\n[fn:1] Def"
453 (let ((org-footnote-section nil))
454 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
455 (org-trim (org-export-as (org-test-default-backend)
456 nil nil nil '(:with-footnotes t)))))))
457 ;; Inlinetasks.
458 (when (featurep 'org-inlinetask)
459 (should
460 (equal
462 (let ((org-inlinetask-min-level 15))
463 (org-test-with-temp-text "*************** Task"
464 (org-export-as (org-test-default-backend)
465 nil nil nil '(:with-inlinetasks nil))))))
466 (should
467 (equal
469 (let ((org-inlinetask-min-level 15))
470 (org-test-with-temp-text
471 "*************** Task\nContents\n*************** END"
472 (org-export-as (org-test-default-backend)
473 nil nil nil '(:with-inlinetasks nil)))))))
474 ;; Plannings.
475 (should
476 (string-match
477 "CLOSED: \\[2012-04-29 .* 10:45\\]"
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 t))))))
482 (should
483 (equal ""
484 (let ((org-closed-string "CLOSED:"))
485 (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
486 (org-export-as (org-test-default-backend)
487 nil nil nil '(:with-planning nil))))))
488 ;; Property Drawers.
489 (should
490 (equal "* H1\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 nil)))))
495 (should
496 (equal "* H1\n:PROPERTIES:\n:PROP: value\n:END:\n"
497 (org-test-with-temp-text
498 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
499 (org-export-as (org-test-default-backend)
500 nil nil nil '(:with-properties t)))))
501 (should
502 (equal "* H1\n:PROPERTIES:\n:B: 2\n:END:\n"
503 (org-test-with-temp-text
504 "* H1\n :PROPERTIES:\n :A: 1\n :B: 2\n:END:"
505 (org-export-as (org-test-default-backend)
506 nil nil nil '(:with-properties ("B"))))))
507 ;; Statistics cookies.
508 (should
509 (equal ""
510 (org-test-with-temp-text "[0/0]"
511 (org-export-as (org-test-default-backend)
512 nil nil nil '(:with-statistics-cookies nil)))))
513 ;; Tables.
514 (should
515 (equal "| A |\n"
516 (org-test-with-temp-text "| A |"
517 (org-export-as (org-test-default-backend) nil nil nil
518 '(:with-tables t)))))
519 (should
520 (equal ""
521 (org-test-with-temp-text "| A |"
522 (org-export-as (org-test-default-backend) nil nil nil
523 '(:with-tables nil))))))
525 (ert-deftest test-org-export/with-timestamps ()
526 "Test `org-export-with-timestamps' specifications."
527 ;; t value.
528 (should
529 (string-match
530 "\\[2012-04-29 .*? 10:45\\]<2012-04-29 .*? 10:45>"
531 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
532 (org-export-as (org-test-default-backend)
533 nil nil nil '(:with-timestamps t)))))
534 ;; nil value.
535 (should
536 (equal
538 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
539 (org-export-as (org-test-default-backend)
540 nil nil nil '(:with-timestamps nil)))))
541 ;; `active' value.
542 (should
543 (string-match
544 "<2012-03-29 .*?>\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
545 (org-test-with-temp-text
546 "<2012-03-29 Thu>[2012-03-29 Thu]
548 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
549 (org-export-as (org-test-default-backend)
550 nil nil nil '(:with-timestamps active)))))
551 ;; `inactive' value.
552 (should
553 (string-match
554 "\\[2012-03-29 .*?\\]\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
555 (org-test-with-temp-text
556 "<2012-03-29 Thu>[2012-03-29 Thu]
558 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
559 (org-export-as (org-test-default-backend)
560 nil nil nil '(:with-timestamps inactive))))))
562 (ert-deftest test-org-export/comment-tree ()
563 "Test if export process ignores commented trees."
564 (should
565 (equal ""
566 (org-test-with-temp-text "* COMMENT Head1"
567 (org-export-as (org-test-default-backend))))))
569 (ert-deftest test-org-export/uninterpreted ()
570 "Test handling of uninterpreted elements."
571 ;; Entities.
572 (should
573 (equal "dummy\n"
574 (org-test-with-temp-text "\\alpha"
575 (org-export-as
576 (org-export-create-backend
577 :transcoders '((entity . (lambda (e c i) "dummy"))
578 (paragraph . (lambda (p c i) c))
579 (section . (lambda (s c i) c))))
580 nil nil nil '(:with-entities t)))))
581 (should
582 (equal "\\alpha\n"
583 (org-test-with-temp-text "\\alpha"
584 (org-export-as
585 (org-export-create-backend
586 :transcoders '((entity . (lambda (e c i) "dummy"))
587 (paragraph . (lambda (p c i) c))
588 (section . (lambda (s c i) c))))
589 nil nil nil '(:with-entities nil)))))
590 ;; Emphasis.
591 (should
592 (equal "dummy\n"
593 (org-test-with-temp-text "*bold*"
594 (org-export-as
595 (org-export-create-backend
596 :transcoders '((bold . (lambda (b c i) "dummy"))
597 (paragraph . (lambda (p c i) c))
598 (section . (lambda (s c i) c))))
599 nil nil nil '(:with-emphasize t)))))
600 (should
601 (equal "*bold*\n"
602 (org-test-with-temp-text "*bold*"
603 (org-export-as
604 (org-export-create-backend
605 :transcoders '((bold . (lambda (b c i) "dummy"))
606 (paragraph . (lambda (p c i) c))
607 (section . (lambda (s c i) c))))
608 nil nil nil '(:with-emphasize nil)))))
609 ;; LaTeX environment.
610 (should
611 (equal "dummy\n"
612 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
613 (org-export-as
614 (org-export-create-backend
615 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
616 (section . (lambda (s c i) c))))
617 nil nil nil '(:with-latex t)))))
618 (should
619 (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
620 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
621 (org-export-as
622 (org-export-create-backend
623 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
624 (section . (lambda (s c i) c))))
625 nil nil nil '(:with-latex verbatim)))))
626 ;; LaTeX fragment.
627 (should
628 (equal "dummy\n"
629 (org-test-with-temp-text "$1$"
630 (org-export-as
631 (org-export-create-backend
632 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
633 (paragraph . (lambda (p c i) c))
634 (section . (lambda (s c i) c))))
635 nil nil nil '(:with-latex t)))))
636 (should
637 (equal "$1$\n"
638 (org-test-with-temp-text "$1$"
639 (org-export-as
640 (org-export-create-backend
641 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
642 (paragraph . (lambda (p c i) c))
643 (section . (lambda (s c i) c))))
644 nil nil nil '(:with-latex verbatim)))))
645 ;; Sub/superscript.
646 (should
647 (equal "adummy\n"
648 (org-test-with-temp-text "a_b"
649 (org-export-as
650 (org-export-create-backend
651 :transcoders '((subscript . (lambda (s c i) "dummy"))
652 (paragraph . (lambda (p c i) c))
653 (section . (lambda (s c i) c))))
654 nil nil nil '(:with-sub-superscript t)))))
655 (should
656 (equal "a_b\n"
657 (org-test-with-temp-text "a_b"
658 (org-export-as
659 (org-export-create-backend
660 :transcoders '((subscript . (lambda (s c i) "dummy"))
661 (paragraph . (lambda (p c i) c))
662 (section . (lambda (s c i) c))))
663 nil nil nil '(:with-sub-superscript nil)))))
664 (should
665 (equal "a_b\n"
666 (org-test-with-temp-text "a_b"
667 (org-export-as
668 (org-export-create-backend
669 :transcoders '((subscript . (lambda (s c i) "dummy"))
670 (paragraph . (lambda (p c i) c))
671 (section . (lambda (s c i) c))))
672 nil nil nil '(:with-sub-superscript {})))))
673 (should
674 (equal "adummy\n"
675 (org-test-with-temp-text "a_{b}"
676 (org-export-as
677 (org-export-create-backend
678 :transcoders '((subscript . (lambda (s c i) "dummy"))
679 (paragraph . (lambda (p c i) c))
680 (section . (lambda (s c i) c))))
681 nil nil nil '(:with-sub-superscript {})))))
682 ;; Also handle uninterpreted objects in title.
683 (should
684 (equal "a_b"
685 (org-test-with-temp-text "#+TITLE: a_b"
686 (org-export-as
687 (org-export-create-backend
688 :transcoders
689 '((subscript . (lambda (s c i) "dummy"))
690 (template . (lambda (c i) (org-export-data
691 (plist-get i :title) i)))
692 (section . (lambda (s c i) c))))
693 nil nil nil '(:with-sub-superscript nil))))))
695 (ert-deftest test-org-export/export-scope ()
696 "Test all export scopes."
697 (org-test-with-temp-text "
698 * Head1
699 ** Head2
700 text
701 *** Head3"
702 ;; Subtree.
703 (forward-line 3)
704 (should (equal (org-export-as (org-test-default-backend) 'subtree)
705 "text\n*** Head3\n"))
706 ;; Visible.
707 (goto-char (point-min))
708 (forward-line)
709 (org-cycle)
710 (should (equal (org-export-as (org-test-default-backend) nil 'visible)
711 "* Head1\n"))
712 ;; Region.
713 (goto-char (point-min))
714 (forward-line 3)
715 (transient-mark-mode 1)
716 (push-mark (point) t t)
717 (goto-char (point-at-eol))
718 (should (equal (org-export-as (org-test-default-backend)) "text\n")))
719 ;; Subtree with a code block calling another block outside.
720 (should
721 (equal ": 3\n"
722 (org-test-with-temp-text "
723 * Head1
724 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
725 <<test>>
726 #+END_SRC
727 * Head2
728 #+NAME: test
729 #+BEGIN_SRC emacs-lisp
730 \(+ 1 2)
731 #+END_SRC"
732 (forward-line 1)
733 (org-export-as (org-test-default-backend) 'subtree))))
734 ;; Body only.
735 (let ((backend (org-test-default-backend)))
736 (setf (org-export-backend-transcoders backend)
737 (cons '(template . (lambda (body i)
738 (format "BEGIN\n%sEND" body)))
739 (org-export-backend-transcoders backend)))
740 (org-test-with-temp-text "Text"
741 (should (equal (org-export-as backend nil nil 'body-only)
742 "Text\n"))
743 (should (equal (org-export-as backend) "BEGIN\nText\nEND")))))
745 (ert-deftest test-org-export/output-file-name ()
746 "Test `org-export-output-file-name' specifications."
747 ;; Export from a file: name is built from original file name.
748 (should
749 (org-test-with-temp-text-in-file "Test"
750 (equal (concat (file-name-as-directory ".")
751 (file-name-nondirectory
752 (file-name-sans-extension (buffer-file-name))))
753 (file-name-sans-extension (org-export-output-file-name ".ext")))))
754 ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
755 (should
756 (org-test-with-temp-text-in-file
757 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
758 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
759 ;; From a buffer not associated to a file, too.
760 (should
761 (org-test-with-temp-text
762 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
763 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
764 ;; When provided name is absolute, preserve it.
765 (should
766 (org-test-with-temp-text
767 (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
768 (expand-file-name "test"))
769 (file-name-absolute-p (org-export-output-file-name ".ext" t))))
770 ;; When PUB-DIR argument is provided, use it.
771 (should
772 (org-test-with-temp-text-in-file "Test"
773 (equal (file-name-directory
774 (org-export-output-file-name ".ext" nil "dir/"))
775 "dir/")))
776 ;; When returned name would overwrite original file, add EXTENSION
777 ;; another time.
778 (should
779 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
780 (equal (org-export-output-file-name ".org") "./normal.org.org"))))
782 (ert-deftest test-org-export/expand-include ()
783 "Test file inclusion in an Org buffer."
784 ;; Error when file isn't specified.
785 (should-error
786 (org-test-with-temp-text "#+INCLUDE: dummy.org"
787 (org-export-expand-include-keyword)))
788 ;; Full insertion with recursive inclusion.
789 (org-test-with-temp-text
790 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
791 (org-export-expand-include-keyword)
792 (should (equal (buffer-string)
793 "Small Org file with an include keyword.
795 #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
797 Success!
799 * Heading
800 body\n")))
801 ;; Localized insertion.
802 (org-test-with-temp-text
803 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
804 org-test-dir)
805 (org-export-expand-include-keyword)
806 (should (equal (buffer-string)
807 "Small Org file with an include keyword.\n")))
808 ;; Insertion with constraints on headlines level.
809 (should
810 (equal
811 "* Top heading\n** Heading\nbody\n"
812 (org-test-with-temp-text
813 (format
814 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
815 org-test-dir)
816 (org-export-expand-include-keyword)
817 (buffer-string))))
818 (should
819 (equal
820 "* Top heading\n* Heading\nbody\n"
821 (org-test-with-temp-text
822 (format
823 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\" :minlevel 1"
824 org-test-dir)
825 (org-export-expand-include-keyword)
826 (buffer-string))))
827 ;; Inclusion within an example block.
828 (org-test-with-temp-text
829 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
830 org-test-dir)
831 (org-export-expand-include-keyword)
832 (should
833 (equal
834 (buffer-string)
835 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
836 ;; Inclusion within a src-block.
837 (org-test-with-temp-text
838 (format
839 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
840 org-test-dir)
841 (org-export-expand-include-keyword)
842 (should (equal (buffer-string)
843 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
845 (ert-deftest test-org-export/expand-macro ()
846 "Test macro expansion in an Org buffer."
847 ;; Standard macro expansion.
848 (should
849 (equal "#+MACRO: macro1 value\nvalue\n"
850 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
851 (org-export-as (org-test-default-backend)))))
852 ;; Expand specific macros.
853 (should
854 (equal "me 2012-03-29 me@here Title\n"
855 (org-test-with-temp-text
857 #+TITLE: Title
858 #+DATE: 2012-03-29
859 #+AUTHOR: me
860 #+EMAIL: me@here
861 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
862 (let ((output (org-export-as (org-test-default-backend))))
863 (substring output (string-match ".*\n\\'" output))))))
864 ;; Expand specific macros when property contained a regular macro
865 ;; already.
866 (should
867 (equal "value\n"
868 (org-test-with-temp-text "
869 #+MACRO: macro1 value
870 #+TITLE: {{{macro1}}}
871 {{{title}}}"
872 (let ((output (org-export-as (org-test-default-backend))))
873 (substring output (string-match ".*\n\\'" output))))))
874 ;; Expand macros with templates in included files.
875 (should
876 (equal "success\n"
877 (org-test-with-temp-text
878 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
879 {{{included-macro}}}" org-test-dir)
880 (let ((output (org-export-as (org-test-default-backend))))
881 (substring output (string-match ".*\n\\'" output)))))))
883 (ert-deftest test-org-export/user-ignore-list ()
884 "Test if `:ignore-list' accepts user input."
885 (let ((backend (org-test-default-backend)))
886 (setf (org-export-backend-transcoders backend)
887 (cons '(template . (lambda (body i)
888 (format "BEGIN\n%sEND" body)))
889 (org-export-backend-transcoders backend)))
890 (org-test-with-temp-text "Text"
891 (should (equal (org-export-as backend nil nil 'body-only)
892 "Text\n"))
893 (should (equal (org-export-as backend) "BEGIN\nText\nEND"))))
894 (should
895 (equal
896 "* Head1\n"
897 (let ((org-export-filter-parse-tree-functions
898 '((lambda (data backend info)
899 ;; Ignore headlines with the word "note" in their title.
900 (org-element-map data 'headline
901 (lambda (headline)
902 (when (string-match "\\<note\\>"
903 (org-element-property :raw-value
904 headline))
905 (org-export-ignore-element headline info)))
906 info)
907 data))))
908 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
909 (org-export-as (org-test-default-backend)))))))
911 (ert-deftest test-org-export/before-processing-hook ()
912 "Test `org-export-before-processing-hook'."
913 (should
914 (equal
915 "#+MACRO: mac val\nTest\n"
916 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
917 (let ((org-export-before-processing-hook
918 '((lambda (backend)
919 (while (re-search-forward "{{{" nil t)
920 (let ((object (org-element-context)))
921 (when (eq (org-element-type object) 'macro)
922 (delete-region
923 (org-element-property :begin object)
924 (org-element-property :end object)))))))))
925 (org-export-as (org-test-default-backend)))))))
927 (ert-deftest test-org-export/before-parsing-hook ()
928 "Test `org-export-before-parsing-hook'."
929 (should
930 (equal "Body 1\nBody 2\n"
931 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
932 (let ((org-export-before-parsing-hook
933 '((lambda (backend)
934 (goto-char (point-min))
935 (while (re-search-forward org-outline-regexp-bol nil t)
936 (delete-region
937 (point-at-bol) (progn (forward-line) (point))))))))
938 (org-export-as (org-test-default-backend)))))))
942 ;;; Affiliated Keywords
944 (ert-deftest test-org-export/read-attribute ()
945 "Test `org-export-read-attribute' specifications."
946 ;; Standard test.
947 (should
948 (equal
949 (org-export-read-attribute
950 :attr_html
951 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
952 (org-element-at-point)))
953 '(:a "1" :b "2")))
954 ;; Return nil on empty attribute.
955 (should-not
956 (org-export-read-attribute
957 :attr_html
958 (org-test-with-temp-text "Paragraph" (org-element-at-point))))
959 ;; Return nil on "nil" string.
960 (should
961 (equal '(:a nil :b nil)
962 (org-export-read-attribute
963 :attr_html
964 (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
965 (org-element-at-point)))))
966 ;; Return nil on empty string.
967 (should
968 (equal '(:a nil :b nil)
969 (org-export-read-attribute
970 :attr_html
971 (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
972 (org-element-at-point)))))
973 ;; Return empty string when value is "".
974 (should
975 (equal '(:a "")
976 (org-export-read-attribute
977 :attr_html
978 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
979 (org-element-at-point)))))
980 ;; Return \"\" when value is """".
981 (should
982 (equal '(:a "\"\"")
983 (org-export-read-attribute
984 :attr_html
985 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
986 (org-element-at-point)))))
987 ;; Ignore text before first property.
988 (should-not
989 (member "ignore"
990 (org-export-read-attribute
991 :attr_html
992 (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
993 (org-element-at-point))))))
995 (ert-deftest test-org-export/get-caption ()
996 "Test `org-export-get-caption' specifications."
997 ;; Without optional argument, return long caption
998 (should
999 (equal
1000 '("l")
1001 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1002 (org-export-get-caption (org-element-at-point)))))
1003 ;; With optional argument, return short caption.
1004 (should
1005 (equal
1006 '("s")
1007 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1008 (org-export-get-caption (org-element-at-point) t))))
1009 ;; Multiple lines are separated by white spaces.
1010 (should
1011 (equal
1012 '("a" " " "b")
1013 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
1014 (org-export-get-caption (org-element-at-point))))))
1018 ;;; Back-End Tools
1020 (ert-deftest test-org-export/define-backend ()
1021 "Test back-end definition and accessors."
1022 ;; Translate table.
1023 (should
1024 (equal '((headline . my-headline-test))
1025 (let (org-export--registered-backends)
1026 (org-export-define-backend 'test '((headline . my-headline-test)))
1027 (org-export-get-all-transcoders 'test))))
1028 ;; Filters.
1029 (should
1030 (equal '((:filter-headline . my-filter))
1031 (let (org-export--registered-backends)
1032 (org-export-define-backend 'test
1033 '((headline . my-headline-test))
1034 :filters-alist '((:filter-headline . my-filter)))
1035 (org-export-backend-filters (org-export-get-backend 'test)))))
1036 ;; Options.
1037 (should
1038 (equal '((:prop value))
1039 (let (org-export--registered-backends)
1040 (org-export-define-backend 'test
1041 '((headline . my-headline-test))
1042 :options-alist '((:prop value)))
1043 (org-export-backend-options (org-export-get-backend 'test)))))
1044 ;; Menu.
1045 (should
1046 (equal '(?k "Test Export" test)
1047 (let (org-export--registered-backends)
1048 (org-export-define-backend 'test
1049 '((headline . my-headline-test))
1050 :menu-entry '(?k "Test Export" test))
1051 (org-export-backend-menu (org-export-get-backend 'test)))))
1052 ;; Export Blocks.
1053 (should
1054 (equal '(("TEST" . org-element-export-block-parser))
1055 (let (org-export--registered-backends org-element-block-name-alist)
1056 (org-export-define-backend 'test
1057 '((headline . my-headline-test))
1058 :export-block '("test"))
1059 org-element-block-name-alist))))
1061 (ert-deftest test-org-export/define-derived-backend ()
1062 "Test `org-export-define-derived-backend' specifications."
1063 ;; Error when parent back-end is not defined.
1064 (should-error
1065 (let (org-export--registered-backends)
1066 (org-export-define-derived-backend 'test 'parent)))
1067 ;; Append translation table to parent's.
1068 (should
1069 (equal '((:headline . test) (:headline . parent))
1070 (let (org-export--registered-backends)
1071 (org-export-define-backend 'parent '((:headline . parent)))
1072 (org-export-define-derived-backend 'test 'parent
1073 :translate-alist '((:headline . test)))
1074 (org-export-get-all-transcoders 'test))))
1075 ;; Options defined in the new back have priority over those defined
1076 ;; in parent.
1077 (should
1078 (eq 'test
1079 (let (org-export--registered-backends)
1080 (org-export-define-backend 'parent
1081 '((:headline . parent))
1082 :options-alist '((:a nil nil 'parent)))
1083 (org-export-define-derived-backend 'test 'parent
1084 :options-alist '((:a nil nil 'test)))
1085 (plist-get (org-export--get-global-options
1086 (org-export-get-backend 'test))
1087 :a)))))
1089 (ert-deftest test-org-export/derived-backend-p ()
1090 "Test `org-export-derived-backend-p' specifications."
1091 ;; Non-nil with direct match.
1092 (should
1093 (let (org-export--registered-backends)
1094 (org-export-define-backend 'test '((headline . test)))
1095 (org-export-derived-backend-p 'test 'test)))
1096 (should
1097 (let (org-export--registered-backends)
1098 (org-export-define-backend 'test '((headline . test)))
1099 (org-export-define-derived-backend 'test2 'test)
1100 (org-export-derived-backend-p 'test2 'test2)))
1101 ;; Non-nil with a direct parent.
1102 (should
1103 (let (org-export--registered-backends)
1104 (org-export-define-backend 'test '((headline . test)))
1105 (org-export-define-derived-backend 'test2 'test)
1106 (org-export-derived-backend-p 'test2 'test)))
1107 ;; Non-nil with an indirect parent.
1108 (should
1109 (let (org-export--registered-backends)
1110 (org-export-define-backend 'test '((headline . test)))
1111 (org-export-define-derived-backend 'test2 'test)
1112 (org-export-define-derived-backend 'test3 'test2)
1113 (org-export-derived-backend-p 'test3 'test)))
1114 ;; Nil otherwise.
1115 (should-not
1116 (let (org-export--registered-backends)
1117 (org-export-define-backend 'test '((headline . test)))
1118 (org-export-define-backend 'test2 '((headline . test2)))
1119 (org-export-derived-backend-p 'test2 'test)))
1120 (should-not
1121 (let (org-export--registered-backends)
1122 (org-export-define-backend 'test '((headline . test)))
1123 (org-export-define-backend 'test2 '((headline . test2)))
1124 (org-export-define-derived-backend 'test3 'test2)
1125 (org-export-derived-backend-p 'test3 'test))))
1127 (ert-deftest test-org-export/get-all-transcoders ()
1128 "Test `org-export-get-all-transcoders' specifications."
1129 ;; Return nil when back-end cannot be found.
1130 (should-not (org-export-get-all-transcoders nil))
1131 ;; Same as `org-export-transcoders' if no parent.
1132 (should
1133 (equal '((headline . ignore))
1134 (org-export-get-all-transcoders
1135 (org-export-create-backend
1136 :transcoders '((headline . ignore))))))
1137 ;; But inherit from all ancestors whenever possible.
1138 (should
1139 (equal '((section . ignore) (headline . ignore))
1140 (let (org-export--registered-backends)
1141 (org-export-define-backend 'b1 '((headline . ignore)))
1142 (org-export-get-all-transcoders
1143 (org-export-create-backend
1144 :parent 'b1 :transcoders '((section . ignore)))))))
1145 (should
1146 (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
1147 (let (org-export--registered-backends)
1148 (org-export-define-backend 'b1 '((headline . ignore)))
1149 (org-export-define-derived-backend 'b2 'b1
1150 :translate-alist '((section . ignore)))
1151 (org-export-get-all-transcoders
1152 (org-export-create-backend
1153 :parent 'b2 :transcoders '((paragraph . ignore)))))))
1154 ;; Back-end transcoders overrule inherited ones.
1155 (should
1156 (eq 'b
1157 (let (org-export--registered-backends)
1158 (org-export-define-backend 'b1 '((headline . a)))
1159 (cdr (assq 'headline
1160 (org-export-get-all-transcoders
1161 (org-export-create-backend
1162 :parent 'b1 :transcoders '((headline . b))))))))))
1164 (ert-deftest test-org-export/get-all-options ()
1165 "Test `org-export-get-all-options' specifications."
1166 ;; Return nil when back-end cannot be found.
1167 (should-not (org-export-get-all-options nil))
1168 ;; Same as `org-export-options' if no parent.
1169 (should
1170 (equal '((headline . ignore))
1171 (org-export-get-all-options
1172 (org-export-create-backend
1173 :options '((headline . ignore))))))
1174 ;; But inherit from all ancestors whenever possible.
1175 (should
1176 (equal '((:key2 value2) (:key1 value1))
1177 (let (org-export--registered-backends)
1178 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1179 (org-export-get-all-options
1180 (org-export-create-backend
1181 :parent 'b1 :options '((:key2 value2)))))))
1182 (should
1183 (equal '((:key3 value3) (:key2 value2) (:key1 value1))
1184 (let (org-export--registered-backends)
1185 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1186 (org-export-define-derived-backend 'b2 'b1
1187 :options-alist '((:key2 value2)))
1188 (org-export-get-all-options
1189 (org-export-create-backend
1190 :parent 'b2 :options '((:key3 value3)))))))
1191 ;; Back-end options overrule inherited ones.
1192 (should
1193 (eq 'b
1194 (let (org-export--registered-backends)
1195 (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
1196 (cdr (assq :key1
1197 (org-export-get-all-options
1198 (org-export-create-backend
1199 :parent 'b1 :options '((:key1 . b))))))))))
1201 (ert-deftest test-org-export/get-all-filters ()
1202 "Test `org-export-get-all-filters' specifications."
1203 ;; Return nil when back-end cannot be found.
1204 (should-not (org-export-get-all-filters nil))
1205 ;; Same as `org-export-filters' if no parent.
1206 (should
1207 (equal '((:filter-headline . ignore))
1208 (org-export-get-all-filters
1209 (org-export-create-backend
1210 :filters '((:filter-headline . ignore))))))
1211 ;; But inherit from all ancestors whenever possible.
1212 (should
1213 (equal '((:filter-section . ignore) (:filter-headline . ignore))
1214 (let (org-export--registered-backends)
1215 (org-export-define-backend 'b1
1216 nil :filters-alist '((:filter-headline . ignore)))
1217 (org-export-get-all-filters
1218 (org-export-create-backend
1219 :parent 'b1 :filters '((:filter-section . ignore)))))))
1220 (should
1221 (equal '((:filter-paragraph . ignore)
1222 (:filter-section . ignore)
1223 (:filter-headline . ignore))
1224 (let (org-export--registered-backends)
1225 (org-export-define-backend 'b1
1226 nil :filters-alist '((:filter-headline . ignore)))
1227 (org-export-define-derived-backend 'b2 'b1
1228 :filters-alist '((:filter-section . ignore)))
1229 (org-export-get-all-filters
1230 (org-export-create-backend
1231 :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
1232 ;; Back-end filters overrule inherited ones.
1233 (should
1234 (eq 'b
1235 (let (org-export--registered-backends)
1236 (org-export-define-backend 'b1 '((:filter-headline . a)))
1237 (cdr (assq :filter-headline
1238 (org-export-get-all-filters
1239 (org-export-create-backend
1240 :parent 'b1 :filters '((:filter-headline . b))))))))))
1242 (ert-deftest test-org-export/with-backend ()
1243 "Test `org-export-with-backend' definition."
1244 ;; Error when calling an undefined back-end
1245 (should-error (org-export-with-backend nil "Test"))
1246 ;; Error when called back-end doesn't have an appropriate
1247 ;; transcoder.
1248 (should-error
1249 (org-export-with-backend
1250 (org-export-create-backend :transcoders '((headline . ignore)))
1251 "Test"))
1252 ;; Otherwise, export using correct transcoder
1253 (should
1254 (equal "Success"
1255 (let (org-export--registered-backends)
1256 (org-export-define-backend 'test
1257 '((plain-text . (lambda (text contents info) "Failure"))))
1258 (org-export-define-backend 'test2
1259 '((plain-text . (lambda (text contents info) "Success"))))
1260 (org-export-with-backend 'test2 "Test"))))
1261 ;; Provide correct back-end if transcoder needs to use recursive
1262 ;; calls anyway.
1263 (should
1264 (equal "Success\n"
1265 (let ((test-back-end
1266 (org-export-create-backend
1267 :transcoders
1268 '((headline . (lambda (headline contents info)
1269 (org-export-data
1270 (org-element-property :title headline)
1271 info)))
1272 (plain-text . (lambda (text info) "Success"))))))
1273 (org-export-string-as
1274 "* Test"
1275 (org-export-create-backend
1276 :transcoders
1277 '((headline . (lambda (headline contents info)
1278 (org-export-with-backend
1279 test-back-end headline contents info))))))))))
1281 (ert-deftest test-org-export/data-with-backend ()
1282 "Test `org-export-data-with-backend' specifications."
1283 ;; Error when calling an undefined back-end.
1284 (should-error (org-export-data-with-backend nil "nil" nil))
1285 ;; Otherwise, export data recursively, using correct back-end.
1286 (should
1287 (equal
1288 "Success!"
1289 (org-export-data-with-backend
1290 '(bold nil "Test")
1291 (org-export-create-backend
1292 :transcoders
1293 '((plain-text . (lambda (text info) "Success"))
1294 (bold . (lambda (bold contents info) (concat contents "!")))))
1295 '(:with-emphasize t)))))
1299 ;;; Export Snippets
1301 (ert-deftest test-org-export/export-snippet ()
1302 "Test export snippets transcoding."
1303 ;; Standard test.
1304 (org-test-with-temp-text "@@test:A@@@@t:B@@"
1305 (let ((backend (org-test-default-backend)))
1306 (setf (org-export-backend-name backend) 'test)
1307 (setf (org-export-backend-transcoders backend)
1308 (cons (cons 'export-snippet
1309 (lambda (snippet contents info)
1310 (when (eq (org-export-snippet-backend snippet) 'test)
1311 (org-element-property :value snippet))))
1312 (org-export-backend-transcoders backend)))
1313 (let ((org-export-snippet-translation-alist nil))
1314 (should (equal (org-export-as backend) "A\n")))
1315 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
1316 (should (equal (org-export-as backend) "AB\n")))))
1317 ;; Ignored export snippets do not remove any blank.
1318 (should
1319 (equal "begin end\n"
1320 (org-test-with-parsed-data "begin @@test:A@@ end"
1321 (org-export-data-with-backend
1322 tree
1323 (org-export-create-backend
1324 :transcoders
1325 '((paragraph . (lambda (paragraph contents info) contents))
1326 (section . (lambda (section contents info) contents))))
1327 info)))))
1331 ;;; Footnotes
1333 (ert-deftest test-org-export/footnotes ()
1334 "Test footnotes specifications."
1335 (let ((org-footnote-section nil)
1336 (org-export-with-footnotes t))
1337 ;; 1. Read every type of footnote.
1338 (should
1339 (equal
1340 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
1341 (org-test-with-parsed-data
1342 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
1343 (org-element-map tree 'footnote-reference
1344 (lambda (ref)
1345 (let ((def (org-export-get-footnote-definition ref info)))
1346 (cons (org-export-get-footnote-number ref info)
1347 (if (eq (org-element-property :type ref) 'inline) (car def)
1348 (car (org-element-contents
1349 (car (org-element-contents def))))))))
1350 info))))
1351 ;; 2. Test nested footnotes order.
1352 (should
1353 (equal
1354 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
1355 (org-test-with-parsed-data
1356 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
1357 (org-element-map tree 'footnote-reference
1358 (lambda (ref)
1359 (when (org-export-footnote-first-reference-p ref info)
1360 (cons (org-export-get-footnote-number ref info)
1361 (org-element-property :label ref))))
1362 info))))
1363 ;; 3. Test nested footnote in invisible definitions.
1364 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
1365 ;; Hide definitions.
1366 (narrow-to-region (point) (point-at-eol))
1367 (let* ((tree (org-element-parse-buffer))
1368 (info (org-combine-plists
1369 `(:parse-tree ,tree)
1370 (org-export-collect-tree-properties
1371 tree (org-export-get-environment)))))
1372 ;; Both footnotes should be seen.
1373 (should
1374 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
1375 ;; 4. Test footnotes definitions collection.
1376 (should
1377 (= 4
1378 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1380 \[fn:2] B [fn:3] [fn::D].
1382 \[fn:3] C."
1383 (length (org-export-collect-footnote-definitions tree info)))))
1384 ;; 5. Test export of footnotes defined outside parsing scope.
1385 (should
1386 (equal
1387 "ParagraphOut of scope\n"
1388 (org-test-with-temp-text "[fn:1] Out of scope
1389 * Title
1390 Paragraph[fn:1]"
1391 (let ((backend (org-test-default-backend)))
1392 (setf (org-export-backend-transcoders backend)
1393 (cons (cons 'footnote-reference
1394 (lambda (fn contents info)
1395 (org-element-interpret-data
1396 (org-export-get-footnote-definition fn info))))
1397 (org-export-backend-transcoders backend)))
1398 (forward-line)
1399 (org-export-as backend 'subtree)))))
1400 ;; 6. Footnotes without a definition should be provided a fallback
1401 ;; definition.
1402 (should
1403 (org-test-with-parsed-data "[fn:1]"
1404 (org-export-get-footnote-definition
1405 (org-element-map tree 'footnote-reference 'identity info t) info)))
1406 ;; 7. Footnote section should be ignored in TOC and in headlines
1407 ;; numbering.
1408 (should
1409 (= 1 (let ((org-footnote-section "Footnotes"))
1410 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
1411 (org-export-collect-headlines info))))))
1412 (should
1413 (equal '(2)
1414 (let ((org-footnote-section "Footnotes"))
1415 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
1416 (org-element-map tree 'headline
1417 (lambda (hl)
1418 (when (equal (org-element-property :raw-value hl) "H2")
1419 (org-export-get-headline-number hl info)))
1420 info t)))))))
1424 ;;; Headlines and Inlinetasks
1426 (ert-deftest test-org-export/get-relative-level ()
1427 "Test `org-export-get-relative-level' specifications."
1428 ;; Standard test.
1429 (should
1430 (equal '(1 2)
1431 (let ((org-odd-levels-only nil))
1432 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1433 (org-element-map tree 'headline
1434 (lambda (h) (org-export-get-relative-level h info))
1435 info)))))
1436 ;; Missing levels
1437 (should
1438 (equal '(1 3)
1439 (let ((org-odd-levels-only nil))
1440 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
1441 (org-element-map tree 'headline
1442 (lambda (h) (org-export-get-relative-level h info))
1443 info))))))
1445 (ert-deftest test-org-export/low-level-p ()
1446 "Test `org-export-low-level-p' specifications."
1447 (should
1448 (equal
1449 '(no yes)
1450 (let ((org-odd-levels-only nil))
1451 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1452 (org-element-map tree 'headline
1453 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
1454 (plist-put info :headline-levels 1)))))))
1456 (ert-deftest test-org-export/get-headline-number ()
1457 "Test `org-export-get-headline-number' specifications."
1458 ;; Standard test.
1459 (should
1460 (equal
1461 '((1) (1 1))
1462 (let ((org-odd-levels-only nil))
1463 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1464 (org-element-map tree 'headline
1465 (lambda (h) (org-export-get-headline-number h info))
1466 info)))))
1467 ;; Missing levels are replaced with 0.
1468 (should
1469 (equal
1470 '((1) (1 0 1))
1471 (let ((org-odd-levels-only nil))
1472 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
1473 (org-element-map tree 'headline
1474 (lambda (h) (org-export-get-headline-number h info))
1475 info))))))
1477 (ert-deftest test-org-export/numbered-headline-p ()
1478 "Test `org-export-numbered-headline-p' specifications."
1479 ;; If `:section-numbers' is nil, never number headlines.
1480 (should-not
1481 (org-test-with-parsed-data "* Headline"
1482 (org-element-map tree 'headline
1483 (lambda (h) (org-export-numbered-headline-p h info))
1484 (plist-put info :section-numbers nil))))
1485 ;; If `:section-numbers' is a number, only number headlines with
1486 ;; a level greater that it.
1487 (should
1488 (equal
1489 '(yes no)
1490 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1491 (org-element-map tree 'headline
1492 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
1493 (plist-put info :section-numbers 1)))))
1494 ;; Otherwise, headlines are always numbered.
1495 (should
1496 (org-test-with-parsed-data "* Headline"
1497 (org-element-map tree 'headline
1498 (lambda (h) (org-export-numbered-headline-p h info))
1499 (plist-put info :section-numbers t)))))
1501 (ert-deftest test-org-export/number-to-roman ()
1502 "Test `org-export-number-to-roman' specifications."
1503 ;; If number is negative, return it as a string.
1504 (should (equal (org-export-number-to-roman -1) "-1"))
1505 ;; Otherwise, return it as a roman number.
1506 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
1508 (ert-deftest test-org-export/get-optional-title ()
1509 "Test `org-export-get-alt-title' specifications."
1510 ;; If ALT_TITLE property is defined, use it.
1511 (should
1512 (equal '("opt")
1513 (org-test-with-parsed-data
1514 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
1515 (org-export-get-alt-title
1516 (org-element-map tree 'headline 'identity info t)
1517 info))))
1518 ;; Otherwise, fall-back to regular title.
1519 (should
1520 (equal '("Headline")
1521 (org-test-with-parsed-data "* Headline"
1522 (org-export-get-alt-title
1523 (org-element-map tree 'headline 'identity info t)
1524 info)))))
1526 (ert-deftest test-org-export/get-tags ()
1527 "Test `org-export-get-tags' specifications."
1528 (let ((org-export-exclude-tags '("noexport"))
1529 (org-export-select-tags '("export")))
1530 ;; Standard test: tags which are not a select tag, an exclude tag,
1531 ;; or specified as optional argument shouldn't be ignored.
1532 (should
1533 (org-test-with-parsed-data "* Headline :tag:"
1534 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1535 info)))
1536 ;; Exclude tags are removed.
1537 (should-not
1538 (org-test-with-parsed-data "* Headline :noexport:"
1539 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1540 info)))
1541 ;; Select tags are removed.
1542 (should-not
1543 (org-test-with-parsed-data "* Headline :export:"
1544 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1545 info)))
1546 (should
1547 (equal
1548 '("tag")
1549 (org-test-with-parsed-data "* Headline :tag:export:"
1550 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1551 info))))
1552 ;; Tags provided in the optional argument are also ignored.
1553 (should-not
1554 (org-test-with-parsed-data "* Headline :ignore:"
1555 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1556 info '("ignore"))))
1557 ;; Allow tag inheritance.
1558 (should
1559 (equal
1560 '(("tag") ("tag"))
1561 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
1562 (org-element-map tree 'headline
1563 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
1564 ;; Tag inheritance checks FILETAGS keywords.
1565 (should
1566 (equal
1567 '(("a" "b" "tag"))
1568 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
1569 (org-element-map tree 'headline
1570 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
1572 (ert-deftest test-org-export/get-node-property ()
1573 "Test`org-export-get-node-property' specifications."
1574 ;; Standard test.
1575 (should
1576 (equal "value"
1577 (org-test-with-parsed-data "* Headline
1578 :PROPERTIES:
1579 :prop: value
1580 :END:"
1581 (org-export-get-node-property
1582 :PROP (org-element-map tree 'headline 'identity nil t)))))
1583 ;; Test inheritance.
1584 (should
1585 (equal "value"
1586 (org-test-with-parsed-data "* Parent
1587 :PROPERTIES:
1588 :prop: value
1589 :END:
1590 ** Headline
1591 Paragraph"
1592 (org-export-get-node-property
1593 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
1594 ;; Cannot return a value before the first headline.
1595 (should-not
1596 (org-test-with-parsed-data "Paragraph
1597 * Headline
1598 :PROPERTIES:
1599 :prop: value
1600 :END:"
1601 (org-export-get-node-property
1602 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
1604 (ert-deftest test-org-export/get-category ()
1605 "Test `org-export-get-category' specifications."
1606 ;; Standard test.
1607 (should
1608 (equal "value"
1609 (org-test-with-parsed-data "* Headline
1610 :PROPERTIES:
1611 :CATEGORY: value
1612 :END:"
1613 (org-export-get-category
1614 (org-element-map tree 'headline 'identity nil t) info))))
1615 ;; Test inheritance from a parent headline.
1616 (should
1617 (equal '("value" "value")
1618 (org-test-with-parsed-data "* Headline1
1619 :PROPERTIES:
1620 :CATEGORY: value
1621 :END:
1622 ** Headline2"
1623 (org-element-map tree 'headline
1624 (lambda (hl) (org-export-get-category hl info)) info))))
1625 ;; Test inheritance from #+CATEGORY keyword
1626 (should
1627 (equal "value"
1628 (org-test-with-parsed-data "#+CATEGORY: value
1629 * Headline"
1630 (org-export-get-category
1631 (org-element-map tree 'headline 'identity nil t) info))))
1632 ;; Test inheritance from file name.
1633 (should
1634 (equal "test"
1635 (org-test-with-parsed-data "* Headline"
1636 (let ((info (plist-put info :input-file "~/test.org")))
1637 (org-export-get-category
1638 (org-element-map tree 'headline 'identity nil t) info)))))
1639 ;; Fall-back value.
1640 (should
1641 (equal "???"
1642 (org-test-with-parsed-data "* Headline"
1643 (org-export-get-category
1644 (org-element-map tree 'headline 'identity nil t) info)))))
1646 (ert-deftest test-org-export/first-sibling-p ()
1647 "Test `org-export-first-sibling-p' specifications."
1648 ;; Standard test.
1649 (should
1650 (equal
1651 '(yes yes no)
1652 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1653 (org-element-map tree 'headline
1654 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1655 info))))
1656 ;; Ignore headlines not exported.
1657 (should
1658 (equal
1659 '(yes)
1660 (let ((org-export-exclude-tags '("ignore")))
1661 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
1662 (org-element-map tree 'headline
1663 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1664 info))))))
1666 (ert-deftest test-org-export/last-sibling-p ()
1667 "Test `org-export-last-sibling-p' specifications."
1668 ;; Standard test.
1669 (should
1670 (equal
1671 '(yes no yes)
1672 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1673 (org-element-map tree 'headline
1674 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1675 info))))
1676 ;; Ignore headlines not exported.
1677 (should
1678 (equal
1679 '(yes)
1680 (let ((org-export-exclude-tags '("ignore")))
1681 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
1682 (org-element-map tree 'headline
1683 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1684 info))))))
1686 (ert-deftest test-org-export/handle-inlinetasks ()
1687 "Test inlinetask export."
1688 ;; Inlinetask with an exclude tag.
1689 (when (featurep 'org-inlinetask)
1690 (should
1691 (equal
1693 (let ((org-inlinetask-min-level 3))
1694 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
1695 (org-export-as (org-test-default-backend)
1696 nil nil nil '(:exclude-tags ("noexp")))))))
1697 ;; Inlinetask with an include tag.
1698 (should
1699 (equal
1700 "* H2\n*** Inline :exp:\n"
1701 (let ((org-inlinetask-min-level 3)
1702 (org-tags-column 0))
1703 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
1704 (org-export-as (org-test-default-backend)
1705 nil nil nil '(:select-tags ("exp")))))))
1706 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
1707 (should
1708 (equal ""
1709 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
1710 (org-inlinetask-min-level 3))
1711 (org-test-with-temp-text "*** TODO Inline"
1712 (org-export-as (org-test-default-backend)
1713 nil nil nil '(:with-tasks nil))))))))
1717 ;;; Keywords
1719 (ert-deftest test-org-export/get-date ()
1720 "Test `org-export-get-date' specifications."
1721 ;; Return a properly formatted string when
1722 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
1723 ;; consists in a single timestamp.
1724 (should
1725 (equal "29 03 2012"
1726 (let ((org-export-date-timestamp-format "%d %m %Y"))
1727 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1728 (org-export-get-date info)))))
1729 ;; Return a secondary string otherwise.
1730 (should-not
1731 (stringp
1732 (let ((org-export-date-timestamp-format nil))
1733 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1734 (org-export-get-date info)))))
1735 (should
1736 (equal '("Date")
1737 (org-test-with-parsed-data "#+DATE: Date"
1738 (org-export-get-date info))))
1739 ;; Optional argument has precedence over
1740 ;; `org-export-date-timestamp-format'.
1741 (should
1742 (equal "29 03"
1743 (let ((org-export-date-timestamp-format "%d %m %Y"))
1744 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
1745 (org-export-get-date info "%d %m"))))))
1749 ;;; Links
1751 (ert-deftest test-org-export/get-coderef-format ()
1752 "Test `org-export-get-coderef-format' specifications."
1753 ;; A link without description returns "%s"
1754 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
1755 "%s"))
1756 ;; Return "%s" when path is matched within description.
1757 (should (equal (org-export-get-coderef-format "path" "desc (path)")
1758 "desc %s"))
1759 ;; Otherwise return description.
1760 (should (equal (org-export-get-coderef-format "path" "desc")
1761 "desc")))
1763 (ert-deftest test-org-export/inline-image-p ()
1764 "Test `org-export-inline-image-p' specifications."
1765 (should
1766 (org-export-inline-image-p
1767 (org-test-with-temp-text "[[#id]]"
1768 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
1769 '(("custom-id" . "id")))))
1771 (ert-deftest test-org-export/fuzzy-link ()
1772 "Test fuzzy links specifications."
1773 ;; Link to an headline should return headline's number.
1774 (org-test-with-parsed-data
1775 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
1776 (should
1777 ;; Note: Headline's number is in fact a list of numbers.
1778 (equal '(2)
1779 (org-element-map tree 'link
1780 (lambda (link)
1781 (org-export-get-ordinal
1782 (org-export-resolve-fuzzy-link link info) info)) info t))))
1783 ;; Link to a target in an item should return item's number.
1784 (org-test-with-parsed-data
1785 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
1786 (should
1787 ;; Note: Item's number is in fact a list of numbers.
1788 (equal '(1 2)
1789 (org-element-map tree 'link
1790 (lambda (link)
1791 (org-export-get-ordinal
1792 (org-export-resolve-fuzzy-link link info) info)) info t))))
1793 ;; Link to a target in a footnote should return footnote's number.
1794 (org-test-with-parsed-data "
1795 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
1796 (should
1797 (equal '(2 3)
1798 (org-element-map tree 'link
1799 (lambda (link)
1800 (org-export-get-ordinal
1801 (org-export-resolve-fuzzy-link link info) info)) info))))
1802 ;; Link to a named element should return sequence number of that
1803 ;; element.
1804 (org-test-with-parsed-data
1805 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
1806 (should
1807 (= 2
1808 (org-element-map tree 'link
1809 (lambda (link)
1810 (org-export-get-ordinal
1811 (org-export-resolve-fuzzy-link link info) info)) info t))))
1812 ;; Link to a target not within an item, a table, a footnote
1813 ;; reference or definition should return section number.
1814 (org-test-with-parsed-data
1815 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
1816 (should
1817 (equal '(2)
1818 (org-element-map tree 'link
1819 (lambda (link)
1820 (org-export-get-ordinal
1821 (org-export-resolve-fuzzy-link link info) info)) info t))))
1822 ;; Space are not significant when matching a fuzzy link.
1823 (should
1824 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
1825 (org-element-map tree 'link
1826 (lambda (link) (org-export-resolve-fuzzy-link link info))
1827 info t)))
1828 ;; Statistics cookies are ignored for headline match.
1829 (should
1830 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
1831 (org-element-map tree 'link
1832 (lambda (link) (org-export-resolve-fuzzy-link link info))
1833 info t)))
1834 (should
1835 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
1836 (org-element-map tree 'link
1837 (lambda (link) (org-export-resolve-fuzzy-link link info))
1838 info t)))
1839 ;; Headline match is position dependent.
1840 (should-not
1841 (apply
1843 (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
1844 (org-element-map tree 'link
1845 (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
1847 (ert-deftest test-org-export/resolve-coderef ()
1848 "Test `org-export-resolve-coderef' specifications."
1849 (let ((org-coderef-label-format "(ref:%s)"))
1850 ;; 1. A link to a "-n -k -r" block returns line number.
1851 (org-test-with-parsed-data
1852 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
1853 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1854 (org-test-with-parsed-data
1855 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1856 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1857 ;; 2. A link to a "-n -r" block returns line number.
1858 (org-test-with-parsed-data
1859 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
1860 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1861 (org-test-with-parsed-data
1862 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1863 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1864 ;; 3. A link to a "-n" block returns coderef.
1865 (org-test-with-parsed-data
1866 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1867 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1868 (org-test-with-parsed-data
1869 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
1870 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1871 ;; 4. A link to a "-r" block returns line number.
1872 (org-test-with-parsed-data
1873 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1874 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1875 (org-test-with-parsed-data
1876 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
1877 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1878 ;; 5. A link to a block without a switch returns coderef.
1879 (org-test-with-parsed-data
1880 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1881 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1882 (org-test-with-parsed-data
1883 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
1884 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1885 ;; 6. Correctly handle continued line numbers. A "+n" switch
1886 ;; should resume numbering from previous block with numbered
1887 ;; lines, ignoring blocks not numbering lines in the process.
1888 ;; A "-n" switch resets count.
1889 (org-test-with-parsed-data "
1890 #+BEGIN_EXAMPLE -n
1891 Text.
1892 #+END_EXAMPLE
1894 #+BEGIN_SRC emacs-lisp
1895 \(- 1 1)
1896 #+END_SRC
1898 #+BEGIN_SRC emacs-lisp +n -r
1899 \(+ 1 1) (ref:addition)
1900 #+END_SRC
1902 #+BEGIN_EXAMPLE -n -r
1903 Another text. (ref:text)
1904 #+END_EXAMPLE"
1905 (should (= (org-export-resolve-coderef "addition" info) 2))
1906 (should (= (org-export-resolve-coderef "text" info) 1)))
1907 ;; 7. Recognize coderef with user-specified syntax.
1908 (org-test-with-parsed-data
1909 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
1910 (should (equal (org-export-resolve-coderef "text" info) "text")))))
1912 (ert-deftest test-org-export/resolve-fuzzy-link ()
1913 "Test `org-export-resolve-fuzzy-link' specifications."
1914 ;; Match target objects.
1915 (should
1916 (org-test-with-parsed-data "<<target>> [[target]]"
1917 (org-export-resolve-fuzzy-link
1918 (org-element-map tree 'link 'identity info t) info)))
1919 ;; Match named elements.
1920 (should
1921 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
1922 (org-export-resolve-fuzzy-link
1923 (org-element-map tree 'link 'identity info t) info)))
1924 ;; Match exact headline's name.
1925 (should
1926 (org-test-with-parsed-data "* My headline\n[[My headline]]"
1927 (org-export-resolve-fuzzy-link
1928 (org-element-map tree 'link 'identity info t) info)))
1929 ;; Targets objects have priority over named elements and headline
1930 ;; titles.
1931 (should
1932 (eq 'target
1933 (org-test-with-parsed-data
1934 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
1935 (org-element-type
1936 (org-export-resolve-fuzzy-link
1937 (org-element-map tree 'link 'identity info t) info)))))
1938 ;; Named elements have priority over headline titles.
1939 (should
1940 (eq 'paragraph
1941 (org-test-with-parsed-data
1942 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
1943 (org-element-type
1944 (org-export-resolve-fuzzy-link
1945 (org-element-map tree 'link 'identity info t) info)))))
1946 ;; If link's path starts with a "*", only match headline titles,
1947 ;; though.
1948 (should
1949 (eq 'headline
1950 (org-test-with-parsed-data
1951 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
1952 (org-element-type
1953 (org-export-resolve-fuzzy-link
1954 (org-element-map tree 'link 'identity info t) info)))))
1955 ;; Return nil if no match.
1956 (should-not
1957 (org-test-with-parsed-data "[[target]]"
1958 (org-export-resolve-fuzzy-link
1959 (org-element-map tree 'link 'identity info t) info)))
1960 ;; Match fuzzy link even when before first headline.
1961 (should
1962 (eq 'headline
1963 (org-test-with-parsed-data "[[hl]]\n* hl"
1964 (org-element-type
1965 (org-export-resolve-fuzzy-link
1966 (org-element-map tree 'link 'identity info t) info))))))
1968 (ert-deftest test-org-export/resolve-id-link ()
1969 "Test `org-export-resolve-id-link' specifications."
1970 ;; 1. Regular test for custom-id link.
1971 (org-test-with-parsed-data "* Headline1
1972 :PROPERTIES:
1973 :CUSTOM_ID: test
1974 :END:
1975 * Headline 2
1976 \[[#test]]"
1977 (should
1978 (org-export-resolve-id-link
1979 (org-element-map tree 'link 'identity info t) info)))
1980 ;; 2. Failing test for custom-id link.
1981 (org-test-with-parsed-data "* Headline1
1982 :PROPERTIES:
1983 :CUSTOM_ID: test
1984 :END:
1985 * Headline 2
1986 \[[#no-match]]"
1987 (should-not
1988 (org-export-resolve-id-link
1989 (org-element-map tree 'link 'identity info t) info)))
1990 ;; 3. Test for internal id target.
1991 (org-test-with-parsed-data "* Headline1
1992 :PROPERTIES:
1993 :ID: aaaa
1994 :END:
1995 * Headline 2
1996 \[[id:aaaa]]"
1997 (should
1998 (org-export-resolve-id-link
1999 (org-element-map tree 'link 'identity info t) info)))
2000 ;; 4. Test for external id target.
2001 (org-test-with-parsed-data "[[id:aaaa]]"
2002 (should
2003 (org-export-resolve-id-link
2004 (org-element-map tree 'link 'identity info t)
2005 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
2007 (ert-deftest test-org-export/resolve-radio-link ()
2008 "Test `org-export-resolve-radio-link' specifications."
2009 ;; Standard test.
2010 (should
2011 (org-test-with-temp-text "<<<radio>>> radio"
2012 (org-update-radio-target-regexp)
2013 (let* ((tree (org-element-parse-buffer))
2014 (info `(:parse-tree ,tree)))
2015 (org-export-resolve-radio-link
2016 (org-element-map tree 'link 'identity info t)
2017 info))))
2018 ;; Radio targets are case-insensitive.
2019 (should
2020 (org-test-with-temp-text "<<<RADIO>>> radio"
2021 (org-update-radio-target-regexp)
2022 (let* ((tree (org-element-parse-buffer))
2023 (info `(:parse-tree ,tree)))
2024 (org-export-resolve-radio-link
2025 (org-element-map tree 'link 'identity info t)
2026 info))))
2027 ;; Radio target with objects.
2028 (should
2029 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
2030 (org-update-radio-target-regexp)
2031 (let* ((tree (org-element-parse-buffer))
2032 (info `(:parse-tree ,tree)))
2033 (org-export-resolve-radio-link
2034 (org-element-map tree 'link 'identity info t)
2035 info))))
2036 ;; Multiple radio targets.
2037 (should
2038 (equal '("radio1" "radio2")
2039 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
2040 (org-update-radio-target-regexp)
2041 (let* ((tree (org-element-parse-buffer))
2042 (info `(:parse-tree ,tree)))
2043 (org-element-map tree 'link
2044 (lambda (link)
2045 (org-element-property
2046 :value (org-export-resolve-radio-link link info)))
2047 info)))))
2048 ;; Radio target is whitespace insensitive.
2049 (should
2050 (org-test-with-temp-text "<<<a radio>>> a\n radio"
2051 (org-update-radio-target-regexp)
2052 (let* ((tree (org-element-parse-buffer))
2053 (info `(:parse-tree ,tree)))
2054 (org-element-map tree 'link
2055 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
2059 ;;; Src-block and example-block
2061 (ert-deftest test-org-export/unravel-code ()
2062 "Test `org-export-unravel-code' function."
2063 ;; Code without reference.
2064 (should
2065 (equal '("(+ 1 1)\n")
2066 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2067 (org-export-unravel-code (org-element-at-point)))))
2068 ;; Code with reference.
2069 (should
2070 (equal '("(+ 1 1)\n" (1 . "test"))
2071 (org-test-with-temp-text
2072 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
2073 (let ((org-coderef-label-format "(ref:%s)"))
2074 (org-export-unravel-code (org-element-at-point))))))
2075 ;; Code with user-defined reference.
2076 (should
2077 (equal
2078 '("(+ 1 1)\n" (1 . "test"))
2079 (org-test-with-temp-text
2080 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
2081 (let ((org-coderef-label-format "(ref:%s)"))
2082 (org-export-unravel-code (org-element-at-point))))))
2083 ;; Code references keys are relative to the current block.
2084 (should
2085 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
2086 (org-test-with-temp-text "
2087 #+BEGIN_EXAMPLE -n
2088 \(+ 1 1)
2089 #+END_EXAMPLE
2090 #+BEGIN_EXAMPLE +n
2091 \(+ 2 2)
2092 \(+ 3 3) (ref:one)
2093 #+END_EXAMPLE"
2094 (goto-line 5)
2095 (let ((org-coderef-label-format "(ref:%s)"))
2096 (org-export-unravel-code (org-element-at-point)))))))
2098 (ert-deftest test-org-export/format-code-default ()
2099 "Test `org-export-format-code-default' specifications."
2100 ;; Return the empty string when code is empty.
2101 (should
2102 (equal ""
2103 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
2104 (org-export-format-code-default
2105 (org-element-map tree 'src-block 'identity info t) info))))
2106 ;; Number lines, two whitespace characters before the actual loc.
2107 (should
2108 (equal "1 a\n2 b\n"
2109 (org-test-with-parsed-data
2110 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
2111 (org-export-format-code-default
2112 (org-element-map tree 'src-block 'identity info t) info))))
2113 ;; Put references 6 whitespace characters after the widest line,
2114 ;; wrapped within parenthesis.
2115 (should
2116 (equal "123 (a)\n1 (b)\n"
2117 (let ((org-coderef-label-format "(ref:%s)"))
2118 (org-test-with-parsed-data
2119 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
2120 (org-export-format-code-default
2121 (org-element-map tree 'src-block 'identity info t) info))))))
2125 ;;; Smart Quotes
2127 (ert-deftest test-org-export/activate-smart-quotes ()
2128 "Test `org-export-activate-smart-quotes' specifications."
2129 ;; Opening double quotes: standard test.
2130 (should
2131 (equal
2132 '("some &ldquo;paragraph")
2133 (let ((org-export-default-language "en"))
2134 (org-test-with-parsed-data "some \"paragraph"
2135 (org-element-map tree 'plain-text
2136 (lambda (s) (org-export-activate-smart-quotes s :html info))
2137 info)))))
2138 ;; Opening quotes: at the beginning of a paragraph.
2139 (should
2140 (equal
2141 '("&ldquo;begin")
2142 (let ((org-export-default-language "en"))
2143 (org-test-with-parsed-data "\"begin"
2144 (org-element-map tree 'plain-text
2145 (lambda (s) (org-export-activate-smart-quotes s :html info))
2146 info)))))
2147 ;; Opening quotes: after an object.
2148 (should
2149 (equal
2150 '("&ldquo;begin")
2151 (let ((org-export-default-language "en"))
2152 (org-test-with-parsed-data "=verb= \"begin"
2153 (org-element-map tree 'plain-text
2154 (lambda (s) (org-export-activate-smart-quotes s :html info))
2155 info)))))
2156 ;; Closing quotes: standard test.
2157 (should
2158 (equal
2159 '("some&rdquo; paragraph")
2160 (let ((org-export-default-language "en"))
2161 (org-test-with-parsed-data "some\" paragraph"
2162 (org-element-map tree 'plain-text
2163 (lambda (s) (org-export-activate-smart-quotes s :html info))
2164 info)))))
2165 ;; Closing quotes: at the end of a paragraph.
2166 (should
2167 (equal
2168 '("end&rdquo;")
2169 (let ((org-export-default-language "en"))
2170 (org-test-with-parsed-data "end\""
2171 (org-element-map tree 'plain-text
2172 (lambda (s) (org-export-activate-smart-quotes s :html info))
2173 info)))))
2174 ;; Apostrophe: standard test.
2175 (should
2176 (equal
2177 '("It shouldn&rsquo;t fail")
2178 (let ((org-export-default-language "en"))
2179 (org-test-with-parsed-data "It shouldn't fail"
2180 (org-element-map tree 'plain-text
2181 (lambda (s) (org-export-activate-smart-quotes s :html info))
2182 info)))))
2183 ;; Apostrophe: before an object.
2184 (should
2185 (equal
2186 '("a&rsquo;")
2187 (let ((org-export-default-language "en"))
2188 (org-test-with-parsed-data "a'=b="
2189 (org-element-map tree 'plain-text
2190 (lambda (s) (org-export-activate-smart-quotes s :html info))
2191 info)))))
2192 ;; Apostrophe: after an object.
2193 (should
2194 (equal
2195 '("&rsquo;s")
2196 (let ((org-export-default-language "en"))
2197 (org-test-with-parsed-data "=code='s"
2198 (org-element-map tree 'plain-text
2199 (lambda (s) (org-export-activate-smart-quotes s :html info))
2200 info)))))
2201 ;; Special case: isolated quotes.
2202 (should
2203 (equal '("&ldquo;" "&rdquo;")
2204 (let ((org-export-default-language "en"))
2205 (org-test-with-parsed-data "\"$x$\""
2206 (org-element-map tree 'plain-text
2207 (lambda (s) (org-export-activate-smart-quotes s :html info))
2208 info)))))
2209 ;; Smart quotes in secondary strings.
2210 (should
2211 (equal '("&ldquo;" "&rdquo;")
2212 (let ((org-export-default-language "en"))
2213 (org-test-with-parsed-data "* \"$x$\""
2214 (org-element-map tree 'plain-text
2215 (lambda (s) (org-export-activate-smart-quotes s :html info))
2216 info)))))
2217 ;; Smart quotes in document keywords.
2218 (should
2219 (equal '("&ldquo;" "&rdquo;")
2220 (let ((org-export-default-language "en"))
2221 (org-test-with-parsed-data "#+TITLE: \"$x$\""
2222 (org-element-map (plist-get info :title) 'plain-text
2223 (lambda (s) (org-export-activate-smart-quotes s :html info))
2224 info)))))
2225 ;; Smart quotes in parsed affiliated keywords.
2226 (should
2227 (equal '("&ldquo;" "&rdquo;" "Paragraph")
2228 (let ((org-export-default-language "en"))
2229 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
2230 (org-element-map tree 'plain-text
2231 (lambda (s) (org-export-activate-smart-quotes s :html info))
2232 info nil nil t))))))
2236 ;;; Tables
2238 (ert-deftest test-org-export/special-column ()
2239 "Test if the table's special column is properly recognized."
2240 ;; 1. First column is special if it contains only a special marking
2241 ;; characters or empty cells.
2242 (org-test-with-temp-text "
2243 | ! | 1 |
2244 | | 2 |"
2245 (should
2246 (org-export-table-has-special-column-p
2247 (org-element-map
2248 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2249 ;; 2. If the column contains anything else, it isn't special.
2250 (org-test-with-temp-text "
2251 | ! | 1 |
2252 | b | 2 |"
2253 (should-not
2254 (org-export-table-has-special-column-p
2255 (org-element-map
2256 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2257 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
2258 ;; and "!".
2259 (org-test-with-temp-text "
2260 | # | 1 |
2261 | ^ | 2 |
2262 | * | 3 |
2263 | _ | 4 |
2264 | / | 5 |
2265 | $ | 6 |
2266 | ! | 7 |"
2267 (should
2268 (org-export-table-has-special-column-p
2269 (org-element-map
2270 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2271 ;; 4. A first column with only empty cells isn't considered as
2272 ;; special.
2273 (org-test-with-temp-text "
2274 | | 1 |
2275 | | 2 |"
2276 (should-not
2277 (org-export-table-has-special-column-p
2278 (org-element-map
2279 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
2281 (ert-deftest test-org-export/table-row-is-special-p ()
2282 "Test `org-export-table-row-is-special-p' specifications."
2283 ;; 1. A row is special if it has a special marking character in the
2284 ;; special column.
2285 (org-test-with-parsed-data "| ! | 1 |"
2286 (should
2287 (org-export-table-row-is-special-p
2288 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2289 ;; 2. A row is special when its first field is "/"
2290 (org-test-with-parsed-data "
2291 | / | 1 |
2292 | a | b |"
2293 (should
2294 (org-export-table-row-is-special-p
2295 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2296 ;; 3. A row only containing alignment cookies is also considered as
2297 ;; special.
2298 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
2299 (should
2300 (org-export-table-row-is-special-p
2301 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2302 ;; 4. Everything else isn't considered as special.
2303 (org-test-with-parsed-data "| \alpha | | c |"
2304 (should-not
2305 (org-export-table-row-is-special-p
2306 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2307 ;; 5. Table's rules are never considered as special rows.
2308 (org-test-with-parsed-data "|---+---|"
2309 (should-not
2310 (org-export-table-row-is-special-p
2311 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
2313 (ert-deftest test-org-export/has-header-p ()
2314 "Test `org-export-table-has-header-p' specifications."
2315 ;; 1. With an header.
2316 (org-test-with-parsed-data "
2317 | a | b |
2318 |---+---|
2319 | c | d |"
2320 (should
2321 (org-export-table-has-header-p
2322 (org-element-map tree 'table 'identity info 'first-match)
2323 info)))
2324 ;; 2. Without an header.
2325 (org-test-with-parsed-data "
2326 | a | b |
2327 | c | d |"
2328 (should-not
2329 (org-export-table-has-header-p
2330 (org-element-map tree 'table 'identity info 'first-match)
2331 info)))
2332 ;; 3. Don't get fooled with starting and ending rules.
2333 (org-test-with-parsed-data "
2334 |---+---|
2335 | a | b |
2336 | c | d |
2337 |---+---|"
2338 (should-not
2339 (org-export-table-has-header-p
2340 (org-element-map tree 'table 'identity info 'first-match)
2341 info))))
2343 (ert-deftest test-org-export/table-row-group ()
2344 "Test `org-export-table-row-group' specifications."
2345 ;; 1. A rule creates a new group.
2346 (should
2347 (equal '(1 rule 2)
2348 (org-test-with-parsed-data "
2349 | a | b |
2350 |---+---|
2351 | 1 | 2 |"
2352 (org-element-map tree 'table-row
2353 (lambda (row)
2354 (if (eq (org-element-property :type row) 'rule) 'rule
2355 (org-export-table-row-group row info)))))))
2356 ;; 2. Special rows are ignored in count.
2357 (should
2358 (equal
2359 '(rule 1)
2360 (org-test-with-parsed-data "
2361 | / | < | > |
2362 |---|---+---|
2363 | | 1 | 2 |"
2364 (org-element-map tree 'table-row
2365 (lambda (row)
2366 (if (eq (org-element-property :type row) 'rule) 'rule
2367 (org-export-table-row-group row info)))
2368 info))))
2369 ;; 3. Double rules also are ignored in count.
2370 (should
2371 (equal '(1 rule rule 2)
2372 (org-test-with-parsed-data "
2373 | a | b |
2374 |---+---|
2375 |---+---|
2376 | 1 | 2 |"
2377 (org-element-map tree 'table-row
2378 (lambda (row)
2379 (if (eq (org-element-property :type row) 'rule) 'rule
2380 (org-export-table-row-group row info))))))))
2382 (ert-deftest test-org-export/table-row-number ()
2383 "Test `org-export-table-row-number' specifications."
2384 ;; Standard test. Number is 0-indexed.
2385 (should
2386 (equal '(0 1)
2387 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
2388 (org-element-map tree 'table-row
2389 (lambda (row) (org-export-table-row-number row info)) info))))
2390 ;; Number ignores separators.
2391 (should
2392 (equal '(0 1)
2393 (org-test-with-parsed-data "
2394 | a | b | c |
2395 |---+---+---|
2396 | d | e | f |"
2397 (org-element-map tree 'table-row
2398 (lambda (row) (org-export-table-row-number row info)) info))))
2399 ;; Number ignores special rows.
2400 (should
2401 (equal '(0 1)
2402 (org-test-with-parsed-data "
2403 | / | < | > |
2404 | | b | c |
2405 |---+-----+-----|
2406 | | <c> | <c> |
2407 | | e | f |"
2408 (org-element-map tree 'table-row
2409 (lambda (row) (org-export-table-row-number row info)) info)))))
2411 (ert-deftest test-org-export/table-cell-width ()
2412 "Test `org-export-table-cell-width' specifications."
2413 ;; 1. Width is primarily determined by width cookies. If no cookie
2414 ;; is found, cell's width is nil.
2415 (org-test-with-parsed-data "
2416 | / | <l> | <6> | <l7> |
2417 | | a | b | c |"
2418 (should
2419 (equal
2420 '(nil 6 7)
2421 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2422 (org-element-map tree 'table-cell 'identity info)))))
2423 ;; 2. The last width cookie has precedence.
2424 (org-test-with-parsed-data "
2425 | <6> |
2426 | <7> |
2427 | a |"
2428 (should
2429 (equal
2430 '(7)
2431 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2432 (org-element-map tree 'table-cell 'identity info)))))
2433 ;; 3. Valid width cookies must have a specific row.
2434 (org-test-with-parsed-data "| <6> | cell |"
2435 (should
2436 (equal
2437 '(nil nil)
2438 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2439 (org-element-map tree 'table-cell 'identity))))))
2441 (ert-deftest test-org-export/table-cell-alignment ()
2442 "Test `org-export-table-cell-alignment' specifications."
2443 ;; 1. Alignment is primarily determined by alignment cookies.
2444 (should
2445 (equal '(left center right)
2446 (let ((org-table-number-fraction 0.5)
2447 (org-table-number-regexp "^[0-9]+$"))
2448 (org-test-with-parsed-data "| <l> | <c> | <r> |"
2449 (mapcar (lambda (cell)
2450 (org-export-table-cell-alignment cell info))
2451 (org-element-map tree 'table-cell 'identity))))))
2452 ;; 2. The last alignment cookie has precedence.
2453 (should
2454 (equal '(right right right)
2455 (org-test-with-parsed-data "
2456 | <l8> |
2457 | cell |
2458 | <r9> |"
2459 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
2460 (org-element-map tree 'table-cell 'identity)))))
2461 ;; 3. If there's no cookie, cell's contents determine alignment.
2462 ;; A column mostly made of cells containing numbers will align
2463 ;; its cells to the right.
2464 (should
2465 (equal '(right right right)
2466 (let ((org-table-number-fraction 0.5)
2467 (org-table-number-regexp "^[0-9]+$"))
2468 (org-test-with-parsed-data "
2469 | 123 |
2470 | some text |
2471 | 12345 |"
2472 (mapcar (lambda (cell)
2473 (org-export-table-cell-alignment cell info))
2474 (org-element-map tree 'table-cell 'identity))))))
2475 ;; 4. Otherwise, they will be aligned to the left.
2476 (should
2477 (equal '(left left left)
2478 (org-test-with-parsed-data "
2479 | text |
2480 | some text |
2481 | \alpha |"
2482 (mapcar (lambda (cell)
2483 (org-export-table-cell-alignment cell info))
2484 (org-element-map tree 'table-cell 'identity info))))))
2486 (ert-deftest test-org-export/table-cell-borders ()
2487 "Test `org-export-table-cell-borders' specifications."
2488 ;; 1. Recognize various column groups indicators.
2489 (org-test-with-parsed-data "| / | < | > | <> |"
2490 (should
2491 (equal
2492 '((right bottom top) (left bottom top) (right bottom top)
2493 (right left bottom top))
2494 (mapcar (lambda (cell)
2495 (org-export-table-cell-borders cell info))
2496 (org-element-map tree 'table-cell 'identity)))))
2497 ;; 2. Accept shortcuts to define column groups.
2498 (org-test-with-parsed-data "| / | < | < |"
2499 (should
2500 (equal
2501 '((right bottom top) (right left bottom top) (left bottom top))
2502 (mapcar (lambda (cell)
2503 (org-export-table-cell-borders cell info))
2504 (org-element-map tree 'table-cell 'identity)))))
2505 ;; 3. A valid column groups row must start with a "/".
2506 (org-test-with-parsed-data "
2507 | | < |
2508 | a | b |"
2509 (should
2510 (equal '((top) (top) (bottom) (bottom))
2511 (mapcar (lambda (cell)
2512 (org-export-table-cell-borders cell info))
2513 (org-element-map tree 'table-cell 'identity)))))
2514 ;; 4. Take table rules into consideration.
2515 (org-test-with-parsed-data "
2516 | 1 |
2517 |---|
2518 | 2 |"
2519 (should
2520 (equal '((below top) (bottom above))
2521 (mapcar (lambda (cell)
2522 (org-export-table-cell-borders cell info))
2523 (org-element-map tree 'table-cell 'identity)))))
2524 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
2525 ;; (resp. `bottom' and `below') borders. Any special row is
2526 ;; ignored.
2527 (org-test-with-parsed-data "
2528 |---+----|
2529 | / | |
2530 | | 1 |
2531 |---+----|"
2532 (should
2533 (equal '((bottom below top above))
2534 (last
2535 (mapcar (lambda (cell)
2536 (org-export-table-cell-borders cell info))
2537 (org-element-map tree 'table-cell 'identity)))))))
2539 (ert-deftest test-org-export/table-dimensions ()
2540 "Test `org-export-table-dimensions' specifications."
2541 ;; 1. Standard test.
2542 (org-test-with-parsed-data "
2543 | 1 | 2 | 3 |
2544 | 4 | 5 | 6 |"
2545 (should
2546 (equal '(2 . 3)
2547 (org-export-table-dimensions
2548 (org-element-map tree 'table 'identity info 'first-match) info))))
2549 ;; 2. Ignore horizontal rules and special columns.
2550 (org-test-with-parsed-data "
2551 | / | < | > |
2552 | 1 | 2 | 3 |
2553 |---+---+---|
2554 | 4 | 5 | 6 |"
2555 (should
2556 (equal '(2 . 3)
2557 (org-export-table-dimensions
2558 (org-element-map tree 'table 'identity info 'first-match) info)))))
2560 (ert-deftest test-org-export/table-cell-address ()
2561 "Test `org-export-table-cell-address' specifications."
2562 ;; 1. Standard test: index is 0-based.
2563 (org-test-with-parsed-data "| a | b |"
2564 (should
2565 (equal '((0 . 0) (0 . 1))
2566 (org-element-map tree 'table-cell
2567 (lambda (cell) (org-export-table-cell-address cell info))
2568 info))))
2569 ;; 2. Special column isn't counted, nor are special rows.
2570 (org-test-with-parsed-data "
2571 | / | <> |
2572 | | c |"
2573 (should
2574 (equal '(0 . 0)
2575 (org-export-table-cell-address
2576 (car (last (org-element-map tree 'table-cell 'identity info)))
2577 info))))
2578 ;; 3. Tables rules do not count either.
2579 (org-test-with-parsed-data "
2580 | a |
2581 |---|
2582 | b |
2583 |---|
2584 | c |"
2585 (should
2586 (equal '(2 . 0)
2587 (org-export-table-cell-address
2588 (car (last (org-element-map tree 'table-cell 'identity info)))
2589 info))))
2590 ;; 4. Return nil for special cells.
2591 (org-test-with-parsed-data "| / | a |"
2592 (should-not
2593 (org-export-table-cell-address
2594 (org-element-map tree 'table-cell 'identity nil 'first-match)
2595 info))))
2597 (ert-deftest test-org-export/get-table-cell-at ()
2598 "Test `org-export-get-table-cell-at' specifications."
2599 ;; 1. Address ignores special columns, special rows and rules.
2600 (org-test-with-parsed-data "
2601 | / | <> |
2602 | | a |
2603 |---+----|
2604 | | b |"
2605 (should
2606 (equal '("b")
2607 (org-element-contents
2608 (org-export-get-table-cell-at
2609 '(1 . 0)
2610 (org-element-map tree 'table 'identity info 'first-match)
2611 info)))))
2612 ;; 2. Return value for a non-existent address is nil.
2613 (org-test-with-parsed-data "| a |"
2614 (should-not
2615 (org-export-get-table-cell-at
2616 '(2 . 2)
2617 (org-element-map tree 'table 'identity info 'first-match)
2618 info)))
2619 (org-test-with-parsed-data "| / |"
2620 (should-not
2621 (org-export-get-table-cell-at
2622 '(0 . 0)
2623 (org-element-map tree 'table 'identity info 'first-match)
2624 info))))
2626 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
2627 "Test `org-export-table-cell-starts-colgroup-p' specifications."
2628 ;; 1. A cell at a beginning of a row always starts a column group.
2629 (org-test-with-parsed-data "| a |"
2630 (should
2631 (org-export-table-cell-starts-colgroup-p
2632 (org-element-map tree 'table-cell 'identity info 'first-match)
2633 info)))
2634 ;; 2. Special column should be ignored when determining the
2635 ;; beginning of the row.
2636 (org-test-with-parsed-data "
2637 | / | |
2638 | | a |"
2639 (should
2640 (org-export-table-cell-starts-colgroup-p
2641 (org-element-map tree 'table-cell 'identity info 'first-match)
2642 info)))
2643 ;; 2. Explicit column groups.
2644 (org-test-with-parsed-data "
2645 | / | | < |
2646 | a | b | c |"
2647 (should
2648 (equal
2649 '(yes no yes)
2650 (org-element-map tree 'table-cell
2651 (lambda (cell)
2652 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
2653 info)))))
2655 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
2656 "Test `org-export-table-cell-ends-colgroup-p' specifications."
2657 ;; 1. A cell at the end of a row always ends a column group.
2658 (org-test-with-parsed-data "| a |"
2659 (should
2660 (org-export-table-cell-ends-colgroup-p
2661 (org-element-map tree 'table-cell 'identity info 'first-match)
2662 info)))
2663 ;; 2. Special column should be ignored when determining the
2664 ;; beginning of the row.
2665 (org-test-with-parsed-data "
2666 | / | |
2667 | | a |"
2668 (should
2669 (org-export-table-cell-ends-colgroup-p
2670 (org-element-map tree 'table-cell 'identity info 'first-match)
2671 info)))
2672 ;; 3. Explicit column groups.
2673 (org-test-with-parsed-data "
2674 | / | < | |
2675 | a | b | c |"
2676 (should
2677 (equal
2678 '(yes no yes)
2679 (org-element-map tree 'table-cell
2680 (lambda (cell)
2681 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
2682 info)))))
2684 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
2685 "Test `org-export-table-row-starts-rowgroup-p' specifications."
2686 ;; 1. A row at the beginning of a table always starts a row group.
2687 ;; So does a row following a table rule.
2688 (org-test-with-parsed-data "
2689 | a |
2690 |---|
2691 | b |"
2692 (should
2693 (equal
2694 '(yes no yes)
2695 (org-element-map tree 'table-row
2696 (lambda (row)
2697 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2698 info))))
2699 ;; 2. Special rows should be ignored when determining the beginning
2700 ;; of the row.
2701 (org-test-with-parsed-data "
2702 | / | < |
2703 | | a |
2704 |---+---|
2705 | / | < |
2706 | | b |"
2707 (should
2708 (equal
2709 '(yes no yes)
2710 (org-element-map tree 'table-row
2711 (lambda (row)
2712 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2713 info)))))
2715 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
2716 "Test `org-export-table-row-ends-rowgroup-p' specifications."
2717 ;; 1. A row at the end of a table always ends a row group. So does
2718 ;; a row preceding a table rule.
2719 (org-test-with-parsed-data "
2720 | a |
2721 |---|
2722 | b |"
2723 (should
2724 (equal
2725 '(yes no yes)
2726 (org-element-map tree 'table-row
2727 (lambda (row)
2728 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2729 info))))
2730 ;; 2. Special rows should be ignored when determining the beginning
2731 ;; of the row.
2732 (org-test-with-parsed-data "
2733 | | a |
2734 | / | < |
2735 |---+---|
2736 | | b |
2737 | / | < |"
2738 (should
2739 (equal
2740 '(yes no yes)
2741 (org-element-map tree 'table-row
2742 (lambda (row)
2743 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2744 info)))))
2746 (ert-deftest test-org-export/table-row-starts-header-p ()
2747 "Test `org-export-table-row-starts-header-p' specifications."
2748 ;; 1. Only the row starting the first row group starts the table
2749 ;; header.
2750 (org-test-with-parsed-data "
2751 | a |
2752 | b |
2753 |---|
2754 | c |"
2755 (should
2756 (equal
2757 '(yes no no no)
2758 (org-element-map tree 'table-row
2759 (lambda (row)
2760 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
2761 info))))
2762 ;; 2. A row cannot start an header if there's no header in the
2763 ;; table.
2764 (org-test-with-parsed-data "
2765 | a |
2766 |---|"
2767 (should-not
2768 (org-export-table-row-starts-header-p
2769 (org-element-map tree 'table-row 'identity info 'first-match)
2770 info))))
2772 (ert-deftest test-org-export/table-row-ends-header-p ()
2773 "Test `org-export-table-row-ends-header-p' specifications."
2774 ;; 1. Only the row starting the first row group starts the table
2775 ;; header.
2776 (org-test-with-parsed-data "
2777 | a |
2778 | b |
2779 |---|
2780 | c |"
2781 (should
2782 (equal
2783 '(no yes no no)
2784 (org-element-map tree 'table-row
2785 (lambda (row)
2786 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
2787 info))))
2788 ;; 2. A row cannot start an header if there's no header in the
2789 ;; table.
2790 (org-test-with-parsed-data "
2791 | a |
2792 |---|"
2793 (should-not
2794 (org-export-table-row-ends-header-p
2795 (org-element-map tree 'table-row 'identity info 'first-match)
2796 info))))
2800 ;;; Tables of Contents
2802 (ert-deftest test-org-export/collect-headlines ()
2803 "Test `org-export-collect-headlines' specifications."
2804 ;; Standard test.
2805 (should
2806 (= 2
2807 (length
2808 (org-test-with-parsed-data "* H1\n** H2"
2809 (org-export-collect-headlines info)))))
2810 ;; Do not collect headlines below optional argument.
2811 (should
2812 (= 1
2813 (length
2814 (org-test-with-parsed-data "* H1\n** H2"
2815 (org-export-collect-headlines info 1)))))
2816 ;; Never collect headlines below maximum headline level.
2817 (should
2818 (= 1
2819 (length
2820 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
2821 (org-export-collect-headlines info)))))
2822 (should
2823 (= 1
2824 (length
2825 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
2826 (org-export-collect-headlines info 2))))))
2830 ;;; Templates
2832 (ert-deftest test-org-export/inner-template ()
2833 "Test `inner-template' translator specifications."
2834 (should
2835 (equal "Success!"
2836 (org-test-with-temp-text "* Headline"
2837 (org-export-as
2838 (org-export-create-backend
2839 :transcoders
2840 '((inner-template . (lambda (contents info) "Success!"))
2841 (headline . (lambda (h c i) "Headline"))))))))
2842 ;; Inner template is applied even in a "body-only" export.
2843 (should
2844 (equal "Success!"
2845 (org-test-with-temp-text "* Headline"
2846 (org-export-as
2847 (org-export-create-backend
2848 :transcoders '((inner-template . (lambda (c i) "Success!"))
2849 (headline . (lambda (h c i) "Headline"))))
2850 nil nil 'body-only)))))
2852 (ert-deftest test-org-export/template ()
2853 "Test `template' translator specifications."
2854 (should
2855 (equal "Success!"
2856 (org-test-with-temp-text "* Headline"
2857 (org-export-as
2858 (org-export-create-backend
2859 :transcoders '((template . (lambda (contents info) "Success!"))
2860 (headline . (lambda (h c i) "Headline"))))))))
2861 ;; Template is not applied in a "body-only" export.
2862 (should-not
2863 (equal "Success!"
2864 (org-test-with-temp-text "* Headline"
2865 (org-export-as
2866 (org-export-create-backend
2867 :transcoders '((template . (lambda (contents info) "Success!"))
2868 (headline . (lambda (h c i) "Headline"))))
2869 nil nil 'body-only)))))
2873 ;;; Topology
2875 (ert-deftest test-org-export/get-next-element ()
2876 "Test `org-export-get-next-element' specifications."
2877 ;; Standard test.
2878 (should
2879 (equal "b"
2880 (org-test-with-parsed-data "* Headline\n*a* b"
2881 (org-export-get-next-element
2882 (org-element-map tree 'bold 'identity info t) info))))
2883 ;; Return nil when no previous element.
2884 (should-not
2885 (org-test-with-parsed-data "* Headline\na *b*"
2886 (org-export-get-next-element
2887 (org-element-map tree 'bold 'identity info t) info)))
2888 ;; Non-exportable elements are ignored.
2889 (should-not
2890 (let ((org-export-with-timestamps nil))
2891 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
2892 (org-export-get-next-element
2893 (org-element-map tree 'entity 'identity info t) info))))
2894 ;; Find next element in secondary strings.
2895 (should
2896 (eq 'verbatim
2897 (org-test-with-parsed-data "* a =verb="
2898 (org-element-type
2899 (org-export-get-next-element
2900 (org-element-map tree 'plain-text 'identity info t) info)))))
2901 (should
2902 (eq 'verbatim
2903 (org-test-with-parsed-data "* /italic/ =verb="
2904 (org-element-type
2905 (org-export-get-next-element
2906 (org-element-map tree 'italic 'identity info t) info)))))
2907 ;; Find next element in document keywords.
2908 (should
2909 (eq 'verbatim
2910 (org-test-with-parsed-data "#+TITLE: a =verb="
2911 (org-element-type
2912 (org-export-get-next-element
2913 (org-element-map
2914 (plist-get info :title) 'plain-text 'identity info t) info)))))
2915 ;; Find next element in parsed affiliated keywords.
2916 (should
2917 (eq 'verbatim
2918 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
2919 (org-element-type
2920 (org-export-get-next-element
2921 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2922 ;; With optional argument N, return a list containing all the
2923 ;; following elements.
2924 (should
2925 (equal
2926 '(bold code underline)
2927 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2928 (mapcar 'car
2929 (org-export-get-next-element
2930 (org-element-map tree 'italic 'identity info t) info t)))))
2931 ;; When N is a positive integer, return a list containing up to
2932 ;; N following elements.
2933 (should
2934 (equal
2935 '(bold code)
2936 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2937 (mapcar 'car
2938 (org-export-get-next-element
2939 (org-element-map tree 'italic 'identity info t) info 2))))))
2941 (ert-deftest test-org-export/get-previous-element ()
2942 "Test `org-export-get-previous-element' specifications."
2943 ;; Standard test.
2944 (should
2945 (equal "a "
2946 (org-test-with-parsed-data "* Headline\na *b*"
2947 (org-export-get-previous-element
2948 (org-element-map tree 'bold 'identity info t) info))))
2949 ;; Return nil when no previous element.
2950 (should-not
2951 (org-test-with-parsed-data "* Headline\n*a* b"
2952 (org-export-get-previous-element
2953 (org-element-map tree 'bold 'identity info t) info)))
2954 ;; Non-exportable elements are ignored.
2955 (should-not
2956 (let ((org-export-with-timestamps nil))
2957 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
2958 (org-export-get-previous-element
2959 (org-element-map tree 'entity 'identity info t) info))))
2960 ;; Find previous element in secondary strings.
2961 (should
2962 (eq 'verbatim
2963 (org-test-with-parsed-data "* =verb= a"
2964 (org-element-type
2965 (org-export-get-previous-element
2966 (org-element-map tree 'plain-text 'identity info t) info)))))
2967 (should
2968 (eq 'verbatim
2969 (org-test-with-parsed-data "* =verb= /italic/"
2970 (org-element-type
2971 (org-export-get-previous-element
2972 (org-element-map tree 'italic 'identity info t) info)))))
2973 ;; Find previous element in document keywords.
2974 (should
2975 (eq 'verbatim
2976 (org-test-with-parsed-data "#+TITLE: =verb= a"
2977 (org-element-type
2978 (org-export-get-previous-element
2979 (org-element-map
2980 (plist-get info :title) 'plain-text 'identity info t) info)))))
2981 ;; Find previous element in parsed affiliated keywords.
2982 (should
2983 (eq 'verbatim
2984 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
2985 (org-element-type
2986 (org-export-get-previous-element
2987 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2988 ;; With optional argument N, return a list containing up to
2989 ;; N previous elements.
2990 (should
2991 (equal '(underline italic bold)
2992 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
2993 (mapcar 'car
2994 (org-export-get-previous-element
2995 (org-element-map tree 'code 'identity info t) info t)))))
2996 ;; When N is a positive integer, return a list containing up to
2997 ;; N previous elements.
2998 (should
2999 (equal '(italic bold)
3000 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3001 (mapcar 'car
3002 (org-export-get-previous-element
3003 (org-element-map tree 'code 'identity info t) info 2))))))
3006 (provide 'test-ox)
3007 ;;; test-org-export.el end here