ox.el: {{{date}}} formatting argument
[org-mode/org-tableheadings.git] / testing / lisp / test-ox.el
blob79b5c69b86b5bf9d1e1c86bae9dfc73127177bd9
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")
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-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/footnotes ()
1542 "Test footnotes specifications."
1543 (let ((org-footnote-section nil)
1544 (org-export-with-footnotes t))
1545 ;; 1. Read every type of footnote.
1546 (should
1547 (equal
1548 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
1549 (org-test-with-parsed-data
1550 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
1551 (org-element-map tree 'footnote-reference
1552 (lambda (ref)
1553 (let ((def (org-export-get-footnote-definition ref info)))
1554 (cons (org-export-get-footnote-number ref info)
1555 (if (eq (org-element-property :type ref) 'inline) (car def)
1556 (car (org-element-contents
1557 (car (org-element-contents def))))))))
1558 info))))
1559 ;; 2. Test nested footnotes order.
1560 (should
1561 (equal
1562 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
1563 (org-test-with-parsed-data
1564 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
1565 (org-element-map tree 'footnote-reference
1566 (lambda (ref)
1567 (when (org-export-footnote-first-reference-p ref info)
1568 (cons (org-export-get-footnote-number ref info)
1569 (org-element-property :label ref))))
1570 info))))
1571 ;; 3. Test nested footnote in invisible definitions.
1572 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
1573 ;; Hide definitions.
1574 (narrow-to-region (point) (point-at-eol))
1575 (let* ((tree (org-element-parse-buffer))
1576 (info (org-combine-plists
1577 `(:parse-tree ,tree)
1578 (org-export-collect-tree-properties
1579 tree (org-export-get-environment)))))
1580 ;; Both footnotes should be seen.
1581 (should
1582 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
1583 ;; 4. Test footnotes definitions collection.
1584 (should
1585 (= 4
1586 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
1588 \[fn:2] B [fn:3] [fn::D].
1590 \[fn:3] C."
1591 (length (org-export-collect-footnote-definitions tree info)))))
1592 ;; 5. Test export of footnotes defined outside parsing scope.
1593 (should
1594 (equal
1595 "ParagraphOut of scope\n"
1596 (org-test-with-temp-text "[fn:1] Out of scope
1597 * Title
1598 Paragraph[fn:1]"
1599 (let ((backend (org-test-default-backend)))
1600 (setf (org-export-backend-transcoders backend)
1601 (cons (cons 'footnote-reference
1602 (lambda (fn contents info)
1603 (org-element-interpret-data
1604 (org-export-get-footnote-definition fn info))))
1605 (org-export-backend-transcoders backend)))
1606 (forward-line)
1607 (org-export-as backend 'subtree)))))
1608 ;; 6. Footnotes without a definition should throw an error.
1609 (should-error
1610 (org-test-with-parsed-data "Text[fn:1]"
1611 (org-export-get-footnote-definition
1612 (org-element-map tree 'footnote-reference 'identity info t) info)))
1613 ;; 7. Footnote section should be ignored in TOC and in headlines
1614 ;; numbering.
1615 (should
1616 (= 1 (let ((org-footnote-section "Footnotes"))
1617 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
1618 (org-export-collect-headlines info))))))
1619 (should
1620 (equal '(2)
1621 (let ((org-footnote-section "Footnotes"))
1622 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
1623 (org-element-map tree 'headline
1624 (lambda (hl)
1625 (when (equal (org-element-property :raw-value hl) "H2")
1626 (org-export-get-headline-number hl info)))
1627 info t)))))))
1631 ;;; Headlines and Inlinetasks
1633 (ert-deftest test-org-export/get-relative-level ()
1634 "Test `org-export-get-relative-level' specifications."
1635 ;; Standard test.
1636 (should
1637 (equal '(1 2)
1638 (let ((org-odd-levels-only nil))
1639 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1640 (org-element-map tree 'headline
1641 (lambda (h) (org-export-get-relative-level h info))
1642 info)))))
1643 ;; Missing levels
1644 (should
1645 (equal '(1 3)
1646 (let ((org-odd-levels-only nil))
1647 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
1648 (org-element-map tree 'headline
1649 (lambda (h) (org-export-get-relative-level h info))
1650 info))))))
1652 (ert-deftest test-org-export/low-level-p ()
1653 "Test `org-export-low-level-p' specifications."
1654 (should
1655 (equal
1656 '(no yes)
1657 (let ((org-odd-levels-only nil))
1658 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1659 (org-element-map tree 'headline
1660 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
1661 (plist-put info :headline-levels 1)))))))
1663 (ert-deftest test-org-export/get-headline-number ()
1664 "Test `org-export-get-headline-number' specifications."
1665 ;; Standard test.
1666 (should
1667 (equal
1668 '((1) (1 1))
1669 (let ((org-odd-levels-only nil))
1670 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1671 (org-element-map tree 'headline
1672 (lambda (h) (org-export-get-headline-number h info))
1673 info)))))
1674 ;; Missing levels are replaced with 0.
1675 (should
1676 (equal
1677 '((1) (1 0 1))
1678 (let ((org-odd-levels-only nil))
1679 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
1680 (org-element-map tree 'headline
1681 (lambda (h) (org-export-get-headline-number h info))
1682 info))))))
1684 (ert-deftest test-org-export/numbered-headline-p ()
1685 "Test `org-export-numbered-headline-p' specifications."
1686 ;; If `:section-numbers' is nil, never number headlines.
1687 (should-not
1688 (org-test-with-parsed-data "* Headline"
1689 (org-element-map tree 'headline
1690 (lambda (h) (org-export-numbered-headline-p h info))
1691 (plist-put info :section-numbers nil))))
1692 ;; If `:section-numbers' is a number, only number headlines with
1693 ;; a level greater that it.
1694 (should
1695 (equal
1696 '(yes no)
1697 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1698 (org-element-map tree 'headline
1699 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
1700 (plist-put info :section-numbers 1)))))
1701 ;; Otherwise, headlines are always numbered.
1702 (should
1703 (org-test-with-parsed-data "* Headline"
1704 (org-element-map tree 'headline
1705 (lambda (h) (org-export-numbered-headline-p h info))
1706 (plist-put info :section-numbers t)))))
1708 (ert-deftest test-org-export/org-export-get-headline-id ()
1709 "Test `org-export-get-headline-id' specifications."
1710 ;; Numbered headlines have IDs akin to "sec-N".
1711 (should
1712 (equal "sec-1"
1713 (org-test-with-parsed-data "* H"
1714 (org-export-get-headline-id
1715 (org-element-map tree 'headline #'identity info t)
1716 info))))
1717 ;; The ID of numbered headlines reflect the hierarchy.
1718 (should
1719 (equal "sec-1-1"
1720 (org-test-with-parsed-data "* H1\n** H2"
1721 (org-export-get-headline-id
1722 (org-element-map tree 'headline
1723 (lambda (h)
1724 (and (equal "H2" (org-element-property :raw-value h)) h))
1725 info t)
1726 info))))
1727 ;; Unnumbered headlines have IDs akin to "unnumbered-N".
1728 (should
1729 (equal "unnumbered-1"
1730 (org-test-with-parsed-data
1731 "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
1732 (org-export-get-headline-id
1733 (org-element-map tree 'headline #'identity info t)
1734 info))))
1735 ;; The ID of Unnumbered headlines do not reflect the hierarchy.
1736 (should
1737 (equal "unnumbered-2"
1738 (org-test-with-parsed-data
1739 "* H1\n:PROPERTIES:\n:UNNUMBERED: t\n:END:\n** H2"
1740 (org-export-get-headline-id
1741 (org-element-map tree 'headline
1742 (lambda (h)
1743 (and (equal "H2" (org-element-property :raw-value h)) h))
1744 info t)
1745 info))))
1746 ;; When #+OPTIONS: num:nil all headlines are unnumbered.
1747 (should
1748 (equal "unnumbered-1"
1749 (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
1750 (org-export-get-headline-id
1751 (org-element-map tree 'headline 'identity info t)
1752 info))))
1753 ;; UNNUMBERED ignores inheritance. Any non-nil value among
1754 ;; ancestors disables numbering.
1755 (should
1756 (org-test-with-parsed-data
1757 "* H
1758 :PROPERTIES:
1759 :UNNUMBERED: t
1760 :END:
1761 ** H2
1762 :PROPERTIES:
1763 :UNNUMBERED: nil
1764 :END:
1765 *** H3"
1766 (org-every
1767 (lambda (h) (not (org-export-numbered-headline-p h info)))
1768 (org-element-map tree 'headline #'identity info)))))
1770 (ert-deftest test-org-export/number-to-roman ()
1771 "Test `org-export-number-to-roman' specifications."
1772 ;; If number is negative, return it as a string.
1773 (should (equal (org-export-number-to-roman -1) "-1"))
1774 ;; Otherwise, return it as a roman number.
1775 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
1777 (ert-deftest test-org-export/get-optional-title ()
1778 "Test `org-export-get-alt-title' specifications."
1779 ;; If ALT_TITLE property is defined, use it.
1780 (should
1781 (equal '("opt")
1782 (org-test-with-parsed-data
1783 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
1784 (org-export-get-alt-title
1785 (org-element-map tree 'headline 'identity info t)
1786 info))))
1787 ;; Otherwise, fall-back to regular title.
1788 (should
1789 (equal '("Headline")
1790 (org-test-with-parsed-data "* Headline"
1791 (org-export-get-alt-title
1792 (org-element-map tree 'headline 'identity info t)
1793 info)))))
1795 (ert-deftest test-org-export/get-tags ()
1796 "Test `org-export-get-tags' specifications."
1797 (let ((org-export-exclude-tags '("noexport"))
1798 (org-export-select-tags '("export")))
1799 ;; Standard test: tags which are not a select tag, an exclude tag,
1800 ;; or specified as optional argument shouldn't be ignored.
1801 (should
1802 (org-test-with-parsed-data "* Headline :tag:"
1803 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1804 info)))
1805 ;; Exclude tags are removed.
1806 (should-not
1807 (org-test-with-parsed-data "* Headline :noexport:"
1808 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1809 info)))
1810 ;; Select tags are removed.
1811 (should-not
1812 (org-test-with-parsed-data "* Headline :export:"
1813 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1814 info)))
1815 (should
1816 (equal
1817 '("tag")
1818 (org-test-with-parsed-data "* Headline :tag:export:"
1819 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1820 info))))
1821 ;; Tags provided in the optional argument are also ignored.
1822 (should-not
1823 (org-test-with-parsed-data "* Headline :ignore:"
1824 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1825 info '("ignore"))))
1826 ;; Allow tag inheritance.
1827 (should
1828 (equal
1829 '(("tag") ("tag"))
1830 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
1831 (org-element-map tree 'headline
1832 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
1833 ;; Tag inheritance checks FILETAGS keywords.
1834 (should
1835 (equal
1836 '(("a" "b" "tag"))
1837 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
1838 (org-element-map tree 'headline
1839 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
1841 (ert-deftest test-org-export/get-node-property ()
1842 "Test`org-export-get-node-property' specifications."
1843 ;; Standard test.
1844 (should
1845 (equal "value"
1846 (org-test-with-parsed-data "* Headline
1847 :PROPERTIES:
1848 :prop: value
1849 :END:"
1850 (org-export-get-node-property
1851 :PROP (org-element-map tree 'headline 'identity nil t)))))
1852 ;; Test inheritance.
1853 (should
1854 (equal "value"
1855 (org-test-with-parsed-data "* Parent
1856 :PROPERTIES:
1857 :prop: value
1858 :END:
1859 ** Headline
1860 Paragraph"
1861 (org-export-get-node-property
1862 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
1863 ;; Cannot return a value before the first headline.
1864 (should-not
1865 (org-test-with-parsed-data "Paragraph
1866 * Headline
1867 :PROPERTIES:
1868 :prop: value
1869 :END:"
1870 (org-export-get-node-property
1871 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
1873 (ert-deftest test-org-export/get-category ()
1874 "Test `org-export-get-category' specifications."
1875 ;; Standard test.
1876 (should
1877 (equal "value"
1878 (org-test-with-parsed-data "* Headline
1879 :PROPERTIES:
1880 :CATEGORY: value
1881 :END:"
1882 (org-export-get-category
1883 (org-element-map tree 'headline 'identity nil t) info))))
1884 ;; Test inheritance from a parent headline.
1885 (should
1886 (equal '("value" "value")
1887 (org-test-with-parsed-data "* Headline1
1888 :PROPERTIES:
1889 :CATEGORY: value
1890 :END:
1891 ** Headline2"
1892 (org-element-map tree 'headline
1893 (lambda (hl) (org-export-get-category hl info)) info))))
1894 ;; Test inheritance from #+CATEGORY keyword
1895 (should
1896 (equal "value"
1897 (org-test-with-parsed-data "#+CATEGORY: value
1898 * Headline"
1899 (org-export-get-category
1900 (org-element-map tree 'headline 'identity nil t) info))))
1901 ;; Test inheritance from file name.
1902 (should
1903 (equal "test"
1904 (org-test-with-parsed-data "* Headline"
1905 (let ((info (plist-put info :input-file "~/test.org")))
1906 (org-export-get-category
1907 (org-element-map tree 'headline 'identity nil t) info)))))
1908 ;; Fall-back value.
1909 (should
1910 (equal "???"
1911 (org-test-with-parsed-data "* Headline"
1912 (org-export-get-category
1913 (org-element-map tree 'headline 'identity nil t) info)))))
1915 (ert-deftest test-org-export/first-sibling-p ()
1916 "Test `org-export-first-sibling-p' specifications."
1917 ;; Standard test.
1918 (should
1919 (equal
1920 '(yes yes no)
1921 (org-test-with-parsed-data "* H\n** H 2\n** H 3"
1922 (org-element-map tree 'headline
1923 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1924 info))))
1925 (should
1926 (equal '(yes no)
1927 (org-test-with-parsed-data "- item\n\n para"
1928 (org-element-map tree 'paragraph
1929 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1930 info))))
1931 ;; Ignore sections for headlines.
1932 (should
1933 (equal '(yes yes)
1934 (org-test-with-parsed-data "* H\nSection\n** H 2"
1935 (org-element-map tree 'headline
1936 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1937 info))))
1938 ;; Ignore headlines not exported.
1939 (should
1940 (equal
1941 '(yes)
1942 (let ((org-export-exclude-tags '("ignore")))
1943 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
1944 (org-element-map tree 'headline
1945 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1946 info))))))
1948 (ert-deftest test-org-export/last-sibling-p ()
1949 "Test `org-export-last-sibling-p' specifications."
1950 ;; Standard test.
1951 (should
1952 (equal
1953 '(yes no yes)
1954 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1955 (org-element-map tree 'headline
1956 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1957 info))))
1958 (should
1959 (equal '(no yes)
1960 (org-test-with-parsed-data "- item\n\n para"
1961 (org-element-map tree 'paragraph
1962 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1963 info))))
1964 ;; Ignore headlines not exported.
1965 (should
1966 (equal
1967 '(yes)
1968 (let ((org-export-exclude-tags '("ignore")))
1969 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
1970 (org-element-map tree 'headline
1971 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1972 info))))))
1974 (ert-deftest test-org-export/handle-inlinetasks ()
1975 "Test inlinetask export."
1976 ;; Inlinetask with an exclude tag.
1977 (when (featurep 'org-inlinetask)
1978 (should
1979 (equal
1981 (let ((org-inlinetask-min-level 3)
1982 org-export-filter-body-functions
1983 org-export-filter-final-output-functions)
1984 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
1985 (org-export-as (org-test-default-backend)
1986 nil nil nil '(:exclude-tags ("noexp")))))))
1987 ;; Inlinetask with an include tag.
1988 (should
1989 (equal
1990 "* H2\n*** Inline :exp:\n"
1991 (let ((org-inlinetask-min-level 3)
1992 (org-tags-column 0))
1993 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
1994 (org-export-as (org-test-default-backend)
1995 nil nil nil '(:select-tags ("exp")))))))
1996 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
1997 (should
1998 (equal ""
1999 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
2000 (org-inlinetask-min-level 3)
2001 org-export-filter-body-functions
2002 org-export-filter-final-output-functions)
2003 (org-test-with-temp-text "*** TODO Inline"
2004 (org-export-as (org-test-default-backend)
2005 nil nil nil '(:with-tasks nil))))))))
2009 ;;; Keywords
2011 (ert-deftest test-org-export/get-date ()
2012 "Test `org-export-get-date' specifications."
2013 ;; Return a properly formatted string when
2014 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
2015 ;; consists in a single timestamp.
2016 (should
2017 (equal "29 03 2012"
2018 (let ((org-export-date-timestamp-format "%d %m %Y"))
2019 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2020 (org-export-get-date info)))))
2021 ;; Return a secondary string otherwise.
2022 (should-not
2023 (stringp
2024 (let ((org-export-date-timestamp-format nil))
2025 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2026 (org-export-get-date info)))))
2027 (should
2028 (equal '("Date")
2029 (org-test-with-parsed-data "#+DATE: Date"
2030 (org-export-get-date info))))
2031 ;; Optional argument has precedence over
2032 ;; `org-export-date-timestamp-format'.
2033 (should
2034 (equal "29 03"
2035 (let ((org-export-date-timestamp-format "%d %m %Y"))
2036 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2037 (org-export-get-date info "%d %m"))))))
2041 ;;; Links
2043 (ert-deftest test-org-export/custom-protocol-maybe ()
2044 "Test `org-export-custom-protocol-maybe' specifications."
2045 (should
2046 (string-match
2047 "success"
2048 (let ((org-link-types (copy-sequence org-link-types)))
2049 (org-add-link-type "foo" nil (lambda (p d f) "success"))
2050 (org-export-string-as
2051 "[[foo:path]]"
2052 (org-export-create-backend
2053 :name 'test
2054 :transcoders '((section . (lambda (s c i) c))
2055 (paragraph . (lambda (p c i) c))
2056 (link . (lambda (l c i)
2057 (or (org-export-custom-protocol-maybe l c i)
2058 "failure")))))))))
2059 (should-not
2060 (string-match
2061 "success"
2062 (let ((org-link-types (copy-sequence org-link-types)))
2063 (org-add-link-type
2064 "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
2065 (org-export-string-as
2066 "[[foo:path]]"
2067 (org-export-create-backend
2068 :name 'no-test
2069 :transcoders '((section . (lambda (s c i) c))
2070 (paragraph . (lambda (p c i) c))
2071 (link . (lambda (l c i)
2072 (or (org-export-custom-protocol-maybe l c i)
2073 "failure")))))))))
2074 ;; Ignore anonymous back-ends.
2075 (should-not
2076 (string-match
2077 "success"
2078 (let ((org-link-types (copy-sequence org-link-types)))
2079 (org-add-link-type
2080 "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
2081 (org-export-string-as
2082 "[[foo:path]]"
2083 (org-export-create-backend
2084 :transcoders '((section . (lambda (s c i) c))
2085 (paragraph . (lambda (p c i) c))
2086 (link . (lambda (l c i)
2087 (or (org-export-custom-protocol-maybe l c i)
2088 "failure"))))))))))
2090 (ert-deftest test-org-export/get-coderef-format ()
2091 "Test `org-export-get-coderef-format' specifications."
2092 ;; A link without description returns "%s"
2093 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
2094 "%s"))
2095 ;; Return "%s" when path is matched within description.
2096 (should (equal (org-export-get-coderef-format "path" "desc (path)")
2097 "desc %s"))
2098 ;; Otherwise return description.
2099 (should (equal (org-export-get-coderef-format "path" "desc")
2100 "desc")))
2102 (ert-deftest test-org-export/inline-image-p ()
2103 "Test `org-export-inline-image-p' specifications."
2104 (should
2105 (org-export-inline-image-p
2106 (org-test-with-temp-text "[[#id]]"
2107 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
2108 '(("custom-id" . "id")))))
2110 (ert-deftest test-org-export/fuzzy-link ()
2111 "Test fuzzy links specifications."
2112 ;; Link to an headline should return headline's number.
2113 (org-test-with-parsed-data
2114 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
2115 (should
2116 ;; Note: Headline's number is in fact a list of numbers.
2117 (equal '(2)
2118 (org-element-map tree 'link
2119 (lambda (link)
2120 (org-export-get-ordinal
2121 (org-export-resolve-fuzzy-link link info) info)) info t))))
2122 ;; Link to a target in an item should return item's number.
2123 (org-test-with-parsed-data
2124 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
2125 (should
2126 ;; Note: Item's number is in fact a list of numbers.
2127 (equal '(1 2)
2128 (org-element-map tree 'link
2129 (lambda (link)
2130 (org-export-get-ordinal
2131 (org-export-resolve-fuzzy-link link info) info)) info t))))
2132 ;; Link to a target in a footnote should return footnote's number.
2133 (org-test-with-parsed-data "
2134 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
2135 (should
2136 (equal '(2 3)
2137 (org-element-map tree 'link
2138 (lambda (link)
2139 (org-export-get-ordinal
2140 (org-export-resolve-fuzzy-link link info) info)) info))))
2141 ;; Link to a named element should return sequence number of that
2142 ;; element.
2143 (org-test-with-parsed-data
2144 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
2145 (should
2146 (= 2
2147 (org-element-map tree 'link
2148 (lambda (link)
2149 (org-export-get-ordinal
2150 (org-export-resolve-fuzzy-link link info) info)) info t))))
2151 ;; Link to a target not within an item, a table, a footnote
2152 ;; reference or definition should return section number.
2153 (org-test-with-parsed-data
2154 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
2155 (should
2156 (equal '(2)
2157 (org-element-map tree 'link
2158 (lambda (link)
2159 (org-export-get-ordinal
2160 (org-export-resolve-fuzzy-link link info) info)) info t))))
2161 ;; Space are not significant when matching a fuzzy link.
2162 (should
2163 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
2164 (org-element-map tree 'link
2165 (lambda (link) (org-export-resolve-fuzzy-link link info))
2166 info t)))
2167 ;; Statistics cookies are ignored for headline match.
2168 (should
2169 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
2170 (org-element-map tree 'link
2171 (lambda (link) (org-export-resolve-fuzzy-link link info))
2172 info t)))
2173 (should
2174 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
2175 (org-element-map tree 'link
2176 (lambda (link) (org-export-resolve-fuzzy-link link info))
2177 info t)))
2178 ;; Headline match is position dependent.
2179 (should-not
2180 (apply
2182 (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
2183 (org-element-map tree 'link
2184 (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
2186 (ert-deftest test-org-export/resolve-coderef ()
2187 "Test `org-export-resolve-coderef' specifications."
2188 (let ((org-coderef-label-format "(ref:%s)"))
2189 ;; 1. A link to a "-n -k -r" block returns line number.
2190 (org-test-with-parsed-data
2191 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
2192 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2193 (org-test-with-parsed-data
2194 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2195 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2196 ;; 2. A link to a "-n -r" block returns line number.
2197 (org-test-with-parsed-data
2198 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
2199 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2200 (org-test-with-parsed-data
2201 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2202 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2203 ;; 3. A link to a "-n" block returns coderef.
2204 (org-test-with-parsed-data
2205 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2206 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2207 (org-test-with-parsed-data
2208 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
2209 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2210 ;; 4. A link to a "-r" block returns line number.
2211 (org-test-with-parsed-data
2212 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2213 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2214 (org-test-with-parsed-data
2215 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
2216 (should (= (org-export-resolve-coderef "coderef" info) 1)))
2217 ;; 5. A link to a block without a switch returns coderef.
2218 (org-test-with-parsed-data
2219 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
2220 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2221 (org-test-with-parsed-data
2222 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
2223 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
2224 ;; 6. Correctly handle continued line numbers. A "+n" switch
2225 ;; should resume numbering from previous block with numbered
2226 ;; lines, ignoring blocks not numbering lines in the process.
2227 ;; A "-n" switch resets count.
2228 (org-test-with-parsed-data "
2229 #+BEGIN_EXAMPLE -n
2230 Text.
2231 #+END_EXAMPLE
2233 #+BEGIN_SRC emacs-lisp
2234 \(- 1 1)
2235 #+END_SRC
2237 #+BEGIN_SRC emacs-lisp +n -r
2238 \(+ 1 1) (ref:addition)
2239 #+END_SRC
2241 #+BEGIN_EXAMPLE -n -r
2242 Another text. (ref:text)
2243 #+END_EXAMPLE"
2244 (should (= (org-export-resolve-coderef "addition" info) 2))
2245 (should (= (org-export-resolve-coderef "text" info) 1)))
2246 ;; 7. Recognize coderef with user-specified syntax.
2247 (org-test-with-parsed-data
2248 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
2249 (should (equal (org-export-resolve-coderef "text" info) "text")))))
2251 (ert-deftest test-org-export/resolve-fuzzy-link ()
2252 "Test `org-export-resolve-fuzzy-link' specifications."
2253 ;; Match target objects.
2254 (should
2255 (org-test-with-parsed-data "<<target>> [[target]]"
2256 (org-export-resolve-fuzzy-link
2257 (org-element-map tree 'link 'identity info t) info)))
2258 ;; Match named elements.
2259 (should
2260 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
2261 (org-export-resolve-fuzzy-link
2262 (org-element-map tree 'link 'identity info t) info)))
2263 ;; Match exact headline's name.
2264 (should
2265 (org-test-with-parsed-data "* My headline\n[[My headline]]"
2266 (org-export-resolve-fuzzy-link
2267 (org-element-map tree 'link 'identity info t) info)))
2268 ;; Targets objects have priority over named elements and headline
2269 ;; titles.
2270 (should
2271 (eq 'target
2272 (org-test-with-parsed-data
2273 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
2274 (org-element-type
2275 (org-export-resolve-fuzzy-link
2276 (org-element-map tree 'link 'identity info t) info)))))
2277 ;; Named elements have priority over headline titles.
2278 (should
2279 (eq 'paragraph
2280 (org-test-with-parsed-data
2281 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
2282 (org-element-type
2283 (org-export-resolve-fuzzy-link
2284 (org-element-map tree 'link 'identity info t) info)))))
2285 ;; If link's path starts with a "*", only match headline titles,
2286 ;; though.
2287 (should
2288 (eq 'headline
2289 (org-test-with-parsed-data
2290 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
2291 (org-element-type
2292 (org-export-resolve-fuzzy-link
2293 (org-element-map tree 'link 'identity info t) info)))))
2294 ;; Return nil if no match.
2295 (should-not
2296 (org-test-with-parsed-data "[[target]]"
2297 (org-export-resolve-fuzzy-link
2298 (org-element-map tree 'link 'identity info t) info)))
2299 ;; Match fuzzy link even when before first headline.
2300 (should
2301 (eq 'headline
2302 (org-test-with-parsed-data "[[hl]]\n* hl"
2303 (org-element-type
2304 (org-export-resolve-fuzzy-link
2305 (org-element-map tree 'link 'identity info t) info))))))
2307 (ert-deftest test-org-export/resolve-id-link ()
2308 "Test `org-export-resolve-id-link' specifications."
2309 ;; 1. Regular test for custom-id link.
2310 (org-test-with-parsed-data "* Headline1
2311 :PROPERTIES:
2312 :CUSTOM_ID: test
2313 :END:
2314 * Headline 2
2315 \[[#test]]"
2316 (should
2317 (org-export-resolve-id-link
2318 (org-element-map tree 'link 'identity info t) info)))
2319 ;; 2. Failing test for custom-id link.
2320 (org-test-with-parsed-data "* Headline1
2321 :PROPERTIES:
2322 :CUSTOM_ID: test
2323 :END:
2324 * Headline 2
2325 \[[#no-match]]"
2326 (should-not
2327 (org-export-resolve-id-link
2328 (org-element-map tree 'link 'identity info t) info)))
2329 ;; 3. Test for internal id target.
2330 (org-test-with-parsed-data "* Headline1
2331 :PROPERTIES:
2332 :ID: aaaa
2333 :END:
2334 * Headline 2
2335 \[[id:aaaa]]"
2336 (should
2337 (org-export-resolve-id-link
2338 (org-element-map tree 'link 'identity info t) info)))
2339 ;; 4. Test for external id target.
2340 (org-test-with-parsed-data "[[id:aaaa]]"
2341 (should
2342 (org-export-resolve-id-link
2343 (org-element-map tree 'link 'identity info t)
2344 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
2346 (ert-deftest test-org-export/resolve-radio-link ()
2347 "Test `org-export-resolve-radio-link' specifications."
2348 ;; Standard test.
2349 (should
2350 (org-test-with-temp-text "<<<radio>>> radio"
2351 (org-update-radio-target-regexp)
2352 (let* ((tree (org-element-parse-buffer))
2353 (info `(:parse-tree ,tree)))
2354 (org-export-resolve-radio-link
2355 (org-element-map tree 'link 'identity info t)
2356 info))))
2357 ;; Radio targets are case-insensitive.
2358 (should
2359 (org-test-with-temp-text "<<<RADIO>>> radio"
2360 (org-update-radio-target-regexp)
2361 (let* ((tree (org-element-parse-buffer))
2362 (info `(:parse-tree ,tree)))
2363 (org-export-resolve-radio-link
2364 (org-element-map tree 'link 'identity info t)
2365 info))))
2366 ;; Radio target with objects.
2367 (should
2368 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
2369 (org-update-radio-target-regexp)
2370 (let* ((tree (org-element-parse-buffer))
2371 (info `(:parse-tree ,tree)))
2372 (org-export-resolve-radio-link
2373 (org-element-map tree 'link 'identity info t)
2374 info))))
2375 ;; Radio target with objects at its beginning.
2376 (should
2377 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
2378 (org-update-radio-target-regexp)
2379 (let* ((tree (org-element-parse-buffer))
2380 (info `(:parse-tree ,tree)))
2381 (org-export-resolve-radio-link
2382 (org-element-map tree 'link 'identity info t)
2383 info))))
2384 ;; Radio link next to an apostrophe.
2385 (should
2386 (org-test-with-temp-text "<<<radio>>> radio's"
2387 (org-update-radio-target-regexp)
2388 (let* ((tree (org-element-parse-buffer))
2389 (info `(:parse-tree ,tree)))
2390 (org-export-resolve-radio-link
2391 (org-element-map tree 'link 'identity info t)
2392 info))))
2393 ;; Multiple radio targets.
2394 (should
2395 (equal '("radio1" "radio2")
2396 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
2397 (org-update-radio-target-regexp)
2398 (let* ((tree (org-element-parse-buffer))
2399 (info `(:parse-tree ,tree)))
2400 (org-element-map tree 'link
2401 (lambda (link)
2402 (org-element-property
2403 :value (org-export-resolve-radio-link link info)))
2404 info)))))
2405 ;; Radio target is whitespace insensitive.
2406 (should
2407 (org-test-with-temp-text "<<<a radio>>> a\n radio"
2408 (org-update-radio-target-regexp)
2409 (let* ((tree (org-element-parse-buffer))
2410 (info `(:parse-tree ,tree)))
2411 (org-element-map tree 'link
2412 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
2416 ;;; Src-block and example-block
2418 (ert-deftest test-org-export/unravel-code ()
2419 "Test `org-export-unravel-code' function."
2420 ;; Code without reference.
2421 (should
2422 (equal '("(+ 1 1)\n")
2423 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
2424 (org-export-unravel-code (org-element-at-point)))))
2425 ;; Code with reference.
2426 (should
2427 (equal '("(+ 1 1)\n" (1 . "test"))
2428 (org-test-with-temp-text
2429 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
2430 (let ((org-coderef-label-format "(ref:%s)"))
2431 (org-export-unravel-code (org-element-at-point))))))
2432 ;; Code with user-defined reference.
2433 (should
2434 (equal
2435 '("(+ 1 1)\n" (1 . "test"))
2436 (org-test-with-temp-text
2437 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
2438 (let ((org-coderef-label-format "(ref:%s)"))
2439 (org-export-unravel-code (org-element-at-point))))))
2440 ;; Code references keys are relative to the current block.
2441 (should
2442 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
2443 (org-test-with-temp-text "
2444 #+BEGIN_EXAMPLE -n
2445 \(+ 1 1)
2446 #+END_EXAMPLE
2447 #+BEGIN_EXAMPLE +n
2448 \(+ 2 2)
2449 \(+ 3 3) (ref:one)
2450 #+END_EXAMPLE"
2451 (goto-line 5)
2452 (let ((org-coderef-label-format "(ref:%s)"))
2453 (org-export-unravel-code (org-element-at-point)))))))
2455 (ert-deftest test-org-export/format-code-default ()
2456 "Test `org-export-format-code-default' specifications."
2457 ;; Return the empty string when code is empty.
2458 (should
2459 (equal ""
2460 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
2461 (org-export-format-code-default
2462 (org-element-map tree 'src-block 'identity info t) info))))
2463 ;; Number lines, two whitespace characters before the actual loc.
2464 (should
2465 (equal "1 a\n2 b\n"
2466 (org-test-with-parsed-data
2467 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
2468 (org-export-format-code-default
2469 (org-element-map tree 'src-block 'identity info t) info))))
2470 ;; Put references 6 whitespace characters after the widest line,
2471 ;; wrapped within parenthesis.
2472 (should
2473 (equal "123 (a)\n1 (b)\n"
2474 (let ((org-coderef-label-format "(ref:%s)"))
2475 (org-test-with-parsed-data
2476 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
2477 (org-export-format-code-default
2478 (org-element-map tree 'src-block 'identity info t) info))))))
2482 ;;; Smart Quotes
2484 (ert-deftest test-org-export/activate-smart-quotes ()
2485 "Test `org-export-activate-smart-quotes' specifications."
2486 ;; Opening double quotes: standard test.
2487 (should
2488 (equal
2489 '("some &ldquo;paragraph")
2490 (let ((org-export-default-language "en"))
2491 (org-test-with-parsed-data "some \"paragraph"
2492 (org-element-map tree 'plain-text
2493 (lambda (s) (org-export-activate-smart-quotes s :html info))
2494 info)))))
2495 ;; Opening quotes: at the beginning of a paragraph.
2496 (should
2497 (equal
2498 '("&ldquo;begin")
2499 (let ((org-export-default-language "en"))
2500 (org-test-with-parsed-data "\"begin"
2501 (org-element-map tree 'plain-text
2502 (lambda (s) (org-export-activate-smart-quotes s :html info))
2503 info)))))
2504 ;; Opening quotes: after an object.
2505 (should
2506 (equal
2507 '("&ldquo;begin")
2508 (let ((org-export-default-language "en"))
2509 (org-test-with-parsed-data "=verb= \"begin"
2510 (org-element-map tree 'plain-text
2511 (lambda (s) (org-export-activate-smart-quotes s :html info))
2512 info)))))
2513 ;; Closing quotes: standard test.
2514 (should
2515 (equal
2516 '("some&rdquo; paragraph")
2517 (let ((org-export-default-language "en"))
2518 (org-test-with-parsed-data "some\" paragraph"
2519 (org-element-map tree 'plain-text
2520 (lambda (s) (org-export-activate-smart-quotes s :html info))
2521 info)))))
2522 ;; Closing quotes: at the end of a paragraph.
2523 (should
2524 (equal
2525 '("end&rdquo;")
2526 (let ((org-export-default-language "en"))
2527 (org-test-with-parsed-data "end\""
2528 (org-element-map tree 'plain-text
2529 (lambda (s) (org-export-activate-smart-quotes s :html info))
2530 info)))))
2531 ;; Apostrophe: standard test.
2532 (should
2533 (equal
2534 '("It shouldn&rsquo;t fail")
2535 (let ((org-export-default-language "en"))
2536 (org-test-with-parsed-data "It shouldn't fail"
2537 (org-element-map tree 'plain-text
2538 (lambda (s) (org-export-activate-smart-quotes s :html info))
2539 info)))))
2540 ;; Apostrophe: before an object.
2541 (should
2542 (equal
2543 '("a&rsquo;")
2544 (let ((org-export-default-language "en"))
2545 (org-test-with-parsed-data "a'=b="
2546 (org-element-map tree 'plain-text
2547 (lambda (s) (org-export-activate-smart-quotes s :html info))
2548 info)))))
2549 ;; Apostrophe: after an object.
2550 (should
2551 (equal
2552 '("&rsquo;s")
2553 (let ((org-export-default-language "en"))
2554 (org-test-with-parsed-data "=code='s"
2555 (org-element-map tree 'plain-text
2556 (lambda (s) (org-export-activate-smart-quotes s :html info))
2557 info)))))
2558 ;; Special case: isolated quotes.
2559 (should
2560 (equal '("&ldquo;" "&rdquo;")
2561 (let ((org-export-default-language "en"))
2562 (org-test-with-parsed-data "\"$x$\""
2563 (org-element-map tree 'plain-text
2564 (lambda (s) (org-export-activate-smart-quotes s :html info))
2565 info)))))
2566 ;; Smart quotes in secondary strings.
2567 (should
2568 (equal '("&ldquo;" "&rdquo;")
2569 (let ((org-export-default-language "en"))
2570 (org-test-with-parsed-data "* \"$x$\""
2571 (org-element-map tree 'plain-text
2572 (lambda (s) (org-export-activate-smart-quotes s :html info))
2573 info)))))
2574 ;; Smart quotes in document keywords.
2575 (should
2576 (equal '("&ldquo;" "&rdquo;")
2577 (let ((org-export-default-language "en"))
2578 (org-test-with-parsed-data "#+TITLE: \"$x$\""
2579 (org-element-map (plist-get info :title) 'plain-text
2580 (lambda (s) (org-export-activate-smart-quotes s :html info))
2581 info)))))
2582 ;; Smart quotes in parsed affiliated keywords.
2583 (should
2584 (equal '("&ldquo;" "&rdquo;" "Paragraph")
2585 (let ((org-export-default-language "en"))
2586 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
2587 (org-element-map tree 'plain-text
2588 (lambda (s) (org-export-activate-smart-quotes s :html info))
2589 info nil nil t))))))
2593 ;;; Tables
2595 (ert-deftest test-org-export/special-column ()
2596 "Test if the table's special column is properly recognized."
2597 ;; 1. First column is special if it contains only a special marking
2598 ;; characters or empty cells.
2599 (org-test-with-temp-text "
2600 | ! | 1 |
2601 | | 2 |"
2602 (should
2603 (org-export-table-has-special-column-p
2604 (org-element-map
2605 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2606 ;; 2. If the column contains anything else, it isn't special.
2607 (org-test-with-temp-text "
2608 | ! | 1 |
2609 | b | 2 |"
2610 (should-not
2611 (org-export-table-has-special-column-p
2612 (org-element-map
2613 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2614 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
2615 ;; and "!".
2616 (org-test-with-temp-text "
2617 | # | 1 |
2618 | ^ | 2 |
2619 | * | 3 |
2620 | _ | 4 |
2621 | / | 5 |
2622 | $ | 6 |
2623 | ! | 7 |"
2624 (should
2625 (org-export-table-has-special-column-p
2626 (org-element-map
2627 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
2628 ;; 4. A first column with only empty cells isn't considered as
2629 ;; special.
2630 (org-test-with-temp-text "
2631 | | 1 |
2632 | | 2 |"
2633 (should-not
2634 (org-export-table-has-special-column-p
2635 (org-element-map
2636 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
2638 (ert-deftest test-org-export/table-row-is-special-p ()
2639 "Test `org-export-table-row-is-special-p' specifications."
2640 ;; 1. A row is special if it has a special marking character in the
2641 ;; special column.
2642 (org-test-with-parsed-data "| ! | 1 |"
2643 (should
2644 (org-export-table-row-is-special-p
2645 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2646 ;; 2. A row is special when its first field is "/"
2647 (org-test-with-parsed-data "
2648 | / | 1 |
2649 | a | b |"
2650 (should
2651 (org-export-table-row-is-special-p
2652 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2653 ;; 3. A row only containing alignment cookies is also considered as
2654 ;; special.
2655 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
2656 (should
2657 (org-export-table-row-is-special-p
2658 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2659 ;; 4. Everything else isn't considered as special.
2660 (org-test-with-parsed-data "| \alpha | | c |"
2661 (should-not
2662 (org-export-table-row-is-special-p
2663 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
2664 ;; 5. Table's rules are never considered as special rows.
2665 (org-test-with-parsed-data "|---+---|"
2666 (should-not
2667 (org-export-table-row-is-special-p
2668 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
2670 (ert-deftest test-org-export/has-header-p ()
2671 "Test `org-export-table-has-header-p' specifications."
2672 ;; 1. With an header.
2673 (org-test-with-parsed-data "
2674 | a | b |
2675 |---+---|
2676 | c | d |"
2677 (should
2678 (org-export-table-has-header-p
2679 (org-element-map tree 'table 'identity info 'first-match)
2680 info)))
2681 ;; 2. Without an header.
2682 (org-test-with-parsed-data "
2683 | a | b |
2684 | c | d |"
2685 (should-not
2686 (org-export-table-has-header-p
2687 (org-element-map tree 'table 'identity info 'first-match)
2688 info)))
2689 ;; 3. Don't get fooled with starting and ending rules.
2690 (org-test-with-parsed-data "
2691 |---+---|
2692 | a | b |
2693 | c | d |
2694 |---+---|"
2695 (should-not
2696 (org-export-table-has-header-p
2697 (org-element-map tree 'table 'identity info 'first-match)
2698 info))))
2700 (ert-deftest test-org-export/table-row-group ()
2701 "Test `org-export-table-row-group' specifications."
2702 ;; 1. A rule creates a new group.
2703 (should
2704 (equal '(1 rule 2)
2705 (org-test-with-parsed-data "
2706 | a | b |
2707 |---+---|
2708 | 1 | 2 |"
2709 (org-element-map tree 'table-row
2710 (lambda (row)
2711 (if (eq (org-element-property :type row) 'rule) 'rule
2712 (org-export-table-row-group row info)))))))
2713 ;; 2. Special rows are ignored in count.
2714 (should
2715 (equal
2716 '(rule 1)
2717 (org-test-with-parsed-data "
2718 | / | < | > |
2719 |---|---+---|
2720 | | 1 | 2 |"
2721 (org-element-map tree 'table-row
2722 (lambda (row)
2723 (if (eq (org-element-property :type row) 'rule) 'rule
2724 (org-export-table-row-group row info)))
2725 info))))
2726 ;; 3. Double rules also are ignored in count.
2727 (should
2728 (equal '(1 rule rule 2)
2729 (org-test-with-parsed-data "
2730 | a | b |
2731 |---+---|
2732 |---+---|
2733 | 1 | 2 |"
2734 (org-element-map tree 'table-row
2735 (lambda (row)
2736 (if (eq (org-element-property :type row) 'rule) 'rule
2737 (org-export-table-row-group row info))))))))
2739 (ert-deftest test-org-export/table-row-number ()
2740 "Test `org-export-table-row-number' specifications."
2741 ;; Standard test. Number is 0-indexed.
2742 (should
2743 (equal '(0 1)
2744 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
2745 (org-element-map tree 'table-row
2746 (lambda (row) (org-export-table-row-number row info)) info))))
2747 ;; Number ignores separators.
2748 (should
2749 (equal '(0 1)
2750 (org-test-with-parsed-data "
2751 | a | b | c |
2752 |---+---+---|
2753 | d | e | f |"
2754 (org-element-map tree 'table-row
2755 (lambda (row) (org-export-table-row-number row info)) info))))
2756 ;; Number ignores special rows.
2757 (should
2758 (equal '(0 1)
2759 (org-test-with-parsed-data "
2760 | / | < | > |
2761 | | b | c |
2762 |---+-----+-----|
2763 | | <c> | <c> |
2764 | | e | f |"
2765 (org-element-map tree 'table-row
2766 (lambda (row) (org-export-table-row-number row info)) info)))))
2768 (ert-deftest test-org-export/table-cell-width ()
2769 "Test `org-export-table-cell-width' specifications."
2770 ;; 1. Width is primarily determined by width cookies. If no cookie
2771 ;; is found, cell's width is nil.
2772 (org-test-with-parsed-data "
2773 | / | <l> | <6> | <l7> |
2774 | | a | b | c |"
2775 (should
2776 (equal
2777 '(nil 6 7)
2778 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2779 (org-element-map tree 'table-cell 'identity info)))))
2780 ;; 2. The last width cookie has precedence.
2781 (org-test-with-parsed-data "
2782 | <6> |
2783 | <7> |
2784 | a |"
2785 (should
2786 (equal
2787 '(7)
2788 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2789 (org-element-map tree 'table-cell 'identity info)))))
2790 ;; 3. Valid width cookies must have a specific row.
2791 (org-test-with-parsed-data "| <6> | cell |"
2792 (should
2793 (equal
2794 '(nil nil)
2795 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
2796 (org-element-map tree 'table-cell 'identity))))))
2798 (ert-deftest test-org-export/table-cell-alignment ()
2799 "Test `org-export-table-cell-alignment' specifications."
2800 ;; 1. Alignment is primarily determined by alignment cookies.
2801 (should
2802 (equal '(left center right)
2803 (let ((org-table-number-fraction 0.5)
2804 (org-table-number-regexp "^[0-9]+$"))
2805 (org-test-with-parsed-data "| <l> | <c> | <r> |"
2806 (mapcar (lambda (cell)
2807 (org-export-table-cell-alignment cell info))
2808 (org-element-map tree 'table-cell 'identity))))))
2809 ;; 2. The last alignment cookie has precedence.
2810 (should
2811 (equal '(right right right)
2812 (org-test-with-parsed-data "
2813 | <l8> |
2814 | cell |
2815 | <r9> |"
2816 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
2817 (org-element-map tree 'table-cell 'identity)))))
2818 ;; 3. If there's no cookie, cell's contents determine alignment.
2819 ;; A column mostly made of cells containing numbers will align
2820 ;; its cells to the right.
2821 (should
2822 (equal '(right right right)
2823 (let ((org-table-number-fraction 0.5)
2824 (org-table-number-regexp "^[0-9]+$"))
2825 (org-test-with-parsed-data "
2826 | 123 |
2827 | some text |
2828 | 12345 |"
2829 (mapcar (lambda (cell)
2830 (org-export-table-cell-alignment cell info))
2831 (org-element-map tree 'table-cell 'identity))))))
2832 ;; 4. Otherwise, they will be aligned to the left.
2833 (should
2834 (equal '(left left left)
2835 (org-test-with-parsed-data "
2836 | text |
2837 | some text |
2838 | \alpha |"
2839 (mapcar (lambda (cell)
2840 (org-export-table-cell-alignment cell info))
2841 (org-element-map tree 'table-cell 'identity info))))))
2843 (ert-deftest test-org-export/table-cell-borders ()
2844 "Test `org-export-table-cell-borders' specifications."
2845 ;; 1. Recognize various column groups indicators.
2846 (org-test-with-parsed-data "| / | < | > | <> |"
2847 (should
2848 (equal
2849 '((right bottom top) (left bottom top) (right bottom top)
2850 (right left bottom top))
2851 (mapcar (lambda (cell)
2852 (org-export-table-cell-borders cell info))
2853 (org-element-map tree 'table-cell 'identity)))))
2854 ;; 2. Accept shortcuts to define column groups.
2855 (org-test-with-parsed-data "| / | < | < |"
2856 (should
2857 (equal
2858 '((right bottom top) (right left bottom top) (left bottom top))
2859 (mapcar (lambda (cell)
2860 (org-export-table-cell-borders cell info))
2861 (org-element-map tree 'table-cell 'identity)))))
2862 ;; 3. A valid column groups row must start with a "/".
2863 (org-test-with-parsed-data "
2864 | | < |
2865 | a | b |"
2866 (should
2867 (equal '((top) (top) (bottom) (bottom))
2868 (mapcar (lambda (cell)
2869 (org-export-table-cell-borders cell info))
2870 (org-element-map tree 'table-cell 'identity)))))
2871 ;; 4. Take table rules into consideration.
2872 (org-test-with-parsed-data "
2873 | 1 |
2874 |---|
2875 | 2 |"
2876 (should
2877 (equal '((below top) (bottom above))
2878 (mapcar (lambda (cell)
2879 (org-export-table-cell-borders cell info))
2880 (org-element-map tree 'table-cell 'identity)))))
2881 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
2882 ;; (resp. `bottom' and `below') borders. Any special row is
2883 ;; ignored.
2884 (org-test-with-parsed-data "
2885 |---+----|
2886 | / | |
2887 | | 1 |
2888 |---+----|"
2889 (should
2890 (equal '((bottom below top above))
2891 (last
2892 (mapcar (lambda (cell)
2893 (org-export-table-cell-borders cell info))
2894 (org-element-map tree 'table-cell 'identity)))))))
2896 (ert-deftest test-org-export/table-dimensions ()
2897 "Test `org-export-table-dimensions' specifications."
2898 ;; 1. Standard test.
2899 (org-test-with-parsed-data "
2900 | 1 | 2 | 3 |
2901 | 4 | 5 | 6 |"
2902 (should
2903 (equal '(2 . 3)
2904 (org-export-table-dimensions
2905 (org-element-map tree 'table 'identity info 'first-match) info))))
2906 ;; 2. Ignore horizontal rules and special columns.
2907 (org-test-with-parsed-data "
2908 | / | < | > |
2909 | 1 | 2 | 3 |
2910 |---+---+---|
2911 | 4 | 5 | 6 |"
2912 (should
2913 (equal '(2 . 3)
2914 (org-export-table-dimensions
2915 (org-element-map tree 'table 'identity info 'first-match) info)))))
2917 (ert-deftest test-org-export/table-cell-address ()
2918 "Test `org-export-table-cell-address' specifications."
2919 ;; 1. Standard test: index is 0-based.
2920 (org-test-with-parsed-data "| a | b |"
2921 (should
2922 (equal '((0 . 0) (0 . 1))
2923 (org-element-map tree 'table-cell
2924 (lambda (cell) (org-export-table-cell-address cell info))
2925 info))))
2926 ;; 2. Special column isn't counted, nor are special rows.
2927 (org-test-with-parsed-data "
2928 | / | <> |
2929 | | c |"
2930 (should
2931 (equal '(0 . 0)
2932 (org-export-table-cell-address
2933 (car (last (org-element-map tree 'table-cell 'identity info)))
2934 info))))
2935 ;; 3. Tables rules do not count either.
2936 (org-test-with-parsed-data "
2937 | a |
2938 |---|
2939 | b |
2940 |---|
2941 | c |"
2942 (should
2943 (equal '(2 . 0)
2944 (org-export-table-cell-address
2945 (car (last (org-element-map tree 'table-cell 'identity info)))
2946 info))))
2947 ;; 4. Return nil for special cells.
2948 (org-test-with-parsed-data "| / | a |"
2949 (should-not
2950 (org-export-table-cell-address
2951 (org-element-map tree 'table-cell 'identity nil 'first-match)
2952 info))))
2954 (ert-deftest test-org-export/get-table-cell-at ()
2955 "Test `org-export-get-table-cell-at' specifications."
2956 ;; 1. Address ignores special columns, special rows and rules.
2957 (org-test-with-parsed-data "
2958 | / | <> |
2959 | | a |
2960 |---+----|
2961 | | b |"
2962 (should
2963 (equal '("b")
2964 (org-element-contents
2965 (org-export-get-table-cell-at
2966 '(1 . 0)
2967 (org-element-map tree 'table 'identity info 'first-match)
2968 info)))))
2969 ;; 2. Return value for a non-existent address is nil.
2970 (org-test-with-parsed-data "| a |"
2971 (should-not
2972 (org-export-get-table-cell-at
2973 '(2 . 2)
2974 (org-element-map tree 'table 'identity info 'first-match)
2975 info)))
2976 (org-test-with-parsed-data "| / |"
2977 (should-not
2978 (org-export-get-table-cell-at
2979 '(0 . 0)
2980 (org-element-map tree 'table 'identity info 'first-match)
2981 info))))
2983 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
2984 "Test `org-export-table-cell-starts-colgroup-p' specifications."
2985 ;; 1. A cell at a beginning of a row always starts a column group.
2986 (org-test-with-parsed-data "| a |"
2987 (should
2988 (org-export-table-cell-starts-colgroup-p
2989 (org-element-map tree 'table-cell 'identity info 'first-match)
2990 info)))
2991 ;; 2. Special column should be ignored when determining the
2992 ;; beginning of the row.
2993 (org-test-with-parsed-data "
2994 | / | |
2995 | | a |"
2996 (should
2997 (org-export-table-cell-starts-colgroup-p
2998 (org-element-map tree 'table-cell 'identity info 'first-match)
2999 info)))
3000 ;; 2. Explicit column groups.
3001 (org-test-with-parsed-data "
3002 | / | | < |
3003 | a | b | c |"
3004 (should
3005 (equal
3006 '(yes no yes)
3007 (org-element-map tree 'table-cell
3008 (lambda (cell)
3009 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
3010 info)))))
3012 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
3013 "Test `org-export-table-cell-ends-colgroup-p' specifications."
3014 ;; 1. A cell at the end of a row always ends a column group.
3015 (org-test-with-parsed-data "| a |"
3016 (should
3017 (org-export-table-cell-ends-colgroup-p
3018 (org-element-map tree 'table-cell 'identity info 'first-match)
3019 info)))
3020 ;; 2. Special column should be ignored when determining the
3021 ;; beginning of the row.
3022 (org-test-with-parsed-data "
3023 | / | |
3024 | | a |"
3025 (should
3026 (org-export-table-cell-ends-colgroup-p
3027 (org-element-map tree 'table-cell 'identity info 'first-match)
3028 info)))
3029 ;; 3. Explicit column groups.
3030 (org-test-with-parsed-data "
3031 | / | < | |
3032 | a | b | c |"
3033 (should
3034 (equal
3035 '(yes no yes)
3036 (org-element-map tree 'table-cell
3037 (lambda (cell)
3038 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
3039 info)))))
3041 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
3042 "Test `org-export-table-row-starts-rowgroup-p' specifications."
3043 ;; 1. A row at the beginning of a table always starts a row group.
3044 ;; So does a row following a table rule.
3045 (org-test-with-parsed-data "
3046 | a |
3047 |---|
3048 | b |"
3049 (should
3050 (equal
3051 '(yes no yes)
3052 (org-element-map tree 'table-row
3053 (lambda (row)
3054 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
3055 info))))
3056 ;; 2. Special rows should be ignored when determining the beginning
3057 ;; of the row.
3058 (org-test-with-parsed-data "
3059 | / | < |
3060 | | a |
3061 |---+---|
3062 | / | < |
3063 | | b |"
3064 (should
3065 (equal
3066 '(yes no yes)
3067 (org-element-map tree 'table-row
3068 (lambda (row)
3069 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
3070 info)))))
3072 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
3073 "Test `org-export-table-row-ends-rowgroup-p' specifications."
3074 ;; 1. A row at the end of a table always ends a row group. So does
3075 ;; a row preceding a table rule.
3076 (org-test-with-parsed-data "
3077 | a |
3078 |---|
3079 | b |"
3080 (should
3081 (equal
3082 '(yes no yes)
3083 (org-element-map tree 'table-row
3084 (lambda (row)
3085 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
3086 info))))
3087 ;; 2. Special rows should be ignored when determining the beginning
3088 ;; of the row.
3089 (org-test-with-parsed-data "
3090 | | a |
3091 | / | < |
3092 |---+---|
3093 | | b |
3094 | / | < |"
3095 (should
3096 (equal
3097 '(yes no yes)
3098 (org-element-map tree 'table-row
3099 (lambda (row)
3100 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
3101 info)))))
3103 (ert-deftest test-org-export/table-row-in-header-p ()
3104 "Test `org-export-table-row-in-header-p' specifications."
3105 ;; Standard test. Separators are always nil.
3106 (should
3107 (equal
3108 '(yes no no)
3109 (org-test-with-parsed-data "| a |\n|---|\n| b |"
3110 (org-element-map tree 'table-row
3111 (lambda (row)
3112 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
3113 ;; Nil when there is no header.
3114 (should
3115 (equal
3116 '(no no)
3117 (org-test-with-parsed-data "| a |\n| b |"
3118 (org-element-map tree 'table-row
3119 (lambda (row)
3120 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
3122 (ert-deftest test-org-export/table-row-starts-header-p ()
3123 "Test `org-export-table-row-starts-header-p' specifications."
3124 ;; 1. Only the row starting the first row group starts the table
3125 ;; header.
3126 (org-test-with-parsed-data "
3127 | a |
3128 | b |
3129 |---|
3130 | c |"
3131 (should
3132 (equal
3133 '(yes no no no)
3134 (org-element-map tree 'table-row
3135 (lambda (row)
3136 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
3137 info))))
3138 ;; 2. A row cannot start an header if there's no header in the
3139 ;; table.
3140 (org-test-with-parsed-data "
3141 | a |
3142 |---|"
3143 (should-not
3144 (org-export-table-row-starts-header-p
3145 (org-element-map tree 'table-row 'identity info 'first-match)
3146 info))))
3148 (ert-deftest test-org-export/table-row-ends-header-p ()
3149 "Test `org-export-table-row-ends-header-p' specifications."
3150 ;; 1. Only the row starting the first row group starts the table
3151 ;; header.
3152 (org-test-with-parsed-data "
3153 | a |
3154 | b |
3155 |---|
3156 | c |"
3157 (should
3158 (equal
3159 '(no yes no no)
3160 (org-element-map tree 'table-row
3161 (lambda (row)
3162 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
3163 info))))
3164 ;; 2. A row cannot start an header if there's no header in the
3165 ;; table.
3166 (org-test-with-parsed-data "
3167 | a |
3168 |---|"
3169 (should-not
3170 (org-export-table-row-ends-header-p
3171 (org-element-map tree 'table-row 'identity info 'first-match)
3172 info))))
3176 ;;; Tables of Contents
3178 (ert-deftest test-org-export/collect-headlines ()
3179 "Test `org-export-collect-headlines' specifications."
3180 ;; Standard test.
3181 (should
3182 (= 2
3183 (length
3184 (org-test-with-parsed-data "* H1\n** H2"
3185 (org-export-collect-headlines info)))))
3186 ;; Do not collect headlines below optional argument.
3187 (should
3188 (= 1
3189 (length
3190 (org-test-with-parsed-data "* H1\n** H2"
3191 (org-export-collect-headlines info 1)))))
3192 ;; Never collect headlines below maximum headline level.
3193 (should
3194 (= 1
3195 (length
3196 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3197 (org-export-collect-headlines info)))))
3198 (should
3199 (= 1
3200 (length
3201 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
3202 (org-export-collect-headlines info 2)))))
3203 ;; Collect headlines locally.
3204 (should
3205 (= 2
3206 (org-test-with-parsed-data "* H1\n** H2\n** H3"
3207 (let ((scope (org-element-map tree 'headline #'identity info t)))
3208 (length (org-export-collect-headlines info nil scope))))))
3209 ;; When collecting locally, optional level is relative.
3210 (should
3211 (= 1
3212 (org-test-with-parsed-data "* H1\n** H2\n*** H3"
3213 (let ((scope (org-element-map tree 'headline #'identity info t)))
3214 (length (org-export-collect-headlines info 1 scope)))))))
3218 ;;; Templates
3220 (ert-deftest test-org-export/inner-template ()
3221 "Test `inner-template' translator specifications."
3222 (should
3223 (equal "Success!"
3224 (org-test-with-temp-text "* Headline"
3225 (org-export-as
3226 (org-export-create-backend
3227 :transcoders
3228 '((inner-template . (lambda (contents info) "Success!"))
3229 (headline . (lambda (h c i) "Headline"))))))))
3230 ;; Inner template is applied even in a "body-only" export.
3231 (should
3232 (equal "Success!"
3233 (org-test-with-temp-text "* Headline"
3234 (org-export-as
3235 (org-export-create-backend
3236 :transcoders '((inner-template . (lambda (c i) "Success!"))
3237 (headline . (lambda (h c i) "Headline"))))
3238 nil nil 'body-only)))))
3240 (ert-deftest test-org-export/template ()
3241 "Test `template' translator specifications."
3242 (should
3243 (equal "Success!"
3244 (org-test-with-temp-text "* Headline"
3245 (org-export-as
3246 (org-export-create-backend
3247 :transcoders '((template . (lambda (contents info) "Success!"))
3248 (headline . (lambda (h c i) "Headline"))))))))
3249 ;; Template is not applied in a "body-only" export.
3250 (should-not
3251 (equal "Success!"
3252 (org-test-with-temp-text "* Headline"
3253 (org-export-as
3254 (org-export-create-backend
3255 :transcoders '((template . (lambda (contents info) "Success!"))
3256 (headline . (lambda (h c i) "Headline"))))
3257 nil nil 'body-only)))))
3261 ;;; Topology
3263 (ert-deftest test-org-export/get-next-element ()
3264 "Test `org-export-get-next-element' specifications."
3265 ;; Standard test.
3266 (should
3267 (equal "b"
3268 (org-test-with-parsed-data "* Headline\n*a* b"
3269 (org-export-get-next-element
3270 (org-element-map tree 'bold 'identity info t) info))))
3271 ;; Return nil when no previous element.
3272 (should-not
3273 (org-test-with-parsed-data "* Headline\na *b*"
3274 (org-export-get-next-element
3275 (org-element-map tree 'bold 'identity info t) info)))
3276 ;; Non-exportable elements are ignored.
3277 (should-not
3278 (let ((org-export-with-timestamps nil))
3279 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
3280 (org-export-get-next-element
3281 (org-element-map tree 'entity 'identity info t) info))))
3282 ;; Find next element in secondary strings.
3283 (should
3284 (eq 'verbatim
3285 (org-test-with-parsed-data "* a =verb="
3286 (org-element-type
3287 (org-export-get-next-element
3288 (org-element-map tree 'plain-text 'identity info t) info)))))
3289 (should
3290 (eq 'verbatim
3291 (org-test-with-parsed-data "* /italic/ =verb="
3292 (org-element-type
3293 (org-export-get-next-element
3294 (org-element-map tree 'italic 'identity info t) info)))))
3295 ;; Find next element in document keywords.
3296 (should
3297 (eq 'verbatim
3298 (org-test-with-parsed-data "#+TITLE: a =verb="
3299 (org-element-type
3300 (org-export-get-next-element
3301 (org-element-map
3302 (plist-get info :title) 'plain-text 'identity info t) info)))))
3303 ;; Find next element in parsed affiliated keywords.
3304 (should
3305 (eq 'verbatim
3306 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
3307 (org-element-type
3308 (org-export-get-next-element
3309 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3310 ;; With optional argument N, return a list containing all the
3311 ;; following elements.
3312 (should
3313 (equal
3314 '(bold code underline)
3315 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3316 (mapcar 'car
3317 (org-export-get-next-element
3318 (org-element-map tree 'italic 'identity info t) info t)))))
3319 ;; When N is a positive integer, return a list containing up to
3320 ;; N following elements.
3321 (should
3322 (equal
3323 '(bold code)
3324 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
3325 (mapcar 'car
3326 (org-export-get-next-element
3327 (org-element-map tree 'italic 'identity info t) info 2))))))
3329 (ert-deftest test-org-export/get-previous-element ()
3330 "Test `org-export-get-previous-element' specifications."
3331 ;; Standard test.
3332 (should
3333 (equal "a "
3334 (org-test-with-parsed-data "* Headline\na *b*"
3335 (org-export-get-previous-element
3336 (org-element-map tree 'bold 'identity info t) info))))
3337 ;; Return nil when no previous element.
3338 (should-not
3339 (org-test-with-parsed-data "* Headline\n*a* b"
3340 (org-export-get-previous-element
3341 (org-element-map tree 'bold 'identity info t) info)))
3342 ;; Non-exportable elements are ignored.
3343 (should-not
3344 (let ((org-export-with-timestamps nil))
3345 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
3346 (org-export-get-previous-element
3347 (org-element-map tree 'entity 'identity info t) info))))
3348 ;; Find previous element in secondary strings.
3349 (should
3350 (eq 'verbatim
3351 (org-test-with-parsed-data "* =verb= a"
3352 (org-element-type
3353 (org-export-get-previous-element
3354 (org-element-map tree 'plain-text 'identity info t) info)))))
3355 (should
3356 (eq 'verbatim
3357 (org-test-with-parsed-data "* =verb= /italic/"
3358 (org-element-type
3359 (org-export-get-previous-element
3360 (org-element-map tree 'italic 'identity info t) info)))))
3361 ;; Find previous element in document keywords.
3362 (should
3363 (eq 'verbatim
3364 (org-test-with-parsed-data "#+TITLE: =verb= a"
3365 (org-element-type
3366 (org-export-get-previous-element
3367 (org-element-map
3368 (plist-get info :title) 'plain-text 'identity info t) info)))))
3369 ;; Find previous element in parsed affiliated keywords.
3370 (should
3371 (eq 'verbatim
3372 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
3373 (org-element-type
3374 (org-export-get-previous-element
3375 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
3376 ;; With optional argument N, return a list containing up to
3377 ;; N previous elements.
3378 (should
3379 (equal '(underline italic bold)
3380 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3381 (mapcar 'car
3382 (org-export-get-previous-element
3383 (org-element-map tree 'code 'identity info t) info t)))))
3384 ;; When N is a positive integer, return a list containing up to
3385 ;; N previous elements.
3386 (should
3387 (equal '(italic bold)
3388 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
3389 (mapcar 'car
3390 (org-export-get-previous-element
3391 (org-element-map tree 'code 'identity info t) info 2))))))
3394 (provide 'test-ox)
3395 ;;; test-org-export.el end here