ox.el: Issue error for unresolved fuzzy link
[org-mode.git] / testing / lisp / test-ox.el
blob23d8341a01f1a2131c0ea0fde43c767a287c681d
1 ;;; test-ox.el --- Tests for ox.el
3 ;; Copyright (C) 2012-2015 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.
46 DATA is a string containing the data to be parsed. BODY is the
47 body to execute. Parse tree is available under the `tree'
48 variable, and communication channel under `info'."
49 (declare (debug (form body)) (indent 1))
50 `(org-test-with-temp-text ,data
51 (let* ((tree (org-element-parse-buffer))
52 (info (org-export-get-environment)))
53 (org-export-prune-tree tree info)
54 (org-export-remove-uninterpreted-data tree info)
55 (let ((info (org-combine-plists
56 info (org-export-collect-tree-properties tree info))))
57 ,@body))))
61 ;;; Internal Tests
63 (ert-deftest test-org-export/bind-keyword ()
64 "Test reading #+BIND: keywords."
65 ;; Test with `org-export-allow-bind-keywords' set to t.
66 (should
67 (org-test-with-temp-text "#+BIND: test-ox-var value"
68 (let ((org-export-allow-bind-keywords t))
69 (org-export-get-environment)
70 (eq test-ox-var 'value))))
71 ;; Test with `org-export-allow-bind-keywords' set to nil.
72 (should-not
73 (org-test-with-temp-text "#+BIND: test-ox-var value"
74 (let ((org-export-allow-bind-keywords nil))
75 (org-export-get-environment)
76 (boundp 'test-ox-var))))
77 ;; BIND keywords are case-insensitive.
78 (should
79 (org-test-with-temp-text "#+bind: test-ox-var value"
80 (let ((org-export-allow-bind-keywords t))
81 (org-export-get-environment)
82 (eq test-ox-var 'value))))
83 ;; Preserve order of BIND keywords.
84 (should
85 (org-test-with-temp-text "#+BIND: test-ox-var 1\n#+BIND: test-ox-var 2"
86 (let ((org-export-allow-bind-keywords t))
87 (org-export-get-environment)
88 (eq test-ox-var 2))))
89 ;; Read BIND keywords in setup files.
90 (should
91 (org-test-with-temp-text
92 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
93 (let ((org-export-allow-bind-keywords t))
94 (org-export-get-environment)
95 (eq variable 'value))))
96 ;; Verify that bound variables are seen during export.
97 (should
98 (equal "Yes\n"
99 (org-test-with-temp-text "#+BIND: test-ox-var value"
100 (let ((org-export-allow-bind-keywords t))
101 (org-export-as
102 (org-export-create-backend
103 :transcoders
104 '((section . (lambda (s c i)
105 (if (eq test-ox-var 'value) "Yes" "No")))))))))))
107 (ert-deftest test-org-export/parse-option-keyword ()
108 "Test reading all standard #+OPTIONS: items."
109 (should
110 (equal
111 (org-export--parse-option-keyword
112 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
113 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
114 stat:t title:t")
115 '(:headline-levels
116 1 :preserve-breaks t :section-numbers t :time-stamp-file t
117 :with-archived-trees t :with-author t :with-creator t :with-drawers t
118 :with-email t :with-emphasize t :with-entities t :with-fixed-width t
119 :with-footnotes t :with-inlinetasks nil :with-priority t
120 :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
121 :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
122 :with-title t :with-todo-keywords t)))
123 ;; Test some special values.
124 (should
125 (equal
126 (org-export--parse-option-keyword
127 "arch:headline d:(\"TEST\") ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
128 '( :section-numbers
130 :with-archived-trees headline :with-drawers ("TEST")
131 :with-sub-superscript {} :with-toc 1 :with-tags not-in-toc
132 :with-tasks todo :with-timestamps active))))
134 (ert-deftest test-org-export/get-inbuffer-options ()
135 "Test reading all standard export keywords."
136 ;; Properties should follow buffer order.
137 (should
138 (equal
139 (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
140 (org-export--get-inbuffer-options))
141 '(:language "fr" :creator "Me" :email "email")))
142 ;; Parse document keywords.
143 (should
144 (equal
145 (org-test-with-temp-text "#+AUTHOR: Me"
146 (org-export--get-inbuffer-options))
147 '(:author ("Me"))))
148 ;; Test `space' behaviour.
149 (should
150 (equal
151 (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
152 (org-export--get-inbuffer-options))
153 '(:title ("Some title with spaces"))))
154 ;; Test `newline' behaviour.
155 (should
156 (equal
157 (org-test-with-temp-text "#+DESCRIPTION: With\n#+DESCRIPTION: two lines"
158 (org-export--get-inbuffer-options))
159 '(:description "With\ntwo lines")))
160 ;; Test `split' behaviour.
161 (should
162 (equal
163 (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
164 (org-export--get-inbuffer-options))
165 '(:select-tags ("a" "b"))))
166 ;; Options set through SETUPFILE.
167 (should
168 (equal
169 (org-test-with-temp-text
170 (format "#+DESCRIPTION: l1
171 #+LANGUAGE: es
172 #+SELECT_TAGS: a
173 #+TITLE: a
174 #+SETUPFILE: \"%s/examples/setupfile.org\"
175 #+DESCRIPTION: l3
176 #+LANGUAGE: fr
177 #+SELECT_TAGS: c
178 #+TITLE: c"
179 org-test-dir)
180 (org-export--get-inbuffer-options))
181 '(:description "l1\nl2\nl3":language "fr" :select-tags ("a" "b" "c")
182 :title ("a b c"))))
183 ;; More than one property can refer to the same buffer keyword.
184 (should
185 (equal '(:k2 "value" :k1 "value")
186 (let ((backend (org-export-create-backend
187 :options '((:k1 "KEYWORD")
188 (:k2 "KEYWORD")))))
189 (org-test-with-temp-text "#+KEYWORD: value"
190 (org-export--get-inbuffer-options backend))))))
192 (ert-deftest test-org-export/get-subtree-options ()
193 "Test setting options from headline's properties."
194 ;; EXPORT_TITLE.
195 (org-test-with-temp-text "#+TITLE: Title
196 * Headline
197 :PROPERTIES:
198 :EXPORT_TITLE: Subtree Title
199 :END:
200 Paragraph"
201 (forward-line)
202 (should (equal (plist-get (org-export-get-environment nil t) :title)
203 '("Subtree Title"))))
204 :title
205 '("subtree-title")
206 ;; EXPORT_OPTIONS.
207 (org-test-with-temp-text "#+OPTIONS: H:1
208 * Headline
209 :PROPERTIES:
210 :EXPORT_OPTIONS: H:2
211 :END:
212 Paragraph"
213 (forward-line)
214 (should
215 (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
216 ;; EXPORT_DATE.
217 (org-test-with-temp-text "#+DATE: today
218 * Headline
219 :PROPERTIES:
220 :EXPORT_DATE: 29-03-2012
221 :END:
222 Paragraph"
223 (forward-line)
224 (should (equal (plist-get (org-export-get-environment nil t) :date)
225 '("29-03-2012"))))
226 ;; Properties with `split' behaviour are stored as a list of
227 ;; strings.
228 (should
229 (equal '("a" "b")
230 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
231 * Headline
232 :PROPERTIES:
233 :EXPORT_EXCLUDE_TAGS: a b
234 :END:
235 Paragraph"
236 (progn
237 (forward-line)
238 (plist-get (org-export-get-environment nil t) :exclude-tags)))))
239 ;; Handle :PROPERTY+: syntax.
240 (should
241 (equal '("a" "b")
242 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
243 * Headline
244 :PROPERTIES:
245 :EXPORT_EXCLUDE_TAGS: a
246 :EXPORT_EXCLUDE_TAGS+: b
247 :END:
248 Paragraph"
249 (progn
250 (forward-line)
251 (plist-get (org-export-get-environment nil t) :exclude-tags)))))
252 ;; Export properties are case-insensitive.
253 (org-test-with-temp-text "* Headline
254 :PROPERTIES:
255 :EXPORT_Date: 29-03-2012
256 :END:
257 Paragraph"
258 (should (equal (plist-get (org-export-get-environment nil t) :date)
259 '("29-03-2012"))))
260 ;; Still grab correct options when section above is empty.
261 (should
262 (equal '("H1")
263 (org-test-with-temp-text "* H1\n** H11\n** H12"
264 (progn (forward-line 2)
265 (plist-get (org-export-get-environment nil t) :title))))))
267 (ert-deftest test-org-export/set-title ()
268 "Test title setting."
269 ;; Without TITLE keyword.
270 (should
271 (equal
273 (let (org-export-filter-body-functions
274 org-export-filter-final-output-functions)
275 (org-test-with-temp-text "Test"
276 (org-export-as
277 (org-export-create-backend
278 :transcoders
279 '((template . (lambda (text info)
280 (org-element-interpret-data
281 (plist-get info :title)))))))))))
282 ;; With a blank TITLE keyword.
283 (should
284 (equal
286 (let (org-export-filter-body-functions
287 org-export-filter-final-output-functions)
288 (org-test-with-temp-text "#+TITLE:\nTest"
289 (org-export-as
290 (org-export-create-backend
291 :transcoders
292 '((template . (lambda (text info)
293 (org-element-interpret-data
294 (plist-get info :title)))))))))))
295 ;; With a non-empty TITLE keyword.
296 (should
297 (equal
298 "Title"
299 (org-test-with-temp-text "#+TITLE: Title\nTest"
300 (org-export-as
301 (org-export-create-backend
302 :transcoders
303 '((template . (lambda (text info)
304 (org-element-interpret-data
305 (plist-get info :title))))))))))
306 ;; When exporting a subtree, its heading becomes the headline of the
307 ;; document...
308 (should
309 (equal
310 "Headline"
311 (org-test-with-temp-text "* Headline\nBody"
312 (org-export-as
313 (org-export-create-backend
314 :transcoders
315 '((template . (lambda (text info)
316 (org-element-interpret-data
317 (plist-get info :title))))))
318 'subtree))))
319 ;; ... unless there is an EXPORT_TITLE property at the root of the
320 ;; subtree.
321 (should
322 (equal
324 (org-test-with-temp-text
325 "* A\n :PROPERTIES:\n :EXPORT_TITLE: B\n :END:\nBody"
326 (org-export-as
327 (org-export-create-backend
328 :transcoders
329 '((template . (lambda (text info)
330 (org-element-interpret-data
331 (plist-get info :title))))))
332 'subtree)))))
334 (ert-deftest test-org-export/handle-options ()
335 "Test if export options have an impact on output."
336 ;; Test exclude tags for headlines and inlinetasks.
337 (should
338 (equal ""
339 (let (org-export-filter-body-functions
340 org-export-filter-final-output-functions)
341 (org-test-with-temp-text "* Head1 :noexp:"
342 (org-export-as (org-test-default-backend)
343 nil nil nil '(:exclude-tags ("noexp")))))))
344 ;; Test include tags for headlines and inlinetasks.
345 (should
346 (equal "* H2\n** Sub :exp:\n*** Sub Sub\n"
347 (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
348 (let ((org-tags-column 0))
349 (org-export-as (org-test-default-backend)
350 nil nil nil '(:select-tags ("exp")))))))
351 ;; If there is an include tag, ignore the section before the first
352 ;; headline, if any.
353 (should
354 (equal "* H1 :exp:\nBody\n"
355 (org-test-with-temp-text "First section\n* H1 :exp:\nBody"
356 (let ((org-tags-column 0))
357 (org-export-as (org-test-default-backend)
358 nil nil nil '(:select-tags ("exp")))))))
359 (should-not
360 (equal "* H1 :exp:\n"
361 (org-test-with-temp-text "* H1 :exp:\nBody"
362 (let ((org-tags-column 0))
363 (org-export-as (org-test-default-backend)
364 nil nil nil '(:select-tags ("exp")))))))
365 ;; Test mixing include tags and exclude tags.
366 (should
367 (string-match
368 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
369 (org-test-with-temp-text "
370 * Head1 :export:
371 ** Sub-Head1 :noexport:
372 ** Sub-Head2
373 * Head2 :noexport:
374 ** Sub-Head1 :export:"
375 (org-export-as (org-test-default-backend) nil nil nil
376 '(:select-tags ("export") :exclude-tags ("noexport"))))))
377 ;; Ignore tasks.
378 (should
379 (equal ""
380 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
381 org-export-filter-body-functions
382 org-export-filter-final-output-functions)
383 (org-test-with-temp-text "* TODO Head1"
384 (org-export-as (org-test-default-backend)
385 nil nil nil '(:with-tasks nil))))))
386 (should
387 (equal "* TODO Head1\n"
388 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
389 (org-test-with-temp-text "* TODO Head1"
390 (org-export-as (org-test-default-backend)
391 nil nil nil '(:with-tasks t))))))
392 ;; Archived tree.
393 (should
394 (equal ""
395 (let (org-export-filter-body-functions
396 org-export-filter-final-output-functions)
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 nil)))))))
401 (should
402 (string-match
403 "\\* Head1[ \t]+:archive:"
404 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
405 (let ((org-archive-tag "archive"))
406 (org-export-as (org-test-default-backend) nil nil nil
407 '(:with-archived-trees headline))))))
408 (should
409 (string-match
410 "\\`\\* Head1[ \t]+:archive:\n\\'"
411 (org-test-with-temp-text "* Head1 :archive:"
412 (let ((org-archive-tag "archive"))
413 (org-export-as (org-test-default-backend)
414 nil nil nil '(:with-archived-trees t))))))
415 ;; Clocks.
416 (should
417 (string-match "CLOCK: \\[2012-04-29 .* 10:45\\]"
418 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
419 (org-export-as (org-test-default-backend)
420 nil nil nil '(:with-clocks t)))))
421 (should
422 (equal ""
423 (let (org-export-filter-body-functions
424 org-export-filter-final-output-functions)
425 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
426 (org-export-as (org-test-default-backend)
427 nil nil nil '(:with-clocks nil))))))
428 ;; Drawers.
429 (should
430 (equal ""
431 (let (org-export-filter-body-functions
432 org-export-filter-final-output-functions)
433 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
434 (org-export-as (org-test-default-backend)
435 nil nil nil '(:with-drawers nil))))))
436 (should
437 (equal ":TEST:\ncontents\n:END:\n"
438 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
439 (org-export-as (org-test-default-backend)
440 nil nil nil '(:with-drawers t)))))
441 (should
442 (equal ":FOO:\nkeep\n:END:\n"
443 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
444 (org-export-as (org-test-default-backend)
445 nil nil nil '(:with-drawers ("FOO"))))))
446 (should
447 (equal ":FOO:\nkeep\n:END:\n"
448 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
449 (org-export-as (org-test-default-backend)
450 nil nil nil '(:with-drawers (not "BAR"))))))
451 ;; Fixed-width.
452 (should
453 (equal ": A\n"
454 (org-test-with-temp-text ": A"
455 (org-export-as (org-test-default-backend) nil nil nil
456 '(:with-fixed-width t)))))
457 (should
458 (equal ""
459 (let (org-export-filter-body-functions
460 org-export-filter-final-output-functions)
461 (org-test-with-temp-text ": A"
462 (org-export-as (org-test-default-backend) nil nil nil
463 '(:with-fixed-width nil))))))
464 ;; Footnotes.
465 (should
466 (equal "Footnote?"
467 (let ((org-footnote-section nil))
468 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
469 (org-trim (org-export-as (org-test-default-backend)
470 nil nil nil '(:with-footnotes nil)))))))
471 (should
472 (equal "Footnote?[fn:1]\n\n[fn:1] Def"
473 (let ((org-footnote-section nil))
474 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
475 (org-trim (org-export-as (org-test-default-backend)
476 nil nil nil '(:with-footnotes t)))))))
477 ;; Inlinetasks.
478 (when (featurep 'org-inlinetask)
479 (should
480 (equal
482 (let ((org-inlinetask-min-level 15)
483 org-export-filter-body-functions
484 org-export-filter-final-output-functions)
485 (org-test-with-temp-text "*************** Task"
486 (org-export-as (org-test-default-backend)
487 nil nil nil '(:with-inlinetasks nil))))))
488 (should
489 (equal
491 (let ((org-inlinetask-min-level 15)
492 org-export-filter-body-functions
493 org-export-filter-final-output-functions)
494 (org-test-with-temp-text
495 "*************** Task\nContents\n*************** END"
496 (org-export-as (org-test-default-backend)
497 nil nil nil '(:with-inlinetasks nil)))))))
498 ;; Plannings.
499 (should
500 (string-match
501 "* H\nCLOSED: \\[2012-04-29 .* 10:45\\]"
502 (let ((org-closed-string "CLOSED:"))
503 (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
504 (org-export-as (org-test-default-backend)
505 nil nil nil '(:with-planning t))))))
506 (should
507 (equal "* H\n"
508 (let ((org-closed-string "CLOSED:"))
509 (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
510 (org-export-as (org-test-default-backend)
511 nil nil nil '(:with-planning nil))))))
512 ;; Property Drawers.
513 (should
514 (equal "* H1\n"
515 (org-test-with-temp-text
516 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
517 (org-export-as (org-test-default-backend)
518 nil nil nil '(:with-properties nil)))))
519 (should
520 (equal "* H1\n:PROPERTIES:\n:PROP: value\n:END:\n"
521 (org-test-with-temp-text
522 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
523 (org-export-as (org-test-default-backend)
524 nil nil nil '(:with-properties t)))))
525 (should
526 (equal "* H1\n:PROPERTIES:\n:B: 2\n:END:\n"
527 (org-test-with-temp-text
528 "* H1\n :PROPERTIES:\n :A: 1\n :B: 2\n:END:"
529 (org-export-as (org-test-default-backend)
530 nil nil nil '(:with-properties ("B"))))))
531 ;; Statistics cookies.
532 (should
533 (equal ""
534 (let (org-export-filter-body-functions
535 org-export-filter-final-output-functions)
536 (org-trim
537 (org-test-with-temp-text "[0/0]"
538 (org-export-as (org-test-default-backend)
539 nil nil nil '(:with-statistics-cookies nil)))))))
540 ;; Tables.
541 (should
542 (equal "| A |\n"
543 (org-test-with-temp-text "| A |"
544 (org-export-as (org-test-default-backend) nil nil nil
545 '(:with-tables t)))))
546 (should
547 (equal ""
548 (let (org-export-filter-body-functions
549 org-export-filter-final-output-functions)
550 (org-test-with-temp-text "| A |"
551 (org-export-as (org-test-default-backend) nil nil nil
552 '(:with-tables nil)))))))
554 (ert-deftest test-org-export/with-timestamps ()
555 "Test `org-export-with-timestamps' specifications."
556 ;; t value.
557 (should
558 (string-match
559 "\\[2012-04-29 .*? 10:45\\]<2012-04-29 .*? 10:45>"
560 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
561 (org-export-as (org-test-default-backend)
562 nil nil nil '(:with-timestamps t)))))
563 ;; nil value.
564 (should
565 (equal
567 (let (org-export-filter-body-functions
568 org-export-filter-final-output-functions)
569 (org-trim
570 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
571 (org-export-as (org-test-default-backend)
572 nil nil nil '(:with-timestamps nil)))))))
573 ;; `active' value.
574 (should
575 (string-match
576 "<2012-03-29 .*?>\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
577 (org-test-with-temp-text
578 "<2012-03-29 Thu>[2012-03-29 Thu]
580 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
581 (org-export-as (org-test-default-backend)
582 nil nil nil '(:with-timestamps active)))))
583 ;; `inactive' value.
584 (should
585 (string-match
586 "\\[2012-03-29 .*?\\]\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
587 (org-test-with-temp-text
588 "<2012-03-29 Thu>[2012-03-29 Thu]
590 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
591 (org-export-as (org-test-default-backend)
592 nil nil nil '(:with-timestamps inactive))))))
594 (ert-deftest test-org-export/comment-tree ()
595 "Test if export process ignores commented trees."
596 (should
597 (equal ""
598 (let (org-export-filter-body-functions
599 org-export-filter-final-output-functions)
600 (org-test-with-temp-text "* COMMENT Head1"
601 (org-export-as (org-test-default-backend)))))))
603 (ert-deftest test-org-export/uninterpreted ()
604 "Test handling of uninterpreted elements."
605 ;; Entities.
606 (should
607 (equal "dummy\n"
608 (org-test-with-temp-text "\\alpha"
609 (org-export-as
610 (org-export-create-backend
611 :transcoders '((entity . (lambda (e c i) "dummy"))
612 (paragraph . (lambda (p c i) c))
613 (section . (lambda (s c i) c))))
614 nil nil nil '(:with-entities t)))))
615 (should
616 (equal "\\alpha\n"
617 (org-test-with-temp-text "\\alpha"
618 (org-export-as
619 (org-export-create-backend
620 :transcoders '((entity . (lambda (e c i) "dummy"))
621 (paragraph . (lambda (p c i) c))
622 (section . (lambda (s c i) c))))
623 nil nil nil '(:with-entities nil)))))
624 ;; Emphasis.
625 (should
626 (equal "dummy\n"
627 (org-test-with-temp-text "*bold*"
628 (org-export-as
629 (org-export-create-backend
630 :transcoders '((bold . (lambda (b c i) "dummy"))
631 (paragraph . (lambda (p c i) c))
632 (section . (lambda (s c i) c))))
633 nil nil nil '(:with-emphasize t)))))
634 (should
635 (equal "*bold*\n"
636 (org-test-with-temp-text "*bold*"
637 (org-export-as
638 (org-export-create-backend
639 :transcoders '((bold . (lambda (b c i) "dummy"))
640 (paragraph . (lambda (p c i) c))
641 (section . (lambda (s c i) c))))
642 nil nil nil '(:with-emphasize nil)))))
643 ;; LaTeX environment.
644 (should
645 (equal "dummy\n"
646 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
647 (org-export-as
648 (org-export-create-backend
649 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
650 (section . (lambda (s c i) c))))
651 nil nil nil '(:with-latex t)))))
652 (should
653 (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
654 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
655 (org-export-as
656 (org-export-create-backend
657 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
658 (section . (lambda (s c i) c))))
659 nil nil nil '(:with-latex verbatim)))))
660 ;; LaTeX fragment.
661 (should
662 (equal "dummy\n"
663 (org-test-with-temp-text "$1$"
664 (org-export-as
665 (org-export-create-backend
666 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
667 (paragraph . (lambda (p c i) c))
668 (section . (lambda (s c i) c))))
669 nil nil nil '(:with-latex t)))))
670 (should
671 (equal "$1$\n"
672 (org-test-with-temp-text "$1$"
673 (org-export-as
674 (org-export-create-backend
675 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
676 (paragraph . (lambda (p c i) c))
677 (section . (lambda (s c i) c))))
678 nil nil nil '(:with-latex verbatim)))))
679 ;; Sub/superscript.
680 (should
681 (equal "adummy\n"
682 (org-test-with-temp-text "a_b"
683 (org-export-as
684 (org-export-create-backend
685 :transcoders '((subscript . (lambda (s c i) "dummy"))
686 (paragraph . (lambda (p c i) c))
687 (section . (lambda (s c i) c))))
688 nil nil nil '(:with-sub-superscript t)))))
689 (should
690 (equal "a_b\n"
691 (org-test-with-temp-text "a_b"
692 (org-export-as
693 (org-export-create-backend
694 :transcoders '((subscript . (lambda (s c i) "dummy"))
695 (paragraph . (lambda (p c i) c))
696 (section . (lambda (s c i) c))))
697 nil nil nil '(:with-sub-superscript nil)))))
698 (should
699 (equal "a_b\n"
700 (org-test-with-temp-text "a_b"
701 (org-export-as
702 (org-export-create-backend
703 :transcoders '((subscript . (lambda (s c i) "dummy"))
704 (paragraph . (lambda (p c i) c))
705 (section . (lambda (s c i) c))))
706 nil nil nil '(:with-sub-superscript {})))))
707 (should
708 (equal "adummy\n"
709 (org-test-with-temp-text "a_{b}"
710 (org-export-as
711 (org-export-create-backend
712 :transcoders '((subscript . (lambda (s c i) "dummy"))
713 (paragraph . (lambda (p c i) c))
714 (section . (lambda (s c i) c))))
715 nil nil nil '(:with-sub-superscript {})))))
716 ;; Also handle uninterpreted objects in title.
717 (should
718 (equal "a_b"
719 (org-test-with-temp-text "#+TITLE: a_b"
720 (org-export-as
721 (org-export-create-backend
722 :transcoders
723 '((subscript . (lambda (s c i) "dummy"))
724 (template . (lambda (c i) (org-export-data
725 (plist-get i :title) i)))
726 (section . (lambda (s c i) c))))
727 nil nil nil '(:with-sub-superscript nil)))))
728 ;; Special case: multiples uninterpreted objects in a row.
729 (should
730 (equal "a_b_c_d\n"
731 (org-test-with-temp-text "a_b_c_d"
732 (org-export-as
733 (org-export-create-backend
734 :transcoders '((subscript . (lambda (s c i) "dummy"))
735 (paragraph . (lambda (p c i) c))
736 (section . (lambda (s c i) c))))
737 nil nil nil '(:with-sub-superscript {}))))))
739 (ert-deftest test-org-export/export-scope ()
740 "Test all export scopes."
741 (org-test-with-temp-text "
742 * Head1
743 ** Head2
744 text
745 *** Head3"
746 ;; Subtree.
747 (forward-line 3)
748 (should (equal (org-export-as (org-test-default-backend) 'subtree)
749 "text\n*** Head3\n"))
750 ;; Visible.
751 (goto-char (point-min))
752 (forward-line)
753 (org-cycle)
754 (should (equal (org-export-as (org-test-default-backend) nil 'visible)
755 "* Head1\n"))
756 ;; Region.
757 (goto-char (point-min))
758 (forward-line 3)
759 (transient-mark-mode 1)
760 (push-mark (point) t t)
761 (goto-char (point-at-eol))
762 (should (equal (org-export-as (org-test-default-backend)) "text\n")))
763 ;; Subtree with a code block calling another block outside.
764 (let ((org-export-babel-evaluate t))
765 (should
766 (equal ": 3\n"
767 (org-test-with-temp-text "
768 * Head1
769 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
770 <<test>>
771 #+END_SRC
772 * Head2
773 #+NAME: test
774 #+BEGIN_SRC emacs-lisp
775 \(+ 1 2)
776 #+END_SRC"
777 (forward-line 1)
778 (org-export-as (org-test-default-backend) 'subtree)))))
779 ;; Body only.
780 (let ((backend (org-test-default-backend)))
781 (setf (org-export-backend-transcoders backend)
782 (cons '(template . (lambda (body i)
783 (format "BEGIN\n%sEND" body)))
784 (org-export-backend-transcoders backend)))
785 (org-test-with-temp-text "Text"
786 (should (equal (org-export-as backend nil nil 'body-only)
787 "Text\n"))
788 (should (equal (org-export-as backend) "BEGIN\nText\nEND")))))
790 (ert-deftest test-org-export/output-file-name ()
791 "Test `org-export-output-file-name' specifications."
792 ;; Export from a file: name is built from original file name.
793 (should
794 (org-test-with-temp-text-in-file "Test"
795 (equal (concat (file-name-as-directory ".")
796 (file-name-nondirectory
797 (file-name-sans-extension (buffer-file-name))))
798 (file-name-sans-extension (org-export-output-file-name ".ext")))))
799 ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
800 (should
801 (org-test-with-temp-text-in-file
802 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
803 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
804 ;; From a buffer not associated to a file, too.
805 (should
806 (org-test-with-temp-text
807 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
808 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
809 ;; When provided name is absolute, preserve it.
810 (should
811 (org-test-with-temp-text
812 (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
813 (expand-file-name "test"))
814 (file-name-absolute-p (org-export-output-file-name ".ext" t))))
815 ;; When PUB-DIR argument is provided, use it.
816 (should
817 (org-test-with-temp-text-in-file "Test"
818 (equal (file-name-directory
819 (org-export-output-file-name ".ext" nil "dir/"))
820 "dir/")))
821 ;; When returned name would overwrite original file, add EXTENSION
822 ;; another time.
823 (should
824 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
825 (equal (org-export-output-file-name ".org") "./normal.org.org"))))
827 (ert-deftest test-org-export/expand-include ()
828 "Test file inclusion in an Org buffer."
829 ;; Error when file isn't specified.
830 (should-error
831 (org-test-with-temp-text "#+INCLUDE: dummy.org"
832 (org-export-expand-include-keyword)))
833 ;; Full insertion with recursive inclusion.
834 (should
835 (equal
836 (with-temp-buffer
837 (insert-file
838 (expand-file-name "examples/include.org" org-test-dir))
839 (replace-regexp-in-string
840 (regexp-quote "#+INCLUDE: \"include2.org\"")
841 "Success!" (buffer-string)))
842 (org-test-with-temp-text
843 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
844 (org-export-expand-include-keyword)
845 (buffer-string))))
846 ;; Localized insertion.
847 (org-test-with-temp-text
848 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
849 org-test-dir)
850 (org-export-expand-include-keyword)
851 (should (equal (buffer-string)
852 "Small Org file with an include keyword.\n")))
853 ;; Insertion with constraints on headlines level.
854 (should
855 (equal
856 "* Top heading\n** Heading\nbody\n"
857 (org-test-with-temp-text
858 (format
859 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\""
860 org-test-dir)
861 (org-export-expand-include-keyword)
862 (buffer-string))))
863 (should
864 (equal
865 "* Top heading\n* Heading\nbody\n"
866 (org-test-with-temp-text
867 (format
868 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\" :minlevel 1"
869 org-test-dir)
870 (org-export-expand-include-keyword)
871 (buffer-string))))
872 ;; Inclusion within an example block.
873 (should
874 (equal
875 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
876 (org-test-with-temp-text
877 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
878 org-test-dir)
879 (org-export-expand-include-keyword)
880 (buffer-string))))
881 ;; Inclusion within a src-block.
882 (should
883 (equal
884 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
885 (org-test-with-temp-text
886 (format
887 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
888 org-test-dir)
889 (org-export-expand-include-keyword)
890 (buffer-string))))
891 ;; Inclusion within an html export-block.
892 (should
893 (equal
894 "#+BEGIN_HTML\n<p>HTML!</p>\n#+END_HTML\n"
895 (org-test-with-temp-text
896 (format
897 "#+INCLUDE: \"%s/examples/include.html\" HTML"
898 org-test-dir)
899 (org-export-expand-include-keyword)
900 (buffer-string))))
901 ;; Inclusion within an center paragraph
902 (should
903 (equal
904 "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
905 (org-test-with-temp-text
906 (format
907 "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
908 org-test-dir)
909 (org-export-expand-include-keyword)
910 (buffer-string))))
911 ;; Footnotes labels are local to each included file.
912 (should
913 (= 6
914 (length
915 (delete-dups
916 (let ((contents "
917 Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote].
918 \[fn:1] Footnote 1
919 \[fn:test] Footnote \"test\""))
920 (org-test-with-temp-text-in-file contents
921 (let ((file1 (buffer-file-name)))
922 (org-test-with-temp-text-in-file contents
923 (let ((file2 (buffer-file-name)))
924 (org-test-with-temp-text
925 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
926 file1 file2)
927 (org-export-expand-include-keyword)
928 (org-element-map (org-element-parse-buffer)
929 'footnote-reference
930 (lambda (ref)
931 (org-element-property :label ref)))))))))))))
932 ;; Footnotes labels are not local to each include keyword.
933 (should
934 (= 4
935 (length
936 (delete-dups
937 (let ((contents "
938 Footnotes[fn:1], [fn:test], [2] and [fn:inline:anonymous footnote].
939 \[fn:1] Footnote 1
940 \[2] Footnote 2
941 \[fn:test] Footnote \"test\""))
942 (org-test-with-temp-text-in-file contents
943 (let ((file (buffer-file-name)))
944 (org-test-with-temp-text
945 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
946 (org-export-expand-include-keyword)
947 (org-element-map (org-element-parse-buffer)
948 'footnote-reference
949 (lambda (ref) (org-element-property :label ref)))))))))))
950 ;; Footnotes are supported by :lines-like elements and unnecessary
951 ;; footnotes are dropped.
952 (should
953 (= 4
954 (length
955 (delete-dups
956 (let ((contents "
957 * foo
958 Footnotes[fn:1]
959 * bar
960 Footnotes[fn:2], foot[fn:test], digit only[3], and [fn:inline:anonymous footnote]
962 \[fn:1] Footnote 1
963 \[fn:2] Footnote 1
964 * Footnotes
965 \[fn:test] Footnote \"test\"
966 \[3] Footnote 3
968 (org-test-with-temp-text-in-file contents
969 (let ((file (buffer-file-name)))
970 (org-test-with-temp-text
971 (format "#+INCLUDE: \"%s::*bar\"
972 " file)
973 (org-export-expand-include-keyword)
974 (org-element-map (org-element-parse-buffer)
975 'footnote-definition
976 (lambda (ref) (org-element-property :label ref)))))))))))
977 ;; If only-contents is non-nil only include contents of element.
978 (should
979 (equal
980 "body\n"
981 (org-test-with-temp-text
982 (concat
983 (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" " org-test-dir)
984 ":only-contents t")
985 (org-export-expand-include-keyword)
986 (buffer-string))))
987 ;; Headings can be included via CUSTOM_ID.
988 (should
989 (org-test-with-temp-text
990 (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
991 (org-export-expand-include-keyword)
992 (goto-char (point-min))
993 (looking-at "* Another heading")))
994 ;; Named objects can be included.
995 (should
996 (equal
997 "| 1 |\n"
998 (org-test-with-temp-text
999 (format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t" org-test-dir)
1000 (org-export-expand-include-keyword)
1001 (buffer-string))))
1002 ;; Including non-existing elements should result in an error.
1003 (should-error
1004 (org-test-with-temp-text
1005 (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\"" org-test-dir)
1006 (org-export-expand-include-keyword)))
1007 ;; Lines work relatively to an included element.
1008 (should
1009 (equal
1010 "2\n3\n"
1011 (org-test-with-temp-text
1012 (format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t :lines \"2-3\"" org-test-dir)
1013 (org-export-expand-include-keyword)
1014 (buffer-string))))
1015 ;; Properties should be dropped from headlines.
1016 (should
1017 (equal
1018 (org-test-with-temp-text
1019 (format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t" org-test-dir)
1020 (org-export-expand-include-keyword)
1021 (buffer-string))
1022 (org-test-with-temp-text
1023 (format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
1024 (org-export-expand-include-keyword)
1025 (buffer-string))))
1026 ;; Properties should be dropped, drawers should not be.
1027 (should
1028 (equal
1029 ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
1030 (org-test-with-temp-text
1031 (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t" org-test-dir)
1032 (org-export-expand-include-keyword)
1033 (buffer-string))))
1034 ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
1035 (should
1036 (org-every (lambda (level) (zerop (1- level)))
1037 (org-test-with-temp-text
1038 (concat
1039 (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n" org-test-dir)
1040 (format "#+INCLUDE: \"%s/examples/include.org::*Heading\"" org-test-dir))
1041 (org-export-expand-include-keyword)
1042 (org-element-map (org-element-parse-buffer) 'headline
1043 (lambda (head) (org-element-property :level head))))))
1044 ;; INCLUDE does not insert induced :minlevel for src-blocks.
1045 (should-not
1046 (equal
1047 (org-test-with-temp-text
1048 (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp" org-test-dir)
1049 (org-export-expand-include-keyword)
1050 (buffer-string))
1051 (org-test-with-temp-text
1052 (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1" org-test-dir)
1053 (org-export-expand-include-keyword)
1054 (buffer-string))))
1055 ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
1056 (should
1057 (org-test-with-temp-text-in-file
1058 (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
1059 (narrow-to-region (point) (point-max))
1060 (org-export-expand-include-keyword)
1061 (eq 1 (org-current-level))))
1062 ;; If :minlevel is present do not alter it.
1063 (should
1064 (org-test-with-temp-text
1065 (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 3" org-test-dir)
1066 (narrow-to-region (point) (point-max))
1067 (org-export-expand-include-keyword)
1068 (eq 3 (org-current-level)))))
1070 (ert-deftest test-org-export/expand-macro ()
1071 "Test macro expansion in an Org buffer."
1072 ;; Standard macro expansion.
1073 (should
1074 (equal "#+MACRO: macro1 value\nvalue\n"
1075 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
1076 (org-export-as (org-test-default-backend)))))
1077 ;; Expand specific macros.
1078 (should
1079 (equal "me 2012-03-29 me@here Title\n"
1080 (org-test-with-temp-text
1082 #+TITLE: Title
1083 #+DATE: 2012-03-29
1084 #+AUTHOR: me
1085 #+EMAIL: me@here
1086 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
1087 (let ((output (org-export-as (org-test-default-backend))))
1088 (substring output (string-match ".*\n\\'" output))))))
1089 ;; Expand specific macros when property contained a regular macro
1090 ;; already.
1091 (should
1092 (equal "value\n"
1093 (org-test-with-temp-text "
1094 #+MACRO: macro1 value
1095 #+TITLE: {{{macro1}}}
1096 {{{title}}}"
1097 (let ((output (org-export-as (org-test-default-backend))))
1098 (substring output (string-match ".*\n\\'" output))))))
1099 ;; Expand macros with templates in included files.
1100 (should
1101 (equal "success\n"
1102 (org-test-with-temp-text
1103 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
1104 {{{included-macro}}}" org-test-dir)
1105 (let ((output (org-export-as (org-test-default-backend))))
1106 (substring output (string-match ".*\n\\'" output))))))
1107 ;; Date macro takes a optional formatting argument
1108 (should
1109 (equal "09-02-15\n"
1110 (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02-09>"
1111 (org-export-as (org-test-default-backend)))))
1112 ;; Only single timestamps are formatted
1113 (should
1114 (equal "<2015-02x-09>\n"
1115 (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02x-09>"
1116 (org-export-as (org-test-default-backend))))))
1118 (ert-deftest test-org-export/before-processing-hook ()
1119 "Test `org-export-before-processing-hook'."
1120 (should
1121 (equal
1122 "#+MACRO: mac val\nTest\n"
1123 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
1124 (let ((org-export-before-processing-hook
1125 '((lambda (backend)
1126 (while (re-search-forward "{{{" nil t)
1127 (let ((object (org-element-context)))
1128 (when (eq (org-element-type object) 'macro)
1129 (delete-region
1130 (org-element-property :begin object)
1131 (org-element-property :end object)))))))))
1132 (org-export-as (org-test-default-backend)))))))
1134 (ert-deftest test-org-export/before-parsing-hook ()
1135 "Test `org-export-before-parsing-hook'."
1136 (should
1137 (equal "Body 1\nBody 2\n"
1138 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
1139 (let ((org-export-before-parsing-hook
1140 '((lambda (backend)
1141 (goto-char (point-min))
1142 (while (re-search-forward org-outline-regexp-bol nil t)
1143 (delete-region
1144 (point-at-bol) (progn (forward-line) (point))))))))
1145 (org-export-as (org-test-default-backend)))))))
1149 ;;; Affiliated Keywords
1151 (ert-deftest test-org-export/read-attribute ()
1152 "Test `org-export-read-attribute' specifications."
1153 ;; Standard test.
1154 (should
1155 (equal
1156 (org-export-read-attribute
1157 :attr_html
1158 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
1159 (org-element-at-point)))
1160 '(:a "1" :b "2")))
1161 ;; Return nil on empty attribute.
1162 (should-not
1163 (org-export-read-attribute
1164 :attr_html
1165 (org-test-with-temp-text "Paragraph" (org-element-at-point))))
1166 ;; Return nil on "nil" string.
1167 (should
1168 (equal '(:a nil :b nil)
1169 (org-export-read-attribute
1170 :attr_html
1171 (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
1172 (org-element-at-point)))))
1173 ;; Return nil on empty string.
1174 (should
1175 (equal '(:a nil :b nil)
1176 (org-export-read-attribute
1177 :attr_html
1178 (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
1179 (org-element-at-point)))))
1180 ;; Return empty string when value is "".
1181 (should
1182 (equal '(:a "")
1183 (org-export-read-attribute
1184 :attr_html
1185 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
1186 (org-element-at-point)))))
1187 ;; Return \"\" when value is """".
1188 (should
1189 (equal '(:a "\"\"")
1190 (org-export-read-attribute
1191 :attr_html
1192 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
1193 (org-element-at-point)))))
1194 ;; Ignore text before first property.
1195 (should-not
1196 (member "ignore"
1197 (org-export-read-attribute
1198 :attr_html
1199 (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
1200 (org-element-at-point))))))
1202 (ert-deftest test-org-export/get-caption ()
1203 "Test `org-export-get-caption' specifications."
1204 ;; Without optional argument, return long caption
1205 (should
1206 (equal
1207 '("l")
1208 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1209 (org-export-get-caption (org-element-at-point)))))
1210 ;; With optional argument, return short caption.
1211 (should
1212 (equal
1213 '("s")
1214 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1215 (org-export-get-caption (org-element-at-point) t))))
1216 ;; Multiple lines are separated by white spaces.
1217 (should
1218 (equal
1219 '("a" " " "b")
1220 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
1221 (org-export-get-caption (org-element-at-point))))))
1225 ;;; Back-End Tools
1227 (ert-deftest test-org-export/define-backend ()
1228 "Test back-end definition and accessors."
1229 ;; Translate table.
1230 (should
1231 (equal '((headline . my-headline-test))
1232 (let (org-export--registered-backends)
1233 (org-export-define-backend 'test '((headline . my-headline-test)))
1234 (org-export-get-all-transcoders 'test))))
1235 ;; Filters.
1236 (should
1237 (equal '((:filter-headline . my-filter))
1238 (let (org-export--registered-backends)
1239 (org-export-define-backend 'test
1240 '((headline . my-headline-test))
1241 :filters-alist '((:filter-headline . my-filter)))
1242 (org-export-backend-filters (org-export-get-backend 'test)))))
1243 ;; Options.
1244 (should
1245 (equal '((:prop value))
1246 (let (org-export--registered-backends)
1247 (org-export-define-backend 'test
1248 '((headline . my-headline-test))
1249 :options-alist '((:prop value)))
1250 (org-export-backend-options (org-export-get-backend 'test)))))
1251 ;; Menu.
1252 (should
1253 (equal '(?k "Test Export" test)
1254 (let (org-export--registered-backends)
1255 (org-export-define-backend 'test
1256 '((headline . my-headline-test))
1257 :menu-entry '(?k "Test Export" test))
1258 (org-export-backend-menu (org-export-get-backend 'test)))))
1259 ;; Export Blocks.
1260 (should
1261 (equal '(("TEST" . org-element-export-block-parser))
1262 (let (org-export--registered-backends org-element-block-name-alist)
1263 (org-export-define-backend 'test
1264 '((headline . my-headline-test))
1265 :export-block '("test"))
1266 org-element-block-name-alist))))
1268 (ert-deftest test-org-export/define-derived-backend ()
1269 "Test `org-export-define-derived-backend' specifications."
1270 ;; Error when parent back-end is not defined.
1271 (should-error
1272 (let (org-export--registered-backends)
1273 (org-export-define-derived-backend 'test 'parent)))
1274 ;; Append translation table to parent's.
1275 (should
1276 (equal '((:headline . test) (:headline . parent))
1277 (let (org-export--registered-backends)
1278 (org-export-define-backend 'parent '((:headline . parent)))
1279 (org-export-define-derived-backend 'test 'parent
1280 :translate-alist '((:headline . test)))
1281 (org-export-get-all-transcoders 'test))))
1282 ;; Options defined in the new back have priority over those defined
1283 ;; in parent.
1284 (should
1285 (eq 'test
1286 (let (org-export--registered-backends)
1287 (org-export-define-backend 'parent
1288 '((:headline . parent))
1289 :options-alist '((:a nil nil 'parent)))
1290 (org-export-define-derived-backend 'test 'parent
1291 :options-alist '((:a nil nil 'test)))
1292 (plist-get (org-export--get-global-options
1293 (org-export-get-backend 'test))
1294 :a)))))
1296 (ert-deftest test-org-export/derived-backend-p ()
1297 "Test `org-export-derived-backend-p' specifications."
1298 ;; Non-nil with direct match.
1299 (should
1300 (let (org-export--registered-backends)
1301 (org-export-define-backend 'test '((headline . test)))
1302 (org-export-derived-backend-p 'test 'test)))
1303 (should
1304 (let (org-export--registered-backends)
1305 (org-export-define-backend 'test '((headline . test)))
1306 (org-export-define-derived-backend 'test2 'test)
1307 (org-export-derived-backend-p 'test2 'test2)))
1308 ;; Non-nil with a direct parent.
1309 (should
1310 (let (org-export--registered-backends)
1311 (org-export-define-backend 'test '((headline . test)))
1312 (org-export-define-derived-backend 'test2 'test)
1313 (org-export-derived-backend-p 'test2 'test)))
1314 ;; Non-nil with an indirect parent.
1315 (should
1316 (let (org-export--registered-backends)
1317 (org-export-define-backend 'test '((headline . test)))
1318 (org-export-define-derived-backend 'test2 'test)
1319 (org-export-define-derived-backend 'test3 'test2)
1320 (org-export-derived-backend-p 'test3 'test)))
1321 ;; Nil otherwise.
1322 (should-not
1323 (let (org-export--registered-backends)
1324 (org-export-define-backend 'test '((headline . test)))
1325 (org-export-define-backend 'test2 '((headline . test2)))
1326 (org-export-derived-backend-p 'test2 'test)))
1327 (should-not
1328 (let (org-export--registered-backends)
1329 (org-export-define-backend 'test '((headline . test)))
1330 (org-export-define-backend 'test2 '((headline . test2)))
1331 (org-export-define-derived-backend 'test3 'test2)
1332 (org-export-derived-backend-p 'test3 'test))))
1334 (ert-deftest test-org-export/get-all-transcoders ()
1335 "Test `org-export-get-all-transcoders' specifications."
1336 ;; Return nil when back-end cannot be found.
1337 (should-not (org-export-get-all-transcoders nil))
1338 ;; Same as `org-export-transcoders' if no parent.
1339 (should
1340 (equal '((headline . ignore))
1341 (org-export-get-all-transcoders
1342 (org-export-create-backend
1343 :transcoders '((headline . ignore))))))
1344 ;; But inherit from all ancestors whenever possible.
1345 (should
1346 (equal '((section . ignore) (headline . ignore))
1347 (let (org-export--registered-backends)
1348 (org-export-define-backend 'b1 '((headline . ignore)))
1349 (org-export-get-all-transcoders
1350 (org-export-create-backend
1351 :parent 'b1 :transcoders '((section . ignore)))))))
1352 (should
1353 (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
1354 (let (org-export--registered-backends)
1355 (org-export-define-backend 'b1 '((headline . ignore)))
1356 (org-export-define-derived-backend 'b2 'b1
1357 :translate-alist '((section . ignore)))
1358 (org-export-get-all-transcoders
1359 (org-export-create-backend
1360 :parent 'b2 :transcoders '((paragraph . ignore)))))))
1361 ;; Back-end transcoders overrule inherited ones.
1362 (should
1363 (eq 'b
1364 (let (org-export--registered-backends)
1365 (org-export-define-backend 'b1 '((headline . a)))
1366 (cdr (assq 'headline
1367 (org-export-get-all-transcoders
1368 (org-export-create-backend
1369 :parent 'b1 :transcoders '((headline . b))))))))))
1371 (ert-deftest test-org-export/get-all-options ()
1372 "Test `org-export-get-all-options' specifications."
1373 ;; Return nil when back-end cannot be found.
1374 (should-not (org-export-get-all-options nil))
1375 ;; Same as `org-export-options' if no parent.
1376 (should
1377 (equal '((headline . ignore))
1378 (org-export-get-all-options
1379 (org-export-create-backend
1380 :options '((headline . ignore))))))
1381 ;; But inherit from all ancestors whenever possible.
1382 (should
1383 (equal '((:key2 value2) (:key1 value1))
1384 (let (org-export--registered-backends)
1385 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1386 (org-export-get-all-options
1387 (org-export-create-backend
1388 :parent 'b1 :options '((:key2 value2)))))))
1389 (should
1390 (equal '((:key3 value3) (:key2 value2) (:key1 value1))
1391 (let (org-export--registered-backends)
1392 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1393 (org-export-define-derived-backend 'b2 'b1
1394 :options-alist '((:key2 value2)))
1395 (org-export-get-all-options
1396 (org-export-create-backend
1397 :parent 'b2 :options '((:key3 value3)))))))
1398 ;; Back-end options overrule inherited ones.
1399 (should
1400 (eq 'b
1401 (let (org-export--registered-backends)
1402 (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
1403 (cdr (assq :key1
1404 (org-export-get-all-options
1405 (org-export-create-backend
1406 :parent 'b1 :options '((:key1 . b))))))))))
1408 (ert-deftest test-org-export/get-all-filters ()
1409 "Test `org-export-get-all-filters' specifications."
1410 ;; Return nil when back-end cannot be found.
1411 (should-not (org-export-get-all-filters nil))
1412 ;; Same as `org-export-filters' if no parent.
1413 (should
1414 (equal '((:filter-headline . ignore))
1415 (org-export-get-all-filters
1416 (org-export-create-backend
1417 :filters '((:filter-headline . ignore))))))
1418 ;; But inherit from all ancestors whenever possible.
1419 (should
1420 (equal '((:filter-section . ignore) (:filter-headline . ignore))
1421 (let (org-export--registered-backends)
1422 (org-export-define-backend 'b1
1423 nil :filters-alist '((:filter-headline . ignore)))
1424 (org-export-get-all-filters
1425 (org-export-create-backend
1426 :parent 'b1 :filters '((:filter-section . ignore)))))))
1427 (should
1428 (equal '((:filter-paragraph . ignore)
1429 (:filter-section . ignore)
1430 (:filter-headline . ignore))
1431 (let (org-export--registered-backends)
1432 (org-export-define-backend 'b1
1433 nil :filters-alist '((:filter-headline . ignore)))
1434 (org-export-define-derived-backend 'b2 'b1
1435 :filters-alist '((:filter-section . ignore)))
1436 (org-export-get-all-filters
1437 (org-export-create-backend
1438 :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
1439 ;; Back-end filters overrule inherited ones.
1440 (should
1441 (eq 'b
1442 (let (org-export--registered-backends)
1443 (org-export-define-backend 'b1 '((:filter-headline . a)))
1444 (cdr (assq :filter-headline
1445 (org-export-get-all-filters
1446 (org-export-create-backend
1447 :parent 'b1 :filters '((:filter-headline . b))))))))))
1449 (ert-deftest test-org-export/with-backend ()
1450 "Test `org-export-with-backend' definition."
1451 ;; Error when calling an undefined back-end
1452 (should-error (org-export-with-backend nil "Test"))
1453 ;; Error when called back-end doesn't have an appropriate
1454 ;; transcoder.
1455 (should-error
1456 (org-export-with-backend
1457 (org-export-create-backend :transcoders '((headline . ignore)))
1458 "Test"))
1459 ;; Otherwise, export using correct transcoder
1460 (should
1461 (equal "Success"
1462 (let (org-export--registered-backends)
1463 (org-export-define-backend 'test
1464 '((plain-text . (lambda (text contents info) "Failure"))))
1465 (org-export-define-backend 'test2
1466 '((plain-text . (lambda (text contents info) "Success"))))
1467 (org-export-with-backend 'test2 "Test"))))
1468 ;; Provide correct back-end if transcoder needs to use recursive
1469 ;; calls anyway.
1470 (should
1471 (equal "Success\n"
1472 (let ((test-back-end
1473 (org-export-create-backend
1474 :transcoders
1475 '((headline . (lambda (headline contents info)
1476 (org-export-data
1477 (org-element-property :title headline)
1478 info)))
1479 (plain-text . (lambda (text info) "Success"))))))
1480 (org-export-string-as
1481 "* Test"
1482 (org-export-create-backend
1483 :transcoders
1484 '((headline . (lambda (headline contents info)
1485 (org-export-with-backend
1486 test-back-end headline contents info))))))))))
1488 (ert-deftest test-org-export/data-with-backend ()
1489 "Test `org-export-data-with-backend' specifications."
1490 ;; Error when calling an undefined back-end.
1491 (should-error (org-export-data-with-backend nil "nil" nil))
1492 ;; Otherwise, export data recursively, using correct back-end.
1493 (should
1494 (equal
1495 "Success!"
1496 (org-export-data-with-backend
1497 '(bold nil "Test")
1498 (org-export-create-backend
1499 :transcoders
1500 '((plain-text . (lambda (text info) "Success"))
1501 (bold . (lambda (bold contents info) (concat contents "!")))))
1502 '(:with-emphasize t)))))
1506 ;;; Export Snippets
1508 (ert-deftest test-org-export/export-snippet ()
1509 "Test export snippets transcoding."
1510 ;; Standard test.
1511 (org-test-with-temp-text "@@test:A@@@@t:B@@"
1512 (let ((backend (org-test-default-backend)))
1513 (setf (org-export-backend-name backend) 'test)
1514 (setf (org-export-backend-transcoders backend)
1515 (cons (cons 'export-snippet
1516 (lambda (snippet contents info)
1517 (when (eq (org-export-snippet-backend snippet) 'test)
1518 (org-element-property :value snippet))))
1519 (org-export-backend-transcoders backend)))
1520 (let ((org-export-snippet-translation-alist nil))
1521 (should (equal (org-export-as backend) "A\n")))
1522 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
1523 (should (equal (org-export-as backend) "AB\n")))))
1524 ;; Ignored export snippets do not remove any blank.
1525 (should
1526 (equal "begin end\n"
1527 (org-test-with-parsed-data "begin @@test:A@@ end"
1528 (org-export-data-with-backend
1529 tree
1530 (org-export-create-backend
1531 :transcoders
1532 '((paragraph . (lambda (paragraph contents info) contents))
1533 (section . (lambda (section contents info) contents))))
1534 info)))))
1538 ;;; Footnotes
1540 (ert-deftest test-org-export/footnote-first-reference-p ()
1541 "Test `org-export-footnote-first-reference-p' specifications."
1542 (should
1543 (equal
1544 '(t nil)
1545 (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
1546 (let (result)
1547 (org-export-as
1548 (org-export-create-backend
1549 :transcoders
1550 `(,(cons 'footnote-reference
1551 (lambda (f c i)
1552 (push (org-export-footnote-first-reference-p f info)
1553 result)
1554 ""))
1555 (section . (lambda (s c i) c))
1556 (paragraph . (lambda (p c i) c))))
1557 nil nil nil '(:with-footnotes t))
1558 (nreverse result)))))
1559 ;; Limit check to DATA, when non-nil.
1560 (should
1561 (equal
1562 '(nil t)
1563 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
1564 (let (result)
1565 (org-element-map tree 'footnote-reference
1566 (lambda (ref)
1567 (push
1568 (org-export-footnote-first-reference-p
1569 ref info (org-element-map tree 'headline #'identity info t))
1570 result))
1571 info)
1572 (nreverse result)))))
1573 (should
1574 (equal
1575 '(t nil)
1576 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
1577 (let (result)
1578 (org-element-map tree 'footnote-reference
1579 (lambda (ref)
1580 (push (org-export-footnote-first-reference-p ref info) result))
1581 info)
1582 (nreverse result)))))
1583 ;; If optional argument BODY-FIRST is non-nil, first find footnote
1584 ;; in the main body of the document. Otherwise, enter footnote
1585 ;; definitions when they are encountered.
1586 (should
1587 (equal
1588 '(t nil)
1589 (org-test-with-temp-text
1590 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
1591 (let (result)
1592 (org-export-as
1593 (org-export-create-backend
1594 :transcoders
1595 `(,(cons 'footnote-reference
1596 (lambda (f c i)
1597 (when (org-element-lineage f '(drawer))
1598 (push (org-export-footnote-first-reference-p f info nil)
1599 result))
1600 ""))
1601 (drawer . (lambda (d c i) c))
1602 (footnote-definition . (lambda (d c i) c))
1603 (section . (lambda (s c i) c))
1604 (paragraph . (lambda (p c i) c))))
1605 nil nil nil '(:with-footnotes t))
1606 (nreverse result)))))
1607 (should
1608 (equal
1609 '(t t)
1610 (org-test-with-temp-text
1611 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
1612 (let (result)
1613 (org-export-as
1614 (org-export-create-backend
1615 :transcoders
1616 `(,(cons 'footnote-reference
1617 (lambda (f c i)
1618 (when (org-element-lineage f '(drawer))
1619 (push (org-export-footnote-first-reference-p f info nil t)
1620 result))
1621 ""))
1622 (drawer . (lambda (d c i) c))
1623 (footnote-definition . (lambda (d c i) c))
1624 (section . (lambda (s c i) c))
1625 (paragraph . (lambda (p c i) c))))
1626 nil nil nil '(:with-footnotes t))
1627 (nreverse result))))))
1629 (ert-deftest test-org-export/get-footnote-number ()
1630 "Test `org-export-get-footnote-number' specifications."
1631 (should
1632 (equal '(1 2 1)
1633 (org-test-with-parsed-data
1634 "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
1635 (org-element-map tree 'footnote-reference
1636 (lambda (ref) (org-export-get-footnote-number ref info))
1637 info))))
1638 ;; Anonymous footnotes all get a new number.
1639 (should
1640 (equal '(1 2)
1641 (org-test-with-parsed-data
1642 "Text[fn::anon1][fn::anon2]"
1643 (org-element-map tree 'footnote-reference
1644 (lambda (ref) (org-export-get-footnote-number ref info))
1645 info))))
1646 ;; Test nested footnotes order.
1647 (should
1648 (equal
1649 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (3 . "fn:3") (4))
1650 (org-test-with-parsed-data
1651 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
1652 (org-element-map tree 'footnote-reference
1653 (lambda (ref)
1654 (cons (org-export-get-footnote-number ref info)
1655 (org-element-property :label ref)))
1656 info))))
1657 ;; Limit number to provided DATA, when non-nil.
1658 (should
1659 (equal
1660 '(1)
1661 (org-test-with-parsed-data
1662 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
1663 (org-element-map tree 'footnote-reference
1664 (lambda (ref)
1665 (org-export-get-footnote-number
1666 ref info (org-element-map tree 'headline #'identity info t)))
1667 info))))
1668 (should
1669 (equal
1670 '(1 2)
1671 (org-test-with-parsed-data
1672 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
1673 (org-element-map tree 'footnote-reference
1674 (lambda (ref) (org-export-get-footnote-number ref info))
1675 info))))
1676 ;; With a non-nil BODY-FIRST optional argument, first check body,
1677 ;; then footnote definitions.
1678 (should
1679 (equal
1680 '(("fn:1" . 1) ("fn:2" . 2) ("fn:3" . 3) ("fn:3" . 3))
1681 (org-test-with-parsed-data
1682 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
1683 (org-element-map tree 'footnote-reference
1684 (lambda (ref)
1685 (cons (org-element-property :label ref)
1686 (org-export-get-footnote-number ref info nil t)))
1687 info))))
1688 (should
1689 (equal
1690 '(("fn:1" . 1) ("fn:2" . 3) ("fn:3" . 2) ("fn:3" . 2))
1691 (org-test-with-parsed-data
1692 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
1693 (org-element-map tree 'footnote-reference
1694 (lambda (ref)
1695 (cons (org-element-property :label ref)
1696 (org-export-get-footnote-number ref info nil)))
1697 info)))))
1699 (ert-deftest test-org-export/collect-footnote-definitions ()
1700 "Test `org-export-collect-footnote-definitions' specifications."
1701 (should
1702 (= 4
1703 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1705 \[fn:2] B [fn:3] [fn::D].
1707 \[fn:3] C."
1708 (length (org-export-collect-footnote-definitions info)))))
1709 ;; Limit number to provided DATA, when non-nil.
1710 (should
1711 (equal
1712 '((1 "fn:2"))
1713 (org-test-with-parsed-data
1714 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
1715 (mapcar #'butlast
1716 (org-export-collect-footnote-definitions
1717 info (org-element-map tree 'headline #'identity info t))))))
1718 (should
1719 (equal
1720 '((1 "fn:1") (2 "fn:2"))
1721 (org-test-with-parsed-data
1722 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
1723 (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
1724 ;; With optional argument BODY-FIRST, first check body, then
1725 ;; footnote definitions.
1726 (should
1727 (equal '("fn:1" "fn:3" "fn:2" nil)
1728 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1730 \[fn:2] B [fn:3] [fn::D].
1732 \[fn:3] C."
1733 (mapcar (lambda (e) (nth 1 e))
1734 (org-export-collect-footnote-definitions info nil t)))))
1735 (should-not
1736 (equal '("fn:1" "fn:3" "fn:2" nil)
1737 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1739 \[fn:2] B [fn:3] [fn::D].
1741 \[fn:3] C."
1742 (mapcar (lambda (e) (nth 1 e))
1743 (org-export-collect-footnote-definitions info))))))
1745 (ert-deftest test-org-export/footnotes ()
1746 "Miscellaneous tests on footnotes."
1747 (let ((org-footnote-section nil)
1748 (org-export-with-footnotes t))
1749 ;; Read every type of footnote.
1750 (should
1751 (equal
1752 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
1753 (org-test-with-parsed-data
1754 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
1755 (org-element-map tree 'footnote-reference
1756 (lambda (ref)
1757 (let ((def (org-export-get-footnote-definition ref info)))
1758 (cons (org-export-get-footnote-number ref info)
1759 (if (eq (org-element-property :type ref) 'inline) (car def)
1760 (car (org-element-contents
1761 (car (org-element-contents def))))))))
1762 info))))
1763 ;; Test nested footnote in invisible definitions.
1764 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
1765 ;; Hide definitions.
1766 (narrow-to-region (point) (point-at-eol))
1767 (let* ((tree (org-element-parse-buffer))
1768 (info (org-combine-plists
1769 `(:parse-tree ,tree)
1770 (org-export-collect-tree-properties
1771 tree (org-export-get-environment)))))
1772 ;; Both footnotes should be seen.
1773 (should
1774 (= (length (org-export-collect-footnote-definitions info)) 2))))
1775 ;; Test export of footnotes defined outside parsing scope.
1776 (should
1777 (equal
1778 "ParagraphOut of scope\n"
1779 (org-test-with-temp-text "[fn:1] Out of scope
1780 * Title
1781 Paragraph[fn:1]"
1782 (let ((backend (org-test-default-backend)))
1783 (setf (org-export-backend-transcoders backend)
1784 (cons (cons 'footnote-reference
1785 (lambda (fn contents info)
1786 (org-element-interpret-data
1787 (org-export-get-footnote-definition fn info))))
1788 (org-export-backend-transcoders backend)))
1789 (forward-line)
1790 (org-export-as backend 'subtree)))))
1791 ;; Footnotes without a definition should throw an error.
1792 (should-error
1793 (org-test-with-parsed-data "Text[fn:1]"
1794 (org-export-get-footnote-definition
1795 (org-element-map tree 'footnote-reference 'identity info t) info)))
1796 ;; Footnote section should be ignored in TOC and in headlines
1797 ;; numbering.
1798 (should
1799 (= 1 (let ((org-footnote-section "Footnotes"))
1800 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
1801 (org-export-collect-headlines info))))))
1802 (should
1803 (equal '(2)
1804 (let ((org-footnote-section "Footnotes"))
1805 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
1806 (org-element-map tree 'headline
1807 (lambda (hl)
1808 (when (equal (org-element-property :raw-value hl) "H2")
1809 (org-export-get-headline-number hl info)))
1810 info t)))))))
1814 ;;; Headlines and Inlinetasks
1816 (ert-deftest test-org-export/get-relative-level ()
1817 "Test `org-export-get-relative-level' specifications."
1818 ;; Standard test.
1819 (should
1820 (equal '(1 2)
1821 (let ((org-odd-levels-only nil))
1822 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1823 (org-element-map tree 'headline
1824 (lambda (h) (org-export-get-relative-level h info))
1825 info)))))
1826 ;; Missing levels
1827 (should
1828 (equal '(1 3)
1829 (let ((org-odd-levels-only nil))
1830 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
1831 (org-element-map tree 'headline
1832 (lambda (h) (org-export-get-relative-level h info))
1833 info))))))
1835 (ert-deftest test-org-export/low-level-p ()
1836 "Test `org-export-low-level-p' specifications."
1837 (should
1838 (equal
1839 '(no yes)
1840 (let ((org-odd-levels-only nil))
1841 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1842 (org-element-map tree 'headline
1843 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
1844 (plist-put info :headline-levels 1)))))))
1846 (ert-deftest test-org-export/get-headline-number ()
1847 "Test `org-export-get-headline-number' specifications."
1848 ;; Standard test.
1849 (should
1850 (equal
1851 '((1) (1 1))
1852 (let ((org-odd-levels-only nil))
1853 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1854 (org-element-map tree 'headline
1855 (lambda (h) (org-export-get-headline-number h info))
1856 info)))))
1857 ;; Missing levels are replaced with 0.
1858 (should
1859 (equal
1860 '((1) (1 0 1))
1861 (let ((org-odd-levels-only nil))
1862 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
1863 (org-element-map tree 'headline
1864 (lambda (h) (org-export-get-headline-number h info))
1865 info))))))
1867 (ert-deftest test-org-export/numbered-headline-p ()
1868 "Test `org-export-numbered-headline-p' specifications."
1869 ;; If `:section-numbers' is nil, never number headlines.
1870 (should-not
1871 (org-test-with-parsed-data "* Headline"
1872 (org-element-map tree 'headline
1873 (lambda (h) (org-export-numbered-headline-p h info))
1874 (plist-put info :section-numbers nil))))
1875 ;; If `:section-numbers' is a number, only number headlines with
1876 ;; a level greater that it.
1877 (should
1878 (equal
1879 '(yes no)
1880 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1881 (org-element-map tree 'headline
1882 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
1883 (plist-put info :section-numbers 1)))))
1884 ;; Otherwise, headlines are always numbered.
1885 (should
1886 (org-test-with-parsed-data "* Headline"
1887 (org-element-map tree 'headline
1888 (lambda (h) (org-export-numbered-headline-p h info))
1889 (plist-put info :section-numbers t)))))
1891 (ert-deftest test-org-export/org-export-get-headline-id ()
1892 "Test `org-export-get-headline-id' specifications."
1893 ;; Numbered headlines have IDs akin to "sec-N".
1894 (should
1895 (equal "sec-1"
1896 (org-test-with-parsed-data "* H"
1897 (org-export-get-headline-id
1898 (org-element-map tree 'headline #'identity info t)
1899 info))))
1900 ;; The ID of numbered headlines reflect the hierarchy.
1901 (should
1902 (equal "sec-1-1"
1903 (org-test-with-parsed-data "* H1\n** H2"
1904 (org-export-get-headline-id
1905 (org-element-map tree 'headline
1906 (lambda (h)
1907 (and (equal "H2" (org-element-property :raw-value h)) h))
1908 info t)
1909 info))))
1910 ;; Unnumbered headlines have IDs akin to "unnumbered-N".
1911 (should
1912 (equal "unnumbered-1"
1913 (org-test-with-parsed-data
1914 "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
1915 (org-export-get-headline-id
1916 (org-element-map tree 'headline #'identity info t)
1917 info))))
1918 ;; The ID of Unnumbered headlines do not reflect the hierarchy.
1919 (should
1920 (equal "unnumbered-2"
1921 (org-test-with-parsed-data
1922 "* H1\n:PROPERTIES:\n:UNNUMBERED: t\n:END:\n** H2"
1923 (org-export-get-headline-id
1924 (org-element-map tree 'headline
1925 (lambda (h)
1926 (and (equal "H2" (org-element-property :raw-value h)) h))
1927 info t)
1928 info))))
1929 ;; When #+OPTIONS: num:nil all headlines are unnumbered.
1930 (should
1931 (equal "unnumbered-1"
1932 (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
1933 (org-export-get-headline-id
1934 (org-element-map tree 'headline 'identity info t)
1935 info))))
1936 ;; UNNUMBERED ignores inheritance. Any non-nil value among
1937 ;; ancestors disables numbering.
1938 (should
1939 (org-test-with-parsed-data
1940 "* H
1941 :PROPERTIES:
1942 :UNNUMBERED: t
1943 :END:
1944 ** H2
1945 :PROPERTIES:
1946 :UNNUMBERED: nil
1947 :END:
1948 *** H3"
1949 (org-every
1950 (lambda (h) (not (org-export-numbered-headline-p h info)))
1951 (org-element-map tree 'headline #'identity info)))))
1953 (ert-deftest test-org-export/number-to-roman ()
1954 "Test `org-export-number-to-roman' specifications."
1955 ;; If number is negative, return it as a string.
1956 (should (equal (org-export-number-to-roman -1) "-1"))
1957 ;; Otherwise, return it as a roman number.
1958 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
1960 (ert-deftest test-org-export/get-optional-title ()
1961 "Test `org-export-get-alt-title' specifications."
1962 ;; If ALT_TITLE property is defined, use it.
1963 (should
1964 (equal '("opt")
1965 (org-test-with-parsed-data
1966 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
1967 (org-export-get-alt-title
1968 (org-element-map tree 'headline 'identity info t)
1969 info))))
1970 ;; Otherwise, fall-back to regular title.
1971 (should
1972 (equal '("Headline")
1973 (org-test-with-parsed-data "* Headline"
1974 (org-export-get-alt-title
1975 (org-element-map tree 'headline 'identity info t)
1976 info)))))
1978 (ert-deftest test-org-export/get-tags ()
1979 "Test `org-export-get-tags' specifications."
1980 (let ((org-export-exclude-tags '("noexport"))
1981 (org-export-select-tags '("export")))
1982 ;; Standard test: tags which are not a select tag, an exclude tag,
1983 ;; or specified as optional argument shouldn't be ignored.
1984 (should
1985 (org-test-with-parsed-data "* Headline :tag:"
1986 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1987 info)))
1988 ;; Exclude tags are removed.
1989 (should-not
1990 (org-test-with-parsed-data "* Headline :noexport:"
1991 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1992 info)))
1993 ;; Select tags are removed.
1994 (should-not
1995 (org-test-with-parsed-data "* Headline :export:"
1996 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1997 info)))
1998 (should
1999 (equal
2000 '("tag")
2001 (org-test-with-parsed-data "* Headline :tag:export:"
2002 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2003 info))))
2004 ;; Tags provided in the optional argument are also ignored.
2005 (should-not
2006 (org-test-with-parsed-data "* Headline :ignore:"
2007 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2008 info '("ignore"))))
2009 ;; Allow tag inheritance.
2010 (should
2011 (equal
2012 '(("tag") ("tag"))
2013 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
2014 (org-element-map tree 'headline
2015 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
2016 ;; Tag inheritance checks FILETAGS keywords.
2017 (should
2018 (equal
2019 '(("a" "b" "tag"))
2020 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
2021 (org-element-map tree 'headline
2022 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
2024 (ert-deftest test-org-export/get-node-property ()
2025 "Test`org-export-get-node-property' specifications."
2026 ;; Standard test.
2027 (should
2028 (equal "value"
2029 (org-test-with-parsed-data "* Headline
2030 :PROPERTIES:
2031 :prop: value
2032 :END:"
2033 (org-export-get-node-property
2034 :PROP (org-element-map tree 'headline 'identity nil t)))))
2035 ;; Test inheritance.
2036 (should
2037 (equal "value"
2038 (org-test-with-parsed-data "* Parent
2039 :PROPERTIES:
2040 :prop: value
2041 :END:
2042 ** Headline
2043 Paragraph"
2044 (org-export-get-node-property
2045 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
2046 ;; Cannot return a value before the first headline.
2047 (should-not
2048 (org-test-with-parsed-data "Paragraph
2049 * Headline
2050 :PROPERTIES:
2051 :prop: value
2052 :END:"
2053 (org-export-get-node-property
2054 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
2056 (ert-deftest test-org-export/get-category ()
2057 "Test `org-export-get-category' specifications."
2058 ;; Standard test.
2059 (should
2060 (equal "value"
2061 (org-test-with-parsed-data "* Headline
2062 :PROPERTIES:
2063 :CATEGORY: value
2064 :END:"
2065 (org-export-get-category
2066 (org-element-map tree 'headline 'identity nil t) info))))
2067 ;; Test inheritance from a parent headline.
2068 (should
2069 (equal '("value" "value")
2070 (org-test-with-parsed-data "* Headline1
2071 :PROPERTIES:
2072 :CATEGORY: value
2073 :END:
2074 ** Headline2"
2075 (org-element-map tree 'headline
2076 (lambda (hl) (org-export-get-category hl info)) info))))
2077 ;; Test inheritance from #+CATEGORY keyword
2078 (should
2079 (equal "value"
2080 (org-test-with-parsed-data "#+CATEGORY: value
2081 * Headline"
2082 (org-export-get-category
2083 (org-element-map tree 'headline 'identity nil t) info))))
2084 ;; Test inheritance from file name.
2085 (should
2086 (equal "test"
2087 (org-test-with-parsed-data "* Headline"
2088 (let ((info (plist-put info :input-file "~/test.org")))
2089 (org-export-get-category
2090 (org-element-map tree 'headline 'identity nil t) info)))))
2091 ;; Fall-back value.
2092 (should
2093 (equal "???"
2094 (org-test-with-parsed-data "* Headline"
2095 (org-export-get-category
2096 (org-element-map tree 'headline 'identity nil t) info)))))
2098 (ert-deftest test-org-export/first-sibling-p ()
2099 "Test `org-export-first-sibling-p' specifications."
2100 ;; Standard test.
2101 (should
2102 (equal
2103 '(yes yes no)
2104 (org-test-with-parsed-data "* H\n** H 2\n** H 3"
2105 (org-element-map tree 'headline
2106 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2107 info))))
2108 (should
2109 (equal '(yes no)
2110 (org-test-with-parsed-data "- item\n\n para"
2111 (org-element-map tree 'paragraph
2112 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2113 info))))
2114 ;; Ignore sections for headlines.
2115 (should
2116 (equal '(yes yes)
2117 (org-test-with-parsed-data "* H\nSection\n** H 2"
2118 (org-element-map tree 'headline
2119 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2120 info))))
2121 ;; Ignore headlines not exported.
2122 (should
2123 (equal
2124 '(yes)
2125 (let ((org-export-exclude-tags '("ignore")))
2126 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
2127 (org-element-map tree 'headline
2128 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2129 info))))))
2131 (ert-deftest test-org-export/last-sibling-p ()
2132 "Test `org-export-last-sibling-p' specifications."
2133 ;; Standard test.
2134 (should
2135 (equal
2136 '(yes no yes)
2137 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
2138 (org-element-map tree 'headline
2139 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2140 info))))
2141 (should
2142 (equal '(no yes)
2143 (org-test-with-parsed-data "- item\n\n para"
2144 (org-element-map tree 'paragraph
2145 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2146 info))))
2147 ;; Ignore headlines not exported.
2148 (should
2149 (equal
2150 '(yes)
2151 (let ((org-export-exclude-tags '("ignore")))
2152 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
2153 (org-element-map tree 'headline
2154 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2155 info))))))
2157 (ert-deftest test-org-export/handle-inlinetasks ()
2158 "Test inlinetask export."
2159 ;; Inlinetask with an exclude tag.
2160 (when (featurep 'org-inlinetask)
2161 (should
2162 (equal
2164 (let ((org-inlinetask-min-level 3)
2165 org-export-filter-body-functions
2166 org-export-filter-final-output-functions)
2167 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
2168 (org-export-as (org-test-default-backend)
2169 nil nil nil '(:exclude-tags ("noexp")))))))
2170 ;; Inlinetask with an include tag.
2171 (should
2172 (equal
2173 "* H2\n*** Inline :exp:\n"
2174 (let ((org-inlinetask-min-level 3)
2175 (org-tags-column 0))
2176 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
2177 (org-export-as (org-test-default-backend)
2178 nil nil nil '(:select-tags ("exp")))))))
2179 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
2180 (should
2181 (equal ""
2182 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
2183 (org-inlinetask-min-level 3)
2184 org-export-filter-body-functions
2185 org-export-filter-final-output-functions)
2186 (org-test-with-temp-text "*** TODO Inline"
2187 (org-export-as (org-test-default-backend)
2188 nil nil nil '(:with-tasks nil))))))))
2192 ;;; Keywords
2194 (ert-deftest test-org-export/get-date ()
2195 "Test `org-export-get-date' specifications."
2196 ;; Return a properly formatted string when
2197 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
2198 ;; consists in a single timestamp.
2199 (should
2200 (equal "29 03 2012"
2201 (let ((org-export-date-timestamp-format "%d %m %Y"))
2202 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2203 (org-export-get-date info)))))
2204 ;; Return a secondary string otherwise.
2205 (should-not
2206 (stringp
2207 (let ((org-export-date-timestamp-format nil))
2208 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2209 (org-export-get-date info)))))
2210 (should
2211 (equal '("Date")
2212 (org-test-with-parsed-data "#+DATE: Date"
2213 (org-export-get-date info))))
2214 ;; Optional argument has precedence over
2215 ;; `org-export-date-timestamp-format'.
2216 (should
2217 (equal "29 03"
2218 (let ((org-export-date-timestamp-format "%d %m %Y"))
2219 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2220 (org-export-get-date info "%d %m"))))))
2224 ;;; Links
2226 (ert-deftest test-org-export/custom-protocol-maybe ()
2227 "Test `org-export-custom-protocol-maybe' specifications."
2228 (should
2229 (string-match
2230 "success"
2231 (let ((org-link-types (copy-sequence org-link-types)))
2232 (org-add-link-type "foo" nil (lambda (p d f) "success"))
2233 (org-export-string-as
2234 "[[foo:path]]"
2235 (org-export-create-backend
2236 :name 'test
2237 :transcoders
2238 '((section . (lambda (s c i) c))
2239 (paragraph . (lambda (p c i) c))
2240 (link . (lambda (l c i)
2241 (or (org-export-custom-protocol-maybe l c 'test)
2242 "failure")))))))))
2243 (should-not
2244 (string-match
2245 "success"
2246 (let ((org-link-types (copy-sequence org-link-types)))
2247 (org-add-link-type
2248 "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
2249 (org-export-string-as
2250 "[[foo:path]]"
2251 (org-export-create-backend
2252 :name 'no-test
2253 :transcoders
2254 '((section . (lambda (s c i) c))
2255 (paragraph . (lambda (p c i) c))
2256 (link . (lambda (l c i)
2257 (or (org-export-custom-protocol-maybe l c 'no-test)
2258 "failure")))))))))
2259 ;; Ignore anonymous back-ends.
2260 (should-not
2261 (string-match
2262 "success"
2263 (let ((org-link-types (copy-sequence org-link-types)))
2264 (org-add-link-type
2265 "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
2266 (org-export-string-as
2267 "[[foo:path]]"
2268 (org-export-create-backend
2269 :transcoders
2270 '((section . (lambda (s c i) c))
2271 (paragraph . (lambda (p c i) c))
2272 (link . (lambda (l c i)
2273 (or (org-export-custom-protocol-maybe l c nil)
2274 "failure"))))))))))
2276 (ert-deftest test-org-export/get-coderef-format ()
2277 "Test `org-export-get-coderef-format' specifications."
2278 ;; A link without description returns "%s"
2279 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
2280 "%s"))
2281 ;; Return "%s" when path is matched within description.
2282 (should (equal (org-export-get-coderef-format "path" "desc (path)")
2283 "desc %s"))
2284 ;; Otherwise return description.
2285 (should (equal (org-export-get-coderef-format "path" "desc")
2286 "desc")))
2288 (ert-deftest test-org-export/inline-image-p ()
2289 "Test `org-export-inline-image-p' specifications."
2290 (should
2291 (org-export-inline-image-p
2292 (org-test-with-temp-text "[[#id]]"
2293 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
2294 '(("custom-id" . "id")))))
2296 (ert-deftest test-org-export/fuzzy-link ()
2297 "Test fuzzy links specifications."
2298 ;; Link to an headline should return headline's number.
2299 (org-test-with-parsed-data
2300 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
2301 (should
2302 ;; Note: Headline's number is in fact a list of numbers.
2303 (equal '(2)
2304 (org-element-map tree 'link
2305 (lambda (link)
2306 (org-export-get-ordinal
2307 (org-export-resolve-fuzzy-link link info) info)) info t))))
2308 ;; Link to a target in an item should return item's number.
2309 (org-test-with-parsed-data
2310 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
2311 (should
2312 ;; Note: Item's number is in fact a list of numbers.
2313 (equal '(1 2)
2314 (org-element-map tree 'link
2315 (lambda (link)
2316 (org-export-get-ordinal
2317 (org-export-resolve-fuzzy-link link info) info)) info t))))
2318 ;; Link to a target in a footnote should return footnote's number.
2319 (org-test-with-parsed-data "
2320 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
2321 (should
2322 (equal '(2 3)
2323 (org-element-map tree 'link
2324 (lambda (link)
2325 (org-export-get-ordinal
2326 (org-export-resolve-fuzzy-link link info) info)) info))))
2327 ;; Link to a named element should return sequence number of that
2328 ;; element.
2329 (org-test-with-parsed-data
2330 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
2331 (should
2332 (= 2
2333 (org-element-map tree 'link
2334 (lambda (link)
2335 (org-export-get-ordinal
2336 (org-export-resolve-fuzzy-link link info) info)) info t))))
2337 ;; Link to a target not within an item, a table, a footnote
2338 ;; reference or definition should return section number.
2339 (org-test-with-parsed-data
2340 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
2341 (should
2342 (equal '(2)
2343 (org-element-map tree 'link
2344 (lambda (link)
2345 (org-export-get-ordinal
2346 (org-export-resolve-fuzzy-link link info) info)) info t))))
2347 ;; Space are not significant when matching a fuzzy link.
2348 (should
2349 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
2350 (org-element-map tree 'link
2351 (lambda (link) (org-export-resolve-fuzzy-link link info))
2352 info t)))
2353 ;; Statistics cookies are ignored for headline match.
2354 (should
2355 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
2356 (org-element-map tree 'link
2357 (lambda (link) (org-export-resolve-fuzzy-link link info))
2358 info t)))
2359 (should
2360 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
2361 (org-element-map tree 'link
2362 (lambda (link) (org-export-resolve-fuzzy-link link info))
2363 info t)))
2364 ;; Headline match is position dependent.
2365 (should-not
2366 (apply
2368 (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
2369 (org-element-map tree 'link
2370 (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
2372 (ert-deftest test-org-export/resolve-coderef ()
2373 "Test `org-export-resolve-coderef' specifications."
2374 (let ((org-coderef-label-format "(ref:%s)"))
2375 ;; A link to a "-n -k -r" block returns line number.
2376 (should
2377 (= 1
2378 (org-test-with-parsed-data
2379 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
2380 (org-export-resolve-coderef "coderef" info))))
2381 (should
2382 (= 1
2383 (org-test-with-parsed-data
2384 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2385 (org-export-resolve-coderef "coderef" info))))
2386 ;; A link to a "-n -r" block returns line number.
2387 (should
2388 (= 1
2389 (org-test-with-parsed-data
2390 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
2391 (org-export-resolve-coderef "coderef" info))))
2392 (should
2393 (= 1
2394 (org-test-with-parsed-data
2395 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2396 (org-export-resolve-coderef "coderef" info))))
2397 ;; A link to a "-n" block returns coderef.
2398 (should
2399 (equal "coderef"
2400 (org-test-with-parsed-data
2401 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2402 (org-export-resolve-coderef "coderef" info))))
2403 (should
2404 (equal "coderef"
2405 (org-test-with-parsed-data
2406 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
2407 (org-export-resolve-coderef "coderef" info))))
2408 ;; A link to a "-r" block returns line number.
2409 (should
2410 (= 1
2411 (org-test-with-parsed-data
2412 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2413 (org-export-resolve-coderef "coderef" info))))
2414 (should
2415 (= 1
2416 (org-test-with-parsed-data
2417 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
2418 (org-export-resolve-coderef "coderef" info))))
2419 ;; A link to a block without a switch returns coderef.
2420 (should
2421 (equal "coderef"
2422 (org-test-with-parsed-data
2423 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2424 (org-export-resolve-coderef "coderef" info))))
2425 (org-test-with-parsed-data
2426 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
2427 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2428 ;; Correctly handle continued line numbers. A "+n" switch should
2429 ;; resume numbering from previous block with numbered lines,
2430 ;; ignoring blocks not numbering lines in the process. A "-n"
2431 ;; switch resets count.
2432 (should
2433 (equal '(2 1)
2434 (org-test-with-parsed-data "
2435 #+BEGIN_EXAMPLE -n
2436 Text.
2437 #+END_EXAMPLE
2439 #+BEGIN_SRC emacs-lisp
2440 \(- 1 1)
2441 #+END_SRC
2443 #+BEGIN_SRC emacs-lisp +n -r
2444 \(+ 1 1) (ref:addition)
2445 #+END_SRC
2447 #+BEGIN_EXAMPLE -n -r
2448 Another text. (ref:text)
2449 #+END_EXAMPLE"
2450 (list (org-export-resolve-coderef "addition" info)
2451 (org-export-resolve-coderef "text" info)))))
2452 ;; Recognize coderef with user-specified syntax.
2453 (should
2454 (equal "text"
2455 (org-test-with-parsed-data
2456 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
2457 (org-export-resolve-coderef "text" info))))
2458 ;; Unresolved coderefs throw an error.
2459 (should-error
2460 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
2461 (org-export-resolve-coderef "unknown" info)))))
2463 (ert-deftest test-org-export/resolve-fuzzy-link ()
2464 "Test `org-export-resolve-fuzzy-link' specifications."
2465 ;; Match target objects.
2466 (should
2467 (org-test-with-parsed-data "<<target>> [[target]]"
2468 (org-export-resolve-fuzzy-link
2469 (org-element-map tree 'link 'identity info t) info)))
2470 ;; Match named elements.
2471 (should
2472 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
2473 (org-export-resolve-fuzzy-link
2474 (org-element-map tree 'link 'identity info t) info)))
2475 ;; Match exact headline's name.
2476 (should
2477 (org-test-with-parsed-data "* My headline\n[[My headline]]"
2478 (org-export-resolve-fuzzy-link
2479 (org-element-map tree 'link 'identity info t) info)))
2480 ;; Targets objects have priority over named elements and headline
2481 ;; titles.
2482 (should
2483 (eq 'target
2484 (org-test-with-parsed-data
2485 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
2486 (org-element-type
2487 (org-export-resolve-fuzzy-link
2488 (org-element-map tree 'link 'identity info t) info)))))
2489 ;; Named elements have priority over headline titles.
2490 (should
2491 (eq 'paragraph
2492 (org-test-with-parsed-data
2493 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
2494 (org-element-type
2495 (org-export-resolve-fuzzy-link
2496 (org-element-map tree 'link 'identity info t) info)))))
2497 ;; If link's path starts with a "*", only match headline titles,
2498 ;; though.
2499 (should
2500 (eq 'headline
2501 (org-test-with-parsed-data
2502 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
2503 (org-element-type
2504 (org-export-resolve-fuzzy-link
2505 (org-element-map tree 'link 'identity info t) info)))))
2506 ;; Error if no match.
2507 (should-error
2508 (org-test-with-parsed-data "[[target]]"
2509 (org-export-resolve-fuzzy-link
2510 (org-element-map tree 'link 'identity info t) info)))
2511 ;; Match fuzzy link even when before first headline.
2512 (should
2513 (eq 'headline
2514 (org-test-with-parsed-data "[[hl]]\n* hl"
2515 (org-element-type
2516 (org-export-resolve-fuzzy-link
2517 (org-element-map tree 'link 'identity info t) info))))))
2519 (ert-deftest test-org-export/resolve-id-link ()
2520 "Test `org-export-resolve-id-link' specifications."
2521 ;; 1. Regular test for custom-id link.
2522 (org-test-with-parsed-data "* Headline1
2523 :PROPERTIES:
2524 :CUSTOM_ID: test
2525 :END:
2526 * Headline 2
2527 \[[#test]]"
2528 (should
2529 (org-export-resolve-id-link
2530 (org-element-map tree 'link 'identity info t) info)))
2531 ;; 2. Failing test for custom-id link.
2532 (org-test-with-parsed-data "* Headline1
2533 :PROPERTIES:
2534 :CUSTOM_ID: test
2535 :END:
2536 * Headline 2
2537 \[[#no-match]]"
2538 (should-not
2539 (org-export-resolve-id-link
2540 (org-element-map tree 'link 'identity info t) info)))
2541 ;; 3. Test for internal id target.
2542 (org-test-with-parsed-data "* Headline1
2543 :PROPERTIES:
2544 :ID: aaaa
2545 :END:
2546 * Headline 2
2547 \[[id:aaaa]]"
2548 (should
2549 (org-export-resolve-id-link
2550 (org-element-map tree 'link 'identity info t) info)))
2551 ;; 4. Test for external id target.
2552 (org-test-with-parsed-data "[[id:aaaa]]"
2553 (should
2554 (org-export-resolve-id-link
2555 (org-element-map tree 'link 'identity info t)
2556 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
2558 (ert-deftest test-org-export/resolve-radio-link ()
2559 "Test `org-export-resolve-radio-link' specifications."
2560 ;; Standard test.
2561 (should
2562 (org-test-with-temp-text "<<<radio>>> radio"
2563 (org-update-radio-target-regexp)
2564 (let* ((tree (org-element-parse-buffer))
2565 (info `(:parse-tree ,tree)))
2566 (org-export-resolve-radio-link
2567 (org-element-map tree 'link 'identity info t)
2568 info))))
2569 ;; Radio targets are case-insensitive.
2570 (should
2571 (org-test-with-temp-text "<<<RADIO>>> radio"
2572 (org-update-radio-target-regexp)
2573 (let* ((tree (org-element-parse-buffer))
2574 (info `(:parse-tree ,tree)))
2575 (org-export-resolve-radio-link
2576 (org-element-map tree 'link 'identity info t)
2577 info))))
2578 ;; Radio target with objects.
2579 (should
2580 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
2581 (org-update-radio-target-regexp)
2582 (let* ((tree (org-element-parse-buffer))
2583 (info `(:parse-tree ,tree)))
2584 (org-export-resolve-radio-link
2585 (org-element-map tree 'link 'identity info t)
2586 info))))
2587 ;; Radio target with objects at its beginning.
2588 (should
2589 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
2590 (org-update-radio-target-regexp)
2591 (let* ((tree (org-element-parse-buffer))
2592 (info `(:parse-tree ,tree)))
2593 (org-export-resolve-radio-link
2594 (org-element-map tree 'link 'identity info t)
2595 info))))
2596 ;; Radio link next to an apostrophe.
2597 (should
2598 (org-test-with-temp-text "<<<radio>>> radio's"
2599 (org-update-radio-target-regexp)
2600 (let* ((tree (org-element-parse-buffer))
2601 (info `(:parse-tree ,tree)))
2602 (org-export-resolve-radio-link
2603 (org-element-map tree 'link 'identity info t)
2604 info))))
2605 ;; Multiple radio targets.
2606 (should
2607 (equal '("radio1" "radio2")
2608 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
2609 (org-update-radio-target-regexp)
2610 (let* ((tree (org-element-parse-buffer))
2611 (info `(:parse-tree ,tree)))
2612 (org-element-map tree 'link
2613 (lambda (link)
2614 (org-element-property
2615 :value (org-export-resolve-radio-link link info)))
2616 info)))))
2617 ;; Radio target is whitespace insensitive.
2618 (should
2619 (org-test-with-temp-text "<<<a radio>>> a\n radio"
2620 (org-update-radio-target-regexp)
2621 (let* ((tree (org-element-parse-buffer))
2622 (info `(:parse-tree ,tree)))
2623 (org-element-map tree 'link
2624 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
2628 ;;; Src-block and example-block
2630 (ert-deftest test-org-export/unravel-code ()
2631 "Test `org-export-unravel-code' function."
2632 ;; Code without reference.
2633 (should
2634 (equal '("(+ 1 1)\n")
2635 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2636 (org-export-unravel-code (org-element-at-point)))))
2637 ;; Code with reference.
2638 (should
2639 (equal '("(+ 1 1)\n" (1 . "test"))
2640 (org-test-with-temp-text
2641 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
2642 (let ((org-coderef-label-format "(ref:%s)"))
2643 (org-export-unravel-code (org-element-at-point))))))
2644 ;; Code with user-defined reference.
2645 (should
2646 (equal
2647 '("(+ 1 1)\n" (1 . "test"))
2648 (org-test-with-temp-text
2649 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
2650 (let ((org-coderef-label-format "(ref:%s)"))
2651 (org-export-unravel-code (org-element-at-point))))))
2652 ;; Code references keys are relative to the current block.
2653 (should
2654 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
2655 (org-test-with-temp-text "
2656 #+BEGIN_EXAMPLE -n
2657 \(+ 1 1)
2658 #+END_EXAMPLE
2659 #+BEGIN_EXAMPLE +n
2660 \(+ 2 2)
2661 \(+ 3 3) (ref:one)
2662 #+END_EXAMPLE"
2663 (goto-line 5)
2664 (let ((org-coderef-label-format "(ref:%s)"))
2665 (org-export-unravel-code (org-element-at-point)))))))
2667 (ert-deftest test-org-export/format-code-default ()
2668 "Test `org-export-format-code-default' specifications."
2669 ;; Return the empty string when code is empty.
2670 (should
2671 (equal ""
2672 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
2673 (org-export-format-code-default
2674 (org-element-map tree 'src-block 'identity info t) info))))
2675 ;; Number lines, two whitespace characters before the actual loc.
2676 (should
2677 (equal "1 a\n2 b\n"
2678 (org-test-with-parsed-data
2679 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
2680 (org-export-format-code-default
2681 (org-element-map tree 'src-block 'identity info t) info))))
2682 ;; Put references 6 whitespace characters after the widest line,
2683 ;; wrapped within parenthesis.
2684 (should
2685 (equal "123 (a)\n1 (b)\n"
2686 (let ((org-coderef-label-format "(ref:%s)"))
2687 (org-test-with-parsed-data
2688 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
2689 (org-export-format-code-default
2690 (org-element-map tree 'src-block 'identity info t) info))))))
2694 ;;; Smart Quotes
2696 (ert-deftest test-org-export/activate-smart-quotes ()
2697 "Test `org-export-activate-smart-quotes' specifications."
2698 ;; Opening double quotes: standard test.
2699 (should
2700 (equal
2701 '("some &ldquo;paragraph")
2702 (let ((org-export-default-language "en"))
2703 (org-test-with-parsed-data "some \"paragraph"
2704 (org-element-map tree 'plain-text
2705 (lambda (s) (org-export-activate-smart-quotes s :html info))
2706 info)))))
2707 ;; Opening quotes: at the beginning of a paragraph.
2708 (should
2709 (equal
2710 '("&ldquo;begin")
2711 (let ((org-export-default-language "en"))
2712 (org-test-with-parsed-data "\"begin"
2713 (org-element-map tree 'plain-text
2714 (lambda (s) (org-export-activate-smart-quotes s :html info))
2715 info)))))
2716 ;; Opening quotes: after an object.
2717 (should
2718 (equal
2719 '("&ldquo;begin")
2720 (let ((org-export-default-language "en"))
2721 (org-test-with-parsed-data "=verb= \"begin"
2722 (org-element-map tree 'plain-text
2723 (lambda (s) (org-export-activate-smart-quotes s :html info))
2724 info)))))
2725 ;; Closing quotes: standard test.
2726 (should
2727 (equal
2728 '("some&rdquo; paragraph")
2729 (let ((org-export-default-language "en"))
2730 (org-test-with-parsed-data "some\" paragraph"
2731 (org-element-map tree 'plain-text
2732 (lambda (s) (org-export-activate-smart-quotes s :html info))
2733 info)))))
2734 ;; Closing quotes: at the end of a paragraph.
2735 (should
2736 (equal
2737 '("end&rdquo;")
2738 (let ((org-export-default-language "en"))
2739 (org-test-with-parsed-data "end\""
2740 (org-element-map tree 'plain-text
2741 (lambda (s) (org-export-activate-smart-quotes s :html info))
2742 info)))))
2743 ;; Apostrophe: standard test.
2744 (should
2745 (equal
2746 '("It shouldn&rsquo;t fail")
2747 (let ((org-export-default-language "en"))
2748 (org-test-with-parsed-data "It shouldn't fail"
2749 (org-element-map tree 'plain-text
2750 (lambda (s) (org-export-activate-smart-quotes s :html info))
2751 info)))))
2752 ;; Apostrophe: before an object.
2753 (should
2754 (equal
2755 '("a&rsquo;")
2756 (let ((org-export-default-language "en"))
2757 (org-test-with-parsed-data "a'=b="
2758 (org-element-map tree 'plain-text
2759 (lambda (s) (org-export-activate-smart-quotes s :html info))
2760 info)))))
2761 ;; Apostrophe: after an object.
2762 (should
2763 (equal
2764 '("&rsquo;s")
2765 (let ((org-export-default-language "en"))
2766 (org-test-with-parsed-data "=code='s"
2767 (org-element-map tree 'plain-text
2768 (lambda (s) (org-export-activate-smart-quotes s :html info))
2769 info)))))
2770 ;; Special case: isolated quotes.
2771 (should
2772 (equal '("&ldquo;" "&rdquo;")
2773 (let ((org-export-default-language "en"))
2774 (org-test-with-parsed-data "\"$x$\""
2775 (org-element-map tree 'plain-text
2776 (lambda (s) (org-export-activate-smart-quotes s :html info))
2777 info)))))
2778 ;; Smart quotes in secondary strings.
2779 (should
2780 (equal '("&ldquo;" "&rdquo;")
2781 (let ((org-export-default-language "en"))
2782 (org-test-with-parsed-data "* \"$x$\""
2783 (org-element-map tree 'plain-text
2784 (lambda (s) (org-export-activate-smart-quotes s :html info))
2785 info)))))
2786 ;; Smart quotes in document keywords.
2787 (should
2788 (equal '("&ldquo;" "&rdquo;")
2789 (let ((org-export-default-language "en"))
2790 (org-test-with-parsed-data "#+TITLE: \"$x$\""
2791 (org-element-map (plist-get info :title) 'plain-text
2792 (lambda (s) (org-export-activate-smart-quotes s :html info))
2793 info)))))
2794 ;; Smart quotes in parsed affiliated keywords.
2795 (should
2796 (equal '("&ldquo;" "&rdquo;" "Paragraph")
2797 (let ((org-export-default-language "en"))
2798 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
2799 (org-element-map tree 'plain-text
2800 (lambda (s) (org-export-activate-smart-quotes s :html info))
2801 info nil nil t))))))
2805 ;;; Tables
2807 (ert-deftest test-org-export/special-column ()
2808 "Test if the table's special column is properly recognized."
2809 ;; 1. First column is special if it contains only a special marking
2810 ;; characters or empty cells.
2811 (org-test-with-temp-text "
2812 | ! | 1 |
2813 | | 2 |"
2814 (should
2815 (org-export-table-has-special-column-p
2816 (org-element-map
2817 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2818 ;; 2. If the column contains anything else, it isn't special.
2819 (org-test-with-temp-text "
2820 | ! | 1 |
2821 | b | 2 |"
2822 (should-not
2823 (org-export-table-has-special-column-p
2824 (org-element-map
2825 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2826 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
2827 ;; and "!".
2828 (org-test-with-temp-text "
2829 | # | 1 |
2830 | ^ | 2 |
2831 | * | 3 |
2832 | _ | 4 |
2833 | / | 5 |
2834 | $ | 6 |
2835 | ! | 7 |"
2836 (should
2837 (org-export-table-has-special-column-p
2838 (org-element-map
2839 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2840 ;; 4. A first column with only empty cells isn't considered as
2841 ;; special.
2842 (org-test-with-temp-text "
2843 | | 1 |
2844 | | 2 |"
2845 (should-not
2846 (org-export-table-has-special-column-p
2847 (org-element-map
2848 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
2850 (ert-deftest test-org-export/table-row-is-special-p ()
2851 "Test `org-export-table-row-is-special-p' specifications."
2852 ;; 1. A row is special if it has a special marking character in the
2853 ;; special column.
2854 (org-test-with-parsed-data "| ! | 1 |"
2855 (should
2856 (org-export-table-row-is-special-p
2857 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2858 ;; 2. A row is special when its first field is "/"
2859 (org-test-with-parsed-data "
2860 | / | 1 |
2861 | a | b |"
2862 (should
2863 (org-export-table-row-is-special-p
2864 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2865 ;; 3. A row only containing alignment cookies is also considered as
2866 ;; special.
2867 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
2868 (should
2869 (org-export-table-row-is-special-p
2870 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2871 ;; 4. Everything else isn't considered as special.
2872 (org-test-with-parsed-data "| \alpha | | c |"
2873 (should-not
2874 (org-export-table-row-is-special-p
2875 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2876 ;; 5. Table's rules are never considered as special rows.
2877 (org-test-with-parsed-data "|---+---|"
2878 (should-not
2879 (org-export-table-row-is-special-p
2880 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
2882 (ert-deftest test-org-export/has-header-p ()
2883 "Test `org-export-table-has-header-p' specifications."
2884 ;; 1. With an header.
2885 (org-test-with-parsed-data "
2886 | a | b |
2887 |---+---|
2888 | c | d |"
2889 (should
2890 (org-export-table-has-header-p
2891 (org-element-map tree 'table 'identity info 'first-match)
2892 info)))
2893 ;; 2. Without an header.
2894 (org-test-with-parsed-data "
2895 | a | b |
2896 | c | d |"
2897 (should-not
2898 (org-export-table-has-header-p
2899 (org-element-map tree 'table 'identity info 'first-match)
2900 info)))
2901 ;; 3. Don't get fooled with starting and ending rules.
2902 (org-test-with-parsed-data "
2903 |---+---|
2904 | a | b |
2905 | c | d |
2906 |---+---|"
2907 (should-not
2908 (org-export-table-has-header-p
2909 (org-element-map tree 'table 'identity info 'first-match)
2910 info))))
2912 (ert-deftest test-org-export/table-row-group ()
2913 "Test `org-export-table-row-group' specifications."
2914 ;; 1. A rule creates a new group.
2915 (should
2916 (equal '(1 rule 2)
2917 (org-test-with-parsed-data "
2918 | a | b |
2919 |---+---|
2920 | 1 | 2 |"
2921 (org-element-map tree 'table-row
2922 (lambda (row)
2923 (if (eq (org-element-property :type row) 'rule) 'rule
2924 (org-export-table-row-group row info)))))))
2925 ;; 2. Special rows are ignored in count.
2926 (should
2927 (equal
2928 '(rule 1)
2929 (org-test-with-parsed-data "
2930 | / | < | > |
2931 |---|---+---|
2932 | | 1 | 2 |"
2933 (org-element-map tree 'table-row
2934 (lambda (row)
2935 (if (eq (org-element-property :type row) 'rule) 'rule
2936 (org-export-table-row-group row info)))
2937 info))))
2938 ;; 3. Double rules also are ignored in count.
2939 (should
2940 (equal '(1 rule rule 2)
2941 (org-test-with-parsed-data "
2942 | a | b |
2943 |---+---|
2944 |---+---|
2945 | 1 | 2 |"
2946 (org-element-map tree 'table-row
2947 (lambda (row)
2948 (if (eq (org-element-property :type row) 'rule) 'rule
2949 (org-export-table-row-group row info))))))))
2951 (ert-deftest test-org-export/table-row-number ()
2952 "Test `org-export-table-row-number' specifications."
2953 ;; Standard test. Number is 0-indexed.
2954 (should
2955 (equal '(0 1)
2956 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
2957 (org-element-map tree 'table-row
2958 (lambda (row) (org-export-table-row-number row info)) info))))
2959 ;; Number ignores separators.
2960 (should
2961 (equal '(0 1)
2962 (org-test-with-parsed-data "
2963 | a | b | c |
2964 |---+---+---|
2965 | d | e | f |"
2966 (org-element-map tree 'table-row
2967 (lambda (row) (org-export-table-row-number row info)) info))))
2968 ;; Number ignores special rows.
2969 (should
2970 (equal '(0 1)
2971 (org-test-with-parsed-data "
2972 | / | < | > |
2973 | | b | c |
2974 |---+-----+-----|
2975 | | <c> | <c> |
2976 | | e | f |"
2977 (org-element-map tree 'table-row
2978 (lambda (row) (org-export-table-row-number row info)) info)))))
2980 (ert-deftest test-org-export/table-cell-width ()
2981 "Test `org-export-table-cell-width' specifications."
2982 ;; 1. Width is primarily determined by width cookies. If no cookie
2983 ;; is found, cell's width is nil.
2984 (org-test-with-parsed-data "
2985 | / | <l> | <6> | <l7> |
2986 | | a | b | c |"
2987 (should
2988 (equal
2989 '(nil 6 7)
2990 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2991 (org-element-map tree 'table-cell 'identity info)))))
2992 ;; 2. The last width cookie has precedence.
2993 (org-test-with-parsed-data "
2994 | <6> |
2995 | <7> |
2996 | a |"
2997 (should
2998 (equal
2999 '(7)
3000 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3001 (org-element-map tree 'table-cell 'identity info)))))
3002 ;; 3. Valid width cookies must have a specific row.
3003 (org-test-with-parsed-data "| <6> | cell |"
3004 (should
3005 (equal
3006 '(nil nil)
3007 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3008 (org-element-map tree 'table-cell 'identity))))))
3010 (ert-deftest test-org-export/table-cell-alignment ()
3011 "Test `org-export-table-cell-alignment' specifications."
3012 ;; 1. Alignment is primarily determined by alignment cookies.
3013 (should
3014 (equal '(left center right)
3015 (let ((org-table-number-fraction 0.5)
3016 (org-table-number-regexp "^[0-9]+$"))
3017 (org-test-with-parsed-data "| <l> | <c> | <r> |"
3018 (mapcar (lambda (cell)
3019 (org-export-table-cell-alignment cell info))
3020 (org-element-map tree 'table-cell 'identity))))))
3021 ;; 2. The last alignment cookie has precedence.
3022 (should
3023 (equal '(right right right)
3024 (org-test-with-parsed-data "
3025 | <l8> |
3026 | cell |
3027 | <r9> |"
3028 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
3029 (org-element-map tree 'table-cell 'identity)))))
3030 ;; 3. If there's no cookie, cell's contents determine alignment.
3031 ;; A column mostly made of cells containing numbers will align
3032 ;; its cells to the right.
3033 (should
3034 (equal '(right right right)
3035 (let ((org-table-number-fraction 0.5)
3036 (org-table-number-regexp "^[0-9]+$"))
3037 (org-test-with-parsed-data "
3038 | 123 |
3039 | some text |
3040 | 12345 |"
3041 (mapcar (lambda (cell)
3042 (org-export-table-cell-alignment cell info))
3043 (org-element-map tree 'table-cell 'identity))))))
3044 ;; 4. Otherwise, they will be aligned to the left.
3045 (should
3046 (equal '(left left left)
3047 (org-test-with-parsed-data "
3048 | text |
3049 | some text |
3050 | \alpha |"
3051 (mapcar (lambda (cell)
3052 (org-export-table-cell-alignment cell info))
3053 (org-element-map tree 'table-cell 'identity info))))))
3055 (ert-deftest test-org-export/table-cell-borders ()
3056 "Test `org-export-table-cell-borders' specifications."
3057 ;; 1. Recognize various column groups indicators.
3058 (org-test-with-parsed-data "| / | < | > | <> |"
3059 (should
3060 (equal
3061 '((right bottom top) (left bottom top) (right bottom top)
3062 (right left bottom top))
3063 (mapcar (lambda (cell)
3064 (org-export-table-cell-borders cell info))
3065 (org-element-map tree 'table-cell 'identity)))))
3066 ;; 2. Accept shortcuts to define column groups.
3067 (org-test-with-parsed-data "| / | < | < |"
3068 (should
3069 (equal
3070 '((right bottom top) (right left bottom top) (left bottom top))
3071 (mapcar (lambda (cell)
3072 (org-export-table-cell-borders cell info))
3073 (org-element-map tree 'table-cell 'identity)))))
3074 ;; 3. A valid column groups row must start with a "/".
3075 (org-test-with-parsed-data "
3076 | | < |
3077 | a | b |"
3078 (should
3079 (equal '((top) (top) (bottom) (bottom))
3080 (mapcar (lambda (cell)
3081 (org-export-table-cell-borders cell info))
3082 (org-element-map tree 'table-cell 'identity)))))
3083 ;; 4. Take table rules into consideration.
3084 (org-test-with-parsed-data "
3085 | 1 |
3086 |---|
3087 | 2 |"
3088 (should
3089 (equal '((below top) (bottom above))
3090 (mapcar (lambda (cell)
3091 (org-export-table-cell-borders cell info))
3092 (org-element-map tree 'table-cell 'identity)))))
3093 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
3094 ;; (resp. `bottom' and `below') borders. Any special row is
3095 ;; ignored.
3096 (org-test-with-parsed-data "
3097 |---+----|
3098 | / | |
3099 | | 1 |
3100 |---+----|"
3101 (should
3102 (equal '((bottom below top above))
3103 (last
3104 (mapcar (lambda (cell)
3105 (org-export-table-cell-borders cell info))
3106 (org-element-map tree 'table-cell 'identity)))))))
3108 (ert-deftest test-org-export/table-dimensions ()
3109 "Test `org-export-table-dimensions' specifications."
3110 ;; 1. Standard test.
3111 (org-test-with-parsed-data "
3112 | 1 | 2 | 3 |
3113 | 4 | 5 | 6 |"
3114 (should
3115 (equal '(2 . 3)
3116 (org-export-table-dimensions
3117 (org-element-map tree 'table 'identity info 'first-match) info))))
3118 ;; 2. Ignore horizontal rules and special columns.
3119 (org-test-with-parsed-data "
3120 | / | < | > |
3121 | 1 | 2 | 3 |
3122 |---+---+---|
3123 | 4 | 5 | 6 |"
3124 (should
3125 (equal '(2 . 3)
3126 (org-export-table-dimensions
3127 (org-element-map tree 'table 'identity info 'first-match) info)))))
3129 (ert-deftest test-org-export/table-cell-address ()
3130 "Test `org-export-table-cell-address' specifications."
3131 ;; 1. Standard test: index is 0-based.
3132 (org-test-with-parsed-data "| a | b |"
3133 (should
3134 (equal '((0 . 0) (0 . 1))
3135 (org-element-map tree 'table-cell
3136 (lambda (cell) (org-export-table-cell-address cell info))
3137 info))))
3138 ;; 2. Special column isn't counted, nor are special rows.
3139 (org-test-with-parsed-data "
3140 | / | <> |
3141 | | c |"
3142 (should
3143 (equal '(0 . 0)
3144 (org-export-table-cell-address
3145 (car (last (org-element-map tree 'table-cell 'identity info)))
3146 info))))
3147 ;; 3. Tables rules do not count either.
3148 (org-test-with-parsed-data "
3149 | a |
3150 |---|
3151 | b |
3152 |---|
3153 | c |"
3154 (should
3155 (equal '(2 . 0)
3156 (org-export-table-cell-address
3157 (car (last (org-element-map tree 'table-cell 'identity info)))
3158 info))))
3159 ;; 4. Return nil for special cells.
3160 (org-test-with-parsed-data "| / | a |"
3161 (should-not
3162 (org-export-table-cell-address
3163 (org-element-map tree 'table-cell 'identity nil 'first-match)
3164 info))))
3166 (ert-deftest test-org-export/get-table-cell-at ()
3167 "Test `org-export-get-table-cell-at' specifications."
3168 ;; 1. Address ignores special columns, special rows and rules.
3169 (org-test-with-parsed-data "
3170 | / | <> |
3171 | | a |
3172 |---+----|
3173 | | b |"
3174 (should
3175 (equal '("b")
3176 (org-element-contents
3177 (org-export-get-table-cell-at
3178 '(1 . 0)
3179 (org-element-map tree 'table 'identity info 'first-match)
3180 info)))))
3181 ;; 2. Return value for a non-existent address is nil.
3182 (org-test-with-parsed-data "| a |"
3183 (should-not
3184 (org-export-get-table-cell-at
3185 '(2 . 2)
3186 (org-element-map tree 'table 'identity info 'first-match)
3187 info)))
3188 (org-test-with-parsed-data "| / |"
3189 (should-not
3190 (org-export-get-table-cell-at
3191 '(0 . 0)
3192 (org-element-map tree 'table 'identity info 'first-match)
3193 info))))
3195 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
3196 "Test `org-export-table-cell-starts-colgroup-p' specifications."
3197 ;; 1. A cell at a beginning of a row always starts a column group.
3198 (org-test-with-parsed-data "| a |"
3199 (should
3200 (org-export-table-cell-starts-colgroup-p
3201 (org-element-map tree 'table-cell 'identity info 'first-match)
3202 info)))
3203 ;; 2. Special column should be ignored when determining the
3204 ;; beginning of the row.
3205 (org-test-with-parsed-data "
3206 | / | |
3207 | | a |"
3208 (should
3209 (org-export-table-cell-starts-colgroup-p
3210 (org-element-map tree 'table-cell 'identity info 'first-match)
3211 info)))
3212 ;; 2. Explicit column groups.
3213 (org-test-with-parsed-data "
3214 | / | | < |
3215 | a | b | c |"
3216 (should
3217 (equal
3218 '(yes no yes)
3219 (org-element-map tree 'table-cell
3220 (lambda (cell)
3221 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
3222 info)))))
3224 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
3225 "Test `org-export-table-cell-ends-colgroup-p' specifications."
3226 ;; 1. A cell at the end of a row always ends a column group.
3227 (org-test-with-parsed-data "| a |"
3228 (should
3229 (org-export-table-cell-ends-colgroup-p
3230 (org-element-map tree 'table-cell 'identity info 'first-match)
3231 info)))
3232 ;; 2. Special column should be ignored when determining the
3233 ;; beginning of the row.
3234 (org-test-with-parsed-data "
3235 | / | |
3236 | | a |"
3237 (should
3238 (org-export-table-cell-ends-colgroup-p
3239 (org-element-map tree 'table-cell 'identity info 'first-match)
3240 info)))
3241 ;; 3. Explicit column groups.
3242 (org-test-with-parsed-data "
3243 | / | < | |
3244 | a | b | c |"
3245 (should
3246 (equal
3247 '(yes no yes)
3248 (org-element-map tree 'table-cell
3249 (lambda (cell)
3250 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
3251 info)))))
3253 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
3254 "Test `org-export-table-row-starts-rowgroup-p' specifications."
3255 ;; 1. A row at the beginning of a table always starts a row group.
3256 ;; So does a row following a table rule.
3257 (org-test-with-parsed-data "
3258 | a |
3259 |---|
3260 | b |"
3261 (should
3262 (equal
3263 '(yes no yes)
3264 (org-element-map tree 'table-row
3265 (lambda (row)
3266 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
3267 info))))
3268 ;; 2. Special rows should be ignored when determining the beginning
3269 ;; of the row.
3270 (org-test-with-parsed-data "
3271 | / | < |
3272 | | a |
3273 |---+---|
3274 | / | < |
3275 | | b |"
3276 (should
3277 (equal
3278 '(yes no yes)
3279 (org-element-map tree 'table-row
3280 (lambda (row)
3281 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
3282 info)))))
3284 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
3285 "Test `org-export-table-row-ends-rowgroup-p' specifications."
3286 ;; 1. A row at the end of a table always ends a row group. So does
3287 ;; a row preceding a table rule.
3288 (org-test-with-parsed-data "
3289 | a |
3290 |---|
3291 | b |"
3292 (should
3293 (equal
3294 '(yes no yes)
3295 (org-element-map tree 'table-row
3296 (lambda (row)
3297 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
3298 info))))
3299 ;; 2. Special rows should be ignored when determining the beginning
3300 ;; of the row.
3301 (org-test-with-parsed-data "
3302 | | a |
3303 | / | < |
3304 |---+---|
3305 | | b |
3306 | / | < |"
3307 (should
3308 (equal
3309 '(yes no yes)
3310 (org-element-map tree 'table-row
3311 (lambda (row)
3312 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
3313 info)))))
3315 (ert-deftest test-org-export/table-row-in-header-p ()
3316 "Test `org-export-table-row-in-header-p' specifications."
3317 ;; Standard test. Separators are always nil.
3318 (should
3319 (equal
3320 '(yes no no)
3321 (org-test-with-parsed-data "| a |\n|---|\n| b |"
3322 (org-element-map tree 'table-row
3323 (lambda (row)
3324 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
3325 ;; Nil when there is no header.
3326 (should
3327 (equal
3328 '(no no)
3329 (org-test-with-parsed-data "| a |\n| b |"
3330 (org-element-map tree 'table-row
3331 (lambda (row)
3332 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
3334 (ert-deftest test-org-export/table-row-starts-header-p ()
3335 "Test `org-export-table-row-starts-header-p' specifications."
3336 ;; 1. Only the row starting the first row group starts the table
3337 ;; header.
3338 (org-test-with-parsed-data "
3339 | a |
3340 | b |
3341 |---|
3342 | c |"
3343 (should
3344 (equal
3345 '(yes no no no)
3346 (org-element-map tree 'table-row
3347 (lambda (row)
3348 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
3349 info))))
3350 ;; 2. A row cannot start an header if there's no header in the
3351 ;; table.
3352 (org-test-with-parsed-data "
3353 | a |
3354 |---|"
3355 (should-not
3356 (org-export-table-row-starts-header-p
3357 (org-element-map tree 'table-row 'identity info 'first-match)
3358 info))))
3360 (ert-deftest test-org-export/table-row-ends-header-p ()
3361 "Test `org-export-table-row-ends-header-p' specifications."
3362 ;; 1. Only the row starting the first row group starts the table
3363 ;; header.
3364 (org-test-with-parsed-data "
3365 | a |
3366 | b |
3367 |---|
3368 | c |"
3369 (should
3370 (equal
3371 '(no yes no no)
3372 (org-element-map tree 'table-row
3373 (lambda (row)
3374 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
3375 info))))
3376 ;; 2. A row cannot start an header if there's no header in the
3377 ;; table.
3378 (org-test-with-parsed-data "
3379 | a |
3380 |---|"
3381 (should-not
3382 (org-export-table-row-ends-header-p
3383 (org-element-map tree 'table-row 'identity info 'first-match)
3384 info))))
3388 ;;; Tables of Contents
3390 (ert-deftest test-org-export/collect-headlines ()
3391 "Test `org-export-collect-headlines' specifications."
3392 ;; Standard test.
3393 (should
3394 (= 2
3395 (length
3396 (org-test-with-parsed-data "* H1\n** H2"
3397 (org-export-collect-headlines info)))))
3398 ;; Do not collect headlines below optional argument.
3399 (should
3400 (= 1
3401 (length
3402 (org-test-with-parsed-data "* H1\n** H2"
3403 (org-export-collect-headlines info 1)))))
3404 ;; Never collect headlines below maximum headline level.
3405 (should
3406 (= 1
3407 (length
3408 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3409 (org-export-collect-headlines info)))))
3410 (should
3411 (= 1
3412 (length
3413 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3414 (org-export-collect-headlines info 2)))))
3415 ;; Collect headlines locally.
3416 (should
3417 (= 2
3418 (org-test-with-parsed-data "* H1\n** H2\n** H3"
3419 (let ((scope (org-element-map tree 'headline #'identity info t)))
3420 (length (org-export-collect-headlines info nil scope))))))
3421 ;; When collecting locally, optional level is relative.
3422 (should
3423 (= 1
3424 (org-test-with-parsed-data "* H1\n** H2\n*** H3"
3425 (let ((scope (org-element-map tree 'headline #'identity info t)))
3426 (length (org-export-collect-headlines info 1 scope)))))))
3430 ;;; Templates
3432 (ert-deftest test-org-export/inner-template ()
3433 "Test `inner-template' translator specifications."
3434 (should
3435 (equal "Success!"
3436 (org-test-with-temp-text "* Headline"
3437 (org-export-as
3438 (org-export-create-backend
3439 :transcoders
3440 '((inner-template . (lambda (contents info) "Success!"))
3441 (headline . (lambda (h c i) "Headline"))))))))
3442 ;; Inner template is applied even in a "body-only" export.
3443 (should
3444 (equal "Success!"
3445 (org-test-with-temp-text "* Headline"
3446 (org-export-as
3447 (org-export-create-backend
3448 :transcoders '((inner-template . (lambda (c i) "Success!"))
3449 (headline . (lambda (h c i) "Headline"))))
3450 nil nil 'body-only)))))
3452 (ert-deftest test-org-export/template ()
3453 "Test `template' translator specifications."
3454 (should
3455 (equal "Success!"
3456 (org-test-with-temp-text "* Headline"
3457 (org-export-as
3458 (org-export-create-backend
3459 :transcoders '((template . (lambda (contents info) "Success!"))
3460 (headline . (lambda (h c i) "Headline"))))))))
3461 ;; Template is not applied in a "body-only" export.
3462 (should-not
3463 (equal "Success!"
3464 (org-test-with-temp-text "* Headline"
3465 (org-export-as
3466 (org-export-create-backend
3467 :transcoders '((template . (lambda (contents info) "Success!"))
3468 (headline . (lambda (h c i) "Headline"))))
3469 nil nil 'body-only)))))
3473 ;;; Topology
3475 (ert-deftest test-org-export/get-next-element ()
3476 "Test `org-export-get-next-element' specifications."
3477 ;; Standard test.
3478 (should
3479 (equal "b"
3480 (org-test-with-parsed-data "* Headline\n*a* b"
3481 (org-export-get-next-element
3482 (org-element-map tree 'bold 'identity info t) info))))
3483 ;; Return nil when no previous element.
3484 (should-not
3485 (org-test-with-parsed-data "* Headline\na *b*"
3486 (org-export-get-next-element
3487 (org-element-map tree 'bold 'identity info t) info)))
3488 ;; Non-exportable elements are ignored.
3489 (should-not
3490 (let ((org-export-with-timestamps nil))
3491 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
3492 (org-export-get-next-element
3493 (org-element-map tree 'entity 'identity info t) info))))
3494 ;; Find next element in secondary strings.
3495 (should
3496 (eq 'verbatim
3497 (org-test-with-parsed-data "* a =verb="
3498 (org-element-type
3499 (org-export-get-next-element
3500 (org-element-map tree 'plain-text 'identity info t) info)))))
3501 (should
3502 (eq 'verbatim
3503 (org-test-with-parsed-data "* /italic/ =verb="
3504 (org-element-type
3505 (org-export-get-next-element
3506 (org-element-map tree 'italic 'identity info t) info)))))
3507 ;; Find next element in document keywords.
3508 (should
3509 (eq 'verbatim
3510 (org-test-with-parsed-data "#+TITLE: a =verb="
3511 (org-element-type
3512 (org-export-get-next-element
3513 (org-element-map
3514 (plist-get info :title) 'plain-text 'identity info t) info)))))
3515 ;; Find next element in parsed affiliated keywords.
3516 (should
3517 (eq 'verbatim
3518 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
3519 (org-element-type
3520 (org-export-get-next-element
3521 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3522 ;; With optional argument N, return a list containing all the
3523 ;; following elements.
3524 (should
3525 (equal
3526 '(bold code underline)
3527 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3528 (mapcar 'car
3529 (org-export-get-next-element
3530 (org-element-map tree 'italic 'identity info t) info t)))))
3531 ;; When N is a positive integer, return a list containing up to
3532 ;; N following elements.
3533 (should
3534 (equal
3535 '(bold code)
3536 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3537 (mapcar 'car
3538 (org-export-get-next-element
3539 (org-element-map tree 'italic 'identity info t) info 2))))))
3541 (ert-deftest test-org-export/get-previous-element ()
3542 "Test `org-export-get-previous-element' specifications."
3543 ;; Standard test.
3544 (should
3545 (equal "a "
3546 (org-test-with-parsed-data "* Headline\na *b*"
3547 (org-export-get-previous-element
3548 (org-element-map tree 'bold 'identity info t) info))))
3549 ;; Return nil when no previous element.
3550 (should-not
3551 (org-test-with-parsed-data "* Headline\n*a* b"
3552 (org-export-get-previous-element
3553 (org-element-map tree 'bold 'identity info t) info)))
3554 ;; Non-exportable elements are ignored.
3555 (should-not
3556 (let ((org-export-with-timestamps nil))
3557 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
3558 (org-export-get-previous-element
3559 (org-element-map tree 'entity 'identity info t) info))))
3560 ;; Find previous element in secondary strings.
3561 (should
3562 (eq 'verbatim
3563 (org-test-with-parsed-data "* =verb= a"
3564 (org-element-type
3565 (org-export-get-previous-element
3566 (org-element-map tree 'plain-text 'identity info t) info)))))
3567 (should
3568 (eq 'verbatim
3569 (org-test-with-parsed-data "* =verb= /italic/"
3570 (org-element-type
3571 (org-export-get-previous-element
3572 (org-element-map tree 'italic 'identity info t) info)))))
3573 ;; Find previous element in document keywords.
3574 (should
3575 (eq 'verbatim
3576 (org-test-with-parsed-data "#+TITLE: =verb= a"
3577 (org-element-type
3578 (org-export-get-previous-element
3579 (org-element-map
3580 (plist-get info :title) 'plain-text 'identity info t) info)))))
3581 ;; Find previous element in parsed affiliated keywords.
3582 (should
3583 (eq 'verbatim
3584 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
3585 (org-element-type
3586 (org-export-get-previous-element
3587 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3588 ;; With optional argument N, return a list containing up to
3589 ;; N previous elements.
3590 (should
3591 (equal '(underline italic bold)
3592 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3593 (mapcar 'car
3594 (org-export-get-previous-element
3595 (org-element-map tree 'code 'identity info t) info t)))))
3596 ;; When N is a positive integer, return a list containing up to
3597 ;; N previous elements.
3598 (should
3599 (equal '(italic bold)
3600 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3601 (mapcar 'car
3602 (org-export-get-previous-element
3603 (org-element-map tree 'code 'identity info t) info 2))))))
3606 (provide 'test-ox)
3607 ;;; test-org-export.el end here