Merge branch 'maint'
[org-mode.git] / testing / lisp / test-ob.el
blob0565043c009db8422055a20c29000af78acba327
1 ;;; test-ob.el --- tests for ob.el
3 ;; Copyright (c) 2010-2012 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
8 (ert-deftest test-org-babel/multi-line-header-regexp ()
9 (should(equal "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
10 org-babel-multi-line-header-regexp))
11 ;;TODO can be optimised - and what about blah4 blah5 blah6?
12 (should (string-match
13 org-babel-multi-line-header-regexp
14 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
15 (should
16 (equal
17 "blah1 blah2 blah3 \t"
18 (match-string
20 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
22 ;;TODO Check - should this fail?
23 (should
24 (not (org-test-string-exact-match
25 org-babel-multi-line-header-regexp
26 " \t #+headers : blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))))
28 (ert-deftest test-org-babel/src-block-regexp ()
29 (let ((test-block
30 (concat
31 "#+begin_src language -n-r-a-b -c :argument-1 yes :argument-2 no\n"
32 "echo this is a test\n"
33 "echo Currently in ' $PWD\n"
34 "#+end_src"))
35 (language "language")
36 (flags "-n-r-a-b -c ")
37 (arguments ":argument-1 yes :argument-2 no")
38 (body "echo this is a test\necho Currently in ' $PWD\n"))
39 (should (string-match org-babel-src-block-regexp test-block))
40 (should (string-match org-babel-src-block-regexp (upcase test-block)))
41 (should (equal language (match-string 2 test-block)))
42 ;;TODO Consider refactoring
43 (should (equal flags (match-string 3 test-block)))
44 (should (equal arguments (match-string 4 test-block)))
45 (should (equal body (match-string 5 test-block)))
46 ;;no switches
47 (should (org-test-string-exact-match
48 org-babel-src-block-regexp
49 (replace-regexp-in-string flags "" test-block)))
50 ;;no header arguments
51 (should (org-test-string-exact-match
52 org-babel-src-block-regexp
53 (replace-regexp-in-string arguments "" test-block)))
54 ;; should be valid with no body
55 (should (org-test-string-exact-match
56 org-babel-src-block-regexp
57 (replace-regexp-in-string body "" test-block)))))
59 (ert-deftest test-org-babel/get-header ()
60 (should (not (org-babel-get-header
61 org-babel-default-header-args :doesnt-exist)))
62 (should(equal '((:session . "none"))
63 (org-babel-get-header
64 org-babel-default-header-args :session)))
65 (should(equal '((:session . "none"))
66 (org-babel-get-header
67 org-babel-default-header-args :session nil)))
68 (should (not (org-babel-get-header
69 org-babel-default-header-args :SESSION)))
70 (should (equal '((:tangle . "no"))
71 (org-babel-get-header
72 org-babel-default-header-args :tangle)))
73 ;; with OTHERS option
74 (should (equal org-babel-default-header-args
75 (org-babel-get-header
76 org-babel-default-header-args :doesnt-exist 'others)))
77 (should (equal org-babel-default-header-args
78 (org-babel-get-header
79 org-babel-default-header-args nil 'others)))
80 (should (null
81 (assoc :noweb
82 (org-babel-get-header
83 org-babel-default-header-args :noweb 'others)))))
85 (ert-deftest test-org-babel/default-inline-header-args ()
86 (should(equal
87 '((:session . "none") (:results . "replace") (:exports . "results"))
88 org-babel-default-inline-header-args)))
90 (ert-deftest ob-test/org-babel-combine-header-arg-lists ()
91 (let ((results (org-babel-combine-header-arg-lists
92 '((foo . :any)
93 (bar)
94 (baz . ((foo bar) (baz)))
95 (qux . ((foo bar baz qux)))
96 (quux . ((foo bar))))
97 '((bar)
98 (baz . ((baz)))
99 (quux . :any)))))
100 (dolist (pair '((foo . :any)
101 (bar)
102 (baz . ((baz)))
103 (quux . :any)
104 (qux . ((foo bar baz qux)))))
105 (should (equal (cdr pair)
106 (cdr (assoc (car pair) results)))))))
108 ;;; ob-get-src-block-info
109 (ert-deftest test-org-babel/get-src-block-info-language ()
110 (org-test-at-marker nil org-test-file-ob-anchor
111 (let ((info (org-babel-get-src-block-info)))
112 (should (string= "emacs-lisp" (nth 0 info))))))
114 (ert-deftest test-org-babel/get-src-block-info-body ()
115 (org-test-at-marker nil org-test-file-ob-anchor
116 (let ((info (org-babel-get-src-block-info)))
117 (should (string-match (regexp-quote org-test-file-ob-anchor)
118 (nth 1 info))))))
120 (ert-deftest test-org-babel/get-src-block-info-tangle ()
121 (org-test-at-marker nil org-test-file-ob-anchor
122 (let ((info (org-babel-get-src-block-info)))
123 (should (string= "no" (cdr (assoc :tangle (nth 2 info))))))))
125 (ert-deftest test-org-babel/elisp-in-header-arguments ()
126 "Test execution of elisp forms in header arguments."
127 (org-test-with-temp-text-in-file "
129 * elisp forms in header arguments
130 :PROPERTIES:
131 :var: prop = (* 7 6)
132 :END:
133 #+begin_src emacs-lisp
134 prop
135 #+end_src"
137 (progn
138 (goto-char (point-min))
139 (org-babel-next-src-block)
140 (let ((info (org-babel-get-src-block-info)))
141 (should (= 42 (org-babel-execute-src-block)))))))
143 (ert-deftest test-org-babel/simple-named-code-block ()
144 "Test that simple named code blocks can be evaluated."
145 (org-test-with-temp-text-in-file "
147 #+name: i-have-a-name
148 #+begin_src emacs-lisp
150 #+end_src"
152 (progn
153 (org-babel-next-src-block 1)
154 (should (= 42 (org-babel-execute-src-block))))))
156 (ert-deftest test-org-babel/simple-variable-resolution ()
157 "Test that simple variable resolution is working."
158 (org-test-with-temp-text-in-file "
160 #+name: four
161 #+begin_src emacs-lisp
162 (list 1 2 3 4)
163 #+end_src
165 #+begin_src emacs-lisp :var four=four
166 (length four)
167 #+end_src"
169 (progn
170 (org-babel-next-src-block 2)
171 (should (= 4 (org-babel-execute-src-block)))
172 (forward-line 5)
173 (should (string= ": 4" (buffer-substring
174 (point-at-bol)
175 (point-at-eol)))))))
177 (ert-deftest test-org-babel/multi-line-header-arguments ()
178 "Test that multi-line header arguments and can be read."
179 (org-test-with-temp-text-in-file "
181 #+headers: :var letters='(a b c d e f g)
182 #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
183 (require 'cl)
184 (defalias 'my-map (if (org-version-check \"24.2.50\" \"cl\" :predicate)
185 'cl-map
186 'map))
187 (my-map 'list #'list numbers letters)
188 #+end_src"
190 (progn
191 (org-babel-next-src-block)
192 (let ((results (org-babel-execute-src-block)))
193 (should(equal 'a (cadr (assoc 1 results))))
194 (should(equal 'd (cadr (assoc 4 results))))))))
196 (ert-deftest test-org-babel/parse-header-args ()
197 (org-test-with-temp-text-in-file "
199 #+begin_src example-lang :session :results output :var num=9
200 the body
201 #+end_src"
203 (progn
204 (org-babel-next-src-block)
205 (let* ((info (org-babel-get-src-block-info))
206 (params (nth 2 info)))
207 (message "%S" params)
208 (should(equal "example-lang" (nth 0 info)))
209 (should(string= "the body" (org-babel-trim (nth 1 info))))
210 (should-not (member '(:session\ \ \ \ ) params))
211 (should(equal '(:session) (assoc :session params)))
212 (should(equal '(:result-type . output) (assoc :result-type params)))
213 (should(equal '(num . 9) (cdr (assoc :var params))))))))
215 (ert-deftest test-org-babel/parse-header-args2 ()
216 (org-test-with-temp-text-in-file "
218 * resolving sub-trees as references
220 #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
221 (length text)
222 #+end_src
224 #+begin_src org :noweb yes
225 <<simple-subtree>>
226 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
227 #+end_src
229 ** simple subtree with custom ID
230 :PROPERTIES:
231 :CUSTOM_ID: simple-subtree
232 :END:
233 this is simple"
235 (progn
236 (should (string-match (regexp-quote "this is simple")
237 (org-babel-ref-resolve "simple-subtree")))
238 (org-babel-next-src-block)
239 (should (= 14 (org-babel-execute-src-block))))))
241 (ert-deftest test-org-babel/inline-src-blocks ()
242 (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
243 (macrolet ((at-next (&rest body)
244 `(progn
245 (move-end-of-line 1)
246 (re-search-forward org-babel-inline-src-block-regexp nil t)
247 (goto-char (match-beginning 1))
248 (save-match-data ,@body))))
249 (at-next (should (equal 1 (org-babel-execute-src-block))))
250 (at-next (should (equal 2 (org-babel-execute-src-block))))
251 (at-next (should (equal 3 (org-babel-execute-src-block)))))))
253 (ert-deftest test-org-babel/org-babel-get-inline-src-block-matches ()
254 (org-test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74"
255 (let ((test-point (point)))
256 (should (fboundp 'org-babel-get-inline-src-block-matches))
257 (should (re-search-forward "src_" nil t)) ;; 1
258 (should (org-babel-get-inline-src-block-matches))
259 (should (re-search-forward "}" nil (point-at-bol))) ;; 1
260 (should-not (org-babel-get-inline-src-block-matches))
261 (should (re-search-forward "in" nil t)) ;; 2
262 (should-not (org-babel-get-inline-src-block-matches))
263 (should (re-search-forward "echo" nil t)) ;; 2
264 (should (org-babel-get-inline-src-block-matches))
265 (should (re-search-forward "blocks" nil t)) ;; 3
266 (backward-char 8) ;; 3
267 (should (org-babel-get-inline-src-block-matches))
268 (forward-char 1) ;;3
269 (should-not (org-babel-get-inline-src-block-matches))
270 (should (re-search-forward ":results" nil t)) ;; 4
271 (should (org-babel-get-inline-src-block-matches))
272 (end-of-line)
273 (should-not (org-babel-get-inline-src-block-matches)))))
275 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-1 ()
276 (let ((test-line "src_sh{echo 1}"))
277 ;; src_ at bol line 1...
278 (org-test-with-temp-text
279 test-line
280 (goto-char (point-min)) (org-ctrl-c-ctrl-c)
281 (should (string=
282 (concat test-line " =1=")
283 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
284 (forward-char) (org-ctrl-c-ctrl-c)
285 (should (string=
286 (concat test-line " =1= =1=")
287 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
288 (re-search-forward "1}")
289 (should-error (org-ctrl-c-ctrl-c))
290 (backward-char) ;; last char of block body
291 (org-ctrl-c-ctrl-c)
292 (should (string=
293 (concat test-line " =1= =1= =1=")
294 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
295 ;; src_ follows space line 1...
296 (let ((test-line " src_emacs-lisp{ 1 }"))
297 (org-test-with-temp-text
298 test-line
299 (should-error (org-ctrl-c-ctrl-c))
300 (forward-char) (org-ctrl-c-ctrl-c)
301 (should (string=
302 (concat test-line " =1=")
303 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
304 (re-search-forward "{ 1 ") (org-ctrl-c-ctrl-c)
305 (should (string=
306 (concat test-line " =1= =1=")
307 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
308 (forward-char)
309 (should-error (org-ctrl-c-ctrl-c)))))
311 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-2 ()
312 ;; src_ at bol line 2...
313 (let ((test-line " src_emacs-lisp{ \"x\" }"))
314 (org-test-with-temp-text
315 (concat "\n" test-line)
316 (should-error (org-ctrl-c-ctrl-c))
317 (goto-char (point-min))
318 (should-error (org-ctrl-c-ctrl-c))
319 (forward-line)
320 (should-error (org-ctrl-c-ctrl-c))
321 (forward-char) (org-ctrl-c-ctrl-c)
322 (should (string=
323 (concat test-line " =x=")
324 (buffer-substring-no-properties
325 (point-at-bol) (point-at-eol))))))
327 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }"))
328 (org-test-with-temp-text
329 test-line
330 (goto-char (point-max))
331 (insert (concat "\n" test-line " end"))
332 (re-search-backward "src") (org-ctrl-c-ctrl-c)
333 (should (string=
334 (concat test-line " =y= end")
335 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
336 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
337 (should (string=
338 (concat test-line " =y= =y= end")
339 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
340 (forward-char)
341 (should-error (org-ctrl-c-ctrl-c))))))
343 (ert-deftest test-org-babel/inline-src_blk-manual-results-replace ()
344 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }"))
345 (org-test-with-temp-text
346 (concat "\n" test-line)
347 (should-error (org-ctrl-c-ctrl-c))
348 (goto-char (point-max))
349 (should-error (org-ctrl-c-ctrl-c))
350 (beginning-of-line)
351 (should-error (org-ctrl-c-ctrl-c))
352 (forward-char) (org-ctrl-c-ctrl-c)
353 (should (string=
354 (concat test-line " =x=")
355 (buffer-substring-no-properties
356 (point-at-bol) (point-at-eol))))))
358 (let ((test-line (concat " Some text prior to block "
359 "src_emacs-lisp[:results replace]{ \"y\" }")))
360 (org-test-with-temp-text test-line
361 (goto-char (point-max))
362 (insert (concat "\n" test-line " end"))
363 (re-search-backward "src") (org-ctrl-c-ctrl-c)
364 (should (string=
365 (concat test-line " =y= end")
366 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
367 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
368 (should (string=
369 (concat test-line " =y= =y= end")
370 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
371 (forward-char)
372 (should-error (org-ctrl-c-ctrl-c)))))
374 (ert-deftest test-org-babel/inline-src_blk-results-silent ()
375 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
376 (org-test-with-temp-text test-line
377 (org-ctrl-c-ctrl-c)
378 (should (string= test-line
379 (buffer-substring-no-properties
380 (point-at-bol) (point-at-eol))))
381 (end-of-buffer)
382 (should-error (org-ctrl-c-ctrl-c))))
383 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
384 "[ :results silent ]{ \"y\" }")))
385 (org-test-with-temp-text
386 test-line
387 (goto-char (point-max))
388 (insert (concat "\n" test-line " end"))
389 (re-search-backward "src_") (org-ctrl-c-ctrl-c)
390 (should (string= (concat test-line " end")
391 (buffer-substring-no-properties
392 (point-at-bol) (point-at-eol))))
393 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
394 (should (string= (concat test-line " end")
395 (buffer-substring-no-properties
396 (point-at-bol) (point-at-eol))))
397 (forward-char)
398 (should-error (org-ctrl-c-ctrl-c)))))
400 (ert-deftest test-org-babel/inline-src_blk-results-raw ()
401 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
402 (org-test-with-temp-text test-line
403 (org-ctrl-c-ctrl-c)
404 (should (string= (concat test-line " x")
405 (buffer-string)))))
406 (let ((test-line (concat " Some text prior to block "
407 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
408 (org-test-with-temp-text (concat test-line " end")
409 (re-search-forward "src_") (org-ctrl-c-ctrl-c)
410 (should (string= (concat test-line " the end")
411 (buffer-substring-no-properties
412 (point-at-bol) (point-at-eol))))
413 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
414 (should (string= (concat test-line " the the end")
415 (buffer-substring-no-properties
416 (point-at-bol) (point-at-eol))))
417 (forward-char)
418 (should-error (org-ctrl-c-ctrl-c)))))
420 (ert-deftest test-org-babel/inline-src_blk-results-file ()
421 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
422 (org-test-with-temp-text
423 test-line
424 (org-ctrl-c-ctrl-c)
425 (should (string= (concat test-line " [[file:~/test-file]]")
426 (buffer-substring-no-properties
427 (point-min) (point-max)))))))
429 (ert-deftest test-org-babel/inline-src_blk-results-scalar ()
430 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }"))
431 (org-test-with-temp-text
432 test-line
433 (org-ctrl-c-ctrl-c)
434 (should (string= (concat test-line " =\"x\"=")
435 (buffer-substring-no-properties
436 (point-min) (point-max)))))))
438 (ert-deftest test-org-babel/inline-src_blk-results-verbatim ()
439 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
440 (org-test-with-temp-text
441 test-line
442 (org-ctrl-c-ctrl-c)
443 (should (string= (concat test-line " =\"x\"=")
444 (buffer-substring-no-properties
445 (point-min) (point-max)))))))
447 (ert-deftest test-org-babel/combining-scalar-and-raw-result-types ()
448 (org-test-with-temp-text-in-file "
450 #+begin_src sh :results scalar
451 echo \"[[file:./cv.cls]]\"
452 #+end_src
454 #+name:
455 : [[file:./cv.cls]]
457 #+begin_src sh :results raw scalar
458 echo \"[[file:./cv.cls]]\"
459 #+end_src
461 (flet ((next-result ()
462 (org-babel-next-src-block)
463 (org-babel-execute-src-block)
464 (goto-char (org-babel-where-is-src-block-result))
465 (forward-line 1)))
466 (goto-char (point-min))
467 (next-result)
468 (should (org-babel-in-example-or-verbatim))
469 (next-result)
470 (should (not (org-babel-in-example-or-verbatim))))))
472 (ert-deftest test-org-babel/no-defaut-value-for-var ()
473 "Test that the absence of a default value for a variable DOES THROW
474 a proper error."
475 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
476 (org-babel-next-src-block)
477 (let ((err
478 (should-error (org-babel-execute-src-block) :type 'error)))
479 (should
480 (equal
481 '(error
482 "variable \"x\" in block \"carre\" must be assigned a default value")
483 err)))))
485 (ert-deftest test-org-babel/just-one-results-block ()
486 "Test that evaluating two times the same code block does not result in a
487 duplicate results block."
488 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
489 (org-babel-execute-src-block)
490 (org-babel-execute-src-block) ; second code block execution
491 (should (search-forward "Hello")) ; the string inside the source code block
492 (should (search-forward "Hello")) ; the same string in the results block
493 (should-error (search-forward "Hello"))))
495 (ert-deftest test-org-babel/nested-code-block ()
496 "Test nested code blocks inside code blocks don't cause problems."
497 (org-test-with-temp-text "#+begin_src org :results silent
498 ,#+begin_src emacs-lisp
499 , 'foo
500 ,#+end_src
501 #+end_src"
502 (should (string= (org-babel-execute-src-block)
503 "#+begin_src emacs-lisp\n 'foo\n#+end_src"))))
505 (ert-deftest test-org-babel/partial-nested-code-block ()
506 "Test nested code blocks inside code blocks don't cause problems."
507 (org-test-with-temp-text "#+begin_src org :results silent
508 ,#+begin_src emacs-lisp
509 #+end_src"
510 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
512 (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
513 (org-test-with-temp-text "#+NAME: foo
514 #+BEGIN_SRC emacs-lisp
515 'foo
516 #+END_SRC\n"
517 (org-babel-next-src-block 1)
518 (should (eq 'foo (org-babel-execute-src-block)))
519 (goto-char (point-min))
520 (org-babel-next-src-block 1)
521 (should (looking-at org-babel-src-block-regexp))))
523 (ert-deftest test-ob/catches-all-references ()
524 (org-test-with-temp-text "
525 #+NAME: literal-example
526 #+BEGIN_EXAMPLE
527 A literal example
528 on two lines
529 #+END_EXAMPLE
531 #+NAME: read-literal-example
532 #+BEGIN_SRC emacs-lisp :var x=literal-example
533 (concatenate 'string x \" for me.\")
534 #+END_SRC"
535 (org-babel-next-src-block 1)
536 (should (string= (org-babel-execute-src-block)
537 "A literal example\non two lines for me."))))
539 (ert-deftest test-ob/resolve-code-blocks-before-data-blocks ()
540 (org-test-with-temp-text "
541 #+name: foo
542 : bar
544 #+name: foo
545 #+begin_src emacs-lisp
546 \"baz\"
547 #+end_src
549 #+begin_src emacs-lisp :var foo=foo
551 #+end_src"
552 (org-babel-next-src-block 2)
553 (should (string= (org-babel-execute-src-block) "baz"))))
555 (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
556 (org-test-with-temp-text "
557 #+tblname: base_plus
558 | 1 |
559 | 2 |
561 #+tblname: base
562 | 3 |
563 | 4 |
565 #+begin_src emacs-lisp :var x=base
567 #+end_src"
568 (org-babel-next-src-block 1)
569 (should (equal (org-babel-execute-src-block) '((3) (4))))))
571 (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
572 (org-test-with-temp-text "
573 #+name: base_plus
574 #+begin_src emacs-lisp
575 'bar
576 #+end_src
578 #+name: base
579 #+begin_src emacs-lisp
580 'foo
581 #+end_src
583 #+begin_src emacs-lisp :var x=base
585 #+end_src"
586 (org-babel-next-src-block 3)
587 (should (equal (org-babel-execute-src-block) "foo"))))
589 (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
590 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
591 (+ a b c d)
592 #+end_src
594 (should (= 10 (org-babel-execute-src-block)))))
596 (ert-deftest test-ob/org-babel-update-intermediate ()
597 (org-test-with-temp-text "#+name: foo
598 #+begin_src emacs-lisp
600 #+end_src
602 #+results: foo
605 #+begin_src emacs-lisp :var it=foo
606 (+ it 1)
607 #+end_src"
608 (let ((org-babel-update-intermediate nil))
609 (goto-char (point-min))
610 (org-babel-next-src-block 2)
611 (should (= 3 (org-babel-execute-src-block)))
612 (goto-char (point-min))
613 (forward-line 6)
614 (should (looking-at ": 4")))
615 (let ((org-babel-update-intermediate t))
616 (goto-char (point-min))
617 (org-babel-next-src-block 2)
618 (should (= 3 (org-babel-execute-src-block)))
619 (goto-char (point-min))
620 (forward-line 6)
621 (should (looking-at ": 2")))))
623 (ert-deftest test-ob/eval-header-argument ()
624 (flet ((check-eval (eval runp)
625 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
626 (setq foo :evald)
627 #+end_src" eval)
628 (let ((foo :not-run))
629 (if runp
630 (progn (should (org-babel-execute-src-block))
631 (should (eq foo :evald)))
632 (progn (should-not (org-babel-execute-src-block))
633 (should-not (eq foo :evald))))))))
634 (check-eval "never" nil)
635 (check-eval "no" nil)
636 (check-eval "never-export" t)
637 (check-eval "no-export" t)
638 (let ((org-current-export-file "something"))
639 (check-eval "never" nil)
640 (check-eval "no" nil)
641 (check-eval "never-export" nil)
642 (check-eval "no-export" nil))))
644 (ert-deftest test-ob/noweb-expansion-1 ()
645 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
646 <<foo>>
647 #+end_src
649 #+name: foo
650 #+begin_src sh
652 #+end_src"
653 (should (string= (org-babel-expand-noweb-references) "bar"))))
655 (ert-deftest test-ob/noweb-expansion-2 ()
656 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
657 <<foo>>
658 #+end_src
660 #+name: foo
661 #+begin_src sh :noweb-sep \"\"
663 #+end_src
665 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
667 #+end_src"
668 (should (string= (org-babel-expand-noweb-references) "barbaz"))))
670 (ert-deftest test-ob/splitting-variable-lists-in-references ()
671 (org-test-with-temp-text ""
672 (should (= 1 (length (org-babel-ref-split-args
673 "a=\"this, no work\""))))
674 (should (= 2 (length (org-babel-ref-split-args
675 "a=\"this, no work\", b=1"))))))
677 (ert-deftest test-ob/org-babel-balanced-split ()
678 (should (equal
679 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
680 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
681 '((32 9) . 58)))))
683 (ert-deftest test-ob/commented-last-block-line-no-var ()
684 (org-test-with-temp-text-in-file "
685 #+begin_src emacs-lisp
687 #+end_src"
688 (progn
689 (org-babel-next-src-block)
690 (org-ctrl-c-ctrl-c)
691 (should (re-search-forward "\\#\\+results:" nil t))
692 (forward-line)
693 (should
694 (string=
696 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
697 (org-test-with-temp-text-in-file "
698 #+begin_src emacs-lisp
699 \"some text\";;
700 #+end_src"
702 (progn
703 (org-babel-next-src-block)
704 (org-ctrl-c-ctrl-c)
705 (should (re-search-forward "\\#\\+results:" nil t))
706 (forward-line)
707 (should
708 (string=
709 ": some text"
710 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
712 (ert-deftest test-ob/commented-last-block-line-with-var ()
713 (org-test-with-temp-text-in-file "
714 #+begin_src emacs-lisp :var a=1
716 #+end_src"
717 (progn
718 (org-babel-next-src-block)
719 (org-ctrl-c-ctrl-c)
720 (re-search-forward "\\#\\+results:" nil t)
721 (forward-line)
722 (should (string=
724 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
725 (org-test-with-temp-text-in-file "
726 #+begin_src emacs-lisp :var a=2
728 #+end_src"
729 (progn
730 (org-babel-next-src-block)
731 (org-ctrl-c-ctrl-c)
732 (re-search-forward "\\#\\+results:" nil t)
733 (forward-line)
734 (should (string=
735 ": 2"
736 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
738 (defun test-ob-verify-result-and-removed-result (result buffer-text)
739 "Test helper function to test `org-babel-remove-result'.
740 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
741 and the result of execution is verified against RESULT.
743 The block is actually executed /twice/ to ensure result
744 replacement happens correctly."
745 (org-test-with-temp-text
746 buffer-text
747 (progn
748 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
749 (should (re-search-forward "\\#\\+results:" nil t))
750 (forward-line)
751 (should (string= result
752 (buffer-substring-no-properties
753 (point-at-bol)
754 (- (point-max) 16))))
755 (org-babel-previous-src-block) (org-babel-remove-result)
756 (should (string= buffer-text
757 (buffer-substring-no-properties
758 (point-min) (point-max)))))))
760 (ert-deftest test-ob/org-babel-remove-result--results-default ()
761 "Test `org-babel-remove-result' with default :results."
762 (mapcar (lambda (language)
763 (test-ob-verify-result-and-removed-result
764 "\n"
765 (concat
766 "* org-babel-remove-result
767 #+begin_src " language "
768 #+end_src
770 * next heading")))
771 '("sh" "emacs-lisp")))
773 (ert-deftest test-ob/org-babel-remove-result--results-list ()
774 "Test `org-babel-remove-result' with :results list."
775 (test-ob-verify-result-and-removed-result
776 "- 1
779 - (quote (4 5))"
781 "* org-babel-remove-result
782 #+begin_src emacs-lisp :results list
783 '(1 2 3 '(4 5))
784 #+end_src
786 * next heading"))
788 (ert-deftest test-ob/org-babel-results-indented-wrap ()
789 "Ensure that wrapped results are inserted correction when indented.
790 If not inserted correctly then the second evaluation will fail
791 trying to find the :END: marker."
792 (org-test-with-temp-text
793 "- indented
794 #+begin_src sh :results file wrap
795 echo test.txt
796 #+end_src"
797 (org-babel-next-src-block 1)
798 (org-babel-execute-src-block)
799 (org-babel-execute-src-block)))
801 (ert-deftest test-ob/file-desc-header-argument ()
802 "Test that the :file-desc header argument is used."
803 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
804 \"foo\"
805 #+end_src
807 #+begin_src emacs-lisp :results file :file-desc
808 \"foo\"
809 #+end_src"
810 (org-babel-execute-src-block)
811 (org-babel-next-src-block 1)
812 (org-babel-execute-src-block)
813 (goto-char (point-min))
814 (should (search-forward "[[file:foo][bar]]" nil t))
815 (should (search-forward "[[file:foo][foo]]" nil t))))
817 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
818 "Test `org-babel-remove-result' with :results pp."
819 (test-ob-verify-result-and-removed-result
820 ": \"I /am/ working!\""
822 "* org-babel-remove-result
823 #+begin_src emacs-lisp :results pp
824 \"I /am/ working!\")
825 #+end_src
827 * next heading"))
829 (ert-deftest test-org-babel/inline-src_blk-preceded-punct-preceded-by-point ()
830 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
831 (org-test-with-temp-text
832 test-line
833 (forward-char 1)
834 (org-ctrl-c-ctrl-c)
835 (should (re-search-forward "=\"x\"=" nil t))
836 (forward-line))))
838 (ert-deftest test-ob/commented-last-block-line-with-var ()
839 (org-test-with-temp-text-in-file "
840 #+begin_src emacs-lisp :var a=1
842 #+end_src"
843 (progn
844 (org-babel-next-src-block)
845 (org-ctrl-c-ctrl-c)
846 (re-search-forward "\\#\\+results:" nil t)
847 (forward-line)
848 (should (string=
850 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
851 (org-test-with-temp-text-in-file "
852 #+begin_src emacs-lisp :var a=2
854 #+end_src"
855 (progn
856 (org-babel-next-src-block)
857 (org-ctrl-c-ctrl-c)
858 (re-search-forward "\\#\\+results:" nil t)
859 (forward-line)
860 (should (string=
861 ": 2"
862 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
864 (defun test-ob-verify-result-and-removed-result (result buffer-text)
865 "Test helper function to test `org-babel-remove-result'.
866 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
867 and the result of execution is verified against RESULT.
869 The block is actually executed /twice/ to ensure result
870 replacement happens correctly."
871 (org-test-with-temp-text
872 buffer-text
873 (progn
874 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
875 (should (re-search-forward "\\#\\+results:" nil t))
876 (forward-line)
877 (should (string= result
878 (buffer-substring-no-properties
879 (point-at-bol)
880 (- (point-max) 16))))
881 (org-babel-previous-src-block) (org-babel-remove-result)
882 (should (string= buffer-text
883 (buffer-substring-no-properties
884 (point-min) (point-max)))))))
886 (ert-deftest test-ob/org-babel-remove-result--results-default ()
887 "Test `org-babel-remove-result' with default :results."
888 (mapcar (lambda (language)
889 (test-ob-verify-result-and-removed-result
890 "\n"
891 (concat
892 "* org-babel-remove-result
893 #+begin_src " language "
894 #+end_src
896 * next heading")))
897 '("sh" "emacs-lisp")))
899 (ert-deftest test-ob/org-babel-remove-result--results-list ()
900 "Test `org-babel-remove-result' with :results list."
901 (test-ob-verify-result-and-removed-result
902 "- 1
905 - (quote (4 5))"
907 "* org-babel-remove-result
908 #+begin_src emacs-lisp :results list
909 '(1 2 3 '(4 5))
910 #+end_src
912 * next heading"))
914 (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
915 "Test `org-babel-remove-result' with :results wrap."
916 (test-ob-verify-result-and-removed-result
917 ":RESULTS:
918 hello there
919 :END:"
921 "* org-babel-remove-result
923 #+begin_src emacs-lisp :results wrap
924 \"hello there\"
925 #+end_src
927 * next heading"))
929 (ert-deftest test-ob/org-babel-remove-result--results-org ()
930 "Test `org-babel-remove-result' with :results org."
931 (test-ob-verify-result-and-removed-result
932 "#+BEGIN_SRC org
933 ,* heading
934 ,** subheading
935 ,content
936 #+END_SRC"
938 "* org-babel-remove-result
939 #+begin_src emacs-lisp :results org
940 \"* heading
941 ** subheading
942 content\"
943 #+end_src
945 * next heading"))
947 (ert-deftest test-ob/org-babel-remove-result--results-html ()
948 "Test `org-babel-remove-result' with :results html."
949 (test-ob-verify-result-and-removed-result
950 "#+BEGIN_HTML
951 <head><body></body></head>
952 #+END_HTML"
954 "* org-babel-remove-result
955 #+begin_src emacs-lisp :results html
956 \"<head><body></body></head>\"
957 #+end_src
959 * next heading"))
961 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
962 "Test `org-babel-remove-result' with :results latex."
963 (test-ob-verify-result-and-removed-result
964 "#+BEGIN_LaTeX
965 Line 1
966 Line 2
967 Line 3
968 #+END_LaTeX"
970 "* org-babel-remove-result
971 #+begin_src emacs-lisp :results latex
972 \"Line 1
973 Line 2
974 Line 3\"
975 #+end_src
977 * next heading"))
979 (ert-deftest test-ob/org-babel-remove-result--results-code ()
980 "Test `org-babel-remove-result' with :results code."
982 (test-ob-verify-result-and-removed-result
983 "#+BEGIN_SRC emacs-lisp
984 \"I am working!\"
985 #+END_SRC"
987 "* org-babel-remove-result
988 #+begin_src emacs-lisp :results code
989 (message \"I am working!\")
990 #+end_src
992 * next heading"))
994 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
995 "Test `org-babel-remove-result' with :results pp."
996 (test-ob-verify-result-and-removed-result
997 ": \"I /am/ working!\""
999 "* org-babel-remove-result
1000 #+begin_src emacs-lisp :results pp
1001 \"I /am/ working!\")
1002 #+end_src
1004 * next heading"))
1006 (ert-deftest test-ob/results-do-not-replace-code-blocks ()
1007 (org-test-with-temp-text "Block two has a space after the name.
1009 #+name: foo
1010 #+begin_src emacs-lisp
1012 #+end_src emacs-lisp
1014 #+name: foo
1015 #+begin_src emacs-lisp
1017 #+end_src
1019 #+name: foo
1020 #+begin_src emacs-lisp
1022 #+end_src
1024 #+RESULTS: foo
1025 : foo
1027 (dolist (num '(1 2 3))
1028 ;; execute the block
1029 (goto-char (point-min))
1030 (org-babel-next-src-block num) (org-babel-execute-src-block)
1031 ;; check the results
1032 (goto-char (point-max))
1033 (move-beginning-of-line 0)
1034 (should (looking-at (format ": %d" num))))))
1036 (provide 'test-ob)
1038 ;;; test-ob ends here