1 ;;; test-ob.el --- tests for ob.el
3 ;; Copyright (c) 2010-2013 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/>.
23 (ert-deftest test-org-babel
/multi-line-header-regexp
()
24 (should(equal "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
25 org-babel-multi-line-header-regexp
))
26 ;;TODO can be optimised - and what about blah4 blah5 blah6?
28 org-babel-multi-line-header-regexp
29 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
32 "blah1 blah2 blah3 \t"
35 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
37 ;;TODO Check - should this fail?
39 (not (org-test-string-exact-match
40 org-babel-multi-line-header-regexp
41 " \t #+headers : blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))))
43 (ert-deftest test-org-babel
/src-block-regexp
()
46 "#+begin_src language -n-r-a-b -c :argument-1 yes :argument-2 no\n"
47 "echo this is a test\n"
48 "echo Currently in ' $PWD\n"
51 (flags "-n-r-a-b -c ")
52 (arguments ":argument-1 yes :argument-2 no")
53 (body "echo this is a test\necho Currently in ' $PWD\n"))
54 (should (string-match org-babel-src-block-regexp test-block
))
55 (should (string-match org-babel-src-block-regexp
(upcase test-block
)))
56 (should (equal language
(match-string 2 test-block
)))
57 ;;TODO Consider refactoring
58 (should (equal flags
(match-string 3 test-block
)))
59 (should (equal arguments
(match-string 4 test-block
)))
60 (should (equal body
(match-string 5 test-block
)))
62 (should (org-test-string-exact-match
63 org-babel-src-block-regexp
64 (replace-regexp-in-string flags
"" test-block
)))
66 (should (org-test-string-exact-match
67 org-babel-src-block-regexp
68 (replace-regexp-in-string arguments
"" test-block
)))
69 ;; should be valid with no body
70 (should (org-test-string-exact-match
71 org-babel-src-block-regexp
72 (replace-regexp-in-string body
"" test-block
)))))
74 (ert-deftest test-org-babel
/get-header
()
75 (should (not (org-babel-get-header
76 org-babel-default-header-args
:doesnt-exist
)))
77 (should(equal '((:session .
"none"))
79 org-babel-default-header-args
:session
)))
80 (should(equal '((:session .
"none"))
82 org-babel-default-header-args
:session nil
)))
83 (should (not (org-babel-get-header
84 org-babel-default-header-args
:SESSION
)))
85 (should (equal '((:tangle .
"no"))
87 org-babel-default-header-args
:tangle
)))
89 (should (equal org-babel-default-header-args
91 org-babel-default-header-args
:doesnt-exist
'others
)))
92 (should (equal org-babel-default-header-args
94 org-babel-default-header-args nil
'others
)))
98 org-babel-default-header-args
:noweb
'others
)))))
100 (ert-deftest test-org-babel
/default-inline-header-args
()
102 '((:session .
"none") (:results .
"replace") (:exports .
"results"))
103 org-babel-default-inline-header-args
)))
105 (ert-deftest ob-test
/org-babel-combine-header-arg-lists
()
106 (let ((results (org-babel-combine-header-arg-lists
109 (baz .
((foo bar
) (baz)))
110 (qux .
((foo bar baz qux
)))
111 (quux .
((foo bar
))))
115 (dolist (pair '((foo .
:any
)
119 (qux .
((foo bar baz qux
)))))
120 (should (equal (cdr pair
)
121 (cdr (assoc (car pair
) results
)))))))
123 ;;; ob-get-src-block-info
124 (ert-deftest test-org-babel
/get-src-block-info-language
()
125 (org-test-at-marker nil org-test-file-ob-anchor
126 (let ((info (org-babel-get-src-block-info)))
127 (should (string= "emacs-lisp" (nth 0 info
))))))
129 (ert-deftest test-org-babel
/get-src-block-info-body
()
130 (org-test-at-marker nil org-test-file-ob-anchor
131 (let ((info (org-babel-get-src-block-info)))
132 (should (string-match (regexp-quote org-test-file-ob-anchor
)
135 (ert-deftest test-org-babel
/get-src-block-info-tangle
()
136 (org-test-at-marker nil org-test-file-ob-anchor
137 (let ((info (org-babel-get-src-block-info)))
138 (should (string= "no" (cdr (assoc :tangle
(nth 2 info
))))))))
140 (ert-deftest test-org-babel
/elisp-in-header-arguments
()
141 "Test execution of elisp forms in header arguments."
142 (org-test-with-temp-text-in-file "
144 * elisp forms in header arguments
148 #+begin_src emacs-lisp
151 (goto-char (point-min))
152 (org-babel-next-src-block)
153 (let ((info (org-babel-get-src-block-info)))
154 (should (= 42 (org-babel-execute-src-block))))))
156 (ert-deftest test-org-babel
/simple-named-code-block
()
157 "Test that simple named code blocks can be evaluated."
158 (org-test-with-temp-text-in-file "
160 #+name: i-have-a-name
161 #+begin_src emacs-lisp
164 (org-babel-next-src-block 1)
165 (should (= 42 (org-babel-execute-src-block)))))
167 (ert-deftest test-org-babel
/simple-variable-resolution
()
168 "Test that simple variable resolution is working."
169 (org-test-with-temp-text-in-file "
172 #+begin_src emacs-lisp
176 #+begin_src emacs-lisp :var four=four
180 (org-babel-next-src-block 2)
181 (should (= 4 (org-babel-execute-src-block)))
183 (should (string= ": 4" (buffer-substring
187 (ert-deftest test-org-babel
/multi-line-header-arguments
()
188 "Test that multi-line header arguments and can be read."
189 (org-test-with-temp-text-in-file "
191 #+headers: :var letters='(a b c d e f g)
192 #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
194 (defalias 'my-map (if (org-version-check \"24.2.50\" \"cl\" :predicate)
197 (my-map 'list #'list numbers letters)
200 (org-babel-next-src-block)
201 (let ((results (org-babel-execute-src-block)))
202 (should(equal 'a
(cadr (assoc 1 results
))))
203 (should(equal 'd
(cadr (assoc 4 results
)))))))
205 (ert-deftest test-org-babel
/parse-header-args
()
206 (org-test-with-temp-text-in-file "
208 #+begin_src example-lang :session :results output :var num=9
212 (org-babel-next-src-block)
213 (let* ((info (org-babel-get-src-block-info))
214 (params (nth 2 info
)))
215 (message "%S" params
)
216 (should (equal "example-lang" (nth 0 info
)))
217 (should (string= "the body" (org-babel-trim (nth 1 info
))))
218 (should-not (member '(:session\ \ \ \
) params
))
219 (should (equal '(:session
) (assoc :session params
)))
220 (should (equal '(:result-type . output
) (assoc :result-type params
)))
221 (should (equal '(num .
9) (cdr (assoc :var params
)))))))
223 (ert-deftest test-org-babel
/parse-header-args2
()
224 (org-test-with-temp-text-in-file "
226 * resolving sub-trees as references
228 #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
232 #+begin_src org :noweb yes
234 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
237 ** simple subtree with custom ID
239 :CUSTOM_ID: simple-subtree
243 (should (string-match (regexp-quote "this is simple")
244 (org-babel-ref-resolve "simple-subtree")))
245 (org-babel-next-src-block)
246 (should (= 14 (org-babel-execute-src-block)))))
248 (ert-deftest test-org-babel
/inline-src-blocks
()
249 (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
250 (macrolet ((at-next (&rest body
)
253 (re-search-forward org-babel-inline-src-block-regexp nil t
)
254 (goto-char (match-beginning 1))
255 (save-match-data ,@body
))))
256 (at-next (should (equal 1 (org-babel-execute-src-block))))
257 (at-next (should (equal 2 (org-babel-execute-src-block))))
258 (at-next (should (equal 3 (org-babel-execute-src-block)))))))
260 (ert-deftest test-org-babel
/org-babel-get-inline-src-block-matches
()
261 (org-test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74"
262 (let ((test-point (point)))
263 (should (fboundp 'org-babel-get-inline-src-block-matches
))
264 (should (re-search-forward "src_" nil t
)) ;; 1
265 (should (org-babel-get-inline-src-block-matches))
266 (should (re-search-forward "}" nil
(point-at-bol))) ;; 1
267 (should-not (org-babel-get-inline-src-block-matches))
268 (should (re-search-forward "in" nil t
)) ;; 2
269 (should-not (org-babel-get-inline-src-block-matches))
270 (should (re-search-forward "echo" nil t
)) ;; 2
271 (should (org-babel-get-inline-src-block-matches))
272 (should (re-search-forward "blocks" nil t
)) ;; 3
273 (backward-char 8) ;; 3
274 (should (org-babel-get-inline-src-block-matches))
276 (should-not (org-babel-get-inline-src-block-matches))
277 (should (re-search-forward ":results" nil t
)) ;; 4
278 (should (org-babel-get-inline-src-block-matches))
280 (should-not (org-babel-get-inline-src-block-matches)))))
282 (ert-deftest test-org-babel
/inline-src_blk-default-results-replace-line-1
()
283 (let ((test-line "src_sh{echo 1}"))
284 ;; src_ at bol line 1...
285 (org-test-with-temp-text
287 (goto-char (point-min)) (org-ctrl-c-ctrl-c)
289 (concat test-line
" =1=")
290 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
291 (forward-char) (org-ctrl-c-ctrl-c)
293 (concat test-line
" =1= =1=")
294 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
295 (re-search-forward "1}")
296 (should-error (org-ctrl-c-ctrl-c))
297 (backward-char) ;; last char of block body
300 (concat test-line
" =1= =1= =1=")
301 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
302 ;; src_ follows space line 1...
303 (let ((test-line " src_emacs-lisp{ 1 }"))
304 (org-test-with-temp-text
306 (should-error (org-ctrl-c-ctrl-c))
307 (forward-char) (org-ctrl-c-ctrl-c)
309 (concat test-line
" =1=")
310 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
311 (re-search-forward "{ 1 ") (org-ctrl-c-ctrl-c)
313 (concat test-line
" =1= =1=")
314 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
316 (should-error (org-ctrl-c-ctrl-c))))))
318 (ert-deftest test-org-babel
/inline-src_blk-default-results-replace-line-2
()
319 ;; src_ at bol line 2...
320 (let ((test-line " src_emacs-lisp{ \"x\" }"))
321 (org-test-with-temp-text
322 (concat "\n" test-line
)
323 (should-error (org-ctrl-c-ctrl-c))
324 (goto-char (point-min))
325 (should-error (org-ctrl-c-ctrl-c))
327 (should-error (org-ctrl-c-ctrl-c))
328 (forward-char) (org-ctrl-c-ctrl-c)
330 (concat test-line
" =x=")
331 (buffer-substring-no-properties
332 (point-at-bol) (point-at-eol))))))
334 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }"))
335 (org-test-with-temp-text
337 (goto-char (point-max))
338 (insert (concat "\n" test-line
" end"))
339 (re-search-backward "src") (org-ctrl-c-ctrl-c)
341 (concat test-line
" =y= end")
342 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
343 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
345 (concat test-line
" =y= =y= end")
346 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
348 (should-error (org-ctrl-c-ctrl-c)))))
350 (ert-deftest test-org-babel
/inline-src_blk-manual-results-replace
()
351 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }"))
352 (org-test-with-temp-text
353 (concat "\n" test-line
)
354 (should-error (org-ctrl-c-ctrl-c))
355 (goto-char (point-max))
356 (should-error (org-ctrl-c-ctrl-c))
358 (should-error (org-ctrl-c-ctrl-c))
359 (forward-char) (org-ctrl-c-ctrl-c)
361 (concat test-line
" =x=")
362 (buffer-substring-no-properties
363 (point-at-bol) (point-at-eol))))))
365 (let ((test-line (concat " Some text prior to block "
366 "src_emacs-lisp[:results replace]{ \"y\" }")))
367 (org-test-with-temp-text test-line
368 (goto-char (point-max))
369 (insert (concat "\n" test-line
" end"))
370 (re-search-backward "src") (org-ctrl-c-ctrl-c)
372 (concat test-line
" =y= end")
373 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
374 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
376 (concat test-line
" =y= =y= end")
377 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
379 (should-error (org-ctrl-c-ctrl-c)))))
381 (ert-deftest test-org-babel
/inline-src_blk-results-silent
()
382 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
383 (org-test-with-temp-text test-line
385 (should (string= test-line
386 (buffer-substring-no-properties
387 (point-at-bol) (point-at-eol))))
389 (should-error (org-ctrl-c-ctrl-c))))
390 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
391 "[ :results silent ]{ \"y\" }")))
392 (org-test-with-temp-text
394 (goto-char (point-max))
395 (insert (concat "\n" test-line
" end"))
396 (re-search-backward "src_") (org-ctrl-c-ctrl-c)
397 (should (string= (concat test-line
" end")
398 (buffer-substring-no-properties
399 (point-at-bol) (point-at-eol))))
400 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
401 (should (string= (concat test-line
" end")
402 (buffer-substring-no-properties
403 (point-at-bol) (point-at-eol))))
405 (should-error (org-ctrl-c-ctrl-c)))))
407 (ert-deftest test-org-babel
/inline-src_blk-results-raw
()
408 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
409 (org-test-with-temp-text test-line
411 (should (string= (concat test-line
" x")
413 (let ((test-line (concat " Some text prior to block "
414 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
415 (org-test-with-temp-text (concat test-line
" end")
416 (re-search-forward "src_") (org-ctrl-c-ctrl-c)
417 (should (string= (concat test-line
" the end")
418 (buffer-substring-no-properties
419 (point-at-bol) (point-at-eol))))
420 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
421 (should (string= (concat test-line
" the the end")
422 (buffer-substring-no-properties
423 (point-at-bol) (point-at-eol))))
425 (should-error (org-ctrl-c-ctrl-c)))))
427 (ert-deftest test-org-babel
/inline-src_blk-results-file
()
428 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
429 (org-test-with-temp-text
432 (should (string= (concat test-line
" [[file:~/test-file]]")
433 (buffer-substring-no-properties
434 (point-min) (point-max)))))))
436 (ert-deftest test-org-babel
/inline-src_blk-results-scalar
()
437 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }"))
438 (org-test-with-temp-text
441 (should (string= (concat test-line
" =\"x\"=")
442 (buffer-substring-no-properties
443 (point-min) (point-max)))))))
445 (ert-deftest test-org-babel
/inline-src_blk-results-verbatim
()
446 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
447 (org-test-with-temp-text
450 (should (string= (concat test-line
" =\"x\"=")
451 (buffer-substring-no-properties
452 (point-min) (point-max)))))))
454 (ert-deftest test-org-babel
/combining-scalar-and-raw-result-types
()
455 (org-test-with-temp-text-in-file "
457 #+begin_src sh :results scalar
458 echo \"[[file:./cv.cls]]\"
464 #+begin_src sh :results raw scalar
465 echo \"[[file:./cv.cls]]\"
468 (flet ((next-result ()
469 (org-babel-next-src-block)
470 (org-babel-execute-src-block)
471 (goto-char (org-babel-where-is-src-block-result))
473 (goto-char (point-min))
475 (should (org-babel-in-example-or-verbatim))
477 (should (not (org-babel-in-example-or-verbatim))))))
479 (ert-deftest test-org-babel
/no-defaut-value-for-var
()
480 "Test that the absence of a default value for a variable DOES THROW
482 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
483 (org-babel-next-src-block)
485 (should-error (org-babel-execute-src-block) :type
'error
)))
489 "Variable \"x\" must be assigned a default value")
492 (ert-deftest test-org-babel
/just-one-results-block
()
493 "Test that evaluating two times the same code block does not result in a
494 duplicate results block."
495 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
496 (org-babel-execute-src-block)
497 (org-babel-execute-src-block) ; second code block execution
498 (should (search-forward "Hello")) ; the string inside the source code block
499 (should (search-forward "Hello")) ; the same string in the results block
500 (should-error (search-forward "Hello"))))
502 (ert-deftest test-org-babel
/nested-code-block
()
503 "Test nested code blocks inside code blocks don't cause problems."
504 (org-test-with-temp-text "#+begin_src org :results silent
505 ,#+begin_src emacs-lisp
509 (should (string= (org-babel-execute-src-block)
510 "#+begin_src emacs-lisp\n 'foo\n#+end_src"))))
512 (ert-deftest test-org-babel
/partial-nested-code-block
()
513 "Test nested code blocks inside code blocks don't cause problems."
514 (org-test-with-temp-text "#+begin_src org :results silent
515 ,#+begin_src emacs-lisp
517 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
519 (ert-deftest test-ob
/does-not-replace-a-block-with-the-results
()
520 (org-test-with-temp-text "#+NAME: foo
521 #+BEGIN_SRC emacs-lisp
524 (org-babel-next-src-block 1)
525 (should (eq 'foo
(org-babel-execute-src-block)))
526 (goto-char (point-min))
527 (org-babel-next-src-block 1)
528 (should (looking-at org-babel-src-block-regexp
))))
530 (ert-deftest test-ob
/catches-all-references
()
531 (org-test-with-temp-text "
532 #+NAME: literal-example
538 #+NAME: read-literal-example
539 #+BEGIN_SRC emacs-lisp :var x=literal-example
540 (concatenate 'string x \" for me.\")
542 (org-babel-next-src-block 1)
543 (should (string= (org-babel-execute-src-block)
544 "A literal example\non two lines for me."))))
546 (ert-deftest test-ob
/resolve-code-blocks-before-data-blocks
()
547 (org-test-with-temp-text "
552 #+begin_src emacs-lisp
556 #+begin_src emacs-lisp :var foo=foo
559 (org-babel-next-src-block 2)
560 (should (string= (org-babel-execute-src-block) "baz"))))
562 (ert-deftest test-ob
/do-not-resolve-to-partial-names-data
()
563 (org-test-with-temp-text "
572 #+begin_src emacs-lisp :var x=base
575 (org-babel-next-src-block 1)
576 (should (equal (org-babel-execute-src-block) '((3) (4))))))
578 (ert-deftest test-ob
/do-not-resolve-to-partial-names-code
()
579 (org-test-with-temp-text "
581 #+begin_src emacs-lisp
586 #+begin_src emacs-lisp
590 #+begin_src emacs-lisp :var x=base
593 (org-babel-next-src-block 3)
594 (should (equal (org-babel-execute-src-block) "foo"))))
596 (ert-deftest test-ob
/allow-spaces-around-
=-in-var-specs
()
597 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
601 (should (= 10 (org-babel-execute-src-block)))))
603 (ert-deftest test-ob
/org-babel-update-intermediate
()
604 (org-test-with-temp-text "#+name: foo
605 #+begin_src emacs-lisp
612 #+begin_src emacs-lisp :var it=foo
615 (let ((org-babel-update-intermediate nil
))
616 (goto-char (point-min))
617 (org-babel-next-src-block 2)
618 (should (= 3 (org-babel-execute-src-block)))
619 (goto-char (point-min))
621 (should (looking-at ": 4")))
622 (let ((org-babel-update-intermediate t
))
623 (goto-char (point-min))
624 (org-babel-next-src-block 2)
625 (should (= 3 (org-babel-execute-src-block)))
626 (goto-char (point-min))
628 (should (looking-at ": 2")))))
630 (ert-deftest test-ob
/eval-header-argument
()
631 (flet ((check-eval (eval runp
)
632 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
635 (let ((foo :not-run
))
637 (progn (should (org-babel-execute-src-block))
638 (should (eq foo
:evald
)))
639 (progn (should-not (org-babel-execute-src-block))
640 (should-not (eq foo
:evald
))))))))
641 (check-eval "never" nil
)
642 (check-eval "no" nil
)
643 (check-eval "never-export" t
)
644 (check-eval "no-export" t
)
645 (let ((org-current-export-file "something"))
646 (check-eval "never" nil
)
647 (check-eval "no" nil
)
648 (check-eval "never-export" nil
)
649 (check-eval "no-export" nil
))))
651 (ert-deftest test-ob
/noweb-expansion-1
()
652 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
660 (should (string= (org-babel-expand-noweb-references) "bar"))))
662 (ert-deftest test-ob
/noweb-expansion-2
()
663 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
668 #+begin_src sh :noweb-sep \"\"
672 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
675 (should (string= (org-babel-expand-noweb-references) "barbaz"))))
677 (ert-deftest test-ob
/splitting-variable-lists-in-references
()
678 (org-test-with-temp-text ""
679 (should (= 1 (length (org-babel-ref-split-args
680 "a=\"this, no work\""))))
681 (should (= 2 (length (org-babel-ref-split-args
682 "a=\"this, no work\", b=1"))))))
684 (ert-deftest test-ob
/org-babel-balanced-split
()
686 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
687 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
690 (ert-deftest test-ob
/commented-last-block-line-no-var
()
691 (org-test-with-temp-text-in-file "
692 #+begin_src emacs-lisp
695 (org-babel-next-src-block)
697 (should (re-search-forward "\\#\\+results:" nil t
))
702 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
703 (org-test-with-temp-text-in-file "
704 #+begin_src emacs-lisp
707 (org-babel-next-src-block)
709 (should (re-search-forward "\\#\\+results:" nil t
))
714 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
716 (ert-deftest test-ob
/commented-last-block-line-with-var
()
717 (org-test-with-temp-text-in-file "
718 #+begin_src emacs-lisp :var a=1
721 (org-babel-next-src-block)
723 (re-search-forward "\\#\\+results:" nil t
)
727 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
728 (org-test-with-temp-text-in-file "
729 #+begin_src emacs-lisp :var a=2
732 (org-babel-next-src-block)
734 (re-search-forward "\\#\\+results:" nil t
)
738 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
740 (defun test-ob-verify-result-and-removed-result (result buffer-text
)
741 "Test helper function to test `org-babel-remove-result'.
742 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
743 and the result of execution is verified against RESULT.
745 The block is actually executed /twice/ to ensure result
746 replacement happens correctly."
747 (org-test-with-temp-text
749 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
750 (should (re-search-forward "\\#\\+results:" nil t
))
752 (should (string= result
753 (buffer-substring-no-properties
755 (- (point-max) 16))))
756 (org-babel-previous-src-block) (org-babel-remove-result)
757 (should (string= buffer-text
758 (buffer-substring-no-properties
759 (point-min) (point-max))))))
761 (ert-deftest test-ob
/org-babel-remove-result--results-default
()
762 "Test `org-babel-remove-result' with default :results."
763 (mapcar (lambda (language)
764 (test-ob-verify-result-and-removed-result
767 "* org-babel-remove-result
768 #+begin_src " language
"
772 '("sh" "emacs-lisp")))
774 (ert-deftest test-ob
/org-babel-remove-result--results-list
()
775 "Test `org-babel-remove-result' with :results list."
776 (test-ob-verify-result-and-removed-result
782 "* org-babel-remove-result
783 #+begin_src emacs-lisp :results list
789 (ert-deftest test-ob
/org-babel-results-indented-wrap
()
790 "Ensure that wrapped results are inserted correction when indented.
791 If not inserted correctly then the second evaluation will fail
792 trying to find the :END: marker."
793 (org-test-with-temp-text
795 #+begin_src sh :results file wrap
798 (org-babel-next-src-block 1)
799 (org-babel-execute-src-block)
800 (org-babel-execute-src-block)))
802 (ert-deftest test-ob
/file-desc-header-argument
()
803 "Test that the :file-desc header argument is used."
804 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
808 #+begin_src emacs-lisp :results file :file-desc
811 (org-babel-execute-src-block)
812 (org-babel-next-src-block 1)
813 (org-babel-execute-src-block)
814 (goto-char (point-min))
815 (should (search-forward "[[file:foo][bar]]" nil t
))
816 (should (search-forward "[[file:foo][foo]]" nil t
))))
818 (ert-deftest test-ob
/org-babel-remove-result--results-pp
()
819 "Test `org-babel-remove-result' with :results pp."
820 (test-ob-verify-result-and-removed-result
821 ": \"I /am/ working!\""
823 "* org-babel-remove-result
824 #+begin_src emacs-lisp :results pp
830 (ert-deftest test-org-babel
/inline-src_blk-preceded-punct-preceded-by-point
()
831 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
832 (org-test-with-temp-text
836 (should (re-search-forward "=\"x\"=" nil t
))
839 (ert-deftest test-ob
/commented-last-block-line-with-var
()
840 (org-test-with-temp-text-in-file "
841 #+begin_src emacs-lisp :var a=1
844 (org-babel-next-src-block)
846 (re-search-forward "\\#\\+results:" nil t
)
850 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
851 (org-test-with-temp-text-in-file "
852 #+begin_src emacs-lisp :var a=2
855 (org-babel-next-src-block)
857 (re-search-forward "\\#\\+results:" nil t
)
861 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
863 (defun test-ob-verify-result-and-removed-result (result buffer-text
)
864 "Test helper function to test `org-babel-remove-result'.
865 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
866 and the result of execution is verified against RESULT.
868 The block is actually executed /twice/ to ensure result
869 replacement happens correctly."
870 (org-test-with-temp-text
872 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
873 (should (re-search-forward "\\#\\+results:" nil t
))
875 (should (string= result
876 (buffer-substring-no-properties
878 (- (point-max) 16))))
879 (org-babel-previous-src-block) (org-babel-remove-result)
880 (should (string= buffer-text
881 (buffer-substring-no-properties
882 (point-min) (point-max))))))
884 (ert-deftest test-ob
/org-babel-remove-result--results-default
()
885 "Test `org-babel-remove-result' with default :results."
886 (mapcar (lambda (language)
887 (test-ob-verify-result-and-removed-result
890 "* org-babel-remove-result
891 #+begin_src " language
"
895 '("sh" "emacs-lisp")))
897 (ert-deftest test-ob
/org-babel-remove-result--results-list
()
898 "Test `org-babel-remove-result' with :results list."
899 (test-ob-verify-result-and-removed-result
905 "* org-babel-remove-result
906 #+begin_src emacs-lisp :results list
912 (ert-deftest test-ob
/org-babel-remove-result--results-wrap
()
913 "Test `org-babel-remove-result' with :results wrap."
914 (test-ob-verify-result-and-removed-result
919 "* org-babel-remove-result
921 #+begin_src emacs-lisp :results wrap
927 (ert-deftest test-ob
/org-babel-remove-result--results-org
()
928 "Test `org-babel-remove-result' with :results org."
929 (test-ob-verify-result-and-removed-result
936 "* org-babel-remove-result
937 #+begin_src emacs-lisp :results org
945 (ert-deftest test-ob
/org-babel-remove-result--results-html
()
946 "Test `org-babel-remove-result' with :results html."
947 (test-ob-verify-result-and-removed-result
949 <head><body></body></head>
952 "* org-babel-remove-result
953 #+begin_src emacs-lisp :results html
954 \"<head><body></body></head>\"
959 (ert-deftest test-ob
/org-babel-remove-result--results-latex
()
960 "Test `org-babel-remove-result' with :results latex."
961 (test-ob-verify-result-and-removed-result
968 "* org-babel-remove-result
969 #+begin_src emacs-lisp :results latex
977 (ert-deftest test-ob
/org-babel-remove-result--results-code
()
978 "Test `org-babel-remove-result' with :results code."
980 (test-ob-verify-result-and-removed-result
981 "#+BEGIN_SRC emacs-lisp
985 "* org-babel-remove-result
986 #+begin_src emacs-lisp :results code
987 (message \"I am working!\")
992 (ert-deftest test-ob
/org-babel-remove-result--results-pp
()
993 "Test `org-babel-remove-result' with :results pp."
994 (test-ob-verify-result-and-removed-result
995 ": \"I /am/ working!\""
997 "* org-babel-remove-result
998 #+begin_src emacs-lisp :results pp
1004 (ert-deftest test-ob
/results-do-not-replace-code-blocks
()
1005 (org-test-with-temp-text "Block two has a space after the name.
1008 #+begin_src emacs-lisp
1010 #+end_src emacs-lisp
1013 #+begin_src emacs-lisp
1018 #+begin_src emacs-lisp
1025 (dolist (num '(1 2 3))
1026 ;; execute the block
1027 (goto-char (point-min))
1028 (org-babel-next-src-block num
) (org-babel-execute-src-block)
1029 ;; check the results
1030 (goto-char (point-max))
1031 (move-beginning-of-line 0)
1032 (should (looking-at (format ": %d" num
))))))
1034 (ert-deftest test-ob
/blocks-with-spaces
()
1035 "Test expansion of blocks followed by blank lines."
1037 (equal "#+BEGIN_SRC emacs-lisp
1043 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1046 (org-babel-execute-src-block)
1049 (ert-deftest test-ob
/results-in-narrowed-buffer
()
1050 "Test block execution in a narrowed buffer."
1051 ;; If results don't exist, they should be inserted in visible part
1055 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1056 (org-test-with-temp-text
1057 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1058 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1059 (org-babel-execute-src-block)
1060 (org-trim (buffer-string)))))
1063 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1064 (org-test-with-temp-text
1065 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1066 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1067 (org-babel-execute-src-block)
1068 (org-trim (buffer-string)))))
1069 ;; Results in visible part of buffer, should be updated here.
1073 #+BEGIN_SRC emacs-lisp
1079 (org-test-with-temp-text
1081 #+BEGIN_SRC emacs-lisp
1089 (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
1090 (org-babel-execute-src-block)
1091 (org-trim (buffer-string)))))
1092 ;; Results in invisible part of buffer, should be updated there.
1093 (org-test-with-temp-text
1095 #+BEGIN_SRC emacs-lisp
1103 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1104 (org-babel-execute-src-block)
1105 (should-not (re-search-forward "^#\\+RESULTS:" nil t
))
1107 (should (should (re-search-forward "^: 3" nil t
)))))
1109 (ert-deftest test-ob
/specific-colnames
()
1110 "Test passing specific column names."
1112 (equal "#+name: input-table
1118 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1129 (org-test-with-temp-text
1130 "#+name: input-table
1136 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1140 ;; we should find a code block
1141 (should (re-search-forward org-babel-src-block-regexp nil t
))
1142 (goto-char (match-beginning 0))
1143 ;; now that we've located the code block, it may be evaluated
1144 (org-babel-execute-src-block)
1147 (ert-deftest test-ob
/location-of-header-arg-eval
()
1148 "Test location of header argument evaluation."
1149 (org-test-with-temp-text "
1151 #+begin_src emacs-lisp :var pt=(point)
1155 #+name: bottom-block
1156 #+begin_src emacs-lisp :var pt=top-block()
1160 ;; the value of the second block should be greater than the first
1162 (< (progn (re-search-forward org-babel-src-block-regexp nil t
)
1163 (goto-char (match-beginning 0))
1164 (prog1 (save-match-data (org-babel-execute-src-block))
1165 (goto-char (match-end 0))))
1166 (progn (re-search-forward org-babel-src-block-regexp nil t
)
1167 (goto-char (match-beginning 0))
1168 (org-babel-execute-src-block))))))
1170 (ert-deftest test-ob
/preserve-results-indentation
()
1171 "Preserve indentation when executing a src block."
1174 (org-test-with-temp-text
1175 " #+begin_src emacs-lisp\n (+ 1 1)\n #+end_src"
1176 (org-babel-execute-src-block)
1178 (let ((case-fold-search t
)) (search-forward "#+results:"))
1179 ;; Check if both #+RESULTS: keyword and actual results are
1180 ;; indented by 2 columns.
1181 (list (org-get-indentation)
1182 (progn (forward-line) (org-get-indentation)))))))
1184 (ert-deftest test-ob
/safe-header-args
()
1185 "Detect safe and unsafe header args."
1186 (let ((safe-args '((:cache .
"foo")
1187 (:results .
"output")
1190 (unsafe-args '((:eval .
"yes")
1191 (:results .
"output file")
1193 (malformed-args '((bar .
"foo")
1196 (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args
)))
1197 (dolist (arg safe-args
)
1198 (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args
)))
1199 (dolist (arg unsafe-args
)
1200 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args
))))
1201 (dolist (arg malformed-args
)
1202 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args
))))
1203 (should (not (funcall safe-p
(append safe-args unsafe-args
))))))
1207 ;;; test-ob ends here