org-notify: Fix an error in `org-notify-process' when unconfigured
[org-mode/org-tableheadings.git] / testing / lisp / test-ob.el
blobffc493cf82681e3af8b4f9fde7a14fe500ab11d0
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 ()
672 ;; Standard test.
673 (should
674 (string=
675 "bar"
676 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
677 <<foo>>
678 #+end_src
680 #+name: foo
681 #+begin_src sh
683 #+end_src"
684 (org-babel-expand-noweb-references))))
685 ;; Handle :noweb-sep.
686 (should
687 (string=
688 "barbaz"
689 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
690 <<foo>>
691 #+end_src
693 #+name: foo
694 #+begin_src sh :noweb-sep \"\"
696 #+end_src
698 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
700 #+end_src"
701 (org-babel-expand-noweb-references))))
702 ;; :noweb-ref is extracted from definition, not point of call.
703 (should
704 (string=
705 "(+ 1 1)"
706 (org-test-with-temp-text
708 * Call
709 :PROPERTIES:
710 :header-args: :noweb-ref bar
711 :END:
713 <point>#+begin_src emacs-lisp :results output :tangle yes
714 <<foo>>
715 #+end_src
717 * Evaluation
718 :PROPERTIES:
719 :header-args: :noweb-ref foo
720 :END:
722 #+begin_src sh :noweb-sep \"\"
723 (+ 1 1)
724 #+end_src"
725 (org-babel-expand-noweb-references))))
726 ;; Handle recursive expansion.
727 (should
728 (equal "baz"
729 (org-test-with-temp-text "
730 #+begin_src emacs-lisp :noweb yes<point>
731 <<foo>>
732 #+end_src
734 #+name: foo
735 #+begin_src emacs-lisp :noweb yes
736 <<bar>>
737 #+end_src
739 #+name: bar
740 #+begin_src emacs-lisp
742 #+end_src"
743 (org-babel-expand-noweb-references))))
744 ;; During recursive expansion, obey to `:noweb' property.
745 (should
746 (equal "<<bar>>"
747 (org-test-with-temp-text "
748 #+begin_src emacs-lisp :noweb yes<point>
749 <<foo>>
750 #+end_src
752 #+name: foo
753 #+begin_src emacs-lisp :noweb no
754 <<bar>>
755 #+end_src
757 #+name: bar
758 #+begin_src emacs-lisp
760 #+end_src"
761 (org-babel-expand-noweb-references)))))
763 (ert-deftest test-ob/splitting-variable-lists-in-references ()
764 (org-test-with-temp-text ""
765 (should (= 1 (length (org-babel-ref-split-args
766 "a=\"this, no work\""))))
767 (should (= 2 (length (org-babel-ref-split-args
768 "a=\"this, no work\", b=1"))))))
770 (ert-deftest test-ob/balanced-split ()
771 "Test `org-babel-balanced-split' specifications."
772 (should (equal
773 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
774 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
775 '((32 9) . 58))))
776 ;; Handle un-balanced parens.
777 (should
778 (equal '(":foo ((6)" "bar 1")
779 (org-babel-balanced-split ":foo ((6) :bar 1" '((32 9) . 58))))
780 (should
781 (equal '(":foo \"(foo\"" "bar 2")
782 (org-babel-balanced-split ":foo \"(foo\" :bar 2" '((32 9) . 58))))
783 ;; Handle un-balanced quotes.
784 (should
785 (equal '(":foo \"1" "bar 3")
786 (org-babel-balanced-split ":foo \"1 :bar 3" '((32 9) . 58))))
787 ;; Handle empty string.
788 (should
789 (equal '(":foo \"\"")
790 (org-babel-balanced-split ":foo \"\"" '((32 9) . 58))))
791 ;; Handle control characters within double quotes.
792 (should
793 (equal '(":foo \"\\n\"")
794 (org-babel-balanced-split ":foo \"\\n\"" '((32 9) . 58)))))
796 (ert-deftest test-ob/commented-last-block-line-no-var ()
797 (org-test-with-temp-text-in-file "
798 #+begin_src emacs-lisp
800 #+end_src"
801 (org-babel-next-src-block)
802 (org-babel-execute-maybe)
803 (should (re-search-forward "\\#\\+results:" nil t))
804 (forward-line)
805 (should
806 (string=
808 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
809 (org-test-with-temp-text-in-file "
810 #+begin_src emacs-lisp
811 \"some text\";;
812 #+end_src"
813 (org-babel-next-src-block)
814 (org-babel-execute-maybe)
815 (should (re-search-forward "\\#\\+results:" nil t))
816 (forward-line)
817 (should
818 (string=
819 ": some text"
820 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
822 (ert-deftest test-ob/commented-last-block-line-with-var ()
823 (org-test-with-temp-text-in-file "
824 #+begin_src emacs-lisp :var a=1
826 #+end_src"
827 (org-babel-next-src-block)
828 (org-babel-execute-maybe)
829 (re-search-forward "\\#\\+results:" nil t)
830 (forward-line)
831 (should (string=
833 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
834 (org-test-with-temp-text-in-file "
835 #+begin_src emacs-lisp :var a=2
837 #+end_src"
838 (org-babel-next-src-block)
839 (org-babel-execute-maybe)
840 (re-search-forward "\\#\\+results:" nil t)
841 (forward-line)
842 (should (string=
843 ": 2"
844 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
846 (ert-deftest test-ob/org-babel-insert-result ()
847 "Test `org-babel-insert-result' specifications."
848 ;; Do not error when output is an improper list.
849 (should
850 (org-test-with-temp-text
852 <point>#+BEGIN_SRC emacs-lisp
853 '((1 . nil) (2 . 3))
854 #+END_SRC
856 (org-babel-execute-maybe) t))
857 ;; Escape headlines when producing an example block.
858 (should
859 (string-match-p
860 ",\\* Not an headline"
861 (org-test-with-temp-text
863 <point>#+BEGIN_SRC emacs-lisp
864 \"* Not an headline\"
865 #+END_SRC
867 (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
868 (buffer-string))))
869 ;; Escape special syntax in example blocks.
870 (should
871 (string-match-p
872 ",#\\+END_SRC"
873 (org-test-with-temp-text
875 <point>#+BEGIN_SRC emacs-lisp
876 \"#+END_SRC\"
877 #+END_SRC
879 (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
880 (buffer-string))))
881 ;; No escaping is done with other blocks or raw type.
882 (should-not
883 (string-match-p
884 ",\\* Not an headline"
885 (org-test-with-temp-text
887 <point>#+BEGIN_SRC emacs-lisp
888 \"* Not an headline\"
889 #+END_SRC
891 (let ((org-babel-min-lines-for-block-output 10))
892 (org-babel-execute-maybe))
893 (buffer-string))))
894 (should-not
895 (string-match-p
896 ",\\* Not an headline"
897 (org-test-with-temp-text
899 <point>#+BEGIN_SRC emacs-lisp :results raw
900 \"* Not an headline\"
901 #+END_SRC
903 (org-babel-execute-maybe)
904 (buffer-string))))
905 (should-not
906 (string-match-p
907 ",\\* Not an headline"
908 (org-test-with-temp-text
910 <point>#+BEGIN_SRC emacs-lisp :results drawer
911 \"* Not an headline\"
912 #+END_SRC
914 (org-babel-execute-maybe)
915 (buffer-string)))))
917 (ert-deftest test-ob/remove-inline-result ()
918 "Test `org-babel-remove-inline-result' honors whitespace."
919 (let*
920 ((inline-sb "src_emacs-lisp{(+ 1 2)}")
921 (inline-res " {{{results(=3=)}}}")
922 (inline-sb-dot (concat inline-sb "."))
923 (inline-sb-res-dot (concat inline-sb inline-res ".")))
924 (org-test-with-temp-text
925 ;; Insert inline_src_block followed by dot.
926 inline-sb-dot
927 ;; Insert result before dot.
928 (org-babel-execute-maybe)
929 (should (string= inline-sb-res-dot
930 (buffer-substring-no-properties
931 (point-at-bol) (point-at-eol))))
932 ;; Delete whitespace and result.
933 (org-babel-remove-inline-result)
934 (should (string= inline-sb-dot
935 (buffer-substring-no-properties
936 (point-at-bol) (point-at-eol))))
937 ;; Add whitespace and result before dot.
938 (search-forward inline-sb)
939 (insert " " inline-res)
940 (goto-char (point-at-bol))
941 ;; Remove whitespace and result.
942 (org-babel-remove-inline-result)
943 (should (string= inline-sb-dot
944 (buffer-substring-no-properties
945 (point-at-bol) (point-at-eol))))
946 ;; Add whitespace before dot.
947 (search-forward inline-sb)
948 (insert " ")
949 (goto-char (point-at-bol))
950 ;; Add result before whitespace.
951 (org-babel-execute-maybe)
952 ;; Remove result - leave trailing whitespace and dot.
953 (org-babel-remove-inline-result)
954 (should (string= (concat inline-sb " .")
955 (buffer-substring-no-properties
956 (point-at-bol) (point-at-eol)))))))
958 (ert-deftest test-ob/org-babel-remove-result--results-default ()
959 "Test `org-babel-remove-result' with default :results."
960 (mapcar (lambda (language)
961 (test-ob-verify-result-and-removed-result
962 "\n"
963 (concat
964 "* org-babel-remove-result
965 #+begin_src " language "
966 #+end_src
968 * next heading")))
969 '("sh" "emacs-lisp")))
971 (ert-deftest test-ob/org-babel-results-indented-wrap ()
972 "Ensure that wrapped results are inserted correction when indented.
973 If not inserted correctly then the second evaluation will fail
974 trying to find the :END: marker."
975 (org-test-with-temp-text
976 "- indented
977 #+begin_src sh :results file wrap
978 echo test.txt
979 #+end_src"
980 (org-babel-next-src-block 1)
981 (org-babel-execute-src-block)
982 (org-babel-execute-src-block)))
984 (ert-deftest test-ob/file-desc-header-argument ()
985 "Test that the :file-desc header argument is used."
986 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
987 \"foo\"
988 #+end_src
990 #+begin_src emacs-lisp :results file :file-desc
991 \"foo\"
992 #+end_src"
993 (org-babel-execute-src-block)
994 (org-babel-next-src-block 1)
995 (org-babel-execute-src-block)
996 (goto-char (point-min))
997 (should (search-forward "[[file:foo][bar]]" nil t))
998 (should (search-forward "[[file:foo][foo]]" nil t))))
1000 (ert-deftest test-ob/inline-src_blk-preceded-punct-preceded-by-point ()
1001 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
1002 (org-babel-inline-result-wrap "=%s="))
1003 (org-test-with-temp-text
1004 test-line
1005 (forward-char 1)
1006 (org-babel-execute-maybe)
1007 (should (re-search-forward "=\"x\"=" nil t))
1008 (forward-line))))
1010 (ert-deftest test-ob/commented-last-block-line-with-var ()
1011 (org-test-with-temp-text-in-file "
1012 #+begin_src emacs-lisp :var a=1
1014 #+end_src"
1015 (org-babel-next-src-block)
1016 (org-babel-execute-maybe)
1017 (re-search-forward "\\#\\+results:" nil t)
1018 (forward-line)
1019 (should (string=
1021 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
1022 (org-test-with-temp-text-in-file "
1023 #+begin_src emacs-lisp :var a=2
1025 #+end_src"
1026 (org-babel-next-src-block)
1027 (org-babel-execute-maybe)
1028 (re-search-forward "\\#\\+results:" nil t)
1029 (forward-line)
1030 (should (string=
1031 ": 2"
1032 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
1034 (defun test-ob-verify-result-and-removed-result (result buffer-text)
1035 "Test helper function to test `org-babel-remove-result'.
1036 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
1037 and the result of execution is verified against RESULT.
1039 The block is actually executed /twice/ to ensure result
1040 replacement happens correctly."
1041 (org-test-with-temp-text
1042 buffer-text
1043 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
1044 (should (re-search-forward "\\#\\+results:" nil t))
1045 (forward-line)
1046 (should (string= result
1047 (buffer-substring-no-properties
1048 (point-at-bol)
1049 (- (point-max) 16))))
1050 (org-babel-previous-src-block) (org-babel-remove-result)
1051 (should (string= buffer-text
1052 (buffer-substring-no-properties
1053 (point-min) (point-max))))))
1055 (ert-deftest test-ob/org-babel-remove-result--results-list ()
1056 "Test `org-babel-remove-result' with :results list."
1057 (test-ob-verify-result-and-removed-result
1058 "- 1
1061 - (quote (4 5))"
1063 "* org-babel-remove-result
1064 #+begin_src emacs-lisp :results list
1065 '(1 2 3 '(4 5))
1066 #+end_src
1068 * next heading"))
1070 (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
1071 "Test `org-babel-remove-result' with :results wrap."
1072 (test-ob-verify-result-and-removed-result
1073 ":RESULTS:
1074 hello there
1075 :END:"
1077 "* org-babel-remove-result
1079 #+begin_src emacs-lisp :results wrap
1080 \"hello there\"
1081 #+end_src
1083 * next heading"))
1085 (ert-deftest test-ob/org-babel-remove-result--results-org ()
1086 "Test `org-babel-remove-result' with :results org."
1087 (test-ob-verify-result-and-removed-result
1088 "#+BEGIN_SRC org
1089 ,* heading
1090 ,** subheading
1091 content
1092 #+END_SRC"
1094 "* org-babel-remove-result
1095 #+begin_src emacs-lisp :results org
1096 \"* heading
1097 ,** subheading
1098 content\"
1099 #+end_src
1101 * next heading"))
1103 (ert-deftest test-ob/org-babel-remove-result--results-html ()
1104 "Test `org-babel-remove-result' with :results html."
1105 (test-ob-verify-result-and-removed-result
1106 "#+BEGIN_EXPORT html
1107 <head><body></body></head>
1108 #+END_EXPORT"
1110 "* org-babel-remove-result
1111 #+begin_src emacs-lisp :results html
1112 \"<head><body></body></head>\"
1113 #+end_src
1115 * next heading"))
1117 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
1118 "Test `org-babel-remove-result' with :results latex."
1119 (test-ob-verify-result-and-removed-result
1120 "#+BEGIN_EXPORT latex
1121 Line 1
1122 Line 2
1123 Line 3
1124 #+END_EXPORT"
1126 "* org-babel-remove-result
1127 #+begin_src emacs-lisp :results latex
1128 \"Line 1
1129 Line 2
1130 Line 3\"
1131 #+end_src
1133 * next heading"))
1135 (ert-deftest test-ob/org-babel-remove-result--results-code ()
1136 "Test `org-babel-remove-result' with :results code."
1138 (test-ob-verify-result-and-removed-result
1139 "#+BEGIN_SRC emacs-lisp
1140 \"I am working!\"
1141 #+END_SRC"
1143 "* org-babel-remove-result
1144 #+begin_src emacs-lisp :results code
1145 (message \"I am working!\")
1146 #+end_src
1148 * next heading"))
1150 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
1151 "Test `org-babel-remove-result' with :results pp."
1152 (test-ob-verify-result-and-removed-result
1153 ": \"I /am/ working!\""
1155 "* org-babel-remove-result
1156 #+begin_src emacs-lisp :results pp
1157 \"I /am/ working!\")
1158 #+end_src
1160 * next heading"))
1162 (ert-deftest test-ob/org-babel-remove-result--no-blank-line ()
1163 "Test `org-babel-remove-result' without blank line between code and results."
1164 (should
1165 (equal "
1166 #+begin_src emacs-lisp
1167 (+ 1 1)
1168 #+end_src
1169 #+results:
1171 * next heading"
1172 (org-test-with-temp-text
1174 <point>#+begin_src emacs-lisp
1175 (+ 1 1)
1176 #+end_src
1177 #+results:
1179 * next heading"
1180 (org-babel-execute-maybe)
1181 (buffer-string)))))
1183 (ert-deftest test-ob/results-do-not-replace-code-blocks ()
1184 (org-test-with-temp-text "Block two has a space after the name.
1186 #+name: foo
1187 #+begin_src emacs-lisp
1189 #+end_src
1191 #+name: foo
1192 #+begin_src emacs-lisp
1194 #+end_src
1196 #+name: foo
1197 #+begin_src emacs-lisp
1199 #+end_src
1201 #+RESULTS: foo
1202 : foo
1204 (dolist (num '(1 2 3))
1205 ;; execute the block
1206 (goto-char (point-min))
1207 (org-babel-next-src-block num) (org-babel-execute-src-block)
1208 ;; check the results
1209 (goto-char (point-max))
1210 (move-beginning-of-line 0)
1211 (should (looking-at (format ": %d" num))))))
1213 (ert-deftest test-ob/blocks-with-spaces ()
1214 "Test expansion of blocks followed by blank lines."
1215 ;; Preserve number of blank lines after block.
1216 (should
1217 (equal "#+BEGIN_SRC emacs-lisp
1218 \(+ 1 2)
1219 #+END_SRC
1221 #+RESULTS:
1222 : 3\n\n\n"
1223 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1224 \(+ 1 2)
1225 #+END_SRC\n\n\n"
1226 (let ((org-babel-next-src-block "RESULTS"))
1227 (org-babel-execute-src-block))
1228 (buffer-string))))
1229 ;; Do not add spurious blank lines after results.
1230 (should
1231 (equal
1233 - item 1
1235 #+begin_src emacs-lisp
1237 #+end_src
1239 #+RESULTS:
1242 - item 2"
1243 (org-test-with-temp-text "
1244 - item 1
1246 #+begin_src emacs-lisp<point>
1248 #+end_src
1250 - item 2"
1251 (org-babel-execute-src-block)
1252 (buffer-string))))
1253 (should
1254 (equal
1256 - item 1
1258 #+begin_src emacs-lisp
1260 #+end_src
1262 #+RESULTS:
1265 - item 2"
1266 (org-test-with-temp-text "
1267 - item 1
1269 #+begin_src emacs-lisp<point>
1271 #+end_src
1273 #+RESULTS:
1276 - item 2"
1277 (org-babel-execute-src-block)
1278 (buffer-string)))))
1280 (ert-deftest test-ob/results-in-narrowed-buffer ()
1281 "Test block execution in a narrowed buffer."
1282 ;; If results don't exist, they should be inserted in visible part
1283 ;; of the buffer.
1284 (should
1285 (equal
1286 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1287 (org-test-with-temp-text
1288 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1289 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1290 (let ((org-babel-results-keyword "RESULTS"))
1291 (org-babel-execute-src-block))
1292 (org-trim (buffer-string)))))
1293 (should
1294 (equal
1295 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1296 (org-test-with-temp-text
1297 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1298 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1299 (let ((org-babel-results-keyword "RESULTS"))
1300 (org-babel-execute-src-block))
1301 (org-trim (buffer-string)))))
1302 ;; Results in visible part of buffer, should be updated here.
1303 (should
1304 (equal
1305 "#+NAME: test
1306 #+BEGIN_SRC emacs-lisp
1307 \(+ 1 2)
1308 #+END_SRC
1310 #+RESULTS: test
1311 : 3"
1312 (org-test-with-temp-text
1313 "#+NAME: test
1314 #+BEGIN_SRC emacs-lisp
1315 \(+ 1 2)
1316 #+END_SRC
1318 #+RESULTS: test
1320 <point>
1321 Paragraph"
1322 (narrow-to-region (point-min) (point))
1323 (goto-char (point-min))
1324 (let ((org-babel-results-keyword "RESULTS"))
1325 (org-babel-execute-src-block))
1326 (org-trim (buffer-string)))))
1327 ;; Results in invisible part of buffer, should be updated there.
1328 (org-test-with-temp-text
1329 "#+NAME: test
1330 #+BEGIN_SRC emacs-lisp
1331 \(+ 1 2)
1332 #+END_SRC
1334 #+RESULTS: test
1337 Paragraph"
1338 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1339 (let ((org-babel-results-keyword "RESULTS"))
1340 (org-babel-execute-src-block))
1341 (should-not (re-search-forward "^#\\+RESULTS:" nil t))
1342 (widen)
1343 (should (re-search-forward "^: 3" nil t))))
1345 (ert-deftest test-ob/specific-colnames ()
1346 "Test passing specific column names."
1347 (should
1348 (equal "#+name: input-table
1349 | id | var1 |
1350 |----+------|
1351 | 1 | bar |
1352 | 2 | baz |
1354 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1355 echo \"$data\"
1356 #+end_src
1358 #+RESULTS:
1359 | Rev | Author |
1360 |-----+--------|
1361 | 1 | bar |
1362 | 2 | baz |
1364 (org-test-with-temp-text
1365 "#+name: input-table
1366 | id | var1 |
1367 |----+------|
1368 | 1 | bar |
1369 | 2 | baz |
1371 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1372 echo \"$data\"
1373 #+end_src
1375 ;; we should find a code block
1376 (should (re-search-forward org-babel-src-block-regexp nil t))
1377 (goto-char (match-beginning 0))
1378 ;; now that we've located the code block, it may be evaluated
1379 (let ((org-babel-execute-src-block "RESULTS"))
1380 (org-babel-execute-src-block))
1381 (buffer-string)))))
1383 (ert-deftest test-ob/location-of-header-arg-eval ()
1384 "Test location of header argument evaluation."
1385 (org-test-with-temp-text "
1386 #+name: top-block
1387 #+begin_src emacs-lisp :var pt=(point)
1389 #+end_src
1391 #+name: bottom-block
1392 #+begin_src emacs-lisp :var pt=top-block()
1394 #+end_src
1396 ;; the value of the second block should be greater than the first
1397 (should
1398 (< (progn (re-search-forward org-babel-src-block-regexp nil t)
1399 (goto-char (match-beginning 0))
1400 (prog1 (save-match-data (org-babel-execute-src-block))
1401 (goto-char (match-end 0))))
1402 (progn (re-search-forward org-babel-src-block-regexp nil t)
1403 (goto-char (match-beginning 0))
1404 (org-babel-execute-src-block))))))
1406 (ert-deftest test-ob/preserve-results-indentation ()
1407 "Preserve indentation when executing a src block."
1408 (should
1409 (equal
1410 '(2 2)
1411 (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1412 (org-babel-execute-src-block)
1413 (let ((case-fold-search t)) (search-forward "RESULTS"))
1414 (list (org-get-indentation)
1415 (progn (forward-line) (org-get-indentation))))))
1416 (should
1417 (equal
1418 '(2 2)
1419 (org-test-with-temp-text
1420 " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1421 (org-babel-execute-src-block)
1422 (let ((case-fold-search t)) (search-forward "RESULTS"))
1423 (list (org-get-indentation)
1424 (progn (forward-line) (org-get-indentation))))))
1425 ;; Don't get fooled by TAB-based indentation.
1426 (should
1427 (equal
1428 '(6 6)
1429 (org-test-with-temp-text
1430 "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
1431 (setq tab-width 4)
1432 (org-babel-execute-src-block)
1433 (let ((case-fold-search t)) (search-forward "RESULTS"))
1434 (list (org-get-indentation)
1435 (progn (forward-line) (org-get-indentation))))))
1436 ;; Properly indent examplified blocks.
1437 (should
1438 (equal
1439 " #+begin_example
1450 #+end_example
1452 (org-test-with-temp-text
1453 " #+begin_src emacs-lisp :results output
1454 (dotimes (i 10) (princ i) (princ \"\\n\"))
1455 #+end_src"
1456 (org-babel-execute-src-block)
1457 (search-forward "begin_example")
1458 (downcase
1459 (buffer-substring-no-properties (line-beginning-position)
1460 (point-max))))))
1461 ;; Properly indent "org" blocks.
1462 (should
1463 (equal
1464 " #+begin_src org
1475 #+end_src
1477 (org-test-with-temp-text
1478 " #+begin_src emacs-lisp :results output org
1479 (dotimes (i 10) (princ i) (princ \"\\n\"))
1480 #+end_src"
1481 (org-babel-execute-src-block)
1482 (search-forward "begin_src org")
1483 (downcase
1484 (buffer-substring-no-properties (line-beginning-position)
1485 (point-max)))))))
1487 (ert-deftest test-ob/safe-header-args ()
1488 "Detect safe and unsafe header args."
1489 (let ((safe-args '((:cache . "foo")
1490 (:results . "output")
1491 (:eval . "never")
1492 (:eval . "query")))
1493 (unsafe-args '((:eval . "yes")
1494 (:results . "output file")
1495 (:foo . "bar")))
1496 (malformed-args '((bar . "foo")
1497 ("foo" . "bar")
1498 :foo))
1499 (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
1500 (dolist (arg safe-args)
1501 (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
1502 (dolist (arg unsafe-args)
1503 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1504 (dolist (arg malformed-args)
1505 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1506 (should (not (funcall safe-p (append safe-args unsafe-args))))))
1508 (ert-deftest test-ob/noweb-expansions-in-cache ()
1509 "Ensure that noweb expansions are expanded before caching."
1510 (let ((noweb-expansions-in-cache-var 0))
1511 (org-test-with-temp-text "
1512 #+name: foo
1513 #+begin_src emacs-lisp
1514 \"I said\"
1515 #+end_src
1517 #+name: bar
1518 #+begin_src emacs-lisp :noweb yes :cache yes
1519 (setq noweb-expansions-in-cache-var
1520 (+ 1 noweb-expansions-in-cache-var))
1521 (concat <<foo>> \" check noweb expansions\")
1522 #+end_src
1524 ;; run the second block to create the cache
1525 (goto-char (point-min))
1526 (re-search-forward (regexp-quote "#+name: bar"))
1527 (should (string= "I said check noweb expansions"
1528 (org-babel-execute-src-block)))
1529 (should (= noweb-expansions-in-cache-var 1))
1530 ;; change the value of the first block
1531 (goto-char (point-min))
1532 (re-search-forward (regexp-quote "said"))
1533 (goto-char (match-beginning 0))
1534 (insert "haven't ")
1535 (re-search-forward (regexp-quote "#+name: bar"))
1536 (should (string= "I haven't said check noweb expansions"
1537 (org-babel-execute-src-block)))
1538 (should (= noweb-expansions-in-cache-var 2)))))
1540 (ert-deftest test-ob/file-ext-and-output-dir ()
1541 (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
1542 (org-babel-next-src-block)
1543 (should (equal "file-ext-basic.txt"
1544 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1545 (org-babel-next-src-block)
1546 (should (equal "foo/file-ext-dir-relative.txt"
1547 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1548 (org-babel-next-src-block)
1549 (should (equal "foo/file-ext-dir-relative-slash.txt"
1550 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1551 (org-babel-next-src-block)
1552 (should (equal "/tmp/file-ext-dir-absolute.txt"
1553 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1554 (org-babel-next-src-block)
1555 (should (equal "foo.bar"
1556 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1557 (org-babel-next-src-block)
1558 (should (equal "xxx/foo.bar"
1559 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1562 (ert-deftest test-ob/script-escape ()
1563 ;; Delimited lists of numbers
1564 (should (equal '(1 2 3)
1565 (org-babel-script-escape "[1 2 3]")))
1566 (should (equal '(1 2 3)
1567 (org-babel-script-escape "{1 2 3}")))
1568 (should (equal '(1 2 3)
1569 (org-babel-script-escape "(1 2 3)")))
1570 ;; Delimited lists of double-quoted strings
1571 (should (equal '("foo" "bar")
1572 (org-babel-script-escape "(\"foo\" \"bar\")")))
1573 (should (equal '("foo" "bar")
1574 (org-babel-script-escape "[\"foo\" \"bar\"]")))
1575 (should (equal '("foo" "bar")
1576 (org-babel-script-escape "{\"foo\" \"bar\"}")))
1577 ;; ... with commas
1578 (should (equal '("foo" "bar")
1579 (org-babel-script-escape "(\"foo\", \"bar\")")))
1580 (should (equal '("foo" "bar")
1581 (org-babel-script-escape "[\"foo\", \"bar\"]")))
1582 (should (equal '("foo" "bar")
1583 (org-babel-script-escape "{\"foo\", \"bar\"}")))
1584 ;; Delimited lists of single-quoted strings
1585 (should (equal '("foo" "bar")
1586 (org-babel-script-escape "('foo' 'bar')")))
1587 (should (equal '("foo" "bar")
1588 (org-babel-script-escape "['foo' 'bar']")))
1589 (should (equal '("foo" "bar")
1590 (org-babel-script-escape "{'foo' 'bar'}")))
1591 ;; ... with commas
1592 (should (equal '("foo" "bar")
1593 (org-babel-script-escape "('foo', 'bar')")))
1594 (should (equal '("foo" "bar")
1595 (org-babel-script-escape "['foo', 'bar']")))
1596 (should (equal '("foo" "bar")
1597 (org-babel-script-escape "{'foo', 'bar'}")))
1598 ;; Single quoted strings
1599 (should (equal "foo"
1600 (org-babel-script-escape "'foo'")))
1601 ;; ... with internal double quote
1602 (should (equal "foo\"bar"
1603 (org-babel-script-escape "'foo\"bar'")))
1604 ;; ... with internal backslash
1605 (should (equal "foo\\bar"
1606 (org-babel-script-escape "'foo\\bar'")))
1607 ;; ... with internal escaped backslash
1608 (should (equal "foo\\bar"
1609 (org-babel-script-escape "'foo\\\\bar'")))
1610 ;; ... with internal backslash-double quote
1611 (should (equal "foo\\\"bar"
1612 (org-babel-script-escape "'foo\\\"bar'")))
1613 ;; ... with internal escaped backslash-double quote
1614 (should (equal "foo\\\"bar"
1615 (org-babel-script-escape "'foo\\\\\"bar'")))
1616 ;; ... with internal escaped single quote
1617 (should (equal "foo'bar"
1618 (org-babel-script-escape "'foo\\'bar'")))
1619 ;; ... with internal escaped backslash-escaped single quote
1620 (should (equal "foo\\'bar"
1621 (org-babel-script-escape "'foo\\\\\\'bar'")))
1622 ;; Double quoted strings
1623 (should (equal "foo"
1624 (org-babel-script-escape "\"foo\"")))
1625 ;; ... with internal single quote
1626 (should (equal "foo'bar"
1627 (org-babel-script-escape "\"foo'bar\"")))
1628 ;; ... with internal backslash
1629 (should (equal "foo\\bar"
1630 (org-babel-script-escape "\"foo\\bar\"")))
1631 ;; ... with internal escaped backslash
1632 (should (equal "foo\\bar"
1633 (org-babel-script-escape "\"foo\\\\bar\"")))
1634 ;; ... with internal backslash-single quote
1635 (should (equal "foo\\'bar"
1636 (org-babel-script-escape "\"foo\\'bar\"")))
1637 ;; ... with internal escaped backslash-single quote
1638 (should (equal "foo\\'bar"
1639 (org-babel-script-escape "\"foo\\\\'bar\"")))
1640 ;; ... with internal escaped double quote
1641 (should (equal "foo\"bar"
1642 (org-babel-script-escape "\"foo\\\"bar\"")))
1643 ;; ... with internal escaped backslash-escaped double quote
1644 (should (equal "foo\\\"bar"
1645 (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
1647 (ert-deftest test-ob/process-params-no-duplicates ()
1648 (should
1649 (equal (org-babel-process-params '((:colname-names)
1650 (:rowname-names)
1651 (:result-params)
1652 (:result-type)
1653 (:var . "\"foo\"")))
1654 '((:var)
1655 (:colname-names)
1656 (:rowname-names)
1657 (:result-params)
1658 (:result-type . value)))))
1660 (defun org-test-babel-confirm-evaluate (eval-value)
1661 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
1663 #+end_src" eval-value)
1664 (goto-char (point-min))
1665 (let ((info (org-babel-get-src-block-info)))
1666 (org-babel-check-confirm-evaluate info))))
1668 (ert-deftest test-ob/check-eval ()
1669 (let ((org-confirm-babel-evaluate t))
1670 ;; Non-export tests
1671 (dolist (pair '(("no" . nil)
1672 ("never" . nil)
1673 ("query" . query)
1674 ("yes" . query)))
1675 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
1676 ;; Export tests
1677 (let ((org-babel-exp-reference-buffer t))
1678 (dolist (pair '(("no" . nil)
1679 ("never" . nil)
1680 ("query" . query)
1681 ("yes" . query)
1682 ("never-export" . nil)
1683 ("no-export" . nil)
1684 ("query-export" . query)))
1685 (message (car pair))
1686 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))))
1687 (let ((org-confirm-babel-evaluate nil))
1688 ;; Non-export tests
1689 (dolist (pair '(("no" . nil)
1690 ("never" . nil)
1691 ("query" . query)
1692 ("yes" . t)))
1693 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
1694 ;; Export tests
1695 (let ((org-babel-exp-reference-buffer t))
1696 (dolist (pair '(("no" . nil)
1697 ("never" . nil)
1698 ("query" . query)
1699 ("yes" . t)
1700 ("never-export" . nil)
1701 ("no-export" . nil)
1702 ("query-export" . query)))
1703 (message (car pair))
1704 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair)))))))
1706 (defun org-test-ob/update-block-body ()
1707 "Test `org-babel-update-block-body' specifications."
1708 (should
1709 (equal "#+begin_src elisp\n 2\n#+end_src"
1710 (let ((org-edit-src-content-indentation 2))
1711 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1712 (org-babel-update-block-body "2")
1713 (buffer-string)))))
1714 ;; Preserve block indentation.
1715 (should
1716 (equal " #+begin_src elisp\n 2\n #+end_src"
1717 (let ((org-edit-src-content-indentation 1))
1718 (org-test-with-temp-text
1719 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1720 (org-babel-update-block-body "2")
1721 (buffer-string)))))
1722 ;; Ignore NEW-BODY global indentation.
1723 (should
1724 (equal "#+begin_src elisp\n 2\n#+end_src"
1725 (let ((org-edit-src-content-indentation 2))
1726 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1727 (org-babel-update-block-body " 2")
1728 (buffer-string)))))
1729 ;; When indentation should be preserved ignore the two rules above.
1730 (should
1731 (equal " #+begin_src elisp\n2\n #+end_src"
1732 (let ((org-edit-src-content-indentation 1)
1733 (org-src-preserve-indentation t))
1734 (org-test-with-temp-text
1735 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1736 (org-babel-update-block-body "2")
1737 (buffer-string)))))
1738 (should
1739 (equal " #+begin_src elisp -i\n2\n #+end_src"
1740 (let ((org-edit-src-content-indentation 1))
1741 (org-test-with-temp-text
1742 " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
1743 (org-babel-update-block-body "2")
1744 (buffer-string)))))
1745 (should
1746 (equal "#+begin_src elisp\n 2\n#+end_src"
1747 (let ((org-edit-src-content-indentation 2)
1748 (org-src-preserve-indentation t))
1749 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1750 (org-babel-update-block-body " 2")
1751 (buffer-string)))))
1752 (should
1753 (equal "#+begin_src elisp -i\n 2\n#+end_src"
1754 (let ((org-edit-src-content-indentation 2)
1755 (org-src-preserve-indentation t))
1756 (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
1757 (org-babel-update-block-body " 2")
1758 (buffer-string))))))
1760 (ert-deftest test-ob/find-named-result ()
1761 "Test `org-babel-find-named-result' specifications."
1762 (should
1763 (= 1
1764 (org-test-with-temp-text "#+results: foo\n: result"
1765 (org-babel-find-named-result "foo"))))
1766 (should-not
1767 (org-test-with-temp-text "#+results: foo\n: result"
1768 (org-babel-find-named-result "bar")))
1769 (should-not
1770 (org-test-with-temp-text "#+results: foobar\n: result"
1771 (org-babel-find-named-result "foo")))
1772 ;; Search is case insensitive.
1773 (should
1774 (org-test-with-temp-text "#+RESULTS: FOO\n: result"
1775 (org-babel-find-named-result "foo")))
1776 ;; Handle hash in results keyword.
1777 (should
1778 (org-test-with-temp-text "#+results[hash]: FOO\n: result"
1779 (org-babel-find-named-result "foo")))
1780 ;; Accept orphaned affiliated keywords.
1781 (should
1782 (org-test-with-temp-text "#+results: foo"
1783 (org-babel-find-named-result "foo"))))
1785 (ert-deftest test-ob/where-is-src-block-result ()
1786 "Test `org-babel-where-is-src-block-result' specifications."
1787 ;; Find anonymous results.
1788 (should
1789 (equal "#+RESULTS:"
1790 (org-test-with-temp-text
1791 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
1792 (goto-char (org-babel-where-is-src-block-result))
1793 (buffer-substring-no-properties (point) (line-end-position)))))
1794 ;; Find named results. Those have priority over anonymous ones.
1795 (should
1796 (equal "#+RESULTS: example"
1797 (org-test-with-temp-text
1799 <point>#+NAME: example
1800 #+BEGIN_SRC emacs-lisp
1801 \(+ 1 1)
1802 #+END_SRC
1804 #+RESULTS: example
1805 : 2"
1806 (goto-char (org-babel-where-is-src-block-result))
1807 (buffer-substring-no-properties (point) (line-end-position)))))
1808 (should
1809 (equal "#+RESULTS: example"
1810 (org-test-with-temp-text
1812 <point>#+NAME: example
1813 #+BEGIN_SRC emacs-lisp
1814 \(+ 1 1)
1815 #+END_SRC
1817 #+RESULTS:
1818 : fake
1820 #+RESULTS: example
1821 : 2"
1822 (goto-char (org-babel-where-is-src-block-result))
1823 (buffer-substring-no-properties (point) (line-end-position)))))
1824 ;; Return nil when no result is found.
1825 (should-not
1826 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1827 (org-babel-where-is-src-block-result)))
1828 (should-not
1829 (org-test-with-temp-text
1830 "- item\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n"
1831 (org-babel-where-is-src-block-result)))
1832 ;; When optional argument INSERT is non-nil, add RESULTS keyword
1833 ;; whenever no RESULTS can be found.
1834 (should
1835 (equal
1836 "#+RESULTS:"
1837 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1838 (let ((org-babel-results-keyword "RESULTS"))
1839 (goto-char (org-babel-where-is-src-block-result t)))
1840 (buffer-substring-no-properties (point) (line-end-position)))))
1841 ;; Insert a named RESULTS keyword if possible.
1842 (should
1843 (equal
1844 "#+RESULTS: e"
1845 (org-test-with-temp-text
1846 "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1847 (let ((org-babel-results-keyword "RESULTS"))
1848 (goto-char (org-babel-where-is-src-block-result t)))
1849 (buffer-substring-no-properties (point) (line-end-position)))))
1850 ;; When optional argument HASH is provided, clear RESULTS keyword
1851 ;; and related contents if they do not match it.
1852 (should
1853 (equal
1854 "#+RESULTS[bbbb]:"
1855 (org-test-with-temp-text
1856 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 3"
1857 (let ((org-babel-results-keyword "RESULTS"))
1858 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
1859 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1860 (should
1861 (equal
1862 "#+RESULTS[bbbb]:"
1863 (org-test-with-temp-text
1864 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
1865 (let ((org-babel-results-keyword "RESULTS"))
1866 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
1867 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1868 ;; Handle hashes with times.
1869 (should
1870 (equal
1871 "#+RESULTS[<2014-03-04 00:41:10> bbbb]:"
1872 (org-test-with-temp-text
1874 <point>#+BEGIN_SRC emacs-lisp
1875 \(+ 1 1)
1876 #+END_SRC
1878 #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1879 (let ((org-babel-results-keyword "RESULTS")
1880 (org-babel-hash-show-time t))
1881 (cl-letf (((symbol-function 'format-time-string)
1882 (lambda (&rest _) "<2014-03-04 00:41:10>")))
1883 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb"))
1884 (org-trim (buffer-substring-no-properties (point) (point-max))))))))
1885 (should
1886 (equal
1887 "#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1888 (org-test-with-temp-text
1890 <point>#+BEGIN_SRC emacs-lisp
1891 \(+ 1 1)
1892 #+END_SRC
1894 #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1895 (let ((org-babel-results-keyword "RESULTS")
1896 (org-babel-hash-show-time t))
1897 (cl-letf (((symbol-function 'format-time-string)
1898 (lambda (&rest _) "<2014-03-04 00:41:10>")))
1899 (goto-char (org-babel-where-is-src-block-result nil nil "aaaa"))
1900 (org-trim (buffer-substring-no-properties (point) (point-max))))))))
1901 ;; RESULTS keyword may not be the last affiliated keyword.
1902 (should
1903 (equal
1904 "#+RESULTS[bbbb]:"
1905 (org-test-with-temp-text
1907 <point>#+BEGIN_SRC emacs-lisp
1908 \(+ 1 1)
1909 #+END_SRC
1911 #+RESULTS[aaaa]:
1912 #+NAME: e
1913 : 3"
1914 (let ((org-babel-results-keyword "RESULTS"))
1915 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
1916 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1917 ;; HASH does nothing if no RESULTS can be found. However, if INSERT
1918 ;; is also non-nil, RESULTS keyword is inserted along with the
1919 ;; expected hash.
1920 (should
1921 (equal
1922 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1923 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1924 (org-babel-where-is-src-block-result nil nil "bbbb")
1925 (buffer-string))))
1926 (should
1927 (equal
1928 "#+RESULTS[bbbb]:"
1929 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1930 (let ((org-babel-results-keyword "RESULTS"))
1931 (goto-char (org-babel-where-is-src-block-result t nil "bbbb")))
1932 (org-trim (buffer-substring-no-properties (point) (point-max)))))))
1934 (ert-deftest test-ob/goto-named-src-block ()
1935 "Test interactive use of `org-babel-goto-named-src-block'."
1936 (org-test-with-temp-text-in-file
1938 #+NAME: abc
1939 #+BEGIN_SRC emacs-lisp :results value
1940 (1+ 1)
1941 #+END_SRC
1942 #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
1943 #+BEGIN_SRC emacs-lisp
1944 <<abc>>
1945 #+END_SRC
1947 #+RESULTS: abc
1950 ;; non-existent name
1951 (should-not
1952 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
1953 ;; correct name
1954 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
1955 (should (= 14 (point)))
1956 ;; call line - autocompletion
1957 (forward-line 3)
1958 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1959 (should (= 14 (point)))
1960 ;; noweb reference - autocompletion
1961 (forward-line 5)
1962 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1963 (should (= 14 (point)))
1964 ;; at symbol - autocompletion
1965 (forward-line 7)
1966 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1967 (should (= 14 (point)))
1968 ;; in results - autocompletion
1969 (forward-line 8)
1970 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1971 (should (= 14 (point)))
1972 (forward-line 9)
1973 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1974 (should (= 14 (point)))))
1976 (ert-deftest test-ob/evaluate-body-with-coderefs ()
1977 (should
1978 (= 2
1979 (org-test-with-temp-text
1980 "#+begin_src emacs-lisp -l \"#(ref:%s)\"\n2 #(ref:foo)\n#+end_src"
1981 (org-babel-execute-src-block))))
1982 (should
1983 (= 3
1984 (org-test-with-temp-text
1985 "#+begin_src emacs-lisp\n3 #(ref:foo)\n#+end_src"
1986 (let ((org-coderef-label-format "#(ref:%s)"))
1987 (org-babel-execute-src-block))))))
1989 (provide 'test-ob)
1991 ;;; test-ob ends here