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