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
15 (let ((load-path (cons (expand-file-name
16 ".." (file-name-directory
17 (or load-file-name buffer-file-name
)))
20 (require 'org-test-ob-consts
)
21 (require '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, as a string, of the back-end. BODY is the
31 body to execute. The defined back-end simply returns parsed data
33 (declare (debug (form body
)) (indent 1))
34 `(flet ,(let (transcoders)
35 (dolist (type (append org-element-all-elements
36 org-element-all-objects
)
38 (push `(,(intern (format "org-%s-%s" backend type
))
40 (,(intern (format "org-element-%s-interpreter" type
))
45 (ert-deftest test-org-export
/parse-option-keyword
()
46 "Test reading all standard #+OPTIONS: items."
49 (org-export-parse-option-keyword
50 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
51 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t")
53 1 :preserve-breaks t
:section-numbers t
:time-stamp-file t
54 :with-archived-trees t
:with-author t
:with-creator t
:with-drawers t
55 :with-email t
:with-emphasize t
:with-entities t
:with-fixed-width t
56 :with-footnotes t
:with-priority t
:with-special-strings t
57 :with-sub-superscript t
:with-toc t
:with-tables t
:with-tags t
58 :with-tasks t
:with-timestamps t
:with-todo-keywords t
)))
59 ;; Test some special values.
62 (org-export-parse-option-keyword
63 "arch:headline creator:comment d:(\"TEST\")
64 ^:{} toc:1 tags:not-in-toc tasks:todo")
65 '(:with-archived-trees
66 headline
:with-creator comment
:with-drawers
("TEST")
67 :with-sub-superscript
{} :with-toc
1 :with-tags not-in-toc
70 (ert-deftest test-org-export
/get-inbuffer-options
()
71 "Test reading all standard export keywords."
74 (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
77 #+DESCRIPTION: Testing
78 #+DESCRIPTION: with two lines
79 #+EMAIL: some@email.org
80 #+EXPORT_EXCLUDE_TAGS: noexport invisible
83 #+EXPORT_SELECT_TAGS: export
86 (org-export-get-inbuffer-options))
88 "Me, Myself and I" :creator
"Idem" :date
"Today"
89 :description
"Testing\nwith two lines" :email
"some@email.org"
90 :exclude-tags
("noexport" "invisible") :keywords
"test" :language
"en"
91 :select-tags
("export") :title
"Some title with spaces"))))
93 (ert-deftest test-org-export
/define-macro
()
94 "Try defining various Org macro using in-buffer #+MACRO: keyword."
96 (should (equal (org-test-with-temp-text "#+MACRO: one 1"
97 (org-export-get-inbuffer-options))
100 (should (equal (org-test-with-temp-text "#+MACRO: two (eval (+ 1 1))"
101 (org-export-get-inbuffer-options))
102 '(:macro-two
"(eval (+ 1 1))")))
104 (should-not (org-test-with-temp-text "#+MACRO: three"
105 (org-export-get-inbuffer-options)))
106 ;; Macro with newline character.
107 (should (equal (org-test-with-temp-text "#+MACRO: four a\\nb"
108 (org-export-get-inbuffer-options))
109 '(:macro-four
("a\nb"))))
110 ;; Macro with protected newline character.
111 (should (equal (org-test-with-temp-text "#+MACRO: five a\\\\nb"
112 (org-export-get-inbuffer-options))
113 '(:macro-five
("a\\nb"))))
115 (org-test-with-temp-text "#+MACRO: six 6\n#+MACRO: seven 1 + {{{six}}}"
118 (org-export-get-inbuffer-options)
122 ("1 + " (macro (:key
"six" :value
"{{{six}}}" :args nil
:begin
5 :end
14
123 :post-blank
0))))))))
125 (ert-deftest test-org-export
/handle-options
()
126 "Test if export options have an impact on output."
127 ;; Test exclude tags.
128 (org-test-with-temp-text "* Head1 :noexport:"
129 (org-test-with-backend "test"
131 (equal (org-export-as 'test nil nil nil
'(:exclude-tags
("noexport")))
133 ;; Test include tags.
134 (org-test-with-temp-text "
136 ** Sub-Head1.1 :export:
139 (org-test-with-backend "test"
142 "\\* Head1\n\\*\\* Sub-Head1.1[ \t]+:export:\n\\*\\*\\* Sub-Head1.1.1\n"
143 (org-export-as 'test nil nil nil
'(:select-tags
("export")))))))
144 ;; Test mixing include tags and exclude tags.
145 (org-test-with-temp-text "
147 ** Sub-Head1 :noexport:
150 ** Sub-Head1 :export:"
151 (org-test-with-backend "test"
154 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
157 '(:select-tags
("export") :exclude-tags
("noexport")))))))
159 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
160 (org-test-with-temp-text "* TODO Head1"
161 (org-test-with-backend "test"
162 (should (equal (org-export-as 'test nil nil nil
'(:with-tasks nil
))
164 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
165 (org-test-with-temp-text "* TODO Head1"
166 (org-test-with-backend "test"
167 (should (equal (org-export-as 'test nil nil nil
'(:with-tasks t
))
168 "* TODO Head1\n")))))
170 (org-test-with-temp-text "* Head1 :archive:"
171 (let ((org-archive-tag "archive"))
172 (org-test-with-backend "test"
174 (equal (org-export-as 'test nil nil nil
'(:with-archived-trees nil
))
176 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
177 (let ((org-archive-tag "archive"))
178 (org-test-with-backend "test"
181 "\\* Head1[ \t]+:archive:"
182 (org-export-as 'test nil nil nil
183 '(:with-archived-trees headline
)))))))
184 (org-test-with-temp-text "* Head1 :archive:"
185 (let ((org-archive-tag "archive"))
186 (org-test-with-backend "test"
189 "\\`\\* Head1[ \t]+:archive:\n\\'"
190 (org-export-as 'test nil nil nil
'(:with-archived-trees t
)))))))
192 (let ((org-drawers '("TEST")))
193 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
194 (org-test-with-backend "test"
195 (should (equal (org-export-as 'test nil nil nil
'(:with-drawers nil
))
197 (let ((org-drawers '("TEST")))
198 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
199 (org-test-with-backend "test"
200 (should (equal (org-export-as 'test nil nil nil
'(:with-drawers t
))
201 ":TEST:\ncontents\n:END:\n"))))))
203 (ert-deftest test-org-export
/comment-tree
()
204 "Test if export process ignores commented trees."
205 (let ((org-comment-string "COMMENT"))
206 (org-test-with-temp-text "* COMMENT Head1"
207 (org-test-with-backend "test"
208 (should (equal (org-export-as 'test
) ""))))))
210 (ert-deftest test-org-export
/export-scope
()
211 "Test all export scopes."
212 (org-test-with-temp-text "
217 (org-test-with-backend "test"
220 (should (equal (org-export-as 'test
'subtree
) "text\n*** Head3\n"))
222 (goto-char (point-min))
225 (should (equal (org-export-as 'test nil
'visible
) "* Head1\n"))
227 (flet ((org-test-template (body info
) (format "BEGIN\n%sEND" body
)))
228 (should (equal (org-export-as 'test nil nil
'body-only
)
229 "* Head1\n** Head2\ntext\n*** Head3\n"))
230 (should (equal (org-export-as 'test
)
231 "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
233 (goto-char (point-min))
236 (should (equal (org-export-as 'test
) "text\n")))))
238 (ert-deftest test-org-export
/export-snippet
()
239 "Test export snippets transcoding."
240 (org-test-with-temp-text "@test{A}@t{B}"
241 (org-test-with-backend "test"
242 (flet ((org-test-export-snippet
243 (snippet contents info
)
244 (when (eq (org-export-snippet-backend snippet
) 'test
)
245 (org-element-property :value snippet
))))
246 (let ((org-export-snippet-translation-alist nil
))
247 (should (equal (org-export-as 'test
) "A\n")))
248 (let ((org-export-snippet-translation-alist '(("t" .
"test"))))
249 (should (equal (org-export-as 'test
) "AB\n")))))))
251 (ert-deftest test-org-export
/expand-include
()
252 "Test file inclusion in an Org buffer."
253 ;; Full insertion with recursive inclusion.
254 (org-test-with-temp-text
255 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir
)
256 (org-export-expand-include-keyword)
257 (should (equal (buffer-string)
258 "Small Org file with an include keyword.
260 #+BEGIN_SRC emacs-lisp :exports results
268 ;; Localized insertion.
269 (org-test-with-temp-text
270 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
272 (org-export-expand-include-keyword)
273 (should (equal (buffer-string)
274 "Small Org file with an include keyword.\n")))
275 ;; Insertion with constraints on headlines level.
276 (org-test-with-temp-text
278 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
280 (org-export-expand-include-keyword)
281 (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
282 ;; Inclusion within an example block.
283 (org-test-with-temp-text
284 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
286 (org-export-expand-include-keyword)
290 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
291 ;; Inclusion within a src-block.
292 (org-test-with-temp-text
294 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
296 (org-export-expand-include-keyword)
297 (should (equal (buffer-string)
298 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))