1 ;;; test-ob.el --- tests for ob.el
3 ;; Copyright (c) 2010-2015, 2019 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 :results file :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 file 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 t
))
1012 (should (with-temp-buffer
1013 (insert-file-contents "/tmp/test.txt")
1014 (string= "hello\n" (buffer-string)))))
1015 ;; Without "link" output type, the result is not a file.
1017 (org-test-with-temp-text "
1018 <point>#+begin_src shell :results value link :file \"/tmp/test.txt\"
1019 echo \"hello\" > /tmp/test.txt
1022 (org-babel-execute-src-block)
1023 (search-forward "[[file:/tmp/test.txt]]" nil t
))))
1025 (ert-deftest test-ob
/result-graphics-link-type-header-argument
()
1026 "Ensure that the result is a link to a file.
1027 The file is just a link to `:file' value. Inhibit non-empty
1028 result write to `:file' value."
1029 (org-test-with-temp-text "
1030 <point>#+begin_src shell :results value file graphics :file \"/tmp/test.txt\"
1031 echo \"hello\" > /tmp/test.txt
1034 (org-babel-execute-src-block)
1035 (should (search-forward "[[file:/tmp/test.txt]]" nil nil
))
1036 (should (with-temp-buffer
1037 (insert-file-contents "/tmp/test.txt")
1038 (string= "hello\n" (buffer-string)))))
1039 ;; Without "link" output type, the result is not a file.
1041 (org-test-with-temp-text "
1042 <point>#+begin_src shell :results value graphics :file \"/tmp/test.txt\"
1043 echo \"hello\" > /tmp/test.txt
1046 (org-babel-execute-src-block)
1047 (search-forward "[[file:/tmp/test.txt]]" nil t
))))
1049 (ert-deftest test-ob
/inline-src_blk-preceded-punct-preceded-by-point
()
1050 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
1051 (org-babel-inline-result-wrap "=%s="))
1052 (org-test-with-temp-text
1055 (org-babel-execute-maybe)
1056 (should (re-search-forward "=\"x\"=" nil t
))
1059 (ert-deftest test-ob
/commented-last-block-line-with-var
()
1060 (org-test-with-temp-text-in-file "
1061 #+begin_src emacs-lisp :var a=1
1064 (org-babel-next-src-block)
1065 (org-babel-execute-maybe)
1066 (re-search-forward "\\#\\+results:" nil t
)
1070 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
1071 (org-test-with-temp-text-in-file "
1072 #+begin_src emacs-lisp :var a=2
1075 (org-babel-next-src-block)
1076 (org-babel-execute-maybe)
1077 (re-search-forward "\\#\\+results:" nil t
)
1081 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
1083 (defun test-ob-verify-result-and-removed-result (result buffer-text
)
1084 "Test helper function to test `org-babel-remove-result'.
1085 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
1086 and the result of execution is verified against RESULT.
1088 The block is actually executed /twice/ to ensure result
1089 replacement happens correctly."
1090 (org-test-with-temp-text
1092 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
1093 (should (re-search-forward "\\#\\+results:" nil t
))
1095 (should (string= result
1096 (buffer-substring-no-properties
1098 (- (point-max) 16))))
1099 (org-babel-previous-src-block) (org-babel-remove-result)
1100 (should (string= buffer-text
1101 (buffer-substring-no-properties
1102 (point-min) (point-max))))))
1104 (ert-deftest test-ob
/org-babel-remove-result--results-list
()
1105 "Test `org-babel-remove-result' with :results list."
1106 (test-ob-verify-result-and-removed-result
1111 "* org-babel-remove-result
1112 #+begin_src emacs-lisp :results list
1118 (ert-deftest test-ob
/org-babel-remove-result--results-wrap
()
1119 "Test `org-babel-remove-result' with :results wrap."
1120 (test-ob-verify-result-and-removed-result
1125 "* org-babel-remove-result
1127 #+begin_src emacs-lisp :results wrap
1133 (ert-deftest test-ob
/org-babel-remove-result--results-org
()
1134 "Test `org-babel-remove-result' with :results org."
1135 (test-ob-verify-result-and-removed-result
1142 "* org-babel-remove-result
1143 #+begin_src emacs-lisp :results org
1151 (ert-deftest test-ob
/org-babel-remove-result--results-html
()
1152 "Test `org-babel-remove-result' with :results html."
1153 (test-ob-verify-result-and-removed-result
1154 "#+begin_export html
1155 <head><body></body></head>
1158 "* org-babel-remove-result
1159 #+begin_src emacs-lisp :results html
1160 \"<head><body></body></head>\"
1165 (ert-deftest test-ob
/org-babel-remove-result--results-latex
()
1166 "Test `org-babel-remove-result' with :results latex."
1167 (test-ob-verify-result-and-removed-result
1168 "#+begin_export latex
1174 "* org-babel-remove-result
1175 #+begin_src emacs-lisp :results latex
1183 (ert-deftest test-ob
/org-babel-remove-result--results-code
()
1184 "Test `org-babel-remove-result' with :results code."
1186 (test-ob-verify-result-and-removed-result
1187 "#+begin_src emacs-lisp
1191 "* org-babel-remove-result
1192 #+begin_src emacs-lisp :results code
1193 (message \"I am working!\")
1198 (ert-deftest test-ob
/org-babel-remove-result--results-pp
()
1199 "Test `org-babel-remove-result' with :results pp."
1200 (test-ob-verify-result-and-removed-result
1201 ": \"I /am/ working!\""
1203 "* org-babel-remove-result
1204 #+begin_src emacs-lisp :results pp
1205 \"I /am/ working!\")
1210 (ert-deftest test-ob
/org-babel-remove-result--no-blank-line
()
1211 "Test `org-babel-remove-result' without blank line between code and results."
1214 #+begin_src emacs-lisp
1220 (org-test-with-temp-text
1222 <point>#+begin_src emacs-lisp
1228 (org-babel-execute-maybe)
1231 (ert-deftest test-ob
/results-do-not-replace-code-blocks
()
1232 (org-test-with-temp-text "Block two has a space after the name.
1235 #+begin_src emacs-lisp
1240 #+begin_src emacs-lisp
1245 #+begin_src emacs-lisp
1252 (dolist (num '(1 2 3))
1253 ;; execute the block
1254 (goto-char (point-min))
1255 (org-babel-next-src-block num
) (org-babel-execute-src-block)
1256 ;; check the results
1257 (goto-char (point-max))
1258 (move-beginning-of-line 0)
1259 (should (looking-at (format ": %d" num
))))))
1261 (ert-deftest test-ob
/blocks-with-spaces
()
1262 "Test expansion of blocks followed by blank lines."
1263 ;; Preserve number of blank lines after block.
1265 (equal "#+BEGIN_SRC emacs-lisp
1271 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1274 (let ((org-babel-next-src-block "RESULTS"))
1275 (org-babel-execute-src-block))
1277 ;; Do not add spurious blank lines after results.
1283 #+begin_src emacs-lisp
1291 (org-test-with-temp-text "
1294 #+begin_src emacs-lisp<point>
1299 (org-babel-execute-src-block)
1306 #+begin_src emacs-lisp
1314 (org-test-with-temp-text "
1317 #+begin_src emacs-lisp<point>
1325 (org-babel-execute-src-block)
1328 (ert-deftest test-ob
/results-in-narrowed-buffer
()
1329 "Test block execution in a narrowed buffer."
1330 ;; If results don't exist, they should be inserted in visible part
1334 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1335 (org-test-with-temp-text
1336 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1337 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1338 (let ((org-babel-results-keyword "RESULTS"))
1339 (org-babel-execute-src-block))
1340 (org-trim (buffer-string)))))
1343 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1344 (org-test-with-temp-text
1345 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1346 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1347 (let ((org-babel-results-keyword "RESULTS"))
1348 (org-babel-execute-src-block))
1349 (org-trim (buffer-string)))))
1350 ;; Results in visible part of buffer, should be updated here.
1354 #+BEGIN_SRC emacs-lisp
1360 (org-test-with-temp-text
1362 #+BEGIN_SRC emacs-lisp
1370 (narrow-to-region (point-min) (point))
1371 (goto-char (point-min))
1372 (let ((org-babel-results-keyword "RESULTS"))
1373 (org-babel-execute-src-block))
1374 (org-trim (buffer-string)))))
1375 ;; Results in invisible part of buffer, should be updated there.
1376 (org-test-with-temp-text
1378 #+BEGIN_SRC emacs-lisp
1386 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1387 (let ((org-babel-results-keyword "RESULTS"))
1388 (org-babel-execute-src-block))
1389 (should-not (re-search-forward "^#\\+RESULTS:" nil t
))
1391 (should (re-search-forward "^: 3" nil t
))))
1393 (ert-deftest test-ob
/specific-colnames
()
1394 "Test passing specific column names."
1396 (equal "#+name: input-table
1402 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1412 (org-test-with-temp-text
1413 "#+name: input-table
1419 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1423 ;; we should find a code block
1424 (should (re-search-forward org-babel-src-block-regexp nil t
))
1425 (goto-char (match-beginning 0))
1426 ;; now that we've located the code block, it may be evaluated
1427 (let ((org-babel-execute-src-block "RESULTS"))
1428 (org-babel-execute-src-block))
1431 (ert-deftest test-ob
/location-of-header-arg-eval
()
1432 "Test location of header argument evaluation."
1433 (org-test-with-temp-text "
1435 #+begin_src emacs-lisp :var pt=(point)
1439 #+name: bottom-block
1440 #+begin_src emacs-lisp :var pt=top-block()
1444 ;; the value of the second block should be greater than the first
1446 (< (progn (re-search-forward org-babel-src-block-regexp nil t
)
1447 (goto-char (match-beginning 0))
1448 (prog1 (save-match-data (org-babel-execute-src-block))
1449 (goto-char (match-end 0))))
1450 (progn (re-search-forward org-babel-src-block-regexp nil t
)
1451 (goto-char (match-beginning 0))
1452 (org-babel-execute-src-block))))))
1454 (ert-deftest test-ob
/preserve-results-indentation
()
1455 "Preserve indentation when executing a source block."
1459 (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1460 (org-babel-execute-src-block)
1461 (let ((case-fold-search t
)) (search-forward "RESULTS"))
1462 (list (org-get-indentation)
1463 (progn (forward-line) (org-get-indentation))))))
1467 (org-test-with-temp-text
1468 " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1469 (org-babel-execute-src-block)
1470 (let ((case-fold-search t
)) (search-forward "RESULTS"))
1471 (list (org-get-indentation)
1472 (progn (forward-line) (org-get-indentation))))))
1473 ;; Don't get fooled by TAB-based indentation.
1477 (org-test-with-temp-text
1478 "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
1480 (org-babel-execute-src-block)
1481 (let ((case-fold-search t
)) (search-forward "RESULTS"))
1482 (list (org-get-indentation)
1483 (progn (forward-line) (org-get-indentation))))))
1484 ;; Properly indent examplified blocks.
1500 (org-test-with-temp-text
1501 " #+begin_src emacs-lisp :results output
1502 (dotimes (i 10) (princ i) (princ \"\\n\"))
1504 (org-babel-execute-src-block)
1505 (search-forward "begin_example")
1507 (buffer-substring-no-properties (line-beginning-position)
1509 ;; Properly indent "org" blocks.
1525 (org-test-with-temp-text
1526 " #+begin_src emacs-lisp :results output org
1527 (dotimes (i 10) (princ i) (princ \"\\n\"))
1529 (org-babel-execute-src-block)
1530 (search-forward "begin_src org")
1532 (buffer-substring-no-properties (line-beginning-position)
1535 (ert-deftest test-ob
/safe-header-args
()
1536 "Detect safe and unsafe header args."
1537 (let ((safe-args '((:cache .
"foo")
1538 (:results .
"output")
1541 (unsafe-args '((:eval .
"yes")
1542 (:results .
"output file")
1544 (malformed-args '((bar .
"foo")
1547 (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args
)))
1548 (dolist (arg safe-args
)
1549 (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args
)))
1550 (dolist (arg unsafe-args
)
1551 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args
))))
1552 (dolist (arg malformed-args
)
1553 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args
))))
1554 (should (not (funcall safe-p
(append safe-args unsafe-args
))))))
1556 (ert-deftest test-ob
/noweb-expansions-in-cache
()
1557 "Ensure that noweb expansions are expanded before caching."
1558 (let ((noweb-expansions-in-cache-var 0))
1559 (org-test-with-temp-text "
1561 #+begin_src emacs-lisp
1566 #+begin_src emacs-lisp :noweb yes :cache yes
1567 (setq noweb-expansions-in-cache-var
1568 (+ 1 noweb-expansions-in-cache-var))
1569 (concat <<foo>> \" check noweb expansions\")
1572 ;; run the second block to create the cache
1573 (goto-char (point-min))
1574 (re-search-forward (regexp-quote "#+name: bar"))
1575 (should (string= "I said check noweb expansions"
1576 (org-babel-execute-src-block)))
1577 (should (= noweb-expansions-in-cache-var
1))
1578 ;; change the value of the first block
1579 (goto-char (point-min))
1580 (re-search-forward (regexp-quote "said"))
1581 (goto-char (match-beginning 0))
1583 (re-search-forward (regexp-quote "#+name: bar"))
1584 (should (string= "I haven't said check noweb expansions"
1585 (org-babel-execute-src-block)))
1586 (should (= noweb-expansions-in-cache-var
2)))))
1588 (ert-deftest test-ob
/file-ext-and-output-dir
()
1589 (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
1590 (org-babel-next-src-block)
1591 (should (equal "file-ext-basic.txt"
1592 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1593 (org-babel-next-src-block)
1594 (should (equal "foo/file-ext-dir-relative.txt"
1595 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1596 (org-babel-next-src-block)
1597 (should (equal "foo/file-ext-dir-relative-slash.txt"
1598 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1599 (org-babel-next-src-block)
1600 (should (equal "/tmp/file-ext-dir-absolute.txt"
1601 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1602 (org-babel-next-src-block)
1603 (should (equal "foo.bar"
1604 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1605 (org-babel-next-src-block)
1606 (should (equal "xxx/foo.bar"
1607 (cdr (assq :file
(nth 2 (org-babel-get-src-block-info t
))))))
1610 (ert-deftest test-ob-core
/dir-mkdirp
()
1611 (org-test-with-temp-text
1612 "#+begin_src sh :mkdirp yes :dir \"data/code\"
1615 (org-babel-execute-src-block))
1616 (should (file-directory-p "data/code")))
1618 (ert-deftest test-ob
/script-escape
()
1619 ;; Delimited lists of numbers
1620 (should (equal '(1 2 3)
1621 (org-babel-script-escape "[1 2 3]")))
1622 (should (equal '(1 2 3)
1623 (org-babel-script-escape "{1 2 3}")))
1624 (should (equal '(1 2 3)
1625 (org-babel-script-escape "(1 2 3)")))
1626 ;; Delimited lists of double-quoted strings
1627 (should (equal '("foo" "bar")
1628 (org-babel-script-escape "(\"foo\" \"bar\")")))
1629 (should (equal '("foo" "bar")
1630 (org-babel-script-escape "[\"foo\" \"bar\"]")))
1631 (should (equal '("foo" "bar")
1632 (org-babel-script-escape "{\"foo\" \"bar\"}")))
1634 (should (equal '("foo" "bar")
1635 (org-babel-script-escape "(\"foo\", \"bar\")")))
1636 (should (equal '("foo" "bar")
1637 (org-babel-script-escape "[\"foo\", \"bar\"]")))
1638 (should (equal '("foo" "bar")
1639 (org-babel-script-escape "{\"foo\", \"bar\"}")))
1640 ;; Delimited lists of single-quoted strings
1641 (should (equal '("foo" "bar")
1642 (org-babel-script-escape "('foo' 'bar')")))
1643 (should (equal '("foo" "bar")
1644 (org-babel-script-escape "['foo' 'bar']")))
1645 (should (equal '("foo" "bar")
1646 (org-babel-script-escape "{'foo' 'bar'}")))
1648 (should (equal '("foo" "bar")
1649 (org-babel-script-escape "('foo', 'bar')")))
1650 (should (equal '("foo" "bar")
1651 (org-babel-script-escape "['foo', 'bar']")))
1652 (should (equal '("foo" "bar")
1653 (org-babel-script-escape "{'foo', 'bar'}")))
1654 ;; Single quoted strings
1655 (should (equal "foo"
1656 (org-babel-script-escape "'foo'")))
1657 ;; ... with internal double quote
1658 (should (equal "foo\"bar"
1659 (org-babel-script-escape "'foo\"bar'")))
1660 ;; ... with internal backslash
1661 (should (equal "foo\\bar"
1662 (org-babel-script-escape "'foo\\bar'")))
1663 ;; ... with internal escaped backslash
1664 (should (equal "foo\\bar"
1665 (org-babel-script-escape "'foo\\\\bar'")))
1666 ;; ... with internal backslash-double quote
1667 (should (equal "foo\\\"bar"
1668 (org-babel-script-escape "'foo\\\"bar'")))
1669 ;; ... with internal escaped backslash-double quote
1670 (should (equal "foo\\\"bar"
1671 (org-babel-script-escape "'foo\\\\\"bar'")))
1672 ;; ... with internal escaped single quote
1673 (should (equal "foo'bar"
1674 (org-babel-script-escape "'foo\\'bar'")))
1675 ;; ... with internal escaped backslash-escaped single quote
1676 (should (equal "foo\\'bar"
1677 (org-babel-script-escape "'foo\\\\\\'bar'")))
1678 ;; Double quoted strings
1679 (should (equal "foo"
1680 (org-babel-script-escape "\"foo\"")))
1681 ;; ... with internal single quote
1682 (should (equal "foo'bar"
1683 (org-babel-script-escape "\"foo'bar\"")))
1684 ;; ... with internal backslash
1685 (should (equal "foo\\bar"
1686 (org-babel-script-escape "\"foo\\bar\"")))
1687 ;; ... with internal escaped backslash
1688 (should (equal "foo\\bar"
1689 (org-babel-script-escape "\"foo\\\\bar\"")))
1690 ;; ... with internal backslash-single quote
1691 (should (equal "foo\\'bar"
1692 (org-babel-script-escape "\"foo\\'bar\"")))
1693 ;; ... with internal escaped backslash-single quote
1694 (should (equal "foo\\'bar"
1695 (org-babel-script-escape "\"foo\\\\'bar\"")))
1696 ;; ... with internal escaped double quote
1697 (should (equal "foo\"bar"
1698 (org-babel-script-escape "\"foo\\\"bar\"")))
1699 ;; ... with internal escaped backslash-escaped double quote
1700 (should (equal "foo\\\"bar"
1701 (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
1703 (ert-deftest test-ob
/process-params-no-duplicates
()
1705 (equal (org-babel-process-params '((:colname-names
)
1709 (:var .
"\"foo\"")))
1714 (:result-type . value
)))))
1716 (defun org-test-babel-confirm-evaluate (eval-value)
1717 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
1719 #+end_src" eval-value
)
1720 (goto-char (point-min))
1721 (let ((info (org-babel-get-src-block-info)))
1722 (org-babel-check-confirm-evaluate info
))))
1724 (ert-deftest test-ob
/check-eval
()
1725 (let ((org-confirm-babel-evaluate t
))
1727 (dolist (pair '(("no" . nil
)
1731 (should (eq (org-test-babel-confirm-evaluate (car pair
)) (cdr pair
))))
1733 (let ((org-babel-exp-reference-buffer t
))
1734 (dolist (pair '(("no" . nil
)
1738 ("never-export" . nil
)
1740 ("query-export" . query
)))
1741 (message (car pair
))
1742 (should (eq (org-test-babel-confirm-evaluate (car pair
)) (cdr pair
))))))
1743 (let ((org-confirm-babel-evaluate nil
))
1745 (dolist (pair '(("no" . nil
)
1749 (should (eq (org-test-babel-confirm-evaluate (car pair
)) (cdr pair
))))
1751 (let ((org-babel-exp-reference-buffer t
))
1752 (dolist (pair '(("no" . nil
)
1756 ("never-export" . nil
)
1758 ("query-export" . query
)))
1759 (message (car pair
))
1760 (should (eq (org-test-babel-confirm-evaluate (car pair
)) (cdr pair
)))))))
1762 (defun org-test-ob/update-block-body
()
1763 "Test `org-babel-update-block-body' specifications."
1765 (equal "#+begin_src elisp\n 2\n#+end_src"
1766 (let ((org-edit-src-content-indentation 2))
1767 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1768 (org-babel-update-block-body "2")
1770 ;; Preserve block indentation.
1772 (equal " #+begin_src elisp\n 2\n #+end_src"
1773 (let ((org-edit-src-content-indentation 1))
1774 (org-test-with-temp-text
1775 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1776 (org-babel-update-block-body "2")
1778 ;; Ignore NEW-BODY global indentation.
1780 (equal "#+begin_src elisp\n 2\n#+end_src"
1781 (let ((org-edit-src-content-indentation 2))
1782 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1783 (org-babel-update-block-body " 2")
1785 ;; When indentation should be preserved ignore the two rules above.
1787 (equal " #+begin_src elisp\n2\n #+end_src"
1788 (let ((org-edit-src-content-indentation 1)
1789 (org-src-preserve-indentation t
))
1790 (org-test-with-temp-text
1791 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1792 (org-babel-update-block-body "2")
1795 (equal " #+begin_src elisp -i\n2\n #+end_src"
1796 (let ((org-edit-src-content-indentation 1))
1797 (org-test-with-temp-text
1798 " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
1799 (org-babel-update-block-body "2")
1802 (equal "#+begin_src elisp\n 2\n#+end_src"
1803 (let ((org-edit-src-content-indentation 2)
1804 (org-src-preserve-indentation t
))
1805 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1806 (org-babel-update-block-body " 2")
1809 (equal "#+begin_src elisp -i\n 2\n#+end_src"
1810 (let ((org-edit-src-content-indentation 2)
1811 (org-src-preserve-indentation t
))
1812 (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
1813 (org-babel-update-block-body " 2")
1814 (buffer-string))))))
1816 (ert-deftest test-ob
/find-named-result
()
1817 "Test `org-babel-find-named-result' specifications."
1820 (org-test-with-temp-text "#+results: foo\n: result"
1821 (org-babel-find-named-result "foo"))))
1823 (org-test-with-temp-text "#+results: foo\n: result"
1824 (org-babel-find-named-result "bar")))
1826 (org-test-with-temp-text "#+results: foobar\n: result"
1827 (org-babel-find-named-result "foo")))
1828 ;; Search is case insensitive.
1830 (org-test-with-temp-text "#+RESULTS: FOO\n: result"
1831 (org-babel-find-named-result "foo")))
1832 ;; Handle hash in results keyword.
1834 (org-test-with-temp-text "#+results[hash]: FOO\n: result"
1835 (org-babel-find-named-result "foo")))
1836 ;; Accept orphaned affiliated keywords.
1838 (org-test-with-temp-text "#+results: foo"
1839 (org-babel-find-named-result "foo"))))
1841 (ert-deftest test-ob
/where-is-src-block-result
()
1842 "Test `org-babel-where-is-src-block-result' specifications."
1843 ;; Find anonymous results.
1846 (org-test-with-temp-text
1847 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
1848 (goto-char (org-babel-where-is-src-block-result))
1849 (buffer-substring-no-properties (point) (line-end-position)))))
1850 ;; Find named results. Those have priority over anonymous ones.
1852 (equal "#+RESULTS: example"
1853 (org-test-with-temp-text
1855 <point>#+NAME: example
1856 #+BEGIN_SRC emacs-lisp
1862 (goto-char (org-babel-where-is-src-block-result))
1863 (buffer-substring-no-properties (point) (line-end-position)))))
1865 (equal "#+RESULTS: example"
1866 (org-test-with-temp-text
1868 <point>#+NAME: example
1869 #+BEGIN_SRC emacs-lisp
1878 (goto-char (org-babel-where-is-src-block-result))
1879 (buffer-substring-no-properties (point) (line-end-position)))))
1880 ;; Return nil when no result is found.
1882 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1883 (org-babel-where-is-src-block-result)))
1885 (org-test-with-temp-text
1886 "- item\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n"
1887 (org-babel-where-is-src-block-result)))
1888 ;; When optional argument INSERT is non-nil, add RESULTS keyword
1889 ;; whenever no RESULTS can be found.
1893 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1894 (let ((org-babel-results-keyword "RESULTS"))
1895 (goto-char (org-babel-where-is-src-block-result t
)))
1896 (buffer-substring-no-properties (point) (line-end-position)))))
1897 ;; Insert a named RESULTS keyword if possible.
1901 (org-test-with-temp-text
1902 "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1903 (let ((org-babel-results-keyword "RESULTS"))
1904 (goto-char (org-babel-where-is-src-block-result t
)))
1905 (buffer-substring-no-properties (point) (line-end-position)))))
1906 ;; When optional argument HASH is provided, clear RESULTS keyword
1907 ;; and related contents if they do not match it.
1911 (org-test-with-temp-text
1912 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 3"
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))))))
1919 (org-test-with-temp-text
1920 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
1921 (let ((org-babel-results-keyword "RESULTS"))
1922 (goto-char (org-babel-where-is-src-block-result nil nil
"bbbb")))
1923 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1924 ;; Handle hashes with times.
1927 "#+RESULTS[<2014-03-04 00:41:10> bbbb]:"
1928 (org-test-with-temp-text
1930 <point>#+BEGIN_SRC emacs-lisp
1934 #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1935 (let ((org-babel-results-keyword "RESULTS")
1936 (org-babel-hash-show-time t
))
1937 (cl-letf (((symbol-function 'format-time-string
)
1938 (lambda (&rest _
) "<2014-03-04 00:41:10>")))
1939 (goto-char (org-babel-where-is-src-block-result nil nil
"bbbb"))
1940 (org-trim (buffer-substring-no-properties (point) (point-max))))))))
1943 "#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1944 (org-test-with-temp-text
1946 <point>#+BEGIN_SRC emacs-lisp
1950 #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1951 (let ((org-babel-results-keyword "RESULTS")
1952 (org-babel-hash-show-time t
))
1953 (cl-letf (((symbol-function 'format-time-string
)
1954 (lambda (&rest _
) "<2014-03-04 00:41:10>")))
1955 (goto-char (org-babel-where-is-src-block-result nil nil
"aaaa"))
1956 (org-trim (buffer-substring-no-properties (point) (point-max))))))))
1957 ;; RESULTS keyword may not be the last affiliated keyword.
1961 (org-test-with-temp-text
1963 <point>#+BEGIN_SRC emacs-lisp
1970 (let ((org-babel-results-keyword "RESULTS"))
1971 (goto-char (org-babel-where-is-src-block-result nil nil
"bbbb")))
1972 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1973 ;; HASH does nothing if no RESULTS can be found. However, if INSERT
1974 ;; is also non-nil, RESULTS keyword is inserted along with the
1978 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1979 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1980 (org-babel-where-is-src-block-result nil nil
"bbbb")
1985 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1986 (let ((org-babel-results-keyword "RESULTS"))
1987 (goto-char (org-babel-where-is-src-block-result t nil
"bbbb")))
1988 (org-trim (buffer-substring-no-properties (point) (point-max)))))))
1990 (ert-deftest test-ob
/goto-named-src-block
()
1991 "Test interactive use of `org-babel-goto-named-src-block'."
1992 (org-test-with-temp-text-in-file
1995 #+BEGIN_SRC emacs-lisp :results value
1998 #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
1999 #+BEGIN_SRC emacs-lisp
2006 ;; non-existent name
2008 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
2010 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
2011 (should (= 14 (point)))
2012 ;; call line - autocompletion
2014 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
2015 (should (= 14 (point)))
2016 ;; noweb reference - autocompletion
2018 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
2019 (should (= 14 (point)))
2020 ;; at symbol - autocompletion
2022 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
2023 (should (= 14 (point)))
2024 ;; in results - autocompletion
2026 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
2027 (should (= 14 (point)))
2029 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
2030 (should (= 14 (point)))))
2032 (ert-deftest test-ob
/evaluate-body-with-coderefs
()
2035 (org-test-with-temp-text
2036 "#+begin_src emacs-lisp -l \"#(ref:%s)\"\n2 #(ref:foo)\n#+end_src"
2037 (org-babel-execute-src-block))))
2040 (org-test-with-temp-text
2041 "#+begin_src emacs-lisp\n3 #(ref:foo)\n#+end_src"
2042 (let ((org-coderef-label-format "#(ref:%s)"))
2043 (org-babel-execute-src-block))))))
2045 (ert-deftest test-ob
/string-to-number
()
2046 (should (= 0 (org-babel--string-to-number "0")))
2047 (should (= 1 (org-babel--string-to-number "1")))
2048 (should (eq nil
(org-babel--string-to-number "000")))
2049 (should (eq nil
(org-babel--string-to-number "001")))
2050 (should (eq nil
(org-babel--string-to-number "010")))
2051 (should (= 100 (org-babel--string-to-number "100")))
2052 (should (= 0.1 (org-babel--string-to-number "0.1")))
2053 (should (= 1.0 (org-babel--string-to-number "1.0"))))
2057 ;;; test-ob ends here