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