3 ;; Copyright (c) 2010-2014 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/>.
23 ;; Template test file for Org-mode tests
27 (defmacro org-test-with-expanded-babel-code
(&rest body
)
28 "Execute BODY while in a buffer with all Babel code evaluated.
29 Current buffer is a copy of the original buffer."
30 `(let ((string (buffer-string))
31 (buf (current-buffer)))
35 (let ((org-current-export-file buf
))
36 (org-babel-exp-process-buffer))
37 (goto-char (point-min))
40 (ert-deftest test-ob-exp
/org-babel-exp-src-blocks
/w-no-headers
()
41 "Testing export without any headlines in the Org mode file."
43 (let ((html-file (concat (file-name-sans-extension org-test-no-heading-file
)
45 (when (file-exists-p html-file
) (delete-file html-file
))
46 (org-test-in-example-file org-test-no-heading-file
47 ;; Export the file to HTML.
48 (org-export-to-file 'html html-file
))
49 ;; should create a .html file
50 (should (file-exists-p html-file
))
51 ;; should not create a file with "::" appended to it's name
52 (should-not (file-exists-p (concat org-test-no-heading-file
"::")))
53 (when (file-exists-p html-file
) (delete-file html-file
))))
55 (ert-deftest test-ob-exp
/org-babel-exp-src-blocks
/w-no-file
()
56 "Testing export from buffers which are not visiting any file."
58 (let ((name (generate-new-buffer-name "*Org HTML Export*")))
59 (org-test-in-example-file nil
60 (org-export-to-buffer 'html name nil nil nil t
))
61 ;; Should create a HTML buffer.
62 (should (buffer-live-p (get-buffer name
)))
63 ;; Should contain the content of the buffer.
64 (with-current-buffer (get-buffer name
)
65 (should (string-match (regexp-quote org-test-file-ob-anchor
)
67 (when (get-buffer name
) (kill-buffer name
))))
69 (ert-deftest test-ob-exp
/org-babel-exp-src-blocks
/w-no-headers2
()
70 "Testing export without any headlines in the org-mode file."
71 (let ((html-file (concat (file-name-sans-extension
72 org-test-link-in-heading-file
)
74 (when (file-exists-p html-file
) (delete-file html-file
))
75 (org-test-in-example-file org-test-link-in-heading-file
76 ;; export the file to html
77 (org-export-to-file 'html html-file
))
78 ;; should create a .html file
79 (should (file-exists-p html-file
))
80 ;; should not create a file with "::" appended to it's name
81 (should-not (file-exists-p (concat org-test-link-in-heading-file
"::")))
82 (when (file-exists-p html-file
) (delete-file html-file
))))
84 (ert-deftest ob-exp
/noweb-on-export
()
85 "Noweb header arguments export correctly.
86 - yes expand on both export and tangle
87 - no expand on neither export or tangle
88 - tangle expand on only tangle not export"
91 '("(message \"expanded1\")" "(message \"expanded2\")" ";; noweb-1-yes-start
92 (message \"expanded1\")
93 (message \"expanded1\")" ";; noweb-no-start
94 <<noweb-example1>>" ";; noweb-2-yes-start
95 (message \"expanded2\")
96 (message \"expanded2\")" ";; noweb-tangle-start
99 (org-test-at-id "eb1f6498-5bd9-45e0-9c56-50717053e7b7"
100 (org-narrow-to-subtree)
102 (org-test-with-expanded-babel-code (org-element-parse-buffer))
104 (lambda (src) (org-trim (org-element-property :value src
))))))))
106 (ert-deftest ob-exp
/noweb-on-export-with-exports-results
()
107 "Noweb header arguments export correctly using :exports results.
108 - yes expand on both export and tangle
109 - no expand on neither export or tangle
110 - tangle expand on only tangle not export"
114 <<noweb-example1>>" "<<noweb-example1>>
116 (org-test-at-id "8701beb4-13d9-468c-997a-8e63e8b66f8d"
117 (org-narrow-to-subtree)
119 (org-test-with-expanded-babel-code (org-element-parse-buffer))
121 (lambda (src) (org-trim (org-element-property :value src
))))))))
123 (ert-deftest ob-exp
/exports-both
()
124 "Test the \":exports both\" header argument.
125 The code block evaluation should create both a code block and
127 (org-test-at-id "92518f2a-a46a-4205-a3ab-bcce1008a4bb"
128 (org-narrow-to-subtree)
129 (let ((tree (org-test-with-expanded-babel-code (org-element-parse-buffer))))
130 (should (and (org-element-map tree
'src-block
'identity
)
131 (org-element-map tree
'table
'identity
))))))
133 (ert-deftest ob-exp
/mixed-blocks-with-exports-both
()
136 '(property-drawer plain-list src-block fixed-width src-block plain-list
)
137 (org-test-at-id "5daa4d03-e3ea-46b7-b093-62c1b7632df3"
138 (org-narrow-to-subtree)
139 (mapcar 'org-element-type
141 (org-test-with-expanded-babel-code
142 (org-element-parse-buffer 'greater-element
))
143 'section
'org-element-contents nil t
))))))
145 (ert-deftest ob-exp
/export-with-name
()
149 (let ((org-babel-exp-code-template
150 "=%name=\n#+BEGIN_SRC %lang%flags\nbody\n#+END_SRC"))
151 (org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9"
152 (org-narrow-to-subtree)
153 (org-test-with-expanded-babel-code
154 (buffer-string)))))))
156 (ert-deftest ob-exp
/export-with-header-argument
()
157 (let ((org-babel-exp-code-template
160 |---------+----------|
162 | results | %results |
163 #+BEGIN_SRC %lang%flags\nbody\n#+END_SRC"))
164 (org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9"
165 (org-narrow-to-subtree)
166 (org-test-with-expanded-babel-code
167 (should (string-match "baz" (buffer-string)))
168 (should (string-match "replace" (buffer-string)))))))
170 (ert-deftest ob-exp
/noweb-no-export-and-exports-both
()
173 "<<noweb-no-export-and-exports-both-1>>"
174 (org-test-at-id "8a820f6c-7980-43db-8a24-0710d33729c9"
175 (org-narrow-to-subtree)
176 (org-test-with-expanded-babel-code
177 (org-element-map (org-element-parse-buffer) 'src-block
178 (lambda (src-block) (org-element-property :value src-block
))
181 (ert-deftest ob-exp
/evaluate-all-executables-in-order
()
184 (let (*evaluation-collector
*)
185 (org-test-at-id "96cc7073-97ec-4556-87cf-1f9bffafd317"
186 (org-narrow-to-subtree)
188 (fboundp 'org-export-execute-babel-code
)
189 (org-test-with-expanded-babel-code *evaluation-collector
*))))))
191 (ert-deftest ob-exp
/exports-inline
()
194 (regexp-quote "Here is one in the middle =1= of a line.
195 Here is one at the end of a line. =2=
196 =3= Here is one at the beginning of a line.")
197 (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
198 (org-narrow-to-subtree)
199 (org-test-with-expanded-babel-code (buffer-string))))))
201 (ert-deftest ob-exp
/export-call-line-information
()
202 (org-test-at-id "bec63a04-491e-4caa-97f5-108f3020365c"
203 (org-narrow-to-subtree)
204 (let ((org-babel-exp-call-line-template "\n: call: %line special-token"))
205 (org-test-with-expanded-babel-code
206 (should (string-match "double" (buffer-string)))
207 (should (string-match "16" (buffer-string)))
208 (should (string-match "special-token" (buffer-string)))))))
210 (ert-deftest ob-exp
/noweb-strip-export-ensure-strips
()
211 (org-test-at-id "8e7bd234-99b2-4b14-8cd6-53945e409775"
212 (org-narrow-to-subtree)
213 (org-babel-next-src-block 2)
214 (should (= 110 (org-babel-execute-src-block)))
215 (let ((result (org-test-with-expanded-babel-code (buffer-string))))
216 (should-not (string-match (regexp-quote "<<strip-export-1>>") result
))
217 (should-not (string-match (regexp-quote "i=\"10\"") result
)))))
219 (ert-deftest ob-exp
/use-case-of-reading-entry-properties
()
220 (org-test-at-id "cc5fbc20-bca5-437a-a7b8-2b4d7a03f820"
221 (org-narrow-to-subtree)
222 (let* ((case-fold-search nil
)
223 (result (org-test-with-expanded-babel-code (buffer-string)))
224 (sect "a:1, b:0, c:3, d:0, e:0")
225 (sub0 "a:1, b:2, c:4, d:0, e:0")
226 (sub1 "a:1, b:2, c:5, d:0, e:6")
229 (should (string-match (concat "_shell sect call\n: shell " sect
"\n")
231 (should (string-match (concat "_elisp sect call\n: elisp " sect
"\n")
233 (should (string-match (concat "\n- sect inline =shell " sect
"=\n")
235 (should (string-match (concat "\n- sect inline =elisp " sect
"=\n")
237 ;; entry "subsection", call without arguments
238 (should (string-match (concat "_shell sub0 call\n: shell " sub0
"\n")
240 (should (string-match (concat "_elisp sub0 call\n: elisp " sub0
"\n")
242 (should (string-match (concat "\n- sub0 inline =shell " sub0
"=\n")
244 (should (string-match (concat "\n- sub0 inline =elisp " sub0
"=\n")
246 ;; entry "subsection", call with arguments
247 (should (string-match (concat "_shell sub1 call\n: shell " sub1
"\n")
249 (should (string-match (concat "_elisp sub1 call\n: elisp " sub1
"\n")
251 (should (string-match (concat "\n- sub1 inline =shell " sub1
"=\n")
253 (should (string-match (concat "\n- sub1 inline =elisp " sub1
"=\n")
255 ;; entry "function definition"
256 (should (string-match (concat "_location_shell\n: shell " func
"\n")
258 (should (string-match (concat "_location_elisp\n: elisp " func
"\n")
261 (ert-deftest ob-exp
/export-from-a-temp-buffer
()
262 :expected-result
:failed
263 (org-test-with-temp-text
265 #+Title: exporting from a temporary buffer
268 #+BEGIN_SRC emacs-lisp
273 #+BEGIN_SRC emacs-lisp
277 #+BEGIN_SRC emacs-lisp :var foo=foo :noweb yes :exports results
281 (let* ((ascii (org-export-as 'ascii
)))
282 (should (string-match (regexp-quote (format nil
"%S" '(:foo
:bar
)))
285 (ert-deftest ob-export
/export-with-results-before-block
()
286 "Test export when results are inserted before source block."
293 #+BEGIN_SRC emacs-lisp
296 (org-test-with-temp-text
300 #+BEGIN_SRC emacs-lisp :exports both
303 (org-export-execute-babel-code)
306 (ert-deftest ob-export
/export-src-block-with-switches
()
307 "Test exporting a source block with switches."
310 "\\`#\\+BEGIN_SRC emacs-lisp -n -r$"
311 (org-test-with-temp-text
312 "#+BEGIN_SRC emacs-lisp -n -r\n\(+ 1 1)\n#+END_SRC"
313 (org-export-execute-babel-code)
316 (ert-deftest ob-export
/export-src-block-with-flags
()
317 "Test exporting a source block with a flag."
320 "\\`#\\+BEGIN_SRC emacs-lisp -some-flag$"
321 (org-test-with-temp-text
322 "#+BEGIN_SRC emacs-lisp :flags -some-flag\n\(+ 1 1)\n#+END_SRC"
323 (org-export-execute-babel-code)
326 (ert-deftest ob-export
/export-and-indentation
()
327 "Test indentation of evaluated source blocks during export."
332 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n t\n#+END_SRC"
333 (let ((indent-tabs-mode t
)
335 (org-src-preserve-indentation nil
))
336 (org-export-execute-babel-code)
338 ;; Preserve indentation with "-i" flag.
342 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n t\n#+END_SRC"
343 (let ((indent-tabs-mode t
)
345 (org-export-execute-babel-code)
347 ;; Preserve indentation with a non-nil
348 ;; `org-src-preserve-indentation'.
352 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n t\n#+END_SRC"
353 (let ((indent-tabs-mode t
)
355 (org-src-preserve-indentation t
))
356 (org-export-execute-babel-code)
359 (ert-deftest ob-export
/export-under-commented-headline
()
360 "Test evaluation of code blocks under COMMENT headings."
361 ;; Do not eval block in a commented headline.
365 (org-test-with-temp-text "* Headline
366 #+BEGIN_SRC emacs-lisp :exports results
369 (org-export-execute-babel-code)
374 (org-test-with-temp-text "* COMMENT Headline
375 #+BEGIN_SRC emacs-lisp :exports results
378 (org-export-execute-babel-code)
380 ;; Do not eval inline blocks either.
384 (org-test-with-temp-text "* Headline
385 src_emacs-lisp{(+ 1 1)}"
386 (org-export-execute-babel-code)
391 (org-test-with-temp-text "* COMMENT Headline
392 src_emacs-lisp{(+ 1 1)}"
393 (org-export-execute-babel-code)
395 ;; Also check parent headlines.
399 (org-test-with-temp-text "
402 #+BEGIN_SRC emacs-lisp :exports results
405 (org-export-execute-babel-code)
409 (provide 'test-ob-exp
)
411 ;;; test-ob-exp.el ends here