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-use-babel 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 ((text-file (concat (file-name-sans-extension org-test-no-heading-file
)
46 (when (file-exists-p text-file
) (delete-file text-file
))
47 (org-test-in-example-file org-test-no-heading-file
48 ;; Export the file to HTML.
49 (org-export-to-file 'ascii text-file
))
50 ;; should create a ".txt" file
51 (should (file-exists-p text-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 text-file
) (delete-file text-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 ASCII Export*")))
60 (org-test-in-example-file nil
61 (org-export-to-buffer 'ascii name nil nil nil t
))
62 ;; Should create a new 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\")" ";; noweb-no-start
94 <<noweb-example1>>" ";; noweb-2-yes-start
95 (message \"expanded2\")"
96 ";; 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 ((org-export-use-babel t
) *evaluation-collector
*)
185 (org-test-at-id "96cc7073-97ec-4556-87cf-1f9bffafd317"
186 (org-narrow-to-subtree)
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 {{{results(=1=)}}} of a line.
194 Here is one at the end of a line. {{{results(=2=)}}}
195 {{{results(=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 (let ((org-babel-inline-result-wrap "=%s="))
199 (org-test-with-expanded-babel-code (buffer-string)))))))
201 (ert-deftest ob-exp
/exports-inline-code
()
203 (equal "src_emacs-lisp[]{(+ 1 1)}"
204 (org-test-with-temp-text "src_emacs-lisp[:exports code]{(+ 1 1)}"
205 (let ((org-babel-inline-result-wrap "=%s=")
206 (org-export-use-babel t
))
207 (org-babel-exp-process-buffer))
210 (equal "src_emacs-lisp[]{(+ 1 1)}"
211 (org-test-with-temp-text "src_emacs-lisp[ :exports code ]{(+ 1 1)}"
212 (let ((org-babel-inline-result-wrap "=%s=")
213 (org-export-use-babel t
))
214 (org-babel-exp-process-buffer))
217 (equal "src_emacs-lisp[]{(+ 1 1)} {{{results(=2=)}}}"
218 (org-test-with-temp-text "src_emacs-lisp[:exports both]{(+ 1 1)}"
219 (let ((org-babel-inline-result-wrap "=%s=")
220 (org-export-use-babel t
))
221 (org-babel-exp-process-buffer))
224 (equal "{{{results(=2=)}}}"
225 (org-test-with-temp-text
226 "src_emacs-lisp[:exports results :results scalar]{(+ 1 1)}"
227 (let ((org-babel-inline-result-wrap "=%s=")
228 (org-export-use-babel t
))
229 (org-babel-exp-process-buffer))
232 (equal "foosrc_emacs-lisp[:exports code]{(+ 1 1)}"
233 (org-test-with-temp-text
234 "foosrc_emacs-lisp[:exports code]{(+ 1 1)}"
235 (let ((org-babel-inline-result-wrap "=%s=")
236 (org-export-use-babel t
))
237 (org-babel-exp-process-buffer))
240 (let ((text "src_emacs lisp{(+ 1 1)}"))
241 (string-match (regexp-quote text
)
242 (org-test-with-temp-text
244 (let ((org-babel-inline-result-wrap "=%s=")
245 (org-export-use-babel t
))
246 (org-babel-exp-process-buffer))
250 (replace-regexp-in-string
251 "\\\\\\[]{" "\\(?:\\[]\\)?{" ;accept both src_sh[]{...} or src_sh{...}
252 (regexp-quote "Here is one in the middle src_sh[]{echo 1} of a line.
253 Here is one at the end of a line. src_sh[]{echo 2}
254 src_sh[]{echo 3} Here is one at the beginning of a line.
255 Here is one that is also evaluated: src_sh[]{echo 4} {{{results(=4=)}}}")
257 (org-test-at-id "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076"
258 (org-narrow-to-subtree)
259 (let ((org-babel-inline-result-wrap "=%s=")
260 (org-export-use-babel t
))
261 (org-test-with-expanded-babel-code (buffer-string)))))))
263 (ert-deftest ob-exp
/exports-inline-code-double-eval
()
264 "Based on default header arguments for inline code blocks (:exports
265 results), the resulting code block `src_emacs-lisp{2}' should also be
267 (let ((org-babel-inline-result-wrap "=%s=")
268 (org-export-use-babel t
))
270 (string-match "\\`{{{results(src_emacs-lisp\\[\\]{2})}}}$"
271 (org-test-with-temp-text
272 "src_emacs-lisp[:exports results :results code]{(+ 1 1)}"
273 (org-babel-exp-process-buffer)
276 (ert-deftest ob-exp
/exports-inline-code-eval-code-once
()
277 "Ibid above, except that the resulting inline code block should not
279 (let ((org-export-use-babel t
))
281 (string-match "{{{results(src_emacs-lisp\\(?:\\[[: a-zA-Z]+]\\)?{2})}}}$"
282 (org-test-with-temp-text
283 (concat "src_emacs-lisp[:exports results :results code "
284 ":results_switches \":exports code\"]{(+ 1 1)}")
285 (org-babel-exp-process-buffer)
288 (ert-deftest ob-exp
/exports-inline-code-double-eval-exports-both
()
289 (let ((org-export-use-babel t
))
291 (string-match (concat "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)} "
292 "{{{results(src_emacs-lisp\\[ :exports code\\]{2})}}}$")
293 (org-test-with-temp-text
294 (concat "src_emacs-lisp[:exports both :results code "
295 ":results_switches \":exports code\"]{(+ 1 1)}")
296 (org-babel-exp-process-buffer)
299 (ert-deftest ob-exp
/export-call-line-information
()
300 (org-test-at-id "bec63a04-491e-4caa-97f5-108f3020365c"
301 (org-narrow-to-subtree)
302 (let ((org-babel-exp-call-line-template "\n: call: %line special-token"))
303 (org-test-with-expanded-babel-code
304 (should (string-match "double" (buffer-string)))
305 (should (string-match "16" (buffer-string)))
306 (should (string-match "special-token" (buffer-string)))))))
308 (ert-deftest ob-exp
/noweb-strip-export-ensure-strips
()
309 (org-test-at-id "8e7bd234-99b2-4b14-8cd6-53945e409775"
310 (org-narrow-to-subtree)
311 (org-babel-next-src-block 2)
312 (should (= 110 (org-babel-execute-src-block)))
313 (let ((result (org-test-with-expanded-babel-code (buffer-string))))
314 (should-not (string-match (regexp-quote "<<strip-export-1>>") result
))
315 (should-not (string-match (regexp-quote "i=\"10\"") result
)))))
317 (ert-deftest ob-exp
/use-case-of-reading-entry-properties
()
318 (org-test-at-id "cc5fbc20-bca5-437a-a7b8-2b4d7a03f820"
319 (org-narrow-to-subtree)
320 (let* ((case-fold-search nil
)
321 (result (org-test-with-expanded-babel-code (buffer-string)))
322 (sect "a:1, b:0, c:3, d:0, e:0")
323 (sub0 "a:1, b:2, c:4, d:0, e:0")
324 (sub1 "a:1, b:2, c:5, d:0, e:6")
327 (should (string-match (concat "_shell-sect-call\n: shell " sect
"\n")
329 (should (string-match (concat "_elisp-sect-call\n: elisp " sect
"\n")
331 (should (string-match (concat "\n- sect inline shell " sect
"\n")
333 (should (string-match (concat "\n- sect inline elisp " sect
"\n")
335 ;; entry "subsection", call without arguments
336 (should (string-match (concat "_shell-sub0-call\n: shell " sub0
"\n")
338 (should (string-match (concat "_elisp-sub0-call\n: elisp " sub0
"\n")
340 (should (string-match (concat "\n- sub0 inline shell " sub0
"\n")
342 (should (string-match (concat "\n- sub0 inline elisp " sub0
"\n")
344 ;; entry "subsection", call with arguments
345 (should (string-match (concat "_shell-sub1-call\n: shell " sub1
"\n")
347 (should (string-match (concat "_elisp-sub1-call\n: elisp " sub1
"\n")
349 (should (string-match (concat "\n- sub1 inline shell " sub1
"\n")
351 (should (string-match (concat "\n- sub1 inline elisp " sub1
"\n")
353 ;; entry "function definition"
354 (should (string-match (concat "_location_shell\n: shell " func
"\n")
356 (should (string-match (concat "_location_elisp\n: elisp " func
"\n")
359 (ert-deftest ob-exp
/export-from-a-temp-buffer
()
360 (let ((org-export-use-babel t
))
361 (org-test-with-temp-text
363 #+Title: exporting from a temporary buffer
366 #+BEGIN_SRC emacs-lisp
371 #+BEGIN_SRC emacs-lisp
375 #+BEGIN_SRC emacs-lisp :var foo=foo :noweb yes :exports results
379 (let* ((ascii (org-export-as 'ascii
)))
380 (should (string-match
381 (regexp-quote " :foo :bar \n")
384 (ert-deftest ob-export
/export-with-results-before-block
()
385 "Test export when results are inserted before source block."
386 (let ((org-export-use-babel t
))
393 #+BEGIN_SRC emacs-lisp
396 (org-test-with-temp-text
400 #+BEGIN_SRC emacs-lisp :exports both
403 (org-babel-exp-process-buffer)
404 (org-trim (org-no-properties (buffer-string))))))))
406 (ert-deftest ob-export
/export-src-block-with-switches
()
407 "Test exporting a source block with switches."
410 "\\`#\\+BEGIN_SRC emacs-lisp -n -r$"
411 (org-test-with-temp-text
412 "#+BEGIN_SRC emacs-lisp -n -r\n\(+ 1 1)\n#+END_SRC"
413 (org-babel-exp-process-buffer)
416 (ert-deftest ob-export
/export-src-block-with-flags
()
417 "Test exporting a source block with a flag."
420 "\\`#\\+BEGIN_SRC emacs-lisp -some-flag$"
421 (org-test-with-temp-text
422 "#+BEGIN_SRC emacs-lisp :flags -some-flag\n\(+ 1 1)\n#+END_SRC"
423 (org-babel-exp-process-buffer)
426 (ert-deftest ob-export
/export-and-indentation
()
427 "Test indentation of evaluated source blocks during export."
432 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n t\n#+END_SRC"
433 (let ((indent-tabs-mode t
)
435 (org-src-preserve-indentation nil
))
436 (org-babel-exp-process-buffer)
438 ;; Preserve indentation with "-i" flag.
442 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n t\n#+END_SRC"
443 (let ((indent-tabs-mode t
)
445 (org-babel-exp-process-buffer)
447 ;; Preserve indentation with a non-nil
448 ;; `org-src-preserve-indentation'.
452 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n t\n#+END_SRC"
453 (let ((indent-tabs-mode t
)
455 (org-src-preserve-indentation t
))
456 (org-babel-exp-process-buffer)
459 (ert-deftest ob-export
/export-under-commented-headline
()
460 "Test evaluation of code blocks under COMMENT headings."
461 (let ((org-export-use-babel t
)
462 (org-babel-inline-result-wrap "=%s="))
463 ;; Do not eval block in a commented headline.
467 (org-test-with-temp-text "* Headline
468 #+BEGIN_SRC emacs-lisp :exports results
471 (org-babel-exp-process-buffer)
476 (org-test-with-temp-text "* COMMENT Headline
477 #+BEGIN_SRC emacs-lisp :exports results
480 (org-babel-exp-process-buffer)
482 ;; Do not eval inline blocks either.
486 (org-test-with-temp-text "* Headline
487 src_emacs-lisp{(+ 1 1)}"
488 (org-babel-exp-process-buffer)
493 (org-test-with-temp-text "* COMMENT Headline
494 src_emacs-lisp{(+ 1 1)}"
495 (org-babel-exp-process-buffer)
497 ;; Also check parent headlines.
501 (org-test-with-temp-text "
504 #+BEGIN_SRC emacs-lisp :exports results
507 (org-babel-exp-process-buffer)
510 (ert-deftest ob-export
/reference-in-post-header
()
511 "Test references in :post header during export."
513 (org-test-with-temp-text "
515 #+BEGIN_SRC emacs-lisp :exports none :var bar=\"baz\"
520 #+BEGIN_SRC emacs-lisp :exports results :post foo(\"nofun\")
522 (org-babel-exp-process-buffer) t
)))
524 (ert-deftest ob-export
/babel-evaluate
()
525 "Test `org-export-use-babel' effect."
526 ;; When nil, no Babel code is executed.
530 (org-test-with-temp-text
531 "#+BEGIN_SRC emacs-lisp :exports results\n(+ 1 1)\n#+END_SRC"
532 (let ((org-export-use-babel nil
)) (org-babel-exp-process-buffer))
537 (org-test-with-temp-text
538 "src_emacs-lisp{(+ 1 1)}"
539 (let ((org-export-use-babel nil
)) (org-babel-exp-process-buffer))
541 ;; When non-nil, all Babel code types are executed.
545 (org-test-with-temp-text
546 "#+BEGIN_SRC emacs-lisp :exports results\n(+ 1 1)\n#+END_SRC"
547 (let ((org-export-use-babel t
)) (org-babel-exp-process-buffer))
552 (org-test-with-temp-text
553 "src_emacs-lisp{(+ 1 1)}"
554 (let ((org-export-use-babel t
)) (org-babel-exp-process-buffer))
557 (ert-deftest ob-export
/body-with-coderef
()
558 "Test exporting a code block with coderefs."
560 (equal "#+BEGIN_SRC emacs-lisp\n0 (ref:foo)\n#+END_SRC"
561 (org-test-with-temp-text
562 "#+BEGIN_SRC emacs-lisp :exports code\n0 (ref:foo)\n#+END_SRC"
563 (let ((org-export-use-babel t
)
564 (org-coderef-label-format "(ref:foo)"))
565 (org-babel-exp-process-buffer))
569 "#+BEGIN_SRC emacs-lisp -l \"r:%s\"\n1 r:foo\n#+END_SRC"
570 (org-test-with-temp-text
571 "#+BEGIN_SRC emacs-lisp -l \"r:%s\" -lisp :exports code\n1 r:foo\n#+END_SRC"
572 (let ((org-export-use-babel t
))
573 (org-babel-exp-process-buffer))
576 (ert-deftest ob-exp
/src-block-with-affiliated-keyword
()
577 "Test exporting a code block with affiliated keywords."
578 ;; Pathological case: affiliated keyword matches inline src block
581 (equal "#+name: call_foo\n#+BEGIN_SRC emacs-lisp\n42\n#+END_SRC"
582 (org-test-with-temp-text
583 "#+name: call_foo\n#+BEGIN_SRC emacs-lisp\n42\n#+END_SRC"
584 (let ((org-export-use-babel t
))
585 (org-babel-exp-process-buffer))
589 (provide 'test-ob-exp
)
591 ;;; test-ob-exp.el ends here