ox: Add test for :minlevel parameter in INCLUDE keywords
[org-mode.git] / testing / lisp / test-ox.el
blob1bf21ded91170076ea09f0a0159bbef7e5d2a72c
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-tags ()
1022 "Test `org-export-get-tags' specifications."
1023 (let ((org-export-exclude-tags '("noexport"))
1024 (org-export-select-tags '("export")))
1025 ;; Standard test: tags which are not a select tag, an exclude tag,
1026 ;; or specified as optional argument shouldn't be ignored.
1027 (should
1028 (org-test-with-parsed-data "* Headline :tag:"
1029 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1030 info)))
1031 ;; Exclude tags are removed.
1032 (should-not
1033 (org-test-with-parsed-data "* Headline :noexport:"
1034 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1035 info)))
1036 ;; Select tags are removed.
1037 (should-not
1038 (org-test-with-parsed-data "* Headline :export:"
1039 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1040 info)))
1041 (should
1042 (equal
1043 '("tag")
1044 (org-test-with-parsed-data "* Headline :tag:export:"
1045 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1046 info))))
1047 ;; Tags provided in the optional argument are also ignored.
1048 (should-not
1049 (org-test-with-parsed-data "* Headline :ignore:"
1050 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1051 info '("ignore"))))
1052 ;; Allow tag inheritance.
1053 (should
1054 (equal
1055 '(("tag") ("tag"))
1056 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
1057 (org-element-map
1058 tree 'headline
1059 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
1060 ;; Tag inheritance checks FILETAGS keywords.
1061 (should
1062 (equal
1063 '(("a" "b" "tag"))
1064 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
1065 (org-element-map
1066 tree 'headline
1067 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
1069 (ert-deftest test-org-export/get-node-property ()
1070 "Test`org-export-get-node-property' specifications."
1071 ;; Standard test.
1072 (should
1073 (equal "value"
1074 (org-test-with-parsed-data "* Headline
1075 :PROPERTIES:
1076 :prop: value
1077 :END:"
1078 (org-export-get-node-property
1079 :PROP (org-element-map tree 'headline 'identity nil t)))))
1080 ;; Test inheritance.
1081 (should
1082 (equal "value"
1083 (org-test-with-parsed-data "* Parent
1084 :PROPERTIES:
1085 :prop: value
1086 :END:
1087 ** Headline
1088 Paragraph"
1089 (org-export-get-node-property
1090 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
1091 ;; Cannot return a value before the first headline.
1092 (should-not
1093 (org-test-with-parsed-data "Paragraph
1094 * Headline
1095 :PROPERTIES:
1096 :prop: value
1097 :END:"
1098 (org-export-get-node-property
1099 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
1101 (ert-deftest test-org-export/get-category ()
1102 "Test `org-export-get-category' specifications."
1103 ;; Standard test.
1104 (should
1105 (equal "value"
1106 (org-test-with-parsed-data "* Headline
1107 :PROPERTIES:
1108 :CATEGORY: value
1109 :END:"
1110 (org-export-get-category
1111 (org-element-map tree 'headline 'identity nil t) info))))
1112 ;; Test inheritance from a parent headline.
1113 (should
1114 (equal '("value" "value")
1115 (org-test-with-parsed-data "* Headline1
1116 :PROPERTIES:
1117 :CATEGORY: value
1118 :END:
1119 ** Headline2"
1120 (org-element-map
1121 tree 'headline
1122 (lambda (hl) (org-export-get-category hl info)) info))))
1123 ;; Test inheritance from #+CATEGORY keyword
1124 (should
1125 (equal "value"
1126 (org-test-with-parsed-data "#+CATEGORY: value
1127 * Headline"
1128 (org-export-get-category
1129 (org-element-map tree 'headline 'identity nil t) info))))
1130 ;; Test inheritance from file name.
1131 (should
1132 (equal "test"
1133 (org-test-with-parsed-data "* Headline"
1134 (let ((info (plist-put info :input-file "~/test.org")))
1135 (org-export-get-category
1136 (org-element-map tree 'headline 'identity nil t) info)))))
1137 ;; Fall-back value.
1138 (should
1139 (equal "???"
1140 (org-test-with-parsed-data "* Headline"
1141 (org-export-get-category
1142 (org-element-map tree 'headline 'identity nil t) info)))))
1144 (ert-deftest test-org-export/first-sibling-p ()
1145 "Test `org-export-first-sibling-p' specifications."
1146 ;; Standard test.
1147 (should
1148 (equal
1149 '(yes yes no)
1150 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1151 (org-element-map
1152 tree 'headline
1153 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1154 info))))
1155 ;; Ignore headlines not exported.
1156 (should
1157 (equal
1158 '(yes)
1159 (let ((org-export-exclude-tags '("ignore")))
1160 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
1161 (org-element-map
1162 tree 'headline
1163 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1164 info))))))
1166 (ert-deftest test-org-export/last-sibling-p ()
1167 "Test `org-export-last-sibling-p' specifications."
1168 ;; Standard test.
1169 (should
1170 (equal
1171 '(yes no yes)
1172 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1173 (org-element-map
1174 tree 'headline
1175 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1176 info))))
1177 ;; Ignore headlines not exported.
1178 (should
1179 (equal
1180 '(yes)
1181 (let ((org-export-exclude-tags '("ignore")))
1182 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
1183 (org-element-map
1184 tree 'headline
1185 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1186 info))))))
1190 ;;; Links
1192 (ert-deftest test-org-export/get-coderef-format ()
1193 "Test `org-export-get-coderef-format' specifications."
1194 ;; A link without description returns "%s"
1195 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
1196 "%s"))
1197 ;; Return "%s" when path is matched within description.
1198 (should (equal (org-export-get-coderef-format "path" "desc (path)")
1199 "desc %s"))
1200 ;; Otherwise return description.
1201 (should (equal (org-export-get-coderef-format "path" "desc")
1202 "desc")))
1204 (ert-deftest test-org-export/inline-image-p ()
1205 "Test `org-export-inline-image-p' specifications."
1206 (should
1207 (org-export-inline-image-p
1208 (org-test-with-temp-text "[[#id]]"
1209 (org-element-map
1210 (org-element-parse-buffer) 'link 'identity nil t))
1211 '(("custom-id" . "id")))))
1213 (ert-deftest test-org-export/fuzzy-link ()
1214 "Test fuzzy links specifications."
1215 ;; 1. Links to invisible (keyword) targets should be ignored.
1216 (org-test-with-parsed-data
1217 "Paragraph.\n#+TARGET: Test\n[[Test]]"
1218 (should-not
1219 (org-element-map
1220 tree 'link
1221 (lambda (link)
1222 (org-export-get-ordinal
1223 (org-export-resolve-fuzzy-link link info) info)) info)))
1224 ;; 2. Link to an headline should return headline's number.
1225 (org-test-with-parsed-data
1226 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
1227 (should
1228 ;; Note: Headline's number is in fact a list of numbers.
1229 (equal '(2)
1230 (org-element-map
1231 tree 'link
1232 (lambda (link)
1233 (org-export-get-ordinal
1234 (org-export-resolve-fuzzy-link link info) info)) info t))))
1235 ;; 3. Link to a target in an item should return item's number.
1236 (org-test-with-parsed-data
1237 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
1238 (should
1239 ;; Note: Item's number is in fact a list of numbers.
1240 (equal '(1 2)
1241 (org-element-map
1242 tree 'link
1243 (lambda (link)
1244 (org-export-get-ordinal
1245 (org-export-resolve-fuzzy-link link info) info)) info t))))
1246 ;; 4. Link to a target in a footnote should return footnote's
1247 ;; number.
1248 (org-test-with-parsed-data "
1249 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
1250 (should
1251 (equal '(2 3)
1252 (org-element-map
1253 tree 'link
1254 (lambda (link)
1255 (org-export-get-ordinal
1256 (org-export-resolve-fuzzy-link link info) info)) info))))
1257 ;; 5. Link to a named element should return sequence number of that
1258 ;; element.
1259 (org-test-with-parsed-data
1260 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
1261 (should
1262 (= 2
1263 (org-element-map
1264 tree 'link
1265 (lambda (link)
1266 (org-export-get-ordinal
1267 (org-export-resolve-fuzzy-link link info) info)) info t))))
1268 ;; 6. Link to a target not within an item, a table, a footnote
1269 ;; reference or definition should return section number.
1270 (org-test-with-parsed-data
1271 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
1272 (should
1273 (equal '(2)
1274 (org-element-map
1275 tree 'link
1276 (lambda (link)
1277 (org-export-get-ordinal
1278 (org-export-resolve-fuzzy-link link info) info)) info t))))
1279 ;; 7. Space are not significant when matching a fuzzy link.
1280 (should
1281 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
1282 (org-element-map tree 'link
1283 (lambda (link) (org-export-resolve-fuzzy-link link info))
1284 info t)))
1285 ;; 8. Statistics cookies are ignored for headline match.
1286 (should
1287 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
1288 (org-element-map tree 'link
1289 (lambda (link) (org-export-resolve-fuzzy-link link info))
1290 info t)))
1291 (should
1292 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
1293 (org-element-map tree 'link
1294 (lambda (link) (org-export-resolve-fuzzy-link link info))
1295 info t))))
1297 (ert-deftest test-org-export/resolve-coderef ()
1298 "Test `org-export-resolve-coderef' specifications."
1299 (let ((org-coderef-label-format "(ref:%s)"))
1300 ;; 1. A link to a "-n -k -r" block returns line number.
1301 (org-test-with-parsed-data
1302 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
1303 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1304 (org-test-with-parsed-data
1305 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1306 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1307 ;; 2. A link to a "-n -r" block returns line number.
1308 (org-test-with-parsed-data
1309 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
1310 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1311 (org-test-with-parsed-data
1312 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1313 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1314 ;; 3. A link to a "-n" block returns coderef.
1315 (org-test-with-parsed-data
1316 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1317 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1318 (org-test-with-parsed-data
1319 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
1320 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1321 ;; 4. A link to a "-r" block returns line number.
1322 (org-test-with-parsed-data
1323 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1324 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1325 (org-test-with-parsed-data
1326 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
1327 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1328 ;; 5. A link to a block without a switch returns coderef.
1329 (org-test-with-parsed-data
1330 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1331 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1332 (org-test-with-parsed-data
1333 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
1334 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1335 ;; 6. Correctly handle continued line numbers. A "+n" switch
1336 ;; should resume numbering from previous block with numbered
1337 ;; lines, ignoring blocks not numbering lines in the process.
1338 ;; A "-n" switch resets count.
1339 (org-test-with-parsed-data "
1340 #+BEGIN_EXAMPLE -n
1341 Text.
1342 #+END_EXAMPLE
1344 #+BEGIN_SRC emacs-lisp
1345 \(- 1 1)
1346 #+END_SRC
1348 #+BEGIN_SRC emacs-lisp +n -r
1349 \(+ 1 1) (ref:addition)
1350 #+END_SRC
1352 #+BEGIN_EXAMPLE -n -r
1353 Another text. (ref:text)
1354 #+END_EXAMPLE"
1355 (should (= (org-export-resolve-coderef "addition" info) 2))
1356 (should (= (org-export-resolve-coderef "text" info) 1)))
1357 ;; 7. Recognize coderef with user-specified syntax.
1358 (org-test-with-parsed-data
1359 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
1360 (should (equal (org-export-resolve-coderef "text" info) "text")))))
1362 (ert-deftest test-org-export/resolve-fuzzy-link ()
1363 "Test `org-export-resolve-fuzzy-link' specifications."
1364 ;; 1. Match target objects.
1365 (org-test-with-parsed-data "<<target>> [[target]]"
1366 (should
1367 (org-export-resolve-fuzzy-link
1368 (org-element-map tree 'link 'identity info t) info)))
1369 ;; 2. Match target elements.
1370 (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
1371 (should
1372 (org-export-resolve-fuzzy-link
1373 (org-element-map tree 'link 'identity info t) info)))
1374 ;; 3. Match named elements.
1375 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
1376 (should
1377 (org-export-resolve-fuzzy-link
1378 (org-element-map tree 'link 'identity info t) info)))
1379 ;; 4. Match exact headline's name.
1380 (org-test-with-parsed-data "* My headline\n[[My headline]]"
1381 (should
1382 (org-export-resolve-fuzzy-link
1383 (org-element-map tree 'link 'identity info t) info)))
1384 ;; 5. Targets objects have priority over named elements and headline
1385 ;; titles.
1386 (org-test-with-parsed-data
1387 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
1388 (should
1389 (eq 'target
1390 (org-element-type
1391 (org-export-resolve-fuzzy-link
1392 (org-element-map tree 'link 'identity info t) info)))))
1393 ;; 6. Named elements have priority over headline titles.
1394 (org-test-with-parsed-data
1395 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
1396 (should
1397 (eq 'paragraph
1398 (org-element-type
1399 (org-export-resolve-fuzzy-link
1400 (org-element-map tree 'link 'identity info t) info)))))
1401 ;; 7. If link's path starts with a "*", only match headline titles,
1402 ;; though.
1403 (org-test-with-parsed-data
1404 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
1405 (should
1406 (eq 'headline
1407 (org-element-type
1408 (org-export-resolve-fuzzy-link
1409 (org-element-map tree 'link 'identity info t) info)))))
1410 ;; 8. Return nil if no match.
1411 (org-test-with-parsed-data "[[target]]"
1412 (should-not
1413 (org-export-resolve-fuzzy-link
1414 (org-element-map tree 'link 'identity info t) info))))
1416 (ert-deftest test-org-export/resolve-id-link ()
1417 "Test `org-export-resolve-id-link' specifications."
1418 ;; 1. Regular test for custom-id link.
1419 (org-test-with-parsed-data "* Headline1
1420 :PROPERTIES:
1421 :CUSTOM_ID: test
1422 :END:
1423 * Headline 2
1424 \[[#test]]"
1425 (should
1426 (org-export-resolve-id-link
1427 (org-element-map tree 'link 'identity info t) info)))
1428 ;; 2. Failing test for custom-id link.
1429 (org-test-with-parsed-data "* Headline1
1430 :PROPERTIES:
1431 :CUSTOM_ID: test
1432 :END:
1433 * Headline 2
1434 \[[#no-match]]"
1435 (should-not
1436 (org-export-resolve-id-link
1437 (org-element-map tree 'link 'identity info t) info)))
1438 ;; 3. Test for internal id target.
1439 (org-test-with-parsed-data "* Headline1
1440 :PROPERTIES:
1441 :ID: aaaa
1442 :END:
1443 * Headline 2
1444 \[[id:aaaa]]"
1445 (should
1446 (org-export-resolve-id-link
1447 (org-element-map tree 'link 'identity info t) info)))
1448 ;; 4. Test for external id target.
1449 (org-test-with-parsed-data "[[id:aaaa]]"
1450 (should
1451 (org-export-resolve-id-link
1452 (org-element-map tree 'link 'identity info t)
1453 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
1455 (ert-deftest test-org-export/resolve-radio-link ()
1456 "Test `org-export-resolve-radio-link' specifications."
1457 ;; Standard test.
1458 (should
1459 (org-test-with-temp-text "<<<radio>>> radio"
1460 (org-update-radio-target-regexp)
1461 (let* ((tree (org-element-parse-buffer))
1462 (info `(:parse-tree ,tree)))
1463 (org-export-resolve-radio-link
1464 (org-element-map tree 'link 'identity info t)
1465 info))))
1466 ;; Radio targets are case-insensitive.
1467 (should
1468 (org-test-with-temp-text "<<<RADIO>>> radio"
1469 (org-update-radio-target-regexp)
1470 (let* ((tree (org-element-parse-buffer))
1471 (info `(:parse-tree ,tree)))
1472 (org-export-resolve-radio-link
1473 (org-element-map tree 'link 'identity info t)
1474 info))))
1475 ;; Radio target with objects.
1476 (should
1477 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
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)))))
1487 ;;; Src-block and example-block
1489 (ert-deftest test-org-export/unravel-code ()
1490 "Test `org-export-unravel-code' function."
1491 (let ((org-coderef-label-format "(ref:%s)"))
1492 ;; 1. Code without reference.
1493 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
1494 (should (equal (org-export-unravel-code (org-element-at-point))
1495 '("(+ 1 1)\n"))))
1496 ;; 2. Code with reference.
1497 (org-test-with-temp-text
1498 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
1499 (should (equal (org-export-unravel-code (org-element-at-point))
1500 '("(+ 1 1)\n" (1 . "test")))))
1501 ;; 3. Code with user-defined reference.
1502 (org-test-with-temp-text
1503 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
1504 (should (equal (org-export-unravel-code (org-element-at-point))
1505 '("(+ 1 1)\n" (1 . "test")))))
1506 ;; 4. Code references keys are relative to the current block.
1507 (org-test-with-temp-text "
1508 #+BEGIN_EXAMPLE -n
1509 \(+ 1 1)
1510 #+END_EXAMPLE
1511 #+BEGIN_EXAMPLE +n
1512 \(+ 2 2)
1513 \(+ 3 3) (ref:one)
1514 #+END_EXAMPLE"
1515 (goto-line 5)
1516 (should (equal (org-export-unravel-code (org-element-at-point))
1517 '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
1521 ;;; Smart Quotes
1523 (ert-deftest test-org-export/activate-smart-quotes ()
1524 "Test `org-export-activate-smart-quotes' specifications."
1525 ;; Opening double quotes: standard test.
1526 (should
1527 (equal
1528 '("some &ldquo;paragraph")
1529 (let ((org-export-default-language "en"))
1530 (org-test-with-parsed-data "some \"paragraph"
1531 (org-element-map
1532 tree 'plain-text
1533 (lambda (s) (org-export-activate-smart-quotes s :html info))
1534 info)))))
1535 ;; Opening quotes: at the beginning of a paragraph.
1536 (should
1537 (equal
1538 '("&ldquo;begin")
1539 (let ((org-export-default-language "en"))
1540 (org-test-with-parsed-data "\"begin"
1541 (org-element-map
1542 tree 'plain-text
1543 (lambda (s) (org-export-activate-smart-quotes s :html info))
1544 info)))))
1545 ;; Opening quotes: after an object.
1546 (should
1547 (equal
1548 '("&ldquo;begin")
1549 (let ((org-export-default-language "en"))
1550 (org-test-with-parsed-data "=verb= \"begin"
1551 (org-element-map
1552 tree 'plain-text
1553 (lambda (s) (org-export-activate-smart-quotes s :html info))
1554 info)))))
1555 ;; Closing quotes: standard test.
1556 (should
1557 (equal
1558 '("some&rdquo; paragraph")
1559 (let ((org-export-default-language "en"))
1560 (org-test-with-parsed-data "some\" paragraph"
1561 (org-element-map
1562 tree 'plain-text
1563 (lambda (s) (org-export-activate-smart-quotes s :html info))
1564 info)))))
1565 ;; Closing quotes: at the end of a paragraph.
1566 (should
1567 (equal
1568 '("end&rdquo;")
1569 (let ((org-export-default-language "en"))
1570 (org-test-with-parsed-data "end\""
1571 (org-element-map
1572 tree 'plain-text
1573 (lambda (s) (org-export-activate-smart-quotes s :html info))
1574 info)))))
1575 ;; Apostrophe: standard test.
1576 (should
1577 (equal
1578 '("It shouldn&rsquo;t fail")
1579 (let ((org-export-default-language "en"))
1580 (org-test-with-parsed-data "It shouldn't fail"
1581 (org-element-map
1582 tree 'plain-text
1583 (lambda (s) (org-export-activate-smart-quotes s :html info))
1584 info)))))
1585 ;; Apostrophe: before an object.
1586 (should
1587 (equal
1588 '("a&rsquo;")
1589 (let ((org-export-default-language "en"))
1590 (org-test-with-parsed-data "a'=b="
1591 (org-element-map
1592 tree 'plain-text
1593 (lambda (s) (org-export-activate-smart-quotes s :html info))
1594 info)))))
1595 ;; Apostrophe: after an object.
1596 (should
1597 (equal
1598 '("&rsquo;s")
1599 (let ((org-export-default-language "en"))
1600 (org-test-with-parsed-data "=code='s"
1601 (org-element-map
1602 tree 'plain-text
1603 (lambda (s) (org-export-activate-smart-quotes s :html info))
1604 info)))))
1605 ;; Special case: isolated quotes.
1606 (should
1607 (equal '("&ldquo;" "&rdquo;")
1608 (let ((org-export-default-language "en"))
1609 (org-test-with-parsed-data "\"$x$\""
1610 (org-element-map
1611 tree 'plain-text
1612 (lambda (s) (org-export-activate-smart-quotes s :html info))
1613 info)))))
1614 ;; Smart quotes in secondary strings.
1615 (should
1616 (equal '("&ldquo;" "&rdquo;")
1617 (let ((org-export-default-language "en"))
1618 (org-test-with-parsed-data "* \"$x$\""
1619 (org-element-map
1620 tree 'plain-text
1621 (lambda (s) (org-export-activate-smart-quotes s :html info))
1622 info)))))
1623 ;; Smart quotes in document keywords.
1624 (should
1625 (equal '("&ldquo;" "&rdquo;")
1626 (let ((org-export-default-language "en"))
1627 (org-test-with-parsed-data "#+TITLE: \"$x$\""
1628 (org-element-map
1629 (plist-get info :title) 'plain-text
1630 (lambda (s) (org-export-activate-smart-quotes s :html info))
1631 info)))))
1632 ;; Smart quotes in parsed affiliated keywords.
1633 (should
1634 (equal '("&ldquo;" "&rdquo;" "Paragraph")
1635 (let ((org-export-default-language "en"))
1636 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
1637 (org-element-map
1638 tree 'plain-text
1639 (lambda (s) (org-export-activate-smart-quotes s :html info))
1640 info nil nil t))))))
1644 ;;; Tables
1646 (ert-deftest test-org-export/special-column ()
1647 "Test if the table's special column is properly recognized."
1648 ;; 1. First column is special if it contains only a special marking
1649 ;; characters or empty cells.
1650 (org-test-with-temp-text "
1651 | ! | 1 |
1652 | | 2 |"
1653 (should
1654 (org-export-table-has-special-column-p
1655 (org-element-map
1656 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1657 ;; 2. If the column contains anything else, it isn't special.
1658 (org-test-with-temp-text "
1659 | ! | 1 |
1660 | b | 2 |"
1661 (should-not
1662 (org-export-table-has-special-column-p
1663 (org-element-map
1664 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1665 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
1666 ;; and "!".
1667 (org-test-with-temp-text "
1668 | # | 1 |
1669 | ^ | 2 |
1670 | * | 3 |
1671 | _ | 4 |
1672 | / | 5 |
1673 | $ | 6 |
1674 | ! | 7 |"
1675 (should
1676 (org-export-table-has-special-column-p
1677 (org-element-map
1678 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1679 ;; 4. A first column with only empty cells isn't considered as
1680 ;; special.
1681 (org-test-with-temp-text "
1682 | | 1 |
1683 | | 2 |"
1684 (should-not
1685 (org-export-table-has-special-column-p
1686 (org-element-map
1687 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
1689 (ert-deftest test-org-export/table-row-is-special-p ()
1690 "Test `org-export-table-row-is-special-p' specifications."
1691 ;; 1. A row is special if it has a special marking character in the
1692 ;; special column.
1693 (org-test-with-parsed-data "| ! | 1 |"
1694 (should
1695 (org-export-table-row-is-special-p
1696 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1697 ;; 2. A row is special when its first field is "/"
1698 (org-test-with-parsed-data "
1699 | / | 1 |
1700 | a | b |"
1701 (should
1702 (org-export-table-row-is-special-p
1703 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1704 ;; 3. A row only containing alignment cookies is also considered as
1705 ;; special.
1706 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
1707 (should
1708 (org-export-table-row-is-special-p
1709 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1710 ;; 4. Everything else isn't considered as special.
1711 (org-test-with-parsed-data "| \alpha | | c |"
1712 (should-not
1713 (org-export-table-row-is-special-p
1714 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1715 ;; 5. Table's rules are never considered as special rows.
1716 (org-test-with-parsed-data "|---+---|"
1717 (should-not
1718 (org-export-table-row-is-special-p
1719 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
1721 (ert-deftest test-org-export/has-header-p ()
1722 "Test `org-export-table-has-header-p' specifications."
1723 ;; 1. With an header.
1724 (org-test-with-parsed-data "
1725 | a | b |
1726 |---+---|
1727 | c | d |"
1728 (should
1729 (org-export-table-has-header-p
1730 (org-element-map tree 'table 'identity info 'first-match)
1731 info)))
1732 ;; 2. Without an header.
1733 (org-test-with-parsed-data "
1734 | a | b |
1735 | c | d |"
1736 (should-not
1737 (org-export-table-has-header-p
1738 (org-element-map tree 'table 'identity info 'first-match)
1739 info)))
1740 ;; 3. Don't get fooled with starting and ending rules.
1741 (org-test-with-parsed-data "
1742 |---+---|
1743 | a | b |
1744 | c | d |
1745 |---+---|"
1746 (should-not
1747 (org-export-table-has-header-p
1748 (org-element-map tree 'table 'identity info 'first-match)
1749 info))))
1751 (ert-deftest test-org-export/table-row-group ()
1752 "Test `org-export-table-row-group' specifications."
1753 ;; 1. A rule creates a new group.
1754 (org-test-with-parsed-data "
1755 | a | b |
1756 |---+---|
1757 | 1 | 2 |"
1758 (should
1759 (equal
1760 '(1 nil 2)
1761 (mapcar (lambda (row) (org-export-table-row-group row info))
1762 (org-element-map tree 'table-row 'identity)))))
1763 ;; 2. Special rows are ignored in count.
1764 (org-test-with-parsed-data "
1765 | / | < | > |
1766 |---|---+---|
1767 | | 1 | 2 |"
1768 (should
1769 (equal
1770 '(nil nil 1)
1771 (mapcar (lambda (row) (org-export-table-row-group row info))
1772 (org-element-map tree 'table-row 'identity)))))
1773 ;; 3. Double rules also are ignored in count.
1774 (org-test-with-parsed-data "
1775 | a | b |
1776 |---+---|
1777 |---+---|
1778 | 1 | 2 |"
1779 (should
1780 (equal
1781 '(1 nil nil 2)
1782 (mapcar (lambda (row) (org-export-table-row-group row info))
1783 (org-element-map tree 'table-row 'identity))))))
1785 (ert-deftest test-org-export/table-cell-width ()
1786 "Test `org-export-table-cell-width' specifications."
1787 ;; 1. Width is primarily determined by width cookies. If no cookie
1788 ;; is found, cell's width is nil.
1789 (org-test-with-parsed-data "
1790 | / | <l> | <6> | <l7> |
1791 | | a | b | c |"
1792 (should
1793 (equal
1794 '(nil 6 7)
1795 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1796 (org-element-map tree 'table-cell 'identity info)))))
1797 ;; 2. The last width cookie has precedence.
1798 (org-test-with-parsed-data "
1799 | <6> |
1800 | <7> |
1801 | a |"
1802 (should
1803 (equal
1804 '(7)
1805 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1806 (org-element-map tree 'table-cell 'identity info)))))
1807 ;; 3. Valid width cookies must have a specific row.
1808 (org-test-with-parsed-data "| <6> | cell |"
1809 (should
1810 (equal
1811 '(nil nil)
1812 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1813 (org-element-map tree 'table-cell 'identity))))))
1815 (ert-deftest test-org-export/table-cell-alignment ()
1816 "Test `org-export-table-cell-alignment' specifications."
1817 (let ((org-table-number-fraction 0.5)
1818 (org-table-number-regexp "^[0-9]+$"))
1819 ;; 1. Alignment is primarily determined by alignment cookies.
1820 (org-test-with-temp-text "| <l> | <c> | <r> |"
1821 (let* ((tree (org-element-parse-buffer))
1822 (info `(:parse-tree ,tree)))
1823 (should
1824 (equal
1825 '(left center right)
1826 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
1827 (org-element-map tree 'table-cell 'identity))))))
1828 ;; 2. The last alignment cookie has precedence.
1829 (org-test-with-parsed-data "
1830 | <l8> |
1831 | cell |
1832 | <r9> |"
1833 (should
1834 (equal
1835 '(right right right)
1836 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
1837 (org-element-map tree 'table-cell 'identity)))))
1838 ;; 3. If there's no cookie, cell's contents determine alignment.
1839 ;; A column mostly made of cells containing numbers will align
1840 ;; its cells to the right.
1841 (org-test-with-parsed-data "
1842 | 123 |
1843 | some text |
1844 | 12345 |"
1845 (should
1846 (equal
1847 '(right right right)
1848 (mapcar (lambda (cell)
1849 (org-export-table-cell-alignment cell info))
1850 (org-element-map tree 'table-cell 'identity)))))
1851 ;; 4. Otherwise, they will be aligned to the left.
1852 (org-test-with-parsed-data "
1853 | text |
1854 | some text |
1855 | \alpha |"
1856 (should
1857 (equal
1858 '(left left left)
1859 (mapcar (lambda (cell)
1860 (org-export-table-cell-alignment cell info))
1861 (org-element-map tree 'table-cell 'identity)))))))
1863 (ert-deftest test-org-export/table-cell-borders ()
1864 "Test `org-export-table-cell-borders' specifications."
1865 ;; 1. Recognize various column groups indicators.
1866 (org-test-with-parsed-data "| / | < | > | <> |"
1867 (should
1868 (equal
1869 '((right bottom top) (left bottom top) (right bottom top)
1870 (right left bottom top))
1871 (mapcar (lambda (cell)
1872 (org-export-table-cell-borders cell info))
1873 (org-element-map tree 'table-cell 'identity)))))
1874 ;; 2. Accept shortcuts to define column groups.
1875 (org-test-with-parsed-data "| / | < | < |"
1876 (should
1877 (equal
1878 '((right bottom top) (right left bottom top) (left bottom top))
1879 (mapcar (lambda (cell)
1880 (org-export-table-cell-borders cell info))
1881 (org-element-map tree 'table-cell 'identity)))))
1882 ;; 3. A valid column groups row must start with a "/".
1883 (org-test-with-parsed-data "
1884 | | < |
1885 | a | b |"
1886 (should
1887 (equal '((top) (top) (bottom) (bottom))
1888 (mapcar (lambda (cell)
1889 (org-export-table-cell-borders cell info))
1890 (org-element-map tree 'table-cell 'identity)))))
1891 ;; 4. Take table rules into consideration.
1892 (org-test-with-parsed-data "
1893 | 1 |
1894 |---|
1895 | 2 |"
1896 (should
1897 (equal '((below top) (bottom above))
1898 (mapcar (lambda (cell)
1899 (org-export-table-cell-borders cell info))
1900 (org-element-map tree 'table-cell 'identity)))))
1901 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
1902 ;; (resp. `bottom' and `below') borders. Any special row is
1903 ;; ignored.
1904 (org-test-with-parsed-data "
1905 |---+----|
1906 | / | |
1907 | | 1 |
1908 |---+----|"
1909 (should
1910 (equal '((bottom below top above))
1911 (last
1912 (mapcar (lambda (cell)
1913 (org-export-table-cell-borders cell info))
1914 (org-element-map tree 'table-cell 'identity)))))))
1916 (ert-deftest test-org-export/table-dimensions ()
1917 "Test `org-export-table-dimensions' specifications."
1918 ;; 1. Standard test.
1919 (org-test-with-parsed-data "
1920 | 1 | 2 | 3 |
1921 | 4 | 5 | 6 |"
1922 (should
1923 (equal '(2 . 3)
1924 (org-export-table-dimensions
1925 (org-element-map tree 'table 'identity info 'first-match) info))))
1926 ;; 2. Ignore horizontal rules and special columns.
1927 (org-test-with-parsed-data "
1928 | / | < | > |
1929 | 1 | 2 | 3 |
1930 |---+---+---|
1931 | 4 | 5 | 6 |"
1932 (should
1933 (equal '(2 . 3)
1934 (org-export-table-dimensions
1935 (org-element-map tree 'table 'identity info 'first-match) info)))))
1937 (ert-deftest test-org-export/table-cell-address ()
1938 "Test `org-export-table-cell-address' specifications."
1939 ;; 1. Standard test: index is 0-based.
1940 (org-test-with-parsed-data "| a | b |"
1941 (should
1942 (equal '((0 . 0) (0 . 1))
1943 (org-element-map
1944 tree 'table-cell
1945 (lambda (cell) (org-export-table-cell-address cell info))
1946 info))))
1947 ;; 2. Special column isn't counted, nor are special rows.
1948 (org-test-with-parsed-data "
1949 | / | <> |
1950 | | c |"
1951 (should
1952 (equal '(0 . 0)
1953 (org-export-table-cell-address
1954 (car (last (org-element-map tree 'table-cell 'identity info)))
1955 info))))
1956 ;; 3. Tables rules do not count either.
1957 (org-test-with-parsed-data "
1958 | a |
1959 |---|
1960 | b |
1961 |---|
1962 | c |"
1963 (should
1964 (equal '(2 . 0)
1965 (org-export-table-cell-address
1966 (car (last (org-element-map tree 'table-cell 'identity info)))
1967 info))))
1968 ;; 4. Return nil for special cells.
1969 (org-test-with-parsed-data "| / | a |"
1970 (should-not
1971 (org-export-table-cell-address
1972 (org-element-map tree 'table-cell 'identity nil 'first-match)
1973 info))))
1975 (ert-deftest test-org-export/get-table-cell-at ()
1976 "Test `org-export-get-table-cell-at' specifications."
1977 ;; 1. Address ignores special columns, special rows and rules.
1978 (org-test-with-parsed-data "
1979 | / | <> |
1980 | | a |
1981 |---+----|
1982 | | b |"
1983 (should
1984 (equal '("b")
1985 (org-element-contents
1986 (org-export-get-table-cell-at
1987 '(1 . 0)
1988 (org-element-map tree 'table 'identity info 'first-match)
1989 info)))))
1990 ;; 2. Return value for a non-existent address is nil.
1991 (org-test-with-parsed-data "| a |"
1992 (should-not
1993 (org-export-get-table-cell-at
1994 '(2 . 2)
1995 (org-element-map tree 'table 'identity info 'first-match)
1996 info)))
1997 (org-test-with-parsed-data "| / |"
1998 (should-not
1999 (org-export-get-table-cell-at
2000 '(0 . 0)
2001 (org-element-map tree 'table 'identity info 'first-match)
2002 info))))
2004 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
2005 "Test `org-export-table-cell-starts-colgroup-p' specifications."
2006 ;; 1. A cell at a beginning of a row always starts a column group.
2007 (org-test-with-parsed-data "| a |"
2008 (should
2009 (org-export-table-cell-starts-colgroup-p
2010 (org-element-map tree 'table-cell 'identity info 'first-match)
2011 info)))
2012 ;; 2. Special column should be ignored when determining the
2013 ;; beginning of the row.
2014 (org-test-with-parsed-data "
2015 | / | |
2016 | | a |"
2017 (should
2018 (org-export-table-cell-starts-colgroup-p
2019 (org-element-map tree 'table-cell 'identity info 'first-match)
2020 info)))
2021 ;; 2. Explicit column groups.
2022 (org-test-with-parsed-data "
2023 | / | | < |
2024 | a | b | c |"
2025 (should
2026 (equal
2027 '(yes no yes)
2028 (org-element-map
2029 tree 'table-cell
2030 (lambda (cell)
2031 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
2032 info)))))
2034 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
2035 "Test `org-export-table-cell-ends-colgroup-p' specifications."
2036 ;; 1. A cell at the end of a row always ends a column group.
2037 (org-test-with-parsed-data "| a |"
2038 (should
2039 (org-export-table-cell-ends-colgroup-p
2040 (org-element-map tree 'table-cell 'identity info 'first-match)
2041 info)))
2042 ;; 2. Special column should be ignored when determining the
2043 ;; beginning of the row.
2044 (org-test-with-parsed-data "
2045 | / | |
2046 | | a |"
2047 (should
2048 (org-export-table-cell-ends-colgroup-p
2049 (org-element-map tree 'table-cell 'identity info 'first-match)
2050 info)))
2051 ;; 3. Explicit column groups.
2052 (org-test-with-parsed-data "
2053 | / | < | |
2054 | a | b | c |"
2055 (should
2056 (equal
2057 '(yes no yes)
2058 (org-element-map
2059 tree 'table-cell
2060 (lambda (cell)
2061 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
2062 info)))))
2064 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
2065 "Test `org-export-table-row-starts-rowgroup-p' specifications."
2066 ;; 1. A row at the beginning of a table always starts a row group.
2067 ;; So does a row following a table rule.
2068 (org-test-with-parsed-data "
2069 | a |
2070 |---|
2071 | b |"
2072 (should
2073 (equal
2074 '(yes no yes)
2075 (org-element-map
2076 tree 'table-row
2077 (lambda (row)
2078 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2079 info))))
2080 ;; 2. Special rows should be ignored when determining the beginning
2081 ;; of the row.
2082 (org-test-with-parsed-data "
2083 | / | < |
2084 | | a |
2085 |---+---|
2086 | / | < |
2087 | | b |"
2088 (should
2089 (equal
2090 '(yes no yes)
2091 (org-element-map
2092 tree 'table-row
2093 (lambda (row)
2094 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2095 info)))))
2097 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
2098 "Test `org-export-table-row-ends-rowgroup-p' specifications."
2099 ;; 1. A row at the end of a table always ends a row group. So does
2100 ;; a row preceding a table rule.
2101 (org-test-with-parsed-data "
2102 | a |
2103 |---|
2104 | b |"
2105 (should
2106 (equal
2107 '(yes no yes)
2108 (org-element-map
2109 tree 'table-row
2110 (lambda (row)
2111 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2112 info))))
2113 ;; 2. Special rows should be ignored when determining the beginning
2114 ;; of the row.
2115 (org-test-with-parsed-data "
2116 | | a |
2117 | / | < |
2118 |---+---|
2119 | | b |
2120 | / | < |"
2121 (should
2122 (equal
2123 '(yes no yes)
2124 (org-element-map
2125 tree 'table-row
2126 (lambda (row)
2127 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2128 info)))))
2130 (ert-deftest test-org-export/table-row-starts-header-p ()
2131 "Test `org-export-table-row-starts-header-p' specifications."
2132 ;; 1. Only the row starting the first row group starts the table
2133 ;; header.
2134 (org-test-with-parsed-data "
2135 | a |
2136 | b |
2137 |---|
2138 | c |"
2139 (should
2140 (equal
2141 '(yes no no no)
2142 (org-element-map
2143 tree 'table-row
2144 (lambda (row)
2145 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
2146 info))))
2147 ;; 2. A row cannot start an header if there's no header in the
2148 ;; table.
2149 (org-test-with-parsed-data "
2150 | a |
2151 |---|"
2152 (should-not
2153 (org-export-table-row-starts-header-p
2154 (org-element-map tree 'table-row 'identity info 'first-match)
2155 info))))
2157 (ert-deftest test-org-export/table-row-ends-header-p ()
2158 "Test `org-export-table-row-ends-header-p' specifications."
2159 ;; 1. Only the row starting the first row group starts the table
2160 ;; header.
2161 (org-test-with-parsed-data "
2162 | a |
2163 | b |
2164 |---|
2165 | c |"
2166 (should
2167 (equal
2168 '(no yes no no)
2169 (org-element-map
2170 tree 'table-row
2171 (lambda (row)
2172 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
2173 info))))
2174 ;; 2. A row cannot start an header if there's no header in the
2175 ;; table.
2176 (org-test-with-parsed-data "
2177 | a |
2178 |---|"
2179 (should-not
2180 (org-export-table-row-ends-header-p
2181 (org-element-map tree 'table-row 'identity info 'first-match)
2182 info))))
2186 ;;; Templates
2188 (ert-deftest test-org-export/inner-template ()
2189 "Test `inner-template' translator specifications."
2190 (should
2191 (equal "Success!"
2192 (let (org-export-registered-backends)
2193 (org-export-define-backend test
2194 ((inner-template . (lambda (contents info) "Success!"))
2195 (headline . (lambda (h c i) "Headline"))))
2196 (org-test-with-temp-text "* Headline"
2197 (org-export-as 'test)))))
2198 ;; Inner template is applied even in a "body-only" export.
2199 (should
2200 (equal "Success!"
2201 (let (org-export-registered-backends)
2202 (org-export-define-backend test
2203 ((inner-template . (lambda (contents info) "Success!"))
2204 (headline . (lambda (h c i) "Headline"))))
2205 (org-test-with-temp-text "* Headline"
2206 (org-export-as 'test nil nil 'body-only))))))
2208 (ert-deftest test-org-export/template ()
2209 "Test `template' translator specifications."
2210 (should
2211 (equal "Success!"
2212 (let (org-export-registered-backends)
2213 (org-export-define-backend test
2214 ((template . (lambda (contents info) "Success!"))
2215 (headline . (lambda (h c i) "Headline"))))
2216 (org-test-with-temp-text "* Headline"
2217 (org-export-as 'test)))))
2218 ;; Template is not applied in a "body-only" export.
2219 (should-not
2220 (equal "Success!"
2221 (let (org-export-registered-backends)
2222 (org-export-define-backend test
2223 ((template . (lambda (contents info) "Success!"))
2224 (headline . (lambda (h c i) "Headline"))))
2225 (org-test-with-temp-text "* Headline"
2226 (org-export-as 'test nil nil 'body-only))))))
2230 ;;; Topology
2232 (ert-deftest test-org-export/get-next-element ()
2233 "Test `org-export-get-next-element' specifications."
2234 ;; Standard test.
2235 (should
2236 (equal "b"
2237 (org-test-with-parsed-data "* Headline\n*a* b"
2238 (org-export-get-next-element
2239 (org-element-map tree 'bold 'identity info t) info))))
2240 ;; Return nil when no previous element.
2241 (should-not
2242 (org-test-with-parsed-data "* Headline\na *b*"
2243 (org-export-get-next-element
2244 (org-element-map tree 'bold 'identity info t) info)))
2245 ;; Non-exportable elements are ignored.
2246 (should-not
2247 (let ((org-export-with-timestamps nil))
2248 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
2249 (org-export-get-next-element
2250 (org-element-map tree 'entity 'identity info t) info))))
2251 ;; Find next element in secondary strings.
2252 (should
2253 (eq 'verbatim
2254 (org-test-with-parsed-data "* a =verb="
2255 (org-element-type
2256 (org-export-get-next-element
2257 (org-element-map tree 'plain-text 'identity info t) info)))))
2258 ;; Find next element in document keywords.
2259 (should
2260 (eq 'verbatim
2261 (org-test-with-parsed-data "#+TITLE: a =verb="
2262 (org-element-type
2263 (org-export-get-next-element
2264 (org-element-map
2265 (plist-get info :title) 'plain-text 'identity info t) info)))))
2266 ;; Find next element in parsed affiliated keywords.
2267 (should
2268 (eq 'verbatim
2269 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
2270 (org-element-type
2271 (org-export-get-next-element
2272 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2273 ;; With optional argument N, return a list containing all the
2274 ;; following elements.
2275 (should
2276 (equal
2277 '(bold code underline)
2278 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2279 (mapcar 'car
2280 (org-export-get-next-element
2281 (org-element-map tree 'italic 'identity info t) info t)))))
2282 ;; When N is a positive integer, return a list containing up to
2283 ;; N following elements.
2284 (should
2285 (equal
2286 '(bold code)
2287 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2288 (mapcar 'car
2289 (org-export-get-next-element
2290 (org-element-map tree 'italic 'identity info t) info 2))))))
2292 (ert-deftest test-org-export/get-previous-element ()
2293 "Test `org-export-get-previous-element' specifications."
2294 ;; Standard test.
2295 (should
2296 (equal "a "
2297 (org-test-with-parsed-data "* Headline\na *b*"
2298 (org-export-get-previous-element
2299 (org-element-map tree 'bold 'identity info t) info))))
2300 ;; Return nil when no previous element.
2301 (should-not
2302 (org-test-with-parsed-data "* Headline\n*a* b"
2303 (org-export-get-previous-element
2304 (org-element-map tree 'bold 'identity info t) info)))
2305 ;; Non-exportable elements are ignored.
2306 (should-not
2307 (let ((org-export-with-timestamps nil))
2308 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
2309 (org-export-get-previous-element
2310 (org-element-map tree 'entity 'identity info t) info))))
2311 ;; Find previous element in secondary strings.
2312 (should
2313 (eq 'verbatim
2314 (org-test-with-parsed-data "* =verb= a"
2315 (org-element-type
2316 (org-export-get-previous-element
2317 (org-element-map tree 'plain-text 'identity info t) info)))))
2318 ;; Find previous element in document keywords.
2319 (should
2320 (eq 'verbatim
2321 (org-test-with-parsed-data "#+TITLE: =verb= a"
2322 (org-element-type
2323 (org-export-get-previous-element
2324 (org-element-map
2325 (plist-get info :title) 'plain-text 'identity info t) info)))))
2326 ;; Find previous element in parsed affiliated keywords.
2327 (should
2328 (eq 'verbatim
2329 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
2330 (org-element-type
2331 (org-export-get-previous-element
2332 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2333 ;; With optional argument N, return a list containing up to
2334 ;; N previous elements.
2335 (should
2336 (equal '(bold italic underline)
2337 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
2338 (mapcar 'car
2339 (org-export-get-previous-element
2340 (org-element-map tree 'code 'identity info t) info t)))))
2341 ;; When N is a positive integer, return a list containing up to
2342 ;; N previous elements.
2343 (should
2344 (equal '(bold italic)
2345 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
2346 (mapcar 'car
2347 (org-export-get-previous-element
2348 (org-element-map tree 'code 'identity info t) info 2))))))
2351 (provide 'test-ox)
2352 ;;; test-org-export.el end here