Merge branch 'maint'
[org-mode.git] / testing / lisp / test-ob.el
blobd7ef41a2b104e5c5d70840b2c0b1cef0501e1427
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")
103 (:results . "replace")
104 (:exports . "results")
105 (:hlines . "yes"))
106 org-babel-default-inline-header-args)))
108 (ert-deftest ob-test/org-babel-combine-header-arg-lists ()
109 (let ((results (org-babel-combine-header-arg-lists
110 '((foo . :any)
111 (bar)
112 (baz . ((foo bar) (baz)))
113 (qux . ((foo bar baz qux)))
114 (quux . ((foo bar))))
115 '((bar)
116 (baz . ((baz)))
117 (quux . :any)))))
118 (dolist (pair '((foo . :any)
119 (bar)
120 (baz . ((baz)))
121 (quux . :any)
122 (qux . ((foo bar baz qux)))))
123 (should (equal (cdr pair)
124 (cdr (assoc (car pair) results)))))))
126 ;;; ob-get-src-block-info
127 (ert-deftest test-org-babel/get-src-block-info-language ()
128 (org-test-at-marker nil org-test-file-ob-anchor
129 (let ((info (org-babel-get-src-block-info)))
130 (should (string= "emacs-lisp" (nth 0 info))))))
132 (ert-deftest test-org-babel/get-src-block-info-body ()
133 (org-test-at-marker nil org-test-file-ob-anchor
134 (let ((info (org-babel-get-src-block-info)))
135 (should (string-match (regexp-quote org-test-file-ob-anchor)
136 (nth 1 info))))))
138 (ert-deftest test-org-babel/get-src-block-info-tangle ()
139 (org-test-at-marker nil org-test-file-ob-anchor
140 (let ((info (org-babel-get-src-block-info)))
141 (should (string= "no" (cdr (assoc :tangle (nth 2 info))))))))
143 (ert-deftest test-org-babel/elisp-in-header-arguments ()
144 "Test execution of elisp forms in header arguments."
145 (org-test-with-temp-text-in-file "
147 * elisp forms in header arguments
148 :PROPERTIES:
149 :var: prop = (* 7 6)
150 :END:
151 #+begin_src emacs-lisp
152 prop
153 #+end_src"
154 (goto-char (point-min))
155 (org-babel-next-src-block)
156 (let ((info (org-babel-get-src-block-info)))
157 (should (= 42 (org-babel-execute-src-block))))))
159 (ert-deftest test-org-babel/simple-named-code-block ()
160 "Test that simple named code blocks can be evaluated."
161 (org-test-with-temp-text-in-file "
163 #+name: i-have-a-name
164 #+begin_src emacs-lisp
166 #+end_src"
167 (org-babel-next-src-block 1)
168 (should (= 42 (org-babel-execute-src-block)))))
170 (ert-deftest test-org-babel/simple-variable-resolution ()
171 "Test that simple variable resolution is working."
172 (org-test-with-temp-text-in-file "
174 #+name: four
175 #+begin_src emacs-lisp
176 (list 1 2 3 4)
177 #+end_src
179 #+begin_src emacs-lisp :var four=four
180 (length four)
181 #+end_src"
183 (org-babel-next-src-block 2)
184 (should (= 4 (org-babel-execute-src-block)))
185 (forward-line 5)
186 (should (string= ": 4" (buffer-substring
187 (point-at-bol)
188 (point-at-eol))))))
190 (ert-deftest test-org-babel/multi-line-header-arguments ()
191 "Test that multi-line header arguments and can be read."
192 (org-test-with-temp-text-in-file "
194 #+headers: :var letters='(a b c d e f g)
195 #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
196 (require 'cl)
197 (defalias 'my-map (if (org-version-check \"24.2.50\" \"cl\" :predicate)
198 'cl-map
199 'map))
200 (my-map 'list #'list numbers letters)
201 #+end_src"
203 (org-babel-next-src-block)
204 (let ((results (org-babel-execute-src-block)))
205 (should(equal 'a (cadr (assoc 1 results))))
206 (should(equal 'd (cadr (assoc 4 results)))))))
208 (ert-deftest test-org-babel/parse-header-args ()
209 (org-test-with-temp-text-in-file "
211 #+begin_src example-lang :session :results output :var num=9
212 the body
213 #+end_src"
215 (org-babel-next-src-block)
216 (let* ((info (org-babel-get-src-block-info))
217 (params (nth 2 info)))
218 (message "%S" params)
219 (should (equal "example-lang" (nth 0 info)))
220 (should (string= "the body" (org-babel-trim (nth 1 info))))
221 (should-not (member '(:session\ \ \ \ ) params))
222 (should (equal '(:session) (assoc :session params)))
223 (should (equal '(:result-type . output) (assoc :result-type params)))
224 (should (equal '(num . 9) (cdr (assoc :var params)))))))
226 (ert-deftest test-org-babel/parse-header-args2 ()
227 (org-test-with-temp-text-in-file "
229 * resolving sub-trees as references
231 #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
232 (length text)
233 #+end_src
235 #+begin_src org :noweb yes
236 <<simple-subtree>>
237 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
238 #+end_src
240 ** simple subtree with custom ID
241 :PROPERTIES:
242 :CUSTOM_ID: simple-subtree
243 :END:
244 this is simple"
246 (should (string-match (regexp-quote "this is simple")
247 (org-babel-ref-resolve "simple-subtree")))
248 (org-babel-next-src-block)
249 (should (= 14 (org-babel-execute-src-block)))))
251 (ert-deftest test-org-babel/inline-src-blocks ()
252 (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
253 (macrolet ((at-next (&rest body)
254 `(progn
255 (move-end-of-line 1)
256 (re-search-forward org-babel-inline-src-block-regexp nil t)
257 (goto-char (match-beginning 1))
258 (save-match-data ,@body))))
259 (at-next (should (equal 1 (org-babel-execute-src-block))))
260 (at-next (should (equal 2 (org-babel-execute-src-block))))
261 (at-next (should (equal 3 (org-babel-execute-src-block)))))))
263 (ert-deftest test-org-babel/org-babel-get-inline-src-block-matches ()
264 (org-test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74"
265 (let ((test-point (point)))
266 (should (fboundp 'org-babel-get-inline-src-block-matches))
267 (should (re-search-forward "src_" nil t)) ;; 1
268 (should (org-babel-get-inline-src-block-matches))
269 (should (re-search-forward "}" nil (point-at-bol))) ;; 1
270 (should-not (org-babel-get-inline-src-block-matches))
271 (should (re-search-forward "in" nil t)) ;; 2
272 (should-not (org-babel-get-inline-src-block-matches))
273 (should (re-search-forward "echo" nil t)) ;; 2
274 (should (org-babel-get-inline-src-block-matches))
275 (should (re-search-forward "blocks" nil t)) ;; 3
276 (backward-char 8) ;; 3
277 (should (org-babel-get-inline-src-block-matches))
278 (forward-char 1) ;;3
279 (should-not (org-babel-get-inline-src-block-matches))
280 (should (re-search-forward ":results" nil t)) ;; 4
281 (should (org-babel-get-inline-src-block-matches))
282 (end-of-line)
283 (should-not (org-babel-get-inline-src-block-matches)))))
285 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-1 ()
286 (let ((test-line "src_sh{echo 1}"))
287 ;; src_ at bol line 1...
288 (org-test-with-temp-text
289 test-line
290 (goto-char (point-min)) (org-ctrl-c-ctrl-c)
291 (should (string=
292 (concat test-line " =1=")
293 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
294 (forward-char) (org-ctrl-c-ctrl-c)
295 (should (string=
296 (concat test-line " =1= =1=")
297 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
298 (re-search-forward "1}")
299 (should-error (org-ctrl-c-ctrl-c))
300 (backward-char) ;; last char of block body
301 (org-ctrl-c-ctrl-c)
302 (should (string=
303 (concat test-line " =1= =1= =1=")
304 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
305 ;; src_ follows space line 1...
306 (let ((test-line " src_emacs-lisp{ 1 }"))
307 (org-test-with-temp-text
308 test-line
309 (should-error (org-ctrl-c-ctrl-c))
310 (forward-char) (org-ctrl-c-ctrl-c)
311 (should (string=
312 (concat test-line " =1=")
313 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
314 (re-search-forward "{ 1 ") (org-ctrl-c-ctrl-c)
315 (should (string=
316 (concat test-line " =1= =1=")
317 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
318 (forward-char)
319 (should-error (org-ctrl-c-ctrl-c))))))
321 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-2 ()
322 ;; src_ at bol line 2...
323 (let ((test-line " src_emacs-lisp{ \"x\" }"))
324 (org-test-with-temp-text
325 (concat "\n" test-line)
326 (should-error (org-ctrl-c-ctrl-c))
327 (goto-char (point-min))
328 (should-error (org-ctrl-c-ctrl-c))
329 (forward-line)
330 (should-error (org-ctrl-c-ctrl-c))
331 (forward-char) (org-ctrl-c-ctrl-c)
332 (should (string=
333 (concat test-line " =x=")
334 (buffer-substring-no-properties
335 (point-at-bol) (point-at-eol))))))
337 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }"))
338 (org-test-with-temp-text
339 test-line
340 (goto-char (point-max))
341 (insert (concat "\n" test-line " end"))
342 (re-search-backward "src") (org-ctrl-c-ctrl-c)
343 (should (string=
344 (concat test-line " =y= end")
345 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
346 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
347 (should (string=
348 (concat test-line " =y= =y= end")
349 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
350 (forward-char)
351 (should-error (org-ctrl-c-ctrl-c)))))
353 (ert-deftest test-org-babel/inline-src_blk-manual-results-replace ()
354 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }"))
355 (org-test-with-temp-text
356 (concat "\n" test-line)
357 (should-error (org-ctrl-c-ctrl-c))
358 (goto-char (point-max))
359 (should-error (org-ctrl-c-ctrl-c))
360 (beginning-of-line)
361 (should-error (org-ctrl-c-ctrl-c))
362 (forward-char) (org-ctrl-c-ctrl-c)
363 (should (string=
364 (concat test-line " =x=")
365 (buffer-substring-no-properties
366 (point-at-bol) (point-at-eol))))))
368 (let ((test-line (concat " Some text prior to block "
369 "src_emacs-lisp[:results replace]{ \"y\" }")))
370 (org-test-with-temp-text test-line
371 (goto-char (point-max))
372 (insert (concat "\n" test-line " end"))
373 (re-search-backward "src") (org-ctrl-c-ctrl-c)
374 (should (string=
375 (concat test-line " =y= end")
376 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
377 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
378 (should (string=
379 (concat test-line " =y= =y= end")
380 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
381 (forward-char)
382 (should-error (org-ctrl-c-ctrl-c)))))
384 (ert-deftest test-org-babel/inline-src_blk-results-silent ()
385 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
386 (org-test-with-temp-text test-line
387 (org-ctrl-c-ctrl-c)
388 (should (string= test-line
389 (buffer-substring-no-properties
390 (point-at-bol) (point-at-eol))))
391 (end-of-buffer)
392 (should-error (org-ctrl-c-ctrl-c))))
393 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
394 "[ :results silent ]{ \"y\" }")))
395 (org-test-with-temp-text
396 test-line
397 (goto-char (point-max))
398 (insert (concat "\n" test-line " end"))
399 (re-search-backward "src_") (org-ctrl-c-ctrl-c)
400 (should (string= (concat test-line " end")
401 (buffer-substring-no-properties
402 (point-at-bol) (point-at-eol))))
403 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
404 (should (string= (concat test-line " end")
405 (buffer-substring-no-properties
406 (point-at-bol) (point-at-eol))))
407 (forward-char)
408 (should-error (org-ctrl-c-ctrl-c)))))
410 (ert-deftest test-org-babel/inline-src_blk-results-raw ()
411 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
412 (org-test-with-temp-text test-line
413 (org-ctrl-c-ctrl-c)
414 (should (string= (concat test-line " x")
415 (buffer-string)))))
416 (let ((test-line (concat " Some text prior to block "
417 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
418 (org-test-with-temp-text (concat test-line " end")
419 (re-search-forward "src_") (org-ctrl-c-ctrl-c)
420 (should (string= (concat test-line " the end")
421 (buffer-substring-no-properties
422 (point-at-bol) (point-at-eol))))
423 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
424 (should (string= (concat test-line " the the end")
425 (buffer-substring-no-properties
426 (point-at-bol) (point-at-eol))))
427 (forward-char)
428 (should-error (org-ctrl-c-ctrl-c)))))
430 (ert-deftest test-org-babel/inline-src_blk-results-file ()
431 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
432 (org-test-with-temp-text
433 test-line
434 (org-ctrl-c-ctrl-c)
435 (should (string= (concat test-line " [[file:~/test-file]]")
436 (buffer-substring-no-properties
437 (point-min) (point-max)))))))
439 (ert-deftest test-org-babel/inline-src_blk-results-scalar ()
440 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }"))
441 (org-test-with-temp-text
442 test-line
443 (org-ctrl-c-ctrl-c)
444 (should (string= (concat test-line " =\"x\"=")
445 (buffer-substring-no-properties
446 (point-min) (point-max)))))))
448 (ert-deftest test-org-babel/inline-src_blk-results-verbatim ()
449 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
450 (org-test-with-temp-text
451 test-line
452 (org-ctrl-c-ctrl-c)
453 (should (string= (concat test-line " =\"x\"=")
454 (buffer-substring-no-properties
455 (point-min) (point-max)))))))
457 (ert-deftest test-org-babel/combining-scalar-and-raw-result-types ()
458 (org-test-with-temp-text-in-file "
460 #+begin_src sh :results scalar
461 echo \"[[file:./cv.cls]]\"
462 #+end_src
464 #+name:
465 : [[file:./cv.cls]]
467 #+begin_src sh :results raw scalar
468 echo \"[[file:./cv.cls]]\"
469 #+end_src
471 (flet ((next-result ()
472 (org-babel-next-src-block)
473 (org-babel-execute-src-block)
474 (goto-char (org-babel-where-is-src-block-result))
475 (forward-line 1)))
476 (goto-char (point-min))
477 (next-result)
478 (should (org-babel-in-example-or-verbatim))
479 (next-result)
480 (should (not (org-babel-in-example-or-verbatim))))))
482 (ert-deftest test-org-babel/no-defaut-value-for-var ()
483 "Test that the absence of a default value for a variable DOES THROW
484 a proper error."
485 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
486 (org-babel-next-src-block)
487 (let ((err
488 (should-error (org-babel-execute-src-block) :type 'error)))
489 (should
490 (equal
491 '(error
492 "Variable \"x\" must be assigned a default value")
493 err)))))
495 (ert-deftest test-org-babel/just-one-results-block ()
496 "Test that evaluating two times the same code block does not result in a
497 duplicate results block."
498 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
499 (org-babel-execute-src-block)
500 (org-babel-execute-src-block) ; second code block execution
501 (should (search-forward "Hello")) ; the string inside the source code block
502 (should (search-forward "Hello")) ; the same string in the results block
503 (should-error (search-forward "Hello"))))
505 (ert-deftest test-org-babel/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 , 'foo
510 ,#+end_src
511 #+end_src"
512 (should (string= (org-babel-execute-src-block)
513 "#+begin_src emacs-lisp\n 'foo\n#+end_src"))))
515 (ert-deftest test-org-babel/partial-nested-code-block ()
516 "Test nested code blocks inside code blocks don't cause problems."
517 (org-test-with-temp-text "#+begin_src org :results silent
518 ,#+begin_src emacs-lisp
519 #+end_src"
520 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
522 (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
523 (org-test-with-temp-text "#+NAME: foo
524 #+BEGIN_SRC emacs-lisp
525 'foo
526 #+END_SRC\n"
527 (org-babel-next-src-block 1)
528 (should (eq 'foo (org-babel-execute-src-block)))
529 (goto-char (point-min))
530 (org-babel-next-src-block 1)
531 (should (looking-at org-babel-src-block-regexp))))
533 (ert-deftest test-ob/catches-all-references ()
534 (org-test-with-temp-text "
535 #+NAME: literal-example
536 #+BEGIN_EXAMPLE
537 A literal example
538 on two lines
539 #+END_EXAMPLE
541 #+NAME: read-literal-example
542 #+BEGIN_SRC emacs-lisp :var x=literal-example
543 (concatenate 'string x \" for me.\")
544 #+END_SRC"
545 (org-babel-next-src-block 1)
546 (should (string= (org-babel-execute-src-block)
547 "A literal example\non two lines\n for me."))))
549 (ert-deftest test-ob/resolve-code-blocks-before-data-blocks ()
550 (org-test-with-temp-text "
551 #+name: foo
552 : bar
554 #+name: foo
555 #+begin_src emacs-lisp
556 \"baz\"
557 #+end_src
559 #+begin_src emacs-lisp :var foo=foo
561 #+end_src"
562 (org-babel-next-src-block 2)
563 (should (string= (org-babel-execute-src-block) "baz"))))
565 (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
566 (org-test-with-temp-text "
567 #+tblname: base_plus
568 | 1 |
569 | 2 |
571 #+tblname: base
572 | 3 |
573 | 4 |
575 #+begin_src emacs-lisp :var x=base
577 #+end_src"
578 (org-babel-next-src-block 1)
579 (should (equal (org-babel-execute-src-block) '((3) (4))))))
581 (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
582 (org-test-with-temp-text "
583 #+name: base_plus
584 #+begin_src emacs-lisp
585 'bar
586 #+end_src
588 #+name: base
589 #+begin_src emacs-lisp
590 'foo
591 #+end_src
593 #+begin_src emacs-lisp :var x=base
595 #+end_src"
596 (org-babel-next-src-block 3)
597 (should (equal (org-babel-execute-src-block) "foo"))))
599 (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
600 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
601 (+ a b c d)
602 #+end_src
604 (should (= 10 (org-babel-execute-src-block)))))
606 (ert-deftest test-ob/org-babel-update-intermediate ()
607 (org-test-with-temp-text "#+name: foo
608 #+begin_src emacs-lisp
610 #+end_src
612 #+results: foo
615 #+begin_src emacs-lisp :var it=foo
616 (+ it 1)
617 #+end_src"
618 (let ((org-babel-update-intermediate nil))
619 (goto-char (point-min))
620 (org-babel-next-src-block 2)
621 (should (= 3 (org-babel-execute-src-block)))
622 (goto-char (point-min))
623 (forward-line 6)
624 (should (looking-at ": 4")))
625 (let ((org-babel-update-intermediate t))
626 (goto-char (point-min))
627 (org-babel-next-src-block 2)
628 (should (= 3 (org-babel-execute-src-block)))
629 (goto-char (point-min))
630 (forward-line 6)
631 (should (looking-at ": 2")))))
633 (ert-deftest test-ob/eval-header-argument ()
634 (flet ((check-eval (eval runp)
635 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
636 (setq foo :evald)
637 #+end_src" eval)
638 (let ((foo :not-run))
639 (if runp
640 (progn (should (org-babel-execute-src-block))
641 (should (eq foo :evald)))
642 (progn (should-not (org-babel-execute-src-block))
643 (should-not (eq foo :evald))))))))
644 (check-eval "never" nil)
645 (check-eval "no" nil)
646 (check-eval "never-export" t)
647 (check-eval "no-export" t)
648 (let ((org-current-export-file "something"))
649 (check-eval "never" nil)
650 (check-eval "no" nil)
651 (check-eval "never-export" nil)
652 (check-eval "no-export" nil))))
654 (ert-deftest test-ob/noweb-expansion-1 ()
655 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
656 <<foo>>
657 #+end_src
659 #+name: foo
660 #+begin_src sh
662 #+end_src"
663 (should (string= (org-babel-expand-noweb-references) "bar"))))
665 (ert-deftest test-ob/noweb-expansion-2 ()
666 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
667 <<foo>>
668 #+end_src
670 #+name: foo
671 #+begin_src sh :noweb-sep \"\"
673 #+end_src
675 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
677 #+end_src"
678 (should (string= (org-babel-expand-noweb-references) "barbaz"))))
680 (ert-deftest test-ob/splitting-variable-lists-in-references ()
681 (org-test-with-temp-text ""
682 (should (= 1 (length (org-babel-ref-split-args
683 "a=\"this, no work\""))))
684 (should (= 2 (length (org-babel-ref-split-args
685 "a=\"this, no work\", b=1"))))))
687 (ert-deftest test-ob/org-babel-balanced-split ()
688 (should (equal
689 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
690 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
691 '((32 9) . 58)))))
693 (ert-deftest test-ob/commented-last-block-line-no-var ()
694 (org-test-with-temp-text-in-file "
695 #+begin_src emacs-lisp
697 #+end_src"
698 (org-babel-next-src-block)
699 (org-ctrl-c-ctrl-c)
700 (should (re-search-forward "\\#\\+results:" nil t))
701 (forward-line)
702 (should
703 (string=
705 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
706 (org-test-with-temp-text-in-file "
707 #+begin_src emacs-lisp
708 \"some text\";;
709 #+end_src"
710 (org-babel-next-src-block)
711 (org-ctrl-c-ctrl-c)
712 (should (re-search-forward "\\#\\+results:" nil t))
713 (forward-line)
714 (should
715 (string=
716 ": some text"
717 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
719 (ert-deftest test-ob/commented-last-block-line-with-var ()
720 (org-test-with-temp-text-in-file "
721 #+begin_src emacs-lisp :var a=1
723 #+end_src"
724 (org-babel-next-src-block)
725 (org-ctrl-c-ctrl-c)
726 (re-search-forward "\\#\\+results:" nil t)
727 (forward-line)
728 (should (string=
730 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
731 (org-test-with-temp-text-in-file "
732 #+begin_src emacs-lisp :var a=2
734 #+end_src"
735 (org-babel-next-src-block)
736 (org-ctrl-c-ctrl-c)
737 (re-search-forward "\\#\\+results:" nil t)
738 (forward-line)
739 (should (string=
740 ": 2"
741 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
743 (defun test-ob-verify-result-and-removed-result (result buffer-text)
744 "Test helper function to test `org-babel-remove-result'.
745 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
746 and the result of execution is verified against RESULT.
748 The block is actually executed /twice/ to ensure result
749 replacement happens correctly."
750 (org-test-with-temp-text
751 buffer-text
752 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
753 (should (re-search-forward "\\#\\+results:" nil t))
754 (forward-line)
755 (should (string= result
756 (buffer-substring-no-properties
757 (point-at-bol)
758 (- (point-max) 16))))
759 (org-babel-previous-src-block) (org-babel-remove-result)
760 (should (string= buffer-text
761 (buffer-substring-no-properties
762 (point-min) (point-max))))))
764 (ert-deftest test-ob/org-babel-remove-result--results-default ()
765 "Test `org-babel-remove-result' with default :results."
766 (mapcar (lambda (language)
767 (test-ob-verify-result-and-removed-result
768 "\n"
769 (concat
770 "* org-babel-remove-result
771 #+begin_src " language "
772 #+end_src
774 * next heading")))
775 '("sh" "emacs-lisp")))
777 (ert-deftest test-ob/org-babel-remove-result--results-list ()
778 "Test `org-babel-remove-result' with :results list."
779 (test-ob-verify-result-and-removed-result
780 "- 1
783 - (quote (4 5))"
785 "* org-babel-remove-result
786 #+begin_src emacs-lisp :results list
787 '(1 2 3 '(4 5))
788 #+end_src
790 * next heading"))
792 (ert-deftest test-ob/org-babel-results-indented-wrap ()
793 "Ensure that wrapped results are inserted correction when indented.
794 If not inserted correctly then the second evaluation will fail
795 trying to find the :END: marker."
796 (org-test-with-temp-text
797 "- indented
798 #+begin_src sh :results file wrap
799 echo test.txt
800 #+end_src"
801 (org-babel-next-src-block 1)
802 (org-babel-execute-src-block)
803 (org-babel-execute-src-block)))
805 (ert-deftest test-ob/file-desc-header-argument ()
806 "Test that the :file-desc header argument is used."
807 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
808 \"foo\"
809 #+end_src
811 #+begin_src emacs-lisp :results file :file-desc
812 \"foo\"
813 #+end_src"
814 (org-babel-execute-src-block)
815 (org-babel-next-src-block 1)
816 (org-babel-execute-src-block)
817 (goto-char (point-min))
818 (should (search-forward "[[file:foo][bar]]" nil t))
819 (should (search-forward "[[file:foo][foo]]" nil t))))
821 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
822 "Test `org-babel-remove-result' with :results pp."
823 (test-ob-verify-result-and-removed-result
824 ": \"I /am/ working!\""
826 "* org-babel-remove-result
827 #+begin_src emacs-lisp :results pp
828 \"I /am/ working!\")
829 #+end_src
831 * next heading"))
833 (ert-deftest test-org-babel/inline-src_blk-preceded-punct-preceded-by-point ()
834 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
835 (org-test-with-temp-text
836 test-line
837 (forward-char 1)
838 (org-ctrl-c-ctrl-c)
839 (should (re-search-forward "=\"x\"=" nil t))
840 (forward-line))))
842 (ert-deftest test-ob/commented-last-block-line-with-var ()
843 (org-test-with-temp-text-in-file "
844 #+begin_src emacs-lisp :var a=1
846 #+end_src"
847 (org-babel-next-src-block)
848 (org-ctrl-c-ctrl-c)
849 (re-search-forward "\\#\\+results:" nil t)
850 (forward-line)
851 (should (string=
853 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
854 (org-test-with-temp-text-in-file "
855 #+begin_src emacs-lisp :var a=2
857 #+end_src"
858 (org-babel-next-src-block)
859 (org-ctrl-c-ctrl-c)
860 (re-search-forward "\\#\\+results:" nil t)
861 (forward-line)
862 (should (string=
863 ": 2"
864 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
866 (defun test-ob-verify-result-and-removed-result (result buffer-text)
867 "Test helper function to test `org-babel-remove-result'.
868 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
869 and the result of execution is verified against RESULT.
871 The block is actually executed /twice/ to ensure result
872 replacement happens correctly."
873 (org-test-with-temp-text
874 buffer-text
875 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
876 (should (re-search-forward "\\#\\+results:" nil t))
877 (forward-line)
878 (should (string= result
879 (buffer-substring-no-properties
880 (point-at-bol)
881 (- (point-max) 16))))
882 (org-babel-previous-src-block) (org-babel-remove-result)
883 (should (string= buffer-text
884 (buffer-substring-no-properties
885 (point-min) (point-max))))))
887 (ert-deftest test-ob/org-babel-remove-result--results-default ()
888 "Test `org-babel-remove-result' with default :results."
889 (mapcar (lambda (language)
890 (test-ob-verify-result-and-removed-result
891 "\n"
892 (concat
893 "* org-babel-remove-result
894 #+begin_src " language "
895 #+end_src
897 * next heading")))
898 '("sh" "emacs-lisp")))
900 (ert-deftest test-ob/org-babel-remove-result--results-list ()
901 "Test `org-babel-remove-result' with :results list."
902 (test-ob-verify-result-and-removed-result
903 "- 1
906 - (quote (4 5))"
908 "* org-babel-remove-result
909 #+begin_src emacs-lisp :results list
910 '(1 2 3 '(4 5))
911 #+end_src
913 * next heading"))
915 (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
916 "Test `org-babel-remove-result' with :results wrap."
917 (test-ob-verify-result-and-removed-result
918 ":RESULTS:
919 hello there
920 :END:"
922 "* org-babel-remove-result
924 #+begin_src emacs-lisp :results wrap
925 \"hello there\"
926 #+end_src
928 * next heading"))
930 (ert-deftest test-ob/org-babel-remove-result--results-org ()
931 "Test `org-babel-remove-result' with :results org."
932 (test-ob-verify-result-and-removed-result
933 "#+BEGIN_SRC org
934 ,* heading
935 ,** subheading
936 content
937 #+END_SRC"
939 "* org-babel-remove-result
940 #+begin_src emacs-lisp :results org
941 \"* heading
942 ** subheading
943 content\"
944 #+end_src
946 * next heading"))
948 (ert-deftest test-ob/org-babel-remove-result--results-html ()
949 "Test `org-babel-remove-result' with :results html."
950 (test-ob-verify-result-and-removed-result
951 "#+BEGIN_HTML
952 <head><body></body></head>
953 #+END_HTML"
955 "* org-babel-remove-result
956 #+begin_src emacs-lisp :results html
957 \"<head><body></body></head>\"
958 #+end_src
960 * next heading"))
962 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
963 "Test `org-babel-remove-result' with :results latex."
964 (test-ob-verify-result-and-removed-result
965 "#+BEGIN_LaTeX
966 Line 1
967 Line 2
968 Line 3
969 #+END_LaTeX"
971 "* org-babel-remove-result
972 #+begin_src emacs-lisp :results latex
973 \"Line 1
974 Line 2
975 Line 3\"
976 #+end_src
978 * next heading"))
980 (ert-deftest test-ob/org-babel-remove-result--results-code ()
981 "Test `org-babel-remove-result' with :results code."
983 (test-ob-verify-result-and-removed-result
984 "#+BEGIN_SRC emacs-lisp
985 \"I am working!\"
986 #+END_SRC"
988 "* org-babel-remove-result
989 #+begin_src emacs-lisp :results code
990 (message \"I am working!\")
991 #+end_src
993 * next heading"))
995 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
996 "Test `org-babel-remove-result' with :results pp."
997 (test-ob-verify-result-and-removed-result
998 ": \"I /am/ working!\""
1000 "* org-babel-remove-result
1001 #+begin_src emacs-lisp :results pp
1002 \"I /am/ working!\")
1003 #+end_src
1005 * next heading"))
1007 (ert-deftest test-ob/results-do-not-replace-code-blocks ()
1008 (org-test-with-temp-text "Block two has a space after the name.
1010 #+name: foo
1011 #+begin_src emacs-lisp
1013 #+end_src emacs-lisp
1015 #+name: foo
1016 #+begin_src emacs-lisp
1018 #+end_src
1020 #+name: foo
1021 #+begin_src emacs-lisp
1023 #+end_src
1025 #+RESULTS: foo
1026 : foo
1028 (dolist (num '(1 2 3))
1029 ;; execute the block
1030 (goto-char (point-min))
1031 (org-babel-next-src-block num) (org-babel-execute-src-block)
1032 ;; check the results
1033 (goto-char (point-max))
1034 (move-beginning-of-line 0)
1035 (should (looking-at (format ": %d" num))))))
1037 (ert-deftest test-ob/blocks-with-spaces ()
1038 "Test expansion of blocks followed by blank lines."
1039 (should
1040 (equal "#+BEGIN_SRC emacs-lisp
1041 \(+ 1 2)
1042 #+END_SRC
1044 #+RESULTS:
1045 : 3\n\n\n"
1046 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1047 \(+ 1 2)
1048 #+END_SRC\n\n\n"
1049 (org-babel-execute-src-block)
1050 (buffer-string)))))
1052 (ert-deftest test-ob/results-in-narrowed-buffer ()
1053 "Test block execution in a narrowed buffer."
1054 ;; If results don't exist, they should be inserted in visible part
1055 ;; of the buffer.
1056 (should
1057 (equal
1058 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1059 (org-test-with-temp-text
1060 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1061 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1062 (org-babel-execute-src-block)
1063 (org-trim (buffer-string)))))
1064 (should
1065 (equal
1066 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1067 (org-test-with-temp-text
1068 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1069 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1070 (org-babel-execute-src-block)
1071 (org-trim (buffer-string)))))
1072 ;; Results in visible part of buffer, should be updated here.
1073 (should
1074 (equal
1075 "#+NAME: test
1076 #+BEGIN_SRC emacs-lisp
1077 \(+ 1 2)
1078 #+END_SRC
1080 #+RESULTS: test
1081 : 3"
1082 (org-test-with-temp-text
1083 "#+NAME: test
1084 #+BEGIN_SRC emacs-lisp
1085 \(+ 1 2)
1086 #+END_SRC
1088 #+RESULTS: test
1091 Paragraph"
1092 (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
1093 (org-babel-execute-src-block)
1094 (org-trim (buffer-string)))))
1095 ;; Results in invisible part of buffer, should be updated there.
1096 (org-test-with-temp-text
1097 "#+NAME: test
1098 #+BEGIN_SRC emacs-lisp
1099 \(+ 1 2)
1100 #+END_SRC
1102 #+RESULTS: test
1105 Paragraph"
1106 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1107 (org-babel-execute-src-block)
1108 (should-not (re-search-forward "^#\\+RESULTS:" nil t))
1109 (widen)
1110 (should (should (re-search-forward "^: 3" nil t)))))
1112 (ert-deftest test-ob/specific-colnames ()
1113 "Test passing specific column names."
1114 (should
1115 (equal "#+name: input-table
1116 | id | var1 |
1117 |----+------|
1118 | 1 | bar |
1119 | 2 | baz |
1121 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1122 echo \"$data\"
1123 #+end_src
1125 #+RESULTS:
1126 | Rev | Author |
1127 |-----+--------|
1128 | 1 | bar |
1129 | 2 | baz |
1132 (org-test-with-temp-text
1133 "#+name: input-table
1134 | id | var1 |
1135 |----+------|
1136 | 1 | bar |
1137 | 2 | baz |
1139 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1140 echo \"$data\"
1141 #+end_src
1143 ;; we should find a code block
1144 (should (re-search-forward org-babel-src-block-regexp nil t))
1145 (goto-char (match-beginning 0))
1146 ;; now that we've located the code block, it may be evaluated
1147 (org-babel-execute-src-block)
1148 (buffer-string)))))
1150 (ert-deftest test-ob/location-of-header-arg-eval ()
1151 "Test location of header argument evaluation."
1152 (org-test-with-temp-text "
1153 #+name: top-block
1154 #+begin_src emacs-lisp :var pt=(point)
1156 #+end_src
1158 #+name: bottom-block
1159 #+begin_src emacs-lisp :var pt=top-block()
1161 #+end_src
1163 ;; the value of the second block should be greater than the first
1164 (should
1165 (< (progn (re-search-forward org-babel-src-block-regexp nil t)
1166 (goto-char (match-beginning 0))
1167 (prog1 (save-match-data (org-babel-execute-src-block))
1168 (goto-char (match-end 0))))
1169 (progn (re-search-forward org-babel-src-block-regexp nil t)
1170 (goto-char (match-beginning 0))
1171 (org-babel-execute-src-block))))))
1173 (ert-deftest test-ob/preserve-results-indentation ()
1174 "Preserve indentation when executing a src block."
1175 (should
1176 (equal '(2 2)
1177 (org-test-with-temp-text
1178 " #+begin_src emacs-lisp\n (+ 1 1)\n #+end_src"
1179 (org-babel-execute-src-block)
1180 (buffer-string)
1181 (let ((case-fold-search t)) (search-forward "#+results:"))
1182 ;; Check if both #+RESULTS: keyword and actual results are
1183 ;; indented by 2 columns.
1184 (list (org-get-indentation)
1185 (progn (forward-line) (org-get-indentation)))))))
1187 (ert-deftest test-ob/safe-header-args ()
1188 "Detect safe and unsafe header args."
1189 (let ((safe-args '((:cache . "foo")
1190 (:results . "output")
1191 (:eval . "never")
1192 (:eval . "query")))
1193 (unsafe-args '((:eval . "yes")
1194 (:results . "output file")
1195 (:foo . "bar")))
1196 (malformed-args '((bar . "foo")
1197 ("foo" . "bar")
1198 :foo))
1199 (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
1200 (dolist (arg safe-args)
1201 (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
1202 (dolist (arg unsafe-args)
1203 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1204 (dolist (arg malformed-args)
1205 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1206 (should (not (funcall safe-p (append safe-args unsafe-args))))))
1208 (ert-deftest test-ob/noweb-expansions-in-cache ()
1209 "Ensure that noweb expansions are expanded before caching."
1210 (let ((noweb-expansions-in-cache-var 0))
1211 (org-test-with-temp-text "
1212 #+name: foo
1213 #+begin_src emacs-lisp
1214 \"I said\"
1215 #+end_src
1217 #+name: bar
1218 #+begin_src emacs-lisp :noweb yes :cache yes
1219 (setq noweb-expansions-in-cache-var
1220 (+ 1 noweb-expansions-in-cache-var))
1221 (concat <<foo>> \" check noweb expansions\")
1222 #+end_src
1224 ;; run the second block to create the cache
1225 (goto-char (point-min))
1226 (re-search-forward (regexp-quote "#+name: bar"))
1227 (should (string= "I said check noweb expansions"
1228 (org-babel-execute-src-block)))
1229 (should (= noweb-expansions-in-cache-var 1))
1230 ;; change the value of the first block
1231 (goto-char (point-min))
1232 (re-search-forward (regexp-quote "said"))
1233 (goto-char (match-beginning 0))
1234 (insert "haven't ")
1235 (re-search-forward (regexp-quote "#+name: bar"))
1236 (should (string= "I haven't said check noweb expansions"
1237 (org-babel-execute-src-block)))
1238 (should (= noweb-expansions-in-cache-var 2)))))
1240 (provide 'test-ob)
1242 ;;; test-ob ends here