ox-html: Plain list supports arbitrary attributes
[org-mode.git] / testing / lisp / test-ox.el
blob6d8b1dd4cd25a856083fce53b8b08544a274bbeb
1 ;;; test-ox.el --- Tests for ox.el -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2016 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 (require 'cl-lib)
26 (unless (featurep 'ox)
27 (signal 'missing-test-dependency "org-export"))
29 (defun org-test-default-backend ()
30 "Return a default export back-end.
31 This back-end simply returns parsed data as Org syntax."
32 (org-export-create-backend
33 :transcoders
34 (mapcar (lambda (type)
35 (cons type
36 (lambda (o c _)
37 (funcall
38 (intern (format "org-element-%s-interpreter" type))
39 o c))))
40 (append org-element-all-elements org-element-all-objects))))
42 (defmacro org-test-with-parsed-data (data &rest body)
43 "Execute body with parsed data available.
44 DATA is a string containing the data to be parsed. BODY is the
45 body to execute. Parse tree is available under the `tree'
46 variable, and communication channel under `info'."
47 (declare (debug (form body)) (indent 1))
48 `(org-test-with-temp-text ,data
49 (org-export--delete-comment-trees)
50 (let* ((tree (org-element-parse-buffer))
51 (info (org-combine-plists
52 (org-export--get-export-attributes)
53 (org-export-get-environment))))
54 (org-export--prune-tree tree info)
55 (org-export--remove-uninterpreted-data tree info)
56 (let ((info (org-combine-plists
57 info (org-export--collect-tree-properties tree info))))
58 ,@body))))
62 ;;; Internal Tests
64 (ert-deftest test-org-export/bind-keyword ()
65 "Test reading #+BIND: keywords."
66 ;; Test with `org-export-allow-bind-keywords' set to t.
67 (should
68 (org-test-with-temp-text "#+BIND: test-ox-var value"
69 (let ((org-export-allow-bind-keywords t))
70 (org-export-get-environment)
71 (eq test-ox-var 'value))))
72 ;; Test with `org-export-allow-bind-keywords' set to nil.
73 (should-not
74 (org-test-with-temp-text "#+BIND: test-ox-var value"
75 (let ((org-export-allow-bind-keywords nil))
76 (org-export-get-environment)
77 (boundp 'test-ox-var))))
78 ;; BIND keywords are case-insensitive.
79 (should
80 (org-test-with-temp-text "#+bind: test-ox-var value"
81 (let ((org-export-allow-bind-keywords t))
82 (org-export-get-environment)
83 (eq test-ox-var 'value))))
84 ;; Preserve order of BIND keywords.
85 (should
86 (org-test-with-temp-text "#+BIND: test-ox-var 1\n#+BIND: test-ox-var 2"
87 (let ((org-export-allow-bind-keywords t))
88 (org-export-get-environment)
89 (eq test-ox-var 2))))
90 ;; Read BIND keywords in setup files.
91 (should
92 (org-test-with-temp-text
93 (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
94 (let ((org-export-allow-bind-keywords t))
95 (org-export-get-environment)
96 (eq variable 'value))))
97 ;; Verify that bound variables are seen during export.
98 (should
99 (equal "Yes\n"
100 (org-test-with-temp-text "#+BIND: test-ox-var value"
101 (let ((org-export-allow-bind-keywords t))
102 (org-export-as
103 (org-export-create-backend
104 :transcoders
105 '((section . (lambda (s c i)
106 (if (eq test-ox-var 'value) "Yes" "No")))))))))))
108 (ert-deftest test-org-export/parse-option-keyword ()
109 "Test reading all standard #+OPTIONS: items."
110 (should
111 (let ((options
112 (org-export--parse-option-keyword
113 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t \
114 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil \
115 stat:t title:t")))
116 (and (eq (plist-get options :headline-levels) 1)
117 (eq (plist-get options :section-numbers) t)
118 (eq (plist-get options :preserve-breaks) t)
119 (eq (plist-get options :time-stamp-file) t)
120 (eq (plist-get options :with-archived-trees) t)
121 (eq (plist-get options :with-author) t)
122 (eq (plist-get options :with-drawers) t)
123 (eq (plist-get options :with-email) t)
124 (eq (plist-get options :with-emphasize) t)
125 (eq (plist-get options :with-entities) t)
126 (eq (plist-get options :with-fixed-width) t)
127 (eq (plist-get options :with-footnotes) t)
128 (eq (plist-get options :with-priority) t)
129 (eq (plist-get options :with-special-strings) t)
130 (eq (plist-get options :with-sub-superscript) t)
131 (eq (plist-get options :with-toc) t)
132 (eq (plist-get options :with-tables) t)
133 (eq (plist-get options :with-tags) t)
134 (eq (plist-get options :with-tasks) t)
135 (eq (plist-get options :with-timestamps) t)
136 (eq (plist-get options :with-todo-keywords) t)
137 (eq (plist-get options :with-inlinetasks) nil)
138 (eq (plist-get options :with-statistics-cookies) t)
139 (eq (plist-get options :with-title) t))))
140 ;; Test some special values.
141 (should
142 (let ((options
143 (org-export--parse-option-keyword
144 "arch:headline d:(\"TEST\") ^:{} toc:1 tags:not-in-toc tasks:todo \
145 num:2 <:active")))
146 (and (eq (plist-get options :with-archived-trees) 'headline)
147 (eq (plist-get options :with-sub-superscript) '{})
148 (eq (plist-get options :with-toc) 1)
149 (eq (plist-get options :with-tags) 'not-in-toc)
150 (eq (plist-get options :with-tasks) 'todo)
151 (eq (plist-get options :section-numbers) 2)
152 (eq (plist-get options :with-timestamps) 'active)
153 (equal (plist-get options :with-drawers) '("TEST")))))
154 ;; Test back-end specific values.
155 (should
156 (equal
157 (org-export--parse-option-keyword
158 "opt:t" (org-export-create-backend :options '((:option nil "opt"))))
159 '(:option t)))
160 ;; More than one property can refer to the same option item.
161 (should
162 (equal '(:opt1 t :opt2 t)
163 (org-export--parse-option-keyword
164 "opt:t"
165 (org-export-create-backend
166 :options '((:opt1 nil "opt") (:opt2 nil "opt")))))))
168 (ert-deftest test-org-export/get-inbuffer-options ()
169 "Test reading all standard export keywords."
170 ;; Properties should follow buffer order.
171 (should
172 (equal
173 (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
174 (org-export--get-inbuffer-options))
175 '(:language "fr" :creator "Me" :email "email")))
176 ;; Test `space' behaviour.
177 (should
178 (equal
179 (let ((back-end (org-export-create-backend
180 :options '((:keyword "KEYWORD" nil nil space)))))
181 (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: spaces"
182 (org-export--get-inbuffer-options back-end)))
183 '(:keyword "With spaces")))
184 ;; Test `newline' behaviour.
185 (should
186 (equal
187 (let ((back-end (org-export-create-backend
188 :options '((:keyword "KEYWORD" nil nil newline)))))
189 (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: two lines"
190 (org-export--get-inbuffer-options back-end)))
191 '(:keyword "With\ntwo lines")))
192 ;; Test `split' behaviour.
193 (should
194 (equal
195 (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
196 (org-export--get-inbuffer-options))
197 '(:select-tags ("a" "b"))))
198 ;; Test `parse' behaviour. `parse' implies `space' but preserve
199 ;; line breaks. Multi-line objects are allowed.
200 (should
201 (org-element-map
202 (org-test-with-temp-text "#+TITLE: *bold*"
203 (plist-get (org-export--get-inbuffer-options) :title))
204 'bold #'identity nil t))
205 (should
206 (equal
207 (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
208 (plist-get (org-export--get-inbuffer-options) :title))
209 '("Some title with spaces")))
210 (should
211 (org-element-map
212 (org-test-with-temp-text "#+TITLE: Some title\\\\\n#+TITLE: with spaces"
213 (plist-get (org-export--get-inbuffer-options) :title))
214 'line-break #'identity nil t))
215 (should
216 (org-element-map
217 (org-test-with-temp-text "#+TITLE: *bold\n#+TITLE: sentence*"
218 (plist-get (org-export--get-inbuffer-options) :title))
219 'bold #'identity nil t))
220 ;; Options set through SETUPFILE.
221 (should
222 (equal
223 (org-test-with-temp-text
224 (format "#+DESCRIPTION: l1
225 #+LANGUAGE: es
226 #+SELECT_TAGS: a
227 #+TITLE: a
228 #+SETUPFILE: \"%s/examples/setupfile.org\"
229 #+LANGUAGE: fr
230 #+SELECT_TAGS: c
231 #+TITLE: c"
232 org-test-dir)
233 (org-export--get-inbuffer-options))
234 '(:language "fr" :select-tags ("a" "b" "c") :title ("a b c"))))
235 ;; More than one property can refer to the same buffer keyword.
236 (should
237 (equal '(:k2 "value" :k1 "value")
238 (let ((backend (org-export-create-backend
239 :options '((:k1 "KEYWORD")
240 (:k2 "KEYWORD")))))
241 (org-test-with-temp-text "#+KEYWORD: value"
242 (org-export--get-inbuffer-options backend)))))
243 ;; Keywords in commented subtrees are ignored.
244 (should-not
245 (equal "Me"
246 (org-test-with-parsed-data "* COMMENT H1\n#+AUTHOR: Me"
247 (plist-get info :author))))
248 (should-not
249 (equal "Mine"
250 (org-test-with-parsed-data "* COMMENT H1\n** H2\n#+EMAIL: Mine"
251 (plist-get info :email)))))
253 (ert-deftest test-org-export/get-subtree-options ()
254 "Test setting options from headline's properties."
255 ;; EXPORT_TITLE.
256 (should
257 (equal '("Subtree Title")
258 (org-test-with-temp-text "#+TITLE: Title
259 * Headline<point>
260 :PROPERTIES:
261 :EXPORT_TITLE: Subtree Title
262 :END:
263 Paragraph"
264 (plist-get (org-export-get-environment nil t) :title))))
265 ;; EXPORT_OPTIONS.
266 (should
267 (= 2
268 (org-test-with-temp-text "#+OPTIONS: H:1
269 * Headline<point>
270 :PROPERTIES:
271 :EXPORT_OPTIONS: H:2
272 :END:
273 Paragraph"
274 (plist-get (org-export-get-environment nil t) :headline-levels))))
275 ;; EXPORT_DATE.
276 (should
277 (equal '("29-03-2012")
278 (org-test-with-temp-text "#+DATE: today
279 * Headline<point>
280 :PROPERTIES:
281 :EXPORT_DATE: 29-03-2012
282 :END:
283 Paragraph"
284 (plist-get (org-export-get-environment nil t) :date))))
285 ;; Properties with `split' behaviour are stored as a list of
286 ;; strings.
287 (should
288 (equal '("a" "b")
289 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
290 * Headline<point>
291 :PROPERTIES:
292 :EXPORT_EXCLUDE_TAGS: a b
293 :END:
294 Paragraph"
295 (plist-get (org-export-get-environment nil t) :exclude-tags))))
296 ;; Handle :PROPERTY+: syntax.
297 (should
298 (equal '("a" "b")
299 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
300 * Headline<point>
301 :PROPERTIES:
302 :EXPORT_EXCLUDE_TAGS: a
303 :EXPORT_EXCLUDE_TAGS+: b
304 :END:
305 Paragraph"
306 (plist-get (org-export-get-environment nil t) :exclude-tags))))
307 ;; Export properties are case-insensitive.
308 (should
309 (equal '("29-03-2012")
310 (org-test-with-temp-text "* Headline
311 :PROPERTIES:
312 :EXPORT_Date: 29-03-2012
313 :END:
314 Paragraph"
315 (plist-get (org-export-get-environment nil t) :date))))
316 ;; Still grab correct options when section above is empty.
317 (should
318 (equal '("H1")
319 (org-test-with-temp-text "* H1\n** H11\n** H12<point>"
320 (plist-get (org-export-get-environment nil t) :title))))
321 ;; More than one property can refer to the same node property.
322 (should
323 (equal '("1" "1")
324 (org-test-with-temp-text
325 "* H\n:PROPERTIES:\n:EXPORT_A: 1\n:END:\n<point>"
326 (let* ((backend (org-export-create-backend
327 :options '((:k1 "A")
328 (:k2 "A"))))
329 (options (org-export-get-environment backend t)))
330 (list (plist-get options :k1) (plist-get options :k2)))))))
332 (ert-deftest test-org-export/set-title ()
333 "Test title setting."
334 ;; Without TITLE keyword.
335 (should
336 (equal
338 (let (org-export-filter-body-functions
339 org-export-filter-final-output-functions)
340 (org-test-with-temp-text "Test"
341 (org-export-as
342 (org-export-create-backend
343 :transcoders
344 '((template . (lambda (text info)
345 (org-element-interpret-data
346 (plist-get info :title)))))))))))
347 ;; With a blank TITLE keyword.
348 (should
349 (equal
351 (let (org-export-filter-body-functions
352 org-export-filter-final-output-functions)
353 (org-test-with-temp-text "#+TITLE:\nTest"
354 (org-export-as
355 (org-export-create-backend
356 :transcoders
357 '((template . (lambda (text info)
358 (org-element-interpret-data
359 (plist-get info :title)))))))))))
360 ;; With a non-empty TITLE keyword.
361 (should
362 (equal
363 "Title"
364 (org-test-with-temp-text "#+TITLE: Title\nTest"
365 (org-export-as
366 (org-export-create-backend
367 :transcoders
368 '((template . (lambda (text info)
369 (org-element-interpret-data
370 (plist-get info :title))))))))))
371 ;; When exporting a subtree, its heading becomes the headline of the
372 ;; document...
373 (should
374 (equal
375 "Headline"
376 (org-test-with-temp-text "* Headline\nBody"
377 (org-export-as
378 (org-export-create-backend
379 :transcoders
380 '((template . (lambda (text info)
381 (org-element-interpret-data
382 (plist-get info :title))))))
383 'subtree))))
384 ;; ... unless there is an EXPORT_TITLE property at the root of the
385 ;; subtree.
386 (should
387 (equal
389 (org-test-with-temp-text
390 "* A\n :PROPERTIES:\n :EXPORT_TITLE: B\n :END:\nBody"
391 (org-export-as
392 (org-export-create-backend
393 :transcoders
394 '((template . (lambda (text info)
395 (org-element-interpret-data
396 (plist-get info :title))))))
397 'subtree)))))
399 (ert-deftest test-org-export/handle-options ()
400 "Test if export options have an impact on output."
401 ;; Test exclude tags for headlines and inlinetasks.
402 (should
403 (equal ""
404 (let (org-export-filter-body-functions
405 org-export-filter-final-output-functions)
406 (org-test-with-temp-text "* Head1 :noexp:"
407 (org-export-as (org-test-default-backend)
408 nil nil nil '(:exclude-tags ("noexp")))))))
409 (should
410 (equal "#+FILETAGS: noexp\n"
411 (let (org-export-filter-body-functions
412 org-export-filter-final-output-functions)
413 (org-test-with-temp-text "#+FILETAGS: noexp\n* Head1"
414 (org-export-as (org-test-default-backend)
415 nil nil nil '(:exclude-tags ("noexp")))))))
416 ;; Test include tags for headlines and inlinetasks.
417 (should
418 (equal (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
419 (let ((org-tags-column 0))
420 (org-export-as (org-test-default-backend)
421 nil nil nil '(:select-tags ("exp")))))
422 "* H2\n** Sub :exp:\n*** Sub Sub\n"))
423 ;; If there is an include tag, ignore the section before the first
424 ;; headline, if any.
425 (should
426 (equal (org-test-with-temp-text "First section\n* H1 :exp:\nBody"
427 (let ((org-tags-column 0))
428 (org-export-as (org-test-default-backend)
429 nil nil nil '(:select-tags ("exp")))))
430 "* H1 :exp:\nBody\n"))
431 (should
432 (equal (org-test-with-temp-text "#+FILETAGS: exp\nFirst section\n* H1\nBody"
433 (org-export-as (org-test-default-backend)
434 nil nil nil '(:select-tags ("exp"))))
435 "* H1\nBody\n"))
436 (should-not
437 (equal (org-test-with-temp-text "* H1 :exp:\nBody"
438 (let ((org-tags-column 0))
439 (org-export-as (org-test-default-backend)
440 nil nil nil '(:select-tags ("exp")))))
441 "* H1 :exp:\n"))
442 ;; Test mixing include tags and exclude tags.
443 (should
444 (string-match
445 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
446 (org-test-with-temp-text "
447 * Head1 :export:
448 ** Sub-Head1 :noexport:
449 ** Sub-Head2
450 * Head2 :noexport:
451 ** Sub-Head1 :export:"
452 (org-export-as (org-test-default-backend) nil nil nil
453 '(:select-tags ("export") :exclude-tags ("noexport"))))))
454 ;; Ignore tasks.
455 (should
456 (equal ""
457 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
458 org-export-filter-body-functions
459 org-export-filter-final-output-functions)
460 (org-test-with-temp-text "* TODO Head1"
461 (org-export-as (org-test-default-backend)
462 nil nil nil '(:with-tasks nil))))))
463 (should
464 (equal "* TODO Head1\n"
465 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
466 (org-test-with-temp-text "* TODO Head1"
467 (org-export-as (org-test-default-backend)
468 nil nil nil '(:with-tasks t))))))
469 ;; Archived tree.
470 (should
471 (equal ""
472 (let (org-export-filter-body-functions
473 org-export-filter-final-output-functions)
474 (org-test-with-temp-text "* Head1 :archive:"
475 (let ((org-archive-tag "archive"))
476 (org-export-as (org-test-default-backend)
477 nil nil nil '(:with-archived-trees nil)))))))
478 (should
479 (string-match
480 "\\* Head1[ \t]+:archive:"
481 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
482 (let ((org-archive-tag "archive"))
483 (org-export-as (org-test-default-backend) nil nil nil
484 '(:with-archived-trees headline))))))
485 (should
486 (string-match
487 "\\`\\* Head1[ \t]+:archive:\n\\'"
488 (org-test-with-temp-text "* Head1 :archive:"
489 (let ((org-archive-tag "archive"))
490 (org-export-as (org-test-default-backend)
491 nil nil nil '(:with-archived-trees t))))))
492 ;; Broken links. Depending on `org-export-with-broken-links', raise
493 ;; an error, ignore link or mark is as broken in output.
494 (should-error
495 (org-test-with-temp-text "[[#broken][link]]"
496 (let ((backend
497 (org-export-create-backend
498 :transcoders
499 '((section . (lambda (_e c _i) c))
500 (paragraph . (lambda (_e c _i) c))
501 (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
502 (org-export-as backend nil nil nil '(:with-broken-links nil)))))
503 (should
504 (org-test-with-temp-text "[[#broken][link]]"
505 (let ((backend
506 (org-export-create-backend
507 :transcoders
508 '((section . (lambda (_e c _i) c))
509 (paragraph . (lambda (_e c _i) c))
510 (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
511 (org-export-as backend nil nil nil '(:with-broken-links t)))))
512 (should
513 (org-test-with-temp-text "[[#broken][link]]"
514 (let ((backend
515 (org-export-create-backend
516 :transcoders
517 '((section . (lambda (_e c _i) c))
518 (paragraph . (lambda (_e c _i) c))
519 (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
520 (org-string-nw-p
521 (org-export-as backend nil nil nil '(:with-broken-links mark))))))
522 ;; Clocks.
523 (should
524 (string-match "CLOCK: \\[2012-04-29 .* 10:45\\]"
525 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
526 (org-export-as (org-test-default-backend)
527 nil nil nil '(:with-clocks t)))))
528 (should
529 (equal ""
530 (let (org-export-filter-body-functions
531 org-export-filter-final-output-functions)
532 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
533 (org-export-as (org-test-default-backend)
534 nil nil nil '(:with-clocks nil))))))
535 ;; Drawers.
536 (should
537 (equal ""
538 (let (org-export-filter-body-functions
539 org-export-filter-final-output-functions)
540 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
541 (org-export-as (org-test-default-backend)
542 nil nil nil '(:with-drawers nil))))))
543 (should
544 (equal ":TEST:\ncontents\n:END:\n"
545 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
546 (org-export-as (org-test-default-backend)
547 nil nil nil '(:with-drawers t)))))
548 (should
549 (equal ":FOO:\nkeep\n:END:\n"
550 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
551 (org-export-as (org-test-default-backend)
552 nil nil nil '(:with-drawers ("FOO"))))))
553 (should
554 (equal ":FOO:\nkeep\n:END:\n"
555 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
556 (org-export-as (org-test-default-backend)
557 nil nil nil '(:with-drawers (not "BAR"))))))
558 ;; Fixed-width.
559 (should
560 (equal ": A\n"
561 (org-test-with-temp-text ": A"
562 (org-export-as (org-test-default-backend) nil nil nil
563 '(:with-fixed-width t)))))
564 (should
565 (equal ""
566 (let (org-export-filter-body-functions
567 org-export-filter-final-output-functions)
568 (org-test-with-temp-text ": A"
569 (org-export-as (org-test-default-backend) nil nil nil
570 '(:with-fixed-width nil))))))
571 ;; Footnotes.
572 (should
573 (equal "Footnote?"
574 (let ((org-footnote-section nil))
575 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
576 (org-trim (org-export-as (org-test-default-backend)
577 nil nil nil '(:with-footnotes nil)))))))
578 (should
579 (equal "Footnote?[fn:1]\n\n[fn:1] Def"
580 (let ((org-footnote-section nil))
581 (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
582 (org-trim (org-export-as (org-test-default-backend)
583 nil nil nil '(:with-footnotes t)))))))
584 ;; Inlinetasks.
585 (when (featurep 'org-inlinetask)
586 (should
587 (equal
589 (let ((org-inlinetask-min-level 15)
590 org-export-filter-body-functions
591 org-export-filter-final-output-functions)
592 (org-test-with-temp-text "*************** Task"
593 (org-export-as (org-test-default-backend)
594 nil nil nil '(:with-inlinetasks nil))))))
595 (should
596 (equal
598 (let ((org-inlinetask-min-level 15)
599 org-export-filter-body-functions
600 org-export-filter-final-output-functions)
601 (org-test-with-temp-text
602 "*************** Task\nContents\n*************** END"
603 (org-export-as (org-test-default-backend)
604 nil nil nil '(:with-inlinetasks nil)))))))
605 ;; Plannings.
606 (should
607 (string-match
608 "* H\nCLOSED: \\[2012-04-29 .* 10:45\\]"
609 (let ((org-closed-string "CLOSED:"))
610 (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
611 (org-export-as (org-test-default-backend)
612 nil nil nil '(:with-planning t))))))
613 (should
614 (equal "* H\n"
615 (let ((org-closed-string "CLOSED:"))
616 (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
617 (org-export-as (org-test-default-backend)
618 nil nil nil '(:with-planning nil))))))
619 ;; Property Drawers.
620 (should
621 (equal "* H1\n"
622 (org-test-with-temp-text
623 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
624 (org-export-as (org-test-default-backend)
625 nil nil nil '(:with-properties nil)))))
626 (should
627 (equal "* H1\n:PROPERTIES:\n:PROP: value\n:END:\n"
628 (org-test-with-temp-text
629 "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
630 (org-export-as (org-test-default-backend)
631 nil nil nil '(:with-properties t)))))
632 (should
633 (equal "* H1\n:PROPERTIES:\n:B: 2\n:END:\n"
634 (org-test-with-temp-text
635 "* H1\n :PROPERTIES:\n :A: 1\n :B: 2\n:END:"
636 (org-export-as (org-test-default-backend)
637 nil nil nil '(:with-properties ("B"))))))
638 ;; Statistics cookies.
639 (should
640 (equal "* Stats"
641 (let (org-export-filter-body-functions
642 org-export-filter-final-output-functions)
643 (org-trim
644 (org-test-with-temp-text "* Stats [0/0]"
645 (org-export-as (org-test-default-backend)
646 nil nil nil '(:with-statistics-cookies nil)))))))
647 ;; Tables.
648 (should
649 (equal "| A |\n"
650 (org-test-with-temp-text "| A |"
651 (org-export-as (org-test-default-backend) nil nil nil
652 '(:with-tables t)))))
653 (should
654 (equal ""
655 (let (org-export-filter-body-functions
656 org-export-filter-final-output-functions)
657 (org-test-with-temp-text "| A |"
658 (org-export-as (org-test-default-backend) nil nil nil
659 '(:with-tables nil)))))))
661 (ert-deftest test-org-export/with-timestamps ()
662 "Test `org-export-with-timestamps' specifications."
663 ;; t value.
664 (should
665 (string-match
666 "\\[2012-04-29 .*? 10:45\\]<2012-04-29 .*? 10:45>"
667 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
668 (org-export-as (org-test-default-backend)
669 nil nil nil '(:with-timestamps t)))))
670 ;; nil value.
671 (should
672 (equal
674 (let (org-export-filter-body-functions
675 org-export-filter-final-output-functions)
676 (org-trim
677 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
678 (org-export-as (org-test-default-backend)
679 nil nil nil '(:with-timestamps nil)))))))
680 ;; `active' value.
681 (should
682 (string-match
683 "<2012-03-29 .*?>\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
684 (org-test-with-temp-text
685 "<2012-03-29 Thu>[2012-03-29 Thu]
687 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
688 (org-export-as (org-test-default-backend)
689 nil nil nil '(:with-timestamps active)))))
690 ;; `inactive' value.
691 (should
692 (string-match
693 "\\[2012-03-29 .*?\\]\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
694 (org-test-with-temp-text
695 "<2012-03-29 Thu>[2012-03-29 Thu]
697 Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
698 (org-export-as (org-test-default-backend)
699 nil nil nil '(:with-timestamps inactive))))))
701 (ert-deftest test-org-export/comment-tree ()
702 "Test if export process ignores commented trees."
703 (should
704 (equal ""
705 (let (org-export-filter-body-functions
706 org-export-filter-final-output-functions)
707 (org-test-with-temp-text "* COMMENT Head1"
708 (org-export-as (org-test-default-backend)))))))
710 (ert-deftest test-org-export/uninterpreted ()
711 "Test handling of uninterpreted elements."
712 ;; Entities.
713 (should
714 (equal "dummy\n"
715 (org-test-with-temp-text "\\alpha"
716 (org-export-as
717 (org-export-create-backend
718 :transcoders '((entity . (lambda (e c i) "dummy"))
719 (paragraph . (lambda (p c i) c))
720 (section . (lambda (s c i) c))))
721 nil nil nil '(:with-entities t)))))
722 (should
723 (equal "\\alpha\n"
724 (org-test-with-temp-text "\\alpha"
725 (org-export-as
726 (org-export-create-backend
727 :transcoders '((entity . (lambda (e c i) "dummy"))
728 (paragraph . (lambda (p c i) c))
729 (section . (lambda (s c i) c))))
730 nil nil nil '(:with-entities nil)))))
731 ;; Emphasis.
732 (should
733 (equal "dummy\n"
734 (org-test-with-temp-text "*bold*"
735 (org-export-as
736 (org-export-create-backend
737 :transcoders '((bold . (lambda (b c i) "dummy"))
738 (paragraph . (lambda (p c i) c))
739 (section . (lambda (s c i) c))))
740 nil nil nil '(:with-emphasize t)))))
741 (should
742 (equal "*bold*\n"
743 (org-test-with-temp-text "*bold*"
744 (org-export-as
745 (org-export-create-backend
746 :transcoders '((bold . (lambda (b c i) "dummy"))
747 (paragraph . (lambda (p c i) c))
748 (section . (lambda (s c i) c))))
749 nil nil nil '(:with-emphasize nil)))))
750 (should
751 (equal "/simple/ /example/\n"
752 (org-test-with-temp-text "/simple/ /example/"
753 (org-export-as
754 (org-export-create-backend
755 :transcoders '((bold . (lambda (b c i) "dummy"))
756 (paragraph . (lambda (p c i) c))
757 (section . (lambda (s c i) c))))
758 nil nil nil '(:with-emphasize nil)))))
759 ;; LaTeX environment.
760 (should
761 (equal "dummy\n"
762 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
763 (org-export-as
764 (org-export-create-backend
765 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
766 (section . (lambda (s c i) c))))
767 nil nil nil '(:with-latex t)))))
768 (should
769 (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
770 (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
771 (org-export-as
772 (org-export-create-backend
773 :transcoders '((latex-environment . (lambda (l c i) "dummy"))
774 (section . (lambda (s c i) c))))
775 nil nil nil '(:with-latex verbatim)))))
776 ;; LaTeX fragment.
777 (should
778 (equal "dummy\n"
779 (org-test-with-temp-text "$1$"
780 (org-export-as
781 (org-export-create-backend
782 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
783 (paragraph . (lambda (p c i) c))
784 (section . (lambda (s c i) c))))
785 nil nil nil '(:with-latex t)))))
786 (should
787 (equal "$1$\n"
788 (org-test-with-temp-text "$1$"
789 (org-export-as
790 (org-export-create-backend
791 :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
792 (paragraph . (lambda (p c i) c))
793 (section . (lambda (s c i) c))))
794 nil nil nil '(:with-latex verbatim)))))
795 ;; Sub/superscript.
796 (should
797 (equal "adummy\n"
798 (org-test-with-temp-text "a_b"
799 (org-export-as
800 (org-export-create-backend
801 :transcoders '((subscript . (lambda (s c i) "dummy"))
802 (paragraph . (lambda (p c i) c))
803 (section . (lambda (s c i) c))))
804 nil nil nil '(:with-sub-superscript t)))))
805 (should
806 (equal "a_b\n"
807 (org-test-with-temp-text "a_b"
808 (org-export-as
809 (org-export-create-backend
810 :transcoders '((subscript . (lambda (s c i) "dummy"))
811 (paragraph . (lambda (p c i) c))
812 (section . (lambda (s c i) c))))
813 nil nil nil '(:with-sub-superscript nil)))))
814 (should
815 (equal "a_b\n"
816 (org-test-with-temp-text "a_b"
817 (org-export-as
818 (org-export-create-backend
819 :transcoders '((subscript . (lambda (s c i) "dummy"))
820 (paragraph . (lambda (p c i) c))
821 (section . (lambda (s c i) c))))
822 nil nil nil '(:with-sub-superscript {})))))
823 (should
824 (equal "adummy\n"
825 (org-test-with-temp-text "a_{b}"
826 (org-export-as
827 (org-export-create-backend
828 :transcoders '((subscript . (lambda (s c i) "dummy"))
829 (paragraph . (lambda (p c i) c))
830 (section . (lambda (s c i) c))))
831 nil nil nil '(:with-sub-superscript {})))))
832 (should
833 (equal "a_entity\n"
834 (org-test-with-temp-text "a_\\alpha"
835 (org-export-as
836 (org-export-create-backend
837 :transcoders '((entity . (lambda (e c i) "entity"))
838 (subscript . (lambda (s c i) "dummy"))
839 (paragraph . (lambda (p c i) c))
840 (section . (lambda (s c i) c))))
841 nil nil nil '(:with-sub-superscript nil)))))
842 ;; Also handle uninterpreted objects in title.
843 (should
844 (equal "a_b"
845 (org-test-with-temp-text "#+TITLE: a_b"
846 (org-export-as
847 (org-export-create-backend
848 :transcoders
849 '((subscript . (lambda (s c i) "dummy"))
850 (template . (lambda (c i) (org-export-data
851 (plist-get i :title) i)))
852 (section . (lambda (s c i) c))))
853 nil nil nil '(:with-sub-superscript nil)))))
854 ;; Handle uninterpreted objects in captions.
855 (should
856 (equal "adummy\n"
857 (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
858 (org-export-as
859 (org-export-create-backend
860 :transcoders
861 '((subscript . (lambda (s c i) "dummy"))
862 (paragraph . (lambda (p c i)
863 (org-export-data (org-export-get-caption p) i)))
864 (section . (lambda (s c i) c))))
865 nil nil nil '(:with-sub-superscript t)))))
866 (should
867 (equal "a_b\n"
868 (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
869 (org-export-as
870 (org-export-create-backend
871 :transcoders
872 '((subscript . (lambda (s c i) "dummy"))
873 (paragraph . (lambda (p c i)
874 (org-export-data (org-export-get-caption p) i)))
875 (section . (lambda (s c i) c))))
876 nil nil nil '(:with-sub-superscript nil)))))
877 ;; Special case: multiples uninterpreted objects in a row.
878 (should
879 (equal "a_b_c_d\n"
880 (org-test-with-temp-text "a_b_c_d"
881 (org-export-as
882 (org-export-create-backend
883 :transcoders '((subscript . (lambda (s c i) "dummy"))
884 (paragraph . (lambda (p c i) c))
885 (section . (lambda (s c i) c))))
886 nil nil nil '(:with-sub-superscript {}))))))
888 (ert-deftest test-org-export/export-scope ()
889 "Test all export scopes."
890 ;; Subtree.
891 (should
892 (equal "text\n*** H3\n"
893 (org-test-with-temp-text "* H1\n<point>** H2\ntext\n*** H3"
894 (org-export-as (org-test-default-backend) 'subtree))))
895 (should
896 (equal "text\n*** H3\n"
897 (org-test-with-temp-text "* H1\n** H2\n<point>text\n*** H3"
898 (org-export-as (org-test-default-backend) 'subtree))))
899 ;; Subtree with a code block calling another block outside.
900 (should
901 (equal ": 3\n"
902 (org-test-with-temp-text "
903 <point>* Head1
904 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
905 <<test>>
906 #+END_SRC
907 * Head2
908 #+NAME: test
909 #+BEGIN_SRC emacs-lisp
910 \(+ 1 2)
911 #+END_SRC"
912 (let ((org-export-use-babel t))
913 (org-export-as (org-test-default-backend) 'subtree)))))
914 ;; Subtree export should ignore leading planning line and property
915 ;; drawer.
916 (should
917 (equal "Text\n"
918 (org-test-with-temp-text "
919 <point>* H
920 SCHEDULED: <2012-03-29 Thu>
921 :PROPERTIES:
922 :A: 1
923 :END:
924 Text"
925 (org-export-as (org-test-default-backend)
926 'subtree nil nil
927 '(:with-planning t :with-properties t)))))
928 ;; Visible.
929 (should
930 (equal "* H1\n"
931 (org-test-with-temp-text "* H1\n** H2\ntext\n*** H3"
932 (org-cycle)
933 (org-export-as (org-test-default-backend) nil 'visible))))
934 ;; Region.
935 (should
936 (equal "text\n"
937 (org-test-with-temp-text "* H1\n** H2\n<point>text\n*** H3"
938 (transient-mark-mode 1)
939 (push-mark (point) t t)
940 (end-of-line)
941 (org-export-as (org-test-default-backend)))))
942 ;; Body only.
943 (should
944 (equal "Text\n"
945 (org-test-with-temp-text "Text"
946 (org-export-as
947 (org-export-create-backend
948 :transcoders
949 '((template . (lambda (b _i) (format "BEGIN\n%sEND" b)))
950 (section . (lambda (_s c _i) c))
951 (paragraph . (lambda (_p c _i) c))))
952 nil nil 'body-only))))
953 (should
954 (equal "BEGIN\nText\nEND"
955 (org-test-with-temp-text "Text"
956 (org-export-as
957 (org-export-create-backend
958 :transcoders
959 '((template . (lambda (b _i) (format "BEGIN\n%sEND" b)))
960 (section . (lambda (_s c _i) c))
961 (paragraph . (lambda (_p c _i) c))))))))
962 ;; Pathological case: Body only on an empty buffer is expected to
963 ;; return an empty string, not nil.
964 (should
965 (org-test-with-temp-text ""
966 (org-export-as (org-test-default-backend) nil nil t))))
968 (ert-deftest test-org-export/output-file-name ()
969 "Test `org-export-output-file-name' specifications."
970 ;; Export from a file: name is built from original file name.
971 (should
972 (org-test-with-temp-text-in-file "Test"
973 (equal (file-name-base (buffer-file-name))
974 (file-name-base (org-export-output-file-name ".ext")))))
975 ;; When #+EXPORT_FILE_NAME is defined, use it.
976 (should
977 (equal "test.ext"
978 (org-test-with-temp-text-in-file "#+EXPORT_FILE_NAME: test"
979 (org-export-output-file-name ".ext" t))))
980 ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
981 (should
982 (equal "test.ext"
983 (org-test-with-temp-text-in-file
984 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
985 (org-export-output-file-name ".ext" t))))
986 (should
987 (equal "property.ext"
988 (org-test-with-temp-text
989 "#+EXPORT_FILE_NAME: keyword
990 * Test<point>
991 :PROPERTIES:
992 :EXPORT_FILE_NAME: property
993 :END:"
994 (org-export-output-file-name ".ext" t))))
995 ;; From a buffer not associated to a file, too.
996 (should
997 (equal "test.ext"
998 (org-test-with-temp-text
999 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
1000 (org-export-output-file-name ".ext" t))))
1001 ;; When provided name is absolute, preserve it.
1002 (should
1003 (org-test-with-temp-text
1004 (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
1005 (expand-file-name "test"))
1006 (file-name-absolute-p (org-export-output-file-name ".ext" t))))
1007 ;; When PUB-DIR argument is provided, use it.
1008 (should
1009 (equal "dir/"
1010 (org-test-with-temp-text-in-file "Test"
1011 (file-name-directory
1012 (org-export-output-file-name ".ext" nil "dir/")))))
1013 ;; PUB-DIR has precedence over EXPORT_FILE_NAME keyword or property.
1014 (should
1015 (equal "pub-dir/"
1016 (org-test-with-temp-text-in-file
1017 "#+EXPORT_FILE_NAME: /dir/keyword\nTest"
1018 (file-name-directory
1019 (org-export-output-file-name ".ext" nil "pub-dir/")))))
1020 ;; When returned name would overwrite original file, add EXTENSION
1021 ;; another time.
1022 (should
1023 (equal "normal.org.org"
1024 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
1025 (org-export-output-file-name ".org")))))
1027 (ert-deftest test-org-export/expand-include ()
1028 "Test file inclusion in an Org buffer."
1029 ;; Error when file isn't specified.
1030 (should-error
1031 (org-test-with-temp-text "#+INCLUDE: dummy.org"
1032 (org-export-expand-include-keyword)))
1033 ;; Full insertion with recursive inclusion.
1034 (should
1035 (equal
1036 (with-temp-buffer
1037 (insert-file
1038 (expand-file-name "examples/include.org" org-test-dir))
1039 (replace-regexp-in-string
1040 (regexp-quote "#+INCLUDE: \"include2.org\"")
1041 "Success!" (buffer-string)))
1042 (org-test-with-temp-text
1043 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
1044 (org-export-expand-include-keyword)
1045 (buffer-string))))
1046 ;; Localized insertion.
1047 (org-test-with-temp-text
1048 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
1049 org-test-dir)
1050 (org-export-expand-include-keyword)
1051 (should (equal (buffer-string)
1052 "Small Org file with an include keyword.\n")))
1053 ;; Insertion with constraints on headlines level.
1054 (should
1055 (equal
1056 "* Top heading\n** Heading\nbody\n"
1057 (org-test-with-temp-text
1058 (format
1059 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\""
1060 org-test-dir)
1061 (org-export-expand-include-keyword)
1062 (buffer-string))))
1063 (should
1064 (equal
1065 "* Top heading\n* Heading\nbody\n"
1066 (org-test-with-temp-text
1067 (format
1068 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\" :minlevel 1"
1069 org-test-dir)
1070 (org-export-expand-include-keyword)
1071 (buffer-string))))
1072 ;; Inclusion within an example block.
1073 (should
1074 (equal
1075 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
1076 (org-test-with-temp-text
1077 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
1078 org-test-dir)
1079 (org-export-expand-include-keyword)
1080 (buffer-string))))
1081 ;; Inclusion within a src-block.
1082 (should
1083 (equal
1084 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
1085 (org-test-with-temp-text
1086 (format
1087 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
1088 org-test-dir)
1089 (org-export-expand-include-keyword)
1090 (buffer-string))))
1091 ;; Inclusion within an html export-block.
1092 (should
1093 (equal
1094 "#+BEGIN_EXPORT html\n<p>HTML!</p>\n#+END_EXPORT\n"
1095 (org-test-with-temp-text
1096 (format
1097 "#+INCLUDE: \"%s/examples/include.html\" EXPORT html"
1098 org-test-dir)
1099 (org-export-expand-include-keyword)
1100 (buffer-string))))
1101 ;; Inclusion within an center paragraph
1102 (should
1103 (equal
1104 "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
1105 (org-test-with-temp-text
1106 (format
1107 "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
1108 org-test-dir)
1109 (org-export-expand-include-keyword)
1110 (buffer-string))))
1111 ;; Footnotes labels are local to each included file.
1112 (should
1113 (= 6
1114 (length
1115 (delete-dups
1116 (let ((contents "
1117 Footnotes[fn:1], [fn:test], [fn:test] and [fn:inline:inline footnote].
1118 \[fn:1] Footnote 1
1119 \[fn:test] Footnote \"test\""))
1120 (org-test-with-temp-text-in-file contents
1121 (let ((file1 (buffer-file-name)))
1122 (org-test-with-temp-text-in-file contents
1123 (let ((file2 (buffer-file-name)))
1124 (org-test-with-temp-text
1125 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
1126 file1 file2)
1127 (org-export-expand-include-keyword)
1128 (org-element-map (org-element-parse-buffer)
1129 'footnote-reference
1130 (lambda (r) (org-element-property :label r)))))))))))))
1131 ;; Footnotes labels are not local to each include keyword.
1132 (should
1133 (= 3
1134 (length
1135 (delete-dups
1136 (let ((contents "
1137 Footnotes[fn:1], [fn:test] and [fn:inline:inline footnote].
1138 \[fn:1] Footnote 1
1139 \[fn:test] Footnote \"test\""))
1140 (org-test-with-temp-text-in-file contents
1141 (let ((file (buffer-file-name)))
1142 (org-test-with-temp-text
1143 (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
1144 (org-export-expand-include-keyword)
1145 (org-element-map (org-element-parse-buffer)
1146 'footnote-reference
1147 (lambda (ref) (org-element-property :label ref)))))))))))
1148 ;; Footnotes are supported by :lines-like elements and unnecessary
1149 ;; footnotes are dropped.
1150 (should
1151 (= 3
1152 (length
1153 (delete-dups
1154 (let ((contents "
1155 * foo
1156 Footnotes[fn:1]
1157 * bar
1158 Footnotes[fn:2], foot[fn:test] and [fn:inline:inline footnote]
1160 \[fn:1] Footnote 1
1161 \[fn:2] Footnote 1
1162 * Footnotes
1163 \[fn:test] Footnote \"test\"
1165 (org-test-with-temp-text-in-file contents
1166 (let ((file (buffer-file-name)))
1167 (org-test-with-temp-text
1168 (format "#+INCLUDE: \"%s::*bar\"\n" file)
1169 (org-export-expand-include-keyword)
1170 (org-element-map (org-element-parse-buffer)
1171 'footnote-definition
1172 (lambda (ref) (org-element-property :label ref)))))))))))
1173 ;; If only-contents is non-nil only include contents of element.
1174 (should
1175 (equal
1176 "body\n"
1177 (org-test-with-temp-text
1178 (concat
1179 (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" "
1180 org-test-dir)
1181 ":only-contents t")
1182 (org-export-expand-include-keyword)
1183 (buffer-string))))
1184 ;; Headings can be included via CUSTOM_ID.
1185 (should
1186 (org-test-with-temp-text
1187 (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
1188 (org-export-expand-include-keyword)
1189 (goto-char (point-min))
1190 (looking-at "* Another heading")))
1191 ;; Named objects can be included.
1192 (should
1193 (equal
1194 "| 1 |\n"
1195 (org-test-with-temp-text
1196 (format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t"
1197 org-test-dir)
1198 (org-export-expand-include-keyword)
1199 (buffer-string))))
1200 ;; Including non-existing elements should result in an error.
1201 (should-error
1202 (org-test-with-temp-text
1203 (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\""
1204 org-test-dir)
1205 (org-export-expand-include-keyword)))
1206 ;; Lines work relatively to an included element.
1207 (should
1208 (equal
1209 "2\n3\n"
1210 (org-test-with-temp-text
1211 (format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t \
1212 :lines \"2-3\""
1213 org-test-dir)
1214 (org-export-expand-include-keyword)
1215 (buffer-string))))
1216 ;; Properties should be dropped from headlines.
1217 (should
1218 (equal
1219 (org-test-with-temp-text
1220 (format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t"
1221 org-test-dir)
1222 (org-export-expand-include-keyword)
1223 (buffer-string))
1224 (org-test-with-temp-text
1225 (format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
1226 (org-export-expand-include-keyword)
1227 (buffer-string))))
1228 ;; Properties should be dropped, drawers should not be.
1229 (should
1230 (equal
1231 ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
1232 (org-test-with-temp-text
1233 (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t"
1234 org-test-dir)
1235 (org-export-expand-include-keyword)
1236 (buffer-string))))
1237 ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
1238 (should
1239 (cl-every (lambda (level) (zerop (1- level)))
1240 (org-test-with-temp-text
1241 (concat
1242 (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n"
1243 org-test-dir)
1244 (format "#+INCLUDE: \"%s/examples/include.org::*Heading\""
1245 org-test-dir))
1246 (org-export-expand-include-keyword)
1247 (org-element-map (org-element-parse-buffer) 'headline
1248 (lambda (head) (org-element-property :level head))))))
1249 ;; INCLUDE does not insert induced :minlevel for src-blocks.
1250 (should-not
1251 (equal
1252 (org-test-with-temp-text
1253 (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp"
1254 org-test-dir)
1255 (org-export-expand-include-keyword)
1256 (buffer-string))
1257 (org-test-with-temp-text
1258 (format
1259 "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1"
1260 org-test-dir)
1261 (org-export-expand-include-keyword)
1262 (buffer-string))))
1263 ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
1264 (should
1265 (org-test-with-temp-text-in-file
1266 (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\""
1267 org-test-dir)
1268 (narrow-to-region (point) (point-max))
1269 (org-export-expand-include-keyword)
1270 (eq 1 (org-current-level))))
1271 ;; If :minlevel is present do not alter it.
1272 (should
1273 (org-test-with-temp-text
1274 (format
1275 "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 3"
1276 org-test-dir)
1277 (narrow-to-region (point) (point-max))
1278 (org-export-expand-include-keyword)
1279 (eq 3 (org-current-level)))))
1281 (ert-deftest test-org-export/expand-macro ()
1282 "Test macro expansion in an Org buffer."
1283 (require 'ox-org)
1284 ;; Standard macro expansion.
1285 (should
1286 (equal "#+MACRO: macro1 value\nvalue\n"
1287 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
1288 (org-export-as (org-test-default-backend)))))
1289 ;; Include global macros. However, local macros override them.
1290 (should
1291 (equal "global\n"
1292 (org-test-with-temp-text "{{{M}}}"
1293 (let ((org-export-global-macros '(("M" . "global"))))
1294 (org-export-as (org-test-default-backend))))))
1295 (should
1296 (equal "global arg\n"
1297 (org-test-with-temp-text "{{{M(arg)}}}"
1298 (let ((org-export-global-macros '(("M" . "global $1"))))
1299 (org-export-as (org-test-default-backend))))))
1300 (should
1301 (equal "2\n"
1302 (org-test-with-temp-text "{{{M}}}"
1303 (let ((org-export-global-macros '(("M" . "(eval (+ 1 1))"))))
1304 (org-export-as (org-test-default-backend))))))
1305 (should
1306 (equal "#+MACRO: M local\nlocal\n"
1307 (org-test-with-temp-text "#+macro: M local\n{{{M}}}"
1308 (let ((org-export-global-macros '(("M" . "global"))))
1309 (org-export-as (org-test-default-backend))))))
1310 ;; Allow macro in parsed keywords and associated properties.
1311 ;; Standard macro expansion.
1312 (should
1313 (string-match
1314 "#\\+K: value"
1315 (let ((backend (org-export-create-backend
1316 :parent 'org
1317 :options '((:k "K" nil nil parse)))))
1318 (org-test-with-temp-text "#+MACRO: macro value\n#+K: {{{macro}}}"
1319 (org-export-as backend)))))
1320 (should
1321 (string-match
1322 ":EXPORT_K: v"
1323 (let ((backend (org-export-create-backend
1324 :parent 'org
1325 :options '((:k "K" nil nil parse)))))
1326 (org-test-with-temp-text
1327 "#+MACRO: m v\n* H\n:PROPERTIES:\n:EXPORT_K: {{{m}}}\n:END:"
1328 (org-export-as backend nil nil nil '(:with-properties t))))))
1329 ;; Expand specific macros.
1330 (should
1331 (equal "me 2012-03-29 me@here Title\n"
1332 (org-test-with-temp-text
1334 #+TITLE: Title
1335 #+DATE: 2012-03-29
1336 #+AUTHOR: me
1337 #+EMAIL: me@here
1338 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
1339 (let ((output (org-export-as (org-test-default-backend))))
1340 (substring output (string-match ".*\n\\'" output))))))
1341 ;; Expand specific macros when property contained a regular macro
1342 ;; already.
1343 (should
1344 (equal "value\n"
1345 (org-test-with-temp-text "
1346 #+MACRO: macro1 value
1347 #+TITLE: {{{macro1}}}
1348 {{{title}}}"
1349 (let ((output (org-export-as (org-test-default-backend))))
1350 (substring output (string-match ".*\n\\'" output))))))
1351 ;; Expand macros with templates in included files.
1352 (should
1353 (equal "success\n"
1354 (org-test-with-temp-text
1355 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
1356 {{{included-macro}}}" org-test-dir)
1357 (let ((output (org-export-as (org-test-default-backend))))
1358 (substring output (string-match ".*\n\\'" output))))))
1359 ;; Date macro takes a optional formatting argument
1360 (should
1361 (equal "09-02-15\n"
1362 (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02-09>"
1363 (org-export-as (org-test-default-backend)))))
1364 ;; Only single timestamps are formatted
1365 (should
1366 (equal "<2015-02x-09>\n"
1367 (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02x-09>"
1368 (org-export-as (org-test-default-backend)))))
1369 ;; Throw an error when a macro definition is missing.
1370 (should-error
1371 (org-test-with-temp-text "{{{missing}}}"
1372 (org-export-as (org-test-default-backend))))
1373 ;; Macros defined in commented subtrees are ignored.
1374 (should-error
1375 (org-test-with-temp-text
1376 "* COMMENT H\n#+MACRO: macro1\n* H2\nvalue\n{{{macro1}}}"
1377 (org-export-as (org-test-default-backend))))
1378 (should-error
1379 (org-test-with-temp-text
1380 "* COMMENT H\n** H2\n#+MACRO: macro1\n* H3\nvalue\n{{{macro1}}}"
1381 (org-export-as (org-test-default-backend)))))
1383 (ert-deftest test-org-export/before-processing-hook ()
1384 "Test `org-export-before-processing-hook'."
1385 (should
1386 (equal
1387 "#+MACRO: mac val\nTest\n"
1388 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
1389 (let ((org-export-before-processing-hook
1390 '((lambda (backend)
1391 (while (re-search-forward "{{{" nil t)
1392 (let ((object (org-element-context)))
1393 (when (eq (org-element-type object) 'macro)
1394 (delete-region
1395 (org-element-property :begin object)
1396 (org-element-property :end object)))))))))
1397 (org-export-as (org-test-default-backend)))))))
1399 (ert-deftest test-org-export/before-parsing-hook ()
1400 "Test `org-export-before-parsing-hook'."
1401 (should
1402 (equal "Body 1\nBody 2\n"
1403 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
1404 (let ((org-export-before-parsing-hook
1405 '((lambda (backend)
1406 (goto-char (point-min))
1407 (while (re-search-forward org-outline-regexp-bol nil t)
1408 (delete-region
1409 (point-at-bol) (progn (forward-line) (point))))))))
1410 (org-export-as (org-test-default-backend)))))))
1414 ;;; Affiliated Keywords
1416 (ert-deftest test-org-export/read-attribute ()
1417 "Test `org-export-read-attribute' specifications."
1418 ;; Standard test.
1419 (should
1420 (equal
1421 (org-export-read-attribute
1422 :attr_html
1423 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
1424 (org-element-at-point)))
1425 '(:a "1" :b "2")))
1426 ;; Return nil on empty attribute.
1427 (should-not
1428 (org-export-read-attribute
1429 :attr_html
1430 (org-test-with-temp-text "Paragraph" (org-element-at-point))))
1431 ;; Return nil on "nil" string.
1432 (should
1433 (equal '(:a nil :b nil)
1434 (org-export-read-attribute
1435 :attr_html
1436 (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
1437 (org-element-at-point)))))
1438 ;; Return nil on empty string.
1439 (should
1440 (equal '(:a nil :b nil)
1441 (org-export-read-attribute
1442 :attr_html
1443 (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
1444 (org-element-at-point)))))
1445 ;; Return empty string when value is "".
1446 (should
1447 (equal '(:a "")
1448 (org-export-read-attribute
1449 :attr_html
1450 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
1451 (org-element-at-point)))))
1452 ;; Return \"\" when value is """".
1453 (should
1454 (equal '(:a "\"\"")
1455 (org-export-read-attribute
1456 :attr_html
1457 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
1458 (org-element-at-point)))))
1459 ;; Ignore text before first property.
1460 (should-not
1461 (member "ignore"
1462 (org-export-read-attribute
1463 :attr_html
1464 (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
1465 (org-element-at-point))))))
1467 (ert-deftest test-org-export/get-caption ()
1468 "Test `org-export-get-caption' specifications."
1469 ;; Without optional argument, return long caption
1470 (should
1471 (equal
1472 '("l")
1473 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1474 (org-export-get-caption (org-element-at-point)))))
1475 ;; With optional argument, return short caption.
1476 (should
1477 (equal
1478 '("s")
1479 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1480 (org-export-get-caption (org-element-at-point) t))))
1481 ;; Multiple lines are separated by white spaces.
1482 (should
1483 (equal
1484 '("a" " " "b")
1485 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
1486 (org-export-get-caption (org-element-at-point))))))
1490 ;;; Back-End Tools
1492 (ert-deftest test-org-export/define-backend ()
1493 "Test back-end definition and accessors."
1494 ;; Translate table.
1495 (should
1496 (equal '((headline . my-headline-test))
1497 (let (org-export-registered-backends)
1498 (org-export-define-backend 'test '((headline . my-headline-test)))
1499 (org-export-get-all-transcoders 'test))))
1500 ;; Filters.
1501 (should
1502 (equal '((:filter-headline . my-filter))
1503 (let (org-export-registered-backends)
1504 (org-export-define-backend 'test
1505 '((headline . my-headline-test))
1506 :filters-alist '((:filter-headline . my-filter)))
1507 (org-export-backend-filters (org-export-get-backend 'test)))))
1508 ;; Options.
1509 (should
1510 (equal '((:prop value))
1511 (let (org-export-registered-backends)
1512 (org-export-define-backend 'test
1513 '((headline . my-headline-test))
1514 :options-alist '((:prop value)))
1515 (org-export-backend-options (org-export-get-backend 'test)))))
1516 ;; Menu.
1517 (should
1518 (equal '(?k "Test Export" test)
1519 (let (org-export-registered-backends)
1520 (org-export-define-backend 'test
1521 '((headline . my-headline-test))
1522 :menu-entry '(?k "Test Export" test))
1523 (org-export-backend-menu (org-export-get-backend 'test))))))
1525 (ert-deftest test-org-export/define-derived-backend ()
1526 "Test `org-export-define-derived-backend' specifications."
1527 ;; Error when parent back-end is not defined.
1528 (should-error
1529 (let (org-export-registered-backends)
1530 (org-export-define-derived-backend 'test 'parent)))
1531 ;; Append translation table to parent's.
1532 (should
1533 (equal '((:headline . test) (:headline . parent))
1534 (let (org-export-registered-backends)
1535 (org-export-define-backend 'parent '((:headline . parent)))
1536 (org-export-define-derived-backend 'test 'parent
1537 :translate-alist '((:headline . test)))
1538 (org-export-get-all-transcoders 'test))))
1539 ;; Options defined in the new back have priority over those defined
1540 ;; in parent.
1541 (should
1542 (eq 'test
1543 (let (org-export-registered-backends)
1544 (org-export-define-backend 'parent
1545 '((:headline . parent))
1546 :options-alist '((:a nil nil 'parent)))
1547 (org-export-define-derived-backend 'test 'parent
1548 :options-alist '((:a nil nil 'test)))
1549 (plist-get (org-export--get-global-options
1550 (org-export-get-backend 'test))
1551 :a)))))
1553 (ert-deftest test-org-export/derived-backend-p ()
1554 "Test `org-export-derived-backend-p' specifications."
1555 ;; Non-nil with direct match.
1556 (should
1557 (let (org-export-registered-backends)
1558 (org-export-define-backend 'test '((headline . test)))
1559 (org-export-derived-backend-p 'test 'test)))
1560 (should
1561 (let (org-export-registered-backends)
1562 (org-export-define-backend 'test '((headline . test)))
1563 (org-export-define-derived-backend 'test2 'test)
1564 (org-export-derived-backend-p 'test2 'test2)))
1565 ;; Non-nil with a direct parent.
1566 (should
1567 (let (org-export-registered-backends)
1568 (org-export-define-backend 'test '((headline . test)))
1569 (org-export-define-derived-backend 'test2 'test)
1570 (org-export-derived-backend-p 'test2 'test)))
1571 ;; Non-nil with an indirect parent.
1572 (should
1573 (let (org-export-registered-backends)
1574 (org-export-define-backend 'test '((headline . test)))
1575 (org-export-define-derived-backend 'test2 'test)
1576 (org-export-define-derived-backend 'test3 'test2)
1577 (org-export-derived-backend-p 'test3 'test)))
1578 ;; Nil otherwise.
1579 (should-not
1580 (let (org-export-registered-backends)
1581 (org-export-define-backend 'test '((headline . test)))
1582 (org-export-define-backend 'test2 '((headline . test2)))
1583 (org-export-derived-backend-p 'test2 'test)))
1584 (should-not
1585 (let (org-export-registered-backends)
1586 (org-export-define-backend 'test '((headline . test)))
1587 (org-export-define-backend 'test2 '((headline . test2)))
1588 (org-export-define-derived-backend 'test3 'test2)
1589 (org-export-derived-backend-p 'test3 'test))))
1591 (ert-deftest test-org-export/get-all-transcoders ()
1592 "Test `org-export-get-all-transcoders' specifications."
1593 ;; Return nil when back-end cannot be found.
1594 (should-not (org-export-get-all-transcoders nil))
1595 ;; Same as `org-export-transcoders' if no parent.
1596 (should
1597 (equal '((headline . ignore))
1598 (org-export-get-all-transcoders
1599 (org-export-create-backend
1600 :transcoders '((headline . ignore))))))
1601 ;; But inherit from all ancestors whenever possible.
1602 (should
1603 (equal '((section . ignore) (headline . ignore))
1604 (let (org-export-registered-backends)
1605 (org-export-define-backend 'b1 '((headline . ignore)))
1606 (org-export-get-all-transcoders
1607 (org-export-create-backend
1608 :parent 'b1 :transcoders '((section . ignore)))))))
1609 (should
1610 (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
1611 (let (org-export-registered-backends)
1612 (org-export-define-backend 'b1 '((headline . ignore)))
1613 (org-export-define-derived-backend 'b2 'b1
1614 :translate-alist '((section . ignore)))
1615 (org-export-get-all-transcoders
1616 (org-export-create-backend
1617 :parent 'b2 :transcoders '((paragraph . ignore)))))))
1618 ;; Back-end transcoders overrule inherited ones.
1619 (should
1620 (eq 'b
1621 (let (org-export-registered-backends)
1622 (org-export-define-backend 'b1 '((headline . a)))
1623 (cdr (assq 'headline
1624 (org-export-get-all-transcoders
1625 (org-export-create-backend
1626 :parent 'b1 :transcoders '((headline . b))))))))))
1628 (ert-deftest test-org-export/get-all-options ()
1629 "Test `org-export-get-all-options' specifications."
1630 ;; Return nil when back-end cannot be found.
1631 (should-not (org-export-get-all-options nil))
1632 ;; Same as `org-export-options' if no parent.
1633 (should
1634 (equal '((headline . ignore))
1635 (org-export-get-all-options
1636 (org-export-create-backend
1637 :options '((headline . ignore))))))
1638 ;; But inherit from all ancestors whenever possible.
1639 (should
1640 (equal '((:key2 value2) (:key1 value1))
1641 (let (org-export-registered-backends)
1642 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1643 (org-export-get-all-options
1644 (org-export-create-backend
1645 :parent 'b1 :options '((:key2 value2)))))))
1646 (should
1647 (equal '((:key3 value3) (:key2 value2) (:key1 value1))
1648 (let (org-export-registered-backends)
1649 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1650 (org-export-define-derived-backend 'b2 'b1
1651 :options-alist '((:key2 value2)))
1652 (org-export-get-all-options
1653 (org-export-create-backend
1654 :parent 'b2 :options '((:key3 value3)))))))
1655 ;; Back-end options overrule inherited ones.
1656 (should
1657 (eq 'b
1658 (let (org-export-registered-backends)
1659 (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
1660 (cdr (assq :key1
1661 (org-export-get-all-options
1662 (org-export-create-backend
1663 :parent 'b1 :options '((:key1 . b))))))))))
1665 (ert-deftest test-org-export/get-all-filters ()
1666 "Test `org-export-get-all-filters' specifications."
1667 ;; Return nil when back-end cannot be found.
1668 (should-not (org-export-get-all-filters nil))
1669 ;; Same as `org-export-filters' if no parent.
1670 (should
1671 (equal '((:filter-headline . ignore))
1672 (org-export-get-all-filters
1673 (org-export-create-backend
1674 :filters '((:filter-headline . ignore))))))
1675 ;; But inherit from all ancestors whenever possible.
1676 (should
1677 (equal '((:filter-section . ignore) (:filter-headline . ignore))
1678 (let (org-export-registered-backends)
1679 (org-export-define-backend 'b1
1680 nil :filters-alist '((:filter-headline . ignore)))
1681 (org-export-get-all-filters
1682 (org-export-create-backend
1683 :parent 'b1 :filters '((:filter-section . ignore)))))))
1684 (should
1685 (equal '((:filter-paragraph . ignore)
1686 (:filter-section . ignore)
1687 (:filter-headline . ignore))
1688 (let (org-export-registered-backends)
1689 (org-export-define-backend 'b1
1690 nil :filters-alist '((:filter-headline . ignore)))
1691 (org-export-define-derived-backend 'b2 'b1
1692 :filters-alist '((:filter-section . ignore)))
1693 (org-export-get-all-filters
1694 (org-export-create-backend
1695 :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
1696 ;; Back-end filters overrule inherited ones.
1697 (should
1698 (eq 'b
1699 (let (org-export-registered-backends)
1700 (org-export-define-backend 'b1 '((:filter-headline . a)))
1701 (cdr (assq :filter-headline
1702 (org-export-get-all-filters
1703 (org-export-create-backend
1704 :parent 'b1 :filters '((:filter-headline . b))))))))))
1706 (ert-deftest test-org-export/with-backend ()
1707 "Test `org-export-with-backend' definition."
1708 ;; Error when calling an undefined back-end
1709 (should-error (org-export-with-backend nil "Test"))
1710 ;; Error when called back-end doesn't have an appropriate
1711 ;; transcoder.
1712 (should-error
1713 (org-export-with-backend
1714 (org-export-create-backend :transcoders '((headline . ignore)))
1715 "Test"))
1716 ;; Otherwise, export using correct transcoder
1717 (should
1718 (equal "Success"
1719 (let (org-export-registered-backends)
1720 (org-export-define-backend 'test
1721 '((plain-text . (lambda (text contents info) "Failure"))))
1722 (org-export-define-backend 'test2
1723 '((plain-text . (lambda (text contents info) "Success"))))
1724 (org-export-with-backend 'test2 "Test"))))
1725 ;; Provide correct back-end if transcoder needs to use recursive
1726 ;; calls anyway.
1727 (should
1728 (equal "Success\n"
1729 (let ((test-back-end
1730 (org-export-create-backend
1731 :transcoders
1732 (list (cons 'headline
1733 (lambda (headline contents info)
1734 (org-export-data
1735 (org-element-property :title headline)
1736 info)))
1737 (cons 'plain-text (lambda (text info) "Success"))))))
1738 (org-export-string-as
1739 "* Test"
1740 (org-export-create-backend
1741 :transcoders
1742 (list (cons 'headline
1743 (lambda (headline contents info)
1744 (org-export-with-backend
1745 test-back-end headline contents info))))))))))
1747 (ert-deftest test-org-export/data-with-backend ()
1748 "Test `org-export-data-with-backend' specifications."
1749 ;; Error when calling an undefined back-end.
1750 (should-error (org-export-data-with-backend nil "nil" nil))
1751 ;; Otherwise, export data recursively, using correct back-end.
1752 (should
1753 (equal
1754 "Success!"
1755 (org-export-data-with-backend
1756 '(bold nil "Test")
1757 (org-export-create-backend
1758 :transcoders
1759 '((plain-text . (lambda (text info) "Success"))
1760 (bold . (lambda (bold contents info) (concat contents "!")))))
1761 '(:with-emphasize t)))))
1765 ;;; Comments
1767 (ert-deftest test-org-export/comments ()
1768 "Test comments handling during export.
1769 In particular, structure of the document mustn't be altered after
1770 comments removal."
1771 (should
1772 (equal "Para1\n\nPara2\n"
1773 (org-test-with-temp-text "
1774 Para1
1775 # Comment
1777 # Comment
1778 Para2"
1779 (org-export-as (org-test-default-backend)))))
1780 (should
1781 (equal "Para1\n\nPara2\n"
1782 (org-test-with-temp-text "
1783 Para1
1784 # Comment
1785 Para2"
1786 (org-export-as (org-test-default-backend)))))
1787 (should
1788 (equal "[fn:1] Para1\n\n\nPara2\n"
1789 (org-test-with-temp-text "
1790 \[fn:1] Para1
1791 # Inside definition
1794 # Outside definition
1795 Para2"
1796 (org-export-as (org-test-default-backend)))))
1797 (should
1798 (equal "[fn:1] Para1\n\nPara2\n"
1799 (org-test-with-temp-text "
1800 \[fn:1] Para1
1802 # Inside definition
1804 # Inside definition
1806 Para2"
1807 (org-export-as (org-test-default-backend)))))
1808 (should
1809 (equal "[fn:1] Para1\n\nPara2\n"
1810 (org-test-with-temp-text "
1811 \[fn:1] Para1
1812 # Inside definition
1814 Para2"
1815 (org-export-as (org-test-default-backend)))))
1816 (should
1817 (equal "[fn:1] Para1\n\nPara2\n"
1818 (org-test-with-temp-text "
1819 \[fn:1] Para1
1821 # Inside definition
1822 Para2"
1823 (org-export-as (org-test-default-backend)))))
1824 (should
1825 (equal "- item 1\n\n- item 2\n"
1826 (org-test-with-temp-text "
1827 - item 1
1829 # Comment
1831 - item 2"
1832 (org-export-as (org-test-default-backend))))))
1836 ;;; Export blocks
1838 (ert-deftest test-org-export/export-block ()
1839 "Test export blocks transcoding."
1840 (should
1841 (equal "Success!\n"
1842 (org-test-with-temp-text
1843 "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
1844 (org-export-as
1845 (org-export-create-backend
1846 :transcoders '((export-block . (lambda (b _c _i)
1847 (org-element-property :value b)))
1848 (section . (lambda (_s c _i) c))))))))
1849 (should
1850 (equal "Success!\n"
1851 (org-test-with-temp-text
1852 "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
1853 (org-export-as
1854 (org-export-create-backend
1855 :transcoders
1856 (list
1857 (cons 'export-block
1858 (lambda (b _c _i)
1859 (and (equal (org-element-property :type b) "BACKEND")
1860 (org-element-property :value b))))
1861 (cons 'section (lambda (_s c _i) c)))))))))
1864 ;;; Export Snippets
1866 (ert-deftest test-org-export/export-snippet ()
1867 "Test export snippets transcoding."
1868 ;; Standard test.
1869 (org-test-with-temp-text "@@test:A@@@@t:B@@"
1870 (let ((backend (org-test-default-backend)))
1871 (setf (org-export-backend-name backend) 'test)
1872 (setf (org-export-backend-transcoders backend)
1873 (cons (cons 'export-snippet
1874 (lambda (snippet contents info)
1875 (when (eq (org-export-snippet-backend snippet) 'test)
1876 (org-element-property :value snippet))))
1877 (org-export-backend-transcoders backend)))
1878 (let ((org-export-snippet-translation-alist nil))
1879 (should (equal (org-export-as backend) "A\n")))
1880 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
1881 (should (equal (org-export-as backend) "AB\n")))))
1882 ;; Ignored export snippets do not remove any blank.
1883 (should
1884 (equal "begin end\n"
1885 (org-test-with-parsed-data "begin @@test:A@@ end"
1886 (org-export-data-with-backend
1887 tree
1888 (org-export-create-backend
1889 :transcoders
1890 '((paragraph . (lambda (paragraph contents info) contents))
1891 (section . (lambda (section contents info) contents))))
1892 info)))))
1895 ;;; Filters
1897 (ert-deftest test-org-export/filter-apply-functions ()
1898 "Test `org-export-filter-apply-functions' specifications."
1899 ;; Functions are applied in order and return values are reduced.
1900 (should
1901 (equal "210"
1902 (org-export-filter-apply-functions
1903 (list (lambda (value &rest _) (concat "1" value))
1904 (lambda (value &rest _) (concat "2" value)))
1905 "0" nil)))
1906 ;; Functions returning nil are skipped.
1907 (should
1908 (equal "20"
1909 (org-export-filter-apply-functions
1910 (list #'ignore (lambda (value &rest _) (concat "2" value)))
1911 "0" nil)))
1912 ;; If all functions are skipped, return the initial value.
1913 (should
1914 (equal "0"
1915 (org-export-filter-apply-functions (list #'ignore) "0" nil)))
1916 ;; If any function returns the empty string, final value is the
1917 ;; empty string.
1918 (should
1919 (equal ""
1920 (org-export-filter-apply-functions
1921 (list (lambda (value &rest _) "")
1922 (lambda (value &rest _) (concat "2" value)))
1923 "0" nil)))
1924 ;; Any function returning the empty string short-circuits the
1925 ;; process.
1926 (should
1927 (org-export-filter-apply-functions
1928 (list (lambda (value &rest _) "")
1929 (lambda (value &rest _) (error "This shouldn't happen")))
1930 "0" nil)))
1933 ;;; Footnotes
1935 (ert-deftest test-org-export/footnote-first-reference-p ()
1936 "Test `org-export-footnote-first-reference-p' specifications."
1937 (should
1938 (equal
1939 '(t nil)
1940 (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
1941 (let (result)
1942 (org-export-as
1943 (org-export-create-backend
1944 :transcoders
1945 `(,(cons 'footnote-reference
1946 (lambda (f c i)
1947 (push (org-export-footnote-first-reference-p f i)
1948 result)
1949 ""))
1950 (section . (lambda (s c i) c))
1951 (paragraph . (lambda (p c i) c))))
1952 nil nil nil '(:with-footnotes t))
1953 (nreverse result)))))
1954 ;; Limit check to DATA, when non-nil.
1955 (should
1956 (equal
1957 '(nil t)
1958 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
1959 (let (result)
1960 (org-element-map tree 'footnote-reference
1961 (lambda (ref)
1962 (push
1963 (org-export-footnote-first-reference-p
1964 ref info (org-element-map tree 'headline #'identity info t))
1965 result))
1966 info)
1967 (nreverse result)))))
1968 (should
1969 (equal
1970 '(t nil)
1971 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
1972 (let (result)
1973 (org-element-map tree 'footnote-reference
1974 (lambda (ref)
1975 (push (org-export-footnote-first-reference-p ref info) result))
1976 info)
1977 (nreverse result)))))
1978 ;; If optional argument BODY-FIRST is non-nil, first find footnote
1979 ;; in the main body of the document. Otherwise, enter footnote
1980 ;; definitions when they are encountered.
1981 (should
1982 (equal
1983 '(t nil)
1984 (org-test-with-temp-text
1985 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
1986 (let (result)
1987 (org-export-as
1988 (org-export-create-backend
1989 :transcoders
1990 `(,(cons 'footnote-reference
1991 (lambda (f c i)
1992 (when (org-element-lineage f '(drawer))
1993 (push (org-export-footnote-first-reference-p f i nil)
1994 result))
1995 ""))
1996 (drawer . (lambda (d c i) c))
1997 (footnote-definition . (lambda (d c i) c))
1998 (section . (lambda (s c i) c))
1999 (paragraph . (lambda (p c i) c))))
2000 nil nil nil '(:with-footnotes t))
2001 (nreverse result)))))
2002 (should
2003 (equal
2004 '(t t)
2005 (org-test-with-temp-text
2006 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
2007 (let (result)
2008 (org-export-as
2009 (org-export-create-backend
2010 :transcoders
2011 `(,(cons 'footnote-reference
2012 (lambda (f c i)
2013 (when (org-element-lineage f '(drawer))
2014 (push (org-export-footnote-first-reference-p f i nil t)
2015 result))
2016 ""))
2017 (drawer . (lambda (d c i) c))
2018 (footnote-definition . (lambda (d c i) c))
2019 (section . (lambda (s c i) c))
2020 (paragraph . (lambda (p c i) c))))
2021 nil nil nil '(:with-footnotes t))
2022 (nreverse result))))))
2024 (ert-deftest test-org-export/get-footnote-number ()
2025 "Test `org-export-get-footnote-number' specifications."
2026 (should
2027 (equal '(1 2 1)
2028 (org-test-with-parsed-data
2029 "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
2030 (org-element-map tree 'footnote-reference
2031 (lambda (ref) (org-export-get-footnote-number ref info))
2032 info))))
2033 ;; Anonymous footnotes all get a new number.
2034 (should
2035 (equal '(1 2)
2036 (org-test-with-parsed-data
2037 "Text[fn::anon1][fn::anon2]"
2038 (org-element-map tree 'footnote-reference
2039 (lambda (ref) (org-export-get-footnote-number ref info))
2040 info))))
2041 ;; Test nested footnotes order.
2042 (should
2043 (equal
2044 '((1 . "1") (2 . "2") (3 . "3") (3 . "3") (4))
2045 (org-test-with-parsed-data
2046 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
2047 (org-element-map tree 'footnote-reference
2048 (lambda (ref)
2049 (cons (org-export-get-footnote-number ref info)
2050 (org-element-property :label ref)))
2051 info))))
2052 ;; Limit number to provided DATA, when non-nil.
2053 (should
2054 (equal
2055 '(1)
2056 (org-test-with-parsed-data
2057 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2058 (org-element-map tree 'footnote-reference
2059 (lambda (ref)
2060 (org-export-get-footnote-number
2061 ref info (org-element-map tree 'headline #'identity info t)))
2062 info))))
2063 (should
2064 (equal
2065 '(1 2)
2066 (org-test-with-parsed-data
2067 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
2068 (org-element-map tree 'footnote-reference
2069 (lambda (ref) (org-export-get-footnote-number ref info))
2070 info))))
2071 ;; With a non-nil BODY-FIRST optional argument, first check body,
2072 ;; then footnote definitions.
2073 (should
2074 (equal
2075 '(("1" . 1) ("2" . 2) ("3" . 3) ("3" . 3))
2076 (org-test-with-parsed-data
2077 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
2078 (org-element-map tree 'footnote-reference
2079 (lambda (ref)
2080 (cons (org-element-property :label ref)
2081 (org-export-get-footnote-number ref info nil t)))
2082 info))))
2083 (should
2084 (equal
2085 '(("1" . 1) ("2" . 3) ("3" . 2) ("3" . 2))
2086 (org-test-with-parsed-data
2087 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
2088 (org-element-map tree 'footnote-reference
2089 (lambda (ref)
2090 (cons (org-element-property :label ref)
2091 (org-export-get-footnote-number ref info nil)))
2092 info)))))
2094 (ert-deftest test-org-export/get-footnote-definition ()
2095 "Test `org-export-get-footnote-definition' specifications."
2096 ;; Standard test.
2097 (should
2098 (equal "A\n"
2099 (org-element-interpret-data
2100 (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1] A"
2101 (org-export-get-footnote-definition
2102 (org-element-map tree 'footnote-reference #'identity nil t)
2103 info)))))
2104 ;; Raise an error if no definition is found.
2105 (should-error
2106 (org-test-with-parsed-data "Text[fn:1]"
2107 (org-export-get-footnote-definition
2108 (org-element-map tree 'footnote-reference #'identity nil t)
2109 info)))
2110 ;; Find inline definitions.
2111 (should
2112 (equal "A"
2113 (org-element-interpret-data
2114 (org-test-with-parsed-data "Text[fn:1:A]"
2115 (org-export-get-footnote-definition
2116 (org-element-map tree 'footnote-reference #'identity nil t)
2117 info)))))
2118 ;; Find anonymous definitions.
2119 (should
2120 (equal "A"
2121 (org-element-interpret-data
2122 (org-test-with-parsed-data "Text[fn::A]"
2123 (org-export-get-footnote-definition
2124 (org-element-map tree 'footnote-reference #'identity nil t)
2125 info)))))
2126 ;; Find empty definitions.
2127 (should
2128 (equal ""
2129 (org-element-interpret-data
2130 (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1]"
2131 (org-export-get-footnote-definition
2132 (org-element-map tree 'footnote-reference #'identity nil t)
2133 info)))))
2134 (should
2135 (equal ""
2136 (org-element-interpret-data
2137 (org-test-with-parsed-data "Text[fn:1:]"
2138 (org-export-get-footnote-definition
2139 (org-element-map tree 'footnote-reference #'identity nil t)
2140 info)))))
2141 (should
2142 (equal ""
2143 (org-element-interpret-data
2144 (org-test-with-parsed-data "Text[fn::]"
2145 (org-export-get-footnote-definition
2146 (org-element-map tree 'footnote-reference #'identity nil t)
2147 info))))))
2149 (ert-deftest test-org-export/collect-footnote-definitions ()
2150 "Test `org-export-collect-footnote-definitions' specifications."
2151 (should
2152 (= 4
2153 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2155 \[fn:2] B [fn:3] [fn::D].
2157 \[fn:3] C."
2158 (length (org-export-collect-footnote-definitions info)))))
2159 ;; Limit number to provided DATA, when non-nil.
2160 (should
2161 (equal
2162 '((1 "2"))
2163 (org-test-with-parsed-data
2164 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2165 (mapcar #'butlast
2166 (org-export-collect-footnote-definitions
2167 info (org-element-map tree 'headline #'identity info t))))))
2168 (should
2169 (equal
2170 '((1 "1") (2 "2"))
2171 (org-test-with-parsed-data
2172 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2173 (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
2174 ;; With optional argument BODY-FIRST, first check body, then
2175 ;; footnote definitions.
2176 (should
2177 (equal '("1" "3" "2" nil)
2178 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2180 \[fn:2] B [fn:3] [fn::D].
2182 \[fn:3] C."
2183 (mapcar (lambda (e) (nth 1 e))
2184 (org-export-collect-footnote-definitions info nil t)))))
2185 (should-not
2186 (equal '("1" "3" "2" nil)
2187 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2189 \[fn:2] B [fn:3] [fn::D].
2191 \[fn:3] C."
2192 (mapcar (lambda (e) (nth 1 e))
2193 (org-export-collect-footnote-definitions info))))))
2195 (ert-deftest test-org-export/footnotes ()
2196 "Miscellaneous tests on footnotes."
2197 (let ((org-footnote-section nil)
2198 (org-export-with-footnotes t))
2199 ;; Read every type of footnote.
2200 (should
2201 (equal
2202 '((1 . "A\n") (2 . "C") (3 . "D"))
2203 (org-test-with-parsed-data
2204 "Text[fn:1] [fn:label:C] [fn::D]\n\n[fn:1] A\n"
2205 (org-element-map tree 'footnote-reference
2206 (lambda (ref)
2207 (let ((def (org-export-get-footnote-definition ref info)))
2208 (cons (org-export-get-footnote-number ref info)
2209 (if (eq (org-element-property :type ref) 'inline) (car def)
2210 (car (org-element-contents
2211 (car (org-element-contents def))))))))
2212 info))))
2213 ;; Export nested footnote in invisible definitions.
2214 (should
2215 (= 2
2216 (org-test-with-temp-text "Text[fn:1]\n\n[fn:1] B [fn:2]\n\n[fn:2] C."
2217 (narrow-to-region (point) (line-end-position))
2218 (catch 'exit
2219 (org-export-as
2220 (org-export-create-backend
2221 :transcoders
2222 '((section
2224 (lambda (s c i)
2225 (throw 'exit (length
2226 (org-export-collect-footnote-definitions
2227 i))))))))))))
2228 ;; Export footnotes defined outside parsing scope.
2229 (should
2230 (string-match
2231 "Out of scope"
2232 (org-test-with-temp-text "[fn:1] Out of scope
2233 * Title
2234 <point>Paragraph[fn:1]"
2235 (org-export-as (org-test-default-backend) 'subtree))))
2236 (should
2237 (string-match
2238 "Out of scope"
2239 (org-test-with-temp-text "[fn:1] Out of scope
2240 * Title
2241 <point>Paragraph[fn:1]"
2242 (narrow-to-region (point) (point-max))
2243 (org-export-as (org-test-default-backend)))))
2244 ;; Export nested footnotes defined outside parsing scope.
2245 (should
2246 (string-match
2247 "Very out of scope"
2248 (org-test-with-temp-text "
2249 \[fn:1] Out of scope[fn:2]
2251 \[fn:2] Very out of scope
2252 * Title
2253 <point>Paragraph[fn:1]"
2254 (org-export-as (org-test-default-backend) 'subtree))))
2255 (should
2256 (string-match
2257 "Very out of scope"
2258 (org-test-with-temp-text "
2259 \[fn:1] Out of scope[fn:2]
2261 \[fn:2] Very out of scope
2262 * Title
2263 <point>Paragraph[fn:1]"
2264 (narrow-to-region (point) (point-max))
2265 (org-export-as (org-test-default-backend)))))
2266 (should
2267 (string-match
2268 "D2"
2269 (org-test-with-temp-text "
2270 \[fn:1] Out of scope[fn:2:D2]
2271 * Title
2272 <point>Paragraph[fn:1]"
2273 (narrow-to-region (point) (point-max))
2274 (org-export-as (org-test-default-backend)))))
2275 ;; Export footnotes in pruned parts of tree.
2276 (should
2277 (string-match
2278 "Definition"
2279 (let ((org-export-exclude-tags '("noexport")))
2280 (org-test-with-temp-text
2281 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] Definition"
2282 (org-export-as (org-test-default-backend))))))
2283 (should
2284 (string-match
2285 "Definition"
2286 (let ((org-export-select-tags '("export")))
2287 (org-test-with-temp-text
2288 "* H :export:\nText[fn:1]\n* H2\n[fn:1] Definition"
2289 (org-export-as (org-test-default-backend))))))
2290 ;; Export nested footnotes in pruned parts of tree.
2291 (should
2292 (string-match
2293 "D2"
2294 (let ((org-export-exclude-tags '("noexport")))
2295 (org-test-with-temp-text
2296 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
2297 (org-export-as (org-test-default-backend))))))
2298 (should
2299 (string-match
2300 "D2"
2301 (let ((org-export-select-tags '("export")))
2302 (org-test-with-temp-text
2303 "* H :export:\nText[fn:1]\n* H2\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
2304 (org-export-as (org-test-default-backend))))))
2305 ;; Handle uninterpreted data in pruned footnote definitions.
2306 (should-not
2307 (string-match
2309 (let ((org-export-with-tables nil))
2310 (org-test-with-temp-text
2311 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1]\n| a |"
2312 (org-export-as (org-test-default-backend))))))
2313 ;; Footnotes without a definition should throw an error.
2314 (should-error
2315 (org-test-with-parsed-data "Text[fn:1]"
2316 (org-export-get-footnote-definition
2317 (org-element-map tree 'footnote-reference #'identity info t) info)))
2318 ;; Footnote section should be ignored in TOC and in headlines
2319 ;; numbering.
2320 (should
2321 (= 1 (let ((org-footnote-section "Footnotes"))
2322 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
2323 (org-export-collect-headlines info))))))
2324 (should
2325 (equal '(2)
2326 (let ((org-footnote-section "Footnotes"))
2327 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
2328 (org-element-map tree 'headline
2329 (lambda (hl)
2330 (when (equal (org-element-property :raw-value hl) "H2")
2331 (org-export-get-headline-number hl info)))
2332 info t)))))))
2336 ;;; Headlines and Inlinetasks
2338 (ert-deftest test-org-export/get-relative-level ()
2339 "Test `org-export-get-relative-level' specifications."
2340 ;; Standard test.
2341 (should
2342 (equal '(1 2)
2343 (let ((org-odd-levels-only nil))
2344 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2345 (org-element-map tree 'headline
2346 (lambda (h) (org-export-get-relative-level h info))
2347 info)))))
2348 ;; Missing levels
2349 (should
2350 (equal '(1 3)
2351 (let ((org-odd-levels-only nil))
2352 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
2353 (org-element-map tree 'headline
2354 (lambda (h) (org-export-get-relative-level h info))
2355 info))))))
2357 (ert-deftest test-org-export/low-level-p ()
2358 "Test `org-export-low-level-p' specifications."
2359 (should
2360 (equal
2361 '(no yes)
2362 (let ((org-odd-levels-only nil))
2363 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2364 (org-element-map tree 'headline
2365 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
2366 (plist-put info :headline-levels 1)))))))
2368 (ert-deftest test-org-export/get-headline-number ()
2369 "Test `org-export-get-headline-number' specifications."
2370 ;; Standard test.
2371 (should
2372 (equal
2373 '((1) (1 1))
2374 (let ((org-odd-levels-only nil))
2375 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2376 (org-element-map tree 'headline
2377 (lambda (h) (org-export-get-headline-number h info))
2378 info)))))
2379 ;; Missing levels are replaced with 0.
2380 (should
2381 (equal
2382 '((1) (1 0 1))
2383 (let ((org-odd-levels-only nil))
2384 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
2385 (org-element-map tree 'headline
2386 (lambda (h) (org-export-get-headline-number h info))
2387 info))))))
2389 (ert-deftest test-org-export/numbered-headline-p ()
2390 "Test `org-export-numbered-headline-p' specifications."
2391 ;; If `:section-numbers' is nil, never number headlines.
2392 (should-not
2393 (org-test-with-parsed-data "* Headline"
2394 (org-element-map tree 'headline
2395 (lambda (h) (org-export-numbered-headline-p h info))
2396 (plist-put info :section-numbers nil))))
2397 ;; If `:section-numbers' is a number, only number headlines with
2398 ;; a level greater that it.
2399 (should
2400 (equal
2401 '(yes no)
2402 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2403 (org-element-map tree 'headline
2404 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
2405 (plist-put info :section-numbers 1)))))
2406 ;; Otherwise, headlines are always numbered.
2407 (should
2408 (org-test-with-parsed-data "* Headline"
2409 (org-element-map tree 'headline
2410 (lambda (h) (org-export-numbered-headline-p h info))
2411 (plist-put info :section-numbers t))))
2412 ;; With #+OPTIONS: num:nil all headlines are unnumbered.
2413 (should-not
2414 (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
2415 (org-export-numbered-headline-p
2416 (org-element-map tree 'headline 'identity info t)
2417 info)))
2418 ;; Headlines with a non-nil UNNUMBERED property are not numbered.
2419 (should-not
2420 (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
2421 (org-export-numbered-headline-p
2422 (org-element-map tree 'headline #'identity info t)
2423 info)))
2424 ;; UNNUMBERED ignores inheritance. Any non-nil value among
2425 ;; ancestors disables numbering.
2426 (should
2427 (org-test-with-parsed-data
2428 "* H
2429 :PROPERTIES:
2430 :UNNUMBERED: t
2431 :END:
2432 ** H2
2433 :PROPERTIES:
2434 :UNNUMBERED: nil
2435 :END:
2436 *** H3"
2437 (cl-every
2438 (lambda (h) (not (org-export-numbered-headline-p h info)))
2439 (org-element-map tree 'headline #'identity info)))))
2441 (ert-deftest test-org-export/number-to-roman ()
2442 "Test `org-export-number-to-roman' specifications."
2443 ;; If number is negative, return it as a string.
2444 (should (equal (org-export-number-to-roman -1) "-1"))
2445 ;; Otherwise, return it as a roman number.
2446 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
2448 (ert-deftest test-org-export/get-optional-title ()
2449 "Test `org-export-get-alt-title' specifications."
2450 ;; If ALT_TITLE property is defined, use it.
2451 (should
2452 (equal '("opt")
2453 (org-test-with-parsed-data
2454 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
2455 (org-export-get-alt-title
2456 (org-element-map tree 'headline 'identity info t)
2457 info))))
2458 ;; Otherwise, fall-back to regular title.
2459 (should
2460 (equal '("Headline")
2461 (org-test-with-parsed-data "* Headline"
2462 (org-export-get-alt-title
2463 (org-element-map tree 'headline 'identity info t)
2464 info)))))
2466 (ert-deftest test-org-export/get-tags ()
2467 "Test `org-export-get-tags' specifications."
2468 ;; Standard test: tags which are not a select tag, an exclude tag,
2469 ;; or specified as optional argument shouldn't be ignored.
2470 (should
2471 (org-test-with-parsed-data "* Headline :tag:"
2472 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2473 info)))
2474 ;; Tags provided in the optional argument are ignored.
2475 (should-not
2476 (org-test-with-parsed-data "* Headline :ignore:"
2477 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2478 info '("ignore"))))
2479 ;; Allow tag inheritance.
2480 (should
2481 (equal
2482 '(("tag") ("tag"))
2483 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
2484 (org-element-map tree 'headline
2485 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
2486 ;; Tag inheritance checks FILETAGS keywords.
2487 (should
2488 (equal
2489 '(("a" "b" "tag"))
2490 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
2491 (org-element-map tree 'headline
2492 (lambda (hl) (org-export-get-tags hl info nil t)) info)))))
2494 (ert-deftest test-org-export/get-node-property ()
2495 "Test`org-export-get-node-property' specifications."
2496 ;; Standard test.
2497 (should
2498 (equal "value"
2499 (org-test-with-parsed-data "* Headline
2500 :PROPERTIES:
2501 :prop: value
2502 :END:"
2503 (org-export-get-node-property
2504 :PROP (org-element-map tree 'headline 'identity nil t)))))
2505 ;; Test inheritance.
2506 (should
2507 (equal "value"
2508 (org-test-with-parsed-data "* Parent
2509 :PROPERTIES:
2510 :prop: value
2511 :END:
2512 ** Headline
2513 Paragraph"
2514 (org-export-get-node-property
2515 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
2516 ;; Cannot return a value before the first headline.
2517 (should-not
2518 (org-test-with-parsed-data "Paragraph
2519 * Headline
2520 :PROPERTIES:
2521 :prop: value
2522 :END:"
2523 (org-export-get-node-property
2524 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
2526 (ert-deftest test-org-export/get-category ()
2527 "Test `org-export-get-category' specifications."
2528 ;; Standard test.
2529 (should
2530 (equal "value"
2531 (org-test-with-parsed-data "* Headline
2532 :PROPERTIES:
2533 :CATEGORY: value
2534 :END:"
2535 (org-export-get-category
2536 (org-element-map tree 'headline 'identity nil t) info))))
2537 ;; Test inheritance from a parent headline.
2538 (should
2539 (equal '("value" "value")
2540 (org-test-with-parsed-data "* Headline1
2541 :PROPERTIES:
2542 :CATEGORY: value
2543 :END:
2544 ** Headline2"
2545 (org-element-map tree 'headline
2546 (lambda (hl) (org-export-get-category hl info)) info))))
2547 ;; Test inheritance from #+CATEGORY keyword
2548 (should
2549 (equal "value"
2550 (org-test-with-parsed-data "#+CATEGORY: value
2551 * Headline"
2552 (org-export-get-category
2553 (org-element-map tree 'headline 'identity nil t) info))))
2554 ;; Test inheritance from file name.
2555 (should
2556 (equal "test"
2557 (org-test-with-parsed-data "* Headline"
2558 (let ((info (plist-put info :input-file "~/test.org")))
2559 (org-export-get-category
2560 (org-element-map tree 'headline 'identity nil t) info)))))
2561 ;; Fall-back value.
2562 (should
2563 (equal "???"
2564 (org-test-with-parsed-data "* Headline"
2565 (org-export-get-category
2566 (org-element-map tree 'headline 'identity nil t) info)))))
2568 (ert-deftest test-org-export/first-sibling-p ()
2569 "Test `org-export-first-sibling-p' specifications."
2570 ;; Standard test.
2571 (should
2572 (equal
2573 '(yes yes no)
2574 (org-test-with-parsed-data "* H\n** H 2\n** H 3"
2575 (org-element-map tree 'headline
2576 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2577 info))))
2578 (should
2579 (equal '(yes no)
2580 (org-test-with-parsed-data "- item\n\n para"
2581 (org-element-map tree 'paragraph
2582 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2583 info))))
2584 ;; Ignore sections for headlines.
2585 (should
2586 (equal '(yes yes)
2587 (org-test-with-parsed-data "* H\nSection\n** H 2"
2588 (org-element-map tree 'headline
2589 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2590 info))))
2591 ;; Ignore headlines not exported.
2592 (should
2593 (equal
2594 '(yes)
2595 (let ((org-export-exclude-tags '("ignore")))
2596 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
2597 (org-element-map tree 'headline
2598 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2599 info))))))
2601 (ert-deftest test-org-export/last-sibling-p ()
2602 "Test `org-export-last-sibling-p' specifications."
2603 ;; Standard test.
2604 (should
2605 (equal
2606 '(yes no yes)
2607 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
2608 (org-element-map tree 'headline
2609 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2610 info))))
2611 (should
2612 (equal '(no yes)
2613 (org-test-with-parsed-data "- item\n\n para"
2614 (org-element-map tree 'paragraph
2615 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2616 info))))
2617 ;; Ignore headlines not exported.
2618 (should
2619 (equal
2620 '(yes)
2621 (let ((org-export-exclude-tags '("ignore")))
2622 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
2623 (org-element-map tree 'headline
2624 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2625 info))))))
2627 (ert-deftest test-org-export/handle-inlinetasks ()
2628 "Test inlinetask export."
2629 ;; Inlinetask with an exclude tag.
2630 (when (featurep 'org-inlinetask)
2631 (should
2632 (equal
2634 (let ((org-inlinetask-min-level 3)
2635 org-export-filter-body-functions
2636 org-export-filter-final-output-functions)
2637 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
2638 (org-export-as (org-test-default-backend)
2639 nil nil nil '(:exclude-tags ("noexp")))))))
2640 ;; Inlinetask with an include tag.
2641 (should
2642 (equal
2643 "* H2\n*** Inline :exp:\n"
2644 (let ((org-inlinetask-min-level 3)
2645 (org-tags-column 0))
2646 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
2647 (org-export-as (org-test-default-backend)
2648 nil nil nil '(:select-tags ("exp")))))))
2649 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
2650 (should
2651 (equal ""
2652 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
2653 (org-inlinetask-min-level 3)
2654 org-export-filter-body-functions
2655 org-export-filter-final-output-functions)
2656 (org-test-with-temp-text "*** TODO Inline"
2657 (org-export-as (org-test-default-backend)
2658 nil nil nil '(:with-tasks nil))))))))
2662 ;;; Keywords
2664 (ert-deftest test-org-export/get-date ()
2665 "Test `org-export-get-date' specifications."
2666 ;; Return a properly formatted string when
2667 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
2668 ;; consists in a single timestamp.
2669 (should
2670 (equal "29 03 2012"
2671 (let ((org-export-date-timestamp-format "%d %m %Y"))
2672 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2673 (org-export-get-date info)))))
2674 ;; Return a secondary string otherwise.
2675 (should-not
2676 (stringp
2677 (let ((org-export-date-timestamp-format nil))
2678 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2679 (org-export-get-date info)))))
2680 (should
2681 (equal '("Date")
2682 (org-test-with-parsed-data "#+DATE: Date"
2683 (org-export-get-date info))))
2684 ;; Optional argument has precedence over
2685 ;; `org-export-date-timestamp-format'.
2686 (should
2687 (equal "29 03"
2688 (let ((org-export-date-timestamp-format "%d %m %Y"))
2689 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2690 (org-export-get-date info "%d %m"))))))
2694 ;;; Links
2696 (ert-deftest test-org-export/custom-protocol-maybe ()
2697 "Test `org-export-custom-protocol-maybe' specifications."
2698 (should
2699 (string-match
2700 "success"
2701 (progn
2702 (org-link-set-parameters "foo" :export (lambda (p d f) "success"))
2703 (org-export-string-as
2704 "[[foo:path]]"
2705 (org-export-create-backend
2706 :name 'test
2707 :transcoders
2708 '((section . (lambda (s c i) c))
2709 (paragraph . (lambda (p c i) c))
2710 (link . (lambda (l c i)
2711 (or (org-export-custom-protocol-maybe l c 'test)
2712 "failure")))))))))
2713 (should-not
2714 (string-match
2715 "success"
2716 (progn
2717 (org-link-set-parameters
2718 "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
2719 (org-export-string-as
2720 "[[foo:path]]"
2721 (org-export-create-backend
2722 :name 'no-test
2723 :transcoders
2724 '((section . (lambda (s c i) c))
2725 (paragraph . (lambda (p c i) c))
2726 (link . (lambda (l c i)
2727 (or (org-export-custom-protocol-maybe l c 'no-test)
2728 "failure")))))))))
2729 ;; Ignore anonymous back-ends.
2730 (should-not
2731 (string-match
2732 "success"
2733 (progn
2734 (org-link-set-parameters
2735 "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
2736 (org-export-string-as
2737 "[[foo:path]]"
2738 (org-export-create-backend
2739 :transcoders
2740 '((section . (lambda (s c i) c))
2741 (paragraph . (lambda (p c i) c))
2742 (link . (lambda (l c i)
2743 (or (org-export-custom-protocol-maybe l c nil)
2744 "failure"))))))))))
2746 (ert-deftest test-org-export/get-coderef-format ()
2747 "Test `org-export-get-coderef-format' specifications."
2748 ;; A link without description returns "%s"
2749 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
2750 "%s"))
2751 ;; Return "%s" when path is matched within description.
2752 (should (equal (org-export-get-coderef-format "path" "desc (path)")
2753 "desc %s"))
2754 ;; Otherwise return description.
2755 (should (equal (org-export-get-coderef-format "path" "desc")
2756 "desc")))
2758 (ert-deftest test-org-export/inline-image-p ()
2759 "Test `org-export-inline-image-p' specifications."
2760 (should
2761 (org-export-inline-image-p
2762 (org-test-with-temp-text "[[#id]]"
2763 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
2764 '(("custom-id" . "id")))))
2766 (ert-deftest test-org-export/insert-image-links ()
2767 "Test `org-export-insert-image-links' specifications."
2768 (should-not
2769 (member "file"
2770 (org-test-with-parsed-data "[[http://orgmode.org][file:image.png]]"
2771 (org-element-map tree 'link
2772 (lambda (l) (org-element-property :type l))))))
2773 (should
2774 (member "file"
2775 (org-test-with-parsed-data "[[http://orgmode.org][file:image.png]]"
2776 (org-element-map (org-export-insert-image-links tree info) 'link
2777 (lambda (l) (org-element-property :type l))))))
2778 ;; Properly set `:parent' property when replace contents with image
2779 ;; link.
2780 (should
2781 (memq 'link
2782 (org-test-with-parsed-data "[[http://orgmode.org][file:image.png]]"
2783 (org-element-map (org-export-insert-image-links tree info) 'link
2784 (lambda (l)
2785 (org-element-type (org-element-property :parent l)))))))
2786 ;; With optional argument RULES, recognize different links as
2787 ;; images.
2788 (should-not
2789 (member "file"
2790 (org-test-with-parsed-data "[[http://orgmode.org][file:image.xxx]]"
2791 (org-element-map (org-export-insert-image-links tree info) 'link
2792 (lambda (l) (org-element-property :type l))))))
2793 (should
2794 (member "file"
2795 (org-test-with-parsed-data "[[http://orgmode.org][file:image.xxx]]"
2796 (org-element-map
2797 (org-export-insert-image-links tree info '(("file" . "xxx")))
2798 'link
2799 (lambda (l) (org-element-property :type l)))))))
2801 (ert-deftest test-org-export/fuzzy-link ()
2802 "Test fuzzy links specifications."
2803 ;; Link to an headline should return headline's number.
2804 (should
2805 ;; Note: Headline's number is in fact a list of numbers.
2806 (equal '(2)
2807 (org-test-with-parsed-data
2808 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
2809 (org-element-map tree 'link
2810 (lambda (link)
2811 (org-export-get-ordinal
2812 (org-export-resolve-fuzzy-link link info) info)) info t))))
2813 ;; Link to a target in an item should return item's number.
2814 (should
2815 ;; Note: Item's number is in fact a list of numbers.
2816 (equal '(1 2)
2817 (org-test-with-parsed-data
2818 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
2819 (org-element-map tree 'link
2820 (lambda (link)
2821 (org-export-get-ordinal
2822 (org-export-resolve-fuzzy-link link info) info)) info t))))
2823 ;; Link to a target in a footnote should return footnote's number.
2824 (should
2825 (equal '(2 3)
2826 (org-test-with-parsed-data "
2827 Paragraph[fn:1][fn:2][fn:lbl3:C<<target>>][[test]][[target]]
2828 \[fn:1] A
2830 \[fn:2] <<test>>B"
2831 (org-element-map tree 'link
2832 (lambda (link)
2833 (org-export-get-ordinal
2834 (org-export-resolve-fuzzy-link link info) info)) info))))
2835 ;; Link to a named element should return sequence number of that
2836 ;; element.
2837 (should
2838 (= 2
2839 (org-test-with-parsed-data
2840 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
2841 (org-element-map tree 'link
2842 (lambda (link)
2843 (org-export-get-ordinal
2844 (org-export-resolve-fuzzy-link link info) info)) info t))))
2845 ;; Link to a target not within an item, a table, a footnote
2846 ;; reference or definition should return section number.
2847 (should
2848 (equal '(2)
2849 (org-test-with-parsed-data
2850 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
2851 (org-element-map tree 'link
2852 (lambda (link)
2853 (org-export-get-ordinal
2854 (org-export-resolve-fuzzy-link link info) info)) info t))))
2855 ;; Space are not significant when matching a fuzzy link.
2856 (should
2857 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
2858 (org-element-map tree 'link
2859 (lambda (link) (org-export-resolve-fuzzy-link link info))
2860 info t)))
2861 ;; Statistics cookies are ignored for headline match.
2862 (should
2863 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
2864 (org-element-map tree 'link
2865 (lambda (link) (org-export-resolve-fuzzy-link link info))
2866 info t)))
2867 (should
2868 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
2869 (org-element-map tree 'link
2870 (lambda (link) (org-export-resolve-fuzzy-link link info))
2871 info t))))
2873 (defun test-org-gen-loc-list(text type)
2874 (org-test-with-parsed-data text
2875 (org-element-map tree type
2876 (lambda (el) (or (org-export-get-loc el info) 'no-loc)))))
2878 (ert-deftest test-org-export/get-loc ()
2879 "Test `org-export-get-loc' specifications."
2880 (should
2881 ;; "-n" resets line number.
2882 (equal '(0)
2883 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n\n Text\n#+END_EXAMPLE"
2884 'example-block)))
2885 ;; The first "+n" has 0 lines before it
2886 (should
2887 (equal '(0)
2888 (test-org-gen-loc-list "#+BEGIN_EXAMPLE +n\n Text\n#+END_EXAMPLE"
2889 'example-block)))
2890 ;; "-n 10" resets line number but has "9 lines" before it.
2891 (should
2892 (equal '(9)
2893 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n 10\n Text\n#+END_EXAMPLE"
2894 'example-block)))
2895 ;; -n10 with two lines then +n 15
2896 (should
2897 (equal '(9 25)
2898 (test-org-gen-loc-list "
2899 #+BEGIN_EXAMPLE -n 10
2900 Text_10
2901 Second line(11)
2902 #+END_EXAMPLE
2903 #+BEGIN_EXAMPLE +n 15
2904 Text line (11 + 15)
2905 #+END_EXAMPLE"
2906 'example-block)))
2907 (should
2908 (equal '(9 19 0)
2909 (test-org-gen-loc-list "
2910 #+BEGIN_EXAMPLE -n 10
2911 Text
2912 #+END_EXAMPLE
2913 #+BEGIN_EXAMPLE +n 10
2914 Text
2915 #+END_EXAMPLE
2917 #+BEGIN_EXAMPLE -n
2918 Text
2919 #+END_EXAMPLE"
2920 'example-block)))
2921 ;; an Example Block without -n does not add to the line count.
2922 (should
2923 (equal '(9 no-loc 19)
2924 (test-org-gen-loc-list "
2925 #+BEGIN_EXAMPLE -n 10
2926 Text
2927 #+END_EXAMPLE
2928 #+BEGIN_EXAMPLE
2929 Text
2930 #+END_EXAMPLE
2931 #+BEGIN_EXAMPLE +n 10
2932 Text
2933 #+END_EXAMPLE"
2934 'example-block)))
2935 ;; "-n" resets line number.
2936 (should
2937 (equal
2938 '(0)
2939 (test-org-gen-loc-list "#+BEGIN_SRC emacs-lisp -n \n (- 1 1) \n#+END_SRC"
2940 'src-block)))
2941 ;; The first "+n" has 0 lines before it.
2942 (should
2943 (equal '(0)
2944 (test-org-gen-loc-list
2945 "#+BEGIN_SRC emacs-lisp +n \n (+ 0 (- 1 1))\n#+END_SRC"
2946 'src-block)))
2947 ;; "-n 10" resets line number but has "9 lines" before it.
2948 (should
2949 (equal '(9)
2950 (test-org-gen-loc-list
2951 "#+BEGIN_SRC emacs-lisp -n 10\n (- 10 1)\n#+END_SRC"
2952 'src-block)))
2953 (should
2954 (equal '(9 25)
2955 (test-org-gen-loc-list "
2956 #+BEGIN_SRC emacs-lisp -n 10
2957 (- 10 1)
2958 (+ (- 10 1) 1)
2959 #+END_SRC
2960 #+BEGIN_SRC emacs-lisp +n 15
2961 (+ (- 10 1) 2 (- 15 1))
2962 #+END_SRC"
2963 'src-block)))
2964 (should
2965 (equal '(9 19 0)
2966 (test-org-gen-loc-list "
2967 #+BEGIN_SRC emacs-lisp -n 10
2968 (- 10 1)
2969 #+END_SRC
2970 #+BEGIN_SRC emacs-lisp +n 10
2971 (+ (- 10 1) 1 (- 10 1))
2972 #+END_SRC
2973 #+BEGIN_SRC emacs-lisp -n
2974 (- 1 1)
2975 #+END_SRC"
2976 'src-block)))
2977 ;; A SRC Block without -n does not add to the line count.
2978 (should
2979 (equal '(9 no-loc 19)
2980 (test-org-gen-loc-list
2981 "#+BEGIN_SRC emacs-lisp -n 10
2982 (+ (-10 1) 1)
2983 #+END_SRC
2984 #+BEGIN_SRC emacs-lisp
2985 (+ 2 2)
2986 #+END_SRC
2987 #+BEGIN_SRC emacs-lisp +n 10
2988 (+ (- 10 1) 1 (- 10 1))
2989 #+END_SRC"
2990 'src-block))))
2992 (ert-deftest test-org-export/resolve-coderef ()
2993 "Test `org-export-resolve-coderef' specifications."
2994 (let ((org-coderef-label-format "(ref:%s)"))
2995 ;; A link to a "-n -k -r" block returns line number.
2996 (should
2997 (= 1
2998 (org-test-with-parsed-data
2999 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3000 (org-export-resolve-coderef "coderef" info))))
3001 (should
3002 (= 10
3003 (org-test-with-parsed-data
3004 "#+BEGIN_EXAMPLE -n 10 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3005 (org-export-resolve-coderef "coderef" info))))
3006 (should
3007 (= 135
3008 (org-test-with-parsed-data
3009 "#+BEGIN_EXAMPLE -n 10 -k -r\nText \n#+END_EXAMPLE\n
3010 #+BEGIN_EXAMPLE +n 125 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3011 (org-export-resolve-coderef "coderef" info))))
3012 (should
3013 (= 1
3014 (org-test-with-parsed-data
3015 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3016 (org-export-resolve-coderef "coderef" info))))
3017 (should
3018 (= 10
3019 (org-test-with-parsed-data
3020 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3021 (org-export-resolve-coderef "coderef" info))))
3022 (should
3023 (= 135
3024 (org-test-with-parsed-data
3025 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) \n#+END_SRC\n
3026 #+BEGIN_SRC emacs-lisp +n 125 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3027 (org-export-resolve-coderef "coderef" info))))
3028 ;; A link to a "-n -r" block returns line number.
3029 (should
3030 (= 1
3031 (org-test-with-parsed-data
3032 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
3033 (org-export-resolve-coderef "coderef" info))))
3034 (should
3035 (= 10
3036 (org-test-with-parsed-data
3037 "#+BEGIN_EXAMPLE -n 10 -r\nText (ref:coderef)\n#+END_EXAMPLE"
3038 (org-export-resolve-coderef "coderef" info))))
3039 (should
3040 (= 135
3041 (org-test-with-parsed-data
3042 "#+BEGIN_EXAMPLE +n 10 -r\nText \n#+END_EXAMPLE
3043 #+BEGIN_EXAMPLE +n 125 -r\nText (ref:coderef)\n#+END_EXAMPLE"
3044 (org-export-resolve-coderef "coderef" info))))
3046 (should
3047 (= 1
3048 (org-test-with-parsed-data
3049 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3050 (org-export-resolve-coderef "coderef" info))))
3051 (should
3052 (= 10
3053 (org-test-with-parsed-data
3054 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3055 (org-export-resolve-coderef "coderef" info))))
3056 (should
3057 (= 135
3058 (org-test-with-parsed-data
3059 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) \n#+END_SRC
3060 #+BEGIN_SRC emacs-lisp +n125 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3061 (org-export-resolve-coderef "coderef" info))))
3062 ;; A link to a "-n" block returns coderef.
3063 (should
3064 (equal "coderef"
3065 (org-test-with-parsed-data
3066 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3067 (org-export-resolve-coderef "coderef" info))))
3068 (should
3069 (equal "coderef"
3070 (org-test-with-parsed-data
3071 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
3072 (org-export-resolve-coderef "coderef" info))))
3073 ;; A link to a "-r" block returns line number.
3074 (should
3075 (= 1
3076 (org-test-with-parsed-data
3077 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3078 (org-export-resolve-coderef "coderef" info))))
3079 (should
3080 (= 1
3081 (org-test-with-parsed-data
3082 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
3083 (org-export-resolve-coderef "coderef" info))))
3084 ;; A link to a block without a switch returns coderef.
3085 (should
3086 (equal "coderef"
3087 (org-test-with-parsed-data
3088 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3089 (org-export-resolve-coderef "coderef" info))))
3090 (org-test-with-parsed-data
3091 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
3092 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
3093 ;; Correctly handle continued line numbers. A "+n" switch should
3094 ;; resume numbering from previous block with numbered lines,
3095 ;; ignoring blocks not numbering lines in the process. A "-n"
3096 ;; switch resets count.
3097 (should
3098 (equal '(2 1)
3099 (org-test-with-parsed-data "
3100 #+BEGIN_EXAMPLE -n
3101 Text.
3102 #+END_EXAMPLE
3104 #+BEGIN_SRC emacs-lisp
3105 \(- 1 1)
3106 #+END_SRC
3108 #+BEGIN_SRC emacs-lisp +n -r
3109 \(+ 1 1) (ref:addition)
3110 #+END_SRC
3112 #+BEGIN_EXAMPLE -n -r
3113 Another text. (ref:text)
3114 #+END_EXAMPLE"
3115 (list (org-export-resolve-coderef "addition" info)
3116 (org-export-resolve-coderef "text" info)))))
3117 ;; Recognize coderef with user-specified syntax.
3118 (should
3119 (equal
3120 "text"
3121 (org-test-with-parsed-data
3122 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
3123 (org-export-resolve-coderef "text" info))))
3124 ;; Unresolved coderefs raise a `org-link-broken' signal.
3125 (should
3126 (condition-case nil
3127 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
3128 (org-export-resolve-coderef "unknown" info))
3129 (org-link-broken t)))))
3131 (ert-deftest test-org-export/resolve-fuzzy-link ()
3132 "Test `org-export-resolve-fuzzy-link' specifications."
3133 ;; Match target objects.
3134 (should
3135 (org-test-with-parsed-data "<<target>> [[target]]"
3136 (org-export-resolve-fuzzy-link
3137 (org-element-map tree 'link 'identity info t) info)))
3138 ;; Match named elements.
3139 (should
3140 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
3141 (org-export-resolve-fuzzy-link
3142 (org-element-map tree 'link 'identity info t) info)))
3143 ;; Match exact headline's name.
3144 (should
3145 (org-test-with-parsed-data "* My headline\n[[My headline]]"
3146 (org-export-resolve-fuzzy-link
3147 (org-element-map tree 'link 'identity info t) info)))
3148 ;; Targets objects have priority over headline titles.
3149 (should
3150 (eq 'target
3151 (org-test-with-parsed-data "* target\n<<target>>[[target]]"
3152 (org-element-type
3153 (org-export-resolve-fuzzy-link
3154 (org-element-map tree 'link 'identity info t) info)))))
3155 ;; Named elements have priority over headline titles.
3156 (should
3157 (eq 'paragraph
3158 (org-test-with-parsed-data
3159 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
3160 (org-element-type
3161 (org-export-resolve-fuzzy-link
3162 (org-element-map tree 'link 'identity info t) info)))))
3163 ;; If link's path starts with a "*", only match headline titles,
3164 ;; though.
3165 (should
3166 (eq 'headline
3167 (org-test-with-parsed-data
3168 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
3169 (org-element-type
3170 (org-export-resolve-fuzzy-link
3171 (org-element-map tree 'link 'identity info t) info)))))
3172 ;; Raise a `org-link-broken' signal if no match.
3173 (should
3174 (org-test-with-parsed-data "[[target]]"
3175 (condition-case nil
3176 (org-export-resolve-fuzzy-link
3177 (org-element-map tree 'link #'identity info t) info)
3178 (org-link-broken t))))
3179 ;; Match fuzzy link even when before first headline.
3180 (should
3181 (eq 'headline
3182 (org-test-with-parsed-data "[[hl]]\n* hl"
3183 (org-element-type
3184 (org-export-resolve-fuzzy-link
3185 (org-element-map tree 'link 'identity info t) info)))))
3186 ;; Handle url-encoded fuzzy links.
3187 (should
3188 (org-test-with-parsed-data "* A B\n[[A%20B]]"
3189 (org-export-resolve-fuzzy-link
3190 (org-element-map tree 'link #'identity info t) info))))
3192 (ert-deftest test-org-export/resolve-id-link ()
3193 "Test `org-export-resolve-id-link' specifications."
3194 ;; Regular test for custom-id link.
3195 (should
3196 (equal '("Headline1")
3197 (org-test-with-parsed-data "* Headline1
3198 :PROPERTIES:
3199 :CUSTOM_ID: test
3200 :END:
3201 * Headline 2
3202 \[[#test]]"
3203 (org-element-property
3204 :title
3205 (org-export-resolve-id-link
3206 (org-element-map tree 'link 'identity info t) info)))))
3207 ;; Raise a `org-link-broken' signal on failing searches.
3208 (should
3209 (org-test-with-parsed-data "* Headline1
3210 :PROPERTIES:
3211 :CUSTOM_ID: test
3212 :END:
3213 * Headline 2
3214 \[[#no-match]]"
3215 (condition-case nil
3216 (org-export-resolve-id-link
3217 (org-element-map tree 'link #'identity info t) info)
3218 (org-link-broken t))))
3219 ;; Test for internal id target.
3220 (should
3221 (equal '("Headline1")
3222 (org-test-with-parsed-data "* Headline1
3223 :PROPERTIES:
3224 :ID: aaaa
3225 :END:
3226 * Headline 2
3227 \[[id:aaaa]]"
3228 (org-element-property
3229 :title
3230 (org-export-resolve-id-link
3231 (org-element-map tree 'link 'identity info t) info)))))
3232 ;; Test for external id target.
3233 (should
3234 (equal
3235 "external-file"
3236 (org-test-with-parsed-data "[[id:aaaa]]"
3237 (org-export-resolve-id-link
3238 (org-element-map tree 'link 'identity info t)
3239 (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
3241 (ert-deftest test-org-export/resolve-radio-link ()
3242 "Test `org-export-resolve-radio-link' specifications."
3243 ;; Standard test.
3244 (should
3245 (org-test-with-temp-text "<<<radio>>> radio"
3246 (org-update-radio-target-regexp)
3247 (let* ((tree (org-element-parse-buffer))
3248 (info `(:parse-tree ,tree)))
3249 (org-export-resolve-radio-link
3250 (org-element-map tree 'link 'identity info t)
3251 info))))
3252 ;; Radio targets are case-insensitive.
3253 (should
3254 (org-test-with-temp-text "<<<RADIO>>> radio"
3255 (org-update-radio-target-regexp)
3256 (let* ((tree (org-element-parse-buffer))
3257 (info `(:parse-tree ,tree)))
3258 (org-export-resolve-radio-link
3259 (org-element-map tree 'link 'identity info t)
3260 info))))
3261 ;; Radio target with objects.
3262 (should
3263 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
3264 (org-update-radio-target-regexp)
3265 (let* ((tree (org-element-parse-buffer))
3266 (info `(:parse-tree ,tree)))
3267 (org-export-resolve-radio-link
3268 (org-element-map tree 'link 'identity info t)
3269 info))))
3270 ;; Radio target with objects at its beginning.
3271 (should
3272 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
3273 (org-update-radio-target-regexp)
3274 (let* ((tree (org-element-parse-buffer))
3275 (info `(:parse-tree ,tree)))
3276 (org-export-resolve-radio-link
3277 (org-element-map tree 'link 'identity info t)
3278 info))))
3279 ;; Radio link next to an apostrophe.
3280 (should
3281 (org-test-with-temp-text "<<<radio>>> radio's"
3282 (org-update-radio-target-regexp)
3283 (let* ((tree (org-element-parse-buffer))
3284 (info `(:parse-tree ,tree)))
3285 (org-export-resolve-radio-link
3286 (org-element-map tree 'link 'identity info t)
3287 info))))
3288 ;; Multiple radio targets.
3289 (should
3290 (equal '("radio1" "radio2")
3291 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
3292 (org-update-radio-target-regexp)
3293 (let* ((tree (org-element-parse-buffer))
3294 (info `(:parse-tree ,tree)))
3295 (org-element-map tree 'link
3296 (lambda (link)
3297 (org-element-property
3298 :value (org-export-resolve-radio-link link info)))
3299 info)))))
3300 ;; Radio target is whitespace insensitive.
3301 (should
3302 (org-test-with-temp-text "<<<a radio>>> a\n radio"
3303 (org-update-radio-target-regexp)
3304 (let* ((tree (org-element-parse-buffer))
3305 (info `(:parse-tree ,tree)))
3306 (org-element-map tree 'link
3307 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
3309 (ert-deftest test-org-export/file-uri ()
3310 "Test `org-export-file-uri' specifications."
3311 ;; Preserve relative filenames.
3312 (should (equal "relative.org" (org-export-file-uri "relative.org")))
3313 ;; Local files start with "file://"
3314 (should (equal (concat "file://" (expand-file-name "/local.org"))
3315 (org-export-file-uri "/local.org")))
3316 ;; Remote files start with "file://"
3317 (should (equal "file://myself@some.where:papers/last.pdf"
3318 (org-export-file-uri "/myself@some.where:papers/last.pdf")))
3319 (should (equal "file://localhost/etc/fstab"
3320 (org-export-file-uri "//localhost/etc/fstab")))
3321 ;; Expand filename starting with "~".
3322 (should (equal (org-export-file-uri "~/file.org")
3323 (concat "file://" (expand-file-name "~/file.org")))))
3325 (ert-deftest test-org-export/get-reference ()
3326 "Test `org-export-get-reference' specifications."
3327 (should
3328 (org-test-with-parsed-data "* Headline"
3329 (org-export-get-reference (org-element-map tree 'headline #'identity nil t)
3330 info)))
3331 ;; For a given element always return the same reference.
3332 (should
3333 (org-test-with-parsed-data "* Headline"
3334 (let ((headline (org-element-map tree 'headline #'identity nil t)))
3335 (equal (org-export-get-reference headline info)
3336 (org-export-get-reference headline info)))))
3337 ;; References get through local export back-ends.
3338 (should
3339 (org-test-with-parsed-data "* Headline"
3340 (let ((headline (org-element-map tree 'headline #'identity nil t))
3341 (backend
3342 (org-export-create-backend
3343 :transcoders
3344 '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
3345 (equal (org-trim (org-export-data-with-backend headline backend info))
3346 (org-export-get-reference headline info)))))
3347 (should
3348 (org-test-with-parsed-data "* Headline"
3349 (let ((headline (org-element-map tree 'headline #'identity nil t))
3350 (backend
3351 (org-export-create-backend
3352 :transcoders
3353 '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
3354 (equal (org-export-with-backend backend headline nil info)
3355 (org-export-get-reference headline info)))))
3356 ;; Use search cells defined in `:crossrefs'. However, handle
3357 ;; duplicate search cells.
3358 (should
3359 (equal "org0000001"
3360 (org-test-with-parsed-data "* Headline"
3361 (let* ((headline (org-element-map tree 'headline #'identity nil t))
3362 (search-cell (car (org-export-search-cells headline))))
3363 (setq info
3364 (plist-put info :crossrefs (list (cons search-cell 1))))
3365 (org-export-get-reference headline info)))))
3366 (should-not
3367 (equal '("org0000001" "org0000001")
3368 (org-test-with-parsed-data "* H\n** H"
3369 (org-element-map tree 'headline
3370 (lambda (h)
3371 (let* ((search-cell (car (org-export-search-cells h)))
3372 (info (plist-put info :crossrefs
3373 (list (cons search-cell 1)))))
3374 (org-export-get-reference h info))))))))
3377 ;;; Pseudo objects and pseudo elements
3379 (ert-deftest test-org-export/pseudo-elements ()
3380 "Test exporting pseudo-elements."
3381 ;; Handle blank lines after pseudo-elements. In particular, do not
3382 ;; replace them with white spaces.
3383 (should
3384 (equal "contents\n\nparagraph\n"
3385 (let ((backend (org-export-create-backend
3386 :transcoders
3387 '((pseudo-element . (lambda (_p c _i) c))
3388 (paragraph . (lambda (_p c _i) c))
3389 (plain-text . (lambda (c _i) c)))))
3390 (element '(pseudo-element (:post-blank 1) "contents"))
3391 (paragraph '(paragraph nil "paragraph"))
3392 (data '(org-data nil)))
3393 (org-element-adopt-elements data element paragraph)
3394 (org-export-data-with-backend data backend nil)))))
3396 (ert-deftest test-org-export/pseudo-objects ()
3397 "Test exporting pseudo-objects."
3398 ;; Handle blank spaces after pseudo-objects. In particular, do not
3399 ;; replace them with newlines.
3400 (should
3401 (equal "begin x end\n"
3402 (let ((backend (org-export-create-backend
3403 :transcoders
3404 '((pseudo-object . (lambda (_p c _i) c))
3405 (paragraph . (lambda (_p c _i) c))
3406 (plain-text . (lambda (c _i) c)))))
3407 (object '(pseudo-object (:post-blank 1) "x"))
3408 (paragraph '(paragraph nil)))
3409 (org-element-adopt-elements paragraph "begin " object "end")
3410 (org-export-data-with-backend paragraph backend nil)))))
3413 ;;; Src-block and example-block
3415 (ert-deftest test-org-export/unravel-code ()
3416 "Test `org-export-unravel-code' function."
3417 ;; Code without reference.
3418 (should
3419 (equal '("(+ 1 1)\n")
3420 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
3421 (org-export-unravel-code (org-element-at-point)))))
3422 ;; Code with reference.
3423 (should
3424 (equal '("(+ 1 1)\n" (1 . "test"))
3425 (org-test-with-temp-text
3426 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
3427 (let ((org-coderef-label-format "(ref:%s)"))
3428 (org-export-unravel-code (org-element-at-point))))))
3429 ;; Code with user-defined reference.
3430 (should
3431 (equal
3432 '("(+ 1 1)\n" (1 . "test"))
3433 (org-test-with-temp-text
3434 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
3435 (let ((org-coderef-label-format "(ref:%s)"))
3436 (org-export-unravel-code (org-element-at-point))))))
3437 ;; Code references keys are relative to the current block.
3438 (should
3439 (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
3440 (org-test-with-temp-text "
3441 #+BEGIN_EXAMPLE -n
3442 \(+ 1 1)
3443 #+END_EXAMPLE
3444 #+BEGIN_EXAMPLE +n
3445 \(+ 2 2)
3446 \(+ 3 3) (ref:one)
3447 #+END_EXAMPLE"
3448 (goto-line 5)
3449 (let ((org-coderef-label-format "(ref:%s)"))
3450 (org-export-unravel-code (org-element-at-point)))))))
3452 (ert-deftest test-org-export/format-code-default ()
3453 "Test `org-export-format-code-default' specifications."
3454 ;; Return the empty string when code is empty.
3455 (should
3456 (equal ""
3457 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
3458 (org-export-format-code-default
3459 (org-element-map tree 'src-block 'identity info t) info))))
3460 ;; Number lines, two whitespace characters before the actual loc.
3461 (should
3462 (equal "1 a\n2 b\n"
3463 (org-test-with-parsed-data
3464 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
3465 (org-export-format-code-default
3466 (org-element-map tree 'src-block 'identity info t) info))))
3467 ;; Put references 6 whitespace characters after the widest line,
3468 ;; wrapped within parenthesis.
3469 (should
3470 (equal "123 (a)\n1 (b)\n"
3471 (let ((org-coderef-label-format "(ref:%s)"))
3472 (org-test-with-parsed-data
3473 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
3474 (org-export-format-code-default
3475 (org-element-map tree 'src-block 'identity info t) info))))))
3479 ;;; Smart Quotes
3481 (ert-deftest test-org-export/activate-smart-quotes ()
3482 "Test `org-export-activate-smart-quotes' specifications."
3483 ;; Double quotes: standard test.
3484 (should
3485 (equal
3486 '("some &ldquo;quoted&rdquo; text")
3487 (let ((org-export-default-language "en"))
3488 (org-test-with-parsed-data "some \"quoted\" text"
3489 (org-element-map tree 'plain-text
3490 (lambda (s) (org-export-activate-smart-quotes s :html info))
3491 info)))))
3492 ;; Opening quotes: at the beginning of a paragraph.
3493 (should
3494 (equal
3495 '("&ldquo;begin")
3496 (let ((org-export-default-language "en"))
3497 (org-test-with-parsed-data "\"begin"
3498 (org-element-map tree 'plain-text
3499 (lambda (s) (org-export-activate-smart-quotes s :html info))
3500 info)))))
3501 ;; Opening quotes: after an object.
3502 (should
3503 (equal
3504 '("&ldquo;quoted&rdquo; text")
3505 (let ((org-export-default-language "en"))
3506 (org-test-with-parsed-data "=verb= \"quoted\" text"
3507 (org-element-map tree 'plain-text
3508 (lambda (s) (org-export-activate-smart-quotes s :html info))
3509 info)))))
3510 ;; Closing quotes: at the end of a paragraph.
3511 (should
3512 (equal
3513 '("Quoted &ldquo;text&rdquo;")
3514 (let ((org-export-default-language "en"))
3515 (org-test-with-parsed-data "Quoted \"text\""
3516 (org-element-map tree 'plain-text
3517 (lambda (s) (org-export-activate-smart-quotes s :html info))
3518 info)))))
3519 ;; Inner quotes: standard test.
3520 (should
3521 (equal '("« outer « inner » outer »")
3522 (let ((org-export-default-language "fr"))
3523 (org-test-with-parsed-data "\"outer 'inner' outer\""
3524 (org-element-map tree 'plain-text
3525 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3526 info)))))
3527 ;; Inner quotes: close to special symbols.
3528 (should
3529 (equal '("« outer (« inner ») outer »")
3530 (let ((org-export-default-language "fr"))
3531 (org-test-with-parsed-data "\"outer ('inner') outer\""
3532 (org-element-map tree 'plain-text
3533 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3534 info)))))
3535 (should
3536 (equal '("« « inner » »")
3537 (let ((org-export-default-language "fr"))
3538 (org-test-with-parsed-data "\"'inner'\""
3539 (org-element-map tree 'plain-text
3540 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3541 info)))))
3542 ;; Apostrophe: standard test.
3543 (should
3544 (equal '("It « shouldn’t » fail")
3545 (let ((org-export-default-language "fr"))
3546 (org-test-with-parsed-data "It \"shouldn't\" fail"
3547 (org-element-map tree 'plain-text
3548 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3549 info)))))
3550 (should
3551 (equal '("It shouldn’t fail")
3552 (let ((org-export-default-language "fr"))
3553 (org-test-with-parsed-data "It shouldn't fail"
3554 (org-element-map tree 'plain-text
3555 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3556 info)))))
3557 ;; Apostrophe: before an object.
3558 (should
3559 (equal
3560 '("« a’" " »")
3561 (let ((org-export-default-language "fr"))
3562 (org-test-with-parsed-data "\"a'=b=\""
3563 (org-element-map tree 'plain-text
3564 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3565 info)))))
3566 ;; Apostrophe: after an object.
3567 (should
3568 (equal '("« " "’s »")
3569 (let ((org-export-default-language "fr"))
3570 (org-test-with-parsed-data "\"=code='s\""
3571 (org-element-map tree 'plain-text
3572 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3573 info)))))
3574 ;; Special case: isolated quotes.
3575 (should
3576 (equal '("&ldquo;" "&rdquo;")
3577 (let ((org-export-default-language "en"))
3578 (org-test-with-parsed-data "\"$x$\""
3579 (org-element-map tree 'plain-text
3580 (lambda (s) (org-export-activate-smart-quotes s :html info))
3581 info)))))
3582 ;; Smart quotes in secondary strings.
3583 (should
3584 (equal '("&ldquo;" "&rdquo;")
3585 (let ((org-export-default-language "en"))
3586 (org-test-with-parsed-data "* \"$x$\""
3587 (org-element-map tree 'plain-text
3588 (lambda (s) (org-export-activate-smart-quotes s :html info))
3589 info)))))
3590 ;; Smart quotes in document keywords.
3591 (should
3592 (equal '("&ldquo;" "&rdquo;")
3593 (let ((org-export-default-language "en"))
3594 (org-test-with-parsed-data "#+TITLE: \"$x$\""
3595 (org-element-map (plist-get info :title) 'plain-text
3596 (lambda (s) (org-export-activate-smart-quotes s :html info))
3597 info)))))
3598 ;; Smart quotes in parsed affiliated keywords.
3599 (should
3600 (equal '("&ldquo;" "&rdquo;" "Paragraph")
3601 (let ((org-export-default-language "en"))
3602 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
3603 (org-element-map tree 'plain-text
3604 (lambda (s) (org-export-activate-smart-quotes s :html info))
3605 info nil nil t)))))
3606 ;; Smart quotes within objects.
3607 (should
3608 (equal '("&ldquo;foo&rdquo;")
3609 (let ((org-export-default-language "en"))
3610 (org-test-with-parsed-data "| \"foo\" |"
3611 (org-element-map tree 'plain-text
3612 (lambda (s) (org-export-activate-smart-quotes s :html info))
3613 info nil nil t)))))
3614 ;; FIXME: Test failing non-interactively.
3616 ;; (should
3617 ;; (equal '("&ldquo;foo&rdquo;")
3618 ;; (let ((org-export-default-language "en"))
3619 ;; (org-test-with-parsed-data "*\"foo\"*"
3620 ;; (org-element-map tree 'plain-text
3621 ;; (lambda (s) (org-export-activate-smart-quotes s :html info))
3622 ;; info nil nil t)))))
3627 ;;; Tables
3629 (ert-deftest test-org-export/special-column ()
3630 "Test if the table's special column is properly recognized."
3631 ;; 1. First column is special if it contains only a special marking
3632 ;; characters or empty cells.
3633 (org-test-with-temp-text "
3634 | ! | 1 |
3635 | | 2 |"
3636 (should
3637 (org-export-table-has-special-column-p
3638 (org-element-map
3639 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3640 ;; 2. If the column contains anything else, it isn't special.
3641 (org-test-with-temp-text "
3642 | ! | 1 |
3643 | b | 2 |"
3644 (should-not
3645 (org-export-table-has-special-column-p
3646 (org-element-map
3647 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3648 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
3649 ;; and "!".
3650 (org-test-with-temp-text "
3651 | # | 1 |
3652 | ^ | 2 |
3653 | * | 3 |
3654 | _ | 4 |
3655 | / | 5 |
3656 | $ | 6 |
3657 | ! | 7 |"
3658 (should
3659 (org-export-table-has-special-column-p
3660 (org-element-map
3661 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3662 ;; 4. A first column with only empty cells isn't considered as
3663 ;; special.
3664 (org-test-with-temp-text "
3665 | | 1 |
3666 | | 2 |"
3667 (should-not
3668 (org-export-table-has-special-column-p
3669 (org-element-map
3670 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
3672 (ert-deftest test-org-export/table-row-is-special-p ()
3673 "Test `org-export-table-row-is-special-p' specifications."
3674 ;; 1. A row is special if it has a special marking character in the
3675 ;; special column.
3676 (org-test-with-parsed-data "| ! | 1 |"
3677 (should
3678 (org-export-table-row-is-special-p
3679 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3680 ;; 2. A row is special when its first field is "/"
3681 (org-test-with-parsed-data "
3682 | / | 1 |
3683 | a | b |"
3684 (should
3685 (org-export-table-row-is-special-p
3686 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3687 ;; 3. A row only containing alignment cookies is also considered as
3688 ;; special.
3689 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
3690 (should
3691 (org-export-table-row-is-special-p
3692 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3693 ;; 4. Everything else isn't considered as special.
3694 (org-test-with-parsed-data "| \alpha | | c |"
3695 (should-not
3696 (org-export-table-row-is-special-p
3697 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3698 ;; 5. Table's rules are never considered as special rows.
3699 (org-test-with-parsed-data "|---+---|"
3700 (should-not
3701 (org-export-table-row-is-special-p
3702 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
3704 (ert-deftest test-org-export/has-header-p ()
3705 "Test `org-export-table-has-header-p' specifications."
3706 ;; With an header.
3707 (should
3708 (org-test-with-parsed-data "
3709 | a | b |
3710 |---+---|
3711 | c | d |"
3712 (org-export-table-has-header-p
3713 (org-element-map tree 'table 'identity info 'first-match)
3714 info)))
3715 ;; Without an header.
3716 (should-not
3717 (org-test-with-parsed-data "
3718 | a | b |
3719 | c | d |"
3720 (org-export-table-has-header-p
3721 (org-element-map tree 'table 'identity info 'first-match)
3722 info)))
3723 ;; Don't get fooled with starting and ending rules.
3724 (should-not
3725 (org-test-with-parsed-data "
3726 |---+---|
3727 | a | b |
3728 | c | d |
3729 |---+---|"
3730 (org-export-table-has-header-p
3731 (org-element-map tree 'table 'identity info 'first-match)
3732 info))))
3734 (ert-deftest test-org-export/table-row-group ()
3735 "Test `org-export-table-row-group' specifications."
3736 ;; A rule creates a new group.
3737 (should
3738 (equal '(1 rule 2)
3739 (org-test-with-parsed-data "
3740 | a | b |
3741 |---+---|
3742 | 1 | 2 |"
3743 (org-element-map tree 'table-row
3744 (lambda (row)
3745 (if (eq (org-element-property :type row) 'rule) 'rule
3746 (org-export-table-row-group row info)))))))
3747 ;; Special rows are ignored in count.
3748 (should
3749 (equal
3750 '(rule 1)
3751 (org-test-with-parsed-data "
3752 | / | < | > |
3753 |---|---+---|
3754 | | 1 | 2 |"
3755 (org-element-map tree 'table-row
3756 (lambda (row)
3757 (if (eq (org-element-property :type row) 'rule) 'rule
3758 (org-export-table-row-group row info)))
3759 info))))
3760 ;; Double rules also are ignored in count.
3761 (should
3762 (equal '(1 rule rule 2)
3763 (org-test-with-parsed-data "
3764 | a | b |
3765 |---+---|
3766 |---+---|
3767 | 1 | 2 |"
3768 (org-element-map tree 'table-row
3769 (lambda (row)
3770 (if (eq (org-element-property :type row) 'rule) 'rule
3771 (org-export-table-row-group row info))))))))
3773 (ert-deftest test-org-export/table-row-number ()
3774 "Test `org-export-table-row-number' specifications."
3775 ;; Standard test. Number is 0-indexed.
3776 (should
3777 (equal '(0 1)
3778 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
3779 (org-element-map tree 'table-row
3780 (lambda (row) (org-export-table-row-number row info)) info))))
3781 ;; Number ignores separators.
3782 (should
3783 (equal '(0 1)
3784 (org-test-with-parsed-data "
3785 | a | b | c |
3786 |---+---+---|
3787 | d | e | f |"
3788 (org-element-map tree 'table-row
3789 (lambda (row) (org-export-table-row-number row info)) info))))
3790 ;; Number ignores special rows.
3791 (should
3792 (equal '(0 1)
3793 (org-test-with-parsed-data "
3794 | / | < | > |
3795 | | b | c |
3796 |---+-----+-----|
3797 | | <c> | <c> |
3798 | | e | f |"
3799 (org-element-map tree 'table-row
3800 (lambda (row) (org-export-table-row-number row info)) info)))))
3802 (ert-deftest test-org-export/table-cell-width ()
3803 "Test `org-export-table-cell-width' specifications."
3804 ;; 1. Width is primarily determined by width cookies. If no cookie
3805 ;; is found, cell's width is nil.
3806 (org-test-with-parsed-data "
3807 | / | <l> | <6> | <l7> |
3808 | | a | b | c |"
3809 (should
3810 (equal
3811 '(nil 6 7)
3812 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3813 (org-element-map tree 'table-cell 'identity info)))))
3814 ;; 2. The last width cookie has precedence.
3815 (org-test-with-parsed-data "
3816 | <6> |
3817 | <7> |
3818 | a |"
3819 (should
3820 (equal
3821 '(7)
3822 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3823 (org-element-map tree 'table-cell 'identity info)))))
3824 ;; 3. Valid width cookies must have a specific row.
3825 (org-test-with-parsed-data "| <6> | cell |"
3826 (should
3827 (equal
3828 '(nil nil)
3829 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3830 (org-element-map tree 'table-cell 'identity))))))
3832 (ert-deftest test-org-export/table-cell-alignment ()
3833 "Test `org-export-table-cell-alignment' specifications."
3834 ;; 1. Alignment is primarily determined by alignment cookies.
3835 (should
3836 (equal '(left center right)
3837 (let ((org-table-number-fraction 0.5)
3838 (org-table-number-regexp "^[0-9]+$"))
3839 (org-test-with-parsed-data "| <l> | <c> | <r> |"
3840 (mapcar (lambda (cell)
3841 (org-export-table-cell-alignment cell info))
3842 (org-element-map tree 'table-cell 'identity))))))
3843 ;; 2. The last alignment cookie has precedence.
3844 (should
3845 (equal '(right right right)
3846 (org-test-with-parsed-data "
3847 | <l8> |
3848 | cell |
3849 | <r9> |"
3850 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
3851 (org-element-map tree 'table-cell 'identity)))))
3852 ;; 3. If there's no cookie, cell's contents determine alignment.
3853 ;; A column mostly made of cells containing numbers will align
3854 ;; its cells to the right.
3855 (should
3856 (equal '(right right right)
3857 (let ((org-table-number-fraction 0.5)
3858 (org-table-number-regexp "^[0-9]+$"))
3859 (org-test-with-parsed-data "
3860 | 123 |
3861 | some text |
3862 | 12345 |"
3863 (mapcar (lambda (cell)
3864 (org-export-table-cell-alignment cell info))
3865 (org-element-map tree 'table-cell 'identity))))))
3866 ;; 4. Otherwise, they will be aligned to the left.
3867 (should
3868 (equal '(left left left)
3869 (org-test-with-parsed-data "
3870 | text |
3871 | some text |
3872 | \alpha |"
3873 (mapcar (lambda (cell)
3874 (org-export-table-cell-alignment cell info))
3875 (org-element-map tree 'table-cell 'identity info))))))
3877 (ert-deftest test-org-export/table-cell-borders ()
3878 "Test `org-export-table-cell-borders' specifications."
3879 ;; 1. Recognize various column groups indicators.
3880 (org-test-with-parsed-data "| / | < | > | <> |"
3881 (should
3882 (equal
3883 '((right bottom top) (left bottom top) (right bottom top)
3884 (right left bottom top))
3885 (mapcar (lambda (cell)
3886 (org-export-table-cell-borders cell info))
3887 (org-element-map tree 'table-cell 'identity)))))
3888 ;; 2. Accept shortcuts to define column groups.
3889 (org-test-with-parsed-data "| / | < | < |"
3890 (should
3891 (equal
3892 '((right bottom top) (right left bottom top) (left bottom top))
3893 (mapcar (lambda (cell)
3894 (org-export-table-cell-borders cell info))
3895 (org-element-map tree 'table-cell 'identity)))))
3896 ;; 3. A valid column groups row must start with a "/".
3897 (org-test-with-parsed-data "
3898 | | < |
3899 | a | b |"
3900 (should
3901 (equal '((top) (top) (bottom) (bottom))
3902 (mapcar (lambda (cell)
3903 (org-export-table-cell-borders cell info))
3904 (org-element-map tree 'table-cell 'identity)))))
3905 ;; 4. Take table rules into consideration.
3906 (org-test-with-parsed-data "
3907 | 1 |
3908 |---|
3909 | 2 |"
3910 (should
3911 (equal '((below top) (bottom above))
3912 (mapcar (lambda (cell)
3913 (org-export-table-cell-borders cell info))
3914 (org-element-map tree 'table-cell 'identity)))))
3915 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
3916 ;; (resp. `bottom' and `below') borders. Any special row is
3917 ;; ignored.
3918 (org-test-with-parsed-data "
3919 |---+----|
3920 | / | |
3921 | | 1 |
3922 |---+----|"
3923 (should
3924 (equal '((bottom below top above))
3925 (last
3926 (mapcar (lambda (cell)
3927 (org-export-table-cell-borders cell info))
3928 (org-element-map tree 'table-cell 'identity)))))))
3930 (ert-deftest test-org-export/table-dimensions ()
3931 "Test `org-export-table-dimensions' specifications."
3932 ;; 1. Standard test.
3933 (org-test-with-parsed-data "
3934 | 1 | 2 | 3 |
3935 | 4 | 5 | 6 |"
3936 (should
3937 (equal '(2 . 3)
3938 (org-export-table-dimensions
3939 (org-element-map tree 'table 'identity info 'first-match) info))))
3940 ;; 2. Ignore horizontal rules and special columns.
3941 (org-test-with-parsed-data "
3942 | / | < | > |
3943 | 1 | 2 | 3 |
3944 |---+---+---|
3945 | 4 | 5 | 6 |"
3946 (should
3947 (equal '(2 . 3)
3948 (org-export-table-dimensions
3949 (org-element-map tree 'table 'identity info 'first-match) info)))))
3951 (ert-deftest test-org-export/table-cell-address ()
3952 "Test `org-export-table-cell-address' specifications."
3953 ;; 1. Standard test: index is 0-based.
3954 (org-test-with-parsed-data "| a | b |"
3955 (should
3956 (equal '((0 . 0) (0 . 1))
3957 (org-element-map tree 'table-cell
3958 (lambda (cell) (org-export-table-cell-address cell info))
3959 info))))
3960 ;; 2. Special column isn't counted, nor are special rows.
3961 (org-test-with-parsed-data "
3962 | / | <> |
3963 | | c |"
3964 (should
3965 (equal '(0 . 0)
3966 (org-export-table-cell-address
3967 (car (last (org-element-map tree 'table-cell 'identity info)))
3968 info))))
3969 ;; 3. Tables rules do not count either.
3970 (org-test-with-parsed-data "
3971 | a |
3972 |---|
3973 | b |
3974 |---|
3975 | c |"
3976 (should
3977 (equal '(2 . 0)
3978 (org-export-table-cell-address
3979 (car (last (org-element-map tree 'table-cell 'identity info)))
3980 info))))
3981 ;; 4. Return nil for special cells.
3982 (org-test-with-parsed-data "| / | a |"
3983 (should-not
3984 (org-export-table-cell-address
3985 (org-element-map tree 'table-cell 'identity nil 'first-match)
3986 info))))
3988 (ert-deftest test-org-export/get-table-cell-at ()
3989 "Test `org-export-get-table-cell-at' specifications."
3990 ;; 1. Address ignores special columns, special rows and rules.
3991 (org-test-with-parsed-data "
3992 | / | <> |
3993 | | a |
3994 |---+----|
3995 | | b |"
3996 (should
3997 (equal '("b")
3998 (org-element-contents
3999 (org-export-get-table-cell-at
4000 '(1 . 0)
4001 (org-element-map tree 'table 'identity info 'first-match)
4002 info)))))
4003 ;; 2. Return value for a non-existent address is nil.
4004 (org-test-with-parsed-data "| a |"
4005 (should-not
4006 (org-export-get-table-cell-at
4007 '(2 . 2)
4008 (org-element-map tree 'table 'identity info 'first-match)
4009 info)))
4010 (org-test-with-parsed-data "| / |"
4011 (should-not
4012 (org-export-get-table-cell-at
4013 '(0 . 0)
4014 (org-element-map tree 'table 'identity info 'first-match)
4015 info))))
4017 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
4018 "Test `org-export-table-cell-starts-colgroup-p' specifications."
4019 ;; 1. A cell at a beginning of a row always starts a column group.
4020 (org-test-with-parsed-data "| a |"
4021 (should
4022 (org-export-table-cell-starts-colgroup-p
4023 (org-element-map tree 'table-cell 'identity info 'first-match)
4024 info)))
4025 ;; 2. Special column should be ignored when determining the
4026 ;; beginning of the row.
4027 (org-test-with-parsed-data "
4028 | / | |
4029 | | a |"
4030 (should
4031 (org-export-table-cell-starts-colgroup-p
4032 (org-element-map tree 'table-cell 'identity info 'first-match)
4033 info)))
4034 ;; 2. Explicit column groups.
4035 (org-test-with-parsed-data "
4036 | / | | < |
4037 | a | b | c |"
4038 (should
4039 (equal
4040 '(yes no yes)
4041 (org-element-map tree 'table-cell
4042 (lambda (cell)
4043 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
4044 info)))))
4046 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
4047 "Test `org-export-table-cell-ends-colgroup-p' specifications."
4048 ;; 1. A cell at the end of a row always ends a column group.
4049 (org-test-with-parsed-data "| a |"
4050 (should
4051 (org-export-table-cell-ends-colgroup-p
4052 (org-element-map tree 'table-cell 'identity info 'first-match)
4053 info)))
4054 ;; 2. Special column should be ignored when determining the
4055 ;; beginning of the row.
4056 (org-test-with-parsed-data "
4057 | / | |
4058 | | a |"
4059 (should
4060 (org-export-table-cell-ends-colgroup-p
4061 (org-element-map tree 'table-cell 'identity info 'first-match)
4062 info)))
4063 ;; 3. Explicit column groups.
4064 (org-test-with-parsed-data "
4065 | / | < | |
4066 | a | b | c |"
4067 (should
4068 (equal
4069 '(yes no yes)
4070 (org-element-map tree 'table-cell
4071 (lambda (cell)
4072 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
4073 info)))))
4075 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
4076 "Test `org-export-table-row-starts-rowgroup-p' specifications."
4077 ;; 1. A row at the beginning of a table always starts a row group.
4078 ;; So does a row following a table rule.
4079 (org-test-with-parsed-data "
4080 | a |
4081 |---|
4082 | b |"
4083 (should
4084 (equal
4085 '(yes no yes)
4086 (org-element-map tree 'table-row
4087 (lambda (row)
4088 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
4089 info))))
4090 ;; 2. Special rows should be ignored when determining the beginning
4091 ;; of the row.
4092 (org-test-with-parsed-data "
4093 | / | < |
4094 | | a |
4095 |---+---|
4096 | / | < |
4097 | | b |"
4098 (should
4099 (equal
4100 '(yes no yes)
4101 (org-element-map tree 'table-row
4102 (lambda (row)
4103 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
4104 info)))))
4106 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
4107 "Test `org-export-table-row-ends-rowgroup-p' specifications."
4108 ;; 1. A row at the end of a table always ends a row group. So does
4109 ;; a row preceding a table rule.
4110 (org-test-with-parsed-data "
4111 | a |
4112 |---|
4113 | b |"
4114 (should
4115 (equal
4116 '(yes no yes)
4117 (org-element-map tree 'table-row
4118 (lambda (row)
4119 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
4120 info))))
4121 ;; 2. Special rows should be ignored when determining the beginning
4122 ;; of the row.
4123 (org-test-with-parsed-data "
4124 | | a |
4125 | / | < |
4126 |---+---|
4127 | | b |
4128 | / | < |"
4129 (should
4130 (equal
4131 '(yes no yes)
4132 (org-element-map tree 'table-row
4133 (lambda (row)
4134 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
4135 info)))))
4137 (ert-deftest test-org-export/table-row-in-header-p ()
4138 "Test `org-export-table-row-in-header-p' specifications."
4139 ;; Standard test. Separators are always nil.
4140 (should
4141 (equal
4142 '(yes no no)
4143 (org-test-with-parsed-data "| a |\n|---|\n| b |"
4144 (org-element-map tree 'table-row
4145 (lambda (row)
4146 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
4147 ;; Nil when there is no header.
4148 (should
4149 (equal
4150 '(no no)
4151 (org-test-with-parsed-data "| a |\n| b |"
4152 (org-element-map tree 'table-row
4153 (lambda (row)
4154 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
4156 (ert-deftest test-org-export/table-row-starts-header-p ()
4157 "Test `org-export-table-row-starts-header-p' specifications."
4158 ;; 1. Only the row starting the first row group starts the table
4159 ;; header.
4160 (org-test-with-parsed-data "
4161 | a |
4162 | b |
4163 |---|
4164 | c |"
4165 (should
4166 (equal
4167 '(yes no no no)
4168 (org-element-map tree 'table-row
4169 (lambda (row)
4170 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
4171 info))))
4172 ;; 2. A row cannot start an header if there's no header in the
4173 ;; table.
4174 (org-test-with-parsed-data "
4175 | a |
4176 |---|"
4177 (should-not
4178 (org-export-table-row-starts-header-p
4179 (org-element-map tree 'table-row 'identity info 'first-match)
4180 info))))
4182 (ert-deftest test-org-export/table-row-ends-header-p ()
4183 "Test `org-export-table-row-ends-header-p' specifications."
4184 ;; 1. Only the row starting the first row group starts the table
4185 ;; header.
4186 (org-test-with-parsed-data "
4187 | a |
4188 | b |
4189 |---|
4190 | c |"
4191 (should
4192 (equal
4193 '(no yes no no)
4194 (org-element-map tree 'table-row
4195 (lambda (row)
4196 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
4197 info))))
4198 ;; 2. A row cannot start an header if there's no header in the
4199 ;; table.
4200 (org-test-with-parsed-data "
4201 | a |
4202 |---|"
4203 (should-not
4204 (org-export-table-row-ends-header-p
4205 (org-element-map tree 'table-row 'identity info 'first-match)
4206 info))))
4210 ;;; Tables of Contents
4212 (ert-deftest test-org-export/collect-headlines ()
4213 "Test `org-export-collect-headlines' specifications."
4214 ;; Standard test.
4215 (should
4216 (= 2
4217 (length
4218 (org-test-with-parsed-data "* H1\n** H2"
4219 (org-export-collect-headlines info)))))
4220 ;; Do not collect headlines below optional argument.
4221 (should
4222 (= 1
4223 (length
4224 (org-test-with-parsed-data "* H1\n** H2"
4225 (org-export-collect-headlines info 1)))))
4226 ;; Never collect headlines below maximum headline level.
4227 (should
4228 (= 1
4229 (length
4230 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4231 (org-export-collect-headlines info)))))
4232 (should
4233 (= 1
4234 (length
4235 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4236 (org-export-collect-headlines info 2)))))
4237 ;; Collect headlines locally.
4238 (should
4239 (= 2
4240 (org-test-with-parsed-data "* H1\n** H2\n** H3"
4241 (let ((scope (org-element-map tree 'headline #'identity info t)))
4242 (length (org-export-collect-headlines info nil scope))))))
4243 ;; When collecting locally, optional level is relative.
4244 (should
4245 (= 1
4246 (org-test-with-parsed-data "* H1\n** H2\n*** H3"
4247 (let ((scope (org-element-map tree 'headline #'identity info t)))
4248 (length (org-export-collect-headlines info 1 scope)))))))
4252 ;;; Templates
4254 (ert-deftest test-org-export/inner-template ()
4255 "Test `inner-template' translator specifications."
4256 (should
4257 (equal "Success!"
4258 (org-test-with-temp-text "* Headline"
4259 (org-export-as
4260 (org-export-create-backend
4261 :transcoders
4262 '((inner-template . (lambda (contents info) "Success!"))
4263 (headline . (lambda (h c i) "Headline"))))))))
4264 ;; Inner template is applied even in a "body-only" export.
4265 (should
4266 (equal "Success!"
4267 (org-test-with-temp-text "* Headline"
4268 (org-export-as
4269 (org-export-create-backend
4270 :transcoders '((inner-template . (lambda (c i) "Success!"))
4271 (headline . (lambda (h c i) "Headline"))))
4272 nil nil 'body-only)))))
4274 (ert-deftest test-org-export/template ()
4275 "Test `template' translator specifications."
4276 (should
4277 (equal "Success!"
4278 (org-test-with-temp-text "* Headline"
4279 (org-export-as
4280 (org-export-create-backend
4281 :transcoders '((template . (lambda (contents info) "Success!"))
4282 (headline . (lambda (h c i) "Headline"))))))))
4283 ;; Template is not applied in a "body-only" export.
4284 (should-not
4285 (equal "Success!"
4286 (org-test-with-temp-text "* Headline"
4287 (org-export-as
4288 (org-export-create-backend
4289 :transcoders '((template . (lambda (contents info) "Success!"))
4290 (headline . (lambda (h c i) "Headline"))))
4291 nil nil 'body-only)))))
4295 ;;; Topology
4297 (ert-deftest test-org-export/get-next-element ()
4298 "Test `org-export-get-next-element' specifications."
4299 ;; Standard test.
4300 (should
4301 (equal "b"
4302 (org-test-with-parsed-data "* Headline\n*a* b"
4303 (org-export-get-next-element
4304 (org-element-map tree 'bold 'identity info t) info))))
4305 ;; Return nil when no previous element.
4306 (should-not
4307 (org-test-with-parsed-data "* Headline\na *b*"
4308 (org-export-get-next-element
4309 (org-element-map tree 'bold 'identity info t) info)))
4310 ;; Non-exportable elements are ignored.
4311 (should-not
4312 (let ((org-export-with-timestamps nil))
4313 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
4314 (org-export-get-next-element
4315 (org-element-map tree 'entity 'identity info t) info))))
4316 ;; Find next element in secondary strings.
4317 (should
4318 (eq 'verbatim
4319 (org-test-with-parsed-data "* a =verb="
4320 (org-element-type
4321 (org-export-get-next-element
4322 (org-element-map tree 'plain-text 'identity info t) info)))))
4323 (should
4324 (eq 'verbatim
4325 (org-test-with-parsed-data "* /italic/ =verb="
4326 (org-element-type
4327 (org-export-get-next-element
4328 (org-element-map tree 'italic 'identity info t) info)))))
4329 ;; Find next element in document keywords.
4330 (should
4331 (eq 'verbatim
4332 (org-test-with-parsed-data "#+TITLE: a =verb="
4333 (org-element-type
4334 (org-export-get-next-element
4335 (org-element-map
4336 (plist-get info :title) 'plain-text 'identity info t) info)))))
4337 ;; Find next element in parsed affiliated keywords.
4338 (should
4339 (eq 'verbatim
4340 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
4341 (org-element-type
4342 (org-export-get-next-element
4343 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4344 ;; With optional argument N, return a list containing all the
4345 ;; following elements.
4346 (should
4347 (equal
4348 '(bold code underline)
4349 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4350 (mapcar 'car
4351 (org-export-get-next-element
4352 (org-element-map tree 'italic 'identity info t) info t)))))
4353 ;; When N is a positive integer, return a list containing up to
4354 ;; N following elements.
4355 (should
4356 (equal
4357 '(bold code)
4358 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4359 (mapcar 'car
4360 (org-export-get-next-element
4361 (org-element-map tree 'italic 'identity info t) info 2))))))
4363 (ert-deftest test-org-export/get-previous-element ()
4364 "Test `org-export-get-previous-element' specifications."
4365 ;; Standard test.
4366 (should
4367 (equal "a "
4368 (org-test-with-parsed-data "* Headline\na *b*"
4369 (org-export-get-previous-element
4370 (org-element-map tree 'bold 'identity info t) info))))
4371 ;; Return nil when no previous element.
4372 (should-not
4373 (org-test-with-parsed-data "* Headline\n*a* b"
4374 (org-export-get-previous-element
4375 (org-element-map tree 'bold 'identity info t) info)))
4376 ;; Non-exportable elements are ignored.
4377 (should-not
4378 (let ((org-export-with-timestamps nil))
4379 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
4380 (org-export-get-previous-element
4381 (org-element-map tree 'entity 'identity info t) info))))
4382 ;; Find previous element in secondary strings.
4383 (should
4384 (eq 'verbatim
4385 (org-test-with-parsed-data "* =verb= a"
4386 (org-element-type
4387 (org-export-get-previous-element
4388 (org-element-map tree 'plain-text 'identity info t) info)))))
4389 (should
4390 (eq 'verbatim
4391 (org-test-with-parsed-data "* =verb= /italic/"
4392 (org-element-type
4393 (org-export-get-previous-element
4394 (org-element-map tree 'italic 'identity info t) info)))))
4395 ;; Find previous element in document keywords.
4396 (should
4397 (eq 'verbatim
4398 (org-test-with-parsed-data "#+TITLE: =verb= a"
4399 (org-element-type
4400 (org-export-get-previous-element
4401 (org-element-map
4402 (plist-get info :title) 'plain-text 'identity info t) info)))))
4403 ;; Find previous element in parsed affiliated keywords.
4404 (should
4405 (eq 'verbatim
4406 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
4407 (org-element-type
4408 (org-export-get-previous-element
4409 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4410 ;; With optional argument N, return a list containing up to
4411 ;; N previous elements.
4412 (should
4413 (equal '(underline italic bold)
4414 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4415 (mapcar 'car
4416 (org-export-get-previous-element
4417 (org-element-map tree 'code 'identity info t) info t)))))
4418 ;; When N is a positive integer, return a list containing up to
4419 ;; N previous elements.
4420 (should
4421 (equal '(italic bold)
4422 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4423 (mapcar 'car
4424 (org-export-get-previous-element
4425 (org-element-map tree 'code 'identity info t) info 2))))))
4428 (provide 'test-ox)
4429 ;;; test-org-export.el end here