test-ob.el 43 KB

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