export-back-ends: Apply changes from b692064e621acbc93876670585f8a4b0fd6a7ffa
[org-mode.git] / testing / lisp / test-ox.el
blob2b5090eff39a2d7eb2f3412a5112ef0ce82a5e01
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 (org-test-with-temp-text
489 (format
490 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
491 org-test-dir)
492 (org-export-expand-include-keyword)
493 (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
494 ;; Inclusion within an example block.
495 (org-test-with-temp-text
496 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
497 org-test-dir)
498 (org-export-expand-include-keyword)
499 (should
500 (equal
501 (buffer-string)
502 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
503 ;; Inclusion within a src-block.
504 (org-test-with-temp-text
505 (format
506 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
507 org-test-dir)
508 (org-export-expand-include-keyword)
509 (should (equal (buffer-string)
510 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
512 (ert-deftest test-org-export/expand-macro ()
513 "Test macro expansion in an Org buffer."
514 ;; Standard macro expansion.
515 (should
516 (equal "#+MACRO: macro1 value\nvalue\n"
517 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
518 (org-test-with-backend test (org-export-as 'test)))))
519 ;; Expand specific macros.
520 (should
521 (equal "me 2012-03-29 me@here Title\n"
522 (org-test-with-temp-text
524 #+TITLE: Title
525 #+DATE: 2012-03-29
526 #+AUTHOR: me
527 #+EMAIL: me@here
528 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
529 (let ((output (org-test-with-backend test (org-export-as 'test))))
530 (substring output (string-match ".*\n\\'" output))))))
531 ;; Expand specific macros when property contained a regular macro
532 ;; already.
533 (should
534 (equal "value\n"
535 (org-test-with-temp-text "
536 #+MACRO: macro1 value
537 #+TITLE: {{{macro1}}}
538 {{{title}}}"
539 (let ((output (org-test-with-backend test (org-export-as 'test))))
540 (substring output (string-match ".*\n\\'" output))))))
541 ;; Expand macros with templates in included files.
542 (should
543 (equal "success\n"
544 (org-test-with-temp-text
545 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
546 {{{included-macro}}}" org-test-dir)
547 (let ((output (org-test-with-backend test (org-export-as 'test))))
548 (substring output (string-match ".*\n\\'" output)))))))
550 (ert-deftest test-org-export/user-ignore-list ()
551 "Test if `:ignore-list' accepts user input."
552 (org-test-with-backend test
553 (flet ((skip-note-head
554 (data backend info)
555 ;; Ignore headlines with the word "note" in their title.
556 (org-element-map
557 data 'headline
558 (lambda (headline)
559 (when (string-match "\\<note\\>"
560 (org-element-property :raw-value headline))
561 (org-export-ignore-element headline info)))
562 info)
563 data))
564 ;; Install function in parse tree filters.
565 (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
566 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
567 (should (equal (org-export-as 'test) "* Head1\n")))))))
569 (ert-deftest test-org-export/before-processing-hook ()
570 "Test `org-export-before-processing-hook'."
571 (should
572 (equal
573 "#+MACRO: mac val\nTest\n"
574 (org-test-with-backend test
575 (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
576 (let ((org-export-before-processing-hook
577 '((lambda (backend)
578 (while (re-search-forward "{{{" nil t)
579 (let ((object (org-element-context)))
580 (when (eq (org-element-type object) 'macro)
581 (delete-region
582 (org-element-property :begin object)
583 (org-element-property :end object)))))))))
584 (org-export-as 'test)))))))
586 (ert-deftest test-org-export/before-parsing-hook ()
587 "Test `org-export-before-parsing-hook'."
588 (should
589 (equal "Body 1\nBody 2\n"
590 (org-test-with-backend test
591 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
592 (let ((org-export-before-parsing-hook
593 '((lambda (backend)
594 (goto-char (point-min))
595 (while (re-search-forward org-outline-regexp-bol nil t)
596 (delete-region
597 (point-at-bol) (progn (forward-line) (point))))))))
598 (org-export-as 'test)))))))
602 ;;; Affiliated Keywords
604 (ert-deftest test-org-export/read-attribute ()
605 "Test `org-export-read-attribute' specifications."
606 ;; Standard test.
607 (should
608 (equal
609 (org-export-read-attribute
610 :attr_html
611 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
612 (org-element-at-point)))
613 '(:a 1 :b 2)))
614 ;; Return nil on empty attribute.
615 (should-not
616 (org-export-read-attribute
617 :attr_html
618 (org-test-with-temp-text "Paragraph" (org-element-at-point)))))
620 (ert-deftest test-org-export/get-caption ()
621 "Test `org-export-get-caption' specifications."
622 ;; Without optional argument, return long caption
623 (should
624 (equal
625 '("l")
626 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
627 (org-export-get-caption (org-element-at-point)))))
628 ;; With optional argument, return short caption.
629 (should
630 (equal
631 '("s")
632 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
633 (org-export-get-caption (org-element-at-point) t))))
634 ;; Multiple lines are separated by white spaces.
635 (should
636 (equal
637 '("a" " " "b")
638 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
639 (org-export-get-caption (org-element-at-point))))))
643 ;;; Back-End Tools
645 (ert-deftest test-org-export/define-backend ()
646 "Test back-end definition and accessors."
647 ;; Translate table.
648 (should
649 (equal '((headline . my-headline-test))
650 (let (org-export-registered-backends)
651 (org-export-define-backend test ((headline . my-headline-test)))
652 (org-export-backend-translate-table 'test))))
653 ;; Filters.
654 (should
655 (equal '((:filter-headline . my-filter))
656 (let (org-export-registered-backends)
657 (org-export-define-backend test
658 ((headline . my-headline-test))
659 :filters-alist ((:filter-headline . my-filter)))
660 (org-export-backend-filters 'test))))
661 ;; Options.
662 (should
663 (equal '((:prop value))
664 (let (org-export-registered-backends)
665 (org-export-define-backend test
666 ((headline . my-headline-test))
667 :options-alist ((:prop value)))
668 (org-export-backend-options 'test))))
669 ;; Menu.
670 (should
671 (equal '(?k "Test Export" test)
672 (let (org-export-registered-backends)
673 (org-export-define-backend test
674 ((headline . my-headline-test))
675 :menu-entry (?k "Test Export" test))
676 (org-export-backend-menu 'test))))
677 ;; Export Blocks.
678 (should
679 (equal '(("TEST" . org-element-export-block-parser))
680 (let (org-export-registered-backends org-element-block-name-alist)
681 (org-export-define-backend test
682 ((headline . my-headline-test))
683 :export-block ("test"))
684 org-element-block-name-alist))))
686 (ert-deftest test-org-export/define-derived-backend ()
687 "Test `org-export-define-derived-backend' specifications."
688 ;; Error when parent back-end is not defined.
689 (should-error
690 (let (org-export-registered-backends)
691 (org-export-define-derived-backend test parent)))
692 ;; Append translation table to parent's.
693 (should
694 (equal '((:headline . test) (:headline . parent))
695 (let (org-export-registered-backends)
696 (org-export-define-backend parent ((:headline . parent)))
697 (org-export-define-derived-backend test parent
698 :translate-alist ((:headline . test)))
699 (org-export-backend-translate-table 'test))))
700 ;; Options defined in the new back have priority over those defined
701 ;; in parent.
702 (should
703 (eq 'test
704 (let (org-export-registered-backends)
705 (org-export-define-backend parent
706 ((:headline . parent))
707 :options-alist ((:a nil nil 'parent)))
708 (org-export-define-derived-backend test parent
709 :options-alist ((:a nil nil 'test)))
710 (plist-get (org-export--get-global-options 'test) :a)))))
712 (ert-deftest test-org-export/derived-backend-p ()
713 "Test `org-export-derived-backend-p' specifications."
714 ;; Non-nil with direct match.
715 (should
716 (let (org-export-registered-backends)
717 (org-export-define-backend test ((headline . test)))
718 (org-export-derived-backend-p 'test 'test)))
719 (should
720 (let (org-export-registered-backends)
721 (org-export-define-backend test ((headline . test)))
722 (org-export-define-derived-backend test2 test)
723 (org-export-derived-backend-p 'test2 'test2)))
724 ;; Non-nil with a direct parent.
725 (should
726 (let (org-export-registered-backends)
727 (org-export-define-backend test ((headline . test)))
728 (org-export-define-derived-backend test2 test)
729 (org-export-derived-backend-p 'test2 'test)))
730 ;; Non-nil with an indirect parent.
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-define-derived-backend test3 test2)
736 (org-export-derived-backend-p 'test3 'test)))
737 ;; Nil otherwise.
738 (should-not
739 (let (org-export-registered-backends)
740 (org-export-define-backend test ((headline . test)))
741 (org-export-define-backend test2 ((headline . test2)))
742 (org-export-derived-backend-p 'test2 'test)))
743 (should-not
744 (let (org-export-registered-backends)
745 (org-export-define-backend test ((headline . test)))
746 (org-export-define-backend test2 ((headline . test2)))
747 (org-export-define-derived-backend test3 test2)
748 (org-export-derived-backend-p 'test3 'test))))
750 (ert-deftest test-org-export/with-backend ()
751 "Test `org-export-with-backend' definition."
752 ;; Error when calling an undefined back-end
753 (should-error
754 (let (org-export-registered-backends)
755 (org-export-with-backend 'test "Test")))
756 ;; Error when called back-end doesn't have an appropriate
757 ;; transcoder.
758 (should-error
759 (let (org-export-registered-backends)
760 (org-export-define-backend test ((headline . ignore)))
761 (org-export-with-backend 'test "Test")))
762 ;; Otherwise, export using correct transcoder
763 (should
764 (equal "Success"
765 (let (org-export-registered-backends)
766 (org-export-define-backend test
767 ((plain-text . (lambda (text contents info) "Failure"))))
768 (org-export-define-backend test2
769 ((plain-text . (lambda (text contents info) "Success"))))
770 (org-export-with-backend 'test2 "Test")))))
772 (ert-deftest test-org-export/data-with-translations ()
773 "Test `org-export-data-with-translations' specifications."
774 (should
775 (equal
776 "Success!"
777 (org-export-data-with-translations
778 '(bold nil "Test")
779 '((plain-text . (lambda (text info) "Success"))
780 (bold . (lambda (bold contents info) (concat contents "!"))))
781 '(:with-emphasize t)))))
783 (ert-deftest test-org-export/data-with-backend ()
784 "Test `org-export-data-with-backend' specifications."
785 ;; Error when calling an undefined back-end.
786 (should-error
787 (let (org-export-registered-backends)
788 (org-export-data-with-backend 'test "Test" nil)))
789 ;; Otherwise, export data recursively, using correct back-end.
790 (should
791 (equal
792 "Success!"
793 (let (org-export-registered-backends)
794 (org-export-define-backend test
795 ((plain-text . (lambda (text info) "Success"))
796 (bold . (lambda (bold contents info) (concat contents "!")))))
797 (org-export-data-with-backend
798 '(bold nil "Test") 'test '(:with-emphasize t))))))
802 ;;; Export Snippets
804 (ert-deftest test-org-export/export-snippet ()
805 "Test export snippets transcoding."
806 (org-test-with-temp-text "@@test:A@@@@t:B@@"
807 (org-test-with-backend test
808 (plist-put
809 (cdr (assq 'test org-export-registered-backends))
810 :translate-alist
811 (cons (cons 'export-snippet
812 (lambda (snippet contents info)
813 (when (eq (org-export-snippet-backend snippet) 'test)
814 (org-element-property :value snippet))))
815 (org-export-backend-translate-table 'test)))
816 (let ((org-export-snippet-translation-alist nil))
817 (should (equal (org-export-as 'test) "A\n")))
818 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
819 (should (equal (org-export-as 'test) "AB\n"))))))
823 ;;; Footnotes
825 (ert-deftest test-org-export/footnotes ()
826 "Test footnotes specifications."
827 (let ((org-footnote-section nil)
828 (org-export-with-footnotes t))
829 ;; 1. Read every type of footnote.
830 (should
831 (equal
832 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
833 (org-test-with-parsed-data
834 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
835 (org-element-map
836 tree 'footnote-reference
837 (lambda (ref)
838 (let ((def (org-export-get-footnote-definition ref info)))
839 (cons (org-export-get-footnote-number ref info)
840 (if (eq (org-element-property :type ref) 'inline) (car def)
841 (car (org-element-contents
842 (car (org-element-contents def))))))))
843 info))))
844 ;; 2. Test nested footnotes order.
845 (org-test-with-parsed-data
846 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
847 (should
848 (equal
849 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
850 (org-element-map
851 tree 'footnote-reference
852 (lambda (ref)
853 (when (org-export-footnote-first-reference-p ref info)
854 (cons (org-export-get-footnote-number ref info)
855 (org-element-property :label ref))))
856 info))))
857 ;; 3. Test nested footnote in invisible definitions.
858 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
859 ;; Hide definitions.
860 (narrow-to-region (point) (point-at-eol))
861 (let* ((tree (org-element-parse-buffer))
862 (info (org-combine-plists
863 `(:parse-tree ,tree)
864 (org-export-collect-tree-properties
865 tree (org-export-get-environment)))))
866 ;; Both footnotes should be seen.
867 (should
868 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
869 ;; 4. Test footnotes definitions collection.
870 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
872 \[fn:2] B [fn:3] [fn::D].
874 \[fn:3] C."
875 (should (= (length (org-export-collect-footnote-definitions tree info))
876 4)))
877 ;; 5. Test export of footnotes defined outside parsing scope.
878 (org-test-with-temp-text "[fn:1] Out of scope
879 * Title
880 Paragraph[fn:1]"
881 (org-test-with-backend test
882 (plist-put
883 (cdr (assq 'test org-export-registered-backends))
884 :translate-alist
885 (cons (cons 'footnote-reference
886 (lambda (fn contents info)
887 (org-element-interpret-data
888 (org-export-get-footnote-definition fn info))))
889 (org-export-backend-translate-table 'test)))
890 (forward-line)
891 (should (equal "ParagraphOut of scope\n"
892 (org-export-as 'test 'subtree)))))
893 ;; 6. Footnotes without a definition should be provided a fallback
894 ;; definition.
895 (should
896 (org-test-with-parsed-data "[fn:1]"
897 (org-export-get-footnote-definition
898 (org-element-map tree 'footnote-reference 'identity info t) info)))
899 ;; 7. Footnote section should be ignored in TOC and in headlines
900 ;; numbering.
901 (should
902 (= 1 (let ((org-footnote-section "Footnotes"))
903 (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
904 (org-export-collect-headlines info))))))
905 (should
906 (equal '(2)
907 (let ((org-footnote-section "Footnotes"))
908 (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
909 (org-element-map tree 'headline
910 (lambda (hl)
911 (when (equal (org-element-property :raw-value hl) "H2")
912 (org-export-get-headline-number hl info)))
913 info t)))))))
917 ;;; Headlines and Inlinetasks
919 (ert-deftest test-org-export/get-relative-level ()
920 "Test `org-export-get-relative-level' specifications."
921 ;; Standard test.
922 (should
923 (equal '(1 2)
924 (let ((org-odd-levels-only nil))
925 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
926 (org-element-map
927 tree 'headline
928 (lambda (h) (org-export-get-relative-level h info))
929 info)))))
930 ;; Missing levels
931 (should
932 (equal '(1 3)
933 (let ((org-odd-levels-only nil))
934 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
935 (org-element-map
936 tree 'headline
937 (lambda (h) (org-export-get-relative-level h info))
938 info))))))
940 (ert-deftest test-org-export/low-level-p ()
941 "Test `org-export-low-level-p' specifications."
942 (should
943 (equal
944 '(no yes)
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) (if (org-export-low-level-p h info) 'yes 'no))
950 (plist-put info :headline-levels 1)))))))
952 (ert-deftest test-org-export/get-headline-number ()
953 "Test `org-export-get-headline-number' specifications."
954 ;; Standard test.
955 (should
956 (equal
957 '((1) (1 1))
958 (let ((org-odd-levels-only nil))
959 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
960 (org-element-map
961 tree 'headline
962 (lambda (h) (org-export-get-headline-number h info))
963 info)))))
964 ;; Missing levels are replaced with 0.
965 (should
966 (equal
967 '((1) (1 0 1))
968 (let ((org-odd-levels-only nil))
969 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
970 (org-element-map
971 tree 'headline
972 (lambda (h) (org-export-get-headline-number h info))
973 info))))))
975 (ert-deftest test-org-export/numbered-headline-p ()
976 "Test `org-export-numbered-headline-p' specifications."
977 ;; If `:section-numbers' is nil, never number headlines.
978 (should-not
979 (org-test-with-parsed-data "* Headline"
980 (org-element-map
981 tree 'headline
982 (lambda (h) (org-export-numbered-headline-p h info))
983 (plist-put info :section-numbers nil))))
984 ;; If `:section-numbers' is a number, only number headlines with
985 ;; a level greater that it.
986 (should
987 (equal
988 '(yes no)
989 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
990 (org-element-map
991 tree 'headline
992 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
993 (plist-put info :section-numbers 1)))))
994 ;; Otherwise, headlines are always numbered.
995 (should
996 (org-test-with-parsed-data "* Headline"
997 (org-element-map
998 tree 'headline
999 (lambda (h) (org-export-numbered-headline-p h info))
1000 (plist-put info :section-numbers t)))))
1002 (ert-deftest test-org-export/number-to-roman ()
1003 "Test `org-export-number-to-roman' specifications."
1004 ;; If number is negative, return it as a string.
1005 (should (equal (org-export-number-to-roman -1) "-1"))
1006 ;; Otherwise, return it as a roman number.
1007 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
1009 (ert-deftest test-org-export/get-tags ()
1010 "Test `org-export-get-tags' specifications."
1011 (let ((org-export-exclude-tags '("noexport"))
1012 (org-export-select-tags '("export")))
1013 ;; Standard test: tags which are not a select tag, an exclude tag,
1014 ;; or specified as optional argument shouldn't be ignored.
1015 (should
1016 (org-test-with-parsed-data "* Headline :tag:"
1017 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1018 info)))
1019 ;; Exclude tags are removed.
1020 (should-not
1021 (org-test-with-parsed-data "* Headline :noexport:"
1022 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1023 info)))
1024 ;; Select tags are removed.
1025 (should-not
1026 (org-test-with-parsed-data "* Headline :export:"
1027 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1028 info)))
1029 (should
1030 (equal
1031 '("tag")
1032 (org-test-with-parsed-data "* Headline :tag:export:"
1033 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1034 info))))
1035 ;; Tags provided in the optional argument are also ignored.
1036 (should-not
1037 (org-test-with-parsed-data "* Headline :ignore:"
1038 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
1039 info '("ignore"))))
1040 ;; Allow tag inheritance.
1041 (should
1042 (equal
1043 '(("tag") ("tag"))
1044 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
1045 (org-element-map
1046 tree 'headline
1047 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
1048 ;; Tag inheritance checks FILETAGS keywords.
1049 (should
1050 (equal
1051 '(("a" "b" "tag"))
1052 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
1053 (org-element-map
1054 tree 'headline
1055 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
1057 (ert-deftest test-org-export/get-node-property ()
1058 "Test`org-export-get-node-property' specifications."
1059 ;; Standard test.
1060 (should
1061 (equal "value"
1062 (org-test-with-parsed-data "* Headline
1063 :PROPERTIES:
1064 :prop: value
1065 :END:"
1066 (org-export-get-node-property
1067 :PROP (org-element-map tree 'headline 'identity nil t)))))
1068 ;; Test inheritance.
1069 (should
1070 (equal "value"
1071 (org-test-with-parsed-data "* Parent
1072 :PROPERTIES:
1073 :prop: value
1074 :END:
1075 ** Headline
1076 Paragraph"
1077 (org-export-get-node-property
1078 :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
1079 ;; Cannot return a value before the first headline.
1080 (should-not
1081 (org-test-with-parsed-data "Paragraph
1082 * Headline
1083 :PROPERTIES:
1084 :prop: value
1085 :END:"
1086 (org-export-get-node-property
1087 :PROP (org-element-map tree 'paragraph 'identity nil t)))))
1089 (ert-deftest test-org-export/get-category ()
1090 "Test `org-export-get-category' specifications."
1091 ;; Standard test.
1092 (should
1093 (equal "value"
1094 (org-test-with-parsed-data "* Headline
1095 :PROPERTIES:
1096 :CATEGORY: value
1097 :END:"
1098 (org-export-get-category
1099 (org-element-map tree 'headline 'identity nil t) info))))
1100 ;; Test inheritance from a parent headline.
1101 (should
1102 (equal '("value" "value")
1103 (org-test-with-parsed-data "* Headline1
1104 :PROPERTIES:
1105 :CATEGORY: value
1106 :END:
1107 ** Headline2"
1108 (org-element-map
1109 tree 'headline
1110 (lambda (hl) (org-export-get-category hl info)) info))))
1111 ;; Test inheritance from #+CATEGORY keyword
1112 (should
1113 (equal "value"
1114 (org-test-with-parsed-data "#+CATEGORY: value
1115 * Headline"
1116 (org-export-get-category
1117 (org-element-map tree 'headline 'identity nil t) info))))
1118 ;; Test inheritance from file name.
1119 (should
1120 (equal "test"
1121 (org-test-with-parsed-data "* Headline"
1122 (let ((info (plist-put info :input-file "~/test.org")))
1123 (org-export-get-category
1124 (org-element-map tree 'headline 'identity nil t) info)))))
1125 ;; Fall-back value.
1126 (should
1127 (equal "???"
1128 (org-test-with-parsed-data "* Headline"
1129 (org-export-get-category
1130 (org-element-map tree 'headline 'identity nil t) info)))))
1132 (ert-deftest test-org-export/first-sibling-p ()
1133 "Test `org-export-first-sibling-p' specifications."
1134 ;; Standard test.
1135 (should
1136 (equal
1137 '(yes yes no)
1138 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1139 (org-element-map
1140 tree 'headline
1141 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1142 info))))
1143 ;; Ignore headlines not exported.
1144 (should
1145 (equal
1146 '(yes)
1147 (let ((org-export-exclude-tags '("ignore")))
1148 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
1149 (org-element-map
1150 tree 'headline
1151 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
1152 info))))))
1154 (ert-deftest test-org-export/last-sibling-p ()
1155 "Test `org-export-last-sibling-p' specifications."
1156 ;; Standard test.
1157 (should
1158 (equal
1159 '(yes no yes)
1160 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
1161 (org-element-map
1162 tree 'headline
1163 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1164 info))))
1165 ;; Ignore headlines not exported.
1166 (should
1167 (equal
1168 '(yes)
1169 (let ((org-export-exclude-tags '("ignore")))
1170 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
1171 (org-element-map
1172 tree 'headline
1173 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
1174 info))))))
1178 ;;; Links
1180 (ert-deftest test-org-export/get-coderef-format ()
1181 "Test `org-export-get-coderef-format' specifications."
1182 ;; A link without description returns "%s"
1183 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
1184 "%s"))
1185 ;; Return "%s" when path is matched within description.
1186 (should (equal (org-export-get-coderef-format "path" "desc (path)")
1187 "desc %s"))
1188 ;; Otherwise return description.
1189 (should (equal (org-export-get-coderef-format "path" "desc")
1190 "desc")))
1192 (ert-deftest test-org-export/inline-image-p ()
1193 "Test `org-export-inline-image-p' specifications."
1194 (should
1195 (org-export-inline-image-p
1196 (org-test-with-temp-text "[[#id]]"
1197 (org-element-map
1198 (org-element-parse-buffer) 'link 'identity nil t))
1199 '(("custom-id" . "id")))))
1201 (ert-deftest test-org-export/fuzzy-link ()
1202 "Test fuzzy links specifications."
1203 ;; 1. Links to invisible (keyword) targets should be ignored.
1204 (org-test-with-parsed-data
1205 "Paragraph.\n#+TARGET: Test\n[[Test]]"
1206 (should-not
1207 (org-element-map
1208 tree 'link
1209 (lambda (link)
1210 (org-export-get-ordinal
1211 (org-export-resolve-fuzzy-link link info) info)) info)))
1212 ;; 2. Link to an headline should return headline's number.
1213 (org-test-with-parsed-data
1214 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
1215 (should
1216 ;; Note: Headline's number is in fact a list of numbers.
1217 (equal '(2)
1218 (org-element-map
1219 tree 'link
1220 (lambda (link)
1221 (org-export-get-ordinal
1222 (org-export-resolve-fuzzy-link link info) info)) info t))))
1223 ;; 3. Link to a target in an item should return item's number.
1224 (org-test-with-parsed-data
1225 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
1226 (should
1227 ;; Note: Item's number is in fact a list of numbers.
1228 (equal '(1 2)
1229 (org-element-map
1230 tree 'link
1231 (lambda (link)
1232 (org-export-get-ordinal
1233 (org-export-resolve-fuzzy-link link info) info)) info t))))
1234 ;; 4. Link to a target in a footnote should return footnote's
1235 ;; number.
1236 (org-test-with-parsed-data "
1237 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
1238 (should
1239 (equal '(2 3)
1240 (org-element-map
1241 tree 'link
1242 (lambda (link)
1243 (org-export-get-ordinal
1244 (org-export-resolve-fuzzy-link link info) info)) info))))
1245 ;; 5. Link to a named element should return sequence number of that
1246 ;; element.
1247 (org-test-with-parsed-data
1248 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
1249 (should
1250 (= 2
1251 (org-element-map
1252 tree 'link
1253 (lambda (link)
1254 (org-export-get-ordinal
1255 (org-export-resolve-fuzzy-link link info) info)) info t))))
1256 ;; 6. Link to a target not within an item, a table, a footnote
1257 ;; reference or definition should return section number.
1258 (org-test-with-parsed-data
1259 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
1260 (should
1261 (equal '(2)
1262 (org-element-map
1263 tree 'link
1264 (lambda (link)
1265 (org-export-get-ordinal
1266 (org-export-resolve-fuzzy-link link info) info)) info t))))
1267 ;; 7. Space are not significant when matching a fuzzy link.
1268 (should
1269 (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
1270 (org-element-map tree 'link
1271 (lambda (link) (org-export-resolve-fuzzy-link link info))
1272 info t)))
1273 ;; 8. Statistics cookies are ignored for headline match.
1274 (should
1275 (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
1276 (org-element-map tree 'link
1277 (lambda (link) (org-export-resolve-fuzzy-link link info))
1278 info t)))
1279 (should
1280 (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
1281 (org-element-map tree 'link
1282 (lambda (link) (org-export-resolve-fuzzy-link link info))
1283 info t))))
1285 (ert-deftest test-org-export/resolve-coderef ()
1286 "Test `org-export-resolve-coderef' specifications."
1287 (let ((org-coderef-label-format "(ref:%s)"))
1288 ;; 1. A link to a "-n -k -r" block returns line number.
1289 (org-test-with-parsed-data
1290 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
1291 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1292 (org-test-with-parsed-data
1293 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1294 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1295 ;; 2. A link to a "-n -r" block returns line number.
1296 (org-test-with-parsed-data
1297 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
1298 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1299 (org-test-with-parsed-data
1300 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1301 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1302 ;; 3. A link to a "-n" block returns coderef.
1303 (org-test-with-parsed-data
1304 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1305 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1306 (org-test-with-parsed-data
1307 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
1308 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1309 ;; 4. A link to a "-r" block returns line number.
1310 (org-test-with-parsed-data
1311 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1312 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1313 (org-test-with-parsed-data
1314 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
1315 (should (= (org-export-resolve-coderef "coderef" info) 1)))
1316 ;; 5. A link to a block without a switch returns coderef.
1317 (org-test-with-parsed-data
1318 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
1319 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1320 (org-test-with-parsed-data
1321 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
1322 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
1323 ;; 6. Correctly handle continued line numbers. A "+n" switch
1324 ;; should resume numbering from previous block with numbered
1325 ;; lines, ignoring blocks not numbering lines in the process.
1326 ;; A "-n" switch resets count.
1327 (org-test-with-parsed-data "
1328 #+BEGIN_EXAMPLE -n
1329 Text.
1330 #+END_EXAMPLE
1332 #+BEGIN_SRC emacs-lisp
1333 \(- 1 1)
1334 #+END_SRC
1336 #+BEGIN_SRC emacs-lisp +n -r
1337 \(+ 1 1) (ref:addition)
1338 #+END_SRC
1340 #+BEGIN_EXAMPLE -n -r
1341 Another text. (ref:text)
1342 #+END_EXAMPLE"
1343 (should (= (org-export-resolve-coderef "addition" info) 2))
1344 (should (= (org-export-resolve-coderef "text" info) 1)))
1345 ;; 7. Recognize coderef with user-specified syntax.
1346 (org-test-with-parsed-data
1347 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
1348 (should (equal (org-export-resolve-coderef "text" info) "text")))))
1350 (ert-deftest test-org-export/resolve-fuzzy-link ()
1351 "Test `org-export-resolve-fuzzy-link' specifications."
1352 ;; 1. Match target objects.
1353 (org-test-with-parsed-data "<<target>> [[target]]"
1354 (should
1355 (org-export-resolve-fuzzy-link
1356 (org-element-map tree 'link 'identity info t) info)))
1357 ;; 2. Match target elements.
1358 (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
1359 (should
1360 (org-export-resolve-fuzzy-link
1361 (org-element-map tree 'link 'identity info t) info)))
1362 ;; 3. Match named elements.
1363 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
1364 (should
1365 (org-export-resolve-fuzzy-link
1366 (org-element-map tree 'link 'identity info t) info)))
1367 ;; 4. Match exact headline's name.
1368 (org-test-with-parsed-data "* My headline\n[[My headline]]"
1369 (should
1370 (org-export-resolve-fuzzy-link
1371 (org-element-map tree 'link 'identity info t) info)))
1372 ;; 5. Targets objects have priority over named elements and headline
1373 ;; titles.
1374 (org-test-with-parsed-data
1375 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
1376 (should
1377 (eq 'target
1378 (org-element-type
1379 (org-export-resolve-fuzzy-link
1380 (org-element-map tree 'link 'identity info t) info)))))
1381 ;; 6. Named elements have priority over headline titles.
1382 (org-test-with-parsed-data
1383 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
1384 (should
1385 (eq 'paragraph
1386 (org-element-type
1387 (org-export-resolve-fuzzy-link
1388 (org-element-map tree 'link 'identity info t) info)))))
1389 ;; 7. If link's path starts with a "*", only match headline titles,
1390 ;; though.
1391 (org-test-with-parsed-data
1392 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
1393 (should
1394 (eq 'headline
1395 (org-element-type
1396 (org-export-resolve-fuzzy-link
1397 (org-element-map tree 'link 'identity info t) info)))))
1398 ;; 8. Return nil if no match.
1399 (org-test-with-parsed-data "[[target]]"
1400 (should-not
1401 (org-export-resolve-fuzzy-link
1402 (org-element-map tree 'link 'identity info t) info))))
1404 (ert-deftest test-org-export/resolve-id-link ()
1405 "Test `org-export-resolve-id-link' specifications."
1406 ;; 1. Regular test for custom-id link.
1407 (org-test-with-parsed-data "* Headline1
1408 :PROPERTIES:
1409 :CUSTOM_ID: test
1410 :END:
1411 * Headline 2
1412 \[[#test]]"
1413 (should
1414 (org-export-resolve-id-link
1415 (org-element-map tree 'link 'identity info t) info)))
1416 ;; 2. Failing test for custom-id link.
1417 (org-test-with-parsed-data "* Headline1
1418 :PROPERTIES:
1419 :CUSTOM_ID: test
1420 :END:
1421 * Headline 2
1422 \[[#no-match]]"
1423 (should-not
1424 (org-export-resolve-id-link
1425 (org-element-map tree 'link 'identity info t) info)))
1426 ;; 3. Test for internal id target.
1427 (org-test-with-parsed-data "* Headline1
1428 :PROPERTIES:
1429 :ID: aaaa
1430 :END:
1431 * Headline 2
1432 \[[id:aaaa]]"
1433 (should
1434 (org-export-resolve-id-link
1435 (org-element-map tree 'link 'identity info t) info)))
1436 ;; 4. Test for external id target.
1437 (org-test-with-parsed-data "[[id:aaaa]]"
1438 (should
1439 (org-export-resolve-id-link
1440 (org-element-map tree 'link 'identity info t)
1441 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
1443 (ert-deftest test-org-export/resolve-radio-link ()
1444 "Test `org-export-resolve-radio-link' specifications."
1445 ;; Standard test.
1446 (should
1447 (org-test-with-temp-text "<<<radio>>> radio"
1448 (org-update-radio-target-regexp)
1449 (let* ((tree (org-element-parse-buffer))
1450 (info `(:parse-tree ,tree)))
1451 (org-export-resolve-radio-link
1452 (org-element-map tree 'link 'identity info t)
1453 info))))
1454 ;; Radio targets are case-insensitive.
1455 (should
1456 (org-test-with-temp-text "<<<RADIO>>> radio"
1457 (org-update-radio-target-regexp)
1458 (let* ((tree (org-element-parse-buffer))
1459 (info `(:parse-tree ,tree)))
1460 (org-export-resolve-radio-link
1461 (org-element-map tree 'link 'identity info t)
1462 info))))
1463 ;; Radio target with objects.
1464 (should
1465 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
1466 (org-update-radio-target-regexp)
1467 (let* ((tree (org-element-parse-buffer))
1468 (info `(:parse-tree ,tree)))
1469 (org-export-resolve-radio-link
1470 (org-element-map tree 'link 'identity info t)
1471 info)))))
1475 ;;; Src-block and example-block
1477 (ert-deftest test-org-export/unravel-code ()
1478 "Test `org-export-unravel-code' function."
1479 (let ((org-coderef-label-format "(ref:%s)"))
1480 ;; 1. Code without reference.
1481 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
1482 (should (equal (org-export-unravel-code (org-element-at-point))
1483 '("(+ 1 1)\n"))))
1484 ;; 2. Code with reference.
1485 (org-test-with-temp-text
1486 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
1487 (should (equal (org-export-unravel-code (org-element-at-point))
1488 '("(+ 1 1)\n" (1 . "test")))))
1489 ;; 3. Code with user-defined reference.
1490 (org-test-with-temp-text
1491 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
1492 (should (equal (org-export-unravel-code (org-element-at-point))
1493 '("(+ 1 1)\n" (1 . "test")))))
1494 ;; 4. Code references keys are relative to the current block.
1495 (org-test-with-temp-text "
1496 #+BEGIN_EXAMPLE -n
1497 \(+ 1 1)
1498 #+END_EXAMPLE
1499 #+BEGIN_EXAMPLE +n
1500 \(+ 2 2)
1501 \(+ 3 3) (ref:one)
1502 #+END_EXAMPLE"
1503 (goto-line 5)
1504 (should (equal (org-export-unravel-code (org-element-at-point))
1505 '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
1509 ;;; Smart Quotes
1511 (ert-deftest test-org-export/activate-smart-quotes ()
1512 "Test `org-export-activate-smart-quotes' specifications."
1513 ;; Opening double quotes: standard test.
1514 (should
1515 (equal
1516 '("some &ldquo;paragraph")
1517 (let ((org-export-default-language "en"))
1518 (org-test-with-parsed-data "some \"paragraph"
1519 (org-element-map
1520 tree 'plain-text
1521 (lambda (s) (org-export-activate-smart-quotes s :html info))
1522 info)))))
1523 ;; Opening quotes: at the beginning of a paragraph.
1524 (should
1525 (equal
1526 '("&ldquo;begin")
1527 (let ((org-export-default-language "en"))
1528 (org-test-with-parsed-data "\"begin"
1529 (org-element-map
1530 tree 'plain-text
1531 (lambda (s) (org-export-activate-smart-quotes s :html info))
1532 info)))))
1533 ;; Opening quotes: after an object.
1534 (should
1535 (equal
1536 '("&ldquo;begin")
1537 (let ((org-export-default-language "en"))
1538 (org-test-with-parsed-data "=verb= \"begin"
1539 (org-element-map
1540 tree 'plain-text
1541 (lambda (s) (org-export-activate-smart-quotes s :html info))
1542 info)))))
1543 ;; Closing quotes: standard test.
1544 (should
1545 (equal
1546 '("some&rdquo; 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 ;; Closing quotes: at the end of a paragraph.
1554 (should
1555 (equal
1556 '("end&rdquo;")
1557 (let ((org-export-default-language "en"))
1558 (org-test-with-parsed-data "end\""
1559 (org-element-map
1560 tree 'plain-text
1561 (lambda (s) (org-export-activate-smart-quotes s :html info))
1562 info)))))
1563 ;; Apostrophe: standard test.
1564 (should
1565 (equal
1566 '("It shouldn&rsquo;t fail")
1567 (let ((org-export-default-language "en"))
1568 (org-test-with-parsed-data "It shouldn't fail"
1569 (org-element-map
1570 tree 'plain-text
1571 (lambda (s) (org-export-activate-smart-quotes s :html info))
1572 info)))))
1573 ;; Apostrophe: before an object.
1574 (should
1575 (equal
1576 '("a&rsquo;")
1577 (let ((org-export-default-language "en"))
1578 (org-test-with-parsed-data "a'=b="
1579 (org-element-map
1580 tree 'plain-text
1581 (lambda (s) (org-export-activate-smart-quotes s :html info))
1582 info)))))
1583 ;; Apostrophe: after an object.
1584 (should
1585 (equal
1586 '("&rsquo;s")
1587 (let ((org-export-default-language "en"))
1588 (org-test-with-parsed-data "=code='s"
1589 (org-element-map
1590 tree 'plain-text
1591 (lambda (s) (org-export-activate-smart-quotes s :html info))
1592 info)))))
1593 ;; Special case: isolated quotes.
1594 (should
1595 (equal '("&ldquo;" "&rdquo;")
1596 (let ((org-export-default-language "en"))
1597 (org-test-with-parsed-data "\"$x$\""
1598 (org-element-map
1599 tree 'plain-text
1600 (lambda (s) (org-export-activate-smart-quotes s :html info))
1601 info)))))
1602 ;; Smart quotes in secondary strings.
1603 (should
1604 (equal '("&ldquo;" "&rdquo;")
1605 (let ((org-export-default-language "en"))
1606 (org-test-with-parsed-data "* \"$x$\""
1607 (org-element-map
1608 tree 'plain-text
1609 (lambda (s) (org-export-activate-smart-quotes s :html info))
1610 info)))))
1611 ;; Smart quotes in document keywords.
1612 (should
1613 (equal '("&ldquo;" "&rdquo;")
1614 (let ((org-export-default-language "en"))
1615 (org-test-with-parsed-data "#+TITLE: \"$x$\""
1616 (org-element-map
1617 (plist-get info :title) 'plain-text
1618 (lambda (s) (org-export-activate-smart-quotes s :html info))
1619 info)))))
1620 ;; Smart quotes in parsed affiliated keywords.
1621 (should
1622 (equal '("&ldquo;" "&rdquo;" "Paragraph")
1623 (let ((org-export-default-language "en"))
1624 (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
1625 (org-element-map
1626 tree 'plain-text
1627 (lambda (s) (org-export-activate-smart-quotes s :html info))
1628 info nil nil t))))))
1632 ;;; Tables
1634 (ert-deftest test-org-export/special-column ()
1635 "Test if the table's special column is properly recognized."
1636 ;; 1. First column is special if it contains only a special marking
1637 ;; characters or empty cells.
1638 (org-test-with-temp-text "
1639 | ! | 1 |
1640 | | 2 |"
1641 (should
1642 (org-export-table-has-special-column-p
1643 (org-element-map
1644 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1645 ;; 2. If the column contains anything else, it isn't special.
1646 (org-test-with-temp-text "
1647 | ! | 1 |
1648 | b | 2 |"
1649 (should-not
1650 (org-export-table-has-special-column-p
1651 (org-element-map
1652 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1653 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
1654 ;; and "!".
1655 (org-test-with-temp-text "
1656 | # | 1 |
1657 | ^ | 2 |
1658 | * | 3 |
1659 | _ | 4 |
1660 | / | 5 |
1661 | $ | 6 |
1662 | ! | 7 |"
1663 (should
1664 (org-export-table-has-special-column-p
1665 (org-element-map
1666 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1667 ;; 4. A first column with only empty cells isn't considered as
1668 ;; special.
1669 (org-test-with-temp-text "
1670 | | 1 |
1671 | | 2 |"
1672 (should-not
1673 (org-export-table-has-special-column-p
1674 (org-element-map
1675 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
1677 (ert-deftest test-org-export/table-row-is-special-p ()
1678 "Test `org-export-table-row-is-special-p' specifications."
1679 ;; 1. A row is special if it has a special marking character in the
1680 ;; special column.
1681 (org-test-with-parsed-data "| ! | 1 |"
1682 (should
1683 (org-export-table-row-is-special-p
1684 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1685 ;; 2. A row is special when its first field is "/"
1686 (org-test-with-parsed-data "
1687 | / | 1 |
1688 | a | b |"
1689 (should
1690 (org-export-table-row-is-special-p
1691 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1692 ;; 3. A row only containing alignment cookies is also considered as
1693 ;; special.
1694 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
1695 (should
1696 (org-export-table-row-is-special-p
1697 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1698 ;; 4. Everything else isn't considered as special.
1699 (org-test-with-parsed-data "| \alpha | | c |"
1700 (should-not
1701 (org-export-table-row-is-special-p
1702 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1703 ;; 5. Table's rules are never considered as special rows.
1704 (org-test-with-parsed-data "|---+---|"
1705 (should-not
1706 (org-export-table-row-is-special-p
1707 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
1709 (ert-deftest test-org-export/has-header-p ()
1710 "Test `org-export-table-has-header-p' specifications."
1711 ;; 1. With an header.
1712 (org-test-with-parsed-data "
1713 | a | b |
1714 |---+---|
1715 | c | d |"
1716 (should
1717 (org-export-table-has-header-p
1718 (org-element-map tree 'table 'identity info 'first-match)
1719 info)))
1720 ;; 2. Without an header.
1721 (org-test-with-parsed-data "
1722 | a | b |
1723 | c | d |"
1724 (should-not
1725 (org-export-table-has-header-p
1726 (org-element-map tree 'table 'identity info 'first-match)
1727 info)))
1728 ;; 3. Don't get fooled with starting and ending rules.
1729 (org-test-with-parsed-data "
1730 |---+---|
1731 | a | b |
1732 | c | d |
1733 |---+---|"
1734 (should-not
1735 (org-export-table-has-header-p
1736 (org-element-map tree 'table 'identity info 'first-match)
1737 info))))
1739 (ert-deftest test-org-export/table-row-group ()
1740 "Test `org-export-table-row-group' specifications."
1741 ;; 1. A rule creates a new group.
1742 (org-test-with-parsed-data "
1743 | a | b |
1744 |---+---|
1745 | 1 | 2 |"
1746 (should
1747 (equal
1748 '(1 nil 2)
1749 (mapcar (lambda (row) (org-export-table-row-group row info))
1750 (org-element-map tree 'table-row 'identity)))))
1751 ;; 2. Special rows are ignored in count.
1752 (org-test-with-parsed-data "
1753 | / | < | > |
1754 |---|---+---|
1755 | | 1 | 2 |"
1756 (should
1757 (equal
1758 '(nil nil 1)
1759 (mapcar (lambda (row) (org-export-table-row-group row info))
1760 (org-element-map tree 'table-row 'identity)))))
1761 ;; 3. Double rules also are ignored in count.
1762 (org-test-with-parsed-data "
1763 | a | b |
1764 |---+---|
1765 |---+---|
1766 | 1 | 2 |"
1767 (should
1768 (equal
1769 '(1 nil nil 2)
1770 (mapcar (lambda (row) (org-export-table-row-group row info))
1771 (org-element-map tree 'table-row 'identity))))))
1773 (ert-deftest test-org-export/table-cell-width ()
1774 "Test `org-export-table-cell-width' specifications."
1775 ;; 1. Width is primarily determined by width cookies. If no cookie
1776 ;; is found, cell's width is nil.
1777 (org-test-with-parsed-data "
1778 | / | <l> | <6> | <l7> |
1779 | | a | b | c |"
1780 (should
1781 (equal
1782 '(nil 6 7)
1783 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1784 (org-element-map tree 'table-cell 'identity info)))))
1785 ;; 2. The last width cookie has precedence.
1786 (org-test-with-parsed-data "
1787 | <6> |
1788 | <7> |
1789 | a |"
1790 (should
1791 (equal
1792 '(7)
1793 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1794 (org-element-map tree 'table-cell 'identity info)))))
1795 ;; 3. Valid width cookies must have a specific row.
1796 (org-test-with-parsed-data "| <6> | cell |"
1797 (should
1798 (equal
1799 '(nil nil)
1800 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1801 (org-element-map tree 'table-cell 'identity))))))
1803 (ert-deftest test-org-export/table-cell-alignment ()
1804 "Test `org-export-table-cell-alignment' specifications."
1805 (let ((org-table-number-fraction 0.5)
1806 (org-table-number-regexp "^[0-9]+$"))
1807 ;; 1. Alignment is primarily determined by alignment cookies.
1808 (org-test-with-temp-text "| <l> | <c> | <r> |"
1809 (let* ((tree (org-element-parse-buffer))
1810 (info `(:parse-tree ,tree)))
1811 (should
1812 (equal
1813 '(left center right)
1814 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
1815 (org-element-map tree 'table-cell 'identity))))))
1816 ;; 2. The last alignment cookie has precedence.
1817 (org-test-with-parsed-data "
1818 | <l8> |
1819 | cell |
1820 | <r9> |"
1821 (should
1822 (equal
1823 '(right right right)
1824 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
1825 (org-element-map tree 'table-cell 'identity)))))
1826 ;; 3. If there's no cookie, cell's contents determine alignment.
1827 ;; A column mostly made of cells containing numbers will align
1828 ;; its cells to the right.
1829 (org-test-with-parsed-data "
1830 | 123 |
1831 | some text |
1832 | 12345 |"
1833 (should
1834 (equal
1835 '(right right right)
1836 (mapcar (lambda (cell)
1837 (org-export-table-cell-alignment cell info))
1838 (org-element-map tree 'table-cell 'identity)))))
1839 ;; 4. Otherwise, they will be aligned to the left.
1840 (org-test-with-parsed-data "
1841 | text |
1842 | some text |
1843 | \alpha |"
1844 (should
1845 (equal
1846 '(left left left)
1847 (mapcar (lambda (cell)
1848 (org-export-table-cell-alignment cell info))
1849 (org-element-map tree 'table-cell 'identity)))))))
1851 (ert-deftest test-org-export/table-cell-borders ()
1852 "Test `org-export-table-cell-borders' specifications."
1853 ;; 1. Recognize various column groups indicators.
1854 (org-test-with-parsed-data "| / | < | > | <> |"
1855 (should
1856 (equal
1857 '((right bottom top) (left bottom top) (right bottom top)
1858 (right left bottom top))
1859 (mapcar (lambda (cell)
1860 (org-export-table-cell-borders cell info))
1861 (org-element-map tree 'table-cell 'identity)))))
1862 ;; 2. Accept shortcuts to define column groups.
1863 (org-test-with-parsed-data "| / | < | < |"
1864 (should
1865 (equal
1866 '((right bottom top) (right left bottom top) (left bottom top))
1867 (mapcar (lambda (cell)
1868 (org-export-table-cell-borders cell info))
1869 (org-element-map tree 'table-cell 'identity)))))
1870 ;; 3. A valid column groups row must start with a "/".
1871 (org-test-with-parsed-data "
1872 | | < |
1873 | a | b |"
1874 (should
1875 (equal '((top) (top) (bottom) (bottom))
1876 (mapcar (lambda (cell)
1877 (org-export-table-cell-borders cell info))
1878 (org-element-map tree 'table-cell 'identity)))))
1879 ;; 4. Take table rules into consideration.
1880 (org-test-with-parsed-data "
1881 | 1 |
1882 |---|
1883 | 2 |"
1884 (should
1885 (equal '((below top) (bottom above))
1886 (mapcar (lambda (cell)
1887 (org-export-table-cell-borders cell info))
1888 (org-element-map tree 'table-cell 'identity)))))
1889 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
1890 ;; (resp. `bottom' and `below') borders. Any special row is
1891 ;; ignored.
1892 (org-test-with-parsed-data "
1893 |---+----|
1894 | / | |
1895 | | 1 |
1896 |---+----|"
1897 (should
1898 (equal '((bottom below top above))
1899 (last
1900 (mapcar (lambda (cell)
1901 (org-export-table-cell-borders cell info))
1902 (org-element-map tree 'table-cell 'identity)))))))
1904 (ert-deftest test-org-export/table-dimensions ()
1905 "Test `org-export-table-dimensions' specifications."
1906 ;; 1. Standard test.
1907 (org-test-with-parsed-data "
1908 | 1 | 2 | 3 |
1909 | 4 | 5 | 6 |"
1910 (should
1911 (equal '(2 . 3)
1912 (org-export-table-dimensions
1913 (org-element-map tree 'table 'identity info 'first-match) info))))
1914 ;; 2. Ignore horizontal rules and special columns.
1915 (org-test-with-parsed-data "
1916 | / | < | > |
1917 | 1 | 2 | 3 |
1918 |---+---+---|
1919 | 4 | 5 | 6 |"
1920 (should
1921 (equal '(2 . 3)
1922 (org-export-table-dimensions
1923 (org-element-map tree 'table 'identity info 'first-match) info)))))
1925 (ert-deftest test-org-export/table-cell-address ()
1926 "Test `org-export-table-cell-address' specifications."
1927 ;; 1. Standard test: index is 0-based.
1928 (org-test-with-parsed-data "| a | b |"
1929 (should
1930 (equal '((0 . 0) (0 . 1))
1931 (org-element-map
1932 tree 'table-cell
1933 (lambda (cell) (org-export-table-cell-address cell info))
1934 info))))
1935 ;; 2. Special column isn't counted, nor are special rows.
1936 (org-test-with-parsed-data "
1937 | / | <> |
1938 | | c |"
1939 (should
1940 (equal '(0 . 0)
1941 (org-export-table-cell-address
1942 (car (last (org-element-map tree 'table-cell 'identity info)))
1943 info))))
1944 ;; 3. Tables rules do not count either.
1945 (org-test-with-parsed-data "
1946 | a |
1947 |---|
1948 | b |
1949 |---|
1950 | c |"
1951 (should
1952 (equal '(2 . 0)
1953 (org-export-table-cell-address
1954 (car (last (org-element-map tree 'table-cell 'identity info)))
1955 info))))
1956 ;; 4. Return nil for special cells.
1957 (org-test-with-parsed-data "| / | a |"
1958 (should-not
1959 (org-export-table-cell-address
1960 (org-element-map tree 'table-cell 'identity nil 'first-match)
1961 info))))
1963 (ert-deftest test-org-export/get-table-cell-at ()
1964 "Test `org-export-get-table-cell-at' specifications."
1965 ;; 1. Address ignores special columns, special rows and rules.
1966 (org-test-with-parsed-data "
1967 | / | <> |
1968 | | a |
1969 |---+----|
1970 | | b |"
1971 (should
1972 (equal '("b")
1973 (org-element-contents
1974 (org-export-get-table-cell-at
1975 '(1 . 0)
1976 (org-element-map tree 'table 'identity info 'first-match)
1977 info)))))
1978 ;; 2. Return value for a non-existent address is nil.
1979 (org-test-with-parsed-data "| a |"
1980 (should-not
1981 (org-export-get-table-cell-at
1982 '(2 . 2)
1983 (org-element-map tree 'table 'identity info 'first-match)
1984 info)))
1985 (org-test-with-parsed-data "| / |"
1986 (should-not
1987 (org-export-get-table-cell-at
1988 '(0 . 0)
1989 (org-element-map tree 'table 'identity info 'first-match)
1990 info))))
1992 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
1993 "Test `org-export-table-cell-starts-colgroup-p' specifications."
1994 ;; 1. A cell at a beginning of a row always starts a column group.
1995 (org-test-with-parsed-data "| a |"
1996 (should
1997 (org-export-table-cell-starts-colgroup-p
1998 (org-element-map tree 'table-cell 'identity info 'first-match)
1999 info)))
2000 ;; 2. Special column should be ignored when determining the
2001 ;; beginning of the row.
2002 (org-test-with-parsed-data "
2003 | / | |
2004 | | a |"
2005 (should
2006 (org-export-table-cell-starts-colgroup-p
2007 (org-element-map tree 'table-cell 'identity info 'first-match)
2008 info)))
2009 ;; 2. Explicit column groups.
2010 (org-test-with-parsed-data "
2011 | / | | < |
2012 | a | b | c |"
2013 (should
2014 (equal
2015 '(yes no yes)
2016 (org-element-map
2017 tree 'table-cell
2018 (lambda (cell)
2019 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
2020 info)))))
2022 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
2023 "Test `org-export-table-cell-ends-colgroup-p' specifications."
2024 ;; 1. A cell at the end of a row always ends a column group.
2025 (org-test-with-parsed-data "| a |"
2026 (should
2027 (org-export-table-cell-ends-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-ends-colgroup-p
2037 (org-element-map tree 'table-cell 'identity info 'first-match)
2038 info)))
2039 ;; 3. 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-ends-colgroup-p cell info) 'yes 'no))
2050 info)))))
2052 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
2053 "Test `org-export-table-row-starts-rowgroup-p' specifications."
2054 ;; 1. A row at the beginning of a table always starts a row group.
2055 ;; So does a row following a table rule.
2056 (org-test-with-parsed-data "
2057 | a |
2058 |---|
2059 | b |"
2060 (should
2061 (equal
2062 '(yes no yes)
2063 (org-element-map
2064 tree 'table-row
2065 (lambda (row)
2066 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2067 info))))
2068 ;; 2. Special rows should be ignored when determining the beginning
2069 ;; of the row.
2070 (org-test-with-parsed-data "
2071 | / | < |
2072 | | a |
2073 |---+---|
2074 | / | < |
2075 | | b |"
2076 (should
2077 (equal
2078 '(yes no yes)
2079 (org-element-map
2080 tree 'table-row
2081 (lambda (row)
2082 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
2083 info)))))
2085 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
2086 "Test `org-export-table-row-ends-rowgroup-p' specifications."
2087 ;; 1. A row at the end of a table always ends a row group. So does
2088 ;; a row preceding a table rule.
2089 (org-test-with-parsed-data "
2090 | a |
2091 |---|
2092 | b |"
2093 (should
2094 (equal
2095 '(yes no yes)
2096 (org-element-map
2097 tree 'table-row
2098 (lambda (row)
2099 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2100 info))))
2101 ;; 2. Special rows should be ignored when determining the beginning
2102 ;; of the row.
2103 (org-test-with-parsed-data "
2104 | | a |
2105 | / | < |
2106 |---+---|
2107 | | b |
2108 | / | < |"
2109 (should
2110 (equal
2111 '(yes no yes)
2112 (org-element-map
2113 tree 'table-row
2114 (lambda (row)
2115 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
2116 info)))))
2118 (ert-deftest test-org-export/table-row-starts-header-p ()
2119 "Test `org-export-table-row-starts-header-p' specifications."
2120 ;; 1. Only the row starting the first row group starts the table
2121 ;; header.
2122 (org-test-with-parsed-data "
2123 | a |
2124 | b |
2125 |---|
2126 | c |"
2127 (should
2128 (equal
2129 '(yes no no no)
2130 (org-element-map
2131 tree 'table-row
2132 (lambda (row)
2133 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
2134 info))))
2135 ;; 2. A row cannot start an header if there's no header in the
2136 ;; table.
2137 (org-test-with-parsed-data "
2138 | a |
2139 |---|"
2140 (should-not
2141 (org-export-table-row-starts-header-p
2142 (org-element-map tree 'table-row 'identity info 'first-match)
2143 info))))
2145 (ert-deftest test-org-export/table-row-ends-header-p ()
2146 "Test `org-export-table-row-ends-header-p' specifications."
2147 ;; 1. Only the row starting the first row group starts the table
2148 ;; header.
2149 (org-test-with-parsed-data "
2150 | a |
2151 | b |
2152 |---|
2153 | c |"
2154 (should
2155 (equal
2156 '(no yes no no)
2157 (org-element-map
2158 tree 'table-row
2159 (lambda (row)
2160 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
2161 info))))
2162 ;; 2. A row cannot start an header if there's no header in the
2163 ;; table.
2164 (org-test-with-parsed-data "
2165 | a |
2166 |---|"
2167 (should-not
2168 (org-export-table-row-ends-header-p
2169 (org-element-map tree 'table-row 'identity info 'first-match)
2170 info))))
2174 ;;; Templates
2176 (ert-deftest test-org-export/inner-template ()
2177 "Test `inner-template' translator specifications."
2178 (should
2179 (equal "Success!"
2180 (let (org-export-registered-backends)
2181 (org-export-define-backend test
2182 ((inner-template . (lambda (contents info) "Success!"))
2183 (headline . (lambda (h c i) "Headline"))))
2184 (org-test-with-temp-text "* Headline"
2185 (org-export-as 'test)))))
2186 ;; Inner template is applied even in a "body-only" export.
2187 (should
2188 (equal "Success!"
2189 (let (org-export-registered-backends)
2190 (org-export-define-backend test
2191 ((inner-template . (lambda (contents info) "Success!"))
2192 (headline . (lambda (h c i) "Headline"))))
2193 (org-test-with-temp-text "* Headline"
2194 (org-export-as 'test nil nil 'body-only))))))
2196 (ert-deftest test-org-export/template ()
2197 "Test `template' translator specifications."
2198 (should
2199 (equal "Success!"
2200 (let (org-export-registered-backends)
2201 (org-export-define-backend test
2202 ((template . (lambda (contents info) "Success!"))
2203 (headline . (lambda (h c i) "Headline"))))
2204 (org-test-with-temp-text "* Headline"
2205 (org-export-as 'test)))))
2206 ;; Template is not applied in a "body-only" export.
2207 (should-not
2208 (equal "Success!"
2209 (let (org-export-registered-backends)
2210 (org-export-define-backend test
2211 ((template . (lambda (contents info) "Success!"))
2212 (headline . (lambda (h c i) "Headline"))))
2213 (org-test-with-temp-text "* Headline"
2214 (org-export-as 'test nil nil 'body-only))))))
2218 ;;; Topology
2220 (ert-deftest test-org-export/get-next-element ()
2221 "Test `org-export-get-next-element' specifications."
2222 ;; Standard test.
2223 (should
2224 (equal "b"
2225 (org-test-with-parsed-data "* Headline\n*a* b"
2226 (org-export-get-next-element
2227 (org-element-map tree 'bold 'identity info t) info))))
2228 ;; Return nil when no previous element.
2229 (should-not
2230 (org-test-with-parsed-data "* Headline\na *b*"
2231 (org-export-get-next-element
2232 (org-element-map tree 'bold 'identity info t) info)))
2233 ;; Non-exportable elements are ignored.
2234 (should-not
2235 (let ((org-export-with-timestamps nil))
2236 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
2237 (org-export-get-next-element
2238 (org-element-map tree 'entity 'identity info t) info))))
2239 ;; Find next element in secondary strings.
2240 (should
2241 (eq 'verbatim
2242 (org-test-with-parsed-data "* a =verb="
2243 (org-element-type
2244 (org-export-get-next-element
2245 (org-element-map tree 'plain-text 'identity info t) info)))))
2246 ;; Find next element in document keywords.
2247 (should
2248 (eq 'verbatim
2249 (org-test-with-parsed-data "#+TITLE: a =verb="
2250 (org-element-type
2251 (org-export-get-next-element
2252 (org-element-map
2253 (plist-get info :title) 'plain-text 'identity info t) info)))))
2254 ;; Find next element in parsed affiliated keywords.
2255 (should
2256 (eq 'verbatim
2257 (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
2258 (org-element-type
2259 (org-export-get-next-element
2260 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2261 ;; With optional argument N, return a list containing all the
2262 ;; following elements.
2263 (should
2264 (equal
2265 '(bold code underline)
2266 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2267 (mapcar 'car
2268 (org-export-get-next-element
2269 (org-element-map tree 'italic 'identity info t) info t)))))
2270 ;; When N is a positive integer, return a list containing up to
2271 ;; N following elements.
2272 (should
2273 (equal
2274 '(bold code)
2275 (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
2276 (mapcar 'car
2277 (org-export-get-next-element
2278 (org-element-map tree 'italic 'identity info t) info 2))))))
2280 (ert-deftest test-org-export/get-previous-element ()
2281 "Test `org-export-get-previous-element' specifications."
2282 ;; Standard test.
2283 (should
2284 (equal "a "
2285 (org-test-with-parsed-data "* Headline\na *b*"
2286 (org-export-get-previous-element
2287 (org-element-map tree 'bold 'identity info t) info))))
2288 ;; Return nil when no previous element.
2289 (should-not
2290 (org-test-with-parsed-data "* Headline\n*a* b"
2291 (org-export-get-previous-element
2292 (org-element-map tree 'bold 'identity info t) info)))
2293 ;; Non-exportable elements are ignored.
2294 (should-not
2295 (let ((org-export-with-timestamps nil))
2296 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
2297 (org-export-get-previous-element
2298 (org-element-map tree 'entity 'identity info t) info))))
2299 ;; Find previous element in secondary strings.
2300 (should
2301 (eq 'verbatim
2302 (org-test-with-parsed-data "* =verb= a"
2303 (org-element-type
2304 (org-export-get-previous-element
2305 (org-element-map tree 'plain-text 'identity info t) info)))))
2306 ;; Find previous element in document keywords.
2307 (should
2308 (eq 'verbatim
2309 (org-test-with-parsed-data "#+TITLE: =verb= a"
2310 (org-element-type
2311 (org-export-get-previous-element
2312 (org-element-map
2313 (plist-get info :title) 'plain-text 'identity info t) info)))))
2314 ;; Find previous element in parsed affiliated keywords.
2315 (should
2316 (eq 'verbatim
2317 (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
2318 (org-element-type
2319 (org-export-get-previous-element
2320 (org-element-map tree 'plain-text 'identity info t nil t) info)))))
2321 ;; With optional argument N, return a list containing up to
2322 ;; N previous elements.
2323 (should
2324 (equal '(bold italic underline)
2325 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
2326 (mapcar 'car
2327 (org-export-get-previous-element
2328 (org-element-map tree 'code 'identity info t) info t)))))
2329 ;; When N is a positive integer, return a list containing up to
2330 ;; N previous elements.
2331 (should
2332 (equal '(bold italic)
2333 (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
2334 (mapcar 'car
2335 (org-export-get-previous-element
2336 (org-element-map tree 'code 'identity info t) info 2))))))
2339 (provide 'test-ox)
2340 ;;; test-org-export.el end here