1 ;;; test-ob.el --- tests for ob.el
3 ;; Copyright (c) 2010-2012 Eric Schulte
4 ;; Authors: Eric Schulte, Martyn Jago
6 ;; Released under the GNU General Public License version 3
7 ;; see: http://www.gnu.org/licenses/gpl-3.0.html
8 (ert-deftest test-org-babel
/multi-line-header-regexp
()
9 (should(equal "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
10 org-babel-multi-line-header-regexp
))
11 ;;TODO can be optimised - and what about blah4 blah5 blah6?
13 org-babel-multi-line-header-regexp
14 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
17 "blah1 blah2 blah3 \t"
20 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
22 ;;TODO Check - should this fail?
24 (not (org-test-string-exact-match
25 org-babel-multi-line-header-regexp
26 " \t #+headers : blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))))
28 (ert-deftest test-org-babel
/src-block-regexp
()
31 "#+begin_src language -n-r-a-b -c :argument-1 yes :argument-2 no\n"
32 "echo this is a test\n"
33 "echo Currently in ' $PWD\n"
36 (flags "-n-r-a-b -c ")
37 (arguments ":argument-1 yes :argument-2 no")
38 (body "echo this is a test\necho Currently in ' $PWD\n"))
39 (should (string-match org-babel-src-block-regexp test-block
))
40 (should (string-match org-babel-src-block-regexp
(upcase test-block
)))
41 (should (equal language
(match-string 2 test-block
)))
42 ;;TODO Consider refactoring
43 (should (equal flags
(match-string 3 test-block
)))
44 (should (equal arguments
(match-string 4 test-block
)))
45 (should (equal body
(match-string 5 test-block
)))
47 (should (org-test-string-exact-match
48 org-babel-src-block-regexp
49 (replace-regexp-in-string flags
"" test-block
)))
51 (should (org-test-string-exact-match
52 org-babel-src-block-regexp
53 (replace-regexp-in-string arguments
"" test-block
)))
54 ;; should be valid with no body
55 (should (org-test-string-exact-match
56 org-babel-src-block-regexp
57 (replace-regexp-in-string body
"" test-block
)))))
59 (ert-deftest test-org-babel
/get-header
()
60 (should (not (org-babel-get-header
61 org-babel-default-header-args
:doesnt-exist
)))
62 (should(equal '((:session .
"none"))
64 org-babel-default-header-args
:session
)))
65 (should(equal '((:session .
"none"))
67 org-babel-default-header-args
:session nil
)))
68 (should (not (org-babel-get-header
69 org-babel-default-header-args
:SESSION
)))
70 (should (equal '((:tangle .
"no"))
72 org-babel-default-header-args
:tangle
)))
74 (should (equal org-babel-default-header-args
76 org-babel-default-header-args
:doesnt-exist
'others
)))
77 (should (equal org-babel-default-header-args
79 org-babel-default-header-args nil
'others
)))
83 org-babel-default-header-args
:noweb
'others
)))))
85 (ert-deftest test-org-babel
/default-inline-header-args
()
87 '((:session .
"none") (:results .
"replace") (:exports .
"results"))
88 org-babel-default-inline-header-args
)))
90 (ert-deftest ob-test
/org-babel-combine-header-arg-lists
()
91 (let ((results (org-babel-combine-header-arg-lists
94 (baz .
((foo bar
) (baz)))
95 (qux .
((foo bar baz qux
)))
100 (dolist (pair '((foo .
:any
)
104 (qux .
((foo bar baz qux
)))))
105 (should (equal (cdr pair
)
106 (cdr (assoc (car pair
) results
)))))))
108 ;;; ob-get-src-block-info
109 (ert-deftest test-org-babel
/get-src-block-info-language
()
110 (org-test-at-marker nil org-test-file-ob-anchor
111 (let ((info (org-babel-get-src-block-info)))
112 (should (string= "emacs-lisp" (nth 0 info
))))))
114 (ert-deftest test-org-babel
/get-src-block-info-body
()
115 (org-test-at-marker nil org-test-file-ob-anchor
116 (let ((info (org-babel-get-src-block-info)))
117 (should (string-match (regexp-quote org-test-file-ob-anchor
)
120 (ert-deftest test-org-babel
/get-src-block-info-tangle
()
121 (org-test-at-marker nil org-test-file-ob-anchor
122 (let ((info (org-babel-get-src-block-info)))
123 (should (string= "no" (cdr (assoc :tangle
(nth 2 info
))))))))
125 (ert-deftest test-org-babel
/elisp-in-header-arguments
()
126 "Test execution of elisp forms in header arguments."
127 (org-test-with-temp-text-in-file "
129 * elisp forms in header arguments
133 #+begin_src emacs-lisp
138 (goto-char (point-min))
139 (org-babel-next-src-block)
140 (let ((info (org-babel-get-src-block-info)))
141 (should (= 42 (org-babel-execute-src-block)))))))
143 (ert-deftest test-org-babel
/simple-named-code-block
()
144 "Test that simple named code blocks can be evaluated."
145 (org-test-with-temp-text-in-file "
147 #+name: i-have-a-name
148 #+begin_src emacs-lisp
153 (org-babel-next-src-block 1)
154 (should (= 42 (org-babel-execute-src-block))))))
156 (ert-deftest test-org-babel
/simple-variable-resolution
()
157 "Test that simple variable resolution is working."
158 (org-test-with-temp-text-in-file "
161 #+begin_src emacs-lisp
165 #+begin_src emacs-lisp :var four=four
170 (org-babel-next-src-block 2)
171 (should (= 4 (org-babel-execute-src-block)))
173 (should (string= ": 4" (buffer-substring
177 (ert-deftest test-org-babel
/multi-line-header-arguments
()
178 "Test that multi-line header arguments and can be read."
179 (org-test-with-temp-text-in-file "
181 #+headers: :var letters='(a b c d e f g)
182 #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
184 (defalias 'my-map (if (org-version-check \"24.2.50\" \"cl\" :predicate)
187 (my-map 'list #'list numbers letters)
191 (org-babel-next-src-block)
192 (let ((results (org-babel-execute-src-block)))
193 (should(equal 'a
(cadr (assoc 1 results
))))
194 (should(equal 'd
(cadr (assoc 4 results
))))))))
196 (ert-deftest test-org-babel
/parse-header-args
()
197 (org-test-with-temp-text-in-file "
199 #+begin_src example-lang :session :results output :var num=9
204 (org-babel-next-src-block)
205 (let* ((info (org-babel-get-src-block-info))
206 (params (nth 2 info
)))
207 (message "%S" params
)
208 (should(equal "example-lang" (nth 0 info
)))
209 (should(string= "the body" (org-babel-trim (nth 1 info
))))
210 (should-not (member '(:session\ \ \ \
) params
))
211 (should(equal '(:session
) (assoc :session params
)))
212 (should(equal '(:result-type . output
) (assoc :result-type params
)))
213 (should(equal '(num .
9) (cdr (assoc :var params
))))))))
215 (ert-deftest test-org-babel
/parse-header-args2
()
216 (org-test-with-temp-text-in-file "
218 * resolving sub-trees as references
220 #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
224 #+begin_src org :noweb yes
226 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
229 ** simple subtree with custom ID
231 :CUSTOM_ID: simple-subtree
236 (should (string-match (regexp-quote "this is simple")
237 (org-babel-ref-resolve "simple-subtree")))
238 (org-babel-next-src-block)
239 (should (= 14 (org-babel-execute-src-block))))))
241 (ert-deftest test-org-babel
/inline-src-blocks
()
242 (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
243 (macrolet ((at-next (&rest body
)
246 (re-search-forward org-babel-inline-src-block-regexp nil t
)
247 (goto-char (match-beginning 1))
248 (save-match-data ,@body
))))
249 (at-next (should (equal 1 (org-babel-execute-src-block))))
250 (at-next (should (equal 2 (org-babel-execute-src-block))))
251 (at-next (should (equal 3 (org-babel-execute-src-block)))))))
253 (ert-deftest test-org-babel
/org-babel-get-inline-src-block-matches
()
254 (org-test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74"
255 (let ((test-point (point)))
256 (should (fboundp 'org-babel-get-inline-src-block-matches
))
257 (should (re-search-forward "src_" nil t
)) ;; 1
258 (should (org-babel-get-inline-src-block-matches))
259 (should (re-search-forward "}" nil
(point-at-bol))) ;; 1
260 (should-not (org-babel-get-inline-src-block-matches))
261 (should (re-search-forward "in" nil t
)) ;; 2
262 (should-not (org-babel-get-inline-src-block-matches))
263 (should (re-search-forward "echo" nil t
)) ;; 2
264 (should (org-babel-get-inline-src-block-matches))
265 (should (re-search-forward "blocks" nil t
)) ;; 3
266 (backward-char 8) ;; 3
267 (should (org-babel-get-inline-src-block-matches))
269 (should-not (org-babel-get-inline-src-block-matches))
270 (should (re-search-forward ":results" nil t
)) ;; 4
271 (should (org-babel-get-inline-src-block-matches))
273 (should-not (org-babel-get-inline-src-block-matches)))))
275 (ert-deftest test-org-babel
/inline-src_blk-default-results-replace-line-1
()
276 (let ((test-line "src_sh{echo 1}"))
277 ;; src_ at bol line 1...
278 (org-test-with-temp-text
280 (goto-char (point-min)) (org-ctrl-c-ctrl-c)
282 (concat test-line
" =1=")
283 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
284 (forward-char) (org-ctrl-c-ctrl-c)
286 (concat test-line
" =1= =1=")
287 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
288 (re-search-forward "1}")
289 (should-error (org-ctrl-c-ctrl-c))
290 (backward-char) ;; last char of block body
293 (concat test-line
" =1= =1= =1=")
294 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
295 ;; src_ follows space line 1...
296 (let ((test-line " src_emacs-lisp{ 1 }"))
297 (org-test-with-temp-text
299 (should-error (org-ctrl-c-ctrl-c))
300 (forward-char) (org-ctrl-c-ctrl-c)
302 (concat test-line
" =1=")
303 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
304 (re-search-forward "{ 1 ") (org-ctrl-c-ctrl-c)
306 (concat test-line
" =1= =1=")
307 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
309 (should-error (org-ctrl-c-ctrl-c)))))
311 (ert-deftest test-org-babel
/inline-src_blk-default-results-replace-line-2
()
312 ;; src_ at bol line 2...
313 (let ((test-line " src_emacs-lisp{ \"x\" }"))
314 (org-test-with-temp-text
315 (concat "\n" test-line
)
316 (should-error (org-ctrl-c-ctrl-c))
317 (goto-char (point-min))
318 (should-error (org-ctrl-c-ctrl-c))
320 (should-error (org-ctrl-c-ctrl-c))
321 (forward-char) (org-ctrl-c-ctrl-c)
323 (concat test-line
" =x=")
324 (buffer-substring-no-properties
325 (point-at-bol) (point-at-eol))))))
327 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }"))
328 (org-test-with-temp-text
330 (goto-char (point-max))
331 (insert (concat "\n" test-line
" end"))
332 (re-search-backward "src") (org-ctrl-c-ctrl-c)
334 (concat test-line
" =y= end")
335 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
336 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
338 (concat test-line
" =y= =y= end")
339 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
341 (should-error (org-ctrl-c-ctrl-c))))))
343 (ert-deftest test-org-babel
/inline-src_blk-manual-results-replace
()
344 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }"))
345 (org-test-with-temp-text
346 (concat "\n" test-line
)
347 (should-error (org-ctrl-c-ctrl-c))
348 (goto-char (point-max))
349 (should-error (org-ctrl-c-ctrl-c))
351 (should-error (org-ctrl-c-ctrl-c))
352 (forward-char) (org-ctrl-c-ctrl-c)
354 (concat test-line
" =x=")
355 (buffer-substring-no-properties
356 (point-at-bol) (point-at-eol))))))
358 (let ((test-line (concat " Some text prior to block "
359 "src_emacs-lisp[:results replace]{ \"y\" }")))
360 (org-test-with-temp-text test-line
361 (goto-char (point-max))
362 (insert (concat "\n" test-line
" end"))
363 (re-search-backward "src") (org-ctrl-c-ctrl-c)
365 (concat test-line
" =y= end")
366 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
367 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
369 (concat test-line
" =y= =y= end")
370 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
372 (should-error (org-ctrl-c-ctrl-c)))))
374 (ert-deftest test-org-babel
/inline-src_blk-results-silent
()
375 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
376 (org-test-with-temp-text test-line
378 (should (string= test-line
379 (buffer-substring-no-properties
380 (point-at-bol) (point-at-eol))))
382 (should-error (org-ctrl-c-ctrl-c))))
383 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
384 "[ :results silent ]{ \"y\" }")))
385 (org-test-with-temp-text
387 (goto-char (point-max))
388 (insert (concat "\n" test-line
" end"))
389 (re-search-backward "src_") (org-ctrl-c-ctrl-c)
390 (should (string= (concat test-line
" end")
391 (buffer-substring-no-properties
392 (point-at-bol) (point-at-eol))))
393 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
394 (should (string= (concat test-line
" end")
395 (buffer-substring-no-properties
396 (point-at-bol) (point-at-eol))))
398 (should-error (org-ctrl-c-ctrl-c)))))
400 (ert-deftest test-org-babel
/inline-src_blk-results-raw
()
401 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
402 (org-test-with-temp-text test-line
404 (should (string= (concat test-line
" x")
406 (let ((test-line (concat " Some text prior to block "
407 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
408 (org-test-with-temp-text (concat test-line
" end")
409 (re-search-forward "src_") (org-ctrl-c-ctrl-c)
410 (should (string= (concat test-line
" the end")
411 (buffer-substring-no-properties
412 (point-at-bol) (point-at-eol))))
413 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
414 (should (string= (concat test-line
" the the end")
415 (buffer-substring-no-properties
416 (point-at-bol) (point-at-eol))))
418 (should-error (org-ctrl-c-ctrl-c)))))
420 (ert-deftest test-org-babel
/inline-src_blk-results-file
()
421 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
422 (org-test-with-temp-text
425 (should (string= (concat test-line
" [[file:~/test-file]]")
426 (buffer-substring-no-properties
427 (point-min) (point-max)))))))
429 (ert-deftest test-org-babel
/inline-src_blk-results-scalar
()
430 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }"))
431 (org-test-with-temp-text
434 (should (string= (concat test-line
" =\"x\"=")
435 (buffer-substring-no-properties
436 (point-min) (point-max)))))))
438 (ert-deftest test-org-babel
/inline-src_blk-results-verbatim
()
439 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
440 (org-test-with-temp-text
443 (should (string= (concat test-line
" =\"x\"=")
444 (buffer-substring-no-properties
445 (point-min) (point-max)))))))
447 (ert-deftest test-org-babel
/combining-scalar-and-raw-result-types
()
448 (org-test-with-temp-text-in-file "
450 #+begin_src sh :results scalar
451 echo \"[[file:./cv.cls]]\"
457 #+begin_src sh :results raw scalar
458 echo \"[[file:./cv.cls]]\"
461 (flet ((next-result ()
462 (org-babel-next-src-block)
463 (org-babel-execute-src-block)
464 (goto-char (org-babel-where-is-src-block-result))
466 (goto-char (point-min))
468 (should (org-babel-in-example-or-verbatim))
470 (should (not (org-babel-in-example-or-verbatim))))))
472 (ert-deftest test-org-babel
/no-defaut-value-for-var
()
473 "Test that the absence of a default value for a variable DOES THROW
475 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
476 (org-babel-next-src-block)
478 (should-error (org-babel-execute-src-block) :type
'error
)))
482 "variable \"x\" in block \"carre\" must be assigned a default value")
485 (ert-deftest test-org-babel
/just-one-results-block
()
486 "Test that evaluating two times the same code block does not result in a
487 duplicate results block."
488 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
489 (org-babel-execute-src-block)
490 (org-babel-execute-src-block) ; second code block execution
491 (should (search-forward "Hello")) ; the string inside the source code block
492 (should (search-forward "Hello")) ; the same string in the results block
493 (should-error (search-forward "Hello"))))
495 (ert-deftest test-org-babel
/nested-code-block
()
496 "Test nested code blocks inside code blocks don't cause problems."
497 (org-test-with-temp-text "#+begin_src org :results silent
498 ,#+begin_src emacs-lisp
502 (should (string= (org-babel-execute-src-block)
503 "#+begin_src emacs-lisp\n 'foo\n#+end_src"))))
505 (ert-deftest test-org-babel
/partial-nested-code-block
()
506 "Test nested code blocks inside code blocks don't cause problems."
507 (org-test-with-temp-text "#+begin_src org :results silent
508 ,#+begin_src emacs-lisp
510 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
512 (ert-deftest test-ob
/does-not-replace-a-block-with-the-results
()
513 (org-test-with-temp-text "#+NAME: foo
514 #+BEGIN_SRC emacs-lisp
517 (org-babel-next-src-block 1)
518 (should (eq 'foo
(org-babel-execute-src-block)))
519 (goto-char (point-min))
520 (org-babel-next-src-block 1)
521 (should (looking-at org-babel-src-block-regexp
))))
523 (ert-deftest test-ob
/catches-all-references
()
524 (org-test-with-temp-text "
525 #+NAME: literal-example
531 #+NAME: read-literal-example
532 #+BEGIN_SRC emacs-lisp :var x=literal-example
533 (concatenate 'string x \" for me.\")
535 (org-babel-next-src-block 1)
536 (should (string= (org-babel-execute-src-block)
537 "A literal example\non two lines for me."))))
539 (ert-deftest test-ob
/resolve-code-blocks-before-data-blocks
()
540 (org-test-with-temp-text "
545 #+begin_src emacs-lisp
549 #+begin_src emacs-lisp :var foo=foo
552 (org-babel-next-src-block 2)
553 (should (string= (org-babel-execute-src-block) "baz"))))
555 (ert-deftest test-ob
/do-not-resolve-to-partial-names-data
()
556 (org-test-with-temp-text "
565 #+begin_src emacs-lisp :var x=base
568 (org-babel-next-src-block 1)
569 (should (equal (org-babel-execute-src-block) '((3) (4))))))
571 (ert-deftest test-ob
/do-not-resolve-to-partial-names-code
()
572 (org-test-with-temp-text "
574 #+begin_src emacs-lisp
579 #+begin_src emacs-lisp
583 #+begin_src emacs-lisp :var x=base
586 (org-babel-next-src-block 3)
587 (should (equal (org-babel-execute-src-block) "foo"))))
589 (ert-deftest test-ob
/allow-spaces-around-
=-in-var-specs
()
590 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
594 (should (= 10 (org-babel-execute-src-block)))))
596 (ert-deftest test-ob
/org-babel-update-intermediate
()
597 (org-test-with-temp-text "#+name: foo
598 #+begin_src emacs-lisp
605 #+begin_src emacs-lisp :var it=foo
608 (let ((org-babel-update-intermediate nil
))
609 (goto-char (point-min))
610 (org-babel-next-src-block 2)
611 (should (= 3 (org-babel-execute-src-block)))
612 (goto-char (point-min))
614 (should (looking-at ": 4")))
615 (let ((org-babel-update-intermediate t
))
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 ": 2")))))
623 (ert-deftest test-ob
/eval-header-argument
()
624 (flet ((check-eval (eval runp
)
625 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
628 (let ((foo :not-run
))
630 (progn (should (org-babel-execute-src-block))
631 (should (eq foo
:evald
)))
632 (progn (should-not (org-babel-execute-src-block))
633 (should-not (eq foo
:evald
))))))))
634 (check-eval "never" nil
)
635 (check-eval "no" nil
)
636 (check-eval "never-export" t
)
637 (check-eval "no-export" t
)
638 (let ((org-current-export-file "something"))
639 (check-eval "never" nil
)
640 (check-eval "no" nil
)
641 (check-eval "never-export" nil
)
642 (check-eval "no-export" nil
))))
644 (ert-deftest test-ob
/noweb-expansion-1
()
645 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
653 (should (string= (org-babel-expand-noweb-references) "bar"))))
655 (ert-deftest test-ob
/noweb-expansion-2
()
656 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
661 #+begin_src sh :noweb-sep \"\"
665 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
668 (should (string= (org-babel-expand-noweb-references) "barbaz"))))
670 (ert-deftest test-ob
/splitting-variable-lists-in-references
()
671 (org-test-with-temp-text ""
672 (should (= 1 (length (org-babel-ref-split-args
673 "a=\"this, no work\""))))
674 (should (= 2 (length (org-babel-ref-split-args
675 "a=\"this, no work\", b=1"))))))
677 (ert-deftest test-ob
/org-babel-balanced-split
()
679 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
680 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
683 (ert-deftest test-ob
/commented-last-block-line-no-var
()
684 (org-test-with-temp-text-in-file "
685 #+begin_src emacs-lisp
689 (org-babel-next-src-block)
691 (should (re-search-forward "\\#\\+results:" nil t
))
696 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
697 (org-test-with-temp-text-in-file "
698 #+begin_src emacs-lisp
703 (org-babel-next-src-block)
705 (should (re-search-forward "\\#\\+results:" nil t
))
710 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
712 (ert-deftest test-ob
/commented-last-block-line-with-var
()
713 (org-test-with-temp-text-in-file "
714 #+begin_src emacs-lisp :var a=1
718 (org-babel-next-src-block)
720 (re-search-forward "\\#\\+results:" nil t
)
724 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
725 (org-test-with-temp-text-in-file "
726 #+begin_src emacs-lisp :var a=2
730 (org-babel-next-src-block)
732 (re-search-forward "\\#\\+results:" nil t
)
736 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
738 (defun test-ob-verify-result-and-removed-result (result buffer-text
)
739 "Test helper function to test `org-babel-remove-result'.
740 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
741 and the result of execution is verified against RESULT.
743 The block is actually executed /twice/ to ensure result
744 replacement happens correctly."
745 (org-test-with-temp-text
748 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
749 (should (re-search-forward "\\#\\+results:" nil t
))
751 (should (string= result
752 (buffer-substring-no-properties
754 (- (point-max) 16))))
755 (org-babel-previous-src-block) (org-babel-remove-result)
756 (should (string= buffer-text
757 (buffer-substring-no-properties
758 (point-min) (point-max)))))))
760 (ert-deftest test-ob
/org-babel-remove-result--results-default
()
761 "Test `org-babel-remove-result' with default :results."
762 (mapcar (lambda (language)
763 (test-ob-verify-result-and-removed-result
766 "* org-babel-remove-result
767 #+begin_src " language
"
771 '("sh" "emacs-lisp")))
773 (ert-deftest test-ob
/org-babel-remove-result--results-list
()
774 "Test `org-babel-remove-result' with :results list."
775 (test-ob-verify-result-and-removed-result
781 "* org-babel-remove-result
782 #+begin_src emacs-lisp :results list
788 (ert-deftest test-ob
/org-babel-results-indented-wrap
()
789 "Ensure that wrapped results are inserted correction when indented.
790 If not inserted correctly then the second evaluation will fail
791 trying to find the :END: marker."
792 (org-test-with-temp-text
794 #+begin_src sh :results file wrap
797 (org-babel-next-src-block 1)
798 (org-babel-execute-src-block)
799 (org-babel-execute-src-block)))
801 (ert-deftest test-ob
/file-desc-header-argument
()
802 "Test that the :file-desc header argument is used."
803 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
807 #+begin_src emacs-lisp :results file :file-desc
810 (org-babel-execute-src-block)
811 (org-babel-next-src-block 1)
812 (org-babel-execute-src-block)
813 (goto-char (point-min))
814 (should (search-forward "[[file:foo][bar]]" nil t
))
815 (should (search-forward "[[file:foo][foo]]" nil t
))))
817 (ert-deftest test-ob
/org-babel-remove-result--results-pp
()
818 "Test `org-babel-remove-result' with :results pp."
819 (test-ob-verify-result-and-removed-result
820 ": \"I /am/ working!\""
822 "* org-babel-remove-result
823 #+begin_src emacs-lisp :results pp
829 (ert-deftest test-org-babel
/inline-src_blk-preceded-punct-preceded-by-point
()
830 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
831 (org-test-with-temp-text
835 (should (re-search-forward "=\"x\"=" nil t
))
838 (ert-deftest test-ob
/commented-last-block-line-with-var
()
839 (org-test-with-temp-text-in-file "
840 #+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
856 (org-babel-next-src-block)
858 (re-search-forward "\\#\\+results:" nil t
)
862 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
864 (defun test-ob-verify-result-and-removed-result (result buffer-text
)
865 "Test helper function to test `org-babel-remove-result'.
866 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
867 and the result of execution is verified against RESULT.
869 The block is actually executed /twice/ to ensure result
870 replacement happens correctly."
871 (org-test-with-temp-text
874 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
875 (should (re-search-forward "\\#\\+results:" nil t
))
877 (should (string= result
878 (buffer-substring-no-properties
880 (- (point-max) 16))))
881 (org-babel-previous-src-block) (org-babel-remove-result)
882 (should (string= buffer-text
883 (buffer-substring-no-properties
884 (point-min) (point-max)))))))
886 (ert-deftest test-ob
/org-babel-remove-result--results-default
()
887 "Test `org-babel-remove-result' with default :results."
888 (mapcar (lambda (language)
889 (test-ob-verify-result-and-removed-result
892 "* org-babel-remove-result
893 #+begin_src " language
"
897 '("sh" "emacs-lisp")))
899 (ert-deftest test-ob
/org-babel-remove-result--results-list
()
900 "Test `org-babel-remove-result' with :results list."
901 (test-ob-verify-result-and-removed-result
907 "* org-babel-remove-result
908 #+begin_src emacs-lisp :results list
914 (ert-deftest test-ob
/org-babel-remove-result--results-wrap
()
915 "Test `org-babel-remove-result' with :results wrap."
916 (test-ob-verify-result-and-removed-result
921 "* org-babel-remove-result
923 #+begin_src emacs-lisp :results wrap
929 (ert-deftest test-ob
/org-babel-remove-result--results-org
()
930 "Test `org-babel-remove-result' with :results org."
931 (test-ob-verify-result-and-removed-result
938 "* org-babel-remove-result
939 #+begin_src emacs-lisp :results org
947 (ert-deftest test-ob
/org-babel-remove-result--results-html
()
948 "Test `org-babel-remove-result' with :results html."
949 (test-ob-verify-result-and-removed-result
951 <head><body></body></head>
954 "* org-babel-remove-result
955 #+begin_src emacs-lisp :results html
956 \"<head><body></body></head>\"
961 (ert-deftest test-ob
/org-babel-remove-result--results-latex
()
962 "Test `org-babel-remove-result' with :results latex."
963 (test-ob-verify-result-and-removed-result
970 "* org-babel-remove-result
971 #+begin_src emacs-lisp :results latex
979 (ert-deftest test-ob
/org-babel-remove-result--results-code
()
980 "Test `org-babel-remove-result' with :results code."
982 (test-ob-verify-result-and-removed-result
983 "#+BEGIN_SRC emacs-lisp
987 "* org-babel-remove-result
988 #+begin_src emacs-lisp :results code
989 (message \"I am working!\")
994 (ert-deftest test-ob
/org-babel-remove-result--results-pp
()
995 "Test `org-babel-remove-result' with :results pp."
996 (test-ob-verify-result-and-removed-result
997 ": \"I /am/ working!\""
999 "* org-babel-remove-result
1000 #+begin_src emacs-lisp :results pp
1001 \"I /am/ working!\")
1006 (ert-deftest test-ob
/results-do-not-replace-code-blocks
()
1007 (org-test-with-temp-text "Block two has a space after the name.
1010 #+begin_src emacs-lisp
1012 #+end_src emacs-lisp
1015 #+begin_src emacs-lisp
1020 #+begin_src emacs-lisp
1027 (dolist (num '(1 2 3))
1028 ;; execute the block
1029 (goto-char (point-min))
1030 (org-babel-next-src-block num
) (org-babel-execute-src-block)
1031 ;; check the results
1032 (goto-char (point-max))
1033 (move-beginning-of-line 0)
1034 (should (looking-at (format ": %d" num
))))))
1038 ;;; test-ob ends here