org-export: Fix select-tag handling
[org-mode/org-mode-NeilSmithlineMods.git] / testing / contrib / lisp / test-org-export.el
blob20640d1f9089d84278497478be49550e2555f8e9
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:
15 (let ((load-path (cons (expand-file-name
16 ".." (file-name-directory
17 (or load-file-name buffer-file-name)))
18 load-path)))
19 (require 'org-test)
20 (require 'org-test-ob-consts)
21 (require 'org-export))
25 ;;; Tests
27 (require 'org-test)
28 (require 'org-export)
30 (defmacro org-test-with-backend (backend &rest body)
31 "Execute body with an export back-end defined.
33 BACKEND is the name, as a string, of the back-end. BODY is the
34 body to execute. The defined back-end simply returns parsed data
35 as Org syntax."
36 (declare (debug (form body)) (indent 1))
37 `(flet ,(let (transcoders)
38 (dolist (type (append org-element-all-elements
39 org-element-all-objects)
40 transcoders)
41 (push `(,(intern (format "org-%s-%s" backend type))
42 (obj contents info)
43 (,(intern (format "org-element-%s-interpreter" type))
44 obj contents))
45 transcoders)))
46 ,@body))
48 (ert-deftest test-org-export/parse-option-keyword ()
49 "Test reading all standard #+OPTIONS: items."
50 (should
51 (equal
52 (org-export-parse-option-keyword
53 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
54 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t")
55 '(:headline-levels
56 1 :preserve-breaks t :section-numbers t :time-stamp-file t
57 :with-archived-trees t :with-author t :with-creator t :with-drawers t
58 :with-email t :with-emphasize t :with-entities t :with-fixed-width t
59 :with-footnotes t :with-priority t :with-special-strings t
60 :with-sub-superscript t :with-toc t :with-tables t :with-tags t
61 :with-tasks t :with-timestamps t :with-todo-keywords t)))
62 ;; Test some special values.
63 (should
64 (equal
65 (org-export-parse-option-keyword
66 "arch:headline creator:comment d:(\"TEST\")
67 ^:{} toc:1 tags:not-in-toc tasks:todo")
68 '(:with-archived-trees
69 headline :with-creator comment :with-drawers ("TEST")
70 :with-sub-superscript {} :with-toc 1 :with-tags not-in-toc
71 :with-tasks todo))))
73 (ert-deftest test-org-export/get-inbuffer-options ()
74 "Test reading all standard export keywords."
75 (should
76 (equal
77 (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
78 #+CREATOR: Idem
79 #+DATE: Today
80 #+DESCRIPTION: Testing
81 #+DESCRIPTION: with two lines
82 #+EMAIL: some@email.org
83 #+EXPORT_EXCLUDE_TAGS: noexport invisible
84 #+KEYWORDS: test
85 #+LANGUAGE: en
86 #+EXPORT_SELECT_TAGS: export
87 #+TITLE: Some title
88 #+TITLE: with spaces"
89 (org-export-get-inbuffer-options))
90 '(:author
91 "Me, Myself and I" :creator "Idem" :date "Today"
92 :description "Testing\nwith two lines" :email "some@email.org"
93 :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
94 :select-tags ("export") :title "Some title with spaces"))))
96 (ert-deftest test-org-export/define-macro ()
97 "Try defining various Org macro using in-buffer #+MACRO: keyword."
98 ;; Parsed macro.
99 (should (equal (org-test-with-temp-text "#+MACRO: one 1"
100 (org-export-get-inbuffer-options))
101 '(:macro-one ("1"))))
102 ;; Evaled macro.
103 (should (equal (org-test-with-temp-text "#+MACRO: two (eval (+ 1 1))"
104 (org-export-get-inbuffer-options))
105 '(:macro-two "(eval (+ 1 1))")))
106 ;; Incomplete macro.
107 (should-not (org-test-with-temp-text "#+MACRO: three"
108 (org-export-get-inbuffer-options)))
109 ;; Macro with newline character.
110 (should (equal (org-test-with-temp-text "#+MACRO: four a\\nb"
111 (org-export-get-inbuffer-options))
112 '(:macro-four ("a\nb"))))
113 ;; Macro with protected newline character.
114 (should (equal (org-test-with-temp-text "#+MACRO: five a\\\\nb"
115 (org-export-get-inbuffer-options))
116 '(:macro-five ("a\\nb"))))
117 ;; Recursive macro.
118 (org-test-with-temp-text "#+MACRO: six 6\n#+MACRO: seven 1 + {{{six}}}"
119 (should
120 (equal
121 (org-export-get-inbuffer-options)
122 '(:macro-six
123 ("6")
124 :macro-seven
125 ("1 + " (macro (:key "six" :value "{{{six}}}" :args nil :begin 5 :end 14
126 :post-blank 0))))))))
128 (ert-deftest test-org-export/handle-options ()
129 "Test if export options have an impact on output."
130 ;; Test exclude tags.
131 (org-test-with-temp-text "* Head1 :noexport:"
132 (org-test-with-backend "test"
133 (should
134 (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
135 ""))))
136 ;; Test include tags.
137 (org-test-with-temp-text "
138 * Head1
139 ** Sub-Head1.1 :export:
140 *** Sub-Head1.1.1
141 * Head2"
142 (org-test-with-backend "test"
143 (should
144 (string-match
145 "\\* Head1\n\\*\\* Sub-Head1.1[ \t]+:export:\n\\*\\*\\* Sub-Head1.1.1\n"
146 (org-export-as 'test nil nil nil '(:select-tags ("export")))))))
147 ;; Test mixing include tags and exclude tags.
148 (org-test-with-temp-text "
149 * Head1 :export:
150 ** Sub-Head1 :noexport:
151 ** Sub-Head2
152 * Head2 :noexport:
153 ** Sub-Head1 :export:"
154 (org-test-with-backend "test"
155 (should
156 (string-match
157 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
158 (org-export-as
159 'test nil nil nil
160 '(:select-tags ("export") :exclude-tags ("noexport")))))))
161 ;; Ignore tasks.
162 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
163 (org-test-with-temp-text "* TODO Head1"
164 (org-test-with-backend "test"
165 (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
166 "")))))
167 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
168 (org-test-with-temp-text "* TODO Head1"
169 (org-test-with-backend "test"
170 (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
171 "* TODO Head1\n")))))
172 ;; Archived tree.
173 (org-test-with-temp-text "* Head1 :archive:"
174 (let ((org-archive-tag "archive"))
175 (org-test-with-backend "test"
176 (should
177 (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
178 "")))))
179 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
180 (let ((org-archive-tag "archive"))
181 (org-test-with-backend "test"
182 (should
183 (string-match
184 "\\* Head1[ \t]+:archive:"
185 (org-export-as 'test nil nil nil
186 '(:with-archived-trees headline)))))))
187 (org-test-with-temp-text "* Head1 :archive:"
188 (let ((org-archive-tag "archive"))
189 (org-test-with-backend "test"
190 (should
191 (string-match
192 "\\`\\* Head1[ \t]+:archive:\n\\'"
193 (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
194 ;; Drawers.
195 (let ((org-drawers '("TEST")))
196 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
197 (org-test-with-backend "test"
198 (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
199 "")))))
200 (let ((org-drawers '("TEST")))
201 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
202 (org-test-with-backend "test"
203 (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
204 ":TEST:\ncontents\n:END:\n"))))))
206 (ert-deftest test-org-export/comment-tree ()
207 "Test if export process ignores commented trees."
208 (let ((org-comment-string "COMMENT"))
209 (org-test-with-temp-text "* COMMENT Head1"
210 (org-test-with-backend "test"
211 (should (equal (org-export-as 'test) ""))))))
213 (ert-deftest test-org-export/export-scope ()
214 "Test all export scopes."
215 (org-test-with-temp-text "
216 * Head1
217 ** Head2
218 text
219 *** Head3"
220 (org-test-with-backend "test"
221 ;; Subtree.
222 (forward-line 3)
223 (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
224 ;; Visible.
225 (goto-char (point-min))
226 (forward-line)
227 (org-cycle)
228 (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
229 ;; Body only.
230 (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
231 (should (equal (org-export-as 'test nil nil 'body-only)
232 "* Head1\n** Head2\ntext\n*** Head3\n"))
233 (should (equal (org-export-as 'test)
234 "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
235 ;; Region.
236 (goto-char (point-min))
237 (forward-line 3)
238 (mark-paragraph)
239 (should (equal (org-export-as 'test) "text\n")))))