test-ob.el 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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/resolve-code-blocks-before-data-blocks ()
  548. (org-test-with-temp-text "
  549. #+name: foo
  550. : bar
  551. #+name: foo
  552. #+begin_src emacs-lisp
  553. \"baz\"
  554. #+end_src
  555. #+begin_src emacs-lisp :var foo=foo
  556. foo
  557. #+end_src"
  558. (org-babel-next-src-block 2)
  559. (should (string= (org-babel-execute-src-block) "baz"))))
  560. (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
  561. (org-test-with-temp-text "
  562. #+tblname: base_plus
  563. | 1 |
  564. | 2 |
  565. #+tblname: base
  566. | 3 |
  567. | 4 |
  568. #+begin_src emacs-lisp :var x=base
  569. x
  570. #+end_src"
  571. (org-babel-next-src-block 1)
  572. (should (equal (org-babel-execute-src-block) '((3) (4))))))
  573. (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
  574. (org-test-with-temp-text "
  575. #+name: base_plus
  576. #+begin_src emacs-lisp
  577. 'bar
  578. #+end_src
  579. #+name: base
  580. #+begin_src emacs-lisp
  581. 'foo
  582. #+end_src
  583. #+begin_src emacs-lisp :var x=base
  584. x
  585. #+end_src"
  586. (org-babel-next-src-block 3)
  587. (should (equal (org-babel-execute-src-block) "foo"))))
  588. (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
  589. (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
  590. (+ a b c d)
  591. #+end_src
  592. "
  593. (should (= 10 (org-babel-execute-src-block)))))
  594. (ert-deftest test-ob/org-babel-update-intermediate ()
  595. (org-test-with-temp-text "#+name: foo
  596. #+begin_src emacs-lisp
  597. 2
  598. #+end_src
  599. #+results: foo
  600. : 4
  601. #+begin_src emacs-lisp :var it=foo
  602. (+ it 1)
  603. #+end_src"
  604. (let ((org-babel-update-intermediate nil))
  605. (goto-char (point-min))
  606. (org-babel-next-src-block 2)
  607. (should (= 3 (org-babel-execute-src-block)))
  608. (goto-char (point-min))
  609. (forward-line 6)
  610. (should (looking-at ": 4")))
  611. (let ((org-babel-update-intermediate t))
  612. (goto-char (point-min))
  613. (org-babel-next-src-block 2)
  614. (should (= 3 (org-babel-execute-src-block)))
  615. (goto-char (point-min))
  616. (forward-line 6)
  617. (should (looking-at ": 2")))))
  618. (ert-deftest test-ob/eval-header-argument ()
  619. (flet ((check-eval (eval runp)
  620. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  621. (setq foo :evald)
  622. #+end_src" eval)
  623. (let ((foo :not-run))
  624. (if runp
  625. (progn (should (org-babel-execute-src-block))
  626. (should (eq foo :evald)))
  627. (progn (should-not (org-babel-execute-src-block))
  628. (should-not (eq foo :evald))))))))
  629. (check-eval "never" nil)
  630. (check-eval "no" nil)
  631. (check-eval "never-export" t)
  632. (check-eval "no-export" t)
  633. (let ((org-babel-exp-reference-buffer (current-buffer)))
  634. (check-eval "never" nil)
  635. (check-eval "no" nil)
  636. (check-eval "never-export" nil)
  637. (check-eval "no-export" nil))))
  638. (ert-deftest test-ob/noweb-expansion-1 ()
  639. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  640. <<foo>>
  641. #+end_src
  642. #+name: foo
  643. #+begin_src sh
  644. bar
  645. #+end_src"
  646. (should (string= (org-babel-expand-noweb-references) "bar"))))
  647. (ert-deftest test-ob/noweb-expansion-2 ()
  648. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  649. <<foo>>
  650. #+end_src
  651. #+name: foo
  652. #+begin_src sh :noweb-sep \"\"
  653. bar
  654. #+end_src
  655. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  656. baz
  657. #+end_src"
  658. (should (string= (org-babel-expand-noweb-references) "barbaz"))))
  659. (ert-deftest test-ob/splitting-variable-lists-in-references ()
  660. (org-test-with-temp-text ""
  661. (should (= 1 (length (org-babel-ref-split-args
  662. "a=\"this, no work\""))))
  663. (should (= 2 (length (org-babel-ref-split-args
  664. "a=\"this, no work\", b=1"))))))
  665. (ert-deftest test-ob/org-babel-balanced-split ()
  666. (should (equal
  667. '(":a 1" "b [2 3]" "c (4 :d (5 6))")
  668. (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
  669. '((32 9) . 58)))))
  670. (ert-deftest test-ob/commented-last-block-line-no-var ()
  671. (org-test-with-temp-text-in-file "
  672. #+begin_src emacs-lisp
  673. ;;
  674. #+end_src"
  675. (org-babel-next-src-block)
  676. (org-babel-execute-maybe)
  677. (should (re-search-forward "\\#\\+results:" nil t))
  678. (forward-line)
  679. (should
  680. (string=
  681. ""
  682. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  683. (org-test-with-temp-text-in-file "
  684. #+begin_src emacs-lisp
  685. \"some text\";;
  686. #+end_src"
  687. (org-babel-next-src-block)
  688. (org-babel-execute-maybe)
  689. (should (re-search-forward "\\#\\+results:" nil t))
  690. (forward-line)
  691. (should
  692. (string=
  693. ": some text"
  694. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  695. (ert-deftest test-ob/commented-last-block-line-with-var ()
  696. (org-test-with-temp-text-in-file "
  697. #+begin_src emacs-lisp :var a=1
  698. ;;
  699. #+end_src"
  700. (org-babel-next-src-block)
  701. (org-babel-execute-maybe)
  702. (re-search-forward "\\#\\+results:" nil t)
  703. (forward-line)
  704. (should (string=
  705. ""
  706. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  707. (org-test-with-temp-text-in-file "
  708. #+begin_src emacs-lisp :var a=2
  709. 2;;
  710. #+end_src"
  711. (org-babel-next-src-block)
  712. (org-babel-execute-maybe)
  713. (re-search-forward "\\#\\+results:" nil t)
  714. (forward-line)
  715. (should (string=
  716. ": 2"
  717. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  718. (ert-deftest test-ob/org-babel-insert-result--improper-lists ()
  719. "Test `org-babel-insert-result' with improper lists."
  720. ;; Do not error when output is an improper list.
  721. (should
  722. (org-test-with-temp-text
  723. "
  724. <point>#+BEGIN_SRC emacs-lisp
  725. '((1 . nil) (2 . 3))
  726. #+END_SRC
  727. "
  728. (org-babel-execute-maybe) t)))
  729. (ert-deftest test-ob/remove-inline-result ()
  730. "Test `org-babel-remove-inline-result' honors whitespace."
  731. (let*
  732. ((inline-sb "src_emacs-lisp{(+ 1 2)}")
  733. (inline-res " {{{results(=3=)}}}")
  734. (inline-sb-dot (concat inline-sb "."))
  735. (inline-sb-res-dot (concat inline-sb inline-res ".")))
  736. (org-test-with-temp-text
  737. ;; Insert inline_src_block followed by dot.
  738. inline-sb-dot
  739. ;; Insert result before dot.
  740. (org-babel-execute-maybe)
  741. (should (string= inline-sb-res-dot
  742. (buffer-substring-no-properties
  743. (point-at-bol) (point-at-eol))))
  744. ;; Delete whitespace and result.
  745. (org-babel-remove-inline-result)
  746. (should (string= inline-sb-dot
  747. (buffer-substring-no-properties
  748. (point-at-bol) (point-at-eol))))
  749. ;; Add whitespace and result before dot.
  750. (search-forward inline-sb)
  751. (insert " " inline-res)
  752. (goto-char (point-at-bol))
  753. ;; Remove whitespace and result.
  754. (org-babel-remove-inline-result)
  755. (should (string= inline-sb-dot
  756. (buffer-substring-no-properties
  757. (point-at-bol) (point-at-eol))))
  758. ;; Add whitespace before dot.
  759. (search-forward inline-sb)
  760. (insert " ")
  761. (goto-char (point-at-bol))
  762. ;; Add result before whitespace.
  763. (org-babel-execute-maybe)
  764. ;; Remove result - leave trailing whitespace and dot.
  765. (org-babel-remove-inline-result)
  766. (should (string= (concat inline-sb " .")
  767. (buffer-substring-no-properties
  768. (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-babel-execute-maybe) (org-babel-execute-maybe)
  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-results-indented-wrap ()
  812. "Ensure that wrapped results are inserted correction when indented.
  813. If not inserted correctly then the second evaluation will fail
  814. trying to find the :END: marker."
  815. (org-test-with-temp-text
  816. "- indented
  817. #+begin_src sh :results file wrap
  818. echo test.txt
  819. #+end_src"
  820. (org-babel-next-src-block 1)
  821. (org-babel-execute-src-block)
  822. (org-babel-execute-src-block)))
  823. (ert-deftest test-ob/file-desc-header-argument ()
  824. "Test that the :file-desc header argument is used."
  825. (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  826. \"foo\"
  827. #+end_src
  828. #+begin_src emacs-lisp :results file :file-desc
  829. \"foo\"
  830. #+end_src"
  831. (org-babel-execute-src-block)
  832. (org-babel-next-src-block 1)
  833. (org-babel-execute-src-block)
  834. (goto-char (point-min))
  835. (should (search-forward "[[file:foo][bar]]" nil t))
  836. (should (search-forward "[[file:foo][foo]]" nil t))))
  837. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  838. "Test `org-babel-remove-result' with :results pp."
  839. (test-ob-verify-result-and-removed-result
  840. ": \"I /am/ working!\""
  841. "* org-babel-remove-result
  842. #+begin_src emacs-lisp :results pp
  843. \"I /am/ working!\")
  844. #+end_src
  845. * next heading"))
  846. (ert-deftest test-org-babel/inline-src_blk-preceded-punct-preceded-by-point ()
  847. (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  848. (org-babel-inline-result-wrap "=%s="))
  849. (org-test-with-temp-text
  850. test-line
  851. (forward-char 1)
  852. (org-babel-execute-maybe)
  853. (should (re-search-forward "=\"x\"=" nil t))
  854. (forward-line))))
  855. (ert-deftest test-ob/commented-last-block-line-with-var ()
  856. (org-test-with-temp-text-in-file "
  857. #+begin_src emacs-lisp :var a=1
  858. ;;
  859. #+end_src"
  860. (org-babel-next-src-block)
  861. (org-babel-execute-maybe)
  862. (re-search-forward "\\#\\+results:" nil t)
  863. (forward-line)
  864. (should (string=
  865. ""
  866. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  867. (org-test-with-temp-text-in-file "
  868. #+begin_src emacs-lisp :var a=2
  869. 2;;
  870. #+end_src"
  871. (org-babel-next-src-block)
  872. (org-babel-execute-maybe)
  873. (re-search-forward "\\#\\+results:" nil t)
  874. (forward-line)
  875. (should (string=
  876. ": 2"
  877. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  878. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  879. "Test helper function to test `org-babel-remove-result'.
  880. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  881. and the result of execution is verified against RESULT.
  882. The block is actually executed /twice/ to ensure result
  883. replacement happens correctly."
  884. (org-test-with-temp-text
  885. buffer-text
  886. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  887. (should (re-search-forward "\\#\\+results:" nil t))
  888. (forward-line)
  889. (should (string= result
  890. (buffer-substring-no-properties
  891. (point-at-bol)
  892. (- (point-max) 16))))
  893. (org-babel-previous-src-block) (org-babel-remove-result)
  894. (should (string= buffer-text
  895. (buffer-substring-no-properties
  896. (point-min) (point-max))))))
  897. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  898. "Test `org-babel-remove-result' with default :results."
  899. (mapcar (lambda (language)
  900. (test-ob-verify-result-and-removed-result
  901. "\n"
  902. (concat
  903. "* org-babel-remove-result
  904. #+begin_src " language "
  905. #+end_src
  906. * next heading")))
  907. '("sh" "emacs-lisp")))
  908. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  909. "Test `org-babel-remove-result' with :results list."
  910. (test-ob-verify-result-and-removed-result
  911. "- 1
  912. - 2
  913. - 3
  914. - (quote (4 5))"
  915. "* org-babel-remove-result
  916. #+begin_src emacs-lisp :results list
  917. '(1 2 3 '(4 5))
  918. #+end_src
  919. * next heading"))
  920. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  921. "Test `org-babel-remove-result' with :results wrap."
  922. (test-ob-verify-result-and-removed-result
  923. ":RESULTS:
  924. hello there
  925. :END:"
  926. "* org-babel-remove-result
  927. #+begin_src emacs-lisp :results wrap
  928. \"hello there\"
  929. #+end_src
  930. * next heading"))
  931. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  932. "Test `org-babel-remove-result' with :results org."
  933. (test-ob-verify-result-and-removed-result
  934. "#+BEGIN_SRC org
  935. ,* heading
  936. ,** subheading
  937. content
  938. #+END_SRC"
  939. "* org-babel-remove-result
  940. #+begin_src emacs-lisp :results org
  941. \"* heading
  942. ,** subheading
  943. content\"
  944. #+end_src
  945. * next heading"))
  946. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  947. "Test `org-babel-remove-result' with :results html."
  948. (test-ob-verify-result-and-removed-result
  949. "#+BEGIN_HTML
  950. <head><body></body></head>
  951. #+END_HTML"
  952. "* org-babel-remove-result
  953. #+begin_src emacs-lisp :results html
  954. \"<head><body></body></head>\"
  955. #+end_src
  956. * next heading"))
  957. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  958. "Test `org-babel-remove-result' with :results latex."
  959. (test-ob-verify-result-and-removed-result
  960. "#+BEGIN_LaTeX
  961. Line 1
  962. Line 2
  963. Line 3
  964. #+END_LaTeX"
  965. "* org-babel-remove-result
  966. #+begin_src emacs-lisp :results latex
  967. \"Line 1
  968. Line 2
  969. Line 3\"
  970. #+end_src
  971. * next heading"))
  972. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  973. "Test `org-babel-remove-result' with :results code."
  974. (test-ob-verify-result-and-removed-result
  975. "#+BEGIN_SRC emacs-lisp
  976. \"I am working!\"
  977. #+END_SRC"
  978. "* org-babel-remove-result
  979. #+begin_src emacs-lisp :results code
  980. (message \"I am working!\")
  981. #+end_src
  982. * next heading"))
  983. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  984. "Test `org-babel-remove-result' with :results pp."
  985. (test-ob-verify-result-and-removed-result
  986. ": \"I /am/ working!\""
  987. "* org-babel-remove-result
  988. #+begin_src emacs-lisp :results pp
  989. \"I /am/ working!\")
  990. #+end_src
  991. * next heading"))
  992. (ert-deftest test-ob/results-do-not-replace-code-blocks ()
  993. (org-test-with-temp-text "Block two has a space after the name.
  994. #+name: foo
  995. #+begin_src emacs-lisp
  996. 1
  997. #+end_src
  998. #+name: foo
  999. #+begin_src emacs-lisp
  1000. 2
  1001. #+end_src
  1002. #+name: foo
  1003. #+begin_src emacs-lisp
  1004. 3
  1005. #+end_src
  1006. #+RESULTS: foo
  1007. : foo
  1008. "
  1009. (dolist (num '(1 2 3))
  1010. ;; execute the block
  1011. (goto-char (point-min))
  1012. (org-babel-next-src-block num) (org-babel-execute-src-block)
  1013. ;; check the results
  1014. (goto-char (point-max))
  1015. (move-beginning-of-line 0)
  1016. (should (looking-at (format ": %d" num))))))
  1017. (ert-deftest test-ob/blocks-with-spaces ()
  1018. "Test expansion of blocks followed by blank lines."
  1019. (should
  1020. (equal "#+BEGIN_SRC emacs-lisp
  1021. \(+ 1 2)
  1022. #+END_SRC
  1023. #+RESULTS:
  1024. : 3\n\n\n"
  1025. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
  1026. \(+ 1 2)
  1027. #+END_SRC\n\n\n"
  1028. (let ((org-babel-next-src-block "RESULTS"))
  1029. (org-babel-execute-src-block))
  1030. (buffer-string)))))
  1031. (ert-deftest test-ob/results-in-narrowed-buffer ()
  1032. "Test block execution in a narrowed buffer."
  1033. ;; If results don't exist, they should be inserted in visible part
  1034. ;; of the buffer.
  1035. (should
  1036. (equal
  1037. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
  1038. (org-test-with-temp-text
  1039. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1040. (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
  1041. (let ((org-babel-results-keyword "RESULTS"))
  1042. (org-babel-execute-src-block))
  1043. (org-trim (buffer-string)))))
  1044. (should
  1045. (equal
  1046. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
  1047. (org-test-with-temp-text
  1048. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1049. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1050. (let ((org-babel-results-keyword "RESULTS"))
  1051. (org-babel-execute-src-block))
  1052. (org-trim (buffer-string)))))
  1053. ;; Results in visible part of buffer, should be updated here.
  1054. (should
  1055. (equal
  1056. "#+NAME: test
  1057. #+BEGIN_SRC emacs-lisp
  1058. \(+ 1 2)
  1059. #+END_SRC
  1060. #+RESULTS: test
  1061. : 3"
  1062. (org-test-with-temp-text
  1063. "#+NAME: test
  1064. #+BEGIN_SRC emacs-lisp
  1065. \(+ 1 2)
  1066. #+END_SRC
  1067. #+RESULTS: test
  1068. : 4
  1069. Paragraph"
  1070. (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
  1071. (let ((org-babel-results-keyword "RESULTS"))
  1072. (org-babel-execute-src-block))
  1073. (org-trim (buffer-string)))))
  1074. ;; Results in invisible part of buffer, should be updated there.
  1075. (org-test-with-temp-text
  1076. "#+NAME: test
  1077. #+BEGIN_SRC emacs-lisp
  1078. \(+ 1 2)
  1079. #+END_SRC
  1080. #+RESULTS: test
  1081. : 4
  1082. Paragraph"
  1083. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1084. (let ((org-babel-results-keyword "RESULTS"))
  1085. (org-babel-execute-src-block))
  1086. (should-not (re-search-forward "^#\\+RESULTS:" nil t))
  1087. (widen)
  1088. (should (should (re-search-forward "^: 3" nil t)))))
  1089. (ert-deftest test-ob/specific-colnames ()
  1090. "Test passing specific column names."
  1091. (should
  1092. (equal "#+name: input-table
  1093. | id | var1 |
  1094. |----+------|
  1095. | 1 | bar |
  1096. | 2 | baz |
  1097. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1098. echo \"$data\"
  1099. #+end_src
  1100. #+RESULTS:
  1101. | Rev | Author |
  1102. |-----+--------|
  1103. | 1 | bar |
  1104. | 2 | baz |
  1105. "
  1106. (org-test-with-temp-text
  1107. "#+name: input-table
  1108. | id | var1 |
  1109. |----+------|
  1110. | 1 | bar |
  1111. | 2 | baz |
  1112. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1113. echo \"$data\"
  1114. #+end_src
  1115. "
  1116. ;; we should find a code block
  1117. (should (re-search-forward org-babel-src-block-regexp nil t))
  1118. (goto-char (match-beginning 0))
  1119. ;; now that we've located the code block, it may be evaluated
  1120. (let ((org-babel-execute-src-block "RESULTS"))
  1121. (org-babel-execute-src-block))
  1122. (buffer-string)))))
  1123. (ert-deftest test-ob/location-of-header-arg-eval ()
  1124. "Test location of header argument evaluation."
  1125. (org-test-with-temp-text "
  1126. #+name: top-block
  1127. #+begin_src emacs-lisp :var pt=(point)
  1128. pt
  1129. #+end_src
  1130. #+name: bottom-block
  1131. #+begin_src emacs-lisp :var pt=top-block()
  1132. pt
  1133. #+end_src
  1134. "
  1135. ;; the value of the second block should be greater than the first
  1136. (should
  1137. (< (progn (re-search-forward org-babel-src-block-regexp nil t)
  1138. (goto-char (match-beginning 0))
  1139. (prog1 (save-match-data (org-babel-execute-src-block))
  1140. (goto-char (match-end 0))))
  1141. (progn (re-search-forward org-babel-src-block-regexp nil t)
  1142. (goto-char (match-beginning 0))
  1143. (org-babel-execute-src-block))))))
  1144. (ert-deftest test-ob/preserve-results-indentation ()
  1145. "Preserve indentation when executing a src block."
  1146. (should
  1147. (equal '(2 2)
  1148. (org-test-with-temp-text
  1149. " #+begin_src emacs-lisp\n (+ 1 1)\n #+end_src"
  1150. (org-babel-execute-src-block)
  1151. (buffer-string)
  1152. (let ((case-fold-search t)) (search-forward "#+results:"))
  1153. ;; Check if both #+RESULTS: keyword and actual results are
  1154. ;; indented by 2 columns.
  1155. (list (org-get-indentation)
  1156. (progn (forward-line) (org-get-indentation)))))))
  1157. (ert-deftest test-ob/safe-header-args ()
  1158. "Detect safe and unsafe header args."
  1159. (let ((safe-args '((:cache . "foo")
  1160. (:results . "output")
  1161. (:eval . "never")
  1162. (:eval . "query")))
  1163. (unsafe-args '((:eval . "yes")
  1164. (:results . "output file")
  1165. (:foo . "bar")))
  1166. (malformed-args '((bar . "foo")
  1167. ("foo" . "bar")
  1168. :foo))
  1169. (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
  1170. (dolist (arg safe-args)
  1171. (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
  1172. (dolist (arg unsafe-args)
  1173. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1174. (dolist (arg malformed-args)
  1175. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1176. (should (not (funcall safe-p (append safe-args unsafe-args))))))
  1177. (ert-deftest test-ob/noweb-expansions-in-cache ()
  1178. "Ensure that noweb expansions are expanded before caching."
  1179. (let ((noweb-expansions-in-cache-var 0))
  1180. (org-test-with-temp-text "
  1181. #+name: foo
  1182. #+begin_src emacs-lisp
  1183. \"I said\"
  1184. #+end_src
  1185. #+name: bar
  1186. #+begin_src emacs-lisp :noweb yes :cache yes
  1187. (setq noweb-expansions-in-cache-var
  1188. (+ 1 noweb-expansions-in-cache-var))
  1189. (concat <<foo>> \" check noweb expansions\")
  1190. #+end_src
  1191. "
  1192. ;; run the second block to create the cache
  1193. (goto-char (point-min))
  1194. (re-search-forward (regexp-quote "#+name: bar"))
  1195. (should (string= "I said check noweb expansions"
  1196. (org-babel-execute-src-block)))
  1197. (should (= noweb-expansions-in-cache-var 1))
  1198. ;; change the value of the first block
  1199. (goto-char (point-min))
  1200. (re-search-forward (regexp-quote "said"))
  1201. (goto-char (match-beginning 0))
  1202. (insert "haven't ")
  1203. (re-search-forward (regexp-quote "#+name: bar"))
  1204. (should (string= "I haven't said check noweb expansions"
  1205. (org-babel-execute-src-block)))
  1206. (should (= noweb-expansions-in-cache-var 2)))))
  1207. (ert-deftest test-org-babel/file-ext-and-output-dir ()
  1208. (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
  1209. (org-babel-next-src-block)
  1210. (should (equal "file-ext-basic.txt"
  1211. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1212. (org-babel-next-src-block)
  1213. (should (equal "foo/file-ext-dir-relative.txt"
  1214. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1215. (org-babel-next-src-block)
  1216. (should (equal "foo/file-ext-dir-relative-slash.txt"
  1217. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1218. (org-babel-next-src-block)
  1219. (should (equal "/tmp/file-ext-dir-absolute.txt"
  1220. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1221. (org-babel-next-src-block)
  1222. (should (equal "foo.bar"
  1223. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1224. (org-babel-next-src-block)
  1225. (should (equal "xxx/foo.bar"
  1226. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1227. ))
  1228. (ert-deftest test-org-babel/script-escape ()
  1229. ;; Delimited lists of numbers
  1230. (should (equal '(1 2 3)
  1231. (org-babel-script-escape "[1 2 3]")))
  1232. (should (equal '(1 2 3)
  1233. (org-babel-script-escape "{1 2 3}")))
  1234. (should (equal '(1 2 3)
  1235. (org-babel-script-escape "(1 2 3)")))
  1236. ;; Delimited lists of double-quoted strings
  1237. (should (equal '("foo" "bar")
  1238. (org-babel-script-escape "(\"foo\" \"bar\")")))
  1239. (should (equal '("foo" "bar")
  1240. (org-babel-script-escape "[\"foo\" \"bar\"]")))
  1241. (should (equal '("foo" "bar")
  1242. (org-babel-script-escape "{\"foo\" \"bar\"}")))
  1243. ;; ... with commas
  1244. (should (equal '("foo" "bar")
  1245. (org-babel-script-escape "(\"foo\", \"bar\")")))
  1246. (should (equal '("foo" "bar")
  1247. (org-babel-script-escape "[\"foo\", \"bar\"]")))
  1248. (should (equal '("foo" "bar")
  1249. (org-babel-script-escape "{\"foo\", \"bar\"}")))
  1250. ;; Delimited lists of single-quoted strings
  1251. (should (equal '("foo" "bar")
  1252. (org-babel-script-escape "('foo' 'bar')")))
  1253. (should (equal '("foo" "bar")
  1254. (org-babel-script-escape "['foo' 'bar']")))
  1255. (should (equal '("foo" "bar")
  1256. (org-babel-script-escape "{'foo' 'bar'}")))
  1257. ;; ... with commas
  1258. (should (equal '("foo" "bar")
  1259. (org-babel-script-escape "('foo', 'bar')")))
  1260. (should (equal '("foo" "bar")
  1261. (org-babel-script-escape "['foo', 'bar']")))
  1262. (should (equal '("foo" "bar")
  1263. (org-babel-script-escape "{'foo', 'bar'}")))
  1264. ;; Single quoted strings
  1265. (should (equal "foo"
  1266. (org-babel-script-escape "'foo'")))
  1267. ;; ... with internal double quote
  1268. (should (equal "foo\"bar"
  1269. (org-babel-script-escape "'foo\"bar'")))
  1270. ;; ... with internal backslash
  1271. (should (equal "foo\\bar"
  1272. (org-babel-script-escape "'foo\\bar'")))
  1273. ;; ... with internal escaped backslash
  1274. (should (equal "foo\\bar"
  1275. (org-babel-script-escape "'foo\\\\bar'")))
  1276. ;; ... with internal backslash-double quote
  1277. (should (equal "foo\\\"bar"
  1278. (org-babel-script-escape "'foo\\\"bar'")))
  1279. ;; ... with internal escaped backslash-double quote
  1280. (should (equal "foo\\\"bar"
  1281. (org-babel-script-escape "'foo\\\\\"bar'")))
  1282. ;; ... with internal escaped single quote
  1283. (should (equal "foo'bar"
  1284. (org-babel-script-escape "'foo\\'bar'")))
  1285. ;; ... with internal escaped backslash-escaped single quote
  1286. (should (equal "foo\\'bar"
  1287. (org-babel-script-escape "'foo\\\\\\'bar'")))
  1288. ;; Double quoted strings
  1289. (should (equal "foo"
  1290. (org-babel-script-escape "\"foo\"")))
  1291. ;; ... with internal single quote
  1292. (should (equal "foo'bar"
  1293. (org-babel-script-escape "\"foo'bar\"")))
  1294. ;; ... with internal backslash
  1295. (should (equal "foo\\bar"
  1296. (org-babel-script-escape "\"foo\\bar\"")))
  1297. ;; ... with internal escaped backslash
  1298. (should (equal "foo\\bar"
  1299. (org-babel-script-escape "\"foo\\\\bar\"")))
  1300. ;; ... with internal backslash-single quote
  1301. (should (equal "foo\\'bar"
  1302. (org-babel-script-escape "\"foo\\'bar\"")))
  1303. ;; ... with internal escaped backslash-single quote
  1304. (should (equal "foo\\'bar"
  1305. (org-babel-script-escape "\"foo\\\\'bar\"")))
  1306. ;; ... with internal escaped double quote
  1307. (should (equal "foo\"bar"
  1308. (org-babel-script-escape "\"foo\\\"bar\"")))
  1309. ;; ... with internal escaped backslash-escaped double quote
  1310. (should (equal "foo\\\"bar"
  1311. (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
  1312. (provide 'test-ob)
  1313. ;;; test-ob ends here