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