876829ff1878ff258fb5b3a565b525055aa6ad81
[org-mode.git] / testing / lisp / test-ox.el
blob876829ff1878ff258fb5b3a565b525055aa6ad81
1 ;;; test-ox.el --- Tests for ox.el
3 ;; Copyright (C) 2012, 2013 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
7 ;; This file is not part of GNU Emacs.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Code:
24 (unless (featurep 'ox)
25 (signal 'missing-test-dependency "org-export"))
27 (defmacro org-test-with-backend (backend &rest body)
28 "Execute body with an export back-end defined.
30 BACKEND is the name of the back-end. BODY is the body to
31 execute. The defined back-end simply returns parsed data as Org
32 syntax."
33 (declare (debug (form body)) (indent 1))
34 `(let ((org-export-registered-backends
35 ',(list
36 (list backend
37 :translate-alist
38 (let (transcode-table)
39 (dolist (type (append org-element-all-elements
40 org-element-all-objects)
41 transcode-table)
42 (push
43 (cons type
44 (lambda (obj contents info)
45 (funcall
46 (intern (format "org-element-%s-interpreter"
47 type))
48 obj contents)))
49 transcode-table)))))))
50 (progn ,@body)))
52 (defmacro org-test-with-parsed-data (data &rest body)
53 "Execute body with parsed data available.
55 DATA is a string containing the data to be parsed. BODY is the
56 body to execute. Parse tree is available under the `tree'
57 variable, and communication channel under `info'.
59 This function calls `org-export-collect-tree-properties'. As
60 such, `:ignore-list' (for `org-element-map') and
61 `:parse-tree' (for `org-export-get-genealogy') properties are
62 already filled in `info'."
63 (declare (debug (form body)) (indent 1))
64 `(org-test-with-temp-text ,data
65 (let* ((tree (org-element-parse-buffer))
66 (info (org-export-collect-tree-properties
67 tree (org-export-get-environment))))
68 ,@body)))
72 ;;; Internal Tests
74 (ert-deftest test-org-export/bind-keyword ()
75 "Test reading #+BIND: keywords."
76 ;; Test with `org-export-allow-bind-keywords' set to t.
77 (should
78 (org-test-with-temp-text "#+BIND: variable value"
79 (let ((org-export-allow-bind-keywords t))
80 (org-export--install-letbind-maybe)
81 (eq variable 'value))))
82 ;; Test with `org-export-allow-bind-keywords' set to nil.
83 (should-not
84 (org-test-with-temp-text "#+BIND: variable value"
85 (let ((org-export-allow-bind-keywords nil))
86 (org-export--install-letbind-maybe)
87 (boundp 'variable))))
88 ;; BIND keywords are case-insensitive.
89 (should
90 (org-test-with-temp-text "#+bind: variable value"
91 (let ((org-export-allow-bind-keywords t))
92 (org-export--install-letbind-maybe)
93 (eq variable 'value)))))
95 (ert-deftest test-org-export/parse-option-keyword ()
96 "Test reading all standard #+OPTIONS: items."
97 (should
98 (equal
99 (org-export--parse-option-keyword
100 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
101 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
102 stat:t")
103 '(:headline-levels
104 1 :preserve-breaks t :section-numbers t :time-stamp-file t
105 :with-archived-trees t :with-author t :with-creator t :with-drawers t
106 :with-email t :with-emphasize t :with-entities t :with-fixed-width t
107 :with-footnotes t :with-inlinetasks nil :with-priority t
108 :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
109 :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
110 :with-todo-keywords t)))
111 ;; Test some special values.
112 (should
113 (equal
114 (org-export--parse-option-keyword
115 "arch:headline creator:comment d:(\"TEST\")
116 ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
117 '( :section-numbers
119 :with-archived-trees headline :with-creator comment
120 :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
121 :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
123 (ert-deftest test-org-export/get-inbuffer-options ()
124 "Test reading all standard export keywords."
125 (should
126 (equal
127 (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
128 #+CREATOR: Idem
129 #+DATE: Today
130 #+DESCRIPTION: Testing
131 #+DESCRIPTION: with two lines
132 #+EMAIL: some@email.org
133 #+EXCLUDE_TAGS: noexport invisible
134 #+KEYWORDS: test
135 #+LANGUAGE: en
136 #+SELECT_TAGS: export
137 #+TITLE: Some title
138 #+TITLE: with spaces"
139 (org-export--get-inbuffer-options))
140 '(:author
141 ("Me, Myself and I") :creator "Idem" :date ("Today")
142 :description "Testing\nwith two lines" :email "some@email.org"
143 :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
144 :select-tags ("export") :title ("Some title with spaces")))))
146 (ert-deftest test-org-export/get-subtree-options ()
147 "Test setting options from headline's properties."
148 ;; EXPORT_TITLE.
149 (org-test-with-temp-text "#+TITLE: Title
150 * Headline
151 :PROPERTIES:
152 :EXPORT_TITLE: Subtree Title
153 :END:
154 Paragraph"
155 (forward-line)
156 (should (equal (plist-get (org-export-get-environment nil t) :title)
157 '("Subtree Title"))))
158 :title
159 '("subtree-title")
160 ;; EXPORT_OPTIONS.
161 (org-test-with-temp-text "#+OPTIONS: H:1
162 * Headline
163 :PROPERTIES:
164 :EXPORT_OPTIONS: H:2
165 :END:
166 Paragraph"
167 (forward-line)
168 (should
169 (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
170 ;; EXPORT_DATE.
171 (org-test-with-temp-text "#+DATE: today
172 * Headline
173 :PROPERTIES:
174 :EXPORT_DATE: 29-03-2012
175 :END:
176 Paragraph"
177 (forward-line)
178 (should (equal (plist-get (org-export-get-environment nil t) :date)
179 '("29-03-2012"))))
180 ;; Properties with `split' behaviour are stored as a list of
181 ;; strings.
182 (should
183 (equal '("a" "b")
184 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
185 * Headline
186 :PROPERTIES:
187 :EXPORT_EXCLUDE_TAGS: a b
188 :END:
189 Paragraph"
190 (progn
191 (forward-line)
192 (plist-get (org-export-get-environment nil t) :exclude-tags)))))
193 ;; Handle :PROPERTY+: syntax.
194 (should
195 (equal '("a" "b")
196 (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
197 * Headline
198 :PROPERTIES:
199 :EXPORT_EXCLUDE_TAGS: a
200 :EXPORT_EXCLUDE_TAGS+: b
201 :END:
202 Paragraph"
203 (progn
204 (forward-line)
205 (plist-get (org-export-get-environment nil t) :exclude-tags)))))
206 ;; Export properties are case-insensitive.
207 (org-test-with-temp-text "* Headline
208 :PROPERTIES:
209 :EXPORT_Date: 29-03-2012
210 :END:
211 Paragraph"
212 (should (equal (plist-get (org-export-get-environment nil t) :date)
213 '("29-03-2012"))))
214 ;; Still grab correct options when section above is empty.
215 (should
216 (equal '("H1")
217 (org-test-with-temp-text "* H1\n** H11\n** H12"
218 (progn (forward-line 2)
219 (plist-get (org-export-get-environment nil t) :title))))))
221 (ert-deftest test-org-export/handle-options ()
222 "Test if export options have an impact on output."
223 ;; Test exclude tags.
224 (org-test-with-temp-text "* Head1 :noexport:"
225 (org-test-with-backend test
226 (should
227 (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
228 ""))))
229 ;; Test include tags.
230 (org-test-with-temp-text "
231 * Head1
232 * Head2
233 ** Sub-Head2.1 :export:
234 *** Sub-Head2.1.1
235 * Head2"
236 (org-test-with-backend test
237 (should
238 (equal
239 "* Head2\n** Sub-Head2.1 :export:\n*** Sub-Head2.1.1\n"
240 (let ((org-tags-column 0))
241 (org-export-as 'test nil nil nil '(:select-tags ("export"))))))))
242 ;; Test mixing include tags and exclude tags.
243 (org-test-with-temp-text "
244 * Head1 :export:
245 ** Sub-Head1 :noexport:
246 ** Sub-Head2
247 * Head2 :noexport:
248 ** Sub-Head1 :export:"
249 (org-test-with-backend test
250 (should
251 (string-match
252 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
253 (org-export-as
254 'test nil nil nil
255 '(:select-tags ("export") :exclude-tags ("noexport")))))))
256 ;; Ignore tasks.
257 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
258 (org-test-with-temp-text "* TODO Head1"
259 (org-test-with-backend test
260 (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
261 "")))))
262 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
263 (org-test-with-temp-text "* TODO Head1"
264 (org-test-with-backend test
265 (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
266 "* TODO Head1\n")))))
267 ;; Archived tree.
268 (org-test-with-temp-text "* Head1 :archive:"
269 (let ((org-archive-tag "archive"))
270 (org-test-with-backend test
271 (should
272 (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
273 "")))))
274 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
275 (let ((org-archive-tag "archive"))
276 (org-test-with-backend test
277 (should
278 (string-match
279 "\\* Head1[ \t]+:archive:"
280 (org-export-as 'test nil nil nil
281 '(:with-archived-trees headline)))))))
282 (org-test-with-temp-text "* Head1 :archive:"
283 (let ((org-archive-tag "archive"))
284 (org-test-with-backend test
285 (should
286 (string-match
287 "\\`\\* Head1[ \t]+:archive:\n\\'"
288 (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
289 ;; Clocks.
290 (let ((org-clock-string "CLOCK:"))
291 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
292 (org-test-with-backend test
293 (should
294 (equal (org-export-as 'test nil nil nil '(:with-clocks t))
295 "CLOCK: [2012-04-29 sun. 10:45]\n"))
296 (should
297 (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
298 ;; Drawers.
299 (let ((org-drawers '("TEST")))
300 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
301 (org-test-with-backend test
302 (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
303 ""))
304 (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
305 ":TEST:\ncontents\n:END:\n")))))
306 (let ((org-drawers '("FOO" "BAR")))
307 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
308 (org-test-with-backend test
309 (should
310 (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
311 ":FOO:\nkeep\n:END:\n")))))
312 (let ((org-drawers '("FOO" "BAR")))
313 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
314 (org-test-with-backend test
315 (should
316 (equal (org-export-as 'test nil nil nil '(:with-drawers (not "BAR")))
317 ":FOO:\nkeep\n:END:\n")))))
318 ;; Inlinetasks.
319 (when (featurep 'org-inlinetask)
320 (should
321 (equal
322 (let ((org-inlinetask-min-level 15))
323 (org-test-with-temp-text "*************** Task"
324 (org-test-with-backend test
325 (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
326 ""))
327 (should
328 (equal
329 (let ((org-inlinetask-min-level 15))
330 (org-test-with-temp-text
331 "*************** Task\nContents\n*************** END"
332 (org-test-with-backend test
333 (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
334 "")))
335 ;; Plannings.
336 (let ((org-closed-string "CLOSED:"))
337 (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
338 (org-test-with-backend test
339 (should
340 (equal (org-export-as 'test nil nil nil '(:with-plannings t))
341 "CLOSED: [2012-04-29 sun. 10:45]\n"))
342 (should
343 (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
344 "")))))
345 ;; Statistics cookies.
346 (should
347 (equal ""
348 (org-test-with-temp-text "[0/0]"
349 (org-test-with-backend test
350 (org-export-as
351 'test nil nil nil '(:with-statistics-cookies nil))))))
352 ;; Timestamps.
353 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
354 (org-test-with-backend test
355 (should
356 (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
357 "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
358 (should
359 (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
360 (should
361 (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
362 "<2012-04-29 sun. 10:45>\n"))
363 (should
364 (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
365 "[2012-04-29 sun. 10:45]\n")))))
367 (ert-deftest test-org-export/comment-tree ()
368 "Test if export process ignores commented trees."
369 (let ((org-comment-string "COMMENT"))
370 (org-test-with-temp-text "* COMMENT Head1"
371 (org-test-with-backend test
372 (should (equal (org-export-as 'test) ""))))))
374 (ert-deftest test-org-export/export-scope ()
375 "Test all export scopes."
376 (org-test-with-temp-text "
377 * Head1
378 ** Head2
379 text
380 *** Head3"
381 (org-test-with-backend test
382 ;; Subtree.
383 (forward-line 3)
384 (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
385 ;; Visible.
386 (goto-char (point-min))
387 (forward-line)
388 (org-cycle)
389 (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
390 ;; Region.
391 (goto-char (point-min))
392 (forward-line 3)
393 (transient-mark-mode 1)
394 (push-mark (point) t t)
395 (goto-char (point-at-eol))
396 (should (equal (org-export-as 'test) "text\n"))))
397 ;; Subtree with a code block calling another block outside.
398 (should
399 (equal ": 3\n"
400 (org-test-with-temp-text "
401 * Head1
402 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
403 <<test>>
404 #+END_SRC
405 * Head2
406 #+NAME: test
407 #+BEGIN_SRC emacs-lisp
408 \(+ 1 2)
409 #+END_SRC"
410 (org-test-with-backend test
411 (forward-line 1)
412 (org-export-as 'test 'subtree)))))
413 ;; Body only.
414 (org-test-with-temp-text "Text"
415 (org-test-with-backend test
416 (plist-put
417 (cdr (assq 'test org-export-registered-backends))
418 :translate-alist
419 (cons (cons 'template (lambda (body info) (format "BEGIN\n%sEND" body)))
420 (org-export-backend-translate-table 'test)))
421 (should (equal (org-export-as 'test nil nil 'body-only) "Text\n"))
422 (should (equal (org-export-as 'test) "BEGIN\nText\nEND")))))
424 (ert-deftest test-org-export/output-file-name ()
425 "Test `org-export-output-file-name' specifications."
426 ;; Export from a file: name is built from original file name.
427 (should
428 (org-test-with-temp-text-in-file "Test"
429 (equal (concat (file-name-as-directory ".")
430 (file-name-nondirectory
431 (file-name-sans-extension (buffer-file-name))))
432 (file-name-sans-extension (org-export-output-file-name ".ext")))))
433 ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
434 (should
435 (org-test-with-temp-text-in-file
436 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
437 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
438 ;; From a buffer not associated to a file, too.
439 (should
440 (org-test-with-temp-text
441 "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
442 (equal (org-export-output-file-name ".ext" t) "./test.ext")))
443 ;; When provided name is absolute, preserve it.
444 (should
445 (org-test-with-temp-text
446 (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
447 (expand-file-name "test"))
448 (file-name-absolute-p (org-export-output-file-name ".ext" t))))
449 ;; When PUB-DIR argument is provided, use it.
450 (should
451 (org-test-with-temp-text-in-file "Test"
452 (equal (file-name-directory
453 (org-export-output-file-name ".ext" nil "dir/"))
454 "dir/")))
455 ;; When returned name would overwrite original file, add EXTENSION
456 ;; another time.
457 (should
458 (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
459 (equal (org-export-output-file-name ".org") "./normal.org.org"))))
461 (ert-deftest test-org-export/expand-include ()
462 "Test file inclusion in an Org buffer."
463 ;; Error when file isn't specified.
464 (should-error
465 (org-test-with-temp-text "#+INCLUDE: dummy.org"
466 (org-export-expand-include-keyword)))
467 ;; Full insertion with recursive inclusion.
468 (org-test-with-temp-text
469 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
470 (org-export-expand-include-keyword)
471 (should (equal (buffer-string)
472 "Small Org file with an include keyword.
474 #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
476 Success!
478 * Heading
479 body\n")))
480 ;; Localized insertion.
481 (org-test-with-temp-text
482 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
483 org-test-dir)
484 (org-export-expand-include-keyword)
485 (should (equal (buffer-string)
486 "Small Org file with an include keyword.\n")))
487 ;; Insertion with constraints on headlines level.
488 (should
489 (equal
490 "* Top heading\n** Heading\nbody\n"
491 (org-test-with-temp-text
492 (format
493 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
494 org-test-dir)
495 (org-export-expand-include-keyword)
496 (buffer-string))))
497 (should
498 (equal
499 "* Top heading\n* Heading\nbody\n"
500 (org-test-with-temp-text
501 (format
502 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\" :minlevel 1"
503 org-test-dir)
504 (org-export-expand-include-keyword)
505 (buffer-string))))
506 ;; Inclusion within an example block.
507 (org-test-with-temp-text
508 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
509 org-test-dir)
510 (org-export-expand-include-keyword)
511 (should
512 (equal
513 (buffer-string)
514 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
515 ;; Inclusion within a src-block.
516 (org-test-with-temp-text
517 (format
518 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
519 org-test-dir)
520 (org-export-expand-include-keyword)
521 (should (equal (buffer-string)
522 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
524 (ert-deftest test-org-export/expand-macro ()
525 "Test macro expansion in an Org buffer."
526 ;; Standard macro expansion.
527 (should
528 (equal "#+MACRO: macro1 value\nvalue\n"
529 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
530 (org-test-with-backend test (org-export-as 'test)))))
531 ;; Expand specific macros.
532 (should
533 (equal "me 2012-03-29 me@here Title\n"
534 (org-test-with-temp-text
536 #+TITLE: Title
537 #+DATE: 2012-03-29
538 #+AUTHOR: me
539 #+EMAIL: me@here
540 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
541 (let ((output (org-test-with-backend test (org-export-as 'test))))
542 (substring output (string-match ".*\n\\'" output))))))
543 ;; Expand specific macros when property contained a regular macro
544 ;; already.
545 (should
546 (equal "value\n"
547 (org-test-with-temp-text "
548 #+MACRO: macro1 value
549 #+TITLE: {{{macro1}}}
550 {{{title}}}"
551 (let ((output (org-test-with-backend test (org-export-as 'test))))
552 (substring output (string-match ".*\n\\'" output))))))
553 ;; Expand macros with templates in included files.
554 (should
555 (equal "success\n"
556 (org-test-with-temp-text
557 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
558 {{{included-macro}}}" org-test-dir)
559 (let ((output (org-test-with-backend test (org-export-as 'test))))
560 (substring output (string-match ".*\n\\'" output)))))))
562 (ert-deftest test-org-export/user-ignore-list ()
563 "Test if `:ignore-list' accepts user input."
564 (org-test-with-backend test
565 (flet ((skip-note-head
566 (data backend info)
567 ;; Ignore headlines with the word "note" in their title.
568 (org-element-map
569 data 'headline
570 (lambda (headline)
571 (when (string-match "\\<note\\>"
572 (org-element-property :raw-value headline))
573 (org-export-ignore-element headline info)))
574 info)
575 data))
576 ;; Install function in parse tree filters.
577 (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
578 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
579 (should (equal (org-export-as 'test) "* Head1\n")))))))
581 (ert-deftest test-org-export/before-processing-hook ()
582 "Test `org-export-before-processing-hook'."
583 (should
584 (equal
585 "#+MACRO: mac val\nTest\n"
586 (org-test-with-backend test
587 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
588 (let ((org-export-before-processing-hook
589 '((lambda (backend)
590 (while (re-search-forward "{{{" nil t)
591 (let ((object (org-element-context)))
592 (when (eq (org-element-type object) 'macro)
593 (delete-region
594 (org-element-property :begin object)
595 (org-element-property :end object)))))))))
596 (org-export-as 'test)))))))
598 (ert-deftest test-org-export/before-parsing-hook ()
599 "Test `org-export-before-parsing-hook'."
600 (should
601 (equal "Body 1\nBody 2\n"
602 (org-test-with-backend test
603 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
604 (let ((org-export-before-parsing-hook
605 '((lambda (backend)
606 (goto-char (point-min))
607 (while (re-search-forward org-outline-regexp-bol nil t)
608 (delete-region
609 (point-at-bol) (progn (forward-line) (point))))))))
610 (org-export-as 'test)))))))
614 ;;; Affiliated Keywords
616 (ert-deftest test-org-export/read-attribute ()
617 "Test `org-export-read-attribute' specifications."
618 ;; Standard test.
619 (should
620 (equal
621 (org-export-read-attribute
622 :attr_html
623 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
624 (org-element-at-point)))
625 '(:a 1 :b 2)))
626 ;; Return nil on empty attribute.
627 (should-not
628 (org-export-read-attribute
629 :attr_html
630 (org-test-with-temp-text "Paragraph" (org-element-at-point)))))
632 (ert-deftest test-org-export/get-caption ()
633 "Test `org-export-get-caption' specifications."
634 ;; Without optional argument, return long caption
635 (should
636 (equal
637 '("l")
638 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
639 (org-export-get-caption (org-element-at-point)))))
640 ;; With optional argument, return short caption.
641 (should
642 (equal
643 '("s")
644 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
645 (org-export-get-caption (org-element-at-point) t))))
646 ;; Multiple lines are separated by white spaces.
647 (should
648 (equal
649 '("a" " " "b")
650 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
651 (org-export-get-caption (org-element-at-point))))))
655 ;;; Back-End Tools
657 (ert-deftest test-org-export/define-backend ()
658 "Test back-end definition and accessors."
659 ;; Translate table.
660 (should
661 (equal '((headline . my-headline-test))
662 (let (org-export-registered-backends)
663 (org-export-define-backend test ((headline . my-headline-test)))
664 (org-export-backend-translate-table 'test))))
665 ;; Filters.
666 (should
667 (equal '((:filter-headline . my-filter))
668 (let (org-export-registered-backends)
669 (org-export-define-backend test
670 ((headline . my-headline-test))
671 :filters-alist ((:filter-headline . my-filter)))
672 (org-export-backend-filters 'test))))
673 ;; Options.
674 (should
675 (equal '((:prop value))
676 (let (org-export-registered-backends)
677 (org-export-define-backend test
678 ((headline . my-headline-test))
679 :options-alist ((:prop value)))
680 (org-export-backend-options 'test))))
681 ;; Menu.
682 (should
683 (equal '(?k "Test Export" test)
684 (let (org-export-registered-backends)
685 (org-export-define-backend test
686 ((headline . my-headline-test))
687 :menu-entry (?k "Test Export" test))
688 (org-export-backend-menu 'test))))
689 ;; Export Blocks.
690 (should
691 (equal '(("TEST" . org-element-export-block-parser))
692 (let (org-export-registered-backends org-element-block-name-alist)
693 (org-export-define-backend test
694 ((headline . my-headline-test))
695 :export-block ("test"))
696 org-element-block-name-alist))))
698 (ert-deftest test-org-export/define-derived-backend ()
699 "Test `org-export-define-derived-backend' specifications."
700 ;; Error when parent back-end is not defined.
701 (should-error
702 (let (org-export-registered-backends)
703 (org-export-define-derived-backend test parent)))
704 ;; Append translation table to parent's.
705 (should
706 (equal '((:headline . test) (:headline . parent))
707 (let (org-export-registered-backends)
708 (org-export-define-backend parent ((:headline . parent)))
709 (org-export-define-derived-backend test parent
710 :translate-alist ((:headline . test)))
711 (org-export-backend-translate-table 'test))))
712 ;; Options defined in the new back have priority over those defined
713 ;; in parent.
714 (should
715 (eq 'test
716 (let (org-export-registered-backends)
717 (org-export-define-backend parent
718 ((:headline . parent))
719 :options-alist ((:a nil nil 'parent)))
720 (org-export-define-derived-backend test parent
721 :options-alist ((:a nil nil 'test)))
722 (plist-get (org-export--get-global-options 'test) :a)))))
724 (ert-deftest test-org-export/derived-backend-p ()
725 "Test `org-export-derived-backend-p' specifications."
726 ;; Non-nil with direct match.
727 (should
728 (let (org-export-registered-backends)
729 (org-export-define-backend test ((headline . test)))
730 (org-export-derived-backend-p 'test 'test)))
731 (should
732 (let (org-export-registered-backends)
733 (org-export-define-backend test ((headline . test)))
734 (org-export-define-derived-backend test2 test)
735 (org-export-derived-backend-p 'test2 'test2)))
736 ;; Non-nil with a direct parent.
737 (should
738 (let (org-export-registered-backends)
739 (org-export-define-backend test ((headline . test)))
740 (org-export-define-derived-backend test2 test)
741 (org-export-derived-backend-p 'test2 'test)))
742 ;; Non-nil with an indirect parent.
743 (should
744 (let (org-export-registered-backends)
745 (org-export-define-backend test ((headline . test)))
746 (org-export-define-derived-backend test2 test)
747 (org-export-define-derived-backend test3 test2)
748 (org-export-derived-backend-p 'test3 'test)))
749 ;; Nil otherwise.
750 (should-not
751 (let (org-export-registered-backends)
752 (org-export-define-backend test ((headline . test)))
753 (org-export-define-backend test2 ((headline . test2)))
754 (org-export-derived-backend-p 'test2 'test)))
755 (should-not
756 (let (org-export-registered-backends)
757 (org-export-define-backend test ((headline . test)))
758 (org-export-define-backend test2 ((headline . test2)))
759 (org-export-define-derived-backend test3 test2)
760 (org-export-derived-backend-p 'test3 'test))))
762 (ert-deftest test-org-export/with-backend ()
763 "Test `org-export-with-backend' definition."
764 ;; Error when calling an undefined back-end
765 (should-error
766 (let (org-export-registered-backends)
767 (org-export-with-backend 'test "Test")))
768 ;; Error when called back-end doesn't have an appropriate
769 ;; transcoder.
770 (should-error
771 (let (org-export-registered-backends)
772 (org-export-define-backend test ((headline . ignore)))
773 (org-export-with-backend 'test "Test")))
774 ;; Otherwise, export using correct transcoder
775 (should
776 (equal "Success"
777 (let (org-export-registered-backends)
778 (org-export-define-backend test
779 ((plain-text . (lambda (text contents info) "Failure"))))
780 (org-export-define-backend test2
781 ((plain-text . (lambda (text contents info) "Success"))))
782 (org-export-with-backend 'test2 "Test")))))
784 (ert-deftest test-org-export/data-with-translations ()
785 "Test `org-export-data-with-translations' specifications."
786 (should
787 (equal
788 "Success!"
789 (org-export-data-with-translations
790 '(bold nil "Test")
791 '((plain-text . (lambda (text info) "Success"))
792 (bold . (lambda (bold contents info) (concat contents "!"))))
793 '(:with-emphasize t)))))
795 (ert-deftest test-org-export/data-with-backend ()
796 "Test `org-export-data-with-backend' specifications."
797 ;; Error when calling an undefined back-end.
798 (should-error
799 (let (org-export-registered-backends)
800 (org-export-data-with-backend 'test "Test" nil)))
801 ;; Otherwise, export data recursively, using correct back-end.
802 (should
803 (equal
804 "Success!"
805 (let (org-export-registered-backends)
806 (org-export-define-backend test
807 ((plain-text . (lambda (text info) "Success"))
808 (bold . (lambda (bold contents info) (concat contents "!")))))
809 (org-export-data-with-backend
810 '(bold nil "Test") 'test '(:with-emphasize t))))))
814 ;;; Export Snippets
816 (ert-deftest test-org-export/export-snippet ()
817 "Test export snippets transcoding."
818 (org-test-with-temp-text "@@test:A@@@@t:B@@"
819 (org-test-with-backend test
820 (plist-put
821 (cdr (assq 'test org-export-registered-backends))
822 :translate-alist
823 (cons (cons 'export-snippet
824 (lambda (snippet contents info)
825 (when (eq (org-export-snippet-backend snippet) 'test)
826 (org-element-property :value snippet))))
827 (org-export-backend-translate-table 'test)))
828 (let ((org-export-snippet-translation-alist nil))
829 (should (equal (org-export-as 'test) "A\n")))
830 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
831 (should (equal (org-export-as 'test) "AB\n"))))))
835 ;;; Footnotes
837 (ert-deftest test-org-export/footnotes ()
838 "Test footnotes specifications."
839 (let ((org-footnote-section nil)
840 (org-export-with-footnotes t))
841 ;; 1. Read every type of footnote.
842 (should
843 (equal
844 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
845 (org-test-with-parsed-data
846 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
847 (org-element-map
848 tree 'footnote-reference
849 (lambda (ref)
850 (let ((def (org-export-get-footnote-definition ref info)))
851 (cons (org-export-get-footnote-number ref info)
852 (if (eq (org-element-property :type ref) 'inline) (car def)
853 (car (org-element-contents
854 (car (org-element-contents def))))))))
855 info))))
856 ;; 2. Test nested footnotes order.
857 (org-test-with-parsed-data
858 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
859 (should
860 (equal
861 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
862 (org-element-map
863 tree 'footnote-reference
864 (lambda (ref)
865 (when (org-export-footnote-first-reference-p ref info)
866 (cons (org-export-get-footnote-number ref info)
867 (org-element-property :label ref))))
868 info))))
869 ;; 3. Test nested footnote in invisible definitions.
870 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
871 ;; Hide definitions.
872 (narrow-to-region (point) (point-at-eol))
873 (let* ((tree (org-element-parse-buffer))
874 (info (org-combine-plists
875 `(:parse-tree ,tree)
876 (org-export-collect-tree-properties
877 tree (org-export-get-environment)))))
878 ;; Both footnotes should be seen.
879 (should
880 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
881 ;; 4. Test footnotes definitions collection.
882 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
884 \[fn:2] B [fn:3] [fn::D].
886 \[fn:3] C."
887 (should (= (length (org-export-collect-footnote-definitions tree info))
888 4)))
889 ;; 5. Test export of footnotes defined outside parsing scope.
890 (org-test-with-temp-text "[fn:1] Out of scope
891 * Title
892 Paragraph[fn:1]"
893 (org-test-with-backend test
894 (plist-put
895 (cdr (assq 'test org-export-registered-backends))
896 :translate-alist
897 (cons (cons 'footnote-reference
898 (lambda (fn contents info)
899 (org-element-interpret-data
900 (org-export-get-footnote-definition fn info))))
901 (org-export-backend-translate-table 'test)))
902 (forward-line)
903 (should (equal "ParagraphOut of scope\n"
904 (org-export-as 'test 'subtree)))))
905 ;; 6. Footnotes without a definition should be provided a fallback
906 ;; definition.
907 (should
908 (org-test-with-parsed-data "[fn:1]"
909 (org-export-get-footnote-definition
910 (org-element-map tree 'footnote-reference 'identity info t) info)))
911 ;; 7. Footnote section should be ignored in TOC and in headlines
912 ;; numbering.
913 (should
914 (= 1 (let ((org-footnote-section "Footnotes"))
915 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
916 (org-export-collect-headlines info))))))
917 (should
918 (equal '(2)
919 (let ((org-footnote-section "Footnotes"))
920 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
921 (org-element-map tree 'headline
922 (lambda (hl)
923 (when (equal (org-element-property :raw-value hl) "H2")
924 (org-export-get-headline-number hl info)))
925 info t)))))))
929 ;;; Headlines and Inlinetasks
931 (ert-deftest test-org-export/get-relative-level ()
932 "Test `org-export-get-relative-level' specifications."
933 ;; Standard test.
934 (should
935 (equal '(1 2)
936 (let ((org-odd-levels-only nil))
937 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
938 (org-element-map
939 tree 'headline
940 (lambda (h) (org-export-get-relative-level h info))
941 info)))))
942 ;; Missing levels
943 (should
944 (equal '(1 3)
945 (let ((org-odd-levels-only nil))
946 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
947 (org-element-map
948 tree 'headline
949 (lambda (h) (org-export-get-relative-level h info))
950 info))))))
952 (ert-deftest test-org-export/low-level-p ()
953 "Test `org-export-low-level-p' specifications."
954 (should
955 (equal
956 '(no yes)
957 (let ((org-odd-levels-only nil))
958 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
959 (org-element-map
960 tree 'headline
961 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
962 (plist-put info :headline-levels 1)))))))
964 (ert-deftest test-org-export/get-headline-number ()
965 "Test `org-export-get-headline-number' specifications."
966 ;; Standard test.
967 (should
968 (equal
969 '((1) (1 1))
970 (let ((org-odd-levels-only nil))
971 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
972 (org-element-map
973 tree 'headline
974 (lambda (h) (org-export-get-headline-number h info))
975 info)))))
976 ;; Missing levels are replaced with 0.
977 (should
978 (equal
979 '((1) (1 0 1))
980 (let ((org-odd-levels-only nil))
981 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
982 (org-element-map
983 tree 'headline
984 (lambda (h) (org-export-get-headline-number h info))
985 info))))))
987 (ert-deftest test-org-export/numbered-headline-p ()
988 "Test `org-export-numbered-headline-p' specifications."
989 ;; If `:section-numbers' is nil, never number headlines.
990 (should-not
991 (org-test-with-parsed-data "* Headline"
992 (org-element-map
993 tree 'headline
994 (lambda (h) (org-export-numbered-headline-p h info))
995 (plist-put info :section-numbers nil))))
996 ;; If `:section-numbers' is a number, only number headlines with
997 ;; a level greater that it.
998 (should
999 (equal
1000 '(yes no)
1001 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
1002 (org-element-map
1003 tree 'headline
1004 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
1005 (plist-put info :section-numbers 1)))))
1006 ;; Otherwise, headlines are always numbered.
1007 (should
1008 (org-test-with-parsed-data "* Headline"
1009 (org-element-map
1010 tree 'headline
1011 (lambda (h) (org-export-numbered-headline-p h info))
1012 (plist-put info :section-numbers t)))))
1014 (ert-deftest test-org-export/number-to-roman ()
1015 "Test `org-export-number-to-roman' specifications."
1016 ;; If number is negative, return it as a string.
1017 (should (equal (org-export-number-to-roman -1) "-1"))
1018 ;; Otherwise, return it as a roman number.
1019 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
1021 (ert-deftest test-org-export/get-optional-title ()
1022 "Test `org-export-get-optional-title' specifications."
1023 ;; If OPTIONAL_TITLE property is defined, use it.
1024 (should
1025 (equal '("opt")
1026 (org-test-with-parsed-data
1027 "* Headline\n:PROPERTIES:\n:OPTIONAL_TITLE: opt\n:END:"
1028 (org-export-get-optional-title
1029 (org-element-map tree 'headline 'identity info t)
1030 info))))
1031 ;; Otherwise, fall-back to regular title.
1032 (should
1033 (equal '("Headline")
1034 (org-test-with-parsed-data "* Headline"
1035 (org-export-get-optional-title
1036 (org-element-map tree 'headline 'identity info t)
1037 info)))))
1039 (ert-deftest test-org-export/get-tags ()
1040 "Test `org-export-get-tags' specifications."
1041 (let ((org-export-exclude-tags '("noexport"))
1042 (org-export-select-tags '("export")))
1043 ;; Standard test: tags which are not a select tag, an exclude tag,
1044 ;; or specified as optional argument shouldn't be ignored.
1045 (should
1046 (org-test-with-parsed-data "* Headline :tag:"
1047 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1048 info)))
1049 ;; Exclude tags are removed.
1050 (should-not
1051 (org-test-with-parsed-data "* Headline :noexport:"
1052 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1053 info)))
1054 ;; Select tags are removed.
1055 (should-not
1056 (org-test-with-parsed-data "* Headline :export:"
1057 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1058 info)))
1059 (should
1060 (equal
1061 '("tag")
1062 (org-test-with-parsed-data "* Headline :tag:export:"
1063 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1064 info))))
1065 ;; Tags provided in the optional argument are also ignored.
1066 (should-not
1067 (org-test-with-parsed-data "* Headline :ignore:"
1068 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1069 info '("ignore"))))
1070 ;; Allow tag inheritance.
1071 (should
1072 (equal
1073 '(("tag") ("tag"))
1074 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
1075 (org-element-map
1076 tree 'headline
1077 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
1078 ;; Tag inheritance checks FILETAGS keywords.
1079 (should
1080 (equal
1081 '(("a" "b" "tag"))
1082 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
1083 (org-element-map
1084 tree 'headline
1085 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
1087 (ert-deftest test-org-export/get-node-property ()
1088 "Test`org-export-get-node-property' specifications."
1089 ;; Standard test.
1090 (should
1091 (equal "value"
1092 (org-test-with-parsed-data "* Headline
1093 :PROPERTIES:
1094 :prop: value
1095 :END:"
1096 (org-export-get-node-property
1097 :PROP (org-element-map tree 'headline 'identity nil t)))))
1098 ;; Test inheritance.
1099 (should
1100 (equal "value"
1101 (org-test-with-parsed-data "* Parent
1102 :PROPERTIES:
1103 :prop: value
1104 :END:
1105 ** Headline
1106 Paragraph"
1107 (org-export-get-node-property
1108 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
1109 ;; Cannot return a value before the first headline.
1110 (should-not
1111 (org-test-with-parsed-data "Paragraph
1112 * Headline
1113 :PROPERTIES:
1114 :prop: value
1115 :END:"
1116 (org-export-get-node-property
1117 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
1119 (ert-deftest test-org-export/get-category ()
1120 "Test `org-export-get-category' specifications."
1121 ;; Standard test.
1122 (should
1123 (equal "value"
1124 (org-test-with-parsed-data "* Headline
1125 :PROPERTIES:
1126 :CATEGORY: value
1127 :END:"
1128 (org-export-get-category
1129 (org-element-map tree 'headline 'identity nil t) info))))
1130 ;; Test inheritance from a parent headline.
1131 (should
1132 (equal '("value" "value")
1133 (org-test-with-parsed-data "* Headline1
1134 :PROPERTIES:
1135 :CATEGORY: value
1136 :END:
1137 ** Headline2"
1138 (org-element-map
1139 tree 'headline
1140 (lambda (hl) (org-export-get-category hl info)) info))))
1141 ;; Test inheritance from #+CATEGORY keyword
1142 (should
1143 (equal "value"
1144 (org-test-with-parsed-data "#+CATEGORY: value
1145 * Headline"
1146 (org-export-get-category
1147 (org-element-map tree 'headline 'identity nil t) info))))
1148 ;; Test inheritance from file name.
1149 (should
1150 (equal "test"
1151 (org-test-with-parsed-data "* Headline"
1152 (let ((info (plist-put info :input-file "~/test.org")))
1153 (org-export-get-category
1154 (org-element-map tree 'headline 'identity nil t) info)))))
1155 ;; Fall-back value.
1156 (should
1157 (equal "???"
1158 (org-test-with-parsed-data "* Headline"
1159 (org-export-get-category
1160 (org-element-map tree 'headline 'identity nil t) info)))))
1162 (ert-deftest test-org-export/first-sibling-p ()
1163 "Test `org-export-first-sibling-p' specifications."
1164 ;; Standard test.
1165 (should
1166 (equal
1167 '(yes yes no)
1168 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1169 (org-element-map
1170 tree 'headline
1171 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1172 info))))
1173 ;; Ignore headlines not exported.
1174 (should
1175 (equal
1176 '(yes)
1177 (let ((org-export-exclude-tags '("ignore")))
1178 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
1179 (org-element-map
1180 tree 'headline
1181 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1182 info))))))
1184 (ert-deftest test-org-export/last-sibling-p ()
1185 "Test `org-export-last-sibling-p' specifications."
1186 ;; Standard test.
1187 (should
1188 (equal
1189 '(yes no yes)
1190 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1191 (org-element-map
1192 tree 'headline
1193 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1194 info))))
1195 ;; Ignore headlines not exported.
1196 (should
1197 (equal
1198 '(yes)
1199 (let ((org-export-exclude-tags '("ignore")))
1200 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
1201 (org-element-map
1202 tree 'headline
1203 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1204 info))))))
1208 ;;; Links
1210 (ert-deftest test-org-export/get-coderef-format ()
1211 "Test `org-export-get-coderef-format' specifications."
1212 ;; A link without description returns "%s"
1213 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
1214 "%s"))
1215 ;; Return "%s" when path is matched within description.
1216 (should (equal (org-export-get-coderef-format "path" "desc (path)")
1217 "desc %s"))
1218 ;; Otherwise return description.
1219 (should (equal (org-export-get-coderef-format "path" "desc")
1220 "desc")))
1222 (ert-deftest test-org-export/inline-image-p ()
1223 "Test `org-export-inline-image-p' specifications."
1224 (should
1225 (org-export-inline-image-p
1226 (org-test-with-temp-text "[[#id]]"
1227 (org-element-map
1228 (org-element-parse-buffer) 'link 'identity nil t))
1229 '(("custom-id" . "id")))))
1231 (ert-deftest test-org-export/fuzzy-link ()
1232 "Test fuzzy links specifications."
1233 ;; 1. Links to invisible (keyword) targets should be ignored.
1234 (org-test-with-parsed-data
1235 "Paragraph.\n#+TARGET: Test\n[[Test]]"
1236 (should-not
1237 (org-element-map
1238 tree 'link
1239 (lambda (link)
1240 (org-export-get-ordinal
1241 (org-export-resolve-fuzzy-link link info) info)) info)))
1242 ;; 2. Link to an headline should return headline's number.
1243 (org-test-with-parsed-data
1244 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
1245 (should
1246 ;; Note: Headline's number is in fact a list of numbers.
1247 (equal '(2)
1248 (org-element-map
1249 tree 'link
1250 (lambda (link)
1251 (org-export-get-ordinal
1252 (org-export-resolve-fuzzy-link link info) info)) info t))))
1253 ;; 3. Link to a target in an item should return item's number.
1254 (org-test-with-parsed-data
1255 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
1256 (should
1257 ;; Note: Item's number is in fact a list of numbers.
1258 (equal '(1 2)
1259 (org-element-map
1260 tree 'link
1261 (lambda (link)
1262 (org-export-get-ordinal
1263 (org-export-resolve-fuzzy-link link info) info)) info t))))
1264 ;; 4. Link to a target in a footnote should return footnote's
1265 ;; number.
1266 (org-test-with-parsed-data "
1267 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
1268 (should
1269 (equal '(2 3)
1270 (org-element-map
1271 tree 'link
1272 (lambda (link)
1273 (org-export-get-ordinal
1274 (org-export-resolve-fuzzy-link link info) info)) info))))
1275 ;; 5. Link to a named element should return sequence number of that
1276 ;; element.
1277 (org-test-with-parsed-data
1278 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
1279 (should
1280 (= 2
1281 (org-element-map
1282 tree 'link
1283 (lambda (link)
1284 (org-export-get-ordinal
1285 (org-export-resolve-fuzzy-link link info) info)) info t))))
1286 ;; 6. Link to a target not within an item, a table, a footnote
1287 ;; reference or definition should return section number.
1288 (org-test-with-parsed-data
1289 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
1290 (should
1291 (equal '(2)
1292 (org-element-map
1293 tree 'link
1294 (lambda (link)
1295 (org-export-get-ordinal
1296 (org-export-resolve-fuzzy-link link info) info)) info t))))
1297 ;; 7. Space are not significant when matching a fuzzy link.
1298 (should
1299 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
1300 (org-element-map tree 'link
1301 (lambda (link) (org-export-resolve-fuzzy-link link info))
1302 info t)))
1303 ;; 8. Statistics cookies are ignored for headline match.
1304 (should
1305 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
1306 (org-element-map tree 'link
1307 (lambda (link) (org-export-resolve-fuzzy-link link info))
1308 info t)))
1309 (should
1310 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
1311 (org-element-map tree 'link
1312 (lambda (link) (org-export-resolve-fuzzy-link link info))
1313 info t))))
1315 (ert-deftest test-org-export/resolve-coderef ()
1316 "Test `org-export-resolve-coderef' specifications."
1317 (let ((org-coderef-label-format "(ref:%s)"))
1318 ;; 1. A link to a "-n -k -r" block returns line number.
1319 (org-test-with-parsed-data
1320 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
1321 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1322 (org-test-with-parsed-data
1323 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1324 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1325 ;; 2. A link to a "-n -r" block returns line number.
1326 (org-test-with-parsed-data
1327 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
1328 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1329 (org-test-with-parsed-data
1330 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1331 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1332 ;; 3. A link to a "-n" block returns coderef.
1333 (org-test-with-parsed-data
1334 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1335 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1336 (org-test-with-parsed-data
1337 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
1338 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1339 ;; 4. A link to a "-r" block returns line number.
1340 (org-test-with-parsed-data
1341 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1342 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1343 (org-test-with-parsed-data
1344 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
1345 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1346 ;; 5. A link to a block without a switch returns coderef.
1347 (org-test-with-parsed-data
1348 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1349 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1350 (org-test-with-parsed-data
1351 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
1352 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1353 ;; 6. Correctly handle continued line numbers. A "+n" switch
1354 ;; should resume numbering from previous block with numbered
1355 ;; lines, ignoring blocks not numbering lines in the process.
1356 ;; A "-n" switch resets count.
1357 (org-test-with-parsed-data "
1358 #+BEGIN_EXAMPLE -n
1359 Text.
1360 #+END_EXAMPLE
1362 #+BEGIN_SRC emacs-lisp
1363 \(- 1 1)
1364 #+END_SRC
1366 #+BEGIN_SRC emacs-lisp +n -r
1367 \(+ 1 1) (ref:addition)
1368 #+END_SRC
1370 #+BEGIN_EXAMPLE -n -r
1371 Another text. (ref:text)
1372 #+END_EXAMPLE"
1373 (should (= (org-export-resolve-coderef "addition" info) 2))
1374 (should (= (org-export-resolve-coderef "text" info) 1)))
1375 ;; 7. Recognize coderef with user-specified syntax.
1376 (org-test-with-parsed-data
1377 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
1378 (should (equal (org-export-resolve-coderef "text" info) "text")))))
1380 (ert-deftest test-org-export/resolve-fuzzy-link ()
1381 "Test `org-export-resolve-fuzzy-link' specifications."
1382 ;; 1. Match target objects.
1383 (org-test-with-parsed-data "<<target>> [[target]]"
1384 (should
1385 (org-export-resolve-fuzzy-link
1386 (org-element-map tree 'link 'identity info t) info)))
1387 ;; 2. Match target elements.
1388 (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
1389 (should
1390 (org-export-resolve-fuzzy-link
1391 (org-element-map tree 'link 'identity info t) info)))
1392 ;; 3. Match named elements.
1393 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
1394 (should
1395 (org-export-resolve-fuzzy-link
1396 (org-element-map tree 'link 'identity info t) info)))
1397 ;; 4. Match exact headline's name.
1398 (org-test-with-parsed-data "* My headline\n[[My headline]]"
1399 (should
1400 (org-export-resolve-fuzzy-link
1401 (org-element-map tree 'link 'identity info t) info)))
1402 ;; 5. Targets objects have priority over named elements and headline
1403 ;; titles.
1404 (org-test-with-parsed-data
1405 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
1406 (should
1407 (eq 'target
1408 (org-element-type
1409 (org-export-resolve-fuzzy-link
1410 (org-element-map tree 'link 'identity info t) info)))))
1411 ;; 6. Named elements have priority over headline titles.
1412 (org-test-with-parsed-data
1413 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
1414 (should
1415 (eq 'paragraph
1416 (org-element-type
1417 (org-export-resolve-fuzzy-link
1418 (org-element-map tree 'link 'identity info t) info)))))
1419 ;; 7. If link's path starts with a "*", only match headline titles,
1420 ;; though.
1421 (org-test-with-parsed-data
1422 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
1423 (should
1424 (eq 'headline
1425 (org-element-type
1426 (org-export-resolve-fuzzy-link
1427 (org-element-map tree 'link 'identity info t) info)))))
1428 ;; 8. Return nil if no match.
1429 (org-test-with-parsed-data "[[target]]"
1430 (should-not
1431 (org-export-resolve-fuzzy-link
1432 (org-element-map tree 'link 'identity info t) info))))
1434 (ert-deftest test-org-export/resolve-id-link ()
1435 "Test `org-export-resolve-id-link' specifications."
1436 ;; 1. Regular test for custom-id link.
1437 (org-test-with-parsed-data "* Headline1
1438 :PROPERTIES:
1439 :CUSTOM_ID: test
1440 :END:
1441 * Headline 2
1442 \[[#test]]"
1443 (should
1444 (org-export-resolve-id-link
1445 (org-element-map tree 'link 'identity info t) info)))
1446 ;; 2. Failing test for custom-id link.
1447 (org-test-with-parsed-data "* Headline1
1448 :PROPERTIES:
1449 :CUSTOM_ID: test
1450 :END:
1451 * Headline 2
1452 \[[#no-match]]"
1453 (should-not
1454 (org-export-resolve-id-link
1455 (org-element-map tree 'link 'identity info t) info)))
1456 ;; 3. Test for internal id target.
1457 (org-test-with-parsed-data "* Headline1
1458 :PROPERTIES:
1459 :ID: aaaa
1460 :END:
1461 * Headline 2
1462 \[[id:aaaa]]"
1463 (should
1464 (org-export-resolve-id-link
1465 (org-element-map tree 'link 'identity info t) info)))
1466 ;; 4. Test for external id target.
1467 (org-test-with-parsed-data "[[id:aaaa]]"
1468 (should
1469 (org-export-resolve-id-link
1470 (org-element-map tree 'link 'identity info t)
1471 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
1473 (ert-deftest test-org-export/resolve-radio-link ()
1474 "Test `org-export-resolve-radio-link' specifications."
1475 ;; Standard test.
1476 (should
1477 (org-test-with-temp-text "<<<radio>>> radio"
1478 (org-update-radio-target-regexp)
1479 (let* ((tree (org-element-parse-buffer))
1480 (info `(:parse-tree ,tree)))
1481 (org-export-resolve-radio-link
1482 (org-element-map tree 'link 'identity info t)
1483 info))))
1484 ;; Radio targets are case-insensitive.
1485 (should
1486 (org-test-with-temp-text "<<<RADIO>>> radio"
1487 (org-update-radio-target-regexp)
1488 (let* ((tree (org-element-parse-buffer))
1489 (info `(:parse-tree ,tree)))
1490 (org-export-resolve-radio-link
1491 (org-element-map tree 'link 'identity info t)
1492 info))))
1493 ;; Radio target with objects.
1494 (should
1495 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
1496 (org-update-radio-target-regexp)
1497 (let* ((tree (org-element-parse-buffer))
1498 (info `(:parse-tree ,tree)))
1499 (org-export-resolve-radio-link
1500 (org-element-map tree 'link 'identity info t)
1501 info)))))
1505 ;;; Src-block and example-block
1507 (ert-deftest test-org-export/unravel-code ()
1508 "Test `org-export-unravel-code' function."
1509 (let ((org-coderef-label-format "(ref:%s)"))
1510 ;; 1. Code without reference.
1511 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
1512 (should (equal (org-export-unravel-code (org-element-at-point))
1513 '("(+ 1 1)\n"))))
1514 ;; 2. Code with reference.
1515 (org-test-with-temp-text
1516 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
1517 (should (equal (org-export-unravel-code (org-element-at-point))
1518 '("(+ 1 1)\n" (1 . "test")))))
1519 ;; 3. Code with user-defined reference.
1520 (org-test-with-temp-text
1521 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
1522 (should (equal (org-export-unravel-code (org-element-at-point))
1523 '("(+ 1 1)\n" (1 . "test")))))
1524 ;; 4. Code references keys are relative to the current block.
1525 (org-test-with-temp-text "
1526 #+BEGIN_EXAMPLE -n
1527 \(+ 1 1)
1528 #+END_EXAMPLE
1529 #+BEGIN_EXAMPLE +n
1530 \(+ 2 2)
1531 \(+ 3 3) (ref:one)
1532 #+END_EXAMPLE"
1533 (goto-line 5)
1534 (should (equal (org-export-unravel-code (org-element-at-point))
1535 '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
1539 ;;; Smart Quotes
1541 (ert-deftest test-org-export/activate-smart-quotes ()
1542 "Test `org-export-activate-smart-quotes' specifications."
1543 ;; Opening double quotes: standard test.
1544 (should
1545 (equal
1546 '("some &ldquo;paragraph")
1547 (let ((org-export-default-language "en"))
1548 (org-test-with-parsed-data "some \"paragraph"
1549 (org-element-map
1550 tree 'plain-text
1551 (lambda (s) (org-export-activate-smart-quotes s :html info))
1552 info)))))
1553 ;; Opening quotes: at the beginning of a paragraph.
1554 (should
1555 (equal
1556 '("&ldquo;begin")
1557 (let ((org-export-default-language "en"))
1558 (org-test-with-parsed-data "\"begin"
1559 (org-element-map
1560 tree 'plain-text
1561 (lambda (s) (org-export-activate-smart-quotes s :html info))
1562 info)))))
1563 ;; Opening quotes: after an object.
1564 (should
1565 (equal
1566 '("&ldquo;begin")
1567 (let ((org-export-default-language "en"))
1568 (org-test-with-parsed-data "=verb= \"begin"
1569 (org-element-map
1570 tree 'plain-text
1571 (lambda (s) (org-export-activate-smart-quotes s :html info))
1572 info)))))
1573 ;; Closing quotes: standard test.
1574 (should
1575 (equal
1576 '("some&rdquo; paragraph")
1577 (let ((org-export-default-language "en"))
1578 (org-test-with-parsed-data "some\" paragraph"
1579 (org-element-map
1580 tree 'plain-text
1581 (lambda (s) (org-export-activate-smart-quotes s :html info))
1582 info)))))
1583 ;; Closing quotes: at the end of a paragraph.
1584 (should
1585 (equal
1586 '("end&rdquo;")
1587 (let ((org-export-default-language "en"))
1588 (org-test-with-parsed-data "end\""
1589 (org-element-map
1590 tree 'plain-text
1591 (lambda (s) (org-export-activate-smart-quotes s :html info))
1592 info)))))
1593 ;; Apostrophe: standard test.
1594 (should
1595 (equal
1596 '("It shouldn&rsquo;t fail")
1597 (let ((org-export-default-language "en"))
1598 (org-test-with-parsed-data "It shouldn't fail"
1599 (org-element-map
1600 tree 'plain-text
1601 (lambda (s) (org-export-activate-smart-quotes s :html info))
1602 info)))))
1603 ;; Apostrophe: before an object.
1604 (should
1605 (equal
1606 '("a&rsquo;")
1607 (let ((org-export-default-language "en"))
1608 (org-test-with-parsed-data "a'=b="
1609 (org-element-map
1610 tree 'plain-text
1611 (lambda (s) (org-export-activate-smart-quotes s :html info))
1612 info)))))
1613 ;; Apostrophe: after an object.
1614 (should
1615 (equal
1616 '("&rsquo;s")
1617 (let ((org-export-default-language "en"))
1618 (org-test-with-parsed-data "=code='s"
1619 (org-element-map
1620 tree 'plain-text
1621 (lambda (s) (org-export-activate-smart-quotes s :html info))
1622 info)))))
1623 ;; Special case: isolated quotes.
1624 (should
1625 (equal '("&ldquo;" "&rdquo;")
1626 (let ((org-export-default-language "en"))
1627 (org-test-with-parsed-data "\"$x$\""
1628 (org-element-map
1629 tree 'plain-text
1630 (lambda (s) (org-export-activate-smart-quotes s :html info))
1631 info)))))
1632 ;; Smart quotes in secondary strings.
1633 (should
1634 (equal '("&ldquo;" "&rdquo;")
1635 (let ((org-export-default-language "en"))
1636 (org-test-with-parsed-data "* \"$x$\""
1637 (org-element-map
1638 tree 'plain-text
1639 (lambda (s) (org-export-activate-smart-quotes s :html info))
1640 info)))))
1641 ;; Smart quotes in document keywords.
1642 (should
1643 (equal '("&ldquo;" "&rdquo;")
1644 (let ((org-export-default-language "en"))
1645 (org-test-with-parsed-data "#+TITLE: \"$x$\""
1646 (org-element-map
1647 (plist-get info :title) 'plain-text
1648 (lambda (s) (org-export-activate-smart-quotes s :html info))
1649 info)))))
1650 ;; Smart quotes in parsed affiliated keywords.
1651 (should
1652 (equal '("&ldquo;" "&rdquo;" "Paragraph")
1653 (let ((org-export-default-language "en"))
1654 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
1655 (org-element-map
1656 tree 'plain-text
1657 (lambda (s) (org-export-activate-smart-quotes s :html info))
1658 info nil nil t))))))
1662 ;;; Tables
1664 (ert-deftest test-org-export/special-column ()
1665 "Test if the table's special column is properly recognized."
1666 ;; 1. First column is special if it contains only a special marking
1667 ;; characters or empty cells.
1668 (org-test-with-temp-text "
1669 | ! | 1 |
1670 | | 2 |"
1671 (should
1672 (org-export-table-has-special-column-p
1673 (org-element-map
1674 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1675 ;; 2. If the column contains anything else, it isn't special.
1676 (org-test-with-temp-text "
1677 | ! | 1 |
1678 | b | 2 |"
1679 (should-not
1680 (org-export-table-has-special-column-p
1681 (org-element-map
1682 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1683 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
1684 ;; and "!".
1685 (org-test-with-temp-text "
1686 | # | 1 |
1687 | ^ | 2 |
1688 | * | 3 |
1689 | _ | 4 |
1690 | / | 5 |
1691 | $ | 6 |
1692 | ! | 7 |"
1693 (should
1694 (org-export-table-has-special-column-p
1695 (org-element-map
1696 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1697 ;; 4. A first column with only empty cells isn't considered as
1698 ;; special.
1699 (org-test-with-temp-text "
1700 | | 1 |
1701 | | 2 |"
1702 (should-not
1703 (org-export-table-has-special-column-p
1704 (org-element-map
1705 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
1707 (ert-deftest test-org-export/table-row-is-special-p ()
1708 "Test `org-export-table-row-is-special-p' specifications."
1709 ;; 1. A row is special if it has a special marking character in the
1710 ;; special column.
1711 (org-test-with-parsed-data "| ! | 1 |"
1712 (should
1713 (org-export-table-row-is-special-p
1714 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1715 ;; 2. A row is special when its first field is "/"
1716 (org-test-with-parsed-data "
1717 | / | 1 |
1718 | a | b |"
1719 (should
1720 (org-export-table-row-is-special-p
1721 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1722 ;; 3. A row only containing alignment cookies is also considered as
1723 ;; special.
1724 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
1725 (should
1726 (org-export-table-row-is-special-p
1727 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1728 ;; 4. Everything else isn't considered as special.
1729 (org-test-with-parsed-data "| \alpha | | c |"
1730 (should-not
1731 (org-export-table-row-is-special-p
1732 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1733 ;; 5. Table's rules are never considered as special rows.
1734 (org-test-with-parsed-data "|---+---|"
1735 (should-not
1736 (org-export-table-row-is-special-p
1737 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
1739 (ert-deftest test-org-export/has-header-p ()
1740 "Test `org-export-table-has-header-p' specifications."
1741 ;; 1. With an header.
1742 (org-test-with-parsed-data "
1743 | a | b |
1744 |---+---|
1745 | c | d |"
1746 (should
1747 (org-export-table-has-header-p
1748 (org-element-map tree 'table 'identity info 'first-match)
1749 info)))
1750 ;; 2. Without an header.
1751 (org-test-with-parsed-data "
1752 | a | b |
1753 | c | d |"
1754 (should-not
1755 (org-export-table-has-header-p
1756 (org-element-map tree 'table 'identity info 'first-match)
1757 info)))
1758 ;; 3. Don't get fooled with starting and ending rules.
1759 (org-test-with-parsed-data "
1760 |---+---|
1761 | a | b |
1762 | c | d |
1763 |---+---|"
1764 (should-not
1765 (org-export-table-has-header-p
1766 (org-element-map tree 'table 'identity info 'first-match)
1767 info))))
1769 (ert-deftest test-org-export/table-row-group ()
1770 "Test `org-export-table-row-group' specifications."
1771 ;; 1. A rule creates a new group.
1772 (org-test-with-parsed-data "
1773 | a | b |
1774 |---+---|
1775 | 1 | 2 |"
1776 (should
1777 (equal
1778 '(1 nil 2)
1779 (mapcar (lambda (row) (org-export-table-row-group row info))
1780 (org-element-map tree 'table-row 'identity)))))
1781 ;; 2. Special rows are ignored in count.
1782 (org-test-with-parsed-data "
1783 | / | < | > |
1784 |---|---+---|
1785 | | 1 | 2 |"
1786 (should
1787 (equal
1788 '(nil nil 1)
1789 (mapcar (lambda (row) (org-export-table-row-group row info))
1790 (org-element-map tree 'table-row 'identity)))))
1791 ;; 3. Double rules also are ignored in count.
1792 (org-test-with-parsed-data "
1793 | a | b |
1794 |---+---|
1795 |---+---|
1796 | 1 | 2 |"
1797 (should
1798 (equal
1799 '(1 nil nil 2)
1800 (mapcar (lambda (row) (org-export-table-row-group row info))
1801 (org-element-map tree 'table-row 'identity))))))
1803 (ert-deftest test-org-export/table-cell-width ()
1804 "Test `org-export-table-cell-width' specifications."
1805 ;; 1. Width is primarily determined by width cookies. If no cookie
1806 ;; is found, cell's width is nil.
1807 (org-test-with-parsed-data "
1808 | / | <l> | <6> | <l7> |
1809 | | a | b | c |"
1810 (should
1811 (equal
1812 '(nil 6 7)
1813 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1814 (org-element-map tree 'table-cell 'identity info)))))
1815 ;; 2. The last width cookie has precedence.
1816 (org-test-with-parsed-data "
1817 | <6> |
1818 | <7> |
1819 | a |"
1820 (should
1821 (equal
1822 '(7)
1823 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1824 (org-element-map tree 'table-cell 'identity info)))))
1825 ;; 3. Valid width cookies must have a specific row.
1826 (org-test-with-parsed-data "| <6> | cell |"
1827 (should
1828 (equal
1829 '(nil nil)
1830 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1831 (org-element-map tree 'table-cell 'identity))))))
1833 (ert-deftest test-org-export/table-cell-alignment ()
1834 "Test `org-export-table-cell-alignment' specifications."
1835 (let ((org-table-number-fraction 0.5)
1836 (org-table-number-regexp "^[0-9]+$"))
1837 ;; 1. Alignment is primarily determined by alignment cookies.
1838 (org-test-with-temp-text "| <l> | <c> | <r> |"
1839 (let* ((tree (org-element-parse-buffer))
1840 (info `(:parse-tree ,tree)))
1841 (should
1842 (equal
1843 '(left center right)
1844 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
1845 (org-element-map tree 'table-cell 'identity))))))
1846 ;; 2. The last alignment cookie has precedence.
1847 (org-test-with-parsed-data "
1848 | <l8> |
1849 | cell |
1850 | <r9> |"
1851 (should
1852 (equal
1853 '(right right right)
1854 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
1855 (org-element-map tree 'table-cell 'identity)))))
1856 ;; 3. If there's no cookie, cell's contents determine alignment.
1857 ;; A column mostly made of cells containing numbers will align
1858 ;; its cells to the right.
1859 (org-test-with-parsed-data "
1860 | 123 |
1861 | some text |
1862 | 12345 |"
1863 (should
1864 (equal
1865 '(right right right)
1866 (mapcar (lambda (cell)
1867 (org-export-table-cell-alignment cell info))
1868 (org-element-map tree 'table-cell 'identity)))))
1869 ;; 4. Otherwise, they will be aligned to the left.
1870 (org-test-with-parsed-data "
1871 | text |
1872 | some text |
1873 | \alpha |"
1874 (should
1875 (equal
1876 '(left left left)
1877 (mapcar (lambda (cell)
1878 (org-export-table-cell-alignment cell info))
1879 (org-element-map tree 'table-cell 'identity)))))))
1881 (ert-deftest test-org-export/table-cell-borders ()
1882 "Test `org-export-table-cell-borders' specifications."
1883 ;; 1. Recognize various column groups indicators.
1884 (org-test-with-parsed-data "| / | < | > | <> |"
1885 (should
1886 (equal
1887 '((right bottom top) (left bottom top) (right bottom top)
1888 (right left bottom top))
1889 (mapcar (lambda (cell)
1890 (org-export-table-cell-borders cell info))
1891 (org-element-map tree 'table-cell 'identity)))))
1892 ;; 2. Accept shortcuts to define column groups.
1893 (org-test-with-parsed-data "| / | < | < |"
1894 (should
1895 (equal
1896 '((right bottom top) (right left bottom top) (left bottom top))
1897 (mapcar (lambda (cell)
1898 (org-export-table-cell-borders cell info))
1899 (org-element-map tree 'table-cell 'identity)))))
1900 ;; 3. A valid column groups row must start with a "/".
1901 (org-test-with-parsed-data "
1902 | | < |
1903 | a | b |"
1904 (should
1905 (equal '((top) (top) (bottom) (bottom))
1906 (mapcar (lambda (cell)
1907 (org-export-table-cell-borders cell info))
1908 (org-element-map tree 'table-cell 'identity)))))
1909 ;; 4. Take table rules into consideration.
1910 (org-test-with-parsed-data "
1911 | 1 |
1912 |---|
1913 | 2 |"
1914 (should
1915 (equal '((below top) (bottom above))
1916 (mapcar (lambda (cell)
1917 (org-export-table-cell-borders cell info))
1918 (org-element-map tree 'table-cell 'identity)))))
1919 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
1920 ;; (resp. `bottom' and `below') borders. Any special row is
1921 ;; ignored.
1922 (org-test-with-parsed-data "
1923 |---+----|
1924 | / | |
1925 | | 1 |
1926 |---+----|"
1927 (should
1928 (equal '((bottom below top above))
1929 (last
1930 (mapcar (lambda (cell)
1931 (org-export-table-cell-borders cell info))
1932 (org-element-map tree 'table-cell 'identity)))))))
1934 (ert-deftest test-org-export/table-dimensions ()
1935 "Test `org-export-table-dimensions' specifications."
1936 ;; 1. Standard test.
1937 (org-test-with-parsed-data "
1938 | 1 | 2 | 3 |
1939 | 4 | 5 | 6 |"
1940 (should
1941 (equal '(2 . 3)
1942 (org-export-table-dimensions
1943 (org-element-map tree 'table 'identity info 'first-match) info))))
1944 ;; 2. Ignore horizontal rules and special columns.
1945 (org-test-with-parsed-data "
1946 | / | < | > |
1947 | 1 | 2 | 3 |
1948 |---+---+---|
1949 | 4 | 5 | 6 |"
1950 (should
1951 (equal '(2 . 3)
1952 (org-export-table-dimensions
1953 (org-element-map tree 'table 'identity info 'first-match) info)))))
1955 (ert-deftest test-org-export/table-cell-address ()
1956 "Test `org-export-table-cell-address' specifications."
1957 ;; 1. Standard test: index is 0-based.
1958 (org-test-with-parsed-data "| a | b |"
1959 (should
1960 (equal '((0 . 0) (0 . 1))
1961 (org-element-map
1962 tree 'table-cell
1963 (lambda (cell) (org-export-table-cell-address cell info))
1964 info))))
1965 ;; 2. Special column isn't counted, nor are special rows.
1966 (org-test-with-parsed-data "
1967 | / | <> |
1968 | | c |"
1969 (should
1970 (equal '(0 . 0)
1971 (org-export-table-cell-address
1972 (car (last (org-element-map tree 'table-cell 'identity info)))
1973 info))))
1974 ;; 3. Tables rules do not count either.
1975 (org-test-with-parsed-data "
1976 | a |
1977 |---|
1978 | b |
1979 |---|
1980 | c |"
1981 (should
1982 (equal '(2 . 0)
1983 (org-export-table-cell-address
1984 (car (last (org-element-map tree 'table-cell 'identity info)))
1985 info))))
1986 ;; 4. Return nil for special cells.
1987 (org-test-with-parsed-data "| / | a |"
1988 (should-not
1989 (org-export-table-cell-address
1990 (org-element-map tree 'table-cell 'identity nil 'first-match)
1991 info))))
1993 (ert-deftest test-org-export/get-table-cell-at ()
1994 "Test `org-export-get-table-cell-at' specifications."
1995 ;; 1. Address ignores special columns, special rows and rules.
1996 (org-test-with-parsed-data "
1997 | / | <> |
1998 | | a |
1999 |---+----|
2000 | | b |"
2001 (should
2002 (equal '("b")
2003 (org-element-contents
2004 (org-export-get-table-cell-at
2005 '(1 . 0)
2006 (org-element-map tree 'table 'identity info 'first-match)
2007 info)))))
2008 ;; 2. Return value for a non-existent address is nil.
2009 (org-test-with-parsed-data "| a |"
2010 (should-not
2011 (org-export-get-table-cell-at
2012 '(2 . 2)
2013 (org-element-map tree 'table 'identity info 'first-match)
2014 info)))
2015 (org-test-with-parsed-data "| / |"
2016 (should-not
2017 (org-export-get-table-cell-at
2018 '(0 . 0)
2019 (org-element-map tree 'table 'identity info 'first-match)
2020 info))))
2022 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
2023 "Test `org-export-table-cell-starts-colgroup-p' specifications."
2024 ;; 1. A cell at a beginning of a row always starts a column group.
2025 (org-test-with-parsed-data "| a |"
2026 (should
2027 (org-export-table-cell-starts-colgroup-p
2028 (org-element-map tree 'table-cell 'identity info 'first-match)
2029 info)))
2030 ;; 2. Special column should be ignored when determining the
2031 ;; beginning of the row.
2032 (org-test-with-parsed-data "
2033 | / | |
2034 | | a |"
2035 (should
2036 (org-export-table-cell-starts-colgroup-p
2037 (org-element-map tree 'table-cell 'identity info 'first-match)
2038 info)))
2039 ;; 2. Explicit column groups.
2040 (org-test-with-parsed-data "
2041 | / | | < |
2042 | a | b | c |"
2043 (should
2044 (equal
2045 '(yes no yes)
2046 (org-element-map
2047 tree 'table-cell
2048 (lambda (cell)
2049 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
2050 info)))))
2052 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
2053 "Test `org-export-table-cell-ends-colgroup-p' specifications."
2054 ;; 1. A cell at the end of a row always ends a column group.
2055 (org-test-with-parsed-data "| a |"
2056 (should
2057 (org-export-table-cell-ends-colgroup-p
2058 (org-element-map tree 'table-cell 'identity info 'first-match)
2059 info)))
2060 ;; 2. Special column should be ignored when determining the
2061 ;; beginning of the row.
2062 (org-test-with-parsed-data "
2063 | / | |
2064 | | a |"
2065 (should
2066 (org-export-table-cell-ends-colgroup-p
2067 (org-element-map tree 'table-cell 'identity info 'first-match)
2068 info)))
2069 ;; 3. Explicit column groups.
2070 (org-test-with-parsed-data "
2071 | / | < | |
2072 | a | b | c |"
2073 (should
2074 (equal
2075 '(yes no yes)
2076 (org-element-map
2077 tree 'table-cell
2078 (lambda (cell)
2079 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
2080 info)))))
2082 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
2083 "Test `org-export-table-row-starts-rowgroup-p' specifications."
2084 ;; 1. A row at the beginning of a table always starts a row group.
2085 ;; So does a row following a table rule.
2086 (org-test-with-parsed-data "
2087 | a |
2088 |---|
2089 | b |"
2090 (should
2091 (equal
2092 '(yes no yes)
2093 (org-element-map
2094 tree 'table-row
2095 (lambda (row)
2096 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2097 info))))
2098 ;; 2. Special rows should be ignored when determining the beginning
2099 ;; of the row.
2100 (org-test-with-parsed-data "
2101 | / | < |
2102 | | a |
2103 |---+---|
2104 | / | < |
2105 | | b |"
2106 (should
2107 (equal
2108 '(yes no yes)
2109 (org-element-map
2110 tree 'table-row
2111 (lambda (row)
2112 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2113 info)))))
2115 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
2116 "Test `org-export-table-row-ends-rowgroup-p' specifications."
2117 ;; 1. A row at the end of a table always ends a row group. So does
2118 ;; a row preceding a table rule.
2119 (org-test-with-parsed-data "
2120 | a |
2121 |---|
2122 | b |"
2123 (should
2124 (equal
2125 '(yes no yes)
2126 (org-element-map
2127 tree 'table-row
2128 (lambda (row)
2129 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2130 info))))
2131 ;; 2. Special rows should be ignored when determining the beginning
2132 ;; of the row.
2133 (org-test-with-parsed-data "
2134 | | a |
2135 | / | < |
2136 |---+---|
2137 | | b |
2138 | / | < |"
2139 (should
2140 (equal
2141 '(yes no yes)
2142 (org-element-map
2143 tree 'table-row
2144 (lambda (row)
2145 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2146 info)))))
2148 (ert-deftest test-org-export/table-row-starts-header-p ()
2149 "Test `org-export-table-row-starts-header-p' specifications."
2150 ;; 1. Only the row starting the first row group starts the table
2151 ;; header.
2152 (org-test-with-parsed-data "
2153 | a |
2154 | b |
2155 |---|
2156 | c |"
2157 (should
2158 (equal
2159 '(yes no no no)
2160 (org-element-map
2161 tree 'table-row
2162 (lambda (row)
2163 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
2164 info))))
2165 ;; 2. A row cannot start an header if there's no header in the
2166 ;; table.
2167 (org-test-with-parsed-data "
2168 | a |
2169 |---|"
2170 (should-not
2171 (org-export-table-row-starts-header-p
2172 (org-element-map tree 'table-row 'identity info 'first-match)
2173 info))))
2175 (ert-deftest test-org-export/table-row-ends-header-p ()
2176 "Test `org-export-table-row-ends-header-p' specifications."
2177 ;; 1. Only the row starting the first row group starts the table
2178 ;; header.
2179 (org-test-with-parsed-data "
2180 | a |
2181 | b |
2182 |---|
2183 | c |"
2184 (should
2185 (equal
2186 '(no yes no no)
2187 (org-element-map
2188 tree 'table-row
2189 (lambda (row)
2190 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
2191 info))))
2192 ;; 2. A row cannot start an header if there's no header in the
2193 ;; table.
2194 (org-test-with-parsed-data "
2195 | a |
2196 |---|"
2197 (should-not
2198 (org-export-table-row-ends-header-p
2199 (org-element-map tree 'table-row 'identity info 'first-match)
2200 info))))
2204 ;;; Templates
2206 (ert-deftest test-org-export/inner-template ()
2207 "Test `inner-template' translator specifications."
2208 (should
2209 (equal "Success!"
2210 (let (org-export-registered-backends)
2211 (org-export-define-backend test
2212 ((inner-template . (lambda (contents info) "Success!"))
2213 (headline . (lambda (h c i) "Headline"))))
2214 (org-test-with-temp-text "* Headline"
2215 (org-export-as 'test)))))
2216 ;; Inner template is applied even in a "body-only" export.
2217 (should
2218 (equal "Success!"
2219 (let (org-export-registered-backends)
2220 (org-export-define-backend test
2221 ((inner-template . (lambda (contents info) "Success!"))
2222 (headline . (lambda (h c i) "Headline"))))
2223 (org-test-with-temp-text "* Headline"
2224 (org-export-as 'test nil nil 'body-only))))))
2226 (ert-deftest test-org-export/template ()
2227 "Test `template' translator specifications."
2228 (should
2229 (equal "Success!"
2230 (let (org-export-registered-backends)
2231 (org-export-define-backend test
2232 ((template . (lambda (contents info) "Success!"))
2233 (headline . (lambda (h c i) "Headline"))))
2234 (org-test-with-temp-text "* Headline"
2235 (org-export-as 'test)))))
2236 ;; Template is not applied in a "body-only" export.
2237 (should-not
2238 (equal "Success!"
2239 (let (org-export-registered-backends)
2240 (org-export-define-backend test
2241 ((template . (lambda (contents info) "Success!"))
2242 (headline . (lambda (h c i) "Headline"))))
2243 (org-test-with-temp-text "* Headline"
2244 (org-export-as 'test nil nil 'body-only))))))
2248 ;;; Topology
2250 (ert-deftest test-org-export/get-next-element ()
2251 "Test `org-export-get-next-element' specifications."
2252 ;; Standard test.
2253 (should
2254 (equal "b"
2255 (org-test-with-parsed-data "* Headline\n*a* b"
2256 (org-export-get-next-element
2257 (org-element-map tree 'bold 'identity info t) info))))
2258 ;; Return nil when no previous element.
2259 (should-not
2260 (org-test-with-parsed-data "* Headline\na *b*"
2261 (org-export-get-next-element
2262 (org-element-map tree 'bold 'identity info t) info)))
2263 ;; Non-exportable elements are ignored.
2264 (should-not
2265 (let ((org-export-with-timestamps nil))
2266 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
2267 (org-export-get-next-element
2268 (org-element-map tree 'entity 'identity info t) info))))
2269 ;; Find next element in secondary strings.
2270 (should
2271 (eq 'verbatim
2272 (org-test-with-parsed-data "* a =verb="
2273 (org-element-type
2274 (org-export-get-next-element
2275 (org-element-map tree 'plain-text 'identity info t) info)))))
2276 ;; Find next element in document keywords.
2277 (should
2278 (eq 'verbatim
2279 (org-test-with-parsed-data "#+TITLE: a =verb="
2280 (org-element-type
2281 (org-export-get-next-element
2282 (org-element-map
2283 (plist-get info :title) 'plain-text 'identity info t) info)))))
2284 ;; Find next element in parsed affiliated keywords.
2285 (should
2286 (eq 'verbatim
2287 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
2288 (org-element-type
2289 (org-export-get-next-element
2290 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2291 ;; With optional argument N, return a list containing all the
2292 ;; following elements.
2293 (should
2294 (equal
2295 '(bold code underline)
2296 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2297 (mapcar 'car
2298 (org-export-get-next-element
2299 (org-element-map tree 'italic 'identity info t) info t)))))
2300 ;; When N is a positive integer, return a list containing up to
2301 ;; N following elements.
2302 (should
2303 (equal
2304 '(bold code)
2305 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2306 (mapcar 'car
2307 (org-export-get-next-element
2308 (org-element-map tree 'italic 'identity info t) info 2))))))
2310 (ert-deftest test-org-export/get-previous-element ()
2311 "Test `org-export-get-previous-element' specifications."
2312 ;; Standard test.
2313 (should
2314 (equal "a "
2315 (org-test-with-parsed-data "* Headline\na *b*"
2316 (org-export-get-previous-element
2317 (org-element-map tree 'bold 'identity info t) info))))
2318 ;; Return nil when no previous element.
2319 (should-not
2320 (org-test-with-parsed-data "* Headline\n*a* b"
2321 (org-export-get-previous-element
2322 (org-element-map tree 'bold 'identity info t) info)))
2323 ;; Non-exportable elements are ignored.
2324 (should-not
2325 (let ((org-export-with-timestamps nil))
2326 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
2327 (org-export-get-previous-element
2328 (org-element-map tree 'entity 'identity info t) info))))
2329 ;; Find previous element in secondary strings.
2330 (should
2331 (eq 'verbatim
2332 (org-test-with-parsed-data "* =verb= a"
2333 (org-element-type
2334 (org-export-get-previous-element
2335 (org-element-map tree 'plain-text 'identity info t) info)))))
2336 ;; Find previous element in document keywords.
2337 (should
2338 (eq 'verbatim
2339 (org-test-with-parsed-data "#+TITLE: =verb= a"
2340 (org-element-type
2341 (org-export-get-previous-element
2342 (org-element-map
2343 (plist-get info :title) 'plain-text 'identity info t) info)))))
2344 ;; Find previous element in parsed affiliated keywords.
2345 (should
2346 (eq 'verbatim
2347 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
2348 (org-element-type
2349 (org-export-get-previous-element
2350 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2351 ;; With optional argument N, return a list containing up to
2352 ;; N previous elements.
2353 (should
2354 (equal '(bold italic underline)
2355 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
2356 (mapcar 'car
2357 (org-export-get-previous-element
2358 (org-element-map tree 'code 'identity info t) info t)))))
2359 ;; When N is a positive integer, return a list containing up to
2360 ;; N previous elements.
2361 (should
2362 (equal '(bold italic)
2363 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
2364 (mapcar 'car
2365 (org-export-get-previous-element
2366 (org-element-map tree 'code 'identity info t) info 2))))))
2369 (provide 'test-ox)
2370 ;;; test-org-export.el end here