3 ;; Copyright (c) 2010-2015 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 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 (org-with-wide-buffer (buffer-string)))
31 (narrowing (list (point-min) (point-max)))
32 (org-export-babel-evaluate t
))
36 (apply #'narrow-to-region narrowing
)
37 (org-babel-exp-process-buffer)
38 (goto-char (point-min))
41 (ert-deftest test-ob-exp
/org-babel-exp-src-blocks
/w-no-headers
()
42 "Testing export without any headlines in the Org mode file."
44 (let ((html-file (concat (file-name-sans-extension org-test-no-heading-file
)
46 (when (file-exists-p html-file
) (delete-file html-file
))
47 (org-test-in-example-file org-test-no-heading-file
48 ;; Export the file to HTML.
49 (org-export-to-file 'html html-file
))
50 ;; should create a .html file
51 (should (file-exists-p html-file
))
52 ;; should not create a file with "::" appended to its name
53 (should-not (file-exists-p (concat org-test-no-heading-file
"::")))
54 (when (file-exists-p html-file
) (delete-file html-file
))))
56 (ert-deftest test-ob-exp
/org-babel-exp-src-blocks
/w-no-file
()
57 "Testing export from buffers which are not visiting any file."
59 (let ((name (generate-new-buffer-name "*Org HTML Export*")))
60 (org-test-in-example-file nil
61 (org-export-to-buffer 'html name nil nil nil t
))
62 ;; Should create a HTML buffer.
63 (should (buffer-live-p (get-buffer name
)))
64 ;; Should contain the content of the buffer.
65 (with-current-buffer (get-buffer name
)
66 (should (string-match (regexp-quote org-test-file-ob-anchor
)
68 (when (get-buffer name
) (kill-buffer name
))))
70 (ert-deftest test-ob-exp
/org-babel-exp-src-blocks
/w-no-headers2
()
71 "Testing export without any headlines in the Org file."
72 (let ((html-file (concat (file-name-sans-extension
73 org-test-link-in-heading-file
)
75 (when (file-exists-p html-file
) (delete-file html-file
))
76 (org-test-in-example-file org-test-link-in-heading-file
77 ;; export the file to html
78 (org-export-to-file 'html html-file
))
79 ;; should create a .html file
80 (should (file-exists-p html-file
))
81 ;; should not create a file with "::" appended to its name
82 (should-not (file-exists-p (concat org-test-link-in-heading-file
"::")))
83 (when (file-exists-p html-file
) (delete-file html-file
))))
85 (ert-deftest ob-exp
/noweb-on-export
()
86 "Noweb header arguments export correctly.
87 - yes expand on both export and tangle
88 - no expand on neither export or tangle
89 - tangle expand on only tangle not export"
92 '("(message \"expanded1\")" "(message \"expanded2\")" ";; noweb-1-yes-start
93 (message \"expanded1\")
94 (message \"expanded1\")" ";; noweb-no-start
95 <<noweb-example1>>" ";; noweb-2-yes-start
96 (message \"expanded2\")
97 (message \"expanded2\")"
98 ";; noweb-tangle-start
101 (org-test-at-id "eb1f6498-5bd9-45e0-9c56-50717053e7b7"
102 (org-narrow-to-subtree)
104 (org-test-with-expanded-babel-code (org-element-parse-buffer))
106 (lambda (src) (org-trim (org-element-property :value src
))))))))
108 (ert-deftest ob-exp
/noweb-on-export-with-exports-results
()
109 "Noweb header arguments export correctly using :exports results.
110 - yes expand on both export and tangle
111 - no expand on neither export or tangle
112 - tangle expand on only tangle not export"
116 <<noweb-example1>>" "<<noweb-example1>>
118 (org-test-at-id "8701beb4-13d9-468c-997a-8e63e8b66f8d"
119 (org-narrow-to-subtree)
121 (org-test-with-expanded-babel-code (org-element-parse-buffer))
123 (lambda (src) (org-trim (org-element-property :value src
))))))))
125 (ert-deftest ob-exp
/exports-both
()
126 "Test the \":exports both\" header argument.
127 The code block evaluation should create both a code block and
129 (org-test-at-id "92518f2a-a46a-4205-a3ab-bcce1008a4bb"
130 (org-narrow-to-subtree)
131 (let ((tree (org-test-with-expanded-babel-code (org-element-parse-buffer))))
132 (should (and (org-element-map tree
'src-block
'identity
)
133 (org-element-map tree
'table
'identity
))))))
135 (ert-deftest ob-exp
/mixed-blocks-with-exports-both
()
138 '(property-drawer plain-list src-block fixed-width src-block plain-list
)
139 (org-test-at-id "5daa4d03-e3ea-46b7-b093-62c1b7632df3"
140 (org-narrow-to-subtree)
141 (mapcar 'org-element-type
143 (org-test-with-expanded-babel-code
144 (org-element-parse-buffer 'greater-element
))
145 'section
'org-element-contents nil t
))))))
147 (ert-deftest ob-exp
/export-with-name
()
151 (let ((org-babel-exp-code-template
152 "=%name=\n#+BEGIN_SRC %lang%flags\nbody\n#+END_SRC"))
153 (org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9"
154 (org-narrow-to-subtree)
155 (org-test-with-expanded-babel-code
156 (buffer-string)))))))
158 (ert-deftest ob-exp
/export-with-header-argument
()
159 (let ((org-babel-exp-code-template
162 |---------+----------|
164 | results | %results |
165 #+BEGIN_SRC %lang%flags\nbody\n#+END_SRC"))
166 (org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9"
167 (org-narrow-to-subtree)
168 (org-test-with-expanded-babel-code
169 (should (string-match "baz" (buffer-string)))
170 (should (string-match "replace" (buffer-string)))))))
172 (ert-deftest ob-exp
/noweb-no-export-and-exports-both
()
175 "<<noweb-no-export-and-exports-both-1>>"
176 (org-test-at-id "8a820f6c-7980-43db-8a24-0710d33729c9"
177 (org-narrow-to-subtree)
178 (org-test-with-expanded-babel-code
179 (org-element-map (org-element-parse-buffer) 'src-block
180 (lambda (src-block) (org-element-property :value src-block
))
183 (ert-deftest ob-exp
/evaluate-all-executables-in-order
()
186 (let ((org-export-babel-evaluate t
) *evaluation-collector
*)
187 (org-test-at-id "96cc7073-97ec-4556-87cf-1f9bffafd317"
188 (org-narrow-to-subtree)
190 (org-test-with-expanded-babel-code *evaluation-collector
*))))))
192 (ert-deftest ob-exp
/exports-inline
()
195 (regexp-quote "Here is one in the middle {{{results(=1=)}}} of a line.
196 Here is one at the end of a line. {{{results(=2=)}}}
197 {{{results(=3=)}}} Here is one at the beginning of a line.")
198 (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
199 (org-narrow-to-subtree)
200 (let ((org-babel-inline-result-wrap "=%s="))
201 (org-test-with-expanded-babel-code (buffer-string)))))))
203 (ert-deftest ob-exp
/exports-inline-code
()
204 (let ((org-babel-inline-result-wrap "=%s=")
205 (org-export-babel-evaluate t
))
207 (string-match "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)}$"
208 (org-test-with-temp-text
209 "src_emacs-lisp[:exports code]{(+ 1 1)}"
210 (org-babel-exp-process-buffer)
213 (string-match "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)}$"
214 (org-test-with-temp-text
215 "src_emacs-lisp[ :exports code ]{(+ 1 1)}"
216 (org-babel-exp-process-buffer)
219 (string-match "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)} {{{results(=2=)}}}$"
220 (org-test-with-temp-text
221 "src_emacs-lisp[:exports both]{(+ 1 1)}"
222 (org-babel-exp-process-buffer)
225 (string-match "\\`{{{results(=2=)}}}$"
226 (org-test-with-temp-text
227 "src_emacs-lisp[:exports results :results scalar]{(+ 1 1)}"
228 (org-babel-exp-process-buffer)
231 (let ((text "foosrc_emacs-lisp[:exports code]{(+ 1 1)}"))
232 (string-match (regexp-quote text
)
233 (org-test-with-temp-text
235 (org-babel-exp-process-buffer)
238 (let ((text "src_emacs lisp{(+ 1 1)}"))
239 (string-match (regexp-quote text
)
240 (org-test-with-temp-text
242 (org-babel-exp-process-buffer)
246 (replace-regexp-in-string
247 "\\\\\\[]{" "\\(?:\\[]\\)?{" ;accept both src_sh[]{...} or src_sh{...}
248 (regexp-quote "Here is one in the middle src_sh[]{echo 1} of a line.
249 Here is one at the end of a line. src_sh[]{echo 2}
250 src_sh[]{echo 3} Here is one at the beginning of a line.
251 Here is one that is also evaluated: src_sh[]{echo 4} {{{results(=4=)}}}")
253 (org-test-at-id "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076"
254 (org-narrow-to-subtree)
255 (org-test-with-expanded-babel-code (buffer-string)))))))
257 (ert-deftest ob-exp
/exports-inline-code-double-eval
()
258 "Based on default header arguments for inline code blocks (:exports
259 results), the resulting code block `src_emacs-lisp{2}' should also be
261 (let ((org-babel-inline-result-wrap "=%s=")
262 (org-export-babel-evaluate t
))
264 (string-match "\\`{{{results(src_emacs-lisp\\[\\]{2})}}}$"
265 (org-test-with-temp-text
266 "src_emacs-lisp[:exports results :results code]{(+ 1 1)}"
267 (org-babel-exp-process-buffer)
270 (ert-deftest ob-exp
/exports-inline-code-eval-code-once
()
271 "Ibid above, except that the resulting inline code block should not
273 (let ((org-export-babel-evaluate t
))
275 (string-match "{{{results(src_emacs-lisp\\(?:\\[[: a-zA-Z]+]\\)?{2})}}}$"
276 (org-test-with-temp-text
277 (concat "src_emacs-lisp[:exports results :results code "
278 ":results_switches \":exports code\"]{(+ 1 1)}")
279 (org-babel-exp-process-buffer)
282 (ert-deftest ob-exp
/exports-inline-code-double-eval-exports-both
()
283 (let ((org-export-babel-evaluate t
))
285 (string-match (concat "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)} "
286 "{{{results(src_emacs-lisp\\[ :exports code\\]{2})}}}$")
287 (org-test-with-temp-text
288 (concat "src_emacs-lisp[:exports both :results code "
289 ":results_switches \":exports code\"]{(+ 1 1)}")
290 (org-babel-exp-process-buffer)
293 (ert-deftest ob-exp
/export-call-line-information
()
294 (org-test-at-id "bec63a04-491e-4caa-97f5-108f3020365c"
295 (org-narrow-to-subtree)
296 (let ((org-babel-exp-call-line-template "\n: call: %line special-token"))
297 (org-test-with-expanded-babel-code
298 (should (string-match "double" (buffer-string)))
299 (should (string-match "16" (buffer-string)))
300 (should (string-match "special-token" (buffer-string)))))))
302 (ert-deftest ob-exp
/noweb-strip-export-ensure-strips
()
303 (org-test-at-id "8e7bd234-99b2-4b14-8cd6-53945e409775"
304 (org-narrow-to-subtree)
305 (org-babel-next-src-block 2)
306 (should (= 110 (org-babel-execute-src-block)))
307 (let ((result (org-test-with-expanded-babel-code (buffer-string))))
308 (should-not (string-match (regexp-quote "<<strip-export-1>>") result
))
309 (should-not (string-match (regexp-quote "i=\"10\"") result
)))))
311 (ert-deftest ob-exp
/use-case-of-reading-entry-properties
()
312 (org-test-at-id "cc5fbc20-bca5-437a-a7b8-2b4d7a03f820"
313 (org-narrow-to-subtree)
314 (let* ((case-fold-search nil
)
315 (result (org-test-with-expanded-babel-code (buffer-string)))
316 (sect "a:1, b:0, c:3, d:0, e:0")
317 (sub0 "a:1, b:2, c:4, d:0, e:0")
318 (sub1 "a:1, b:2, c:5, d:0, e:6")
321 (should (string-match (concat "_shell-sect-call\n: shell " sect
"\n")
323 (should (string-match (concat "_elisp-sect-call\n: elisp " sect
"\n")
325 (should (string-match (concat "\n- sect inline shell " sect
"\n")
327 (should (string-match (concat "\n- sect inline elisp " sect
"\n")
329 ;; entry "subsection", call without arguments
330 (should (string-match (concat "_shell-sub0-call\n: shell " sub0
"\n")
332 (should (string-match (concat "_elisp-sub0-call\n: elisp " sub0
"\n")
334 (should (string-match (concat "\n- sub0 inline shell " sub0
"\n")
336 (should (string-match (concat "\n- sub0 inline elisp " sub0
"\n")
338 ;; entry "subsection", call with arguments
339 (should (string-match (concat "_shell-sub1-call\n: shell " sub1
"\n")
341 (should (string-match (concat "_elisp-sub1-call\n: elisp " sub1
"\n")
343 (should (string-match (concat "\n- sub1 inline shell " sub1
"\n")
345 (should (string-match (concat "\n- sub1 inline elisp " sub1
"\n")
347 ;; entry "function definition"
348 (should (string-match (concat "_location_shell\n: shell " func
"\n")
350 (should (string-match (concat "_location_elisp\n: elisp " func
"\n")
353 (ert-deftest ob-exp
/export-from-a-temp-buffer
()
354 (let ((org-export-babel-evaluate t
))
355 (org-test-with-temp-text
357 #+Title: exporting from a temporary buffer
360 #+BEGIN_SRC emacs-lisp
365 #+BEGIN_SRC emacs-lisp
369 #+BEGIN_SRC emacs-lisp :var foo=foo :noweb yes :exports results
373 (let* ((ascii (org-export-as 'ascii
)))
374 (should (string-match
375 (regexp-quote " :foo :bar \n")
378 (ert-deftest ob-export
/export-with-results-before-block
()
379 "Test export when results are inserted before source block."
380 (let ((org-export-babel-evaluate t
))
387 #+BEGIN_SRC emacs-lisp
390 (org-test-with-temp-text
394 #+BEGIN_SRC emacs-lisp :exports both
397 (org-babel-exp-process-buffer)
398 (org-trim (org-no-properties (buffer-string))))))))
400 (ert-deftest ob-export
/export-src-block-with-switches
()
401 "Test exporting a source block with switches."
404 "\\`#\\+BEGIN_SRC emacs-lisp -n -r$"
405 (org-test-with-temp-text
406 "#+BEGIN_SRC emacs-lisp -n -r\n\(+ 1 1)\n#+END_SRC"
407 (org-babel-exp-process-buffer)
410 (ert-deftest ob-export
/export-src-block-with-flags
()
411 "Test exporting a source block with a flag."
414 "\\`#\\+BEGIN_SRC emacs-lisp -some-flag$"
415 (org-test-with-temp-text
416 "#+BEGIN_SRC emacs-lisp :flags -some-flag\n\(+ 1 1)\n#+END_SRC"
417 (org-babel-exp-process-buffer)
420 (ert-deftest ob-export
/export-and-indentation
()
421 "Test indentation of evaluated source blocks during export."
426 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n t\n#+END_SRC"
427 (let ((indent-tabs-mode t
)
429 (org-src-preserve-indentation nil
))
430 (org-babel-exp-process-buffer)
432 ;; Preserve indentation with "-i" flag.
436 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n t\n#+END_SRC"
437 (let ((indent-tabs-mode t
)
439 (org-babel-exp-process-buffer)
441 ;; Preserve indentation with a non-nil
442 ;; `org-src-preserve-indentation'.
446 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n t\n#+END_SRC"
447 (let ((indent-tabs-mode t
)
449 (org-src-preserve-indentation t
))
450 (org-babel-exp-process-buffer)
453 (ert-deftest ob-export
/export-under-commented-headline
()
454 "Test evaluation of code blocks under COMMENT headings."
455 (let ((org-export-babel-evaluate t
)
456 (org-babel-inline-result-wrap "=%s="))
457 ;; Do not eval block in a commented headline.
461 (org-test-with-temp-text "* Headline
462 #+BEGIN_SRC emacs-lisp :exports results
465 (org-babel-exp-process-buffer)
470 (org-test-with-temp-text "* COMMENT Headline
471 #+BEGIN_SRC emacs-lisp :exports results
474 (org-babel-exp-process-buffer)
476 ;; Do not eval inline blocks either.
480 (org-test-with-temp-text "* Headline
481 src_emacs-lisp{(+ 1 1)}"
482 (org-babel-exp-process-buffer)
487 (org-test-with-temp-text "* COMMENT Headline
488 src_emacs-lisp{(+ 1 1)}"
489 (org-babel-exp-process-buffer)
491 ;; Also check parent headlines.
495 (org-test-with-temp-text "
498 #+BEGIN_SRC emacs-lisp :exports results
501 (org-babel-exp-process-buffer)
504 (ert-deftest ob-export
/reference-in-post-header
()
505 "Test references in :post header during export."
507 (org-test-with-temp-text "
509 #+BEGIN_SRC emacs-lisp :exports none :var bar=\"baz\"
514 #+BEGIN_SRC emacs-lisp :exports results :post foo(\"nofun\")
516 (org-babel-exp-process-buffer) t
)))
518 (ert-deftest ob-export
/babel-evaluate
()
519 "Test `org-export-babel-evaluate' effect."
520 ;; When nil, no Babel code is executed.
524 (org-test-with-temp-text
525 "#+BEGIN_SRC emacs-lisp :exports results\n(+ 1 1)\n#+END_SRC"
526 (let ((org-export-babel-evaluate nil
)) (org-babel-exp-process-buffer))
531 (org-test-with-temp-text
532 "src_emacs-lisp{(+ 1 1)}"
533 (let ((org-export-babel-evaluate nil
)) (org-babel-exp-process-buffer))
535 ;; When non-nil, all Babel code types are executed.
539 (org-test-with-temp-text
540 "#+BEGIN_SRC emacs-lisp :exports results\n(+ 1 1)\n#+END_SRC"
541 (let ((org-export-babel-evaluate t
)) (org-babel-exp-process-buffer))
546 (org-test-with-temp-text
547 "src_emacs-lisp{(+ 1 1)}"
548 (let ((org-export-babel-evaluate t
)) (org-babel-exp-process-buffer))
550 ;; When set to `inline-only' limit evaluation to inline code.
554 (org-test-with-temp-text
555 "#+BEGIN_SRC emacs-lisp :exports results\n(+ 1 1)\n#+END_SRC"
556 (let ((org-export-babel-evaluate 'inline-only
))
557 (org-babel-exp-process-buffer))
562 (org-test-with-temp-text
563 "src_emacs-lisp{(+ 1 1)}"
564 (let ((org-export-babel-evaluate 'inline-only
))
565 (org-babel-exp-process-buffer))
568 (ert-deftest ob-export
/body-with-coderef
()
569 "Test exporting a code block with coderefs."
571 (equal "#+BEGIN_SRC emacs-lisp\n0 (ref:foo)\n#+END_SRC"
572 (org-test-with-temp-text
573 "#+BEGIN_SRC emacs-lisp :exports code\n0 (ref:foo)\n#+END_SRC"
574 (let ((org-export-babel-evaluate t
)
575 (org-coderef-label-format "(ref:foo)"))
576 (org-babel-exp-process-buffer))
580 "#+BEGIN_SRC emacs-lisp -l \"r:%s\"\n1 r:foo\n#+END_SRC"
581 (org-test-with-temp-text
582 "#+BEGIN_SRC emacs-lisp -l \"r:%s\" -lisp :exports code\n1 r:foo\n#+END_SRC"
583 (let ((org-export-babel-evaluate t
))
584 (org-babel-exp-process-buffer))
587 (ert-deftest ob-exp
/src-block-with-affiliated-keyword
()
588 "Test exporting a code block with affiliated keywords."
589 ;; Pathological case: affiliated keyword matches inline src block
592 (equal "#+name: call_foo\n#+BEGIN_SRC emacs-lisp\n42\n#+END_SRC"
593 (org-test-with-temp-text
594 "#+name: call_foo\n#+BEGIN_SRC emacs-lisp\n42\n#+END_SRC"
595 (let ((org-export-use-babel t
))
596 (org-babel-exp-process-buffer))
600 (provide 'test-ob-exp
)
602 ;;; test-ob-exp.el ends here