ob-core: Make :mkdirp work for :dir too
[org-mode/org-tableheadings.git] / testing / lisp / test-ob.el
blob33d6ae8f7ed542e43e5af4991ea0f4285838e10e
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/>.
21 ;;; Code:
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."
28 (should
29 (let ((default-directory temporary-file-directory))
30 (org-test-with-temp-text
32 * Test
33 #+<point>BEGIN_SRC emacs-lisp :results file :file test.txt :cache yes
34 (message \"test\")
35 #+END_SRC"
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?
45 (should (string-match
46 org-babel-multi-line-header-regexp
47 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
48 (should
49 (equal
50 "blah1 blah2 blah3 \t"
51 (match-string
53 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
55 ;;TODO Check - should this fail?
56 (should
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 ()
62 (let ((test-block
63 (concat
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"
67 "#+end_src"))
68 (language "language")
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)))
79 ;;no switches
80 (should (org-test-string-exact-match
81 org-babel-src-block-regexp
82 (replace-regexp-in-string flags "" test-block)))
83 ;;no header arguments
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 ()
93 (should(equal
94 '((:session . "none")
95 (:results . "replace")
96 (:exports . "results")
97 (:hlines . "yes"))
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
102 '((foo . :any)
103 (bar)
104 (baz . ((foo bar) (baz)))
105 (qux . ((foo bar baz qux)))
106 (quux . ((foo bar))))
107 '((bar)
108 (baz . ((baz)))
109 (quux . :any)))))
110 (dolist (pair '((foo . :any)
111 (bar)
112 (baz . ((baz)))
113 (quux . :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)
128 (nth 1 info))))))
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
140 :PROPERTIES:
141 :header-args: :var prop = (* 7 6)
142 :END:
143 #+begin_src emacs-lisp
144 prop
145 #+end_src"
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
157 #+end_src"
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 "
165 #+name: four
166 #+begin_src emacs-lisp
167 (list 1 2 3 4)
168 #+end_src
170 #+begin_src emacs-lisp :var four=four
171 (length four)
172 #+end_src"
174 (org-babel-next-src-block 2)
175 (should (= 4 (org-babel-execute-src-block)))
176 (forward-line 5)
177 (should (string= ": 4" (buffer-substring
178 (point-at-bol)
179 (point-at-eol))))))
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)
187 (require 'cl-lib)
188 (cl-map 'list #'list numbers letters)
189 #+end_src"
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
199 the body
200 #+end_src"
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
219 (length text)
220 #+end_src
222 #+begin_src org :noweb yes
223 <<simple-subtree>>
224 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
225 #+end_src
227 ** simple subtree with custom ID
228 :PROPERTIES:
229 :CUSTOM_ID: simple-subtree
230 :END:
231 this is simple"
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 ()
239 (should
240 (= 1
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))))
244 (should
245 (= 2
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))))
249 (should
250 (= 3
251 (org-test-with-temp-text
252 "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
253 (org-babel-execute-src-block))))
254 (should
255 (= 4
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))))
260 (should
261 (= 5
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))))
266 (should
267 (= 6
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))))
272 (should
273 (= 7
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
284 test-line
285 (goto-char (point-min)) (org-babel-execute-maybe)
286 (should (string=
287 (concat test-line " {{{results(=1=)}}}")
288 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
289 (forward-char) (org-babel-execute-maybe)
290 (should (string=
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)
297 (should (string=
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
303 test-line
304 (should-error (org-ctrl-c-ctrl-c))
305 (forward-char) (org-babel-execute-maybe)
306 (should (string=
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)
310 (should (string=
311 (concat test-line " {{{results(=1=)}}}")
312 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
313 (forward-char 6)
314 (should-error (org-ctrl-c-ctrl-c))))
315 ;; Results on a subsequent line are replaced.
316 (should
317 (equal
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))
321 (buffer-string))))
322 ;; Also handle results at the beginning of a line.
323 (should
324 (equal
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))
328 (buffer-string))))))
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))
339 (forward-line)
340 (should-error (org-ctrl-c-ctrl-c))
341 (forward-char) (org-babel-execute-maybe)
342 (should (string=
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
349 test-line
350 (goto-char (point-max))
351 (insert (concat "\n" test-line " end"))
352 (re-search-backward "src") (org-babel-execute-maybe)
353 (should (string=
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)
357 (should (string=
358 (concat test-line " {{{results(=y=)}}} end")
359 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
360 (forward-char 3)
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)
371 (beginning-of-line)
372 (should-error (org-ctrl-c-ctrl-c))
373 (forward-char) (org-babel-execute-maybe)
374 (should (string=
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)
385 (should (string=
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)
389 (should (string=
390 (concat test-line " {{{results(=y=)}}} end")
391 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
392 (forward-char 3)
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
405 test-line
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))))
416 (forward-char 2)
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")
424 (buffer-string)))))
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))))
436 (forward-char 2)
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
442 test-line
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
452 test-line
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
462 test-line
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]]\"
473 #+end_src
475 #+name:
476 : [[file:./cv.cls]]
478 #+begin_src sh :results raw scalar
479 echo \"[[file:./cv.cls]]\"
480 #+end_src
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))
486 (forward-line 1)))
487 (goto-char (point-min))
488 (next-result)
489 (should (eq (org-element-type (org-element-at-point)) 'fixed-width))
490 (next-result)
491 (should-not
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
496 a proper error."
497 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
498 (org-babel-next-src-block)
499 (let ((err
500 (should-error (org-babel-execute-src-block) :type 'error)))
501 (should
502 (equal
503 '(error
504 "Variable \"x\" must be assigned a default value")
505 err)))))
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."
519 (should
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
523 'foo
524 ,#+end_src
525 #+end_src"
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
534 #+end_src"
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
540 'foo
541 #+END_SRC\n"
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
551 #+BEGIN_EXAMPLE
552 A literal example
553 on two lines
554 #+END_EXAMPLE
556 #+NAME: read-literal-example
557 #+BEGIN_SRC emacs-lisp :var x=literal-example
558 (cl-concatenate 'string x \" for me.\")
559 #+END_SRC"
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 ()
565 (should
566 (= 2
567 (org-test-with-temp-text
569 * COMMENT H1
570 #+NAME: n
573 * H2
574 #+NAME: n
577 * Code
579 <point>#+BEGIN_SRC emacs-lisp :var x=n
581 #+END_SRC"
582 (org-babel-execute-src-block)))))
584 (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
585 (org-test-with-temp-text "
586 #+name: base_plus
587 | 1 |
588 | 2 |
590 #+name: base
591 | 3 |
592 | 4 |
594 #+begin_src emacs-lisp :var x=base
596 #+end_src"
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 "
602 #+name: base_plus
603 #+begin_src emacs-lisp
604 'bar
605 #+end_src
607 #+name: base
608 #+begin_src emacs-lisp
609 'foo
610 #+end_src
612 #+begin_src emacs-lisp :var x=base
614 #+end_src"
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
620 (+ a b c d)
621 #+end_src
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
629 #+end_src
631 #+results: foo
634 #+begin_src emacs-lisp :var it=foo
635 (+ it 1)
636 #+end_src"
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))
642 (forward-line 6)
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))
649 (forward-line 6)
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
655 (setq foo :evald)
656 #+end_src" eval)
657 (let ((foo :not-run))
658 (if runp
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 ()
674 ;; Standard test.
675 (should
676 (string=
677 "bar"
678 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
679 <<foo>>
680 #+end_src
682 #+name: foo
683 #+begin_src sh
685 #+end_src"
686 (org-babel-expand-noweb-references))))
687 ;; Handle :noweb-sep.
688 (should
689 (string=
690 "barbaz"
691 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
692 <<foo>>
693 #+end_src
695 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
697 #+end_src
699 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
701 #+end_src"
702 (org-babel-expand-noweb-references))))
703 ;; :noweb-ref is extracted from definition, not point of call.
704 (should
705 (string=
706 "(+ 1 1)"
707 (org-test-with-temp-text
709 * Call
710 :PROPERTIES:
711 :header-args: :noweb-ref bar
712 :END:
714 <point>#+begin_src emacs-lisp :results output :tangle yes
715 <<foo>>
716 #+end_src
718 * Evaluation
719 :PROPERTIES:
720 :header-args: :noweb-ref foo
721 :END:
723 #+begin_src sh :noweb-sep \"\"
724 (+ 1 1)
725 #+end_src"
726 (org-babel-expand-noweb-references))))
727 ;; Handle recursive expansion.
728 (should
729 (equal "baz"
730 (org-test-with-temp-text "
731 #+begin_src emacs-lisp :noweb yes<point>
732 <<foo>>
733 #+end_src
735 #+name: foo
736 #+begin_src emacs-lisp :noweb yes
737 <<bar>>
738 #+end_src
740 #+name: bar
741 #+begin_src emacs-lisp
743 #+end_src"
744 (org-babel-expand-noweb-references))))
745 ;; During recursive expansion, obey to `:noweb' property.
746 (should
747 (equal "<<bar>>"
748 (org-test-with-temp-text "
749 #+begin_src emacs-lisp :noweb yes<point>
750 <<foo>>
751 #+end_src
753 #+name: foo
754 #+begin_src emacs-lisp :noweb no
755 <<bar>>
756 #+end_src
758 #+name: bar
759 #+begin_src emacs-lisp
761 #+end_src"
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."
773 (should (equal
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))"
776 '((32 9) . 58))))
777 ;; Handle un-balanced parens.
778 (should
779 (equal '(":foo ((6)" "bar 1")
780 (org-babel-balanced-split ":foo ((6) :bar 1" '((32 9) . 58))))
781 (should
782 (equal '(":foo \"(foo\"" "bar 2")
783 (org-babel-balanced-split ":foo \"(foo\" :bar 2" '((32 9) . 58))))
784 ;; Handle un-balanced quotes.
785 (should
786 (equal '(":foo \"1" "bar 3")
787 (org-babel-balanced-split ":foo \"1 :bar 3" '((32 9) . 58))))
788 ;; Handle empty string.
789 (should
790 (equal '(":foo \"\"")
791 (org-babel-balanced-split ":foo \"\"" '((32 9) . 58))))
792 ;; Handle control characters within double quotes.
793 (should
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
801 #+end_src"
802 (org-babel-next-src-block)
803 (org-babel-execute-maybe)
804 (should (re-search-forward "\\#\\+results:" nil t))
805 (forward-line)
806 (should
807 (string=
809 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
810 (org-test-with-temp-text-in-file "
811 #+begin_src emacs-lisp
812 \"some text\";;
813 #+end_src"
814 (org-babel-next-src-block)
815 (org-babel-execute-maybe)
816 (should (re-search-forward "\\#\\+results:" nil t))
817 (forward-line)
818 (should
819 (string=
820 ": some text"
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
827 #+end_src"
828 (org-babel-next-src-block)
829 (org-babel-execute-maybe)
830 (re-search-forward "\\#\\+results:" nil t)
831 (forward-line)
832 (should (string=
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
838 #+end_src"
839 (org-babel-next-src-block)
840 (org-babel-execute-maybe)
841 (re-search-forward "\\#\\+results:" nil t)
842 (forward-line)
843 (should (string=
844 ": 2"
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.
850 (should
851 (org-test-with-temp-text
853 <point>#+BEGIN_SRC emacs-lisp
854 '((1 . nil) (2 . 3))
855 #+END_SRC
857 (org-babel-execute-maybe) t))
858 ;; Escape headlines when producing an example block.
859 (should
860 (string-match-p
861 ",\\* Not an headline"
862 (org-test-with-temp-text
864 <point>#+BEGIN_SRC emacs-lisp
865 \"* Not an headline\"
866 #+END_SRC
868 (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
869 (buffer-string))))
870 ;; Escape special syntax in example blocks.
871 (should
872 (string-match-p
873 ",#\\+END_SRC"
874 (org-test-with-temp-text
876 <point>#+BEGIN_SRC emacs-lisp
877 \"#+END_SRC\"
878 #+END_SRC
880 (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
881 (buffer-string))))
882 ;; No escaping is done with other blocks or raw type.
883 (should-not
884 (string-match-p
885 ",\\* Not an headline"
886 (org-test-with-temp-text
888 <point>#+BEGIN_SRC emacs-lisp
889 \"* Not an headline\"
890 #+END_SRC
892 (let ((org-babel-min-lines-for-block-output 10))
893 (org-babel-execute-maybe))
894 (buffer-string))))
895 (should-not
896 (string-match-p
897 ",\\* Not an headline"
898 (org-test-with-temp-text
900 <point>#+BEGIN_SRC emacs-lisp :results raw
901 \"* Not an headline\"
902 #+END_SRC
904 (org-babel-execute-maybe)
905 (buffer-string))))
906 (should-not
907 (string-match-p
908 ",\\* Not an headline"
909 (org-test-with-temp-text
911 <point>#+BEGIN_SRC emacs-lisp :results drawer
912 \"* Not an headline\"
913 #+END_SRC
915 (org-babel-execute-maybe)
916 (buffer-string)))))
918 (ert-deftest test-ob/remove-inline-result ()
919 "Test `org-babel-remove-inline-result' honors whitespace."
920 (let*
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.
927 inline-sb-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)
949 (insert " ")
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
963 "\n"
964 (concat
965 "* org-babel-remove-result
966 #+begin_src " language "
967 #+end_src
969 * next heading")))
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
977 "- indented
978 #+begin_src sh :results file wrap
979 echo test.txt
980 #+end_src"
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
988 \"foo\"
989 #+end_src
991 #+begin_src emacs-lisp :results file :file-desc
992 \"foo\"
993 #+end_src"
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
1008 echo \"test\"
1009 #+end_src"
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.
1016 (should-not
1017 (org-test-with-temp-text "
1018 <point>#+begin_src shell :results value link :file \"/tmp/test.txt\"
1019 echo \"hello\" > /tmp/test.txt
1020 echo \"test\"
1021 #+end_src"
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
1032 echo \"test\"
1033 #+end_src"
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.
1040 (should-not
1041 (org-test-with-temp-text "
1042 <point>#+begin_src shell :results value graphics :file \"/tmp/test.txt\"
1043 echo \"hello\" > /tmp/test.txt
1044 echo \"test\"
1045 #+end_src"
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
1053 test-line
1054 (forward-char 1)
1055 (org-babel-execute-maybe)
1056 (should (re-search-forward "=\"x\"=" nil t))
1057 (forward-line))))
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
1063 #+end_src"
1064 (org-babel-next-src-block)
1065 (org-babel-execute-maybe)
1066 (re-search-forward "\\#\\+results:" nil t)
1067 (forward-line)
1068 (should (string=
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
1074 #+end_src"
1075 (org-babel-next-src-block)
1076 (org-babel-execute-maybe)
1077 (re-search-forward "\\#\\+results:" nil t)
1078 (forward-line)
1079 (should (string=
1080 ": 2"
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
1091 buffer-text
1092 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
1093 (should (re-search-forward "\\#\\+results:" nil t))
1094 (forward-line)
1095 (should (string= result
1096 (buffer-substring-no-properties
1097 (point-at-bol)
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
1107 "- 1
1109 - 3"
1111 "* org-babel-remove-result
1112 #+begin_src emacs-lisp :results list
1113 '(1 2 3)
1114 #+end_src
1116 * next heading"))
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
1121 ":results:
1122 hello there
1123 :end:"
1125 "* org-babel-remove-result
1127 #+begin_src emacs-lisp :results wrap
1128 \"hello there\"
1129 #+end_src
1131 * next heading"))
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
1136 "#+begin_src org
1137 ,* heading
1138 ,** subheading
1139 content
1140 #+end_src"
1142 "* org-babel-remove-result
1143 #+begin_src emacs-lisp :results org
1144 \"* heading
1145 ,** subheading
1146 content\"
1147 #+end_src
1149 * next heading"))
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>
1156 #+end_export"
1158 "* org-babel-remove-result
1159 #+begin_src emacs-lisp :results html
1160 \"<head><body></body></head>\"
1161 #+end_src
1163 * next heading"))
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
1169 Line 1
1170 Line 2
1171 Line 3
1172 #+end_export"
1174 "* org-babel-remove-result
1175 #+begin_src emacs-lisp :results latex
1176 \"Line 1
1177 Line 2
1178 Line 3\"
1179 #+end_src
1181 * next heading"))
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
1188 \"I am working!\"
1189 #+end_src"
1191 "* org-babel-remove-result
1192 #+begin_src emacs-lisp :results code
1193 (message \"I am working!\")
1194 #+end_src
1196 * next heading"))
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!\")
1206 #+end_src
1208 * next heading"))
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."
1212 (should
1213 (equal "
1214 #+begin_src emacs-lisp
1215 (+ 1 1)
1216 #+end_src
1217 #+results:
1219 * next heading"
1220 (org-test-with-temp-text
1222 <point>#+begin_src emacs-lisp
1223 (+ 1 1)
1224 #+end_src
1225 #+results:
1227 * next heading"
1228 (org-babel-execute-maybe)
1229 (buffer-string)))))
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.
1234 #+name: foo
1235 #+begin_src emacs-lisp
1237 #+end_src
1239 #+name: foo
1240 #+begin_src emacs-lisp
1242 #+end_src
1244 #+name: foo
1245 #+begin_src emacs-lisp
1247 #+end_src
1249 #+RESULTS: foo
1250 : foo
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.
1264 (should
1265 (equal "#+BEGIN_SRC emacs-lisp
1266 \(+ 1 2)
1267 #+END_SRC
1269 #+RESULTS:
1270 : 3\n\n\n"
1271 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1272 \(+ 1 2)
1273 #+END_SRC\n\n\n"
1274 (let ((org-babel-next-src-block "RESULTS"))
1275 (org-babel-execute-src-block))
1276 (buffer-string))))
1277 ;; Do not add spurious blank lines after results.
1278 (should
1279 (equal
1281 - item 1
1283 #+begin_src emacs-lisp
1285 #+end_src
1287 #+RESULTS:
1290 - item 2"
1291 (org-test-with-temp-text "
1292 - item 1
1294 #+begin_src emacs-lisp<point>
1296 #+end_src
1298 - item 2"
1299 (org-babel-execute-src-block)
1300 (buffer-string))))
1301 (should
1302 (equal
1304 - item 1
1306 #+begin_src emacs-lisp
1308 #+end_src
1310 #+RESULTS:
1313 - item 2"
1314 (org-test-with-temp-text "
1315 - item 1
1317 #+begin_src emacs-lisp<point>
1319 #+end_src
1321 #+RESULTS:
1324 - item 2"
1325 (org-babel-execute-src-block)
1326 (buffer-string)))))
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
1331 ;; of the buffer.
1332 (should
1333 (equal
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)))))
1341 (should
1342 (equal
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.
1351 (should
1352 (equal
1353 "#+NAME: test
1354 #+BEGIN_SRC emacs-lisp
1355 \(+ 1 2)
1356 #+END_SRC
1358 #+RESULTS: test
1359 : 3"
1360 (org-test-with-temp-text
1361 "#+NAME: test
1362 #+BEGIN_SRC emacs-lisp
1363 \(+ 1 2)
1364 #+END_SRC
1366 #+RESULTS: test
1368 <point>
1369 Paragraph"
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
1377 "#+NAME: test
1378 #+BEGIN_SRC emacs-lisp
1379 \(+ 1 2)
1380 #+END_SRC
1382 #+RESULTS: test
1385 Paragraph"
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))
1390 (widen)
1391 (should (re-search-forward "^: 3" nil t))))
1393 (ert-deftest test-ob/specific-colnames ()
1394 "Test passing specific column names."
1395 (should
1396 (equal "#+name: input-table
1397 | id | var1 |
1398 |----+------|
1399 | 1 | bar |
1400 | 2 | baz |
1402 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1403 echo \"$data\"
1404 #+end_src
1406 #+RESULTS:
1407 | Rev | Author |
1408 |-----+--------|
1409 | 1 | bar |
1410 | 2 | baz |
1412 (org-test-with-temp-text
1413 "#+name: input-table
1414 | id | var1 |
1415 |----+------|
1416 | 1 | bar |
1417 | 2 | baz |
1419 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1420 echo \"$data\"
1421 #+end_src
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))
1429 (buffer-string)))))
1431 (ert-deftest test-ob/location-of-header-arg-eval ()
1432 "Test location of header argument evaluation."
1433 (org-test-with-temp-text "
1434 #+name: top-block
1435 #+begin_src emacs-lisp :var pt=(point)
1437 #+end_src
1439 #+name: bottom-block
1440 #+begin_src emacs-lisp :var pt=top-block()
1442 #+end_src
1444 ;; the value of the second block should be greater than the first
1445 (should
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."
1456 (should
1457 (equal
1458 '(2 2)
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))))))
1464 (should
1465 (equal
1466 '(2 2)
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.
1474 (should
1475 (equal
1476 '(6 6)
1477 (org-test-with-temp-text
1478 "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
1479 (setq tab-width 4)
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.
1485 (should
1486 (equal
1487 " #+begin_example
1498 #+end_example
1500 (org-test-with-temp-text
1501 " #+begin_src emacs-lisp :results output
1502 (dotimes (i 10) (princ i) (princ \"\\n\"))
1503 #+end_src"
1504 (org-babel-execute-src-block)
1505 (search-forward "begin_example")
1506 (downcase
1507 (buffer-substring-no-properties (line-beginning-position)
1508 (point-max))))))
1509 ;; Properly indent "org" blocks.
1510 (should
1511 (equal
1512 " #+begin_src org
1523 #+end_src
1525 (org-test-with-temp-text
1526 " #+begin_src emacs-lisp :results output org
1527 (dotimes (i 10) (princ i) (princ \"\\n\"))
1528 #+end_src"
1529 (org-babel-execute-src-block)
1530 (search-forward "begin_src org")
1531 (downcase
1532 (buffer-substring-no-properties (line-beginning-position)
1533 (point-max)))))))
1535 (ert-deftest test-ob/safe-header-args ()
1536 "Detect safe and unsafe header args."
1537 (let ((safe-args '((:cache . "foo")
1538 (:results . "output")
1539 (:eval . "never")
1540 (:eval . "query")))
1541 (unsafe-args '((:eval . "yes")
1542 (:results . "output file")
1543 (:foo . "bar")))
1544 (malformed-args '((bar . "foo")
1545 ("foo" . "bar")
1546 :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 "
1560 #+name: foo
1561 #+begin_src emacs-lisp
1562 \"I said\"
1563 #+end_src
1565 #+name: bar
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\")
1570 #+end_src
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))
1582 (insert "haven't ")
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\"
1614 #+end_src"
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\"}")))
1633 ;; ... with commas
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'}")))
1647 ;; ... with commas
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 ()
1704 (should
1705 (equal (org-babel-process-params '((:colname-names)
1706 (:rowname-names)
1707 (:result-params)
1708 (:result-type)
1709 (:var . "\"foo\"")))
1710 '((:var)
1711 (:colname-names)
1712 (:rowname-names)
1713 (:result-params)
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))
1726 ;; Non-export tests
1727 (dolist (pair '(("no" . nil)
1728 ("never" . nil)
1729 ("query" . query)
1730 ("yes" . query)))
1731 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
1732 ;; Export tests
1733 (let ((org-babel-exp-reference-buffer t))
1734 (dolist (pair '(("no" . nil)
1735 ("never" . nil)
1736 ("query" . query)
1737 ("yes" . query)
1738 ("never-export" . nil)
1739 ("no-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))
1744 ;; Non-export tests
1745 (dolist (pair '(("no" . nil)
1746 ("never" . nil)
1747 ("query" . query)
1748 ("yes" . t)))
1749 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
1750 ;; Export tests
1751 (let ((org-babel-exp-reference-buffer t))
1752 (dolist (pair '(("no" . nil)
1753 ("never" . nil)
1754 ("query" . query)
1755 ("yes" . t)
1756 ("never-export" . nil)
1757 ("no-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."
1764 (should
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")
1769 (buffer-string)))))
1770 ;; Preserve block indentation.
1771 (should
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")
1777 (buffer-string)))))
1778 ;; Ignore NEW-BODY global indentation.
1779 (should
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")
1784 (buffer-string)))))
1785 ;; When indentation should be preserved ignore the two rules above.
1786 (should
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")
1793 (buffer-string)))))
1794 (should
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")
1800 (buffer-string)))))
1801 (should
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")
1807 (buffer-string)))))
1808 (should
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."
1818 (should
1819 (= 1
1820 (org-test-with-temp-text "#+results: foo\n: result"
1821 (org-babel-find-named-result "foo"))))
1822 (should-not
1823 (org-test-with-temp-text "#+results: foo\n: result"
1824 (org-babel-find-named-result "bar")))
1825 (should-not
1826 (org-test-with-temp-text "#+results: foobar\n: result"
1827 (org-babel-find-named-result "foo")))
1828 ;; Search is case insensitive.
1829 (should
1830 (org-test-with-temp-text "#+RESULTS: FOO\n: result"
1831 (org-babel-find-named-result "foo")))
1832 ;; Handle hash in results keyword.
1833 (should
1834 (org-test-with-temp-text "#+results[hash]: FOO\n: result"
1835 (org-babel-find-named-result "foo")))
1836 ;; Accept orphaned affiliated keywords.
1837 (should
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.
1844 (should
1845 (equal "#+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.
1851 (should
1852 (equal "#+RESULTS: example"
1853 (org-test-with-temp-text
1855 <point>#+NAME: example
1856 #+BEGIN_SRC emacs-lisp
1857 \(+ 1 1)
1858 #+END_SRC
1860 #+RESULTS: example
1861 : 2"
1862 (goto-char (org-babel-where-is-src-block-result))
1863 (buffer-substring-no-properties (point) (line-end-position)))))
1864 (should
1865 (equal "#+RESULTS: example"
1866 (org-test-with-temp-text
1868 <point>#+NAME: example
1869 #+BEGIN_SRC emacs-lisp
1870 \(+ 1 1)
1871 #+END_SRC
1873 #+RESULTS:
1874 : fake
1876 #+RESULTS: example
1877 : 2"
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.
1881 (should-not
1882 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1883 (org-babel-where-is-src-block-result)))
1884 (should-not
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.
1890 (should
1891 (equal
1892 "#+RESULTS:"
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.
1898 (should
1899 (equal
1900 "#+RESULTS: e"
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.
1908 (should
1909 (equal
1910 "#+RESULTS[bbbb]:"
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))))))
1916 (should
1917 (equal
1918 "#+RESULTS[bbbb]:"
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.
1925 (should
1926 (equal
1927 "#+RESULTS[<2014-03-04 00:41:10> bbbb]:"
1928 (org-test-with-temp-text
1930 <point>#+BEGIN_SRC emacs-lisp
1931 \(+ 1 1)
1932 #+END_SRC
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))))))))
1941 (should
1942 (equal
1943 "#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1944 (org-test-with-temp-text
1946 <point>#+BEGIN_SRC emacs-lisp
1947 \(+ 1 1)
1948 #+END_SRC
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.
1958 (should
1959 (equal
1960 "#+RESULTS[bbbb]:"
1961 (org-test-with-temp-text
1963 <point>#+BEGIN_SRC emacs-lisp
1964 \(+ 1 1)
1965 #+END_SRC
1967 #+RESULTS[aaaa]:
1968 #+NAME: e
1969 : 3"
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
1975 ;; expected hash.
1976 (should
1977 (equal
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")
1981 (buffer-string))))
1982 (should
1983 (equal
1984 "#+RESULTS[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
1994 #+NAME: abc
1995 #+BEGIN_SRC emacs-lisp :results value
1996 (1+ 1)
1997 #+END_SRC
1998 #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
1999 #+BEGIN_SRC emacs-lisp
2000 <<abc>>
2001 #+END_SRC
2003 #+RESULTS: abc
2006 ;; non-existent name
2007 (should-not
2008 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
2009 ;; correct name
2010 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
2011 (should (= 14 (point)))
2012 ;; call line - autocompletion
2013 (forward-line 3)
2014 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
2015 (should (= 14 (point)))
2016 ;; noweb reference - autocompletion
2017 (forward-line 5)
2018 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
2019 (should (= 14 (point)))
2020 ;; at symbol - autocompletion
2021 (forward-line 7)
2022 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
2023 (should (= 14 (point)))
2024 ;; in results - autocompletion
2025 (forward-line 8)
2026 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
2027 (should (= 14 (point)))
2028 (forward-line 9)
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 ()
2033 (should
2034 (= 2
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))))
2038 (should
2039 (= 3
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"))))
2055 (provide 'test-ob)
2057 ;;; test-ob ends here