Merge branch 'maint'
[org-mode.git] / testing / lisp / test-ob-exp.el
bloba0c4d00d779a0318cde34001196736633949af4d
1 ;;; test-ob-exp.el
3 ;; Copyright (c) 2010-2012 Eric Schulte
4 ;; Authors: Eric Schulte
6 ;; This file is not part of GNU Emacs.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Comments:
23 ;; Template test file for Org-mode tests
25 ;;; Code:
26 (ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-headers ()
27 "Testing export without any headlines in the org-mode file."
28 (let ((html-file (concat (file-name-sans-extension org-test-no-heading-file)
29 ".html")))
30 (when (file-exists-p html-file) (delete-file html-file))
31 (org-test-in-example-file org-test-no-heading-file
32 ;; export the file to html
33 (org-export-as-html nil))
34 ;; should create a .html file
35 (should (file-exists-p html-file))
36 ;; should not create a file with "::" appended to it's name
37 (should-not (file-exists-p (concat org-test-no-heading-file "::")))
38 (when (file-exists-p html-file) (delete-file html-file))))
40 (ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-file ()
41 "Testing export from buffers which are not visiting any file."
42 (when (get-buffer "*Org HTML Export*") (kill-buffer "*Org HTML Export*"))
43 (should-not (get-buffer "*Org HTML Export*"))
44 ;; export the file to HTML in a temporary buffer
45 (org-test-in-example-file nil (org-export-as-html-to-buffer nil))
46 ;; should create a .html buffer
47 (should (buffer-live-p (get-buffer "*Org HTML Export*")))
48 ;; should contain the content of the buffer
49 (save-excursion
50 (set-buffer (get-buffer "*Org HTML Export*"))
51 (should (string-match (regexp-quote org-test-file-ob-anchor)
52 (buffer-string))))
53 (when (get-buffer "*Org HTML Export*") (kill-buffer "*Org HTML Export*")))
55 (ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-headers2 ()
56 "Testing export without any headlines in the org-mode file."
57 (let ((html-file (concat (file-name-sans-extension
58 org-test-link-in-heading-file)
59 ".html")))
60 (when (file-exists-p html-file) (delete-file html-file))
61 (org-test-in-example-file org-test-link-in-heading-file
62 ;; export the file to html
63 (org-export-as-html nil))
64 ;; should create a .html file
65 (should (file-exists-p html-file))
66 ;; should not create a file with "::" appended to it's name
67 (should-not (file-exists-p (concat org-test-link-in-heading-file "::")))
68 (when (file-exists-p html-file) (delete-file html-file))))
70 (ert-deftest ob-exp/noweb-on-export ()
71 "Noweb header arguments export correctly.
72 - yes expand on both export and tangle
73 - no expand on neither export or tangle
74 - tangle expand on only tangle not export"
75 (org-test-at-id "eb1f6498-5bd9-45e0-9c56-50717053e7b7"
76 (org-narrow-to-subtree)
77 (let ((exported-html
78 (org-export-as-html nil nil 'string 'body-only))
79 (test-point 0))
81 (org-test-with-temp-text-in-file
82 exported-html
84 ;; check following ouput exists and in order
85 (mapcar (lambda (x)
86 (should (< test-point
87 (re-search-forward
89 nil t)))
90 (setq test-point (point)))
91 '("<code>:noweb</code> header argument expansion"
92 "message" "expanded1"
93 "message" "expanded2"
94 "noweb-1-yes-start"
95 "message" "expanded1"
96 "noweb-no-start"
97 "&lt;&lt;noweb-example1&gt;&gt;"
98 "noweb-2-yes-start"
99 "message" "expanded2"
100 "noweb-tangle-start"
101 "&lt;&lt;noweb-example1&gt;&gt;"
102 "&lt;&lt;noweb-example2&gt;&gt;"))))))
104 (ert-deftest ob-exp/noweb-on-export-with-exports-results ()
105 "Noweb header arguments export correctly using :exports results.
106 - yes expand on both export and tangle
107 - no expand on neither export or tangle
108 - tangle expand on only tangle not export"
109 (org-test-at-id "8701beb4-13d9-468c-997a-8e63e8b66f8d"
110 (org-narrow-to-subtree)
111 (let ((exported-html
112 (org-export-as-html nil nil 'string 'body-only))
113 (test-point 0))
115 (org-test-with-temp-text-in-file
116 exported-html
118 ;; check following ouput exists and in order
119 (mapcar (lambda (x)
120 (should (< test-point
121 (re-search-forward
123 nil t)))
124 (setq test-point (point)))
125 '("<code>:noweb</code> header argument expansion using :exports results"
126 "expanded1"
127 "expanded2"
128 "expanded1"
129 "noweb-no-start"
130 "&lt;&lt;noweb-example1&gt;&gt;"
131 "expanded2"
132 "&lt;&lt;noweb-example1&gt;&gt;"
133 "&lt;&lt;noweb-example2&gt;&gt;"))))))
135 (ert-deftest ob-exp/exports-both ()
136 "Test the :exports both header argument.
137 The code block should create both <pre></pre> and <table></table>
138 elements in the final html."
139 (org-test-at-id "92518f2a-a46a-4205-a3ab-bcce1008a4bb"
140 (org-narrow-to-subtree)
141 (let ((exported-html
142 (org-export-as-html nil nil 'string 'body-only))
143 (test-point 0))
144 (org-test-with-temp-text-in-file
145 exported-html
147 ;; check following ouput exists and in order
148 (mapcar (lambda (x)
149 (should (< test-point
150 (re-search-forward
152 nil t)))
153 (setq test-point (point)))
154 '( "Pascal's Triangle &ndash; exports both test"
155 "<pre"
156 "defun" "pascals-triangle"
157 "if""list""list""let*""prev-triangle"
158 "pascals-triangle""prev-row""car""reverse""prev-triangle"
159 "append""prev-triangle""list""map""list"
160 "append""prev-row""append""prev-row""pascals-triangle"
161 "</pre>"
162 "<table""<tbody>"
163 "<tr>"">1<""</tr>"
164 "<tr>"">1<"">1<""</tr>"
165 "<tr>"">1<"">2<"">1<""</tr>"
166 "<tr>"">1<"">3<"">3<"">1<""</tr>"
167 "<tr>"">1<"">4<"">6<"">4<"">1<""</tr>"
168 "<tr>"">1<"">5<"">10<"">10<"">5<"">1<""</tr>"
169 "</tbody>""</table>"))))))
171 (ert-deftest ob-exp/mixed-blocks-with-exports-both ()
172 (org-test-at-id "5daa4d03-e3ea-46b7-b093-62c1b7632df3"
173 (org-narrow-to-subtree)
174 (let ((exported-html
175 (org-export-as-html nil nil 'string 'body-only))
176 (test-point 0))
177 (org-test-with-temp-text-in-file
178 exported-html
179 ;; check following ouput exists and in order
180 (mapcar (lambda (x)
181 (should (< test-point (re-search-forward x nil t)))
182 (setq test-point (point)))
183 '("mixed blocks with exports both"
184 "<ul class=\"org-ul\">"
185 "<li>""a""</li>"
186 "<li>""b""</li>"
187 "<li>""c""</li>"
188 "</ul>"
189 "<pre"
190 "\"code block results\""
191 "</pre>"
192 "<pre class=\"example\">"
193 "code block results"
194 "</pre>"))))))
196 (ert-deftest ob-exp/export-with-name ()
197 (let ((org-babel-exp-code-template
198 "=%name=\n#+BEGIN_SRC %lang%flags\nbody\n#+END_SRC"))
199 (org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9"
200 (org-narrow-to-subtree)
201 (let ((ascii (org-export-as-ascii nil nil 'string 'body-only)))
202 (should (string-match "qux" ascii))))))
204 (ert-deftest ob-exp/export-with-header-argument ()
205 (let ((org-babel-exp-code-template
207 | header | value |
208 |---------+----------|
209 | foo | %foo |
210 | results | %results |
211 #+BEGIN_SRC %lang%flags\nbody\n#+END_SRC"))
212 (org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9"
213 (org-narrow-to-subtree)
214 (let ((ascii (org-export-as-ascii nil nil 'string 'body-only)))
215 (should (string-match "baz" ascii))
216 (should (string-match "replace" ascii))))))
218 (ert-deftest ob-exp/noweb-no-export-and-exports-both ()
219 (org-test-at-id "8a820f6c-7980-43db-8a24-0710d33729c9"
220 (org-narrow-to-subtree)
221 (let ((html (org-export-as-html nil nil 'string 'body-only)))
222 (should (string-match (regexp-quote "noweb-no-export-and-exports-both-1")
223 html)))))
225 (ert-deftest ob-exp/evaluate-all-executables-in-order ()
226 (org-test-at-id "96cc7073-97ec-4556-87cf-1f9bffafd317"
227 (org-narrow-to-subtree)
228 (let (*evaluation-collector*)
229 (org-export-as-ascii nil nil 'string)
230 (should (equal '(5 4 3 2 1) *evaluation-collector*)))))
232 (ert-deftest ob-exp/exports-inline ()
233 (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
234 (org-narrow-to-subtree)
235 (let ((html (org-export-as-html nil nil nil 'string 'body-only)))
236 (dolist (rx '("middle <\\(code\\|tt\\)>1</\\(code\\|tt\\)> of"
237 "end of a line. <\\(code\\|tt\\)>2</\\(code\\|tt\\)>"
238 "<\\(code\\|tt\\)>3</\\(code\\|tt\\)> Here is one"))
239 (should (string-match rx html))))))
241 (ert-deftest ob-exp/export-call-line-information ()
242 (org-test-at-id "bec63a04-491e-4caa-97f5-108f3020365c"
243 (org-narrow-to-subtree)
244 (let* ((org-babel-exp-call-line-template "\n: call: %line special-token")
245 (html (org-export-as-html nil nil 'string t)))
246 (should (string-match "double" html))
247 (should (string-match "16" html))
248 (should (string-match "special-token" html)))))
250 (ert-deftest ob-exp/noweb-strip-export-ensure-strips ()
251 (org-test-at-id "8e7bd234-99b2-4b14-8cd6-53945e409775"
252 (org-narrow-to-subtree)
253 (org-babel-next-src-block 2)
254 (should (= 110 (org-babel-execute-src-block)))
255 (let ((ascii (org-export-as-ascii nil nil 'string t)))
256 (should-not (string-match (regexp-quote "<<strip-export-1>>") ascii))
257 (should-not (string-match (regexp-quote "i=\"10\"") ascii)))))
259 (ert-deftest ob-exp/export-from-a-temp-buffer ()
260 :expected-result :failed
261 (org-test-with-temp-text
263 #+Title: exporting from a temporary buffer
265 #+name: foo
266 #+BEGIN_SRC emacs-lisp
267 :foo
268 #+END_SRC
270 #+name: bar
271 #+BEGIN_SRC emacs-lisp
272 :bar
273 #+END_SRC
275 #+BEGIN_SRC emacs-lisp :var foo=foo :noweb yes :exports results
276 (list foo <<bar>>)
277 #+END_SRC
279 (let* ((org-current-export-file (current-buffer))
280 (ascii (org-export-as-ascii nil nil 'string)))
281 (should (string-match (regexp-quote (format nil "%S" '(:foo :bar)))
282 ascii)))))
285 (provide 'test-ob-exp)
287 ;;; test-ob-exp.el ends here