org-duration: Fix copyright
[org-mode/org-tableheadings.git] / testing / lisp / test-ox.el
blob72b6c8ccd1e25ac010606fc781a3302fbb903547
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)))))
1436 (ert-deftest test-org-export/before-processing-hook ()
1437 "Test `org-export-before-processing-hook'."
1438 (should
1439 (equal
1440 "#+MACRO: mac val\nTest\n"
1441 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
1442 (let ((org-export-before-processing-hook
1443 '((lambda (backend)
1444 (while (re-search-forward "{{{" nil t)
1445 (let ((object (org-element-context)))
1446 (when (eq (org-element-type object) 'macro)
1447 (delete-region
1448 (org-element-property :begin object)
1449 (org-element-property :end object)))))))))
1450 (org-export-as (org-test-default-backend)))))))
1452 (ert-deftest test-org-export/before-parsing-hook ()
1453 "Test `org-export-before-parsing-hook'."
1454 (should
1455 (equal "Body 1\nBody 2\n"
1456 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
1457 (let ((org-export-before-parsing-hook
1458 '((lambda (backend)
1459 (goto-char (point-min))
1460 (while (re-search-forward org-outline-regexp-bol nil t)
1461 (delete-region
1462 (point-at-bol) (progn (forward-line) (point))))))))
1463 (org-export-as (org-test-default-backend)))))))
1467 ;;; Affiliated Keywords
1469 (ert-deftest test-org-export/read-attribute ()
1470 "Test `org-export-read-attribute' specifications."
1471 ;; Standard test.
1472 (should
1473 (equal
1474 (org-export-read-attribute
1475 :attr_html
1476 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
1477 (org-element-at-point)))
1478 '(:a "1" :b "2")))
1479 ;; Return nil on empty attribute.
1480 (should-not
1481 (org-export-read-attribute
1482 :attr_html
1483 (org-test-with-temp-text "Paragraph" (org-element-at-point))))
1484 ;; Return nil on "nil" string.
1485 (should
1486 (equal '(:a nil :b nil)
1487 (org-export-read-attribute
1488 :attr_html
1489 (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
1490 (org-element-at-point)))))
1491 ;; Return nil on empty string.
1492 (should
1493 (equal '(:a nil :b nil)
1494 (org-export-read-attribute
1495 :attr_html
1496 (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
1497 (org-element-at-point)))))
1498 ;; Return empty string when value is "".
1499 (should
1500 (equal '(:a "")
1501 (org-export-read-attribute
1502 :attr_html
1503 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
1504 (org-element-at-point)))))
1505 ;; Return \"\" when value is """".
1506 (should
1507 (equal '(:a "\"\"")
1508 (org-export-read-attribute
1509 :attr_html
1510 (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
1511 (org-element-at-point)))))
1512 ;; Ignore text before first property.
1513 (should-not
1514 (member "ignore"
1515 (org-export-read-attribute
1516 :attr_html
1517 (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
1518 (org-element-at-point))))))
1520 (ert-deftest test-org-export/get-caption ()
1521 "Test `org-export-get-caption' specifications."
1522 ;; Without optional argument, return long caption
1523 (should
1524 (equal
1525 '("l")
1526 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1527 (org-export-get-caption (org-element-at-point)))))
1528 ;; With optional argument, return short caption.
1529 (should
1530 (equal
1531 '("s")
1532 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
1533 (org-export-get-caption (org-element-at-point) t))))
1534 ;; Multiple lines are separated by white spaces.
1535 (should
1536 (equal
1537 '("a" " " "b")
1538 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
1539 (org-export-get-caption (org-element-at-point))))))
1543 ;;; Back-End Tools
1545 (ert-deftest test-org-export/define-backend ()
1546 "Test back-end definition and accessors."
1547 ;; Translate table.
1548 (should
1549 (equal '((headline . my-headline-test))
1550 (let (org-export-registered-backends)
1551 (org-export-define-backend 'test '((headline . my-headline-test)))
1552 (org-export-get-all-transcoders 'test))))
1553 ;; Filters.
1554 (should
1555 (equal '((:filter-headline . my-filter))
1556 (let (org-export-registered-backends)
1557 (org-export-define-backend 'test
1558 '((headline . my-headline-test))
1559 :filters-alist '((:filter-headline . my-filter)))
1560 (org-export-backend-filters (org-export-get-backend 'test)))))
1561 ;; Options.
1562 (should
1563 (equal '((:prop value))
1564 (let (org-export-registered-backends)
1565 (org-export-define-backend 'test
1566 '((headline . my-headline-test))
1567 :options-alist '((:prop value)))
1568 (org-export-backend-options (org-export-get-backend 'test)))))
1569 ;; Menu.
1570 (should
1571 (equal '(?k "Test Export" test)
1572 (let (org-export-registered-backends)
1573 (org-export-define-backend 'test
1574 '((headline . my-headline-test))
1575 :menu-entry '(?k "Test Export" test))
1576 (org-export-backend-menu (org-export-get-backend 'test))))))
1578 (ert-deftest test-org-export/define-derived-backend ()
1579 "Test `org-export-define-derived-backend' specifications."
1580 ;; Error when parent back-end is not defined.
1581 (should-error
1582 (let (org-export-registered-backends)
1583 (org-export-define-derived-backend 'test 'parent)))
1584 ;; Append translation table to parent's.
1585 (should
1586 (equal '((:headline . test) (:headline . parent))
1587 (let (org-export-registered-backends)
1588 (org-export-define-backend 'parent '((:headline . parent)))
1589 (org-export-define-derived-backend 'test 'parent
1590 :translate-alist '((:headline . test)))
1591 (org-export-get-all-transcoders 'test))))
1592 ;; Options defined in the new back have priority over those defined
1593 ;; in parent.
1594 (should
1595 (eq 'test
1596 (let (org-export-registered-backends)
1597 (org-export-define-backend 'parent
1598 '((:headline . parent))
1599 :options-alist '((:a nil nil 'parent)))
1600 (org-export-define-derived-backend 'test 'parent
1601 :options-alist '((:a nil nil 'test)))
1602 (plist-get (org-export--get-global-options
1603 (org-export-get-backend 'test))
1604 :a)))))
1606 (ert-deftest test-org-export/derived-backend-p ()
1607 "Test `org-export-derived-backend-p' specifications."
1608 ;; Non-nil with direct match.
1609 (should
1610 (let (org-export-registered-backends)
1611 (org-export-define-backend 'test '((headline . test)))
1612 (org-export-derived-backend-p 'test 'test)))
1613 (should
1614 (let (org-export-registered-backends)
1615 (org-export-define-backend 'test '((headline . test)))
1616 (org-export-define-derived-backend 'test2 'test)
1617 (org-export-derived-backend-p 'test2 'test2)))
1618 ;; Non-nil with a direct parent.
1619 (should
1620 (let (org-export-registered-backends)
1621 (org-export-define-backend 'test '((headline . test)))
1622 (org-export-define-derived-backend 'test2 'test)
1623 (org-export-derived-backend-p 'test2 'test)))
1624 ;; Non-nil with an indirect parent.
1625 (should
1626 (let (org-export-registered-backends)
1627 (org-export-define-backend 'test '((headline . test)))
1628 (org-export-define-derived-backend 'test2 'test)
1629 (org-export-define-derived-backend 'test3 'test2)
1630 (org-export-derived-backend-p 'test3 'test)))
1631 ;; Nil otherwise.
1632 (should-not
1633 (let (org-export-registered-backends)
1634 (org-export-define-backend 'test '((headline . test)))
1635 (org-export-define-backend 'test2 '((headline . test2)))
1636 (org-export-derived-backend-p 'test2 'test)))
1637 (should-not
1638 (let (org-export-registered-backends)
1639 (org-export-define-backend 'test '((headline . test)))
1640 (org-export-define-backend 'test2 '((headline . test2)))
1641 (org-export-define-derived-backend 'test3 'test2)
1642 (org-export-derived-backend-p 'test3 'test))))
1644 (ert-deftest test-org-export/get-all-transcoders ()
1645 "Test `org-export-get-all-transcoders' specifications."
1646 ;; Return nil when back-end cannot be found.
1647 (should-not (org-export-get-all-transcoders nil))
1648 ;; Same as `org-export-transcoders' if no parent.
1649 (should
1650 (equal '((headline . ignore))
1651 (org-export-get-all-transcoders
1652 (org-export-create-backend
1653 :transcoders '((headline . ignore))))))
1654 ;; But inherit from all ancestors whenever possible.
1655 (should
1656 (equal '((section . ignore) (headline . ignore))
1657 (let (org-export-registered-backends)
1658 (org-export-define-backend 'b1 '((headline . ignore)))
1659 (org-export-get-all-transcoders
1660 (org-export-create-backend
1661 :parent 'b1 :transcoders '((section . ignore)))))))
1662 (should
1663 (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
1664 (let (org-export-registered-backends)
1665 (org-export-define-backend 'b1 '((headline . ignore)))
1666 (org-export-define-derived-backend 'b2 'b1
1667 :translate-alist '((section . ignore)))
1668 (org-export-get-all-transcoders
1669 (org-export-create-backend
1670 :parent 'b2 :transcoders '((paragraph . ignore)))))))
1671 ;; Back-end transcoders overrule inherited ones.
1672 (should
1673 (eq 'b
1674 (let (org-export-registered-backends)
1675 (org-export-define-backend 'b1 '((headline . a)))
1676 (cdr (assq 'headline
1677 (org-export-get-all-transcoders
1678 (org-export-create-backend
1679 :parent 'b1 :transcoders '((headline . b))))))))))
1681 (ert-deftest test-org-export/get-all-options ()
1682 "Test `org-export-get-all-options' specifications."
1683 ;; Return nil when back-end cannot be found.
1684 (should-not (org-export-get-all-options nil))
1685 ;; Same as `org-export-options' if no parent.
1686 (should
1687 (equal '((headline . ignore))
1688 (org-export-get-all-options
1689 (org-export-create-backend
1690 :options '((headline . ignore))))))
1691 ;; But inherit from all ancestors whenever possible.
1692 (should
1693 (equal '((:key2 value2) (:key1 value1))
1694 (let (org-export-registered-backends)
1695 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1696 (org-export-get-all-options
1697 (org-export-create-backend
1698 :parent 'b1 :options '((:key2 value2)))))))
1699 (should
1700 (equal '((:key3 value3) (:key2 value2) (:key1 value1))
1701 (let (org-export-registered-backends)
1702 (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
1703 (org-export-define-derived-backend 'b2 'b1
1704 :options-alist '((:key2 value2)))
1705 (org-export-get-all-options
1706 (org-export-create-backend
1707 :parent 'b2 :options '((:key3 value3)))))))
1708 ;; Back-end options overrule inherited ones.
1709 (should
1710 (eq 'b
1711 (let (org-export-registered-backends)
1712 (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
1713 (cdr (assq :key1
1714 (org-export-get-all-options
1715 (org-export-create-backend
1716 :parent 'b1 :options '((:key1 . b))))))))))
1718 (ert-deftest test-org-export/get-all-filters ()
1719 "Test `org-export-get-all-filters' specifications."
1720 ;; Return nil when back-end cannot be found.
1721 (should-not (org-export-get-all-filters nil))
1722 ;; Same as `org-export-filters' if no parent.
1723 (should
1724 (equal '((:filter-headline . ignore))
1725 (org-export-get-all-filters
1726 (org-export-create-backend
1727 :filters '((:filter-headline . ignore))))))
1728 ;; But inherit from all ancestors whenever possible.
1729 (should
1730 (equal '((:filter-section . ignore) (:filter-headline . ignore))
1731 (let (org-export-registered-backends)
1732 (org-export-define-backend 'b1
1733 nil :filters-alist '((:filter-headline . ignore)))
1734 (org-export-get-all-filters
1735 (org-export-create-backend
1736 :parent 'b1 :filters '((:filter-section . ignore)))))))
1737 (should
1738 (equal '((:filter-paragraph . ignore)
1739 (:filter-section . ignore)
1740 (:filter-headline . ignore))
1741 (let (org-export-registered-backends)
1742 (org-export-define-backend 'b1
1743 nil :filters-alist '((:filter-headline . ignore)))
1744 (org-export-define-derived-backend 'b2 'b1
1745 :filters-alist '((:filter-section . ignore)))
1746 (org-export-get-all-filters
1747 (org-export-create-backend
1748 :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
1749 ;; Back-end filters overrule inherited ones.
1750 (should
1751 (eq 'b
1752 (let (org-export-registered-backends)
1753 (org-export-define-backend 'b1 '((:filter-headline . a)))
1754 (cdr (assq :filter-headline
1755 (org-export-get-all-filters
1756 (org-export-create-backend
1757 :parent 'b1 :filters '((:filter-headline . b))))))))))
1759 (ert-deftest test-org-export/with-backend ()
1760 "Test `org-export-with-backend' definition."
1761 ;; Error when calling an undefined back-end
1762 (should-error (org-export-with-backend nil "Test"))
1763 ;; Error when called back-end doesn't have an appropriate
1764 ;; transcoder.
1765 (should-error
1766 (org-export-with-backend
1767 (org-export-create-backend :transcoders '((headline . ignore)))
1768 "Test"))
1769 ;; Otherwise, export using correct transcoder
1770 (should
1771 (equal "Success"
1772 (let (org-export-registered-backends)
1773 (org-export-define-backend 'test
1774 '((plain-text . (lambda (text contents info) "Failure"))))
1775 (org-export-define-backend 'test2
1776 '((plain-text . (lambda (text contents info) "Success"))))
1777 (org-export-with-backend 'test2 "Test"))))
1778 ;; Provide correct back-end if transcoder needs to use recursive
1779 ;; calls anyway.
1780 (should
1781 (equal "Success\n"
1782 (let ((test-back-end
1783 (org-export-create-backend
1784 :transcoders
1785 (list (cons 'headline
1786 (lambda (headline contents info)
1787 (org-export-data
1788 (org-element-property :title headline)
1789 info)))
1790 (cons 'plain-text (lambda (text info) "Success"))))))
1791 (org-export-string-as
1792 "* Test"
1793 (org-export-create-backend
1794 :transcoders
1795 (list (cons 'headline
1796 (lambda (headline contents info)
1797 (org-export-with-backend
1798 test-back-end headline contents info))))))))))
1800 (ert-deftest test-org-export/data-with-backend ()
1801 "Test `org-export-data-with-backend' specifications."
1802 ;; Error when calling an undefined back-end.
1803 (should-error (org-export-data-with-backend nil "nil" nil))
1804 ;; Otherwise, export data recursively, using correct back-end.
1805 (should
1806 (equal
1807 "Success!"
1808 (org-export-data-with-backend
1809 '(bold nil "Test")
1810 (org-export-create-backend
1811 :transcoders
1812 '((plain-text . (lambda (text info) "Success"))
1813 (bold . (lambda (bold contents info) (concat contents "!")))))
1814 '(:with-emphasize t)))))
1818 ;;; Comments
1820 (ert-deftest test-org-export/comments ()
1821 "Test comments handling during export.
1822 In particular, structure of the document mustn't be altered after
1823 comments removal."
1824 (should
1825 (equal "Para1\n\nPara2\n"
1826 (org-test-with-temp-text "
1827 Para1
1828 # Comment
1830 # Comment
1831 Para2"
1832 (org-export-as (org-test-default-backend)))))
1833 (should
1834 (equal "Para1\n\nPara2\n"
1835 (org-test-with-temp-text "
1836 Para1
1837 # Comment
1838 Para2"
1839 (org-export-as (org-test-default-backend)))))
1840 (should
1841 (equal "[fn:1] Para1\n\n\nPara2\n"
1842 (org-test-with-temp-text "
1843 \[fn:1] Para1
1844 # Inside definition
1847 # Outside definition
1848 Para2"
1849 (org-export-as (org-test-default-backend)))))
1850 (should
1851 (equal "[fn:1] Para1\n\nPara2\n"
1852 (org-test-with-temp-text "
1853 \[fn:1] Para1
1855 # Inside definition
1857 # Inside definition
1859 Para2"
1860 (org-export-as (org-test-default-backend)))))
1861 (should
1862 (equal "[fn:1] Para1\n\nPara2\n"
1863 (org-test-with-temp-text "
1864 \[fn:1] Para1
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
1874 # Inside definition
1875 Para2"
1876 (org-export-as (org-test-default-backend)))))
1877 (should
1878 (equal "- item 1\n\n- item 2\n"
1879 (org-test-with-temp-text "
1880 - item 1
1882 # Comment
1884 - item 2"
1885 (org-export-as (org-test-default-backend))))))
1889 ;;; Export blocks
1891 (ert-deftest test-org-export/export-block ()
1892 "Test export blocks transcoding."
1893 (should
1894 (equal "Success!\n"
1895 (org-test-with-temp-text
1896 "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
1897 (org-export-as
1898 (org-export-create-backend
1899 :transcoders '((export-block . (lambda (b _c _i)
1900 (org-element-property :value b)))
1901 (section . (lambda (_s c _i) c))))))))
1902 (should
1903 (equal "Success!\n"
1904 (org-test-with-temp-text
1905 "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
1906 (org-export-as
1907 (org-export-create-backend
1908 :transcoders
1909 (list
1910 (cons 'export-block
1911 (lambda (b _c _i)
1912 (and (equal (org-element-property :type b) "BACKEND")
1913 (org-element-property :value b))))
1914 (cons 'section (lambda (_s c _i) c)))))))))
1917 ;;; Export Snippets
1919 (ert-deftest test-org-export/export-snippet ()
1920 "Test export snippets transcoding."
1921 ;; Standard test.
1922 (org-test-with-temp-text "@@test:A@@@@t:B@@"
1923 (let ((backend (org-test-default-backend)))
1924 (setf (org-export-backend-name backend) 'test)
1925 (setf (org-export-backend-transcoders backend)
1926 (cons (cons 'export-snippet
1927 (lambda (snippet contents info)
1928 (when (eq (org-export-snippet-backend snippet) 'test)
1929 (org-element-property :value snippet))))
1930 (org-export-backend-transcoders backend)))
1931 (let ((org-export-snippet-translation-alist nil))
1932 (should (equal (org-export-as backend) "A\n")))
1933 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
1934 (should (equal (org-export-as backend) "AB\n")))))
1935 ;; Ignored export snippets do not remove any blank.
1936 (should
1937 (equal "begin end\n"
1938 (org-test-with-parsed-data "begin @@test:A@@ end"
1939 (org-export-data-with-backend
1940 tree
1941 (org-export-create-backend
1942 :transcoders
1943 '((paragraph . (lambda (paragraph contents info) contents))
1944 (section . (lambda (section contents info) contents))))
1945 info)))))
1948 ;;; Filters
1950 (ert-deftest test-org-export/filter-apply-functions ()
1951 "Test `org-export-filter-apply-functions' specifications."
1952 ;; Functions are applied in order and return values are reduced.
1953 (should
1954 (equal "210"
1955 (org-export-filter-apply-functions
1956 (list (lambda (value &rest _) (concat "1" value))
1957 (lambda (value &rest _) (concat "2" value)))
1958 "0" nil)))
1959 ;; Functions returning nil are skipped.
1960 (should
1961 (equal "20"
1962 (org-export-filter-apply-functions
1963 (list #'ignore (lambda (value &rest _) (concat "2" value)))
1964 "0" nil)))
1965 ;; If all functions are skipped, return the initial value.
1966 (should
1967 (equal "0"
1968 (org-export-filter-apply-functions (list #'ignore) "0" nil)))
1969 ;; If any function returns the empty string, final value is the
1970 ;; empty string.
1971 (should
1972 (equal ""
1973 (org-export-filter-apply-functions
1974 (list (lambda (value &rest _) "")
1975 (lambda (value &rest _) (concat "2" value)))
1976 "0" nil)))
1977 ;; Any function returning the empty string short-circuits the
1978 ;; process.
1979 (should
1980 (org-export-filter-apply-functions
1981 (list (lambda (value &rest _) "")
1982 (lambda (value &rest _) (error "This shouldn't happen")))
1983 "0" nil)))
1986 ;;; Footnotes
1988 (ert-deftest test-org-export/footnote-first-reference-p ()
1989 "Test `org-export-footnote-first-reference-p' specifications."
1990 (should
1991 (equal
1992 '(t nil)
1993 (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
1994 (let (result)
1995 (org-export-as
1996 (org-export-create-backend
1997 :transcoders
1998 `(,(cons 'footnote-reference
1999 (lambda (f c i)
2000 (push (org-export-footnote-first-reference-p f i)
2001 result)
2002 ""))
2003 (section . (lambda (s c i) c))
2004 (paragraph . (lambda (p c i) c))))
2005 nil nil nil '(:with-footnotes t))
2006 (nreverse result)))))
2007 ;; Limit check to DATA, when non-nil.
2008 (should
2009 (equal
2010 '(nil t)
2011 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
2012 (let (result)
2013 (org-element-map tree 'footnote-reference
2014 (lambda (ref)
2015 (push
2016 (org-export-footnote-first-reference-p
2017 ref info (org-element-map tree 'headline #'identity info t))
2018 result))
2019 info)
2020 (nreverse result)))))
2021 (should
2022 (equal
2023 '(t nil)
2024 (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
2025 (let (result)
2026 (org-element-map tree 'footnote-reference
2027 (lambda (ref)
2028 (push (org-export-footnote-first-reference-p ref info) result))
2029 info)
2030 (nreverse result)))))
2031 ;; If optional argument BODY-FIRST is non-nil, first find footnote
2032 ;; in the main body of the document. Otherwise, enter footnote
2033 ;; definitions when they are encountered.
2034 (should
2035 (equal
2036 '(t nil)
2037 (org-test-with-temp-text
2038 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
2039 (let (result)
2040 (org-export-as
2041 (org-export-create-backend
2042 :transcoders
2043 `(,(cons 'footnote-reference
2044 (lambda (f c i)
2045 (when (org-element-lineage f '(drawer))
2046 (push (org-export-footnote-first-reference-p f i nil)
2047 result))
2048 ""))
2049 (drawer . (lambda (d c i) c))
2050 (footnote-definition . (lambda (d c i) c))
2051 (section . (lambda (s c i) c))
2052 (paragraph . (lambda (p c i) c))))
2053 nil nil nil '(:with-footnotes t))
2054 (nreverse result)))))
2055 (should
2056 (equal
2057 '(t t)
2058 (org-test-with-temp-text
2059 ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
2060 (let (result)
2061 (org-export-as
2062 (org-export-create-backend
2063 :transcoders
2064 `(,(cons 'footnote-reference
2065 (lambda (f c i)
2066 (when (org-element-lineage f '(drawer))
2067 (push (org-export-footnote-first-reference-p f i nil t)
2068 result))
2069 ""))
2070 (drawer . (lambda (d c i) c))
2071 (footnote-definition . (lambda (d c i) c))
2072 (section . (lambda (s c i) c))
2073 (paragraph . (lambda (p c i) c))))
2074 nil nil nil '(:with-footnotes t))
2075 (nreverse result))))))
2077 (ert-deftest test-org-export/get-footnote-number ()
2078 "Test `org-export-get-footnote-number' specifications."
2079 (should
2080 (equal '(1 2 1)
2081 (org-test-with-parsed-data
2082 "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
2083 (org-element-map tree 'footnote-reference
2084 (lambda (ref) (org-export-get-footnote-number ref info))
2085 info))))
2086 ;; Anonymous footnotes all get a new number.
2087 (should
2088 (equal '(1 2)
2089 (org-test-with-parsed-data
2090 "Text[fn::anon1][fn::anon2]"
2091 (org-element-map tree 'footnote-reference
2092 (lambda (ref) (org-export-get-footnote-number ref info))
2093 info))))
2094 ;; Test nested footnotes order.
2095 (should
2096 (equal
2097 '((1 . "1") (2 . "2") (3 . "3") (3 . "3") (4))
2098 (org-test-with-parsed-data
2099 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
2100 (org-element-map tree 'footnote-reference
2101 (lambda (ref)
2102 (cons (org-export-get-footnote-number ref info)
2103 (org-element-property :label ref)))
2104 info))))
2105 ;; Limit number to provided DATA, when non-nil.
2106 (should
2107 (equal
2108 '(1)
2109 (org-test-with-parsed-data
2110 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2111 (org-element-map tree 'footnote-reference
2112 (lambda (ref)
2113 (org-export-get-footnote-number
2114 ref info (org-element-map tree 'headline #'identity info t)))
2115 info))))
2116 (should
2117 (equal
2118 '(1 2)
2119 (org-test-with-parsed-data
2120 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
2121 (org-element-map tree 'footnote-reference
2122 (lambda (ref) (org-export-get-footnote-number ref info))
2123 info))))
2124 ;; With a non-nil BODY-FIRST optional argument, first check body,
2125 ;; then footnote definitions.
2126 (should
2127 (equal
2128 '(("1" . 1) ("2" . 2) ("3" . 3) ("3" . 3))
2129 (org-test-with-parsed-data
2130 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
2131 (org-element-map tree 'footnote-reference
2132 (lambda (ref)
2133 (cons (org-element-property :label ref)
2134 (org-export-get-footnote-number ref info nil t)))
2135 info))))
2136 (should
2137 (equal
2138 '(("1" . 1) ("2" . 3) ("3" . 2) ("3" . 2))
2139 (org-test-with-parsed-data
2140 "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
2141 (org-element-map tree 'footnote-reference
2142 (lambda (ref)
2143 (cons (org-element-property :label ref)
2144 (org-export-get-footnote-number ref info nil)))
2145 info)))))
2147 (ert-deftest test-org-export/get-footnote-definition ()
2148 "Test `org-export-get-footnote-definition' specifications."
2149 ;; Standard test.
2150 (should
2151 (equal "A\n"
2152 (org-element-interpret-data
2153 (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1] A"
2154 (org-export-get-footnote-definition
2155 (org-element-map tree 'footnote-reference #'identity nil t)
2156 info)))))
2157 ;; Raise an error if no definition is found.
2158 (should-error
2159 (org-test-with-parsed-data "Text[fn:1]"
2160 (org-export-get-footnote-definition
2161 (org-element-map tree 'footnote-reference #'identity nil t)
2162 info)))
2163 ;; Find inline definitions.
2164 (should
2165 (equal "A"
2166 (org-element-interpret-data
2167 (org-test-with-parsed-data "Text[fn:1:A]"
2168 (org-export-get-footnote-definition
2169 (org-element-map tree 'footnote-reference #'identity nil t)
2170 info)))))
2171 ;; Find anonymous definitions.
2172 (should
2173 (equal "A"
2174 (org-element-interpret-data
2175 (org-test-with-parsed-data "Text[fn::A]"
2176 (org-export-get-footnote-definition
2177 (org-element-map tree 'footnote-reference #'identity nil t)
2178 info)))))
2179 ;; Find empty definitions.
2180 (should
2181 (equal ""
2182 (org-element-interpret-data
2183 (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1]"
2184 (org-export-get-footnote-definition
2185 (org-element-map tree 'footnote-reference #'identity nil t)
2186 info)))))
2187 (should
2188 (equal ""
2189 (org-element-interpret-data
2190 (org-test-with-parsed-data "Text[fn:1:]"
2191 (org-export-get-footnote-definition
2192 (org-element-map tree 'footnote-reference #'identity nil t)
2193 info)))))
2194 (should
2195 (equal ""
2196 (org-element-interpret-data
2197 (org-test-with-parsed-data "Text[fn::]"
2198 (org-export-get-footnote-definition
2199 (org-element-map tree 'footnote-reference #'identity nil t)
2200 info))))))
2202 (ert-deftest test-org-export/collect-footnote-definitions ()
2203 "Test `org-export-collect-footnote-definitions' specifications."
2204 (should
2205 (= 4
2206 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2208 \[fn:2] B [fn:3] [fn::D].
2210 \[fn:3] C."
2211 (length (org-export-collect-footnote-definitions info)))))
2212 ;; Limit number to provided DATA, when non-nil.
2213 (should
2214 (equal
2215 '((1 "2"))
2216 (org-test-with-parsed-data
2217 "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
2218 (mapcar #'butlast
2219 (org-export-collect-footnote-definitions
2220 info (org-element-map tree 'headline #'identity info t))))))
2221 (should
2222 (equal
2223 '((1 "1") (2 "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 (org-export-collect-footnote-definitions info)))))
2227 ;; With optional argument BODY-FIRST, first check body, then
2228 ;; footnote definitions.
2229 (should
2230 (equal '("1" "3" "2" nil)
2231 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2233 \[fn:2] B [fn:3] [fn::D].
2235 \[fn:3] C."
2236 (mapcar (lambda (e) (nth 1 e))
2237 (org-export-collect-footnote-definitions info nil t)))))
2238 (should-not
2239 (equal '("1" "3" "2" nil)
2240 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
2242 \[fn:2] B [fn:3] [fn::D].
2244 \[fn:3] C."
2245 (mapcar (lambda (e) (nth 1 e))
2246 (org-export-collect-footnote-definitions info))))))
2248 (ert-deftest test-org-export/footnotes ()
2249 "Miscellaneous tests on footnotes."
2250 (let ((org-footnote-section nil)
2251 (org-export-with-footnotes t))
2252 ;; Read every type of footnote.
2253 (should
2254 (equal
2255 '((1 . "A\n") (2 . "C") (3 . "D"))
2256 (org-test-with-parsed-data
2257 "Text[fn:1] [fn:label:C] [fn::D]\n\n[fn:1] A\n"
2258 (org-element-map tree 'footnote-reference
2259 (lambda (ref)
2260 (let ((def (org-export-get-footnote-definition ref info)))
2261 (cons (org-export-get-footnote-number ref info)
2262 (if (eq (org-element-property :type ref) 'inline) (car def)
2263 (car (org-element-contents
2264 (car (org-element-contents def))))))))
2265 info))))
2266 ;; Export nested footnote in invisible definitions.
2267 (should
2268 (= 2
2269 (org-test-with-temp-text "Text[fn:1]\n\n[fn:1] B [fn:2]\n\n[fn:2] C."
2270 (narrow-to-region (point) (line-end-position))
2271 (catch 'exit
2272 (org-export-as
2273 (org-export-create-backend
2274 :transcoders
2275 '((section
2277 (lambda (s c i)
2278 (throw 'exit (length
2279 (org-export-collect-footnote-definitions
2280 i))))))))))))
2281 ;; Export footnotes defined outside parsing scope.
2282 (should
2283 (string-match
2284 "Out of scope"
2285 (org-test-with-temp-text "[fn:1] Out of scope
2286 * Title
2287 <point>Paragraph[fn:1]"
2288 (org-export-as (org-test-default-backend) 'subtree))))
2289 (should
2290 (string-match
2291 "Out of scope"
2292 (org-test-with-temp-text "[fn:1] Out of scope
2293 * Title
2294 <point>Paragraph[fn:1]"
2295 (narrow-to-region (point) (point-max))
2296 (org-export-as (org-test-default-backend)))))
2297 ;; Export nested footnotes defined outside parsing scope.
2298 (should
2299 (string-match
2300 "Very out of scope"
2301 (org-test-with-temp-text "
2302 \[fn:1] Out of scope[fn:2]
2304 \[fn:2] Very out of scope
2305 * Title
2306 <point>Paragraph[fn:1]"
2307 (org-export-as (org-test-default-backend) 'subtree))))
2308 (should
2309 (string-match
2310 "Very out of scope"
2311 (org-test-with-temp-text "
2312 \[fn:1] Out of scope[fn:2]
2314 \[fn:2] Very out of scope
2315 * Title
2316 <point>Paragraph[fn:1]"
2317 (narrow-to-region (point) (point-max))
2318 (org-export-as (org-test-default-backend)))))
2319 (should
2320 (string-match
2321 "D2"
2322 (org-test-with-temp-text "
2323 \[fn:1] Out of scope[fn:2:D2]
2324 * Title
2325 <point>Paragraph[fn:1]"
2326 (narrow-to-region (point) (point-max))
2327 (org-export-as (org-test-default-backend)))))
2328 ;; Export footnotes in pruned parts of tree.
2329 (should
2330 (string-match
2331 "Definition"
2332 (let ((org-export-exclude-tags '("noexport")))
2333 (org-test-with-temp-text
2334 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] Definition"
2335 (org-export-as (org-test-default-backend))))))
2336 (should
2337 (string-match
2338 "Definition"
2339 (let ((org-export-select-tags '("export")))
2340 (org-test-with-temp-text
2341 "* H :export:\nText[fn:1]\n* H2\n[fn:1] Definition"
2342 (org-export-as (org-test-default-backend))))))
2343 ;; Export nested footnotes in pruned parts of tree.
2344 (should
2345 (string-match
2346 "D2"
2347 (let ((org-export-exclude-tags '("noexport")))
2348 (org-test-with-temp-text
2349 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
2350 (org-export-as (org-test-default-backend))))))
2351 (should
2352 (string-match
2353 "D2"
2354 (let ((org-export-select-tags '("export")))
2355 (org-test-with-temp-text
2356 "* H :export:\nText[fn:1]\n* H2\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
2357 (org-export-as (org-test-default-backend))))))
2358 ;; Handle uninterpreted data in pruned footnote definitions.
2359 (should-not
2360 (string-match
2362 (let ((org-export-with-tables nil))
2363 (org-test-with-temp-text
2364 "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1]\n| a |"
2365 (org-export-as (org-test-default-backend))))))
2366 ;; Footnotes without a definition should throw an error.
2367 (should-error
2368 (org-test-with-parsed-data "Text[fn:1]"
2369 (org-export-get-footnote-definition
2370 (org-element-map tree 'footnote-reference #'identity info t) info)))
2371 ;; Footnote section should be ignored in TOC and in headlines
2372 ;; numbering.
2373 (should
2374 (= 1 (let ((org-footnote-section "Footnotes"))
2375 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
2376 (org-export-collect-headlines info))))))
2377 (should
2378 (equal '(2)
2379 (let ((org-footnote-section "Footnotes"))
2380 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
2381 (org-element-map tree 'headline
2382 (lambda (hl)
2383 (when (equal (org-element-property :raw-value hl) "H2")
2384 (org-export-get-headline-number hl info)))
2385 info t)))))))
2389 ;;; Headlines and Inlinetasks
2391 (ert-deftest test-org-export/get-relative-level ()
2392 "Test `org-export-get-relative-level' specifications."
2393 ;; Standard test.
2394 (should
2395 (equal '(1 2)
2396 (let ((org-odd-levels-only nil))
2397 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2398 (org-element-map tree 'headline
2399 (lambda (h) (org-export-get-relative-level h info))
2400 info)))))
2401 ;; Missing levels
2402 (should
2403 (equal '(1 3)
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))))))
2410 (ert-deftest test-org-export/low-level-p ()
2411 "Test `org-export-low-level-p' specifications."
2412 (should
2413 (equal
2414 '(no yes)
2415 (let ((org-odd-levels-only nil))
2416 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2417 (org-element-map tree 'headline
2418 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
2419 (plist-put info :headline-levels 1)))))))
2421 (ert-deftest test-org-export/get-headline-number ()
2422 "Test `org-export-get-headline-number' specifications."
2423 ;; Standard test.
2424 (should
2425 (equal
2426 '((1) (1 1))
2427 (let ((org-odd-levels-only nil))
2428 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2429 (org-element-map tree 'headline
2430 (lambda (h) (org-export-get-headline-number h info))
2431 info)))))
2432 ;; Missing levels are replaced with 0.
2433 (should
2434 (equal
2435 '((1) (1 0 1))
2436 (let ((org-odd-levels-only nil))
2437 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
2438 (org-element-map tree 'headline
2439 (lambda (h) (org-export-get-headline-number h info))
2440 info))))))
2442 (ert-deftest test-org-export/numbered-headline-p ()
2443 "Test `org-export-numbered-headline-p' specifications."
2444 ;; If `:section-numbers' is nil, never number headlines.
2445 (should-not
2446 (org-test-with-parsed-data "* Headline"
2447 (org-element-map tree 'headline
2448 (lambda (h) (org-export-numbered-headline-p h info))
2449 (plist-put info :section-numbers nil))))
2450 ;; If `:section-numbers' is a number, only number headlines with
2451 ;; a level greater that it.
2452 (should
2453 (equal
2454 '(yes no)
2455 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
2456 (org-element-map tree 'headline
2457 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
2458 (plist-put info :section-numbers 1)))))
2459 ;; Otherwise, headlines are always numbered.
2460 (should
2461 (org-test-with-parsed-data "* Headline"
2462 (org-element-map tree 'headline
2463 (lambda (h) (org-export-numbered-headline-p h info))
2464 (plist-put info :section-numbers t))))
2465 ;; With #+OPTIONS: num:nil all headlines are unnumbered.
2466 (should-not
2467 (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
2468 (org-export-numbered-headline-p
2469 (org-element-map tree 'headline 'identity info t)
2470 info)))
2471 ;; Headlines with a non-nil UNNUMBERED property are not numbered.
2472 (should-not
2473 (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
2474 (org-export-numbered-headline-p
2475 (org-element-map tree 'headline #'identity info t)
2476 info)))
2477 ;; UNNUMBERED ignores inheritance. Any non-nil value among
2478 ;; ancestors disables numbering.
2479 (should
2480 (org-test-with-parsed-data
2481 "* H
2482 :PROPERTIES:
2483 :UNNUMBERED: t
2484 :END:
2485 ** H2
2486 :PROPERTIES:
2487 :UNNUMBERED: nil
2488 :END:
2489 *** H3"
2490 (cl-every
2491 (lambda (h) (not (org-export-numbered-headline-p h info)))
2492 (org-element-map tree 'headline #'identity info)))))
2494 (ert-deftest test-org-export/number-to-roman ()
2495 "Test `org-export-number-to-roman' specifications."
2496 ;; If number is negative, return it as a string.
2497 (should (equal (org-export-number-to-roman -1) "-1"))
2498 ;; Otherwise, return it as a roman number.
2499 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
2501 (ert-deftest test-org-export/get-optional-title ()
2502 "Test `org-export-get-alt-title' specifications."
2503 ;; If ALT_TITLE property is defined, use it.
2504 (should
2505 (equal '("opt")
2506 (org-test-with-parsed-data
2507 "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
2508 (org-export-get-alt-title
2509 (org-element-map tree 'headline 'identity info t)
2510 info))))
2511 ;; Otherwise, fall-back to regular title.
2512 (should
2513 (equal '("Headline")
2514 (org-test-with-parsed-data "* Headline"
2515 (org-export-get-alt-title
2516 (org-element-map tree 'headline 'identity info t)
2517 info)))))
2519 (ert-deftest test-org-export/get-tags ()
2520 "Test `org-export-get-tags' specifications."
2521 ;; Standard test: tags which are not a select tag, an exclude tag,
2522 ;; or specified as optional argument shouldn't be ignored.
2523 (should
2524 (org-test-with-parsed-data "* Headline :tag:"
2525 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2526 info)))
2527 ;; Tags provided in the optional argument are ignored.
2528 (should-not
2529 (org-test-with-parsed-data "* Headline :ignore:"
2530 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
2531 info '("ignore"))))
2532 ;; Allow tag inheritance.
2533 (should
2534 (equal
2535 '(("tag") ("tag"))
2536 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
2537 (org-element-map tree 'headline
2538 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
2539 ;; Tag inheritance checks FILETAGS keywords.
2540 (should
2541 (equal
2542 '(("a" "b" "tag"))
2543 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
2544 (org-element-map tree 'headline
2545 (lambda (hl) (org-export-get-tags hl info nil t)) info)))))
2547 (ert-deftest test-org-export/get-node-property ()
2548 "Test`org-export-get-node-property' specifications."
2549 ;; Standard test.
2550 (should
2551 (equal "value"
2552 (org-test-with-parsed-data "* Headline
2553 :PROPERTIES:
2554 :prop: value
2555 :END:"
2556 (org-export-get-node-property
2557 :PROP (org-element-map tree 'headline 'identity nil t)))))
2558 ;; Test inheritance.
2559 (should
2560 (equal "value"
2561 (org-test-with-parsed-data "* Parent
2562 :PROPERTIES:
2563 :prop: value
2564 :END:
2565 ** Headline
2566 Paragraph"
2567 (org-export-get-node-property
2568 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
2569 ;; Cannot return a value before the first headline.
2570 (should-not
2571 (org-test-with-parsed-data "Paragraph
2572 * Headline
2573 :PROPERTIES:
2574 :prop: value
2575 :END:"
2576 (org-export-get-node-property
2577 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
2579 (ert-deftest test-org-export/get-category ()
2580 "Test `org-export-get-category' specifications."
2581 ;; Standard test.
2582 (should
2583 (equal "value"
2584 (org-test-with-parsed-data "* Headline
2585 :PROPERTIES:
2586 :CATEGORY: value
2587 :END:"
2588 (org-export-get-category
2589 (org-element-map tree 'headline 'identity nil t) info))))
2590 ;; Test inheritance from a parent headline.
2591 (should
2592 (equal '("value" "value")
2593 (org-test-with-parsed-data "* Headline1
2594 :PROPERTIES:
2595 :CATEGORY: value
2596 :END:
2597 ** Headline2"
2598 (org-element-map tree 'headline
2599 (lambda (hl) (org-export-get-category hl info)) info))))
2600 ;; Test inheritance from #+CATEGORY keyword
2601 (should
2602 (equal "value"
2603 (org-test-with-parsed-data "#+CATEGORY: value
2604 * Headline"
2605 (org-export-get-category
2606 (org-element-map tree 'headline 'identity nil t) info))))
2607 ;; Test inheritance from file name.
2608 (should
2609 (equal "test"
2610 (org-test-with-parsed-data "* Headline"
2611 (let ((info (plist-put info :input-file "~/test.org")))
2612 (org-export-get-category
2613 (org-element-map tree 'headline 'identity nil t) info)))))
2614 ;; Fall-back value.
2615 (should
2616 (equal "???"
2617 (org-test-with-parsed-data "* Headline"
2618 (org-export-get-category
2619 (org-element-map tree 'headline 'identity nil t) info)))))
2621 (ert-deftest test-org-export/first-sibling-p ()
2622 "Test `org-export-first-sibling-p' specifications."
2623 ;; Standard test.
2624 (should
2625 (equal
2626 '(yes yes no)
2627 (org-test-with-parsed-data "* H\n** H 2\n** H 3"
2628 (org-element-map tree 'headline
2629 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2630 info))))
2631 (should
2632 (equal '(yes no)
2633 (org-test-with-parsed-data "- item\n\n para"
2634 (org-element-map tree 'paragraph
2635 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2636 info))))
2637 ;; Ignore sections for headlines.
2638 (should
2639 (equal '(yes yes)
2640 (org-test-with-parsed-data "* H\nSection\n** H 2"
2641 (org-element-map tree 'headline
2642 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2643 info))))
2644 ;; Ignore headlines not exported.
2645 (should
2646 (equal
2647 '(yes)
2648 (let ((org-export-exclude-tags '("ignore")))
2649 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
2650 (org-element-map tree 'headline
2651 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
2652 info))))))
2654 (ert-deftest test-org-export/last-sibling-p ()
2655 "Test `org-export-last-sibling-p' specifications."
2656 ;; Standard test.
2657 (should
2658 (equal
2659 '(yes no yes)
2660 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
2661 (org-element-map tree 'headline
2662 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2663 info))))
2664 (should
2665 (equal '(no yes)
2666 (org-test-with-parsed-data "- item\n\n para"
2667 (org-element-map tree 'paragraph
2668 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2669 info))))
2670 ;; Ignore headlines not exported.
2671 (should
2672 (equal
2673 '(yes)
2674 (let ((org-export-exclude-tags '("ignore")))
2675 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
2676 (org-element-map tree 'headline
2677 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
2678 info))))))
2680 (ert-deftest test-org-export/handle-inlinetasks ()
2681 "Test inlinetask export."
2682 ;; Inlinetask with an exclude tag.
2683 (when (featurep 'org-inlinetask)
2684 (should
2685 (equal
2687 (let ((org-inlinetask-min-level 3)
2688 org-export-filter-body-functions
2689 org-export-filter-final-output-functions)
2690 (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
2691 (org-export-as (org-test-default-backend)
2692 nil nil nil '(:exclude-tags ("noexp")))))))
2693 ;; Inlinetask with an include tag.
2694 (should
2695 (equal
2696 "* H2\n*** Inline :exp:\n"
2697 (let ((org-inlinetask-min-level 3)
2698 (org-tags-column 0))
2699 (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
2700 (org-export-as (org-test-default-backend)
2701 nil nil nil '(:select-tags ("exp")))))))
2702 ;; Ignore inlinetask with a TODO keyword and tasks excluded.
2703 (should
2704 (equal ""
2705 (let ((org-todo-keywords '((sequence "TODO" "DONE")))
2706 (org-inlinetask-min-level 3)
2707 org-export-filter-body-functions
2708 org-export-filter-final-output-functions)
2709 (org-test-with-temp-text "*** TODO Inline"
2710 (org-export-as (org-test-default-backend)
2711 nil nil nil '(:with-tasks nil))))))))
2715 ;;; Keywords
2717 (ert-deftest test-org-export/get-date ()
2718 "Test `org-export-get-date' specifications."
2719 ;; Return a properly formatted string when
2720 ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
2721 ;; consists in a single timestamp.
2722 (should
2723 (equal "29 03 2012"
2724 (let ((org-export-date-timestamp-format "%d %m %Y"))
2725 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2726 (org-export-get-date info)))))
2727 ;; Return a secondary string otherwise.
2728 (should-not
2729 (stringp
2730 (let ((org-export-date-timestamp-format nil))
2731 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2732 (org-export-get-date info)))))
2733 (should
2734 (equal '("Date")
2735 (org-test-with-parsed-data "#+DATE: Date"
2736 (org-export-get-date info))))
2737 ;; Optional argument has precedence over
2738 ;; `org-export-date-timestamp-format'.
2739 (should
2740 (equal "29 03"
2741 (let ((org-export-date-timestamp-format "%d %m %Y"))
2742 (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
2743 (org-export-get-date info "%d %m"))))))
2747 ;;; Links
2749 (ert-deftest test-org-export/custom-protocol-maybe ()
2750 "Test `org-export-custom-protocol-maybe' specifications."
2751 (should
2752 (string-match
2753 "success"
2754 (progn
2755 (org-link-set-parameters "foo" :export (lambda (p d f) "success"))
2756 (org-export-string-as
2757 "[[foo:path]]"
2758 (org-export-create-backend
2759 :name 'test
2760 :transcoders
2761 '((section . (lambda (s c i) c))
2762 (paragraph . (lambda (p c i) c))
2763 (link . (lambda (l c i)
2764 (or (org-export-custom-protocol-maybe l c 'test)
2765 "failure")))))))))
2766 (should-not
2767 (string-match
2768 "success"
2769 (progn
2770 (org-link-set-parameters
2771 "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
2772 (org-export-string-as
2773 "[[foo:path]]"
2774 (org-export-create-backend
2775 :name 'no-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 'no-test)
2781 "failure")))))))))
2782 ;; Ignore anonymous back-ends.
2783 (should-not
2784 (string-match
2785 "success"
2786 (progn
2787 (org-link-set-parameters
2788 "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
2789 (org-export-string-as
2790 "[[foo:path]]"
2791 (org-export-create-backend
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 nil)
2797 "failure"))))))))))
2799 (ert-deftest test-org-export/get-coderef-format ()
2800 "Test `org-export-get-coderef-format' specifications."
2801 ;; A link without description returns "%s"
2802 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
2803 "%s"))
2804 ;; Return "%s" when path is matched within description.
2805 (should (equal (org-export-get-coderef-format "path" "desc (path)")
2806 "desc %s"))
2807 ;; Otherwise return description.
2808 (should (equal (org-export-get-coderef-format "path" "desc")
2809 "desc")))
2811 (ert-deftest test-org-export/inline-image-p ()
2812 "Test `org-export-inline-image-p' specifications."
2813 (should
2814 (org-export-inline-image-p
2815 (org-test-with-temp-text "[[#id]]"
2816 (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
2817 '(("custom-id" . "id")))))
2819 (ert-deftest test-org-export/insert-image-links ()
2820 "Test `org-export-insert-image-links' specifications."
2821 (should-not
2822 (member "file"
2823 (org-test-with-parsed-data "[[http://orgmode.org][file:image.png]]"
2824 (org-element-map tree 'link
2825 (lambda (l) (org-element-property :type l))))))
2826 (should
2827 (member "file"
2828 (org-test-with-parsed-data "[[http://orgmode.org][file:image.png]]"
2829 (org-element-map (org-export-insert-image-links tree info) 'link
2830 (lambda (l) (org-element-property :type l))))))
2831 ;; Properly set `:parent' property when replace contents with image
2832 ;; link.
2833 (should
2834 (memq 'link
2835 (org-test-with-parsed-data "[[http://orgmode.org][file:image.png]]"
2836 (org-element-map (org-export-insert-image-links tree info) 'link
2837 (lambda (l)
2838 (org-element-type (org-element-property :parent l)))))))
2839 ;; With optional argument RULES, recognize different links as
2840 ;; images.
2841 (should-not
2842 (member "file"
2843 (org-test-with-parsed-data "[[http://orgmode.org][file:image.xxx]]"
2844 (org-element-map (org-export-insert-image-links tree info) 'link
2845 (lambda (l) (org-element-property :type l))))))
2846 (should
2847 (member "file"
2848 (org-test-with-parsed-data "[[http://orgmode.org][file:image.xxx]]"
2849 (org-element-map
2850 (org-export-insert-image-links tree info '(("file" . "xxx")))
2851 'link
2852 (lambda (l) (org-element-property :type l)))))))
2854 (ert-deftest test-org-export/fuzzy-link ()
2855 "Test fuzzy links specifications."
2856 ;; Link to an headline should return headline's number.
2857 (should
2858 ;; Note: Headline's number is in fact a list of numbers.
2859 (equal '(2)
2860 (org-test-with-parsed-data
2861 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
2862 (org-element-map tree 'link
2863 (lambda (link)
2864 (org-export-get-ordinal
2865 (org-export-resolve-fuzzy-link link info) info)) info t))))
2866 ;; Link to a target in an item should return item's number.
2867 (should
2868 ;; Note: Item's number is in fact a list of numbers.
2869 (equal '(1 2)
2870 (org-test-with-parsed-data
2871 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
2872 (org-element-map tree 'link
2873 (lambda (link)
2874 (org-export-get-ordinal
2875 (org-export-resolve-fuzzy-link link info) info)) info t))))
2876 ;; Link to a target in a footnote should return footnote's number.
2877 (should
2878 (equal '(2 3)
2879 (org-test-with-parsed-data "
2880 Paragraph[fn:1][fn:2][fn:lbl3:C<<target>>][[test]][[target]]
2881 \[fn:1] A
2883 \[fn:2] <<test>>B"
2884 (org-element-map tree 'link
2885 (lambda (link)
2886 (org-export-get-ordinal
2887 (org-export-resolve-fuzzy-link link info) info)) info))))
2888 ;; Link to a named element should return sequence number of that
2889 ;; element.
2890 (should
2891 (= 2
2892 (org-test-with-parsed-data
2893 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
2894 (org-element-map tree 'link
2895 (lambda (link)
2896 (org-export-get-ordinal
2897 (org-export-resolve-fuzzy-link link info) info)) info t))))
2898 ;; Link to a target not within an item, a table, a footnote
2899 ;; reference or definition should return section number.
2900 (should
2901 (equal '(2)
2902 (org-test-with-parsed-data
2903 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
2904 (org-element-map tree 'link
2905 (lambda (link)
2906 (org-export-get-ordinal
2907 (org-export-resolve-fuzzy-link link info) info)) info t))))
2908 ;; Space are not significant when matching a fuzzy link.
2909 (should
2910 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
2911 (org-element-map tree 'link
2912 (lambda (link) (org-export-resolve-fuzzy-link link info))
2913 info t)))
2914 ;; Statistics cookies are ignored for headline match.
2915 (should
2916 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
2917 (org-element-map tree 'link
2918 (lambda (link) (org-export-resolve-fuzzy-link link info))
2919 info t)))
2920 (should
2921 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
2922 (org-element-map tree 'link
2923 (lambda (link) (org-export-resolve-fuzzy-link link info))
2924 info t))))
2926 (defun test-org-gen-loc-list(text type)
2927 (org-test-with-parsed-data text
2928 (org-element-map tree type
2929 (lambda (el) (or (org-export-get-loc el info) 'no-loc)))))
2931 (ert-deftest test-org-export/get-loc ()
2932 "Test `org-export-get-loc' specifications."
2933 (should
2934 ;; "-n" resets line number.
2935 (equal '(0)
2936 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n\n Text\n#+END_EXAMPLE"
2937 'example-block)))
2938 ;; The first "+n" has 0 lines before it
2939 (should
2940 (equal '(0)
2941 (test-org-gen-loc-list "#+BEGIN_EXAMPLE +n\n Text\n#+END_EXAMPLE"
2942 'example-block)))
2943 ;; "-n 10" resets line number but has "9 lines" before it.
2944 (should
2945 (equal '(9)
2946 (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n 10\n Text\n#+END_EXAMPLE"
2947 'example-block)))
2948 ;; -n10 with two lines then +n 15
2949 (should
2950 (equal '(9 25)
2951 (test-org-gen-loc-list "
2952 #+BEGIN_EXAMPLE -n 10
2953 Text_10
2954 Second line(11)
2955 #+END_EXAMPLE
2956 #+BEGIN_EXAMPLE +n 15
2957 Text line (11 + 15)
2958 #+END_EXAMPLE"
2959 'example-block)))
2960 (should
2961 (equal '(9 19 0)
2962 (test-org-gen-loc-list "
2963 #+BEGIN_EXAMPLE -n 10
2964 Text
2965 #+END_EXAMPLE
2966 #+BEGIN_EXAMPLE +n 10
2967 Text
2968 #+END_EXAMPLE
2970 #+BEGIN_EXAMPLE -n
2971 Text
2972 #+END_EXAMPLE"
2973 'example-block)))
2974 ;; an Example Block without -n does not add to the line count.
2975 (should
2976 (equal '(9 no-loc 19)
2977 (test-org-gen-loc-list "
2978 #+BEGIN_EXAMPLE -n 10
2979 Text
2980 #+END_EXAMPLE
2981 #+BEGIN_EXAMPLE
2982 Text
2983 #+END_EXAMPLE
2984 #+BEGIN_EXAMPLE +n 10
2985 Text
2986 #+END_EXAMPLE"
2987 'example-block)))
2988 ;; "-n" resets line number.
2989 (should
2990 (equal
2991 '(0)
2992 (test-org-gen-loc-list "#+BEGIN_SRC emacs-lisp -n \n (- 1 1) \n#+END_SRC"
2993 'src-block)))
2994 ;; The first "+n" has 0 lines before it.
2995 (should
2996 (equal '(0)
2997 (test-org-gen-loc-list
2998 "#+BEGIN_SRC emacs-lisp +n \n (+ 0 (- 1 1))\n#+END_SRC"
2999 'src-block)))
3000 ;; "-n 10" resets line number but has "9 lines" before it.
3001 (should
3002 (equal '(9)
3003 (test-org-gen-loc-list
3004 "#+BEGIN_SRC emacs-lisp -n 10\n (- 10 1)\n#+END_SRC"
3005 'src-block)))
3006 (should
3007 (equal '(9 25)
3008 (test-org-gen-loc-list "
3009 #+BEGIN_SRC emacs-lisp -n 10
3010 (- 10 1)
3011 (+ (- 10 1) 1)
3012 #+END_SRC
3013 #+BEGIN_SRC emacs-lisp +n 15
3014 (+ (- 10 1) 2 (- 15 1))
3015 #+END_SRC"
3016 'src-block)))
3017 (should
3018 (equal '(9 19 0)
3019 (test-org-gen-loc-list "
3020 #+BEGIN_SRC emacs-lisp -n 10
3021 (- 10 1)
3022 #+END_SRC
3023 #+BEGIN_SRC emacs-lisp +n 10
3024 (+ (- 10 1) 1 (- 10 1))
3025 #+END_SRC
3026 #+BEGIN_SRC emacs-lisp -n
3027 (- 1 1)
3028 #+END_SRC"
3029 'src-block)))
3030 ;; A SRC Block without -n does not add to the line count.
3031 (should
3032 (equal '(9 no-loc 19)
3033 (test-org-gen-loc-list
3034 "#+BEGIN_SRC emacs-lisp -n 10
3035 (+ (-10 1) 1)
3036 #+END_SRC
3037 #+BEGIN_SRC emacs-lisp
3038 (+ 2 2)
3039 #+END_SRC
3040 #+BEGIN_SRC emacs-lisp +n 10
3041 (+ (- 10 1) 1 (- 10 1))
3042 #+END_SRC"
3043 'src-block))))
3045 (ert-deftest test-org-export/resolve-coderef ()
3046 "Test `org-export-resolve-coderef' specifications."
3047 (let ((org-coderef-label-format "(ref:%s)"))
3048 ;; A link to a "-n -k -r" block returns line number.
3049 (should
3050 (= 1
3051 (org-test-with-parsed-data
3052 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3053 (org-export-resolve-coderef "coderef" info))))
3054 (should
3055 (= 10
3056 (org-test-with-parsed-data
3057 "#+BEGIN_EXAMPLE -n 10 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3058 (org-export-resolve-coderef "coderef" info))))
3059 (should
3060 (= 135
3061 (org-test-with-parsed-data
3062 "#+BEGIN_EXAMPLE -n 10 -k -r\nText \n#+END_EXAMPLE\n
3063 #+BEGIN_EXAMPLE +n 125 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
3064 (org-export-resolve-coderef "coderef" info))))
3065 (should
3066 (= 1
3067 (org-test-with-parsed-data
3068 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3069 (org-export-resolve-coderef "coderef" info))))
3070 (should
3071 (= 10
3072 (org-test-with-parsed-data
3073 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3074 (org-export-resolve-coderef "coderef" info))))
3075 (should
3076 (= 135
3077 (org-test-with-parsed-data
3078 "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) \n#+END_SRC\n
3079 #+BEGIN_SRC emacs-lisp +n 125 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3080 (org-export-resolve-coderef "coderef" info))))
3081 ;; A link to a "-n -r" block returns line number.
3082 (should
3083 (= 1
3084 (org-test-with-parsed-data
3085 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
3086 (org-export-resolve-coderef "coderef" info))))
3087 (should
3088 (= 10
3089 (org-test-with-parsed-data
3090 "#+BEGIN_EXAMPLE -n 10 -r\nText (ref:coderef)\n#+END_EXAMPLE"
3091 (org-export-resolve-coderef "coderef" info))))
3092 (should
3093 (= 135
3094 (org-test-with-parsed-data
3095 "#+BEGIN_EXAMPLE +n 10 -r\nText \n#+END_EXAMPLE
3096 #+BEGIN_EXAMPLE +n 125 -r\nText (ref:coderef)\n#+END_EXAMPLE"
3097 (org-export-resolve-coderef "coderef" info))))
3099 (should
3100 (= 1
3101 (org-test-with-parsed-data
3102 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3103 (org-export-resolve-coderef "coderef" info))))
3104 (should
3105 (= 10
3106 (org-test-with-parsed-data
3107 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3108 (org-export-resolve-coderef "coderef" info))))
3109 (should
3110 (= 135
3111 (org-test-with-parsed-data
3112 "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) \n#+END_SRC
3113 #+BEGIN_SRC emacs-lisp +n125 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3114 (org-export-resolve-coderef "coderef" info))))
3115 ;; A link to a "-n" block returns coderef.
3116 (should
3117 (equal "coderef"
3118 (org-test-with-parsed-data
3119 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3120 (org-export-resolve-coderef "coderef" info))))
3121 (should
3122 (equal "coderef"
3123 (org-test-with-parsed-data
3124 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
3125 (org-export-resolve-coderef "coderef" info))))
3126 ;; A link to a "-r" block returns line number.
3127 (should
3128 (= 1
3129 (org-test-with-parsed-data
3130 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3131 (org-export-resolve-coderef "coderef" info))))
3132 (should
3133 (= 1
3134 (org-test-with-parsed-data
3135 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
3136 (org-export-resolve-coderef "coderef" info))))
3137 ;; A link to a block without a switch returns coderef.
3138 (should
3139 (equal "coderef"
3140 (org-test-with-parsed-data
3141 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
3142 (org-export-resolve-coderef "coderef" info))))
3143 (org-test-with-parsed-data
3144 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
3145 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
3146 ;; Correctly handle continued line numbers. A "+n" switch should
3147 ;; resume numbering from previous block with numbered lines,
3148 ;; ignoring blocks not numbering lines in the process. A "-n"
3149 ;; switch resets count.
3150 (should
3151 (equal '(2 1)
3152 (org-test-with-parsed-data "
3153 #+BEGIN_EXAMPLE -n
3154 Text.
3155 #+END_EXAMPLE
3157 #+BEGIN_SRC emacs-lisp
3158 \(- 1 1)
3159 #+END_SRC
3161 #+BEGIN_SRC emacs-lisp +n -r
3162 \(+ 1 1) (ref:addition)
3163 #+END_SRC
3165 #+BEGIN_EXAMPLE -n -r
3166 Another text. (ref:text)
3167 #+END_EXAMPLE"
3168 (list (org-export-resolve-coderef "addition" info)
3169 (org-export-resolve-coderef "text" info)))))
3170 ;; Recognize coderef with user-specified syntax.
3171 (should
3172 (equal
3173 "text"
3174 (org-test-with-parsed-data
3175 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
3176 (org-export-resolve-coderef "text" info))))
3177 ;; Unresolved coderefs raise a `org-link-broken' signal.
3178 (should
3179 (condition-case nil
3180 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
3181 (org-export-resolve-coderef "unknown" info))
3182 (org-link-broken t)))))
3184 (ert-deftest test-org-export/resolve-fuzzy-link ()
3185 "Test `org-export-resolve-fuzzy-link' specifications."
3186 ;; Match target objects.
3187 (should
3188 (org-test-with-parsed-data "<<target>> [[target]]"
3189 (org-export-resolve-fuzzy-link
3190 (org-element-map tree 'link 'identity info t) info)))
3191 ;; Match named elements.
3192 (should
3193 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
3194 (org-export-resolve-fuzzy-link
3195 (org-element-map tree 'link 'identity info t) info)))
3196 ;; Match exact headline's name.
3197 (should
3198 (org-test-with-parsed-data "* My headline\n[[My headline]]"
3199 (org-export-resolve-fuzzy-link
3200 (org-element-map tree 'link 'identity info t) info)))
3201 ;; Targets objects have priority over headline titles.
3202 (should
3203 (eq 'target
3204 (org-test-with-parsed-data "* target\n<<target>>[[target]]"
3205 (org-element-type
3206 (org-export-resolve-fuzzy-link
3207 (org-element-map tree 'link 'identity info t) info)))))
3208 ;; Named elements have priority over headline titles.
3209 (should
3210 (eq 'paragraph
3211 (org-test-with-parsed-data
3212 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
3213 (org-element-type
3214 (org-export-resolve-fuzzy-link
3215 (org-element-map tree 'link 'identity info t) info)))))
3216 ;; If link's path starts with a "*", only match headline titles,
3217 ;; though.
3218 (should
3219 (eq 'headline
3220 (org-test-with-parsed-data
3221 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
3222 (org-element-type
3223 (org-export-resolve-fuzzy-link
3224 (org-element-map tree 'link 'identity info t) info)))))
3225 ;; Raise a `org-link-broken' signal if no match.
3226 (should
3227 (org-test-with-parsed-data "[[target]]"
3228 (condition-case nil
3229 (org-export-resolve-fuzzy-link
3230 (org-element-map tree 'link #'identity info t) info)
3231 (org-link-broken t))))
3232 ;; Match fuzzy link even when before first headline.
3233 (should
3234 (eq 'headline
3235 (org-test-with-parsed-data "[[hl]]\n* hl"
3236 (org-element-type
3237 (org-export-resolve-fuzzy-link
3238 (org-element-map tree 'link 'identity info t) info)))))
3239 ;; Handle url-encoded fuzzy links.
3240 (should
3241 (org-test-with-parsed-data "* A B\n[[A%20B]]"
3242 (org-export-resolve-fuzzy-link
3243 (org-element-map tree 'link #'identity info t) info))))
3245 (ert-deftest test-org-export/resolve-id-link ()
3246 "Test `org-export-resolve-id-link' specifications."
3247 ;; Regular test for custom-id link.
3248 (should
3249 (equal '("Headline1")
3250 (org-test-with-parsed-data "* Headline1
3251 :PROPERTIES:
3252 :CUSTOM_ID: test
3253 :END:
3254 * Headline 2
3255 \[[#test]]"
3256 (org-element-property
3257 :title
3258 (org-export-resolve-id-link
3259 (org-element-map tree 'link 'identity info t) info)))))
3260 ;; Raise a `org-link-broken' signal on failing searches.
3261 (should
3262 (org-test-with-parsed-data "* Headline1
3263 :PROPERTIES:
3264 :CUSTOM_ID: test
3265 :END:
3266 * Headline 2
3267 \[[#no-match]]"
3268 (condition-case nil
3269 (org-export-resolve-id-link
3270 (org-element-map tree 'link #'identity info t) info)
3271 (org-link-broken t))))
3272 ;; Test for internal id target.
3273 (should
3274 (equal '("Headline1")
3275 (org-test-with-parsed-data "* Headline1
3276 :PROPERTIES:
3277 :ID: aaaa
3278 :END:
3279 * Headline 2
3280 \[[id:aaaa]]"
3281 (org-element-property
3282 :title
3283 (org-export-resolve-id-link
3284 (org-element-map tree 'link 'identity info t) info)))))
3285 ;; Test for external id target.
3286 (should
3287 (equal
3288 "external-file"
3289 (org-test-with-parsed-data "[[id:aaaa]]"
3290 (org-export-resolve-id-link
3291 (org-element-map tree 'link 'identity info t)
3292 (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
3294 (ert-deftest test-org-export/resolve-radio-link ()
3295 "Test `org-export-resolve-radio-link' specifications."
3296 ;; Standard test.
3297 (should
3298 (org-test-with-temp-text "<<<radio>>> radio"
3299 (org-update-radio-target-regexp)
3300 (let* ((tree (org-element-parse-buffer))
3301 (info `(:parse-tree ,tree)))
3302 (org-export-resolve-radio-link
3303 (org-element-map tree 'link 'identity info t)
3304 info))))
3305 ;; Radio targets are case-insensitive.
3306 (should
3307 (org-test-with-temp-text "<<<RADIO>>> radio"
3308 (org-update-radio-target-regexp)
3309 (let* ((tree (org-element-parse-buffer))
3310 (info `(:parse-tree ,tree)))
3311 (org-export-resolve-radio-link
3312 (org-element-map tree 'link 'identity info t)
3313 info))))
3314 ;; Radio target with objects.
3315 (should
3316 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
3317 (org-update-radio-target-regexp)
3318 (let* ((tree (org-element-parse-buffer))
3319 (info `(:parse-tree ,tree)))
3320 (org-export-resolve-radio-link
3321 (org-element-map tree 'link 'identity info t)
3322 info))))
3323 ;; Radio target with objects at its beginning.
3324 (should
3325 (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
3326 (org-update-radio-target-regexp)
3327 (let* ((tree (org-element-parse-buffer))
3328 (info `(:parse-tree ,tree)))
3329 (org-export-resolve-radio-link
3330 (org-element-map tree 'link 'identity info t)
3331 info))))
3332 ;; Radio link next to an apostrophe.
3333 (should
3334 (org-test-with-temp-text "<<<radio>>> radio's"
3335 (org-update-radio-target-regexp)
3336 (let* ((tree (org-element-parse-buffer))
3337 (info `(:parse-tree ,tree)))
3338 (org-export-resolve-radio-link
3339 (org-element-map tree 'link 'identity info t)
3340 info))))
3341 ;; Multiple radio targets.
3342 (should
3343 (equal '("radio1" "radio2")
3344 (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
3345 (org-update-radio-target-regexp)
3346 (let* ((tree (org-element-parse-buffer))
3347 (info `(:parse-tree ,tree)))
3348 (org-element-map tree 'link
3349 (lambda (link)
3350 (org-element-property
3351 :value (org-export-resolve-radio-link link info)))
3352 info)))))
3353 ;; Radio target is whitespace insensitive.
3354 (should
3355 (org-test-with-temp-text "<<<a radio>>> a\n radio"
3356 (org-update-radio-target-regexp)
3357 (let* ((tree (org-element-parse-buffer))
3358 (info `(:parse-tree ,tree)))
3359 (org-element-map tree 'link
3360 (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
3362 (ert-deftest test-org-export/file-uri ()
3363 "Test `org-export-file-uri' specifications."
3364 ;; Preserve relative filenames.
3365 (should (equal "relative.org" (org-export-file-uri "relative.org")))
3366 ;; Local files start with "file://"
3367 (should (equal (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "/local.org"))
3368 (org-export-file-uri "/local.org")))
3369 ;; Remote files start with "file://"
3370 (should (equal "file://ssh:myself@some.where:papers/last.pdf"
3371 (org-export-file-uri "/ssh:myself@some.where:papers/last.pdf")))
3372 (should (equal "file://localhost/etc/fstab"
3373 (org-export-file-uri "//localhost/etc/fstab")))
3374 ;; Expand filename starting with "~".
3375 (should (equal (org-export-file-uri "~/file.org")
3376 (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "~/file.org")))))
3378 (ert-deftest test-org-export/get-reference ()
3379 "Test `org-export-get-reference' specifications."
3380 (should
3381 (org-test-with-parsed-data "* Headline"
3382 (org-export-get-reference (org-element-map tree 'headline #'identity nil t)
3383 info)))
3384 ;; For a given element always return the same reference.
3385 (should
3386 (org-test-with-parsed-data "* Headline"
3387 (let ((headline (org-element-map tree 'headline #'identity nil t)))
3388 (equal (org-export-get-reference headline info)
3389 (org-export-get-reference headline info)))))
3390 ;; References get through local export back-ends.
3391 (should
3392 (org-test-with-parsed-data "* Headline"
3393 (let ((headline (org-element-map tree 'headline #'identity nil t))
3394 (backend
3395 (org-export-create-backend
3396 :transcoders
3397 '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
3398 (equal (org-trim (org-export-data-with-backend headline backend info))
3399 (org-export-get-reference headline info)))))
3400 (should
3401 (org-test-with-parsed-data "* Headline"
3402 (let ((headline (org-element-map tree 'headline #'identity nil t))
3403 (backend
3404 (org-export-create-backend
3405 :transcoders
3406 '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
3407 (equal (org-export-with-backend backend headline nil info)
3408 (org-export-get-reference headline info)))))
3409 ;; Use search cells defined in `:crossrefs'. However, handle
3410 ;; duplicate search cells.
3411 (should
3412 (equal "org0000001"
3413 (org-test-with-parsed-data "* Headline"
3414 (let* ((headline (org-element-map tree 'headline #'identity nil t))
3415 (search-cell (car (org-export-search-cells headline))))
3416 (setq info
3417 (plist-put info :crossrefs (list (cons search-cell 1))))
3418 (org-export-get-reference headline info)))))
3419 (should-not
3420 (equal '("org0000001" "org0000001")
3421 (org-test-with-parsed-data "* H\n** H"
3422 (org-element-map tree 'headline
3423 (lambda (h)
3424 (let* ((search-cell (car (org-export-search-cells h)))
3425 (info (plist-put info :crossrefs
3426 (list (cons search-cell 1)))))
3427 (org-export-get-reference h info))))))))
3430 ;;; Pseudo objects and pseudo elements
3432 (ert-deftest test-org-export/pseudo-elements ()
3433 "Test exporting pseudo-elements."
3434 ;; Handle blank lines after pseudo-elements. In particular, do not
3435 ;; replace them with white spaces.
3436 (should
3437 (equal "contents\n\nparagraph\n"
3438 (let ((backend (org-export-create-backend
3439 :transcoders
3440 '((pseudo-element . (lambda (_p c _i) c))
3441 (paragraph . (lambda (_p c _i) c))
3442 (plain-text . (lambda (c _i) c)))))
3443 (element '(pseudo-element (:post-blank 1) "contents"))
3444 (paragraph '(paragraph nil "paragraph"))
3445 (data '(org-data nil)))
3446 (org-element-adopt-elements data element paragraph)
3447 (org-export-data-with-backend data backend nil)))))
3449 (ert-deftest test-org-export/pseudo-objects ()
3450 "Test exporting pseudo-objects."
3451 ;; Handle blank spaces after pseudo-objects. In particular, do not
3452 ;; replace them with newlines.
3453 (should
3454 (equal "begin x end\n"
3455 (let ((backend (org-export-create-backend
3456 :transcoders
3457 '((pseudo-object . (lambda (_p c _i) c))
3458 (paragraph . (lambda (_p c _i) c))
3459 (plain-text . (lambda (c _i) c)))))
3460 (object '(pseudo-object (:post-blank 1) "x"))
3461 (paragraph '(paragraph nil)))
3462 (org-element-adopt-elements paragraph "begin " object "end")
3463 (org-export-data-with-backend paragraph backend nil)))))
3466 ;;; Src-block and example-block
3468 (ert-deftest test-org-export/unravel-code ()
3469 "Test `org-export-unravel-code' function."
3470 ;; Code without reference.
3471 (should
3472 (equal '("(+ 1 1)")
3473 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
3474 (org-export-unravel-code (org-element-at-point)))))
3475 ;; Code with reference.
3476 (should
3477 (equal '("(+ 1 1)" (1 . "test"))
3478 (org-test-with-temp-text
3479 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
3480 (let ((org-coderef-label-format "(ref:%s)"))
3481 (org-export-unravel-code (org-element-at-point))))))
3482 ;; Code with user-defined reference.
3483 (should
3484 (equal
3485 '("(+ 1 1)" (1 . "test"))
3486 (org-test-with-temp-text
3487 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
3488 (let ((org-coderef-label-format "(ref:%s)"))
3489 (org-export-unravel-code (org-element-at-point))))))
3490 ;; Code references keys are relative to the current block.
3491 (should
3492 (equal '("(+ 2 2)\n(+ 3 3)" (2 . "one"))
3493 (org-test-with-temp-text "
3494 #+BEGIN_EXAMPLE -n
3495 \(+ 1 1)
3496 #+END_EXAMPLE
3497 #+BEGIN_EXAMPLE +n
3498 \(+ 2 2)
3499 \(+ 3 3) (ref:one)
3500 #+END_EXAMPLE"
3501 (goto-line 5)
3502 (let ((org-coderef-label-format "(ref:%s)"))
3503 (org-export-unravel-code (org-element-at-point)))))\x14))
3505 (ert-deftest test-org-export/format-code-default ()
3506 "Test `org-export-format-code-default' specifications."
3507 ;; Preserve blank lines, even when code is empty.
3508 (should
3509 (equal "\n\n"
3510 (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
3511 (org-export-format-code-default
3512 (org-element-map tree 'src-block #'identity info t) info))))
3513 ;; Likewise, preserve leading and trailing blank lines in the code.
3514 (should
3515 (equal "\n(+ 1 1)\n"
3516 (org-test-with-parsed-data
3517 "#+BEGIN_SRC emacs-lisp\n\n(+ 1 1)\n#+END_SRC"
3518 (org-export-format-code-default
3519 (org-element-map tree 'src-block #'identity info t) info))))
3520 (should
3521 (equal "(+ 1 1)\n\n"
3522 (org-test-with-parsed-data
3523 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n\n#+END_SRC"
3524 (org-export-format-code-default
3525 (org-element-map tree 'src-block #'identity info t) info))))
3526 ;; Number lines, two whitespace characters before the actual loc.
3527 (should
3528 (equal "1 a\n2 b\n"
3529 (org-test-with-parsed-data
3530 "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
3531 (org-export-format-code-default
3532 (org-element-map tree 'src-block #'identity info t) info))))
3533 ;; Numbering includes blank lines.
3534 (should
3535 (equal "1 \n2 a\n3 \n4 b\n5 \n"
3536 (org-test-with-parsed-data
3537 "#+BEGIN_SRC emacs-lisp +n\n\na\n\nb\n\n#+END_SRC"
3538 (org-export-format-code-default
3539 (org-element-map tree 'src-block #'identity info t) info))))
3540 ;; Put references 6 whitespace characters after the widest line,
3541 ;; wrapped within parenthesis.
3542 (should
3543 (equal "123 (a)\n1 (b)\n"
3544 (let ((org-coderef-label-format "(ref:%s)"))
3545 (org-test-with-parsed-data
3546 "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
3547 (org-export-format-code-default
3548 (org-element-map tree 'src-block #'identity info t) info))))))
3552 ;;; Smart Quotes
3554 (ert-deftest test-org-export/activate-smart-quotes ()
3555 "Test `org-export-activate-smart-quotes' specifications."
3556 ;; Double quotes: standard test.
3557 (should
3558 (equal
3559 '("some &ldquo;quoted&rdquo; text")
3560 (let ((org-export-default-language "en"))
3561 (org-test-with-parsed-data "some \"quoted\" text"
3562 (org-element-map tree 'plain-text
3563 (lambda (s) (org-export-activate-smart-quotes s :html info))
3564 info)))))
3565 ;; Opening quotes: at the beginning of a paragraph.
3566 (should
3567 (equal
3568 '("&ldquo;begin")
3569 (let ((org-export-default-language "en"))
3570 (org-test-with-parsed-data "\"begin"
3571 (org-element-map tree 'plain-text
3572 (lambda (s) (org-export-activate-smart-quotes s :html info))
3573 info)))))
3574 ;; Opening quotes: after an object.
3575 (should
3576 (equal
3577 '("&ldquo;quoted&rdquo; text")
3578 (let ((org-export-default-language "en"))
3579 (org-test-with-parsed-data "=verb= \"quoted\" text"
3580 (org-element-map tree 'plain-text
3581 (lambda (s) (org-export-activate-smart-quotes s :html info))
3582 info)))))
3583 ;; Closing quotes: at the end of a paragraph.
3584 (should
3585 (equal
3586 '("Quoted &ldquo;text&rdquo;")
3587 (let ((org-export-default-language "en"))
3588 (org-test-with-parsed-data "Quoted \"text\""
3589 (org-element-map tree 'plain-text
3590 (lambda (s) (org-export-activate-smart-quotes s :html info))
3591 info)))))
3592 ;; Inner quotes: standard test.
3593 (should
3594 (equal '("« outer « inner » outer »")
3595 (let ((org-export-default-language "fr"))
3596 (org-test-with-parsed-data "\"outer 'inner' outer\""
3597 (org-element-map tree 'plain-text
3598 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3599 info)))))
3600 ;; Inner quotes: close to special symbols.
3601 (should
3602 (equal '("« outer (« inner ») outer »")
3603 (let ((org-export-default-language "fr"))
3604 (org-test-with-parsed-data "\"outer ('inner') outer\""
3605 (org-element-map tree 'plain-text
3606 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3607 info)))))
3608 (should
3609 (equal '("« « inner » »")
3610 (let ((org-export-default-language "fr"))
3611 (org-test-with-parsed-data "\"'inner'\""
3612 (org-element-map tree 'plain-text
3613 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3614 info)))))
3615 ;; Apostrophe: standard test.
3616 (should
3617 (equal '("It « shouldn’t » fail")
3618 (let ((org-export-default-language "fr"))
3619 (org-test-with-parsed-data "It \"shouldn't\" fail"
3620 (org-element-map tree 'plain-text
3621 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3622 info)))))
3623 (should
3624 (equal '("It shouldn’t fail")
3625 (let ((org-export-default-language "fr"))
3626 (org-test-with-parsed-data "It shouldn't fail"
3627 (org-element-map tree 'plain-text
3628 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3629 info)))))
3630 ;; Apostrophe: before an object.
3631 (should
3632 (equal
3633 '("« a’" " »")
3634 (let ((org-export-default-language "fr"))
3635 (org-test-with-parsed-data "\"a'=b=\""
3636 (org-element-map tree 'plain-text
3637 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3638 info)))))
3639 ;; Apostrophe: after an object.
3640 (should
3641 (equal '("« " "’s »")
3642 (let ((org-export-default-language "fr"))
3643 (org-test-with-parsed-data "\"=code='s\""
3644 (org-element-map tree 'plain-text
3645 (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
3646 info)))))
3647 ;; Special case: isolated quotes.
3648 (should
3649 (equal '("&ldquo;" "&rdquo;")
3650 (let ((org-export-default-language "en"))
3651 (org-test-with-parsed-data "\"$x$\""
3652 (org-element-map tree 'plain-text
3653 (lambda (s) (org-export-activate-smart-quotes s :html info))
3654 info)))))
3655 ;; Smart quotes in secondary strings.
3656 (should
3657 (equal '("&ldquo;" "&rdquo;")
3658 (let ((org-export-default-language "en"))
3659 (org-test-with-parsed-data "* \"$x$\""
3660 (org-element-map tree 'plain-text
3661 (lambda (s) (org-export-activate-smart-quotes s :html info))
3662 info)))))
3663 ;; Smart quotes in document keywords.
3664 (should
3665 (equal '("&ldquo;" "&rdquo;")
3666 (let ((org-export-default-language "en"))
3667 (org-test-with-parsed-data "#+TITLE: \"$x$\""
3668 (org-element-map (plist-get info :title) 'plain-text
3669 (lambda (s) (org-export-activate-smart-quotes s :html info))
3670 info)))))
3671 ;; Smart quotes in parsed affiliated keywords.
3672 (should
3673 (equal '("&ldquo;" "&rdquo;" "Paragraph")
3674 (let ((org-export-default-language "en"))
3675 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
3676 (org-element-map tree 'plain-text
3677 (lambda (s) (org-export-activate-smart-quotes s :html info))
3678 info nil nil t)))))
3679 ;; Smart quotes within objects.
3680 (should
3681 (equal '("&ldquo;foo&rdquo;")
3682 (let ((org-export-default-language "en"))
3683 (org-test-with-parsed-data "| \"foo\" |"
3684 (org-element-map tree 'plain-text
3685 (lambda (s) (org-export-activate-smart-quotes s :html info))
3686 info nil nil t)))))
3687 ;; FIXME: Test failing non-interactively.
3689 ;; (should
3690 ;; (equal '("&ldquo;foo&rdquo;")
3691 ;; (let ((org-export-default-language "en"))
3692 ;; (org-test-with-parsed-data "*\"foo\"*"
3693 ;; (org-element-map tree 'plain-text
3694 ;; (lambda (s) (org-export-activate-smart-quotes s :html info))
3695 ;; info nil nil t)))))
3700 ;;; Tables
3702 (ert-deftest test-org-export/special-column ()
3703 "Test if the table's special column is properly recognized."
3704 ;; 1. First column is special if it contains only a special marking
3705 ;; characters or empty cells.
3706 (org-test-with-temp-text "
3707 | ! | 1 |
3708 | | 2 |"
3709 (should
3710 (org-export-table-has-special-column-p
3711 (org-element-map
3712 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3713 ;; 2. If the column contains anything else, it isn't special.
3714 (org-test-with-temp-text "
3715 | ! | 1 |
3716 | b | 2 |"
3717 (should-not
3718 (org-export-table-has-special-column-p
3719 (org-element-map
3720 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3721 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
3722 ;; and "!".
3723 (org-test-with-temp-text "
3724 | # | 1 |
3725 | ^ | 2 |
3726 | * | 3 |
3727 | _ | 4 |
3728 | / | 5 |
3729 | $ | 6 |
3730 | ! | 7 |"
3731 (should
3732 (org-export-table-has-special-column-p
3733 (org-element-map
3734 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
3735 ;; 4. A first column with only empty cells isn't considered as
3736 ;; special.
3737 (org-test-with-temp-text "
3738 | | 1 |
3739 | | 2 |"
3740 (should-not
3741 (org-export-table-has-special-column-p
3742 (org-element-map
3743 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
3745 (ert-deftest test-org-export/table-row-is-special-p ()
3746 "Test `org-export-table-row-is-special-p' specifications."
3747 ;; 1. A row is special if it has a special marking character in the
3748 ;; special column.
3749 (org-test-with-parsed-data "| ! | 1 |"
3750 (should
3751 (org-export-table-row-is-special-p
3752 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3753 ;; 2. A row is special when its first field is "/"
3754 (org-test-with-parsed-data "
3755 | / | 1 |
3756 | a | b |"
3757 (should
3758 (org-export-table-row-is-special-p
3759 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3760 ;; 3. A row only containing alignment cookies is also considered as
3761 ;; special.
3762 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
3763 (should
3764 (org-export-table-row-is-special-p
3765 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3766 ;; 4. Everything else isn't considered as special.
3767 (org-test-with-parsed-data "| \alpha | | c |"
3768 (should-not
3769 (org-export-table-row-is-special-p
3770 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
3771 ;; 5. Table's rules are never considered as special rows.
3772 (org-test-with-parsed-data "|---+---|"
3773 (should-not
3774 (org-export-table-row-is-special-p
3775 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
3777 (ert-deftest test-org-export/has-header-p ()
3778 "Test `org-export-table-has-header-p' specifications."
3779 ;; With an header.
3780 (should
3781 (org-test-with-parsed-data "
3782 | a | b |
3783 |---+---|
3784 | c | d |"
3785 (org-export-table-has-header-p
3786 (org-element-map tree 'table 'identity info 'first-match)
3787 info)))
3788 ;; Without an header.
3789 (should-not
3790 (org-test-with-parsed-data "
3791 | a | b |
3792 | c | d |"
3793 (org-export-table-has-header-p
3794 (org-element-map tree 'table 'identity info 'first-match)
3795 info)))
3796 ;; Don't get fooled with starting and ending rules.
3797 (should-not
3798 (org-test-with-parsed-data "
3799 |---+---|
3800 | a | b |
3801 | c | d |
3802 |---+---|"
3803 (org-export-table-has-header-p
3804 (org-element-map tree 'table 'identity info 'first-match)
3805 info))))
3807 (ert-deftest test-org-export/table-row-group ()
3808 "Test `org-export-table-row-group' specifications."
3809 ;; A rule creates a new group.
3810 (should
3811 (equal '(1 rule 2)
3812 (org-test-with-parsed-data "
3813 | a | b |
3814 |---+---|
3815 | 1 | 2 |"
3816 (org-element-map tree 'table-row
3817 (lambda (row)
3818 (if (eq (org-element-property :type row) 'rule) 'rule
3819 (org-export-table-row-group row info)))))))
3820 ;; Special rows are ignored in count.
3821 (should
3822 (equal
3823 '(rule 1)
3824 (org-test-with-parsed-data "
3825 | / | < | > |
3826 |---|---+---|
3827 | | 1 | 2 |"
3828 (org-element-map tree 'table-row
3829 (lambda (row)
3830 (if (eq (org-element-property :type row) 'rule) 'rule
3831 (org-export-table-row-group row info)))
3832 info))))
3833 ;; Double rules also are ignored in count.
3834 (should
3835 (equal '(1 rule rule 2)
3836 (org-test-with-parsed-data "
3837 | a | b |
3838 |---+---|
3839 |---+---|
3840 | 1 | 2 |"
3841 (org-element-map tree 'table-row
3842 (lambda (row)
3843 (if (eq (org-element-property :type row) 'rule) 'rule
3844 (org-export-table-row-group row info))))))))
3846 (ert-deftest test-org-export/table-row-number ()
3847 "Test `org-export-table-row-number' specifications."
3848 ;; Standard test. Number is 0-indexed.
3849 (should
3850 (equal '(0 1)
3851 (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
3852 (org-element-map tree 'table-row
3853 (lambda (row) (org-export-table-row-number row info)) info))))
3854 ;; Number ignores separators.
3855 (should
3856 (equal '(0 1)
3857 (org-test-with-parsed-data "
3858 | a | b | c |
3859 |---+---+---|
3860 | d | e | f |"
3861 (org-element-map tree 'table-row
3862 (lambda (row) (org-export-table-row-number row info)) info))))
3863 ;; Number ignores special rows.
3864 (should
3865 (equal '(0 1)
3866 (org-test-with-parsed-data "
3867 | / | < | > |
3868 | | b | c |
3869 |---+-----+-----|
3870 | | <c> | <c> |
3871 | | e | f |"
3872 (org-element-map tree 'table-row
3873 (lambda (row) (org-export-table-row-number row info)) info)))))
3875 (ert-deftest test-org-export/table-cell-width ()
3876 "Test `org-export-table-cell-width' specifications."
3877 ;; 1. Width is primarily determined by width cookies. If no cookie
3878 ;; is found, cell's width is nil.
3879 (org-test-with-parsed-data "
3880 | / | <l> | <6> | <l7> |
3881 | | a | b | c |"
3882 (should
3883 (equal
3884 '(nil 6 7)
3885 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3886 (org-element-map tree 'table-cell 'identity info)))))
3887 ;; 2. The last width cookie has precedence.
3888 (org-test-with-parsed-data "
3889 | <6> |
3890 | <7> |
3891 | a |"
3892 (should
3893 (equal
3894 '(7)
3895 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3896 (org-element-map tree 'table-cell 'identity info)))))
3897 ;; 3. Valid width cookies must have a specific row.
3898 (org-test-with-parsed-data "| <6> | cell |"
3899 (should
3900 (equal
3901 '(nil nil)
3902 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
3903 (org-element-map tree 'table-cell 'identity))))))
3905 (ert-deftest test-org-export/table-cell-alignment ()
3906 "Test `org-export-table-cell-alignment' specifications."
3907 ;; 1. Alignment is primarily determined by alignment cookies.
3908 (should
3909 (equal '(left center right)
3910 (let ((org-table-number-fraction 0.5)
3911 (org-table-number-regexp "^[0-9]+$"))
3912 (org-test-with-parsed-data "| <l> | <c> | <r> |"
3913 (mapcar (lambda (cell)
3914 (org-export-table-cell-alignment cell info))
3915 (org-element-map tree 'table-cell 'identity))))))
3916 ;; 2. The last alignment cookie has precedence.
3917 (should
3918 (equal '(right right right)
3919 (org-test-with-parsed-data "
3920 | <l8> |
3921 | cell |
3922 | <r9> |"
3923 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
3924 (org-element-map tree 'table-cell 'identity)))))
3925 ;; 3. If there's no cookie, cell's contents determine alignment.
3926 ;; A column mostly made of cells containing numbers will align
3927 ;; its cells to the right.
3928 (should
3929 (equal '(right right right)
3930 (let ((org-table-number-fraction 0.5)
3931 (org-table-number-regexp "^[0-9]+$"))
3932 (org-test-with-parsed-data "
3933 | 123 |
3934 | some text |
3935 | 12345 |"
3936 (mapcar (lambda (cell)
3937 (org-export-table-cell-alignment cell info))
3938 (org-element-map tree 'table-cell 'identity))))))
3939 ;; 4. Otherwise, they will be aligned to the left.
3940 (should
3941 (equal '(left left left)
3942 (org-test-with-parsed-data "
3943 | text |
3944 | some text |
3945 | \alpha |"
3946 (mapcar (lambda (cell)
3947 (org-export-table-cell-alignment cell info))
3948 (org-element-map tree 'table-cell 'identity info))))))
3950 (ert-deftest test-org-export/table-cell-borders ()
3951 "Test `org-export-table-cell-borders' specifications."
3952 ;; 1. Recognize various column groups indicators.
3953 (org-test-with-parsed-data "| / | < | > | <> |"
3954 (should
3955 (equal
3956 '((right bottom top) (left bottom top) (right bottom top)
3957 (right left bottom top))
3958 (mapcar (lambda (cell)
3959 (org-export-table-cell-borders cell info))
3960 (org-element-map tree 'table-cell 'identity)))))
3961 ;; 2. Accept shortcuts to define column groups.
3962 (org-test-with-parsed-data "| / | < | < |"
3963 (should
3964 (equal
3965 '((right bottom top) (right left bottom top) (left bottom top))
3966 (mapcar (lambda (cell)
3967 (org-export-table-cell-borders cell info))
3968 (org-element-map tree 'table-cell 'identity)))))
3969 ;; 3. A valid column groups row must start with a "/".
3970 (org-test-with-parsed-data "
3971 | | < |
3972 | a | b |"
3973 (should
3974 (equal '((top) (top) (bottom) (bottom))
3975 (mapcar (lambda (cell)
3976 (org-export-table-cell-borders cell info))
3977 (org-element-map tree 'table-cell 'identity)))))
3978 ;; 4. Take table rules into consideration.
3979 (org-test-with-parsed-data "
3980 | 1 |
3981 |---|
3982 | 2 |"
3983 (should
3984 (equal '((below top) (bottom above))
3985 (mapcar (lambda (cell)
3986 (org-export-table-cell-borders cell info))
3987 (org-element-map tree 'table-cell 'identity)))))
3988 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
3989 ;; (resp. `bottom' and `below') borders. Any special row is
3990 ;; ignored.
3991 (org-test-with-parsed-data "
3992 |---+----|
3993 | / | |
3994 | | 1 |
3995 |---+----|"
3996 (should
3997 (equal '((bottom below top above))
3998 (last
3999 (mapcar (lambda (cell)
4000 (org-export-table-cell-borders cell info))
4001 (org-element-map tree 'table-cell 'identity)))))))
4003 (ert-deftest test-org-export/table-dimensions ()
4004 "Test `org-export-table-dimensions' specifications."
4005 ;; 1. Standard test.
4006 (org-test-with-parsed-data "
4007 | 1 | 2 | 3 |
4008 | 4 | 5 | 6 |"
4009 (should
4010 (equal '(2 . 3)
4011 (org-export-table-dimensions
4012 (org-element-map tree 'table 'identity info 'first-match) info))))
4013 ;; 2. Ignore horizontal rules and special columns.
4014 (org-test-with-parsed-data "
4015 | / | < | > |
4016 | 1 | 2 | 3 |
4017 |---+---+---|
4018 | 4 | 5 | 6 |"
4019 (should
4020 (equal '(2 . 3)
4021 (org-export-table-dimensions
4022 (org-element-map tree 'table 'identity info 'first-match) info)))))
4024 (ert-deftest test-org-export/table-cell-address ()
4025 "Test `org-export-table-cell-address' specifications."
4026 ;; 1. Standard test: index is 0-based.
4027 (org-test-with-parsed-data "| a | b |"
4028 (should
4029 (equal '((0 . 0) (0 . 1))
4030 (org-element-map tree 'table-cell
4031 (lambda (cell) (org-export-table-cell-address cell info))
4032 info))))
4033 ;; 2. Special column isn't counted, nor are special rows.
4034 (org-test-with-parsed-data "
4035 | / | <> |
4036 | | c |"
4037 (should
4038 (equal '(0 . 0)
4039 (org-export-table-cell-address
4040 (car (last (org-element-map tree 'table-cell 'identity info)))
4041 info))))
4042 ;; 3. Tables rules do not count either.
4043 (org-test-with-parsed-data "
4044 | a |
4045 |---|
4046 | b |
4047 |---|
4048 | c |"
4049 (should
4050 (equal '(2 . 0)
4051 (org-export-table-cell-address
4052 (car (last (org-element-map tree 'table-cell 'identity info)))
4053 info))))
4054 ;; 4. Return nil for special cells.
4055 (org-test-with-parsed-data "| / | a |"
4056 (should-not
4057 (org-export-table-cell-address
4058 (org-element-map tree 'table-cell 'identity nil 'first-match)
4059 info))))
4061 (ert-deftest test-org-export/get-table-cell-at ()
4062 "Test `org-export-get-table-cell-at' specifications."
4063 ;; 1. Address ignores special columns, special rows and rules.
4064 (org-test-with-parsed-data "
4065 | / | <> |
4066 | | a |
4067 |---+----|
4068 | | b |"
4069 (should
4070 (equal '("b")
4071 (org-element-contents
4072 (org-export-get-table-cell-at
4073 '(1 . 0)
4074 (org-element-map tree 'table 'identity info 'first-match)
4075 info)))))
4076 ;; 2. Return value for a non-existent address is nil.
4077 (org-test-with-parsed-data "| a |"
4078 (should-not
4079 (org-export-get-table-cell-at
4080 '(2 . 2)
4081 (org-element-map tree 'table 'identity info 'first-match)
4082 info)))
4083 (org-test-with-parsed-data "| / |"
4084 (should-not
4085 (org-export-get-table-cell-at
4086 '(0 . 0)
4087 (org-element-map tree 'table 'identity info 'first-match)
4088 info))))
4090 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
4091 "Test `org-export-table-cell-starts-colgroup-p' specifications."
4092 ;; 1. A cell at a beginning of a row always starts a column group.
4093 (org-test-with-parsed-data "| a |"
4094 (should
4095 (org-export-table-cell-starts-colgroup-p
4096 (org-element-map tree 'table-cell 'identity info 'first-match)
4097 info)))
4098 ;; 2. Special column should be ignored when determining the
4099 ;; beginning of the row.
4100 (org-test-with-parsed-data "
4101 | / | |
4102 | | a |"
4103 (should
4104 (org-export-table-cell-starts-colgroup-p
4105 (org-element-map tree 'table-cell 'identity info 'first-match)
4106 info)))
4107 ;; 2. Explicit column groups.
4108 (org-test-with-parsed-data "
4109 | / | | < |
4110 | a | b | c |"
4111 (should
4112 (equal
4113 '(yes no yes)
4114 (org-element-map tree 'table-cell
4115 (lambda (cell)
4116 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
4117 info)))))
4119 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
4120 "Test `org-export-table-cell-ends-colgroup-p' specifications."
4121 ;; 1. A cell at the end of a row always ends a column group.
4122 (org-test-with-parsed-data "| a |"
4123 (should
4124 (org-export-table-cell-ends-colgroup-p
4125 (org-element-map tree 'table-cell 'identity info 'first-match)
4126 info)))
4127 ;; 2. Special column should be ignored when determining the
4128 ;; beginning of the row.
4129 (org-test-with-parsed-data "
4130 | / | |
4131 | | a |"
4132 (should
4133 (org-export-table-cell-ends-colgroup-p
4134 (org-element-map tree 'table-cell 'identity info 'first-match)
4135 info)))
4136 ;; 3. Explicit column groups.
4137 (org-test-with-parsed-data "
4138 | / | < | |
4139 | a | b | c |"
4140 (should
4141 (equal
4142 '(yes no yes)
4143 (org-element-map tree 'table-cell
4144 (lambda (cell)
4145 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
4146 info)))))
4148 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
4149 "Test `org-export-table-row-starts-rowgroup-p' specifications."
4150 ;; 1. A row at the beginning of a table always starts a row group.
4151 ;; So does a row following a table rule.
4152 (org-test-with-parsed-data "
4153 | a |
4154 |---|
4155 | b |"
4156 (should
4157 (equal
4158 '(yes no yes)
4159 (org-element-map tree 'table-row
4160 (lambda (row)
4161 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
4162 info))))
4163 ;; 2. Special rows should be ignored when determining the beginning
4164 ;; of the row.
4165 (org-test-with-parsed-data "
4166 | / | < |
4167 | | a |
4168 |---+---|
4169 | / | < |
4170 | | b |"
4171 (should
4172 (equal
4173 '(yes no yes)
4174 (org-element-map tree 'table-row
4175 (lambda (row)
4176 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
4177 info)))))
4179 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
4180 "Test `org-export-table-row-ends-rowgroup-p' specifications."
4181 ;; 1. A row at the end of a table always ends a row group. So does
4182 ;; a row preceding a table rule.
4183 (org-test-with-parsed-data "
4184 | a |
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-ends-rowgroup-p row info) 'yes 'no))
4193 info))))
4194 ;; 2. Special rows should be ignored when determining the beginning
4195 ;; of the row.
4196 (org-test-with-parsed-data "
4197 | | a |
4198 | / | < |
4199 |---+---|
4200 | | b |
4201 | / | < |"
4202 (should
4203 (equal
4204 '(yes no yes)
4205 (org-element-map tree 'table-row
4206 (lambda (row)
4207 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
4208 info)))))
4210 (ert-deftest test-org-export/table-row-in-header-p ()
4211 "Test `org-export-table-row-in-header-p' specifications."
4212 ;; Standard test. Separators are always nil.
4213 (should
4214 (equal
4215 '(yes no no)
4216 (org-test-with-parsed-data "| a |\n|---|\n| b |"
4217 (org-element-map tree 'table-row
4218 (lambda (row)
4219 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
4220 ;; Nil when there is no header.
4221 (should
4222 (equal
4223 '(no no)
4224 (org-test-with-parsed-data "| a |\n| b |"
4225 (org-element-map tree 'table-row
4226 (lambda (row)
4227 (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
4229 (ert-deftest test-org-export/table-row-starts-header-p ()
4230 "Test `org-export-table-row-starts-header-p' specifications."
4231 ;; 1. Only the row starting the first row group starts the table
4232 ;; header.
4233 (org-test-with-parsed-data "
4234 | a |
4235 | b |
4236 |---|
4237 | c |"
4238 (should
4239 (equal
4240 '(yes no no no)
4241 (org-element-map tree 'table-row
4242 (lambda (row)
4243 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
4244 info))))
4245 ;; 2. A row cannot start an header if there's no header in the
4246 ;; table.
4247 (org-test-with-parsed-data "
4248 | a |
4249 |---|"
4250 (should-not
4251 (org-export-table-row-starts-header-p
4252 (org-element-map tree 'table-row 'identity info 'first-match)
4253 info))))
4255 (ert-deftest test-org-export/table-row-ends-header-p ()
4256 "Test `org-export-table-row-ends-header-p' specifications."
4257 ;; 1. Only the row starting the first row group starts the table
4258 ;; header.
4259 (org-test-with-parsed-data "
4260 | a |
4261 | b |
4262 |---|
4263 | c |"
4264 (should
4265 (equal
4266 '(no yes no no)
4267 (org-element-map tree 'table-row
4268 (lambda (row)
4269 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
4270 info))))
4271 ;; 2. A row cannot start an header if there's no header in the
4272 ;; table.
4273 (org-test-with-parsed-data "
4274 | a |
4275 |---|"
4276 (should-not
4277 (org-export-table-row-ends-header-p
4278 (org-element-map tree 'table-row 'identity info 'first-match)
4279 info))))
4283 ;;; Tables of Contents
4285 (ert-deftest test-org-export/collect-headlines ()
4286 "Test `org-export-collect-headlines' specifications."
4287 ;; Standard test.
4288 (should
4289 (= 2
4290 (length
4291 (org-test-with-parsed-data "* H1\n** H2"
4292 (org-export-collect-headlines info)))))
4293 ;; Do not collect headlines below optional argument.
4294 (should
4295 (= 1
4296 (length
4297 (org-test-with-parsed-data "* H1\n** H2"
4298 (org-export-collect-headlines info 1)))))
4299 ;; Never collect headlines below maximum headline level.
4300 (should
4301 (= 1
4302 (length
4303 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4304 (org-export-collect-headlines info)))))
4305 (should
4306 (= 1
4307 (length
4308 (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
4309 (org-export-collect-headlines info 2)))))
4310 ;; Collect headlines locally.
4311 (should
4312 (= 2
4313 (org-test-with-parsed-data "* H1\n** H2\n** H3"
4314 (let ((scope (org-element-map tree 'headline #'identity info t)))
4315 (length (org-export-collect-headlines info nil scope))))))
4316 ;; When collecting locally, optional level is relative.
4317 (should
4318 (= 1
4319 (org-test-with-parsed-data "* H1\n** H2\n*** H3"
4320 (let ((scope (org-element-map tree 'headline #'identity info t)))
4321 (length (org-export-collect-headlines info 1 scope)))))))
4325 ;;; Templates
4327 (ert-deftest test-org-export/inner-template ()
4328 "Test `inner-template' translator specifications."
4329 (should
4330 (equal "Success!"
4331 (org-test-with-temp-text "* Headline"
4332 (org-export-as
4333 (org-export-create-backend
4334 :transcoders
4335 '((inner-template . (lambda (contents info) "Success!"))
4336 (headline . (lambda (h c i) "Headline"))))))))
4337 ;; Inner template is applied even in a "body-only" export.
4338 (should
4339 (equal "Success!"
4340 (org-test-with-temp-text "* Headline"
4341 (org-export-as
4342 (org-export-create-backend
4343 :transcoders '((inner-template . (lambda (c i) "Success!"))
4344 (headline . (lambda (h c i) "Headline"))))
4345 nil nil 'body-only)))))
4347 (ert-deftest test-org-export/template ()
4348 "Test `template' translator specifications."
4349 (should
4350 (equal "Success!"
4351 (org-test-with-temp-text "* Headline"
4352 (org-export-as
4353 (org-export-create-backend
4354 :transcoders '((template . (lambda (contents info) "Success!"))
4355 (headline . (lambda (h c i) "Headline"))))))))
4356 ;; Template is not applied in a "body-only" export.
4357 (should-not
4358 (equal "Success!"
4359 (org-test-with-temp-text "* Headline"
4360 (org-export-as
4361 (org-export-create-backend
4362 :transcoders '((template . (lambda (contents info) "Success!"))
4363 (headline . (lambda (h c i) "Headline"))))
4364 nil nil 'body-only)))))
4368 ;;; Topology
4370 (ert-deftest test-org-export/get-next-element ()
4371 "Test `org-export-get-next-element' specifications."
4372 ;; Standard test.
4373 (should
4374 (equal "b"
4375 (org-test-with-parsed-data "* Headline\n*a* b"
4376 (org-export-get-next-element
4377 (org-element-map tree 'bold 'identity info t) info))))
4378 ;; Return nil when no previous element.
4379 (should-not
4380 (org-test-with-parsed-data "* Headline\na *b*"
4381 (org-export-get-next-element
4382 (org-element-map tree 'bold 'identity info t) info)))
4383 ;; Non-exportable elements are ignored.
4384 (should-not
4385 (let ((org-export-with-timestamps nil))
4386 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
4387 (org-export-get-next-element
4388 (org-element-map tree 'entity 'identity info t) info))))
4389 ;; Find next element in secondary strings.
4390 (should
4391 (eq 'verbatim
4392 (org-test-with-parsed-data "* a =verb="
4393 (org-element-type
4394 (org-export-get-next-element
4395 (org-element-map tree 'plain-text 'identity info t) info)))))
4396 (should
4397 (eq 'verbatim
4398 (org-test-with-parsed-data "* /italic/ =verb="
4399 (org-element-type
4400 (org-export-get-next-element
4401 (org-element-map tree 'italic 'identity info t) info)))))
4402 ;; Find next element in document keywords.
4403 (should
4404 (eq 'verbatim
4405 (org-test-with-parsed-data "#+TITLE: a =verb="
4406 (org-element-type
4407 (org-export-get-next-element
4408 (org-element-map
4409 (plist-get info :title) 'plain-text 'identity info t) info)))))
4410 ;; Find next element in parsed affiliated keywords.
4411 (should
4412 (eq 'verbatim
4413 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
4414 (org-element-type
4415 (org-export-get-next-element
4416 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4417 ;; With optional argument N, return a list containing all the
4418 ;; following elements.
4419 (should
4420 (equal
4421 '(bold code underline)
4422 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4423 (mapcar 'car
4424 (org-export-get-next-element
4425 (org-element-map tree 'italic 'identity info t) info t)))))
4426 ;; When N is a positive integer, return a list containing up to
4427 ;; N following elements.
4428 (should
4429 (equal
4430 '(bold code)
4431 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
4432 (mapcar 'car
4433 (org-export-get-next-element
4434 (org-element-map tree 'italic 'identity info t) info 2))))))
4436 (ert-deftest test-org-export/get-previous-element ()
4437 "Test `org-export-get-previous-element' specifications."
4438 ;; Standard test.
4439 (should
4440 (equal "a "
4441 (org-test-with-parsed-data "* Headline\na *b*"
4442 (org-export-get-previous-element
4443 (org-element-map tree 'bold 'identity info t) info))))
4444 ;; Return nil when no previous element.
4445 (should-not
4446 (org-test-with-parsed-data "* Headline\n*a* b"
4447 (org-export-get-previous-element
4448 (org-element-map tree 'bold 'identity info t) info)))
4449 ;; Non-exportable elements are ignored.
4450 (should-not
4451 (let ((org-export-with-timestamps nil))
4452 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
4453 (org-export-get-previous-element
4454 (org-element-map tree 'entity 'identity info t) info))))
4455 ;; Find previous element in secondary strings.
4456 (should
4457 (eq 'verbatim
4458 (org-test-with-parsed-data "* =verb= a"
4459 (org-element-type
4460 (org-export-get-previous-element
4461 (org-element-map tree 'plain-text 'identity info t) info)))))
4462 (should
4463 (eq 'verbatim
4464 (org-test-with-parsed-data "* =verb= /italic/"
4465 (org-element-type
4466 (org-export-get-previous-element
4467 (org-element-map tree 'italic 'identity info t) info)))))
4468 ;; Find previous element in document keywords.
4469 (should
4470 (eq 'verbatim
4471 (org-test-with-parsed-data "#+TITLE: =verb= a"
4472 (org-element-type
4473 (org-export-get-previous-element
4474 (org-element-map
4475 (plist-get info :title) 'plain-text 'identity info t) info)))))
4476 ;; Find previous element in parsed affiliated keywords.
4477 (should
4478 (eq 'verbatim
4479 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
4480 (org-element-type
4481 (org-export-get-previous-element
4482 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
4483 ;; With optional argument N, return a list containing up to
4484 ;; N previous elements.
4485 (should
4486 (equal '(underline italic bold)
4487 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4488 (mapcar 'car
4489 (org-export-get-previous-element
4490 (org-element-map tree 'code 'identity info t) info t)))))
4491 ;; When N is a positive integer, return a list containing up to
4492 ;; N previous elements.
4493 (should
4494 (equal '(italic bold)
4495 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
4496 (mapcar 'car
4497 (org-export-get-previous-element
4498 (org-element-map tree 'code 'identity info t) info 2))))))
4501 (provide 'test-ox)
4502 ;;; test-org-export.el end here