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