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