ob-core: Add document and test for "graphics" format
[org-mode/org-tableheadings.git] / testing / lisp / test-ob.el
blobff86d5eeb33e653d0cb54af0165ab3e04a7415a1
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 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
695 #+end_src
697 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
699 #+end_src"
700 (org-babel-expand-noweb-references))))
701 ;; :noweb-ref is extracted from definition, not point of call.
702 (should
703 (string=
704 "(+ 1 1)"
705 (org-test-with-temp-text
707 * Call
708 :PROPERTIES:
709 :header-args: :noweb-ref bar
710 :END:
712 <point>#+begin_src emacs-lisp :results output :tangle yes
713 <<foo>>
714 #+end_src
716 * Evaluation
717 :PROPERTIES:
718 :header-args: :noweb-ref foo
719 :END:
721 #+begin_src sh :noweb-sep \"\"
722 (+ 1 1)
723 #+end_src"
724 (org-babel-expand-noweb-references))))
725 ;; Handle recursive expansion.
726 (should
727 (equal "baz"
728 (org-test-with-temp-text "
729 #+begin_src emacs-lisp :noweb yes<point>
730 <<foo>>
731 #+end_src
733 #+name: foo
734 #+begin_src emacs-lisp :noweb yes
735 <<bar>>
736 #+end_src
738 #+name: bar
739 #+begin_src emacs-lisp
741 #+end_src"
742 (org-babel-expand-noweb-references))))
743 ;; During recursive expansion, obey to `:noweb' property.
744 (should
745 (equal "<<bar>>"
746 (org-test-with-temp-text "
747 #+begin_src emacs-lisp :noweb yes<point>
748 <<foo>>
749 #+end_src
751 #+name: foo
752 #+begin_src emacs-lisp :noweb no
753 <<bar>>
754 #+end_src
756 #+name: bar
757 #+begin_src emacs-lisp
759 #+end_src"
760 (org-babel-expand-noweb-references)))))
762 (ert-deftest test-ob/splitting-variable-lists-in-references ()
763 (org-test-with-temp-text ""
764 (should (= 1 (length (org-babel-ref-split-args
765 "a=\"this, no work\""))))
766 (should (= 2 (length (org-babel-ref-split-args
767 "a=\"this, no work\", b=1"))))))
769 (ert-deftest test-ob/balanced-split ()
770 "Test `org-babel-balanced-split' specifications."
771 (should (equal
772 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
773 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
774 '((32 9) . 58))))
775 ;; Handle un-balanced parens.
776 (should
777 (equal '(":foo ((6)" "bar 1")
778 (org-babel-balanced-split ":foo ((6) :bar 1" '((32 9) . 58))))
779 (should
780 (equal '(":foo \"(foo\"" "bar 2")
781 (org-babel-balanced-split ":foo \"(foo\" :bar 2" '((32 9) . 58))))
782 ;; Handle un-balanced quotes.
783 (should
784 (equal '(":foo \"1" "bar 3")
785 (org-babel-balanced-split ":foo \"1 :bar 3" '((32 9) . 58))))
786 ;; Handle empty string.
787 (should
788 (equal '(":foo \"\"")
789 (org-babel-balanced-split ":foo \"\"" '((32 9) . 58))))
790 ;; Handle control characters within double quotes.
791 (should
792 (equal '(":foo \"\\n\"")
793 (org-babel-balanced-split ":foo \"\\n\"" '((32 9) . 58)))))
795 (ert-deftest test-ob/commented-last-block-line-no-var ()
796 (org-test-with-temp-text-in-file "
797 #+begin_src emacs-lisp
799 #+end_src"
800 (org-babel-next-src-block)
801 (org-babel-execute-maybe)
802 (should (re-search-forward "\\#\\+results:" nil t))
803 (forward-line)
804 (should
805 (string=
807 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
808 (org-test-with-temp-text-in-file "
809 #+begin_src emacs-lisp
810 \"some text\";;
811 #+end_src"
812 (org-babel-next-src-block)
813 (org-babel-execute-maybe)
814 (should (re-search-forward "\\#\\+results:" nil t))
815 (forward-line)
816 (should
817 (string=
818 ": some text"
819 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
821 (ert-deftest test-ob/commented-last-block-line-with-var ()
822 (org-test-with-temp-text-in-file "
823 #+begin_src emacs-lisp :var a=1
825 #+end_src"
826 (org-babel-next-src-block)
827 (org-babel-execute-maybe)
828 (re-search-forward "\\#\\+results:" nil t)
829 (forward-line)
830 (should (string=
832 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
833 (org-test-with-temp-text-in-file "
834 #+begin_src emacs-lisp :var a=2
836 #+end_src"
837 (org-babel-next-src-block)
838 (org-babel-execute-maybe)
839 (re-search-forward "\\#\\+results:" nil t)
840 (forward-line)
841 (should (string=
842 ": 2"
843 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
845 (ert-deftest test-ob/org-babel-insert-result ()
846 "Test `org-babel-insert-result' specifications."
847 ;; Do not error when output is an improper list.
848 (should
849 (org-test-with-temp-text
851 <point>#+BEGIN_SRC emacs-lisp
852 '((1 . nil) (2 . 3))
853 #+END_SRC
855 (org-babel-execute-maybe) t))
856 ;; Escape headlines when producing an example block.
857 (should
858 (string-match-p
859 ",\\* Not an headline"
860 (org-test-with-temp-text
862 <point>#+BEGIN_SRC emacs-lisp
863 \"* Not an headline\"
864 #+END_SRC
866 (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
867 (buffer-string))))
868 ;; Escape special syntax in example blocks.
869 (should
870 (string-match-p
871 ",#\\+END_SRC"
872 (org-test-with-temp-text
874 <point>#+BEGIN_SRC emacs-lisp
875 \"#+END_SRC\"
876 #+END_SRC
878 (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
879 (buffer-string))))
880 ;; No escaping is done with other blocks or raw type.
881 (should-not
882 (string-match-p
883 ",\\* Not an headline"
884 (org-test-with-temp-text
886 <point>#+BEGIN_SRC emacs-lisp
887 \"* Not an headline\"
888 #+END_SRC
890 (let ((org-babel-min-lines-for-block-output 10))
891 (org-babel-execute-maybe))
892 (buffer-string))))
893 (should-not
894 (string-match-p
895 ",\\* Not an headline"
896 (org-test-with-temp-text
898 <point>#+BEGIN_SRC emacs-lisp :results raw
899 \"* Not an headline\"
900 #+END_SRC
902 (org-babel-execute-maybe)
903 (buffer-string))))
904 (should-not
905 (string-match-p
906 ",\\* Not an headline"
907 (org-test-with-temp-text
909 <point>#+BEGIN_SRC emacs-lisp :results drawer
910 \"* Not an headline\"
911 #+END_SRC
913 (org-babel-execute-maybe)
914 (buffer-string)))))
916 (ert-deftest test-ob/remove-inline-result ()
917 "Test `org-babel-remove-inline-result' honors whitespace."
918 (let*
919 ((inline-sb "src_emacs-lisp{(+ 1 2)}")
920 (inline-res " {{{results(=3=)}}}")
921 (inline-sb-dot (concat inline-sb "."))
922 (inline-sb-res-dot (concat inline-sb inline-res ".")))
923 (org-test-with-temp-text
924 ;; Insert inline_src_block followed by dot.
925 inline-sb-dot
926 ;; Insert result before dot.
927 (org-babel-execute-maybe)
928 (should (string= inline-sb-res-dot
929 (buffer-substring-no-properties
930 (point-at-bol) (point-at-eol))))
931 ;; Delete whitespace and result.
932 (org-babel-remove-inline-result)
933 (should (string= inline-sb-dot
934 (buffer-substring-no-properties
935 (point-at-bol) (point-at-eol))))
936 ;; Add whitespace and result before dot.
937 (search-forward inline-sb)
938 (insert " " inline-res)
939 (goto-char (point-at-bol))
940 ;; Remove whitespace and result.
941 (org-babel-remove-inline-result)
942 (should (string= inline-sb-dot
943 (buffer-substring-no-properties
944 (point-at-bol) (point-at-eol))))
945 ;; Add whitespace before dot.
946 (search-forward inline-sb)
947 (insert " ")
948 (goto-char (point-at-bol))
949 ;; Add result before whitespace.
950 (org-babel-execute-maybe)
951 ;; Remove result - leave trailing whitespace and dot.
952 (org-babel-remove-inline-result)
953 (should (string= (concat inline-sb " .")
954 (buffer-substring-no-properties
955 (point-at-bol) (point-at-eol)))))))
957 (ert-deftest test-ob/org-babel-remove-result--results-default ()
958 "Test `org-babel-remove-result' with default :results."
959 (mapcar (lambda (language)
960 (test-ob-verify-result-and-removed-result
961 "\n"
962 (concat
963 "* org-babel-remove-result
964 #+begin_src " language "
965 #+end_src
967 * next heading")))
968 '("sh" "emacs-lisp")))
970 (ert-deftest test-ob/org-babel-results-indented-wrap ()
971 "Ensure that wrapped results are inserted correction when indented.
972 If not inserted correctly then the second evaluation will fail
973 trying to find the :END: marker."
974 (org-test-with-temp-text
975 "- indented
976 #+begin_src sh :results file wrap
977 echo test.txt
978 #+end_src"
979 (org-babel-next-src-block 1)
980 (org-babel-execute-src-block)
981 (org-babel-execute-src-block)))
983 (ert-deftest test-ob/file-desc-header-argument ()
984 "Test that the :file-desc header argument is used."
985 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
986 \"foo\"
987 #+end_src
989 #+begin_src emacs-lisp :results file :file-desc
990 \"foo\"
991 #+end_src"
992 (org-babel-execute-src-block)
993 (org-babel-next-src-block 1)
994 (org-babel-execute-src-block)
995 (goto-char (point-min))
996 (should (search-forward "[[file:foo][bar]]" nil t))
997 (should (search-forward "[[file:foo][foo]]" nil t))))
999 (ert-deftest test-ob/result-file-link-type-header-argument ()
1000 "Ensure that the result is a link to a file.
1001 The file is just a link to `:file' value. Inhibit non-empty
1002 result write to `:file' value."
1003 (org-test-with-temp-text "
1004 <point>#+begin_src shell :results value link :file \"/tmp/test.txt\"
1005 echo \"hello\" > /tmp/test.txt
1006 echo \"test\"
1007 #+end_src"
1008 (org-babel-execute-src-block)
1009 (should (search-forward "[[file:/tmp/test.txt]]" nil nil))
1010 (should (with-temp-buffer
1011 (insert-file-contents "/tmp/test.txt")
1012 (string= "hello\n" (buffer-string))))))
1014 (ert-deftest test-ob/result-graphics-link-type-header-argument ()
1015 "Ensure that the result is a link to a file.
1016 The file is just a link to `:file' value. Inhibit non-empty
1017 result write to `:file' value."
1018 (org-test-with-temp-text "
1019 <point>#+begin_src shell :results value graphics :file \"/tmp/test.txt\"
1020 echo \"hello\" > /tmp/test.txt
1021 echo \"test\"
1022 #+end_src"
1023 (org-babel-execute-src-block)
1024 (should (search-forward "[[file:/tmp/test.txt]]" nil nil))
1025 (should (with-temp-buffer
1026 (insert-file-contents "/tmp/test.txt")
1027 (string= "hello\n" (buffer-string))))))
1029 (ert-deftest test-ob/inline-src_blk-preceded-punct-preceded-by-point ()
1030 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
1031 (org-babel-inline-result-wrap "=%s="))
1032 (org-test-with-temp-text
1033 test-line
1034 (forward-char 1)
1035 (org-babel-execute-maybe)
1036 (should (re-search-forward "=\"x\"=" nil t))
1037 (forward-line))))
1039 (ert-deftest test-ob/commented-last-block-line-with-var ()
1040 (org-test-with-temp-text-in-file "
1041 #+begin_src emacs-lisp :var a=1
1043 #+end_src"
1044 (org-babel-next-src-block)
1045 (org-babel-execute-maybe)
1046 (re-search-forward "\\#\\+results:" nil t)
1047 (forward-line)
1048 (should (string=
1050 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
1051 (org-test-with-temp-text-in-file "
1052 #+begin_src emacs-lisp :var a=2
1054 #+end_src"
1055 (org-babel-next-src-block)
1056 (org-babel-execute-maybe)
1057 (re-search-forward "\\#\\+results:" nil t)
1058 (forward-line)
1059 (should (string=
1060 ": 2"
1061 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
1063 (defun test-ob-verify-result-and-removed-result (result buffer-text)
1064 "Test helper function to test `org-babel-remove-result'.
1065 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
1066 and the result of execution is verified against RESULT.
1068 The block is actually executed /twice/ to ensure result
1069 replacement happens correctly."
1070 (org-test-with-temp-text
1071 buffer-text
1072 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
1073 (should (re-search-forward "\\#\\+results:" nil t))
1074 (forward-line)
1075 (should (string= result
1076 (buffer-substring-no-properties
1077 (point-at-bol)
1078 (- (point-max) 16))))
1079 (org-babel-previous-src-block) (org-babel-remove-result)
1080 (should (string= buffer-text
1081 (buffer-substring-no-properties
1082 (point-min) (point-max))))))
1084 (ert-deftest test-ob/org-babel-remove-result--results-list ()
1085 "Test `org-babel-remove-result' with :results list."
1086 (test-ob-verify-result-and-removed-result
1087 "- 1
1089 - 3"
1091 "* org-babel-remove-result
1092 #+begin_src emacs-lisp :results list
1093 '(1 2 3)
1094 #+end_src
1096 * next heading"))
1098 (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
1099 "Test `org-babel-remove-result' with :results wrap."
1100 (test-ob-verify-result-and-removed-result
1101 ":results:
1102 hello there
1103 :end:"
1105 "* org-babel-remove-result
1107 #+begin_src emacs-lisp :results wrap
1108 \"hello there\"
1109 #+end_src
1111 * next heading"))
1113 (ert-deftest test-ob/org-babel-remove-result--results-org ()
1114 "Test `org-babel-remove-result' with :results org."
1115 (test-ob-verify-result-and-removed-result
1116 "#+begin_src org
1117 ,* heading
1118 ,** subheading
1119 content
1120 #+end_src"
1122 "* org-babel-remove-result
1123 #+begin_src emacs-lisp :results org
1124 \"* heading
1125 ,** subheading
1126 content\"
1127 #+end_src
1129 * next heading"))
1131 (ert-deftest test-ob/org-babel-remove-result--results-html ()
1132 "Test `org-babel-remove-result' with :results html."
1133 (test-ob-verify-result-and-removed-result
1134 "#+begin_export html
1135 <head><body></body></head>
1136 #+end_export"
1138 "* org-babel-remove-result
1139 #+begin_src emacs-lisp :results html
1140 \"<head><body></body></head>\"
1141 #+end_src
1143 * next heading"))
1145 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
1146 "Test `org-babel-remove-result' with :results latex."
1147 (test-ob-verify-result-and-removed-result
1148 "#+begin_export latex
1149 Line 1
1150 Line 2
1151 Line 3
1152 #+end_export"
1154 "* org-babel-remove-result
1155 #+begin_src emacs-lisp :results latex
1156 \"Line 1
1157 Line 2
1158 Line 3\"
1159 #+end_src
1161 * next heading"))
1163 (ert-deftest test-ob/org-babel-remove-result--results-code ()
1164 "Test `org-babel-remove-result' with :results code."
1166 (test-ob-verify-result-and-removed-result
1167 "#+begin_src emacs-lisp
1168 \"I am working!\"
1169 #+end_src"
1171 "* org-babel-remove-result
1172 #+begin_src emacs-lisp :results code
1173 (message \"I am working!\")
1174 #+end_src
1176 * next heading"))
1178 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
1179 "Test `org-babel-remove-result' with :results pp."
1180 (test-ob-verify-result-and-removed-result
1181 ": \"I /am/ working!\""
1183 "* org-babel-remove-result
1184 #+begin_src emacs-lisp :results pp
1185 \"I /am/ working!\")
1186 #+end_src
1188 * next heading"))
1190 (ert-deftest test-ob/org-babel-remove-result--no-blank-line ()
1191 "Test `org-babel-remove-result' without blank line between code and results."
1192 (should
1193 (equal "
1194 #+begin_src emacs-lisp
1195 (+ 1 1)
1196 #+end_src
1197 #+results:
1199 * next heading"
1200 (org-test-with-temp-text
1202 <point>#+begin_src emacs-lisp
1203 (+ 1 1)
1204 #+end_src
1205 #+results:
1207 * next heading"
1208 (org-babel-execute-maybe)
1209 (buffer-string)))))
1211 (ert-deftest test-ob/results-do-not-replace-code-blocks ()
1212 (org-test-with-temp-text "Block two has a space after the name.
1214 #+name: foo
1215 #+begin_src emacs-lisp
1217 #+end_src
1219 #+name: foo
1220 #+begin_src emacs-lisp
1222 #+end_src
1224 #+name: foo
1225 #+begin_src emacs-lisp
1227 #+end_src
1229 #+RESULTS: foo
1230 : foo
1232 (dolist (num '(1 2 3))
1233 ;; execute the block
1234 (goto-char (point-min))
1235 (org-babel-next-src-block num) (org-babel-execute-src-block)
1236 ;; check the results
1237 (goto-char (point-max))
1238 (move-beginning-of-line 0)
1239 (should (looking-at (format ": %d" num))))))
1241 (ert-deftest test-ob/blocks-with-spaces ()
1242 "Test expansion of blocks followed by blank lines."
1243 ;; Preserve number of blank lines after block.
1244 (should
1245 (equal "#+BEGIN_SRC emacs-lisp
1246 \(+ 1 2)
1247 #+END_SRC
1249 #+RESULTS:
1250 : 3\n\n\n"
1251 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1252 \(+ 1 2)
1253 #+END_SRC\n\n\n"
1254 (let ((org-babel-next-src-block "RESULTS"))
1255 (org-babel-execute-src-block))
1256 (buffer-string))))
1257 ;; Do not add spurious blank lines after results.
1258 (should
1259 (equal
1261 - item 1
1263 #+begin_src emacs-lisp
1265 #+end_src
1267 #+RESULTS:
1270 - item 2"
1271 (org-test-with-temp-text "
1272 - item 1
1274 #+begin_src emacs-lisp<point>
1276 #+end_src
1278 - item 2"
1279 (org-babel-execute-src-block)
1280 (buffer-string))))
1281 (should
1282 (equal
1284 - item 1
1286 #+begin_src emacs-lisp
1288 #+end_src
1290 #+RESULTS:
1293 - item 2"
1294 (org-test-with-temp-text "
1295 - item 1
1297 #+begin_src emacs-lisp<point>
1299 #+end_src
1301 #+RESULTS:
1304 - item 2"
1305 (org-babel-execute-src-block)
1306 (buffer-string)))))
1308 (ert-deftest test-ob/results-in-narrowed-buffer ()
1309 "Test block execution in a narrowed buffer."
1310 ;; If results don't exist, they should be inserted in visible part
1311 ;; of the buffer.
1312 (should
1313 (equal
1314 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1315 (org-test-with-temp-text
1316 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1317 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1318 (let ((org-babel-results-keyword "RESULTS"))
1319 (org-babel-execute-src-block))
1320 (org-trim (buffer-string)))))
1321 (should
1322 (equal
1323 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1324 (org-test-with-temp-text
1325 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1326 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1327 (let ((org-babel-results-keyword "RESULTS"))
1328 (org-babel-execute-src-block))
1329 (org-trim (buffer-string)))))
1330 ;; Results in visible part of buffer, should be updated here.
1331 (should
1332 (equal
1333 "#+NAME: test
1334 #+BEGIN_SRC emacs-lisp
1335 \(+ 1 2)
1336 #+END_SRC
1338 #+RESULTS: test
1339 : 3"
1340 (org-test-with-temp-text
1341 "#+NAME: test
1342 #+BEGIN_SRC emacs-lisp
1343 \(+ 1 2)
1344 #+END_SRC
1346 #+RESULTS: test
1348 <point>
1349 Paragraph"
1350 (narrow-to-region (point-min) (point))
1351 (goto-char (point-min))
1352 (let ((org-babel-results-keyword "RESULTS"))
1353 (org-babel-execute-src-block))
1354 (org-trim (buffer-string)))))
1355 ;; Results in invisible part of buffer, should be updated there.
1356 (org-test-with-temp-text
1357 "#+NAME: test
1358 #+BEGIN_SRC emacs-lisp
1359 \(+ 1 2)
1360 #+END_SRC
1362 #+RESULTS: test
1365 Paragraph"
1366 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1367 (let ((org-babel-results-keyword "RESULTS"))
1368 (org-babel-execute-src-block))
1369 (should-not (re-search-forward "^#\\+RESULTS:" nil t))
1370 (widen)
1371 (should (re-search-forward "^: 3" nil t))))
1373 (ert-deftest test-ob/specific-colnames ()
1374 "Test passing specific column names."
1375 (should
1376 (equal "#+name: input-table
1377 | id | var1 |
1378 |----+------|
1379 | 1 | bar |
1380 | 2 | baz |
1382 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1383 echo \"$data\"
1384 #+end_src
1386 #+RESULTS:
1387 | Rev | Author |
1388 |-----+--------|
1389 | 1 | bar |
1390 | 2 | baz |
1392 (org-test-with-temp-text
1393 "#+name: input-table
1394 | id | var1 |
1395 |----+------|
1396 | 1 | bar |
1397 | 2 | baz |
1399 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1400 echo \"$data\"
1401 #+end_src
1403 ;; we should find a code block
1404 (should (re-search-forward org-babel-src-block-regexp nil t))
1405 (goto-char (match-beginning 0))
1406 ;; now that we've located the code block, it may be evaluated
1407 (let ((org-babel-execute-src-block "RESULTS"))
1408 (org-babel-execute-src-block))
1409 (buffer-string)))))
1411 (ert-deftest test-ob/location-of-header-arg-eval ()
1412 "Test location of header argument evaluation."
1413 (org-test-with-temp-text "
1414 #+name: top-block
1415 #+begin_src emacs-lisp :var pt=(point)
1417 #+end_src
1419 #+name: bottom-block
1420 #+begin_src emacs-lisp :var pt=top-block()
1422 #+end_src
1424 ;; the value of the second block should be greater than the first
1425 (should
1426 (< (progn (re-search-forward org-babel-src-block-regexp nil t)
1427 (goto-char (match-beginning 0))
1428 (prog1 (save-match-data (org-babel-execute-src-block))
1429 (goto-char (match-end 0))))
1430 (progn (re-search-forward org-babel-src-block-regexp nil t)
1431 (goto-char (match-beginning 0))
1432 (org-babel-execute-src-block))))))
1434 (ert-deftest test-ob/preserve-results-indentation ()
1435 "Preserve indentation when executing a src block."
1436 (should
1437 (equal
1438 '(2 2)
1439 (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1440 (org-babel-execute-src-block)
1441 (let ((case-fold-search t)) (search-forward "RESULTS"))
1442 (list (org-get-indentation)
1443 (progn (forward-line) (org-get-indentation))))))
1444 (should
1445 (equal
1446 '(2 2)
1447 (org-test-with-temp-text
1448 " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
1449 (org-babel-execute-src-block)
1450 (let ((case-fold-search t)) (search-forward "RESULTS"))
1451 (list (org-get-indentation)
1452 (progn (forward-line) (org-get-indentation))))))
1453 ;; Don't get fooled by TAB-based indentation.
1454 (should
1455 (equal
1456 '(6 6)
1457 (org-test-with-temp-text
1458 "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
1459 (setq tab-width 4)
1460 (org-babel-execute-src-block)
1461 (let ((case-fold-search t)) (search-forward "RESULTS"))
1462 (list (org-get-indentation)
1463 (progn (forward-line) (org-get-indentation))))))
1464 ;; Properly indent examplified blocks.
1465 (should
1466 (equal
1467 " #+begin_example
1478 #+end_example
1480 (org-test-with-temp-text
1481 " #+begin_src emacs-lisp :results output
1482 (dotimes (i 10) (princ i) (princ \"\\n\"))
1483 #+end_src"
1484 (org-babel-execute-src-block)
1485 (search-forward "begin_example")
1486 (downcase
1487 (buffer-substring-no-properties (line-beginning-position)
1488 (point-max))))))
1489 ;; Properly indent "org" blocks.
1490 (should
1491 (equal
1492 " #+begin_src org
1503 #+end_src
1505 (org-test-with-temp-text
1506 " #+begin_src emacs-lisp :results output org
1507 (dotimes (i 10) (princ i) (princ \"\\n\"))
1508 #+end_src"
1509 (org-babel-execute-src-block)
1510 (search-forward "begin_src org")
1511 (downcase
1512 (buffer-substring-no-properties (line-beginning-position)
1513 (point-max)))))))
1515 (ert-deftest test-ob/safe-header-args ()
1516 "Detect safe and unsafe header args."
1517 (let ((safe-args '((:cache . "foo")
1518 (:results . "output")
1519 (:eval . "never")
1520 (:eval . "query")))
1521 (unsafe-args '((:eval . "yes")
1522 (:results . "output file")
1523 (:foo . "bar")))
1524 (malformed-args '((bar . "foo")
1525 ("foo" . "bar")
1526 :foo))
1527 (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
1528 (dolist (arg safe-args)
1529 (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
1530 (dolist (arg unsafe-args)
1531 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1532 (dolist (arg malformed-args)
1533 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1534 (should (not (funcall safe-p (append safe-args unsafe-args))))))
1536 (ert-deftest test-ob/noweb-expansions-in-cache ()
1537 "Ensure that noweb expansions are expanded before caching."
1538 (let ((noweb-expansions-in-cache-var 0))
1539 (org-test-with-temp-text "
1540 #+name: foo
1541 #+begin_src emacs-lisp
1542 \"I said\"
1543 #+end_src
1545 #+name: bar
1546 #+begin_src emacs-lisp :noweb yes :cache yes
1547 (setq noweb-expansions-in-cache-var
1548 (+ 1 noweb-expansions-in-cache-var))
1549 (concat <<foo>> \" check noweb expansions\")
1550 #+end_src
1552 ;; run the second block to create the cache
1553 (goto-char (point-min))
1554 (re-search-forward (regexp-quote "#+name: bar"))
1555 (should (string= "I said check noweb expansions"
1556 (org-babel-execute-src-block)))
1557 (should (= noweb-expansions-in-cache-var 1))
1558 ;; change the value of the first block
1559 (goto-char (point-min))
1560 (re-search-forward (regexp-quote "said"))
1561 (goto-char (match-beginning 0))
1562 (insert "haven't ")
1563 (re-search-forward (regexp-quote "#+name: bar"))
1564 (should (string= "I haven't said check noweb expansions"
1565 (org-babel-execute-src-block)))
1566 (should (= noweb-expansions-in-cache-var 2)))))
1568 (ert-deftest test-ob/file-ext-and-output-dir ()
1569 (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
1570 (org-babel-next-src-block)
1571 (should (equal "file-ext-basic.txt"
1572 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1573 (org-babel-next-src-block)
1574 (should (equal "foo/file-ext-dir-relative.txt"
1575 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1576 (org-babel-next-src-block)
1577 (should (equal "foo/file-ext-dir-relative-slash.txt"
1578 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1579 (org-babel-next-src-block)
1580 (should (equal "/tmp/file-ext-dir-absolute.txt"
1581 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1582 (org-babel-next-src-block)
1583 (should (equal "foo.bar"
1584 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1585 (org-babel-next-src-block)
1586 (should (equal "xxx/foo.bar"
1587 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1590 (ert-deftest test-ob/script-escape ()
1591 ;; Delimited lists of numbers
1592 (should (equal '(1 2 3)
1593 (org-babel-script-escape "[1 2 3]")))
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 ;; Delimited lists of double-quoted strings
1599 (should (equal '("foo" "bar")
1600 (org-babel-script-escape "(\"foo\" \"bar\")")))
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 ;; ... with commas
1606 (should (equal '("foo" "bar")
1607 (org-babel-script-escape "(\"foo\", \"bar\")")))
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 ;; Delimited lists of single-quoted strings
1613 (should (equal '("foo" "bar")
1614 (org-babel-script-escape "('foo' 'bar')")))
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 ;; ... with commas
1620 (should (equal '("foo" "bar")
1621 (org-babel-script-escape "('foo', 'bar')")))
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 ;; Single quoted strings
1627 (should (equal "foo"
1628 (org-babel-script-escape "'foo'")))
1629 ;; ... with internal double quote
1630 (should (equal "foo\"bar"
1631 (org-babel-script-escape "'foo\"bar'")))
1632 ;; ... with internal backslash
1633 (should (equal "foo\\bar"
1634 (org-babel-script-escape "'foo\\bar'")))
1635 ;; ... with internal escaped backslash
1636 (should (equal "foo\\bar"
1637 (org-babel-script-escape "'foo\\\\bar'")))
1638 ;; ... with internal backslash-double quote
1639 (should (equal "foo\\\"bar"
1640 (org-babel-script-escape "'foo\\\"bar'")))
1641 ;; ... with internal escaped backslash-double quote
1642 (should (equal "foo\\\"bar"
1643 (org-babel-script-escape "'foo\\\\\"bar'")))
1644 ;; ... with internal escaped single quote
1645 (should (equal "foo'bar"
1646 (org-babel-script-escape "'foo\\'bar'")))
1647 ;; ... with internal escaped backslash-escaped single quote
1648 (should (equal "foo\\'bar"
1649 (org-babel-script-escape "'foo\\\\\\'bar'")))
1650 ;; Double quoted strings
1651 (should (equal "foo"
1652 (org-babel-script-escape "\"foo\"")))
1653 ;; ... with internal single quote
1654 (should (equal "foo'bar"
1655 (org-babel-script-escape "\"foo'bar\"")))
1656 ;; ... with internal backslash
1657 (should (equal "foo\\bar"
1658 (org-babel-script-escape "\"foo\\bar\"")))
1659 ;; ... with internal escaped backslash
1660 (should (equal "foo\\bar"
1661 (org-babel-script-escape "\"foo\\\\bar\"")))
1662 ;; ... with internal backslash-single quote
1663 (should (equal "foo\\'bar"
1664 (org-babel-script-escape "\"foo\\'bar\"")))
1665 ;; ... with internal escaped backslash-single quote
1666 (should (equal "foo\\'bar"
1667 (org-babel-script-escape "\"foo\\\\'bar\"")))
1668 ;; ... with internal escaped double quote
1669 (should (equal "foo\"bar"
1670 (org-babel-script-escape "\"foo\\\"bar\"")))
1671 ;; ... with internal escaped backslash-escaped double quote
1672 (should (equal "foo\\\"bar"
1673 (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
1675 (ert-deftest test-ob/process-params-no-duplicates ()
1676 (should
1677 (equal (org-babel-process-params '((:colname-names)
1678 (:rowname-names)
1679 (:result-params)
1680 (:result-type)
1681 (:var . "\"foo\"")))
1682 '((:var)
1683 (:colname-names)
1684 (:rowname-names)
1685 (:result-params)
1686 (:result-type . value)))))
1688 (defun org-test-babel-confirm-evaluate (eval-value)
1689 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
1691 #+end_src" eval-value)
1692 (goto-char (point-min))
1693 (let ((info (org-babel-get-src-block-info)))
1694 (org-babel-check-confirm-evaluate info))))
1696 (ert-deftest test-ob/check-eval ()
1697 (let ((org-confirm-babel-evaluate t))
1698 ;; Non-export tests
1699 (dolist (pair '(("no" . nil)
1700 ("never" . nil)
1701 ("query" . query)
1702 ("yes" . query)))
1703 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
1704 ;; Export tests
1705 (let ((org-babel-exp-reference-buffer t))
1706 (dolist (pair '(("no" . nil)
1707 ("never" . nil)
1708 ("query" . query)
1709 ("yes" . query)
1710 ("never-export" . nil)
1711 ("no-export" . nil)
1712 ("query-export" . query)))
1713 (message (car pair))
1714 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))))
1715 (let ((org-confirm-babel-evaluate nil))
1716 ;; Non-export tests
1717 (dolist (pair '(("no" . nil)
1718 ("never" . nil)
1719 ("query" . query)
1720 ("yes" . t)))
1721 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
1722 ;; Export tests
1723 (let ((org-babel-exp-reference-buffer t))
1724 (dolist (pair '(("no" . nil)
1725 ("never" . nil)
1726 ("query" . query)
1727 ("yes" . t)
1728 ("never-export" . nil)
1729 ("no-export" . nil)
1730 ("query-export" . query)))
1731 (message (car pair))
1732 (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair)))))))
1734 (defun org-test-ob/update-block-body ()
1735 "Test `org-babel-update-block-body' specifications."
1736 (should
1737 (equal "#+begin_src elisp\n 2\n#+end_src"
1738 (let ((org-edit-src-content-indentation 2))
1739 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1740 (org-babel-update-block-body "2")
1741 (buffer-string)))))
1742 ;; Preserve block indentation.
1743 (should
1744 (equal " #+begin_src elisp\n 2\n #+end_src"
1745 (let ((org-edit-src-content-indentation 1))
1746 (org-test-with-temp-text
1747 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1748 (org-babel-update-block-body "2")
1749 (buffer-string)))))
1750 ;; Ignore NEW-BODY global indentation.
1751 (should
1752 (equal "#+begin_src elisp\n 2\n#+end_src"
1753 (let ((org-edit-src-content-indentation 2))
1754 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1755 (org-babel-update-block-body " 2")
1756 (buffer-string)))))
1757 ;; When indentation should be preserved ignore the two rules above.
1758 (should
1759 (equal " #+begin_src elisp\n2\n #+end_src"
1760 (let ((org-edit-src-content-indentation 1)
1761 (org-src-preserve-indentation t))
1762 (org-test-with-temp-text
1763 " #+begin_src elisp\n (+ 1 1)\n #+end_src"
1764 (org-babel-update-block-body "2")
1765 (buffer-string)))))
1766 (should
1767 (equal " #+begin_src elisp -i\n2\n #+end_src"
1768 (let ((org-edit-src-content-indentation 1))
1769 (org-test-with-temp-text
1770 " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
1771 (org-babel-update-block-body "2")
1772 (buffer-string)))))
1773 (should
1774 (equal "#+begin_src elisp\n 2\n#+end_src"
1775 (let ((org-edit-src-content-indentation 2)
1776 (org-src-preserve-indentation t))
1777 (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
1778 (org-babel-update-block-body " 2")
1779 (buffer-string)))))
1780 (should
1781 (equal "#+begin_src elisp -i\n 2\n#+end_src"
1782 (let ((org-edit-src-content-indentation 2)
1783 (org-src-preserve-indentation t))
1784 (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
1785 (org-babel-update-block-body " 2")
1786 (buffer-string))))))
1788 (ert-deftest test-ob/find-named-result ()
1789 "Test `org-babel-find-named-result' specifications."
1790 (should
1791 (= 1
1792 (org-test-with-temp-text "#+results: foo\n: result"
1793 (org-babel-find-named-result "foo"))))
1794 (should-not
1795 (org-test-with-temp-text "#+results: foo\n: result"
1796 (org-babel-find-named-result "bar")))
1797 (should-not
1798 (org-test-with-temp-text "#+results: foobar\n: result"
1799 (org-babel-find-named-result "foo")))
1800 ;; Search is case insensitive.
1801 (should
1802 (org-test-with-temp-text "#+RESULTS: FOO\n: result"
1803 (org-babel-find-named-result "foo")))
1804 ;; Handle hash in results keyword.
1805 (should
1806 (org-test-with-temp-text "#+results[hash]: FOO\n: result"
1807 (org-babel-find-named-result "foo")))
1808 ;; Accept orphaned affiliated keywords.
1809 (should
1810 (org-test-with-temp-text "#+results: foo"
1811 (org-babel-find-named-result "foo"))))
1813 (ert-deftest test-ob/where-is-src-block-result ()
1814 "Test `org-babel-where-is-src-block-result' specifications."
1815 ;; Find anonymous results.
1816 (should
1817 (equal "#+RESULTS:"
1818 (org-test-with-temp-text
1819 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
1820 (goto-char (org-babel-where-is-src-block-result))
1821 (buffer-substring-no-properties (point) (line-end-position)))))
1822 ;; Find named results. Those have priority over anonymous ones.
1823 (should
1824 (equal "#+RESULTS: example"
1825 (org-test-with-temp-text
1827 <point>#+NAME: example
1828 #+BEGIN_SRC emacs-lisp
1829 \(+ 1 1)
1830 #+END_SRC
1832 #+RESULTS: example
1833 : 2"
1834 (goto-char (org-babel-where-is-src-block-result))
1835 (buffer-substring-no-properties (point) (line-end-position)))))
1836 (should
1837 (equal "#+RESULTS: example"
1838 (org-test-with-temp-text
1840 <point>#+NAME: example
1841 #+BEGIN_SRC emacs-lisp
1842 \(+ 1 1)
1843 #+END_SRC
1845 #+RESULTS:
1846 : fake
1848 #+RESULTS: example
1849 : 2"
1850 (goto-char (org-babel-where-is-src-block-result))
1851 (buffer-substring-no-properties (point) (line-end-position)))))
1852 ;; Return nil when no result is found.
1853 (should-not
1854 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1855 (org-babel-where-is-src-block-result)))
1856 (should-not
1857 (org-test-with-temp-text
1858 "- item\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n"
1859 (org-babel-where-is-src-block-result)))
1860 ;; When optional argument INSERT is non-nil, add RESULTS keyword
1861 ;; whenever no RESULTS can be found.
1862 (should
1863 (equal
1864 "#+RESULTS:"
1865 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1866 (let ((org-babel-results-keyword "RESULTS"))
1867 (goto-char (org-babel-where-is-src-block-result t)))
1868 (buffer-substring-no-properties (point) (line-end-position)))))
1869 ;; Insert a named RESULTS keyword if possible.
1870 (should
1871 (equal
1872 "#+RESULTS: e"
1873 (org-test-with-temp-text
1874 "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1875 (let ((org-babel-results-keyword "RESULTS"))
1876 (goto-char (org-babel-where-is-src-block-result t)))
1877 (buffer-substring-no-properties (point) (line-end-position)))))
1878 ;; When optional argument HASH is provided, clear RESULTS keyword
1879 ;; and related contents if they do not match it.
1880 (should
1881 (equal
1882 "#+RESULTS[bbbb]:"
1883 (org-test-with-temp-text
1884 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 3"
1885 (let ((org-babel-results-keyword "RESULTS"))
1886 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
1887 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1888 (should
1889 (equal
1890 "#+RESULTS[bbbb]:"
1891 (org-test-with-temp-text
1892 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
1893 (let ((org-babel-results-keyword "RESULTS"))
1894 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
1895 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1896 ;; Handle hashes with times.
1897 (should
1898 (equal
1899 "#+RESULTS[<2014-03-04 00:41:10> bbbb]:"
1900 (org-test-with-temp-text
1902 <point>#+BEGIN_SRC emacs-lisp
1903 \(+ 1 1)
1904 #+END_SRC
1906 #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1907 (let ((org-babel-results-keyword "RESULTS")
1908 (org-babel-hash-show-time t))
1909 (cl-letf (((symbol-function 'format-time-string)
1910 (lambda (&rest _) "<2014-03-04 00:41:10>")))
1911 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb"))
1912 (org-trim (buffer-substring-no-properties (point) (point-max))))))))
1913 (should
1914 (equal
1915 "#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1916 (org-test-with-temp-text
1918 <point>#+BEGIN_SRC emacs-lisp
1919 \(+ 1 1)
1920 #+END_SRC
1922 #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
1923 (let ((org-babel-results-keyword "RESULTS")
1924 (org-babel-hash-show-time t))
1925 (cl-letf (((symbol-function 'format-time-string)
1926 (lambda (&rest _) "<2014-03-04 00:41:10>")))
1927 (goto-char (org-babel-where-is-src-block-result nil nil "aaaa"))
1928 (org-trim (buffer-substring-no-properties (point) (point-max))))))))
1929 ;; RESULTS keyword may not be the last affiliated keyword.
1930 (should
1931 (equal
1932 "#+RESULTS[bbbb]:"
1933 (org-test-with-temp-text
1935 <point>#+BEGIN_SRC emacs-lisp
1936 \(+ 1 1)
1937 #+END_SRC
1939 #+RESULTS[aaaa]:
1940 #+NAME: e
1941 : 3"
1942 (let ((org-babel-results-keyword "RESULTS"))
1943 (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
1944 (org-trim (buffer-substring-no-properties (point) (point-max))))))
1945 ;; HASH does nothing if no RESULTS can be found. However, if INSERT
1946 ;; is also non-nil, RESULTS keyword is inserted along with the
1947 ;; expected hash.
1948 (should
1949 (equal
1950 "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1951 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1952 (org-babel-where-is-src-block-result nil nil "bbbb")
1953 (buffer-string))))
1954 (should
1955 (equal
1956 "#+RESULTS[bbbb]:"
1957 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
1958 (let ((org-babel-results-keyword "RESULTS"))
1959 (goto-char (org-babel-where-is-src-block-result t nil "bbbb")))
1960 (org-trim (buffer-substring-no-properties (point) (point-max)))))))
1962 (ert-deftest test-ob/goto-named-src-block ()
1963 "Test interactive use of `org-babel-goto-named-src-block'."
1964 (org-test-with-temp-text-in-file
1966 #+NAME: abc
1967 #+BEGIN_SRC emacs-lisp :results value
1968 (1+ 1)
1969 #+END_SRC
1970 #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
1971 #+BEGIN_SRC emacs-lisp
1972 <<abc>>
1973 #+END_SRC
1975 #+RESULTS: abc
1978 ;; non-existent name
1979 (should-not
1980 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
1981 ;; correct name
1982 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
1983 (should (= 14 (point)))
1984 ;; call line - autocompletion
1985 (forward-line 3)
1986 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1987 (should (= 14 (point)))
1988 ;; noweb reference - autocompletion
1989 (forward-line 5)
1990 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1991 (should (= 14 (point)))
1992 ;; at symbol - autocompletion
1993 (forward-line 7)
1994 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1995 (should (= 14 (point)))
1996 ;; in results - autocompletion
1997 (forward-line 8)
1998 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
1999 (should (= 14 (point)))
2000 (forward-line 9)
2001 (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
2002 (should (= 14 (point)))))
2004 (ert-deftest test-ob/evaluate-body-with-coderefs ()
2005 (should
2006 (= 2
2007 (org-test-with-temp-text
2008 "#+begin_src emacs-lisp -l \"#(ref:%s)\"\n2 #(ref:foo)\n#+end_src"
2009 (org-babel-execute-src-block))))
2010 (should
2011 (= 3
2012 (org-test-with-temp-text
2013 "#+begin_src emacs-lisp\n3 #(ref:foo)\n#+end_src"
2014 (let ((org-coderef-label-format "#(ref:%s)"))
2015 (org-babel-execute-src-block))))))
2017 (provide 'test-ob)
2019 ;;; test-ob ends here