org-export: Allow to toggle inlinetasks inclusion in export output
[org-mode.git] / testing / lisp / test-ob.el
blob548efd7f968e3ff47fdf6844531f0ee3efe04630
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?
12 (should (string-match
13 org-babel-multi-line-header-regexp
14 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
15 (should
16 (equal
17 "blah1 blah2 blah3 \t"
18 (match-string
20 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
22 ;;TODO Check - should this fail?
23 (should
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 ()
29 (let ((test-block
30 (concat
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"
34 "#+end_src"))
35 (language "language")
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)))
46 ;;no switches
47 (should (org-test-string-exact-match
48 org-babel-src-block-regexp
49 (replace-regexp-in-string flags "" test-block)))
50 ;;no header arguments
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"))
63 (org-babel-get-header
64 org-babel-default-header-args :session)))
65 (should(equal '((:session . "none"))
66 (org-babel-get-header
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"))
71 (org-babel-get-header
72 org-babel-default-header-args :tangle)))
73 ;; with OTHERS option
74 (should (equal org-babel-default-header-args
75 (org-babel-get-header
76 org-babel-default-header-args :doesnt-exist 'others)))
77 (should (equal org-babel-default-header-args
78 (org-babel-get-header
79 org-babel-default-header-args nil 'others)))
80 (should (null
81 (assoc :noweb
82 (org-babel-get-header
83 org-babel-default-header-args :noweb 'others)))))
85 (ert-deftest test-org-babel/default-inline-header-args ()
86 (should(equal
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
92 '((foo . :any)
93 (bar)
94 (baz . ((foo bar) (baz)))
95 (qux . ((foo bar baz qux)))
96 (quux . ((foo bar))))
97 '((bar)
98 (baz . ((baz)))
99 (quux . :any)))))
100 (dolist (pair '((foo . :any)
101 (bar)
102 (baz . ((baz)))
103 (quux . :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)
118 (nth 1 info))))))
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
130 :PROPERTIES:
131 :var: prop = (* 7 6)
132 :END:
133 #+begin_src emacs-lisp
134 prop
135 #+end_src"
137 (progn
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
150 #+end_src"
152 (progn
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 "
160 #+name: four
161 #+begin_src emacs-lisp
162 (list 1 2 3 4)
163 #+end_src
165 #+begin_src emacs-lisp :var four=four
166 (length four)
167 #+end_src"
169 (progn
170 (org-babel-next-src-block 2)
171 (should (= 4 (org-babel-execute-src-block)))
172 (forward-line 5)
173 (should (string= ": 4" (buffer-substring
174 (point-at-bol)
175 (point-at-eol)))))))
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 (require 'cl)
184 (defalias 'my-map (if (org-version-check \"24.1.50\" \"cl\" :predicate)
185 'cl-map
186 'map))
187 (my-map 'list #'list numbers letters)
188 #+end_src"
190 (progn
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
200 the body
201 #+end_src"
203 (progn
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
221 (length text)
222 #+end_src
224 #+begin_src org :noweb yes
225 <<simple-subtree>>
226 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
227 #+end_src
229 ** simple subtree with custom ID
230 :PROPERTIES:
231 :CUSTOM_ID: simple-subtree
232 :END:
233 this is simple"
235 (progn
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)
244 `(progn
245 (move-end-of-line 1)
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))
268 (forward-char 1) ;;3
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))
272 (end-of-line)
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
279 test-line
280 (goto-char (point-min)) (org-ctrl-c-ctrl-c)
281 (should (string=
282 (concat test-line " =1=")
283 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
284 (forward-char) (org-ctrl-c-ctrl-c)
285 (should (string=
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
291 (org-ctrl-c-ctrl-c)
292 (should (string=
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
298 test-line
299 (should-error (org-ctrl-c-ctrl-c))
300 (forward-char) (org-ctrl-c-ctrl-c)
301 (should (string=
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)
305 (should (string=
306 (concat test-line " =1= =1=")
307 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
308 (forward-char)
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))
319 (forward-line)
320 (should-error (org-ctrl-c-ctrl-c))
321 (forward-char) (org-ctrl-c-ctrl-c)
322 (should (string=
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
329 test-line
330 (goto-char (point-max))
331 (insert (concat "\n" test-line " end"))
332 (re-search-backward "src") (org-ctrl-c-ctrl-c)
333 (should (string=
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)
337 (should (string=
338 (concat test-line " =y= =y= end")
339 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
340 (forward-char)
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))
350 (beginning-of-line)
351 (should-error (org-ctrl-c-ctrl-c))
352 (forward-char) (org-ctrl-c-ctrl-c)
353 (should (string=
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)
364 (should (string=
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)
368 (should (string=
369 (concat test-line " =y= =y= end")
370 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
371 (forward-char)
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
377 (org-ctrl-c-ctrl-c)
378 (should (string= test-line
379 (buffer-substring-no-properties
380 (point-at-bol) (point-at-eol))))
381 (end-of-buffer)
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
386 test-line
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))))
397 (forward-char)
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
403 (org-ctrl-c-ctrl-c)
404 (should (string= (concat test-line " x")
405 (buffer-string)))))
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))))
417 (forward-char)
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
423 test-line
424 (org-ctrl-c-ctrl-c)
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
432 test-line
433 (org-ctrl-c-ctrl-c)
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
441 test-line
442 (org-ctrl-c-ctrl-c)
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]]\"
452 #+end_src
454 #+name:
455 : [[file:./cv.cls]]
457 #+begin_src sh :results raw scalar
458 echo \"[[file:./cv.cls]]\"
459 #+end_src
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))
465 (forward-line 1)))
466 (goto-char (point-min))
467 (next-result)
468 (should (org-babel-in-example-or-verbatim))
469 (next-result)
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
474 a proper error."
475 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
476 (org-babel-next-src-block)
477 (let ((err
478 (should-error (org-babel-execute-src-block) :type 'error)))
479 (should
480 (equal
481 '(error
482 "variable \"x\" in block \"carre\" must be assigned a default value")
483 err)))))
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
499 , 'foo
500 ,#+end_src
501 #+end_src"
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
509 #+end_src"
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
515 'foo
516 #+END_SRC\n"
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
526 #+BEGIN_EXAMPLE
527 A literal example
528 on two lines
529 #+END_EXAMPLE
531 #+NAME: read-literal-example
532 #+BEGIN_SRC emacs-lisp :var x=literal-example
533 (concatenate 'string x \" for me.\")
534 #+END_SRC"
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 "
541 #+name: foo
542 : bar
544 #+name: foo
545 #+begin_src emacs-lisp
546 \"baz\"
547 #+end_src
549 #+begin_src emacs-lisp :var foo=foo
551 #+end_src"
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 "
557 #+tblname: base_plus
558 | 1 |
559 | 2 |
561 #+tblname: base
562 | 3 |
563 | 4 |
565 #+begin_src emacs-lisp :var x=base
567 #+end_src"
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 "
573 #+name: base_plus
574 #+begin_src emacs-lisp
575 'bar
576 #+end_src
578 #+name: base
579 #+begin_src emacs-lisp
580 'foo
581 #+end_src
583 #+begin_src emacs-lisp :var x=base
585 #+end_src"
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
591 (+ a b c d)
592 #+end_src
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
600 #+end_src
602 #+results: foo
605 #+begin_src emacs-lisp :var it=foo
606 (+ it 1)
607 #+end_src"
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))
613 (forward-line 6)
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))
620 (forward-line 6)
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
626 (setq foo :evald)
627 #+end_src" eval)
628 (let ((foo :not-run))
629 (if runp
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
646 <<foo>>
647 #+end_src
649 #+name: foo
650 #+begin_src sh
652 #+end_src"
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
657 <<foo>>
658 #+end_src
660 #+name: foo
661 #+begin_src sh :noweb-sep \"\"
663 #+end_src
665 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
667 #+end_src"
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 ()
678 (should (equal
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))"
681 '((32 9) . 58)))))
683 (ert-deftest test-ob/commented-last-block-line-no-var ()
684 (org-test-with-temp-text-in-file "
685 #+begin_src emacs-lisp
687 #+end_src"
688 (progn
689 (org-babel-next-src-block)
690 (org-ctrl-c-ctrl-c)
691 (should (re-search-forward "\\#\\+results:" nil t))
692 (forward-line)
693 (should
694 (string=
696 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
697 (org-test-with-temp-text-in-file "
698 #+begin_src emacs-lisp
699 \"some text\";;
700 #+end_src"
702 (progn
703 (org-babel-next-src-block)
704 (org-ctrl-c-ctrl-c)
705 (should (re-search-forward "\\#\\+results:" nil t))
706 (forward-line)
707 (should
708 (string=
709 ": some text"
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
716 #+end_src"
717 (progn
718 (org-babel-next-src-block)
719 (org-ctrl-c-ctrl-c)
720 (re-search-forward "\\#\\+results:" nil t)
721 (forward-line)
722 (should (string=
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
728 #+end_src"
729 (progn
730 (org-babel-next-src-block)
731 (org-ctrl-c-ctrl-c)
732 (re-search-forward "\\#\\+results:" nil t)
733 (forward-line)
734 (should (string=
735 ": 2"
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
746 buffer-text
747 (progn
748 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
749 (should (re-search-forward "\\#\\+results:" nil t))
750 (forward-line)
751 (should (string= result
752 (buffer-substring-no-properties
753 (point-at-bol)
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
764 "\n"
765 (concat
766 "* org-babel-remove-result
767 #+begin_src " language "
768 #+end_src
770 * next heading")))
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
776 "- 1
779 - (quote (4 5))"
781 "* org-babel-remove-result
782 #+begin_src emacs-lisp :results list
783 '(1 2 3 '(4 5))
784 #+end_src
786 * next heading"))
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
793 "- indented
794 #+begin_src sh :results file wrap
795 echo test.txt
796 #+end_src"
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
804 \"foo\"
805 #+end_src
807 #+begin_src emacs-lisp :results file :file-desc
808 \"foo\"
809 #+end_src"
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-wrap ()
818 "Test `org-babel-remove-result' with :results wrap."
819 (test-ob-verify-result-and-removed-result
820 ":RESULTS:
821 hello there
822 :END:"
824 "* org-babel-remove-result
826 #+begin_src emacs-lisp :results wrap
827 \"hello there\"
828 #+end_src
830 * next heading"))
832 (ert-deftest test-ob/org-babel-remove-result--results-org ()
833 "Test `org-babel-remove-result' with :results org."
834 (test-ob-verify-result-and-removed-result
835 "#+BEGIN_ORG
836 * heading
837 ** subheading
838 content
839 #+END_ORG"
841 "* org-babel-remove-result
842 #+begin_src emacs-lisp :results org
843 \"* heading
844 ** subheading
845 content\"
846 #+end_src
848 * next heading"))
850 (ert-deftest test-ob/org-babel-remove-result--results-html ()
851 "Test `org-babel-remove-result' with :results html."
852 (test-ob-verify-result-and-removed-result
853 "#+BEGIN_HTML
854 <head><body></body></head>
855 #+END_HTML"
857 "* org-babel-remove-result
858 #+begin_src emacs-lisp :results html
859 \"<head><body></body></head>\"
860 #+end_src
862 * next heading"))
864 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
865 "Test `org-babel-remove-result' with :results latex."
866 (test-ob-verify-result-and-removed-result
867 "#+BEGIN_LaTeX
868 Line 1
869 Line 2
870 Line 3
871 #+END_LaTeX"
873 "* org-babel-remove-result
874 #+begin_src emacs-lisp :results latex
875 \"Line 1
876 Line 2
877 Line 3\"
878 #+end_src
880 * next heading"))
882 (ert-deftest test-ob/org-babel-remove-result--results-code ()
883 "Test `org-babel-remove-result' with :results code."
885 (test-ob-verify-result-and-removed-result
886 "#+BEGIN_SRC emacs-lisp
887 \"I am working!\"
888 #+END_SRC"
890 "* org-babel-remove-result
891 #+begin_src emacs-lisp :results code
892 (message \"I am working!\")
893 #+end_src
895 * next heading"))
897 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
898 "Test `org-babel-remove-result' with :results pp."
899 (test-ob-verify-result-and-removed-result
900 ": \"I /am/ working!\""
902 "* org-babel-remove-result
903 #+begin_src emacs-lisp :results pp
904 \"I /am/ working!\")
905 #+end_src
907 * next heading"))
909 (ert-deftest test-org-babel/inline-src_blk-preceded-punct-preceded-by-point ()
910 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
911 (org-test-with-temp-text
912 test-line
913 (forward-char 1)
914 (org-ctrl-c-ctrl-c)
915 (should (re-search-forward "=\"x\"=" nil t))
916 (forward-line))))
918 (ert-deftest test-ob/commented-last-block-line-with-var ()
919 (org-test-with-temp-text-in-file "
920 #+begin_src emacs-lisp :var a=1
922 #+end_src"
923 (progn
924 (org-babel-next-src-block)
925 (org-ctrl-c-ctrl-c)
926 (re-search-forward "\\#\\+results:" nil t)
927 (forward-line)
928 (should (string=
930 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
931 (org-test-with-temp-text-in-file "
932 #+begin_src emacs-lisp :var a=2
934 #+end_src"
935 (progn
936 (org-babel-next-src-block)
937 (org-ctrl-c-ctrl-c)
938 (re-search-forward "\\#\\+results:" nil t)
939 (forward-line)
940 (should (string=
941 ": 2"
942 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
944 (defun test-ob-verify-result-and-removed-result (result buffer-text)
945 "Test helper function to test `org-babel-remove-result'.
946 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
947 and the result of execution is verified against RESULT.
949 The block is actually executed /twice/ to ensure result
950 replacement happens correctly."
951 (org-test-with-temp-text
952 buffer-text
953 (progn
954 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
955 (should (re-search-forward "\\#\\+results:" nil t))
956 (forward-line)
957 (should (string= result
958 (buffer-substring-no-properties
959 (point-at-bol)
960 (- (point-max) 16))))
961 (org-babel-previous-src-block) (org-babel-remove-result)
962 (should (string= buffer-text
963 (buffer-substring-no-properties
964 (point-min) (point-max)))))))
966 (ert-deftest test-ob/org-babel-remove-result--results-default ()
967 "Test `org-babel-remove-result' with default :results."
968 (mapcar (lambda (language)
969 (test-ob-verify-result-and-removed-result
970 "\n"
971 (concat
972 "* org-babel-remove-result
973 #+begin_src " language "
974 #+end_src
976 * next heading")))
977 '("sh" "emacs-lisp")))
979 (ert-deftest test-ob/org-babel-remove-result--results-list ()
980 "Test `org-babel-remove-result' with :results list."
981 (test-ob-verify-result-and-removed-result
982 "- 1
985 - (quote (4 5))"
987 "* org-babel-remove-result
988 #+begin_src emacs-lisp :results list
989 '(1 2 3 '(4 5))
990 #+end_src
992 * next heading"))
994 (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
995 "Test `org-babel-remove-result' with :results wrap."
996 (test-ob-verify-result-and-removed-result
997 ":RESULTS:
998 hello there
999 :END:"
1001 "* org-babel-remove-result
1003 #+begin_src emacs-lisp :results wrap
1004 \"hello there\"
1005 #+end_src
1007 * next heading"))
1009 (ert-deftest test-ob/org-babel-remove-result--results-org ()
1010 "Test `org-babel-remove-result' with :results org."
1011 (test-ob-verify-result-and-removed-result
1012 "#+BEGIN_ORG
1013 * heading
1014 ** subheading
1015 content
1016 #+END_ORG"
1018 "* org-babel-remove-result
1019 #+begin_src emacs-lisp :results org
1020 \"* heading
1021 ** subheading
1022 content\"
1023 #+end_src
1025 * next heading"))
1027 (ert-deftest test-ob/org-babel-remove-result--results-html ()
1028 "Test `org-babel-remove-result' with :results html."
1029 (test-ob-verify-result-and-removed-result
1030 "#+BEGIN_HTML
1031 <head><body></body></head>
1032 #+END_HTML"
1034 "* org-babel-remove-result
1035 #+begin_src emacs-lisp :results html
1036 \"<head><body></body></head>\"
1037 #+end_src
1039 * next heading"))
1041 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
1042 "Test `org-babel-remove-result' with :results latex."
1043 (test-ob-verify-result-and-removed-result
1044 "#+BEGIN_LaTeX
1045 Line 1
1046 Line 2
1047 Line 3
1048 #+END_LaTeX"
1050 "* org-babel-remove-result
1051 #+begin_src emacs-lisp :results latex
1052 \"Line 1
1053 Line 2
1054 Line 3\"
1055 #+end_src
1057 * next heading"))
1059 (ert-deftest test-ob/org-babel-remove-result--results-code ()
1060 "Test `org-babel-remove-result' with :results code."
1062 (test-ob-verify-result-and-removed-result
1063 "#+BEGIN_SRC emacs-lisp
1064 \"I am working!\"
1065 #+END_SRC"
1067 "* org-babel-remove-result
1068 #+begin_src emacs-lisp :results code
1069 (message \"I am working!\")
1070 #+end_src
1072 * next heading"))
1074 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
1075 "Test `org-babel-remove-result' with :results pp."
1076 (test-ob-verify-result-and-removed-result
1077 ": \"I /am/ working!\""
1079 "* org-babel-remove-result
1080 #+begin_src emacs-lisp :results pp
1081 \"I /am/ working!\")
1082 #+end_src
1084 * next heading"))
1086 (ert-deftest test-ob/results-do-not-replace-code-blocks ()
1087 (org-test-with-temp-text "Block two has a space after the name.
1089 #+name: foo
1090 #+begin_src emacs-lisp
1092 #+end_src emacs-lisp
1094 #+name: foo
1095 #+begin_src emacs-lisp
1097 #+end_src
1099 #+name: foo
1100 #+begin_src emacs-lisp
1102 #+end_src
1104 #+RESULTS: foo
1105 : foo
1107 (dolist (num '(1 2 3))
1108 ;; execute the block
1109 (goto-char (point-min))
1110 (org-babel-next-src-block num) (org-babel-execute-src-block)
1111 ;; check the results
1112 (goto-char (point-max))
1113 (move-beginning-of-line 0)
1114 (should (looking-at (format ": %d" num))))))
1116 (provide 'test-ob)
1118 ;;; test-ob ends here