Update copyright years again.
[org-mode.git] / testing / lisp / test-ob.el
blobafc809ecba68d42236aa21dc86edff465e75a1b9
1 ;;; test-ob.el --- tests for ob.el
3 ;; Copyright (c) 2010-2014 Eric Schulte
4 ;; Authors: Eric Schulte, Martyn Jago
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/>.
21 ;;; Code:
23 (ert-deftest test-org-babel/multi-line-header-regexp ()
24 (should(equal "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
25 org-babel-multi-line-header-regexp))
26 ;;TODO can be optimised - and what about blah4 blah5 blah6?
27 (should (string-match
28 org-babel-multi-line-header-regexp
29 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
30 (should
31 (equal
32 "blah1 blah2 blah3 \t"
33 (match-string
35 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
37 ;;TODO Check - should this fail?
38 (should
39 (not (org-test-string-exact-match
40 org-babel-multi-line-header-regexp
41 " \t #+headers : blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))))
43 (ert-deftest test-org-babel/src-block-regexp ()
44 (let ((test-block
45 (concat
46 "#+begin_src language -n-r-a-b -c :argument-1 yes :argument-2 no\n"
47 "echo this is a test\n"
48 "echo Currently in ' $PWD\n"
49 "#+end_src"))
50 (language "language")
51 (flags "-n-r-a-b -c ")
52 (arguments ":argument-1 yes :argument-2 no")
53 (body "echo this is a test\necho Currently in ' $PWD\n"))
54 (should (string-match org-babel-src-block-regexp test-block))
55 (should (string-match org-babel-src-block-regexp (upcase test-block)))
56 (should (equal language (match-string 2 test-block)))
57 ;;TODO Consider refactoring
58 (should (equal flags (match-string 3 test-block)))
59 (should (equal arguments (match-string 4 test-block)))
60 (should (equal body (match-string 5 test-block)))
61 ;;no switches
62 (should (org-test-string-exact-match
63 org-babel-src-block-regexp
64 (replace-regexp-in-string flags "" test-block)))
65 ;;no header arguments
66 (should (org-test-string-exact-match
67 org-babel-src-block-regexp
68 (replace-regexp-in-string arguments "" test-block)))
69 ;; should be valid with no body
70 (should (org-test-string-exact-match
71 org-babel-src-block-regexp
72 (replace-regexp-in-string body "" test-block)))))
74 (ert-deftest test-org-babel/get-header ()
75 (should (not (org-babel-get-header
76 org-babel-default-header-args :doesnt-exist)))
77 (should(equal '((:session . "none"))
78 (org-babel-get-header
79 org-babel-default-header-args :session)))
80 (should(equal '((:session . "none"))
81 (org-babel-get-header
82 org-babel-default-header-args :session nil)))
83 (should (not (org-babel-get-header
84 org-babel-default-header-args :SESSION)))
85 (should (equal '((:tangle . "no"))
86 (org-babel-get-header
87 org-babel-default-header-args :tangle)))
88 ;; with OTHERS option
89 (should (equal org-babel-default-header-args
90 (org-babel-get-header
91 org-babel-default-header-args :doesnt-exist 'others)))
92 (should (equal org-babel-default-header-args
93 (org-babel-get-header
94 org-babel-default-header-args nil 'others)))
95 (should (null
96 (assoc :noweb
97 (org-babel-get-header
98 org-babel-default-header-args :noweb 'others)))))
100 (ert-deftest test-org-babel/default-inline-header-args ()
101 (should(equal
102 '((:session . "none") (:results . "replace") (:exports . "results"))
103 org-babel-default-inline-header-args)))
105 (ert-deftest ob-test/org-babel-combine-header-arg-lists ()
106 (let ((results (org-babel-combine-header-arg-lists
107 '((foo . :any)
108 (bar)
109 (baz . ((foo bar) (baz)))
110 (qux . ((foo bar baz qux)))
111 (quux . ((foo bar))))
112 '((bar)
113 (baz . ((baz)))
114 (quux . :any)))))
115 (dolist (pair '((foo . :any)
116 (bar)
117 (baz . ((baz)))
118 (quux . :any)
119 (qux . ((foo bar baz qux)))))
120 (should (equal (cdr pair)
121 (cdr (assoc (car pair) results)))))))
123 ;;; ob-get-src-block-info
124 (ert-deftest test-org-babel/get-src-block-info-language ()
125 (org-test-at-marker nil org-test-file-ob-anchor
126 (let ((info (org-babel-get-src-block-info)))
127 (should (string= "emacs-lisp" (nth 0 info))))))
129 (ert-deftest test-org-babel/get-src-block-info-body ()
130 (org-test-at-marker nil org-test-file-ob-anchor
131 (let ((info (org-babel-get-src-block-info)))
132 (should (string-match (regexp-quote org-test-file-ob-anchor)
133 (nth 1 info))))))
135 (ert-deftest test-org-babel/get-src-block-info-tangle ()
136 (org-test-at-marker nil org-test-file-ob-anchor
137 (let ((info (org-babel-get-src-block-info)))
138 (should (string= "no" (cdr (assoc :tangle (nth 2 info))))))))
140 (ert-deftest test-org-babel/elisp-in-header-arguments ()
141 "Test execution of elisp forms in header arguments."
142 (org-test-with-temp-text-in-file "
144 * elisp forms in header arguments
145 :PROPERTIES:
146 :var: prop = (* 7 6)
147 :END:
148 #+begin_src emacs-lisp
149 prop
150 #+end_src"
151 (goto-char (point-min))
152 (org-babel-next-src-block)
153 (let ((info (org-babel-get-src-block-info)))
154 (should (= 42 (org-babel-execute-src-block))))))
156 (ert-deftest test-org-babel/simple-named-code-block ()
157 "Test that simple named code blocks can be evaluated."
158 (org-test-with-temp-text-in-file "
160 #+name: i-have-a-name
161 #+begin_src emacs-lisp
163 #+end_src"
164 (org-babel-next-src-block 1)
165 (should (= 42 (org-babel-execute-src-block)))))
167 (ert-deftest test-org-babel/simple-variable-resolution ()
168 "Test that simple variable resolution is working."
169 (org-test-with-temp-text-in-file "
171 #+name: four
172 #+begin_src emacs-lisp
173 (list 1 2 3 4)
174 #+end_src
176 #+begin_src emacs-lisp :var four=four
177 (length four)
178 #+end_src"
180 (org-babel-next-src-block 2)
181 (should (= 4 (org-babel-execute-src-block)))
182 (forward-line 5)
183 (should (string= ": 4" (buffer-substring
184 (point-at-bol)
185 (point-at-eol))))))
187 (ert-deftest test-org-babel/multi-line-header-arguments ()
188 "Test that multi-line header arguments and can be read."
189 (org-test-with-temp-text-in-file "
191 #+headers: :var letters='(a b c d e f g)
192 #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
193 (require 'cl)
194 (defalias 'my-map (if (org-version-check \"24.2.50\" \"cl\" :predicate)
195 'cl-map
196 'map))
197 (my-map 'list #'list numbers letters)
198 #+end_src"
200 (org-babel-next-src-block)
201 (let ((results (org-babel-execute-src-block)))
202 (should(equal 'a (cadr (assoc 1 results))))
203 (should(equal 'd (cadr (assoc 4 results)))))))
205 (ert-deftest test-org-babel/parse-header-args ()
206 (org-test-with-temp-text-in-file "
208 #+begin_src example-lang :session :results output :var num=9
209 the body
210 #+end_src"
212 (org-babel-next-src-block)
213 (let* ((info (org-babel-get-src-block-info))
214 (params (nth 2 info)))
215 (message "%S" params)
216 (should (equal "example-lang" (nth 0 info)))
217 (should (string= "the body" (org-babel-trim (nth 1 info))))
218 (should-not (member '(:session\ \ \ \ ) params))
219 (should (equal '(:session) (assoc :session params)))
220 (should (equal '(:result-type . output) (assoc :result-type params)))
221 (should (equal '(num . 9) (cdr (assoc :var params)))))))
223 (ert-deftest test-org-babel/parse-header-args2 ()
224 (org-test-with-temp-text-in-file "
226 * resolving sub-trees as references
228 #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
229 (length text)
230 #+end_src
232 #+begin_src org :noweb yes
233 <<simple-subtree>>
234 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
235 #+end_src
237 ** simple subtree with custom ID
238 :PROPERTIES:
239 :CUSTOM_ID: simple-subtree
240 :END:
241 this is simple"
243 (should (string-match (regexp-quote "this is simple")
244 (org-babel-ref-resolve "simple-subtree")))
245 (org-babel-next-src-block)
246 (should (= 14 (org-babel-execute-src-block)))))
248 (ert-deftest test-org-babel/inline-src-blocks ()
249 (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
250 (macrolet ((at-next (&rest body)
251 `(progn
252 (move-end-of-line 1)
253 (re-search-forward org-babel-inline-src-block-regexp nil t)
254 (goto-char (match-beginning 1))
255 (save-match-data ,@body))))
256 (at-next (should (equal 1 (org-babel-execute-src-block))))
257 (at-next (should (equal 2 (org-babel-execute-src-block))))
258 (at-next (should (equal 3 (org-babel-execute-src-block)))))))
260 (ert-deftest test-org-babel/org-babel-get-inline-src-block-matches ()
261 (org-test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74"
262 (let ((test-point (point)))
263 (should (fboundp 'org-babel-get-inline-src-block-matches))
264 (should (re-search-forward "src_" nil t)) ;; 1
265 (should (org-babel-get-inline-src-block-matches))
266 (should (re-search-forward "}" nil (point-at-bol))) ;; 1
267 (should-not (org-babel-get-inline-src-block-matches))
268 (should (re-search-forward "in" nil t)) ;; 2
269 (should-not (org-babel-get-inline-src-block-matches))
270 (should (re-search-forward "echo" nil t)) ;; 2
271 (should (org-babel-get-inline-src-block-matches))
272 (should (re-search-forward "blocks" nil t)) ;; 3
273 (backward-char 8) ;; 3
274 (should (org-babel-get-inline-src-block-matches))
275 (forward-char 1) ;;3
276 (should-not (org-babel-get-inline-src-block-matches))
277 (should (re-search-forward ":results" nil t)) ;; 4
278 (should (org-babel-get-inline-src-block-matches))
279 (end-of-line)
280 (should-not (org-babel-get-inline-src-block-matches)))))
282 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-1 ()
283 (let ((test-line "src_sh{echo 1}"))
284 ;; src_ at bol line 1...
285 (org-test-with-temp-text
286 test-line
287 (goto-char (point-min)) (org-ctrl-c-ctrl-c)
288 (should (string=
289 (concat test-line " =1=")
290 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
291 (forward-char) (org-ctrl-c-ctrl-c)
292 (should (string=
293 (concat test-line " =1= =1=")
294 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
295 (re-search-forward "1}")
296 (should-error (org-ctrl-c-ctrl-c))
297 (backward-char) ;; last char of block body
298 (org-ctrl-c-ctrl-c)
299 (should (string=
300 (concat test-line " =1= =1= =1=")
301 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
302 ;; src_ follows space line 1...
303 (let ((test-line " src_emacs-lisp{ 1 }"))
304 (org-test-with-temp-text
305 test-line
306 (should-error (org-ctrl-c-ctrl-c))
307 (forward-char) (org-ctrl-c-ctrl-c)
308 (should (string=
309 (concat test-line " =1=")
310 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
311 (re-search-forward "{ 1 ") (org-ctrl-c-ctrl-c)
312 (should (string=
313 (concat test-line " =1= =1=")
314 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
315 (forward-char)
316 (should-error (org-ctrl-c-ctrl-c))))))
318 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-2 ()
319 ;; src_ at bol line 2...
320 (let ((test-line " src_emacs-lisp{ \"x\" }"))
321 (org-test-with-temp-text
322 (concat "\n" test-line)
323 (should-error (org-ctrl-c-ctrl-c))
324 (goto-char (point-min))
325 (should-error (org-ctrl-c-ctrl-c))
326 (forward-line)
327 (should-error (org-ctrl-c-ctrl-c))
328 (forward-char) (org-ctrl-c-ctrl-c)
329 (should (string=
330 (concat test-line " =x=")
331 (buffer-substring-no-properties
332 (point-at-bol) (point-at-eol))))))
334 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }"))
335 (org-test-with-temp-text
336 test-line
337 (goto-char (point-max))
338 (insert (concat "\n" test-line " end"))
339 (re-search-backward "src") (org-ctrl-c-ctrl-c)
340 (should (string=
341 (concat test-line " =y= end")
342 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
343 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
344 (should (string=
345 (concat test-line " =y= =y= end")
346 (buffer-substring-no-properties (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-manual-results-replace ()
351 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }"))
352 (org-test-with-temp-text
353 (concat "\n" test-line)
354 (should-error (org-ctrl-c-ctrl-c))
355 (goto-char (point-max))
356 (should-error (org-ctrl-c-ctrl-c))
357 (beginning-of-line)
358 (should-error (org-ctrl-c-ctrl-c))
359 (forward-char) (org-ctrl-c-ctrl-c)
360 (should (string=
361 (concat test-line " =x=")
362 (buffer-substring-no-properties
363 (point-at-bol) (point-at-eol))))))
365 (let ((test-line (concat " Some text prior to block "
366 "src_emacs-lisp[:results replace]{ \"y\" }")))
367 (org-test-with-temp-text test-line
368 (goto-char (point-max))
369 (insert (concat "\n" test-line " end"))
370 (re-search-backward "src") (org-ctrl-c-ctrl-c)
371 (should (string=
372 (concat test-line " =y= end")
373 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
374 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
375 (should (string=
376 (concat test-line " =y= =y= end")
377 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
378 (forward-char)
379 (should-error (org-ctrl-c-ctrl-c)))))
381 (ert-deftest test-org-babel/inline-src_blk-results-silent ()
382 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
383 (org-test-with-temp-text test-line
384 (org-ctrl-c-ctrl-c)
385 (should (string= test-line
386 (buffer-substring-no-properties
387 (point-at-bol) (point-at-eol))))
388 (end-of-buffer)
389 (should-error (org-ctrl-c-ctrl-c))))
390 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
391 "[ :results silent ]{ \"y\" }")))
392 (org-test-with-temp-text
393 test-line
394 (goto-char (point-max))
395 (insert (concat "\n" test-line " end"))
396 (re-search-backward "src_") (org-ctrl-c-ctrl-c)
397 (should (string= (concat test-line " end")
398 (buffer-substring-no-properties
399 (point-at-bol) (point-at-eol))))
400 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
401 (should (string= (concat test-line " end")
402 (buffer-substring-no-properties
403 (point-at-bol) (point-at-eol))))
404 (forward-char)
405 (should-error (org-ctrl-c-ctrl-c)))))
407 (ert-deftest test-org-babel/inline-src_blk-results-raw ()
408 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
409 (org-test-with-temp-text test-line
410 (org-ctrl-c-ctrl-c)
411 (should (string= (concat test-line " x")
412 (buffer-string)))))
413 (let ((test-line (concat " Some text prior to block "
414 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
415 (org-test-with-temp-text (concat test-line " end")
416 (re-search-forward "src_") (org-ctrl-c-ctrl-c)
417 (should (string= (concat test-line " the end")
418 (buffer-substring-no-properties
419 (point-at-bol) (point-at-eol))))
420 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
421 (should (string= (concat test-line " the the end")
422 (buffer-substring-no-properties
423 (point-at-bol) (point-at-eol))))
424 (forward-char)
425 (should-error (org-ctrl-c-ctrl-c)))))
427 (ert-deftest test-org-babel/inline-src_blk-results-file ()
428 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
429 (org-test-with-temp-text
430 test-line
431 (org-ctrl-c-ctrl-c)
432 (should (string= (concat test-line " [[file:~/test-file]]")
433 (buffer-substring-no-properties
434 (point-min) (point-max)))))))
436 (ert-deftest test-org-babel/inline-src_blk-results-scalar ()
437 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }"))
438 (org-test-with-temp-text
439 test-line
440 (org-ctrl-c-ctrl-c)
441 (should (string= (concat test-line " =\"x\"=")
442 (buffer-substring-no-properties
443 (point-min) (point-max)))))))
445 (ert-deftest test-org-babel/inline-src_blk-results-verbatim ()
446 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
447 (org-test-with-temp-text
448 test-line
449 (org-ctrl-c-ctrl-c)
450 (should (string= (concat test-line " =\"x\"=")
451 (buffer-substring-no-properties
452 (point-min) (point-max)))))))
454 (ert-deftest test-org-babel/combining-scalar-and-raw-result-types ()
455 (org-test-with-temp-text-in-file "
457 #+begin_src sh :results scalar
458 echo \"[[file:./cv.cls]]\"
459 #+end_src
461 #+name:
462 : [[file:./cv.cls]]
464 #+begin_src sh :results raw scalar
465 echo \"[[file:./cv.cls]]\"
466 #+end_src
468 (flet ((next-result ()
469 (org-babel-next-src-block)
470 (org-babel-execute-src-block)
471 (goto-char (org-babel-where-is-src-block-result))
472 (forward-line 1)))
473 (goto-char (point-min))
474 (next-result)
475 (should (org-babel-in-example-or-verbatim))
476 (next-result)
477 (should (not (org-babel-in-example-or-verbatim))))))
479 (ert-deftest test-org-babel/no-defaut-value-for-var ()
480 "Test that the absence of a default value for a variable DOES THROW
481 a proper error."
482 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
483 (org-babel-next-src-block)
484 (let ((err
485 (should-error (org-babel-execute-src-block) :type 'error)))
486 (should
487 (equal
488 '(error
489 "Variable \"x\" must be assigned a default value")
490 err)))))
492 (ert-deftest test-org-babel/just-one-results-block ()
493 "Test that evaluating two times the same code block does not result in a
494 duplicate results block."
495 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
496 (org-babel-execute-src-block)
497 (org-babel-execute-src-block) ; second code block execution
498 (should (search-forward "Hello")) ; the string inside the source code block
499 (should (search-forward "Hello")) ; the same string in the results block
500 (should-error (search-forward "Hello"))))
502 (ert-deftest test-org-babel/nested-code-block ()
503 "Test nested code blocks inside code blocks don't cause problems."
504 (org-test-with-temp-text "#+begin_src org :results silent
505 ,#+begin_src emacs-lisp
506 , 'foo
507 ,#+end_src
508 #+end_src"
509 (should (string= (org-babel-execute-src-block)
510 "#+begin_src emacs-lisp\n 'foo\n#+end_src"))))
512 (ert-deftest test-org-babel/partial-nested-code-block ()
513 "Test nested code blocks inside code blocks don't cause problems."
514 (org-test-with-temp-text "#+begin_src org :results silent
515 ,#+begin_src emacs-lisp
516 #+end_src"
517 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
519 (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
520 (org-test-with-temp-text "#+NAME: foo
521 #+BEGIN_SRC emacs-lisp
522 'foo
523 #+END_SRC\n"
524 (org-babel-next-src-block 1)
525 (should (eq 'foo (org-babel-execute-src-block)))
526 (goto-char (point-min))
527 (org-babel-next-src-block 1)
528 (should (looking-at org-babel-src-block-regexp))))
530 (ert-deftest test-ob/catches-all-references ()
531 (org-test-with-temp-text "
532 #+NAME: literal-example
533 #+BEGIN_EXAMPLE
534 A literal example
535 on two lines
536 #+END_EXAMPLE
538 #+NAME: read-literal-example
539 #+BEGIN_SRC emacs-lisp :var x=literal-example
540 (concatenate 'string x \" for me.\")
541 #+END_SRC"
542 (org-babel-next-src-block 1)
543 (should (string= (org-babel-execute-src-block)
544 "A literal example\non two lines for me."))))
546 (ert-deftest test-ob/resolve-code-blocks-before-data-blocks ()
547 (org-test-with-temp-text "
548 #+name: foo
549 : bar
551 #+name: foo
552 #+begin_src emacs-lisp
553 \"baz\"
554 #+end_src
556 #+begin_src emacs-lisp :var foo=foo
558 #+end_src"
559 (org-babel-next-src-block 2)
560 (should (string= (org-babel-execute-src-block) "baz"))))
562 (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
563 (org-test-with-temp-text "
564 #+tblname: base_plus
565 | 1 |
566 | 2 |
568 #+tblname: base
569 | 3 |
570 | 4 |
572 #+begin_src emacs-lisp :var x=base
574 #+end_src"
575 (org-babel-next-src-block 1)
576 (should (equal (org-babel-execute-src-block) '((3) (4))))))
578 (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
579 (org-test-with-temp-text "
580 #+name: base_plus
581 #+begin_src emacs-lisp
582 'bar
583 #+end_src
585 #+name: base
586 #+begin_src emacs-lisp
587 'foo
588 #+end_src
590 #+begin_src emacs-lisp :var x=base
592 #+end_src"
593 (org-babel-next-src-block 3)
594 (should (equal (org-babel-execute-src-block) "foo"))))
596 (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
597 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
598 (+ a b c d)
599 #+end_src
601 (should (= 10 (org-babel-execute-src-block)))))
603 (ert-deftest test-ob/org-babel-update-intermediate ()
604 (org-test-with-temp-text "#+name: foo
605 #+begin_src emacs-lisp
607 #+end_src
609 #+results: foo
612 #+begin_src emacs-lisp :var it=foo
613 (+ it 1)
614 #+end_src"
615 (let ((org-babel-update-intermediate nil))
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 ": 4")))
622 (let ((org-babel-update-intermediate t))
623 (goto-char (point-min))
624 (org-babel-next-src-block 2)
625 (should (= 3 (org-babel-execute-src-block)))
626 (goto-char (point-min))
627 (forward-line 6)
628 (should (looking-at ": 2")))))
630 (ert-deftest test-ob/eval-header-argument ()
631 (flet ((check-eval (eval runp)
632 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
633 (setq foo :evald)
634 #+end_src" eval)
635 (let ((foo :not-run))
636 (if runp
637 (progn (should (org-babel-execute-src-block))
638 (should (eq foo :evald)))
639 (progn (should-not (org-babel-execute-src-block))
640 (should-not (eq foo :evald))))))))
641 (check-eval "never" nil)
642 (check-eval "no" nil)
643 (check-eval "never-export" t)
644 (check-eval "no-export" t)
645 (let ((org-current-export-file "something"))
646 (check-eval "never" nil)
647 (check-eval "no" nil)
648 (check-eval "never-export" nil)
649 (check-eval "no-export" nil))))
651 (ert-deftest test-ob/noweb-expansion-1 ()
652 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
653 <<foo>>
654 #+end_src
656 #+name: foo
657 #+begin_src sh
659 #+end_src"
660 (should (string= (org-babel-expand-noweb-references) "bar"))))
662 (ert-deftest test-ob/noweb-expansion-2 ()
663 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
664 <<foo>>
665 #+end_src
667 #+name: foo
668 #+begin_src sh :noweb-sep \"\"
670 #+end_src
672 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
674 #+end_src"
675 (should (string= (org-babel-expand-noweb-references) "barbaz"))))
677 (ert-deftest test-ob/splitting-variable-lists-in-references ()
678 (org-test-with-temp-text ""
679 (should (= 1 (length (org-babel-ref-split-args
680 "a=\"this, no work\""))))
681 (should (= 2 (length (org-babel-ref-split-args
682 "a=\"this, no work\", b=1"))))))
684 (ert-deftest test-ob/org-babel-balanced-split ()
685 (should (equal
686 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
687 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
688 '((32 9) . 58)))))
690 (ert-deftest test-ob/commented-last-block-line-no-var ()
691 (org-test-with-temp-text-in-file "
692 #+begin_src emacs-lisp
694 #+end_src"
695 (org-babel-next-src-block)
696 (org-ctrl-c-ctrl-c)
697 (should (re-search-forward "\\#\\+results:" nil t))
698 (forward-line)
699 (should
700 (string=
702 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
703 (org-test-with-temp-text-in-file "
704 #+begin_src emacs-lisp
705 \"some text\";;
706 #+end_src"
707 (org-babel-next-src-block)
708 (org-ctrl-c-ctrl-c)
709 (should (re-search-forward "\\#\\+results:" nil t))
710 (forward-line)
711 (should
712 (string=
713 ": some text"
714 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
716 (ert-deftest test-ob/commented-last-block-line-with-var ()
717 (org-test-with-temp-text-in-file "
718 #+begin_src emacs-lisp :var a=1
720 #+end_src"
721 (org-babel-next-src-block)
722 (org-ctrl-c-ctrl-c)
723 (re-search-forward "\\#\\+results:" nil t)
724 (forward-line)
725 (should (string=
727 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
728 (org-test-with-temp-text-in-file "
729 #+begin_src emacs-lisp :var a=2
731 #+end_src"
732 (org-babel-next-src-block)
733 (org-ctrl-c-ctrl-c)
734 (re-search-forward "\\#\\+results:" nil t)
735 (forward-line)
736 (should (string=
737 ": 2"
738 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
740 (defun test-ob-verify-result-and-removed-result (result buffer-text)
741 "Test helper function to test `org-babel-remove-result'.
742 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
743 and the result of execution is verified against RESULT.
745 The block is actually executed /twice/ to ensure result
746 replacement happens correctly."
747 (org-test-with-temp-text
748 buffer-text
749 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
750 (should (re-search-forward "\\#\\+results:" nil t))
751 (forward-line)
752 (should (string= result
753 (buffer-substring-no-properties
754 (point-at-bol)
755 (- (point-max) 16))))
756 (org-babel-previous-src-block) (org-babel-remove-result)
757 (should (string= buffer-text
758 (buffer-substring-no-properties
759 (point-min) (point-max))))))
761 (ert-deftest test-ob/org-babel-remove-result--results-default ()
762 "Test `org-babel-remove-result' with default :results."
763 (mapcar (lambda (language)
764 (test-ob-verify-result-and-removed-result
765 "\n"
766 (concat
767 "* org-babel-remove-result
768 #+begin_src " language "
769 #+end_src
771 * next heading")))
772 '("sh" "emacs-lisp")))
774 (ert-deftest test-ob/org-babel-remove-result--results-list ()
775 "Test `org-babel-remove-result' with :results list."
776 (test-ob-verify-result-and-removed-result
777 "- 1
780 - (quote (4 5))"
782 "* org-babel-remove-result
783 #+begin_src emacs-lisp :results list
784 '(1 2 3 '(4 5))
785 #+end_src
787 * next heading"))
789 (ert-deftest test-ob/org-babel-results-indented-wrap ()
790 "Ensure that wrapped results are inserted correction when indented.
791 If not inserted correctly then the second evaluation will fail
792 trying to find the :END: marker."
793 (org-test-with-temp-text
794 "- indented
795 #+begin_src sh :results file wrap
796 echo test.txt
797 #+end_src"
798 (org-babel-next-src-block 1)
799 (org-babel-execute-src-block)
800 (org-babel-execute-src-block)))
802 (ert-deftest test-ob/file-desc-header-argument ()
803 "Test that the :file-desc header argument is used."
804 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
805 \"foo\"
806 #+end_src
808 #+begin_src emacs-lisp :results file :file-desc
809 \"foo\"
810 #+end_src"
811 (org-babel-execute-src-block)
812 (org-babel-next-src-block 1)
813 (org-babel-execute-src-block)
814 (goto-char (point-min))
815 (should (search-forward "[[file:foo][bar]]" nil t))
816 (should (search-forward "[[file:foo][foo]]" nil t))))
818 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
819 "Test `org-babel-remove-result' with :results pp."
820 (test-ob-verify-result-and-removed-result
821 ": \"I /am/ working!\""
823 "* org-babel-remove-result
824 #+begin_src emacs-lisp :results pp
825 \"I /am/ working!\")
826 #+end_src
828 * next heading"))
830 (ert-deftest test-org-babel/inline-src_blk-preceded-punct-preceded-by-point ()
831 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
832 (org-test-with-temp-text
833 test-line
834 (forward-char 1)
835 (org-ctrl-c-ctrl-c)
836 (should (re-search-forward "=\"x\"=" nil t))
837 (forward-line))))
839 (ert-deftest test-ob/commented-last-block-line-with-var ()
840 (org-test-with-temp-text-in-file "
841 #+begin_src emacs-lisp :var a=1
843 #+end_src"
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 (org-babel-next-src-block)
856 (org-ctrl-c-ctrl-c)
857 (re-search-forward "\\#\\+results:" nil t)
858 (forward-line)
859 (should (string=
860 ": 2"
861 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
863 (defun test-ob-verify-result-and-removed-result (result buffer-text)
864 "Test helper function to test `org-babel-remove-result'.
865 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
866 and the result of execution is verified against RESULT.
868 The block is actually executed /twice/ to ensure result
869 replacement happens correctly."
870 (org-test-with-temp-text
871 buffer-text
872 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
873 (should (re-search-forward "\\#\\+results:" nil t))
874 (forward-line)
875 (should (string= result
876 (buffer-substring-no-properties
877 (point-at-bol)
878 (- (point-max) 16))))
879 (org-babel-previous-src-block) (org-babel-remove-result)
880 (should (string= buffer-text
881 (buffer-substring-no-properties
882 (point-min) (point-max))))))
884 (ert-deftest test-ob/org-babel-remove-result--results-default ()
885 "Test `org-babel-remove-result' with default :results."
886 (mapcar (lambda (language)
887 (test-ob-verify-result-and-removed-result
888 "\n"
889 (concat
890 "* org-babel-remove-result
891 #+begin_src " language "
892 #+end_src
894 * next heading")))
895 '("sh" "emacs-lisp")))
897 (ert-deftest test-ob/org-babel-remove-result--results-list ()
898 "Test `org-babel-remove-result' with :results list."
899 (test-ob-verify-result-and-removed-result
900 "- 1
903 - (quote (4 5))"
905 "* org-babel-remove-result
906 #+begin_src emacs-lisp :results list
907 '(1 2 3 '(4 5))
908 #+end_src
910 * next heading"))
912 (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
913 "Test `org-babel-remove-result' with :results wrap."
914 (test-ob-verify-result-and-removed-result
915 ":RESULTS:
916 hello there
917 :END:"
919 "* org-babel-remove-result
921 #+begin_src emacs-lisp :results wrap
922 \"hello there\"
923 #+end_src
925 * next heading"))
927 (ert-deftest test-ob/org-babel-remove-result--results-org ()
928 "Test `org-babel-remove-result' with :results org."
929 (test-ob-verify-result-and-removed-result
930 "#+BEGIN_SRC org
931 ,* heading
932 ,** subheading
933 content
934 #+END_SRC"
936 "* org-babel-remove-result
937 #+begin_src emacs-lisp :results org
938 \"* heading
939 ** subheading
940 content\"
941 #+end_src
943 * next heading"))
945 (ert-deftest test-ob/org-babel-remove-result--results-html ()
946 "Test `org-babel-remove-result' with :results html."
947 (test-ob-verify-result-and-removed-result
948 "#+BEGIN_HTML
949 <head><body></body></head>
950 #+END_HTML"
952 "* org-babel-remove-result
953 #+begin_src emacs-lisp :results html
954 \"<head><body></body></head>\"
955 #+end_src
957 * next heading"))
959 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
960 "Test `org-babel-remove-result' with :results latex."
961 (test-ob-verify-result-and-removed-result
962 "#+BEGIN_LaTeX
963 Line 1
964 Line 2
965 Line 3
966 #+END_LaTeX"
968 "* org-babel-remove-result
969 #+begin_src emacs-lisp :results latex
970 \"Line 1
971 Line 2
972 Line 3\"
973 #+end_src
975 * next heading"))
977 (ert-deftest test-ob/org-babel-remove-result--results-code ()
978 "Test `org-babel-remove-result' with :results code."
980 (test-ob-verify-result-and-removed-result
981 "#+BEGIN_SRC emacs-lisp
982 \"I am working!\"
983 #+END_SRC"
985 "* org-babel-remove-result
986 #+begin_src emacs-lisp :results code
987 (message \"I am working!\")
988 #+end_src
990 * next heading"))
992 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
993 "Test `org-babel-remove-result' with :results pp."
994 (test-ob-verify-result-and-removed-result
995 ": \"I /am/ working!\""
997 "* org-babel-remove-result
998 #+begin_src emacs-lisp :results pp
999 \"I /am/ working!\")
1000 #+end_src
1002 * next heading"))
1004 (ert-deftest test-ob/results-do-not-replace-code-blocks ()
1005 (org-test-with-temp-text "Block two has a space after the name.
1007 #+name: foo
1008 #+begin_src emacs-lisp
1010 #+end_src emacs-lisp
1012 #+name: foo
1013 #+begin_src emacs-lisp
1015 #+end_src
1017 #+name: foo
1018 #+begin_src emacs-lisp
1020 #+end_src
1022 #+RESULTS: foo
1023 : foo
1025 (dolist (num '(1 2 3))
1026 ;; execute the block
1027 (goto-char (point-min))
1028 (org-babel-next-src-block num) (org-babel-execute-src-block)
1029 ;; check the results
1030 (goto-char (point-max))
1031 (move-beginning-of-line 0)
1032 (should (looking-at (format ": %d" num))))))
1034 (ert-deftest test-ob/blocks-with-spaces ()
1035 "Test expansion of blocks followed by blank lines."
1036 (should
1037 (equal "#+BEGIN_SRC emacs-lisp
1038 \(+ 1 2)
1039 #+END_SRC
1041 #+RESULTS:
1042 : 3\n\n\n"
1043 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1044 \(+ 1 2)
1045 #+END_SRC\n\n\n"
1046 (org-babel-execute-src-block)
1047 (buffer-string)))))
1049 (ert-deftest test-ob/results-in-narrowed-buffer ()
1050 "Test block execution in a narrowed buffer."
1051 ;; If results don't exist, they should be inserted in visible part
1052 ;; of the buffer.
1053 (should
1054 (equal
1055 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1056 (org-test-with-temp-text
1057 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1058 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1059 (org-babel-execute-src-block)
1060 (org-trim (buffer-string)))))
1061 (should
1062 (equal
1063 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1064 (org-test-with-temp-text
1065 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1066 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1067 (org-babel-execute-src-block)
1068 (org-trim (buffer-string)))))
1069 ;; Results in visible part of buffer, should be updated here.
1070 (should
1071 (equal
1072 "#+NAME: test
1073 #+BEGIN_SRC emacs-lisp
1074 \(+ 1 2)
1075 #+END_SRC
1077 #+RESULTS: test
1078 : 3"
1079 (org-test-with-temp-text
1080 "#+NAME: test
1081 #+BEGIN_SRC emacs-lisp
1082 \(+ 1 2)
1083 #+END_SRC
1085 #+RESULTS: test
1088 Paragraph"
1089 (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
1090 (org-babel-execute-src-block)
1091 (org-trim (buffer-string)))))
1092 ;; Results in invisible part of buffer, should be updated there.
1093 (org-test-with-temp-text
1094 "#+NAME: test
1095 #+BEGIN_SRC emacs-lisp
1096 \(+ 1 2)
1097 #+END_SRC
1099 #+RESULTS: test
1102 Paragraph"
1103 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1104 (org-babel-execute-src-block)
1105 (should-not (re-search-forward "^#\\+RESULTS:" nil t))
1106 (widen)
1107 (should (should (re-search-forward "^: 3" nil t)))))
1109 (ert-deftest test-ob/specific-colnames ()
1110 "Test passing specific column names."
1111 (should
1112 (equal "#+name: input-table
1113 | id | var1 |
1114 |----+------|
1115 | 1 | bar |
1116 | 2 | baz |
1118 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1119 echo \"$data\"
1120 #+end_src
1122 #+RESULTS:
1123 | Rev | Author |
1124 |-----+--------|
1125 | 1 | bar |
1126 | 2 | baz |
1129 (org-test-with-temp-text
1130 "#+name: input-table
1131 | id | var1 |
1132 |----+------|
1133 | 1 | bar |
1134 | 2 | baz |
1136 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1137 echo \"$data\"
1138 #+end_src
1140 ;; we should find a code block
1141 (should (re-search-forward org-babel-src-block-regexp nil t))
1142 (goto-char (match-beginning 0))
1143 ;; now that we've located the code block, it may be evaluated
1144 (org-babel-execute-src-block)
1145 (buffer-string)))))
1147 (ert-deftest test-ob/location-of-header-arg-eval ()
1148 "Test location of header argument evaluation."
1149 (org-test-with-temp-text "
1150 #+name: top-block
1151 #+begin_src emacs-lisp :var pt=(point)
1153 #+end_src
1155 #+name: bottom-block
1156 #+begin_src emacs-lisp :var pt=top-block()
1158 #+end_src
1160 ;; the value of the second block should be greater than the first
1161 (should
1162 (< (progn (re-search-forward org-babel-src-block-regexp nil t)
1163 (goto-char (match-beginning 0))
1164 (prog1 (save-match-data (org-babel-execute-src-block))
1165 (goto-char (match-end 0))))
1166 (progn (re-search-forward org-babel-src-block-regexp nil t)
1167 (goto-char (match-beginning 0))
1168 (org-babel-execute-src-block))))))
1170 (ert-deftest test-ob/preserve-results-indentation ()
1171 "Preserve indentation when executing a src block."
1172 (should
1173 (equal '(2 2)
1174 (org-test-with-temp-text
1175 " #+begin_src emacs-lisp\n (+ 1 1)\n #+end_src"
1176 (org-babel-execute-src-block)
1177 (buffer-string)
1178 (let ((case-fold-search t)) (search-forward "#+results:"))
1179 ;; Check if both #+RESULTS: keyword and actual results are
1180 ;; indented by 2 columns.
1181 (list (org-get-indentation)
1182 (progn (forward-line) (org-get-indentation)))))))
1184 (provide 'test-ob)
1186 ;;; test-ob ends here