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