org-export: Add FILETAGS when retreiving tags with inheritance
[org-mode.git] / testing / lisp / test-org-export.el
blobf938dff3dd8d98798257cc3a6ca6036892909553
1 ;;; test-org-export.el --- Tests for org-export.el
3 ;; Copyright (C) 2012 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
7 ;; Released under the GNU General Public License version 3
8 ;; see: http://www.gnu.org/licenses/gpl-3.0.html
10 ;;;; Comments
14 ;;; Code:
16 (unless (featurep 'org-export)
17 (signal 'missing-test-dependency "org-export"))
19 (defmacro org-test-with-backend (backend &rest body)
20 "Execute body with an export back-end defined.
22 BACKEND is the name of the back-end. BODY is the body to
23 execute. The defined back-end simply returns parsed data as Org
24 syntax."
25 (declare (debug (form body)) (indent 1))
26 `(let ((,(intern (format "org-%s-translate-alist" backend))
27 ',(let (transcode-table)
28 (dolist (type (append org-element-all-elements
29 org-element-all-objects)
30 transcode-table)
31 (push
32 (cons type
33 (lambda (obj contents info)
34 (funcall
35 (intern (format "org-element-%s-interpreter" type))
36 obj contents)))
37 transcode-table)))))
38 (progn ,@body)))
40 (defmacro org-test-with-parsed-data (data &rest body)
41 "Execute body with parsed data available.
43 DATA is a string containing the data to be parsed. BODY is the
44 body to execute. Parse tree is available under the `tree'
45 variable, and communication channel under `info'.
47 This function calls `org-export-collect-tree-properties'. As
48 such, `:ignore-list' (for `org-element-map') and
49 `:parse-tree' (for `org-export-get-genealogy') properties are
50 already filled in `info'."
51 (declare (debug (form body)) (indent 1))
52 `(org-test-with-temp-text ,data
53 (let* ((tree (org-element-parse-buffer))
54 (info (org-export-collect-tree-properties
55 tree (org-export-get-environment))))
56 ,@body)))
60 ;;; Internal Tests
62 (ert-deftest test-org-export/bind-keyword ()
63 "Test reading #+BIND: keywords."
64 ;; Test with `org-export-all-BIND' set to t.
65 (should
66 (org-test-with-temp-text "#+BIND: variable value"
67 (let ((org-export-allow-BIND t))
68 (org-export--install-letbind-maybe)
69 (eq variable 'value))))
70 ;; Test with `org-export-all-BIND' set to nil.
71 (should-not
72 (org-test-with-temp-text "#+BIND: variable value"
73 (let ((org-export-allow-BIND nil))
74 (org-export--install-letbind-maybe)
75 (boundp 'variable))))
76 ;; Test with `org-export-all-BIND' set to 'confirm and
77 ;; `org-export--allow-BIND-local' to t .
78 (should
79 (org-test-with-temp-text "#+BIND: variable value"
80 (let ((org-export-allow-BIND 'confirm))
81 (org-set-local 'org-export--allow-BIND-local t)
82 (org-export--install-letbind-maybe)
83 (eq variable 'value))))
84 ;; Test with `org-export-all-BIND' set to 'confirm and
85 ;; `org-export--allow-BIND-local' to nil.
86 (should-not
87 (org-test-with-temp-text "#+BIND: variable value"
88 (let ((org-export-allow-BIND 'confirm))
89 (org-set-local 'org-export--allow-BIND-local nil)
90 (org-export--install-letbind-maybe)
91 (boundp 'variable))))
92 ;; BIND keywords are case-insensitive.
93 (should
94 (org-test-with-temp-text "#+bind: variable value"
95 (let ((org-export-allow-BIND t))
96 (org-export--install-letbind-maybe)
97 (eq variable 'value)))))
99 (ert-deftest test-org-export/parse-option-keyword ()
100 "Test reading all standard #+OPTIONS: items."
101 (should
102 (equal
103 (org-export--parse-option-keyword
104 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
105 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
106 stat:t")
107 '(:headline-levels
108 1 :preserve-breaks t :section-numbers t :time-stamp-file t
109 :with-archived-trees t :with-author t :with-creator t :with-drawers t
110 :with-email t :with-emphasize t :with-entities t :with-fixed-width t
111 :with-footnotes t :with-inlinetasks nil :with-priority t
112 :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
113 :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
114 :with-todo-keywords t)))
115 ;; Test some special values.
116 (should
117 (equal
118 (org-export--parse-option-keyword
119 "arch:headline creator:comment d:(\"TEST\")
120 ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
121 '( :section-numbers
123 :with-archived-trees headline :with-creator comment
124 :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
125 :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
127 (ert-deftest test-org-export/get-inbuffer-options ()
128 "Test reading all standard export keywords."
129 (should
130 (equal
131 (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
132 #+CREATOR: Idem
133 #+DATE: Today
134 #+DESCRIPTION: Testing
135 #+DESCRIPTION: with two lines
136 #+EMAIL: some@email.org
137 #+EXCLUDE_TAGS: noexport invisible
138 #+KEYWORDS: test
139 #+LANGUAGE: en
140 #+SELECT_TAGS: export
141 #+TITLE: Some title
142 #+TITLE: with spaces"
143 (org-export--get-inbuffer-options))
144 '(:author
145 ("Me, Myself and I") :creator "Idem" :date ("Today")
146 :description "Testing\nwith two lines" :email "some@email.org"
147 :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
148 :select-tags ("export") :title ("Some title with spaces")))))
150 (ert-deftest test-org-export/get-subtree-options ()
151 "Test setting options from headline's properties."
152 ;; EXPORT_TITLE.
153 (org-test-with-temp-text "#+TITLE: Title
154 * Headline
155 :PROPERTIES:
156 :EXPORT_TITLE: Subtree Title
157 :END:
158 Paragraph"
159 (forward-line)
160 (should (equal (plist-get (org-export-get-environment nil t) :title)
161 '("Subtree Title"))))
162 :title
163 '("subtree-title")
164 ;; EXPORT_OPTIONS.
165 (org-test-with-temp-text "#+OPTIONS: H:1
166 * Headline
167 :PROPERTIES:
168 :EXPORT_OPTIONS: H:2
169 :END:
170 Paragraph"
171 (forward-line)
172 (should
173 (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
174 ;; EXPORT_DATE.
175 (org-test-with-temp-text "#+DATE: today
176 * Headline
177 :PROPERTIES:
178 :EXPORT_DATE: 29-03-2012
179 :END:
180 Paragraph"
181 (forward-line)
182 (should (equal (plist-get (org-export-get-environment nil t) :date)
183 '("29-03-2012"))))
184 ;; Export properties are case-insensitive.
185 (org-test-with-temp-text "* Headline
186 :PROPERTIES:
187 :EXPORT_Date: 29-03-2012
188 :END:
189 Paragraph"
190 (should (equal (plist-get (org-export-get-environment nil t) :date)
191 '("29-03-2012")))))
193 (ert-deftest test-org-export/handle-options ()
194 "Test if export options have an impact on output."
195 ;; Test exclude tags.
196 (org-test-with-temp-text "* Head1 :noexport:"
197 (org-test-with-backend test
198 (should
199 (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
200 ""))))
201 ;; Test include tags.
202 (org-test-with-temp-text "
203 * Head1
204 * Head2
205 ** Sub-Head2.1 :export:
206 *** Sub-Head2.1.1
207 * Head2"
208 (org-test-with-backend test
209 (should
210 (equal
211 "* Head2\n** Sub-Head2.1 :export:\n*** Sub-Head2.1.1\n"
212 (let ((org-tags-column 0))
213 (org-export-as 'test nil nil nil '(:select-tags ("export"))))))))
214 ;; Test mixing include tags and exclude tags.
215 (org-test-with-temp-text "
216 * Head1 :export:
217 ** Sub-Head1 :noexport:
218 ** Sub-Head2
219 * Head2 :noexport:
220 ** Sub-Head1 :export:"
221 (org-test-with-backend test
222 (should
223 (string-match
224 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
225 (org-export-as
226 'test nil nil nil
227 '(:select-tags ("export") :exclude-tags ("noexport")))))))
228 ;; Ignore tasks.
229 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
230 (org-test-with-temp-text "* TODO Head1"
231 (org-test-with-backend test
232 (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
233 "")))))
234 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
235 (org-test-with-temp-text "* TODO Head1"
236 (org-test-with-backend test
237 (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
238 "* TODO Head1\n")))))
239 ;; Archived tree.
240 (org-test-with-temp-text "* Head1 :archive:"
241 (let ((org-archive-tag "archive"))
242 (org-test-with-backend test
243 (should
244 (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
245 "")))))
246 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
247 (let ((org-archive-tag "archive"))
248 (org-test-with-backend test
249 (should
250 (string-match
251 "\\* Head1[ \t]+:archive:"
252 (org-export-as 'test nil nil nil
253 '(:with-archived-trees headline)))))))
254 (org-test-with-temp-text "* Head1 :archive:"
255 (let ((org-archive-tag "archive"))
256 (org-test-with-backend test
257 (should
258 (string-match
259 "\\`\\* Head1[ \t]+:archive:\n\\'"
260 (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
261 ;; Drawers.
262 (let ((org-drawers '("TEST")))
263 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
264 (org-test-with-backend test
265 (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
266 ""))
267 (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
268 ":TEST:\ncontents\n:END:\n")))))
269 (let ((org-drawers '("FOO" "BAR")))
270 (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
271 (org-test-with-backend test
272 (should
273 (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
274 ":FOO:\nkeep\n:END:\n")))))
275 ;; Timestamps.
276 (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
277 (org-test-with-backend test
278 (should
279 (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
280 "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
281 (should
282 (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
283 (should
284 (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
285 "<2012-04-29 sun. 10:45>\n"))
286 (should
287 (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
288 "[2012-04-29 sun. 10:45]\n"))))
289 ;; Clocks.
290 (let ((org-clock-string "CLOCK:"))
291 (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
292 (org-test-with-backend test
293 (should
294 (equal (org-export-as 'test nil nil nil '(:with-clocks t))
295 "CLOCK: [2012-04-29 sun. 10:45]\n"))
296 (should
297 (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
298 ;; Plannings.
299 (let ((org-closed-string "CLOSED:"))
300 (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
301 (org-test-with-backend test
302 (should
303 (equal (org-export-as 'test nil nil nil '(:with-plannings t))
304 "CLOSED: [2012-04-29 sun. 10:45]\n"))
305 (should
306 (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
307 "")))))
308 ;; Inlinetasks.
309 (when (featurep 'org-inlinetask)
310 (should
311 (equal
312 (let ((org-inlinetask-min-level 15))
313 (org-test-with-temp-text "*************** Task"
314 (org-test-with-backend test
315 (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
316 ""))
317 (should
318 (equal
319 (let ((org-inlinetask-min-level 15))
320 (org-test-with-temp-text
321 "*************** Task\nContents\n*************** END"
322 (org-test-with-backend test
323 (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
324 "")))
325 ;; Statistics cookies.
326 (should
327 (equal ""
328 (org-test-with-temp-text "[0/0]"
329 (org-test-with-backend test
330 (org-export-as
331 'test nil nil nil '(:with-statistics-cookies nil)))))))
333 (ert-deftest test-org-export/comment-tree ()
334 "Test if export process ignores commented trees."
335 (let ((org-comment-string "COMMENT"))
336 (org-test-with-temp-text "* COMMENT Head1"
337 (org-test-with-backend test
338 (should (equal (org-export-as 'test) ""))))))
340 (ert-deftest test-org-export/export-scope ()
341 "Test all export scopes."
342 (org-test-with-temp-text "
343 * Head1
344 ** Head2
345 text
346 *** Head3"
347 (org-test-with-backend test
348 ;; Subtree.
349 (forward-line 3)
350 (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
351 ;; Visible.
352 (goto-char (point-min))
353 (forward-line)
354 (org-cycle)
355 (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
356 ;; Body only.
357 (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
358 (push '(template . org-test-template) org-test-translate-alist)
359 (should (equal (org-export-as 'test nil nil 'body-only)
360 "* Head1\n** Head2\ntext\n*** Head3\n"))
361 (should (equal (org-export-as 'test)
362 "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
363 ;; Region.
364 (goto-char (point-min))
365 (forward-line 3)
366 (transient-mark-mode 1)
367 (push-mark (point) t t)
368 (goto-char (point-at-eol))
369 (should (equal (org-export-as 'test) "text\n"))))
370 ;; Subtree with a code block calling another block outside.
371 (should
372 (equal ": 3\n"
373 (org-test-with-temp-text "
374 * Head1
375 #+BEGIN_SRC emacs-lisp :noweb yes :exports results
376 <<test>>
377 #+END_SRC
378 * Head2
379 #+NAME: test
380 #+BEGIN_SRC emacs-lisp
381 \(+ 1 2)
382 #+END_SRC"
383 (org-test-with-backend test
384 (forward-line 1)
385 (org-export-as 'test 'subtree))))))
387 (ert-deftest test-org-export/expand-include ()
388 "Test file inclusion in an Org buffer."
389 ;; Error when file isn't specified.
390 (should-error
391 (org-test-with-temp-text "#+INCLUDE: dummy.org"
392 (org-export-expand-include-keyword)))
393 ;; Full insertion with recursive inclusion.
394 (org-test-with-temp-text
395 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
396 (org-export-expand-include-keyword)
397 (should (equal (buffer-string)
398 "Small Org file with an include keyword.
400 #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
402 Success!
404 * Heading
405 body\n")))
406 ;; Localized insertion.
407 (org-test-with-temp-text
408 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
409 org-test-dir)
410 (org-export-expand-include-keyword)
411 (should (equal (buffer-string)
412 "Small Org file with an include keyword.\n")))
413 ;; Insertion with constraints on headlines level.
414 (org-test-with-temp-text
415 (format
416 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
417 org-test-dir)
418 (org-export-expand-include-keyword)
419 (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
420 ;; Inclusion within an example block.
421 (org-test-with-temp-text
422 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
423 org-test-dir)
424 (org-export-expand-include-keyword)
425 (should
426 (equal
427 (buffer-string)
428 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
429 ;; Inclusion within a src-block.
430 (org-test-with-temp-text
431 (format
432 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
433 org-test-dir)
434 (org-export-expand-include-keyword)
435 (should (equal (buffer-string)
436 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
438 (ert-deftest test-org-export/expand-macro ()
439 "Test macro expansion in an Org buffer."
440 ;; Standard macro expansion.
441 (should
442 (equal "#+MACRO: macro1 value\nvalue\n"
443 (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
444 (org-test-with-backend test (org-export-as 'test)))))
445 ;; Expand specific macros.
446 (should
447 (equal "me 2012-03-29 me@here Title\n"
448 (org-test-with-temp-text
450 #+TITLE: Title
451 #+DATE: 2012-03-29
452 #+AUTHOR: me
453 #+EMAIL: me@here
454 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
455 (let ((output (org-test-with-backend test (org-export-as 'test))))
456 (substring output (string-match ".*\n\\'" output))))))
457 ;; Expand specific macros when property contained a regular macro
458 ;; already.
459 (should
460 (equal "value\n"
461 (org-test-with-temp-text "
462 #+MACRO: macro1 value
463 #+TITLE: {{{macro1}}}
464 {{{title}}}"
465 (let ((output (org-test-with-backend test (org-export-as 'test))))
466 (substring output (string-match ".*\n\\'" output))))))
467 ;; Expand macros with templates in included files.
468 (should
469 (equal "success\n"
470 (org-test-with-temp-text
471 (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
472 {{{included-macro}}}" org-test-dir)
473 (let ((output (org-test-with-backend test (org-export-as 'test))))
474 (substring output (string-match ".*\n\\'" output)))))))
476 (ert-deftest test-org-export/user-ignore-list ()
477 "Test if `:ignore-list' accepts user input."
478 (org-test-with-backend test
479 (flet ((skip-note-head
480 (data backend info)
481 ;; Ignore headlines with the word "note" in their title.
482 (org-element-map
483 data 'headline
484 (lambda (headline)
485 (when (string-match "\\<note\\>"
486 (org-element-property :raw-value headline))
487 (org-export-ignore-element headline info)))
488 info)
489 data))
490 ;; Install function in parse tree filters.
491 (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
492 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
493 (should (equal (org-export-as 'test) "* Head1\n")))))))
495 (ert-deftest test-org-export/before-parsing-hook ()
496 "Test `org-export-before-parsing-hook'."
497 (org-test-with-backend test
498 (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
499 (let ((org-export-before-parsing-hook
500 '((lambda (backend)
501 (org-map-entries
502 (lambda ()
503 (delete-region (point) (progn (forward-line) (point)))))))))
504 (should (equal (org-export-as 'test) "Body 1\nBody 2\n"))))))
508 ;;; Affiliated Keywords
510 (ert-deftest test-org-export/read-attribute ()
511 "Test `org-export-read-attribute' specifications."
512 ;; Standard test.
513 (should
514 (equal
515 (org-export-read-attribute
516 :attr_html
517 (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
518 (org-element-at-point)))
519 '(:a 1 :b 2)))
520 ;; Return nil on empty attribute.
521 (should-not
522 (org-export-read-attribute
523 :attr_html
524 (org-test-with-temp-text "Paragraph" (org-element-at-point)))))
526 (ert-deftest test-org-export/get-caption ()
527 "Test `org-export-get-caption' specifications."
528 ;; Without optional argument, return long caption
529 (should
530 (equal
531 '("l")
532 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
533 (org-export-get-caption (org-element-at-point)))))
534 ;; With optional argument, return short caption.
535 (should
536 (equal
537 '("s")
538 (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
539 (org-export-get-caption (org-element-at-point) t))))
540 ;; Multiple lines are separated by white spaces.
541 (should
542 (equal
543 '("a" " " "b")
544 (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
545 (org-export-get-caption (org-element-at-point))))))
549 ;;; Export Snippets
551 (ert-deftest test-org-export/export-snippet ()
552 "Test export snippets transcoding."
553 (org-test-with-temp-text "@@test:A@@@@t:B@@"
554 (org-test-with-backend test
555 (let ((org-test-translate-alist
556 (cons (cons 'export-snippet
557 (lambda (snippet contents info)
558 (when (eq (org-export-snippet-backend snippet) 'test)
559 (org-element-property :value snippet))))
560 org-test-translate-alist)))
561 (let ((org-export-snippet-translation-alist nil))
562 (should (equal (org-export-as 'test) "A\n")))
563 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
564 (should (equal (org-export-as 'test) "AB\n")))))))
568 ;;; Footnotes
570 (ert-deftest test-org-export/footnotes ()
571 "Test footnotes specifications."
572 (let ((org-footnote-section nil)
573 (org-export-with-footnotes t))
574 ;; 1. Read every type of footnote.
575 (should
576 (equal
577 '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
578 (org-test-with-parsed-data
579 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
580 (org-element-map
581 tree 'footnote-reference
582 (lambda (ref)
583 (let ((def (org-export-get-footnote-definition ref info)))
584 (cons (org-export-get-footnote-number ref info)
585 (if (eq (org-element-property :type ref) 'inline) (car def)
586 (car (org-element-contents
587 (car (org-element-contents def))))))))
588 info))))
589 ;; 2. Test nested footnotes order.
590 (org-test-with-parsed-data
591 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
592 (should
593 (equal
594 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
595 (org-element-map
596 tree 'footnote-reference
597 (lambda (ref)
598 (when (org-export-footnote-first-reference-p ref info)
599 (cons (org-export-get-footnote-number ref info)
600 (org-element-property :label ref))))
601 info))))
602 ;; 3. Test nested footnote in invisible definitions.
603 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
604 ;; Hide definitions.
605 (narrow-to-region (point) (point-at-eol))
606 (let* ((tree (org-element-parse-buffer))
607 (info (org-combine-plists
608 `(:parse-tree ,tree)
609 (org-export-collect-tree-properties
610 tree (org-export-get-environment)))))
611 ;; Both footnotes should be seen.
612 (should
613 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
614 ;; 4. Test footnotes definitions collection.
615 (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
617 \[fn:2] B [fn:3] [fn::D].
619 \[fn:3] C."
620 (should (= (length (org-export-collect-footnote-definitions tree info))
621 4)))
622 ;; 5. Test export of footnotes defined outside parsing scope.
623 (org-test-with-temp-text "[fn:1] Out of scope
624 * Title
625 Paragraph[fn:1]"
626 (org-test-with-backend test
627 (let ((org-test-translate-alist
628 (cons (cons 'footnote-reference
629 (lambda (fn contents info)
630 (org-element-interpret-data
631 (org-export-get-footnote-definition fn info))))
632 org-test-translate-alist)))
633 (forward-line)
634 (should (equal "ParagraphOut of scope\n"
635 (org-export-as 'test 'subtree))))))))
639 ;;; Headlines and Inlinetasks
641 (ert-deftest test-org-export/get-relative-level ()
642 "Test `org-export-get-relative-level' specifications."
643 ;; Standard test.
644 (should
645 (equal '(1 2)
646 (let ((org-odd-levels-only nil))
647 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
648 (org-element-map
649 tree 'headline
650 (lambda (h) (org-export-get-relative-level h info))
651 info)))))
652 ;; Missing levels
653 (should
654 (equal '(1 3)
655 (let ((org-odd-levels-only nil))
656 (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
657 (org-element-map
658 tree 'headline
659 (lambda (h) (org-export-get-relative-level h info))
660 info))))))
662 (ert-deftest test-org-export/low-level-p ()
663 "Test `org-export-low-level-p' specifications."
664 (should
665 (equal
666 '(no yes)
667 (let ((org-odd-levels-only nil))
668 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
669 (org-element-map
670 tree 'headline
671 (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
672 (plist-put info :headline-levels 1)))))))
674 (ert-deftest test-org-export/get-headline-number ()
675 "Test `org-export-get-headline-number' specifications."
676 ;; Standard test.
677 (should
678 (equal
679 '((1) (1 1))
680 (let ((org-odd-levels-only nil))
681 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
682 (org-element-map
683 tree 'headline
684 (lambda (h) (org-export-get-headline-number h info))
685 info)))))
686 ;; Missing levels are replaced with 0.
687 (should
688 (equal
689 '((1) (1 0 1))
690 (let ((org-odd-levels-only nil))
691 (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
692 (org-element-map
693 tree 'headline
694 (lambda (h) (org-export-get-headline-number h info))
695 info))))))
697 (ert-deftest test-org-export/numbered-headline-p ()
698 "Test `org-export-numbered-headline-p' specifications."
699 ;; If `:section-numbers' is nil, never number headlines.
700 (should-not
701 (org-test-with-parsed-data "* Headline"
702 (org-element-map
703 tree 'headline
704 (lambda (h) (org-export-numbered-headline-p h info))
705 (plist-put info :section-numbers nil))))
706 ;; If `:section-numbers' is a number, only number headlines with
707 ;; a level greater that it.
708 (should
709 (equal
710 '(yes no)
711 (org-test-with-parsed-data "* Headline 1\n** Headline 2"
712 (org-element-map
713 tree 'headline
714 (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
715 (plist-put info :section-numbers 1)))))
716 ;; Otherwise, headlines are always numbered.
717 (should
718 (org-test-with-parsed-data "* Headline"
719 (org-element-map
720 tree 'headline
721 (lambda (h) (org-export-numbered-headline-p h info))
722 (plist-put info :section-numbers t)))))
724 (ert-deftest test-org-export/number-to-roman ()
725 "Test `org-export-number-to-roman' specifications."
726 ;; If number is negative, return it as a string.
727 (should (equal (org-export-number-to-roman -1) "-1"))
728 ;; Otherwise, return it as a roman number.
729 (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
731 (ert-deftest test-org-export/get-tags ()
732 "Test `org-export-get-tags' specifications."
733 (let ((org-export-exclude-tags '("noexport"))
734 (org-export-select-tags '("export")))
735 ;; Standard test: tags which are not a select tag, an exclude tag,
736 ;; or specified as optional argument shouldn't be ignored.
737 (should
738 (org-test-with-parsed-data "* Headline :tag:"
739 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
740 info)))
741 ;; Exclude tags are removed.
742 (should-not
743 (org-test-with-parsed-data "* Headline :noexport:"
744 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
745 info)))
746 ;; Select tags are removed.
747 (should-not
748 (org-test-with-parsed-data "* Headline :export:"
749 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
750 info)))
751 (should
752 (equal
753 '("tag")
754 (org-test-with-parsed-data "* Headline :tag:export:"
755 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
756 info))))
757 ;; Tags provided in the optional argument are also ignored.
758 (should-not
759 (org-test-with-parsed-data "* Headline :ignore:"
760 (org-export-get-tags (org-element-map tree 'headline 'identity info t)
761 info '("ignore"))))
762 ;; Allow tag inheritance.
763 (should
764 (equal
765 '(("tag") ("tag"))
766 (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
767 (org-element-map
768 tree 'headline
769 (lambda (hl) (org-export-get-tags hl info nil t)) info))))
770 ;; Tag inheritance checks FILETAGS keywords.
771 (should
772 (equal
773 '(("a" "b" "tag"))
774 (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
775 (org-element-map
776 tree 'headline
777 (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
779 (ert-deftest test-org-export/get-node-property ()
780 "Test`org-export-get-node-property' specifications."
781 ;; Standard test.
782 (should
783 (equal "value"
784 (org-test-with-parsed-data "* Headline
785 :PROPERTIES:
786 :prop: value
787 :END:"
788 (org-export-get-node-property
789 :prop (org-element-map tree 'headline 'identity nil t)))))
790 ;; Test inheritance.
791 (should
792 (equal "value"
793 (org-test-with-parsed-data "* Parent
794 :PROPERTIES:
795 :prop: value
796 :END:
797 ** Headline
798 Paragraph"
799 (org-export-get-node-property
800 :prop (org-element-map tree 'paragraph 'identity nil t) t))))
801 ;; Cannot return a value before the first headline.
802 (should-not
803 (org-test-with-parsed-data "Paragraph
804 * Headline
805 :PROPERTIES:
806 :prop: value
807 :END:"
808 (org-export-get-node-property
809 :prop (org-element-map tree 'paragraph 'identity nil t)))))
811 (ert-deftest test-org-export/first-sibling-p ()
812 "Test `org-export-first-sibling-p' specifications."
813 ;; Standard test.
814 (should
815 (equal
816 '(yes yes no)
817 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
818 (org-element-map
819 tree 'headline
820 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
821 info))))
822 ;; Ignore headlines not exported.
823 (should
824 (equal
825 '(yes)
826 (let ((org-export-exclude-tags '("ignore")))
827 (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
828 (org-element-map
829 tree 'headline
830 (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
831 info))))))
833 (ert-deftest test-org-export/last-sibling-p ()
834 "Test `org-export-last-sibling-p' specifications."
835 ;; Standard test.
836 (should
837 (equal
838 '(yes no yes)
839 (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
840 (org-element-map
841 tree 'headline
842 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
843 info))))
844 ;; Ignore headlines not exported.
845 (should
846 (equal
847 '(yes)
848 (let ((org-export-exclude-tags '("ignore")))
849 (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
850 (org-element-map
851 tree 'headline
852 (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
853 info))))))
857 ;;; Links
859 (ert-deftest test-org-export/get-coderef-format ()
860 "Test `org-export-get-coderef-format' specifications."
861 ;; A link without description returns "%s"
862 (should (equal (org-export-get-coderef-format "(ref:line)" nil)
863 "%s"))
864 ;; Return "%s" when path is matched within description.
865 (should (equal (org-export-get-coderef-format "path" "desc (path)")
866 "desc %s"))
867 ;; Otherwise return description.
868 (should (equal (org-export-get-coderef-format "path" "desc")
869 "desc")))
871 (ert-deftest test-org-export/inline-image-p ()
872 "Test `org-export-inline-image-p' specifications."
873 (should
874 (org-export-inline-image-p
875 (org-test-with-temp-text "[[#id]]"
876 (org-element-map
877 (org-element-parse-buffer) 'link 'identity nil t))
878 '(("custom-id" . "id")))))
880 (ert-deftest test-org-export/fuzzy-link ()
881 "Test fuzzy links specifications."
882 ;; 1. Links to invisible (keyword) targets should be ignored.
883 (org-test-with-parsed-data
884 "Paragraph.\n#+TARGET: Test\n[[Test]]"
885 (should-not
886 (org-element-map
887 tree 'link
888 (lambda (link)
889 (org-export-get-ordinal
890 (org-export-resolve-fuzzy-link link info) info)) info)))
891 ;; 2. Link to an headline should return headline's number.
892 (org-test-with-parsed-data
893 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
894 (should
895 ;; Note: Headline's number is in fact a list of numbers.
896 (equal '(2)
897 (org-element-map
898 tree 'link
899 (lambda (link)
900 (org-export-get-ordinal
901 (org-export-resolve-fuzzy-link link info) info)) info t))))
902 ;; 3. Link to a target in an item should return item's number.
903 (org-test-with-parsed-data
904 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
905 (should
906 ;; Note: Item's number is in fact a list of numbers.
907 (equal '(1 2)
908 (org-element-map
909 tree 'link
910 (lambda (link)
911 (org-export-get-ordinal
912 (org-export-resolve-fuzzy-link link info) info)) info t))))
913 ;; 4. Link to a target in a footnote should return footnote's
914 ;; number.
915 (org-test-with-parsed-data "
916 Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
917 (should
918 (equal '(2 3)
919 (org-element-map
920 tree 'link
921 (lambda (link)
922 (org-export-get-ordinal
923 (org-export-resolve-fuzzy-link link info) info)) info))))
924 ;; 5. Link to a named element should return sequence number of that
925 ;; element.
926 (org-test-with-parsed-data
927 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
928 (should
929 (= 2
930 (org-element-map
931 tree 'link
932 (lambda (link)
933 (org-export-get-ordinal
934 (org-export-resolve-fuzzy-link link info) info)) info t))))
935 ;; 6. Link to a target not within an item, a table, a footnote
936 ;; reference or definition should return section number.
937 (org-test-with-parsed-data
938 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
939 (should
940 (equal '(2)
941 (org-element-map
942 tree 'link
943 (lambda (link)
944 (org-export-get-ordinal
945 (org-export-resolve-fuzzy-link link info) info)) info t)))))
947 (ert-deftest test-org-export/resolve-coderef ()
948 "Test `org-export-resolve-coderef' specifications."
949 (let ((org-coderef-label-format "(ref:%s)"))
950 ;; 1. A link to a "-n -k -r" block returns line number.
951 (org-test-with-parsed-data
952 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
953 (should (= (org-export-resolve-coderef "coderef" info) 1)))
954 (org-test-with-parsed-data
955 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
956 (should (= (org-export-resolve-coderef "coderef" info) 1)))
957 ;; 2. A link to a "-n -r" block returns line number.
958 (org-test-with-parsed-data
959 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
960 (should (= (org-export-resolve-coderef "coderef" info) 1)))
961 (org-test-with-parsed-data
962 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
963 (should (= (org-export-resolve-coderef "coderef" info) 1)))
964 ;; 3. A link to a "-n" block returns coderef.
965 (org-test-with-parsed-data
966 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
967 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
968 (org-test-with-parsed-data
969 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
970 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
971 ;; 4. A link to a "-r" block returns line number.
972 (org-test-with-parsed-data
973 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
974 (should (= (org-export-resolve-coderef "coderef" info) 1)))
975 (org-test-with-parsed-data
976 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
977 (should (= (org-export-resolve-coderef "coderef" info) 1)))
978 ;; 5. A link to a block without a switch returns coderef.
979 (org-test-with-parsed-data
980 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
981 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
982 (org-test-with-parsed-data
983 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
984 (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
985 ;; 6. Correctly handle continued line numbers. A "+n" switch
986 ;; should resume numbering from previous block with numbered
987 ;; lines, ignoring blocks not numbering lines in the process.
988 ;; A "-n" switch resets count.
989 (org-test-with-parsed-data "
990 #+BEGIN_EXAMPLE -n
991 Text.
992 #+END_EXAMPLE
994 #+BEGIN_SRC emacs-lisp
995 \(- 1 1)
996 #+END_SRC
998 #+BEGIN_SRC emacs-lisp +n -r
999 \(+ 1 1) (ref:addition)
1000 #+END_SRC
1002 #+BEGIN_EXAMPLE -n -r
1003 Another text. (ref:text)
1004 #+END_EXAMPLE"
1005 (should (= (org-export-resolve-coderef "addition" info) 2))
1006 (should (= (org-export-resolve-coderef "text" info) 1)))
1007 ;; 7. Recognize coderef with user-specified syntax.
1008 (org-test-with-parsed-data
1009 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
1010 (should (equal (org-export-resolve-coderef "text" info) "text")))))
1012 (ert-deftest test-org-export/resolve-fuzzy-link ()
1013 "Test `org-export-resolve-fuzzy-link' specifications."
1014 ;; 1. Match target objects.
1015 (org-test-with-parsed-data "<<target>> [[target]]"
1016 (should
1017 (org-export-resolve-fuzzy-link
1018 (org-element-map tree 'link 'identity info t) info)))
1019 ;; 2. Match target elements.
1020 (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
1021 (should
1022 (org-export-resolve-fuzzy-link
1023 (org-element-map tree 'link 'identity info t) info)))
1024 ;; 3. Match named elements.
1025 (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
1026 (should
1027 (org-export-resolve-fuzzy-link
1028 (org-element-map tree 'link 'identity info t) info)))
1029 ;; 4. Match exact headline's name.
1030 (org-test-with-parsed-data "* My headline\n[[My headline]]"
1031 (should
1032 (org-export-resolve-fuzzy-link
1033 (org-element-map tree 'link 'identity info t) info)))
1034 ;; 5. Targets objects have priority over named elements and headline
1035 ;; titles.
1036 (org-test-with-parsed-data
1037 "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
1038 (should
1039 (eq 'target
1040 (org-element-type
1041 (org-export-resolve-fuzzy-link
1042 (org-element-map tree 'link 'identity info t) info)))))
1043 ;; 6. Named elements have priority over headline titles.
1044 (org-test-with-parsed-data
1045 "* target\n#+NAME: target\nParagraph\n\n[[target]]"
1046 (should
1047 (eq 'paragraph
1048 (org-element-type
1049 (org-export-resolve-fuzzy-link
1050 (org-element-map tree 'link 'identity info t) info)))))
1051 ;; 7. If link's path starts with a "*", only match headline titles,
1052 ;; though.
1053 (org-test-with-parsed-data
1054 "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
1055 (should
1056 (eq 'headline
1057 (org-element-type
1058 (org-export-resolve-fuzzy-link
1059 (org-element-map tree 'link 'identity info t) info)))))
1060 ;; 8. Return nil if no match.
1061 (org-test-with-parsed-data "[[target]]"
1062 (should-not
1063 (org-export-resolve-fuzzy-link
1064 (org-element-map tree 'link 'identity info t) info))))
1066 (ert-deftest test-org-export/resolve-id-link ()
1067 "Test `org-export-resolve-id-link' specifications."
1068 ;; 1. Regular test for custom-id link.
1069 (org-test-with-parsed-data "* Headline1
1070 :PROPERTIES:
1071 :CUSTOM-ID: test
1072 :END:
1073 * Headline 2
1074 \[[#test]]"
1075 (should
1076 (org-export-resolve-id-link
1077 (org-element-map tree 'link 'identity info t) info)))
1078 ;; 2. Failing test for custom-id link.
1079 (org-test-with-parsed-data "* Headline1
1080 :PROPERTIES:
1081 :CUSTOM-ID: test
1082 :END:
1083 * Headline 2
1084 \[[#no-match]]"
1085 (should-not
1086 (org-export-resolve-id-link
1087 (org-element-map tree 'link 'identity info t) info)))
1088 ;; 3. Test for internal id target.
1089 (org-test-with-parsed-data "* Headline1
1090 :PROPERTIES:
1091 :ID: aaaa
1092 :END:
1093 * Headline 2
1094 \[[id:aaaa]]"
1095 (should
1096 (org-export-resolve-id-link
1097 (org-element-map tree 'link 'identity info t) info)))
1098 ;; 4. Test for external id target.
1099 (org-test-with-parsed-data "[[id:aaaa]]"
1100 (should
1101 (org-export-resolve-id-link
1102 (org-element-map tree 'link 'identity info t)
1103 (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
1105 (ert-deftest test-org-export/resolve-radio-link ()
1106 "Test `org-export-resolve-radio-link' specifications."
1107 ;; Standard test.
1108 (org-test-with-temp-text "<<<radio>>> radio"
1109 (org-update-radio-target-regexp)
1110 (should
1111 (let* ((tree (org-element-parse-buffer))
1112 (info `(:parse-tree ,tree)))
1113 (org-export-resolve-radio-link
1114 (org-element-map tree 'link 'identity info t)
1115 info))))
1116 ;; Radio target with objects.
1117 (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
1118 (org-update-radio-target-regexp)
1119 (should
1120 (let* ((tree (org-element-parse-buffer))
1121 (info `(:parse-tree ,tree)))
1122 (org-export-resolve-radio-link
1123 (org-element-map tree 'link 'identity info t)
1124 info)))))
1128 ;;; Src-block and example-block
1130 (ert-deftest test-org-export/unravel-code ()
1131 "Test `org-export-unravel-code' function."
1132 (let ((org-coderef-label-format "(ref:%s)"))
1133 ;; 1. Code without reference.
1134 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
1135 (should (equal (org-export-unravel-code (org-element-at-point))
1136 '("(+ 1 1)\n"))))
1137 ;; 2. Code with reference.
1138 (org-test-with-temp-text
1139 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
1140 (should (equal (org-export-unravel-code (org-element-at-point))
1141 '("(+ 1 1)\n" (1 . "test")))))
1142 ;; 3. Code with user-defined reference.
1143 (org-test-with-temp-text
1144 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
1145 (should (equal (org-export-unravel-code (org-element-at-point))
1146 '("(+ 1 1)\n" (1 . "test")))))
1147 ;; 4. Code references keys are relative to the current block.
1148 (org-test-with-temp-text "
1149 #+BEGIN_EXAMPLE -n
1150 \(+ 1 1)
1151 #+END_EXAMPLE
1152 #+BEGIN_EXAMPLE +n
1153 \(+ 2 2)
1154 \(+ 3 3) (ref:one)
1155 #+END_EXAMPLE"
1156 (goto-line 5)
1157 (should (equal (org-export-unravel-code (org-element-at-point))
1158 '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
1162 ;;; Smart Quotes
1164 (ert-deftest test-org-export/activate-smart-quotes ()
1165 "Test `org-export-activate-smart-quotes' specifications."
1166 ;; Opening double quotes: standard test.
1167 (should
1168 (equal
1169 '("some &ldquo;paragraph")
1170 (let ((org-export-default-language "en"))
1171 (org-test-with-parsed-data "some \"paragraph"
1172 (org-element-map
1173 tree 'plain-text
1174 (lambda (s) (org-export-activate-smart-quotes s :html info))
1175 info)))))
1176 ;; Opening quotes: at the beginning of a paragraph.
1177 (should
1178 (equal
1179 '("&ldquo;begin")
1180 (let ((org-export-default-language "en"))
1181 (org-test-with-parsed-data "\"begin"
1182 (org-element-map
1183 tree 'plain-text
1184 (lambda (s) (org-export-activate-smart-quotes s :html info))
1185 info)))))
1186 ;; Opening quotes: after an object.
1187 (should
1188 (equal
1189 '("&ldquo;begin")
1190 (let ((org-export-default-language "en"))
1191 (org-test-with-parsed-data "=verb= \"begin"
1192 (org-element-map
1193 tree 'plain-text
1194 (lambda (s) (org-export-activate-smart-quotes s :html info))
1195 info)))))
1196 ;; Closing quotes: standard test.
1197 (should
1198 (equal
1199 '("some&rdquo; paragraph")
1200 (let ((org-export-default-language "en"))
1201 (org-test-with-parsed-data "some\" paragraph"
1202 (org-element-map
1203 tree 'plain-text
1204 (lambda (s) (org-export-activate-smart-quotes s :html info))
1205 info)))))
1206 ;; Closing quotes: at the end of a paragraph.
1207 (should
1208 (equal
1209 '("end&rdquo;")
1210 (let ((org-export-default-language "en"))
1211 (org-test-with-parsed-data "end\""
1212 (org-element-map
1213 tree 'plain-text
1214 (lambda (s) (org-export-activate-smart-quotes s :html info))
1215 info)))))
1216 ;; Apostrophe: standard test.
1217 (should
1218 (equal
1219 '("It shouldn&rsquo;t fail")
1220 (let ((org-export-default-language "en"))
1221 (org-test-with-parsed-data "It shouldn't fail"
1222 (org-element-map
1223 tree 'plain-text
1224 (lambda (s) (org-export-activate-smart-quotes s :html info))
1225 info)))))
1226 ;; Apostrophe: before an object.
1227 (should
1228 (equal
1229 '("a&rsquo;")
1230 (let ((org-export-default-language "en"))
1231 (org-test-with-parsed-data "a'=b="
1232 (org-element-map
1233 tree 'plain-text
1234 (lambda (s) (org-export-activate-smart-quotes s :html info))
1235 info)))))
1236 ;; Apostrophe: after an object.
1237 (should
1238 (equal
1239 '("&rsquo;s")
1240 (let ((org-export-default-language "en"))
1241 (org-test-with-parsed-data "=code='s"
1242 (org-element-map
1243 tree 'plain-text
1244 (lambda (s) (org-export-activate-smart-quotes s :html info))
1245 info)))))
1246 ;; Special case: isolated quotes.
1247 (should
1248 (equal '("&ldquo;" "&rdquo;")
1249 (let ((org-export-default-language "en"))
1250 (org-test-with-parsed-data "\"$x$\""
1251 (org-element-map
1252 tree 'plain-text
1253 (lambda (s) (org-export-activate-smart-quotes s :html info))
1254 info))))))
1258 ;;; Tables
1260 (ert-deftest test-org-export/special-column ()
1261 "Test if the table's special column is properly recognized."
1262 ;; 1. First column is special if it contains only a special marking
1263 ;; characters or empty cells.
1264 (org-test-with-temp-text "
1265 | ! | 1 |
1266 | | 2 |"
1267 (should
1268 (org-export-table-has-special-column-p
1269 (org-element-map
1270 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1271 ;; 2. If the column contains anything else, it isn't special.
1272 (org-test-with-temp-text "
1273 | ! | 1 |
1274 | b | 2 |"
1275 (should-not
1276 (org-export-table-has-special-column-p
1277 (org-element-map
1278 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1279 ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
1280 ;; and "!".
1281 (org-test-with-temp-text "
1282 | # | 1 |
1283 | ^ | 2 |
1284 | * | 3 |
1285 | _ | 4 |
1286 | / | 5 |
1287 | $ | 6 |
1288 | ! | 7 |"
1289 (should
1290 (org-export-table-has-special-column-p
1291 (org-element-map
1292 (org-element-parse-buffer) 'table 'identity nil 'first-match))))
1293 ;; 4. A first column with only empty cells isn't considered as
1294 ;; special.
1295 (org-test-with-temp-text "
1296 | | 1 |
1297 | | 2 |"
1298 (should-not
1299 (org-export-table-has-special-column-p
1300 (org-element-map
1301 (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
1303 (ert-deftest test-org-export/table-row-is-special-p ()
1304 "Test `org-export-table-row-is-special-p' specifications."
1305 ;; 1. A row is special if it has a special marking character in the
1306 ;; special column.
1307 (org-test-with-parsed-data "| ! | 1 |"
1308 (should
1309 (org-export-table-row-is-special-p
1310 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1311 ;; 2. A row is special when its first field is "/"
1312 (org-test-with-parsed-data "
1313 | / | 1 |
1314 | a | b |"
1315 (should
1316 (org-export-table-row-is-special-p
1317 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1318 ;; 3. A row only containing alignment cookies is also considered as
1319 ;; special.
1320 (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
1321 (should
1322 (org-export-table-row-is-special-p
1323 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1324 ;; 4. Everything else isn't considered as special.
1325 (org-test-with-parsed-data "| \alpha | | c |"
1326 (should-not
1327 (org-export-table-row-is-special-p
1328 (org-element-map tree 'table-row 'identity nil 'first-match) info)))
1329 ;; 5. Table's rules are never considered as special rows.
1330 (org-test-with-parsed-data "|---+---|"
1331 (should-not
1332 (org-export-table-row-is-special-p
1333 (org-element-map tree 'table-row 'identity nil 'first-match) info))))
1335 (ert-deftest test-org-export/has-header-p ()
1336 "Test `org-export-table-has-header-p' specifications."
1337 ;; 1. With an header.
1338 (org-test-with-parsed-data "
1339 | a | b |
1340 |---+---|
1341 | c | d |"
1342 (should
1343 (org-export-table-has-header-p
1344 (org-element-map tree 'table 'identity info 'first-match)
1345 info)))
1346 ;; 2. Without an header.
1347 (org-test-with-parsed-data "
1348 | a | b |
1349 | c | d |"
1350 (should-not
1351 (org-export-table-has-header-p
1352 (org-element-map tree 'table 'identity info 'first-match)
1353 info)))
1354 ;; 3. Don't get fooled with starting and ending rules.
1355 (org-test-with-parsed-data "
1356 |---+---|
1357 | a | b |
1358 | c | d |
1359 |---+---|"
1360 (should-not
1361 (org-export-table-has-header-p
1362 (org-element-map tree 'table 'identity info 'first-match)
1363 info))))
1365 (ert-deftest test-org-export/table-row-group ()
1366 "Test `org-export-table-row-group' specifications."
1367 ;; 1. A rule creates a new group.
1368 (org-test-with-parsed-data "
1369 | a | b |
1370 |---+---|
1371 | 1 | 2 |"
1372 (should
1373 (equal
1374 '(1 nil 2)
1375 (mapcar (lambda (row) (org-export-table-row-group row info))
1376 (org-element-map tree 'table-row 'identity)))))
1377 ;; 2. Special rows are ignored in count.
1378 (org-test-with-parsed-data "
1379 | / | < | > |
1380 |---|---+---|
1381 | | 1 | 2 |"
1382 (should
1383 (equal
1384 '(nil nil 1)
1385 (mapcar (lambda (row) (org-export-table-row-group row info))
1386 (org-element-map tree 'table-row 'identity)))))
1387 ;; 3. Double rules also are ignored in count.
1388 (org-test-with-parsed-data "
1389 | a | b |
1390 |---+---|
1391 |---+---|
1392 | 1 | 2 |"
1393 (should
1394 (equal
1395 '(1 nil nil 2)
1396 (mapcar (lambda (row) (org-export-table-row-group row info))
1397 (org-element-map tree 'table-row 'identity))))))
1399 (ert-deftest test-org-export/table-cell-width ()
1400 "Test `org-export-table-cell-width' specifications."
1401 ;; 1. Width is primarily determined by width cookies. If no cookie
1402 ;; is found, cell's width is nil.
1403 (org-test-with-parsed-data "
1404 | / | <l> | <6> | <l7> |
1405 | | a | b | c |"
1406 (should
1407 (equal
1408 '(nil 6 7)
1409 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1410 (org-element-map tree 'table-cell 'identity info)))))
1411 ;; 2. The last width cookie has precedence.
1412 (org-test-with-parsed-data "
1413 | <6> |
1414 | <7> |
1415 | a |"
1416 (should
1417 (equal
1418 '(7)
1419 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1420 (org-element-map tree 'table-cell 'identity info)))))
1421 ;; 3. Valid width cookies must have a specific row.
1422 (org-test-with-parsed-data "| <6> | cell |"
1423 (should
1424 (equal
1425 '(nil nil)
1426 (mapcar (lambda (cell) (org-export-table-cell-width cell info))
1427 (org-element-map tree 'table-cell 'identity))))))
1429 (ert-deftest test-org-export/table-cell-alignment ()
1430 "Test `org-export-table-cell-alignment' specifications."
1431 (let ((org-table-number-fraction 0.5)
1432 (org-table-number-regexp "^[0-9]+$"))
1433 ;; 1. Alignment is primarily determined by alignment cookies.
1434 (org-test-with-temp-text "| <l> | <c> | <r> |"
1435 (let* ((tree (org-element-parse-buffer))
1436 (info `(:parse-tree ,tree)))
1437 (should
1438 (equal
1439 '(left center right)
1440 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
1441 (org-element-map tree 'table-cell 'identity))))))
1442 ;; 2. The last alignment cookie has precedence.
1443 (org-test-with-parsed-data "
1444 | <l8> |
1445 | cell |
1446 | <r9> |"
1447 (should
1448 (equal
1449 '(right right right)
1450 (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
1451 (org-element-map tree 'table-cell 'identity)))))
1452 ;; 3. If there's no cookie, cell's contents determine alignment.
1453 ;; A column mostly made of cells containing numbers will align
1454 ;; its cells to the right.
1455 (org-test-with-parsed-data "
1456 | 123 |
1457 | some text |
1458 | 12345 |"
1459 (should
1460 (equal
1461 '(right right right)
1462 (mapcar (lambda (cell)
1463 (org-export-table-cell-alignment cell info))
1464 (org-element-map tree 'table-cell 'identity)))))
1465 ;; 4. Otherwise, they will be aligned to the left.
1466 (org-test-with-parsed-data "
1467 | text |
1468 | some text |
1469 | \alpha |"
1470 (should
1471 (equal
1472 '(left left left)
1473 (mapcar (lambda (cell)
1474 (org-export-table-cell-alignment cell info))
1475 (org-element-map tree 'table-cell 'identity)))))))
1477 (ert-deftest test-org-export/table-cell-borders ()
1478 "Test `org-export-table-cell-borders' specifications."
1479 ;; 1. Recognize various column groups indicators.
1480 (org-test-with-parsed-data "| / | < | > | <> |"
1481 (should
1482 (equal
1483 '((right bottom top) (left bottom top) (right bottom top)
1484 (right left bottom top))
1485 (mapcar (lambda (cell)
1486 (org-export-table-cell-borders cell info))
1487 (org-element-map tree 'table-cell 'identity)))))
1488 ;; 2. Accept shortcuts to define column groups.
1489 (org-test-with-parsed-data "| / | < | < |"
1490 (should
1491 (equal
1492 '((right bottom top) (right left bottom top) (left bottom top))
1493 (mapcar (lambda (cell)
1494 (org-export-table-cell-borders cell info))
1495 (org-element-map tree 'table-cell 'identity)))))
1496 ;; 3. A valid column groups row must start with a "/".
1497 (org-test-with-parsed-data "
1498 | | < |
1499 | a | b |"
1500 (should
1501 (equal '((top) (top) (bottom) (bottom))
1502 (mapcar (lambda (cell)
1503 (org-export-table-cell-borders cell info))
1504 (org-element-map tree 'table-cell 'identity)))))
1505 ;; 4. Take table rules into consideration.
1506 (org-test-with-parsed-data "
1507 | 1 |
1508 |---|
1509 | 2 |"
1510 (should
1511 (equal '((below top) (bottom above))
1512 (mapcar (lambda (cell)
1513 (org-export-table-cell-borders cell info))
1514 (org-element-map tree 'table-cell 'identity)))))
1515 ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
1516 ;; (resp. `bottom' and `below') borders. Any special row is
1517 ;; ignored.
1518 (org-test-with-parsed-data "
1519 |---+----|
1520 | / | |
1521 | | 1 |
1522 |---+----|"
1523 (should
1524 (equal '((bottom below top above))
1525 (last
1526 (mapcar (lambda (cell)
1527 (org-export-table-cell-borders cell info))
1528 (org-element-map tree 'table-cell 'identity)))))))
1530 (ert-deftest test-org-export/table-dimensions ()
1531 "Test `org-export-table-dimensions' specifications."
1532 ;; 1. Standard test.
1533 (org-test-with-parsed-data "
1534 | 1 | 2 | 3 |
1535 | 4 | 5 | 6 |"
1536 (should
1537 (equal '(2 . 3)
1538 (org-export-table-dimensions
1539 (org-element-map tree 'table 'identity info 'first-match) info))))
1540 ;; 2. Ignore horizontal rules and special columns.
1541 (org-test-with-parsed-data "
1542 | / | < | > |
1543 | 1 | 2 | 3 |
1544 |---+---+---|
1545 | 4 | 5 | 6 |"
1546 (should
1547 (equal '(2 . 3)
1548 (org-export-table-dimensions
1549 (org-element-map tree 'table 'identity info 'first-match) info)))))
1551 (ert-deftest test-org-export/table-cell-address ()
1552 "Test `org-export-table-cell-address' specifications."
1553 ;; 1. Standard test: index is 0-based.
1554 (org-test-with-parsed-data "| a | b |"
1555 (should
1556 (equal '((0 . 0) (0 . 1))
1557 (org-element-map
1558 tree 'table-cell
1559 (lambda (cell) (org-export-table-cell-address cell info))
1560 info))))
1561 ;; 2. Special column isn't counted, nor are special rows.
1562 (org-test-with-parsed-data "
1563 | / | <> |
1564 | | c |"
1565 (should
1566 (equal '(0 . 0)
1567 (org-export-table-cell-address
1568 (car (last (org-element-map tree 'table-cell 'identity info)))
1569 info))))
1570 ;; 3. Tables rules do not count either.
1571 (org-test-with-parsed-data "
1572 | a |
1573 |---|
1574 | b |
1575 |---|
1576 | c |"
1577 (should
1578 (equal '(2 . 0)
1579 (org-export-table-cell-address
1580 (car (last (org-element-map tree 'table-cell 'identity info)))
1581 info))))
1582 ;; 4. Return nil for special cells.
1583 (org-test-with-parsed-data "| / | a |"
1584 (should-not
1585 (org-export-table-cell-address
1586 (org-element-map tree 'table-cell 'identity nil 'first-match)
1587 info))))
1589 (ert-deftest test-org-export/get-table-cell-at ()
1590 "Test `org-export-get-table-cell-at' specifications."
1591 ;; 1. Address ignores special columns, special rows and rules.
1592 (org-test-with-parsed-data "
1593 | / | <> |
1594 | | a |
1595 |---+----|
1596 | | b |"
1597 (should
1598 (equal '("b")
1599 (org-element-contents
1600 (org-export-get-table-cell-at
1601 '(1 . 0)
1602 (org-element-map tree 'table 'identity info 'first-match)
1603 info)))))
1604 ;; 2. Return value for a non-existent address is nil.
1605 (org-test-with-parsed-data "| a |"
1606 (should-not
1607 (org-export-get-table-cell-at
1608 '(2 . 2)
1609 (org-element-map tree 'table 'identity info 'first-match)
1610 info)))
1611 (org-test-with-parsed-data "| / |"
1612 (should-not
1613 (org-export-get-table-cell-at
1614 '(0 . 0)
1615 (org-element-map tree 'table 'identity info 'first-match)
1616 info))))
1618 (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
1619 "Test `org-export-table-cell-starts-colgroup-p' specifications."
1620 ;; 1. A cell at a beginning of a row always starts a column group.
1621 (org-test-with-parsed-data "| a |"
1622 (should
1623 (org-export-table-cell-starts-colgroup-p
1624 (org-element-map tree 'table-cell 'identity info 'first-match)
1625 info)))
1626 ;; 2. Special column should be ignored when determining the
1627 ;; beginning of the row.
1628 (org-test-with-parsed-data "
1629 | / | |
1630 | | a |"
1631 (should
1632 (org-export-table-cell-starts-colgroup-p
1633 (org-element-map tree 'table-cell 'identity info 'first-match)
1634 info)))
1635 ;; 2. Explicit column groups.
1636 (org-test-with-parsed-data "
1637 | / | | < |
1638 | a | b | c |"
1639 (should
1640 (equal
1641 '(yes no yes)
1642 (org-element-map
1643 tree 'table-cell
1644 (lambda (cell)
1645 (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
1646 info)))))
1648 (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
1649 "Test `org-export-table-cell-ends-colgroup-p' specifications."
1650 ;; 1. A cell at the end of a row always ends a column group.
1651 (org-test-with-parsed-data "| a |"
1652 (should
1653 (org-export-table-cell-ends-colgroup-p
1654 (org-element-map tree 'table-cell 'identity info 'first-match)
1655 info)))
1656 ;; 2. Special column should be ignored when determining the
1657 ;; beginning of the row.
1658 (org-test-with-parsed-data "
1659 | / | |
1660 | | a |"
1661 (should
1662 (org-export-table-cell-ends-colgroup-p
1663 (org-element-map tree 'table-cell 'identity info 'first-match)
1664 info)))
1665 ;; 3. Explicit column groups.
1666 (org-test-with-parsed-data "
1667 | / | < | |
1668 | a | b | c |"
1669 (should
1670 (equal
1671 '(yes no yes)
1672 (org-element-map
1673 tree 'table-cell
1674 (lambda (cell)
1675 (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
1676 info)))))
1678 (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
1679 "Test `org-export-table-row-starts-rowgroup-p' specifications."
1680 ;; 1. A row at the beginning of a table always starts a row group.
1681 ;; So does a row following a table rule.
1682 (org-test-with-parsed-data "
1683 | a |
1684 |---|
1685 | b |"
1686 (should
1687 (equal
1688 '(yes no yes)
1689 (org-element-map
1690 tree 'table-row
1691 (lambda (row)
1692 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
1693 info))))
1694 ;; 2. Special rows should be ignored when determining the beginning
1695 ;; of the row.
1696 (org-test-with-parsed-data "
1697 | / | < |
1698 | | a |
1699 |---+---|
1700 | / | < |
1701 | | b |"
1702 (should
1703 (equal
1704 '(yes no yes)
1705 (org-element-map
1706 tree 'table-row
1707 (lambda (row)
1708 (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
1709 info)))))
1711 (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
1712 "Test `org-export-table-row-ends-rowgroup-p' specifications."
1713 ;; 1. A row at the end of a table always ends a row group. So does
1714 ;; a row preceding a table rule.
1715 (org-test-with-parsed-data "
1716 | a |
1717 |---|
1718 | b |"
1719 (should
1720 (equal
1721 '(yes no yes)
1722 (org-element-map
1723 tree 'table-row
1724 (lambda (row)
1725 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
1726 info))))
1727 ;; 2. Special rows should be ignored when determining the beginning
1728 ;; of the row.
1729 (org-test-with-parsed-data "
1730 | | a |
1731 | / | < |
1732 |---+---|
1733 | | b |
1734 | / | < |"
1735 (should
1736 (equal
1737 '(yes no yes)
1738 (org-element-map
1739 tree 'table-row
1740 (lambda (row)
1741 (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
1742 info)))))
1744 (ert-deftest test-org-export/table-row-starts-header-p ()
1745 "Test `org-export-table-row-starts-header-p' specifications."
1746 ;; 1. Only the row starting the first row group starts the table
1747 ;; header.
1748 (org-test-with-parsed-data "
1749 | a |
1750 | b |
1751 |---|
1752 | c |"
1753 (should
1754 (equal
1755 '(yes no no no)
1756 (org-element-map
1757 tree 'table-row
1758 (lambda (row)
1759 (if (org-export-table-row-starts-header-p row info) 'yes 'no))
1760 info))))
1761 ;; 2. A row cannot start an header if there's no header in the
1762 ;; table.
1763 (org-test-with-parsed-data "
1764 | a |
1765 |---|"
1766 (should-not
1767 (org-export-table-row-starts-header-p
1768 (org-element-map tree 'table-row 'identity info 'first-match)
1769 info))))
1771 (ert-deftest test-org-export/table-row-ends-header-p ()
1772 "Test `org-export-table-row-ends-header-p' specifications."
1773 ;; 1. Only the row starting the first row group starts the table
1774 ;; header.
1775 (org-test-with-parsed-data "
1776 | a |
1777 | b |
1778 |---|
1779 | c |"
1780 (should
1781 (equal
1782 '(no yes no no)
1783 (org-element-map
1784 tree 'table-row
1785 (lambda (row)
1786 (if (org-export-table-row-ends-header-p row info) 'yes 'no))
1787 info))))
1788 ;; 2. A row cannot start an header if there's no header in the
1789 ;; table.
1790 (org-test-with-parsed-data "
1791 | a |
1792 |---|"
1793 (should-not
1794 (org-export-table-row-ends-header-p
1795 (org-element-map tree 'table-row 'identity info 'first-match)
1796 info))))
1800 ;;; Topology
1802 (ert-deftest test-org-export/get-next-element ()
1803 "Test `org-export-get-next-element' specifications."
1804 ;; Standard test.
1805 (should
1806 (equal "b"
1807 (org-test-with-parsed-data "* Headline\n*a* b"
1808 (org-export-get-next-element
1809 (org-element-map tree 'bold 'identity info t) info))))
1810 ;; Return nil when no previous element.
1811 (should-not
1812 (org-test-with-parsed-data "* Headline\na *b*"
1813 (org-export-get-next-element
1814 (org-element-map tree 'bold 'identity info t) info)))
1815 ;; Non-exportable elements are ignored.
1816 (should-not
1817 (let ((org-export-with-timestamps nil))
1818 (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
1819 (org-export-get-next-element
1820 (org-element-map tree 'entity 'identity info t) info)))))
1822 (ert-deftest test-org-export/get-previous-element ()
1823 "Test `org-export-get-previous-element' specifications."
1824 ;; Standard test.
1825 (should
1826 (equal "a "
1827 (org-test-with-parsed-data "* Headline\na *b*"
1828 (org-export-get-previous-element
1829 (org-element-map tree 'bold 'identity info t) info))))
1830 ;; Return nil when no previous element.
1831 (should-not
1832 (org-test-with-parsed-data "* Headline\n*a* b"
1833 (org-export-get-previous-element
1834 (org-element-map tree 'bold 'identity info t) info)))
1835 ;; Non-exportable elements are ignored.
1836 (should-not
1837 (let ((org-export-with-timestamps nil))
1838 (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
1839 (org-export-get-previous-element
1840 (org-element-map tree 'entity 'identity info t) info)))))
1843 (provide 'test-org-export)
1844 ;;; test-org-export.el end here