test-ob.el 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. ;;; test-ob.el --- tests for ob.el
  2. ;; Copyright (c) 2010-2014 Eric Schulte
  3. ;; Authors: Eric Schulte, Martyn Jago
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (ert-deftest test-org-babel/multi-line-header-regexp ()
  17. (should(equal "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
  18. org-babel-multi-line-header-regexp))
  19. ;;TODO can be optimised - and what about blah4 blah5 blah6?
  20. (should (string-match
  21. org-babel-multi-line-header-regexp
  22. " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
  23. (should
  24. (equal
  25. "blah1 blah2 blah3 \t"
  26. (match-string
  27. 1
  28. " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
  29. ;;TODO Check - should this fail?
  30. (should
  31. (not (org-test-string-exact-match
  32. org-babel-multi-line-header-regexp
  33. " \t #+headers : blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))))
  34. (ert-deftest test-org-babel/src-block-regexp ()
  35. (let ((test-block
  36. (concat
  37. "#+begin_src language -n-r-a-b -c :argument-1 yes :argument-2 no\n"
  38. "echo this is a test\n"
  39. "echo Currently in ' $PWD\n"
  40. "#+end_src"))
  41. (language "language")
  42. (flags "-n-r-a-b -c ")
  43. (arguments ":argument-1 yes :argument-2 no")
  44. (body "echo this is a test\necho Currently in ' $PWD\n"))
  45. (should (string-match org-babel-src-block-regexp test-block))
  46. (should (string-match org-babel-src-block-regexp (upcase test-block)))
  47. (should (equal language (match-string 2 test-block)))
  48. ;;TODO Consider refactoring
  49. (should (equal flags (match-string 3 test-block)))
  50. (should (equal arguments (match-string 4 test-block)))
  51. (should (equal body (match-string 5 test-block)))
  52. ;;no switches
  53. (should (org-test-string-exact-match
  54. org-babel-src-block-regexp
  55. (replace-regexp-in-string flags "" test-block)))
  56. ;;no header arguments
  57. (should (org-test-string-exact-match
  58. org-babel-src-block-regexp
  59. (replace-regexp-in-string arguments "" test-block)))
  60. ;; should be valid with no body
  61. (should (org-test-string-exact-match
  62. org-babel-src-block-regexp
  63. (replace-regexp-in-string body "" test-block)))))
  64. (ert-deftest test-org-babel/get-header ()
  65. (should (not (org-babel-get-header
  66. org-babel-default-header-args :doesnt-exist)))
  67. (should(equal '((:session . "none"))
  68. (org-babel-get-header
  69. org-babel-default-header-args :session)))
  70. (should(equal '((:session . "none"))
  71. (org-babel-get-header
  72. org-babel-default-header-args :session nil)))
  73. (should (not (org-babel-get-header
  74. org-babel-default-header-args :SESSION)))
  75. (should (equal '((:tangle . "no"))
  76. (org-babel-get-header
  77. org-babel-default-header-args :tangle)))
  78. ;; with OTHERS option
  79. (should (equal org-babel-default-header-args
  80. (org-babel-get-header
  81. org-babel-default-header-args :doesnt-exist 'others)))
  82. (should (equal org-babel-default-header-args
  83. (org-babel-get-header
  84. org-babel-default-header-args nil 'others)))
  85. (should (null
  86. (assoc :noweb
  87. (org-babel-get-header
  88. org-babel-default-header-args :noweb 'others)))))
  89. (ert-deftest test-org-babel/default-inline-header-args ()
  90. (should(equal
  91. '((:session . "none")
  92. (:results . "replace")
  93. (:exports . "results")
  94. (:hlines . "yes"))
  95. org-babel-default-inline-header-args)))
  96. (ert-deftest ob-test/org-babel-combine-header-arg-lists ()
  97. (let ((results (org-babel-combine-header-arg-lists
  98. '((foo . :any)
  99. (bar)
  100. (baz . ((foo bar) (baz)))
  101. (qux . ((foo bar baz qux)))
  102. (quux . ((foo bar))))
  103. '((bar)
  104. (baz . ((baz)))
  105. (quux . :any)))))
  106. (dolist (pair '((foo . :any)
  107. (bar)
  108. (baz . ((baz)))
  109. (quux . :any)
  110. (qux . ((foo bar baz qux)))))
  111. (should (equal (cdr pair)
  112. (cdr (assoc (car pair) results)))))))
  113. ;;; ob-get-src-block-info
  114. (ert-deftest test-org-babel/get-src-block-info-language ()
  115. (org-test-at-marker nil org-test-file-ob-anchor
  116. (let ((info (org-babel-get-src-block-info)))
  117. (should (string= "emacs-lisp" (nth 0 info))))))
  118. (ert-deftest test-org-babel/get-src-block-info-body ()
  119. (org-test-at-marker nil org-test-file-ob-anchor
  120. (let ((info (org-babel-get-src-block-info)))
  121. (should (string-match (regexp-quote org-test-file-ob-anchor)
  122. (nth 1 info))))))
  123. (ert-deftest test-org-babel/get-src-block-info-tangle ()
  124. (org-test-at-marker nil org-test-file-ob-anchor
  125. (let ((info (org-babel-get-src-block-info)))
  126. (should (string= "no" (cdr (assoc :tangle (nth 2 info))))))))
  127. (ert-deftest test-org-babel/elisp-in-header-arguments ()
  128. "Test execution of elisp forms in header arguments."
  129. (org-test-with-temp-text-in-file "
  130. * elisp forms in header arguments
  131. :PROPERTIES:
  132. :var: prop = (* 7 6)
  133. :END:
  134. #+begin_src emacs-lisp
  135. prop
  136. #+end_src"
  137. (goto-char (point-min))
  138. (org-babel-next-src-block)
  139. (let ((info (org-babel-get-src-block-info)))
  140. (should (= 42 (org-babel-execute-src-block))))))
  141. (ert-deftest test-org-babel/simple-named-code-block ()
  142. "Test that simple named code blocks can be evaluated."
  143. (org-test-with-temp-text-in-file "
  144. #+name: i-have-a-name
  145. #+begin_src emacs-lisp
  146. 42
  147. #+end_src"
  148. (org-babel-next-src-block 1)
  149. (should (= 42 (org-babel-execute-src-block)))))
  150. (ert-deftest test-org-babel/simple-variable-resolution ()
  151. "Test that simple variable resolution is working."
  152. (org-test-with-temp-text-in-file "
  153. #+name: four
  154. #+begin_src emacs-lisp
  155. (list 1 2 3 4)
  156. #+end_src
  157. #+begin_src emacs-lisp :var four=four
  158. (length four)
  159. #+end_src"
  160. (org-babel-next-src-block 2)
  161. (should (= 4 (org-babel-execute-src-block)))
  162. (forward-line 5)
  163. (should (string= ": 4" (buffer-substring
  164. (point-at-bol)
  165. (point-at-eol))))))
  166. (ert-deftest test-org-babel/multi-line-header-arguments ()
  167. "Test that multi-line header arguments and can be read."
  168. (org-test-with-temp-text-in-file "
  169. #+headers: :var letters='(a b c d e f g)
  170. #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
  171. (require 'cl)
  172. (defalias 'my-map (if (org-version-check \"24.2.50\" \"cl\" :predicate)
  173. 'cl-map
  174. 'map))
  175. (my-map 'list #'list numbers letters)
  176. #+end_src"
  177. (org-babel-next-src-block)
  178. (let ((results (org-babel-execute-src-block)))
  179. (should(equal 'a (cadr (assoc 1 results))))
  180. (should(equal 'd (cadr (assoc 4 results)))))))
  181. (ert-deftest test-org-babel/parse-header-args ()
  182. (org-test-with-temp-text-in-file "
  183. #+begin_src example-lang :session :results output :var num=9
  184. the body
  185. #+end_src"
  186. (org-babel-next-src-block)
  187. (let* ((info (org-babel-get-src-block-info))
  188. (params (nth 2 info)))
  189. (message "%S" params)
  190. (should (equal "example-lang" (nth 0 info)))
  191. (should (string= "the body" (org-babel-trim (nth 1 info))))
  192. (should-not (member '(:session\ \ \ \ ) params))
  193. (should (equal '(:session) (assoc :session params)))
  194. (should (equal '(:result-type . output) (assoc :result-type params)))
  195. (should (equal '(num . 9) (cdr (assoc :var params)))))))
  196. (ert-deftest test-org-babel/parse-header-args2 ()
  197. (org-test-with-temp-text-in-file "
  198. * resolving sub-trees as references
  199. #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
  200. (length text)
  201. #+end_src
  202. #+begin_src org :noweb yes
  203. <<simple-subtree>>
  204. <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
  205. #+end_src
  206. ** simple subtree with custom ID
  207. :PROPERTIES:
  208. :CUSTOM_ID: simple-subtree
  209. :END:
  210. this is simple"
  211. (should (string-match (regexp-quote "this is simple")
  212. (org-babel-ref-resolve "simple-subtree")))
  213. (org-babel-next-src-block)
  214. (should (= 14 (org-babel-execute-src-block)))))
  215. (ert-deftest test-org-babel/inline-src-blocks ()
  216. (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
  217. (macrolet ((at-next (&rest body)
  218. `(progn
  219. (move-end-of-line 1)
  220. (re-search-forward org-babel-inline-src-block-regexp nil t)
  221. (goto-char (match-beginning 1))
  222. (save-match-data ,@body))))
  223. (at-next (should (equal 1 (org-babel-execute-src-block))))
  224. (at-next (should (equal 2 (org-babel-execute-src-block))))
  225. (at-next (should (equal 3 (org-babel-execute-src-block)))))))
  226. (ert-deftest test-org-babel/org-babel-get-inline-src-block-matches ()
  227. (org-test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74"
  228. (let ((test-point (point)))
  229. (should (fboundp 'org-babel-get-inline-src-block-matches))
  230. (should (re-search-forward "src_" nil t)) ;; 1
  231. (should (org-babel-get-inline-src-block-matches))
  232. (should (re-search-forward "}" nil (point-at-bol))) ;; 1
  233. (should-not (org-babel-get-inline-src-block-matches))
  234. (should (re-search-forward "in" nil t)) ;; 2
  235. (should-not (org-babel-get-inline-src-block-matches))
  236. (should (re-search-forward "echo" nil t)) ;; 2
  237. (should (org-babel-get-inline-src-block-matches))
  238. (should (re-search-forward "blocks" nil t)) ;; 3
  239. (backward-char 8) ;; 3
  240. (should (org-babel-get-inline-src-block-matches))
  241. (forward-char 1) ;;3
  242. (should-not (org-babel-get-inline-src-block-matches))
  243. (should (re-search-forward ":results" nil t)) ;; 4
  244. (should (org-babel-get-inline-src-block-matches))
  245. (end-of-line)
  246. (should-not (org-babel-get-inline-src-block-matches)))))
  247. (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-1 ()
  248. (let ((test-line "src_sh{echo 1}"))
  249. ;; src_ at bol line 1...
  250. (org-test-with-temp-text
  251. test-line
  252. (goto-char (point-min)) (org-ctrl-c-ctrl-c)
  253. (should (string=
  254. (concat test-line " =1=")
  255. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  256. (forward-char) (org-ctrl-c-ctrl-c)
  257. (should (string=
  258. (concat test-line " =1= =1=")
  259. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  260. (re-search-forward "1}")
  261. (should-error (org-ctrl-c-ctrl-c))
  262. (backward-char) ;; last char of block body
  263. (org-ctrl-c-ctrl-c)
  264. (should (string=
  265. (concat test-line " =1= =1= =1=")
  266. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  267. ;; src_ follows space line 1...
  268. (let ((test-line " src_emacs-lisp{ 1 }"))
  269. (org-test-with-temp-text
  270. test-line
  271. (should-error (org-ctrl-c-ctrl-c))
  272. (forward-char) (org-ctrl-c-ctrl-c)
  273. (should (string=
  274. (concat test-line " =1=")
  275. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  276. (re-search-forward "{ 1 ") (org-ctrl-c-ctrl-c)
  277. (should (string=
  278. (concat test-line " =1= =1=")
  279. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  280. (forward-char)
  281. (should-error (org-ctrl-c-ctrl-c))))))
  282. (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-2 ()
  283. ;; src_ at bol line 2...
  284. (let ((test-line " src_emacs-lisp{ \"x\" }"))
  285. (org-test-with-temp-text
  286. (concat "\n" test-line)
  287. (should-error (org-ctrl-c-ctrl-c))
  288. (goto-char (point-min))
  289. (should-error (org-ctrl-c-ctrl-c))
  290. (forward-line)
  291. (should-error (org-ctrl-c-ctrl-c))
  292. (forward-char) (org-ctrl-c-ctrl-c)
  293. (should (string=
  294. (concat test-line " =x=")
  295. (buffer-substring-no-properties
  296. (point-at-bol) (point-at-eol))))))
  297. (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }"))
  298. (org-test-with-temp-text
  299. test-line
  300. (goto-char (point-max))
  301. (insert (concat "\n" test-line " end"))
  302. (re-search-backward "src") (org-ctrl-c-ctrl-c)
  303. (should (string=
  304. (concat test-line " =y= end")
  305. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  306. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  307. (should (string=
  308. (concat test-line " =y= =y= end")
  309. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  310. (forward-char)
  311. (should-error (org-ctrl-c-ctrl-c)))))
  312. (ert-deftest test-org-babel/inline-src_blk-manual-results-replace ()
  313. (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }"))
  314. (org-test-with-temp-text
  315. (concat "\n" test-line)
  316. (should-error (org-ctrl-c-ctrl-c))
  317. (goto-char (point-max))
  318. (should-error (org-ctrl-c-ctrl-c))
  319. (beginning-of-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))))))
  326. (let ((test-line (concat " Some text prior to block "
  327. "src_emacs-lisp[:results replace]{ \"y\" }")))
  328. (org-test-with-temp-text test-line
  329. (goto-char (point-max))
  330. (insert (concat "\n" test-line " end"))
  331. (re-search-backward "src") (org-ctrl-c-ctrl-c)
  332. (should (string=
  333. (concat test-line " =y= end")
  334. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  335. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  336. (should (string=
  337. (concat test-line " =y= =y= end")
  338. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  339. (forward-char)
  340. (should-error (org-ctrl-c-ctrl-c)))))
  341. (ert-deftest test-org-babel/inline-src_blk-results-silent ()
  342. (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
  343. (org-test-with-temp-text test-line
  344. (org-ctrl-c-ctrl-c)
  345. (should (string= test-line
  346. (buffer-substring-no-properties
  347. (point-at-bol) (point-at-eol))))
  348. (end-of-buffer)
  349. (should-error (org-ctrl-c-ctrl-c))))
  350. (let ((test-line (concat " Some text prior to block src_emacs-lisp"
  351. "[ :results silent ]{ \"y\" }")))
  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-ctrl-c-ctrl-c)
  357. (should (string= (concat test-line " end")
  358. (buffer-substring-no-properties
  359. (point-at-bol) (point-at-eol))))
  360. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  361. (should (string= (concat test-line " end")
  362. (buffer-substring-no-properties
  363. (point-at-bol) (point-at-eol))))
  364. (forward-char)
  365. (should-error (org-ctrl-c-ctrl-c)))))
  366. (ert-deftest test-org-babel/inline-src_blk-results-raw ()
  367. (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
  368. (org-test-with-temp-text test-line
  369. (org-ctrl-c-ctrl-c)
  370. (should (string= (concat test-line " x")
  371. (buffer-string)))))
  372. (let ((test-line (concat " Some text prior to block "
  373. "src_emacs-lisp[ :results raw ]{ \"the\" }")))
  374. (org-test-with-temp-text (concat test-line " end")
  375. (re-search-forward "src_") (org-ctrl-c-ctrl-c)
  376. (should (string= (concat test-line " the end")
  377. (buffer-substring-no-properties
  378. (point-at-bol) (point-at-eol))))
  379. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  380. (should (string= (concat test-line " the the end")
  381. (buffer-substring-no-properties
  382. (point-at-bol) (point-at-eol))))
  383. (forward-char)
  384. (should-error (org-ctrl-c-ctrl-c)))))
  385. (ert-deftest test-org-babel/inline-src_blk-results-file ()
  386. (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
  387. (org-test-with-temp-text
  388. test-line
  389. (org-ctrl-c-ctrl-c)
  390. (should (string= (concat test-line " [[file:~/test-file]]")
  391. (buffer-substring-no-properties
  392. (point-min) (point-max)))))))
  393. (ert-deftest test-org-babel/inline-src_blk-results-scalar ()
  394. (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }"))
  395. (org-test-with-temp-text
  396. test-line
  397. (org-ctrl-c-ctrl-c)
  398. (should (string= (concat test-line " =\"x\"=")
  399. (buffer-substring-no-properties
  400. (point-min) (point-max)))))))
  401. (ert-deftest test-org-babel/inline-src_blk-results-verbatim ()
  402. (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
  403. (org-test-with-temp-text
  404. test-line
  405. (org-ctrl-c-ctrl-c)
  406. (should (string= (concat test-line " =\"x\"=")
  407. (buffer-substring-no-properties
  408. (point-min) (point-max)))))))
  409. (ert-deftest test-org-babel/combining-scalar-and-raw-result-types ()
  410. (org-test-with-temp-text-in-file "
  411. #+begin_src sh :results scalar
  412. echo \"[[file:./cv.cls]]\"
  413. #+end_src
  414. #+name:
  415. : [[file:./cv.cls]]
  416. #+begin_src sh :results raw scalar
  417. echo \"[[file:./cv.cls]]\"
  418. #+end_src
  419. "
  420. (flet ((next-result ()
  421. (org-babel-next-src-block)
  422. (org-babel-execute-src-block)
  423. (goto-char (org-babel-where-is-src-block-result))
  424. (forward-line 1)))
  425. (goto-char (point-min))
  426. (next-result)
  427. (should (org-babel-in-example-or-verbatim))
  428. (next-result)
  429. (should (not (org-babel-in-example-or-verbatim))))))
  430. (ert-deftest test-org-babel/no-defaut-value-for-var ()
  431. "Test that the absence of a default value for a variable DOES THROW
  432. a proper error."
  433. (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
  434. (org-babel-next-src-block)
  435. (let ((err
  436. (should-error (org-babel-execute-src-block) :type 'error)))
  437. (should
  438. (equal
  439. '(error
  440. "Variable \"x\" must be assigned a default value")
  441. err)))))
  442. (ert-deftest test-org-babel/just-one-results-block ()
  443. "Test that evaluating two times the same code block does not result in a
  444. duplicate results block."
  445. (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
  446. (org-babel-execute-src-block)
  447. (org-babel-execute-src-block) ; second code block execution
  448. (should (search-forward "Hello")) ; the string inside the source code block
  449. (should (search-forward "Hello")) ; the same string in the results block
  450. (should-error (search-forward "Hello"))))
  451. (ert-deftest test-org-babel/nested-code-block ()
  452. "Test nested code blocks inside code blocks don't cause problems."
  453. (org-test-with-temp-text "#+begin_src org :results silent
  454. ,#+begin_src emacs-lisp
  455. , 'foo
  456. ,#+end_src
  457. #+end_src"
  458. (should (string= (org-babel-execute-src-block)
  459. "#+begin_src emacs-lisp\n 'foo\n#+end_src"))))
  460. (ert-deftest test-org-babel/partial-nested-code-block ()
  461. "Test nested code blocks inside code blocks don't cause problems."
  462. (org-test-with-temp-text "#+begin_src org :results silent
  463. ,#+begin_src emacs-lisp
  464. #+end_src"
  465. (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
  466. (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
  467. (org-test-with-temp-text "#+NAME: foo
  468. #+BEGIN_SRC emacs-lisp
  469. 'foo
  470. #+END_SRC\n"
  471. (org-babel-next-src-block 1)
  472. (should (eq 'foo (org-babel-execute-src-block)))
  473. (goto-char (point-min))
  474. (org-babel-next-src-block 1)
  475. (should (looking-at org-babel-src-block-regexp))))
  476. (ert-deftest test-ob/catches-all-references ()
  477. (org-test-with-temp-text "
  478. #+NAME: literal-example
  479. #+BEGIN_EXAMPLE
  480. A literal example
  481. on two lines
  482. #+END_EXAMPLE
  483. #+NAME: read-literal-example
  484. #+BEGIN_SRC emacs-lisp :var x=literal-example
  485. (concatenate 'string x \" for me.\")
  486. #+END_SRC"
  487. (org-babel-next-src-block 1)
  488. (should (string= (org-babel-execute-src-block)
  489. "A literal example\non two lines\n for me."))))
  490. (ert-deftest test-ob/resolve-code-blocks-before-data-blocks ()
  491. (org-test-with-temp-text "
  492. #+name: foo
  493. : bar
  494. #+name: foo
  495. #+begin_src emacs-lisp
  496. \"baz\"
  497. #+end_src
  498. #+begin_src emacs-lisp :var foo=foo
  499. foo
  500. #+end_src"
  501. (org-babel-next-src-block 2)
  502. (should (string= (org-babel-execute-src-block) "baz"))))
  503. (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
  504. (org-test-with-temp-text "
  505. #+tblname: base_plus
  506. | 1 |
  507. | 2 |
  508. #+tblname: base
  509. | 3 |
  510. | 4 |
  511. #+begin_src emacs-lisp :var x=base
  512. x
  513. #+end_src"
  514. (org-babel-next-src-block 1)
  515. (should (equal (org-babel-execute-src-block) '((3) (4))))))
  516. (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
  517. (org-test-with-temp-text "
  518. #+name: base_plus
  519. #+begin_src emacs-lisp
  520. 'bar
  521. #+end_src
  522. #+name: base
  523. #+begin_src emacs-lisp
  524. 'foo
  525. #+end_src
  526. #+begin_src emacs-lisp :var x=base
  527. x
  528. #+end_src"
  529. (org-babel-next-src-block 3)
  530. (should (equal (org-babel-execute-src-block) "foo"))))
  531. (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
  532. (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
  533. (+ a b c d)
  534. #+end_src
  535. "
  536. (should (= 10 (org-babel-execute-src-block)))))
  537. (ert-deftest test-ob/org-babel-update-intermediate ()
  538. (org-test-with-temp-text "#+name: foo
  539. #+begin_src emacs-lisp
  540. 2
  541. #+end_src
  542. #+results: foo
  543. : 4
  544. #+begin_src emacs-lisp :var it=foo
  545. (+ it 1)
  546. #+end_src"
  547. (let ((org-babel-update-intermediate nil))
  548. (goto-char (point-min))
  549. (org-babel-next-src-block 2)
  550. (should (= 3 (org-babel-execute-src-block)))
  551. (goto-char (point-min))
  552. (forward-line 6)
  553. (should (looking-at ": 4")))
  554. (let ((org-babel-update-intermediate t))
  555. (goto-char (point-min))
  556. (org-babel-next-src-block 2)
  557. (should (= 3 (org-babel-execute-src-block)))
  558. (goto-char (point-min))
  559. (forward-line 6)
  560. (should (looking-at ": 2")))))
  561. (ert-deftest test-ob/eval-header-argument ()
  562. (flet ((check-eval (eval runp)
  563. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  564. (setq foo :evald)
  565. #+end_src" eval)
  566. (let ((foo :not-run))
  567. (if runp
  568. (progn (should (org-babel-execute-src-block))
  569. (should (eq foo :evald)))
  570. (progn (should-not (org-babel-execute-src-block))
  571. (should-not (eq foo :evald))))))))
  572. (check-eval "never" nil)
  573. (check-eval "no" nil)
  574. (check-eval "never-export" t)
  575. (check-eval "no-export" t)
  576. (let ((org-babel-exp-reference-buffer (current-buffer)))
  577. (check-eval "never" nil)
  578. (check-eval "no" nil)
  579. (check-eval "never-export" nil)
  580. (check-eval "no-export" nil))))
  581. (ert-deftest test-ob/noweb-expansion-1 ()
  582. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  583. <<foo>>
  584. #+end_src
  585. #+name: foo
  586. #+begin_src sh
  587. bar
  588. #+end_src"
  589. (should (string= (org-babel-expand-noweb-references) "bar"))))
  590. (ert-deftest test-ob/noweb-expansion-2 ()
  591. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  592. <<foo>>
  593. #+end_src
  594. #+name: foo
  595. #+begin_src sh :noweb-sep \"\"
  596. bar
  597. #+end_src
  598. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  599. baz
  600. #+end_src"
  601. (should (string= (org-babel-expand-noweb-references) "barbaz"))))
  602. (ert-deftest test-ob/splitting-variable-lists-in-references ()
  603. (org-test-with-temp-text ""
  604. (should (= 1 (length (org-babel-ref-split-args
  605. "a=\"this, no work\""))))
  606. (should (= 2 (length (org-babel-ref-split-args
  607. "a=\"this, no work\", b=1"))))))
  608. (ert-deftest test-ob/org-babel-balanced-split ()
  609. (should (equal
  610. '(":a 1" "b [2 3]" "c (4 :d (5 6))")
  611. (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
  612. '((32 9) . 58)))))
  613. (ert-deftest test-ob/commented-last-block-line-no-var ()
  614. (org-test-with-temp-text-in-file "
  615. #+begin_src emacs-lisp
  616. ;;
  617. #+end_src"
  618. (org-babel-next-src-block)
  619. (org-ctrl-c-ctrl-c)
  620. (should (re-search-forward "\\#\\+results:" nil t))
  621. (forward-line)
  622. (should
  623. (string=
  624. ""
  625. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  626. (org-test-with-temp-text-in-file "
  627. #+begin_src emacs-lisp
  628. \"some text\";;
  629. #+end_src"
  630. (org-babel-next-src-block)
  631. (org-ctrl-c-ctrl-c)
  632. (should (re-search-forward "\\#\\+results:" nil t))
  633. (forward-line)
  634. (should
  635. (string=
  636. ": some text"
  637. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  638. (ert-deftest test-ob/commented-last-block-line-with-var ()
  639. (org-test-with-temp-text-in-file "
  640. #+begin_src emacs-lisp :var a=1
  641. ;;
  642. #+end_src"
  643. (org-babel-next-src-block)
  644. (org-ctrl-c-ctrl-c)
  645. (re-search-forward "\\#\\+results:" nil t)
  646. (forward-line)
  647. (should (string=
  648. ""
  649. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  650. (org-test-with-temp-text-in-file "
  651. #+begin_src emacs-lisp :var a=2
  652. 2;;
  653. #+end_src"
  654. (org-babel-next-src-block)
  655. (org-ctrl-c-ctrl-c)
  656. (re-search-forward "\\#\\+results:" nil t)
  657. (forward-line)
  658. (should (string=
  659. ": 2"
  660. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  661. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  662. "Test helper function to test `org-babel-remove-result'.
  663. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  664. and the result of execution is verified against RESULT.
  665. The block is actually executed /twice/ to ensure result
  666. replacement happens correctly."
  667. (org-test-with-temp-text
  668. buffer-text
  669. (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
  670. (should (re-search-forward "\\#\\+results:" nil t))
  671. (forward-line)
  672. (should (string= result
  673. (buffer-substring-no-properties
  674. (point-at-bol)
  675. (- (point-max) 16))))
  676. (org-babel-previous-src-block) (org-babel-remove-result)
  677. (should (string= buffer-text
  678. (buffer-substring-no-properties
  679. (point-min) (point-max))))))
  680. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  681. "Test `org-babel-remove-result' with default :results."
  682. (mapcar (lambda (language)
  683. (test-ob-verify-result-and-removed-result
  684. "\n"
  685. (concat
  686. "* org-babel-remove-result
  687. #+begin_src " language "
  688. #+end_src
  689. * next heading")))
  690. '("sh" "emacs-lisp")))
  691. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  692. "Test `org-babel-remove-result' with :results list."
  693. (test-ob-verify-result-and-removed-result
  694. "- 1
  695. - 2
  696. - 3
  697. - (quote (4 5))"
  698. "* org-babel-remove-result
  699. #+begin_src emacs-lisp :results list
  700. '(1 2 3 '(4 5))
  701. #+end_src
  702. * next heading"))
  703. (ert-deftest test-ob/org-babel-results-indented-wrap ()
  704. "Ensure that wrapped results are inserted correction when indented.
  705. If not inserted correctly then the second evaluation will fail
  706. trying to find the :END: marker."
  707. (org-test-with-temp-text
  708. "- indented
  709. #+begin_src sh :results file wrap
  710. echo test.txt
  711. #+end_src"
  712. (org-babel-next-src-block 1)
  713. (org-babel-execute-src-block)
  714. (org-babel-execute-src-block)))
  715. (ert-deftest test-ob/file-desc-header-argument ()
  716. "Test that the :file-desc header argument is used."
  717. (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  718. \"foo\"
  719. #+end_src
  720. #+begin_src emacs-lisp :results file :file-desc
  721. \"foo\"
  722. #+end_src"
  723. (org-babel-execute-src-block)
  724. (org-babel-next-src-block 1)
  725. (org-babel-execute-src-block)
  726. (goto-char (point-min))
  727. (should (search-forward "[[file:foo][bar]]" nil t))
  728. (should (search-forward "[[file:foo][foo]]" nil t))))
  729. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  730. "Test `org-babel-remove-result' with :results pp."
  731. (test-ob-verify-result-and-removed-result
  732. ": \"I /am/ working!\""
  733. "* org-babel-remove-result
  734. #+begin_src emacs-lisp :results pp
  735. \"I /am/ working!\")
  736. #+end_src
  737. * next heading"))
  738. (ert-deftest test-org-babel/inline-src_blk-preceded-punct-preceded-by-point ()
  739. (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
  740. (org-test-with-temp-text
  741. test-line
  742. (forward-char 1)
  743. (org-ctrl-c-ctrl-c)
  744. (should (re-search-forward "=\"x\"=" nil t))
  745. (forward-line))))
  746. (ert-deftest test-ob/commented-last-block-line-with-var ()
  747. (org-test-with-temp-text-in-file "
  748. #+begin_src emacs-lisp :var a=1
  749. ;;
  750. #+end_src"
  751. (org-babel-next-src-block)
  752. (org-ctrl-c-ctrl-c)
  753. (re-search-forward "\\#\\+results:" nil t)
  754. (forward-line)
  755. (should (string=
  756. ""
  757. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  758. (org-test-with-temp-text-in-file "
  759. #+begin_src emacs-lisp :var a=2
  760. 2;;
  761. #+end_src"
  762. (org-babel-next-src-block)
  763. (org-ctrl-c-ctrl-c)
  764. (re-search-forward "\\#\\+results:" nil t)
  765. (forward-line)
  766. (should (string=
  767. ": 2"
  768. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  769. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  770. "Test helper function to test `org-babel-remove-result'.
  771. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  772. and the result of execution is verified against RESULT.
  773. The block is actually executed /twice/ to ensure result
  774. replacement happens correctly."
  775. (org-test-with-temp-text
  776. buffer-text
  777. (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
  778. (should (re-search-forward "\\#\\+results:" nil t))
  779. (forward-line)
  780. (should (string= result
  781. (buffer-substring-no-properties
  782. (point-at-bol)
  783. (- (point-max) 16))))
  784. (org-babel-previous-src-block) (org-babel-remove-result)
  785. (should (string= buffer-text
  786. (buffer-substring-no-properties
  787. (point-min) (point-max))))))
  788. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  789. "Test `org-babel-remove-result' with default :results."
  790. (mapcar (lambda (language)
  791. (test-ob-verify-result-and-removed-result
  792. "\n"
  793. (concat
  794. "* org-babel-remove-result
  795. #+begin_src " language "
  796. #+end_src
  797. * next heading")))
  798. '("sh" "emacs-lisp")))
  799. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  800. "Test `org-babel-remove-result' with :results list."
  801. (test-ob-verify-result-and-removed-result
  802. "- 1
  803. - 2
  804. - 3
  805. - (quote (4 5))"
  806. "* org-babel-remove-result
  807. #+begin_src emacs-lisp :results list
  808. '(1 2 3 '(4 5))
  809. #+end_src
  810. * next heading"))
  811. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  812. "Test `org-babel-remove-result' with :results wrap."
  813. (test-ob-verify-result-and-removed-result
  814. ":RESULTS:
  815. hello there
  816. :END:"
  817. "* org-babel-remove-result
  818. #+begin_src emacs-lisp :results wrap
  819. \"hello there\"
  820. #+end_src
  821. * next heading"))
  822. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  823. "Test `org-babel-remove-result' with :results org."
  824. (test-ob-verify-result-and-removed-result
  825. "#+BEGIN_SRC org
  826. ,* heading
  827. ,** subheading
  828. content
  829. #+END_SRC"
  830. "* org-babel-remove-result
  831. #+begin_src emacs-lisp :results org
  832. \"* heading
  833. ** subheading
  834. content\"
  835. #+end_src
  836. * next heading"))
  837. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  838. "Test `org-babel-remove-result' with :results html."
  839. (test-ob-verify-result-and-removed-result
  840. "#+BEGIN_HTML
  841. <head><body></body></head>
  842. #+END_HTML"
  843. "* org-babel-remove-result
  844. #+begin_src emacs-lisp :results html
  845. \"<head><body></body></head>\"
  846. #+end_src
  847. * next heading"))
  848. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  849. "Test `org-babel-remove-result' with :results latex."
  850. (test-ob-verify-result-and-removed-result
  851. "#+BEGIN_LaTeX
  852. Line 1
  853. Line 2
  854. Line 3
  855. #+END_LaTeX"
  856. "* org-babel-remove-result
  857. #+begin_src emacs-lisp :results latex
  858. \"Line 1
  859. Line 2
  860. Line 3\"
  861. #+end_src
  862. * next heading"))
  863. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  864. "Test `org-babel-remove-result' with :results code."
  865. (test-ob-verify-result-and-removed-result
  866. "#+BEGIN_SRC emacs-lisp
  867. \"I am working!\"
  868. #+END_SRC"
  869. "* org-babel-remove-result
  870. #+begin_src emacs-lisp :results code
  871. (message \"I am working!\")
  872. #+end_src
  873. * next heading"))
  874. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  875. "Test `org-babel-remove-result' with :results pp."
  876. (test-ob-verify-result-and-removed-result
  877. ": \"I /am/ working!\""
  878. "* org-babel-remove-result
  879. #+begin_src emacs-lisp :results pp
  880. \"I /am/ working!\")
  881. #+end_src
  882. * next heading"))
  883. (ert-deftest test-ob/results-do-not-replace-code-blocks ()
  884. (org-test-with-temp-text "Block two has a space after the name.
  885. #+name: foo
  886. #+begin_src emacs-lisp
  887. 1
  888. #+end_src emacs-lisp
  889. #+name: foo
  890. #+begin_src emacs-lisp
  891. 2
  892. #+end_src
  893. #+name: foo
  894. #+begin_src emacs-lisp
  895. 3
  896. #+end_src
  897. #+RESULTS: foo
  898. : foo
  899. "
  900. (dolist (num '(1 2 3))
  901. ;; execute the block
  902. (goto-char (point-min))
  903. (org-babel-next-src-block num) (org-babel-execute-src-block)
  904. ;; check the results
  905. (goto-char (point-max))
  906. (move-beginning-of-line 0)
  907. (should (looking-at (format ": %d" num))))))
  908. (ert-deftest test-ob/blocks-with-spaces ()
  909. "Test expansion of blocks followed by blank lines."
  910. (should
  911. (equal "#+BEGIN_SRC emacs-lisp
  912. \(+ 1 2)
  913. #+END_SRC
  914. #+RESULTS:
  915. : 3\n\n\n"
  916. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
  917. \(+ 1 2)
  918. #+END_SRC\n\n\n"
  919. (org-babel-execute-src-block)
  920. (buffer-string)))))
  921. (ert-deftest test-ob/results-in-narrowed-buffer ()
  922. "Test block execution in a narrowed buffer."
  923. ;; If results don't exist, they should be inserted in visible part
  924. ;; of the buffer.
  925. (should
  926. (equal
  927. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
  928. (org-test-with-temp-text
  929. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  930. (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
  931. (org-babel-execute-src-block)
  932. (org-trim (buffer-string)))))
  933. (should
  934. (equal
  935. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
  936. (org-test-with-temp-text
  937. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  938. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  939. (org-babel-execute-src-block)
  940. (org-trim (buffer-string)))))
  941. ;; Results in visible part of buffer, should be updated here.
  942. (should
  943. (equal
  944. "#+NAME: test
  945. #+BEGIN_SRC emacs-lisp
  946. \(+ 1 2)
  947. #+END_SRC
  948. #+RESULTS: test
  949. : 3"
  950. (org-test-with-temp-text
  951. "#+NAME: test
  952. #+BEGIN_SRC emacs-lisp
  953. \(+ 1 2)
  954. #+END_SRC
  955. #+RESULTS: test
  956. : 4
  957. Paragraph"
  958. (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
  959. (org-babel-execute-src-block)
  960. (org-trim (buffer-string)))))
  961. ;; Results in invisible part of buffer, should be updated there.
  962. (org-test-with-temp-text
  963. "#+NAME: test
  964. #+BEGIN_SRC emacs-lisp
  965. \(+ 1 2)
  966. #+END_SRC
  967. #+RESULTS: test
  968. : 4
  969. Paragraph"
  970. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  971. (org-babel-execute-src-block)
  972. (should-not (re-search-forward "^#\\+RESULTS:" nil t))
  973. (widen)
  974. (should (should (re-search-forward "^: 3" nil t)))))
  975. (ert-deftest test-ob/specific-colnames ()
  976. "Test passing specific column names."
  977. (should
  978. (equal "#+name: input-table
  979. | id | var1 |
  980. |----+------|
  981. | 1 | bar |
  982. | 2 | baz |
  983. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  984. echo \"$data\"
  985. #+end_src
  986. #+RESULTS:
  987. | Rev | Author |
  988. |-----+--------|
  989. | 1 | bar |
  990. | 2 | baz |
  991. "
  992. (org-test-with-temp-text
  993. "#+name: input-table
  994. | id | var1 |
  995. |----+------|
  996. | 1 | bar |
  997. | 2 | baz |
  998. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  999. echo \"$data\"
  1000. #+end_src
  1001. "
  1002. ;; we should find a code block
  1003. (should (re-search-forward org-babel-src-block-regexp nil t))
  1004. (goto-char (match-beginning 0))
  1005. ;; now that we've located the code block, it may be evaluated
  1006. (org-babel-execute-src-block)
  1007. (buffer-string)))))
  1008. (ert-deftest test-ob/location-of-header-arg-eval ()
  1009. "Test location of header argument evaluation."
  1010. (org-test-with-temp-text "
  1011. #+name: top-block
  1012. #+begin_src emacs-lisp :var pt=(point)
  1013. pt
  1014. #+end_src
  1015. #+name: bottom-block
  1016. #+begin_src emacs-lisp :var pt=top-block()
  1017. pt
  1018. #+end_src
  1019. "
  1020. ;; the value of the second block should be greater than the first
  1021. (should
  1022. (< (progn (re-search-forward org-babel-src-block-regexp nil t)
  1023. (goto-char (match-beginning 0))
  1024. (prog1 (save-match-data (org-babel-execute-src-block))
  1025. (goto-char (match-end 0))))
  1026. (progn (re-search-forward org-babel-src-block-regexp nil t)
  1027. (goto-char (match-beginning 0))
  1028. (org-babel-execute-src-block))))))
  1029. (ert-deftest test-ob/preserve-results-indentation ()
  1030. "Preserve indentation when executing a src block."
  1031. (should
  1032. (equal '(2 2)
  1033. (org-test-with-temp-text
  1034. " #+begin_src emacs-lisp\n (+ 1 1)\n #+end_src"
  1035. (org-babel-execute-src-block)
  1036. (buffer-string)
  1037. (let ((case-fold-search t)) (search-forward "#+results:"))
  1038. ;; Check if both #+RESULTS: keyword and actual results are
  1039. ;; indented by 2 columns.
  1040. (list (org-get-indentation)
  1041. (progn (forward-line) (org-get-indentation)))))))
  1042. (ert-deftest test-ob/safe-header-args ()
  1043. "Detect safe and unsafe header args."
  1044. (let ((safe-args '((:cache . "foo")
  1045. (:results . "output")
  1046. (:eval . "never")
  1047. (:eval . "query")))
  1048. (unsafe-args '((:eval . "yes")
  1049. (:results . "output file")
  1050. (:foo . "bar")))
  1051. (malformed-args '((bar . "foo")
  1052. ("foo" . "bar")
  1053. :foo))
  1054. (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
  1055. (dolist (arg safe-args)
  1056. (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
  1057. (dolist (arg unsafe-args)
  1058. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1059. (dolist (arg malformed-args)
  1060. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1061. (should (not (funcall safe-p (append safe-args unsafe-args))))))
  1062. (ert-deftest test-ob/noweb-expansions-in-cache ()
  1063. "Ensure that noweb expansions are expanded before caching."
  1064. (let ((noweb-expansions-in-cache-var 0))
  1065. (org-test-with-temp-text "
  1066. #+name: foo
  1067. #+begin_src emacs-lisp
  1068. \"I said\"
  1069. #+end_src
  1070. #+name: bar
  1071. #+begin_src emacs-lisp :noweb yes :cache yes
  1072. (setq noweb-expansions-in-cache-var
  1073. (+ 1 noweb-expansions-in-cache-var))
  1074. (concat <<foo>> \" check noweb expansions\")
  1075. #+end_src
  1076. "
  1077. ;; run the second block to create the cache
  1078. (goto-char (point-min))
  1079. (re-search-forward (regexp-quote "#+name: bar"))
  1080. (should (string= "I said check noweb expansions"
  1081. (org-babel-execute-src-block)))
  1082. (should (= noweb-expansions-in-cache-var 1))
  1083. ;; change the value of the first block
  1084. (goto-char (point-min))
  1085. (re-search-forward (regexp-quote "said"))
  1086. (goto-char (match-beginning 0))
  1087. (insert "haven't ")
  1088. (re-search-forward (regexp-quote "#+name: bar"))
  1089. (should (string= "I haven't said check noweb expansions"
  1090. (org-babel-execute-src-block)))
  1091. (should (= noweb-expansions-in-cache-var 2)))))
  1092. (provide 'test-ob)
  1093. ;;; test-ob ends here