fix bug in indented wrapped results insertion
[org-mode.git] / testing / lisp / test-ob.el
blob8fba2246c1835cb5c0b2b1dd34fb344b553293a9
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)))
12 load-path)))
13 (require 'org-test)
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?
20 (should (string-match
21 org-babel-multi-line-header-regexp
22 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
23 (should
24 (equal
25 "blah1 blah2 blah3 \t"
26 (match-string
28 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
30 ;;TODO Check - should this fail?
31 (should
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 ()
37 (let ((test-block
38 (concat
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"
42 "#+end_src"))
43 (language "language")
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)))
54 ;;no switches
55 (should (org-test-string-exact-match
56 org-babel-src-block-regexp
57 (replace-regexp-in-string flags "" test-block)))
58 ;;no header arguments
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"))
71 (org-babel-get-header
72 org-babel-default-header-args :session)))
73 (should(equal '((:session . "none"))
74 (org-babel-get-header
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"))
79 (org-babel-get-header
80 org-babel-default-header-args :tangle)))
81 ;; with OTHERS option
82 (should (equal org-babel-default-header-args
83 (org-babel-get-header
84 org-babel-default-header-args :doesnt-exist 'others)))
85 (should (equal org-babel-default-header-args
86 (org-babel-get-header
87 org-babel-default-header-args nil 'others)))
88 (should (null
89 (assoc :noweb
90 (org-babel-get-header
91 org-babel-default-header-args :noweb 'others)))))
93 (ert-deftest test-org-babel/default-inline-header-args ()
94 (should(equal
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)
108 (nth 1 info))))))
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-with-temp-text-in-file "
127 #+name: i-have-a-name
128 #+begin_src emacs-lisp
130 #+end_src"
132 (progn
133 (org-babel-next-src-block 1)
134 (should (= 42 (org-babel-execute-src-block))))))
136 (ert-deftest test-org-babel/simple-variable-resolution ()
137 "Test that simple variable resolution is working."
138 (org-test-at-id "f68821bc-7f49-4389-85b5-914791ee3718"
139 (org-babel-next-src-block 2)
140 (should (= 4 (org-babel-execute-src-block)))))
142 (ert-deftest test-org-babel/multi-line-header-arguments ()
143 "Test that multi-line header arguments and can be read."
144 (org-test-at-id "b77c8857-6c76-4ea9-8a61-ddc2648d96c4"
145 (org-babel-next-src-block)
146 (let ((results (org-babel-execute-src-block)))
147 (should(equal 'a (cadr (assoc 1 results))))
148 (should(equal 'd (cadr (assoc 4 results)))))))
150 (ert-deftest test-org-babel/parse-header-args ()
151 (org-test-at-id "7eb0dc6e-1c53-4275-88b3-b22f3113b9c3"
152 (org-babel-next-src-block)
153 (let* ((info (org-babel-get-src-block-info))
154 (params (nth 2 info)))
155 (message "%S" params)
156 (should(equal "example-lang" (nth 0 info)))
157 (should(string= "the body" (org-babel-trim (nth 1 info))))
158 (should-not (member '(:session\ \ \ \ ) params))
159 (should(equal '(:session) (assoc :session params)))
160 (should(equal '(:result-type . output) (assoc :result-type params)))
161 (should(equal '(num . 9) (cdr (assoc :var params)))))))
163 (ert-deftest test-org-babel/parse-header-args2 ()
164 (org-test-at-id "2409e8ba-7b5f-4678-8888-e48aa02d8cb4"
165 (should (string-match (regexp-quote "this is simple")
166 (org-babel-ref-resolve "simple-subtree")))
167 (org-babel-next-src-block)
168 (should (= 14 (org-babel-execute-src-block)))))
170 (ert-deftest test-org-babel/inline-src-blocks ()
171 (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
172 (macrolet ((at-next (&rest body)
173 `(progn
174 (move-end-of-line 1)
175 (re-search-forward org-babel-inline-src-block-regexp nil t)
176 (goto-char (match-beginning 1))
177 (save-match-data ,@body))))
178 (at-next (should (equal 1 (org-babel-execute-src-block))))
179 (at-next (should (equal 2 (org-babel-execute-src-block))))
180 (at-next (should (equal 3 (org-babel-execute-src-block)))))))
182 (ert-deftest test-org-babel/org-babel-get-inline-src-block-matches ()
183 (org-test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74"
184 (let ((test-point (point)))
185 (should (fboundp 'org-babel-get-inline-src-block-matches))
186 (should (re-search-forward "src_" nil t)) ;; 1
187 (should (org-babel-get-inline-src-block-matches))
188 (should (re-search-forward "}" nil (point-at-bol))) ;; 1
189 (should-not (org-babel-get-inline-src-block-matches))
190 (should (re-search-forward "in" nil t)) ;; 2
191 (should-not (org-babel-get-inline-src-block-matches))
192 (should (re-search-forward "echo" nil t)) ;; 2
193 (should (org-babel-get-inline-src-block-matches))
194 (should (re-search-forward "blocks" nil t)) ;; 3
195 (backward-char 8) ;; 3
196 (should (org-babel-get-inline-src-block-matches))
197 (forward-char 1) ;;3
198 (should-not (org-babel-get-inline-src-block-matches))
199 (should (re-search-forward ":results" nil t)) ;; 4
200 (should (org-babel-get-inline-src-block-matches))
201 (end-of-line)
202 (should-not (org-babel-get-inline-src-block-matches))
205 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-1 ()
206 (let ((test-line "src_sh{echo 1}"))
207 ;; src_ at bol line 1...
208 (org-test-with-temp-text
209 test-line
210 (goto-char (point-min)) (org-ctrl-c-ctrl-c)
211 (should (string=
212 (concat test-line " =1=")
213 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
214 (forward-char) (org-ctrl-c-ctrl-c)
215 (should (string=
216 (concat test-line " =1= =1=")
217 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
218 (re-search-forward "1}")
219 (should-error (org-ctrl-c-ctrl-c))
220 (backward-char) ;; last char of block body
221 (org-ctrl-c-ctrl-c)
222 (should (string=
223 (concat test-line " =1= =1= =1=")
224 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
225 ;; src_ follows space line 1...
226 (let ((test-line " src_emacs-lisp{ 1 }"))
227 (org-test-with-temp-text
228 test-line
229 (should-error (org-ctrl-c-ctrl-c))
230 (forward-char) (org-ctrl-c-ctrl-c)
231 (should (string=
232 (concat test-line " =1=")
233 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
234 (re-search-forward "{ 1 ") (org-ctrl-c-ctrl-c)
235 (should (string=
236 (concat test-line " =1= =1=")
237 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
238 (forward-char)
239 (should-error (org-ctrl-c-ctrl-c)))))
241 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-2 ()
242 ;; src_ at bol line 2...
243 (let ((test-line " src_emacs-lisp{ \"x\" }"))
244 (org-test-with-temp-text
245 (concat "\n" test-line)
246 (should-error (org-ctrl-c-ctrl-c))
247 (goto-char (point-min))
248 (should-error (org-ctrl-c-ctrl-c))
249 (forward-line)
250 (should-error (org-ctrl-c-ctrl-c))
251 (forward-char) (org-ctrl-c-ctrl-c)
252 (should (string=
253 (concat test-line " =x=")
254 (buffer-substring-no-properties
255 (point-at-bol) (point-at-eol))))))
257 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }"))
258 (org-test-with-temp-text
259 test-line
260 (goto-char (point-max))
261 (insert (concat "\n" test-line " end"))
262 (re-search-backward "src") (org-ctrl-c-ctrl-c)
263 (should (string=
264 (concat test-line " =y= end")
265 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
266 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
267 (should (string=
268 (concat test-line " =y= =y= end")
269 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
270 (forward-char)
271 (should-error (org-ctrl-c-ctrl-c))))))
273 (ert-deftest test-org-babel/inline-src_blk-manual-results-replace ()
274 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }"))
275 (org-test-with-temp-text
276 (concat "\n" test-line)
277 (should-error (org-ctrl-c-ctrl-c))
278 (goto-char (point-max))
279 (should-error (org-ctrl-c-ctrl-c))
280 (beginning-of-line)
281 (should-error (org-ctrl-c-ctrl-c))
282 (forward-char) (org-ctrl-c-ctrl-c)
283 (should (string=
284 (concat test-line " =x=")
285 (buffer-substring-no-properties
286 (point-at-bol) (point-at-eol))))))
288 (let ((test-line (concat " Some text prior to block "
289 "src_emacs-lisp[:results replace]{ \"y\" }")))
290 (org-test-with-temp-text test-line
291 (goto-char (point-max))
292 (insert (concat "\n" test-line " end"))
293 (re-search-backward "src") (org-ctrl-c-ctrl-c)
294 (should (string=
295 (concat test-line " =y= end")
296 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
297 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
298 (should (string=
299 (concat test-line " =y= =y= end")
300 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
301 (forward-char)
302 (should-error (org-ctrl-c-ctrl-c)))))
304 (ert-deftest test-org-babel/inline-src_blk-results-silent ()
305 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
306 (org-test-with-temp-text test-line
307 (org-ctrl-c-ctrl-c)
308 (should (string= test-line
309 (buffer-substring-no-properties
310 (point-at-bol) (point-at-eol))))
311 (end-of-buffer)
312 (should-error (org-ctrl-c-ctrl-c))))
313 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
314 "[ :results silent ]{ \"y\" }")))
315 (org-test-with-temp-text
316 test-line
317 (goto-char (point-max))
318 (insert (concat "\n" test-line " end"))
319 (re-search-backward "src_") (org-ctrl-c-ctrl-c)
320 (should (string= (concat test-line " end")
321 (buffer-substring-no-properties
322 (point-at-bol) (point-at-eol))))
323 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
324 (should (string= (concat test-line " end")
325 (buffer-substring-no-properties
326 (point-at-bol) (point-at-eol))))
327 (forward-char)
328 (should-error (org-ctrl-c-ctrl-c)))))
330 (ert-deftest test-org-babel/inline-src_blk-results-raw ()
331 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
332 (org-test-with-temp-text test-line
333 (org-ctrl-c-ctrl-c)
334 (should (string= (concat test-line " x")
335 (buffer-string)))))
336 (let ((test-line (concat " Some text prior to block "
337 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
338 (org-test-with-temp-text (concat test-line " end")
339 (re-search-forward "src_") (org-ctrl-c-ctrl-c)
340 (should (string= (concat test-line " the end")
341 (buffer-substring-no-properties
342 (point-at-bol) (point-at-eol))))
343 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
344 (should (string= (concat test-line " the the end")
345 (buffer-substring-no-properties
346 (point-at-bol) (point-at-eol))))
347 (forward-char)
348 (should-error (org-ctrl-c-ctrl-c)))))
350 (ert-deftest test-org-babel/inline-src_blk-results-file ()
351 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
352 (org-test-with-temp-text
353 test-line
354 (org-ctrl-c-ctrl-c)
355 (should (string= (concat test-line " [[file:~/test-file]]")
356 (buffer-substring-no-properties
357 (point-min) (point-max)))))))
359 (ert-deftest test-org-babel/inline-src_blk-results-scalar ()
360 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }"))
361 (org-test-with-temp-text
362 test-line
363 (org-ctrl-c-ctrl-c)
364 (should (string= (concat test-line " =\"x\"=")
365 (buffer-substring-no-properties
366 (point-min) (point-max)))))))
368 (ert-deftest test-org-babel/inline-src_blk-results-verbatim ()
369 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
370 (org-test-with-temp-text
371 test-line
372 (org-ctrl-c-ctrl-c)
373 (should (string= (concat test-line " =\"x\"=")
374 (buffer-substring-no-properties
375 (point-min) (point-max)))))))
377 (ert-deftest test-org-babel/combining-scalar-and-raw-result-types ()
378 (flet ((next-result ()
379 (org-babel-next-src-block)
380 (org-babel-execute-src-block)
381 (goto-char (org-babel-where-is-src-block-result))
382 (forward-line 1)))
383 (org-test-at-id "a73a2ab6-b8b2-4c0e-ae7f-23ad14eab7bc"
384 (next-result)
385 (should (org-babel-in-example-or-verbatim))
386 (next-result)
387 (should (not (org-babel-in-example-or-verbatim))))))
389 (ert-deftest test-org-babel/no-defaut-value-for-var ()
390 "Test that the absence of a default value for a variable DOES THROW
391 a proper error."
392 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
393 (org-babel-next-src-block)
394 (let ((err
395 (should-error (org-babel-execute-src-block) :type 'error)))
396 (should
397 (equal
398 '(error
399 "variable \"x\" in block \"carre\" must be assigned a default value")
400 err)))))
402 (ert-deftest test-org-babel/just-one-results-block ()
403 "Test that evaluating two times the same code block does not result in a
404 duplicate results block."
405 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
406 (org-babel-execute-src-block)
407 (org-babel-execute-src-block) ; second code block execution
408 (should (search-forward "Hello")) ; the string inside the source code block
409 (should (search-forward "Hello")) ; the same string in the results block
410 (should-error (search-forward "Hello"))))
412 (ert-deftest test-org-babel/nested-code-block ()
413 "Test nested code blocks inside code blocks don't cause problems."
414 (org-test-with-temp-text "#+begin_src org :results silent
415 ,#+begin_src emacs-lisp
416 , 'foo
417 ,#+end_src
418 #+end_src"
419 (should (string= (org-babel-execute-src-block)
420 "#+begin_src emacs-lisp\n 'foo\n#+end_src"))))
422 (ert-deftest test-org-babel/partial-nested-code-block ()
423 "Test nested code blocks inside code blocks don't cause problems."
424 (org-test-with-temp-text "#+begin_src org :results silent
425 ,#+begin_src emacs-lisp
426 #+end_src"
427 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
429 (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
430 (org-test-with-temp-text "#+NAME: foo
431 #+BEGIN_SRC emacs-lisp
432 'foo
433 #+END_SRC\n"
434 (org-babel-next-src-block 1)
435 (should (eq 'foo (org-babel-execute-src-block)))
436 (goto-char (point-min))
437 (org-babel-next-src-block 1)
438 (should (looking-at org-babel-src-block-regexp))))
440 (ert-deftest test-ob/catches-all-references ()
441 (org-test-with-temp-text "
442 #+NAME: literal-example
443 #+BEGIN_EXAMPLE
444 A literal example
445 on two lines
446 #+END_EXAMPLE
448 #+NAME: read-literal-example
449 #+BEGIN_SRC emacs-lisp :var x=literal-example
450 (concatenate 'string x \" for me.\")
451 #+END_SRC"
452 (org-babel-next-src-block 1)
453 (should (string= (org-babel-execute-src-block)
454 "A literal example\non two lines for me."))))
456 (ert-deftest test-ob/resolve-code-blocks-before-data-blocks ()
457 (org-test-with-temp-text "
458 #+name: foo
459 : bar
461 #+name: foo
462 #+begin_src emacs-lisp
463 \"baz\"
464 #+end_src
466 #+begin_src emacs-lisp :var foo=foo
468 #+end_src"
469 (org-babel-next-src-block 2)
470 (should (string= (org-babel-execute-src-block) "baz"))))
472 (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
473 (org-test-with-temp-text "
474 #+tblname: base_plus
475 | 1 |
476 | 2 |
478 #+tblname: base
479 | 3 |
480 | 4 |
482 #+begin_src emacs-lisp :var x=base
484 #+end_src"
485 (org-babel-next-src-block 1)
486 (should (equal (org-babel-execute-src-block) '((3) (4))))))
488 (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
489 (org-test-with-temp-text "
490 #+name: base_plus
491 #+begin_src emacs-lisp
492 'bar
493 #+end_src
495 #+name: base
496 #+begin_src emacs-lisp
497 'foo
498 #+end_src
500 #+begin_src emacs-lisp :var x=base
502 #+end_src"
503 (org-babel-next-src-block 3)
504 (should (equal (org-babel-execute-src-block) "foo"))))
506 (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
507 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
508 (+ a b c d)
509 #+end_src
511 (should (= 10 (org-babel-execute-src-block)))))
513 (ert-deftest test-ob/org-babel-update-intermediate ()
514 (org-test-with-temp-text "#+name: foo
515 #+begin_src emacs-lisp
517 #+end_src
519 #+results: foo
522 #+begin_src emacs-lisp :var it=foo
523 (+ it 1)
524 #+end_src"
525 (let ((org-babel-update-intermediate nil))
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))
530 (forward-line 6)
531 (should (looking-at ": 4")))
532 (let ((org-babel-update-intermediate t))
533 (goto-char (point-min))
534 (org-babel-next-src-block 2)
535 (should (= 3 (org-babel-execute-src-block)))
536 (goto-char (point-min))
537 (forward-line 6)
538 (should (looking-at ": 2")))))
540 (ert-deftest test-ob/eval-header-argument ()
541 (flet ((check-eval (eval runp)
542 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
543 (setq foo :evald)
544 #+end_src" eval)
545 (let ((foo :not-run))
546 (if runp
547 (progn (should (org-babel-execute-src-block))
548 (should (eq foo :evald)))
549 (progn (should-not (org-babel-execute-src-block))
550 (should-not (eq foo :evald))))))))
551 (check-eval "never" nil)
552 (check-eval "no" nil)
553 (check-eval "never-export" t)
554 (check-eval "no-export" t)
555 (let ((org-current-export-file "something"))
556 (check-eval "never" nil)
557 (check-eval "no" nil)
558 (check-eval "never-export" nil)
559 (check-eval "no-export" nil))))
561 (ert-deftest test-ob/noweb-expansion ()
562 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
563 <<foo>>
564 #+end_src
566 #+name: foo
567 #+begin_src sh
569 #+end_src"
570 (should (string= (org-babel-expand-noweb-references) "bar")))
571 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
572 <<foo>>
573 #+end_src
575 #+name: foo
576 #+begin_src sh :noweb-sep \"\"
578 #+end_src
580 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
582 #+end_src"
583 (should (string= (org-babel-expand-noweb-references) "barbaz"))))
585 (ert-deftest test-ob/splitting-variable-lists-in-references ()
586 (org-test-with-temp-text ""
587 (should (= 1 (length (org-babel-ref-split-args
588 "a=\"this, no work\""))))
589 (should (= 2 (length (org-babel-ref-split-args
590 "a=\"this, no work\", b=1"))))))
592 (ert-deftest test-ob/org-babel-balanced-split ()
593 (should (equal
594 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
595 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
596 '((32 9) . 58)))))
597 (ert-deftest test-ob/commented-last-block-line-no-var ()
598 (org-test-with-temp-text-in-file "
599 #+begin_src emacs-lisp
601 #+end_src"
602 (progn
603 (org-babel-next-src-block)
604 (org-ctrl-c-ctrl-c)
605 (should (re-search-forward "\\#\\+results:" nil t))
606 (forward-line)
607 (should
608 (string=
610 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
611 (org-test-with-temp-text-in-file "
612 #+begin_src emacs-lisp
613 \"some text\";;
614 #+end_src"
616 (progn
617 (org-babel-next-src-block)
618 (org-ctrl-c-ctrl-c)
619 (should (re-search-forward "\\#\\+results:" nil t))
620 (forward-line)
621 (should
622 (string=
623 ": some text"
624 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
626 (ert-deftest test-ob/commented-last-block-line-with-var ()
627 (org-test-with-temp-text-in-file "
628 #+begin_src emacs-lisp :var a=1
630 #+end_src"
631 (progn
632 (org-babel-next-src-block)
633 (org-ctrl-c-ctrl-c)
634 (re-search-forward "\\#\\+results:" nil t)
635 (forward-line)
636 (should (string=
638 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
639 (org-test-with-temp-text-in-file "
640 #+begin_src emacs-lisp :var a=2
642 #+end_src"
643 (progn
644 (org-babel-next-src-block)
645 (org-ctrl-c-ctrl-c)
646 (re-search-forward "\\#\\+results:" nil t)
647 (forward-line)
648 (should (string=
649 ": 2"
650 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
652 (defun test-ob-verify-result-and-removed-result (result buffer-text)
653 "Test helper function to test `org-babel-remove-result'.
654 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
655 and the result of execution is verified against RESULT.
657 The block is actually executed /twice/ to ensure result
658 replacement happens correctly."
659 (org-test-with-temp-text
660 buffer-text
661 (progn
662 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
663 (should (re-search-forward "\\#\\+results:" nil t))
664 (forward-line)
665 (should (string= result
666 (buffer-substring-no-properties
667 (point-at-bol)
668 (- (point-max) 16))))
669 (org-babel-previous-src-block) (org-babel-remove-result)
670 (should (string= buffer-text
671 (buffer-substring-no-properties
672 (point-min) (point-max)))))))
674 (ert-deftest test-ob/org-babel-remove-result--results-default ()
675 "Test `org-babel-remove-result' with default :results."
676 (mapcar (lambda (language)
677 (test-ob-verify-result-and-removed-result
678 "\n"
679 (concat
680 "* org-babel-remove-result
681 #+begin_src " language "
682 #+end_src
684 * next heading")))
685 '("sh" "emacs-lisp")))
687 (ert-deftest test-ob/org-babel-remove-result--results-list ()
688 "Test `org-babel-remove-result' with :results list."
689 (test-ob-verify-result-and-removed-result
690 "- 1
693 - (quote (4 5))"
695 "* org-babel-remove-result
696 #+begin_src emacs-lisp :results list
697 '(1 2 3 '(4 5))
698 #+end_src
700 * next heading"))
702 (ert-deftest test-ob/org-babel-results-indented-wrap ()
703 "Ensure that wrapped results are inserted correction when indented.
704 If not inserted correctly then the second evaluation will fail
705 trying to find the :END: marker."
706 (org-test-with-temp-text
707 "- indented
708 #+begin_src sh :results file wrap
709 echo test.txt
710 #+end_src"
711 (org-babel-next-src-block 1)
712 (org-babel-execute-src-block)
713 (org-babel-execute-src-block)))
715 (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
716 "Test `org-babel-remove-result' with :results wrap."
717 (test-ob-verify-result-and-removed-result
718 ":RESULTS:
719 hello there
720 :END:"
722 "* org-babel-remove-result
724 #+begin_src emacs-lisp :results wrap
725 \"hello there\"
726 #+end_src
728 * next heading"))
730 (ert-deftest test-ob/org-babel-remove-result--results-org ()
731 "Test `org-babel-remove-result' with :results org."
732 (test-ob-verify-result-and-removed-result
733 "#+BEGIN_ORG
734 * heading
735 ** subheading
736 content
737 #+END_ORG"
739 "* org-babel-remove-result
740 #+begin_src emacs-lisp :results org
741 \"* heading
742 ** subheading
743 content\"
744 #+end_src
746 * next heading"))
748 (ert-deftest test-ob/org-babel-remove-result--results-html ()
749 "Test `org-babel-remove-result' with :results html."
750 (test-ob-verify-result-and-removed-result
751 "#+BEGIN_HTML
752 <head><body></body></head>
753 #+END_HTML"
755 "* org-babel-remove-result
756 #+begin_src emacs-lisp :results html
757 \"<head><body></body></head>\"
758 #+end_src
760 * next heading"))
762 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
763 "Test `org-babel-remove-result' with :results latex."
764 (test-ob-verify-result-and-removed-result
765 "#+BEGIN_LaTeX
766 Line 1
767 Line 2
768 Line 3
769 #+END_LaTeX"
771 "* org-babel-remove-result
772 #+begin_src emacs-lisp :results latex
773 \"Line 1
774 Line 2
775 Line 3\"
776 #+end_src
778 * next heading"))
780 (ert-deftest test-ob/org-babel-remove-result--results-code ()
781 "Test `org-babel-remove-result' with :results code."
783 (test-ob-verify-result-and-removed-result
784 "#+BEGIN_SRC emacs-lisp
785 \"I am working!\"
786 #+END_SRC"
788 "* org-babel-remove-result
789 #+begin_src emacs-lisp :results code
790 (message \"I am working!\")
791 #+end_src
793 * next heading"))
795 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
796 "Test `org-babel-remove-result' with :results pp."
797 (test-ob-verify-result-and-removed-result
798 ": \"I /am/ working!\""
800 "* org-babel-remove-result
801 #+begin_src emacs-lisp :results pp
802 \"I /am/ working!\")
803 #+end_src
805 * next heading"))
807 (provide 'test-ob)
809 ;;; test-ob ends here