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/>.
23 (eval-and-compile (require 'cl-lib
))
25 (ert-deftest test-ob
/indented-cached-org-bracket-link
()
26 "When the result of a source block is a cached indented link it
27 should still return the link."
29 (let ((default-directory temporary-file-directory
))
30 (org-test-with-temp-text
33 #+<point>BEGIN_SRC emacs-lisp :file test.txt :cache yes
36 ;; Execute twice as the first time creates the cache.
37 (org-babel-execute-src-block)
38 (string= (expand-file-name "test.txt")
39 (org-babel-execute-src-block))))))
41 (ert-deftest test-ob
/multi-line-header-regexp
()
42 (should(equal "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
43 org-babel-multi-line-header-regexp
))
44 ;;TODO can be optimised - and what about blah4 blah5 blah6?
46 org-babel-multi-line-header-regexp
47 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
50 "blah1 blah2 blah3 \t"
53 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
55 ;;TODO Check - should this fail?
57 (not (org-test-string-exact-match
58 org-babel-multi-line-header-regexp
59 " \t #+headers : blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))))
61 (ert-deftest test-ob
/src-block-regexp
()
64 "#+begin_src language -n-r-a-b -c :argument-1 yes :argument-2 no\n"
65 "echo this is a test\n"
66 "echo Currently in ' $PWD\n"
69 (flags "-n-r-a-b -c ")
70 (arguments ":argument-1 yes :argument-2 no")
71 (body "echo this is a test\necho Currently in ' $PWD\n"))
72 (should (string-match org-babel-src-block-regexp test-block
))
73 (should (string-match org-babel-src-block-regexp
(upcase test-block
)))
74 (should (equal language
(match-string 2 test-block
)))
75 ;;TODO Consider refactoring
76 (should (equal flags
(match-string 3 test-block
)))
77 (should (equal arguments
(match-string 4 test-block
)))
78 (should (equal body
(match-string 5 test-block
)))
80 (should (org-test-string-exact-match
81 org-babel-src-block-regexp
82 (replace-regexp-in-string flags
"" test-block
)))
84 (should (org-test-string-exact-match
85 org-babel-src-block-regexp
86 (replace-regexp-in-string arguments
"" test-block
)))
87 ;; should be valid with no body
88 (should (org-test-string-exact-match
89 org-babel-src-block-regexp
90 (replace-regexp-in-string body
"" test-block
)))))
92 (ert-deftest test-ob
/default-inline-header-args
()
95 (:results .
"replace")
96 (:exports .
"results")
98 org-babel-default-inline-header-args
)))
100 (ert-deftest ob-test
/org-babel-combine-header-arg-lists
()
101 (let ((results (org-babel-combine-header-arg-lists
104 (baz .
((foo bar
) (baz)))
105 (qux .
((foo bar baz qux
)))
106 (quux .
((foo bar
))))
110 (dolist (pair '((foo .
:any
)
114 (qux .
((foo bar baz qux
)))))
115 (should (equal (cdr pair
)
116 (cdr (assoc (car pair
) results
)))))))
118 ;;; ob-get-src-block-info
119 (ert-deftest test-ob
/get-src-block-info-language
()
120 (org-test-at-marker nil org-test-file-ob-anchor
121 (let ((info (org-babel-get-src-block-info)))
122 (should (string= "emacs-lisp" (nth 0 info
))))))
124 (ert-deftest test-ob
/get-src-block-info-body
()
125 (org-test-at-marker nil org-test-file-ob-anchor
126 (let ((info (org-babel-get-src-block-info)))
127 (should (string-match (regexp-quote org-test-file-ob-anchor
)
130 (ert-deftest test-ob
/get-src-block-info-tangle
()
131 (org-test-at-marker nil org-test-file-ob-anchor
132 (let ((info (org-babel-get-src-block-info)))
133 (should (string= "no" (cdr (assq :tangle
(nth 2 info
))))))))
135 (ert-deftest test-ob
/elisp-in-header-arguments
()
136 "Test execution of elisp forms in header arguments."
137 (org-test-with-temp-text-in-file "
139 * elisp forms in header arguments
141 :header-args: :var prop = (* 7 6)
143 #+begin_src emacs-lisp
146 (goto-char (point-min))
147 (org-babel-next-src-block)
148 (should (= 42 (org-babel-execute-src-block)))))
150 (ert-deftest test-ob
/simple-named-code-block
()
151 "Test that simple named code blocks can be evaluated."
152 (org-test-with-temp-text-in-file "
154 #+name: i-have-a-name
155 #+begin_src emacs-lisp
158 (org-babel-next-src-block 1)
159 (should (= 42 (org-babel-execute-src-block)))))
161 (ert-deftest test-ob
/simple-variable-resolution
()
162 "Test that simple variable resolution is working."
163 (org-test-with-temp-text-in-file "
166 #+begin_src emacs-lisp
170 #+begin_src emacs-lisp :var four=four
174 (org-babel-next-src-block 2)
175 (should (= 4 (org-babel-execute-src-block)))
177 (should (string= ": 4" (buffer-substring
181 (ert-deftest test-ob
/multi-line-header-arguments
()
182 "Test that multi-line header arguments and can be read."
183 (org-test-with-temp-text-in-file "
185 #+headers: :var letters='(a b c d e f g)
186 #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
188 (cl-map 'list #'list numbers letters)
190 (org-babel-next-src-block)
191 (let ((results (org-babel-execute-src-block)))
192 (should (eq 'a
(cadr (assoc 1 results
))))
193 (should (eq 'd
(cadr (assoc 4 results
)))))))
195 (ert-deftest test-ob
/parse-header-args
()
196 (org-test-with-temp-text-in-file "
198 #+begin_src example-lang :session :results output :var num=9
202 (org-babel-next-src-block)
203 (let* ((info (org-babel-get-src-block-info))
204 (params (nth 2 info
)))
205 (message "%S" params
)
206 (should (equal "example-lang" (nth 0 info
)))
207 (should (string= "the body" (org-trim (nth 1 info
))))
208 (should-not (member '(:session\ \ \ \
) params
))
209 (should (equal '(:session
) (assq :session params
)))
210 (should (equal '(:result-type . output
) (assq :result-type params
)))
211 (should (equal '(num .
9) (cdr (assq :var params
)))))))
213 (ert-deftest test-ob
/parse-header-args2
()
214 (org-test-with-temp-text-in-file "
216 * resolving sub-trees as references
218 #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
222 #+begin_src org :noweb yes
224 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
227 ** simple subtree with custom ID
229 :CUSTOM_ID: simple-subtree
233 (should (string-match (regexp-quote "this is simple")
234 (org-babel-ref-resolve "simple-subtree")))
235 (org-babel-next-src-block)
236 (should (= 14 (org-babel-execute-src-block)))))
238 (ert-deftest test-ob
/inline-src-blocks
()
241 (org-test-with-temp-text
242 "In the middle <point>src_emacs-lisp{(+ 0 1)} of a line"
243 (org-babel-execute-src-block))))
246 (org-test-with-temp-text
247 "One at the end of a line: <point>src_emacs-lisp{(+ 1 1)}"
248 (org-babel-execute-src-block))))
251 (org-test-with-temp-text
252 "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
253 (org-babel-execute-src-block))))
256 (org-test-with-temp-text
257 "In the middle <point>src_emacs-lisp[:results silent\
258 :exports code]{(+ 3 1)} of a line"
259 (org-babel-execute-src-block))))
262 (org-test-with-temp-text
263 "One at the end of a line: <point>src_emacs-lisp[:results silent\
264 :exports code]{(+ 4 1)}"
265 (org-babel-execute-src-block))))
268 (org-test-with-temp-text
269 "src_emacs-lisp[:results silent :exports code]{(+ 5 1)}\
270 at the beginning of a line."
271 (org-babel-execute-src-block))))
274 (org-test-with-temp-text
275 "One also evaluated: <point>src_emacs-lisp[:exports both\
276 :results silent]{(+ 6 1)}"
277 (org-babel-execute-src-block)))))
279 (ert-deftest test-ob
/inline-src_blk-default-results-replace-line-1
()
280 (let ((test-line "src_sh{echo 1}")
281 (org-babel-inline-result-wrap "=%s="))
282 ;; src_ at bol line 1...
283 (org-test-with-temp-text
285 (goto-char (point-min)) (org-babel-execute-maybe)
287 (concat test-line
" {{{results(=1=)}}}")
288 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
289 (forward-char) (org-babel-execute-maybe)
291 (concat test-line
" {{{results(=1=)}}}")
292 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
293 (re-search-forward "{{{")
294 ;;(should-error (org-ctrl-c-ctrl-c))
295 (backward-char 4) ;; last char of block body
296 (org-babel-execute-maybe)
298 (concat test-line
" {{{results(=1=)}}}")
299 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
300 ;; src_ follows space line 1...
301 (let ((test-line " src_emacs-lisp{ 1 }"))
302 (org-test-with-temp-text
304 (should-error (org-ctrl-c-ctrl-c))
305 (forward-char) (org-babel-execute-maybe)
307 (concat test-line
" {{{results(=1=)}}}")
308 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
309 (re-search-forward "{ 1 ") (org-babel-execute-maybe)
311 (concat test-line
" {{{results(=1=)}}}")
312 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
314 (should-error (org-ctrl-c-ctrl-c))))
315 ;; Results on a subsequent line are replaced.
318 "src_emacs-lisp{(+ 1 2)}\n {{{results(=3=)}}}"
319 (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n {{{results(=2=)}}}"
320 (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
322 ;; Also handle results at the beginning of a line.
325 "src_emacs-lisp{(+ 1 2)}\n{{{results(=3=)}}}"
326 (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n{{{results(=2=)}}}"
327 (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
330 (ert-deftest test-ob
/inline-src_blk-default-results-replace-line-2
()
331 ;; src_ at bol line 2...
332 (let ((test-line " src_emacs-lisp{ \"x\" }")
333 (org-babel-inline-result-wrap "=%s="))
334 (org-test-with-temp-text
335 (concat "\n" test-line
)
336 (should-error (org-ctrl-c-ctrl-c))
337 (goto-char (point-min))
338 (should-error (org-ctrl-c-ctrl-c))
340 (should-error (org-ctrl-c-ctrl-c))
341 (forward-char) (org-babel-execute-maybe)
343 (concat test-line
" {{{results(=x=)}}}")
344 (buffer-substring-no-properties
345 (point-at-bol) (point-at-eol))))))
346 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
347 (org-babel-inline-result-wrap "=%s="))
348 (org-test-with-temp-text
350 (goto-char (point-max))
351 (insert (concat "\n" test-line
" end"))
352 (re-search-backward "src") (org-babel-execute-maybe)
354 (concat test-line
" {{{results(=y=)}}} end")
355 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
356 (re-search-forward "\" ") (org-babel-execute-maybe)
358 (concat test-line
" {{{results(=y=)}}} end")
359 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
361 (should-error (org-ctrl-c-ctrl-c)))))
363 (ert-deftest test-ob
/inline-src_blk-manual-results-replace
()
364 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
365 (org-babel-inline-result-wrap "=%s="))
366 (org-test-with-temp-text
367 (concat "\n" test-line
)
368 (should-error (org-ctrl-c-ctrl-c))
369 (goto-char (point-max))
370 (org-babel-execute-maybe)
372 (should-error (org-ctrl-c-ctrl-c))
373 (forward-char) (org-babel-execute-maybe)
375 (concat test-line
" {{{results(=x=)}}}")
376 (buffer-substring-no-properties
377 (point-at-bol) (point-at-eol))))))
378 (let ((test-line (concat " Some text prior to block "
379 "src_emacs-lisp[:results replace]{ \"y\" }"))
380 (org-babel-inline-result-wrap "=%s="))
381 (org-test-with-temp-text test-line
382 (goto-char (point-max))
383 (insert (concat "\n" test-line
" end"))
384 (re-search-backward "src") (org-babel-execute-maybe)
386 (concat test-line
" {{{results(=y=)}}} end")
387 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
388 (re-search-forward "\" ") (org-babel-execute-maybe)
390 (concat test-line
" {{{results(=y=)}}} end")
391 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
393 (should-error (org-ctrl-c-ctrl-c)))))
395 (ert-deftest test-ob
/inline-src_blk-results-silent
()
396 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
397 (org-test-with-temp-text test-line
398 (org-babel-execute-maybe)
399 (should (string= test-line
400 (buffer-substring-no-properties
401 (point-at-bol) (point-at-eol))))))
402 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
403 "[ :results silent ]{ \"y\" }")))
404 (org-test-with-temp-text
406 (goto-char (point-max))
407 (insert (concat "\n" test-line
" end"))
408 (re-search-backward "src_") (org-babel-execute-maybe)
409 (should (string= (concat test-line
" end")
410 (buffer-substring-no-properties
411 (point-at-bol) (point-at-eol))))
412 (re-search-forward "\" ") (org-babel-execute-maybe)
413 (should (string= (concat test-line
" end")
414 (buffer-substring-no-properties
415 (point-at-bol) (point-at-eol))))
417 (should-error (org-ctrl-c-ctrl-c)))))
419 (ert-deftest test-ob
/inline-src_blk-results-raw
()
420 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
421 (org-test-with-temp-text test-line
422 (org-babel-execute-maybe)
423 (should (string= (concat test-line
" x")
425 (let ((test-line (concat " Some text prior to block "
426 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
427 (org-test-with-temp-text (concat test-line
" end")
428 (re-search-forward "src_") (org-babel-execute-maybe)
429 (should (string= (concat test-line
" the end")
430 (buffer-substring-no-properties
431 (point-at-bol) (point-at-eol))))
432 (re-search-forward "\" ") (org-babel-execute-maybe)
433 (should (string= (concat test-line
" the the end")
434 (buffer-substring-no-properties
435 (point-at-bol) (point-at-eol))))
437 (should-error (org-ctrl-c-ctrl-c)))))
439 (ert-deftest test-ob
/inline-src_blk-results-file
()
440 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
441 (org-test-with-temp-text
443 (org-babel-execute-maybe)
444 (should (string= (concat test-line
" {{{results([[file:~/test-file]])}}}")
445 (buffer-substring-no-properties
446 (point-min) (point-max)))))))
448 (ert-deftest test-ob
/inline-src_blk-results-scalar
()
449 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
450 (org-babel-inline-result-wrap "=%s="))
451 (org-test-with-temp-text
453 (org-babel-execute-maybe)
454 (should (string= (concat test-line
" {{{results(=\"x\"=)}}}")
455 (buffer-substring-no-properties
456 (point-min) (point-max)))))))
458 (ert-deftest test-ob
/inline-src_blk-results-verbatim
()
459 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
460 (org-babel-inline-result-wrap "=%s="))
461 (org-test-with-temp-text
463 (org-babel-execute-maybe)
464 (should (string= (concat test-line
" {{{results(=\"x\"=)}}}")
465 (buffer-substring-no-properties
466 (point-min) (point-max)))))))
468 (ert-deftest test-ob
/combining-scalar-and-raw-result-types
()
469 (org-test-with-temp-text-in-file "
471 #+begin_src sh :results scalar
472 echo \"[[file:./cv.cls]]\"
478 #+begin_src sh :results raw scalar
479 echo \"[[file:./cv.cls]]\"
482 (cl-flet ((next-result ()
483 (org-babel-next-src-block)
484 (org-babel-execute-src-block)
485 (goto-char (org-babel-where-is-src-block-result))
487 (goto-char (point-min))
489 (should (eq (org-element-type (org-element-at-point)) 'fixed-width
))
492 (eq (org-element-type (org-element-at-point)) 'fixed-width
)))))
494 (ert-deftest test-ob
/no-defaut-value-for-var
()
495 "Test that the absence of a default value for a variable DOES THROW
497 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
498 (org-babel-next-src-block)
500 (should-error (org-babel-execute-src-block) :type
'error
)))
504 "Variable \"x\" must be assigned a default value")
507 (ert-deftest test-ob
/just-one-results-block
()
508 "Test that evaluating two times the same code block does not result in a
509 duplicate results block."
510 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
511 (org-babel-execute-src-block)
512 (org-babel-execute-src-block) ; second code block execution
513 (should (search-forward "Hello")) ; the string inside the source code block
514 (should (search-forward "Hello")) ; the same string in the results block
515 (should-error (search-forward "Hello"))))
517 (ert-deftest test-ob
/nested-code-block
()
518 "Test nested code blocks inside code blocks don't cause problems."
520 (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
521 (org-test-with-temp-text "#+begin_src org :results silent
522 ,#+begin_src emacs-lisp
526 (let ((org-edit-src-content-indentation 2)
527 (org-src-preserve-indentation nil
))
528 (org-babel-execute-src-block))))))
530 (ert-deftest test-ob
/partial-nested-code-block
()
531 "Test nested code blocks inside code blocks don't cause problems."
532 (org-test-with-temp-text "#+begin_src org :results silent
533 ,#+begin_src emacs-lisp
535 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
537 (ert-deftest test-ob
/does-not-replace-a-block-with-the-results
()
538 (org-test-with-temp-text "#+NAME: foo
539 #+BEGIN_SRC emacs-lisp
542 (org-babel-next-src-block 1)
543 (should (eq 'foo
(org-babel-execute-src-block)))
544 (goto-char (point-min))
545 (org-babel-next-src-block 1)
546 (should (looking-at org-babel-src-block-regexp
))))
548 (ert-deftest test-ob
/catches-all-references
()
549 (org-test-with-temp-text "
550 #+NAME: literal-example
556 #+NAME: read-literal-example
557 #+BEGIN_SRC emacs-lisp :var x=literal-example
558 (cl-concatenate 'string x \" for me.\")
560 (org-babel-next-src-block 1)
561 (should (string= (org-babel-execute-src-block)
562 "A literal example\non two lines\n for me."))))
564 (ert-deftest test-ob
/ignore-reference-in-commented-headings
()
567 (org-test-with-temp-text
579 <point>#+BEGIN_SRC emacs-lisp :var x=n
582 (org-babel-execute-src-block)))))
584 (ert-deftest test-ob
/do-not-resolve-to-partial-names-data
()
585 (org-test-with-temp-text "
594 #+begin_src emacs-lisp :var x=base
597 (org-babel-next-src-block 1)
598 (should (equal (org-babel-execute-src-block) '((3) (4))))))
600 (ert-deftest test-ob
/do-not-resolve-to-partial-names-code
()
601 (org-test-with-temp-text "
603 #+begin_src emacs-lisp
608 #+begin_src emacs-lisp
612 #+begin_src emacs-lisp :var x=base
615 (org-babel-next-src-block 3)
616 (should (equal (org-babel-execute-src-block) "foo"))))
618 (ert-deftest test-ob
/allow-spaces-around-
=-in-var-specs
()
619 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
623 (should (= 10 (org-babel-execute-src-block)))))
625 (ert-deftest test-ob
/org-babel-update-intermediate
()
626 (org-test-with-temp-text "#+name: foo
627 #+begin_src emacs-lisp
634 #+begin_src emacs-lisp :var it=foo
637 (let ((org-babel-update-intermediate nil
))
638 (goto-char (point-min))
639 (org-babel-next-src-block 2)
640 (should (= 3 (org-babel-execute-src-block)))
641 (goto-char (point-min))
643 (should (looking-at ": 4")))
644 (let ((org-babel-update-intermediate t
))
645 (goto-char (point-min))
646 (org-babel-next-src-block 2)
647 (should (= 3 (org-babel-execute-src-block)))
648 (goto-char (point-min))
650 (should (looking-at ": 2")))))
652 (ert-deftest test-ob
/eval-header-argument
()
653 (cl-flet ((check-eval (eval runp
)
654 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
657 (let ((foo :not-run
))
659 (progn (should (org-babel-execute-src-block))
660 (should (eq foo
:evald
)))
661 (progn (should-not (org-babel-execute-src-block))
662 (should-not (eq foo
:evald
))))))))
663 (check-eval "never" nil
)
664 (check-eval "no" nil
)
665 (check-eval "never-export" t
)
666 (check-eval "no-export" t
)
667 (let ((org-babel-exp-reference-buffer (current-buffer)))
668 (check-eval "never" nil
)
669 (check-eval "no" nil
)
670 (check-eval "never-export" nil
)
671 (check-eval "no-export" nil
))))
673 (ert-deftest test-ob
/noweb-expansion
()
678 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
686 (org-babel-expand-noweb-references))))
687 ;; Handle :noweb-sep.
691 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
695 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
699 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
702 (org-babel-expand-noweb-references))))
703 ;; :noweb-ref is extracted from definition, not point of call.
707 (org-test-with-temp-text
711 :header-args: :noweb-ref bar
714 <point>#+begin_src emacs-lisp :results output :tangle yes
720 :header-args: :noweb-ref foo
723 #+begin_src sh :noweb-sep \"\"
726 (org-babel-expand-noweb-references))))
727 ;; Handle recursive expansion.
730 (org-test-with-temp-text "
731 #+begin_src emacs-lisp :noweb yes<point>
736 #+begin_src emacs-lisp :noweb yes
741 #+begin_src emacs-lisp
744 (org-babel-expand-noweb-references))))
745 ;; During recursive expansion, obey to `:noweb' property.
748 (org-test-with-temp-text "
749 #+begin_src emacs-lisp :noweb yes<point>
754 #+begin_src emacs-lisp :noweb no
759 #+begin_src emacs-lisp
762 (org-babel-expand-noweb-references)))))
764 (ert-deftest test-ob
/splitting-variable-lists-in-references
()
765 (org-test-with-temp-text ""
766 (should (= 1 (length (org-babel-ref-split-args
767 "a=\"this, no work\""))))
768 (should (= 2 (length (org-babel-ref-split-args
769 "a=\"this, no work\", b=1"))))))
771 (ert-deftest test-ob
/balanced-split
()
772 "Test `org-babel-balanced-split' specifications."
774 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
775 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
777 ;; Handle un-balanced parens.
779 (equal '(":foo ((6)" "bar 1")
780 (org-babel-balanced-split ":foo ((6) :bar 1" '((32 9) .
58))))
782 (equal '(":foo \"(foo\"" "bar 2")
783 (org-babel-balanced-split ":foo \"(foo\" :bar 2" '((32 9) .
58))))
784 ;; Handle un-balanced quotes.
786 (equal '(":foo \"1" "bar 3")
787 (org-babel-balanced-split ":foo \"1 :bar 3" '((32 9) .
58))))
788 ;; Handle empty string.
790 (equal '(":foo \"\"")
791 (org-babel-balanced-split ":foo \"\"" '((32 9) .
58))))
792 ;; Handle control characters within double quotes.
794 (equal '(":foo \"\\n\"")
795 (org-babel-balanced-split ":foo \"\\n\"" '((32 9) .
58)))))
797 (ert-deftest test-ob
/commented-last-block-line-no-var
()
798 (org-test-with-temp-text-in-file "
799 #+begin_src emacs-lisp
802 (org-babel-next-src-block)
803 (org-babel-execute-maybe)
804 (should (re-search-forward "\\#\\+results:" nil t
))
809 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
810 (org-test-with-temp-text-in-file "
811 #+begin_src emacs-lisp
814 (org-babel-next-src-block)
815 (org-babel-execute-maybe)
816 (should (re-search-forward "\\#\\+results:" nil t
))
821 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
823 (ert-deftest test-ob
/commented-last-block-line-with-var
()
824 (org-test-with-temp-text-in-file "
825 #+begin_src emacs-lisp :var a=1
828 (org-babel-next-src-block)
829 (org-babel-execute-maybe)
830 (re-search-forward "\\#\\+results:" nil t
)
834 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
835 (org-test-with-temp-text-in-file "
836 #+begin_src emacs-lisp :var a=2
839 (org-babel-next-src-block)
840 (org-babel-execute-maybe)
841 (re-search-forward "\\#\\+results:" nil t
)
845 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
847 (ert-deftest test-ob
/org-babel-insert-result
()
848 "Test `org-babel-insert-result' specifications."
849 ;; Do not error when output is an improper list.
851 (org-test-with-temp-text
853 <point>#+BEGIN_SRC emacs-lisp
857 (org-babel-execute-maybe) t
))
858 ;; Escape headlines when producing an example block.
861 ",\\* Not an headline"
862 (org-test-with-temp-text
864 <point>#+BEGIN_SRC emacs-lisp
865 \"* Not an headline\"
868 (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
870 ;; Escape special syntax in example blocks.
874 (org-test-with-temp-text
876 <point>#+BEGIN_SRC emacs-lisp
880 (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
882 ;; No escaping is done with other blocks or raw type.
885 ",\\* Not an headline"
886 (org-test-with-temp-text
888 <point>#+BEGIN_SRC emacs-lisp
889 \"* Not an headline\"
892 (let ((org-babel-min-lines-for-block-output 10))
893 (org-babel-execute-maybe))
897 ",\\* Not an headline"
898 (org-test-with-temp-text
900 <point>#+BEGIN_SRC emacs-lisp :results raw
901 \"* Not an headline\"
904 (org-babel-execute-maybe)
908 ",\\* Not an headline"
909 (org-test-with-temp-text
911 <point>#+BEGIN_SRC emacs-lisp :results drawer
912 \"* Not an headline\"
915 (org-babel-execute-maybe)
918 (ert-deftest test-ob
/remove-inline-result
()
919 "Test `org-babel-remove-inline-result' honors whitespace."
921 ((inline-sb "src_emacs-lisp{(+ 1 2)}")
922 (inline-res " {{{results(=3=)}}}")
923 (inline-sb-dot (concat inline-sb
"."))
924 (inline-sb-res-dot (concat inline-sb inline-res
".")))
925 (org-test-with-temp-text
926 ;; Insert inline_src_block followed by dot.
928 ;; Insert result before dot.
929 (org-babel-execute-maybe)
930 (should (string= inline-sb-res-dot
931 (buffer-substring-no-properties
932 (point-at-bol) (point-at-eol))))
933 ;; Delete whitespace and result.
934 (org-babel-remove-inline-result)
935 (should (string= inline-sb-dot
936 (buffer-substring-no-properties
937 (point-at-bol) (point-at-eol))))
938 ;; Add whitespace and result before dot.
939 (search-forward inline-sb
)
940 (insert " " inline-res
)
941 (goto-char (point-at-bol))
942 ;; Remove whitespace and result.
943 (org-babel-remove-inline-result)
944 (should (string= inline-sb-dot
945 (buffer-substring-no-properties
946 (point-at-bol) (point-at-eol))))
947 ;; Add whitespace before dot.
948 (search-forward inline-sb
)
950 (goto-char (point-at-bol))
951 ;; Add result before whitespace.
952 (org-babel-execute-maybe)
953 ;; Remove result - leave trailing whitespace and dot.
954 (org-babel-remove-inline-result)
955 (should (string= (concat inline-sb
" .")
956 (buffer-substring-no-properties
957 (point-at-bol) (point-at-eol)))))))
959 (ert-deftest test-ob
/org-babel-remove-result--results-default
()
960 "Test `org-babel-remove-result' with default :results."
961 (mapcar (lambda (language)
962 (test-ob-verify-result-and-removed-result
965 "* org-babel-remove-result
966 #+begin_src " language
"
970 '("sh" "emacs-lisp")))
972 (ert-deftest test-ob
/org-babel-results-indented-wrap
()
973 "Ensure that wrapped results are inserted correction when indented.
974 If not inserted correctly then the second evaluation will fail
975 trying to find the :END: marker."
976 (org-test-with-temp-text
978 #+begin_src sh :results file wrap
981 (org-babel-next-src-block 1)
982 (org-babel-execute-src-block)
983 (org-babel-execute-src-block)))
985 (ert-deftest test-ob
/file-desc-header-argument
()
986 "Test that the :file-desc header argument is used."
987 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
991 #+begin_src emacs-lisp :results file :file-desc
994 (org-babel-execute-src-block)
995 (org-babel-next-src-block 1)
996 (org-babel-execute-src-block)
997 (goto-char (point-min))
998 (should (search-forward "[[file:foo][bar]]" nil t
))
999 (should (search-forward "[[file:foo][foo]]" nil t
))))
1001 (ert-deftest test-ob
/result-file-link-type-header-argument
()
1002 "Ensure that the result is a link to a file.
1003 The file is just a link to `:file' value. Inhibit non-empty
1004 result write to `:file' value."
1005 (org-test-with-temp-text "
1006 <point>#+begin_src shell :results value link :file \"/tmp/test.txt\"
1007 echo \"hello\" > /tmp/test.txt
1010 (org-babel-execute-src-block)
1011 (should (search-forward "[[file:/tmp/test.txt]]" nil nil
))
1012 (should (with-temp-buffer
1013 (insert-file-contents "/tmp/test.txt")
1014 (string= "hello\n" (buffer-string))))))
1016 (ert-deftest test-ob
/result-graphics-link-type-header-argument
()
1017 "Ensure that the result is a link to a file.
1018 The file is just a link to `:file' value. Inhibit non-empty
1019 result write to `:file' value."
1020 (org-test-with-temp-text "
1021 <point>#+begin_src shell :results value graphics :file \"/tmp/test.txt\"
1022 echo \"hello\" > /tmp/test.txt
1025 (org-babel-execute-src-block)
1026 (should (search-forward "[[file:/tmp/test.txt]]" nil nil
))
1027 (should (with-temp-buffer
1028 (insert-file-contents "/tmp/test.txt")
1029 (string= "hello\n" (buffer-string))))))
1031 (ert-deftest test-ob
/inline-src_blk-preceded-punct-preceded-by-point
()
1032 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
1033 (org-babel-inline-result-wrap "=%s="))
1034 (org-test-with-temp-text
1037 (org-babel-execute-maybe)
1038 (should (re-search-forward "=\"x\"=" nil t
))
1041 (ert-deftest test-ob
/commented-last-block-line-with-var
()
1042 (org-test-with-temp-text-in-file "
1043 #+begin_src emacs-lisp :var a=1
1046 (org-babel-next-src-block)
1047 (org-babel-execute-maybe)
1048 (re-search-forward "\\#\\+results:" nil t
)
1052 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
1053 (org-test-with-temp-text-in-file "
1054 #+begin_src emacs-lisp :var a=2
1057 (org-babel-next-src-block)
1058 (org-babel-execute-maybe)
1059 (re-search-forward "\\#\\+results:" nil t
)
1063 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
1065 (defun test-ob-verify-result-and-removed-result (result buffer-text
)
1066 "Test helper function to test `org-babel-remove-result'.
1067 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
1068 and the result of execution is verified against RESULT.
1070 The block is actually executed /twice/ to ensure result
1071 replacement happens correctly."
1072 (org-test-with-temp-text
1074 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
1075 (should (re-search-forward "\\#\\+results:" nil t
))
1077 (should (string= result
1078 (buffer-substring-no-properties
1080 (- (point-max) 16))))
1081 (org-babel-previous-src-block) (org-babel-remove-result)
1082 (should (string= buffer-text
1083 (buffer-substring-no-properties
1084 (point-min) (point-max))))))
1086 (ert-deftest test-ob
/org-babel-remove-result--results-list
()
1087 "Test `org-babel-remove-result' with :results list."
1088 (test-ob-verify-result-and-removed-result
1093 "* org-babel-remove-result
1094 #+begin_src emacs-lisp :results list
1100 (ert-deftest test-ob
/org-babel-remove-result--results-wrap
()
1101 "Test `org-babel-remove-result' with :results wrap."
1102 (test-ob-verify-result-and-removed-result
1107 "* org-babel-remove-result
1109 #+begin_src emacs-lisp :results wrap
1115 (ert-deftest test-ob
/org-babel-remove-result--results-org
()
1116 "Test `org-babel-remove-result' with :results org."
1117 (test-ob-verify-result-and-removed-result
1124 "* org-babel-remove-result
1125 #+begin_src emacs-lisp :results org
1133 (ert-deftest test-ob
/org-babel-remove-result--results-html
()
1134 "Test `org-babel-remove-result' with :results html."
1135 (test-ob-verify-result-and-removed-result
1136 "#+begin_export html
1137 <head><body></body></head>
1140 "* org-babel-remove-result
1141 #+begin_src emacs-lisp :results html
1142 \"<head><body></body></head>\"
1147 (ert-deftest test-ob
/org-babel-remove-result--results-latex
()
1148 "Test `org-babel-remove-result' with :results latex."
1149 (test-ob-verify-result-and-removed-result
1150 "#+begin_export latex
1156 "* org-babel-remove-result
1157 #+begin_src emacs-lisp :results latex
1165 (ert-deftest test-ob
/org-babel-remove-result--results-code
()
1166 "Test `org-babel-remove-result' with :results code."
1168 (test-ob-verify-result-and-removed-result
1169 "#+begin_src emacs-lisp
1173 "* org-babel-remove-result
1174 #+begin_src emacs-lisp :results code
1175 (message \"I am working!\")
1180 (ert-deftest test-ob
/org-babel-remove-result--results-pp
()
1181 "Test `org-babel-remove-result' with :results pp."
1182 (test-ob-verify-result-and-removed-result
1183 ": \"I /am/ working!\""
1185 "* org-babel-remove-result
1186 #+begin_src emacs-lisp :results pp
1187 \"I /am/ working!\")
1192 (ert-deftest test-ob
/org-babel-remove-result--no-blank-line
()
1193 "Test `org-babel-remove-result' without blank line between code and results."
1196 #+begin_src emacs-lisp
1202 (org-test-with-temp-text
1204 <point>#+begin_src emacs-lisp
1210 (org-babel-execute-maybe)
1213 (ert-deftest test-ob
/results-do-not-replace-code-blocks
()
1214 (org-test-with-temp-text "Block two has a space after the name.
1217 #+begin_src emacs-lisp
1222 #+begin_src emacs-lisp
1227 #+begin_src emacs-lisp
1234 (dolist (num '(1 2 3))
1235 ;; execute the block
1236 (goto-char (point-min))
1237 (org-babel-next-src-block num
) (org-babel-execute-src-block)
1238 ;; check the results
1239 (goto-char (point-max))
1240 (move-beginning-of-line 0)
1241 (should (looking-at (format ": %d" num
))))))
1243 (ert-deftest test-ob
/blocks-with-spaces
()
1244 "Test expansion of blocks followed by blank lines."
1245 ;; Preserve number of blank lines after block.
1247 (equal "#+BEGIN_SRC emacs-lisp
1253 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1256 (let ((org-babel-next-src-block "RESULTS"))
1257 (org-babel-execute-src-block))
1259 ;; Do not add spurious blank lines after results.
1265 #+begin_src emacs-lisp
1273 (org-test-with-temp-text "
1276 #+begin_src emacs-lisp<point>
1281 (org-babel-execute-src-block)
1288 #+begin_src emacs-lisp
1296 (org-test-with-temp-text "
1299 #+begin_src emacs-lisp<point>
1307 (org-babel-execute-src-block)
1310 (ert-deftest test-ob
/results-in-narrowed-buffer
()
1311 "Test block execution in a narrowed buffer."
1312 ;; If results don't exist, they should be inserted in visible part
1316 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1317 (org-test-with-temp-text
1318 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1319 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1320 (let ((org-babel-results-keyword "RESULTS"))
1321 (org-babel-execute-src-block))
1322 (org-trim (buffer-string)))))
1325 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1326 (org-test-with-temp-text
1327 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1328 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1329 (let ((org-babel-results-keyword "RESULTS"))
1330 (org-babel-execute-src-block))
1331 (org-trim (buffer-string)))))
1332 ;; Results in visible part of buffer, should be updated here.
1336 #+BEGIN_SRC emacs-lisp
1342 (org-test-with-temp-text
1344 #+BEGIN_SRC emacs-lisp
1352 (narrow-to-region (point-min) (point))
1353 (goto-char (point-min))
1354 (let ((org-babel-results-keyword "RESULTS"))
1355 (org-babel-execute-src-block))
1356 (org-trim (buffer-string)))))
1357 ;; Results in invisible part of buffer, should be updated there.
1358 (org-test-with-temp-text
1360 #+BEGIN_SRC emacs-lisp
1368 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1369 (let ((org-babel-results-keyword "RESULTS"))
1370 (org-babel-execute-src-block))
1371 (should-not (re-search-forward "^#\\+RESULTS:" nil t
))
1373 (should (re-search-forward "^: 3" nil t
))))
1375 (ert-deftest test-ob
/specific-colnames
()
1376 "Test passing specific column names."
1378 (equal "#+name: input-table
1384 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1394 (org-test-with-temp-text
1395 "#+name: input-table
1401 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1405 ;; we should find a code block
1406 (should (re-search-forward org-babel-src-block-regexp nil t
))
1407 (goto-char (match-beginning 0))
1408 ;; now that we've located the code block, it may be evaluated
1409 (let ((org-babel-execute-src-block "RESULTS"))
1410 (org-babel-execute-src-block))
1413 (ert-deftest test-ob
/location-of-header-arg-eval
()
1414 "Test location of header argument evaluation."
1415 (org-test-with-temp-text "
1417 #+begin_src emacs-lisp :var pt=(point)
1421 #+name: bottom-block
1422 #+begin_src emacs-lisp :var pt=top-block()
1426 ;; the value of the second block should be greater than the first
1428 (< (progn (re-search-forward org-babel-src-block-regexp nil t
)
1429 (goto-char (match-beginning 0))
1430 (prog1 (save-match-data (org-babel-execute-src-block))
1431 (goto-char (match-end 0))))
1432 (progn (re-search-forward org-babel-src-block-regexp nil t
)
1433 (goto-char (match-beginning 0))
1434 (org-babel-execute-src-block))))))
1436 (ert-deftest test-ob
/preserve-results-indentation
()
1437 "Preserve indentation when executing a source block."
1441 (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1442 (org-babel-execute-src-block)
1443 (let ((case-fold-search t
)) (search-forward "RESULTS"))
1444 (list (org-get-indentation)
1445 (progn (forward-line) (org-get-indentation))))))
1449 (org-test-with-temp-text
1450 " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1451 (org-babel-execute-src-block)
1452 (let ((case-fold-search t
)) (search-forward "RESULTS"))
1453 (list (org-get-indentation)
1454 (progn (forward-line) (org-get-indentation))))))
1455 ;; Don't get fooled by TAB-based indentation.
1459 (org-test-with-temp-text
1460 "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
1462 (org-babel-execute-src-block)
1463 (let ((case-fold-search t
)) (search-forward "RESULTS"))
1464 (list (org-get-indentation)
1465 (progn (forward-line) (org-get-indentation))))))
1466 ;; Properly indent examplified blocks.
1482 (org-test-with-temp-text
1483 " #+begin_src emacs-lisp :results output
1484 (dotimes (i 10) (princ i) (princ \"\\n\"))
1486 (org-babel-execute-src-block)
1487 (search-forward "begin_example")
1489 (buffer-substring-no-properties (line-beginning-position)
1491 ;; Properly indent "org" blocks.
1507 (org-test-with-temp-text
1508 " #+begin_src emacs-lisp :results output org
1509 (dotimes (i 10) (princ i) (princ \"\\n\"))
1511 (org-babel-execute-src-block)
1512 (search-forward "begin_src org")
1514 (buffer-substring-no-properties (line-beginning-position)
1517 (ert-deftest test-ob
/safe-header-args
()
1518 "Detect safe and unsafe header args."
1519 (let ((safe-args '((:cache .
"foo")
1520 (:results .
"output")
1523 (unsafe-args '((:eval .
"yes")
1524 (:results .
"output file")
1526 (malformed-args '((bar .
"foo")
1529 (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args
)))
1530 (dolist (arg safe-args
)
1531 (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args
)))
1532 (dolist (arg unsafe-args
)
1533 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args
))))
1534 (dolist (arg malformed-args
)
1535 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args
))))
1536 (should (not (funcall safe-p
(append safe-args unsafe-args
))))))
1538 (ert-deftest test-ob
/noweb-expansions-in-cache
()
1539 "Ensure that noweb expansions are expanded before caching."
1540 (let ((noweb-expansions-in-cache-var 0))
1541 (org-test-with-temp-text "
1543 #+begin_src emacs-lisp
1548 #+begin_src emacs-lisp :noweb yes :cache yes
1549 (setq noweb-expansions-in-cache-var
1550 (+ 1 noweb-expansions-in-cache-var))
1551 (concat <<foo>> \" check noweb expansions\")
1554 ;; run the second block to create the cache
1555 (goto-char (point-min))
1556 (re-search-forward (regexp-quote "#+name: bar"))
1557 (should (string= "I said check noweb expansions"
1558 (org-babel-execute-src-block)))
1559 (should (= noweb-expansions-in-cache-var
1))
1560 ;; change the value of the first block
1561 (goto-char (point-min))
1562 (re-search-forward (regexp-quote "said"))
1563 (goto-char (match-beginning 0))
1565 (re-search-forward (regexp-quote "#+name: bar"))
1566 (should (string= "I haven't said check noweb expansions"
1567 (org-babel-execute-src-block)))
1568 (should (= noweb-expansions-in-cache-var
2)))))
1570 (ert-deftest test-ob
/file-ext-and-output-dir
()
1571 (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
1572 (org-babel-next-src-block)
1573 (should (equal "file-ext-basic.txt"
1574 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1575 (org-babel-next-src-block)
1576 (should (equal "foo/file-ext-dir-relative.txt"
1577 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1578 (org-babel-next-src-block)
1579 (should (equal "foo/file-ext-dir-relative-slash.txt"
1580 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1581 (org-babel-next-src-block)
1582 (should (equal "/tmp/file-ext-dir-absolute.txt"
1583 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1584 (org-babel-next-src-block)
1585 (should (equal "foo.bar"
1586 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1587 (org-babel-next-src-block)
1588 (should (equal "xxx/foo.bar"
1589 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1592 (ert-deftest test-ob
/script-escape
()
1593 ;; Delimited lists of numbers
1594 (should (equal '(1 2 3)
1595 (org-babel-script-escape "[1 2 3]")))
1596 (should (equal '(1 2 3)
1597 (org-babel-script-escape "{1 2 3}")))
1598 (should (equal '(1 2 3)
1599 (org-babel-script-escape "(1 2 3)")))
1600 ;; Delimited lists of double-quoted strings
1601 (should (equal '("foo" "bar")
1602 (org-babel-script-escape "(\"foo\" \"bar\")")))
1603 (should (equal '("foo" "bar")
1604 (org-babel-script-escape "[\"foo\" \"bar\"]")))
1605 (should (equal '("foo" "bar")
1606 (org-babel-script-escape "{\"foo\" \"bar\"}")))
1608 (should (equal '("foo" "bar")
1609 (org-babel-script-escape "(\"foo\", \"bar\")")))
1610 (should (equal '("foo" "bar")
1611 (org-babel-script-escape "[\"foo\", \"bar\"]")))
1612 (should (equal '("foo" "bar")
1613 (org-babel-script-escape "{\"foo\", \"bar\"}")))
1614 ;; Delimited lists of single-quoted strings
1615 (should (equal '("foo" "bar")
1616 (org-babel-script-escape "('foo' 'bar')")))
1617 (should (equal '("foo" "bar")
1618 (org-babel-script-escape "['foo' 'bar']")))
1619 (should (equal '("foo" "bar")
1620 (org-babel-script-escape "{'foo' 'bar'}")))
1622 (should (equal '("foo" "bar")
1623 (org-babel-script-escape "('foo', 'bar')")))
1624 (should (equal '("foo" "bar")
1625 (org-babel-script-escape "['foo', 'bar']")))
1626 (should (equal '("foo" "bar")
1627 (org-babel-script-escape "{'foo', 'bar'}")))
1628 ;; Single quoted strings
1629 (should (equal "foo"
1630 (org-babel-script-escape "'foo'")))
1631 ;; ... with internal double quote
1632 (should (equal "foo\"bar"
1633 (org-babel-script-escape "'foo\"bar'")))
1634 ;; ... with internal backslash
1635 (should (equal "foo\\bar"
1636 (org-babel-script-escape "'foo\\bar'")))
1637 ;; ... with internal escaped backslash
1638 (should (equal "foo\\bar"
1639 (org-babel-script-escape "'foo\\\\bar'")))
1640 ;; ... with internal backslash-double quote
1641 (should (equal "foo\\\"bar"
1642 (org-babel-script-escape "'foo\\\"bar'")))
1643 ;; ... with internal escaped backslash-double quote
1644 (should (equal "foo\\\"bar"
1645 (org-babel-script-escape "'foo\\\\\"bar'")))
1646 ;; ... with internal escaped single quote
1647 (should (equal "foo'bar"
1648 (org-babel-script-escape "'foo\\'bar'")))
1649 ;; ... with internal escaped backslash-escaped single quote
1650 (should (equal "foo\\'bar"
1651 (org-babel-script-escape "'foo\\\\\\'bar'")))
1652 ;; Double quoted strings
1653 (should (equal "foo"
1654 (org-babel-script-escape "\"foo\"")))
1655 ;; ... with internal single quote
1656 (should (equal "foo'bar"
1657 (org-babel-script-escape "\"foo'bar\"")))
1658 ;; ... with internal backslash
1659 (should (equal "foo\\bar"
1660 (org-babel-script-escape "\"foo\\bar\"")))
1661 ;; ... with internal escaped backslash
1662 (should (equal "foo\\bar"
1663 (org-babel-script-escape "\"foo\\\\bar\"")))
1664 ;; ... with internal backslash-single quote
1665 (should (equal "foo\\'bar"
1666 (org-babel-script-escape "\"foo\\'bar\"")))
1667 ;; ... with internal escaped backslash-single quote
1668 (should (equal "foo\\'bar"
1669 (org-babel-script-escape "\"foo\\\\'bar\"")))
1670 ;; ... with internal escaped double quote
1671 (should (equal "foo\"bar"
1672 (org-babel-script-escape "\"foo\\\"bar\"")))
1673 ;; ... with internal escaped backslash-escaped double quote
1674 (should (equal "foo\\\"bar"
1675 (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
1677 (ert-deftest test-ob
/process-params-no-duplicates
()
1679 (equal (org-babel-process-params '((:colname-names
)
1683 (:var .
"\"foo\"")))
1688 (:result-type . value
)))))
1690 (defun org-test-babel-confirm-evaluate (eval-value)
1691 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
1693 #+end_src" eval-value
)
1694 (goto-char (point-min))
1695 (let ((info (org-babel-get-src-block-info)))
1696 (org-babel-check-confirm-evaluate info
))))
1698 (ert-deftest test-ob
/check-eval
()
1699 (let ((org-confirm-babel-evaluate t
))
1701 (dolist (pair '(("no" . nil
)
1705 (should (eq (org-test-babel-confirm-evaluate (car pair
)) (cdr pair
))))
1707 (let ((org-babel-exp-reference-buffer t
))
1708 (dolist (pair '(("no" . nil
)
1712 ("never-export" . nil
)
1714 ("query-export" . query
)))
1715 (message (car pair
))
1716 (should (eq (org-test-babel-confirm-evaluate (car pair
)) (cdr pair
))))))
1717 (let ((org-confirm-babel-evaluate nil
))
1719 (dolist (pair '(("no" . nil
)
1723 (should (eq (org-test-babel-confirm-evaluate (car pair
)) (cdr pair
))))
1725 (let ((org-babel-exp-reference-buffer t
))
1726 (dolist (pair '(("no" . nil
)
1730 ("never-export" . nil
)
1732 ("query-export" . query
)))
1733 (message (car pair
))
1734 (should (eq (org-test-babel-confirm-evaluate (car pair
)) (cdr pair
)))))))
1736 (defun org-test-ob/update-block-body
()
1737 "Test `org-babel-update-block-body' specifications."
1739 (equal "#+begin_src elisp\n 2\n#+end_src"
1740 (let ((org-edit-src-content-indentation 2))
1741 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1742 (org-babel-update-block-body "2")
1744 ;; Preserve block indentation.
1746 (equal " #+begin_src elisp\n 2\n #+end_src"
1747 (let ((org-edit-src-content-indentation 1))
1748 (org-test-with-temp-text
1749 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1750 (org-babel-update-block-body "2")
1752 ;; Ignore NEW-BODY global indentation.
1754 (equal "#+begin_src elisp\n 2\n#+end_src"
1755 (let ((org-edit-src-content-indentation 2))
1756 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1757 (org-babel-update-block-body " 2")
1759 ;; When indentation should be preserved ignore the two rules above.
1761 (equal " #+begin_src elisp\n2\n #+end_src"
1762 (let ((org-edit-src-content-indentation 1)
1763 (org-src-preserve-indentation t
))
1764 (org-test-with-temp-text
1765 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1766 (org-babel-update-block-body "2")
1769 (equal " #+begin_src elisp -i\n2\n #+end_src"
1770 (let ((org-edit-src-content-indentation 1))
1771 (org-test-with-temp-text
1772 " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
1773 (org-babel-update-block-body "2")
1776 (equal "#+begin_src elisp\n 2\n#+end_src"
1777 (let ((org-edit-src-content-indentation 2)
1778 (org-src-preserve-indentation t
))
1779 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1780 (org-babel-update-block-body " 2")
1783 (equal "#+begin_src elisp -i\n 2\n#+end_src"
1784 (let ((org-edit-src-content-indentation 2)
1785 (org-src-preserve-indentation t
))
1786 (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
1787 (org-babel-update-block-body " 2")
1788 (buffer-string))))))
1790 (ert-deftest test-ob
/find-named-result
()
1791 "Test `org-babel-find-named-result' specifications."
1794 (org-test-with-temp-text "#+results: foo\n: result"
1795 (org-babel-find-named-result "foo"))))
1797 (org-test-with-temp-text "#+results: foo\n: result"
1798 (org-babel-find-named-result "bar")))
1800 (org-test-with-temp-text "#+results: foobar\n: result"
1801 (org-babel-find-named-result "foo")))
1802 ;; Search is case insensitive.
1804 (org-test-with-temp-text "#+RESULTS: FOO\n: result"
1805 (org-babel-find-named-result "foo")))
1806 ;; Handle hash in results keyword.
1808 (org-test-with-temp-text "#+results[hash]: FOO\n: result"
1809 (org-babel-find-named-result "foo")))
1810 ;; Accept orphaned affiliated keywords.
1812 (org-test-with-temp-text "#+results: foo"
1813 (org-babel-find-named-result "foo"))))
1815 (ert-deftest test-ob
/where-is-src-block-result
()
1816 "Test `org-babel-where-is-src-block-result' specifications."
1817 ;; Find anonymous results.
1820 (org-test-with-temp-text
1821 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
1822 (goto-char (org-babel-where-is-src-block-result))
1823 (buffer-substring-no-properties (point) (line-end-position)))))
1824 ;; Find named results. Those have priority over anonymous ones.
1826 (equal "#+RESULTS: example"
1827 (org-test-with-temp-text
1829 <point>#+NAME: example
1830 #+BEGIN_SRC emacs-lisp
1836 (goto-char (org-babel-where-is-src-block-result))
1837 (buffer-substring-no-properties (point) (line-end-position)))))
1839 (equal "#+RESULTS: example"
1840 (org-test-with-temp-text
1842 <point>#+NAME: example
1843 #+BEGIN_SRC emacs-lisp
1852 (goto-char (org-babel-where-is-src-block-result))
1853 (buffer-substring-no-properties (point) (line-end-position)))))
1854 ;; Return nil when no result is found.
1856 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1857 (org-babel-where-is-src-block-result)))
1859 (org-test-with-temp-text
1860 "- item\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n"
1861 (org-babel-where-is-src-block-result)))
1862 ;; When optional argument INSERT is non-nil, add RESULTS keyword
1863 ;; whenever no RESULTS can be found.
1867 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1868 (let ((org-babel-results-keyword "RESULTS"))
1869 (goto-char (org-babel-where-is-src-block-result t
)))
1870 (buffer-substring-no-properties (point) (line-end-position)))))
1871 ;; Insert a named RESULTS keyword if possible.
1875 (org-test-with-temp-text
1876 "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1877 (let ((org-babel-results-keyword "RESULTS"))
1878 (goto-char (org-babel-where-is-src-block-result t
)))
1879 (buffer-substring-no-properties (point) (line-end-position)))))
1880 ;; When optional argument HASH is provided, clear RESULTS keyword
1881 ;; and related contents if they do not match it.
1885 (org-test-with-temp-text
1886 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 3"
1887 (let ((org-babel-results-keyword "RESULTS"))
1888 (goto-char (org-babel-where-is-src-block-result nil nil
"bbbb")))
1889 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1893 (org-test-with-temp-text
1894 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
1895 (let ((org-babel-results-keyword "RESULTS"))
1896 (goto-char (org-babel-where-is-src-block-result nil nil
"bbbb")))
1897 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1898 ;; Handle hashes with times.
1901 "#+RESULTS[<2014-03-04 00:41:10> bbbb]:"
1902 (org-test-with-temp-text
1904 <point>#+BEGIN_SRC emacs-lisp
1908 #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1909 (let ((org-babel-results-keyword "RESULTS")
1910 (org-babel-hash-show-time t
))
1911 (cl-letf (((symbol-function 'format-time-string
)
1912 (lambda (&rest _
) "<2014-03-04 00:41:10>")))
1913 (goto-char (org-babel-where-is-src-block-result nil nil
"bbbb"))
1914 (org-trim (buffer-substring-no-properties (point) (point-max))))))))
1917 "#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1918 (org-test-with-temp-text
1920 <point>#+BEGIN_SRC emacs-lisp
1924 #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1925 (let ((org-babel-results-keyword "RESULTS")
1926 (org-babel-hash-show-time t
))
1927 (cl-letf (((symbol-function 'format-time-string
)
1928 (lambda (&rest _
) "<2014-03-04 00:41:10>")))
1929 (goto-char (org-babel-where-is-src-block-result nil nil
"aaaa"))
1930 (org-trim (buffer-substring-no-properties (point) (point-max))))))))
1931 ;; RESULTS keyword may not be the last affiliated keyword.
1935 (org-test-with-temp-text
1937 <point>#+BEGIN_SRC emacs-lisp
1944 (let ((org-babel-results-keyword "RESULTS"))
1945 (goto-char (org-babel-where-is-src-block-result nil nil
"bbbb")))
1946 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1947 ;; HASH does nothing if no RESULTS can be found. However, if INSERT
1948 ;; is also non-nil, RESULTS keyword is inserted along with the
1952 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1953 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1954 (org-babel-where-is-src-block-result nil nil
"bbbb")
1959 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1960 (let ((org-babel-results-keyword "RESULTS"))
1961 (goto-char (org-babel-where-is-src-block-result t nil
"bbbb")))
1962 (org-trim (buffer-substring-no-properties (point) (point-max)))))))
1964 (ert-deftest test-ob
/goto-named-src-block
()
1965 "Test interactive use of `org-babel-goto-named-src-block'."
1966 (org-test-with-temp-text-in-file
1969 #+BEGIN_SRC emacs-lisp :results value
1972 #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
1973 #+BEGIN_SRC emacs-lisp
1980 ;; non-existent name
1982 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
1984 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
1985 (should (= 14 (point)))
1986 ;; call line - autocompletion
1988 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1989 (should (= 14 (point)))
1990 ;; noweb reference - autocompletion
1992 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1993 (should (= 14 (point)))
1994 ;; at symbol - autocompletion
1996 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1997 (should (= 14 (point)))
1998 ;; in results - autocompletion
2000 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
2001 (should (= 14 (point)))
2003 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
2004 (should (= 14 (point)))))
2006 (ert-deftest test-ob
/evaluate-body-with-coderefs
()
2009 (org-test-with-temp-text
2010 "#+begin_src emacs-lisp -l \"#(ref:%s)\"\n2 #(ref:foo)\n#+end_src"
2011 (org-babel-execute-src-block))))
2014 (org-test-with-temp-text
2015 "#+begin_src emacs-lisp\n3 #(ref:foo)\n#+end_src"
2016 (let ((org-coderef-label-format "#(ref:%s)"))
2017 (org-babel-execute-src-block))))))
2021 ;;; test-ob ends here