Fix opening custom ID links with percent escaped syntax
[org-mode/org-tableheadings.git] / testing / lisp / test-ob.el
blob095f66a764024afa689b1e546885128150a6dcd9
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 (eval-and-compile (require 'cl-lib))
25 (ert-deftest test-ob/indented-cached-org-bracket-link ()
26 "When the result of a source block is a cached indented link it
27 should still return the link."
28 (should
29 (let ((default-directory temporary-file-directory))
30 (org-test-with-temp-text
32 * Test
33 #+<point>BEGIN_SRC emacs-lisp :file test.txt :cache yes
34 (message \"test\")
35 #+END_SRC"
36 ;; Execute twice as the first time creates the cache.
37 (org-babel-execute-src-block)
38 (string= (expand-file-name "test.txt")
39 (org-babel-execute-src-block))))))
41 (ert-deftest test-ob/multi-line-header-regexp ()
42 (should(equal "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
43 org-babel-multi-line-header-regexp))
44 ;;TODO can be optimised - and what about blah4 blah5 blah6?
45 (should (string-match
46 org-babel-multi-line-header-regexp
47 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
48 (should
49 (equal
50 "blah1 blah2 blah3 \t"
51 (match-string
53 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
55 ;;TODO Check - should this fail?
56 (should
57 (not (org-test-string-exact-match
58 org-babel-multi-line-header-regexp
59 " \t #+headers : blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))))
61 (ert-deftest test-ob/src-block-regexp ()
62 (let ((test-block
63 (concat
64 "#+begin_src language -n-r-a-b -c :argument-1 yes :argument-2 no\n"
65 "echo this is a test\n"
66 "echo Currently in ' $PWD\n"
67 "#+end_src"))
68 (language "language")
69 (flags "-n-r-a-b -c ")
70 (arguments ":argument-1 yes :argument-2 no")
71 (body "echo this is a test\necho Currently in ' $PWD\n"))
72 (should (string-match org-babel-src-block-regexp test-block))
73 (should (string-match org-babel-src-block-regexp (upcase test-block)))
74 (should (equal language (match-string 2 test-block)))
75 ;;TODO Consider refactoring
76 (should (equal flags (match-string 3 test-block)))
77 (should (equal arguments (match-string 4 test-block)))
78 (should (equal body (match-string 5 test-block)))
79 ;;no switches
80 (should (org-test-string-exact-match
81 org-babel-src-block-regexp
82 (replace-regexp-in-string flags "" test-block)))
83 ;;no header arguments
84 (should (org-test-string-exact-match
85 org-babel-src-block-regexp
86 (replace-regexp-in-string arguments "" test-block)))
87 ;; should be valid with no body
88 (should (org-test-string-exact-match
89 org-babel-src-block-regexp
90 (replace-regexp-in-string body "" test-block)))))
92 (ert-deftest test-ob/default-inline-header-args ()
93 (should(equal
94 '((:session . "none")
95 (:results . "replace")
96 (:exports . "results")
97 (:hlines . "yes"))
98 org-babel-default-inline-header-args)))
100 (ert-deftest ob-test/org-babel-combine-header-arg-lists ()
101 (let ((results (org-babel-combine-header-arg-lists
102 '((foo . :any)
103 (bar)
104 (baz . ((foo bar) (baz)))
105 (qux . ((foo bar baz qux)))
106 (quux . ((foo bar))))
107 '((bar)
108 (baz . ((baz)))
109 (quux . :any)))))
110 (dolist (pair '((foo . :any)
111 (bar)
112 (baz . ((baz)))
113 (quux . :any)
114 (qux . ((foo bar baz qux)))))
115 (should (equal (cdr pair)
116 (cdr (assoc (car pair) results)))))))
118 ;;; ob-get-src-block-info
119 (ert-deftest test-ob/get-src-block-info-language ()
120 (org-test-at-marker nil org-test-file-ob-anchor
121 (let ((info (org-babel-get-src-block-info)))
122 (should (string= "emacs-lisp" (nth 0 info))))))
124 (ert-deftest test-ob/get-src-block-info-body ()
125 (org-test-at-marker nil org-test-file-ob-anchor
126 (let ((info (org-babel-get-src-block-info)))
127 (should (string-match (regexp-quote org-test-file-ob-anchor)
128 (nth 1 info))))))
130 (ert-deftest test-ob/get-src-block-info-tangle ()
131 (org-test-at-marker nil org-test-file-ob-anchor
132 (let ((info (org-babel-get-src-block-info)))
133 (should (string= "no" (cdr (assq :tangle (nth 2 info))))))))
135 (ert-deftest test-ob/elisp-in-header-arguments ()
136 "Test execution of elisp forms in header arguments."
137 (org-test-with-temp-text-in-file "
139 * elisp forms in header arguments
140 :PROPERTIES:
141 :header-args: :var prop = (* 7 6)
142 :END:
143 #+begin_src emacs-lisp
144 prop
145 #+end_src"
146 (goto-char (point-min))
147 (org-babel-next-src-block)
148 (should (= 42 (org-babel-execute-src-block)))))
150 (ert-deftest test-ob/simple-named-code-block ()
151 "Test that simple named code blocks can be evaluated."
152 (org-test-with-temp-text-in-file "
154 #+name: i-have-a-name
155 #+begin_src emacs-lisp
157 #+end_src"
158 (org-babel-next-src-block 1)
159 (should (= 42 (org-babel-execute-src-block)))))
161 (ert-deftest test-ob/simple-variable-resolution ()
162 "Test that simple variable resolution is working."
163 (org-test-with-temp-text-in-file "
165 #+name: four
166 #+begin_src emacs-lisp
167 (list 1 2 3 4)
168 #+end_src
170 #+begin_src emacs-lisp :var four=four
171 (length four)
172 #+end_src"
174 (org-babel-next-src-block 2)
175 (should (= 4 (org-babel-execute-src-block)))
176 (forward-line 5)
177 (should (string= ": 4" (buffer-substring
178 (point-at-bol)
179 (point-at-eol))))))
181 (ert-deftest test-ob/multi-line-header-arguments ()
182 "Test that multi-line header arguments and can be read."
183 (org-test-with-temp-text-in-file "
185 #+headers: :var letters='(a b c d e f g)
186 #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
187 (require 'cl-lib)
188 (cl-map 'list #'list numbers letters)
189 #+end_src"
190 (org-babel-next-src-block)
191 (let ((results (org-babel-execute-src-block)))
192 (should (eq 'a (cadr (assoc 1 results))))
193 (should (eq 'd (cadr (assoc 4 results)))))))
195 (ert-deftest test-ob/parse-header-args ()
196 (org-test-with-temp-text-in-file "
198 #+begin_src example-lang :session :results output :var num=9
199 the body
200 #+end_src"
202 (org-babel-next-src-block)
203 (let* ((info (org-babel-get-src-block-info))
204 (params (nth 2 info)))
205 (message "%S" params)
206 (should (equal "example-lang" (nth 0 info)))
207 (should (string= "the body" (org-trim (nth 1 info))))
208 (should-not (member '(:session\ \ \ \ ) params))
209 (should (equal '(:session) (assq :session params)))
210 (should (equal '(:result-type . output) (assq :result-type params)))
211 (should (equal '(num . 9) (cdr (assq :var params)))))))
213 (ert-deftest test-ob/parse-header-args2 ()
214 (org-test-with-temp-text-in-file "
216 * resolving sub-trees as references
218 #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
219 (length text)
220 #+end_src
222 #+begin_src org :noweb yes
223 <<simple-subtree>>
224 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
225 #+end_src
227 ** simple subtree with custom ID
228 :PROPERTIES:
229 :CUSTOM_ID: simple-subtree
230 :END:
231 this is simple"
233 (should (string-match (regexp-quote "this is simple")
234 (org-babel-ref-resolve "simple-subtree")))
235 (org-babel-next-src-block)
236 (should (= 14 (org-babel-execute-src-block)))))
238 (ert-deftest test-ob/inline-src-blocks ()
239 (should
240 (= 1
241 (org-test-with-temp-text
242 "In the middle <point>src_emacs-lisp{(+ 0 1)} of a line"
243 (org-babel-execute-src-block))))
244 (should
245 (= 2
246 (org-test-with-temp-text
247 "One at the end of a line: <point>src_emacs-lisp{(+ 1 1)}"
248 (org-babel-execute-src-block))))
249 (should
250 (= 3
251 (org-test-with-temp-text
252 "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
253 (org-babel-execute-src-block))))
254 (should
255 (= 4
256 (org-test-with-temp-text
257 "In the middle <point>src_emacs-lisp[:results silent\
258 :exports code]{(+ 3 1)} of a line"
259 (org-babel-execute-src-block))))
260 (should
261 (= 5
262 (org-test-with-temp-text
263 "One at the end of a line: <point>src_emacs-lisp[:results silent\
264 :exports code]{(+ 4 1)}"
265 (org-babel-execute-src-block))))
266 (should
267 (= 6
268 (org-test-with-temp-text
269 "src_emacs-lisp[:results silent :exports code]{(+ 5 1)}\
270 at the beginning of a line."
271 (org-babel-execute-src-block))))
272 (should
273 (= 7
274 (org-test-with-temp-text
275 "One also evaluated: <point>src_emacs-lisp[:exports both\
276 :results silent]{(+ 6 1)}"
277 (org-babel-execute-src-block)))))
279 (ert-deftest test-ob/inline-src_blk-default-results-replace-line-1 ()
280 (let ((test-line "src_sh{echo 1}")
281 (org-babel-inline-result-wrap "=%s="))
282 ;; src_ at bol line 1...
283 (org-test-with-temp-text
284 test-line
285 (goto-char (point-min)) (org-babel-execute-maybe)
286 (should (string=
287 (concat test-line " {{{results(=1=)}}}")
288 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
289 (forward-char) (org-babel-execute-maybe)
290 (should (string=
291 (concat test-line " {{{results(=1=)}}}")
292 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
293 (re-search-forward "{{{")
294 ;;(should-error (org-ctrl-c-ctrl-c))
295 (backward-char 4) ;; last char of block body
296 (org-babel-execute-maybe)
297 (should (string=
298 (concat test-line " {{{results(=1=)}}}")
299 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
300 ;; src_ follows space line 1...
301 (let ((test-line " src_emacs-lisp{ 1 }"))
302 (org-test-with-temp-text
303 test-line
304 (should-error (org-ctrl-c-ctrl-c))
305 (forward-char) (org-babel-execute-maybe)
306 (should (string=
307 (concat test-line " {{{results(=1=)}}}")
308 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
309 (re-search-forward "{ 1 ") (org-babel-execute-maybe)
310 (should (string=
311 (concat test-line " {{{results(=1=)}}}")
312 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
313 (forward-char 6)
314 (should-error (org-ctrl-c-ctrl-c))))
315 ;; Results on a subsequent line are replaced.
316 (should
317 (equal
318 "src_emacs-lisp{(+ 1 2)}\n {{{results(=3=)}}}"
319 (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n {{{results(=2=)}}}"
320 (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
321 (buffer-string))))
322 ;; Also handle results at the beginning of a line.
323 (should
324 (equal
325 "src_emacs-lisp{(+ 1 2)}\n{{{results(=3=)}}}"
326 (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n{{{results(=2=)}}}"
327 (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
328 (buffer-string))))))
330 (ert-deftest test-ob/inline-src_blk-default-results-replace-line-2 ()
331 ;; src_ at bol line 2...
332 (let ((test-line " src_emacs-lisp{ \"x\" }")
333 (org-babel-inline-result-wrap "=%s="))
334 (org-test-with-temp-text
335 (concat "\n" test-line)
336 (should-error (org-ctrl-c-ctrl-c))
337 (goto-char (point-min))
338 (should-error (org-ctrl-c-ctrl-c))
339 (forward-line)
340 (should-error (org-ctrl-c-ctrl-c))
341 (forward-char) (org-babel-execute-maybe)
342 (should (string=
343 (concat test-line " {{{results(=x=)}}}")
344 (buffer-substring-no-properties
345 (point-at-bol) (point-at-eol))))))
346 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
347 (org-babel-inline-result-wrap "=%s="))
348 (org-test-with-temp-text
349 test-line
350 (goto-char (point-max))
351 (insert (concat "\n" test-line " end"))
352 (re-search-backward "src") (org-babel-execute-maybe)
353 (should (string=
354 (concat test-line " {{{results(=y=)}}} end")
355 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
356 (re-search-forward "\" ") (org-babel-execute-maybe)
357 (should (string=
358 (concat test-line " {{{results(=y=)}}} end")
359 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
360 (forward-char 3)
361 (should-error (org-ctrl-c-ctrl-c)))))
363 (ert-deftest test-ob/inline-src_blk-manual-results-replace ()
364 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
365 (org-babel-inline-result-wrap "=%s="))
366 (org-test-with-temp-text
367 (concat "\n" test-line)
368 (should-error (org-ctrl-c-ctrl-c))
369 (goto-char (point-max))
370 (org-babel-execute-maybe)
371 (beginning-of-line)
372 (should-error (org-ctrl-c-ctrl-c))
373 (forward-char) (org-babel-execute-maybe)
374 (should (string=
375 (concat test-line " {{{results(=x=)}}}")
376 (buffer-substring-no-properties
377 (point-at-bol) (point-at-eol))))))
378 (let ((test-line (concat " Some text prior to block "
379 "src_emacs-lisp[:results replace]{ \"y\" }"))
380 (org-babel-inline-result-wrap "=%s="))
381 (org-test-with-temp-text test-line
382 (goto-char (point-max))
383 (insert (concat "\n" test-line " end"))
384 (re-search-backward "src") (org-babel-execute-maybe)
385 (should (string=
386 (concat test-line " {{{results(=y=)}}} end")
387 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
388 (re-search-forward "\" ") (org-babel-execute-maybe)
389 (should (string=
390 (concat test-line " {{{results(=y=)}}} end")
391 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
392 (forward-char 3)
393 (should-error (org-ctrl-c-ctrl-c)))))
395 (ert-deftest test-ob/inline-src_blk-results-silent ()
396 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
397 (org-test-with-temp-text test-line
398 (org-babel-execute-maybe)
399 (should (string= test-line
400 (buffer-substring-no-properties
401 (point-at-bol) (point-at-eol))))))
402 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
403 "[ :results silent ]{ \"y\" }")))
404 (org-test-with-temp-text
405 test-line
406 (goto-char (point-max))
407 (insert (concat "\n" test-line " end"))
408 (re-search-backward "src_") (org-babel-execute-maybe)
409 (should (string= (concat test-line " end")
410 (buffer-substring-no-properties
411 (point-at-bol) (point-at-eol))))
412 (re-search-forward "\" ") (org-babel-execute-maybe)
413 (should (string= (concat test-line " end")
414 (buffer-substring-no-properties
415 (point-at-bol) (point-at-eol))))
416 (forward-char 2)
417 (should-error (org-ctrl-c-ctrl-c)))))
419 (ert-deftest test-ob/inline-src_blk-results-raw ()
420 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
421 (org-test-with-temp-text test-line
422 (org-babel-execute-maybe)
423 (should (string= (concat test-line " x")
424 (buffer-string)))))
425 (let ((test-line (concat " Some text prior to block "
426 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
427 (org-test-with-temp-text (concat test-line " end")
428 (re-search-forward "src_") (org-babel-execute-maybe)
429 (should (string= (concat test-line " the end")
430 (buffer-substring-no-properties
431 (point-at-bol) (point-at-eol))))
432 (re-search-forward "\" ") (org-babel-execute-maybe)
433 (should (string= (concat test-line " the the end")
434 (buffer-substring-no-properties
435 (point-at-bol) (point-at-eol))))
436 (forward-char 2)
437 (should-error (org-ctrl-c-ctrl-c)))))
439 (ert-deftest test-ob/inline-src_blk-results-file ()
440 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
441 (org-test-with-temp-text
442 test-line
443 (org-babel-execute-maybe)
444 (should (string= (concat test-line " {{{results([[file:~/test-file]])}}}")
445 (buffer-substring-no-properties
446 (point-min) (point-max)))))))
448 (ert-deftest test-ob/inline-src_blk-results-scalar ()
449 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
450 (org-babel-inline-result-wrap "=%s="))
451 (org-test-with-temp-text
452 test-line
453 (org-babel-execute-maybe)
454 (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
455 (buffer-substring-no-properties
456 (point-min) (point-max)))))))
458 (ert-deftest test-ob/inline-src_blk-results-verbatim ()
459 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
460 (org-babel-inline-result-wrap "=%s="))
461 (org-test-with-temp-text
462 test-line
463 (org-babel-execute-maybe)
464 (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
465 (buffer-substring-no-properties
466 (point-min) (point-max)))))))
468 (ert-deftest test-ob/combining-scalar-and-raw-result-types ()
469 (org-test-with-temp-text-in-file "
471 #+begin_src sh :results scalar
472 echo \"[[file:./cv.cls]]\"
473 #+end_src
475 #+name:
476 : [[file:./cv.cls]]
478 #+begin_src sh :results raw scalar
479 echo \"[[file:./cv.cls]]\"
480 #+end_src
482 (cl-flet ((next-result ()
483 (org-babel-next-src-block)
484 (org-babel-execute-src-block)
485 (goto-char (org-babel-where-is-src-block-result))
486 (forward-line 1)))
487 (goto-char (point-min))
488 (next-result)
489 (should (eq (org-element-type (org-element-at-point)) 'fixed-width))
490 (next-result)
491 (should-not
492 (eq (org-element-type (org-element-at-point)) 'fixed-width)))))
494 (ert-deftest test-ob/no-defaut-value-for-var ()
495 "Test that the absence of a default value for a variable DOES THROW
496 a proper error."
497 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
498 (org-babel-next-src-block)
499 (let ((err
500 (should-error (org-babel-execute-src-block) :type 'error)))
501 (should
502 (equal
503 '(error
504 "Variable \"x\" must be assigned a default value")
505 err)))))
507 (ert-deftest test-ob/just-one-results-block ()
508 "Test that evaluating two times the same code block does not result in a
509 duplicate results block."
510 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
511 (org-babel-execute-src-block)
512 (org-babel-execute-src-block) ; second code block execution
513 (should (search-forward "Hello")) ; the string inside the source code block
514 (should (search-forward "Hello")) ; the same string in the results block
515 (should-error (search-forward "Hello"))))
517 (ert-deftest test-ob/nested-code-block ()
518 "Test nested code blocks inside code blocks don't cause problems."
519 (should
520 (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
521 (org-test-with-temp-text "#+begin_src org :results silent
522 ,#+begin_src emacs-lisp
523 'foo
524 ,#+end_src
525 #+end_src"
526 (let ((org-edit-src-content-indentation 2)
527 (org-src-preserve-indentation nil))
528 (org-babel-execute-src-block))))))
530 (ert-deftest test-ob/partial-nested-code-block ()
531 "Test nested code blocks inside code blocks don't cause problems."
532 (org-test-with-temp-text "#+begin_src org :results silent
533 ,#+begin_src emacs-lisp
534 #+end_src"
535 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
537 (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
538 (org-test-with-temp-text "#+NAME: foo
539 #+BEGIN_SRC emacs-lisp
540 'foo
541 #+END_SRC\n"
542 (org-babel-next-src-block 1)
543 (should (eq 'foo (org-babel-execute-src-block)))
544 (goto-char (point-min))
545 (org-babel-next-src-block 1)
546 (should (looking-at org-babel-src-block-regexp))))
548 (ert-deftest test-ob/catches-all-references ()
549 (org-test-with-temp-text "
550 #+NAME: literal-example
551 #+BEGIN_EXAMPLE
552 A literal example
553 on two lines
554 #+END_EXAMPLE
556 #+NAME: read-literal-example
557 #+BEGIN_SRC emacs-lisp :var x=literal-example
558 (cl-concatenate 'string x \" for me.\")
559 #+END_SRC"
560 (org-babel-next-src-block 1)
561 (should (string= (org-babel-execute-src-block)
562 "A literal example\non two lines\n for me."))))
564 (ert-deftest test-ob/ignore-reference-in-commented-headings ()
565 (should
566 (= 2
567 (org-test-with-temp-text
569 * COMMENT H1
570 #+NAME: n
573 * H2
574 #+NAME: n
577 * Code
579 <point>#+BEGIN_SRC emacs-lisp :var x=n
581 #+END_SRC"
582 (org-babel-execute-src-block)))))
584 (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
585 (org-test-with-temp-text "
586 #+name: base_plus
587 | 1 |
588 | 2 |
590 #+name: base
591 | 3 |
592 | 4 |
594 #+begin_src emacs-lisp :var x=base
596 #+end_src"
597 (org-babel-next-src-block 1)
598 (should (equal (org-babel-execute-src-block) '((3) (4))))))
600 (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
601 (org-test-with-temp-text "
602 #+name: base_plus
603 #+begin_src emacs-lisp
604 'bar
605 #+end_src
607 #+name: base
608 #+begin_src emacs-lisp
609 'foo
610 #+end_src
612 #+begin_src emacs-lisp :var x=base
614 #+end_src"
615 (org-babel-next-src-block 3)
616 (should (equal (org-babel-execute-src-block) "foo"))))
618 (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
619 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
620 (+ a b c d)
621 #+end_src
623 (should (= 10 (org-babel-execute-src-block)))))
625 (ert-deftest test-ob/org-babel-update-intermediate ()
626 (org-test-with-temp-text "#+name: foo
627 #+begin_src emacs-lisp
629 #+end_src
631 #+results: foo
634 #+begin_src emacs-lisp :var it=foo
635 (+ it 1)
636 #+end_src"
637 (let ((org-babel-update-intermediate nil))
638 (goto-char (point-min))
639 (org-babel-next-src-block 2)
640 (should (= 3 (org-babel-execute-src-block)))
641 (goto-char (point-min))
642 (forward-line 6)
643 (should (looking-at ": 4")))
644 (let ((org-babel-update-intermediate t))
645 (goto-char (point-min))
646 (org-babel-next-src-block 2)
647 (should (= 3 (org-babel-execute-src-block)))
648 (goto-char (point-min))
649 (forward-line 6)
650 (should (looking-at ": 2")))))
652 (ert-deftest test-ob/eval-header-argument ()
653 (cl-flet ((check-eval (eval runp)
654 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
655 (setq foo :evald)
656 #+end_src" eval)
657 (let ((foo :not-run))
658 (if runp
659 (progn (should (org-babel-execute-src-block))
660 (should (eq foo :evald)))
661 (progn (should-not (org-babel-execute-src-block))
662 (should-not (eq foo :evald))))))))
663 (check-eval "never" nil)
664 (check-eval "no" nil)
665 (check-eval "never-export" t)
666 (check-eval "no-export" t)
667 (let ((org-babel-exp-reference-buffer (current-buffer)))
668 (check-eval "never" nil)
669 (check-eval "no" nil)
670 (check-eval "never-export" nil)
671 (check-eval "no-export" nil))))
673 (ert-deftest test-ob/noweb-expansion ()
674 ;; Standard test.
675 (should
676 (string=
677 "bar"
678 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
679 <<foo>>
680 #+end_src
682 #+name: foo
683 #+begin_src sh
685 #+end_src"
686 (org-babel-expand-noweb-references))))
687 ;; Handle :noweb-sep.
688 (should
689 (string=
690 "barbaz"
691 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
692 <<foo>>
693 #+end_src
695 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
697 #+end_src
699 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
701 #+end_src"
702 (org-babel-expand-noweb-references))))
703 ;; :noweb-ref is extracted from definition, not point of call.
704 (should
705 (string=
706 "(+ 1 1)"
707 (org-test-with-temp-text
709 * Call
710 :PROPERTIES:
711 :header-args: :noweb-ref bar
712 :END:
714 <point>#+begin_src emacs-lisp :results output :tangle yes
715 <<foo>>
716 #+end_src
718 * Evaluation
719 :PROPERTIES:
720 :header-args: :noweb-ref foo
721 :END:
723 #+begin_src sh :noweb-sep \"\"
724 (+ 1 1)
725 #+end_src"
726 (org-babel-expand-noweb-references))))
727 ;; Handle recursive expansion.
728 (should
729 (equal "baz"
730 (org-test-with-temp-text "
731 #+begin_src emacs-lisp :noweb yes<point>
732 <<foo>>
733 #+end_src
735 #+name: foo
736 #+begin_src emacs-lisp :noweb yes
737 <<bar>>
738 #+end_src
740 #+name: bar
741 #+begin_src emacs-lisp
743 #+end_src"
744 (org-babel-expand-noweb-references))))
745 ;; During recursive expansion, obey to `:noweb' property.
746 (should
747 (equal "<<bar>>"
748 (org-test-with-temp-text "
749 #+begin_src emacs-lisp :noweb yes<point>
750 <<foo>>
751 #+end_src
753 #+name: foo
754 #+begin_src emacs-lisp :noweb no
755 <<bar>>
756 #+end_src
758 #+name: bar
759 #+begin_src emacs-lisp
761 #+end_src"
762 (org-babel-expand-noweb-references)))))
764 (ert-deftest test-ob/splitting-variable-lists-in-references ()
765 (org-test-with-temp-text ""
766 (should (= 1 (length (org-babel-ref-split-args
767 "a=\"this, no work\""))))
768 (should (= 2 (length (org-babel-ref-split-args
769 "a=\"this, no work\", b=1"))))))
771 (ert-deftest test-ob/balanced-split ()
772 "Test `org-babel-balanced-split' specifications."
773 (should (equal
774 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
775 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
776 '((32 9) . 58))))
777 ;; Handle un-balanced parens.
778 (should
779 (equal '(":foo ((6)" "bar 1")
780 (org-babel-balanced-split ":foo ((6) :bar 1" '((32 9) . 58))))
781 (should
782 (equal '(":foo \"(foo\"" "bar 2")
783 (org-babel-balanced-split ":foo \"(foo\" :bar 2" '((32 9) . 58))))
784 ;; Handle un-balanced quotes.
785 (should
786 (equal '(":foo \"1" "bar 3")
787 (org-babel-balanced-split ":foo \"1 :bar 3" '((32 9) . 58))))
788 ;; Handle empty string.
789 (should
790 (equal '(":foo \"\"")
791 (org-babel-balanced-split ":foo \"\"" '((32 9) . 58))))
792 ;; Handle control characters within double quotes.
793 (should
794 (equal '(":foo \"\\n\"")
795 (org-babel-balanced-split ":foo \"\\n\"" '((32 9) . 58)))))
797 (ert-deftest test-ob/commented-last-block-line-no-var ()
798 (org-test-with-temp-text-in-file "
799 #+begin_src emacs-lisp
801 #+end_src"
802 (org-babel-next-src-block)
803 (org-babel-execute-maybe)
804 (should (re-search-forward "\\#\\+results:" nil t))
805 (forward-line)
806 (should
807 (string=
809 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
810 (org-test-with-temp-text-in-file "
811 #+begin_src emacs-lisp
812 \"some text\";;
813 #+end_src"
814 (org-babel-next-src-block)
815 (org-babel-execute-maybe)
816 (should (re-search-forward "\\#\\+results:" nil t))
817 (forward-line)
818 (should
819 (string=
820 ": some text"
821 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
823 (ert-deftest test-ob/commented-last-block-line-with-var ()
824 (org-test-with-temp-text-in-file "
825 #+begin_src emacs-lisp :var a=1
827 #+end_src"
828 (org-babel-next-src-block)
829 (org-babel-execute-maybe)
830 (re-search-forward "\\#\\+results:" nil t)
831 (forward-line)
832 (should (string=
834 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
835 (org-test-with-temp-text-in-file "
836 #+begin_src emacs-lisp :var a=2
838 #+end_src"
839 (org-babel-next-src-block)
840 (org-babel-execute-maybe)
841 (re-search-forward "\\#\\+results:" nil t)
842 (forward-line)
843 (should (string=
844 ": 2"
845 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
847 (ert-deftest test-ob/org-babel-insert-result ()
848 "Test `org-babel-insert-result' specifications."
849 ;; Do not error when output is an improper list.
850 (should
851 (org-test-with-temp-text
853 <point>#+BEGIN_SRC emacs-lisp
854 '((1 . nil) (2 . 3))
855 #+END_SRC
857 (org-babel-execute-maybe) t))
858 ;; Escape headlines when producing an example block.
859 (should
860 (string-match-p
861 ",\\* Not an headline"
862 (org-test-with-temp-text
864 <point>#+BEGIN_SRC emacs-lisp
865 \"* Not an headline\"
866 #+END_SRC
868 (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
869 (buffer-string))))
870 ;; Escape special syntax in example blocks.
871 (should
872 (string-match-p
873 ",#\\+END_SRC"
874 (org-test-with-temp-text
876 <point>#+BEGIN_SRC emacs-lisp
877 \"#+END_SRC\"
878 #+END_SRC
880 (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
881 (buffer-string))))
882 ;; No escaping is done with other blocks or raw type.
883 (should-not
884 (string-match-p
885 ",\\* Not an headline"
886 (org-test-with-temp-text
888 <point>#+BEGIN_SRC emacs-lisp
889 \"* Not an headline\"
890 #+END_SRC
892 (let ((org-babel-min-lines-for-block-output 10))
893 (org-babel-execute-maybe))
894 (buffer-string))))
895 (should-not
896 (string-match-p
897 ",\\* Not an headline"
898 (org-test-with-temp-text
900 <point>#+BEGIN_SRC emacs-lisp :results raw
901 \"* Not an headline\"
902 #+END_SRC
904 (org-babel-execute-maybe)
905 (buffer-string))))
906 (should-not
907 (string-match-p
908 ",\\* Not an headline"
909 (org-test-with-temp-text
911 <point>#+BEGIN_SRC emacs-lisp :results drawer
912 \"* Not an headline\"
913 #+END_SRC
915 (org-babel-execute-maybe)
916 (buffer-string)))))
918 (ert-deftest test-ob/remove-inline-result ()
919 "Test `org-babel-remove-inline-result' honors whitespace."
920 (let*
921 ((inline-sb "src_emacs-lisp{(+ 1 2)}")
922 (inline-res " {{{results(=3=)}}}")
923 (inline-sb-dot (concat inline-sb "."))
924 (inline-sb-res-dot (concat inline-sb inline-res ".")))
925 (org-test-with-temp-text
926 ;; Insert inline_src_block followed by dot.
927 inline-sb-dot
928 ;; Insert result before dot.
929 (org-babel-execute-maybe)
930 (should (string= inline-sb-res-dot
931 (buffer-substring-no-properties
932 (point-at-bol) (point-at-eol))))
933 ;; Delete whitespace and result.
934 (org-babel-remove-inline-result)
935 (should (string= inline-sb-dot
936 (buffer-substring-no-properties
937 (point-at-bol) (point-at-eol))))
938 ;; Add whitespace and result before dot.
939 (search-forward inline-sb)
940 (insert " " inline-res)
941 (goto-char (point-at-bol))
942 ;; Remove whitespace and result.
943 (org-babel-remove-inline-result)
944 (should (string= inline-sb-dot
945 (buffer-substring-no-properties
946 (point-at-bol) (point-at-eol))))
947 ;; Add whitespace before dot.
948 (search-forward inline-sb)
949 (insert " ")
950 (goto-char (point-at-bol))
951 ;; Add result before whitespace.
952 (org-babel-execute-maybe)
953 ;; Remove result - leave trailing whitespace and dot.
954 (org-babel-remove-inline-result)
955 (should (string= (concat inline-sb " .")
956 (buffer-substring-no-properties
957 (point-at-bol) (point-at-eol)))))))
959 (ert-deftest test-ob/org-babel-remove-result--results-default ()
960 "Test `org-babel-remove-result' with default :results."
961 (mapcar (lambda (language)
962 (test-ob-verify-result-and-removed-result
963 "\n"
964 (concat
965 "* org-babel-remove-result
966 #+begin_src " language "
967 #+end_src
969 * next heading")))
970 '("sh" "emacs-lisp")))
972 (ert-deftest test-ob/org-babel-results-indented-wrap ()
973 "Ensure that wrapped results are inserted correction when indented.
974 If not inserted correctly then the second evaluation will fail
975 trying to find the :END: marker."
976 (org-test-with-temp-text
977 "- indented
978 #+begin_src sh :results file wrap
979 echo test.txt
980 #+end_src"
981 (org-babel-next-src-block 1)
982 (org-babel-execute-src-block)
983 (org-babel-execute-src-block)))
985 (ert-deftest test-ob/file-desc-header-argument ()
986 "Test that the :file-desc header argument is used."
987 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
988 \"foo\"
989 #+end_src
991 #+begin_src emacs-lisp :results file :file-desc
992 \"foo\"
993 #+end_src"
994 (org-babel-execute-src-block)
995 (org-babel-next-src-block 1)
996 (org-babel-execute-src-block)
997 (goto-char (point-min))
998 (should (search-forward "[[file:foo][bar]]" nil t))
999 (should (search-forward "[[file:foo][foo]]" nil t))))
1001 (ert-deftest test-ob/result-file-link-type-header-argument ()
1002 "Ensure that the result is a link to a file.
1003 The file is just a link to `:file' value. Inhibit non-empty
1004 result write to `:file' value."
1005 (org-test-with-temp-text "
1006 <point>#+begin_src shell :results value link :file \"/tmp/test.txt\"
1007 echo \"hello\" > /tmp/test.txt
1008 echo \"test\"
1009 #+end_src"
1010 (org-babel-execute-src-block)
1011 (should (search-forward "[[file:/tmp/test.txt]]" nil nil))
1012 (should (with-temp-buffer
1013 (insert-file-contents "/tmp/test.txt")
1014 (string= "hello\n" (buffer-string))))))
1016 (ert-deftest test-ob/result-graphics-link-type-header-argument ()
1017 "Ensure that the result is a link to a file.
1018 The file is just a link to `:file' value. Inhibit non-empty
1019 result write to `:file' value."
1020 (org-test-with-temp-text "
1021 <point>#+begin_src shell :results value graphics :file \"/tmp/test.txt\"
1022 echo \"hello\" > /tmp/test.txt
1023 echo \"test\"
1024 #+end_src"
1025 (org-babel-execute-src-block)
1026 (should (search-forward "[[file:/tmp/test.txt]]" nil nil))
1027 (should (with-temp-buffer
1028 (insert-file-contents "/tmp/test.txt")
1029 (string= "hello\n" (buffer-string))))))
1031 (ert-deftest test-ob/inline-src_blk-preceded-punct-preceded-by-point ()
1032 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
1033 (org-babel-inline-result-wrap "=%s="))
1034 (org-test-with-temp-text
1035 test-line
1036 (forward-char 1)
1037 (org-babel-execute-maybe)
1038 (should (re-search-forward "=\"x\"=" nil t))
1039 (forward-line))))
1041 (ert-deftest test-ob/commented-last-block-line-with-var ()
1042 (org-test-with-temp-text-in-file "
1043 #+begin_src emacs-lisp :var a=1
1045 #+end_src"
1046 (org-babel-next-src-block)
1047 (org-babel-execute-maybe)
1048 (re-search-forward "\\#\\+results:" nil t)
1049 (forward-line)
1050 (should (string=
1052 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
1053 (org-test-with-temp-text-in-file "
1054 #+begin_src emacs-lisp :var a=2
1056 #+end_src"
1057 (org-babel-next-src-block)
1058 (org-babel-execute-maybe)
1059 (re-search-forward "\\#\\+results:" nil t)
1060 (forward-line)
1061 (should (string=
1062 ": 2"
1063 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
1065 (defun test-ob-verify-result-and-removed-result (result buffer-text)
1066 "Test helper function to test `org-babel-remove-result'.
1067 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
1068 and the result of execution is verified against RESULT.
1070 The block is actually executed /twice/ to ensure result
1071 replacement happens correctly."
1072 (org-test-with-temp-text
1073 buffer-text
1074 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
1075 (should (re-search-forward "\\#\\+results:" nil t))
1076 (forward-line)
1077 (should (string= result
1078 (buffer-substring-no-properties
1079 (point-at-bol)
1080 (- (point-max) 16))))
1081 (org-babel-previous-src-block) (org-babel-remove-result)
1082 (should (string= buffer-text
1083 (buffer-substring-no-properties
1084 (point-min) (point-max))))))
1086 (ert-deftest test-ob/org-babel-remove-result--results-list ()
1087 "Test `org-babel-remove-result' with :results list."
1088 (test-ob-verify-result-and-removed-result
1089 "- 1
1091 - 3"
1093 "* org-babel-remove-result
1094 #+begin_src emacs-lisp :results list
1095 '(1 2 3)
1096 #+end_src
1098 * next heading"))
1100 (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
1101 "Test `org-babel-remove-result' with :results wrap."
1102 (test-ob-verify-result-and-removed-result
1103 ":results:
1104 hello there
1105 :end:"
1107 "* org-babel-remove-result
1109 #+begin_src emacs-lisp :results wrap
1110 \"hello there\"
1111 #+end_src
1113 * next heading"))
1115 (ert-deftest test-ob/org-babel-remove-result--results-org ()
1116 "Test `org-babel-remove-result' with :results org."
1117 (test-ob-verify-result-and-removed-result
1118 "#+begin_src org
1119 ,* heading
1120 ,** subheading
1121 content
1122 #+end_src"
1124 "* org-babel-remove-result
1125 #+begin_src emacs-lisp :results org
1126 \"* heading
1127 ,** subheading
1128 content\"
1129 #+end_src
1131 * next heading"))
1133 (ert-deftest test-ob/org-babel-remove-result--results-html ()
1134 "Test `org-babel-remove-result' with :results html."
1135 (test-ob-verify-result-and-removed-result
1136 "#+begin_export html
1137 <head><body></body></head>
1138 #+end_export"
1140 "* org-babel-remove-result
1141 #+begin_src emacs-lisp :results html
1142 \"<head><body></body></head>\"
1143 #+end_src
1145 * next heading"))
1147 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
1148 "Test `org-babel-remove-result' with :results latex."
1149 (test-ob-verify-result-and-removed-result
1150 "#+begin_export latex
1151 Line 1
1152 Line 2
1153 Line 3
1154 #+end_export"
1156 "* org-babel-remove-result
1157 #+begin_src emacs-lisp :results latex
1158 \"Line 1
1159 Line 2
1160 Line 3\"
1161 #+end_src
1163 * next heading"))
1165 (ert-deftest test-ob/org-babel-remove-result--results-code ()
1166 "Test `org-babel-remove-result' with :results code."
1168 (test-ob-verify-result-and-removed-result
1169 "#+begin_src emacs-lisp
1170 \"I am working!\"
1171 #+end_src"
1173 "* org-babel-remove-result
1174 #+begin_src emacs-lisp :results code
1175 (message \"I am working!\")
1176 #+end_src
1178 * next heading"))
1180 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
1181 "Test `org-babel-remove-result' with :results pp."
1182 (test-ob-verify-result-and-removed-result
1183 ": \"I /am/ working!\""
1185 "* org-babel-remove-result
1186 #+begin_src emacs-lisp :results pp
1187 \"I /am/ working!\")
1188 #+end_src
1190 * next heading"))
1192 (ert-deftest test-ob/org-babel-remove-result--no-blank-line ()
1193 "Test `org-babel-remove-result' without blank line between code and results."
1194 (should
1195 (equal "
1196 #+begin_src emacs-lisp
1197 (+ 1 1)
1198 #+end_src
1199 #+results:
1201 * next heading"
1202 (org-test-with-temp-text
1204 <point>#+begin_src emacs-lisp
1205 (+ 1 1)
1206 #+end_src
1207 #+results:
1209 * next heading"
1210 (org-babel-execute-maybe)
1211 (buffer-string)))))
1213 (ert-deftest test-ob/results-do-not-replace-code-blocks ()
1214 (org-test-with-temp-text "Block two has a space after the name.
1216 #+name: foo
1217 #+begin_src emacs-lisp
1219 #+end_src
1221 #+name: foo
1222 #+begin_src emacs-lisp
1224 #+end_src
1226 #+name: foo
1227 #+begin_src emacs-lisp
1229 #+end_src
1231 #+RESULTS: foo
1232 : foo
1234 (dolist (num '(1 2 3))
1235 ;; execute the block
1236 (goto-char (point-min))
1237 (org-babel-next-src-block num) (org-babel-execute-src-block)
1238 ;; check the results
1239 (goto-char (point-max))
1240 (move-beginning-of-line 0)
1241 (should (looking-at (format ": %d" num))))))
1243 (ert-deftest test-ob/blocks-with-spaces ()
1244 "Test expansion of blocks followed by blank lines."
1245 ;; Preserve number of blank lines after block.
1246 (should
1247 (equal "#+BEGIN_SRC emacs-lisp
1248 \(+ 1 2)
1249 #+END_SRC
1251 #+RESULTS:
1252 : 3\n\n\n"
1253 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1254 \(+ 1 2)
1255 #+END_SRC\n\n\n"
1256 (let ((org-babel-next-src-block "RESULTS"))
1257 (org-babel-execute-src-block))
1258 (buffer-string))))
1259 ;; Do not add spurious blank lines after results.
1260 (should
1261 (equal
1263 - item 1
1265 #+begin_src emacs-lisp
1267 #+end_src
1269 #+RESULTS:
1272 - item 2"
1273 (org-test-with-temp-text "
1274 - item 1
1276 #+begin_src emacs-lisp<point>
1278 #+end_src
1280 - item 2"
1281 (org-babel-execute-src-block)
1282 (buffer-string))))
1283 (should
1284 (equal
1286 - item 1
1288 #+begin_src emacs-lisp
1290 #+end_src
1292 #+RESULTS:
1295 - item 2"
1296 (org-test-with-temp-text "
1297 - item 1
1299 #+begin_src emacs-lisp<point>
1301 #+end_src
1303 #+RESULTS:
1306 - item 2"
1307 (org-babel-execute-src-block)
1308 (buffer-string)))))
1310 (ert-deftest test-ob/results-in-narrowed-buffer ()
1311 "Test block execution in a narrowed buffer."
1312 ;; If results don't exist, they should be inserted in visible part
1313 ;; of the buffer.
1314 (should
1315 (equal
1316 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1317 (org-test-with-temp-text
1318 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1319 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1320 (let ((org-babel-results-keyword "RESULTS"))
1321 (org-babel-execute-src-block))
1322 (org-trim (buffer-string)))))
1323 (should
1324 (equal
1325 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1326 (org-test-with-temp-text
1327 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1328 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1329 (let ((org-babel-results-keyword "RESULTS"))
1330 (org-babel-execute-src-block))
1331 (org-trim (buffer-string)))))
1332 ;; Results in visible part of buffer, should be updated here.
1333 (should
1334 (equal
1335 "#+NAME: test
1336 #+BEGIN_SRC emacs-lisp
1337 \(+ 1 2)
1338 #+END_SRC
1340 #+RESULTS: test
1341 : 3"
1342 (org-test-with-temp-text
1343 "#+NAME: test
1344 #+BEGIN_SRC emacs-lisp
1345 \(+ 1 2)
1346 #+END_SRC
1348 #+RESULTS: test
1350 <point>
1351 Paragraph"
1352 (narrow-to-region (point-min) (point))
1353 (goto-char (point-min))
1354 (let ((org-babel-results-keyword "RESULTS"))
1355 (org-babel-execute-src-block))
1356 (org-trim (buffer-string)))))
1357 ;; Results in invisible part of buffer, should be updated there.
1358 (org-test-with-temp-text
1359 "#+NAME: test
1360 #+BEGIN_SRC emacs-lisp
1361 \(+ 1 2)
1362 #+END_SRC
1364 #+RESULTS: test
1367 Paragraph"
1368 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1369 (let ((org-babel-results-keyword "RESULTS"))
1370 (org-babel-execute-src-block))
1371 (should-not (re-search-forward "^#\\+RESULTS:" nil t))
1372 (widen)
1373 (should (re-search-forward "^: 3" nil t))))
1375 (ert-deftest test-ob/specific-colnames ()
1376 "Test passing specific column names."
1377 (should
1378 (equal "#+name: input-table
1379 | id | var1 |
1380 |----+------|
1381 | 1 | bar |
1382 | 2 | baz |
1384 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1385 echo \"$data\"
1386 #+end_src
1388 #+RESULTS:
1389 | Rev | Author |
1390 |-----+--------|
1391 | 1 | bar |
1392 | 2 | baz |
1394 (org-test-with-temp-text
1395 "#+name: input-table
1396 | id | var1 |
1397 |----+------|
1398 | 1 | bar |
1399 | 2 | baz |
1401 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1402 echo \"$data\"
1403 #+end_src
1405 ;; we should find a code block
1406 (should (re-search-forward org-babel-src-block-regexp nil t))
1407 (goto-char (match-beginning 0))
1408 ;; now that we've located the code block, it may be evaluated
1409 (let ((org-babel-execute-src-block "RESULTS"))
1410 (org-babel-execute-src-block))
1411 (buffer-string)))))
1413 (ert-deftest test-ob/location-of-header-arg-eval ()
1414 "Test location of header argument evaluation."
1415 (org-test-with-temp-text "
1416 #+name: top-block
1417 #+begin_src emacs-lisp :var pt=(point)
1419 #+end_src
1421 #+name: bottom-block
1422 #+begin_src emacs-lisp :var pt=top-block()
1424 #+end_src
1426 ;; the value of the second block should be greater than the first
1427 (should
1428 (< (progn (re-search-forward org-babel-src-block-regexp nil t)
1429 (goto-char (match-beginning 0))
1430 (prog1 (save-match-data (org-babel-execute-src-block))
1431 (goto-char (match-end 0))))
1432 (progn (re-search-forward org-babel-src-block-regexp nil t)
1433 (goto-char (match-beginning 0))
1434 (org-babel-execute-src-block))))))
1436 (ert-deftest test-ob/preserve-results-indentation ()
1437 "Preserve indentation when executing a source block."
1438 (should
1439 (equal
1440 '(2 2)
1441 (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1442 (org-babel-execute-src-block)
1443 (let ((case-fold-search t)) (search-forward "RESULTS"))
1444 (list (org-get-indentation)
1445 (progn (forward-line) (org-get-indentation))))))
1446 (should
1447 (equal
1448 '(2 2)
1449 (org-test-with-temp-text
1450 " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1451 (org-babel-execute-src-block)
1452 (let ((case-fold-search t)) (search-forward "RESULTS"))
1453 (list (org-get-indentation)
1454 (progn (forward-line) (org-get-indentation))))))
1455 ;; Don't get fooled by TAB-based indentation.
1456 (should
1457 (equal
1458 '(6 6)
1459 (org-test-with-temp-text
1460 "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
1461 (setq tab-width 4)
1462 (org-babel-execute-src-block)
1463 (let ((case-fold-search t)) (search-forward "RESULTS"))
1464 (list (org-get-indentation)
1465 (progn (forward-line) (org-get-indentation))))))
1466 ;; Properly indent examplified blocks.
1467 (should
1468 (equal
1469 " #+begin_example
1480 #+end_example
1482 (org-test-with-temp-text
1483 " #+begin_src emacs-lisp :results output
1484 (dotimes (i 10) (princ i) (princ \"\\n\"))
1485 #+end_src"
1486 (org-babel-execute-src-block)
1487 (search-forward "begin_example")
1488 (downcase
1489 (buffer-substring-no-properties (line-beginning-position)
1490 (point-max))))))
1491 ;; Properly indent "org" blocks.
1492 (should
1493 (equal
1494 " #+begin_src org
1505 #+end_src
1507 (org-test-with-temp-text
1508 " #+begin_src emacs-lisp :results output org
1509 (dotimes (i 10) (princ i) (princ \"\\n\"))
1510 #+end_src"
1511 (org-babel-execute-src-block)
1512 (search-forward "begin_src org")
1513 (downcase
1514 (buffer-substring-no-properties (line-beginning-position)
1515 (point-max)))))))
1517 (ert-deftest test-ob/safe-header-args ()
1518 "Detect safe and unsafe header args."
1519 (let ((safe-args '((:cache . "foo")
1520 (:results . "output")
1521 (:eval . "never")
1522 (:eval . "query")))
1523 (unsafe-args '((:eval . "yes")
1524 (:results . "output file")
1525 (:foo . "bar")))
1526 (malformed-args '((bar . "foo")
1527 ("foo" . "bar")
1528 :foo))
1529 (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
1530 (dolist (arg safe-args)
1531 (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
1532 (dolist (arg unsafe-args)
1533 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1534 (dolist (arg malformed-args)
1535 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1536 (should (not (funcall safe-p (append safe-args unsafe-args))))))
1538 (ert-deftest test-ob/noweb-expansions-in-cache ()
1539 "Ensure that noweb expansions are expanded before caching."
1540 (let ((noweb-expansions-in-cache-var 0))
1541 (org-test-with-temp-text "
1542 #+name: foo
1543 #+begin_src emacs-lisp
1544 \"I said\"
1545 #+end_src
1547 #+name: bar
1548 #+begin_src emacs-lisp :noweb yes :cache yes
1549 (setq noweb-expansions-in-cache-var
1550 (+ 1 noweb-expansions-in-cache-var))
1551 (concat <<foo>> \" check noweb expansions\")
1552 #+end_src
1554 ;; run the second block to create the cache
1555 (goto-char (point-min))
1556 (re-search-forward (regexp-quote "#+name: bar"))
1557 (should (string= "I said check noweb expansions"
1558 (org-babel-execute-src-block)))
1559 (should (= noweb-expansions-in-cache-var 1))
1560 ;; change the value of the first block
1561 (goto-char (point-min))
1562 (re-search-forward (regexp-quote "said"))
1563 (goto-char (match-beginning 0))
1564 (insert "haven't ")
1565 (re-search-forward (regexp-quote "#+name: bar"))
1566 (should (string= "I haven't said check noweb expansions"
1567 (org-babel-execute-src-block)))
1568 (should (= noweb-expansions-in-cache-var 2)))))
1570 (ert-deftest test-ob/file-ext-and-output-dir ()
1571 (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
1572 (org-babel-next-src-block)
1573 (should (equal "file-ext-basic.txt"
1574 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1575 (org-babel-next-src-block)
1576 (should (equal "foo/file-ext-dir-relative.txt"
1577 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1578 (org-babel-next-src-block)
1579 (should (equal "foo/file-ext-dir-relative-slash.txt"
1580 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1581 (org-babel-next-src-block)
1582 (should (equal "/tmp/file-ext-dir-absolute.txt"
1583 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1584 (org-babel-next-src-block)
1585 (should (equal "foo.bar"
1586 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1587 (org-babel-next-src-block)
1588 (should (equal "xxx/foo.bar"
1589 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1592 (ert-deftest test-ob/script-escape ()
1593 ;; Delimited lists of numbers
1594 (should (equal '(1 2 3)
1595 (org-babel-script-escape "[1 2 3]")))
1596 (should (equal '(1 2 3)
1597 (org-babel-script-escape "{1 2 3}")))
1598 (should (equal '(1 2 3)
1599 (org-babel-script-escape "(1 2 3)")))
1600 ;; Delimited lists of double-quoted strings
1601 (should (equal '("foo" "bar")
1602 (org-babel-script-escape "(\"foo\" \"bar\")")))
1603 (should (equal '("foo" "bar")
1604 (org-babel-script-escape "[\"foo\" \"bar\"]")))
1605 (should (equal '("foo" "bar")
1606 (org-babel-script-escape "{\"foo\" \"bar\"}")))
1607 ;; ... with commas
1608 (should (equal '("foo" "bar")
1609 (org-babel-script-escape "(\"foo\", \"bar\")")))
1610 (should (equal '("foo" "bar")
1611 (org-babel-script-escape "[\"foo\", \"bar\"]")))
1612 (should (equal '("foo" "bar")
1613 (org-babel-script-escape "{\"foo\", \"bar\"}")))
1614 ;; Delimited lists of single-quoted strings
1615 (should (equal '("foo" "bar")
1616 (org-babel-script-escape "('foo' 'bar')")))
1617 (should (equal '("foo" "bar")
1618 (org-babel-script-escape "['foo' 'bar']")))
1619 (should (equal '("foo" "bar")
1620 (org-babel-script-escape "{'foo' 'bar'}")))
1621 ;; ... with commas
1622 (should (equal '("foo" "bar")
1623 (org-babel-script-escape "('foo', 'bar')")))
1624 (should (equal '("foo" "bar")
1625 (org-babel-script-escape "['foo', 'bar']")))
1626 (should (equal '("foo" "bar")
1627 (org-babel-script-escape "{'foo', 'bar'}")))
1628 ;; Single quoted strings
1629 (should (equal "foo"
1630 (org-babel-script-escape "'foo'")))
1631 ;; ... with internal double quote
1632 (should (equal "foo\"bar"
1633 (org-babel-script-escape "'foo\"bar'")))
1634 ;; ... with internal backslash
1635 (should (equal "foo\\bar"
1636 (org-babel-script-escape "'foo\\bar'")))
1637 ;; ... with internal escaped backslash
1638 (should (equal "foo\\bar"
1639 (org-babel-script-escape "'foo\\\\bar'")))
1640 ;; ... with internal backslash-double quote
1641 (should (equal "foo\\\"bar"
1642 (org-babel-script-escape "'foo\\\"bar'")))
1643 ;; ... with internal escaped backslash-double quote
1644 (should (equal "foo\\\"bar"
1645 (org-babel-script-escape "'foo\\\\\"bar'")))
1646 ;; ... with internal escaped single quote
1647 (should (equal "foo'bar"
1648 (org-babel-script-escape "'foo\\'bar'")))
1649 ;; ... with internal escaped backslash-escaped single quote
1650 (should (equal "foo\\'bar"
1651 (org-babel-script-escape "'foo\\\\\\'bar'")))
1652 ;; Double quoted strings
1653 (should (equal "foo"
1654 (org-babel-script-escape "\"foo\"")))
1655 ;; ... with internal single quote
1656 (should (equal "foo'bar"
1657 (org-babel-script-escape "\"foo'bar\"")))
1658 ;; ... with internal backslash
1659 (should (equal "foo\\bar"
1660 (org-babel-script-escape "\"foo\\bar\"")))
1661 ;; ... with internal escaped backslash
1662 (should (equal "foo\\bar"
1663 (org-babel-script-escape "\"foo\\\\bar\"")))
1664 ;; ... with internal backslash-single quote
1665 (should (equal "foo\\'bar"
1666 (org-babel-script-escape "\"foo\\'bar\"")))
1667 ;; ... with internal escaped backslash-single quote
1668 (should (equal "foo\\'bar"
1669 (org-babel-script-escape "\"foo\\\\'bar\"")))
1670 ;; ... with internal escaped double quote
1671 (should (equal "foo\"bar"
1672 (org-babel-script-escape "\"foo\\\"bar\"")))
1673 ;; ... with internal escaped backslash-escaped double quote
1674 (should (equal "foo\\\"bar"
1675 (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
1677 (ert-deftest test-ob/process-params-no-duplicates ()
1678 (should
1679 (equal (org-babel-process-params '((:colname-names)
1680 (:rowname-names)
1681 (:result-params)
1682 (:result-type)
1683 (:var . "\"foo\"")))
1684 '((:var)
1685 (:colname-names)
1686 (:rowname-names)
1687 (:result-params)
1688 (:result-type . value)))))
1690 (defun org-test-babel-confirm-evaluate (eval-value)
1691 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
1693 #+end_src" eval-value)
1694 (goto-char (point-min))
1695 (let ((info (org-babel-get-src-block-info)))
1696 (org-babel-check-confirm-evaluate info))))
1698 (ert-deftest test-ob/check-eval ()
1699 (let ((org-confirm-babel-evaluate t))
1700 ;; Non-export tests
1701 (dolist (pair '(("no" . nil)
1702 ("never" . nil)
1703 ("query" . query)
1704 ("yes" . query)))
1705 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
1706 ;; Export tests
1707 (let ((org-babel-exp-reference-buffer t))
1708 (dolist (pair '(("no" . nil)
1709 ("never" . nil)
1710 ("query" . query)
1711 ("yes" . query)
1712 ("never-export" . nil)
1713 ("no-export" . nil)
1714 ("query-export" . query)))
1715 (message (car pair))
1716 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))))
1717 (let ((org-confirm-babel-evaluate nil))
1718 ;; Non-export tests
1719 (dolist (pair '(("no" . nil)
1720 ("never" . nil)
1721 ("query" . query)
1722 ("yes" . t)))
1723 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
1724 ;; Export tests
1725 (let ((org-babel-exp-reference-buffer t))
1726 (dolist (pair '(("no" . nil)
1727 ("never" . nil)
1728 ("query" . query)
1729 ("yes" . t)
1730 ("never-export" . nil)
1731 ("no-export" . nil)
1732 ("query-export" . query)))
1733 (message (car pair))
1734 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair)))))))
1736 (defun org-test-ob/update-block-body ()
1737 "Test `org-babel-update-block-body' specifications."
1738 (should
1739 (equal "#+begin_src elisp\n 2\n#+end_src"
1740 (let ((org-edit-src-content-indentation 2))
1741 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1742 (org-babel-update-block-body "2")
1743 (buffer-string)))))
1744 ;; Preserve block indentation.
1745 (should
1746 (equal " #+begin_src elisp\n 2\n #+end_src"
1747 (let ((org-edit-src-content-indentation 1))
1748 (org-test-with-temp-text
1749 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1750 (org-babel-update-block-body "2")
1751 (buffer-string)))))
1752 ;; Ignore NEW-BODY global indentation.
1753 (should
1754 (equal "#+begin_src elisp\n 2\n#+end_src"
1755 (let ((org-edit-src-content-indentation 2))
1756 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1757 (org-babel-update-block-body " 2")
1758 (buffer-string)))))
1759 ;; When indentation should be preserved ignore the two rules above.
1760 (should
1761 (equal " #+begin_src elisp\n2\n #+end_src"
1762 (let ((org-edit-src-content-indentation 1)
1763 (org-src-preserve-indentation t))
1764 (org-test-with-temp-text
1765 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1766 (org-babel-update-block-body "2")
1767 (buffer-string)))))
1768 (should
1769 (equal " #+begin_src elisp -i\n2\n #+end_src"
1770 (let ((org-edit-src-content-indentation 1))
1771 (org-test-with-temp-text
1772 " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
1773 (org-babel-update-block-body "2")
1774 (buffer-string)))))
1775 (should
1776 (equal "#+begin_src elisp\n 2\n#+end_src"
1777 (let ((org-edit-src-content-indentation 2)
1778 (org-src-preserve-indentation t))
1779 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1780 (org-babel-update-block-body " 2")
1781 (buffer-string)))))
1782 (should
1783 (equal "#+begin_src elisp -i\n 2\n#+end_src"
1784 (let ((org-edit-src-content-indentation 2)
1785 (org-src-preserve-indentation t))
1786 (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
1787 (org-babel-update-block-body " 2")
1788 (buffer-string))))))
1790 (ert-deftest test-ob/find-named-result ()
1791 "Test `org-babel-find-named-result' specifications."
1792 (should
1793 (= 1
1794 (org-test-with-temp-text "#+results: foo\n: result"
1795 (org-babel-find-named-result "foo"))))
1796 (should-not
1797 (org-test-with-temp-text "#+results: foo\n: result"
1798 (org-babel-find-named-result "bar")))
1799 (should-not
1800 (org-test-with-temp-text "#+results: foobar\n: result"
1801 (org-babel-find-named-result "foo")))
1802 ;; Search is case insensitive.
1803 (should
1804 (org-test-with-temp-text "#+RESULTS: FOO\n: result"
1805 (org-babel-find-named-result "foo")))
1806 ;; Handle hash in results keyword.
1807 (should
1808 (org-test-with-temp-text "#+results[hash]: FOO\n: result"
1809 (org-babel-find-named-result "foo")))
1810 ;; Accept orphaned affiliated keywords.
1811 (should
1812 (org-test-with-temp-text "#+results: foo"
1813 (org-babel-find-named-result "foo"))))
1815 (ert-deftest test-ob/where-is-src-block-result ()
1816 "Test `org-babel-where-is-src-block-result' specifications."
1817 ;; Find anonymous results.
1818 (should
1819 (equal "#+RESULTS:"
1820 (org-test-with-temp-text
1821 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
1822 (goto-char (org-babel-where-is-src-block-result))
1823 (buffer-substring-no-properties (point) (line-end-position)))))
1824 ;; Find named results. Those have priority over anonymous ones.
1825 (should
1826 (equal "#+RESULTS: example"
1827 (org-test-with-temp-text
1829 <point>#+NAME: example
1830 #+BEGIN_SRC emacs-lisp
1831 \(+ 1 1)
1832 #+END_SRC
1834 #+RESULTS: example
1835 : 2"
1836 (goto-char (org-babel-where-is-src-block-result))
1837 (buffer-substring-no-properties (point) (line-end-position)))))
1838 (should
1839 (equal "#+RESULTS: example"
1840 (org-test-with-temp-text
1842 <point>#+NAME: example
1843 #+BEGIN_SRC emacs-lisp
1844 \(+ 1 1)
1845 #+END_SRC
1847 #+RESULTS:
1848 : fake
1850 #+RESULTS: example
1851 : 2"
1852 (goto-char (org-babel-where-is-src-block-result))
1853 (buffer-substring-no-properties (point) (line-end-position)))))
1854 ;; Return nil when no result is found.
1855 (should-not
1856 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1857 (org-babel-where-is-src-block-result)))
1858 (should-not
1859 (org-test-with-temp-text
1860 "- item\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n"
1861 (org-babel-where-is-src-block-result)))
1862 ;; When optional argument INSERT is non-nil, add RESULTS keyword
1863 ;; whenever no RESULTS can be found.
1864 (should
1865 (equal
1866 "#+RESULTS:"
1867 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1868 (let ((org-babel-results-keyword "RESULTS"))
1869 (goto-char (org-babel-where-is-src-block-result t)))
1870 (buffer-substring-no-properties (point) (line-end-position)))))
1871 ;; Insert a named RESULTS keyword if possible.
1872 (should
1873 (equal
1874 "#+RESULTS: e"
1875 (org-test-with-temp-text
1876 "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1877 (let ((org-babel-results-keyword "RESULTS"))
1878 (goto-char (org-babel-where-is-src-block-result t)))
1879 (buffer-substring-no-properties (point) (line-end-position)))))
1880 ;; When optional argument HASH is provided, clear RESULTS keyword
1881 ;; and related contents if they do not match it.
1882 (should
1883 (equal
1884 "#+RESULTS[bbbb]:"
1885 (org-test-with-temp-text
1886 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 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 (should
1891 (equal
1892 "#+RESULTS[bbbb]:"
1893 (org-test-with-temp-text
1894 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
1895 (let ((org-babel-results-keyword "RESULTS"))
1896 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
1897 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1898 ;; Handle hashes with times.
1899 (should
1900 (equal
1901 "#+RESULTS[<2014-03-04 00:41:10> bbbb]:"
1902 (org-test-with-temp-text
1904 <point>#+BEGIN_SRC emacs-lisp
1905 \(+ 1 1)
1906 #+END_SRC
1908 #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1909 (let ((org-babel-results-keyword "RESULTS")
1910 (org-babel-hash-show-time t))
1911 (cl-letf (((symbol-function 'format-time-string)
1912 (lambda (&rest _) "<2014-03-04 00:41:10>")))
1913 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb"))
1914 (org-trim (buffer-substring-no-properties (point) (point-max))))))))
1915 (should
1916 (equal
1917 "#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1918 (org-test-with-temp-text
1920 <point>#+BEGIN_SRC emacs-lisp
1921 \(+ 1 1)
1922 #+END_SRC
1924 #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1925 (let ((org-babel-results-keyword "RESULTS")
1926 (org-babel-hash-show-time t))
1927 (cl-letf (((symbol-function 'format-time-string)
1928 (lambda (&rest _) "<2014-03-04 00:41:10>")))
1929 (goto-char (org-babel-where-is-src-block-result nil nil "aaaa"))
1930 (org-trim (buffer-substring-no-properties (point) (point-max))))))))
1931 ;; RESULTS keyword may not be the last affiliated keyword.
1932 (should
1933 (equal
1934 "#+RESULTS[bbbb]:"
1935 (org-test-with-temp-text
1937 <point>#+BEGIN_SRC emacs-lisp
1938 \(+ 1 1)
1939 #+END_SRC
1941 #+RESULTS[aaaa]:
1942 #+NAME: e
1943 : 3"
1944 (let ((org-babel-results-keyword "RESULTS"))
1945 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
1946 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1947 ;; HASH does nothing if no RESULTS can be found. However, if INSERT
1948 ;; is also non-nil, RESULTS keyword is inserted along with the
1949 ;; expected hash.
1950 (should
1951 (equal
1952 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1953 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1954 (org-babel-where-is-src-block-result nil nil "bbbb")
1955 (buffer-string))))
1956 (should
1957 (equal
1958 "#+RESULTS[bbbb]:"
1959 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1960 (let ((org-babel-results-keyword "RESULTS"))
1961 (goto-char (org-babel-where-is-src-block-result t nil "bbbb")))
1962 (org-trim (buffer-substring-no-properties (point) (point-max)))))))
1964 (ert-deftest test-ob/goto-named-src-block ()
1965 "Test interactive use of `org-babel-goto-named-src-block'."
1966 (org-test-with-temp-text-in-file
1968 #+NAME: abc
1969 #+BEGIN_SRC emacs-lisp :results value
1970 (1+ 1)
1971 #+END_SRC
1972 #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
1973 #+BEGIN_SRC emacs-lisp
1974 <<abc>>
1975 #+END_SRC
1977 #+RESULTS: abc
1980 ;; non-existent name
1981 (should-not
1982 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
1983 ;; correct name
1984 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
1985 (should (= 14 (point)))
1986 ;; call line - autocompletion
1987 (forward-line 3)
1988 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1989 (should (= 14 (point)))
1990 ;; noweb reference - autocompletion
1991 (forward-line 5)
1992 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1993 (should (= 14 (point)))
1994 ;; at symbol - autocompletion
1995 (forward-line 7)
1996 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1997 (should (= 14 (point)))
1998 ;; in results - autocompletion
1999 (forward-line 8)
2000 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
2001 (should (= 14 (point)))
2002 (forward-line 9)
2003 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
2004 (should (= 14 (point)))))
2006 (ert-deftest test-ob/evaluate-body-with-coderefs ()
2007 (should
2008 (= 2
2009 (org-test-with-temp-text
2010 "#+begin_src emacs-lisp -l \"#(ref:%s)\"\n2 #(ref:foo)\n#+end_src"
2011 (org-babel-execute-src-block))))
2012 (should
2013 (= 3
2014 (org-test-with-temp-text
2015 "#+begin_src emacs-lisp\n3 #(ref:foo)\n#+end_src"
2016 (let ((org-coderef-label-format "#(ref:%s)"))
2017 (org-babel-execute-src-block))))))
2019 (provide 'test-ob)
2021 ;;; test-ob ends here