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