Fix indentation in empty items
[org-mode/org-tableheadings.git] / testing / lisp / test-ob.el
blobf52ff2419dc4e252ddf959e3dae403e7cbf1dc80
1 ;;; test-ob.el --- tests for ob.el
3 ;; Copyright (c) 2010-2015 Eric Schulte
4 ;; Authors: Eric Schulte, Martyn Jago
6 ;; This file is not part of GNU Emacs.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Code:
23 (ert-deftest test-org-babel/multi-line-header-regexp ()
24 (should(equal "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
25 org-babel-multi-line-header-regexp))
26 ;;TODO can be optimised - and what about blah4 blah5 blah6?
27 (should (string-match
28 org-babel-multi-line-header-regexp
29 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
30 (should
31 (equal
32 "blah1 blah2 blah3 \t"
33 (match-string
35 " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
37 ;;TODO Check - should this fail?
38 (should
39 (not (org-test-string-exact-match
40 org-babel-multi-line-header-regexp
41 " \t #+headers : blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))))
43 (ert-deftest test-org-babel/src-block-regexp ()
44 (let ((test-block
45 (concat
46 "#+begin_src language -n-r-a-b -c :argument-1 yes :argument-2 no\n"
47 "echo this is a test\n"
48 "echo Currently in ' $PWD\n"
49 "#+end_src"))
50 (language "language")
51 (flags "-n-r-a-b -c ")
52 (arguments ":argument-1 yes :argument-2 no")
53 (body "echo this is a test\necho Currently in ' $PWD\n"))
54 (should (string-match org-babel-src-block-regexp test-block))
55 (should (string-match org-babel-src-block-regexp (upcase test-block)))
56 (should (equal language (match-string 2 test-block)))
57 ;;TODO Consider refactoring
58 (should (equal flags (match-string 3 test-block)))
59 (should (equal arguments (match-string 4 test-block)))
60 (should (equal body (match-string 5 test-block)))
61 ;;no switches
62 (should (org-test-string-exact-match
63 org-babel-src-block-regexp
64 (replace-regexp-in-string flags "" test-block)))
65 ;;no header arguments
66 (should (org-test-string-exact-match
67 org-babel-src-block-regexp
68 (replace-regexp-in-string arguments "" test-block)))
69 ;; should be valid with no body
70 (should (org-test-string-exact-match
71 org-babel-src-block-regexp
72 (replace-regexp-in-string body "" test-block)))))
74 (ert-deftest test-org-babel/get-header ()
75 (should (not (org-babel-get-header
76 org-babel-default-header-args :doesnt-exist)))
77 (should(equal '((:session . "none"))
78 (org-babel-get-header
79 org-babel-default-header-args :session)))
80 (should(equal '((:session . "none"))
81 (org-babel-get-header
82 org-babel-default-header-args :session nil)))
83 (should (not (org-babel-get-header
84 org-babel-default-header-args :SESSION)))
85 (should (equal '((:tangle . "no"))
86 (org-babel-get-header
87 org-babel-default-header-args :tangle)))
88 ;; with OTHERS option
89 (should (equal org-babel-default-header-args
90 (org-babel-get-header
91 org-babel-default-header-args :doesnt-exist 'others)))
92 (should (equal org-babel-default-header-args
93 (org-babel-get-header
94 org-babel-default-header-args nil 'others)))
95 (should (null
96 (assoc :noweb
97 (org-babel-get-header
98 org-babel-default-header-args :noweb 'others)))))
100 (ert-deftest test-org-babel/default-inline-header-args ()
101 (should(equal
102 '((:session . "none")
103 (:results . "replace")
104 (:exports . "results")
105 (:hlines . "yes"))
106 org-babel-default-inline-header-args)))
108 (ert-deftest ob-test/org-babel-combine-header-arg-lists ()
109 (let ((results (org-babel-combine-header-arg-lists
110 '((foo . :any)
111 (bar)
112 (baz . ((foo bar) (baz)))
113 (qux . ((foo bar baz qux)))
114 (quux . ((foo bar))))
115 '((bar)
116 (baz . ((baz)))
117 (quux . :any)))))
118 (dolist (pair '((foo . :any)
119 (bar)
120 (baz . ((baz)))
121 (quux . :any)
122 (qux . ((foo bar baz qux)))))
123 (should (equal (cdr pair)
124 (cdr (assoc (car pair) results)))))))
126 ;;; ob-get-src-block-info
127 (ert-deftest test-org-babel/get-src-block-info-language ()
128 (org-test-at-marker nil org-test-file-ob-anchor
129 (let ((info (org-babel-get-src-block-info)))
130 (should (string= "emacs-lisp" (nth 0 info))))))
132 (ert-deftest test-org-babel/get-src-block-info-body ()
133 (org-test-at-marker nil org-test-file-ob-anchor
134 (let ((info (org-babel-get-src-block-info)))
135 (should (string-match (regexp-quote org-test-file-ob-anchor)
136 (nth 1 info))))))
138 (ert-deftest test-org-babel/get-src-block-info-tangle ()
139 (org-test-at-marker nil org-test-file-ob-anchor
140 (let ((info (org-babel-get-src-block-info)))
141 (should (string= "no" (cdr (assoc :tangle (nth 2 info))))))))
143 (ert-deftest test-org-babel/elisp-in-header-arguments ()
144 "Test execution of elisp forms in header arguments."
145 (org-test-with-temp-text-in-file "
147 * elisp forms in header arguments
148 :PROPERTIES:
149 :var: prop = (* 7 6)
150 :END:
151 #+begin_src emacs-lisp
152 prop
153 #+end_src"
154 (goto-char (point-min))
155 (org-babel-next-src-block)
156 (let ((info (org-babel-get-src-block-info)))
157 (should (= 42 (org-babel-execute-src-block))))))
159 (ert-deftest test-org-babel/simple-named-code-block ()
160 "Test that simple named code blocks can be evaluated."
161 (org-test-with-temp-text-in-file "
163 #+name: i-have-a-name
164 #+begin_src emacs-lisp
166 #+end_src"
167 (org-babel-next-src-block 1)
168 (should (= 42 (org-babel-execute-src-block)))))
170 (ert-deftest test-org-babel/simple-variable-resolution ()
171 "Test that simple variable resolution is working."
172 (org-test-with-temp-text-in-file "
174 #+name: four
175 #+begin_src emacs-lisp
176 (list 1 2 3 4)
177 #+end_src
179 #+begin_src emacs-lisp :var four=four
180 (length four)
181 #+end_src"
183 (org-babel-next-src-block 2)
184 (should (= 4 (org-babel-execute-src-block)))
185 (forward-line 5)
186 (should (string= ": 4" (buffer-substring
187 (point-at-bol)
188 (point-at-eol))))))
190 (ert-deftest test-org-babel/multi-line-header-arguments ()
191 "Test that multi-line header arguments and can be read."
192 (org-test-with-temp-text-in-file "
194 #+headers: :var letters='(a b c d e f g)
195 #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
196 (require 'cl)
197 (defalias 'my-map (if (org-version-check \"24.2.50\" \"cl\" :predicate)
198 'cl-map
199 'map))
200 (my-map 'list #'list numbers letters)
201 #+end_src"
203 (org-babel-next-src-block)
204 (let ((results (org-babel-execute-src-block)))
205 (should(equal 'a (cadr (assoc 1 results))))
206 (should(equal 'd (cadr (assoc 4 results)))))))
208 (ert-deftest test-org-babel/parse-header-args ()
209 (org-test-with-temp-text-in-file "
211 #+begin_src example-lang :session :results output :var num=9
212 the body
213 #+end_src"
215 (org-babel-next-src-block)
216 (let* ((info (org-babel-get-src-block-info))
217 (params (nth 2 info)))
218 (message "%S" params)
219 (should (equal "example-lang" (nth 0 info)))
220 (should (string= "the body" (org-babel-trim (nth 1 info))))
221 (should-not (member '(:session\ \ \ \ ) params))
222 (should (equal '(:session) (assoc :session params)))
223 (should (equal '(:result-type . output) (assoc :result-type params)))
224 (should (equal '(num . 9) (cdr (assoc :var params)))))))
226 (ert-deftest test-org-babel/parse-header-args2 ()
227 (org-test-with-temp-text-in-file "
229 * resolving sub-trees as references
231 #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
232 (length text)
233 #+end_src
235 #+begin_src org :noweb yes
236 <<simple-subtree>>
237 <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
238 #+end_src
240 ** simple subtree with custom ID
241 :PROPERTIES:
242 :CUSTOM_ID: simple-subtree
243 :END:
244 this is simple"
246 (should (string-match (regexp-quote "this is simple")
247 (org-babel-ref-resolve "simple-subtree")))
248 (org-babel-next-src-block)
249 (should (= 14 (org-babel-execute-src-block)))))
251 (ert-deftest test-org-babel/inline-src-blocks ()
252 (macrolet ((at-next (&rest body)
253 `(progn
254 (move-end-of-line 1)
255 (re-search-forward org-babel-inline-src-block-regexp nil t)
256 (goto-char (match-beginning 1))
257 (save-match-data ,@body))))
258 (org-test-at-id
259 "54cb8dc3-298c-4883-a933-029b3c9d4b18"
260 (at-next (should (equal 1 (org-babel-execute-src-block))))
261 (at-next (should (equal 2 (org-babel-execute-src-block))))
262 (at-next (should (equal 3 (org-babel-execute-src-block)))))
263 (org-test-at-id
264 "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076"
265 (at-next (should (equal 1 (org-babel-execute-src-block))))
266 (at-next (should (equal 2 (org-babel-execute-src-block))))
267 (at-next (should (equal 3 (org-babel-execute-src-block))))
268 (at-next (should (equal 4 (org-babel-execute-src-block)))))))
270 (ert-deftest test-org-babel/org-babel-get-inline-src-block-matches ()
271 (flet ((test-at-id (id)
272 (org-test-at-id
274 (let ((test-point (point)))
275 (should (fboundp 'org-babel-get-inline-src-block-matches))
276 (should (re-search-forward "src_" nil t)) ;; 1
277 (should (org-babel-get-inline-src-block-matches))
278 (should (re-search-forward " b" nil (point-at-bol))) ;; 1
279 (should-not (org-babel-get-inline-src-block-matches))
280 (should (re-search-forward "in" nil t)) ;; 2
281 (should-not (org-babel-get-inline-src-block-matches))
282 (should (re-search-forward "echo" nil t)) ;; 2
283 (should (org-babel-get-inline-src-block-matches))
284 (should (re-search-forward "blocks" nil t)) ;; 3
285 (backward-char 7) ;; 3
286 (should (org-babel-get-inline-src-block-matches))
287 (forward-char 1) ;;3
288 (should-not (org-babel-get-inline-src-block-matches))
289 (should (re-search-forward ":results" nil t)) ;; 4
290 (should (org-babel-get-inline-src-block-matches))
291 (end-of-line)
292 (should-not (org-babel-get-inline-src-block-matches))))))
293 (test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74")
294 (test-at-id "d55dada7-de0e-4340-8061-787cccbedee5")))
296 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-1 ()
297 (let ((test-line "src_sh{echo 1}")
298 (org-babel-inline-result-wrap "=%s="))
299 ;; src_ at bol line 1...
300 (org-test-with-temp-text
301 test-line
302 (goto-char (point-min)) (org-babel-execute-maybe)
303 (should (string=
304 (concat test-line " {{{results(=1=)}}}")
305 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
306 (forward-char) (org-babel-execute-maybe)
307 (should (string=
308 (concat test-line " {{{results(=1=)}}}")
309 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
310 (re-search-forward "{{{")
311 ;;(should-error (org-ctrl-c-ctrl-c))
312 (backward-char 4) ;; last char of block body
313 (org-babel-execute-maybe)
314 (should (string=
315 (concat test-line " {{{results(=1=)}}}")
316 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
317 ;; src_ follows space line 1...
318 (let ((test-line " src_emacs-lisp{ 1 }"))
319 (org-test-with-temp-text
320 test-line
321 (should-error (org-ctrl-c-ctrl-c))
322 (forward-char) (org-babel-execute-maybe)
323 (should (string=
324 (concat test-line " {{{results(=1=)}}}")
325 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
326 (re-search-forward "{ 1 ") (org-babel-execute-maybe)
327 (should (string=
328 (concat test-line " {{{results(=1=)}}}")
329 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
330 (forward-char 6)
331 (should-error (org-ctrl-c-ctrl-c))
332 ))))
334 (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-2 ()
335 ;; src_ at bol line 2...
336 (let ((test-line " src_emacs-lisp{ \"x\" }")
337 (org-babel-inline-result-wrap "=%s="))
338 (org-test-with-temp-text
339 (concat "\n" test-line)
340 (should-error (org-ctrl-c-ctrl-c))
341 (goto-char (point-min))
342 (should-error (org-ctrl-c-ctrl-c))
343 (forward-line)
344 (should-error (org-ctrl-c-ctrl-c))
345 (forward-char) (org-babel-execute-maybe)
346 (should (string=
347 (concat test-line " {{{results(=x=)}}}")
348 (buffer-substring-no-properties
349 (point-at-bol) (point-at-eol))))))
350 (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
351 (org-babel-inline-result-wrap "=%s="))
352 (org-test-with-temp-text
353 test-line
354 (goto-char (point-max))
355 (insert (concat "\n" test-line " end"))
356 (re-search-backward "src") (org-babel-execute-maybe)
357 (should (string=
358 (concat test-line " {{{results(=y=)}}} end")
359 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
360 (re-search-forward "\" ") (org-babel-execute-maybe)
361 (should (string=
362 (concat test-line " {{{results(=y=)}}} end")
363 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
364 (forward-char 3)
365 (should-error (org-ctrl-c-ctrl-c)))))
367 (ert-deftest test-org-babel/inline-src_blk-manual-results-replace ()
368 (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
369 (org-babel-inline-result-wrap "=%s="))
370 (org-test-with-temp-text
371 (concat "\n" test-line)
372 (should-error (org-ctrl-c-ctrl-c))
373 (goto-char (point-max))
374 (org-babel-execute-maybe)
375 (beginning-of-line)
376 (should-error (org-ctrl-c-ctrl-c))
377 (forward-char) (org-babel-execute-maybe)
378 (should (string=
379 (concat test-line " {{{results(=x=)}}}")
380 (buffer-substring-no-properties
381 (point-at-bol) (point-at-eol))))))
382 (let ((test-line (concat " Some text prior to block "
383 "src_emacs-lisp[:results replace]{ \"y\" }"))
384 (org-babel-inline-result-wrap "=%s="))
385 (org-test-with-temp-text test-line
386 (goto-char (point-max))
387 (insert (concat "\n" test-line " end"))
388 (re-search-backward "src") (org-babel-execute-maybe)
389 (should (string=
390 (concat test-line " {{{results(=y=)}}} end")
391 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
392 (re-search-forward "\" ") (org-babel-execute-maybe)
393 (should (string=
394 (concat test-line " {{{results(=y=)}}} end")
395 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
396 (forward-char 3)
397 (should-error (org-ctrl-c-ctrl-c)))))
399 (ert-deftest test-org-babel/inline-src_blk-results-silent ()
400 (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
401 (org-test-with-temp-text test-line
402 (org-babel-execute-maybe)
403 (should (string= test-line
404 (buffer-substring-no-properties
405 (point-at-bol) (point-at-eol))))))
406 (let ((test-line (concat " Some text prior to block src_emacs-lisp"
407 "[ :results silent ]{ \"y\" }")))
408 (org-test-with-temp-text
409 test-line
410 (goto-char (point-max))
411 (insert (concat "\n" test-line " end"))
412 (re-search-backward "src_") (org-babel-execute-maybe)
413 (should (string= (concat test-line " end")
414 (buffer-substring-no-properties
415 (point-at-bol) (point-at-eol))))
416 (re-search-forward "\" ") (org-babel-execute-maybe)
417 (should (string= (concat test-line " end")
418 (buffer-substring-no-properties
419 (point-at-bol) (point-at-eol))))
420 (forward-char 2)
421 (should-error (org-ctrl-c-ctrl-c)))))
423 (ert-deftest test-org-babel/inline-src_blk-results-raw ()
424 (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
425 (org-test-with-temp-text test-line
426 (org-babel-execute-maybe)
427 (should (string= (concat test-line " x")
428 (buffer-string)))))
429 (let ((test-line (concat " Some text prior to block "
430 "src_emacs-lisp[ :results raw ]{ \"the\" }")))
431 (org-test-with-temp-text (concat test-line " end")
432 (re-search-forward "src_") (org-babel-execute-maybe)
433 (should (string= (concat test-line " the end")
434 (buffer-substring-no-properties
435 (point-at-bol) (point-at-eol))))
436 (re-search-forward "\" ") (org-babel-execute-maybe)
437 (should (string= (concat test-line " the the end")
438 (buffer-substring-no-properties
439 (point-at-bol) (point-at-eol))))
440 (forward-char 2)
441 (should-error (org-ctrl-c-ctrl-c)))))
443 (ert-deftest test-org-babel/inline-src_blk-results-file ()
444 (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
445 (org-test-with-temp-text
446 test-line
447 (org-babel-execute-maybe)
448 (should (string= (concat test-line " {{{results([[file:~/test-file]])}}}")
449 (buffer-substring-no-properties
450 (point-min) (point-max)))))))
452 (ert-deftest test-org-babel/inline-src_blk-results-scalar ()
453 (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
454 (org-babel-inline-result-wrap "=%s="))
455 (org-test-with-temp-text
456 test-line
457 (org-babel-execute-maybe)
458 (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
459 (buffer-substring-no-properties
460 (point-min) (point-max)))))))
462 (ert-deftest test-org-babel/inline-src_blk-results-verbatim ()
463 (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
464 (org-babel-inline-result-wrap "=%s="))
465 (org-test-with-temp-text
466 test-line
467 (org-babel-execute-maybe)
468 (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
469 (buffer-substring-no-properties
470 (point-min) (point-max)))))))
472 (ert-deftest test-org-babel/combining-scalar-and-raw-result-types ()
473 (org-test-with-temp-text-in-file "
475 #+begin_src sh :results scalar
476 echo \"[[file:./cv.cls]]\"
477 #+end_src
479 #+name:
480 : [[file:./cv.cls]]
482 #+begin_src sh :results raw scalar
483 echo \"[[file:./cv.cls]]\"
484 #+end_src
486 (flet ((next-result ()
487 (org-babel-next-src-block)
488 (org-babel-execute-src-block)
489 (goto-char (org-babel-where-is-src-block-result))
490 (forward-line 1)))
491 (goto-char (point-min))
492 (next-result)
493 (should (org-babel-in-example-or-verbatim))
494 (next-result)
495 (should (not (org-babel-in-example-or-verbatim))))))
497 (ert-deftest test-org-babel/no-defaut-value-for-var ()
498 "Test that the absence of a default value for a variable DOES THROW
499 a proper error."
500 (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
501 (org-babel-next-src-block)
502 (let ((err
503 (should-error (org-babel-execute-src-block) :type 'error)))
504 (should
505 (equal
506 '(error
507 "Variable \"x\" must be assigned a default value")
508 err)))))
510 (ert-deftest test-org-babel/just-one-results-block ()
511 "Test that evaluating two times the same code block does not result in a
512 duplicate results block."
513 (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
514 (org-babel-execute-src-block)
515 (org-babel-execute-src-block) ; second code block execution
516 (should (search-forward "Hello")) ; the string inside the source code block
517 (should (search-forward "Hello")) ; the same string in the results block
518 (should-error (search-forward "Hello"))))
520 (ert-deftest test-org-babel/nested-code-block ()
521 "Test nested code blocks inside code blocks don't cause problems."
522 (should
523 (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
524 (org-test-with-temp-text "#+begin_src org :results silent
525 ,#+begin_src emacs-lisp
526 , 'foo
527 ,#+end_src
528 #+end_src"
529 (let ((org-edit-src-content-indentation 2)
530 (org-src-preserve-indentation nil))
531 (org-babel-execute-src-block))))))
533 (ert-deftest test-org-babel/partial-nested-code-block ()
534 "Test nested code blocks inside code blocks don't cause problems."
535 (org-test-with-temp-text "#+begin_src org :results silent
536 ,#+begin_src emacs-lisp
537 #+end_src"
538 (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
540 (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
541 (org-test-with-temp-text "#+NAME: foo
542 #+BEGIN_SRC emacs-lisp
543 'foo
544 #+END_SRC\n"
545 (org-babel-next-src-block 1)
546 (should (eq 'foo (org-babel-execute-src-block)))
547 (goto-char (point-min))
548 (org-babel-next-src-block 1)
549 (should (looking-at org-babel-src-block-regexp))))
551 (ert-deftest test-ob/catches-all-references ()
552 (org-test-with-temp-text "
553 #+NAME: literal-example
554 #+BEGIN_EXAMPLE
555 A literal example
556 on two lines
557 #+END_EXAMPLE
559 #+NAME: read-literal-example
560 #+BEGIN_SRC emacs-lisp :var x=literal-example
561 (concatenate 'string x \" for me.\")
562 #+END_SRC"
563 (org-babel-next-src-block 1)
564 (should (string= (org-babel-execute-src-block)
565 "A literal example\non two lines\n for me."))))
567 (ert-deftest test-ob/resolve-code-blocks-before-data-blocks ()
568 (org-test-with-temp-text "
569 #+name: foo
570 : bar
572 #+name: foo
573 #+begin_src emacs-lisp
574 \"baz\"
575 #+end_src
577 #+begin_src emacs-lisp :var foo=foo
579 #+end_src"
580 (org-babel-next-src-block 2)
581 (should (string= (org-babel-execute-src-block) "baz"))))
583 (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
584 (org-test-with-temp-text "
585 #+tblname: base_plus
586 | 1 |
587 | 2 |
589 #+tblname: base
590 | 3 |
591 | 4 |
593 #+begin_src emacs-lisp :var x=base
595 #+end_src"
596 (org-babel-next-src-block 1)
597 (should (equal (org-babel-execute-src-block) '((3) (4))))))
599 (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
600 (org-test-with-temp-text "
601 #+name: base_plus
602 #+begin_src emacs-lisp
603 'bar
604 #+end_src
606 #+name: base
607 #+begin_src emacs-lisp
608 'foo
609 #+end_src
611 #+begin_src emacs-lisp :var x=base
613 #+end_src"
614 (org-babel-next-src-block 3)
615 (should (equal (org-babel-execute-src-block) "foo"))))
617 (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
618 (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
619 (+ a b c d)
620 #+end_src
622 (should (= 10 (org-babel-execute-src-block)))))
624 (ert-deftest test-ob/org-babel-update-intermediate ()
625 (org-test-with-temp-text "#+name: foo
626 #+begin_src emacs-lisp
628 #+end_src
630 #+results: foo
633 #+begin_src emacs-lisp :var it=foo
634 (+ it 1)
635 #+end_src"
636 (let ((org-babel-update-intermediate nil))
637 (goto-char (point-min))
638 (org-babel-next-src-block 2)
639 (should (= 3 (org-babel-execute-src-block)))
640 (goto-char (point-min))
641 (forward-line 6)
642 (should (looking-at ": 4")))
643 (let ((org-babel-update-intermediate t))
644 (goto-char (point-min))
645 (org-babel-next-src-block 2)
646 (should (= 3 (org-babel-execute-src-block)))
647 (goto-char (point-min))
648 (forward-line 6)
649 (should (looking-at ": 2")))))
651 (ert-deftest test-ob/eval-header-argument ()
652 (flet ((check-eval (eval runp)
653 (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
654 (setq foo :evald)
655 #+end_src" eval)
656 (let ((foo :not-run))
657 (if runp
658 (progn (should (org-babel-execute-src-block))
659 (should (eq foo :evald)))
660 (progn (should-not (org-babel-execute-src-block))
661 (should-not (eq foo :evald))))))))
662 (check-eval "never" nil)
663 (check-eval "no" nil)
664 (check-eval "never-export" t)
665 (check-eval "no-export" t)
666 (let ((org-babel-exp-reference-buffer (current-buffer)))
667 (check-eval "never" nil)
668 (check-eval "no" nil)
669 (check-eval "never-export" nil)
670 (check-eval "no-export" nil))))
672 (ert-deftest test-ob/noweb-expansion-1 ()
673 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
674 <<foo>>
675 #+end_src
677 #+name: foo
678 #+begin_src sh
680 #+end_src"
681 (should (string= (org-babel-expand-noweb-references) "bar"))))
683 (ert-deftest test-ob/noweb-expansion-2 ()
684 (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
685 <<foo>>
686 #+end_src
688 #+name: foo
689 #+begin_src sh :noweb-sep \"\"
691 #+end_src
693 #+begin_src sh :noweb-ref foo :noweb-sep \"\"
695 #+end_src"
696 (should (string= (org-babel-expand-noweb-references) "barbaz"))))
698 (ert-deftest test-ob/splitting-variable-lists-in-references ()
699 (org-test-with-temp-text ""
700 (should (= 1 (length (org-babel-ref-split-args
701 "a=\"this, no work\""))))
702 (should (= 2 (length (org-babel-ref-split-args
703 "a=\"this, no work\", b=1"))))))
705 (ert-deftest test-ob/org-babel-balanced-split ()
706 (should (equal
707 '(":a 1" "b [2 3]" "c (4 :d (5 6))")
708 (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
709 '((32 9) . 58)))))
711 (ert-deftest test-ob/commented-last-block-line-no-var ()
712 (org-test-with-temp-text-in-file "
713 #+begin_src emacs-lisp
715 #+end_src"
716 (org-babel-next-src-block)
717 (org-babel-execute-maybe)
718 (should (re-search-forward "\\#\\+results:" nil t))
719 (forward-line)
720 (should
721 (string=
723 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
724 (org-test-with-temp-text-in-file "
725 #+begin_src emacs-lisp
726 \"some text\";;
727 #+end_src"
728 (org-babel-next-src-block)
729 (org-babel-execute-maybe)
730 (should (re-search-forward "\\#\\+results:" nil t))
731 (forward-line)
732 (should
733 (string=
734 ": some text"
735 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
737 (ert-deftest test-ob/commented-last-block-line-with-var ()
738 (org-test-with-temp-text-in-file "
739 #+begin_src emacs-lisp :var a=1
741 #+end_src"
742 (org-babel-next-src-block)
743 (org-babel-execute-maybe)
744 (re-search-forward "\\#\\+results:" nil t)
745 (forward-line)
746 (should (string=
748 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
749 (org-test-with-temp-text-in-file "
750 #+begin_src emacs-lisp :var a=2
752 #+end_src"
753 (org-babel-next-src-block)
754 (org-babel-execute-maybe)
755 (re-search-forward "\\#\\+results:" nil t)
756 (forward-line)
757 (should (string=
758 ": 2"
759 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
761 (ert-deftest test-ob/org-babel-insert-result--improper-lists ()
762 "Test `org-babel-insert-result' with improper lists."
763 ;; Do not error when output is an improper list.
764 (should
765 (org-test-with-temp-text
767 <point>#+BEGIN_SRC emacs-lisp
768 '((1 . nil) (2 . 3))
769 #+END_SRC
771 (org-babel-execute-maybe) t)))
773 (ert-deftest test-ob/remove-inline-result ()
774 "Test `org-babel-remove-inline-result' honors whitespace."
775 (let*
776 ((inline-sb "src_emacs-lisp{(+ 1 2)}")
777 (inline-res " {{{results(=3=)}}}")
778 (inline-sb-dot (concat inline-sb "."))
779 (inline-sb-res-dot (concat inline-sb inline-res ".")))
780 (org-test-with-temp-text
781 ;; Insert inline_src_block followed by dot.
782 inline-sb-dot
783 ;; Insert result before dot.
784 (org-babel-execute-maybe)
785 (should (string= inline-sb-res-dot
786 (buffer-substring-no-properties
787 (point-at-bol) (point-at-eol))))
788 ;; Delete whitespace and result.
789 (org-babel-remove-inline-result)
790 (should (string= inline-sb-dot
791 (buffer-substring-no-properties
792 (point-at-bol) (point-at-eol))))
793 ;; Add whitespace and result before dot.
794 (search-forward inline-sb)
795 (insert " " inline-res)
796 (goto-char (point-at-bol))
797 ;; Remove whitespace and result.
798 (org-babel-remove-inline-result)
799 (should (string= inline-sb-dot
800 (buffer-substring-no-properties
801 (point-at-bol) (point-at-eol))))
802 ;; Add whitespace before dot.
803 (search-forward inline-sb)
804 (insert " ")
805 (goto-char (point-at-bol))
806 ;; Add result before whitespace.
807 (org-babel-execute-maybe)
808 ;; Remove result - leave trailing whitespace and dot.
809 (org-babel-remove-inline-result)
810 (should (string= (concat inline-sb " .")
811 (buffer-substring-no-properties
812 (point-at-bol) (point-at-eol)))))))
814 (defun test-ob-verify-result-and-removed-result (result buffer-text)
815 "Test helper function to test `org-babel-remove-result'.
816 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
817 and the result of execution is verified against RESULT.
819 The block is actually executed /twice/ to ensure result
820 replacement happens correctly."
821 (org-test-with-temp-text
822 buffer-text
823 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
824 (should (re-search-forward "\\#\\+results:" nil t))
825 (forward-line)
826 (should (string= result
827 (buffer-substring-no-properties
828 (point-at-bol)
829 (- (point-max) 16))))
830 (org-babel-previous-src-block) (org-babel-remove-result)
831 (should (string= buffer-text
832 (buffer-substring-no-properties
833 (point-min) (point-max))))))
835 (ert-deftest test-ob/org-babel-remove-result--results-default ()
836 "Test `org-babel-remove-result' with default :results."
837 (mapcar (lambda (language)
838 (test-ob-verify-result-and-removed-result
839 "\n"
840 (concat
841 "* org-babel-remove-result
842 #+begin_src " language "
843 #+end_src
845 * next heading")))
846 '("sh" "emacs-lisp")))
848 (ert-deftest test-ob/org-babel-remove-result--results-list ()
849 "Test `org-babel-remove-result' with :results list."
850 (test-ob-verify-result-and-removed-result
851 "- 1
854 - (quote (4 5))"
856 "* org-babel-remove-result
857 #+begin_src emacs-lisp :results list
858 '(1 2 3 '(4 5))
859 #+end_src
861 * next heading"))
863 (ert-deftest test-ob/org-babel-results-indented-wrap ()
864 "Ensure that wrapped results are inserted correction when indented.
865 If not inserted correctly then the second evaluation will fail
866 trying to find the :END: marker."
867 (org-test-with-temp-text
868 "- indented
869 #+begin_src sh :results file wrap
870 echo test.txt
871 #+end_src"
872 (org-babel-next-src-block 1)
873 (org-babel-execute-src-block)
874 (org-babel-execute-src-block)))
876 (ert-deftest test-ob/file-desc-header-argument ()
877 "Test that the :file-desc header argument is used."
878 (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
879 \"foo\"
880 #+end_src
882 #+begin_src emacs-lisp :results file :file-desc
883 \"foo\"
884 #+end_src"
885 (org-babel-execute-src-block)
886 (org-babel-next-src-block 1)
887 (org-babel-execute-src-block)
888 (goto-char (point-min))
889 (should (search-forward "[[file:foo][bar]]" nil t))
890 (should (search-forward "[[file:foo][foo]]" nil t))))
892 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
893 "Test `org-babel-remove-result' with :results pp."
894 (test-ob-verify-result-and-removed-result
895 ": \"I /am/ working!\""
897 "* org-babel-remove-result
898 #+begin_src emacs-lisp :results pp
899 \"I /am/ working!\")
900 #+end_src
902 * next heading"))
904 (ert-deftest test-org-babel/inline-src_blk-preceded-punct-preceded-by-point ()
905 (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
906 (org-babel-inline-result-wrap "=%s="))
907 (org-test-with-temp-text
908 test-line
909 (forward-char 1)
910 (org-babel-execute-maybe)
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 (org-babel-next-src-block)
920 (org-babel-execute-maybe)
921 (re-search-forward "\\#\\+results:" nil t)
922 (forward-line)
923 (should (string=
925 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
926 (org-test-with-temp-text-in-file "
927 #+begin_src emacs-lisp :var a=2
929 #+end_src"
930 (org-babel-next-src-block)
931 (org-babel-execute-maybe)
932 (re-search-forward "\\#\\+results:" nil t)
933 (forward-line)
934 (should (string=
935 ": 2"
936 (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
938 (defun test-ob-verify-result-and-removed-result (result buffer-text)
939 "Test helper function to test `org-babel-remove-result'.
940 A temp buffer is populated with BUFFER-TEXT, the first block is executed,
941 and the result of execution is verified against RESULT.
943 The block is actually executed /twice/ to ensure result
944 replacement happens correctly."
945 (org-test-with-temp-text
946 buffer-text
947 (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
948 (should (re-search-forward "\\#\\+results:" nil t))
949 (forward-line)
950 (should (string= result
951 (buffer-substring-no-properties
952 (point-at-bol)
953 (- (point-max) 16))))
954 (org-babel-previous-src-block) (org-babel-remove-result)
955 (should (string= buffer-text
956 (buffer-substring-no-properties
957 (point-min) (point-max))))))
959 (ert-deftest test-ob/org-babel-remove-result--results-default ()
960 "Test `org-babel-remove-result' with default :results."
961 (mapcar (lambda (language)
962 (test-ob-verify-result-and-removed-result
963 "\n"
964 (concat
965 "* org-babel-remove-result
966 #+begin_src " language "
967 #+end_src
969 * next heading")))
970 '("sh" "emacs-lisp")))
972 (ert-deftest test-ob/org-babel-remove-result--results-list ()
973 "Test `org-babel-remove-result' with :results list."
974 (test-ob-verify-result-and-removed-result
975 "- 1
978 - (quote (4 5))"
980 "* org-babel-remove-result
981 #+begin_src emacs-lisp :results list
982 '(1 2 3 '(4 5))
983 #+end_src
985 * next heading"))
987 (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
988 "Test `org-babel-remove-result' with :results wrap."
989 (test-ob-verify-result-and-removed-result
990 ":RESULTS:
991 hello there
992 :END:"
994 "* org-babel-remove-result
996 #+begin_src emacs-lisp :results wrap
997 \"hello there\"
998 #+end_src
1000 * next heading"))
1002 (ert-deftest test-ob/org-babel-remove-result--results-org ()
1003 "Test `org-babel-remove-result' with :results org."
1004 (test-ob-verify-result-and-removed-result
1005 "#+BEGIN_SRC org
1006 ,* heading
1007 ,** subheading
1008 content
1009 #+END_SRC"
1011 "* org-babel-remove-result
1012 #+begin_src emacs-lisp :results org
1013 \"* heading
1014 ,** subheading
1015 content\"
1016 #+end_src
1018 * next heading"))
1020 (ert-deftest test-ob/org-babel-remove-result--results-html ()
1021 "Test `org-babel-remove-result' with :results html."
1022 (test-ob-verify-result-and-removed-result
1023 "#+BEGIN_HTML
1024 <head><body></body></head>
1025 #+END_HTML"
1027 "* org-babel-remove-result
1028 #+begin_src emacs-lisp :results html
1029 \"<head><body></body></head>\"
1030 #+end_src
1032 * next heading"))
1034 (ert-deftest test-ob/org-babel-remove-result--results-latex ()
1035 "Test `org-babel-remove-result' with :results latex."
1036 (test-ob-verify-result-and-removed-result
1037 "#+BEGIN_LaTeX
1038 Line 1
1039 Line 2
1040 Line 3
1041 #+END_LaTeX"
1043 "* org-babel-remove-result
1044 #+begin_src emacs-lisp :results latex
1045 \"Line 1
1046 Line 2
1047 Line 3\"
1048 #+end_src
1050 * next heading"))
1052 (ert-deftest test-ob/org-babel-remove-result--results-code ()
1053 "Test `org-babel-remove-result' with :results code."
1055 (test-ob-verify-result-and-removed-result
1056 "#+BEGIN_SRC emacs-lisp
1057 \"I am working!\"
1058 #+END_SRC"
1060 "* org-babel-remove-result
1061 #+begin_src emacs-lisp :results code
1062 (message \"I am working!\")
1063 #+end_src
1065 * next heading"))
1067 (ert-deftest test-ob/org-babel-remove-result--results-pp ()
1068 "Test `org-babel-remove-result' with :results pp."
1069 (test-ob-verify-result-and-removed-result
1070 ": \"I /am/ working!\""
1072 "* org-babel-remove-result
1073 #+begin_src emacs-lisp :results pp
1074 \"I /am/ working!\")
1075 #+end_src
1077 * next heading"))
1079 (ert-deftest test-ob/results-do-not-replace-code-blocks ()
1080 (org-test-with-temp-text "Block two has a space after the name.
1082 #+name: foo
1083 #+begin_src emacs-lisp
1085 #+end_src
1087 #+name: foo
1088 #+begin_src emacs-lisp
1090 #+end_src
1092 #+name: foo
1093 #+begin_src emacs-lisp
1095 #+end_src
1097 #+RESULTS: foo
1098 : foo
1100 (dolist (num '(1 2 3))
1101 ;; execute the block
1102 (goto-char (point-min))
1103 (org-babel-next-src-block num) (org-babel-execute-src-block)
1104 ;; check the results
1105 (goto-char (point-max))
1106 (move-beginning-of-line 0)
1107 (should (looking-at (format ": %d" num))))))
1109 (ert-deftest test-ob/blocks-with-spaces ()
1110 "Test expansion of blocks followed by blank lines."
1111 (should
1112 (equal "#+BEGIN_SRC emacs-lisp
1113 \(+ 1 2)
1114 #+END_SRC
1116 #+RESULTS:
1117 : 3\n\n\n"
1118 (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
1119 \(+ 1 2)
1120 #+END_SRC\n\n\n"
1121 (let ((org-babel-next-src-block "RESULTS"))
1122 (org-babel-execute-src-block))
1123 (buffer-string)))))
1125 (ert-deftest test-ob/results-in-narrowed-buffer ()
1126 "Test block execution in a narrowed buffer."
1127 ;; If results don't exist, they should be inserted in visible part
1128 ;; of the buffer.
1129 (should
1130 (equal
1131 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
1132 (org-test-with-temp-text
1133 "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1134 (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
1135 (let ((org-babel-results-keyword "RESULTS"))
1136 (org-babel-execute-src-block))
1137 (org-trim (buffer-string)))))
1138 (should
1139 (equal
1140 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
1141 (org-test-with-temp-text
1142 "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
1143 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1144 (let ((org-babel-results-keyword "RESULTS"))
1145 (org-babel-execute-src-block))
1146 (org-trim (buffer-string)))))
1147 ;; Results in visible part of buffer, should be updated here.
1148 (should
1149 (equal
1150 "#+NAME: test
1151 #+BEGIN_SRC emacs-lisp
1152 \(+ 1 2)
1153 #+END_SRC
1155 #+RESULTS: test
1156 : 3"
1157 (org-test-with-temp-text
1158 "#+NAME: test
1159 #+BEGIN_SRC emacs-lisp
1160 \(+ 1 2)
1161 #+END_SRC
1163 #+RESULTS: test
1166 Paragraph"
1167 (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
1168 (let ((org-babel-results-keyword "RESULTS"))
1169 (org-babel-execute-src-block))
1170 (org-trim (buffer-string)))))
1171 ;; Results in invisible part of buffer, should be updated there.
1172 (org-test-with-temp-text
1173 "#+NAME: test
1174 #+BEGIN_SRC emacs-lisp
1175 \(+ 1 2)
1176 #+END_SRC
1178 #+RESULTS: test
1181 Paragraph"
1182 (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
1183 (let ((org-babel-results-keyword "RESULTS"))
1184 (org-babel-execute-src-block))
1185 (should-not (re-search-forward "^#\\+RESULTS:" nil t))
1186 (widen)
1187 (should (should (re-search-forward "^: 3" nil t)))))
1189 (ert-deftest test-ob/specific-colnames ()
1190 "Test passing specific column names."
1191 (should
1192 (equal "#+name: input-table
1193 | id | var1 |
1194 |----+------|
1195 | 1 | bar |
1196 | 2 | baz |
1198 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1199 echo \"$data\"
1200 #+end_src
1202 #+RESULTS:
1203 | Rev | Author |
1204 |-----+--------|
1205 | 1 | bar |
1206 | 2 | baz |
1209 (org-test-with-temp-text
1210 "#+name: input-table
1211 | id | var1 |
1212 |----+------|
1213 | 1 | bar |
1214 | 2 | baz |
1216 #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
1217 echo \"$data\"
1218 #+end_src
1220 ;; we should find a code block
1221 (should (re-search-forward org-babel-src-block-regexp nil t))
1222 (goto-char (match-beginning 0))
1223 ;; now that we've located the code block, it may be evaluated
1224 (let ((org-babel-execute-src-block "RESULTS"))
1225 (org-babel-execute-src-block))
1226 (buffer-string)))))
1228 (ert-deftest test-ob/location-of-header-arg-eval ()
1229 "Test location of header argument evaluation."
1230 (org-test-with-temp-text "
1231 #+name: top-block
1232 #+begin_src emacs-lisp :var pt=(point)
1234 #+end_src
1236 #+name: bottom-block
1237 #+begin_src emacs-lisp :var pt=top-block()
1239 #+end_src
1241 ;; the value of the second block should be greater than the first
1242 (should
1243 (< (progn (re-search-forward org-babel-src-block-regexp nil t)
1244 (goto-char (match-beginning 0))
1245 (prog1 (save-match-data (org-babel-execute-src-block))
1246 (goto-char (match-end 0))))
1247 (progn (re-search-forward org-babel-src-block-regexp nil t)
1248 (goto-char (match-beginning 0))
1249 (org-babel-execute-src-block))))))
1251 (ert-deftest test-ob/preserve-results-indentation ()
1252 "Preserve indentation when executing a src block."
1253 (should
1254 (equal '(2 2)
1255 (org-test-with-temp-text
1256 " #+begin_src emacs-lisp\n (+ 1 1)\n #+end_src"
1257 (org-babel-execute-src-block)
1258 (buffer-string)
1259 (let ((case-fold-search t)) (search-forward "#+results:"))
1260 ;; Check if both #+RESULTS: keyword and actual results are
1261 ;; indented by 2 columns.
1262 (list (org-get-indentation)
1263 (progn (forward-line) (org-get-indentation)))))))
1265 (ert-deftest test-ob/safe-header-args ()
1266 "Detect safe and unsafe header args."
1267 (let ((safe-args '((:cache . "foo")
1268 (:results . "output")
1269 (:eval . "never")
1270 (:eval . "query")))
1271 (unsafe-args '((:eval . "yes")
1272 (:results . "output file")
1273 (:foo . "bar")))
1274 (malformed-args '((bar . "foo")
1275 ("foo" . "bar")
1276 :foo))
1277 (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
1278 (dolist (arg safe-args)
1279 (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
1280 (dolist (arg unsafe-args)
1281 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1282 (dolist (arg malformed-args)
1283 (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
1284 (should (not (funcall safe-p (append safe-args unsafe-args))))))
1286 (ert-deftest test-ob/noweb-expansions-in-cache ()
1287 "Ensure that noweb expansions are expanded before caching."
1288 (let ((noweb-expansions-in-cache-var 0))
1289 (org-test-with-temp-text "
1290 #+name: foo
1291 #+begin_src emacs-lisp
1292 \"I said\"
1293 #+end_src
1295 #+name: bar
1296 #+begin_src emacs-lisp :noweb yes :cache yes
1297 (setq noweb-expansions-in-cache-var
1298 (+ 1 noweb-expansions-in-cache-var))
1299 (concat <<foo>> \" check noweb expansions\")
1300 #+end_src
1302 ;; run the second block to create the cache
1303 (goto-char (point-min))
1304 (re-search-forward (regexp-quote "#+name: bar"))
1305 (should (string= "I said check noweb expansions"
1306 (org-babel-execute-src-block)))
1307 (should (= noweb-expansions-in-cache-var 1))
1308 ;; change the value of the first block
1309 (goto-char (point-min))
1310 (re-search-forward (regexp-quote "said"))
1311 (goto-char (match-beginning 0))
1312 (insert "haven't ")
1313 (re-search-forward (regexp-quote "#+name: bar"))
1314 (should (string= "I haven't said check noweb expansions"
1315 (org-babel-execute-src-block)))
1316 (should (= noweb-expansions-in-cache-var 2)))))
1318 (ert-deftest test-org-babel/file-ext-and-output-dir ()
1319 (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
1320 (org-babel-next-src-block)
1321 (should (equal "file-ext-basic.txt"
1322 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1323 (org-babel-next-src-block)
1324 (should (equal "foo/file-ext-dir-relative.txt"
1325 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1326 (org-babel-next-src-block)
1327 (should (equal "foo/file-ext-dir-relative-slash.txt"
1328 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1329 (org-babel-next-src-block)
1330 (should (equal "/tmp/file-ext-dir-absolute.txt"
1331 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1332 (org-babel-next-src-block)
1333 (should (equal "foo.bar"
1334 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1335 (org-babel-next-src-block)
1336 (should (equal "xxx/foo.bar"
1337 (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
1340 (ert-deftest test-org-babel/script-escape ()
1341 ;; Delimited lists of numbers
1342 (should (equal '(1 2 3)
1343 (org-babel-script-escape "[1 2 3]")))
1344 (should (equal '(1 2 3)
1345 (org-babel-script-escape "{1 2 3}")))
1346 (should (equal '(1 2 3)
1347 (org-babel-script-escape "(1 2 3)")))
1348 ;; Delimited lists of double-quoted strings
1349 (should (equal '("foo" "bar")
1350 (org-babel-script-escape "(\"foo\" \"bar\")")))
1351 (should (equal '("foo" "bar")
1352 (org-babel-script-escape "[\"foo\" \"bar\"]")))
1353 (should (equal '("foo" "bar")
1354 (org-babel-script-escape "{\"foo\" \"bar\"}")))
1355 ;; ... with commas
1356 (should (equal '("foo" "bar")
1357 (org-babel-script-escape "(\"foo\", \"bar\")")))
1358 (should (equal '("foo" "bar")
1359 (org-babel-script-escape "[\"foo\", \"bar\"]")))
1360 (should (equal '("foo" "bar")
1361 (org-babel-script-escape "{\"foo\", \"bar\"}")))
1362 ;; Delimited lists of single-quoted strings
1363 (should (equal '("foo" "bar")
1364 (org-babel-script-escape "('foo' 'bar')")))
1365 (should (equal '("foo" "bar")
1366 (org-babel-script-escape "['foo' 'bar']")))
1367 (should (equal '("foo" "bar")
1368 (org-babel-script-escape "{'foo' 'bar'}")))
1369 ;; ... with commas
1370 (should (equal '("foo" "bar")
1371 (org-babel-script-escape "('foo', 'bar')")))
1372 (should (equal '("foo" "bar")
1373 (org-babel-script-escape "['foo', 'bar']")))
1374 (should (equal '("foo" "bar")
1375 (org-babel-script-escape "{'foo', 'bar'}")))
1376 ;; Single quoted strings
1377 (should (equal "foo"
1378 (org-babel-script-escape "'foo'")))
1379 ;; ... with internal double quote
1380 (should (equal "foo\"bar"
1381 (org-babel-script-escape "'foo\"bar'")))
1382 ;; ... with internal backslash
1383 (should (equal "foo\\bar"
1384 (org-babel-script-escape "'foo\\bar'")))
1385 ;; ... with internal escaped backslash
1386 (should (equal "foo\\bar"
1387 (org-babel-script-escape "'foo\\\\bar'")))
1388 ;; ... with internal backslash-double quote
1389 (should (equal "foo\\\"bar"
1390 (org-babel-script-escape "'foo\\\"bar'")))
1391 ;; ... with internal escaped backslash-double quote
1392 (should (equal "foo\\\"bar"
1393 (org-babel-script-escape "'foo\\\\\"bar'")))
1394 ;; ... with internal escaped single quote
1395 (should (equal "foo'bar"
1396 (org-babel-script-escape "'foo\\'bar'")))
1397 ;; ... with internal escaped backslash-escaped single quote
1398 (should (equal "foo\\'bar"
1399 (org-babel-script-escape "'foo\\\\\\'bar'")))
1400 ;; Double quoted strings
1401 (should (equal "foo"
1402 (org-babel-script-escape "\"foo\"")))
1403 ;; ... with internal single quote
1404 (should (equal "foo'bar"
1405 (org-babel-script-escape "\"foo'bar\"")))
1406 ;; ... with internal backslash
1407 (should (equal "foo\\bar"
1408 (org-babel-script-escape "\"foo\\bar\"")))
1409 ;; ... with internal escaped backslash
1410 (should (equal "foo\\bar"
1411 (org-babel-script-escape "\"foo\\\\bar\"")))
1412 ;; ... with internal backslash-single quote
1413 (should (equal "foo\\'bar"
1414 (org-babel-script-escape "\"foo\\'bar\"")))
1415 ;; ... with internal escaped backslash-single quote
1416 (should (equal "foo\\'bar"
1417 (org-babel-script-escape "\"foo\\\\'bar\"")))
1418 ;; ... with internal escaped double quote
1419 (should (equal "foo\"bar"
1420 (org-babel-script-escape "\"foo\\\"bar\"")))
1421 ;; ... with internal escaped backslash-escaped double quote
1422 (should (equal "foo\\\"bar"
1423 (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
1425 (provide 'test-ob)
1427 ;;; test-ob ends here