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