1 ;;; test-ob.el --- tests for ob.el
3 ;; Copyright (c) 2010, 2011 Eric Schulte
4 ;; Authors: Eric Schulte, Martyn Jago
6 ;; Released under the GNU General Public License version 3
7 ;; see: http://www.gnu.org/licenses/gpl-3.0.html
9 (let ((load-path (cons (expand-file-name
10 ".." (file-name-directory
11 (or load-file-name buffer-file-name
)))
14 (require 'org-test-ob-consts
))
16 (ert-deftest test-org-babel
/multi-line-header-regexp
()
17 (should(equal "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
18 org-babel-multi-line-header-regexp
))
19 ;;TODO can be optimised - and what about blah4 blah5 blah6?
21 org-babel-multi-line-header-regexp
22 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
25 "blah1 blah2 blah3 \t"
28 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
30 ;;TODO Check - should this fail?
32 (not (org-test-string-exact-match
33 org-babel-multi-line-header-regexp
34 " \t #+headers : blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))))
36 (ert-deftest test-org-babel
/src-block-regexp
()
39 "#+begin_src language -n-r-a-b -c :argument-1 yes :argument-2 no\n"
40 "echo this is a test\n"
41 "echo Currently in ' $PWD\n"
44 (flags "-n-r-a-b -c ")
45 (arguments ":argument-1 yes :argument-2 no")
46 (body "echo this is a test\necho Currently in ' $PWD\n"))
47 (should (string-match org-babel-src-block-regexp test-block
))
48 (should (string-match org-babel-src-block-regexp
(upcase test-block
)))
49 (should (equal language
(match-string 2 test-block
)))
50 ;;TODO Consider refactoring
51 (should (equal flags
(match-string 3 test-block
)))
52 (should (equal arguments
(match-string 4 test-block
)))
53 (should (equal body
(match-string 5 test-block
)))
55 (should (org-test-string-exact-match
56 org-babel-src-block-regexp
57 (replace-regexp-in-string flags
"" test-block
)))
59 (should (org-test-string-exact-match
60 org-babel-src-block-regexp
61 (replace-regexp-in-string arguments
"" test-block
)))
62 ;; should be valid with no body
63 (should (org-test-string-exact-match
64 org-babel-src-block-regexp
65 (replace-regexp-in-string body
"" test-block
)))))
67 (ert-deftest test-org-babel
/get-header
()
68 (should (not (org-babel-get-header
69 org-babel-default-header-args
:doesnt-exist
)))
70 (should(equal '((:session .
"none"))
72 org-babel-default-header-args
:session
)))
73 (should(equal '((:session .
"none"))
75 org-babel-default-header-args
:session nil
)))
76 (should (not (org-babel-get-header
77 org-babel-default-header-args
:SESSION
)))
78 (should (equal '((:tangle .
"no"))
80 org-babel-default-header-args
:tangle
)))
82 (should (equal org-babel-default-header-args
84 org-babel-default-header-args
:doesnt-exist
'others
)))
85 (should (equal org-babel-default-header-args
87 org-babel-default-header-args nil
'others
)))
91 org-babel-default-header-args
:noweb
'others
)))))
93 (ert-deftest test-org-babel
/default-inline-header-args
()
95 '((:session .
"none") (:results .
"replace") (:exports .
"results"))
96 org-babel-default-inline-header-args
)))
98 ;;; ob-get-src-block-info
99 (ert-deftest test-org-babel
/get-src-block-info-language
()
100 (org-test-at-marker nil org-test-file-ob-anchor
101 (let ((info (org-babel-get-src-block-info)))
102 (should (string= "emacs-lisp" (nth 0 info
))))))
104 (ert-deftest test-org-babel
/get-src-block-info-body
()
105 (org-test-at-marker nil org-test-file-ob-anchor
106 (let ((info (org-babel-get-src-block-info)))
107 (should (string-match (regexp-quote org-test-file-ob-anchor
)
110 (ert-deftest test-org-babel
/get-src-block-info-tangle
()
111 (org-test-at-marker nil org-test-file-ob-anchor
112 (let ((info (org-babel-get-src-block-info)))
113 (should (string= "no" (cdr (assoc :tangle
(nth 2 info
))))))))
115 (ert-deftest test-org-babel
/elisp-in-header-arguments
()
116 "Test execution of elisp forms in header arguments."
117 ;; at the babel.org:elisp-forms-in-header-arguments header
118 (org-test-at-id "22d67284-bf14-4cdc-8319-f4bd876829d7"
119 (org-babel-next-src-block)
120 (let ((info (org-babel-get-src-block-info)))
121 (should (= 4 (org-babel-execute-src-block))))))
123 (ert-deftest test-org-babel
/simple-named-code-block
()
124 "Test that simple named code blocks can be evaluated."
125 (org-test-at-id "0d82b52d-1bb9-4916-816b-2c67c8108dbb"
126 (org-babel-next-src-block 1)
127 (should (= 42 (org-babel-execute-src-block)))))
129 (ert-deftest test-org-babel
/simple-variable-resolution
()
130 "Test that simple variable resolution is working."
131 (org-test-at-id "f68821bc-7f49-4389-85b5-914791ee3718"
132 (org-babel-next-src-block 2)
133 (should (= 4 (org-babel-execute-src-block)))))
135 (ert-deftest test-org-babel
/multi-line-header-arguments
()
136 "Test that multi-line header arguments and can be read."
137 (org-test-at-id "b77c8857-6c76-4ea9-8a61-ddc2648d96c4"
138 (org-babel-next-src-block)
139 (let ((results (org-babel-execute-src-block)))
140 (should(equal 'a
(cadr (assoc 1 results
))))
141 (should(equal 'd
(cadr (assoc 4 results
)))))))
143 (ert-deftest test-org-babel
/parse-header-args
()
144 (org-test-at-id "7eb0dc6e-1c53-4275-88b3-b22f3113b9c3"
145 (org-babel-next-src-block)
146 (let* ((info (org-babel-get-src-block-info))
147 (params (nth 2 info
)))
148 (message "%S" params
)
149 (should(equal "example-lang" (nth 0 info
)))
150 (should(string= "the body" (org-babel-trim (nth 1 info
))))
151 (should-not (member '(:session\ \ \ \
) params
))
152 (should(equal '(:session
) (assoc :session params
)))
153 (should(equal '(:result-type . output
) (assoc :result-type params
)))
154 (should(equal '(num .
9) (cdr (assoc :var params
)))))))
156 (ert-deftest test-org-babel
/parse-header-args2
()
157 (org-test-at-id "2409e8ba-7b5f-4678-8888-e48aa02d8cb4"
158 (should (string-match (regexp-quote "this is simple")
159 (org-babel-ref-resolve "simple-subtree")))
160 (org-babel-next-src-block)
161 (should (= 14 (org-babel-execute-src-block)))))
163 (ert-deftest test-org-babel
/inline-src-blocks
()
164 (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
165 (macrolet ((at-next (&rest body
)
168 (re-search-forward org-babel-inline-src-block-regexp nil t
)
169 (goto-char (match-beginning 1))
170 (save-match-data ,@body
))))
171 (at-next (should (equal 1 (org-babel-execute-src-block))))
172 (at-next (should (equal 2 (org-babel-execute-src-block))))
173 (at-next (should (equal 3 (org-babel-execute-src-block)))))))
175 (ert-deftest test-org-babel
/org-babel-get-inline-src-block-matches
()
176 (org-test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74"
177 (let ((test-point (point)))
178 (should (fboundp 'org-babel-get-inline-src-block-matches
))
179 (should (re-search-forward "src_" nil t
)) ;; 1
180 (should (org-babel-get-inline-src-block-matches))
181 (should (re-search-forward "}" nil
(point-at-bol))) ;; 1
182 (should-not (org-babel-get-inline-src-block-matches))
183 (should (re-search-forward "in" nil t
)) ;; 2
184 (should-not (org-babel-get-inline-src-block-matches))
185 (should (re-search-forward "echo" nil t
)) ;; 2
186 (should (org-babel-get-inline-src-block-matches))
187 (should (re-search-forward "blocks" nil t
)) ;; 3
188 (backward-char 8) ;; 3
189 (should (org-babel-get-inline-src-block-matches))
191 (should-not (org-babel-get-inline-src-block-matches))
192 (should (re-search-forward ":results" nil t
)) ;; 4
193 (should (org-babel-get-inline-src-block-matches))
195 (should-not (org-babel-get-inline-src-block-matches))
198 (ert-deftest test-org-babel
/inline-src_blk-default-results-replace-line-1
()
199 (let ((test-line "src_sh{echo 1}"))
200 ;; src_ at bol line 1...
201 (org-test-with-temp-text
203 (goto-char (point-min)) (org-ctrl-c-ctrl-c)
205 (concat test-line
" =1=")
206 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
207 (forward-char) (org-ctrl-c-ctrl-c)
209 (concat test-line
" =1= =1=")
210 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
211 (re-search-forward "1}")
212 (should-error (org-ctrl-c-ctrl-c))
213 (backward-char) ;; last char of block body
216 (concat test-line
" =1= =1= =1=")
217 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
218 ;; src_ follows space line 1...
219 (let ((test-line " src_emacs-lisp{ 1 }"))
220 (org-test-with-temp-text
222 (should-error (org-ctrl-c-ctrl-c))
223 (forward-char) (org-ctrl-c-ctrl-c)
225 (concat test-line
" =1=")
226 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
227 (re-search-forward "{ 1 ") (org-ctrl-c-ctrl-c)
229 (concat test-line
" =1= =1=")
230 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
232 (should-error (org-ctrl-c-ctrl-c)))))
234 (ert-deftest test-org-babel
/inline-src_blk-default-results-replace-line-2
()
235 ;; src_ at bol line 2...
236 (let ((test-line " src_emacs-lisp{ \"x\" }"))
237 (org-test-with-temp-text
238 (concat "\n" test-line
)
239 (should-error (org-ctrl-c-ctrl-c))
240 (goto-char (point-min))
241 (should-error (org-ctrl-c-ctrl-c))
243 (should-error (org-ctrl-c-ctrl-c))
244 (forward-char) (org-ctrl-c-ctrl-c)
246 (concat test-line
" =x=")
247 (buffer-substring-no-properties
248 (point-at-bol) (point-at-eol))))))
250 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }"))
251 (org-test-with-temp-text
253 (goto-char (point-max))
254 (insert (concat "\n" test-line
" end"))
255 (re-search-backward "src") (org-ctrl-c-ctrl-c)
257 (concat test-line
" =y= end")
258 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
259 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
261 (concat test-line
" =y= =y= end")
262 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
264 (should-error (org-ctrl-c-ctrl-c))))))
266 (ert-deftest test-org-babel
/inline-src_blk-manual-results-replace
()
267 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }"))
268 (org-test-with-temp-text
269 (concat "\n" test-line
)
270 (should-error (org-ctrl-c-ctrl-c))
271 (goto-char (point-max))
272 (should-error (org-ctrl-c-ctrl-c))
274 (should-error (org-ctrl-c-ctrl-c))
275 (forward-char) (org-ctrl-c-ctrl-c)
277 (concat test-line
" =x=")
278 (buffer-substring-no-properties
279 (point-at-bol) (point-at-eol))))))
281 (let ((test-line (concat " Some text prior to block "
282 "src_emacs-lisp[:results replace]{ \"y\" }")))
283 (org-test-with-temp-text test-line
284 (goto-char (point-max))
285 (insert (concat "\n" test-line
" end"))
286 (re-search-backward "src") (org-ctrl-c-ctrl-c)
288 (concat test-line
" =y= end")
289 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
290 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
292 (concat test-line
" =y= =y= end")
293 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
295 (should-error (org-ctrl-c-ctrl-c)))))
297 (ert-deftest test-org-babel
/inline-src_blk-results-silent
()
298 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
299 (org-test-with-temp-text test-line
301 (should (string= test-line
302 (buffer-substring-no-properties
303 (point-at-bol) (point-at-eol))))
305 (should-error (org-ctrl-c-ctrl-c))))
306 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
307 "[ :results silent ]{ \"y\" }")))
308 (org-test-with-temp-text
310 (goto-char (point-max))
311 (insert (concat "\n" test-line
" end"))
312 (re-search-backward "src_") (org-ctrl-c-ctrl-c)
313 (should (string= (concat test-line
" end")
314 (buffer-substring-no-properties
315 (point-at-bol) (point-at-eol))))
316 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
317 (should (string= (concat test-line
" end")
318 (buffer-substring-no-properties
319 (point-at-bol) (point-at-eol))))
321 (should-error (org-ctrl-c-ctrl-c)))))
323 (ert-deftest test-org-babel
/inline-src_blk-results-raw
()
324 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
325 (org-test-with-temp-text test-line
327 (should (string= (concat test-line
" x")
329 (let ((test-line (concat " Some text prior to block "
330 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
331 (org-test-with-temp-text (concat test-line
" end")
332 (re-search-forward "src_") (org-ctrl-c-ctrl-c)
333 (should (string= (concat test-line
" the end")
334 (buffer-substring-no-properties
335 (point-at-bol) (point-at-eol))))
336 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
337 (should (string= (concat test-line
" the the end")
338 (buffer-substring-no-properties
339 (point-at-bol) (point-at-eol))))
341 (should-error (org-ctrl-c-ctrl-c)))))
343 (ert-deftest test-org-babel
/inline-src_blk-results-file
()
344 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
345 (org-test-with-temp-text
348 (should (string= (concat test-line
" [[file:~/test-file]]")
349 (buffer-substring-no-properties
350 (point-min) (point-max)))))))
352 (ert-deftest test-org-babel
/inline-src_blk-results-scalar
()
353 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }"))
354 (org-test-with-temp-text
357 (should (string= (concat test-line
" =\"x\"=")
358 (buffer-substring-no-properties
359 (point-min) (point-max)))))))
361 (ert-deftest test-org-babel
/inline-src_blk-results-verbatim
()
362 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
363 (org-test-with-temp-text
366 (should (string= (concat test-line
" =\"x\"=")
367 (buffer-substring-no-properties
368 (point-min) (point-max)))))))
370 (ert-deftest test-org-babel
/combining-scalar-and-raw-result-types
()
371 (flet ((next-result ()
372 (org-babel-next-src-block)
373 (org-babel-execute-src-block)
374 (goto-char (org-babel-where-is-src-block-result))
376 (org-test-at-id "a73a2ab6-b8b2-4c0e-ae7f-23ad14eab7bc"
378 (should (org-babel-in-example-or-verbatim))
380 (should (not (org-babel-in-example-or-verbatim))))))
382 (ert-deftest test-org-babel
/no-defaut-value-for-var
()
383 "Test that the absence of a default value for a variable DOES THROW
385 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
386 (org-babel-next-src-block)
388 (should-error (org-babel-execute-src-block) :type
'error
)))
392 "variable \"x\" in block \"carre\" must be assigned a default value")
395 (ert-deftest test-org-babel
/just-one-results-block
()
396 "Test that evaluating two times the same code block does not result in a
397 duplicate results block."
398 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
399 (org-babel-execute-src-block)
400 (org-babel-execute-src-block) ; second code block execution
401 (should (search-forward "Hello")) ; the string inside the source code block
402 (should (search-forward "Hello")) ; the same string in the results block
403 (should-error (search-forward "Hello"))))
405 (ert-deftest test-org-babel
/nested-code-block
()
406 "Test nested code blocks inside code blocks don't cause problems."
407 (org-test-with-temp-text "#+begin_src org :results silent
408 ,#+begin_src emacs-lisp
412 (should (string= (org-babel-execute-src-block)
413 "#+begin_src emacs-lisp\n 'foo\n#+end_src"))))
415 (ert-deftest test-org-babel
/partial-nested-code-block
()
416 "Test nested code blocks inside code blocks don't cause problems."
417 (org-test-with-temp-text "#+begin_src org :results silent
418 ,#+begin_src emacs-lisp
420 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
422 (ert-deftest test-ob
/does-not-replace-a-block-with-the-results
()
423 (org-test-with-temp-text "#+NAME: foo
424 #+BEGIN_SRC emacs-lisp
427 (org-babel-next-src-block 1)
428 (should (eq 'foo
(org-babel-execute-src-block)))
429 (goto-char (point-min))
430 (org-babel-next-src-block 1)
431 (should (looking-at org-babel-src-block-regexp
))))
433 (ert-deftest test-ob
/catches-all-references
()
434 (org-test-with-temp-text "
435 #+NAME: literal-example
441 #+NAME: read-literal-example
442 #+BEGIN_SRC emacs-lisp :var x=literal-example
443 (concatenate 'string x \" for me.\")
445 (org-babel-next-src-block 1)
446 (should (string= (org-babel-execute-src-block)
447 "A literal example\non two lines for me."))))
449 (ert-deftest test-ob
/resolve-code-blocks-before-data-blocks
()
450 (org-test-with-temp-text "
455 #+begin_src emacs-lisp
459 #+begin_src emacs-lisp :var foo=foo
462 (org-babel-next-src-block 2)
463 (should (string= (org-babel-execute-src-block) "baz"))))
465 (ert-deftest test-ob
/do-not-resolve-to-partial-names-data
()
466 (org-test-with-temp-text "
475 #+begin_src emacs-lisp :var x=base
478 (org-babel-next-src-block 1)
479 (should (equal (org-babel-execute-src-block) '((3) (4))))))
481 (ert-deftest test-ob
/do-not-resolve-to-partial-names-code
()
482 (org-test-with-temp-text "
484 #+begin_src emacs-lisp
489 #+begin_src emacs-lisp
493 #+begin_src emacs-lisp :var x=base
496 (org-babel-next-src-block 3)
497 (should (equal (org-babel-execute-src-block) "foo"))))
499 (ert-deftest test-ob
/allow-spaces-around-
=-in-var-specs
()
500 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
504 (should (= 10 (org-babel-execute-src-block)))))
506 (ert-deftest test-ob
/org-babel-update-intermediate
()
507 (org-test-with-temp-text "#+name: foo
508 #+begin_src emacs-lisp
515 #+begin_src emacs-lisp :var it=foo
518 (let ((org-babel-update-intermediate nil
))
519 (goto-char (point-min))
520 (org-babel-next-src-block 2)
521 (should (= 3 (org-babel-execute-src-block)))
522 (goto-char (point-min))
524 (should (looking-at ": 4")))
525 (let ((org-babel-update-intermediate t
))
526 (goto-char (point-min))
527 (org-babel-next-src-block 2)
528 (should (= 3 (org-babel-execute-src-block)))
529 (goto-char (point-min))
531 (should (looking-at ": 2")))))
533 (ert-deftest test-ob
/eval-header-argument
()
534 (flet ((check-eval (eval runp
)
535 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
538 (let ((foo :not-run
))
540 (progn (should (org-babel-execute-src-block))
541 (should (eq foo
:evald
)))
542 (progn (should-not (org-babel-execute-src-block))
543 (should-not (eq foo
:evald
))))))))
544 (check-eval "never" nil
)
545 (check-eval "no" nil
)
546 (check-eval "never-export" t
)
547 (check-eval "no-export" t
)
548 (let ((org-current-export-file "something"))
549 (check-eval "never" nil
)
550 (check-eval "no" nil
)
551 (check-eval "never-export" nil
)
552 (check-eval "no-export" nil
))))
554 (ert-deftest test-ob
/noweb-expansion
()
555 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
563 (should (string= (org-babel-expand-noweb-references) "bar")))
564 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
569 #+begin_src sh :noweb-sep \"\"
573 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
576 (should (string= (org-babel-expand-noweb-references) "barbaz"))))
578 (ert-deftest test-ob
/splitting-variable-lists-in-references
()
579 (org-test-with-temp-text ""
580 (should (= 1 (length (org-babel-ref-split-args
581 "a=\"this, no work\""))))
582 (should (= 2 (length (org-babel-ref-split-args
583 "a=\"this, no work\", b=1"))))))
585 (ert-deftest test-ob
/org-babel-balanced-split
()
587 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
588 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
593 ;;; test-ob ends here