ob-core: Refactor `org-babel-where-is-src-block-head'
[org-mode.git] / testing / lisp / test-ob.el
blobce28435b6a8ce676aaa9326f01d41edc398b657b
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 (should
519 (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
520 (org-test-with-temp-text "#+begin_src org :results silent
521 ,#+begin_src emacs-lisp
522 , 'foo
523 ,#+end_src
524 #+end_src"
525 (let ((org-edit-src-content-indentation 2)
526 (org-src-preserve-indentation nil))
527 (org-babel-execute-src-block))))))
529 (ert-deftest test-org-babel/partial-nested-code-block ()
530 "Test nested code blocks inside code blocks don't cause problems."
531 (org-test-with-temp-text "#+begin_src org :results silent
532 ,#+begin_src emacs-lisp
533 #+end_src"
534 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
536 (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
537 (org-test-with-temp-text "#+NAME: foo
538 #+BEGIN_SRC emacs-lisp
539 'foo
540 #+END_SRC\n"
541 (org-babel-next-src-block 1)
542 (should (eq 'foo (org-babel-execute-src-block)))
543 (goto-char (point-min))
544 (org-babel-next-src-block 1)
545 (should (looking-at org-babel-src-block-regexp))))
547 (ert-deftest test-ob/catches-all-references ()
548 (org-test-with-temp-text "
549 #+NAME: literal-example
550 #+BEGIN_EXAMPLE
551 A literal example
552 on two lines
553 #+END_EXAMPLE
555 #+NAME: read-literal-example
556 #+BEGIN_SRC emacs-lisp :var x=literal-example
557 (concatenate 'string x \" for me.\")
558 #+END_SRC"
559 (org-babel-next-src-block 1)
560 (should (string= (org-babel-execute-src-block)
561 "A literal example\non two lines\n for me."))))
563 (ert-deftest test-ob/resolve-code-blocks-before-data-blocks ()
564 (org-test-with-temp-text "
565 #+name: foo
566 : bar
568 #+name: foo
569 #+begin_src emacs-lisp
570 \"baz\"
571 #+end_src
573 #+begin_src emacs-lisp :var foo=foo
575 #+end_src"
576 (org-babel-next-src-block 2)
577 (should (string= (org-babel-execute-src-block) "baz"))))
579 (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
580 (org-test-with-temp-text "
581 #+tblname: base_plus
582 | 1 |
583 | 2 |
585 #+tblname: base
586 | 3 |
587 | 4 |
589 #+begin_src emacs-lisp :var x=base
591 #+end_src"
592 (org-babel-next-src-block 1)
593 (should (equal (org-babel-execute-src-block) '((3) (4))))))
595 (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
596 (org-test-with-temp-text "
597 #+name: base_plus
598 #+begin_src emacs-lisp
599 'bar
600 #+end_src
602 #+name: base
603 #+begin_src emacs-lisp
604 'foo
605 #+end_src
607 #+begin_src emacs-lisp :var x=base
609 #+end_src"
610 (org-babel-next-src-block 3)
611 (should (equal (org-babel-execute-src-block) "foo"))))
613 (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
614 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
615 (+ a b c d)
616 #+end_src
618 (should (= 10 (org-babel-execute-src-block)))))
620 (ert-deftest test-ob/org-babel-update-intermediate ()
621 (org-test-with-temp-text "#+name: foo
622 #+begin_src emacs-lisp
624 #+end_src
626 #+results: foo
629 #+begin_src emacs-lisp :var it=foo
630 (+ it 1)
631 #+end_src"
632 (let ((org-babel-update-intermediate nil))
633 (goto-char (point-min))
634 (org-babel-next-src-block 2)
635 (should (= 3 (org-babel-execute-src-block)))
636 (goto-char (point-min))
637 (forward-line 6)
638 (should (looking-at ": 4")))
639 (let ((org-babel-update-intermediate t))
640 (goto-char (point-min))
641 (org-babel-next-src-block 2)
642 (should (= 3 (org-babel-execute-src-block)))
643 (goto-char (point-min))
644 (forward-line 6)
645 (should (looking-at ": 2")))))
647 (ert-deftest test-ob/eval-header-argument ()
648 (flet ((check-eval (eval runp)
649 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
650 (setq foo :evald)
651 #+end_src" eval)
652 (let ((foo :not-run))
653 (if runp
654 (progn (should (org-babel-execute-src-block))
655 (should (eq foo :evald)))
656 (progn (should-not (org-babel-execute-src-block))
657 (should-not (eq foo :evald))))))))
658 (check-eval "never" nil)
659 (check-eval "no" nil)
660 (check-eval "never-export" t)
661 (check-eval "no-export" t)
662 (let ((org-babel-exp-reference-buffer (current-buffer)))
663 (check-eval "never" nil)
664 (check-eval "no" nil)
665 (check-eval "never-export" nil)
666 (check-eval "no-export" nil))))
668 (ert-deftest test-ob/noweb-expansion-1 ()
669 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
670 <<foo>>
671 #+end_src
673 #+name: foo
674 #+begin_src sh
676 #+end_src"
677 (should (string= (org-babel-expand-noweb-references) "bar"))))
679 (ert-deftest test-ob/noweb-expansion-2 ()
680 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
681 <<foo>>
682 #+end_src
684 #+name: foo
685 #+begin_src sh :noweb-sep \"\"
687 #+end_src
689 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
691 #+end_src"
692 (should (string= (org-babel-expand-noweb-references) "barbaz"))))
694 (ert-deftest test-ob/splitting-variable-lists-in-references ()
695 (org-test-with-temp-text ""
696 (should (= 1 (length (org-babel-ref-split-args
697 "a=\"this, no work\""))))
698 (should (= 2 (length (org-babel-ref-split-args
699 "a=\"this, no work\", b=1"))))))
701 (ert-deftest test-ob/org-babel-balanced-split ()
702 (should (equal
703 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
704 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
705 '((32 9) . 58)))))
707 (ert-deftest test-ob/commented-last-block-line-no-var ()
708 (org-test-with-temp-text-in-file "
709 #+begin_src emacs-lisp
711 #+end_src"
712 (org-babel-next-src-block)
713 (org-ctrl-c-ctrl-c)
714 (should (re-search-forward "\\#\\+results:" nil t))
715 (forward-line)
716 (should
717 (string=
719 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
720 (org-test-with-temp-text-in-file "
721 #+begin_src emacs-lisp
722 \"some text\";;
723 #+end_src"
724 (org-babel-next-src-block)
725 (org-ctrl-c-ctrl-c)
726 (should (re-search-forward "\\#\\+results:" nil t))
727 (forward-line)
728 (should
729 (string=
730 ": some text"
731 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
733 (ert-deftest test-ob/commented-last-block-line-with-var ()
734 (org-test-with-temp-text-in-file "
735 #+begin_src emacs-lisp :var a=1
737 #+end_src"
738 (org-babel-next-src-block)
739 (org-ctrl-c-ctrl-c)
740 (re-search-forward "\\#\\+results:" nil t)
741 (forward-line)
742 (should (string=
744 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
745 (org-test-with-temp-text-in-file "
746 #+begin_src emacs-lisp :var a=2
748 #+end_src"
749 (org-babel-next-src-block)
750 (org-ctrl-c-ctrl-c)
751 (re-search-forward "\\#\\+results:" nil t)
752 (forward-line)
753 (should (string=
754 ": 2"
755 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
757 (defun test-ob-verify-result-and-removed-result (result buffer-text)
758 "Test helper function to test `org-babel-remove-result'.
759 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
760 and the result of execution is verified against RESULT.
762 The block is actually executed /twice/ to ensure result
763 replacement happens correctly."
764 (org-test-with-temp-text
765 buffer-text
766 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
767 (should (re-search-forward "\\#\\+results:" nil t))
768 (forward-line)
769 (should (string= result
770 (buffer-substring-no-properties
771 (point-at-bol)
772 (- (point-max) 16))))
773 (org-babel-previous-src-block) (org-babel-remove-result)
774 (should (string= buffer-text
775 (buffer-substring-no-properties
776 (point-min) (point-max))))))
778 (ert-deftest test-ob/org-babel-remove-result--results-default ()
779 "Test `org-babel-remove-result' with default :results."
780 (mapcar (lambda (language)
781 (test-ob-verify-result-and-removed-result
782 "\n"
783 (concat
784 "* org-babel-remove-result
785 #+begin_src " language "
786 #+end_src
788 * next heading")))
789 '("sh" "emacs-lisp")))
791 (ert-deftest test-ob/org-babel-remove-result--results-list ()
792 "Test `org-babel-remove-result' with :results list."
793 (test-ob-verify-result-and-removed-result
794 "- 1
797 - (quote (4 5))"
799 "* org-babel-remove-result
800 #+begin_src emacs-lisp :results list
801 '(1 2 3 '(4 5))
802 #+end_src
804 * next heading"))
806 (ert-deftest test-ob/org-babel-results-indented-wrap ()
807 "Ensure that wrapped results are inserted correction when indented.
808 If not inserted correctly then the second evaluation will fail
809 trying to find the :END: marker."
810 (org-test-with-temp-text
811 "- indented
812 #+begin_src sh :results file wrap
813 echo test.txt
814 #+end_src"
815 (org-babel-next-src-block 1)
816 (org-babel-execute-src-block)
817 (org-babel-execute-src-block)))
819 (ert-deftest test-ob/file-desc-header-argument ()
820 "Test that the :file-desc header argument is used."
821 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
822 \"foo\"
823 #+end_src
825 #+begin_src emacs-lisp :results file :file-desc
826 \"foo\"
827 #+end_src"
828 (org-babel-execute-src-block)
829 (org-babel-next-src-block 1)
830 (org-babel-execute-src-block)
831 (goto-char (point-min))
832 (should (search-forward "[[file:foo][bar]]" nil t))
833 (should (search-forward "[[file:foo][foo]]" nil t))))
835 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
836 "Test `org-babel-remove-result' with :results pp."
837 (test-ob-verify-result-and-removed-result
838 ": \"I /am/ working!\""
840 "* org-babel-remove-result
841 #+begin_src emacs-lisp :results pp
842 \"I /am/ working!\")
843 #+end_src
845 * next heading"))
847 (ert-deftest test-org-babel/inline-src_blk-preceded-punct-preceded-by-point ()
848 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
849 (org-test-with-temp-text
850 test-line
851 (forward-char 1)
852 (org-ctrl-c-ctrl-c)
853 (should (re-search-forward "=\"x\"=" nil t))
854 (forward-line))))
856 (ert-deftest test-ob/commented-last-block-line-with-var ()
857 (org-test-with-temp-text-in-file "
858 #+begin_src emacs-lisp :var a=1
860 #+end_src"
861 (org-babel-next-src-block)
862 (org-ctrl-c-ctrl-c)
863 (re-search-forward "\\#\\+results:" nil t)
864 (forward-line)
865 (should (string=
867 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
868 (org-test-with-temp-text-in-file "
869 #+begin_src emacs-lisp :var a=2
871 #+end_src"
872 (org-babel-next-src-block)
873 (org-ctrl-c-ctrl-c)
874 (re-search-forward "\\#\\+results:" nil t)
875 (forward-line)
876 (should (string=
877 ": 2"
878 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
880 (defun test-ob-verify-result-and-removed-result (result buffer-text)
881 "Test helper function to test `org-babel-remove-result'.
882 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
883 and the result of execution is verified against RESULT.
885 The block is actually executed /twice/ to ensure result
886 replacement happens correctly."
887 (org-test-with-temp-text
888 buffer-text
889 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
890 (should (re-search-forward "\\#\\+results:" nil t))
891 (forward-line)
892 (should (string= result
893 (buffer-substring-no-properties
894 (point-at-bol)
895 (- (point-max) 16))))
896 (org-babel-previous-src-block) (org-babel-remove-result)
897 (should (string= buffer-text
898 (buffer-substring-no-properties
899 (point-min) (point-max))))))
901 (ert-deftest test-ob/org-babel-remove-result--results-default ()
902 "Test `org-babel-remove-result' with default :results."
903 (mapcar (lambda (language)
904 (test-ob-verify-result-and-removed-result
905 "\n"
906 (concat
907 "* org-babel-remove-result
908 #+begin_src " language "
909 #+end_src
911 * next heading")))
912 '("sh" "emacs-lisp")))
914 (ert-deftest test-ob/org-babel-remove-result--results-list ()
915 "Test `org-babel-remove-result' with :results list."
916 (test-ob-verify-result-and-removed-result
917 "- 1
920 - (quote (4 5))"
922 "* org-babel-remove-result
923 #+begin_src emacs-lisp :results list
924 '(1 2 3 '(4 5))
925 #+end_src
927 * next heading"))
929 (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
930 "Test `org-babel-remove-result' with :results wrap."
931 (test-ob-verify-result-and-removed-result
932 ":RESULTS:
933 hello there
934 :END:"
936 "* org-babel-remove-result
938 #+begin_src emacs-lisp :results wrap
939 \"hello there\"
940 #+end_src
942 * next heading"))
944 (ert-deftest test-ob/org-babel-remove-result--results-org ()
945 "Test `org-babel-remove-result' with :results org."
946 (test-ob-verify-result-and-removed-result
947 "#+BEGIN_SRC org
948 ,* heading
949 ,** subheading
950 content
951 #+END_SRC"
953 "* org-babel-remove-result
954 #+begin_src emacs-lisp :results org
955 \"* heading
956 ,** subheading
957 content\"
958 #+end_src
960 * next heading"))
962 (ert-deftest test-ob/org-babel-remove-result--results-html ()
963 "Test `org-babel-remove-result' with :results html."
964 (test-ob-verify-result-and-removed-result
965 "#+BEGIN_HTML
966 <head><body></body></head>
967 #+END_HTML"
969 "* org-babel-remove-result
970 #+begin_src emacs-lisp :results html
971 \"<head><body></body></head>\"
972 #+end_src
974 * next heading"))
976 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
977 "Test `org-babel-remove-result' with :results latex."
978 (test-ob-verify-result-and-removed-result
979 "#+BEGIN_LaTeX
980 Line 1
981 Line 2
982 Line 3
983 #+END_LaTeX"
985 "* org-babel-remove-result
986 #+begin_src emacs-lisp :results latex
987 \"Line 1
988 Line 2
989 Line 3\"
990 #+end_src
992 * next heading"))
994 (ert-deftest test-ob/org-babel-remove-result--results-code ()
995 "Test `org-babel-remove-result' with :results code."
997 (test-ob-verify-result-and-removed-result
998 "#+BEGIN_SRC emacs-lisp
999 \"I am working!\"
1000 #+END_SRC"
1002 "* org-babel-remove-result
1003 #+begin_src emacs-lisp :results code
1004 (message \"I am working!\")
1005 #+end_src
1007 * next heading"))
1009 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
1010 "Test `org-babel-remove-result' with :results pp."
1011 (test-ob-verify-result-and-removed-result
1012 ": \"I /am/ working!\""
1014 "* org-babel-remove-result
1015 #+begin_src emacs-lisp :results pp
1016 \"I /am/ working!\")
1017 #+end_src
1019 * next heading"))
1021 (ert-deftest test-ob/results-do-not-replace-code-blocks ()
1022 (org-test-with-temp-text "Block two has a space after the name.
1024 #+name: foo
1025 #+begin_src emacs-lisp
1027 #+end_src
1029 #+name: foo
1030 #+begin_src emacs-lisp
1032 #+end_src
1034 #+name: foo
1035 #+begin_src emacs-lisp
1037 #+end_src
1039 #+RESULTS: foo
1040 : foo
1042 (dolist (num '(1 2 3))
1043 ;; execute the block
1044 (goto-char (point-min))
1045 (org-babel-next-src-block num) (org-babel-execute-src-block)
1046 ;; check the results
1047 (goto-char (point-max))
1048 (move-beginning-of-line 0)
1049 (should (looking-at (format ": %d" num))))))
1051 (ert-deftest test-ob/blocks-with-spaces ()
1052 "Test expansion of blocks followed by blank lines."
1053 (should
1054 (equal "#+BEGIN_SRC emacs-lisp
1055 \(+ 1 2)
1056 #+END_SRC
1058 #+RESULTS:
1059 : 3\n\n\n"
1060 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1061 \(+ 1 2)
1062 #+END_SRC\n\n\n"
1063 (let ((org-babel-next-src-block "RESULTS"))
1064 (org-babel-execute-src-block))
1065 (buffer-string)))))
1067 (ert-deftest test-ob/results-in-narrowed-buffer ()
1068 "Test block execution in a narrowed buffer."
1069 ;; If results don't exist, they should be inserted in visible part
1070 ;; of the buffer.
1071 (should
1072 (equal
1073 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1074 (org-test-with-temp-text
1075 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1076 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1077 (let ((org-babel-results-keyword "RESULTS"))
1078 (org-babel-execute-src-block))
1079 (org-trim (buffer-string)))))
1080 (should
1081 (equal
1082 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1083 (org-test-with-temp-text
1084 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1085 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1086 (let ((org-babel-results-keyword "RESULTS"))
1087 (org-babel-execute-src-block))
1088 (org-trim (buffer-string)))))
1089 ;; Results in visible part of buffer, should be updated here.
1090 (should
1091 (equal
1092 "#+NAME: test
1093 #+BEGIN_SRC emacs-lisp
1094 \(+ 1 2)
1095 #+END_SRC
1097 #+RESULTS: test
1098 : 3"
1099 (org-test-with-temp-text
1100 "#+NAME: test
1101 #+BEGIN_SRC emacs-lisp
1102 \(+ 1 2)
1103 #+END_SRC
1105 #+RESULTS: test
1108 Paragraph"
1109 (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
1110 (let ((org-babel-results-keyword "RESULTS"))
1111 (org-babel-execute-src-block))
1112 (org-trim (buffer-string)))))
1113 ;; Results in invisible part of buffer, should be updated there.
1114 (org-test-with-temp-text
1115 "#+NAME: test
1116 #+BEGIN_SRC emacs-lisp
1117 \(+ 1 2)
1118 #+END_SRC
1120 #+RESULTS: test
1123 Paragraph"
1124 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1125 (let ((org-babel-results-keyword "RESULTS"))
1126 (org-babel-execute-src-block))
1127 (should-not (re-search-forward "^#\\+RESULTS:" nil t))
1128 (widen)
1129 (should (should (re-search-forward "^: 3" nil t)))))
1131 (ert-deftest test-ob/specific-colnames ()
1132 "Test passing specific column names."
1133 (should
1134 (equal "#+name: input-table
1135 | id | var1 |
1136 |----+------|
1137 | 1 | bar |
1138 | 2 | baz |
1140 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1141 echo \"$data\"
1142 #+end_src
1144 #+RESULTS:
1145 | Rev | Author |
1146 |-----+--------|
1147 | 1 | bar |
1148 | 2 | baz |
1151 (org-test-with-temp-text
1152 "#+name: input-table
1153 | id | var1 |
1154 |----+------|
1155 | 1 | bar |
1156 | 2 | baz |
1158 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1159 echo \"$data\"
1160 #+end_src
1162 ;; we should find a code block
1163 (should (re-search-forward org-babel-src-block-regexp nil t))
1164 (goto-char (match-beginning 0))
1165 ;; now that we've located the code block, it may be evaluated
1166 (let ((org-babel-execute-src-block "RESULTS"))
1167 (org-babel-execute-src-block))
1168 (buffer-string)))))
1170 (ert-deftest test-ob/location-of-header-arg-eval ()
1171 "Test location of header argument evaluation."
1172 (org-test-with-temp-text "
1173 #+name: top-block
1174 #+begin_src emacs-lisp :var pt=(point)
1176 #+end_src
1178 #+name: bottom-block
1179 #+begin_src emacs-lisp :var pt=top-block()
1181 #+end_src
1183 ;; the value of the second block should be greater than the first
1184 (should
1185 (< (progn (re-search-forward org-babel-src-block-regexp nil t)
1186 (goto-char (match-beginning 0))
1187 (prog1 (save-match-data (org-babel-execute-src-block))
1188 (goto-char (match-end 0))))
1189 (progn (re-search-forward org-babel-src-block-regexp nil t)
1190 (goto-char (match-beginning 0))
1191 (org-babel-execute-src-block))))))
1193 (ert-deftest test-ob/preserve-results-indentation ()
1194 "Preserve indentation when executing a src block."
1195 (should
1196 (equal '(2 2)
1197 (org-test-with-temp-text
1198 " #+begin_src emacs-lisp\n (+ 1 1)\n #+end_src"
1199 (org-babel-execute-src-block)
1200 (buffer-string)
1201 (let ((case-fold-search t)) (search-forward "#+results:"))
1202 ;; Check if both #+RESULTS: keyword and actual results are
1203 ;; indented by 2 columns.
1204 (list (org-get-indentation)
1205 (progn (forward-line) (org-get-indentation)))))))
1207 (ert-deftest test-ob/safe-header-args ()
1208 "Detect safe and unsafe header args."
1209 (let ((safe-args '((:cache . "foo")
1210 (:results . "output")
1211 (:eval . "never")
1212 (:eval . "query")))
1213 (unsafe-args '((:eval . "yes")
1214 (:results . "output file")
1215 (:foo . "bar")))
1216 (malformed-args '((bar . "foo")
1217 ("foo" . "bar")
1218 :foo))
1219 (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
1220 (dolist (arg safe-args)
1221 (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
1222 (dolist (arg unsafe-args)
1223 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1224 (dolist (arg malformed-args)
1225 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1226 (should (not (funcall safe-p (append safe-args unsafe-args))))))
1228 (ert-deftest test-ob/noweb-expansions-in-cache ()
1229 "Ensure that noweb expansions are expanded before caching."
1230 (let ((noweb-expansions-in-cache-var 0))
1231 (org-test-with-temp-text "
1232 #+name: foo
1233 #+begin_src emacs-lisp
1234 \"I said\"
1235 #+end_src
1237 #+name: bar
1238 #+begin_src emacs-lisp :noweb yes :cache yes
1239 (setq noweb-expansions-in-cache-var
1240 (+ 1 noweb-expansions-in-cache-var))
1241 (concat <<foo>> \" check noweb expansions\")
1242 #+end_src
1244 ;; run the second block to create the cache
1245 (goto-char (point-min))
1246 (re-search-forward (regexp-quote "#+name: bar"))
1247 (should (string= "I said check noweb expansions"
1248 (org-babel-execute-src-block)))
1249 (should (= noweb-expansions-in-cache-var 1))
1250 ;; change the value of the first block
1251 (goto-char (point-min))
1252 (re-search-forward (regexp-quote "said"))
1253 (goto-char (match-beginning 0))
1254 (insert "haven't ")
1255 (re-search-forward (regexp-quote "#+name: bar"))
1256 (should (string= "I haven't said check noweb expansions"
1257 (org-babel-execute-src-block)))
1258 (should (= noweb-expansions-in-cache-var 2)))))
1260 (ert-deftest test-org-babel/file-ext-and-output-dir ()
1261 (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
1262 (org-babel-next-src-block)
1263 (should (equal "file-ext-basic.txt"
1264 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1265 (org-babel-next-src-block)
1266 (should (equal "foo/file-ext-dir-relative.txt"
1267 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1268 (org-babel-next-src-block)
1269 (should (equal "foo/file-ext-dir-relative-slash.txt"
1270 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1271 (org-babel-next-src-block)
1272 (should (equal "/tmp/file-ext-dir-absolute.txt"
1273 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1274 (org-babel-next-src-block)
1275 (should (equal "foo.bar"
1276 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1277 (org-babel-next-src-block)
1278 (should (equal "xxx/foo.bar"
1279 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1282 (provide 'test-ob)
1284 ;;; test-ob ends here