Merge branch 'master' of orgmode.org:org-mode
[org-mode/org-tableheadings.git] / testing / lisp / test-ob.el
blobfdb5a3e57fde1756cecaf444e3c60b8c68fd1345
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 (macrolet ((at-next (&rest body)
253 `(progn
254 (move-end-of-line 1)
255 (re-search-forward org-babel-inline-src-block-regexp nil t)
256 (goto-char (match-beginning 1))
257 (save-match-data ,@body))))
258 (org-test-at-id
259 "54cb8dc3-298c-4883-a933-029b3c9d4b18"
260 (at-next (should (equal 1 (org-babel-execute-src-block))))
261 (at-next (should (equal 2 (org-babel-execute-src-block))))
262 (at-next (should (equal 3 (org-babel-execute-src-block)))))
263 (org-test-at-id
264 "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076"
265 (at-next (should (equal 1 (org-babel-execute-src-block))))
266 (at-next (should (equal 2 (org-babel-execute-src-block))))
267 (at-next (should (equal 3 (org-babel-execute-src-block))))
268 (at-next (should (equal 4 (org-babel-execute-src-block)))))))
270 (ert-deftest test-org-babel/org-babel-get-inline-src-block-matches ()
271 (flet ((test-at-id (id)
272 (org-test-at-id
274 (let ((test-point (point)))
275 (should (fboundp 'org-babel-get-inline-src-block-matches))
276 (should (re-search-forward "src_" nil t)) ;; 1
277 (should (org-babel-get-inline-src-block-matches))
278 (should (re-search-forward "}" nil (point-at-bol))) ;; 1
279 (should-not (org-babel-get-inline-src-block-matches))
280 (should (re-search-forward "in" nil t)) ;; 2
281 (should-not (org-babel-get-inline-src-block-matches))
282 (should (re-search-forward "echo" nil t)) ;; 2
283 (should (org-babel-get-inline-src-block-matches))
284 (should (re-search-forward "blocks" nil t)) ;; 3
285 (backward-char 8) ;; 3
286 (should (org-babel-get-inline-src-block-matches))
287 (forward-char 1) ;;3
288 (should-not (org-babel-get-inline-src-block-matches))
289 (should (re-search-forward ":results" nil t)) ;; 4
290 (should (org-babel-get-inline-src-block-matches))
291 (end-of-line)
292 (should-not (org-babel-get-inline-src-block-matches))))))
293 (test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74")
294 (test-at-id "d55dada7-de0e-4340-8061-787cccbedee5")))
296 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-1 ()
297 (let ((test-line "src_sh{echo 1}"))
298 ;; src_ at bol line 1...
299 (org-test-with-temp-text
300 test-line
301 (goto-char (point-min)) (org-ctrl-c-ctrl-c)
302 (should (string=
303 (concat test-line " =1=")
304 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
305 (forward-char) (org-ctrl-c-ctrl-c)
306 (should (string=
307 (concat test-line " =1= =1=")
308 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
309 (re-search-forward "1}")
310 (should-error (org-ctrl-c-ctrl-c))
311 (backward-char) ;; last char of block body
312 (org-ctrl-c-ctrl-c)
313 (should (string=
314 (concat test-line " =1= =1= =1=")
315 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
316 ;; src_ follows space line 1...
317 (let ((test-line " src_emacs-lisp{ 1 }"))
318 (org-test-with-temp-text
319 test-line
320 (should-error (org-ctrl-c-ctrl-c))
321 (forward-char) (org-ctrl-c-ctrl-c)
322 (should (string=
323 (concat test-line " =1=")
324 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
325 (re-search-forward "{ 1 ") (org-ctrl-c-ctrl-c)
326 (should (string=
327 (concat test-line " =1= =1=")
328 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
329 (forward-char)
330 (should-error (org-ctrl-c-ctrl-c))))))
332 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-2 ()
333 ;; src_ at bol line 2...
334 (let ((test-line " src_emacs-lisp{ \"x\" }"))
335 (org-test-with-temp-text
336 (concat "\n" test-line)
337 (should-error (org-ctrl-c-ctrl-c))
338 (goto-char (point-min))
339 (should-error (org-ctrl-c-ctrl-c))
340 (forward-line)
341 (should-error (org-ctrl-c-ctrl-c))
342 (forward-char) (org-ctrl-c-ctrl-c)
343 (should (string=
344 (concat test-line " =x=")
345 (buffer-substring-no-properties
346 (point-at-bol) (point-at-eol))))))
348 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }"))
349 (org-test-with-temp-text
350 test-line
351 (goto-char (point-max))
352 (insert (concat "\n" test-line " end"))
353 (re-search-backward "src") (org-ctrl-c-ctrl-c)
354 (should (string=
355 (concat test-line " =y= end")
356 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
357 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
358 (should (string=
359 (concat test-line " =y= =y= end")
360 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
361 (forward-char)
362 (should-error (org-ctrl-c-ctrl-c)))))
364 (ert-deftest test-org-babel/inline-src_blk-manual-results-replace ()
365 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }"))
366 (org-test-with-temp-text
367 (concat "\n" test-line)
368 (should-error (org-ctrl-c-ctrl-c))
369 (goto-char (point-max))
370 (should-error (org-ctrl-c-ctrl-c))
371 (beginning-of-line)
372 (should-error (org-ctrl-c-ctrl-c))
373 (forward-char) (org-ctrl-c-ctrl-c)
374 (should (string=
375 (concat test-line " =x=")
376 (buffer-substring-no-properties
377 (point-at-bol) (point-at-eol))))))
379 (let ((test-line (concat " Some text prior to block "
380 "src_emacs-lisp[:results replace]{ \"y\" }")))
381 (org-test-with-temp-text test-line
382 (goto-char (point-max))
383 (insert (concat "\n" test-line " end"))
384 (re-search-backward "src") (org-ctrl-c-ctrl-c)
385 (should (string=
386 (concat test-line " =y= end")
387 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
388 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
389 (should (string=
390 (concat test-line " =y= =y= end")
391 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
392 (forward-char)
393 (should-error (org-ctrl-c-ctrl-c)))))
395 (ert-deftest test-org-babel/inline-src_blk-results-silent ()
396 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
397 (org-test-with-temp-text test-line
398 (org-ctrl-c-ctrl-c)
399 (should (string= test-line
400 (buffer-substring-no-properties
401 (point-at-bol) (point-at-eol))))
402 (end-of-buffer)
403 (should-error (org-ctrl-c-ctrl-c))))
404 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
405 "[ :results silent ]{ \"y\" }")))
406 (org-test-with-temp-text
407 test-line
408 (goto-char (point-max))
409 (insert (concat "\n" test-line " end"))
410 (re-search-backward "src_") (org-ctrl-c-ctrl-c)
411 (should (string= (concat test-line " end")
412 (buffer-substring-no-properties
413 (point-at-bol) (point-at-eol))))
414 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
415 (should (string= (concat test-line " end")
416 (buffer-substring-no-properties
417 (point-at-bol) (point-at-eol))))
418 (forward-char)
419 (should-error (org-ctrl-c-ctrl-c)))))
421 (ert-deftest test-org-babel/inline-src_blk-results-raw ()
422 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
423 (org-test-with-temp-text test-line
424 (org-ctrl-c-ctrl-c)
425 (should (string= (concat test-line " x")
426 (buffer-string)))))
427 (let ((test-line (concat " Some text prior to block "
428 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
429 (org-test-with-temp-text (concat test-line " end")
430 (re-search-forward "src_") (org-ctrl-c-ctrl-c)
431 (should (string= (concat test-line " the end")
432 (buffer-substring-no-properties
433 (point-at-bol) (point-at-eol))))
434 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
435 (should (string= (concat test-line " the the end")
436 (buffer-substring-no-properties
437 (point-at-bol) (point-at-eol))))
438 (forward-char)
439 (should-error (org-ctrl-c-ctrl-c)))))
441 (ert-deftest test-org-babel/inline-src_blk-results-file ()
442 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
443 (org-test-with-temp-text
444 test-line
445 (org-ctrl-c-ctrl-c)
446 (should (string= (concat test-line " [[file:~/test-file]]")
447 (buffer-substring-no-properties
448 (point-min) (point-max)))))))
450 (ert-deftest test-org-babel/inline-src_blk-results-scalar ()
451 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }"))
452 (org-test-with-temp-text
453 test-line
454 (org-ctrl-c-ctrl-c)
455 (should (string= (concat test-line " =\"x\"=")
456 (buffer-substring-no-properties
457 (point-min) (point-max)))))))
459 (ert-deftest test-org-babel/inline-src_blk-results-verbatim ()
460 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
461 (org-test-with-temp-text
462 test-line
463 (org-ctrl-c-ctrl-c)
464 (should (string= (concat test-line " =\"x\"=")
465 (buffer-substring-no-properties
466 (point-min) (point-max)))))))
468 (ert-deftest test-org-babel/combining-scalar-and-raw-result-types ()
469 (org-test-with-temp-text-in-file "
471 #+begin_src sh :results scalar
472 echo \"[[file:./cv.cls]]\"
473 #+end_src
475 #+name:
476 : [[file:./cv.cls]]
478 #+begin_src sh :results raw scalar
479 echo \"[[file:./cv.cls]]\"
480 #+end_src
482 (flet ((next-result ()
483 (org-babel-next-src-block)
484 (org-babel-execute-src-block)
485 (goto-char (org-babel-where-is-src-block-result))
486 (forward-line 1)))
487 (goto-char (point-min))
488 (next-result)
489 (should (org-babel-in-example-or-verbatim))
490 (next-result)
491 (should (not (org-babel-in-example-or-verbatim))))))
493 (ert-deftest test-org-babel/no-defaut-value-for-var ()
494 "Test that the absence of a default value for a variable DOES THROW
495 a proper error."
496 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
497 (org-babel-next-src-block)
498 (let ((err
499 (should-error (org-babel-execute-src-block) :type 'error)))
500 (should
501 (equal
502 '(error
503 "Variable \"x\" must be assigned a default value")
504 err)))))
506 (ert-deftest test-org-babel/just-one-results-block ()
507 "Test that evaluating two times the same code block does not result in a
508 duplicate results block."
509 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
510 (org-babel-execute-src-block)
511 (org-babel-execute-src-block) ; second code block execution
512 (should (search-forward "Hello")) ; the string inside the source code block
513 (should (search-forward "Hello")) ; the same string in the results block
514 (should-error (search-forward "Hello"))))
516 (ert-deftest test-org-babel/nested-code-block ()
517 "Test nested code blocks inside code blocks don't cause problems."
518 (org-test-with-temp-text "#+begin_src org :results silent
519 ,#+begin_src emacs-lisp
520 , 'foo
521 ,#+end_src
522 #+end_src"
523 (should (string= (org-babel-execute-src-block)
524 "#+begin_src emacs-lisp\n 'foo\n#+end_src"))))
526 (ert-deftest test-org-babel/partial-nested-code-block ()
527 "Test nested code blocks inside code blocks don't cause problems."
528 (org-test-with-temp-text "#+begin_src org :results silent
529 ,#+begin_src emacs-lisp
530 #+end_src"
531 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
533 (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
534 (org-test-with-temp-text "#+NAME: foo
535 #+BEGIN_SRC emacs-lisp
536 'foo
537 #+END_SRC\n"
538 (org-babel-next-src-block 1)
539 (should (eq 'foo (org-babel-execute-src-block)))
540 (goto-char (point-min))
541 (org-babel-next-src-block 1)
542 (should (looking-at org-babel-src-block-regexp))))
544 (ert-deftest test-ob/catches-all-references ()
545 (org-test-with-temp-text "
546 #+NAME: literal-example
547 #+BEGIN_EXAMPLE
548 A literal example
549 on two lines
550 #+END_EXAMPLE
552 #+NAME: read-literal-example
553 #+BEGIN_SRC emacs-lisp :var x=literal-example
554 (concatenate 'string x \" for me.\")
555 #+END_SRC"
556 (org-babel-next-src-block 1)
557 (should (string= (org-babel-execute-src-block)
558 "A literal example\non two lines\n for me."))))
560 (ert-deftest test-ob/resolve-code-blocks-before-data-blocks ()
561 (org-test-with-temp-text "
562 #+name: foo
563 : bar
565 #+name: foo
566 #+begin_src emacs-lisp
567 \"baz\"
568 #+end_src
570 #+begin_src emacs-lisp :var foo=foo
572 #+end_src"
573 (org-babel-next-src-block 2)
574 (should (string= (org-babel-execute-src-block) "baz"))))
576 (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
577 (org-test-with-temp-text "
578 #+tblname: base_plus
579 | 1 |
580 | 2 |
582 #+tblname: base
583 | 3 |
584 | 4 |
586 #+begin_src emacs-lisp :var x=base
588 #+end_src"
589 (org-babel-next-src-block 1)
590 (should (equal (org-babel-execute-src-block) '((3) (4))))))
592 (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
593 (org-test-with-temp-text "
594 #+name: base_plus
595 #+begin_src emacs-lisp
596 'bar
597 #+end_src
599 #+name: base
600 #+begin_src emacs-lisp
601 'foo
602 #+end_src
604 #+begin_src emacs-lisp :var x=base
606 #+end_src"
607 (org-babel-next-src-block 3)
608 (should (equal (org-babel-execute-src-block) "foo"))))
610 (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
611 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
612 (+ a b c d)
613 #+end_src
615 (should (= 10 (org-babel-execute-src-block)))))
617 (ert-deftest test-ob/org-babel-update-intermediate ()
618 (org-test-with-temp-text "#+name: foo
619 #+begin_src emacs-lisp
621 #+end_src
623 #+results: foo
626 #+begin_src emacs-lisp :var it=foo
627 (+ it 1)
628 #+end_src"
629 (let ((org-babel-update-intermediate nil))
630 (goto-char (point-min))
631 (org-babel-next-src-block 2)
632 (should (= 3 (org-babel-execute-src-block)))
633 (goto-char (point-min))
634 (forward-line 6)
635 (should (looking-at ": 4")))
636 (let ((org-babel-update-intermediate t))
637 (goto-char (point-min))
638 (org-babel-next-src-block 2)
639 (should (= 3 (org-babel-execute-src-block)))
640 (goto-char (point-min))
641 (forward-line 6)
642 (should (looking-at ": 2")))))
644 (ert-deftest test-ob/eval-header-argument ()
645 (flet ((check-eval (eval runp)
646 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
647 (setq foo :evald)
648 #+end_src" eval)
649 (let ((foo :not-run))
650 (if runp
651 (progn (should (org-babel-execute-src-block))
652 (should (eq foo :evald)))
653 (progn (should-not (org-babel-execute-src-block))
654 (should-not (eq foo :evald))))))))
655 (check-eval "never" nil)
656 (check-eval "no" nil)
657 (check-eval "never-export" t)
658 (check-eval "no-export" t)
659 (let ((org-babel-exp-reference-buffer (current-buffer)))
660 (check-eval "never" nil)
661 (check-eval "no" nil)
662 (check-eval "never-export" nil)
663 (check-eval "no-export" nil))))
665 (ert-deftest test-ob/noweb-expansion-1 ()
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
673 #+end_src"
674 (should (string= (org-babel-expand-noweb-references) "bar"))))
676 (ert-deftest test-ob/noweb-expansion-2 ()
677 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
678 <<foo>>
679 #+end_src
681 #+name: foo
682 #+begin_src sh :noweb-sep \"\"
684 #+end_src
686 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
688 #+end_src"
689 (should (string= (org-babel-expand-noweb-references) "barbaz"))))
691 (ert-deftest test-ob/splitting-variable-lists-in-references ()
692 (org-test-with-temp-text ""
693 (should (= 1 (length (org-babel-ref-split-args
694 "a=\"this, no work\""))))
695 (should (= 2 (length (org-babel-ref-split-args
696 "a=\"this, no work\", b=1"))))))
698 (ert-deftest test-ob/org-babel-balanced-split ()
699 (should (equal
700 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
701 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
702 '((32 9) . 58)))))
704 (ert-deftest test-ob/commented-last-block-line-no-var ()
705 (org-test-with-temp-text-in-file "
706 #+begin_src emacs-lisp
708 #+end_src"
709 (org-babel-next-src-block)
710 (org-ctrl-c-ctrl-c)
711 (should (re-search-forward "\\#\\+results:" nil t))
712 (forward-line)
713 (should
714 (string=
716 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
717 (org-test-with-temp-text-in-file "
718 #+begin_src emacs-lisp
719 \"some text\";;
720 #+end_src"
721 (org-babel-next-src-block)
722 (org-ctrl-c-ctrl-c)
723 (should (re-search-forward "\\#\\+results:" nil t))
724 (forward-line)
725 (should
726 (string=
727 ": some text"
728 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
730 (ert-deftest test-ob/commented-last-block-line-with-var ()
731 (org-test-with-temp-text-in-file "
732 #+begin_src emacs-lisp :var a=1
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=
741 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
742 (org-test-with-temp-text-in-file "
743 #+begin_src emacs-lisp :var a=2
745 #+end_src"
746 (org-babel-next-src-block)
747 (org-ctrl-c-ctrl-c)
748 (re-search-forward "\\#\\+results:" nil t)
749 (forward-line)
750 (should (string=
751 ": 2"
752 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
754 (defun test-ob-verify-result-and-removed-result (result buffer-text)
755 "Test helper function to test `org-babel-remove-result'.
756 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
757 and the result of execution is verified against RESULT.
759 The block is actually executed /twice/ to ensure result
760 replacement happens correctly."
761 (org-test-with-temp-text
762 buffer-text
763 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
764 (should (re-search-forward "\\#\\+results:" nil t))
765 (forward-line)
766 (should (string= result
767 (buffer-substring-no-properties
768 (point-at-bol)
769 (- (point-max) 16))))
770 (org-babel-previous-src-block) (org-babel-remove-result)
771 (should (string= buffer-text
772 (buffer-substring-no-properties
773 (point-min) (point-max))))))
775 (ert-deftest test-ob/org-babel-remove-result--results-default ()
776 "Test `org-babel-remove-result' with default :results."
777 (mapcar (lambda (language)
778 (test-ob-verify-result-and-removed-result
779 "\n"
780 (concat
781 "* org-babel-remove-result
782 #+begin_src " language "
783 #+end_src
785 * next heading")))
786 '("sh" "emacs-lisp")))
788 (ert-deftest test-ob/org-babel-remove-result--results-list ()
789 "Test `org-babel-remove-result' with :results list."
790 (test-ob-verify-result-and-removed-result
791 "- 1
794 - (quote (4 5))"
796 "* org-babel-remove-result
797 #+begin_src emacs-lisp :results list
798 '(1 2 3 '(4 5))
799 #+end_src
801 * next heading"))
803 (ert-deftest test-ob/org-babel-results-indented-wrap ()
804 "Ensure that wrapped results are inserted correction when indented.
805 If not inserted correctly then the second evaluation will fail
806 trying to find the :END: marker."
807 (org-test-with-temp-text
808 "- indented
809 #+begin_src sh :results file wrap
810 echo test.txt
811 #+end_src"
812 (org-babel-next-src-block 1)
813 (org-babel-execute-src-block)
814 (org-babel-execute-src-block)))
816 (ert-deftest test-ob/file-desc-header-argument ()
817 "Test that the :file-desc header argument is used."
818 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
819 \"foo\"
820 #+end_src
822 #+begin_src emacs-lisp :results file :file-desc
823 \"foo\"
824 #+end_src"
825 (org-babel-execute-src-block)
826 (org-babel-next-src-block 1)
827 (org-babel-execute-src-block)
828 (goto-char (point-min))
829 (should (search-forward "[[file:foo][bar]]" nil t))
830 (should (search-forward "[[file:foo][foo]]" nil t))))
832 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
833 "Test `org-babel-remove-result' with :results pp."
834 (test-ob-verify-result-and-removed-result
835 ": \"I /am/ working!\""
837 "* org-babel-remove-result
838 #+begin_src emacs-lisp :results pp
839 \"I /am/ working!\")
840 #+end_src
842 * next heading"))
844 (ert-deftest test-org-babel/inline-src_blk-preceded-punct-preceded-by-point ()
845 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
846 (org-test-with-temp-text
847 test-line
848 (forward-char 1)
849 (org-ctrl-c-ctrl-c)
850 (should (re-search-forward "=\"x\"=" nil t))
851 (forward-line))))
853 (ert-deftest test-ob/commented-last-block-line-with-var ()
854 (org-test-with-temp-text-in-file "
855 #+begin_src emacs-lisp :var a=1
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=
864 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
865 (org-test-with-temp-text-in-file "
866 #+begin_src emacs-lisp :var a=2
868 #+end_src"
869 (org-babel-next-src-block)
870 (org-ctrl-c-ctrl-c)
871 (re-search-forward "\\#\\+results:" nil t)
872 (forward-line)
873 (should (string=
874 ": 2"
875 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
877 (defun test-ob-verify-result-and-removed-result (result buffer-text)
878 "Test helper function to test `org-babel-remove-result'.
879 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
880 and the result of execution is verified against RESULT.
882 The block is actually executed /twice/ to ensure result
883 replacement happens correctly."
884 (org-test-with-temp-text
885 buffer-text
886 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
887 (should (re-search-forward "\\#\\+results:" nil t))
888 (forward-line)
889 (should (string= result
890 (buffer-substring-no-properties
891 (point-at-bol)
892 (- (point-max) 16))))
893 (org-babel-previous-src-block) (org-babel-remove-result)
894 (should (string= buffer-text
895 (buffer-substring-no-properties
896 (point-min) (point-max))))))
898 (ert-deftest test-ob/org-babel-remove-result--results-default ()
899 "Test `org-babel-remove-result' with default :results."
900 (mapcar (lambda (language)
901 (test-ob-verify-result-and-removed-result
902 "\n"
903 (concat
904 "* org-babel-remove-result
905 #+begin_src " language "
906 #+end_src
908 * next heading")))
909 '("sh" "emacs-lisp")))
911 (ert-deftest test-ob/org-babel-remove-result--results-list ()
912 "Test `org-babel-remove-result' with :results list."
913 (test-ob-verify-result-and-removed-result
914 "- 1
917 - (quote (4 5))"
919 "* org-babel-remove-result
920 #+begin_src emacs-lisp :results list
921 '(1 2 3 '(4 5))
922 #+end_src
924 * next heading"))
926 (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
927 "Test `org-babel-remove-result' with :results wrap."
928 (test-ob-verify-result-and-removed-result
929 ":RESULTS:
930 hello there
931 :END:"
933 "* org-babel-remove-result
935 #+begin_src emacs-lisp :results wrap
936 \"hello there\"
937 #+end_src
939 * next heading"))
941 (ert-deftest test-ob/org-babel-remove-result--results-org ()
942 "Test `org-babel-remove-result' with :results org."
943 (test-ob-verify-result-and-removed-result
944 "#+BEGIN_SRC org
945 ,* heading
946 ,** subheading
947 content
948 #+END_SRC"
950 "* org-babel-remove-result
951 #+begin_src emacs-lisp :results org
952 \"* heading
953 ** subheading
954 content\"
955 #+end_src
957 * next heading"))
959 (ert-deftest test-ob/org-babel-remove-result--results-html ()
960 "Test `org-babel-remove-result' with :results html."
961 (test-ob-verify-result-and-removed-result
962 "#+BEGIN_HTML
963 <head><body></body></head>
964 #+END_HTML"
966 "* org-babel-remove-result
967 #+begin_src emacs-lisp :results html
968 \"<head><body></body></head>\"
969 #+end_src
971 * next heading"))
973 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
974 "Test `org-babel-remove-result' with :results latex."
975 (test-ob-verify-result-and-removed-result
976 "#+BEGIN_LaTeX
977 Line 1
978 Line 2
979 Line 3
980 #+END_LaTeX"
982 "* org-babel-remove-result
983 #+begin_src emacs-lisp :results latex
984 \"Line 1
985 Line 2
986 Line 3\"
987 #+end_src
989 * next heading"))
991 (ert-deftest test-ob/org-babel-remove-result--results-code ()
992 "Test `org-babel-remove-result' with :results code."
994 (test-ob-verify-result-and-removed-result
995 "#+BEGIN_SRC emacs-lisp
996 \"I am working!\"
997 #+END_SRC"
999 "* org-babel-remove-result
1000 #+begin_src emacs-lisp :results code
1001 (message \"I am working!\")
1002 #+end_src
1004 * next heading"))
1006 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
1007 "Test `org-babel-remove-result' with :results pp."
1008 (test-ob-verify-result-and-removed-result
1009 ": \"I /am/ working!\""
1011 "* org-babel-remove-result
1012 #+begin_src emacs-lisp :results pp
1013 \"I /am/ working!\")
1014 #+end_src
1016 * next heading"))
1018 (ert-deftest test-ob/results-do-not-replace-code-blocks ()
1019 (org-test-with-temp-text "Block two has a space after the name.
1021 #+name: foo
1022 #+begin_src emacs-lisp
1024 #+end_src emacs-lisp
1026 #+name: foo
1027 #+begin_src emacs-lisp
1029 #+end_src
1031 #+name: foo
1032 #+begin_src emacs-lisp
1034 #+end_src
1036 #+RESULTS: foo
1037 : foo
1039 (dolist (num '(1 2 3))
1040 ;; execute the block
1041 (goto-char (point-min))
1042 (org-babel-next-src-block num) (org-babel-execute-src-block)
1043 ;; check the results
1044 (goto-char (point-max))
1045 (move-beginning-of-line 0)
1046 (should (looking-at (format ": %d" num))))))
1048 (ert-deftest test-ob/blocks-with-spaces ()
1049 "Test expansion of blocks followed by blank lines."
1050 (should
1051 (equal "#+BEGIN_SRC emacs-lisp
1052 \(+ 1 2)
1053 #+END_SRC
1055 #+RESULTS:
1056 : 3\n\n\n"
1057 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1058 \(+ 1 2)
1059 #+END_SRC\n\n\n"
1060 (org-babel-execute-src-block)
1061 (buffer-string)))))
1063 (ert-deftest test-ob/results-in-narrowed-buffer ()
1064 "Test block execution in a narrowed buffer."
1065 ;; If results don't exist, they should be inserted in visible part
1066 ;; of the buffer.
1067 (should
1068 (equal
1069 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1070 (org-test-with-temp-text
1071 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1072 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1073 (org-babel-execute-src-block)
1074 (org-trim (buffer-string)))))
1075 (should
1076 (equal
1077 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1078 (org-test-with-temp-text
1079 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1080 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1081 (org-babel-execute-src-block)
1082 (org-trim (buffer-string)))))
1083 ;; Results in visible part of buffer, should be updated here.
1084 (should
1085 (equal
1086 "#+NAME: test
1087 #+BEGIN_SRC emacs-lisp
1088 \(+ 1 2)
1089 #+END_SRC
1091 #+RESULTS: test
1092 : 3"
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 7) (point)))
1104 (org-babel-execute-src-block)
1105 (org-trim (buffer-string)))))
1106 ;; Results in invisible part of buffer, should be updated there.
1107 (org-test-with-temp-text
1108 "#+NAME: test
1109 #+BEGIN_SRC emacs-lisp
1110 \(+ 1 2)
1111 #+END_SRC
1113 #+RESULTS: test
1116 Paragraph"
1117 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1118 (org-babel-execute-src-block)
1119 (should-not (re-search-forward "^#\\+RESULTS:" nil t))
1120 (widen)
1121 (should (should (re-search-forward "^: 3" nil t)))))
1123 (ert-deftest test-ob/specific-colnames ()
1124 "Test passing specific column names."
1125 (should
1126 (equal "#+name: input-table
1127 | id | var1 |
1128 |----+------|
1129 | 1 | bar |
1130 | 2 | baz |
1132 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1133 echo \"$data\"
1134 #+end_src
1136 #+RESULTS:
1137 | Rev | Author |
1138 |-----+--------|
1139 | 1 | bar |
1140 | 2 | baz |
1143 (org-test-with-temp-text
1144 "#+name: input-table
1145 | id | var1 |
1146 |----+------|
1147 | 1 | bar |
1148 | 2 | baz |
1150 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1151 echo \"$data\"
1152 #+end_src
1154 ;; we should find a code block
1155 (should (re-search-forward org-babel-src-block-regexp nil t))
1156 (goto-char (match-beginning 0))
1157 ;; now that we've located the code block, it may be evaluated
1158 (org-babel-execute-src-block)
1159 (buffer-string)))))
1161 (ert-deftest test-ob/location-of-header-arg-eval ()
1162 "Test location of header argument evaluation."
1163 (org-test-with-temp-text "
1164 #+name: top-block
1165 #+begin_src emacs-lisp :var pt=(point)
1167 #+end_src
1169 #+name: bottom-block
1170 #+begin_src emacs-lisp :var pt=top-block()
1172 #+end_src
1174 ;; the value of the second block should be greater than the first
1175 (should
1176 (< (progn (re-search-forward org-babel-src-block-regexp nil t)
1177 (goto-char (match-beginning 0))
1178 (prog1 (save-match-data (org-babel-execute-src-block))
1179 (goto-char (match-end 0))))
1180 (progn (re-search-forward org-babel-src-block-regexp nil t)
1181 (goto-char (match-beginning 0))
1182 (org-babel-execute-src-block))))))
1184 (ert-deftest test-ob/preserve-results-indentation ()
1185 "Preserve indentation when executing a src block."
1186 (should
1187 (equal '(2 2)
1188 (org-test-with-temp-text
1189 " #+begin_src emacs-lisp\n (+ 1 1)\n #+end_src"
1190 (org-babel-execute-src-block)
1191 (buffer-string)
1192 (let ((case-fold-search t)) (search-forward "#+results:"))
1193 ;; Check if both #+RESULTS: keyword and actual results are
1194 ;; indented by 2 columns.
1195 (list (org-get-indentation)
1196 (progn (forward-line) (org-get-indentation)))))))
1198 (ert-deftest test-ob/safe-header-args ()
1199 "Detect safe and unsafe header args."
1200 (let ((safe-args '((:cache . "foo")
1201 (:results . "output")
1202 (:eval . "never")
1203 (:eval . "query")))
1204 (unsafe-args '((:eval . "yes")
1205 (:results . "output file")
1206 (:foo . "bar")))
1207 (malformed-args '((bar . "foo")
1208 ("foo" . "bar")
1209 :foo))
1210 (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
1211 (dolist (arg safe-args)
1212 (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
1213 (dolist (arg unsafe-args)
1214 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1215 (dolist (arg malformed-args)
1216 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1217 (should (not (funcall safe-p (append safe-args unsafe-args))))))
1219 (ert-deftest test-ob/noweb-expansions-in-cache ()
1220 "Ensure that noweb expansions are expanded before caching."
1221 (let ((noweb-expansions-in-cache-var 0))
1222 (org-test-with-temp-text "
1223 #+name: foo
1224 #+begin_src emacs-lisp
1225 \"I said\"
1226 #+end_src
1228 #+name: bar
1229 #+begin_src emacs-lisp :noweb yes :cache yes
1230 (setq noweb-expansions-in-cache-var
1231 (+ 1 noweb-expansions-in-cache-var))
1232 (concat <<foo>> \" check noweb expansions\")
1233 #+end_src
1235 ;; run the second block to create the cache
1236 (goto-char (point-min))
1237 (re-search-forward (regexp-quote "#+name: bar"))
1238 (should (string= "I said check noweb expansions"
1239 (org-babel-execute-src-block)))
1240 (should (= noweb-expansions-in-cache-var 1))
1241 ;; change the value of the first block
1242 (goto-char (point-min))
1243 (re-search-forward (regexp-quote "said"))
1244 (goto-char (match-beginning 0))
1245 (insert "haven't ")
1246 (re-search-forward (regexp-quote "#+name: bar"))
1247 (should (string= "I haven't said check noweb expansions"
1248 (org-babel-execute-src-block)))
1249 (should (= noweb-expansions-in-cache-var 2)))))
1251 (ert-deftest test-org-babel/file-ext-and-output-dir ()
1252 (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
1253 (org-babel-next-src-block)
1254 (should (equal "file-ext-basic.txt"
1255 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1256 (org-babel-next-src-block)
1257 (should (equal "foo/file-ext-dir-relative.txt"
1258 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1259 (org-babel-next-src-block)
1260 (should (equal "foo/file-ext-dir-relative-slash.txt"
1261 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1262 (org-babel-next-src-block)
1263 (should (equal "/tmp/file-ext-dir-absolute.txt"
1264 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1265 (org-babel-next-src-block)
1266 (should (equal "foo.bar"
1267 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1268 (org-babel-next-src-block)
1269 (should (equal "xxx/foo.bar"
1270 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1273 (provide 'test-ob)
1275 ;;; test-ob ends here