org-drill: prevent errors caused by attempting to display inline images on a non...
[org-mode/org-mode-NeilSmithlineMods.git] / testing / lisp / test-ob.el
blob5cf689aefa5354579cff56fa0bed15d1a4b69691
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 (map 'list #'list numbers letters)
184 #+end_src"
186 (progn
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
196 the body
197 #+end_src"
199 (progn
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
217 (length text)
218 #+end_src
220 #+begin_src org :noweb yes
221 <<simple-subtree>>
222 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
223 #+end_src
225 ** simple subtree with custom ID
226 :PROPERTIES:
227 :CUSTOM_ID: simple-subtree
228 :END:
229 this is simple"
231 (progn
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)
240 `(progn
241 (move-end-of-line 1)
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))
264 (forward-char 1) ;;3
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))
268 (end-of-line)
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
275 test-line
276 (goto-char (point-min)) (org-ctrl-c-ctrl-c)
277 (should (string=
278 (concat test-line " =1=")
279 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
280 (forward-char) (org-ctrl-c-ctrl-c)
281 (should (string=
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
287 (org-ctrl-c-ctrl-c)
288 (should (string=
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
294 test-line
295 (should-error (org-ctrl-c-ctrl-c))
296 (forward-char) (org-ctrl-c-ctrl-c)
297 (should (string=
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)
301 (should (string=
302 (concat test-line " =1= =1=")
303 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
304 (forward-char)
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))
315 (forward-line)
316 (should-error (org-ctrl-c-ctrl-c))
317 (forward-char) (org-ctrl-c-ctrl-c)
318 (should (string=
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
325 test-line
326 (goto-char (point-max))
327 (insert (concat "\n" test-line " end"))
328 (re-search-backward "src") (org-ctrl-c-ctrl-c)
329 (should (string=
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)
333 (should (string=
334 (concat test-line " =y= =y= end")
335 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
336 (forward-char)
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))
346 (beginning-of-line)
347 (should-error (org-ctrl-c-ctrl-c))
348 (forward-char) (org-ctrl-c-ctrl-c)
349 (should (string=
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)
360 (should (string=
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)
364 (should (string=
365 (concat test-line " =y= =y= end")
366 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
367 (forward-char)
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
373 (org-ctrl-c-ctrl-c)
374 (should (string= test-line
375 (buffer-substring-no-properties
376 (point-at-bol) (point-at-eol))))
377 (end-of-buffer)
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
382 test-line
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))))
393 (forward-char)
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
399 (org-ctrl-c-ctrl-c)
400 (should (string= (concat test-line " x")
401 (buffer-string)))))
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))))
413 (forward-char)
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
419 test-line
420 (org-ctrl-c-ctrl-c)
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
428 test-line
429 (org-ctrl-c-ctrl-c)
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
437 test-line
438 (org-ctrl-c-ctrl-c)
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]]\"
448 #+end_src
450 #+name:
451 : [[file:./cv.cls]]
453 #+begin_src sh :results raw scalar
454 echo \"[[file:./cv.cls]]\"
455 #+end_src
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))
461 (forward-line 1)))
462 (goto-char (point-min))
463 (next-result)
464 (should (org-babel-in-example-or-verbatim))
465 (next-result)
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
470 a proper error."
471 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
472 (org-babel-next-src-block)
473 (let ((err
474 (should-error (org-babel-execute-src-block) :type 'error)))
475 (should
476 (equal
477 '(error
478 "variable \"x\" in block \"carre\" must be assigned a default value")
479 err)))))
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
495 , 'foo
496 ,#+end_src
497 #+end_src"
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
505 #+end_src"
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
511 'foo
512 #+END_SRC\n"
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
522 #+BEGIN_EXAMPLE
523 A literal example
524 on two lines
525 #+END_EXAMPLE
527 #+NAME: read-literal-example
528 #+BEGIN_SRC emacs-lisp :var x=literal-example
529 (concatenate 'string x \" for me.\")
530 #+END_SRC"
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 "
537 #+name: foo
538 : bar
540 #+name: foo
541 #+begin_src emacs-lisp
542 \"baz\"
543 #+end_src
545 #+begin_src emacs-lisp :var foo=foo
547 #+end_src"
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 "
553 #+tblname: base_plus
554 | 1 |
555 | 2 |
557 #+tblname: base
558 | 3 |
559 | 4 |
561 #+begin_src emacs-lisp :var x=base
563 #+end_src"
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 "
569 #+name: base_plus
570 #+begin_src emacs-lisp
571 'bar
572 #+end_src
574 #+name: base
575 #+begin_src emacs-lisp
576 'foo
577 #+end_src
579 #+begin_src emacs-lisp :var x=base
581 #+end_src"
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
587 (+ a b c d)
588 #+end_src
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
596 #+end_src
598 #+results: foo
601 #+begin_src emacs-lisp :var it=foo
602 (+ it 1)
603 #+end_src"
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))
609 (forward-line 6)
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))
616 (forward-line 6)
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
622 (setq foo :evald)
623 #+end_src" eval)
624 (let ((foo :not-run))
625 (if runp
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
642 <<foo>>
643 #+end_src
645 #+name: foo
646 #+begin_src sh
648 #+end_src"
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
653 <<foo>>
654 #+end_src
656 #+name: foo
657 #+begin_src sh :noweb-sep \"\"
659 #+end_src
661 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
663 #+end_src"
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 ()
674 (should (equal
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))"
677 '((32 9) . 58)))))
679 (ert-deftest test-ob/commented-last-block-line-no-var ()
680 (org-test-with-temp-text-in-file "
681 #+begin_src emacs-lisp
683 #+end_src"
684 (progn
685 (org-babel-next-src-block)
686 (org-ctrl-c-ctrl-c)
687 (should (re-search-forward "\\#\\+results:" nil t))
688 (forward-line)
689 (should
690 (string=
692 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
693 (org-test-with-temp-text-in-file "
694 #+begin_src emacs-lisp
695 \"some text\";;
696 #+end_src"
698 (progn
699 (org-babel-next-src-block)
700 (org-ctrl-c-ctrl-c)
701 (should (re-search-forward "\\#\\+results:" nil t))
702 (forward-line)
703 (should
704 (string=
705 ": some text"
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
712 #+end_src"
713 (progn
714 (org-babel-next-src-block)
715 (org-ctrl-c-ctrl-c)
716 (re-search-forward "\\#\\+results:" nil t)
717 (forward-line)
718 (should (string=
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
724 #+end_src"
725 (progn
726 (org-babel-next-src-block)
727 (org-ctrl-c-ctrl-c)
728 (re-search-forward "\\#\\+results:" nil t)
729 (forward-line)
730 (should (string=
731 ": 2"
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
742 buffer-text
743 (progn
744 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
745 (should (re-search-forward "\\#\\+results:" nil t))
746 (forward-line)
747 (should (string= result
748 (buffer-substring-no-properties
749 (point-at-bol)
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
760 "\n"
761 (concat
762 "* org-babel-remove-result
763 #+begin_src " language "
764 #+end_src
766 * next heading")))
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
772 "- 1
775 - (quote (4 5))"
777 "* org-babel-remove-result
778 #+begin_src emacs-lisp :results list
779 '(1 2 3 '(4 5))
780 #+end_src
782 * next heading"))
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
789 "- indented
790 #+begin_src sh :results file wrap
791 echo test.txt
792 #+end_src"
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
800 \"foo\"
801 #+end_src
803 #+begin_src emacs-lisp :results file :file-desc
804 \"foo\"
805 #+end_src"
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
816 ":RESULTS:
817 hello there
818 :END:"
820 "* org-babel-remove-result
822 #+begin_src emacs-lisp :results wrap
823 \"hello there\"
824 #+end_src
826 * next heading"))
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
831 "#+BEGIN_ORG
832 * heading
833 ** subheading
834 content
835 #+END_ORG"
837 "* org-babel-remove-result
838 #+begin_src emacs-lisp :results org
839 \"* heading
840 ** subheading
841 content\"
842 #+end_src
844 * next heading"))
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
849 "#+BEGIN_HTML
850 <head><body></body></head>
851 #+END_HTML"
853 "* org-babel-remove-result
854 #+begin_src emacs-lisp :results html
855 \"<head><body></body></head>\"
856 #+end_src
858 * next heading"))
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
863 "#+BEGIN_LaTeX
864 Line 1
865 Line 2
866 Line 3
867 #+END_LaTeX"
869 "* org-babel-remove-result
870 #+begin_src emacs-lisp :results latex
871 \"Line 1
872 Line 2
873 Line 3\"
874 #+end_src
876 * next heading"))
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
883 \"I am working!\"
884 #+END_SRC"
886 "* org-babel-remove-result
887 #+begin_src emacs-lisp :results code
888 (message \"I am working!\")
889 #+end_src
891 * next heading"))
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
900 \"I /am/ working!\")
901 #+end_src
903 * next heading"))
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
908 test-line
909 (forward-char 1)
910 (org-ctrl-c-ctrl-c)
911 (should (re-search-forward "=\"x\"=" nil t))
912 (forward-line))))
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
918 #+end_src"
919 (progn
920 (org-babel-next-src-block)
921 (org-ctrl-c-ctrl-c)
922 (re-search-forward "\\#\\+results:" nil t)
923 (forward-line)
924 (should (string=
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
930 #+end_src"
931 (progn
932 (org-babel-next-src-block)
933 (org-ctrl-c-ctrl-c)
934 (re-search-forward "\\#\\+results:" nil t)
935 (forward-line)
936 (should (string=
937 ": 2"
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
948 buffer-text
949 (progn
950 (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
951 (should (re-search-forward "\\#\\+results:" nil t))
952 (forward-line)
953 (should (string= result
954 (buffer-substring-no-properties
955 (point-at-bol)
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
966 "\n"
967 (concat
968 "* org-babel-remove-result
969 #+begin_src " language "
970 #+end_src
972 * next heading")))
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
978 "- 1
981 - (quote (4 5))"
983 "* org-babel-remove-result
984 #+begin_src emacs-lisp :results list
985 '(1 2 3 '(4 5))
986 #+end_src
988 * next heading"))
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
993 ":RESULTS:
994 hello there
995 :END:"
997 "* org-babel-remove-result
999 #+begin_src emacs-lisp :results wrap
1000 \"hello there\"
1001 #+end_src
1003 * next heading"))
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
1008 "#+BEGIN_ORG
1009 * heading
1010 ** subheading
1011 content
1012 #+END_ORG"
1014 "* org-babel-remove-result
1015 #+begin_src emacs-lisp :results org
1016 \"* heading
1017 ** subheading
1018 content\"
1019 #+end_src
1021 * next heading"))
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
1026 "#+BEGIN_HTML
1027 <head><body></body></head>
1028 #+END_HTML"
1030 "* org-babel-remove-result
1031 #+begin_src emacs-lisp :results html
1032 \"<head><body></body></head>\"
1033 #+end_src
1035 * next heading"))
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
1040 "#+BEGIN_LaTeX
1041 Line 1
1042 Line 2
1043 Line 3
1044 #+END_LaTeX"
1046 "* org-babel-remove-result
1047 #+begin_src emacs-lisp :results latex
1048 \"Line 1
1049 Line 2
1050 Line 3\"
1051 #+end_src
1053 * next heading"))
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
1060 \"I am working!\"
1061 #+END_SRC"
1063 "* org-babel-remove-result
1064 #+begin_src emacs-lisp :results code
1065 (message \"I am working!\")
1066 #+end_src
1068 * next heading"))
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!\")
1078 #+end_src
1080 * next heading"))
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.
1085 #+name: foo
1086 #+begin_src emacs-lisp
1088 #+end_src emacs-lisp
1090 #+name: foo
1091 #+begin_src emacs-lisp
1093 #+end_src
1095 #+name: foo
1096 #+begin_src emacs-lisp
1098 #+end_src
1100 #+RESULTS: foo
1101 : foo
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))))))
1112 (provide 'test-ob)
1114 ;;; test-ob ends here