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 (org-babel-exp-process-buffer buf
)
36 (goto-char (point-min))
39 (ert-deftest test-ob-exp
/org-babel-exp-src-blocks
/w-no-headers
()
40 "Testing export without any headlines in the Org mode file."
42 (let ((html-file (concat (file-name-sans-extension org-test-no-heading-file
)
44 (when (file-exists-p html-file
) (delete-file html-file
))
45 (org-test-in-example-file org-test-no-heading-file
46 ;; Export the file to HTML.
47 (org-export-to-file 'html html-file
))
48 ;; should create a .html file
49 (should (file-exists-p html-file
))
50 ;; should not create a file with "::" appended to it's name
51 (should-not (file-exists-p (concat org-test-no-heading-file
"::")))
52 (when (file-exists-p html-file
) (delete-file html-file
))))
54 (ert-deftest test-ob-exp
/org-babel-exp-src-blocks
/w-no-file
()
55 "Testing export from buffers which are not visiting any file."
57 (let ((name (generate-new-buffer-name "*Org HTML Export*")))
58 (org-test-in-example-file nil
59 (org-export-to-buffer 'html name nil nil nil t
))
60 ;; Should create a HTML buffer.
61 (should (buffer-live-p (get-buffer name
)))
62 ;; Should contain the content of the buffer.
63 (with-current-buffer (get-buffer name
)
64 (should (string-match (regexp-quote org-test-file-ob-anchor
)
66 (when (get-buffer name
) (kill-buffer name
))))
68 (ert-deftest test-ob-exp
/org-babel-exp-src-blocks
/w-no-headers2
()
69 "Testing export without any headlines in the org-mode file."
70 (let ((html-file (concat (file-name-sans-extension
71 org-test-link-in-heading-file
)
73 (when (file-exists-p html-file
) (delete-file html-file
))
74 (org-test-in-example-file org-test-link-in-heading-file
75 ;; export the file to html
76 (org-export-to-file 'html html-file
))
77 ;; should create a .html file
78 (should (file-exists-p html-file
))
79 ;; should not create a file with "::" appended to it's name
80 (should-not (file-exists-p (concat org-test-link-in-heading-file
"::")))
81 (when (file-exists-p html-file
) (delete-file html-file
))))
83 (ert-deftest ob-exp
/noweb-on-export
()
84 "Noweb header arguments export correctly.
85 - yes expand on both export and tangle
86 - no expand on neither export or tangle
87 - tangle expand on only tangle not export"
90 '("(message \"expanded1\")" "(message \"expanded2\")" ";; noweb-1-yes-start
91 (message \"expanded1\")
92 (message \"expanded1\")" ";; noweb-no-start
93 <<noweb-example1>>" ";; noweb-2-yes-start
94 (message \"expanded2\")
95 (message \"expanded2\")" ";; noweb-tangle-start
98 (org-test-at-id "eb1f6498-5bd9-45e0-9c56-50717053e7b7"
99 (org-narrow-to-subtree)
101 (org-test-with-expanded-babel-code (org-element-parse-buffer))
103 (lambda (src) (org-trim (org-element-property :value src
))))))))
105 (ert-deftest ob-exp
/noweb-on-export-with-exports-results
()
106 "Noweb header arguments export correctly using :exports results.
107 - yes expand on both export and tangle
108 - no expand on neither export or tangle
109 - tangle expand on only tangle not export"
113 <<noweb-example1>>" "<<noweb-example1>>
115 (org-test-at-id "8701beb4-13d9-468c-997a-8e63e8b66f8d"
116 (org-narrow-to-subtree)
118 (org-test-with-expanded-babel-code (org-element-parse-buffer))
120 (lambda (src) (org-trim (org-element-property :value src
))))))))
122 (ert-deftest ob-exp
/exports-both
()
123 "Test the \":exports both\" header argument.
124 The code block evaluation should create both a code block and
126 (org-test-at-id "92518f2a-a46a-4205-a3ab-bcce1008a4bb"
127 (org-narrow-to-subtree)
128 (let ((tree (org-test-with-expanded-babel-code (org-element-parse-buffer))))
129 (should (and (org-element-map tree
'src-block
'identity
)
130 (org-element-map tree
'table
'identity
))))))
132 (ert-deftest ob-exp
/mixed-blocks-with-exports-both
()
135 '(property-drawer plain-list src-block fixed-width src-block plain-list
)
136 (org-test-at-id "5daa4d03-e3ea-46b7-b093-62c1b7632df3"
137 (org-narrow-to-subtree)
138 (mapcar 'org-element-type
140 (org-test-with-expanded-babel-code
141 (org-element-parse-buffer 'greater-element
))
142 'section
'org-element-contents nil t
))))))
144 (ert-deftest ob-exp
/export-with-name
()
148 (let ((org-babel-exp-code-template
149 "=%name=\n#+BEGIN_SRC %lang%flags\nbody\n#+END_SRC"))
150 (org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9"
151 (org-narrow-to-subtree)
152 (org-test-with-expanded-babel-code
153 (buffer-string)))))))
155 (ert-deftest ob-exp
/export-with-header-argument
()
156 (let ((org-babel-exp-code-template
159 |---------+----------|
161 | results | %results |
162 #+BEGIN_SRC %lang%flags\nbody\n#+END_SRC"))
163 (org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9"
164 (org-narrow-to-subtree)
165 (org-test-with-expanded-babel-code
166 (should (string-match "baz" (buffer-string)))
167 (should (string-match "replace" (buffer-string)))))))
169 (ert-deftest ob-exp
/noweb-no-export-and-exports-both
()
172 "<<noweb-no-export-and-exports-both-1>>"
173 (org-test-at-id "8a820f6c-7980-43db-8a24-0710d33729c9"
174 (org-narrow-to-subtree)
175 (org-test-with-expanded-babel-code
176 (org-element-map (org-element-parse-buffer) 'src-block
177 (lambda (src-block) (org-element-property :value src-block
))
180 (ert-deftest ob-exp
/evaluate-all-executables-in-order
()
183 (let (*evaluation-collector
*)
184 (org-test-at-id "96cc7073-97ec-4556-87cf-1f9bffafd317"
185 (org-narrow-to-subtree)
187 (fboundp 'org-export-execute-babel-code
)
188 (org-test-with-expanded-babel-code *evaluation-collector
*))))))
190 (ert-deftest ob-exp
/exports-inline
()
193 (regexp-quote "Here is one in the middle =1= of a line.
194 Here is one at the end of a line. =2=
195 =3= Here is one at the beginning of a line.")
196 (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
197 (org-narrow-to-subtree)
198 (org-test-with-expanded-babel-code (buffer-string))))))
200 (ert-deftest ob-exp
/export-call-line-information
()
201 (org-test-at-id "bec63a04-491e-4caa-97f5-108f3020365c"
202 (org-narrow-to-subtree)
203 (let ((org-babel-exp-call-line-template "\n: call: %line special-token"))
204 (org-test-with-expanded-babel-code
205 (should (string-match "double" (buffer-string)))
206 (should (string-match "16" (buffer-string)))
207 (should (string-match "special-token" (buffer-string)))))))
209 (ert-deftest ob-exp
/noweb-strip-export-ensure-strips
()
210 (org-test-at-id "8e7bd234-99b2-4b14-8cd6-53945e409775"
211 (org-narrow-to-subtree)
212 (org-babel-next-src-block 2)
213 (should (= 110 (org-babel-execute-src-block)))
214 (let ((result (org-test-with-expanded-babel-code (buffer-string))))
215 (should-not (string-match (regexp-quote "<<strip-export-1>>") result
))
216 (should-not (string-match (regexp-quote "i=\"10\"") result
)))))
218 (ert-deftest ob-exp
/use-case-of-reading-entry-properties
()
219 (org-test-at-id "cc5fbc20-bca5-437a-a7b8-2b4d7a03f820"
220 (org-narrow-to-subtree)
221 (let* ((case-fold-search nil
)
222 (result (org-test-with-expanded-babel-code (buffer-string)))
223 (sect "a:1, b:0, c:3, d:0, e:0")
224 (sub0 "a:1, b:2, c:4, d:0, e:0")
225 (sub1 "a:1, b:2, c:5, d:0, e:6")
228 (should (string-match (concat "_shell sect call\n: shell " sect
"\n")
230 (should (string-match (concat "_elisp sect call\n: elisp " sect
"\n")
232 (should (string-match (concat "\n- sect inline =shell " sect
"=\n")
234 (should (string-match (concat "\n- sect inline =elisp " sect
"=\n")
236 ;; entry "subsection", call without arguments
237 (should (string-match (concat "_shell sub0 call\n: shell " sub0
"\n")
239 (should (string-match (concat "_elisp sub0 call\n: elisp " sub0
"\n")
241 (should (string-match (concat "\n- sub0 inline =shell " sub0
"=\n")
243 (should (string-match (concat "\n- sub0 inline =elisp " sub0
"=\n")
245 ;; entry "subsection", call with arguments
246 (should (string-match (concat "_shell sub1 call\n: shell " sub1
"\n")
248 (should (string-match (concat "_elisp sub1 call\n: elisp " sub1
"\n")
250 (should (string-match (concat "\n- sub1 inline =shell " sub1
"=\n")
252 (should (string-match (concat "\n- sub1 inline =elisp " sub1
"=\n")
254 ;; entry "function definition"
255 (should (string-match (concat "_location_shell\n: shell " func
"\n")
257 (should (string-match (concat "_location_elisp\n: elisp " func
"\n")
260 (ert-deftest ob-exp
/export-from-a-temp-buffer
()
261 :expected-result
:failed
262 (org-test-with-temp-text
264 #+Title: exporting from a temporary buffer
267 #+BEGIN_SRC emacs-lisp
272 #+BEGIN_SRC emacs-lisp
276 #+BEGIN_SRC emacs-lisp :var foo=foo :noweb yes :exports results
280 (let* ((ascii (org-export-as 'ascii
)))
281 (should (string-match (regexp-quote (format nil
"%S" '(:foo
:bar
)))
284 (ert-deftest ob-export
/export-with-results-before-block
()
285 "Test export when results are inserted before source block."
292 #+BEGIN_SRC emacs-lisp
295 (org-test-with-temp-text
299 #+BEGIN_SRC emacs-lisp :exports both
302 (org-export-execute-babel-code)
305 (ert-deftest ob-export
/export-src-block-with-switches
()
306 "Test exporting a source block with switches."
309 "\\`#\\+BEGIN_SRC emacs-lisp -n -r$"
310 (org-test-with-temp-text
311 "#+BEGIN_SRC emacs-lisp -n -r\n\(+ 1 1)\n#+END_SRC"
312 (org-export-execute-babel-code)
315 (ert-deftest ob-export
/export-src-block-with-flags
()
316 "Test exporting a source block with a flag."
319 "\\`#\\+BEGIN_SRC emacs-lisp -some-flag$"
320 (org-test-with-temp-text
321 "#+BEGIN_SRC emacs-lisp :flags -some-flag\n\(+ 1 1)\n#+END_SRC"
322 (org-export-execute-babel-code)
325 (ert-deftest ob-export
/export-and-indentation
()
326 "Test indentation of evaluated source blocks during export."
331 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n t\n#+END_SRC"
332 (let ((indent-tabs-mode t
)
334 (org-src-preserve-indentation nil
))
335 (org-export-execute-babel-code)
337 ;; Preserve indentation with "-i" flag.
341 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n t\n#+END_SRC"
342 (let ((indent-tabs-mode t
)
344 (org-export-execute-babel-code)
346 ;; Preserve indentation with a non-nil
347 ;; `org-src-preserve-indentation'.
351 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n t\n#+END_SRC"
352 (let ((indent-tabs-mode t
)
354 (org-src-preserve-indentation t
))
355 (org-export-execute-babel-code)
358 (ert-deftest ob-export
/export-under-commented-headline
()
359 "Test evaluation of code blocks under COMMENT headings."
360 ;; Do not eval block in a commented headline.
364 (org-test-with-temp-text "* Headline
365 #+BEGIN_SRC emacs-lisp :exports results
368 (org-export-execute-babel-code)
373 (org-test-with-temp-text "* COMMENT Headline
374 #+BEGIN_SRC emacs-lisp :exports results
377 (org-export-execute-babel-code)
379 ;; Do not eval inline blocks either.
383 (org-test-with-temp-text "* Headline
384 src_emacs-lisp{(+ 1 1)}"
385 (org-export-execute-babel-code)
390 (org-test-with-temp-text "* COMMENT Headline
391 src_emacs-lisp{(+ 1 1)}"
392 (org-export-execute-babel-code)
394 ;; Also check parent headlines.
398 (org-test-with-temp-text "
401 #+BEGIN_SRC emacs-lisp :exports results
404 (org-export-execute-babel-code)
407 (ert-deftest ob-export
/reference-in-post-header
()
408 "Test references in :post header during export."
410 (org-test-with-temp-text "
412 #+BEGIN_SRC emacs-lisp :exports none :var bar=\"baz\"
417 #+BEGIN_SRC emacs-lisp :exports results :post foo(\"nofun\")
419 (org-export-execute-babel-code) t
)))
422 (provide 'test-ob-exp
)
424 ;;; test-ob-exp.el ends here