test-ob.el 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. ;;; test-ob.el --- tests for ob.el
  2. ;; Copyright (c) 2010-2015 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/indented-cached-org-bracket-link ()
  17. "When the result of a source block is a cached indented link it
  18. should still return the link."
  19. (should
  20. (let ((default-directory temporary-file-directory))
  21. (org-test-with-temp-text
  22. "
  23. * Test
  24. #+<point>BEGIN_SRC emacs-lisp :file test.txt :cache yes
  25. (message \"test\")
  26. #+END_SRC"
  27. ;; Execute twice as the first time creates the cache.
  28. (org-babel-execute-src-block)
  29. (string= (concat default-directory "test.txt")
  30. (org-babel-execute-src-block))))))
  31. (ert-deftest test-org-babel/multi-line-header-regexp ()
  32. (should(equal "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
  33. org-babel-multi-line-header-regexp))
  34. ;;TODO can be optimised - and what about blah4 blah5 blah6?
  35. (should (string-match
  36. org-babel-multi-line-header-regexp
  37. " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
  38. (should
  39. (equal
  40. "blah1 blah2 blah3 \t"
  41. (match-string
  42. 1
  43. " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
  44. ;;TODO Check - should this fail?
  45. (should
  46. (not (org-test-string-exact-match
  47. org-babel-multi-line-header-regexp
  48. " \t #+headers : blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))))
  49. (ert-deftest test-org-babel/src-block-regexp ()
  50. (let ((test-block
  51. (concat
  52. "#+begin_src language -n-r-a-b -c :argument-1 yes :argument-2 no\n"
  53. "echo this is a test\n"
  54. "echo Currently in ' $PWD\n"
  55. "#+end_src"))
  56. (language "language")
  57. (flags "-n-r-a-b -c ")
  58. (arguments ":argument-1 yes :argument-2 no")
  59. (body "echo this is a test\necho Currently in ' $PWD\n"))
  60. (should (string-match org-babel-src-block-regexp test-block))
  61. (should (string-match org-babel-src-block-regexp (upcase test-block)))
  62. (should (equal language (match-string 2 test-block)))
  63. ;;TODO Consider refactoring
  64. (should (equal flags (match-string 3 test-block)))
  65. (should (equal arguments (match-string 4 test-block)))
  66. (should (equal body (match-string 5 test-block)))
  67. ;;no switches
  68. (should (org-test-string-exact-match
  69. org-babel-src-block-regexp
  70. (replace-regexp-in-string flags "" test-block)))
  71. ;;no header arguments
  72. (should (org-test-string-exact-match
  73. org-babel-src-block-regexp
  74. (replace-regexp-in-string arguments "" test-block)))
  75. ;; should be valid with no body
  76. (should (org-test-string-exact-match
  77. org-babel-src-block-regexp
  78. (replace-regexp-in-string body "" test-block)))))
  79. (ert-deftest test-org-babel/get-header ()
  80. (should (not (org-babel-get-header
  81. org-babel-default-header-args :doesnt-exist)))
  82. (should(equal '((:session . "none"))
  83. (org-babel-get-header
  84. org-babel-default-header-args :session)))
  85. (should(equal '((:session . "none"))
  86. (org-babel-get-header
  87. org-babel-default-header-args :session nil)))
  88. (should (not (org-babel-get-header
  89. org-babel-default-header-args :SESSION)))
  90. (should (equal '((:tangle . "no"))
  91. (org-babel-get-header
  92. org-babel-default-header-args :tangle)))
  93. ;; with OTHERS option
  94. (should (equal org-babel-default-header-args
  95. (org-babel-get-header
  96. org-babel-default-header-args :doesnt-exist 'others)))
  97. (should (equal org-babel-default-header-args
  98. (org-babel-get-header
  99. org-babel-default-header-args nil 'others)))
  100. (should (null
  101. (assoc :noweb
  102. (org-babel-get-header
  103. org-babel-default-header-args :noweb 'others)))))
  104. (ert-deftest test-org-babel/default-inline-header-args ()
  105. (should(equal
  106. '((:session . "none")
  107. (:results . "replace")
  108. (:exports . "results")
  109. (:hlines . "yes"))
  110. org-babel-default-inline-header-args)))
  111. (ert-deftest ob-test/org-babel-combine-header-arg-lists ()
  112. (let ((results (org-babel-combine-header-arg-lists
  113. '((foo . :any)
  114. (bar)
  115. (baz . ((foo bar) (baz)))
  116. (qux . ((foo bar baz qux)))
  117. (quux . ((foo bar))))
  118. '((bar)
  119. (baz . ((baz)))
  120. (quux . :any)))))
  121. (dolist (pair '((foo . :any)
  122. (bar)
  123. (baz . ((baz)))
  124. (quux . :any)
  125. (qux . ((foo bar baz qux)))))
  126. (should (equal (cdr pair)
  127. (cdr (assoc (car pair) results)))))))
  128. ;;; ob-get-src-block-info
  129. (ert-deftest test-org-babel/get-src-block-info-language ()
  130. (org-test-at-marker nil org-test-file-ob-anchor
  131. (let ((info (org-babel-get-src-block-info)))
  132. (should (string= "emacs-lisp" (nth 0 info))))))
  133. (ert-deftest test-org-babel/get-src-block-info-body ()
  134. (org-test-at-marker nil org-test-file-ob-anchor
  135. (let ((info (org-babel-get-src-block-info)))
  136. (should (string-match (regexp-quote org-test-file-ob-anchor)
  137. (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))))))))
  142. (ert-deftest test-org-babel/elisp-in-header-arguments ()
  143. "Test execution of elisp forms in header arguments."
  144. (org-test-with-temp-text-in-file "
  145. * elisp forms in header arguments
  146. :PROPERTIES:
  147. :var: prop = (* 7 6)
  148. :END:
  149. #+begin_src emacs-lisp
  150. prop
  151. #+end_src"
  152. (goto-char (point-min))
  153. (org-babel-next-src-block)
  154. (let ((info (org-babel-get-src-block-info)))
  155. (should (= 42 (org-babel-execute-src-block))))))
  156. (ert-deftest test-org-babel/simple-named-code-block ()
  157. "Test that simple named code blocks can be evaluated."
  158. (org-test-with-temp-text-in-file "
  159. #+name: i-have-a-name
  160. #+begin_src emacs-lisp
  161. 42
  162. #+end_src"
  163. (org-babel-next-src-block 1)
  164. (should (= 42 (org-babel-execute-src-block)))))
  165. (ert-deftest test-org-babel/simple-variable-resolution ()
  166. "Test that simple variable resolution is working."
  167. (org-test-with-temp-text-in-file "
  168. #+name: four
  169. #+begin_src emacs-lisp
  170. (list 1 2 3 4)
  171. #+end_src
  172. #+begin_src emacs-lisp :var four=four
  173. (length four)
  174. #+end_src"
  175. (org-babel-next-src-block 2)
  176. (should (= 4 (org-babel-execute-src-block)))
  177. (forward-line 5)
  178. (should (string= ": 4" (buffer-substring
  179. (point-at-bol)
  180. (point-at-eol))))))
  181. (ert-deftest test-org-babel/multi-line-header-arguments ()
  182. "Test that multi-line header arguments and can be read."
  183. (org-test-with-temp-text-in-file "
  184. #+headers: :var letters='(a b c d e f g)
  185. #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
  186. (require 'cl)
  187. (defalias 'my-map (if (org-version-check \"24.2.50\" \"cl\" :predicate)
  188. 'cl-map
  189. 'map))
  190. (my-map 'list #'list numbers letters)
  191. #+end_src"
  192. (org-babel-next-src-block)
  193. (let ((results (org-babel-execute-src-block)))
  194. (should(equal 'a (cadr (assoc 1 results))))
  195. (should(equal 'd (cadr (assoc 4 results)))))))
  196. (ert-deftest test-org-babel/parse-header-args ()
  197. (org-test-with-temp-text-in-file "
  198. #+begin_src example-lang :session :results output :var num=9
  199. the body
  200. #+end_src"
  201. (org-babel-next-src-block)
  202. (let* ((info (org-babel-get-src-block-info))
  203. (params (nth 2 info)))
  204. (message "%S" params)
  205. (should (equal "example-lang" (nth 0 info)))
  206. (should (string= "the body" (org-babel-trim (nth 1 info))))
  207. (should-not (member '(:session\ \ \ \ ) params))
  208. (should (equal '(:session) (assoc :session params)))
  209. (should (equal '(:result-type . output) (assoc :result-type params)))
  210. (should (equal '(num . 9) (cdr (assoc :var params)))))))
  211. (ert-deftest test-org-babel/parse-header-args2 ()
  212. (org-test-with-temp-text-in-file "
  213. * resolving sub-trees as references
  214. #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
  215. (length text)
  216. #+end_src
  217. #+begin_src org :noweb yes
  218. <<simple-subtree>>
  219. <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
  220. #+end_src
  221. ** simple subtree with custom ID
  222. :PROPERTIES:
  223. :CUSTOM_ID: simple-subtree
  224. :END:
  225. this is simple"
  226. (should (string-match (regexp-quote "this is simple")
  227. (org-babel-ref-resolve "simple-subtree")))
  228. (org-babel-next-src-block)
  229. (should (= 14 (org-babel-execute-src-block)))))
  230. (ert-deftest test-org-babel/inline-src-blocks ()
  231. (should
  232. (= 1
  233. (org-test-with-temp-text
  234. "In the middle <point>src_emacs-lisp{(+ 0 1)} of a line"
  235. (org-babel-execute-src-block))))
  236. (should
  237. (= 2
  238. (org-test-with-temp-text
  239. "One at the end of a line: <point>src_emacs-lisp{(+ 1 1)}"
  240. (org-babel-execute-src-block))))
  241. (should
  242. (= 3
  243. (org-test-with-temp-text
  244. "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
  245. (org-babel-execute-src-block))))
  246. (should
  247. (= 4
  248. (org-test-with-temp-text
  249. "In the middle <point>src_emacs-lisp[:results silent\
  250. :exports code]{(+ 3 1)} of a line"
  251. (org-babel-execute-src-block))))
  252. (should
  253. (= 5
  254. (org-test-with-temp-text
  255. "One at the end of a line: <point>src_emacs-lisp[:results silent\
  256. :exports code]{(+ 4 1)}"
  257. (org-babel-execute-src-block))))
  258. (should
  259. (= 6
  260. (org-test-with-temp-text
  261. "src_emacs-lisp[:results silent :exports code]{(+ 5 1)}\
  262. at the beginning of a line."
  263. (org-babel-execute-src-block))))
  264. (should
  265. (= 7
  266. (org-test-with-temp-text
  267. "One also evaluated: <point>src_emacs-lisp[:exports both\
  268. :results silent]{(+ 6 1)}"
  269. (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
  273. 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")))
  295. (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-1 ()
  296. (let ((test-line "src_sh{echo 1}")
  297. (org-babel-inline-result-wrap "=%s="))
  298. ;; src_ at bol line 1...
  299. (org-test-with-temp-text
  300. test-line
  301. (goto-char (point-min)) (org-babel-execute-maybe)
  302. (should (string=
  303. (concat test-line " {{{results(=1=)}}}")
  304. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  305. (forward-char) (org-babel-execute-maybe)
  306. (should (string=
  307. (concat test-line " {{{results(=1=)}}}")
  308. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  309. (re-search-forward "{{{")
  310. ;;(should-error (org-ctrl-c-ctrl-c))
  311. (backward-char 4) ;; last char of block body
  312. (org-babel-execute-maybe)
  313. (should (string=
  314. (concat test-line " {{{results(=1=)}}}")
  315. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  316. ;; src_ follows space line 1...
  317. (let ((test-line " src_emacs-lisp{ 1 }"))
  318. (org-test-with-temp-text
  319. test-line
  320. (should-error (org-ctrl-c-ctrl-c))
  321. (forward-char) (org-babel-execute-maybe)
  322. (should (string=
  323. (concat test-line " {{{results(=1=)}}}")
  324. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  325. (re-search-forward "{ 1 ") (org-babel-execute-maybe)
  326. (should (string=
  327. (concat test-line " {{{results(=1=)}}}")
  328. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  329. (forward-char 6)
  330. (should-error (org-ctrl-c-ctrl-c))
  331. ))))
  332. (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-2 ()
  333. ;; src_ at bol line 2...
  334. (let ((test-line " src_emacs-lisp{ \"x\" }")
  335. (org-babel-inline-result-wrap "=%s="))
  336. (org-test-with-temp-text
  337. (concat "\n" test-line)
  338. (should-error (org-ctrl-c-ctrl-c))
  339. (goto-char (point-min))
  340. (should-error (org-ctrl-c-ctrl-c))
  341. (forward-line)
  342. (should-error (org-ctrl-c-ctrl-c))
  343. (forward-char) (org-babel-execute-maybe)
  344. (should (string=
  345. (concat test-line " {{{results(=x=)}}}")
  346. (buffer-substring-no-properties
  347. (point-at-bol) (point-at-eol))))))
  348. (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
  349. (org-babel-inline-result-wrap "=%s="))
  350. (org-test-with-temp-text
  351. test-line
  352. (goto-char (point-max))
  353. (insert (concat "\n" test-line " end"))
  354. (re-search-backward "src") (org-babel-execute-maybe)
  355. (should (string=
  356. (concat test-line " {{{results(=y=)}}} end")
  357. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  358. (re-search-forward "\" ") (org-babel-execute-maybe)
  359. (should (string=
  360. (concat test-line " {{{results(=y=)}}} end")
  361. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  362. (forward-char 3)
  363. (should-error (org-ctrl-c-ctrl-c)))))
  364. (ert-deftest test-org-babel/inline-src_blk-manual-results-replace ()
  365. (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
  366. (org-babel-inline-result-wrap "=%s="))
  367. (org-test-with-temp-text
  368. (concat "\n" test-line)
  369. (should-error (org-ctrl-c-ctrl-c))
  370. (goto-char (point-max))
  371. (org-babel-execute-maybe)
  372. (beginning-of-line)
  373. (should-error (org-ctrl-c-ctrl-c))
  374. (forward-char) (org-babel-execute-maybe)
  375. (should (string=
  376. (concat test-line " {{{results(=x=)}}}")
  377. (buffer-substring-no-properties
  378. (point-at-bol) (point-at-eol))))))
  379. (let ((test-line (concat " Some text prior to block "
  380. "src_emacs-lisp[:results replace]{ \"y\" }"))
  381. (org-babel-inline-result-wrap "=%s="))
  382. (org-test-with-temp-text test-line
  383. (goto-char (point-max))
  384. (insert (concat "\n" test-line " end"))
  385. (re-search-backward "src") (org-babel-execute-maybe)
  386. (should (string=
  387. (concat test-line " {{{results(=y=)}}} end")
  388. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  389. (re-search-forward "\" ") (org-babel-execute-maybe)
  390. (should (string=
  391. (concat test-line " {{{results(=y=)}}} end")
  392. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  393. (forward-char 3)
  394. (should-error (org-ctrl-c-ctrl-c)))))
  395. (ert-deftest test-org-babel/inline-src_blk-results-silent ()
  396. (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
  397. (org-test-with-temp-text test-line
  398. (org-babel-execute-maybe)
  399. (should (string= test-line
  400. (buffer-substring-no-properties
  401. (point-at-bol) (point-at-eol))))))
  402. (let ((test-line (concat " Some text prior to block src_emacs-lisp"
  403. "[ :results silent ]{ \"y\" }")))
  404. (org-test-with-temp-text
  405. test-line
  406. (goto-char (point-max))
  407. (insert (concat "\n" test-line " end"))
  408. (re-search-backward "src_") (org-babel-execute-maybe)
  409. (should (string= (concat test-line " end")
  410. (buffer-substring-no-properties
  411. (point-at-bol) (point-at-eol))))
  412. (re-search-forward "\" ") (org-babel-execute-maybe)
  413. (should (string= (concat test-line " end")
  414. (buffer-substring-no-properties
  415. (point-at-bol) (point-at-eol))))
  416. (forward-char 2)
  417. (should-error (org-ctrl-c-ctrl-c)))))
  418. (ert-deftest test-org-babel/inline-src_blk-results-raw ()
  419. (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
  420. (org-test-with-temp-text test-line
  421. (org-babel-execute-maybe)
  422. (should (string= (concat test-line " x")
  423. (buffer-string)))))
  424. (let ((test-line (concat " Some text prior to block "
  425. "src_emacs-lisp[ :results raw ]{ \"the\" }")))
  426. (org-test-with-temp-text (concat test-line " end")
  427. (re-search-forward "src_") (org-babel-execute-maybe)
  428. (should (string= (concat test-line " the end")
  429. (buffer-substring-no-properties
  430. (point-at-bol) (point-at-eol))))
  431. (re-search-forward "\" ") (org-babel-execute-maybe)
  432. (should (string= (concat test-line " the the end")
  433. (buffer-substring-no-properties
  434. (point-at-bol) (point-at-eol))))
  435. (forward-char 2)
  436. (should-error (org-ctrl-c-ctrl-c)))))
  437. (ert-deftest test-org-babel/inline-src_blk-results-file ()
  438. (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
  439. (org-test-with-temp-text
  440. test-line
  441. (org-babel-execute-maybe)
  442. (should (string= (concat test-line " {{{results([[file:~/test-file]])}}}")
  443. (buffer-substring-no-properties
  444. (point-min) (point-max)))))))
  445. (ert-deftest test-org-babel/inline-src_blk-results-scalar ()
  446. (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
  447. (org-babel-inline-result-wrap "=%s="))
  448. (org-test-with-temp-text
  449. test-line
  450. (org-babel-execute-maybe)
  451. (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
  452. (buffer-substring-no-properties
  453. (point-min) (point-max)))))))
  454. (ert-deftest test-org-babel/inline-src_blk-results-verbatim ()
  455. (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  456. (org-babel-inline-result-wrap "=%s="))
  457. (org-test-with-temp-text
  458. test-line
  459. (org-babel-execute-maybe)
  460. (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
  461. (buffer-substring-no-properties
  462. (point-min) (point-max)))))))
  463. (ert-deftest test-org-babel/combining-scalar-and-raw-result-types ()
  464. (org-test-with-temp-text-in-file "
  465. #+begin_src sh :results scalar
  466. echo \"[[file:./cv.cls]]\"
  467. #+end_src
  468. #+name:
  469. : [[file:./cv.cls]]
  470. #+begin_src sh :results raw scalar
  471. echo \"[[file:./cv.cls]]\"
  472. #+end_src
  473. "
  474. (flet ((next-result ()
  475. (org-babel-next-src-block)
  476. (org-babel-execute-src-block)
  477. (goto-char (org-babel-where-is-src-block-result))
  478. (forward-line 1)))
  479. (goto-char (point-min))
  480. (next-result)
  481. (should (org-babel-in-example-or-verbatim))
  482. (next-result)
  483. (should (not (org-babel-in-example-or-verbatim))))))
  484. (ert-deftest test-org-babel/no-defaut-value-for-var ()
  485. "Test that the absence of a default value for a variable DOES THROW
  486. a proper error."
  487. (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
  488. (org-babel-next-src-block)
  489. (let ((err
  490. (should-error (org-babel-execute-src-block) :type 'error)))
  491. (should
  492. (equal
  493. '(error
  494. "Variable \"x\" must be assigned a default value")
  495. err)))))
  496. (ert-deftest test-org-babel/just-one-results-block ()
  497. "Test that evaluating two times the same code block does not result in a
  498. duplicate results block."
  499. (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
  500. (org-babel-execute-src-block)
  501. (org-babel-execute-src-block) ; second code block execution
  502. (should (search-forward "Hello")) ; the string inside the source code block
  503. (should (search-forward "Hello")) ; the same string in the results block
  504. (should-error (search-forward "Hello"))))
  505. (ert-deftest test-org-babel/nested-code-block ()
  506. "Test nested code blocks inside code blocks don't cause problems."
  507. (should
  508. (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
  509. (org-test-with-temp-text "#+begin_src org :results silent
  510. ,#+begin_src emacs-lisp
  511. , 'foo
  512. ,#+end_src
  513. #+end_src"
  514. (let ((org-edit-src-content-indentation 2)
  515. (org-src-preserve-indentation nil))
  516. (org-babel-execute-src-block))))))
  517. (ert-deftest test-org-babel/partial-nested-code-block ()
  518. "Test nested code blocks inside code blocks don't cause problems."
  519. (org-test-with-temp-text "#+begin_src org :results silent
  520. ,#+begin_src emacs-lisp
  521. #+end_src"
  522. (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
  523. (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
  524. (org-test-with-temp-text "#+NAME: foo
  525. #+BEGIN_SRC emacs-lisp
  526. 'foo
  527. #+END_SRC\n"
  528. (org-babel-next-src-block 1)
  529. (should (eq 'foo (org-babel-execute-src-block)))
  530. (goto-char (point-min))
  531. (org-babel-next-src-block 1)
  532. (should (looking-at org-babel-src-block-regexp))))
  533. (ert-deftest test-ob/catches-all-references ()
  534. (org-test-with-temp-text "
  535. #+NAME: literal-example
  536. #+BEGIN_EXAMPLE
  537. A literal example
  538. on two lines
  539. #+END_EXAMPLE
  540. #+NAME: read-literal-example
  541. #+BEGIN_SRC emacs-lisp :var x=literal-example
  542. (concatenate 'string x \" for me.\")
  543. #+END_SRC"
  544. (org-babel-next-src-block 1)
  545. (should (string= (org-babel-execute-src-block)
  546. "A literal example\non two lines\n for me."))))
  547. (ert-deftest test-ob/ignore-reference-in-commented-headings ()
  548. (should
  549. (= 2
  550. (org-test-with-temp-text
  551. "
  552. * COMMENT H1
  553. #+NAME: n
  554. : 1
  555. * H2
  556. #+NAME: n
  557. : 2
  558. * Code
  559. <point>#+BEGIN_SRC emacs-lisp :var x=n
  560. x
  561. #+END_SRC"
  562. (org-babel-execute-src-block)))))
  563. (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
  564. (org-test-with-temp-text "
  565. #+tblname: base_plus
  566. | 1 |
  567. | 2 |
  568. #+tblname: base
  569. | 3 |
  570. | 4 |
  571. #+begin_src emacs-lisp :var x=base
  572. x
  573. #+end_src"
  574. (org-babel-next-src-block 1)
  575. (should (equal (org-babel-execute-src-block) '((3) (4))))))
  576. (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
  577. (org-test-with-temp-text "
  578. #+name: base_plus
  579. #+begin_src emacs-lisp
  580. 'bar
  581. #+end_src
  582. #+name: base
  583. #+begin_src emacs-lisp
  584. 'foo
  585. #+end_src
  586. #+begin_src emacs-lisp :var x=base
  587. x
  588. #+end_src"
  589. (org-babel-next-src-block 3)
  590. (should (equal (org-babel-execute-src-block) "foo"))))
  591. (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
  592. (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
  593. (+ a b c d)
  594. #+end_src
  595. "
  596. (should (= 10 (org-babel-execute-src-block)))))
  597. (ert-deftest test-ob/org-babel-update-intermediate ()
  598. (org-test-with-temp-text "#+name: foo
  599. #+begin_src emacs-lisp
  600. 2
  601. #+end_src
  602. #+results: foo
  603. : 4
  604. #+begin_src emacs-lisp :var it=foo
  605. (+ it 1)
  606. #+end_src"
  607. (let ((org-babel-update-intermediate nil))
  608. (goto-char (point-min))
  609. (org-babel-next-src-block 2)
  610. (should (= 3 (org-babel-execute-src-block)))
  611. (goto-char (point-min))
  612. (forward-line 6)
  613. (should (looking-at ": 4")))
  614. (let ((org-babel-update-intermediate t))
  615. (goto-char (point-min))
  616. (org-babel-next-src-block 2)
  617. (should (= 3 (org-babel-execute-src-block)))
  618. (goto-char (point-min))
  619. (forward-line 6)
  620. (should (looking-at ": 2")))))
  621. (ert-deftest test-ob/eval-header-argument ()
  622. (flet ((check-eval (eval runp)
  623. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  624. (setq foo :evald)
  625. #+end_src" eval)
  626. (let ((foo :not-run))
  627. (if runp
  628. (progn (should (org-babel-execute-src-block))
  629. (should (eq foo :evald)))
  630. (progn (should-not (org-babel-execute-src-block))
  631. (should-not (eq foo :evald))))))))
  632. (check-eval "never" nil)
  633. (check-eval "no" nil)
  634. (check-eval "never-export" t)
  635. (check-eval "no-export" t)
  636. (let ((org-babel-exp-reference-buffer (current-buffer)))
  637. (check-eval "never" nil)
  638. (check-eval "no" nil)
  639. (check-eval "never-export" nil)
  640. (check-eval "no-export" nil))))
  641. (ert-deftest test-ob/noweb-expansion-1 ()
  642. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  643. <<foo>>
  644. #+end_src
  645. #+name: foo
  646. #+begin_src sh
  647. bar
  648. #+end_src"
  649. (should (string= (org-babel-expand-noweb-references) "bar"))))
  650. (ert-deftest test-ob/noweb-expansion-2 ()
  651. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  652. <<foo>>
  653. #+end_src
  654. #+name: foo
  655. #+begin_src sh :noweb-sep \"\"
  656. bar
  657. #+end_src
  658. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  659. baz
  660. #+end_src"
  661. (should (string= (org-babel-expand-noweb-references) "barbaz"))))
  662. (ert-deftest test-ob/splitting-variable-lists-in-references ()
  663. (org-test-with-temp-text ""
  664. (should (= 1 (length (org-babel-ref-split-args
  665. "a=\"this, no work\""))))
  666. (should (= 2 (length (org-babel-ref-split-args
  667. "a=\"this, no work\", b=1"))))))
  668. (ert-deftest test-ob/org-babel-balanced-split ()
  669. (should (equal
  670. '(":a 1" "b [2 3]" "c (4 :d (5 6))")
  671. (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
  672. '((32 9) . 58)))))
  673. (ert-deftest test-ob/commented-last-block-line-no-var ()
  674. (org-test-with-temp-text-in-file "
  675. #+begin_src emacs-lisp
  676. ;;
  677. #+end_src"
  678. (org-babel-next-src-block)
  679. (org-babel-execute-maybe)
  680. (should (re-search-forward "\\#\\+results:" nil t))
  681. (forward-line)
  682. (should
  683. (string=
  684. ""
  685. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  686. (org-test-with-temp-text-in-file "
  687. #+begin_src emacs-lisp
  688. \"some text\";;
  689. #+end_src"
  690. (org-babel-next-src-block)
  691. (org-babel-execute-maybe)
  692. (should (re-search-forward "\\#\\+results:" nil t))
  693. (forward-line)
  694. (should
  695. (string=
  696. ": some text"
  697. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  698. (ert-deftest test-ob/commented-last-block-line-with-var ()
  699. (org-test-with-temp-text-in-file "
  700. #+begin_src emacs-lisp :var a=1
  701. ;;
  702. #+end_src"
  703. (org-babel-next-src-block)
  704. (org-babel-execute-maybe)
  705. (re-search-forward "\\#\\+results:" nil t)
  706. (forward-line)
  707. (should (string=
  708. ""
  709. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  710. (org-test-with-temp-text-in-file "
  711. #+begin_src emacs-lisp :var a=2
  712. 2;;
  713. #+end_src"
  714. (org-babel-next-src-block)
  715. (org-babel-execute-maybe)
  716. (re-search-forward "\\#\\+results:" nil t)
  717. (forward-line)
  718. (should (string=
  719. ": 2"
  720. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  721. (ert-deftest test-ob/org-babel-insert-result--improper-lists ()
  722. "Test `org-babel-insert-result' with improper lists."
  723. ;; Do not error when output is an improper list.
  724. (should
  725. (org-test-with-temp-text
  726. "
  727. <point>#+BEGIN_SRC emacs-lisp
  728. '((1 . nil) (2 . 3))
  729. #+END_SRC
  730. "
  731. (org-babel-execute-maybe) t)))
  732. (ert-deftest test-ob/remove-inline-result ()
  733. "Test `org-babel-remove-inline-result' honors whitespace."
  734. (let*
  735. ((inline-sb "src_emacs-lisp{(+ 1 2)}")
  736. (inline-res " {{{results(=3=)}}}")
  737. (inline-sb-dot (concat inline-sb "."))
  738. (inline-sb-res-dot (concat inline-sb inline-res ".")))
  739. (org-test-with-temp-text
  740. ;; Insert inline_src_block followed by dot.
  741. inline-sb-dot
  742. ;; Insert result before dot.
  743. (org-babel-execute-maybe)
  744. (should (string= inline-sb-res-dot
  745. (buffer-substring-no-properties
  746. (point-at-bol) (point-at-eol))))
  747. ;; Delete whitespace and result.
  748. (org-babel-remove-inline-result)
  749. (should (string= inline-sb-dot
  750. (buffer-substring-no-properties
  751. (point-at-bol) (point-at-eol))))
  752. ;; Add whitespace and result before dot.
  753. (search-forward inline-sb)
  754. (insert " " inline-res)
  755. (goto-char (point-at-bol))
  756. ;; Remove whitespace and result.
  757. (org-babel-remove-inline-result)
  758. (should (string= inline-sb-dot
  759. (buffer-substring-no-properties
  760. (point-at-bol) (point-at-eol))))
  761. ;; Add whitespace before dot.
  762. (search-forward inline-sb)
  763. (insert " ")
  764. (goto-char (point-at-bol))
  765. ;; Add result before whitespace.
  766. (org-babel-execute-maybe)
  767. ;; Remove result - leave trailing whitespace and dot.
  768. (org-babel-remove-inline-result)
  769. (should (string= (concat inline-sb " .")
  770. (buffer-substring-no-properties
  771. (point-at-bol) (point-at-eol)))))))
  772. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  773. "Test helper function to test `org-babel-remove-result'.
  774. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  775. and the result of execution is verified against RESULT.
  776. The block is actually executed /twice/ to ensure result
  777. replacement happens correctly."
  778. (org-test-with-temp-text
  779. buffer-text
  780. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  781. (should (re-search-forward "\\#\\+results:" nil t))
  782. (forward-line)
  783. (should (string= result
  784. (buffer-substring-no-properties
  785. (point-at-bol)
  786. (- (point-max) 16))))
  787. (org-babel-previous-src-block) (org-babel-remove-result)
  788. (should (string= buffer-text
  789. (buffer-substring-no-properties
  790. (point-min) (point-max))))))
  791. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  792. "Test `org-babel-remove-result' with default :results."
  793. (mapcar (lambda (language)
  794. (test-ob-verify-result-and-removed-result
  795. "\n"
  796. (concat
  797. "* org-babel-remove-result
  798. #+begin_src " language "
  799. #+end_src
  800. * next heading")))
  801. '("sh" "emacs-lisp")))
  802. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  803. "Test `org-babel-remove-result' with :results list."
  804. (test-ob-verify-result-and-removed-result
  805. "- 1
  806. - 2
  807. - 3
  808. - (quote (4 5))"
  809. "* org-babel-remove-result
  810. #+begin_src emacs-lisp :results list
  811. '(1 2 3 '(4 5))
  812. #+end_src
  813. * next heading"))
  814. (ert-deftest test-ob/org-babel-results-indented-wrap ()
  815. "Ensure that wrapped results are inserted correction when indented.
  816. If not inserted correctly then the second evaluation will fail
  817. trying to find the :END: marker."
  818. (org-test-with-temp-text
  819. "- indented
  820. #+begin_src sh :results file wrap
  821. echo test.txt
  822. #+end_src"
  823. (org-babel-next-src-block 1)
  824. (org-babel-execute-src-block)
  825. (org-babel-execute-src-block)))
  826. (ert-deftest test-ob/file-desc-header-argument ()
  827. "Test that the :file-desc header argument is used."
  828. (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  829. \"foo\"
  830. #+end_src
  831. #+begin_src emacs-lisp :results file :file-desc
  832. \"foo\"
  833. #+end_src"
  834. (org-babel-execute-src-block)
  835. (org-babel-next-src-block 1)
  836. (org-babel-execute-src-block)
  837. (goto-char (point-min))
  838. (should (search-forward "[[file:foo][bar]]" nil t))
  839. (should (search-forward "[[file:foo][foo]]" nil t))))
  840. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  841. "Test `org-babel-remove-result' with :results pp."
  842. (test-ob-verify-result-and-removed-result
  843. ": \"I /am/ working!\""
  844. "* org-babel-remove-result
  845. #+begin_src emacs-lisp :results pp
  846. \"I /am/ working!\")
  847. #+end_src
  848. * next heading"))
  849. (ert-deftest test-org-babel/inline-src_blk-preceded-punct-preceded-by-point ()
  850. (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  851. (org-babel-inline-result-wrap "=%s="))
  852. (org-test-with-temp-text
  853. test-line
  854. (forward-char 1)
  855. (org-babel-execute-maybe)
  856. (should (re-search-forward "=\"x\"=" nil t))
  857. (forward-line))))
  858. (ert-deftest test-ob/commented-last-block-line-with-var ()
  859. (org-test-with-temp-text-in-file "
  860. #+begin_src emacs-lisp :var a=1
  861. ;;
  862. #+end_src"
  863. (org-babel-next-src-block)
  864. (org-babel-execute-maybe)
  865. (re-search-forward "\\#\\+results:" nil t)
  866. (forward-line)
  867. (should (string=
  868. ""
  869. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  870. (org-test-with-temp-text-in-file "
  871. #+begin_src emacs-lisp :var a=2
  872. 2;;
  873. #+end_src"
  874. (org-babel-next-src-block)
  875. (org-babel-execute-maybe)
  876. (re-search-forward "\\#\\+results:" nil t)
  877. (forward-line)
  878. (should (string=
  879. ": 2"
  880. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  881. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  882. "Test helper function to test `org-babel-remove-result'.
  883. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  884. and the result of execution is verified against RESULT.
  885. The block is actually executed /twice/ to ensure result
  886. replacement happens correctly."
  887. (org-test-with-temp-text
  888. buffer-text
  889. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  890. (should (re-search-forward "\\#\\+results:" nil t))
  891. (forward-line)
  892. (should (string= result
  893. (buffer-substring-no-properties
  894. (point-at-bol)
  895. (- (point-max) 16))))
  896. (org-babel-previous-src-block) (org-babel-remove-result)
  897. (should (string= buffer-text
  898. (buffer-substring-no-properties
  899. (point-min) (point-max))))))
  900. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  901. "Test `org-babel-remove-result' with default :results."
  902. (mapcar (lambda (language)
  903. (test-ob-verify-result-and-removed-result
  904. "\n"
  905. (concat
  906. "* org-babel-remove-result
  907. #+begin_src " language "
  908. #+end_src
  909. * next heading")))
  910. '("sh" "emacs-lisp")))
  911. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  912. "Test `org-babel-remove-result' with :results list."
  913. (test-ob-verify-result-and-removed-result
  914. "- 1
  915. - 2
  916. - 3
  917. - (quote (4 5))"
  918. "* org-babel-remove-result
  919. #+begin_src emacs-lisp :results list
  920. '(1 2 3 '(4 5))
  921. #+end_src
  922. * next heading"))
  923. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  924. "Test `org-babel-remove-result' with :results wrap."
  925. (test-ob-verify-result-and-removed-result
  926. ":RESULTS:
  927. hello there
  928. :END:"
  929. "* org-babel-remove-result
  930. #+begin_src emacs-lisp :results wrap
  931. \"hello there\"
  932. #+end_src
  933. * next heading"))
  934. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  935. "Test `org-babel-remove-result' with :results org."
  936. (test-ob-verify-result-and-removed-result
  937. "#+BEGIN_SRC org
  938. ,* heading
  939. ,** subheading
  940. content
  941. #+END_SRC"
  942. "* org-babel-remove-result
  943. #+begin_src emacs-lisp :results org
  944. \"* heading
  945. ,** subheading
  946. content\"
  947. #+end_src
  948. * next heading"))
  949. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  950. "Test `org-babel-remove-result' with :results html."
  951. (test-ob-verify-result-and-removed-result
  952. "#+BEGIN_HTML
  953. <head><body></body></head>
  954. #+END_HTML"
  955. "* org-babel-remove-result
  956. #+begin_src emacs-lisp :results html
  957. \"<head><body></body></head>\"
  958. #+end_src
  959. * next heading"))
  960. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  961. "Test `org-babel-remove-result' with :results latex."
  962. (test-ob-verify-result-and-removed-result
  963. "#+BEGIN_LaTeX
  964. Line 1
  965. Line 2
  966. Line 3
  967. #+END_LaTeX"
  968. "* org-babel-remove-result
  969. #+begin_src emacs-lisp :results latex
  970. \"Line 1
  971. Line 2
  972. Line 3\"
  973. #+end_src
  974. * next heading"))
  975. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  976. "Test `org-babel-remove-result' with :results code."
  977. (test-ob-verify-result-and-removed-result
  978. "#+BEGIN_SRC emacs-lisp
  979. \"I am working!\"
  980. #+END_SRC"
  981. "* org-babel-remove-result
  982. #+begin_src emacs-lisp :results code
  983. (message \"I am working!\")
  984. #+end_src
  985. * next heading"))
  986. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  987. "Test `org-babel-remove-result' with :results pp."
  988. (test-ob-verify-result-and-removed-result
  989. ": \"I /am/ working!\""
  990. "* org-babel-remove-result
  991. #+begin_src emacs-lisp :results pp
  992. \"I /am/ working!\")
  993. #+end_src
  994. * next heading"))
  995. (ert-deftest test-ob/results-do-not-replace-code-blocks ()
  996. (org-test-with-temp-text "Block two has a space after the name.
  997. #+name: foo
  998. #+begin_src emacs-lisp
  999. 1
  1000. #+end_src
  1001. #+name: foo
  1002. #+begin_src emacs-lisp
  1003. 2
  1004. #+end_src
  1005. #+name: foo
  1006. #+begin_src emacs-lisp
  1007. 3
  1008. #+end_src
  1009. #+RESULTS: foo
  1010. : foo
  1011. "
  1012. (dolist (num '(1 2 3))
  1013. ;; execute the block
  1014. (goto-char (point-min))
  1015. (org-babel-next-src-block num) (org-babel-execute-src-block)
  1016. ;; check the results
  1017. (goto-char (point-max))
  1018. (move-beginning-of-line 0)
  1019. (should (looking-at (format ": %d" num))))))
  1020. (ert-deftest test-ob/blocks-with-spaces ()
  1021. "Test expansion of blocks followed by blank lines."
  1022. (should
  1023. (equal "#+BEGIN_SRC emacs-lisp
  1024. \(+ 1 2)
  1025. #+END_SRC
  1026. #+RESULTS:
  1027. : 3\n\n\n"
  1028. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
  1029. \(+ 1 2)
  1030. #+END_SRC\n\n\n"
  1031. (let ((org-babel-next-src-block "RESULTS"))
  1032. (org-babel-execute-src-block))
  1033. (buffer-string)))))
  1034. (ert-deftest test-ob/results-in-narrowed-buffer ()
  1035. "Test block execution in a narrowed buffer."
  1036. ;; If results don't exist, they should be inserted in visible part
  1037. ;; of the buffer.
  1038. (should
  1039. (equal
  1040. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
  1041. (org-test-with-temp-text
  1042. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1043. (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
  1044. (let ((org-babel-results-keyword "RESULTS"))
  1045. (org-babel-execute-src-block))
  1046. (org-trim (buffer-string)))))
  1047. (should
  1048. (equal
  1049. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
  1050. (org-test-with-temp-text
  1051. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1052. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1053. (let ((org-babel-results-keyword "RESULTS"))
  1054. (org-babel-execute-src-block))
  1055. (org-trim (buffer-string)))))
  1056. ;; Results in visible part of buffer, should be updated here.
  1057. (should
  1058. (equal
  1059. "#+NAME: test
  1060. #+BEGIN_SRC emacs-lisp
  1061. \(+ 1 2)
  1062. #+END_SRC
  1063. #+RESULTS: test
  1064. : 3"
  1065. (org-test-with-temp-text
  1066. "#+NAME: test
  1067. #+BEGIN_SRC emacs-lisp
  1068. \(+ 1 2)
  1069. #+END_SRC
  1070. #+RESULTS: test
  1071. : 4
  1072. Paragraph"
  1073. (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
  1074. (let ((org-babel-results-keyword "RESULTS"))
  1075. (org-babel-execute-src-block))
  1076. (org-trim (buffer-string)))))
  1077. ;; Results in invisible part of buffer, should be updated there.
  1078. (org-test-with-temp-text
  1079. "#+NAME: test
  1080. #+BEGIN_SRC emacs-lisp
  1081. \(+ 1 2)
  1082. #+END_SRC
  1083. #+RESULTS: test
  1084. : 4
  1085. Paragraph"
  1086. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1087. (let ((org-babel-results-keyword "RESULTS"))
  1088. (org-babel-execute-src-block))
  1089. (should-not (re-search-forward "^#\\+RESULTS:" nil t))
  1090. (widen)
  1091. (should (should (re-search-forward "^: 3" nil t)))))
  1092. (ert-deftest test-ob/specific-colnames ()
  1093. "Test passing specific column names."
  1094. (should
  1095. (equal "#+name: input-table
  1096. | id | var1 |
  1097. |----+------|
  1098. | 1 | bar |
  1099. | 2 | baz |
  1100. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1101. echo \"$data\"
  1102. #+end_src
  1103. #+RESULTS:
  1104. | Rev | Author |
  1105. |-----+--------|
  1106. | 1 | bar |
  1107. | 2 | baz |
  1108. "
  1109. (org-test-with-temp-text
  1110. "#+name: input-table
  1111. | id | var1 |
  1112. |----+------|
  1113. | 1 | bar |
  1114. | 2 | baz |
  1115. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1116. echo \"$data\"
  1117. #+end_src
  1118. "
  1119. ;; we should find a code block
  1120. (should (re-search-forward org-babel-src-block-regexp nil t))
  1121. (goto-char (match-beginning 0))
  1122. ;; now that we've located the code block, it may be evaluated
  1123. (let ((org-babel-execute-src-block "RESULTS"))
  1124. (org-babel-execute-src-block))
  1125. (buffer-string)))))
  1126. (ert-deftest test-ob/location-of-header-arg-eval ()
  1127. "Test location of header argument evaluation."
  1128. (org-test-with-temp-text "
  1129. #+name: top-block
  1130. #+begin_src emacs-lisp :var pt=(point)
  1131. pt
  1132. #+end_src
  1133. #+name: bottom-block
  1134. #+begin_src emacs-lisp :var pt=top-block()
  1135. pt
  1136. #+end_src
  1137. "
  1138. ;; the value of the second block should be greater than the first
  1139. (should
  1140. (< (progn (re-search-forward org-babel-src-block-regexp nil t)
  1141. (goto-char (match-beginning 0))
  1142. (prog1 (save-match-data (org-babel-execute-src-block))
  1143. (goto-char (match-end 0))))
  1144. (progn (re-search-forward org-babel-src-block-regexp nil t)
  1145. (goto-char (match-beginning 0))
  1146. (org-babel-execute-src-block))))))
  1147. (ert-deftest test-ob/preserve-results-indentation ()
  1148. "Preserve indentation when executing a src block."
  1149. (should
  1150. (equal
  1151. '(2 2)
  1152. (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1153. (org-babel-execute-src-block)
  1154. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1155. (list (org-get-indentation)
  1156. (progn (forward-line) (org-get-indentation))))))
  1157. (should
  1158. (equal
  1159. '(2 2)
  1160. (org-test-with-temp-text
  1161. " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1162. (org-babel-execute-src-block)
  1163. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1164. (list (org-get-indentation)
  1165. (progn (forward-line) (org-get-indentation))))))
  1166. ;; Don't get fooled by TAB-based indentation.
  1167. (should
  1168. (equal
  1169. '(6 6)
  1170. (org-test-with-temp-text
  1171. "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
  1172. (setq tab-width 4)
  1173. (org-babel-execute-src-block)
  1174. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1175. (list (org-get-indentation)
  1176. (progn (forward-line) (org-get-indentation)))))))
  1177. (ert-deftest test-ob/safe-header-args ()
  1178. "Detect safe and unsafe header args."
  1179. (let ((safe-args '((:cache . "foo")
  1180. (:results . "output")
  1181. (:eval . "never")
  1182. (:eval . "query")))
  1183. (unsafe-args '((:eval . "yes")
  1184. (:results . "output file")
  1185. (:foo . "bar")))
  1186. (malformed-args '((bar . "foo")
  1187. ("foo" . "bar")
  1188. :foo))
  1189. (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
  1190. (dolist (arg safe-args)
  1191. (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
  1192. (dolist (arg unsafe-args)
  1193. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1194. (dolist (arg malformed-args)
  1195. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1196. (should (not (funcall safe-p (append safe-args unsafe-args))))))
  1197. (ert-deftest test-ob/noweb-expansions-in-cache ()
  1198. "Ensure that noweb expansions are expanded before caching."
  1199. (let ((noweb-expansions-in-cache-var 0))
  1200. (org-test-with-temp-text "
  1201. #+name: foo
  1202. #+begin_src emacs-lisp
  1203. \"I said\"
  1204. #+end_src
  1205. #+name: bar
  1206. #+begin_src emacs-lisp :noweb yes :cache yes
  1207. (setq noweb-expansions-in-cache-var
  1208. (+ 1 noweb-expansions-in-cache-var))
  1209. (concat <<foo>> \" check noweb expansions\")
  1210. #+end_src
  1211. "
  1212. ;; run the second block to create the cache
  1213. (goto-char (point-min))
  1214. (re-search-forward (regexp-quote "#+name: bar"))
  1215. (should (string= "I said check noweb expansions"
  1216. (org-babel-execute-src-block)))
  1217. (should (= noweb-expansions-in-cache-var 1))
  1218. ;; change the value of the first block
  1219. (goto-char (point-min))
  1220. (re-search-forward (regexp-quote "said"))
  1221. (goto-char (match-beginning 0))
  1222. (insert "haven't ")
  1223. (re-search-forward (regexp-quote "#+name: bar"))
  1224. (should (string= "I haven't said check noweb expansions"
  1225. (org-babel-execute-src-block)))
  1226. (should (= noweb-expansions-in-cache-var 2)))))
  1227. (ert-deftest test-org-babel/file-ext-and-output-dir ()
  1228. (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
  1229. (org-babel-next-src-block)
  1230. (should (equal "file-ext-basic.txt"
  1231. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1232. (org-babel-next-src-block)
  1233. (should (equal "foo/file-ext-dir-relative.txt"
  1234. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1235. (org-babel-next-src-block)
  1236. (should (equal "foo/file-ext-dir-relative-slash.txt"
  1237. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1238. (org-babel-next-src-block)
  1239. (should (equal "/tmp/file-ext-dir-absolute.txt"
  1240. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1241. (org-babel-next-src-block)
  1242. (should (equal "foo.bar"
  1243. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1244. (org-babel-next-src-block)
  1245. (should (equal "xxx/foo.bar"
  1246. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1247. ))
  1248. (ert-deftest test-org-babel/script-escape ()
  1249. ;; Delimited lists of numbers
  1250. (should (equal '(1 2 3)
  1251. (org-babel-script-escape "[1 2 3]")))
  1252. (should (equal '(1 2 3)
  1253. (org-babel-script-escape "{1 2 3}")))
  1254. (should (equal '(1 2 3)
  1255. (org-babel-script-escape "(1 2 3)")))
  1256. ;; Delimited lists of double-quoted strings
  1257. (should (equal '("foo" "bar")
  1258. (org-babel-script-escape "(\"foo\" \"bar\")")))
  1259. (should (equal '("foo" "bar")
  1260. (org-babel-script-escape "[\"foo\" \"bar\"]")))
  1261. (should (equal '("foo" "bar")
  1262. (org-babel-script-escape "{\"foo\" \"bar\"}")))
  1263. ;; ... with commas
  1264. (should (equal '("foo" "bar")
  1265. (org-babel-script-escape "(\"foo\", \"bar\")")))
  1266. (should (equal '("foo" "bar")
  1267. (org-babel-script-escape "[\"foo\", \"bar\"]")))
  1268. (should (equal '("foo" "bar")
  1269. (org-babel-script-escape "{\"foo\", \"bar\"}")))
  1270. ;; Delimited lists of single-quoted strings
  1271. (should (equal '("foo" "bar")
  1272. (org-babel-script-escape "('foo' 'bar')")))
  1273. (should (equal '("foo" "bar")
  1274. (org-babel-script-escape "['foo' 'bar']")))
  1275. (should (equal '("foo" "bar")
  1276. (org-babel-script-escape "{'foo' 'bar'}")))
  1277. ;; ... with commas
  1278. (should (equal '("foo" "bar")
  1279. (org-babel-script-escape "('foo', 'bar')")))
  1280. (should (equal '("foo" "bar")
  1281. (org-babel-script-escape "['foo', 'bar']")))
  1282. (should (equal '("foo" "bar")
  1283. (org-babel-script-escape "{'foo', 'bar'}")))
  1284. ;; Single quoted strings
  1285. (should (equal "foo"
  1286. (org-babel-script-escape "'foo'")))
  1287. ;; ... with internal double quote
  1288. (should (equal "foo\"bar"
  1289. (org-babel-script-escape "'foo\"bar'")))
  1290. ;; ... with internal backslash
  1291. (should (equal "foo\\bar"
  1292. (org-babel-script-escape "'foo\\bar'")))
  1293. ;; ... with internal escaped backslash
  1294. (should (equal "foo\\bar"
  1295. (org-babel-script-escape "'foo\\\\bar'")))
  1296. ;; ... with internal backslash-double quote
  1297. (should (equal "foo\\\"bar"
  1298. (org-babel-script-escape "'foo\\\"bar'")))
  1299. ;; ... with internal escaped backslash-double quote
  1300. (should (equal "foo\\\"bar"
  1301. (org-babel-script-escape "'foo\\\\\"bar'")))
  1302. ;; ... with internal escaped single quote
  1303. (should (equal "foo'bar"
  1304. (org-babel-script-escape "'foo\\'bar'")))
  1305. ;; ... with internal escaped backslash-escaped single quote
  1306. (should (equal "foo\\'bar"
  1307. (org-babel-script-escape "'foo\\\\\\'bar'")))
  1308. ;; Double quoted strings
  1309. (should (equal "foo"
  1310. (org-babel-script-escape "\"foo\"")))
  1311. ;; ... with internal single quote
  1312. (should (equal "foo'bar"
  1313. (org-babel-script-escape "\"foo'bar\"")))
  1314. ;; ... with internal backslash
  1315. (should (equal "foo\\bar"
  1316. (org-babel-script-escape "\"foo\\bar\"")))
  1317. ;; ... with internal escaped backslash
  1318. (should (equal "foo\\bar"
  1319. (org-babel-script-escape "\"foo\\\\bar\"")))
  1320. ;; ... with internal backslash-single quote
  1321. (should (equal "foo\\'bar"
  1322. (org-babel-script-escape "\"foo\\'bar\"")))
  1323. ;; ... with internal escaped backslash-single quote
  1324. (should (equal "foo\\'bar"
  1325. (org-babel-script-escape "\"foo\\\\'bar\"")))
  1326. ;; ... with internal escaped double quote
  1327. (should (equal "foo\"bar"
  1328. (org-babel-script-escape "\"foo\\\"bar\"")))
  1329. ;; ... with internal escaped backslash-escaped double quote
  1330. (should (equal "foo\\\"bar"
  1331. (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
  1332. (ert-deftest ob/process-params-no-duplicates ()
  1333. (should (equal (org-babel-process-params '((:colname-names . 1)
  1334. (:rowname-names . 1)
  1335. (:result-params . 1)
  1336. (:result-type . 1)
  1337. (:var . "\"foo\"")))
  1338. '((:var)
  1339. (:colname-names . 1)
  1340. (:rowname-names . 1)
  1341. (:result-params . 1)
  1342. (:result-type . value)))))
  1343. (provide 'test-ob)
  1344. ;;; test-ob ends here