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)
183 (map 'list #'list numbers letters)
187 (org-babel-next-src-block)
188 (let ((results (org-babel-execute-src-block)))
189 (should(equal 'a
(cadr (assoc 1 results
))))
190 (should(equal 'd
(cadr (assoc 4 results
))))))))
192 (ert-deftest test-org-babel
/parse-header-args
()
193 (org-test-with-temp-text-in-file "
195 #+begin_src example-lang :session :results output :var num=9
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-babel-trim (nth 1 info
))))
206 (should-not (member '(:session\ \ \ \
) params
))
207 (should(equal '(:session
) (assoc :session params
)))
208 (should(equal '(:result-type . output
) (assoc :result-type params
)))
209 (should(equal '(num .
9) (cdr (assoc :var params
))))))))
211 (ert-deftest test-org-babel
/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
220 #+begin_src org :noweb yes
222 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
225 ** simple subtree with custom ID
227 :CUSTOM_ID: simple-subtree
232 (should (string-match (regexp-quote "this is simple")
233 (org-babel-ref-resolve "simple-subtree")))
234 (org-babel-next-src-block)
235 (should (= 14 (org-babel-execute-src-block))))))
237 (ert-deftest test-org-babel
/inline-src-blocks
()
238 (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
239 (macrolet ((at-next (&rest body
)
242 (re-search-forward org-babel-inline-src-block-regexp nil t
)
243 (goto-char (match-beginning 1))
244 (save-match-data ,@body
))))
245 (at-next (should (equal 1 (org-babel-execute-src-block))))
246 (at-next (should (equal 2 (org-babel-execute-src-block))))
247 (at-next (should (equal 3 (org-babel-execute-src-block)))))))
249 (ert-deftest test-org-babel
/org-babel-get-inline-src-block-matches
()
250 (org-test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74"
251 (let ((test-point (point)))
252 (should (fboundp 'org-babel-get-inline-src-block-matches
))
253 (should (re-search-forward "src_" nil t
)) ;; 1
254 (should (org-babel-get-inline-src-block-matches))
255 (should (re-search-forward "}" nil
(point-at-bol))) ;; 1
256 (should-not (org-babel-get-inline-src-block-matches))
257 (should (re-search-forward "in" nil t
)) ;; 2
258 (should-not (org-babel-get-inline-src-block-matches))
259 (should (re-search-forward "echo" nil t
)) ;; 2
260 (should (org-babel-get-inline-src-block-matches))
261 (should (re-search-forward "blocks" nil t
)) ;; 3
262 (backward-char 8) ;; 3
263 (should (org-babel-get-inline-src-block-matches))
265 (should-not (org-babel-get-inline-src-block-matches))
266 (should (re-search-forward ":results" nil t
)) ;; 4
267 (should (org-babel-get-inline-src-block-matches))
269 (should-not (org-babel-get-inline-src-block-matches)))))
271 (ert-deftest test-org-babel
/inline-src_blk-default-results-replace-line-1
()
272 (let ((test-line "src_sh{echo 1}"))
273 ;; src_ at bol line 1...
274 (org-test-with-temp-text
276 (goto-char (point-min)) (org-ctrl-c-ctrl-c)
278 (concat test-line
" =1=")
279 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
280 (forward-char) (org-ctrl-c-ctrl-c)
282 (concat test-line
" =1= =1=")
283 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
284 (re-search-forward "1}")
285 (should-error (org-ctrl-c-ctrl-c))
286 (backward-char) ;; last char of block body
289 (concat test-line
" =1= =1= =1=")
290 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
291 ;; src_ follows space line 1...
292 (let ((test-line " src_emacs-lisp{ 1 }"))
293 (org-test-with-temp-text
295 (should-error (org-ctrl-c-ctrl-c))
296 (forward-char) (org-ctrl-c-ctrl-c)
298 (concat test-line
" =1=")
299 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
300 (re-search-forward "{ 1 ") (org-ctrl-c-ctrl-c)
302 (concat test-line
" =1= =1=")
303 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
305 (should-error (org-ctrl-c-ctrl-c)))))
307 (ert-deftest test-org-babel
/inline-src_blk-default-results-replace-line-2
()
308 ;; src_ at bol line 2...
309 (let ((test-line " src_emacs-lisp{ \"x\" }"))
310 (org-test-with-temp-text
311 (concat "\n" test-line
)
312 (should-error (org-ctrl-c-ctrl-c))
313 (goto-char (point-min))
314 (should-error (org-ctrl-c-ctrl-c))
316 (should-error (org-ctrl-c-ctrl-c))
317 (forward-char) (org-ctrl-c-ctrl-c)
319 (concat test-line
" =x=")
320 (buffer-substring-no-properties
321 (point-at-bol) (point-at-eol))))))
323 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }"))
324 (org-test-with-temp-text
326 (goto-char (point-max))
327 (insert (concat "\n" test-line
" end"))
328 (re-search-backward "src") (org-ctrl-c-ctrl-c)
330 (concat test-line
" =y= end")
331 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
332 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
334 (concat test-line
" =y= =y= end")
335 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
337 (should-error (org-ctrl-c-ctrl-c))))))
339 (ert-deftest test-org-babel
/inline-src_blk-manual-results-replace
()
340 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }"))
341 (org-test-with-temp-text
342 (concat "\n" test-line
)
343 (should-error (org-ctrl-c-ctrl-c))
344 (goto-char (point-max))
345 (should-error (org-ctrl-c-ctrl-c))
347 (should-error (org-ctrl-c-ctrl-c))
348 (forward-char) (org-ctrl-c-ctrl-c)
350 (concat test-line
" =x=")
351 (buffer-substring-no-properties
352 (point-at-bol) (point-at-eol))))))
354 (let ((test-line (concat " Some text prior to block "
355 "src_emacs-lisp[:results replace]{ \"y\" }")))
356 (org-test-with-temp-text test-line
357 (goto-char (point-max))
358 (insert (concat "\n" test-line
" end"))
359 (re-search-backward "src") (org-ctrl-c-ctrl-c)
361 (concat test-line
" =y= end")
362 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
363 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
365 (concat test-line
" =y= =y= end")
366 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
368 (should-error (org-ctrl-c-ctrl-c)))))
370 (ert-deftest test-org-babel
/inline-src_blk-results-silent
()
371 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
372 (org-test-with-temp-text test-line
374 (should (string= test-line
375 (buffer-substring-no-properties
376 (point-at-bol) (point-at-eol))))
378 (should-error (org-ctrl-c-ctrl-c))))
379 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
380 "[ :results silent ]{ \"y\" }")))
381 (org-test-with-temp-text
383 (goto-char (point-max))
384 (insert (concat "\n" test-line
" end"))
385 (re-search-backward "src_") (org-ctrl-c-ctrl-c)
386 (should (string= (concat test-line
" end")
387 (buffer-substring-no-properties
388 (point-at-bol) (point-at-eol))))
389 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
390 (should (string= (concat test-line
" end")
391 (buffer-substring-no-properties
392 (point-at-bol) (point-at-eol))))
394 (should-error (org-ctrl-c-ctrl-c)))))
396 (ert-deftest test-org-babel
/inline-src_blk-results-raw
()
397 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
398 (org-test-with-temp-text test-line
400 (should (string= (concat test-line
" x")
402 (let ((test-line (concat " Some text prior to block "
403 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
404 (org-test-with-temp-text (concat test-line
" end")
405 (re-search-forward "src_") (org-ctrl-c-ctrl-c)
406 (should (string= (concat test-line
" the end")
407 (buffer-substring-no-properties
408 (point-at-bol) (point-at-eol))))
409 (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
410 (should (string= (concat test-line
" the the end")
411 (buffer-substring-no-properties
412 (point-at-bol) (point-at-eol))))
414 (should-error (org-ctrl-c-ctrl-c)))))
416 (ert-deftest test-org-babel
/inline-src_blk-results-file
()
417 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
418 (org-test-with-temp-text
421 (should (string= (concat test-line
" [[file:~/test-file]]")
422 (buffer-substring-no-properties
423 (point-min) (point-max)))))))
425 (ert-deftest test-org-babel
/inline-src_blk-results-scalar
()
426 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }"))
427 (org-test-with-temp-text
430 (should (string= (concat test-line
" =\"x\"=")
431 (buffer-substring-no-properties
432 (point-min) (point-max)))))))
434 (ert-deftest test-org-babel
/inline-src_blk-results-verbatim
()
435 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
436 (org-test-with-temp-text
439 (should (string= (concat test-line
" =\"x\"=")
440 (buffer-substring-no-properties
441 (point-min) (point-max)))))))
443 (ert-deftest test-org-babel
/combining-scalar-and-raw-result-types
()
444 (org-test-with-temp-text-in-file "
446 #+begin_src sh :results scalar
447 echo \"[[file:./cv.cls]]\"
453 #+begin_src sh :results raw scalar
454 echo \"[[file:./cv.cls]]\"
457 (flet ((next-result ()
458 (org-babel-next-src-block)
459 (org-babel-execute-src-block)
460 (goto-char (org-babel-where-is-src-block-result))
462 (goto-char (point-min))
464 (should (org-babel-in-example-or-verbatim))
466 (should (not (org-babel-in-example-or-verbatim))))))
468 (ert-deftest test-org-babel
/no-defaut-value-for-var
()
469 "Test that the absence of a default value for a variable DOES THROW
471 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
472 (org-babel-next-src-block)
474 (should-error (org-babel-execute-src-block) :type
'error
)))
478 "variable \"x\" in block \"carre\" must be assigned a default value")
481 (ert-deftest test-org-babel
/just-one-results-block
()
482 "Test that evaluating two times the same code block does not result in a
483 duplicate results block."
484 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
485 (org-babel-execute-src-block)
486 (org-babel-execute-src-block) ; second code block execution
487 (should (search-forward "Hello")) ; the string inside the source code block
488 (should (search-forward "Hello")) ; the same string in the results block
489 (should-error (search-forward "Hello"))))
491 (ert-deftest test-org-babel
/nested-code-block
()
492 "Test nested code blocks inside code blocks don't cause problems."
493 (org-test-with-temp-text "#+begin_src org :results silent
494 ,#+begin_src emacs-lisp
498 (should (string= (org-babel-execute-src-block)
499 "#+begin_src emacs-lisp\n 'foo\n#+end_src"))))
501 (ert-deftest test-org-babel
/partial-nested-code-block
()
502 "Test nested code blocks inside code blocks don't cause problems."
503 (org-test-with-temp-text "#+begin_src org :results silent
504 ,#+begin_src emacs-lisp
506 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
508 (ert-deftest test-ob
/does-not-replace-a-block-with-the-results
()
509 (org-test-with-temp-text "#+NAME: foo
510 #+BEGIN_SRC emacs-lisp
513 (org-babel-next-src-block 1)
514 (should (eq 'foo
(org-babel-execute-src-block)))
515 (goto-char (point-min))
516 (org-babel-next-src-block 1)
517 (should (looking-at org-babel-src-block-regexp
))))
519 (ert-deftest test-ob
/catches-all-references
()
520 (org-test-with-temp-text "
521 #+NAME: literal-example
527 #+NAME: read-literal-example
528 #+BEGIN_SRC emacs-lisp :var x=literal-example
529 (concatenate 'string x \" for me.\")
531 (org-babel-next-src-block 1)
532 (should (string= (org-babel-execute-src-block)
533 "A literal example\non two lines for me."))))
535 (ert-deftest test-ob
/resolve-code-blocks-before-data-blocks
()
536 (org-test-with-temp-text "
541 #+begin_src emacs-lisp
545 #+begin_src emacs-lisp :var foo=foo
548 (org-babel-next-src-block 2)
549 (should (string= (org-babel-execute-src-block) "baz"))))
551 (ert-deftest test-ob
/do-not-resolve-to-partial-names-data
()
552 (org-test-with-temp-text "
561 #+begin_src emacs-lisp :var x=base
564 (org-babel-next-src-block 1)
565 (should (equal (org-babel-execute-src-block) '((3) (4))))))
567 (ert-deftest test-ob
/do-not-resolve-to-partial-names-code
()
568 (org-test-with-temp-text "
570 #+begin_src emacs-lisp
575 #+begin_src emacs-lisp
579 #+begin_src emacs-lisp :var x=base
582 (org-babel-next-src-block 3)
583 (should (equal (org-babel-execute-src-block) "foo"))))
585 (ert-deftest test-ob
/allow-spaces-around-
=-in-var-specs
()
586 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
590 (should (= 10 (org-babel-execute-src-block)))))
592 (ert-deftest test-ob
/org-babel-update-intermediate
()
593 (org-test-with-temp-text "#+name: foo
594 #+begin_src emacs-lisp
601 #+begin_src emacs-lisp :var it=foo
604 (let ((org-babel-update-intermediate nil
))
605 (goto-char (point-min))
606 (org-babel-next-src-block 2)
607 (should (= 3 (org-babel-execute-src-block)))
608 (goto-char (point-min))
610 (should (looking-at ": 4")))
611 (let ((org-babel-update-intermediate t
))
612 (goto-char (point-min))
613 (org-babel-next-src-block 2)
614 (should (= 3 (org-babel-execute-src-block)))
615 (goto-char (point-min))
617 (should (looking-at ": 2")))))
619 (ert-deftest test-ob
/eval-header-argument
()
620 (flet ((check-eval (eval runp
)
621 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
624 (let ((foo :not-run
))
626 (progn (should (org-babel-execute-src-block))
627 (should (eq foo
:evald
)))
628 (progn (should-not (org-babel-execute-src-block))
629 (should-not (eq foo
:evald
))))))))
630 (check-eval "never" nil
)
631 (check-eval "no" nil
)
632 (check-eval "never-export" t
)
633 (check-eval "no-export" t
)
634 (let ((org-current-export-file "something"))
635 (check-eval "never" nil
)
636 (check-eval "no" nil
)
637 (check-eval "never-export" nil
)
638 (check-eval "no-export" nil
))))
640 (ert-deftest test-ob
/noweb-expansion-1
()
641 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
649 (should (string= (org-babel-expand-noweb-references) "bar"))))
651 (ert-deftest test-ob
/noweb-expansion-2
()
652 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
657 #+begin_src sh :noweb-sep \"\"
661 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
664 (should (string= (org-babel-expand-noweb-references) "barbaz"))))
666 (ert-deftest test-ob
/splitting-variable-lists-in-references
()
667 (org-test-with-temp-text ""
668 (should (= 1 (length (org-babel-ref-split-args
669 "a=\"this, no work\""))))
670 (should (= 2 (length (org-babel-ref-split-args
671 "a=\"this, no work\", b=1"))))))
673 (ert-deftest test-ob
/org-babel-balanced-split
()
675 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
676 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
679 (ert-deftest test-ob
/commented-last-block-line-no-var
()
680 (org-test-with-temp-text-in-file "
681 #+begin_src emacs-lisp
685 (org-babel-next-src-block)
687 (should (re-search-forward "\\#\\+results:" nil t
))
692 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
693 (org-test-with-temp-text-in-file "
694 #+begin_src emacs-lisp
699 (org-babel-next-src-block)
701 (should (re-search-forward "\\#\\+results:" nil t
))
706 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
708 (ert-deftest test-ob
/commented-last-block-line-with-var
()
709 (org-test-with-temp-text-in-file "
710 #+begin_src emacs-lisp :var a=1
714 (org-babel-next-src-block)
716 (re-search-forward "\\#\\+results:" nil t
)
720 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
721 (org-test-with-temp-text-in-file "
722 #+begin_src emacs-lisp :var a=2
726 (org-babel-next-src-block)
728 (re-search-forward "\\#\\+results:" nil t
)
732 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
734 (defun test-ob-verify-result-and-removed-result (result buffer-text
)
735 "Test helper function to test `org-babel-remove-result'.
736 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
737 and the result of execution is verified against RESULT.
739 The block is actually executed /twice/ to ensure result
740 replacement happens correctly."
741 (org-test-with-temp-text
744 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
745 (should (re-search-forward "\\#\\+results:" nil t
))
747 (should (string= result
748 (buffer-substring-no-properties
750 (- (point-max) 16))))
751 (org-babel-previous-src-block) (org-babel-remove-result)
752 (should (string= buffer-text
753 (buffer-substring-no-properties
754 (point-min) (point-max)))))))
756 (ert-deftest test-ob
/org-babel-remove-result--results-default
()
757 "Test `org-babel-remove-result' with default :results."
758 (mapcar (lambda (language)
759 (test-ob-verify-result-and-removed-result
762 "* org-babel-remove-result
763 #+begin_src " language
"
767 '("sh" "emacs-lisp")))
769 (ert-deftest test-ob
/org-babel-remove-result--results-list
()
770 "Test `org-babel-remove-result' with :results list."
771 (test-ob-verify-result-and-removed-result
777 "* org-babel-remove-result
778 #+begin_src emacs-lisp :results list
784 (ert-deftest test-ob
/org-babel-results-indented-wrap
()
785 "Ensure that wrapped results are inserted correction when indented.
786 If not inserted correctly then the second evaluation will fail
787 trying to find the :END: marker."
788 (org-test-with-temp-text
790 #+begin_src sh :results file wrap
793 (org-babel-next-src-block 1)
794 (org-babel-execute-src-block)
795 (org-babel-execute-src-block)))
797 (ert-deftest test-ob
/file-desc-header-argument
()
798 "Test that the :file-desc header argument is used."
799 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
803 #+begin_src emacs-lisp :results file :file-desc
806 (org-babel-execute-src-block)
807 (org-babel-next-src-block 1)
808 (org-babel-execute-src-block)
809 (goto-char (point-min))
810 (should (search-forward "[[file:foo][bar]]" nil t
))
811 (should (search-forward "[[file:foo][foo]]" nil t
))))
813 (ert-deftest test-ob
/org-babel-remove-result--results-wrap
()
814 "Test `org-babel-remove-result' with :results wrap."
815 (test-ob-verify-result-and-removed-result
820 "* org-babel-remove-result
822 #+begin_src emacs-lisp :results wrap
828 (ert-deftest test-ob
/org-babel-remove-result--results-org
()
829 "Test `org-babel-remove-result' with :results org."
830 (test-ob-verify-result-and-removed-result
837 "* org-babel-remove-result
838 #+begin_src emacs-lisp :results org
846 (ert-deftest test-ob
/org-babel-remove-result--results-html
()
847 "Test `org-babel-remove-result' with :results html."
848 (test-ob-verify-result-and-removed-result
850 <head><body></body></head>
853 "* org-babel-remove-result
854 #+begin_src emacs-lisp :results html
855 \"<head><body></body></head>\"
860 (ert-deftest test-ob
/org-babel-remove-result--results-latex
()
861 "Test `org-babel-remove-result' with :results latex."
862 (test-ob-verify-result-and-removed-result
869 "* org-babel-remove-result
870 #+begin_src emacs-lisp :results latex
878 (ert-deftest test-ob
/org-babel-remove-result--results-code
()
879 "Test `org-babel-remove-result' with :results code."
881 (test-ob-verify-result-and-removed-result
882 "#+BEGIN_SRC emacs-lisp
886 "* org-babel-remove-result
887 #+begin_src emacs-lisp :results code
888 (message \"I am working!\")
893 (ert-deftest test-ob
/org-babel-remove-result--results-pp
()
894 "Test `org-babel-remove-result' with :results pp."
895 (test-ob-verify-result-and-removed-result
896 ": \"I /am/ working!\""
898 "* org-babel-remove-result
899 #+begin_src emacs-lisp :results pp
905 (ert-deftest test-org-babel
/inline-src_blk-preceded-punct-preceded-by-point
()
906 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
907 (org-test-with-temp-text
911 (should (re-search-forward "=\"x\"=" nil t
))
914 (ert-deftest test-ob
/commented-last-block-line-with-var
()
915 (org-test-with-temp-text-in-file "
916 #+begin_src emacs-lisp :var a=1
920 (org-babel-next-src-block)
922 (re-search-forward "\\#\\+results:" nil t
)
926 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
927 (org-test-with-temp-text-in-file "
928 #+begin_src emacs-lisp :var a=2
932 (org-babel-next-src-block)
934 (re-search-forward "\\#\\+results:" nil t
)
938 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
940 (defun test-ob-verify-result-and-removed-result (result buffer-text
)
941 "Test helper function to test `org-babel-remove-result'.
942 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
943 and the result of execution is verified against RESULT.
945 The block is actually executed /twice/ to ensure result
946 replacement happens correctly."
947 (org-test-with-temp-text
950 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
951 (should (re-search-forward "\\#\\+results:" nil t
))
953 (should (string= result
954 (buffer-substring-no-properties
956 (- (point-max) 16))))
957 (org-babel-previous-src-block) (org-babel-remove-result)
958 (should (string= buffer-text
959 (buffer-substring-no-properties
960 (point-min) (point-max)))))))
962 (ert-deftest test-ob
/org-babel-remove-result--results-default
()
963 "Test `org-babel-remove-result' with default :results."
964 (mapcar (lambda (language)
965 (test-ob-verify-result-and-removed-result
968 "* org-babel-remove-result
969 #+begin_src " language
"
973 '("sh" "emacs-lisp")))
975 (ert-deftest test-ob
/org-babel-remove-result--results-list
()
976 "Test `org-babel-remove-result' with :results list."
977 (test-ob-verify-result-and-removed-result
983 "* org-babel-remove-result
984 #+begin_src emacs-lisp :results list
990 (ert-deftest test-ob
/org-babel-remove-result--results-wrap
()
991 "Test `org-babel-remove-result' with :results wrap."
992 (test-ob-verify-result-and-removed-result
997 "* org-babel-remove-result
999 #+begin_src emacs-lisp :results wrap
1005 (ert-deftest test-ob
/org-babel-remove-result--results-org
()
1006 "Test `org-babel-remove-result' with :results org."
1007 (test-ob-verify-result-and-removed-result
1014 "* org-babel-remove-result
1015 #+begin_src emacs-lisp :results org
1023 (ert-deftest test-ob
/org-babel-remove-result--results-html
()
1024 "Test `org-babel-remove-result' with :results html."
1025 (test-ob-verify-result-and-removed-result
1027 <head><body></body></head>
1030 "* org-babel-remove-result
1031 #+begin_src emacs-lisp :results html
1032 \"<head><body></body></head>\"
1037 (ert-deftest test-ob
/org-babel-remove-result--results-latex
()
1038 "Test `org-babel-remove-result' with :results latex."
1039 (test-ob-verify-result-and-removed-result
1046 "* org-babel-remove-result
1047 #+begin_src emacs-lisp :results latex
1055 (ert-deftest test-ob
/org-babel-remove-result--results-code
()
1056 "Test `org-babel-remove-result' with :results code."
1058 (test-ob-verify-result-and-removed-result
1059 "#+BEGIN_SRC emacs-lisp
1063 "* org-babel-remove-result
1064 #+begin_src emacs-lisp :results code
1065 (message \"I am working!\")
1070 (ert-deftest test-ob
/org-babel-remove-result--results-pp
()
1071 "Test `org-babel-remove-result' with :results pp."
1072 (test-ob-verify-result-and-removed-result
1073 ": \"I /am/ working!\""
1075 "* org-babel-remove-result
1076 #+begin_src emacs-lisp :results pp
1077 \"I /am/ working!\")
1082 (ert-deftest test-ob
/results-do-not-replace-code-blocks
()
1083 (org-test-with-temp-text "Block two has a space after the name.
1086 #+begin_src emacs-lisp
1088 #+end_src emacs-lisp
1091 #+begin_src emacs-lisp
1096 #+begin_src emacs-lisp
1103 (dolist (num '(1 2 3))
1104 ;; execute the block
1105 (goto-char (point-min))
1106 (org-babel-next-src-block num
) (org-babel-execute-src-block)
1107 ;; check the results
1108 (goto-char (point-max))
1109 (move-beginning-of-line 0)
1110 (should (looking-at (format ": %d" num
))))))
1114 ;;; test-ob ends here