Merge branch 'maint'
[org-mode.git] / testing / lisp / test-ob.el
blob2a929cd8fc53dc93ab547dd0866bcd42648bfdf7
1 ;;; test-ob.el --- tests for ob.el
3 ;; Copyright (c) 2010-2015 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/indented-cached-org-bracket-link ()
24 "When the result of a source block is a cached indented link it
25 should still return the link."
26 (should
27 (let ((default-directory temporary-file-directory))
28 (org-test-with-temp-text
30 * Test
31 #+<point>BEGIN_SRC emacs-lisp :file test.txt :cache yes
32 (message \"test\")
33 #+END_SRC"
34 ;; Execute twice as the first time creates the cache.
35 (org-babel-execute-src-block)
36 (string= (concat default-directory "test.txt")
37 (org-babel-execute-src-block))))))
39 (ert-deftest test-org-babel/multi-line-header-regexp ()
40 (should(equal "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
41 org-babel-multi-line-header-regexp))
42 ;;TODO can be optimised - and what about blah4 blah5 blah6?
43 (should (string-match
44 org-babel-multi-line-header-regexp
45 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
46 (should
47 (equal
48 "blah1 blah2 blah3 \t"
49 (match-string
51 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
53 ;;TODO Check - should this fail?
54 (should
55 (not (org-test-string-exact-match
56 org-babel-multi-line-header-regexp
57 " \t #+headers : blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))))
59 (ert-deftest test-org-babel/src-block-regexp ()
60 (let ((test-block
61 (concat
62 "#+begin_src language -n-r-a-b -c :argument-1 yes :argument-2 no\n"
63 "echo this is a test\n"
64 "echo Currently in ' $PWD\n"
65 "#+end_src"))
66 (language "language")
67 (flags "-n-r-a-b -c ")
68 (arguments ":argument-1 yes :argument-2 no")
69 (body "echo this is a test\necho Currently in ' $PWD\n"))
70 (should (string-match org-babel-src-block-regexp test-block))
71 (should (string-match org-babel-src-block-regexp (upcase test-block)))
72 (should (equal language (match-string 2 test-block)))
73 ;;TODO Consider refactoring
74 (should (equal flags (match-string 3 test-block)))
75 (should (equal arguments (match-string 4 test-block)))
76 (should (equal body (match-string 5 test-block)))
77 ;;no switches
78 (should (org-test-string-exact-match
79 org-babel-src-block-regexp
80 (replace-regexp-in-string flags "" test-block)))
81 ;;no header arguments
82 (should (org-test-string-exact-match
83 org-babel-src-block-regexp
84 (replace-regexp-in-string arguments "" test-block)))
85 ;; should be valid with no body
86 (should (org-test-string-exact-match
87 org-babel-src-block-regexp
88 (replace-regexp-in-string body "" test-block)))))
90 (ert-deftest test-org-babel/get-header ()
91 (should (not (org-babel-get-header
92 org-babel-default-header-args :doesnt-exist)))
93 (should(equal '((:session . "none"))
94 (org-babel-get-header
95 org-babel-default-header-args :session)))
96 (should(equal '((:session . "none"))
97 (org-babel-get-header
98 org-babel-default-header-args :session nil)))
99 (should (not (org-babel-get-header
100 org-babel-default-header-args :SESSION)))
101 (should (equal '((:tangle . "no"))
102 (org-babel-get-header
103 org-babel-default-header-args :tangle)))
104 ;; with OTHERS option
105 (should (equal org-babel-default-header-args
106 (org-babel-get-header
107 org-babel-default-header-args :doesnt-exist 'others)))
108 (should (equal org-babel-default-header-args
109 (org-babel-get-header
110 org-babel-default-header-args nil 'others)))
111 (should (null
112 (assoc :noweb
113 (org-babel-get-header
114 org-babel-default-header-args :noweb 'others)))))
116 (ert-deftest test-org-babel/default-inline-header-args ()
117 (should(equal
118 '((:session . "none")
119 (:results . "replace")
120 (:exports . "results")
121 (:hlines . "yes"))
122 org-babel-default-inline-header-args)))
124 (ert-deftest ob-test/org-babel-combine-header-arg-lists ()
125 (let ((results (org-babel-combine-header-arg-lists
126 '((foo . :any)
127 (bar)
128 (baz . ((foo bar) (baz)))
129 (qux . ((foo bar baz qux)))
130 (quux . ((foo bar))))
131 '((bar)
132 (baz . ((baz)))
133 (quux . :any)))))
134 (dolist (pair '((foo . :any)
135 (bar)
136 (baz . ((baz)))
137 (quux . :any)
138 (qux . ((foo bar baz qux)))))
139 (should (equal (cdr pair)
140 (cdr (assoc (car pair) results)))))))
142 ;;; ob-get-src-block-info
143 (ert-deftest test-org-babel/get-src-block-info-language ()
144 (org-test-at-marker nil org-test-file-ob-anchor
145 (let ((info (org-babel-get-src-block-info)))
146 (should (string= "emacs-lisp" (nth 0 info))))))
148 (ert-deftest test-org-babel/get-src-block-info-body ()
149 (org-test-at-marker nil org-test-file-ob-anchor
150 (let ((info (org-babel-get-src-block-info)))
151 (should (string-match (regexp-quote org-test-file-ob-anchor)
152 (nth 1 info))))))
154 (ert-deftest test-org-babel/get-src-block-info-tangle ()
155 (org-test-at-marker nil org-test-file-ob-anchor
156 (let ((info (org-babel-get-src-block-info)))
157 (should (string= "no" (cdr (assoc :tangle (nth 2 info))))))))
159 (ert-deftest test-org-babel/elisp-in-header-arguments ()
160 "Test execution of elisp forms in header arguments."
161 (org-test-with-temp-text-in-file "
163 * elisp forms in header arguments
164 :PROPERTIES:
165 :var: prop = (* 7 6)
166 :END:
167 #+begin_src emacs-lisp
168 prop
169 #+end_src"
170 (goto-char (point-min))
171 (org-babel-next-src-block)
172 (let ((info (org-babel-get-src-block-info)))
173 (should (= 42 (org-babel-execute-src-block))))))
175 (ert-deftest test-org-babel/simple-named-code-block ()
176 "Test that simple named code blocks can be evaluated."
177 (org-test-with-temp-text-in-file "
179 #+name: i-have-a-name
180 #+begin_src emacs-lisp
182 #+end_src"
183 (org-babel-next-src-block 1)
184 (should (= 42 (org-babel-execute-src-block)))))
186 (ert-deftest test-org-babel/simple-variable-resolution ()
187 "Test that simple variable resolution is working."
188 (org-test-with-temp-text-in-file "
190 #+name: four
191 #+begin_src emacs-lisp
192 (list 1 2 3 4)
193 #+end_src
195 #+begin_src emacs-lisp :var four=four
196 (length four)
197 #+end_src"
199 (org-babel-next-src-block 2)
200 (should (= 4 (org-babel-execute-src-block)))
201 (forward-line 5)
202 (should (string= ": 4" (buffer-substring
203 (point-at-bol)
204 (point-at-eol))))))
206 (ert-deftest test-org-babel/multi-line-header-arguments ()
207 "Test that multi-line header arguments and can be read."
208 (org-test-with-temp-text-in-file "
210 #+headers: :var letters='(a b c d e f g)
211 #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
212 (require 'cl)
213 (defalias 'my-map (if (org-version-check \"24.2.50\" \"cl\" :predicate)
214 'cl-map
215 'map))
216 (my-map 'list #'list numbers letters)
217 #+end_src"
219 (org-babel-next-src-block)
220 (let ((results (org-babel-execute-src-block)))
221 (should(equal 'a (cadr (assoc 1 results))))
222 (should(equal 'd (cadr (assoc 4 results)))))))
224 (ert-deftest test-org-babel/parse-header-args ()
225 (org-test-with-temp-text-in-file "
227 #+begin_src example-lang :session :results output :var num=9
228 the body
229 #+end_src"
231 (org-babel-next-src-block)
232 (let* ((info (org-babel-get-src-block-info))
233 (params (nth 2 info)))
234 (message "%S" params)
235 (should (equal "example-lang" (nth 0 info)))
236 (should (string= "the body" (org-babel-trim (nth 1 info))))
237 (should-not (member '(:session\ \ \ \ ) params))
238 (should (equal '(:session) (assoc :session params)))
239 (should (equal '(:result-type . output) (assoc :result-type params)))
240 (should (equal '(num . 9) (cdr (assoc :var params)))))))
242 (ert-deftest test-org-babel/parse-header-args2 ()
243 (org-test-with-temp-text-in-file "
245 * resolving sub-trees as references
247 #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
248 (length text)
249 #+end_src
251 #+begin_src org :noweb yes
252 <<simple-subtree>>
253 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
254 #+end_src
256 ** simple subtree with custom ID
257 :PROPERTIES:
258 :CUSTOM_ID: simple-subtree
259 :END:
260 this is simple"
262 (should (string-match (regexp-quote "this is simple")
263 (org-babel-ref-resolve "simple-subtree")))
264 (org-babel-next-src-block)
265 (should (= 14 (org-babel-execute-src-block)))))
267 (ert-deftest test-org-babel/inline-src-blocks ()
268 (should
269 (= 1
270 (org-test-with-temp-text
271 "In the middle <point>src_emacs-lisp{(+ 0 1)} of a line"
272 (org-babel-execute-src-block))))
273 (should
274 (= 2
275 (org-test-with-temp-text
276 "One at the end of a line: <point>src_emacs-lisp{(+ 1 1)}"
277 (org-babel-execute-src-block))))
278 (should
279 (= 3
280 (org-test-with-temp-text
281 "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
282 (org-babel-execute-src-block))))
283 (should
284 (= 4
285 (org-test-with-temp-text
286 "In the middle <point>src_emacs-lisp[:results silent\
287 :exports code]{(+ 3 1)} of a line"
288 (org-babel-execute-src-block))))
289 (should
290 (= 5
291 (org-test-with-temp-text
292 "One at the end of a line: <point>src_emacs-lisp[:results silent\
293 :exports code]{(+ 4 1)}"
294 (org-babel-execute-src-block))))
295 (should
296 (= 6
297 (org-test-with-temp-text
298 "src_emacs-lisp[:results silent :exports code]{(+ 5 1)}\
299 at the beginning of a line."
300 (org-babel-execute-src-block))))
301 (should
302 (= 7
303 (org-test-with-temp-text
304 "One also evaluated: <point>src_emacs-lisp[:exports both\
305 :results silent]{(+ 6 1)}"
306 (org-babel-execute-src-block)))))
308 (ert-deftest test-org-babel/org-babel-get-inline-src-block-matches ()
309 (flet ((test-at-id (id)
310 (org-test-at-id
312 (let ((test-point (point)))
313 (should (fboundp 'org-babel-get-inline-src-block-matches))
314 (should (re-search-forward "src_" nil t)) ;; 1
315 (should (org-babel-get-inline-src-block-matches))
316 (should (re-search-forward " b" nil (point-at-bol))) ;; 1
317 (should-not (org-babel-get-inline-src-block-matches))
318 (should (re-search-forward "in" nil t)) ;; 2
319 (should-not (org-babel-get-inline-src-block-matches))
320 (should (re-search-forward "echo" nil t)) ;; 2
321 (should (org-babel-get-inline-src-block-matches))
322 (should (re-search-forward "blocks" nil t)) ;; 3
323 (backward-char 7) ;; 3
324 (should (org-babel-get-inline-src-block-matches))
325 (forward-char 1) ;;3
326 (should-not (org-babel-get-inline-src-block-matches))
327 (should (re-search-forward ":results" nil t)) ;; 4
328 (should (org-babel-get-inline-src-block-matches))
329 (end-of-line)
330 (should-not (org-babel-get-inline-src-block-matches))))))
331 (test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74")
332 (test-at-id "d55dada7-de0e-4340-8061-787cccbedee5")))
334 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-1 ()
335 (let ((test-line "src_sh{echo 1}")
336 (org-babel-inline-result-wrap "=%s="))
337 ;; src_ at bol line 1...
338 (org-test-with-temp-text
339 test-line
340 (goto-char (point-min)) (org-babel-execute-maybe)
341 (should (string=
342 (concat test-line " {{{results(=1=)}}}")
343 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
344 (forward-char) (org-babel-execute-maybe)
345 (should (string=
346 (concat test-line " {{{results(=1=)}}}")
347 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
348 (re-search-forward "{{{")
349 ;;(should-error (org-ctrl-c-ctrl-c))
350 (backward-char 4) ;; last char of block body
351 (org-babel-execute-maybe)
352 (should (string=
353 (concat test-line " {{{results(=1=)}}}")
354 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
355 ;; src_ follows space line 1...
356 (let ((test-line " src_emacs-lisp{ 1 }"))
357 (org-test-with-temp-text
358 test-line
359 (should-error (org-ctrl-c-ctrl-c))
360 (forward-char) (org-babel-execute-maybe)
361 (should (string=
362 (concat test-line " {{{results(=1=)}}}")
363 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
364 (re-search-forward "{ 1 ") (org-babel-execute-maybe)
365 (should (string=
366 (concat test-line " {{{results(=1=)}}}")
367 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
368 (forward-char 6)
369 (should-error (org-ctrl-c-ctrl-c))
370 ))))
372 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-2 ()
373 ;; src_ at bol line 2...
374 (let ((test-line " src_emacs-lisp{ \"x\" }")
375 (org-babel-inline-result-wrap "=%s="))
376 (org-test-with-temp-text
377 (concat "\n" test-line)
378 (should-error (org-ctrl-c-ctrl-c))
379 (goto-char (point-min))
380 (should-error (org-ctrl-c-ctrl-c))
381 (forward-line)
382 (should-error (org-ctrl-c-ctrl-c))
383 (forward-char) (org-babel-execute-maybe)
384 (should (string=
385 (concat test-line " {{{results(=x=)}}}")
386 (buffer-substring-no-properties
387 (point-at-bol) (point-at-eol))))))
388 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
389 (org-babel-inline-result-wrap "=%s="))
390 (org-test-with-temp-text
391 test-line
392 (goto-char (point-max))
393 (insert (concat "\n" test-line " end"))
394 (re-search-backward "src") (org-babel-execute-maybe)
395 (should (string=
396 (concat test-line " {{{results(=y=)}}} end")
397 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
398 (re-search-forward "\" ") (org-babel-execute-maybe)
399 (should (string=
400 (concat test-line " {{{results(=y=)}}} end")
401 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
402 (forward-char 3)
403 (should-error (org-ctrl-c-ctrl-c)))))
405 (ert-deftest test-org-babel/inline-src_blk-manual-results-replace ()
406 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
407 (org-babel-inline-result-wrap "=%s="))
408 (org-test-with-temp-text
409 (concat "\n" test-line)
410 (should-error (org-ctrl-c-ctrl-c))
411 (goto-char (point-max))
412 (org-babel-execute-maybe)
413 (beginning-of-line)
414 (should-error (org-ctrl-c-ctrl-c))
415 (forward-char) (org-babel-execute-maybe)
416 (should (string=
417 (concat test-line " {{{results(=x=)}}}")
418 (buffer-substring-no-properties
419 (point-at-bol) (point-at-eol))))))
420 (let ((test-line (concat " Some text prior to block "
421 "src_emacs-lisp[:results replace]{ \"y\" }"))
422 (org-babel-inline-result-wrap "=%s="))
423 (org-test-with-temp-text test-line
424 (goto-char (point-max))
425 (insert (concat "\n" test-line " end"))
426 (re-search-backward "src") (org-babel-execute-maybe)
427 (should (string=
428 (concat test-line " {{{results(=y=)}}} end")
429 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
430 (re-search-forward "\" ") (org-babel-execute-maybe)
431 (should (string=
432 (concat test-line " {{{results(=y=)}}} end")
433 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
434 (forward-char 3)
435 (should-error (org-ctrl-c-ctrl-c)))))
437 (ert-deftest test-org-babel/inline-src_blk-results-silent ()
438 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
439 (org-test-with-temp-text test-line
440 (org-babel-execute-maybe)
441 (should (string= test-line
442 (buffer-substring-no-properties
443 (point-at-bol) (point-at-eol))))))
444 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
445 "[ :results silent ]{ \"y\" }")))
446 (org-test-with-temp-text
447 test-line
448 (goto-char (point-max))
449 (insert (concat "\n" test-line " end"))
450 (re-search-backward "src_") (org-babel-execute-maybe)
451 (should (string= (concat test-line " end")
452 (buffer-substring-no-properties
453 (point-at-bol) (point-at-eol))))
454 (re-search-forward "\" ") (org-babel-execute-maybe)
455 (should (string= (concat test-line " end")
456 (buffer-substring-no-properties
457 (point-at-bol) (point-at-eol))))
458 (forward-char 2)
459 (should-error (org-ctrl-c-ctrl-c)))))
461 (ert-deftest test-org-babel/inline-src_blk-results-raw ()
462 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
463 (org-test-with-temp-text test-line
464 (org-babel-execute-maybe)
465 (should (string= (concat test-line " x")
466 (buffer-string)))))
467 (let ((test-line (concat " Some text prior to block "
468 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
469 (org-test-with-temp-text (concat test-line " end")
470 (re-search-forward "src_") (org-babel-execute-maybe)
471 (should (string= (concat test-line " the end")
472 (buffer-substring-no-properties
473 (point-at-bol) (point-at-eol))))
474 (re-search-forward "\" ") (org-babel-execute-maybe)
475 (should (string= (concat test-line " the the end")
476 (buffer-substring-no-properties
477 (point-at-bol) (point-at-eol))))
478 (forward-char 2)
479 (should-error (org-ctrl-c-ctrl-c)))))
481 (ert-deftest test-org-babel/inline-src_blk-results-file ()
482 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
483 (org-test-with-temp-text
484 test-line
485 (org-babel-execute-maybe)
486 (should (string= (concat test-line " {{{results([[file:~/test-file]])}}}")
487 (buffer-substring-no-properties
488 (point-min) (point-max)))))))
490 (ert-deftest test-org-babel/inline-src_blk-results-scalar ()
491 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
492 (org-babel-inline-result-wrap "=%s="))
493 (org-test-with-temp-text
494 test-line
495 (org-babel-execute-maybe)
496 (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
497 (buffer-substring-no-properties
498 (point-min) (point-max)))))))
500 (ert-deftest test-org-babel/inline-src_blk-results-verbatim ()
501 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
502 (org-babel-inline-result-wrap "=%s="))
503 (org-test-with-temp-text
504 test-line
505 (org-babel-execute-maybe)
506 (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
507 (buffer-substring-no-properties
508 (point-min) (point-max)))))))
510 (ert-deftest test-org-babel/combining-scalar-and-raw-result-types ()
511 (org-test-with-temp-text-in-file "
513 #+begin_src sh :results scalar
514 echo \"[[file:./cv.cls]]\"
515 #+end_src
517 #+name:
518 : [[file:./cv.cls]]
520 #+begin_src sh :results raw scalar
521 echo \"[[file:./cv.cls]]\"
522 #+end_src
524 (flet ((next-result ()
525 (org-babel-next-src-block)
526 (org-babel-execute-src-block)
527 (goto-char (org-babel-where-is-src-block-result))
528 (forward-line 1)))
529 (goto-char (point-min))
530 (next-result)
531 (should (org-babel-in-example-or-verbatim))
532 (next-result)
533 (should (not (org-babel-in-example-or-verbatim))))))
535 (ert-deftest test-org-babel/no-defaut-value-for-var ()
536 "Test that the absence of a default value for a variable DOES THROW
537 a proper error."
538 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
539 (org-babel-next-src-block)
540 (let ((err
541 (should-error (org-babel-execute-src-block) :type 'error)))
542 (should
543 (equal
544 '(error
545 "Variable \"x\" must be assigned a default value")
546 err)))))
548 (ert-deftest test-org-babel/just-one-results-block ()
549 "Test that evaluating two times the same code block does not result in a
550 duplicate results block."
551 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
552 (org-babel-execute-src-block)
553 (org-babel-execute-src-block) ; second code block execution
554 (should (search-forward "Hello")) ; the string inside the source code block
555 (should (search-forward "Hello")) ; the same string in the results block
556 (should-error (search-forward "Hello"))))
558 (ert-deftest test-org-babel/nested-code-block ()
559 "Test nested code blocks inside code blocks don't cause problems."
560 (should
561 (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
562 (org-test-with-temp-text "#+begin_src org :results silent
563 ,#+begin_src emacs-lisp
564 , 'foo
565 ,#+end_src
566 #+end_src"
567 (let ((org-edit-src-content-indentation 2)
568 (org-src-preserve-indentation nil))
569 (org-babel-execute-src-block))))))
571 (ert-deftest test-org-babel/partial-nested-code-block ()
572 "Test nested code blocks inside code blocks don't cause problems."
573 (org-test-with-temp-text "#+begin_src org :results silent
574 ,#+begin_src emacs-lisp
575 #+end_src"
576 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
578 (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
579 (org-test-with-temp-text "#+NAME: foo
580 #+BEGIN_SRC emacs-lisp
581 'foo
582 #+END_SRC\n"
583 (org-babel-next-src-block 1)
584 (should (eq 'foo (org-babel-execute-src-block)))
585 (goto-char (point-min))
586 (org-babel-next-src-block 1)
587 (should (looking-at org-babel-src-block-regexp))))
589 (ert-deftest test-ob/catches-all-references ()
590 (org-test-with-temp-text "
591 #+NAME: literal-example
592 #+BEGIN_EXAMPLE
593 A literal example
594 on two lines
595 #+END_EXAMPLE
597 #+NAME: read-literal-example
598 #+BEGIN_SRC emacs-lisp :var x=literal-example
599 (concatenate 'string x \" for me.\")
600 #+END_SRC"
601 (org-babel-next-src-block 1)
602 (should (string= (org-babel-execute-src-block)
603 "A literal example\non two lines\n for me."))))
605 (ert-deftest test-ob/resolve-code-blocks-before-data-blocks ()
606 (org-test-with-temp-text "
607 #+name: foo
608 : bar
610 #+name: foo
611 #+begin_src emacs-lisp
612 \"baz\"
613 #+end_src
615 #+begin_src emacs-lisp :var foo=foo
617 #+end_src"
618 (org-babel-next-src-block 2)
619 (should (string= (org-babel-execute-src-block) "baz"))))
621 (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
622 (org-test-with-temp-text "
623 #+tblname: base_plus
624 | 1 |
625 | 2 |
627 #+tblname: base
628 | 3 |
629 | 4 |
631 #+begin_src emacs-lisp :var x=base
633 #+end_src"
634 (org-babel-next-src-block 1)
635 (should (equal (org-babel-execute-src-block) '((3) (4))))))
637 (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
638 (org-test-with-temp-text "
639 #+name: base_plus
640 #+begin_src emacs-lisp
641 'bar
642 #+end_src
644 #+name: base
645 #+begin_src emacs-lisp
646 'foo
647 #+end_src
649 #+begin_src emacs-lisp :var x=base
651 #+end_src"
652 (org-babel-next-src-block 3)
653 (should (equal (org-babel-execute-src-block) "foo"))))
655 (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
656 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
657 (+ a b c d)
658 #+end_src
660 (should (= 10 (org-babel-execute-src-block)))))
662 (ert-deftest test-ob/org-babel-update-intermediate ()
663 (org-test-with-temp-text "#+name: foo
664 #+begin_src emacs-lisp
666 #+end_src
668 #+results: foo
671 #+begin_src emacs-lisp :var it=foo
672 (+ it 1)
673 #+end_src"
674 (let ((org-babel-update-intermediate nil))
675 (goto-char (point-min))
676 (org-babel-next-src-block 2)
677 (should (= 3 (org-babel-execute-src-block)))
678 (goto-char (point-min))
679 (forward-line 6)
680 (should (looking-at ": 4")))
681 (let ((org-babel-update-intermediate t))
682 (goto-char (point-min))
683 (org-babel-next-src-block 2)
684 (should (= 3 (org-babel-execute-src-block)))
685 (goto-char (point-min))
686 (forward-line 6)
687 (should (looking-at ": 2")))))
689 (ert-deftest test-ob/eval-header-argument ()
690 (flet ((check-eval (eval runp)
691 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
692 (setq foo :evald)
693 #+end_src" eval)
694 (let ((foo :not-run))
695 (if runp
696 (progn (should (org-babel-execute-src-block))
697 (should (eq foo :evald)))
698 (progn (should-not (org-babel-execute-src-block))
699 (should-not (eq foo :evald))))))))
700 (check-eval "never" nil)
701 (check-eval "no" nil)
702 (check-eval "never-export" t)
703 (check-eval "no-export" t)
704 (let ((org-babel-exp-reference-buffer (current-buffer)))
705 (check-eval "never" nil)
706 (check-eval "no" nil)
707 (check-eval "never-export" nil)
708 (check-eval "no-export" nil))))
710 (ert-deftest test-ob/noweb-expansion-1 ()
711 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
712 <<foo>>
713 #+end_src
715 #+name: foo
716 #+begin_src sh
718 #+end_src"
719 (should (string= (org-babel-expand-noweb-references) "bar"))))
721 (ert-deftest test-ob/noweb-expansion-2 ()
722 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
723 <<foo>>
724 #+end_src
726 #+name: foo
727 #+begin_src sh :noweb-sep \"\"
729 #+end_src
731 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
733 #+end_src"
734 (should (string= (org-babel-expand-noweb-references) "barbaz"))))
736 (ert-deftest test-ob/splitting-variable-lists-in-references ()
737 (org-test-with-temp-text ""
738 (should (= 1 (length (org-babel-ref-split-args
739 "a=\"this, no work\""))))
740 (should (= 2 (length (org-babel-ref-split-args
741 "a=\"this, no work\", b=1"))))))
743 (ert-deftest test-ob/org-babel-balanced-split ()
744 (should (equal
745 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
746 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
747 '((32 9) . 58)))))
749 (ert-deftest test-ob/commented-last-block-line-no-var ()
750 (org-test-with-temp-text-in-file "
751 #+begin_src emacs-lisp
753 #+end_src"
754 (org-babel-next-src-block)
755 (org-babel-execute-maybe)
756 (should (re-search-forward "\\#\\+results:" nil t))
757 (forward-line)
758 (should
759 (string=
761 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
762 (org-test-with-temp-text-in-file "
763 #+begin_src emacs-lisp
764 \"some text\";;
765 #+end_src"
766 (org-babel-next-src-block)
767 (org-babel-execute-maybe)
768 (should (re-search-forward "\\#\\+results:" nil t))
769 (forward-line)
770 (should
771 (string=
772 ": some text"
773 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
775 (ert-deftest test-ob/commented-last-block-line-with-var ()
776 (org-test-with-temp-text-in-file "
777 #+begin_src emacs-lisp :var a=1
779 #+end_src"
780 (org-babel-next-src-block)
781 (org-babel-execute-maybe)
782 (re-search-forward "\\#\\+results:" nil t)
783 (forward-line)
784 (should (string=
786 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
787 (org-test-with-temp-text-in-file "
788 #+begin_src emacs-lisp :var a=2
790 #+end_src"
791 (org-babel-next-src-block)
792 (org-babel-execute-maybe)
793 (re-search-forward "\\#\\+results:" nil t)
794 (forward-line)
795 (should (string=
796 ": 2"
797 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
799 (ert-deftest test-ob/org-babel-insert-result--improper-lists ()
800 "Test `org-babel-insert-result' with improper lists."
801 ;; Do not error when output is an improper list.
802 (should
803 (org-test-with-temp-text
805 <point>#+BEGIN_SRC emacs-lisp
806 '((1 . nil) (2 . 3))
807 #+END_SRC
809 (org-babel-execute-maybe) t)))
811 (ert-deftest test-ob/remove-inline-result ()
812 "Test `org-babel-remove-inline-result' honors whitespace."
813 (let*
814 ((inline-sb "src_emacs-lisp{(+ 1 2)}")
815 (inline-res " {{{results(=3=)}}}")
816 (inline-sb-dot (concat inline-sb "."))
817 (inline-sb-res-dot (concat inline-sb inline-res ".")))
818 (org-test-with-temp-text
819 ;; Insert inline_src_block followed by dot.
820 inline-sb-dot
821 ;; Insert result before dot.
822 (org-babel-execute-maybe)
823 (should (string= inline-sb-res-dot
824 (buffer-substring-no-properties
825 (point-at-bol) (point-at-eol))))
826 ;; Delete whitespace and result.
827 (org-babel-remove-inline-result)
828 (should (string= inline-sb-dot
829 (buffer-substring-no-properties
830 (point-at-bol) (point-at-eol))))
831 ;; Add whitespace and result before dot.
832 (search-forward inline-sb)
833 (insert " " inline-res)
834 (goto-char (point-at-bol))
835 ;; Remove whitespace and result.
836 (org-babel-remove-inline-result)
837 (should (string= inline-sb-dot
838 (buffer-substring-no-properties
839 (point-at-bol) (point-at-eol))))
840 ;; Add whitespace before dot.
841 (search-forward inline-sb)
842 (insert " ")
843 (goto-char (point-at-bol))
844 ;; Add result before whitespace.
845 (org-babel-execute-maybe)
846 ;; Remove result - leave trailing whitespace and dot.
847 (org-babel-remove-inline-result)
848 (should (string= (concat inline-sb " .")
849 (buffer-substring-no-properties
850 (point-at-bol) (point-at-eol)))))))
852 (defun test-ob-verify-result-and-removed-result (result buffer-text)
853 "Test helper function to test `org-babel-remove-result'.
854 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
855 and the result of execution is verified against RESULT.
857 The block is actually executed /twice/ to ensure result
858 replacement happens correctly."
859 (org-test-with-temp-text
860 buffer-text
861 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
862 (should (re-search-forward "\\#\\+results:" nil t))
863 (forward-line)
864 (should (string= result
865 (buffer-substring-no-properties
866 (point-at-bol)
867 (- (point-max) 16))))
868 (org-babel-previous-src-block) (org-babel-remove-result)
869 (should (string= buffer-text
870 (buffer-substring-no-properties
871 (point-min) (point-max))))))
873 (ert-deftest test-ob/org-babel-remove-result--results-default ()
874 "Test `org-babel-remove-result' with default :results."
875 (mapcar (lambda (language)
876 (test-ob-verify-result-and-removed-result
877 "\n"
878 (concat
879 "* org-babel-remove-result
880 #+begin_src " language "
881 #+end_src
883 * next heading")))
884 '("sh" "emacs-lisp")))
886 (ert-deftest test-ob/org-babel-remove-result--results-list ()
887 "Test `org-babel-remove-result' with :results list."
888 (test-ob-verify-result-and-removed-result
889 "- 1
892 - (quote (4 5))"
894 "* org-babel-remove-result
895 #+begin_src emacs-lisp :results list
896 '(1 2 3 '(4 5))
897 #+end_src
899 * next heading"))
901 (ert-deftest test-ob/org-babel-results-indented-wrap ()
902 "Ensure that wrapped results are inserted correction when indented.
903 If not inserted correctly then the second evaluation will fail
904 trying to find the :END: marker."
905 (org-test-with-temp-text
906 "- indented
907 #+begin_src sh :results file wrap
908 echo test.txt
909 #+end_src"
910 (org-babel-next-src-block 1)
911 (org-babel-execute-src-block)
912 (org-babel-execute-src-block)))
914 (ert-deftest test-ob/file-desc-header-argument ()
915 "Test that the :file-desc header argument is used."
916 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
917 \"foo\"
918 #+end_src
920 #+begin_src emacs-lisp :results file :file-desc
921 \"foo\"
922 #+end_src"
923 (org-babel-execute-src-block)
924 (org-babel-next-src-block 1)
925 (org-babel-execute-src-block)
926 (goto-char (point-min))
927 (should (search-forward "[[file:foo][bar]]" nil t))
928 (should (search-forward "[[file:foo][foo]]" nil t))))
930 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
931 "Test `org-babel-remove-result' with :results pp."
932 (test-ob-verify-result-and-removed-result
933 ": \"I /am/ working!\""
935 "* org-babel-remove-result
936 #+begin_src emacs-lisp :results pp
937 \"I /am/ working!\")
938 #+end_src
940 * next heading"))
942 (ert-deftest test-org-babel/inline-src_blk-preceded-punct-preceded-by-point ()
943 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
944 (org-babel-inline-result-wrap "=%s="))
945 (org-test-with-temp-text
946 test-line
947 (forward-char 1)
948 (org-babel-execute-maybe)
949 (should (re-search-forward "=\"x\"=" nil t))
950 (forward-line))))
952 (ert-deftest test-ob/commented-last-block-line-with-var ()
953 (org-test-with-temp-text-in-file "
954 #+begin_src emacs-lisp :var a=1
956 #+end_src"
957 (org-babel-next-src-block)
958 (org-babel-execute-maybe)
959 (re-search-forward "\\#\\+results:" nil t)
960 (forward-line)
961 (should (string=
963 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
964 (org-test-with-temp-text-in-file "
965 #+begin_src emacs-lisp :var a=2
967 #+end_src"
968 (org-babel-next-src-block)
969 (org-babel-execute-maybe)
970 (re-search-forward "\\#\\+results:" nil t)
971 (forward-line)
972 (should (string=
973 ": 2"
974 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
976 (defun test-ob-verify-result-and-removed-result (result buffer-text)
977 "Test helper function to test `org-babel-remove-result'.
978 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
979 and the result of execution is verified against RESULT.
981 The block is actually executed /twice/ to ensure result
982 replacement happens correctly."
983 (org-test-with-temp-text
984 buffer-text
985 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
986 (should (re-search-forward "\\#\\+results:" nil t))
987 (forward-line)
988 (should (string= result
989 (buffer-substring-no-properties
990 (point-at-bol)
991 (- (point-max) 16))))
992 (org-babel-previous-src-block) (org-babel-remove-result)
993 (should (string= buffer-text
994 (buffer-substring-no-properties
995 (point-min) (point-max))))))
997 (ert-deftest test-ob/org-babel-remove-result--results-default ()
998 "Test `org-babel-remove-result' with default :results."
999 (mapcar (lambda (language)
1000 (test-ob-verify-result-and-removed-result
1001 "\n"
1002 (concat
1003 "* org-babel-remove-result
1004 #+begin_src " language "
1005 #+end_src
1007 * next heading")))
1008 '("sh" "emacs-lisp")))
1010 (ert-deftest test-ob/org-babel-remove-result--results-list ()
1011 "Test `org-babel-remove-result' with :results list."
1012 (test-ob-verify-result-and-removed-result
1013 "- 1
1016 - (quote (4 5))"
1018 "* org-babel-remove-result
1019 #+begin_src emacs-lisp :results list
1020 '(1 2 3 '(4 5))
1021 #+end_src
1023 * next heading"))
1025 (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
1026 "Test `org-babel-remove-result' with :results wrap."
1027 (test-ob-verify-result-and-removed-result
1028 ":RESULTS:
1029 hello there
1030 :END:"
1032 "* org-babel-remove-result
1034 #+begin_src emacs-lisp :results wrap
1035 \"hello there\"
1036 #+end_src
1038 * next heading"))
1040 (ert-deftest test-ob/org-babel-remove-result--results-org ()
1041 "Test `org-babel-remove-result' with :results org."
1042 (test-ob-verify-result-and-removed-result
1043 "#+BEGIN_SRC org
1044 ,* heading
1045 ,** subheading
1046 content
1047 #+END_SRC"
1049 "* org-babel-remove-result
1050 #+begin_src emacs-lisp :results org
1051 \"* heading
1052 ,** subheading
1053 content\"
1054 #+end_src
1056 * next heading"))
1058 (ert-deftest test-ob/org-babel-remove-result--results-html ()
1059 "Test `org-babel-remove-result' with :results html."
1060 (test-ob-verify-result-and-removed-result
1061 "#+BEGIN_HTML
1062 <head><body></body></head>
1063 #+END_HTML"
1065 "* org-babel-remove-result
1066 #+begin_src emacs-lisp :results html
1067 \"<head><body></body></head>\"
1068 #+end_src
1070 * next heading"))
1072 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
1073 "Test `org-babel-remove-result' with :results latex."
1074 (test-ob-verify-result-and-removed-result
1075 "#+BEGIN_LaTeX
1076 Line 1
1077 Line 2
1078 Line 3
1079 #+END_LaTeX"
1081 "* org-babel-remove-result
1082 #+begin_src emacs-lisp :results latex
1083 \"Line 1
1084 Line 2
1085 Line 3\"
1086 #+end_src
1088 * next heading"))
1090 (ert-deftest test-ob/org-babel-remove-result--results-code ()
1091 "Test `org-babel-remove-result' with :results code."
1093 (test-ob-verify-result-and-removed-result
1094 "#+BEGIN_SRC emacs-lisp
1095 \"I am working!\"
1096 #+END_SRC"
1098 "* org-babel-remove-result
1099 #+begin_src emacs-lisp :results code
1100 (message \"I am working!\")
1101 #+end_src
1103 * next heading"))
1105 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
1106 "Test `org-babel-remove-result' with :results pp."
1107 (test-ob-verify-result-and-removed-result
1108 ": \"I /am/ working!\""
1110 "* org-babel-remove-result
1111 #+begin_src emacs-lisp :results pp
1112 \"I /am/ working!\")
1113 #+end_src
1115 * next heading"))
1117 (ert-deftest test-ob/results-do-not-replace-code-blocks ()
1118 (org-test-with-temp-text "Block two has a space after the name.
1120 #+name: foo
1121 #+begin_src emacs-lisp
1123 #+end_src
1125 #+name: foo
1126 #+begin_src emacs-lisp
1128 #+end_src
1130 #+name: foo
1131 #+begin_src emacs-lisp
1133 #+end_src
1135 #+RESULTS: foo
1136 : foo
1138 (dolist (num '(1 2 3))
1139 ;; execute the block
1140 (goto-char (point-min))
1141 (org-babel-next-src-block num) (org-babel-execute-src-block)
1142 ;; check the results
1143 (goto-char (point-max))
1144 (move-beginning-of-line 0)
1145 (should (looking-at (format ": %d" num))))))
1147 (ert-deftest test-ob/blocks-with-spaces ()
1148 "Test expansion of blocks followed by blank lines."
1149 (should
1150 (equal "#+BEGIN_SRC emacs-lisp
1151 \(+ 1 2)
1152 #+END_SRC
1154 #+RESULTS:
1155 : 3\n\n\n"
1156 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1157 \(+ 1 2)
1158 #+END_SRC\n\n\n"
1159 (let ((org-babel-next-src-block "RESULTS"))
1160 (org-babel-execute-src-block))
1161 (buffer-string)))))
1163 (ert-deftest test-ob/results-in-narrowed-buffer ()
1164 "Test block execution in a narrowed buffer."
1165 ;; If results don't exist, they should be inserted in visible part
1166 ;; of the buffer.
1167 (should
1168 (equal
1169 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1170 (org-test-with-temp-text
1171 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1172 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1173 (let ((org-babel-results-keyword "RESULTS"))
1174 (org-babel-execute-src-block))
1175 (org-trim (buffer-string)))))
1176 (should
1177 (equal
1178 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1179 (org-test-with-temp-text
1180 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1181 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1182 (let ((org-babel-results-keyword "RESULTS"))
1183 (org-babel-execute-src-block))
1184 (org-trim (buffer-string)))))
1185 ;; Results in visible part of buffer, should be updated here.
1186 (should
1187 (equal
1188 "#+NAME: test
1189 #+BEGIN_SRC emacs-lisp
1190 \(+ 1 2)
1191 #+END_SRC
1193 #+RESULTS: test
1194 : 3"
1195 (org-test-with-temp-text
1196 "#+NAME: test
1197 #+BEGIN_SRC emacs-lisp
1198 \(+ 1 2)
1199 #+END_SRC
1201 #+RESULTS: test
1204 Paragraph"
1205 (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
1206 (let ((org-babel-results-keyword "RESULTS"))
1207 (org-babel-execute-src-block))
1208 (org-trim (buffer-string)))))
1209 ;; Results in invisible part of buffer, should be updated there.
1210 (org-test-with-temp-text
1211 "#+NAME: test
1212 #+BEGIN_SRC emacs-lisp
1213 \(+ 1 2)
1214 #+END_SRC
1216 #+RESULTS: test
1219 Paragraph"
1220 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1221 (let ((org-babel-results-keyword "RESULTS"))
1222 (org-babel-execute-src-block))
1223 (should-not (re-search-forward "^#\\+RESULTS:" nil t))
1224 (widen)
1225 (should (should (re-search-forward "^: 3" nil t)))))
1227 (ert-deftest test-ob/specific-colnames ()
1228 "Test passing specific column names."
1229 (should
1230 (equal "#+name: input-table
1231 | id | var1 |
1232 |----+------|
1233 | 1 | bar |
1234 | 2 | baz |
1236 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1237 echo \"$data\"
1238 #+end_src
1240 #+RESULTS:
1241 | Rev | Author |
1242 |-----+--------|
1243 | 1 | bar |
1244 | 2 | baz |
1247 (org-test-with-temp-text
1248 "#+name: input-table
1249 | id | var1 |
1250 |----+------|
1251 | 1 | bar |
1252 | 2 | baz |
1254 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1255 echo \"$data\"
1256 #+end_src
1258 ;; we should find a code block
1259 (should (re-search-forward org-babel-src-block-regexp nil t))
1260 (goto-char (match-beginning 0))
1261 ;; now that we've located the code block, it may be evaluated
1262 (let ((org-babel-execute-src-block "RESULTS"))
1263 (org-babel-execute-src-block))
1264 (buffer-string)))))
1266 (ert-deftest test-ob/location-of-header-arg-eval ()
1267 "Test location of header argument evaluation."
1268 (org-test-with-temp-text "
1269 #+name: top-block
1270 #+begin_src emacs-lisp :var pt=(point)
1272 #+end_src
1274 #+name: bottom-block
1275 #+begin_src emacs-lisp :var pt=top-block()
1277 #+end_src
1279 ;; the value of the second block should be greater than the first
1280 (should
1281 (< (progn (re-search-forward org-babel-src-block-regexp nil t)
1282 (goto-char (match-beginning 0))
1283 (prog1 (save-match-data (org-babel-execute-src-block))
1284 (goto-char (match-end 0))))
1285 (progn (re-search-forward org-babel-src-block-regexp nil t)
1286 (goto-char (match-beginning 0))
1287 (org-babel-execute-src-block))))))
1289 (ert-deftest test-ob/preserve-results-indentation ()
1290 "Preserve indentation when executing a src block."
1291 (should
1292 (equal '(2 2)
1293 (org-test-with-temp-text
1294 " #+begin_src emacs-lisp\n (+ 1 1)\n #+end_src"
1295 (org-babel-execute-src-block)
1296 (buffer-string)
1297 (let ((case-fold-search t)) (search-forward "#+results:"))
1298 ;; Check if both #+RESULTS: keyword and actual results are
1299 ;; indented by 2 columns.
1300 (list (org-get-indentation)
1301 (progn (forward-line) (org-get-indentation)))))))
1303 (ert-deftest test-ob/safe-header-args ()
1304 "Detect safe and unsafe header args."
1305 (let ((safe-args '((:cache . "foo")
1306 (:results . "output")
1307 (:eval . "never")
1308 (:eval . "query")))
1309 (unsafe-args '((:eval . "yes")
1310 (:results . "output file")
1311 (:foo . "bar")))
1312 (malformed-args '((bar . "foo")
1313 ("foo" . "bar")
1314 :foo))
1315 (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
1316 (dolist (arg safe-args)
1317 (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
1318 (dolist (arg unsafe-args)
1319 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1320 (dolist (arg malformed-args)
1321 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1322 (should (not (funcall safe-p (append safe-args unsafe-args))))))
1324 (ert-deftest test-ob/noweb-expansions-in-cache ()
1325 "Ensure that noweb expansions are expanded before caching."
1326 (let ((noweb-expansions-in-cache-var 0))
1327 (org-test-with-temp-text "
1328 #+name: foo
1329 #+begin_src emacs-lisp
1330 \"I said\"
1331 #+end_src
1333 #+name: bar
1334 #+begin_src emacs-lisp :noweb yes :cache yes
1335 (setq noweb-expansions-in-cache-var
1336 (+ 1 noweb-expansions-in-cache-var))
1337 (concat <<foo>> \" check noweb expansions\")
1338 #+end_src
1340 ;; run the second block to create the cache
1341 (goto-char (point-min))
1342 (re-search-forward (regexp-quote "#+name: bar"))
1343 (should (string= "I said check noweb expansions"
1344 (org-babel-execute-src-block)))
1345 (should (= noweb-expansions-in-cache-var 1))
1346 ;; change the value of the first block
1347 (goto-char (point-min))
1348 (re-search-forward (regexp-quote "said"))
1349 (goto-char (match-beginning 0))
1350 (insert "haven't ")
1351 (re-search-forward (regexp-quote "#+name: bar"))
1352 (should (string= "I haven't said check noweb expansions"
1353 (org-babel-execute-src-block)))
1354 (should (= noweb-expansions-in-cache-var 2)))))
1356 (ert-deftest test-org-babel/file-ext-and-output-dir ()
1357 (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
1358 (org-babel-next-src-block)
1359 (should (equal "file-ext-basic.txt"
1360 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1361 (org-babel-next-src-block)
1362 (should (equal "foo/file-ext-dir-relative.txt"
1363 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1364 (org-babel-next-src-block)
1365 (should (equal "foo/file-ext-dir-relative-slash.txt"
1366 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1367 (org-babel-next-src-block)
1368 (should (equal "/tmp/file-ext-dir-absolute.txt"
1369 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1370 (org-babel-next-src-block)
1371 (should (equal "foo.bar"
1372 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1373 (org-babel-next-src-block)
1374 (should (equal "xxx/foo.bar"
1375 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1378 (ert-deftest test-org-babel/script-escape ()
1379 ;; Delimited lists of numbers
1380 (should (equal '(1 2 3)
1381 (org-babel-script-escape "[1 2 3]")))
1382 (should (equal '(1 2 3)
1383 (org-babel-script-escape "{1 2 3}")))
1384 (should (equal '(1 2 3)
1385 (org-babel-script-escape "(1 2 3)")))
1386 ;; Delimited lists of double-quoted strings
1387 (should (equal '("foo" "bar")
1388 (org-babel-script-escape "(\"foo\" \"bar\")")))
1389 (should (equal '("foo" "bar")
1390 (org-babel-script-escape "[\"foo\" \"bar\"]")))
1391 (should (equal '("foo" "bar")
1392 (org-babel-script-escape "{\"foo\" \"bar\"}")))
1393 ;; ... with commas
1394 (should (equal '("foo" "bar")
1395 (org-babel-script-escape "(\"foo\", \"bar\")")))
1396 (should (equal '("foo" "bar")
1397 (org-babel-script-escape "[\"foo\", \"bar\"]")))
1398 (should (equal '("foo" "bar")
1399 (org-babel-script-escape "{\"foo\", \"bar\"}")))
1400 ;; Delimited lists of single-quoted strings
1401 (should (equal '("foo" "bar")
1402 (org-babel-script-escape "('foo' 'bar')")))
1403 (should (equal '("foo" "bar")
1404 (org-babel-script-escape "['foo' 'bar']")))
1405 (should (equal '("foo" "bar")
1406 (org-babel-script-escape "{'foo' 'bar'}")))
1407 ;; ... with commas
1408 (should (equal '("foo" "bar")
1409 (org-babel-script-escape "('foo', 'bar')")))
1410 (should (equal '("foo" "bar")
1411 (org-babel-script-escape "['foo', 'bar']")))
1412 (should (equal '("foo" "bar")
1413 (org-babel-script-escape "{'foo', 'bar'}")))
1414 ;; Single quoted strings
1415 (should (equal "foo"
1416 (org-babel-script-escape "'foo'")))
1417 ;; ... with internal double quote
1418 (should (equal "foo\"bar"
1419 (org-babel-script-escape "'foo\"bar'")))
1420 ;; ... with internal backslash
1421 (should (equal "foo\\bar"
1422 (org-babel-script-escape "'foo\\bar'")))
1423 ;; ... with internal escaped backslash
1424 (should (equal "foo\\bar"
1425 (org-babel-script-escape "'foo\\\\bar'")))
1426 ;; ... with internal backslash-double quote
1427 (should (equal "foo\\\"bar"
1428 (org-babel-script-escape "'foo\\\"bar'")))
1429 ;; ... with internal escaped backslash-double quote
1430 (should (equal "foo\\\"bar"
1431 (org-babel-script-escape "'foo\\\\\"bar'")))
1432 ;; ... with internal escaped single quote
1433 (should (equal "foo'bar"
1434 (org-babel-script-escape "'foo\\'bar'")))
1435 ;; ... with internal escaped backslash-escaped single quote
1436 (should (equal "foo\\'bar"
1437 (org-babel-script-escape "'foo\\\\\\'bar'")))
1438 ;; Double quoted strings
1439 (should (equal "foo"
1440 (org-babel-script-escape "\"foo\"")))
1441 ;; ... with internal single quote
1442 (should (equal "foo'bar"
1443 (org-babel-script-escape "\"foo'bar\"")))
1444 ;; ... with internal backslash
1445 (should (equal "foo\\bar"
1446 (org-babel-script-escape "\"foo\\bar\"")))
1447 ;; ... with internal escaped backslash
1448 (should (equal "foo\\bar"
1449 (org-babel-script-escape "\"foo\\\\bar\"")))
1450 ;; ... with internal backslash-single quote
1451 (should (equal "foo\\'bar"
1452 (org-babel-script-escape "\"foo\\'bar\"")))
1453 ;; ... with internal escaped backslash-single quote
1454 (should (equal "foo\\'bar"
1455 (org-babel-script-escape "\"foo\\\\'bar\"")))
1456 ;; ... with internal escaped double quote
1457 (should (equal "foo\"bar"
1458 (org-babel-script-escape "\"foo\\\"bar\"")))
1459 ;; ... with internal escaped backslash-escaped double quote
1460 (should (equal "foo\\\"bar"
1461 (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
1463 (provide 'test-ob)
1465 ;;; test-ob ends here