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 (ert-deftest test-ob
/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."
27 (let ((default-directory temporary-file-directory
))
28 (org-test-with-temp-text
31 #+<point>BEGIN_SRC emacs-lisp :file test.txt :cache yes
34 ;; Execute twice as the first time creates the cache.
35 (org-babel-execute-src-block)
36 (string= (expand-file-name "test.txt")
37 (org-babel-execute-src-block))))))
39 (ert-deftest test-ob
/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?
44 org-babel-multi-line-header-regexp
45 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
48 "blah1 blah2 blah3 \t"
51 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
53 ;;TODO Check - should this fail?
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-ob
/src-block-regexp
()
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"
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
)))
78 (should (org-test-string-exact-match
79 org-babel-src-block-regexp
80 (replace-regexp-in-string flags
"" test-block
)))
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-ob
/default-inline-header-args
()
93 (:results .
"replace")
94 (:exports .
"results")
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
102 (baz .
((foo bar
) (baz)))
103 (qux .
((foo bar baz qux
)))
104 (quux .
((foo bar
))))
108 (dolist (pair '((foo .
: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-ob
/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-ob
/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
)
128 (ert-deftest test-ob
/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 (assq :tangle
(nth 2 info
))))))))
133 (ert-deftest test-ob
/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
139 :header-args: :var prop = (* 7 6)
141 #+begin_src emacs-lisp
144 (goto-char (point-min))
145 (org-babel-next-src-block)
146 (should (= 42 (org-babel-execute-src-block)))))
148 (ert-deftest test-ob
/simple-named-code-block
()
149 "Test that simple named code blocks can be evaluated."
150 (org-test-with-temp-text-in-file "
152 #+name: i-have-a-name
153 #+begin_src emacs-lisp
156 (org-babel-next-src-block 1)
157 (should (= 42 (org-babel-execute-src-block)))))
159 (ert-deftest test-ob
/simple-variable-resolution
()
160 "Test that simple variable resolution is working."
161 (org-test-with-temp-text-in-file "
164 #+begin_src emacs-lisp
168 #+begin_src emacs-lisp :var four=four
172 (org-babel-next-src-block 2)
173 (should (= 4 (org-babel-execute-src-block)))
175 (should (string= ": 4" (buffer-substring
179 (ert-deftest test-ob
/multi-line-header-arguments
()
180 "Test that multi-line header arguments and can be read."
181 (org-test-with-temp-text-in-file "
183 #+headers: :var letters='(a b c d e f g)
184 #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
186 (cl-map 'list #'list numbers letters)
188 (org-babel-next-src-block)
189 (let ((results (org-babel-execute-src-block)))
190 (should (eq 'a
(cadr (assoc 1 results
))))
191 (should (eq 'd
(cadr (assoc 4 results
)))))))
193 (ert-deftest test-ob
/parse-header-args
()
194 (org-test-with-temp-text-in-file "
196 #+begin_src example-lang :session :results output :var num=9
200 (org-babel-next-src-block)
201 (let* ((info (org-babel-get-src-block-info))
202 (params (nth 2 info
)))
203 (message "%S" params
)
204 (should (equal "example-lang" (nth 0 info
)))
205 (should (string= "the body" (org-trim (nth 1 info
))))
206 (should-not (member '(:session\ \ \ \
) params
))
207 (should (equal '(:session
) (assq :session params
)))
208 (should (equal '(:result-type . output
) (assq :result-type params
)))
209 (should (equal '(num .
9) (cdr (assq :var params
)))))))
211 (ert-deftest test-ob
/parse-header-args2
()
212 (org-test-with-temp-text-in-file "
214 * resolving sub-trees as references
216 #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
220 #+begin_src org :noweb yes
222 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
225 ** simple subtree with custom ID
227 :CUSTOM_ID: simple-subtree
231 (should (string-match (regexp-quote "this is simple")
232 (org-babel-ref-resolve "simple-subtree")))
233 (org-babel-next-src-block)
234 (should (= 14 (org-babel-execute-src-block)))))
236 (ert-deftest test-ob
/inline-src-blocks
()
239 (org-test-with-temp-text
240 "In the middle <point>src_emacs-lisp{(+ 0 1)} of a line"
241 (org-babel-execute-src-block))))
244 (org-test-with-temp-text
245 "One at the end of a line: <point>src_emacs-lisp{(+ 1 1)}"
246 (org-babel-execute-src-block))))
249 (org-test-with-temp-text
250 "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
251 (org-babel-execute-src-block))))
254 (org-test-with-temp-text
255 "In the middle <point>src_emacs-lisp[:results silent\
256 :exports code]{(+ 3 1)} of a line"
257 (org-babel-execute-src-block))))
260 (org-test-with-temp-text
261 "One at the end of a line: <point>src_emacs-lisp[:results silent\
262 :exports code]{(+ 4 1)}"
263 (org-babel-execute-src-block))))
266 (org-test-with-temp-text
267 "src_emacs-lisp[:results silent :exports code]{(+ 5 1)}\
268 at the beginning of a line."
269 (org-babel-execute-src-block))))
272 (org-test-with-temp-text
273 "One also evaluated: <point>src_emacs-lisp[:exports both\
274 :results silent]{(+ 6 1)}"
275 (org-babel-execute-src-block)))))
277 (ert-deftest test-ob
/inline-src_blk-default-results-replace-line-1
()
278 (let ((test-line "src_sh{echo 1}")
279 (org-babel-inline-result-wrap "=%s="))
280 ;; src_ at bol line 1...
281 (org-test-with-temp-text
283 (goto-char (point-min)) (org-babel-execute-maybe)
285 (concat test-line
" {{{results(=1=)}}}")
286 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
287 (forward-char) (org-babel-execute-maybe)
289 (concat test-line
" {{{results(=1=)}}}")
290 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
291 (re-search-forward "{{{")
292 ;;(should-error (org-ctrl-c-ctrl-c))
293 (backward-char 4) ;; last char of block body
294 (org-babel-execute-maybe)
296 (concat test-line
" {{{results(=1=)}}}")
297 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
298 ;; src_ follows space line 1...
299 (let ((test-line " src_emacs-lisp{ 1 }"))
300 (org-test-with-temp-text
302 (should-error (org-ctrl-c-ctrl-c))
303 (forward-char) (org-babel-execute-maybe)
305 (concat test-line
" {{{results(=1=)}}}")
306 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
307 (re-search-forward "{ 1 ") (org-babel-execute-maybe)
309 (concat test-line
" {{{results(=1=)}}}")
310 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
312 (should-error (org-ctrl-c-ctrl-c))))
313 ;; Results on a subsequent line are replaced.
316 "src_emacs-lisp{(+ 1 2)}\n {{{results(=3=)}}}"
317 (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n {{{results(=2=)}}}"
318 (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
320 ;; Also handle results at the beginning of a line.
323 "src_emacs-lisp{(+ 1 2)}\n{{{results(=3=)}}}"
324 (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n{{{results(=2=)}}}"
325 (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
328 (ert-deftest test-ob
/inline-src_blk-default-results-replace-line-2
()
329 ;; src_ at bol line 2...
330 (let ((test-line " src_emacs-lisp{ \"x\" }")
331 (org-babel-inline-result-wrap "=%s="))
332 (org-test-with-temp-text
333 (concat "\n" test-line
)
334 (should-error (org-ctrl-c-ctrl-c))
335 (goto-char (point-min))
336 (should-error (org-ctrl-c-ctrl-c))
338 (should-error (org-ctrl-c-ctrl-c))
339 (forward-char) (org-babel-execute-maybe)
341 (concat test-line
" {{{results(=x=)}}}")
342 (buffer-substring-no-properties
343 (point-at-bol) (point-at-eol))))))
344 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
345 (org-babel-inline-result-wrap "=%s="))
346 (org-test-with-temp-text
348 (goto-char (point-max))
349 (insert (concat "\n" test-line
" end"))
350 (re-search-backward "src") (org-babel-execute-maybe)
352 (concat test-line
" {{{results(=y=)}}} end")
353 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
354 (re-search-forward "\" ") (org-babel-execute-maybe)
356 (concat test-line
" {{{results(=y=)}}} end")
357 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
359 (should-error (org-ctrl-c-ctrl-c)))))
361 (ert-deftest test-ob
/inline-src_blk-manual-results-replace
()
362 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
363 (org-babel-inline-result-wrap "=%s="))
364 (org-test-with-temp-text
365 (concat "\n" test-line
)
366 (should-error (org-ctrl-c-ctrl-c))
367 (goto-char (point-max))
368 (org-babel-execute-maybe)
370 (should-error (org-ctrl-c-ctrl-c))
371 (forward-char) (org-babel-execute-maybe)
373 (concat test-line
" {{{results(=x=)}}}")
374 (buffer-substring-no-properties
375 (point-at-bol) (point-at-eol))))))
376 (let ((test-line (concat " Some text prior to block "
377 "src_emacs-lisp[:results replace]{ \"y\" }"))
378 (org-babel-inline-result-wrap "=%s="))
379 (org-test-with-temp-text test-line
380 (goto-char (point-max))
381 (insert (concat "\n" test-line
" end"))
382 (re-search-backward "src") (org-babel-execute-maybe)
384 (concat test-line
" {{{results(=y=)}}} end")
385 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
386 (re-search-forward "\" ") (org-babel-execute-maybe)
388 (concat test-line
" {{{results(=y=)}}} end")
389 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
391 (should-error (org-ctrl-c-ctrl-c)))))
393 (ert-deftest test-ob
/inline-src_blk-results-silent
()
394 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
395 (org-test-with-temp-text test-line
396 (org-babel-execute-maybe)
397 (should (string= test-line
398 (buffer-substring-no-properties
399 (point-at-bol) (point-at-eol))))))
400 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
401 "[ :results silent ]{ \"y\" }")))
402 (org-test-with-temp-text
404 (goto-char (point-max))
405 (insert (concat "\n" test-line
" end"))
406 (re-search-backward "src_") (org-babel-execute-maybe)
407 (should (string= (concat test-line
" end")
408 (buffer-substring-no-properties
409 (point-at-bol) (point-at-eol))))
410 (re-search-forward "\" ") (org-babel-execute-maybe)
411 (should (string= (concat test-line
" end")
412 (buffer-substring-no-properties
413 (point-at-bol) (point-at-eol))))
415 (should-error (org-ctrl-c-ctrl-c)))))
417 (ert-deftest test-ob
/inline-src_blk-results-raw
()
418 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
419 (org-test-with-temp-text test-line
420 (org-babel-execute-maybe)
421 (should (string= (concat test-line
" x")
423 (let ((test-line (concat " Some text prior to block "
424 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
425 (org-test-with-temp-text (concat test-line
" end")
426 (re-search-forward "src_") (org-babel-execute-maybe)
427 (should (string= (concat test-line
" the end")
428 (buffer-substring-no-properties
429 (point-at-bol) (point-at-eol))))
430 (re-search-forward "\" ") (org-babel-execute-maybe)
431 (should (string= (concat test-line
" the the end")
432 (buffer-substring-no-properties
433 (point-at-bol) (point-at-eol))))
435 (should-error (org-ctrl-c-ctrl-c)))))
437 (ert-deftest test-ob
/inline-src_blk-results-file
()
438 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
439 (org-test-with-temp-text
441 (org-babel-execute-maybe)
442 (should (string= (concat test-line
" {{{results([[file:~/test-file]])}}}")
443 (buffer-substring-no-properties
444 (point-min) (point-max)))))))
446 (ert-deftest test-ob
/inline-src_blk-results-scalar
()
447 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
448 (org-babel-inline-result-wrap "=%s="))
449 (org-test-with-temp-text
451 (org-babel-execute-maybe)
452 (should (string= (concat test-line
" {{{results(=\"x\"=)}}}")
453 (buffer-substring-no-properties
454 (point-min) (point-max)))))))
456 (ert-deftest test-ob
/inline-src_blk-results-verbatim
()
457 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
458 (org-babel-inline-result-wrap "=%s="))
459 (org-test-with-temp-text
461 (org-babel-execute-maybe)
462 (should (string= (concat test-line
" {{{results(=\"x\"=)}}}")
463 (buffer-substring-no-properties
464 (point-min) (point-max)))))))
466 (ert-deftest test-ob
/combining-scalar-and-raw-result-types
()
467 (org-test-with-temp-text-in-file "
469 #+begin_src sh :results scalar
470 echo \"[[file:./cv.cls]]\"
476 #+begin_src sh :results raw scalar
477 echo \"[[file:./cv.cls]]\"
480 (cl-flet ((next-result ()
481 (org-babel-next-src-block)
482 (org-babel-execute-src-block)
483 (goto-char (org-babel-where-is-src-block-result))
485 (goto-char (point-min))
487 (should (eq (org-element-type (org-element-at-point)) 'fixed-width
))
490 (eq (org-element-type (org-element-at-point)) 'fixed-width
)))))
492 (ert-deftest test-ob
/no-defaut-value-for-var
()
493 "Test that the absence of a default value for a variable DOES THROW
495 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
496 (org-babel-next-src-block)
498 (should-error (org-babel-execute-src-block) :type
'error
)))
502 "Variable \"x\" must be assigned a default value")
505 (ert-deftest test-ob
/just-one-results-block
()
506 "Test that evaluating two times the same code block does not result in a
507 duplicate results block."
508 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
509 (org-babel-execute-src-block)
510 (org-babel-execute-src-block) ; second code block execution
511 (should (search-forward "Hello")) ; the string inside the source code block
512 (should (search-forward "Hello")) ; the same string in the results block
513 (should-error (search-forward "Hello"))))
515 (ert-deftest test-ob
/nested-code-block
()
516 "Test nested code blocks inside code blocks don't cause problems."
518 (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
519 (org-test-with-temp-text "#+begin_src org :results silent
520 ,#+begin_src emacs-lisp
524 (let ((org-edit-src-content-indentation 2)
525 (org-src-preserve-indentation nil
))
526 (org-babel-execute-src-block))))))
528 (ert-deftest test-ob
/partial-nested-code-block
()
529 "Test nested code blocks inside code blocks don't cause problems."
530 (org-test-with-temp-text "#+begin_src org :results silent
531 ,#+begin_src emacs-lisp
533 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
535 (ert-deftest test-ob
/does-not-replace-a-block-with-the-results
()
536 (org-test-with-temp-text "#+NAME: foo
537 #+BEGIN_SRC emacs-lisp
540 (org-babel-next-src-block 1)
541 (should (eq 'foo
(org-babel-execute-src-block)))
542 (goto-char (point-min))
543 (org-babel-next-src-block 1)
544 (should (looking-at org-babel-src-block-regexp
))))
546 (ert-deftest test-ob
/catches-all-references
()
547 (org-test-with-temp-text "
548 #+NAME: literal-example
554 #+NAME: read-literal-example
555 #+BEGIN_SRC emacs-lisp :var x=literal-example
556 (concatenate 'string x \" for me.\")
558 (org-babel-next-src-block 1)
559 (should (string= (org-babel-execute-src-block)
560 "A literal example\non two lines\n for me."))))
562 (ert-deftest test-ob
/ignore-reference-in-commented-headings
()
565 (org-test-with-temp-text
577 <point>#+BEGIN_SRC emacs-lisp :var x=n
580 (org-babel-execute-src-block)))))
582 (ert-deftest test-ob
/do-not-resolve-to-partial-names-data
()
583 (org-test-with-temp-text "
592 #+begin_src emacs-lisp :var x=base
595 (org-babel-next-src-block 1)
596 (should (equal (org-babel-execute-src-block) '((3) (4))))))
598 (ert-deftest test-ob
/do-not-resolve-to-partial-names-code
()
599 (org-test-with-temp-text "
601 #+begin_src emacs-lisp
606 #+begin_src emacs-lisp
610 #+begin_src emacs-lisp :var x=base
613 (org-babel-next-src-block 3)
614 (should (equal (org-babel-execute-src-block) "foo"))))
616 (ert-deftest test-ob
/allow-spaces-around-
=-in-var-specs
()
617 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
621 (should (= 10 (org-babel-execute-src-block)))))
623 (ert-deftest test-ob
/org-babel-update-intermediate
()
624 (org-test-with-temp-text "#+name: foo
625 #+begin_src emacs-lisp
632 #+begin_src emacs-lisp :var it=foo
635 (let ((org-babel-update-intermediate nil
))
636 (goto-char (point-min))
637 (org-babel-next-src-block 2)
638 (should (= 3 (org-babel-execute-src-block)))
639 (goto-char (point-min))
641 (should (looking-at ": 4")))
642 (let ((org-babel-update-intermediate t
))
643 (goto-char (point-min))
644 (org-babel-next-src-block 2)
645 (should (= 3 (org-babel-execute-src-block)))
646 (goto-char (point-min))
648 (should (looking-at ": 2")))))
650 (ert-deftest test-ob
/eval-header-argument
()
651 (cl-flet ((check-eval (eval runp
)
652 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
655 (let ((foo :not-run
))
657 (progn (should (org-babel-execute-src-block))
658 (should (eq foo
:evald
)))
659 (progn (should-not (org-babel-execute-src-block))
660 (should-not (eq foo
:evald
))))))))
661 (check-eval "never" nil
)
662 (check-eval "no" nil
)
663 (check-eval "never-export" t
)
664 (check-eval "no-export" t
)
665 (let ((org-babel-exp-reference-buffer (current-buffer)))
666 (check-eval "never" nil
)
667 (check-eval "no" nil
)
668 (check-eval "never-export" nil
)
669 (check-eval "no-export" nil
))))
671 (ert-deftest test-ob
/noweb-expansion
()
676 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
684 (org-babel-expand-noweb-references))))
685 ;; Handle :noweb-sep.
689 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
693 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
697 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
700 (org-babel-expand-noweb-references))))
701 ;; :noweb-ref is extracted from definition, not point of call.
705 (org-test-with-temp-text
709 :header-args: :noweb-ref bar
712 <point>#+begin_src emacs-lisp :results output :tangle yes
718 :header-args: :noweb-ref foo
721 #+begin_src sh :noweb-sep \"\"
724 (org-babel-expand-noweb-references))))
725 ;; Handle recursive expansion.
728 (org-test-with-temp-text "
729 #+begin_src emacs-lisp :noweb yes<point>
734 #+begin_src emacs-lisp :noweb yes
739 #+begin_src emacs-lisp
742 (org-babel-expand-noweb-references))))
743 ;; During recursive expansion, obey to `:noweb' property.
746 (org-test-with-temp-text "
747 #+begin_src emacs-lisp :noweb yes<point>
752 #+begin_src emacs-lisp :noweb no
757 #+begin_src emacs-lisp
760 (org-babel-expand-noweb-references)))))
762 (ert-deftest test-ob
/splitting-variable-lists-in-references
()
763 (org-test-with-temp-text ""
764 (should (= 1 (length (org-babel-ref-split-args
765 "a=\"this, no work\""))))
766 (should (= 2 (length (org-babel-ref-split-args
767 "a=\"this, no work\", b=1"))))))
769 (ert-deftest test-ob
/balanced-split
()
770 "Test `org-babel-balanced-split' specifications."
772 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
773 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
775 ;; Handle un-balanced parens.
777 (equal '(":foo ((6)" "bar 1")
778 (org-babel-balanced-split ":foo ((6) :bar 1" '((32 9) .
58))))
780 (equal '(":foo \"(foo\"" "bar 2")
781 (org-babel-balanced-split ":foo \"(foo\" :bar 2" '((32 9) .
58))))
782 ;; Handle un-balanced quotes.
784 (equal '(":foo \"1" "bar 3")
785 (org-babel-balanced-split ":foo \"1 :bar 3" '((32 9) .
58))))
786 ;; Handle empty string.
788 (equal '(":foo \"\"")
789 (org-babel-balanced-split ":foo \"\"" '((32 9) .
58))))
790 ;; Handle control characters within double quotes.
792 (equal '(":foo \"\\n\"")
793 (org-babel-balanced-split ":foo \"\\n\"" '((32 9) .
58)))))
795 (ert-deftest test-ob
/commented-last-block-line-no-var
()
796 (org-test-with-temp-text-in-file "
797 #+begin_src emacs-lisp
800 (org-babel-next-src-block)
801 (org-babel-execute-maybe)
802 (should (re-search-forward "\\#\\+results:" nil t
))
807 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
808 (org-test-with-temp-text-in-file "
809 #+begin_src emacs-lisp
812 (org-babel-next-src-block)
813 (org-babel-execute-maybe)
814 (should (re-search-forward "\\#\\+results:" nil t
))
819 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
821 (ert-deftest test-ob
/commented-last-block-line-with-var
()
822 (org-test-with-temp-text-in-file "
823 #+begin_src emacs-lisp :var a=1
826 (org-babel-next-src-block)
827 (org-babel-execute-maybe)
828 (re-search-forward "\\#\\+results:" nil t
)
832 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
833 (org-test-with-temp-text-in-file "
834 #+begin_src emacs-lisp :var a=2
837 (org-babel-next-src-block)
838 (org-babel-execute-maybe)
839 (re-search-forward "\\#\\+results:" nil t
)
843 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
845 (ert-deftest test-ob
/org-babel-insert-result
()
846 "Test `org-babel-insert-result' specifications."
847 ;; Do not error when output is an improper list.
849 (org-test-with-temp-text
851 <point>#+BEGIN_SRC emacs-lisp
855 (org-babel-execute-maybe) t
))
856 ;; Escape headlines when producing an example block.
859 ",\\* Not an headline"
860 (org-test-with-temp-text
862 <point>#+BEGIN_SRC emacs-lisp
863 \"* Not an headline\"
866 (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
868 ;; Escape special syntax in example blocks.
872 (org-test-with-temp-text
874 <point>#+BEGIN_SRC emacs-lisp
878 (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
880 ;; No escaping is done with other blocks or raw type.
883 ",\\* Not an headline"
884 (org-test-with-temp-text
886 <point>#+BEGIN_SRC emacs-lisp
887 \"* Not an headline\"
890 (let ((org-babel-min-lines-for-block-output 10))
891 (org-babel-execute-maybe))
895 ",\\* Not an headline"
896 (org-test-with-temp-text
898 <point>#+BEGIN_SRC emacs-lisp :results raw
899 \"* Not an headline\"
902 (org-babel-execute-maybe)
906 ",\\* Not an headline"
907 (org-test-with-temp-text
909 <point>#+BEGIN_SRC emacs-lisp :results drawer
910 \"* Not an headline\"
913 (org-babel-execute-maybe)
916 (ert-deftest test-ob
/remove-inline-result
()
917 "Test `org-babel-remove-inline-result' honors whitespace."
919 ((inline-sb "src_emacs-lisp{(+ 1 2)}")
920 (inline-res " {{{results(=3=)}}}")
921 (inline-sb-dot (concat inline-sb
"."))
922 (inline-sb-res-dot (concat inline-sb inline-res
".")))
923 (org-test-with-temp-text
924 ;; Insert inline_src_block followed by dot.
926 ;; Insert result before dot.
927 (org-babel-execute-maybe)
928 (should (string= inline-sb-res-dot
929 (buffer-substring-no-properties
930 (point-at-bol) (point-at-eol))))
931 ;; Delete whitespace and result.
932 (org-babel-remove-inline-result)
933 (should (string= inline-sb-dot
934 (buffer-substring-no-properties
935 (point-at-bol) (point-at-eol))))
936 ;; Add whitespace and result before dot.
937 (search-forward inline-sb
)
938 (insert " " inline-res
)
939 (goto-char (point-at-bol))
940 ;; Remove whitespace and result.
941 (org-babel-remove-inline-result)
942 (should (string= inline-sb-dot
943 (buffer-substring-no-properties
944 (point-at-bol) (point-at-eol))))
945 ;; Add whitespace before dot.
946 (search-forward inline-sb
)
948 (goto-char (point-at-bol))
949 ;; Add result before whitespace.
950 (org-babel-execute-maybe)
951 ;; Remove result - leave trailing whitespace and dot.
952 (org-babel-remove-inline-result)
953 (should (string= (concat inline-sb
" .")
954 (buffer-substring-no-properties
955 (point-at-bol) (point-at-eol)))))))
957 (ert-deftest test-ob
/org-babel-remove-result--results-default
()
958 "Test `org-babel-remove-result' with default :results."
959 (mapcar (lambda (language)
960 (test-ob-verify-result-and-removed-result
963 "* org-babel-remove-result
964 #+begin_src " language
"
968 '("sh" "emacs-lisp")))
970 (ert-deftest test-ob
/org-babel-results-indented-wrap
()
971 "Ensure that wrapped results are inserted correction when indented.
972 If not inserted correctly then the second evaluation will fail
973 trying to find the :END: marker."
974 (org-test-with-temp-text
976 #+begin_src sh :results file wrap
979 (org-babel-next-src-block 1)
980 (org-babel-execute-src-block)
981 (org-babel-execute-src-block)))
983 (ert-deftest test-ob
/file-desc-header-argument
()
984 "Test that the :file-desc header argument is used."
985 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
989 #+begin_src emacs-lisp :results file :file-desc
992 (org-babel-execute-src-block)
993 (org-babel-next-src-block 1)
994 (org-babel-execute-src-block)
995 (goto-char (point-min))
996 (should (search-forward "[[file:foo][bar]]" nil t
))
997 (should (search-forward "[[file:foo][foo]]" nil t
))))
999 (ert-deftest test-ob
/inline-src_blk-preceded-punct-preceded-by-point
()
1000 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
1001 (org-babel-inline-result-wrap "=%s="))
1002 (org-test-with-temp-text
1005 (org-babel-execute-maybe)
1006 (should (re-search-forward "=\"x\"=" nil t
))
1009 (ert-deftest test-ob
/commented-last-block-line-with-var
()
1010 (org-test-with-temp-text-in-file "
1011 #+begin_src emacs-lisp :var a=1
1014 (org-babel-next-src-block)
1015 (org-babel-execute-maybe)
1016 (re-search-forward "\\#\\+results:" nil t
)
1020 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
1021 (org-test-with-temp-text-in-file "
1022 #+begin_src emacs-lisp :var a=2
1025 (org-babel-next-src-block)
1026 (org-babel-execute-maybe)
1027 (re-search-forward "\\#\\+results:" nil t
)
1031 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
1033 (defun test-ob-verify-result-and-removed-result (result buffer-text
)
1034 "Test helper function to test `org-babel-remove-result'.
1035 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
1036 and the result of execution is verified against RESULT.
1038 The block is actually executed /twice/ to ensure result
1039 replacement happens correctly."
1040 (org-test-with-temp-text
1042 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
1043 (should (re-search-forward "\\#\\+results:" nil t
))
1045 (should (string= result
1046 (buffer-substring-no-properties
1048 (- (point-max) 16))))
1049 (org-babel-previous-src-block) (org-babel-remove-result)
1050 (should (string= buffer-text
1051 (buffer-substring-no-properties
1052 (point-min) (point-max))))))
1054 (ert-deftest test-ob
/org-babel-remove-result--results-list
()
1055 "Test `org-babel-remove-result' with :results list."
1056 (test-ob-verify-result-and-removed-result
1062 "* org-babel-remove-result
1063 #+begin_src emacs-lisp :results list
1069 (ert-deftest test-ob
/org-babel-remove-result--results-wrap
()
1070 "Test `org-babel-remove-result' with :results wrap."
1071 (test-ob-verify-result-and-removed-result
1076 "* org-babel-remove-result
1078 #+begin_src emacs-lisp :results wrap
1084 (ert-deftest test-ob
/org-babel-remove-result--results-org
()
1085 "Test `org-babel-remove-result' with :results org."
1086 (test-ob-verify-result-and-removed-result
1093 "* org-babel-remove-result
1094 #+begin_src emacs-lisp :results org
1102 (ert-deftest test-ob
/org-babel-remove-result--results-html
()
1103 "Test `org-babel-remove-result' with :results html."
1104 (test-ob-verify-result-and-removed-result
1105 "#+begin_export html
1106 <head><body></body></head>
1109 "* org-babel-remove-result
1110 #+begin_src emacs-lisp :results html
1111 \"<head><body></body></head>\"
1116 (ert-deftest test-ob
/org-babel-remove-result--results-latex
()
1117 "Test `org-babel-remove-result' with :results latex."
1118 (test-ob-verify-result-and-removed-result
1119 "#+begin_export latex
1125 "* org-babel-remove-result
1126 #+begin_src emacs-lisp :results latex
1134 (ert-deftest test-ob
/org-babel-remove-result--results-code
()
1135 "Test `org-babel-remove-result' with :results code."
1137 (test-ob-verify-result-and-removed-result
1138 "#+begin_src emacs-lisp
1142 "* org-babel-remove-result
1143 #+begin_src emacs-lisp :results code
1144 (message \"I am working!\")
1149 (ert-deftest test-ob
/org-babel-remove-result--results-pp
()
1150 "Test `org-babel-remove-result' with :results pp."
1151 (test-ob-verify-result-and-removed-result
1152 ": \"I /am/ working!\""
1154 "* org-babel-remove-result
1155 #+begin_src emacs-lisp :results pp
1156 \"I /am/ working!\")
1161 (ert-deftest test-ob
/org-babel-remove-result--no-blank-line
()
1162 "Test `org-babel-remove-result' without blank line between code and results."
1165 #+begin_src emacs-lisp
1171 (org-test-with-temp-text
1173 <point>#+begin_src emacs-lisp
1179 (org-babel-execute-maybe)
1182 (ert-deftest test-ob
/results-do-not-replace-code-blocks
()
1183 (org-test-with-temp-text "Block two has a space after the name.
1186 #+begin_src emacs-lisp
1191 #+begin_src emacs-lisp
1196 #+begin_src emacs-lisp
1203 (dolist (num '(1 2 3))
1204 ;; execute the block
1205 (goto-char (point-min))
1206 (org-babel-next-src-block num
) (org-babel-execute-src-block)
1207 ;; check the results
1208 (goto-char (point-max))
1209 (move-beginning-of-line 0)
1210 (should (looking-at (format ": %d" num
))))))
1212 (ert-deftest test-ob
/blocks-with-spaces
()
1213 "Test expansion of blocks followed by blank lines."
1214 ;; Preserve number of blank lines after block.
1216 (equal "#+BEGIN_SRC emacs-lisp
1222 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1225 (let ((org-babel-next-src-block "RESULTS"))
1226 (org-babel-execute-src-block))
1228 ;; Do not add spurious blank lines after results.
1234 #+begin_src emacs-lisp
1242 (org-test-with-temp-text "
1245 #+begin_src emacs-lisp<point>
1250 (org-babel-execute-src-block)
1257 #+begin_src emacs-lisp
1265 (org-test-with-temp-text "
1268 #+begin_src emacs-lisp<point>
1276 (org-babel-execute-src-block)
1279 (ert-deftest test-ob
/results-in-narrowed-buffer
()
1280 "Test block execution in a narrowed buffer."
1281 ;; If results don't exist, they should be inserted in visible part
1285 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1286 (org-test-with-temp-text
1287 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1288 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1289 (let ((org-babel-results-keyword "RESULTS"))
1290 (org-babel-execute-src-block))
1291 (org-trim (buffer-string)))))
1294 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1295 (org-test-with-temp-text
1296 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1297 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1298 (let ((org-babel-results-keyword "RESULTS"))
1299 (org-babel-execute-src-block))
1300 (org-trim (buffer-string)))))
1301 ;; Results in visible part of buffer, should be updated here.
1305 #+BEGIN_SRC emacs-lisp
1311 (org-test-with-temp-text
1313 #+BEGIN_SRC emacs-lisp
1321 (narrow-to-region (point-min) (point))
1322 (goto-char (point-min))
1323 (let ((org-babel-results-keyword "RESULTS"))
1324 (org-babel-execute-src-block))
1325 (org-trim (buffer-string)))))
1326 ;; Results in invisible part of buffer, should be updated there.
1327 (org-test-with-temp-text
1329 #+BEGIN_SRC emacs-lisp
1337 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1338 (let ((org-babel-results-keyword "RESULTS"))
1339 (org-babel-execute-src-block))
1340 (should-not (re-search-forward "^#\\+RESULTS:" nil t
))
1342 (should (re-search-forward "^: 3" nil t
))))
1344 (ert-deftest test-ob
/specific-colnames
()
1345 "Test passing specific column names."
1347 (equal "#+name: input-table
1353 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1363 (org-test-with-temp-text
1364 "#+name: input-table
1370 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1374 ;; we should find a code block
1375 (should (re-search-forward org-babel-src-block-regexp nil t
))
1376 (goto-char (match-beginning 0))
1377 ;; now that we've located the code block, it may be evaluated
1378 (let ((org-babel-execute-src-block "RESULTS"))
1379 (org-babel-execute-src-block))
1382 (ert-deftest test-ob
/location-of-header-arg-eval
()
1383 "Test location of header argument evaluation."
1384 (org-test-with-temp-text "
1386 #+begin_src emacs-lisp :var pt=(point)
1390 #+name: bottom-block
1391 #+begin_src emacs-lisp :var pt=top-block()
1395 ;; the value of the second block should be greater than the first
1397 (< (progn (re-search-forward org-babel-src-block-regexp nil t
)
1398 (goto-char (match-beginning 0))
1399 (prog1 (save-match-data (org-babel-execute-src-block))
1400 (goto-char (match-end 0))))
1401 (progn (re-search-forward org-babel-src-block-regexp nil t
)
1402 (goto-char (match-beginning 0))
1403 (org-babel-execute-src-block))))))
1405 (ert-deftest test-ob
/preserve-results-indentation
()
1406 "Preserve indentation when executing a src block."
1410 (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1411 (org-babel-execute-src-block)
1412 (let ((case-fold-search t
)) (search-forward "RESULTS"))
1413 (list (org-get-indentation)
1414 (progn (forward-line) (org-get-indentation))))))
1418 (org-test-with-temp-text
1419 " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1420 (org-babel-execute-src-block)
1421 (let ((case-fold-search t
)) (search-forward "RESULTS"))
1422 (list (org-get-indentation)
1423 (progn (forward-line) (org-get-indentation))))))
1424 ;; Don't get fooled by TAB-based indentation.
1428 (org-test-with-temp-text
1429 "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
1431 (org-babel-execute-src-block)
1432 (let ((case-fold-search t
)) (search-forward "RESULTS"))
1433 (list (org-get-indentation)
1434 (progn (forward-line) (org-get-indentation))))))
1435 ;; Properly indent examplified blocks.
1451 (org-test-with-temp-text
1452 " #+begin_src emacs-lisp :results output
1453 (dotimes (i 10) (princ i) (princ \"\\n\"))
1455 (org-babel-execute-src-block)
1456 (search-forward "begin_example")
1458 (buffer-substring-no-properties (line-beginning-position)
1460 ;; Properly indent "org" blocks.
1476 (org-test-with-temp-text
1477 " #+begin_src emacs-lisp :results output org
1478 (dotimes (i 10) (princ i) (princ \"\\n\"))
1480 (org-babel-execute-src-block)
1481 (search-forward "begin_src org")
1483 (buffer-substring-no-properties (line-beginning-position)
1486 (ert-deftest test-ob
/safe-header-args
()
1487 "Detect safe and unsafe header args."
1488 (let ((safe-args '((:cache .
"foo")
1489 (:results .
"output")
1492 (unsafe-args '((:eval .
"yes")
1493 (:results .
"output file")
1495 (malformed-args '((bar .
"foo")
1498 (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args
)))
1499 (dolist (arg safe-args
)
1500 (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args
)))
1501 (dolist (arg unsafe-args
)
1502 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args
))))
1503 (dolist (arg malformed-args
)
1504 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args
))))
1505 (should (not (funcall safe-p
(append safe-args unsafe-args
))))))
1507 (ert-deftest test-ob
/noweb-expansions-in-cache
()
1508 "Ensure that noweb expansions are expanded before caching."
1509 (let ((noweb-expansions-in-cache-var 0))
1510 (org-test-with-temp-text "
1512 #+begin_src emacs-lisp
1517 #+begin_src emacs-lisp :noweb yes :cache yes
1518 (setq noweb-expansions-in-cache-var
1519 (+ 1 noweb-expansions-in-cache-var))
1520 (concat <<foo>> \" check noweb expansions\")
1523 ;; run the second block to create the cache
1524 (goto-char (point-min))
1525 (re-search-forward (regexp-quote "#+name: bar"))
1526 (should (string= "I said check noweb expansions"
1527 (org-babel-execute-src-block)))
1528 (should (= noweb-expansions-in-cache-var
1))
1529 ;; change the value of the first block
1530 (goto-char (point-min))
1531 (re-search-forward (regexp-quote "said"))
1532 (goto-char (match-beginning 0))
1534 (re-search-forward (regexp-quote "#+name: bar"))
1535 (should (string= "I haven't said check noweb expansions"
1536 (org-babel-execute-src-block)))
1537 (should (= noweb-expansions-in-cache-var
2)))))
1539 (ert-deftest test-ob
/file-ext-and-output-dir
()
1540 (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
1541 (org-babel-next-src-block)
1542 (should (equal "file-ext-basic.txt"
1543 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1544 (org-babel-next-src-block)
1545 (should (equal "foo/file-ext-dir-relative.txt"
1546 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1547 (org-babel-next-src-block)
1548 (should (equal "foo/file-ext-dir-relative-slash.txt"
1549 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1550 (org-babel-next-src-block)
1551 (should (equal "/tmp/file-ext-dir-absolute.txt"
1552 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1553 (org-babel-next-src-block)
1554 (should (equal "foo.bar"
1555 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1556 (org-babel-next-src-block)
1557 (should (equal "xxx/foo.bar"
1558 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1561 (ert-deftest test-ob
/script-escape
()
1562 ;; Delimited lists of numbers
1563 (should (equal '(1 2 3)
1564 (org-babel-script-escape "[1 2 3]")))
1565 (should (equal '(1 2 3)
1566 (org-babel-script-escape "{1 2 3}")))
1567 (should (equal '(1 2 3)
1568 (org-babel-script-escape "(1 2 3)")))
1569 ;; Delimited lists of double-quoted strings
1570 (should (equal '("foo" "bar")
1571 (org-babel-script-escape "(\"foo\" \"bar\")")))
1572 (should (equal '("foo" "bar")
1573 (org-babel-script-escape "[\"foo\" \"bar\"]")))
1574 (should (equal '("foo" "bar")
1575 (org-babel-script-escape "{\"foo\" \"bar\"}")))
1577 (should (equal '("foo" "bar")
1578 (org-babel-script-escape "(\"foo\", \"bar\")")))
1579 (should (equal '("foo" "bar")
1580 (org-babel-script-escape "[\"foo\", \"bar\"]")))
1581 (should (equal '("foo" "bar")
1582 (org-babel-script-escape "{\"foo\", \"bar\"}")))
1583 ;; Delimited lists of single-quoted strings
1584 (should (equal '("foo" "bar")
1585 (org-babel-script-escape "('foo' 'bar')")))
1586 (should (equal '("foo" "bar")
1587 (org-babel-script-escape "['foo' 'bar']")))
1588 (should (equal '("foo" "bar")
1589 (org-babel-script-escape "{'foo' 'bar'}")))
1591 (should (equal '("foo" "bar")
1592 (org-babel-script-escape "('foo', 'bar')")))
1593 (should (equal '("foo" "bar")
1594 (org-babel-script-escape "['foo', 'bar']")))
1595 (should (equal '("foo" "bar")
1596 (org-babel-script-escape "{'foo', 'bar'}")))
1597 ;; Single quoted strings
1598 (should (equal "foo"
1599 (org-babel-script-escape "'foo'")))
1600 ;; ... with internal double quote
1601 (should (equal "foo\"bar"
1602 (org-babel-script-escape "'foo\"bar'")))
1603 ;; ... with internal backslash
1604 (should (equal "foo\\bar"
1605 (org-babel-script-escape "'foo\\bar'")))
1606 ;; ... with internal escaped backslash
1607 (should (equal "foo\\bar"
1608 (org-babel-script-escape "'foo\\\\bar'")))
1609 ;; ... with internal backslash-double quote
1610 (should (equal "foo\\\"bar"
1611 (org-babel-script-escape "'foo\\\"bar'")))
1612 ;; ... with internal escaped backslash-double quote
1613 (should (equal "foo\\\"bar"
1614 (org-babel-script-escape "'foo\\\\\"bar'")))
1615 ;; ... with internal escaped single quote
1616 (should (equal "foo'bar"
1617 (org-babel-script-escape "'foo\\'bar'")))
1618 ;; ... with internal escaped backslash-escaped single quote
1619 (should (equal "foo\\'bar"
1620 (org-babel-script-escape "'foo\\\\\\'bar'")))
1621 ;; Double quoted strings
1622 (should (equal "foo"
1623 (org-babel-script-escape "\"foo\"")))
1624 ;; ... with internal single quote
1625 (should (equal "foo'bar"
1626 (org-babel-script-escape "\"foo'bar\"")))
1627 ;; ... with internal backslash
1628 (should (equal "foo\\bar"
1629 (org-babel-script-escape "\"foo\\bar\"")))
1630 ;; ... with internal escaped backslash
1631 (should (equal "foo\\bar"
1632 (org-babel-script-escape "\"foo\\\\bar\"")))
1633 ;; ... with internal backslash-single quote
1634 (should (equal "foo\\'bar"
1635 (org-babel-script-escape "\"foo\\'bar\"")))
1636 ;; ... with internal escaped backslash-single quote
1637 (should (equal "foo\\'bar"
1638 (org-babel-script-escape "\"foo\\\\'bar\"")))
1639 ;; ... with internal escaped double quote
1640 (should (equal "foo\"bar"
1641 (org-babel-script-escape "\"foo\\\"bar\"")))
1642 ;; ... with internal escaped backslash-escaped double quote
1643 (should (equal "foo\\\"bar"
1644 (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
1646 (ert-deftest test-ob
/process-params-no-duplicates
()
1648 (equal (org-babel-process-params '((:colname-names
)
1652 (:var .
"\"foo\"")))
1657 (:result-type . value
)))))
1659 (defun org-test-babel-confirm-evaluate (eval-value)
1660 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
1662 #+end_src" eval-value
)
1663 (goto-char (point-min))
1664 (let ((info (org-babel-get-src-block-info)))
1665 (org-babel-check-confirm-evaluate info
))))
1667 (ert-deftest test-ob
/check-eval
()
1668 (let ((org-confirm-babel-evaluate t
))
1670 (dolist (pair '(("no" . nil
)
1674 (should (eq (org-test-babel-confirm-evaluate (car pair
)) (cdr pair
))))
1676 (let ((org-babel-exp-reference-buffer t
))
1677 (dolist (pair '(("no" . nil
)
1681 ("never-export" . nil
)
1683 ("query-export" . query
)))
1684 (message (car pair
))
1685 (should (eq (org-test-babel-confirm-evaluate (car pair
)) (cdr pair
))))))
1686 (let ((org-confirm-babel-evaluate nil
))
1688 (dolist (pair '(("no" . nil
)
1692 (should (eq (org-test-babel-confirm-evaluate (car pair
)) (cdr pair
))))
1694 (let ((org-babel-exp-reference-buffer t
))
1695 (dolist (pair '(("no" . nil
)
1699 ("never-export" . nil
)
1701 ("query-export" . query
)))
1702 (message (car pair
))
1703 (should (eq (org-test-babel-confirm-evaluate (car pair
)) (cdr pair
)))))))
1705 (defun org-test-ob/update-block-body
()
1706 "Test `org-babel-update-block-body' specifications."
1708 (equal "#+begin_src elisp\n 2\n#+end_src"
1709 (let ((org-edit-src-content-indentation 2))
1710 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1711 (org-babel-update-block-body "2")
1713 ;; Preserve block indentation.
1715 (equal " #+begin_src elisp\n 2\n #+end_src"
1716 (let ((org-edit-src-content-indentation 1))
1717 (org-test-with-temp-text
1718 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1719 (org-babel-update-block-body "2")
1721 ;; Ignore NEW-BODY global indentation.
1723 (equal "#+begin_src elisp\n 2\n#+end_src"
1724 (let ((org-edit-src-content-indentation 2))
1725 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1726 (org-babel-update-block-body " 2")
1728 ;; When indentation should be preserved ignore the two rules above.
1730 (equal " #+begin_src elisp\n2\n #+end_src"
1731 (let ((org-edit-src-content-indentation 1)
1732 (org-src-preserve-indentation t
))
1733 (org-test-with-temp-text
1734 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1735 (org-babel-update-block-body "2")
1738 (equal " #+begin_src elisp -i\n2\n #+end_src"
1739 (let ((org-edit-src-content-indentation 1))
1740 (org-test-with-temp-text
1741 " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
1742 (org-babel-update-block-body "2")
1745 (equal "#+begin_src elisp\n 2\n#+end_src"
1746 (let ((org-edit-src-content-indentation 2)
1747 (org-src-preserve-indentation t
))
1748 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1749 (org-babel-update-block-body " 2")
1752 (equal "#+begin_src elisp -i\n 2\n#+end_src"
1753 (let ((org-edit-src-content-indentation 2)
1754 (org-src-preserve-indentation t
))
1755 (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
1756 (org-babel-update-block-body " 2")
1757 (buffer-string))))))
1759 (ert-deftest test-ob
/find-named-result
()
1760 "Test `org-babel-find-named-result' specifications."
1763 (org-test-with-temp-text "#+results: foo\n: result"
1764 (org-babel-find-named-result "foo"))))
1766 (org-test-with-temp-text "#+results: foo\n: result"
1767 (org-babel-find-named-result "bar")))
1769 (org-test-with-temp-text "#+results: foobar\n: result"
1770 (org-babel-find-named-result "foo")))
1771 ;; Search is case insensitive.
1773 (org-test-with-temp-text "#+RESULTS: FOO\n: result"
1774 (org-babel-find-named-result "foo")))
1775 ;; Handle hash in results keyword.
1777 (org-test-with-temp-text "#+results[hash]: FOO\n: result"
1778 (org-babel-find-named-result "foo")))
1779 ;; Accept orphaned affiliated keywords.
1781 (org-test-with-temp-text "#+results: foo"
1782 (org-babel-find-named-result "foo"))))
1784 (ert-deftest test-ob
/where-is-src-block-result
()
1785 "Test `org-babel-where-is-src-block-result' specifications."
1786 ;; Find anonymous results.
1789 (org-test-with-temp-text
1790 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
1791 (goto-char (org-babel-where-is-src-block-result))
1792 (buffer-substring-no-properties (point) (line-end-position)))))
1793 ;; Find named results. Those have priority over anonymous ones.
1795 (equal "#+RESULTS: example"
1796 (org-test-with-temp-text
1798 <point>#+NAME: example
1799 #+BEGIN_SRC emacs-lisp
1805 (goto-char (org-babel-where-is-src-block-result))
1806 (buffer-substring-no-properties (point) (line-end-position)))))
1808 (equal "#+RESULTS: example"
1809 (org-test-with-temp-text
1811 <point>#+NAME: example
1812 #+BEGIN_SRC emacs-lisp
1821 (goto-char (org-babel-where-is-src-block-result))
1822 (buffer-substring-no-properties (point) (line-end-position)))))
1823 ;; Return nil when no result is found.
1825 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1826 (org-babel-where-is-src-block-result)))
1828 (org-test-with-temp-text
1829 "- item\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n"
1830 (org-babel-where-is-src-block-result)))
1831 ;; When optional argument INSERT is non-nil, add RESULTS keyword
1832 ;; whenever no RESULTS can be found.
1836 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1837 (let ((org-babel-results-keyword "RESULTS"))
1838 (goto-char (org-babel-where-is-src-block-result t
)))
1839 (buffer-substring-no-properties (point) (line-end-position)))))
1840 ;; Insert a named RESULTS keyword if possible.
1844 (org-test-with-temp-text
1845 "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1846 (let ((org-babel-results-keyword "RESULTS"))
1847 (goto-char (org-babel-where-is-src-block-result t
)))
1848 (buffer-substring-no-properties (point) (line-end-position)))))
1849 ;; When optional argument HASH is provided, clear RESULTS keyword
1850 ;; and related contents if they do not match it.
1854 (org-test-with-temp-text
1855 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 3"
1856 (let ((org-babel-results-keyword "RESULTS"))
1857 (goto-char (org-babel-where-is-src-block-result nil nil
"bbbb")))
1858 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1862 (org-test-with-temp-text
1863 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
1864 (let ((org-babel-results-keyword "RESULTS"))
1865 (goto-char (org-babel-where-is-src-block-result nil nil
"bbbb")))
1866 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1867 ;; Handle hashes with times.
1870 "#+RESULTS[<2014-03-04 00:41:10> bbbb]:"
1871 (org-test-with-temp-text
1873 <point>#+BEGIN_SRC emacs-lisp
1877 #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1878 (let ((org-babel-results-keyword "RESULTS")
1879 (org-babel-hash-show-time t
))
1880 (cl-letf (((symbol-function 'format-time-string
)
1881 (lambda (&rest _
) "<2014-03-04 00:41:10>")))
1882 (goto-char (org-babel-where-is-src-block-result nil nil
"bbbb"))
1883 (org-trim (buffer-substring-no-properties (point) (point-max))))))))
1886 "#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1887 (org-test-with-temp-text
1889 <point>#+BEGIN_SRC emacs-lisp
1893 #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1894 (let ((org-babel-results-keyword "RESULTS")
1895 (org-babel-hash-show-time t
))
1896 (cl-letf (((symbol-function 'format-time-string
)
1897 (lambda (&rest _
) "<2014-03-04 00:41:10>")))
1898 (goto-char (org-babel-where-is-src-block-result nil nil
"aaaa"))
1899 (org-trim (buffer-substring-no-properties (point) (point-max))))))))
1900 ;; RESULTS keyword may not be the last affiliated keyword.
1904 (org-test-with-temp-text
1906 <point>#+BEGIN_SRC emacs-lisp
1913 (let ((org-babel-results-keyword "RESULTS"))
1914 (goto-char (org-babel-where-is-src-block-result nil nil
"bbbb")))
1915 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1916 ;; HASH does nothing if no RESULTS can be found. However, if INSERT
1917 ;; is also non-nil, RESULTS keyword is inserted along with the
1921 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1922 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1923 (org-babel-where-is-src-block-result nil nil
"bbbb")
1928 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1929 (let ((org-babel-results-keyword "RESULTS"))
1930 (goto-char (org-babel-where-is-src-block-result t nil
"bbbb")))
1931 (org-trim (buffer-substring-no-properties (point) (point-max)))))))
1933 (ert-deftest test-ob
/goto-named-src-block
()
1934 "Test interactive use of `org-babel-goto-named-src-block'."
1935 (org-test-with-temp-text-in-file
1938 #+BEGIN_SRC emacs-lisp :results value
1941 #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
1942 #+BEGIN_SRC emacs-lisp
1949 ;; non-existent name
1951 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
1953 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
1954 (should (= 14 (point)))
1955 ;; call line - autocompletion
1957 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1958 (should (= 14 (point)))
1959 ;; noweb reference - autocompletion
1961 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1962 (should (= 14 (point)))
1963 ;; at symbol - autocompletion
1965 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1966 (should (= 14 (point)))
1967 ;; in results - autocompletion
1969 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1970 (should (= 14 (point)))
1972 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1973 (should (= 14 (point)))))
1975 (ert-deftest test-ob
/evaluate-body-with-coderefs
()
1978 (org-test-with-temp-text
1979 "#+begin_src emacs-lisp -l \"#(ref:%s)\"\n2 #(ref:foo)\n#+end_src"
1980 (org-babel-execute-src-block))))
1983 (org-test-with-temp-text
1984 "#+begin_src emacs-lisp\n3 #(ref:foo)\n#+end_src"
1985 (let ((org-coderef-label-format "#(ref:%s)"))
1986 (org-babel-execute-src-block))))))
1990 ;;; test-ob ends here