Merge branch 'maint'
[org-mode/org-tableheadings.git] / testing / lisp / test-ob.el
blob44bd508f91aa0be7130e9fa6293a0c5ee070218d
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/default-inline-header-args ()
91 (should(equal
92 '((:session . "none")
93 (:results . "replace")
94 (:exports . "results")
95 (:hlines . "yes"))
96 org-babel-default-inline-header-args)))
98 (ert-deftest ob-test/org-babel-combine-header-arg-lists ()
99 (let ((results (org-babel-combine-header-arg-lists
100 '((foo . :any)
101 (bar)
102 (baz . ((foo bar) (baz)))
103 (qux . ((foo bar baz qux)))
104 (quux . ((foo bar))))
105 '((bar)
106 (baz . ((baz)))
107 (quux . :any)))))
108 (dolist (pair '((foo . :any)
109 (bar)
110 (baz . ((baz)))
111 (quux . :any)
112 (qux . ((foo bar baz qux)))))
113 (should (equal (cdr pair)
114 (cdr (assoc (car pair) results)))))))
116 ;;; ob-get-src-block-info
117 (ert-deftest test-org-babel/get-src-block-info-language ()
118 (org-test-at-marker nil org-test-file-ob-anchor
119 (let ((info (org-babel-get-src-block-info)))
120 (should (string= "emacs-lisp" (nth 0 info))))))
122 (ert-deftest test-org-babel/get-src-block-info-body ()
123 (org-test-at-marker nil org-test-file-ob-anchor
124 (let ((info (org-babel-get-src-block-info)))
125 (should (string-match (regexp-quote org-test-file-ob-anchor)
126 (nth 1 info))))))
128 (ert-deftest test-org-babel/get-src-block-info-tangle ()
129 (org-test-at-marker nil org-test-file-ob-anchor
130 (let ((info (org-babel-get-src-block-info)))
131 (should (string= "no" (cdr (assoc :tangle (nth 2 info))))))))
133 (ert-deftest test-org-babel/elisp-in-header-arguments ()
134 "Test execution of elisp forms in header arguments."
135 (org-test-with-temp-text-in-file "
137 * elisp forms in header arguments
138 :PROPERTIES:
139 :var: prop = (* 7 6)
140 :END:
141 #+begin_src emacs-lisp
142 prop
143 #+end_src"
144 (goto-char (point-min))
145 (org-babel-next-src-block)
146 (let ((info (org-babel-get-src-block-info)))
147 (should (= 42 (org-babel-execute-src-block))))))
149 (ert-deftest test-org-babel/simple-named-code-block ()
150 "Test that simple named code blocks can be evaluated."
151 (org-test-with-temp-text-in-file "
153 #+name: i-have-a-name
154 #+begin_src emacs-lisp
156 #+end_src"
157 (org-babel-next-src-block 1)
158 (should (= 42 (org-babel-execute-src-block)))))
160 (ert-deftest test-org-babel/simple-variable-resolution ()
161 "Test that simple variable resolution is working."
162 (org-test-with-temp-text-in-file "
164 #+name: four
165 #+begin_src emacs-lisp
166 (list 1 2 3 4)
167 #+end_src
169 #+begin_src emacs-lisp :var four=four
170 (length four)
171 #+end_src"
173 (org-babel-next-src-block 2)
174 (should (= 4 (org-babel-execute-src-block)))
175 (forward-line 5)
176 (should (string= ": 4" (buffer-substring
177 (point-at-bol)
178 (point-at-eol))))))
180 (ert-deftest test-org-babel/multi-line-header-arguments ()
181 "Test that multi-line header arguments and can be read."
182 (org-test-with-temp-text-in-file "
184 #+headers: :var letters='(a b c d e f g)
185 #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
186 (require 'cl)
187 (defalias 'my-map (if (org-version-check \"24.2.50\" \"cl\" :predicate)
188 'cl-map
189 'map))
190 (my-map 'list #'list numbers letters)
191 #+end_src"
193 (org-babel-next-src-block)
194 (let ((results (org-babel-execute-src-block)))
195 (should(equal 'a (cadr (assoc 1 results))))
196 (should(equal 'd (cadr (assoc 4 results)))))))
198 (ert-deftest test-org-babel/parse-header-args ()
199 (org-test-with-temp-text-in-file "
201 #+begin_src example-lang :session :results output :var num=9
202 the body
203 #+end_src"
205 (org-babel-next-src-block)
206 (let* ((info (org-babel-get-src-block-info))
207 (params (nth 2 info)))
208 (message "%S" params)
209 (should (equal "example-lang" (nth 0 info)))
210 (should (string= "the body" (org-babel-trim (nth 1 info))))
211 (should-not (member '(:session\ \ \ \ ) params))
212 (should (equal '(:session) (assoc :session params)))
213 (should (equal '(:result-type . output) (assoc :result-type params)))
214 (should (equal '(num . 9) (cdr (assoc :var params)))))))
216 (ert-deftest test-org-babel/parse-header-args2 ()
217 (org-test-with-temp-text-in-file "
219 * resolving sub-trees as references
221 #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
222 (length text)
223 #+end_src
225 #+begin_src org :noweb yes
226 <<simple-subtree>>
227 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
228 #+end_src
230 ** simple subtree with custom ID
231 :PROPERTIES:
232 :CUSTOM_ID: simple-subtree
233 :END:
234 this is simple"
236 (should (string-match (regexp-quote "this is simple")
237 (org-babel-ref-resolve "simple-subtree")))
238 (org-babel-next-src-block)
239 (should (= 14 (org-babel-execute-src-block)))))
241 (ert-deftest test-org-babel/inline-src-blocks ()
242 (should
243 (= 1
244 (org-test-with-temp-text
245 "In the middle <point>src_emacs-lisp{(+ 0 1)} of a line"
246 (org-babel-execute-src-block))))
247 (should
248 (= 2
249 (org-test-with-temp-text
250 "One at the end of a line: <point>src_emacs-lisp{(+ 1 1)}"
251 (org-babel-execute-src-block))))
252 (should
253 (= 3
254 (org-test-with-temp-text
255 "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
256 (org-babel-execute-src-block))))
257 (should
258 (= 4
259 (org-test-with-temp-text
260 "In the middle <point>src_emacs-lisp[:results silent\
261 :exports code]{(+ 3 1)} of a line"
262 (org-babel-execute-src-block))))
263 (should
264 (= 5
265 (org-test-with-temp-text
266 "One at the end of a line: <point>src_emacs-lisp[:results silent\
267 :exports code]{(+ 4 1)}"
268 (org-babel-execute-src-block))))
269 (should
270 (= 6
271 (org-test-with-temp-text
272 "src_emacs-lisp[:results silent :exports code]{(+ 5 1)}\
273 at the beginning of a line."
274 (org-babel-execute-src-block))))
275 (should
276 (= 7
277 (org-test-with-temp-text
278 "One also evaluated: <point>src_emacs-lisp[:exports both\
279 :results silent]{(+ 6 1)}"
280 (org-babel-execute-src-block)))))
282 (ert-deftest test-org-babel/org-babel-get-inline-src-block-matches ()
283 (flet ((test-at-id (id)
284 (org-test-at-id
286 (let ((test-point (point)))
287 (should (fboundp 'org-babel-get-inline-src-block-matches))
288 (should (re-search-forward "src_" nil t)) ;; 1
289 (should (org-babel-get-inline-src-block-matches))
290 (should (re-search-forward " b" nil (point-at-bol))) ;; 1
291 (should-not (org-babel-get-inline-src-block-matches))
292 (should (re-search-forward "in" nil t)) ;; 2
293 (should-not (org-babel-get-inline-src-block-matches))
294 (should (re-search-forward "echo" nil t)) ;; 2
295 (should (org-babel-get-inline-src-block-matches))
296 (should (re-search-forward "blocks" nil t)) ;; 3
297 (backward-char 7) ;; 3
298 (should (org-babel-get-inline-src-block-matches))
299 (forward-char 1) ;;3
300 (should-not (org-babel-get-inline-src-block-matches))
301 (should (re-search-forward ":results" nil t)) ;; 4
302 (should (org-babel-get-inline-src-block-matches))
303 (end-of-line)
304 (should-not (org-babel-get-inline-src-block-matches))))))
305 (test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74")
306 (test-at-id "d55dada7-de0e-4340-8061-787cccbedee5")))
308 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-1 ()
309 (let ((test-line "src_sh{echo 1}")
310 (org-babel-inline-result-wrap "=%s="))
311 ;; src_ at bol line 1...
312 (org-test-with-temp-text
313 test-line
314 (goto-char (point-min)) (org-babel-execute-maybe)
315 (should (string=
316 (concat test-line " {{{results(=1=)}}}")
317 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
318 (forward-char) (org-babel-execute-maybe)
319 (should (string=
320 (concat test-line " {{{results(=1=)}}}")
321 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
322 (re-search-forward "{{{")
323 ;;(should-error (org-ctrl-c-ctrl-c))
324 (backward-char 4) ;; last char of block body
325 (org-babel-execute-maybe)
326 (should (string=
327 (concat test-line " {{{results(=1=)}}}")
328 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
329 ;; src_ follows space line 1...
330 (let ((test-line " src_emacs-lisp{ 1 }"))
331 (org-test-with-temp-text
332 test-line
333 (should-error (org-ctrl-c-ctrl-c))
334 (forward-char) (org-babel-execute-maybe)
335 (should (string=
336 (concat test-line " {{{results(=1=)}}}")
337 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
338 (re-search-forward "{ 1 ") (org-babel-execute-maybe)
339 (should (string=
340 (concat test-line " {{{results(=1=)}}}")
341 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
342 (forward-char 6)
343 (should-error (org-ctrl-c-ctrl-c))
344 ))))
346 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-2 ()
347 ;; src_ at bol line 2...
348 (let ((test-line " src_emacs-lisp{ \"x\" }")
349 (org-babel-inline-result-wrap "=%s="))
350 (org-test-with-temp-text
351 (concat "\n" test-line)
352 (should-error (org-ctrl-c-ctrl-c))
353 (goto-char (point-min))
354 (should-error (org-ctrl-c-ctrl-c))
355 (forward-line)
356 (should-error (org-ctrl-c-ctrl-c))
357 (forward-char) (org-babel-execute-maybe)
358 (should (string=
359 (concat test-line " {{{results(=x=)}}}")
360 (buffer-substring-no-properties
361 (point-at-bol) (point-at-eol))))))
362 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
363 (org-babel-inline-result-wrap "=%s="))
364 (org-test-with-temp-text
365 test-line
366 (goto-char (point-max))
367 (insert (concat "\n" test-line " end"))
368 (re-search-backward "src") (org-babel-execute-maybe)
369 (should (string=
370 (concat test-line " {{{results(=y=)}}} end")
371 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
372 (re-search-forward "\" ") (org-babel-execute-maybe)
373 (should (string=
374 (concat test-line " {{{results(=y=)}}} end")
375 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
376 (forward-char 3)
377 (should-error (org-ctrl-c-ctrl-c)))))
379 (ert-deftest test-org-babel/inline-src_blk-manual-results-replace ()
380 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
381 (org-babel-inline-result-wrap "=%s="))
382 (org-test-with-temp-text
383 (concat "\n" test-line)
384 (should-error (org-ctrl-c-ctrl-c))
385 (goto-char (point-max))
386 (org-babel-execute-maybe)
387 (beginning-of-line)
388 (should-error (org-ctrl-c-ctrl-c))
389 (forward-char) (org-babel-execute-maybe)
390 (should (string=
391 (concat test-line " {{{results(=x=)}}}")
392 (buffer-substring-no-properties
393 (point-at-bol) (point-at-eol))))))
394 (let ((test-line (concat " Some text prior to block "
395 "src_emacs-lisp[:results replace]{ \"y\" }"))
396 (org-babel-inline-result-wrap "=%s="))
397 (org-test-with-temp-text test-line
398 (goto-char (point-max))
399 (insert (concat "\n" test-line " end"))
400 (re-search-backward "src") (org-babel-execute-maybe)
401 (should (string=
402 (concat test-line " {{{results(=y=)}}} end")
403 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
404 (re-search-forward "\" ") (org-babel-execute-maybe)
405 (should (string=
406 (concat test-line " {{{results(=y=)}}} end")
407 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
408 (forward-char 3)
409 (should-error (org-ctrl-c-ctrl-c)))))
411 (ert-deftest test-org-babel/inline-src_blk-results-silent ()
412 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
413 (org-test-with-temp-text test-line
414 (org-babel-execute-maybe)
415 (should (string= test-line
416 (buffer-substring-no-properties
417 (point-at-bol) (point-at-eol))))))
418 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
419 "[ :results silent ]{ \"y\" }")))
420 (org-test-with-temp-text
421 test-line
422 (goto-char (point-max))
423 (insert (concat "\n" test-line " end"))
424 (re-search-backward "src_") (org-babel-execute-maybe)
425 (should (string= (concat test-line " end")
426 (buffer-substring-no-properties
427 (point-at-bol) (point-at-eol))))
428 (re-search-forward "\" ") (org-babel-execute-maybe)
429 (should (string= (concat test-line " end")
430 (buffer-substring-no-properties
431 (point-at-bol) (point-at-eol))))
432 (forward-char 2)
433 (should-error (org-ctrl-c-ctrl-c)))))
435 (ert-deftest test-org-babel/inline-src_blk-results-raw ()
436 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
437 (org-test-with-temp-text test-line
438 (org-babel-execute-maybe)
439 (should (string= (concat test-line " x")
440 (buffer-string)))))
441 (let ((test-line (concat " Some text prior to block "
442 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
443 (org-test-with-temp-text (concat test-line " end")
444 (re-search-forward "src_") (org-babel-execute-maybe)
445 (should (string= (concat test-line " the end")
446 (buffer-substring-no-properties
447 (point-at-bol) (point-at-eol))))
448 (re-search-forward "\" ") (org-babel-execute-maybe)
449 (should (string= (concat test-line " the the end")
450 (buffer-substring-no-properties
451 (point-at-bol) (point-at-eol))))
452 (forward-char 2)
453 (should-error (org-ctrl-c-ctrl-c)))))
455 (ert-deftest test-org-babel/inline-src_blk-results-file ()
456 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
457 (org-test-with-temp-text
458 test-line
459 (org-babel-execute-maybe)
460 (should (string= (concat test-line " {{{results([[file:~/test-file]])}}}")
461 (buffer-substring-no-properties
462 (point-min) (point-max)))))))
464 (ert-deftest test-org-babel/inline-src_blk-results-scalar ()
465 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
466 (org-babel-inline-result-wrap "=%s="))
467 (org-test-with-temp-text
468 test-line
469 (org-babel-execute-maybe)
470 (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
471 (buffer-substring-no-properties
472 (point-min) (point-max)))))))
474 (ert-deftest test-org-babel/inline-src_blk-results-verbatim ()
475 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
476 (org-babel-inline-result-wrap "=%s="))
477 (org-test-with-temp-text
478 test-line
479 (org-babel-execute-maybe)
480 (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
481 (buffer-substring-no-properties
482 (point-min) (point-max)))))))
484 (ert-deftest test-org-babel/combining-scalar-and-raw-result-types ()
485 (org-test-with-temp-text-in-file "
487 #+begin_src sh :results scalar
488 echo \"[[file:./cv.cls]]\"
489 #+end_src
491 #+name:
492 : [[file:./cv.cls]]
494 #+begin_src sh :results raw scalar
495 echo \"[[file:./cv.cls]]\"
496 #+end_src
498 (flet ((next-result ()
499 (org-babel-next-src-block)
500 (org-babel-execute-src-block)
501 (goto-char (org-babel-where-is-src-block-result))
502 (forward-line 1)))
503 (goto-char (point-min))
504 (next-result)
505 (should (org-babel-in-example-or-verbatim))
506 (next-result)
507 (should (not (org-babel-in-example-or-verbatim))))))
509 (ert-deftest test-org-babel/no-defaut-value-for-var ()
510 "Test that the absence of a default value for a variable DOES THROW
511 a proper error."
512 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
513 (org-babel-next-src-block)
514 (let ((err
515 (should-error (org-babel-execute-src-block) :type 'error)))
516 (should
517 (equal
518 '(error
519 "Variable \"x\" must be assigned a default value")
520 err)))))
522 (ert-deftest test-org-babel/just-one-results-block ()
523 "Test that evaluating two times the same code block does not result in a
524 duplicate results block."
525 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
526 (org-babel-execute-src-block)
527 (org-babel-execute-src-block) ; second code block execution
528 (should (search-forward "Hello")) ; the string inside the source code block
529 (should (search-forward "Hello")) ; the same string in the results block
530 (should-error (search-forward "Hello"))))
532 (ert-deftest test-org-babel/nested-code-block ()
533 "Test nested code blocks inside code blocks don't cause problems."
534 (should
535 (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
536 (org-test-with-temp-text "#+begin_src org :results silent
537 ,#+begin_src emacs-lisp
538 , 'foo
539 ,#+end_src
540 #+end_src"
541 (let ((org-edit-src-content-indentation 2)
542 (org-src-preserve-indentation nil))
543 (org-babel-execute-src-block))))))
545 (ert-deftest test-org-babel/partial-nested-code-block ()
546 "Test nested code blocks inside code blocks don't cause problems."
547 (org-test-with-temp-text "#+begin_src org :results silent
548 ,#+begin_src emacs-lisp
549 #+end_src"
550 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
552 (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
553 (org-test-with-temp-text "#+NAME: foo
554 #+BEGIN_SRC emacs-lisp
555 'foo
556 #+END_SRC\n"
557 (org-babel-next-src-block 1)
558 (should (eq 'foo (org-babel-execute-src-block)))
559 (goto-char (point-min))
560 (org-babel-next-src-block 1)
561 (should (looking-at org-babel-src-block-regexp))))
563 (ert-deftest test-ob/catches-all-references ()
564 (org-test-with-temp-text "
565 #+NAME: literal-example
566 #+BEGIN_EXAMPLE
567 A literal example
568 on two lines
569 #+END_EXAMPLE
571 #+NAME: read-literal-example
572 #+BEGIN_SRC emacs-lisp :var x=literal-example
573 (concatenate 'string x \" for me.\")
574 #+END_SRC"
575 (org-babel-next-src-block 1)
576 (should (string= (org-babel-execute-src-block)
577 "A literal example\non two lines\n for me."))))
579 (ert-deftest test-ob/ignore-reference-in-commented-headings ()
580 (should
581 (= 2
582 (org-test-with-temp-text
584 * COMMENT H1
585 #+NAME: n
588 * H2
589 #+NAME: n
592 * Code
594 <point>#+BEGIN_SRC emacs-lisp :var x=n
596 #+END_SRC"
597 (org-babel-execute-src-block)))))
599 (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
600 (org-test-with-temp-text "
601 #+tblname: base_plus
602 | 1 |
603 | 2 |
605 #+tblname: base
606 | 3 |
607 | 4 |
609 #+begin_src emacs-lisp :var x=base
611 #+end_src"
612 (org-babel-next-src-block 1)
613 (should (equal (org-babel-execute-src-block) '((3) (4))))))
615 (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
616 (org-test-with-temp-text "
617 #+name: base_plus
618 #+begin_src emacs-lisp
619 'bar
620 #+end_src
622 #+name: base
623 #+begin_src emacs-lisp
624 'foo
625 #+end_src
627 #+begin_src emacs-lisp :var x=base
629 #+end_src"
630 (org-babel-next-src-block 3)
631 (should (equal (org-babel-execute-src-block) "foo"))))
633 (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
634 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
635 (+ a b c d)
636 #+end_src
638 (should (= 10 (org-babel-execute-src-block)))))
640 (ert-deftest test-ob/org-babel-update-intermediate ()
641 (org-test-with-temp-text "#+name: foo
642 #+begin_src emacs-lisp
644 #+end_src
646 #+results: foo
649 #+begin_src emacs-lisp :var it=foo
650 (+ it 1)
651 #+end_src"
652 (let ((org-babel-update-intermediate nil))
653 (goto-char (point-min))
654 (org-babel-next-src-block 2)
655 (should (= 3 (org-babel-execute-src-block)))
656 (goto-char (point-min))
657 (forward-line 6)
658 (should (looking-at ": 4")))
659 (let ((org-babel-update-intermediate t))
660 (goto-char (point-min))
661 (org-babel-next-src-block 2)
662 (should (= 3 (org-babel-execute-src-block)))
663 (goto-char (point-min))
664 (forward-line 6)
665 (should (looking-at ": 2")))))
667 (ert-deftest test-ob/eval-header-argument ()
668 (flet ((check-eval (eval runp)
669 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
670 (setq foo :evald)
671 #+end_src" eval)
672 (let ((foo :not-run))
673 (if runp
674 (progn (should (org-babel-execute-src-block))
675 (should (eq foo :evald)))
676 (progn (should-not (org-babel-execute-src-block))
677 (should-not (eq foo :evald))))))))
678 (check-eval "never" nil)
679 (check-eval "no" nil)
680 (check-eval "never-export" t)
681 (check-eval "no-export" t)
682 (let ((org-babel-exp-reference-buffer (current-buffer)))
683 (check-eval "never" nil)
684 (check-eval "no" nil)
685 (check-eval "never-export" nil)
686 (check-eval "no-export" nil))))
688 (ert-deftest test-ob/noweb-expansion-1 ()
689 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
690 <<foo>>
691 #+end_src
693 #+name: foo
694 #+begin_src sh
696 #+end_src"
697 (should (string= (org-babel-expand-noweb-references) "bar"))))
699 (ert-deftest test-ob/noweb-expansion-2 ()
700 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
701 <<foo>>
702 #+end_src
704 #+name: foo
705 #+begin_src sh :noweb-sep \"\"
707 #+end_src
709 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
711 #+end_src"
712 (should (string= (org-babel-expand-noweb-references) "barbaz"))))
714 (ert-deftest test-ob/splitting-variable-lists-in-references ()
715 (org-test-with-temp-text ""
716 (should (= 1 (length (org-babel-ref-split-args
717 "a=\"this, no work\""))))
718 (should (= 2 (length (org-babel-ref-split-args
719 "a=\"this, no work\", b=1"))))))
721 (ert-deftest test-ob/org-babel-balanced-split ()
722 (should (equal
723 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
724 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
725 '((32 9) . 58)))))
727 (ert-deftest test-ob/commented-last-block-line-no-var ()
728 (org-test-with-temp-text-in-file "
729 #+begin_src emacs-lisp
731 #+end_src"
732 (org-babel-next-src-block)
733 (org-babel-execute-maybe)
734 (should (re-search-forward "\\#\\+results:" nil t))
735 (forward-line)
736 (should
737 (string=
739 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
740 (org-test-with-temp-text-in-file "
741 #+begin_src emacs-lisp
742 \"some text\";;
743 #+end_src"
744 (org-babel-next-src-block)
745 (org-babel-execute-maybe)
746 (should (re-search-forward "\\#\\+results:" nil t))
747 (forward-line)
748 (should
749 (string=
750 ": some text"
751 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
753 (ert-deftest test-ob/commented-last-block-line-with-var ()
754 (org-test-with-temp-text-in-file "
755 #+begin_src emacs-lisp :var a=1
757 #+end_src"
758 (org-babel-next-src-block)
759 (org-babel-execute-maybe)
760 (re-search-forward "\\#\\+results:" nil t)
761 (forward-line)
762 (should (string=
764 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
765 (org-test-with-temp-text-in-file "
766 #+begin_src emacs-lisp :var a=2
768 #+end_src"
769 (org-babel-next-src-block)
770 (org-babel-execute-maybe)
771 (re-search-forward "\\#\\+results:" nil t)
772 (forward-line)
773 (should (string=
774 ": 2"
775 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
777 (ert-deftest test-ob/org-babel-insert-result--improper-lists ()
778 "Test `org-babel-insert-result' with improper lists."
779 ;; Do not error when output is an improper list.
780 (should
781 (org-test-with-temp-text
783 <point>#+BEGIN_SRC emacs-lisp
784 '((1 . nil) (2 . 3))
785 #+END_SRC
787 (org-babel-execute-maybe) t)))
789 (ert-deftest test-ob/remove-inline-result ()
790 "Test `org-babel-remove-inline-result' honors whitespace."
791 (let*
792 ((inline-sb "src_emacs-lisp{(+ 1 2)}")
793 (inline-res " {{{results(=3=)}}}")
794 (inline-sb-dot (concat inline-sb "."))
795 (inline-sb-res-dot (concat inline-sb inline-res ".")))
796 (org-test-with-temp-text
797 ;; Insert inline_src_block followed by dot.
798 inline-sb-dot
799 ;; Insert result before dot.
800 (org-babel-execute-maybe)
801 (should (string= inline-sb-res-dot
802 (buffer-substring-no-properties
803 (point-at-bol) (point-at-eol))))
804 ;; Delete whitespace and result.
805 (org-babel-remove-inline-result)
806 (should (string= inline-sb-dot
807 (buffer-substring-no-properties
808 (point-at-bol) (point-at-eol))))
809 ;; Add whitespace and result before dot.
810 (search-forward inline-sb)
811 (insert " " inline-res)
812 (goto-char (point-at-bol))
813 ;; Remove whitespace and result.
814 (org-babel-remove-inline-result)
815 (should (string= inline-sb-dot
816 (buffer-substring-no-properties
817 (point-at-bol) (point-at-eol))))
818 ;; Add whitespace before dot.
819 (search-forward inline-sb)
820 (insert " ")
821 (goto-char (point-at-bol))
822 ;; Add result before whitespace.
823 (org-babel-execute-maybe)
824 ;; Remove result - leave trailing whitespace and dot.
825 (org-babel-remove-inline-result)
826 (should (string= (concat inline-sb " .")
827 (buffer-substring-no-properties
828 (point-at-bol) (point-at-eol)))))))
830 (defun test-ob-verify-result-and-removed-result (result buffer-text)
831 "Test helper function to test `org-babel-remove-result'.
832 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
833 and the result of execution is verified against RESULT.
835 The block is actually executed /twice/ to ensure result
836 replacement happens correctly."
837 (org-test-with-temp-text
838 buffer-text
839 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
840 (should (re-search-forward "\\#\\+results:" nil t))
841 (forward-line)
842 (should (string= result
843 (buffer-substring-no-properties
844 (point-at-bol)
845 (- (point-max) 16))))
846 (org-babel-previous-src-block) (org-babel-remove-result)
847 (should (string= buffer-text
848 (buffer-substring-no-properties
849 (point-min) (point-max))))))
851 (ert-deftest test-ob/org-babel-remove-result--results-default ()
852 "Test `org-babel-remove-result' with default :results."
853 (mapcar (lambda (language)
854 (test-ob-verify-result-and-removed-result
855 "\n"
856 (concat
857 "* org-babel-remove-result
858 #+begin_src " language "
859 #+end_src
861 * next heading")))
862 '("sh" "emacs-lisp")))
864 (ert-deftest test-ob/org-babel-remove-result--results-list ()
865 "Test `org-babel-remove-result' with :results list."
866 (test-ob-verify-result-and-removed-result
867 "- 1
870 - (quote (4 5))"
872 "* org-babel-remove-result
873 #+begin_src emacs-lisp :results list
874 '(1 2 3 '(4 5))
875 #+end_src
877 * next heading"))
879 (ert-deftest test-ob/org-babel-results-indented-wrap ()
880 "Ensure that wrapped results are inserted correction when indented.
881 If not inserted correctly then the second evaluation will fail
882 trying to find the :END: marker."
883 (org-test-with-temp-text
884 "- indented
885 #+begin_src sh :results file wrap
886 echo test.txt
887 #+end_src"
888 (org-babel-next-src-block 1)
889 (org-babel-execute-src-block)
890 (org-babel-execute-src-block)))
892 (ert-deftest test-ob/file-desc-header-argument ()
893 "Test that the :file-desc header argument is used."
894 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
895 \"foo\"
896 #+end_src
898 #+begin_src emacs-lisp :results file :file-desc
899 \"foo\"
900 #+end_src"
901 (org-babel-execute-src-block)
902 (org-babel-next-src-block 1)
903 (org-babel-execute-src-block)
904 (goto-char (point-min))
905 (should (search-forward "[[file:foo][bar]]" nil t))
906 (should (search-forward "[[file:foo][foo]]" nil t))))
908 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
909 "Test `org-babel-remove-result' with :results pp."
910 (test-ob-verify-result-and-removed-result
911 ": \"I /am/ working!\""
913 "* org-babel-remove-result
914 #+begin_src emacs-lisp :results pp
915 \"I /am/ working!\")
916 #+end_src
918 * next heading"))
920 (ert-deftest test-org-babel/inline-src_blk-preceded-punct-preceded-by-point ()
921 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
922 (org-babel-inline-result-wrap "=%s="))
923 (org-test-with-temp-text
924 test-line
925 (forward-char 1)
926 (org-babel-execute-maybe)
927 (should (re-search-forward "=\"x\"=" nil t))
928 (forward-line))))
930 (ert-deftest test-ob/commented-last-block-line-with-var ()
931 (org-test-with-temp-text-in-file "
932 #+begin_src emacs-lisp :var a=1
934 #+end_src"
935 (org-babel-next-src-block)
936 (org-babel-execute-maybe)
937 (re-search-forward "\\#\\+results:" nil t)
938 (forward-line)
939 (should (string=
941 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
942 (org-test-with-temp-text-in-file "
943 #+begin_src emacs-lisp :var a=2
945 #+end_src"
946 (org-babel-next-src-block)
947 (org-babel-execute-maybe)
948 (re-search-forward "\\#\\+results:" nil t)
949 (forward-line)
950 (should (string=
951 ": 2"
952 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
954 (defun test-ob-verify-result-and-removed-result (result buffer-text)
955 "Test helper function to test `org-babel-remove-result'.
956 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
957 and the result of execution is verified against RESULT.
959 The block is actually executed /twice/ to ensure result
960 replacement happens correctly."
961 (org-test-with-temp-text
962 buffer-text
963 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
964 (should (re-search-forward "\\#\\+results:" nil t))
965 (forward-line)
966 (should (string= result
967 (buffer-substring-no-properties
968 (point-at-bol)
969 (- (point-max) 16))))
970 (org-babel-previous-src-block) (org-babel-remove-result)
971 (should (string= buffer-text
972 (buffer-substring-no-properties
973 (point-min) (point-max))))))
975 (ert-deftest test-ob/org-babel-remove-result--results-default ()
976 "Test `org-babel-remove-result' with default :results."
977 (mapcar (lambda (language)
978 (test-ob-verify-result-and-removed-result
979 "\n"
980 (concat
981 "* org-babel-remove-result
982 #+begin_src " language "
983 #+end_src
985 * next heading")))
986 '("sh" "emacs-lisp")))
988 (ert-deftest test-ob/org-babel-remove-result--results-list ()
989 "Test `org-babel-remove-result' with :results list."
990 (test-ob-verify-result-and-removed-result
991 "- 1
994 - (quote (4 5))"
996 "* org-babel-remove-result
997 #+begin_src emacs-lisp :results list
998 '(1 2 3 '(4 5))
999 #+end_src
1001 * next heading"))
1003 (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
1004 "Test `org-babel-remove-result' with :results wrap."
1005 (test-ob-verify-result-and-removed-result
1006 ":RESULTS:
1007 hello there
1008 :END:"
1010 "* org-babel-remove-result
1012 #+begin_src emacs-lisp :results wrap
1013 \"hello there\"
1014 #+end_src
1016 * next heading"))
1018 (ert-deftest test-ob/org-babel-remove-result--results-org ()
1019 "Test `org-babel-remove-result' with :results org."
1020 (test-ob-verify-result-and-removed-result
1021 "#+BEGIN_SRC org
1022 ,* heading
1023 ,** subheading
1024 content
1025 #+END_SRC"
1027 "* org-babel-remove-result
1028 #+begin_src emacs-lisp :results org
1029 \"* heading
1030 ,** subheading
1031 content\"
1032 #+end_src
1034 * next heading"))
1036 (ert-deftest test-ob/org-babel-remove-result--results-html ()
1037 "Test `org-babel-remove-result' with :results html."
1038 (test-ob-verify-result-and-removed-result
1039 "#+BEGIN_EXPORT html
1040 <head><body></body></head>
1041 #+END_EXPORT"
1043 "* org-babel-remove-result
1044 #+begin_src emacs-lisp :results html
1045 \"<head><body></body></head>\"
1046 #+end_src
1048 * next heading"))
1050 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
1051 "Test `org-babel-remove-result' with :results latex."
1052 (test-ob-verify-result-and-removed-result
1053 "#+BEGIN_EXPORT latex
1054 Line 1
1055 Line 2
1056 Line 3
1057 #+END_EXPORT"
1059 "* org-babel-remove-result
1060 #+begin_src emacs-lisp :results latex
1061 \"Line 1
1062 Line 2
1063 Line 3\"
1064 #+end_src
1066 * next heading"))
1068 (ert-deftest test-ob/org-babel-remove-result--results-code ()
1069 "Test `org-babel-remove-result' with :results code."
1071 (test-ob-verify-result-and-removed-result
1072 "#+BEGIN_SRC emacs-lisp
1073 \"I am working!\"
1074 #+END_SRC"
1076 "* org-babel-remove-result
1077 #+begin_src emacs-lisp :results code
1078 (message \"I am working!\")
1079 #+end_src
1081 * next heading"))
1083 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
1084 "Test `org-babel-remove-result' with :results pp."
1085 (test-ob-verify-result-and-removed-result
1086 ": \"I /am/ working!\""
1088 "* org-babel-remove-result
1089 #+begin_src emacs-lisp :results pp
1090 \"I /am/ working!\")
1091 #+end_src
1093 * next heading"))
1095 (ert-deftest test-ob/results-do-not-replace-code-blocks ()
1096 (org-test-with-temp-text "Block two has a space after the name.
1098 #+name: foo
1099 #+begin_src emacs-lisp
1101 #+end_src
1103 #+name: foo
1104 #+begin_src emacs-lisp
1106 #+end_src
1108 #+name: foo
1109 #+begin_src emacs-lisp
1111 #+end_src
1113 #+RESULTS: foo
1114 : foo
1116 (dolist (num '(1 2 3))
1117 ;; execute the block
1118 (goto-char (point-min))
1119 (org-babel-next-src-block num) (org-babel-execute-src-block)
1120 ;; check the results
1121 (goto-char (point-max))
1122 (move-beginning-of-line 0)
1123 (should (looking-at (format ": %d" num))))))
1125 (ert-deftest test-ob/blocks-with-spaces ()
1126 "Test expansion of blocks followed by blank lines."
1127 (should
1128 (equal "#+BEGIN_SRC emacs-lisp
1129 \(+ 1 2)
1130 #+END_SRC
1132 #+RESULTS:
1133 : 3\n\n\n"
1134 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1135 \(+ 1 2)
1136 #+END_SRC\n\n\n"
1137 (let ((org-babel-next-src-block "RESULTS"))
1138 (org-babel-execute-src-block))
1139 (buffer-string)))))
1141 (ert-deftest test-ob/results-in-narrowed-buffer ()
1142 "Test block execution in a narrowed buffer."
1143 ;; If results don't exist, they should be inserted in visible part
1144 ;; of the buffer.
1145 (should
1146 (equal
1147 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1148 (org-test-with-temp-text
1149 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1150 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1151 (let ((org-babel-results-keyword "RESULTS"))
1152 (org-babel-execute-src-block))
1153 (org-trim (buffer-string)))))
1154 (should
1155 (equal
1156 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1157 (org-test-with-temp-text
1158 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1159 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1160 (let ((org-babel-results-keyword "RESULTS"))
1161 (org-babel-execute-src-block))
1162 (org-trim (buffer-string)))))
1163 ;; Results in visible part of buffer, should be updated here.
1164 (should
1165 (equal
1166 "#+NAME: test
1167 #+BEGIN_SRC emacs-lisp
1168 \(+ 1 2)
1169 #+END_SRC
1171 #+RESULTS: test
1172 : 3"
1173 (org-test-with-temp-text
1174 "#+NAME: test
1175 #+BEGIN_SRC emacs-lisp
1176 \(+ 1 2)
1177 #+END_SRC
1179 #+RESULTS: test
1182 Paragraph"
1183 (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
1184 (let ((org-babel-results-keyword "RESULTS"))
1185 (org-babel-execute-src-block))
1186 (org-trim (buffer-string)))))
1187 ;; Results in invisible part of buffer, should be updated there.
1188 (org-test-with-temp-text
1189 "#+NAME: test
1190 #+BEGIN_SRC emacs-lisp
1191 \(+ 1 2)
1192 #+END_SRC
1194 #+RESULTS: test
1197 Paragraph"
1198 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1199 (let ((org-babel-results-keyword "RESULTS"))
1200 (org-babel-execute-src-block))
1201 (should-not (re-search-forward "^#\\+RESULTS:" nil t))
1202 (widen)
1203 (should (should (re-search-forward "^: 3" nil t)))))
1205 (ert-deftest test-ob/specific-colnames ()
1206 "Test passing specific column names."
1207 (should
1208 (equal "#+name: input-table
1209 | id | var1 |
1210 |----+------|
1211 | 1 | bar |
1212 | 2 | baz |
1214 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1215 echo \"$data\"
1216 #+end_src
1218 #+RESULTS:
1219 | Rev | Author |
1220 |-----+--------|
1221 | 1 | bar |
1222 | 2 | baz |
1225 (org-test-with-temp-text
1226 "#+name: input-table
1227 | id | var1 |
1228 |----+------|
1229 | 1 | bar |
1230 | 2 | baz |
1232 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1233 echo \"$data\"
1234 #+end_src
1236 ;; we should find a code block
1237 (should (re-search-forward org-babel-src-block-regexp nil t))
1238 (goto-char (match-beginning 0))
1239 ;; now that we've located the code block, it may be evaluated
1240 (let ((org-babel-execute-src-block "RESULTS"))
1241 (org-babel-execute-src-block))
1242 (buffer-string)))))
1244 (ert-deftest test-ob/location-of-header-arg-eval ()
1245 "Test location of header argument evaluation."
1246 (org-test-with-temp-text "
1247 #+name: top-block
1248 #+begin_src emacs-lisp :var pt=(point)
1250 #+end_src
1252 #+name: bottom-block
1253 #+begin_src emacs-lisp :var pt=top-block()
1255 #+end_src
1257 ;; the value of the second block should be greater than the first
1258 (should
1259 (< (progn (re-search-forward org-babel-src-block-regexp nil t)
1260 (goto-char (match-beginning 0))
1261 (prog1 (save-match-data (org-babel-execute-src-block))
1262 (goto-char (match-end 0))))
1263 (progn (re-search-forward org-babel-src-block-regexp nil t)
1264 (goto-char (match-beginning 0))
1265 (org-babel-execute-src-block))))))
1267 (ert-deftest test-ob/preserve-results-indentation ()
1268 "Preserve indentation when executing a src block."
1269 (should
1270 (equal
1271 '(2 2)
1272 (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1273 (org-babel-execute-src-block)
1274 (let ((case-fold-search t)) (search-forward "RESULTS"))
1275 (list (org-get-indentation)
1276 (progn (forward-line) (org-get-indentation))))))
1277 (should
1278 (equal
1279 '(2 2)
1280 (org-test-with-temp-text
1281 " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1282 (org-babel-execute-src-block)
1283 (let ((case-fold-search t)) (search-forward "RESULTS"))
1284 (list (org-get-indentation)
1285 (progn (forward-line) (org-get-indentation))))))
1286 ;; Don't get fooled by TAB-based indentation.
1287 (should
1288 (equal
1289 '(6 6)
1290 (org-test-with-temp-text
1291 "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
1292 (setq tab-width 4)
1293 (org-babel-execute-src-block)
1294 (let ((case-fold-search t)) (search-forward "RESULTS"))
1295 (list (org-get-indentation)
1296 (progn (forward-line) (org-get-indentation)))))))
1298 (ert-deftest test-ob/safe-header-args ()
1299 "Detect safe and unsafe header args."
1300 (let ((safe-args '((:cache . "foo")
1301 (:results . "output")
1302 (:eval . "never")
1303 (:eval . "query")))
1304 (unsafe-args '((:eval . "yes")
1305 (:results . "output file")
1306 (:foo . "bar")))
1307 (malformed-args '((bar . "foo")
1308 ("foo" . "bar")
1309 :foo))
1310 (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
1311 (dolist (arg safe-args)
1312 (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
1313 (dolist (arg unsafe-args)
1314 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1315 (dolist (arg malformed-args)
1316 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1317 (should (not (funcall safe-p (append safe-args unsafe-args))))))
1319 (ert-deftest test-ob/noweb-expansions-in-cache ()
1320 "Ensure that noweb expansions are expanded before caching."
1321 (let ((noweb-expansions-in-cache-var 0))
1322 (org-test-with-temp-text "
1323 #+name: foo
1324 #+begin_src emacs-lisp
1325 \"I said\"
1326 #+end_src
1328 #+name: bar
1329 #+begin_src emacs-lisp :noweb yes :cache yes
1330 (setq noweb-expansions-in-cache-var
1331 (+ 1 noweb-expansions-in-cache-var))
1332 (concat <<foo>> \" check noweb expansions\")
1333 #+end_src
1335 ;; run the second block to create the cache
1336 (goto-char (point-min))
1337 (re-search-forward (regexp-quote "#+name: bar"))
1338 (should (string= "I said check noweb expansions"
1339 (org-babel-execute-src-block)))
1340 (should (= noweb-expansions-in-cache-var 1))
1341 ;; change the value of the first block
1342 (goto-char (point-min))
1343 (re-search-forward (regexp-quote "said"))
1344 (goto-char (match-beginning 0))
1345 (insert "haven't ")
1346 (re-search-forward (regexp-quote "#+name: bar"))
1347 (should (string= "I haven't said check noweb expansions"
1348 (org-babel-execute-src-block)))
1349 (should (= noweb-expansions-in-cache-var 2)))))
1351 (ert-deftest test-org-babel/file-ext-and-output-dir ()
1352 (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
1353 (org-babel-next-src-block)
1354 (should (equal "file-ext-basic.txt"
1355 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1356 (org-babel-next-src-block)
1357 (should (equal "foo/file-ext-dir-relative.txt"
1358 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1359 (org-babel-next-src-block)
1360 (should (equal "foo/file-ext-dir-relative-slash.txt"
1361 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1362 (org-babel-next-src-block)
1363 (should (equal "/tmp/file-ext-dir-absolute.txt"
1364 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1365 (org-babel-next-src-block)
1366 (should (equal "foo.bar"
1367 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1368 (org-babel-next-src-block)
1369 (should (equal "xxx/foo.bar"
1370 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1373 (ert-deftest test-org-babel/script-escape ()
1374 ;; Delimited lists of numbers
1375 (should (equal '(1 2 3)
1376 (org-babel-script-escape "[1 2 3]")))
1377 (should (equal '(1 2 3)
1378 (org-babel-script-escape "{1 2 3}")))
1379 (should (equal '(1 2 3)
1380 (org-babel-script-escape "(1 2 3)")))
1381 ;; Delimited lists of double-quoted strings
1382 (should (equal '("foo" "bar")
1383 (org-babel-script-escape "(\"foo\" \"bar\")")))
1384 (should (equal '("foo" "bar")
1385 (org-babel-script-escape "[\"foo\" \"bar\"]")))
1386 (should (equal '("foo" "bar")
1387 (org-babel-script-escape "{\"foo\" \"bar\"}")))
1388 ;; ... with commas
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 (should (equal '("foo" "bar")
1394 (org-babel-script-escape "{\"foo\", \"bar\"}")))
1395 ;; Delimited lists of single-quoted strings
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 (should (equal '("foo" "bar")
1401 (org-babel-script-escape "{'foo' 'bar'}")))
1402 ;; ... with commas
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 (should (equal '("foo" "bar")
1408 (org-babel-script-escape "{'foo', 'bar'}")))
1409 ;; Single quoted strings
1410 (should (equal "foo"
1411 (org-babel-script-escape "'foo'")))
1412 ;; ... with internal double quote
1413 (should (equal "foo\"bar"
1414 (org-babel-script-escape "'foo\"bar'")))
1415 ;; ... with internal backslash
1416 (should (equal "foo\\bar"
1417 (org-babel-script-escape "'foo\\bar'")))
1418 ;; ... with internal escaped backslash
1419 (should (equal "foo\\bar"
1420 (org-babel-script-escape "'foo\\\\bar'")))
1421 ;; ... with internal backslash-double quote
1422 (should (equal "foo\\\"bar"
1423 (org-babel-script-escape "'foo\\\"bar'")))
1424 ;; ... with internal escaped backslash-double quote
1425 (should (equal "foo\\\"bar"
1426 (org-babel-script-escape "'foo\\\\\"bar'")))
1427 ;; ... with internal escaped single quote
1428 (should (equal "foo'bar"
1429 (org-babel-script-escape "'foo\\'bar'")))
1430 ;; ... with internal escaped backslash-escaped single quote
1431 (should (equal "foo\\'bar"
1432 (org-babel-script-escape "'foo\\\\\\'bar'")))
1433 ;; Double quoted strings
1434 (should (equal "foo"
1435 (org-babel-script-escape "\"foo\"")))
1436 ;; ... with internal single quote
1437 (should (equal "foo'bar"
1438 (org-babel-script-escape "\"foo'bar\"")))
1439 ;; ... with internal backslash
1440 (should (equal "foo\\bar"
1441 (org-babel-script-escape "\"foo\\bar\"")))
1442 ;; ... with internal escaped backslash
1443 (should (equal "foo\\bar"
1444 (org-babel-script-escape "\"foo\\\\bar\"")))
1445 ;; ... with internal backslash-single quote
1446 (should (equal "foo\\'bar"
1447 (org-babel-script-escape "\"foo\\'bar\"")))
1448 ;; ... with internal escaped backslash-single quote
1449 (should (equal "foo\\'bar"
1450 (org-babel-script-escape "\"foo\\\\'bar\"")))
1451 ;; ... with internal escaped double quote
1452 (should (equal "foo\"bar"
1453 (org-babel-script-escape "\"foo\\\"bar\"")))
1454 ;; ... with internal escaped backslash-escaped double quote
1455 (should (equal "foo\\\"bar"
1456 (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
1458 (ert-deftest ob/process-params-no-duplicates ()
1459 (should (equal (org-babel-process-params '((:colname-names . 1)
1460 (:rowname-names . 1)
1461 (:result-params . 1)
1462 (:result-type . 1)
1463 (:var . "\"foo\"")))
1464 '((:var)
1465 (:colname-names . 1)
1466 (:rowname-names . 1)
1467 (:result-params . 1)
1468 (:result-type . value)))))
1470 (defun org-test-babel-confirm-evaluate (eval-value)
1471 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
1473 #+end_src" eval-value)
1474 (goto-char (point-min))
1475 (let ((info (org-babel-get-src-block-info)))
1476 (org-babel-check-confirm-evaluate info))))
1478 (ert-deftest ob/check-eval ()
1479 (let ((org-confirm-babel-evaluate t))
1480 ;; Non-export tests
1481 (dolist (pair '(("no" . nil)
1482 ("never" . nil)
1483 ("query" . query)
1484 ("yes" . query)))
1485 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
1486 ;; Export tests
1487 (let ((org-babel-exp-reference-buffer t))
1488 (dolist (pair '(("no" . nil)
1489 ("never" . nil)
1490 ("query" . query)
1491 ("yes" . query)
1492 ("never-export" . nil)
1493 ("no-export" . nil)
1494 ("query-export" . query)))
1495 (message (car pair))
1496 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))))
1497 (let ((org-confirm-babel-evaluate nil))
1498 ;; Non-export tests
1499 (dolist (pair '(("no" . nil)
1500 ("never" . nil)
1501 ("query" . query)
1502 ("yes" . t)))
1503 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
1504 ;; Export tests
1505 (let ((org-babel-exp-reference-buffer t))
1506 (dolist (pair '(("no" . nil)
1507 ("never" . nil)
1508 ("query" . query)
1509 ("yes" . t)
1510 ("never-export" . nil)
1511 ("no-export" . nil)
1512 ("query-export" . query)))
1513 (message (car pair))
1514 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair)))))))
1516 (defun org-test-ob/update-block-body ()
1517 "Test `org-babel-update-block-body' specifications."
1518 (should
1519 (equal "#+begin_src elisp\n 2\n#+end_src"
1520 (let ((org-edit-src-content-indentation 2))
1521 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1522 (org-babel-update-block-body "2")
1523 (buffer-string)))))
1524 ;; Preserve block indentation.
1525 (should
1526 (equal " #+begin_src elisp\n 2\n #+end_src"
1527 (let ((org-edit-src-content-indentation 1))
1528 (org-test-with-temp-text
1529 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1530 (org-babel-update-block-body "2")
1531 (buffer-string)))))
1532 ;; Ignore NEW-BODY global indentation.
1533 (should
1534 (equal "#+begin_src elisp\n 2\n#+end_src"
1535 (let ((org-edit-src-content-indentation 2))
1536 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1537 (org-babel-update-block-body " 2")
1538 (buffer-string)))))
1539 ;; When indentation should be preserved ignore the two rules above.
1540 (should
1541 (equal " #+begin_src elisp\n2\n #+end_src"
1542 (let ((org-edit-src-content-indentation 1)
1543 (org-src-preserve-indentation t))
1544 (org-test-with-temp-text
1545 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1546 (org-babel-update-block-body "2")
1547 (buffer-string)))))
1548 (should
1549 (equal " #+begin_src elisp -i\n2\n #+end_src"
1550 (let ((org-edit-src-content-indentation 1))
1551 (org-test-with-temp-text
1552 " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
1553 (org-babel-update-block-body "2")
1554 (buffer-string)))))
1555 (should
1556 (equal "#+begin_src elisp\n 2\n#+end_src"
1557 (let ((org-edit-src-content-indentation 2)
1558 (org-src-preserve-indentation t))
1559 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1560 (org-babel-update-block-body " 2")
1561 (buffer-string)))))
1562 (should
1563 (equal "#+begin_src elisp -i\n 2\n#+end_src"
1564 (let ((org-edit-src-content-indentation 2)
1565 (org-src-preserve-indentation t))
1566 (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
1567 (org-babel-update-block-body " 2")
1568 (buffer-string))))))
1570 (provide 'test-ob)
1572 ;;; test-ob ends here