Merge branch 'maint'
[org-mode/org-tableheadings.git] / testing / lisp / test-ob.el
blob4f0f6be2c8d7fce9c574baa4639461e28225ad3a
1 ;;; test-ob.el --- tests for ob.el
3 ;; Copyright (c) 2010-2015 Eric Schulte
4 ;; Authors: Eric Schulte, Martyn Jago
6 ;; This file is not part of GNU Emacs.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Code:
23 (ert-deftest test-ob/indented-cached-org-bracket-link ()
24 "When the result of a source block is a cached indented link it
25 should still return the link."
26 (should
27 (let ((default-directory temporary-file-directory))
28 (org-test-with-temp-text
30 * Test
31 #+<point>BEGIN_SRC emacs-lisp :file test.txt :cache yes
32 (message \"test\")
33 #+END_SRC"
34 ;; Execute twice as the first time creates the cache.
35 (org-babel-execute-src-block)
36 (string= (expand-file-name "test.txt")
37 (org-babel-execute-src-block))))))
39 (ert-deftest test-ob/multi-line-header-regexp ()
40 (should(equal "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
41 org-babel-multi-line-header-regexp))
42 ;;TODO can be optimised - and what about blah4 blah5 blah6?
43 (should (string-match
44 org-babel-multi-line-header-regexp
45 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
46 (should
47 (equal
48 "blah1 blah2 blah3 \t"
49 (match-string
51 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
53 ;;TODO Check - should this fail?
54 (should
55 (not (org-test-string-exact-match
56 org-babel-multi-line-header-regexp
57 " \t #+headers : blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))))
59 (ert-deftest test-ob/src-block-regexp ()
60 (let ((test-block
61 (concat
62 "#+begin_src language -n-r-a-b -c :argument-1 yes :argument-2 no\n"
63 "echo this is a test\n"
64 "echo Currently in ' $PWD\n"
65 "#+end_src"))
66 (language "language")
67 (flags "-n-r-a-b -c ")
68 (arguments ":argument-1 yes :argument-2 no")
69 (body "echo this is a test\necho Currently in ' $PWD\n"))
70 (should (string-match org-babel-src-block-regexp test-block))
71 (should (string-match org-babel-src-block-regexp (upcase test-block)))
72 (should (equal language (match-string 2 test-block)))
73 ;;TODO Consider refactoring
74 (should (equal flags (match-string 3 test-block)))
75 (should (equal arguments (match-string 4 test-block)))
76 (should (equal body (match-string 5 test-block)))
77 ;;no switches
78 (should (org-test-string-exact-match
79 org-babel-src-block-regexp
80 (replace-regexp-in-string flags "" test-block)))
81 ;;no header arguments
82 (should (org-test-string-exact-match
83 org-babel-src-block-regexp
84 (replace-regexp-in-string arguments "" test-block)))
85 ;; should be valid with no body
86 (should (org-test-string-exact-match
87 org-babel-src-block-regexp
88 (replace-regexp-in-string body "" test-block)))))
90 (ert-deftest test-ob/default-inline-header-args ()
91 (should(equal
92 '((:session . "none")
93 (:results . "replace")
94 (:exports . "results")
95 (:hlines . "yes"))
96 org-babel-default-inline-header-args)))
98 (ert-deftest ob-test/org-babel-combine-header-arg-lists ()
99 (let ((results (org-babel-combine-header-arg-lists
100 '((foo . :any)
101 (bar)
102 (baz . ((foo bar) (baz)))
103 (qux . ((foo bar baz qux)))
104 (quux . ((foo bar))))
105 '((bar)
106 (baz . ((baz)))
107 (quux . :any)))))
108 (dolist (pair '((foo . :any)
109 (bar)
110 (baz . ((baz)))
111 (quux . :any)
112 (qux . ((foo bar baz qux)))))
113 (should (equal (cdr pair)
114 (cdr (assoc (car pair) results)))))))
116 ;;; ob-get-src-block-info
117 (ert-deftest test-ob/get-src-block-info-language ()
118 (org-test-at-marker nil org-test-file-ob-anchor
119 (let ((info (org-babel-get-src-block-info)))
120 (should (string= "emacs-lisp" (nth 0 info))))))
122 (ert-deftest test-ob/get-src-block-info-body ()
123 (org-test-at-marker nil org-test-file-ob-anchor
124 (let ((info (org-babel-get-src-block-info)))
125 (should (string-match (regexp-quote org-test-file-ob-anchor)
126 (nth 1 info))))))
128 (ert-deftest test-ob/get-src-block-info-tangle ()
129 (org-test-at-marker nil org-test-file-ob-anchor
130 (let ((info (org-babel-get-src-block-info)))
131 (should (string= "no" (cdr (assq :tangle (nth 2 info))))))))
133 (ert-deftest test-ob/elisp-in-header-arguments ()
134 "Test execution of elisp forms in header arguments."
135 (org-test-with-temp-text-in-file "
137 * elisp forms in header arguments
138 :PROPERTIES:
139 :header-args: :var prop = (* 7 6)
140 :END:
141 #+begin_src emacs-lisp
142 prop
143 #+end_src"
144 (goto-char (point-min))
145 (org-babel-next-src-block)
146 (should (= 42 (org-babel-execute-src-block)))))
148 (ert-deftest test-ob/simple-named-code-block ()
149 "Test that simple named code blocks can be evaluated."
150 (org-test-with-temp-text-in-file "
152 #+name: i-have-a-name
153 #+begin_src emacs-lisp
155 #+end_src"
156 (org-babel-next-src-block 1)
157 (should (= 42 (org-babel-execute-src-block)))))
159 (ert-deftest test-ob/simple-variable-resolution ()
160 "Test that simple variable resolution is working."
161 (org-test-with-temp-text-in-file "
163 #+name: four
164 #+begin_src emacs-lisp
165 (list 1 2 3 4)
166 #+end_src
168 #+begin_src emacs-lisp :var four=four
169 (length four)
170 #+end_src"
172 (org-babel-next-src-block 2)
173 (should (= 4 (org-babel-execute-src-block)))
174 (forward-line 5)
175 (should (string= ": 4" (buffer-substring
176 (point-at-bol)
177 (point-at-eol))))))
179 (ert-deftest test-ob/multi-line-header-arguments ()
180 "Test that multi-line header arguments and can be read."
181 (org-test-with-temp-text-in-file "
183 #+headers: :var letters='(a b c d e f g)
184 #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
185 (require 'cl-lib)
186 (cl-map 'list #'list numbers letters)
187 #+end_src"
188 (org-babel-next-src-block)
189 (let ((results (org-babel-execute-src-block)))
190 (should (eq 'a (cadr (assoc 1 results))))
191 (should (eq 'd (cadr (assoc 4 results)))))))
193 (ert-deftest test-ob/parse-header-args ()
194 (org-test-with-temp-text-in-file "
196 #+begin_src example-lang :session :results output :var num=9
197 the body
198 #+end_src"
200 (org-babel-next-src-block)
201 (let* ((info (org-babel-get-src-block-info))
202 (params (nth 2 info)))
203 (message "%S" params)
204 (should (equal "example-lang" (nth 0 info)))
205 (should (string= "the body" (org-trim (nth 1 info))))
206 (should-not (member '(:session\ \ \ \ ) params))
207 (should (equal '(:session) (assq :session params)))
208 (should (equal '(:result-type . output) (assq :result-type params)))
209 (should (equal '(num . 9) (cdr (assq :var params)))))))
211 (ert-deftest test-ob/parse-header-args2 ()
212 (org-test-with-temp-text-in-file "
214 * resolving sub-trees as references
216 #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
217 (length text)
218 #+end_src
220 #+begin_src org :noweb yes
221 <<simple-subtree>>
222 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
223 #+end_src
225 ** simple subtree with custom ID
226 :PROPERTIES:
227 :CUSTOM_ID: simple-subtree
228 :END:
229 this is simple"
231 (should (string-match (regexp-quote "this is simple")
232 (org-babel-ref-resolve "simple-subtree")))
233 (org-babel-next-src-block)
234 (should (= 14 (org-babel-execute-src-block)))))
236 (ert-deftest test-ob/inline-src-blocks ()
237 (should
238 (= 1
239 (org-test-with-temp-text
240 "In the middle <point>src_emacs-lisp{(+ 0 1)} of a line"
241 (org-babel-execute-src-block))))
242 (should
243 (= 2
244 (org-test-with-temp-text
245 "One at the end of a line: <point>src_emacs-lisp{(+ 1 1)}"
246 (org-babel-execute-src-block))))
247 (should
248 (= 3
249 (org-test-with-temp-text
250 "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
251 (org-babel-execute-src-block))))
252 (should
253 (= 4
254 (org-test-with-temp-text
255 "In the middle <point>src_emacs-lisp[:results silent\
256 :exports code]{(+ 3 1)} of a line"
257 (org-babel-execute-src-block))))
258 (should
259 (= 5
260 (org-test-with-temp-text
261 "One at the end of a line: <point>src_emacs-lisp[:results silent\
262 :exports code]{(+ 4 1)}"
263 (org-babel-execute-src-block))))
264 (should
265 (= 6
266 (org-test-with-temp-text
267 "src_emacs-lisp[:results silent :exports code]{(+ 5 1)}\
268 at the beginning of a line."
269 (org-babel-execute-src-block))))
270 (should
271 (= 7
272 (org-test-with-temp-text
273 "One also evaluated: <point>src_emacs-lisp[:exports both\
274 :results silent]{(+ 6 1)}"
275 (org-babel-execute-src-block)))))
277 (ert-deftest test-ob/inline-src_blk-default-results-replace-line-1 ()
278 (let ((test-line "src_sh{echo 1}")
279 (org-babel-inline-result-wrap "=%s="))
280 ;; src_ at bol line 1...
281 (org-test-with-temp-text
282 test-line
283 (goto-char (point-min)) (org-babel-execute-maybe)
284 (should (string=
285 (concat test-line " {{{results(=1=)}}}")
286 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
287 (forward-char) (org-babel-execute-maybe)
288 (should (string=
289 (concat test-line " {{{results(=1=)}}}")
290 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
291 (re-search-forward "{{{")
292 ;;(should-error (org-ctrl-c-ctrl-c))
293 (backward-char 4) ;; last char of block body
294 (org-babel-execute-maybe)
295 (should (string=
296 (concat test-line " {{{results(=1=)}}}")
297 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
298 ;; src_ follows space line 1...
299 (let ((test-line " src_emacs-lisp{ 1 }"))
300 (org-test-with-temp-text
301 test-line
302 (should-error (org-ctrl-c-ctrl-c))
303 (forward-char) (org-babel-execute-maybe)
304 (should (string=
305 (concat test-line " {{{results(=1=)}}}")
306 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
307 (re-search-forward "{ 1 ") (org-babel-execute-maybe)
308 (should (string=
309 (concat test-line " {{{results(=1=)}}}")
310 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
311 (forward-char 6)
312 (should-error (org-ctrl-c-ctrl-c))))
313 ;; Results on a subsequent line are replaced.
314 (should
315 (equal
316 "src_emacs-lisp{(+ 1 2)}\n {{{results(=3=)}}}"
317 (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n {{{results(=2=)}}}"
318 (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
319 (buffer-string))))
320 ;; Also handle results at the beginning of a line.
321 (should
322 (equal
323 "src_emacs-lisp{(+ 1 2)}\n{{{results(=3=)}}}"
324 (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n{{{results(=2=)}}}"
325 (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
326 (buffer-string))))))
328 (ert-deftest test-ob/inline-src_blk-default-results-replace-line-2 ()
329 ;; src_ at bol line 2...
330 (let ((test-line " src_emacs-lisp{ \"x\" }")
331 (org-babel-inline-result-wrap "=%s="))
332 (org-test-with-temp-text
333 (concat "\n" test-line)
334 (should-error (org-ctrl-c-ctrl-c))
335 (goto-char (point-min))
336 (should-error (org-ctrl-c-ctrl-c))
337 (forward-line)
338 (should-error (org-ctrl-c-ctrl-c))
339 (forward-char) (org-babel-execute-maybe)
340 (should (string=
341 (concat test-line " {{{results(=x=)}}}")
342 (buffer-substring-no-properties
343 (point-at-bol) (point-at-eol))))))
344 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
345 (org-babel-inline-result-wrap "=%s="))
346 (org-test-with-temp-text
347 test-line
348 (goto-char (point-max))
349 (insert (concat "\n" test-line " end"))
350 (re-search-backward "src") (org-babel-execute-maybe)
351 (should (string=
352 (concat test-line " {{{results(=y=)}}} end")
353 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
354 (re-search-forward "\" ") (org-babel-execute-maybe)
355 (should (string=
356 (concat test-line " {{{results(=y=)}}} end")
357 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
358 (forward-char 3)
359 (should-error (org-ctrl-c-ctrl-c)))))
361 (ert-deftest test-ob/inline-src_blk-manual-results-replace ()
362 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
363 (org-babel-inline-result-wrap "=%s="))
364 (org-test-with-temp-text
365 (concat "\n" test-line)
366 (should-error (org-ctrl-c-ctrl-c))
367 (goto-char (point-max))
368 (org-babel-execute-maybe)
369 (beginning-of-line)
370 (should-error (org-ctrl-c-ctrl-c))
371 (forward-char) (org-babel-execute-maybe)
372 (should (string=
373 (concat test-line " {{{results(=x=)}}}")
374 (buffer-substring-no-properties
375 (point-at-bol) (point-at-eol))))))
376 (let ((test-line (concat " Some text prior to block "
377 "src_emacs-lisp[:results replace]{ \"y\" }"))
378 (org-babel-inline-result-wrap "=%s="))
379 (org-test-with-temp-text test-line
380 (goto-char (point-max))
381 (insert (concat "\n" test-line " end"))
382 (re-search-backward "src") (org-babel-execute-maybe)
383 (should (string=
384 (concat test-line " {{{results(=y=)}}} end")
385 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
386 (re-search-forward "\" ") (org-babel-execute-maybe)
387 (should (string=
388 (concat test-line " {{{results(=y=)}}} end")
389 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
390 (forward-char 3)
391 (should-error (org-ctrl-c-ctrl-c)))))
393 (ert-deftest test-ob/inline-src_blk-results-silent ()
394 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
395 (org-test-with-temp-text test-line
396 (org-babel-execute-maybe)
397 (should (string= test-line
398 (buffer-substring-no-properties
399 (point-at-bol) (point-at-eol))))))
400 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
401 "[ :results silent ]{ \"y\" }")))
402 (org-test-with-temp-text
403 test-line
404 (goto-char (point-max))
405 (insert (concat "\n" test-line " end"))
406 (re-search-backward "src_") (org-babel-execute-maybe)
407 (should (string= (concat test-line " end")
408 (buffer-substring-no-properties
409 (point-at-bol) (point-at-eol))))
410 (re-search-forward "\" ") (org-babel-execute-maybe)
411 (should (string= (concat test-line " end")
412 (buffer-substring-no-properties
413 (point-at-bol) (point-at-eol))))
414 (forward-char 2)
415 (should-error (org-ctrl-c-ctrl-c)))))
417 (ert-deftest test-ob/inline-src_blk-results-raw ()
418 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
419 (org-test-with-temp-text test-line
420 (org-babel-execute-maybe)
421 (should (string= (concat test-line " x")
422 (buffer-string)))))
423 (let ((test-line (concat " Some text prior to block "
424 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
425 (org-test-with-temp-text (concat test-line " end")
426 (re-search-forward "src_") (org-babel-execute-maybe)
427 (should (string= (concat test-line " the end")
428 (buffer-substring-no-properties
429 (point-at-bol) (point-at-eol))))
430 (re-search-forward "\" ") (org-babel-execute-maybe)
431 (should (string= (concat test-line " the the end")
432 (buffer-substring-no-properties
433 (point-at-bol) (point-at-eol))))
434 (forward-char 2)
435 (should-error (org-ctrl-c-ctrl-c)))))
437 (ert-deftest test-ob/inline-src_blk-results-file ()
438 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
439 (org-test-with-temp-text
440 test-line
441 (org-babel-execute-maybe)
442 (should (string= (concat test-line " {{{results([[file:~/test-file]])}}}")
443 (buffer-substring-no-properties
444 (point-min) (point-max)))))))
446 (ert-deftest test-ob/inline-src_blk-results-scalar ()
447 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
448 (org-babel-inline-result-wrap "=%s="))
449 (org-test-with-temp-text
450 test-line
451 (org-babel-execute-maybe)
452 (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
453 (buffer-substring-no-properties
454 (point-min) (point-max)))))))
456 (ert-deftest test-ob/inline-src_blk-results-verbatim ()
457 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
458 (org-babel-inline-result-wrap "=%s="))
459 (org-test-with-temp-text
460 test-line
461 (org-babel-execute-maybe)
462 (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
463 (buffer-substring-no-properties
464 (point-min) (point-max)))))))
466 (ert-deftest test-ob/combining-scalar-and-raw-result-types ()
467 (org-test-with-temp-text-in-file "
469 #+begin_src sh :results scalar
470 echo \"[[file:./cv.cls]]\"
471 #+end_src
473 #+name:
474 : [[file:./cv.cls]]
476 #+begin_src sh :results raw scalar
477 echo \"[[file:./cv.cls]]\"
478 #+end_src
480 (cl-flet ((next-result ()
481 (org-babel-next-src-block)
482 (org-babel-execute-src-block)
483 (goto-char (org-babel-where-is-src-block-result))
484 (forward-line 1)))
485 (goto-char (point-min))
486 (next-result)
487 (should (eq (org-element-type (org-element-at-point)) 'fixed-width))
488 (next-result)
489 (should-not
490 (eq (org-element-type (org-element-at-point)) 'fixed-width)))))
492 (ert-deftest test-ob/no-defaut-value-for-var ()
493 "Test that the absence of a default value for a variable DOES THROW
494 a proper error."
495 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
496 (org-babel-next-src-block)
497 (let ((err
498 (should-error (org-babel-execute-src-block) :type 'error)))
499 (should
500 (equal
501 '(error
502 "Variable \"x\" must be assigned a default value")
503 err)))))
505 (ert-deftest test-ob/just-one-results-block ()
506 "Test that evaluating two times the same code block does not result in a
507 duplicate results block."
508 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
509 (org-babel-execute-src-block)
510 (org-babel-execute-src-block) ; second code block execution
511 (should (search-forward "Hello")) ; the string inside the source code block
512 (should (search-forward "Hello")) ; the same string in the results block
513 (should-error (search-forward "Hello"))))
515 (ert-deftest test-ob/nested-code-block ()
516 "Test nested code blocks inside code blocks don't cause problems."
517 (should
518 (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
519 (org-test-with-temp-text "#+begin_src org :results silent
520 ,#+begin_src emacs-lisp
521 'foo
522 ,#+end_src
523 #+end_src"
524 (let ((org-edit-src-content-indentation 2)
525 (org-src-preserve-indentation nil))
526 (org-babel-execute-src-block))))))
528 (ert-deftest test-ob/partial-nested-code-block ()
529 "Test nested code blocks inside code blocks don't cause problems."
530 (org-test-with-temp-text "#+begin_src org :results silent
531 ,#+begin_src emacs-lisp
532 #+end_src"
533 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
535 (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
536 (org-test-with-temp-text "#+NAME: foo
537 #+BEGIN_SRC emacs-lisp
538 'foo
539 #+END_SRC\n"
540 (org-babel-next-src-block 1)
541 (should (eq 'foo (org-babel-execute-src-block)))
542 (goto-char (point-min))
543 (org-babel-next-src-block 1)
544 (should (looking-at org-babel-src-block-regexp))))
546 (ert-deftest test-ob/catches-all-references ()
547 (org-test-with-temp-text "
548 #+NAME: literal-example
549 #+BEGIN_EXAMPLE
550 A literal example
551 on two lines
552 #+END_EXAMPLE
554 #+NAME: read-literal-example
555 #+BEGIN_SRC emacs-lisp :var x=literal-example
556 (concatenate 'string x \" for me.\")
557 #+END_SRC"
558 (org-babel-next-src-block 1)
559 (should (string= (org-babel-execute-src-block)
560 "A literal example\non two lines\n for me."))))
562 (ert-deftest test-ob/ignore-reference-in-commented-headings ()
563 (should
564 (= 2
565 (org-test-with-temp-text
567 * COMMENT H1
568 #+NAME: n
571 * H2
572 #+NAME: n
575 * Code
577 <point>#+BEGIN_SRC emacs-lisp :var x=n
579 #+END_SRC"
580 (org-babel-execute-src-block)))))
582 (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
583 (org-test-with-temp-text "
584 #+name: base_plus
585 | 1 |
586 | 2 |
588 #+name: base
589 | 3 |
590 | 4 |
592 #+begin_src emacs-lisp :var x=base
594 #+end_src"
595 (org-babel-next-src-block 1)
596 (should (equal (org-babel-execute-src-block) '((3) (4))))))
598 (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
599 (org-test-with-temp-text "
600 #+name: base_plus
601 #+begin_src emacs-lisp
602 'bar
603 #+end_src
605 #+name: base
606 #+begin_src emacs-lisp
607 'foo
608 #+end_src
610 #+begin_src emacs-lisp :var x=base
612 #+end_src"
613 (org-babel-next-src-block 3)
614 (should (equal (org-babel-execute-src-block) "foo"))))
616 (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
617 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
618 (+ a b c d)
619 #+end_src
621 (should (= 10 (org-babel-execute-src-block)))))
623 (ert-deftest test-ob/org-babel-update-intermediate ()
624 (org-test-with-temp-text "#+name: foo
625 #+begin_src emacs-lisp
627 #+end_src
629 #+results: foo
632 #+begin_src emacs-lisp :var it=foo
633 (+ it 1)
634 #+end_src"
635 (let ((org-babel-update-intermediate nil))
636 (goto-char (point-min))
637 (org-babel-next-src-block 2)
638 (should (= 3 (org-babel-execute-src-block)))
639 (goto-char (point-min))
640 (forward-line 6)
641 (should (looking-at ": 4")))
642 (let ((org-babel-update-intermediate t))
643 (goto-char (point-min))
644 (org-babel-next-src-block 2)
645 (should (= 3 (org-babel-execute-src-block)))
646 (goto-char (point-min))
647 (forward-line 6)
648 (should (looking-at ": 2")))))
650 (ert-deftest test-ob/eval-header-argument ()
651 (cl-flet ((check-eval (eval runp)
652 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
653 (setq foo :evald)
654 #+end_src" eval)
655 (let ((foo :not-run))
656 (if runp
657 (progn (should (org-babel-execute-src-block))
658 (should (eq foo :evald)))
659 (progn (should-not (org-babel-execute-src-block))
660 (should-not (eq foo :evald))))))))
661 (check-eval "never" nil)
662 (check-eval "no" nil)
663 (check-eval "never-export" t)
664 (check-eval "no-export" t)
665 (let ((org-babel-exp-reference-buffer (current-buffer)))
666 (check-eval "never" nil)
667 (check-eval "no" nil)
668 (check-eval "never-export" nil)
669 (check-eval "no-export" nil))))
671 (ert-deftest test-ob/noweb-expansion-1 ()
672 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
673 <<foo>>
674 #+end_src
676 #+name: foo
677 #+begin_src sh
679 #+end_src"
680 (should (string= (org-babel-expand-noweb-references) "bar"))))
682 (ert-deftest test-ob/noweb-expansion-2 ()
683 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
684 <<foo>>
685 #+end_src
687 #+name: foo
688 #+begin_src sh :noweb-sep \"\"
690 #+end_src
692 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
694 #+end_src"
695 (should (string= (org-babel-expand-noweb-references) "barbaz"))))
697 (ert-deftest test-ob/splitting-variable-lists-in-references ()
698 (org-test-with-temp-text ""
699 (should (= 1 (length (org-babel-ref-split-args
700 "a=\"this, no work\""))))
701 (should (= 2 (length (org-babel-ref-split-args
702 "a=\"this, no work\", b=1"))))))
704 (ert-deftest test-ob/balanced-split ()
705 "Test `org-babel-balanced-split' specifications."
706 (should (equal
707 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
708 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
709 '((32 9) . 58))))
710 ;; Handle un-balanced parens.
711 (should
712 (equal '(":foo ((6)" "bar 1")
713 (org-babel-balanced-split ":foo ((6) :bar 1" '((32 9) . 58))))
714 (should
715 (equal '(":foo \"(foo\"" "bar 2")
716 (org-babel-balanced-split ":foo \"(foo\" :bar 2" '((32 9) . 58))))
717 ;; Handle un-balanced quotes.
718 (should
719 (equal '(":foo \"1" "bar 3")
720 (org-babel-balanced-split ":foo \"1 :bar 3" '((32 9) . 58))))
721 ;; Handle empty string.
722 (should
723 (equal '(":foo \"\"")
724 (org-babel-balanced-split ":foo \"\"" '((32 9) . 58))))
725 ;; Handle control characters within double quotes.
726 (should
727 (equal '(":foo \"\\n\"")
728 (org-babel-balanced-split ":foo \"\\n\"" '((32 9) . 58)))))
730 (ert-deftest test-ob/commented-last-block-line-no-var ()
731 (org-test-with-temp-text-in-file "
732 #+begin_src emacs-lisp
734 #+end_src"
735 (org-babel-next-src-block)
736 (org-babel-execute-maybe)
737 (should (re-search-forward "\\#\\+results:" nil t))
738 (forward-line)
739 (should
740 (string=
742 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
743 (org-test-with-temp-text-in-file "
744 #+begin_src emacs-lisp
745 \"some text\";;
746 #+end_src"
747 (org-babel-next-src-block)
748 (org-babel-execute-maybe)
749 (should (re-search-forward "\\#\\+results:" nil t))
750 (forward-line)
751 (should
752 (string=
753 ": some text"
754 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
756 (ert-deftest test-ob/commented-last-block-line-with-var ()
757 (org-test-with-temp-text-in-file "
758 #+begin_src emacs-lisp :var a=1
760 #+end_src"
761 (org-babel-next-src-block)
762 (org-babel-execute-maybe)
763 (re-search-forward "\\#\\+results:" nil t)
764 (forward-line)
765 (should (string=
767 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
768 (org-test-with-temp-text-in-file "
769 #+begin_src emacs-lisp :var a=2
771 #+end_src"
772 (org-babel-next-src-block)
773 (org-babel-execute-maybe)
774 (re-search-forward "\\#\\+results:" nil t)
775 (forward-line)
776 (should (string=
777 ": 2"
778 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
780 (ert-deftest test-ob/org-babel-insert-result ()
781 "Test `org-babel-insert-result' specifications."
782 ;; Do not error when output is an improper list.
783 (should
784 (org-test-with-temp-text
786 <point>#+BEGIN_SRC emacs-lisp
787 '((1 . nil) (2 . 3))
788 #+END_SRC
790 (org-babel-execute-maybe) t))
791 ;; Escape headlines when producing an example block.
792 (should
793 (string-match-p
794 ",\\* Not an headline"
795 (org-test-with-temp-text
797 <point>#+BEGIN_SRC emacs-lisp
798 \"* Not an headline\"
799 #+END_SRC
801 (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
802 (buffer-string))))
803 ;; Escape special syntax in example blocks.
804 (should
805 (string-match-p
806 ",#\\+END_SRC"
807 (org-test-with-temp-text
809 <point>#+BEGIN_SRC emacs-lisp
810 \"#+END_SRC\"
811 #+END_SRC
813 (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
814 (buffer-string))))
815 ;; No escaping is done with other blocks or raw type.
816 (should-not
817 (string-match-p
818 ",\\* Not an headline"
819 (org-test-with-temp-text
821 <point>#+BEGIN_SRC emacs-lisp
822 \"* Not an headline\"
823 #+END_SRC
825 (let ((org-babel-min-lines-for-block-output 10))
826 (org-babel-execute-maybe))
827 (buffer-string))))
828 (should-not
829 (string-match-p
830 ",\\* Not an headline"
831 (org-test-with-temp-text
833 <point>#+BEGIN_SRC emacs-lisp :results raw
834 \"* Not an headline\"
835 #+END_SRC
837 (org-babel-execute-maybe)
838 (buffer-string))))
839 (should-not
840 (string-match-p
841 ",\\* Not an headline"
842 (org-test-with-temp-text
844 <point>#+BEGIN_SRC emacs-lisp :results drawer
845 \"* Not an headline\"
846 #+END_SRC
848 (org-babel-execute-maybe)
849 (buffer-string)))))
851 (ert-deftest test-ob/remove-inline-result ()
852 "Test `org-babel-remove-inline-result' honors whitespace."
853 (let*
854 ((inline-sb "src_emacs-lisp{(+ 1 2)}")
855 (inline-res " {{{results(=3=)}}}")
856 (inline-sb-dot (concat inline-sb "."))
857 (inline-sb-res-dot (concat inline-sb inline-res ".")))
858 (org-test-with-temp-text
859 ;; Insert inline_src_block followed by dot.
860 inline-sb-dot
861 ;; Insert result before dot.
862 (org-babel-execute-maybe)
863 (should (string= inline-sb-res-dot
864 (buffer-substring-no-properties
865 (point-at-bol) (point-at-eol))))
866 ;; Delete whitespace and result.
867 (org-babel-remove-inline-result)
868 (should (string= inline-sb-dot
869 (buffer-substring-no-properties
870 (point-at-bol) (point-at-eol))))
871 ;; Add whitespace and result before dot.
872 (search-forward inline-sb)
873 (insert " " inline-res)
874 (goto-char (point-at-bol))
875 ;; Remove whitespace and result.
876 (org-babel-remove-inline-result)
877 (should (string= inline-sb-dot
878 (buffer-substring-no-properties
879 (point-at-bol) (point-at-eol))))
880 ;; Add whitespace before dot.
881 (search-forward inline-sb)
882 (insert " ")
883 (goto-char (point-at-bol))
884 ;; Add result before whitespace.
885 (org-babel-execute-maybe)
886 ;; Remove result - leave trailing whitespace and dot.
887 (org-babel-remove-inline-result)
888 (should (string= (concat inline-sb " .")
889 (buffer-substring-no-properties
890 (point-at-bol) (point-at-eol)))))))
892 (defun test-ob-verify-result-and-removed-result (result buffer-text)
893 "Test helper function to test `org-babel-remove-result'.
894 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
895 and the result of execution is verified against RESULT.
897 The block is actually executed /twice/ to ensure result
898 replacement happens correctly."
899 (org-test-with-temp-text
900 buffer-text
901 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
902 (should (re-search-forward "\\#\\+results:" nil t))
903 (forward-line)
904 (should (string= result
905 (buffer-substring-no-properties
906 (point-at-bol)
907 (- (point-max) 16))))
908 (org-babel-previous-src-block) (org-babel-remove-result)
909 (should (string= buffer-text
910 (buffer-substring-no-properties
911 (point-min) (point-max))))))
913 (ert-deftest test-ob/org-babel-remove-result--results-default ()
914 "Test `org-babel-remove-result' with default :results."
915 (mapcar (lambda (language)
916 (test-ob-verify-result-and-removed-result
917 "\n"
918 (concat
919 "* org-babel-remove-result
920 #+begin_src " language "
921 #+end_src
923 * next heading")))
924 '("sh" "emacs-lisp")))
926 (ert-deftest test-ob/org-babel-remove-result--results-list ()
927 "Test `org-babel-remove-result' with :results list."
928 (test-ob-verify-result-and-removed-result
929 "- 1
932 - (quote (4 5))"
934 "* org-babel-remove-result
935 #+begin_src emacs-lisp :results list
936 '(1 2 3 '(4 5))
937 #+end_src
939 * next heading"))
941 (ert-deftest test-ob/org-babel-results-indented-wrap ()
942 "Ensure that wrapped results are inserted correction when indented.
943 If not inserted correctly then the second evaluation will fail
944 trying to find the :END: marker."
945 (org-test-with-temp-text
946 "- indented
947 #+begin_src sh :results file wrap
948 echo test.txt
949 #+end_src"
950 (org-babel-next-src-block 1)
951 (org-babel-execute-src-block)
952 (org-babel-execute-src-block)))
954 (ert-deftest test-ob/file-desc-header-argument ()
955 "Test that the :file-desc header argument is used."
956 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
957 \"foo\"
958 #+end_src
960 #+begin_src emacs-lisp :results file :file-desc
961 \"foo\"
962 #+end_src"
963 (org-babel-execute-src-block)
964 (org-babel-next-src-block 1)
965 (org-babel-execute-src-block)
966 (goto-char (point-min))
967 (should (search-forward "[[file:foo][bar]]" nil t))
968 (should (search-forward "[[file:foo][foo]]" nil t))))
970 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
971 "Test `org-babel-remove-result' with :results pp."
972 (test-ob-verify-result-and-removed-result
973 ": \"I /am/ working!\""
975 "* org-babel-remove-result
976 #+begin_src emacs-lisp :results pp
977 \"I /am/ working!\")
978 #+end_src
980 * next heading"))
982 (ert-deftest test-ob/inline-src_blk-preceded-punct-preceded-by-point ()
983 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
984 (org-babel-inline-result-wrap "=%s="))
985 (org-test-with-temp-text
986 test-line
987 (forward-char 1)
988 (org-babel-execute-maybe)
989 (should (re-search-forward "=\"x\"=" nil t))
990 (forward-line))))
992 (ert-deftest test-ob/commented-last-block-line-with-var ()
993 (org-test-with-temp-text-in-file "
994 #+begin_src emacs-lisp :var a=1
996 #+end_src"
997 (org-babel-next-src-block)
998 (org-babel-execute-maybe)
999 (re-search-forward "\\#\\+results:" nil t)
1000 (forward-line)
1001 (should (string=
1003 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
1004 (org-test-with-temp-text-in-file "
1005 #+begin_src emacs-lisp :var a=2
1007 #+end_src"
1008 (org-babel-next-src-block)
1009 (org-babel-execute-maybe)
1010 (re-search-forward "\\#\\+results:" nil t)
1011 (forward-line)
1012 (should (string=
1013 ": 2"
1014 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
1016 (defun test-ob-verify-result-and-removed-result (result buffer-text)
1017 "Test helper function to test `org-babel-remove-result'.
1018 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
1019 and the result of execution is verified against RESULT.
1021 The block is actually executed /twice/ to ensure result
1022 replacement happens correctly."
1023 (org-test-with-temp-text
1024 buffer-text
1025 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
1026 (should (re-search-forward "\\#\\+results:" nil t))
1027 (forward-line)
1028 (should (string= result
1029 (buffer-substring-no-properties
1030 (point-at-bol)
1031 (- (point-max) 16))))
1032 (org-babel-previous-src-block) (org-babel-remove-result)
1033 (should (string= buffer-text
1034 (buffer-substring-no-properties
1035 (point-min) (point-max))))))
1037 (ert-deftest test-ob/org-babel-remove-result--results-default ()
1038 "Test `org-babel-remove-result' with default :results."
1039 (mapcar (lambda (language)
1040 (test-ob-verify-result-and-removed-result
1041 "\n"
1042 (concat
1043 "* org-babel-remove-result
1044 #+begin_src " language "
1045 #+end_src
1047 * next heading")))
1048 '("sh" "emacs-lisp")))
1050 (ert-deftest test-ob/org-babel-remove-result--results-list ()
1051 "Test `org-babel-remove-result' with :results list."
1052 (test-ob-verify-result-and-removed-result
1053 "- 1
1056 - (quote (4 5))"
1058 "* org-babel-remove-result
1059 #+begin_src emacs-lisp :results list
1060 '(1 2 3 '(4 5))
1061 #+end_src
1063 * next heading"))
1065 (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
1066 "Test `org-babel-remove-result' with :results wrap."
1067 (test-ob-verify-result-and-removed-result
1068 ":RESULTS:
1069 hello there
1070 :END:"
1072 "* org-babel-remove-result
1074 #+begin_src emacs-lisp :results wrap
1075 \"hello there\"
1076 #+end_src
1078 * next heading"))
1080 (ert-deftest test-ob/org-babel-remove-result--results-org ()
1081 "Test `org-babel-remove-result' with :results org."
1082 (test-ob-verify-result-and-removed-result
1083 "#+BEGIN_SRC org
1084 ,* heading
1085 ,** subheading
1086 content
1087 #+END_SRC"
1089 "* org-babel-remove-result
1090 #+begin_src emacs-lisp :results org
1091 \"* heading
1092 ,** subheading
1093 content\"
1094 #+end_src
1096 * next heading"))
1098 (ert-deftest test-ob/org-babel-remove-result--results-html ()
1099 "Test `org-babel-remove-result' with :results html."
1100 (test-ob-verify-result-and-removed-result
1101 "#+BEGIN_EXPORT html
1102 <head><body></body></head>
1103 #+END_EXPORT"
1105 "* org-babel-remove-result
1106 #+begin_src emacs-lisp :results html
1107 \"<head><body></body></head>\"
1108 #+end_src
1110 * next heading"))
1112 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
1113 "Test `org-babel-remove-result' with :results latex."
1114 (test-ob-verify-result-and-removed-result
1115 "#+BEGIN_EXPORT latex
1116 Line 1
1117 Line 2
1118 Line 3
1119 #+END_EXPORT"
1121 "* org-babel-remove-result
1122 #+begin_src emacs-lisp :results latex
1123 \"Line 1
1124 Line 2
1125 Line 3\"
1126 #+end_src
1128 * next heading"))
1130 (ert-deftest test-ob/org-babel-remove-result--results-code ()
1131 "Test `org-babel-remove-result' with :results code."
1133 (test-ob-verify-result-and-removed-result
1134 "#+BEGIN_SRC emacs-lisp
1135 \"I am working!\"
1136 #+END_SRC"
1138 "* org-babel-remove-result
1139 #+begin_src emacs-lisp :results code
1140 (message \"I am working!\")
1141 #+end_src
1143 * next heading"))
1145 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
1146 "Test `org-babel-remove-result' with :results pp."
1147 (test-ob-verify-result-and-removed-result
1148 ": \"I /am/ working!\""
1150 "* org-babel-remove-result
1151 #+begin_src emacs-lisp :results pp
1152 \"I /am/ working!\")
1153 #+end_src
1155 * next heading"))
1157 (ert-deftest test-ob/results-do-not-replace-code-blocks ()
1158 (org-test-with-temp-text "Block two has a space after the name.
1160 #+name: foo
1161 #+begin_src emacs-lisp
1163 #+end_src
1165 #+name: foo
1166 #+begin_src emacs-lisp
1168 #+end_src
1170 #+name: foo
1171 #+begin_src emacs-lisp
1173 #+end_src
1175 #+RESULTS: foo
1176 : foo
1178 (dolist (num '(1 2 3))
1179 ;; execute the block
1180 (goto-char (point-min))
1181 (org-babel-next-src-block num) (org-babel-execute-src-block)
1182 ;; check the results
1183 (goto-char (point-max))
1184 (move-beginning-of-line 0)
1185 (should (looking-at (format ": %d" num))))))
1187 (ert-deftest test-ob/blocks-with-spaces ()
1188 "Test expansion of blocks followed by blank lines."
1189 ;; Preserve number of blank lines after block.
1190 (should
1191 (equal "#+BEGIN_SRC emacs-lisp
1192 \(+ 1 2)
1193 #+END_SRC
1195 #+RESULTS:
1196 : 3\n\n\n"
1197 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1198 \(+ 1 2)
1199 #+END_SRC\n\n\n"
1200 (let ((org-babel-next-src-block "RESULTS"))
1201 (org-babel-execute-src-block))
1202 (buffer-string))))
1203 ;; Do not add spurious blank lines after results.
1204 (should
1205 (equal
1207 - item 1
1209 #+begin_src emacs-lisp
1211 #+end_src
1213 #+RESULTS:
1216 - item 2"
1217 (org-test-with-temp-text "
1218 - item 1
1220 #+begin_src emacs-lisp<point>
1222 #+end_src
1224 - item 2"
1225 (org-babel-execute-src-block)
1226 (buffer-string))))
1227 (should
1228 (equal
1230 - item 1
1232 #+begin_src emacs-lisp
1234 #+end_src
1236 #+RESULTS:
1239 - item 2"
1240 (org-test-with-temp-text "
1241 - item 1
1243 #+begin_src emacs-lisp<point>
1245 #+end_src
1247 #+RESULTS:
1250 - item 2"
1251 (org-babel-execute-src-block)
1252 (buffer-string)))))
1254 (ert-deftest test-ob/results-in-narrowed-buffer ()
1255 "Test block execution in a narrowed buffer."
1256 ;; If results don't exist, they should be inserted in visible part
1257 ;; of the buffer.
1258 (should
1259 (equal
1260 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1261 (org-test-with-temp-text
1262 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1263 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1264 (let ((org-babel-results-keyword "RESULTS"))
1265 (org-babel-execute-src-block))
1266 (org-trim (buffer-string)))))
1267 (should
1268 (equal
1269 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1270 (org-test-with-temp-text
1271 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1272 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1273 (let ((org-babel-results-keyword "RESULTS"))
1274 (org-babel-execute-src-block))
1275 (org-trim (buffer-string)))))
1276 ;; Results in visible part of buffer, should be updated here.
1277 (should
1278 (equal
1279 "#+NAME: test
1280 #+BEGIN_SRC emacs-lisp
1281 \(+ 1 2)
1282 #+END_SRC
1284 #+RESULTS: test
1285 : 3"
1286 (org-test-with-temp-text
1287 "#+NAME: test
1288 #+BEGIN_SRC emacs-lisp
1289 \(+ 1 2)
1290 #+END_SRC
1292 #+RESULTS: test
1295 Paragraph"
1296 (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
1297 (let ((org-babel-results-keyword "RESULTS"))
1298 (org-babel-execute-src-block))
1299 (org-trim (buffer-string)))))
1300 ;; Results in invisible part of buffer, should be updated there.
1301 (org-test-with-temp-text
1302 "#+NAME: test
1303 #+BEGIN_SRC emacs-lisp
1304 \(+ 1 2)
1305 #+END_SRC
1307 #+RESULTS: test
1310 Paragraph"
1311 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1312 (let ((org-babel-results-keyword "RESULTS"))
1313 (org-babel-execute-src-block))
1314 (should-not (re-search-forward "^#\\+RESULTS:" nil t))
1315 (widen)
1316 (should (re-search-forward "^: 3" nil t))))
1318 (ert-deftest test-ob/specific-colnames ()
1319 "Test passing specific column names."
1320 (should
1321 (equal "#+name: input-table
1322 | id | var1 |
1323 |----+------|
1324 | 1 | bar |
1325 | 2 | baz |
1327 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1328 echo \"$data\"
1329 #+end_src
1331 #+RESULTS:
1332 | Rev | Author |
1333 |-----+--------|
1334 | 1 | bar |
1335 | 2 | baz |
1337 (org-test-with-temp-text
1338 "#+name: input-table
1339 | id | var1 |
1340 |----+------|
1341 | 1 | bar |
1342 | 2 | baz |
1344 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1345 echo \"$data\"
1346 #+end_src
1348 ;; we should find a code block
1349 (should (re-search-forward org-babel-src-block-regexp nil t))
1350 (goto-char (match-beginning 0))
1351 ;; now that we've located the code block, it may be evaluated
1352 (let ((org-babel-execute-src-block "RESULTS"))
1353 (org-babel-execute-src-block))
1354 (buffer-string)))))
1356 (ert-deftest test-ob/location-of-header-arg-eval ()
1357 "Test location of header argument evaluation."
1358 (org-test-with-temp-text "
1359 #+name: top-block
1360 #+begin_src emacs-lisp :var pt=(point)
1362 #+end_src
1364 #+name: bottom-block
1365 #+begin_src emacs-lisp :var pt=top-block()
1367 #+end_src
1369 ;; the value of the second block should be greater than the first
1370 (should
1371 (< (progn (re-search-forward org-babel-src-block-regexp nil t)
1372 (goto-char (match-beginning 0))
1373 (prog1 (save-match-data (org-babel-execute-src-block))
1374 (goto-char (match-end 0))))
1375 (progn (re-search-forward org-babel-src-block-regexp nil t)
1376 (goto-char (match-beginning 0))
1377 (org-babel-execute-src-block))))))
1379 (ert-deftest test-ob/preserve-results-indentation ()
1380 "Preserve indentation when executing a src block."
1381 (should
1382 (equal
1383 '(2 2)
1384 (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1385 (org-babel-execute-src-block)
1386 (let ((case-fold-search t)) (search-forward "RESULTS"))
1387 (list (org-get-indentation)
1388 (progn (forward-line) (org-get-indentation))))))
1389 (should
1390 (equal
1391 '(2 2)
1392 (org-test-with-temp-text
1393 " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1394 (org-babel-execute-src-block)
1395 (let ((case-fold-search t)) (search-forward "RESULTS"))
1396 (list (org-get-indentation)
1397 (progn (forward-line) (org-get-indentation))))))
1398 ;; Don't get fooled by TAB-based indentation.
1399 (should
1400 (equal
1401 '(6 6)
1402 (org-test-with-temp-text
1403 "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
1404 (setq tab-width 4)
1405 (org-babel-execute-src-block)
1406 (let ((case-fold-search t)) (search-forward "RESULTS"))
1407 (list (org-get-indentation)
1408 (progn (forward-line) (org-get-indentation))))))
1409 ;; Properly indent examplified blocks.
1410 (should
1411 (equal
1412 " #+begin_example
1423 #+end_example
1425 (org-test-with-temp-text
1426 " #+begin_src emacs-lisp :results output
1427 (dotimes (i 10) (princ i) (princ \"\\n\"))
1428 #+end_src"
1429 (org-babel-execute-src-block)
1430 (search-forward "begin_example")
1431 (downcase
1432 (buffer-substring-no-properties (line-beginning-position)
1433 (point-max))))))
1434 ;; Properly indent "org" blocks.
1435 (should
1436 (equal
1437 " #+begin_src org
1448 #+end_src
1450 (org-test-with-temp-text
1451 " #+begin_src emacs-lisp :results output org
1452 (dotimes (i 10) (princ i) (princ \"\\n\"))
1453 #+end_src"
1454 (org-babel-execute-src-block)
1455 (search-forward "begin_src org")
1456 (downcase
1457 (buffer-substring-no-properties (line-beginning-position)
1458 (point-max)))))))
1460 (ert-deftest test-ob/safe-header-args ()
1461 "Detect safe and unsafe header args."
1462 (let ((safe-args '((:cache . "foo")
1463 (:results . "output")
1464 (:eval . "never")
1465 (:eval . "query")))
1466 (unsafe-args '((:eval . "yes")
1467 (:results . "output file")
1468 (:foo . "bar")))
1469 (malformed-args '((bar . "foo")
1470 ("foo" . "bar")
1471 :foo))
1472 (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
1473 (dolist (arg safe-args)
1474 (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
1475 (dolist (arg unsafe-args)
1476 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1477 (dolist (arg malformed-args)
1478 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1479 (should (not (funcall safe-p (append safe-args unsafe-args))))))
1481 (ert-deftest test-ob/noweb-expansions-in-cache ()
1482 "Ensure that noweb expansions are expanded before caching."
1483 (let ((noweb-expansions-in-cache-var 0))
1484 (org-test-with-temp-text "
1485 #+name: foo
1486 #+begin_src emacs-lisp
1487 \"I said\"
1488 #+end_src
1490 #+name: bar
1491 #+begin_src emacs-lisp :noweb yes :cache yes
1492 (setq noweb-expansions-in-cache-var
1493 (+ 1 noweb-expansions-in-cache-var))
1494 (concat <<foo>> \" check noweb expansions\")
1495 #+end_src
1497 ;; run the second block to create the cache
1498 (goto-char (point-min))
1499 (re-search-forward (regexp-quote "#+name: bar"))
1500 (should (string= "I said check noweb expansions"
1501 (org-babel-execute-src-block)))
1502 (should (= noweb-expansions-in-cache-var 1))
1503 ;; change the value of the first block
1504 (goto-char (point-min))
1505 (re-search-forward (regexp-quote "said"))
1506 (goto-char (match-beginning 0))
1507 (insert "haven't ")
1508 (re-search-forward (regexp-quote "#+name: bar"))
1509 (should (string= "I haven't said check noweb expansions"
1510 (org-babel-execute-src-block)))
1511 (should (= noweb-expansions-in-cache-var 2)))))
1513 (ert-deftest test-ob/file-ext-and-output-dir ()
1514 (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
1515 (org-babel-next-src-block)
1516 (should (equal "file-ext-basic.txt"
1517 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1518 (org-babel-next-src-block)
1519 (should (equal "foo/file-ext-dir-relative.txt"
1520 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1521 (org-babel-next-src-block)
1522 (should (equal "foo/file-ext-dir-relative-slash.txt"
1523 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1524 (org-babel-next-src-block)
1525 (should (equal "/tmp/file-ext-dir-absolute.txt"
1526 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1527 (org-babel-next-src-block)
1528 (should (equal "foo.bar"
1529 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1530 (org-babel-next-src-block)
1531 (should (equal "xxx/foo.bar"
1532 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1535 (ert-deftest test-ob/script-escape ()
1536 ;; Delimited lists of numbers
1537 (should (equal '(1 2 3)
1538 (org-babel-script-escape "[1 2 3]")))
1539 (should (equal '(1 2 3)
1540 (org-babel-script-escape "{1 2 3}")))
1541 (should (equal '(1 2 3)
1542 (org-babel-script-escape "(1 2 3)")))
1543 ;; Delimited lists of double-quoted strings
1544 (should (equal '("foo" "bar")
1545 (org-babel-script-escape "(\"foo\" \"bar\")")))
1546 (should (equal '("foo" "bar")
1547 (org-babel-script-escape "[\"foo\" \"bar\"]")))
1548 (should (equal '("foo" "bar")
1549 (org-babel-script-escape "{\"foo\" \"bar\"}")))
1550 ;; ... with commas
1551 (should (equal '("foo" "bar")
1552 (org-babel-script-escape "(\"foo\", \"bar\")")))
1553 (should (equal '("foo" "bar")
1554 (org-babel-script-escape "[\"foo\", \"bar\"]")))
1555 (should (equal '("foo" "bar")
1556 (org-babel-script-escape "{\"foo\", \"bar\"}")))
1557 ;; Delimited lists of single-quoted strings
1558 (should (equal '("foo" "bar")
1559 (org-babel-script-escape "('foo' 'bar')")))
1560 (should (equal '("foo" "bar")
1561 (org-babel-script-escape "['foo' 'bar']")))
1562 (should (equal '("foo" "bar")
1563 (org-babel-script-escape "{'foo' 'bar'}")))
1564 ;; ... with commas
1565 (should (equal '("foo" "bar")
1566 (org-babel-script-escape "('foo', 'bar')")))
1567 (should (equal '("foo" "bar")
1568 (org-babel-script-escape "['foo', 'bar']")))
1569 (should (equal '("foo" "bar")
1570 (org-babel-script-escape "{'foo', 'bar'}")))
1571 ;; Single quoted strings
1572 (should (equal "foo"
1573 (org-babel-script-escape "'foo'")))
1574 ;; ... with internal double quote
1575 (should (equal "foo\"bar"
1576 (org-babel-script-escape "'foo\"bar'")))
1577 ;; ... with internal backslash
1578 (should (equal "foo\\bar"
1579 (org-babel-script-escape "'foo\\bar'")))
1580 ;; ... with internal escaped backslash
1581 (should (equal "foo\\bar"
1582 (org-babel-script-escape "'foo\\\\bar'")))
1583 ;; ... with internal backslash-double quote
1584 (should (equal "foo\\\"bar"
1585 (org-babel-script-escape "'foo\\\"bar'")))
1586 ;; ... with internal escaped backslash-double quote
1587 (should (equal "foo\\\"bar"
1588 (org-babel-script-escape "'foo\\\\\"bar'")))
1589 ;; ... with internal escaped single quote
1590 (should (equal "foo'bar"
1591 (org-babel-script-escape "'foo\\'bar'")))
1592 ;; ... with internal escaped backslash-escaped single quote
1593 (should (equal "foo\\'bar"
1594 (org-babel-script-escape "'foo\\\\\\'bar'")))
1595 ;; Double quoted strings
1596 (should (equal "foo"
1597 (org-babel-script-escape "\"foo\"")))
1598 ;; ... with internal single quote
1599 (should (equal "foo'bar"
1600 (org-babel-script-escape "\"foo'bar\"")))
1601 ;; ... with internal backslash
1602 (should (equal "foo\\bar"
1603 (org-babel-script-escape "\"foo\\bar\"")))
1604 ;; ... with internal escaped backslash
1605 (should (equal "foo\\bar"
1606 (org-babel-script-escape "\"foo\\\\bar\"")))
1607 ;; ... with internal backslash-single quote
1608 (should (equal "foo\\'bar"
1609 (org-babel-script-escape "\"foo\\'bar\"")))
1610 ;; ... with internal escaped backslash-single quote
1611 (should (equal "foo\\'bar"
1612 (org-babel-script-escape "\"foo\\\\'bar\"")))
1613 ;; ... with internal escaped double quote
1614 (should (equal "foo\"bar"
1615 (org-babel-script-escape "\"foo\\\"bar\"")))
1616 ;; ... with internal escaped backslash-escaped double quote
1617 (should (equal "foo\\\"bar"
1618 (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
1620 (ert-deftest test-ob/process-params-no-duplicates ()
1621 (should
1622 (equal (org-babel-process-params '((:colname-names)
1623 (:rowname-names)
1624 (:result-params)
1625 (:result-type)
1626 (:var . "\"foo\"")))
1627 '((:var)
1628 (:colname-names)
1629 (:rowname-names)
1630 (:result-params)
1631 (:result-type . value)))))
1633 (defun org-test-babel-confirm-evaluate (eval-value)
1634 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
1636 #+end_src" eval-value)
1637 (goto-char (point-min))
1638 (let ((info (org-babel-get-src-block-info)))
1639 (org-babel-check-confirm-evaluate info))))
1641 (ert-deftest test-ob/check-eval ()
1642 (let ((org-confirm-babel-evaluate t))
1643 ;; Non-export tests
1644 (dolist (pair '(("no" . nil)
1645 ("never" . nil)
1646 ("query" . query)
1647 ("yes" . query)))
1648 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
1649 ;; Export tests
1650 (let ((org-babel-exp-reference-buffer t))
1651 (dolist (pair '(("no" . nil)
1652 ("never" . nil)
1653 ("query" . query)
1654 ("yes" . query)
1655 ("never-export" . nil)
1656 ("no-export" . nil)
1657 ("query-export" . query)))
1658 (message (car pair))
1659 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))))
1660 (let ((org-confirm-babel-evaluate nil))
1661 ;; Non-export tests
1662 (dolist (pair '(("no" . nil)
1663 ("never" . nil)
1664 ("query" . query)
1665 ("yes" . t)))
1666 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
1667 ;; Export tests
1668 (let ((org-babel-exp-reference-buffer t))
1669 (dolist (pair '(("no" . nil)
1670 ("never" . nil)
1671 ("query" . query)
1672 ("yes" . t)
1673 ("never-export" . nil)
1674 ("no-export" . nil)
1675 ("query-export" . query)))
1676 (message (car pair))
1677 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair)))))))
1679 (defun org-test-ob/update-block-body ()
1680 "Test `org-babel-update-block-body' specifications."
1681 (should
1682 (equal "#+begin_src elisp\n 2\n#+end_src"
1683 (let ((org-edit-src-content-indentation 2))
1684 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1685 (org-babel-update-block-body "2")
1686 (buffer-string)))))
1687 ;; Preserve block indentation.
1688 (should
1689 (equal " #+begin_src elisp\n 2\n #+end_src"
1690 (let ((org-edit-src-content-indentation 1))
1691 (org-test-with-temp-text
1692 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1693 (org-babel-update-block-body "2")
1694 (buffer-string)))))
1695 ;; Ignore NEW-BODY global indentation.
1696 (should
1697 (equal "#+begin_src elisp\n 2\n#+end_src"
1698 (let ((org-edit-src-content-indentation 2))
1699 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1700 (org-babel-update-block-body " 2")
1701 (buffer-string)))))
1702 ;; When indentation should be preserved ignore the two rules above.
1703 (should
1704 (equal " #+begin_src elisp\n2\n #+end_src"
1705 (let ((org-edit-src-content-indentation 1)
1706 (org-src-preserve-indentation t))
1707 (org-test-with-temp-text
1708 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1709 (org-babel-update-block-body "2")
1710 (buffer-string)))))
1711 (should
1712 (equal " #+begin_src elisp -i\n2\n #+end_src"
1713 (let ((org-edit-src-content-indentation 1))
1714 (org-test-with-temp-text
1715 " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
1716 (org-babel-update-block-body "2")
1717 (buffer-string)))))
1718 (should
1719 (equal "#+begin_src elisp\n 2\n#+end_src"
1720 (let ((org-edit-src-content-indentation 2)
1721 (org-src-preserve-indentation t))
1722 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1723 (org-babel-update-block-body " 2")
1724 (buffer-string)))))
1725 (should
1726 (equal "#+begin_src elisp -i\n 2\n#+end_src"
1727 (let ((org-edit-src-content-indentation 2)
1728 (org-src-preserve-indentation t))
1729 (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
1730 (org-babel-update-block-body " 2")
1731 (buffer-string))))))
1733 (ert-deftest test-ob/find-named-result ()
1734 "Test `org-babel-find-named-result' specifications."
1735 (should
1736 (= 1
1737 (org-test-with-temp-text "#+results: foo\n: result"
1738 (org-babel-find-named-result "foo"))))
1739 (should-not
1740 (org-test-with-temp-text "#+results: foo\n: result"
1741 (org-babel-find-named-result "bar")))
1742 (should-not
1743 (org-test-with-temp-text "#+results: foobar\n: result"
1744 (org-babel-find-named-result "foo")))
1745 ;; Search is case insensitive.
1746 (should
1747 (org-test-with-temp-text "#+RESULTS: FOO\n: result"
1748 (org-babel-find-named-result "foo")))
1749 ;; Handle hash in results keyword.
1750 (should
1751 (org-test-with-temp-text "#+results[hash]: FOO\n: result"
1752 (org-babel-find-named-result "foo")))
1753 ;; Accept orphaned affiliated keywords.
1754 (should
1755 (org-test-with-temp-text "#+results: foo"
1756 (org-babel-find-named-result "foo"))))
1758 (ert-deftest test-ob/where-is-src-block-result ()
1759 "Test `org-babel-where-is-src-block-result' specifications."
1760 ;; Find anonymous results.
1761 (should
1762 (equal "#+RESULTS:"
1763 (org-test-with-temp-text
1764 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
1765 (goto-char (org-babel-where-is-src-block-result))
1766 (buffer-substring-no-properties (point) (line-end-position)))))
1767 ;; Find named results. Those have priority over anonymous ones.
1768 (should
1769 (equal "#+RESULTS: example"
1770 (org-test-with-temp-text
1772 <point>#+NAME: example
1773 #+BEGIN_SRC emacs-lisp
1774 \(+ 1 1)
1775 #+END_SRC
1777 #+RESULTS: example
1778 : 2"
1779 (goto-char (org-babel-where-is-src-block-result))
1780 (buffer-substring-no-properties (point) (line-end-position)))))
1781 (should
1782 (equal "#+RESULTS: example"
1783 (org-test-with-temp-text
1785 <point>#+NAME: example
1786 #+BEGIN_SRC emacs-lisp
1787 \(+ 1 1)
1788 #+END_SRC
1790 #+RESULTS:
1791 : fake
1793 #+RESULTS: example
1794 : 2"
1795 (goto-char (org-babel-where-is-src-block-result))
1796 (buffer-substring-no-properties (point) (line-end-position)))))
1797 ;; Return nil when no result is found.
1798 (should-not
1799 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1800 (org-babel-where-is-src-block-result)))
1801 (should-not
1802 (org-test-with-temp-text
1803 "- item\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n"
1804 (org-babel-where-is-src-block-result)))
1805 ;; When optional argument INSERT is non-nil, add RESULTS keyword
1806 ;; whenever no RESULTS can be found.
1807 (should
1808 (equal
1809 "#+RESULTS:"
1810 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1811 (let ((org-babel-results-keyword "RESULTS"))
1812 (goto-char (org-babel-where-is-src-block-result t)))
1813 (buffer-substring-no-properties (point) (line-end-position)))))
1814 ;; Insert a named RESULTS keyword if possible.
1815 (should
1816 (equal
1817 "#+RESULTS: e"
1818 (org-test-with-temp-text
1819 "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1820 (let ((org-babel-results-keyword "RESULTS"))
1821 (goto-char (org-babel-where-is-src-block-result t)))
1822 (buffer-substring-no-properties (point) (line-end-position)))))
1823 ;; When optional argument HASH is provided, clear RESULTS keyword
1824 ;; and related contents if they do not match it.
1825 (should
1826 (equal
1827 "#+RESULTS[bbbb]:"
1828 (org-test-with-temp-text
1829 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 3"
1830 (let ((org-babel-results-keyword "RESULTS"))
1831 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
1832 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1833 (should
1834 (equal
1835 "#+RESULTS[bbbb]:"
1836 (org-test-with-temp-text
1837 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
1838 (let ((org-babel-results-keyword "RESULTS"))
1839 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
1840 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1841 ;; Handle hashes with times.
1842 (should
1843 (equal
1844 "#+RESULTS[<2014-03-04 00:41:10> bbbb]:"
1845 (org-test-with-temp-text
1847 <point>#+BEGIN_SRC emacs-lisp
1848 \(+ 1 1)
1849 #+END_SRC
1851 #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1852 (let ((org-babel-results-keyword "RESULTS")
1853 (org-babel-hash-show-time t))
1854 (cl-letf (((symbol-function 'format-time-string)
1855 (lambda (&rest _) "<2014-03-04 00:41:10>")))
1856 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb"))
1857 (org-trim (buffer-substring-no-properties (point) (point-max))))))))
1858 (should
1859 (equal
1860 "#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1861 (org-test-with-temp-text
1863 <point>#+BEGIN_SRC emacs-lisp
1864 \(+ 1 1)
1865 #+END_SRC
1867 #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1868 (let ((org-babel-results-keyword "RESULTS")
1869 (org-babel-hash-show-time t))
1870 (cl-letf (((symbol-function 'format-time-string)
1871 (lambda (&rest _) "<2014-03-04 00:41:10>")))
1872 (goto-char (org-babel-where-is-src-block-result nil nil "aaaa"))
1873 (org-trim (buffer-substring-no-properties (point) (point-max))))))))
1874 ;; RESULTS keyword may not be the last affiliated keyword.
1875 (should
1876 (equal
1877 "#+RESULTS[bbbb]:"
1878 (org-test-with-temp-text
1880 <point>#+BEGIN_SRC emacs-lisp
1881 \(+ 1 1)
1882 #+END_SRC
1884 #+RESULTS[aaaa]:
1885 #+NAME: e
1886 : 3"
1887 (let ((org-babel-results-keyword "RESULTS"))
1888 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
1889 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1890 ;; HASH does nothing if no RESULTS can be found. However, if INSERT
1891 ;; is also non-nil, RESULTS keyword is inserted along with the
1892 ;; expected hash.
1893 (should
1894 (equal
1895 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1896 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1897 (org-babel-where-is-src-block-result nil nil "bbbb")
1898 (buffer-string))))
1899 (should
1900 (equal
1901 "#+RESULTS[bbbb]:"
1902 (org-test-with-temp-text "#+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 nil "bbbb")))
1905 (org-trim (buffer-substring-no-properties (point) (point-max)))))))
1907 (ert-deftest test-ob/goto-named-src-block ()
1908 "Test interactive use of `org-babel-goto-named-src-block'."
1909 (org-test-with-temp-text-in-file
1911 #+NAME: abc
1912 #+BEGIN_SRC emacs-lisp :results value
1913 (1+ 1)
1914 #+END_SRC
1915 #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
1916 #+BEGIN_SRC emacs-lisp
1917 <<abc>>
1918 #+END_SRC
1920 #+RESULTS: abc
1923 ;; non-existent name
1924 (should-not
1925 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
1926 ;; correct name
1927 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
1928 (should (= 14 (point)))
1929 ;; call line - autocompletion
1930 (forward-line 3)
1931 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1932 (should (= 14 (point)))
1933 ;; noweb reference - autocompletion
1934 (forward-line 5)
1935 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1936 (should (= 14 (point)))
1937 ;; at symbol - autocompletion
1938 (forward-line 7)
1939 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1940 (should (= 14 (point)))
1941 ;; in results - autocompletion
1942 (forward-line 8)
1943 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1944 (should (= 14 (point)))
1945 (forward-line 9)
1946 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1947 (should (= 14 (point)))))
1949 (ert-deftest test-ob/evaluate-body-with-coderefs ()
1950 (should
1951 (= 2
1952 (org-test-with-temp-text
1953 "#+begin_src emacs-lisp -l \"#(ref:%s)\"\n2 #(ref:foo)\n#+end_src"
1954 (org-babel-execute-src-block))))
1955 (should
1956 (= 3
1957 (org-test-with-temp-text
1958 "#+begin_src emacs-lisp\n3 #(ref:foo)\n#+end_src"
1959 (let ((org-coderef-label-format "#(ref:%s)"))
1960 (org-babel-execute-src-block))))))
1962 (provide 'test-ob)
1964 ;;; test-ob ends here