test-ob.el 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  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-ob/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= (expand-file-name "test.txt")
  30. (org-babel-execute-src-block))))))
  31. (ert-deftest test-ob/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-ob/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-ob/default-inline-header-args ()
  80. (should(equal
  81. '((:session . "none")
  82. (:results . "replace")
  83. (:exports . "results")
  84. (:hlines . "yes"))
  85. org-babel-default-inline-header-args)))
  86. (ert-deftest ob-test/org-babel-combine-header-arg-lists ()
  87. (let ((results (org-babel-combine-header-arg-lists
  88. '((foo . :any)
  89. (bar)
  90. (baz . ((foo bar) (baz)))
  91. (qux . ((foo bar baz qux)))
  92. (quux . ((foo bar))))
  93. '((bar)
  94. (baz . ((baz)))
  95. (quux . :any)))))
  96. (dolist (pair '((foo . :any)
  97. (bar)
  98. (baz . ((baz)))
  99. (quux . :any)
  100. (qux . ((foo bar baz qux)))))
  101. (should (equal (cdr pair)
  102. (cdr (assoc (car pair) results)))))))
  103. ;;; ob-get-src-block-info
  104. (ert-deftest test-ob/get-src-block-info-language ()
  105. (org-test-at-marker nil org-test-file-ob-anchor
  106. (let ((info (org-babel-get-src-block-info)))
  107. (should (string= "emacs-lisp" (nth 0 info))))))
  108. (ert-deftest test-ob/get-src-block-info-body ()
  109. (org-test-at-marker nil org-test-file-ob-anchor
  110. (let ((info (org-babel-get-src-block-info)))
  111. (should (string-match (regexp-quote org-test-file-ob-anchor)
  112. (nth 1 info))))))
  113. (ert-deftest test-ob/get-src-block-info-tangle ()
  114. (org-test-at-marker nil org-test-file-ob-anchor
  115. (let ((info (org-babel-get-src-block-info)))
  116. (should (string= "no" (cdr (assq :tangle (nth 2 info))))))))
  117. (ert-deftest test-ob/elisp-in-header-arguments ()
  118. "Test execution of elisp forms in header arguments."
  119. (org-test-with-temp-text-in-file "
  120. * elisp forms in header arguments
  121. :PROPERTIES:
  122. :header-args: :var prop = (* 7 6)
  123. :END:
  124. #+begin_src emacs-lisp
  125. prop
  126. #+end_src"
  127. (goto-char (point-min))
  128. (org-babel-next-src-block)
  129. (should (= 42 (org-babel-execute-src-block)))))
  130. (ert-deftest test-ob/simple-named-code-block ()
  131. "Test that simple named code blocks can be evaluated."
  132. (org-test-with-temp-text-in-file "
  133. #+name: i-have-a-name
  134. #+begin_src emacs-lisp
  135. 42
  136. #+end_src"
  137. (org-babel-next-src-block 1)
  138. (should (= 42 (org-babel-execute-src-block)))))
  139. (ert-deftest test-ob/simple-variable-resolution ()
  140. "Test that simple variable resolution is working."
  141. (org-test-with-temp-text-in-file "
  142. #+name: four
  143. #+begin_src emacs-lisp
  144. (list 1 2 3 4)
  145. #+end_src
  146. #+begin_src emacs-lisp :var four=four
  147. (length four)
  148. #+end_src"
  149. (org-babel-next-src-block 2)
  150. (should (= 4 (org-babel-execute-src-block)))
  151. (forward-line 5)
  152. (should (string= ": 4" (buffer-substring
  153. (point-at-bol)
  154. (point-at-eol))))))
  155. (ert-deftest test-ob/multi-line-header-arguments ()
  156. "Test that multi-line header arguments and can be read."
  157. (org-test-with-temp-text-in-file "
  158. #+headers: :var letters='(a b c d e f g)
  159. #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
  160. (require 'cl-lib)
  161. (cl-map 'list #'list numbers letters)
  162. #+end_src"
  163. (org-babel-next-src-block)
  164. (let ((results (org-babel-execute-src-block)))
  165. (should (eq 'a (cadr (assoc 1 results))))
  166. (should (eq 'd (cadr (assoc 4 results)))))))
  167. (ert-deftest test-ob/parse-header-args ()
  168. (org-test-with-temp-text-in-file "
  169. #+begin_src example-lang :session :results output :var num=9
  170. the body
  171. #+end_src"
  172. (org-babel-next-src-block)
  173. (let* ((info (org-babel-get-src-block-info))
  174. (params (nth 2 info)))
  175. (message "%S" params)
  176. (should (equal "example-lang" (nth 0 info)))
  177. (should (string= "the body" (org-trim (nth 1 info))))
  178. (should-not (member '(:session\ \ \ \ ) params))
  179. (should (equal '(:session) (assq :session params)))
  180. (should (equal '(:result-type . output) (assq :result-type params)))
  181. (should (equal '(num . 9) (cdr (assq :var params)))))))
  182. (ert-deftest test-ob/parse-header-args2 ()
  183. (org-test-with-temp-text-in-file "
  184. * resolving sub-trees as references
  185. #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
  186. (length text)
  187. #+end_src
  188. #+begin_src org :noweb yes
  189. <<simple-subtree>>
  190. <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
  191. #+end_src
  192. ** simple subtree with custom ID
  193. :PROPERTIES:
  194. :CUSTOM_ID: simple-subtree
  195. :END:
  196. this is simple"
  197. (should (string-match (regexp-quote "this is simple")
  198. (org-babel-ref-resolve "simple-subtree")))
  199. (org-babel-next-src-block)
  200. (should (= 14 (org-babel-execute-src-block)))))
  201. (ert-deftest test-ob/inline-src-blocks ()
  202. (should
  203. (= 1
  204. (org-test-with-temp-text
  205. "In the middle <point>src_emacs-lisp{(+ 0 1)} of a line"
  206. (org-babel-execute-src-block))))
  207. (should
  208. (= 2
  209. (org-test-with-temp-text
  210. "One at the end of a line: <point>src_emacs-lisp{(+ 1 1)}"
  211. (org-babel-execute-src-block))))
  212. (should
  213. (= 3
  214. (org-test-with-temp-text
  215. "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
  216. (org-babel-execute-src-block))))
  217. (should
  218. (= 4
  219. (org-test-with-temp-text
  220. "In the middle <point>src_emacs-lisp[:results silent\
  221. :exports code]{(+ 3 1)} of a line"
  222. (org-babel-execute-src-block))))
  223. (should
  224. (= 5
  225. (org-test-with-temp-text
  226. "One at the end of a line: <point>src_emacs-lisp[:results silent\
  227. :exports code]{(+ 4 1)}"
  228. (org-babel-execute-src-block))))
  229. (should
  230. (= 6
  231. (org-test-with-temp-text
  232. "src_emacs-lisp[:results silent :exports code]{(+ 5 1)}\
  233. at the beginning of a line."
  234. (org-babel-execute-src-block))))
  235. (should
  236. (= 7
  237. (org-test-with-temp-text
  238. "One also evaluated: <point>src_emacs-lisp[:exports both\
  239. :results silent]{(+ 6 1)}"
  240. (org-babel-execute-src-block)))))
  241. (ert-deftest test-ob/inline-src_blk-default-results-replace-line-1 ()
  242. (let ((test-line "src_sh{echo 1}")
  243. (org-babel-inline-result-wrap "=%s="))
  244. ;; src_ at bol line 1...
  245. (org-test-with-temp-text
  246. test-line
  247. (goto-char (point-min)) (org-babel-execute-maybe)
  248. (should (string=
  249. (concat test-line " {{{results(=1=)}}}")
  250. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  251. (forward-char) (org-babel-execute-maybe)
  252. (should (string=
  253. (concat test-line " {{{results(=1=)}}}")
  254. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  255. (re-search-forward "{{{")
  256. ;;(should-error (org-ctrl-c-ctrl-c))
  257. (backward-char 4) ;; last char of block body
  258. (org-babel-execute-maybe)
  259. (should (string=
  260. (concat test-line " {{{results(=1=)}}}")
  261. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  262. ;; src_ follows space line 1...
  263. (let ((test-line " src_emacs-lisp{ 1 }"))
  264. (org-test-with-temp-text
  265. test-line
  266. (should-error (org-ctrl-c-ctrl-c))
  267. (forward-char) (org-babel-execute-maybe)
  268. (should (string=
  269. (concat test-line " {{{results(=1=)}}}")
  270. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  271. (re-search-forward "{ 1 ") (org-babel-execute-maybe)
  272. (should (string=
  273. (concat test-line " {{{results(=1=)}}}")
  274. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  275. (forward-char 6)
  276. (should-error (org-ctrl-c-ctrl-c))))
  277. ;; Results on a subsequent line are replaced.
  278. (should
  279. (equal
  280. "src_emacs-lisp{(+ 1 2)}\n {{{results(=3=)}}}"
  281. (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n {{{results(=2=)}}}"
  282. (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
  283. (buffer-string))))
  284. ;; Also handle results at the beginning of a line.
  285. (should
  286. (equal
  287. "src_emacs-lisp{(+ 1 2)}\n{{{results(=3=)}}}"
  288. (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n{{{results(=2=)}}}"
  289. (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
  290. (buffer-string))))))
  291. (ert-deftest test-ob/inline-src_blk-default-results-replace-line-2 ()
  292. ;; src_ at bol line 2...
  293. (let ((test-line " src_emacs-lisp{ \"x\" }")
  294. (org-babel-inline-result-wrap "=%s="))
  295. (org-test-with-temp-text
  296. (concat "\n" test-line)
  297. (should-error (org-ctrl-c-ctrl-c))
  298. (goto-char (point-min))
  299. (should-error (org-ctrl-c-ctrl-c))
  300. (forward-line)
  301. (should-error (org-ctrl-c-ctrl-c))
  302. (forward-char) (org-babel-execute-maybe)
  303. (should (string=
  304. (concat test-line " {{{results(=x=)}}}")
  305. (buffer-substring-no-properties
  306. (point-at-bol) (point-at-eol))))))
  307. (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
  308. (org-babel-inline-result-wrap "=%s="))
  309. (org-test-with-temp-text
  310. test-line
  311. (goto-char (point-max))
  312. (insert (concat "\n" test-line " end"))
  313. (re-search-backward "src") (org-babel-execute-maybe)
  314. (should (string=
  315. (concat test-line " {{{results(=y=)}}} end")
  316. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  317. (re-search-forward "\" ") (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. (forward-char 3)
  322. (should-error (org-ctrl-c-ctrl-c)))))
  323. (ert-deftest test-ob/inline-src_blk-manual-results-replace ()
  324. (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
  325. (org-babel-inline-result-wrap "=%s="))
  326. (org-test-with-temp-text
  327. (concat "\n" test-line)
  328. (should-error (org-ctrl-c-ctrl-c))
  329. (goto-char (point-max))
  330. (org-babel-execute-maybe)
  331. (beginning-of-line)
  332. (should-error (org-ctrl-c-ctrl-c))
  333. (forward-char) (org-babel-execute-maybe)
  334. (should (string=
  335. (concat test-line " {{{results(=x=)}}}")
  336. (buffer-substring-no-properties
  337. (point-at-bol) (point-at-eol))))))
  338. (let ((test-line (concat " Some text prior to block "
  339. "src_emacs-lisp[:results replace]{ \"y\" }"))
  340. (org-babel-inline-result-wrap "=%s="))
  341. (org-test-with-temp-text test-line
  342. (goto-char (point-max))
  343. (insert (concat "\n" test-line " end"))
  344. (re-search-backward "src") (org-babel-execute-maybe)
  345. (should (string=
  346. (concat test-line " {{{results(=y=)}}} end")
  347. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  348. (re-search-forward "\" ") (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. (forward-char 3)
  353. (should-error (org-ctrl-c-ctrl-c)))))
  354. (ert-deftest test-ob/inline-src_blk-results-silent ()
  355. (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
  356. (org-test-with-temp-text test-line
  357. (org-babel-execute-maybe)
  358. (should (string= test-line
  359. (buffer-substring-no-properties
  360. (point-at-bol) (point-at-eol))))))
  361. (let ((test-line (concat " Some text prior to block src_emacs-lisp"
  362. "[ :results silent ]{ \"y\" }")))
  363. (org-test-with-temp-text
  364. test-line
  365. (goto-char (point-max))
  366. (insert (concat "\n" test-line " end"))
  367. (re-search-backward "src_") (org-babel-execute-maybe)
  368. (should (string= (concat test-line " end")
  369. (buffer-substring-no-properties
  370. (point-at-bol) (point-at-eol))))
  371. (re-search-forward "\" ") (org-babel-execute-maybe)
  372. (should (string= (concat test-line " end")
  373. (buffer-substring-no-properties
  374. (point-at-bol) (point-at-eol))))
  375. (forward-char 2)
  376. (should-error (org-ctrl-c-ctrl-c)))))
  377. (ert-deftest test-ob/inline-src_blk-results-raw ()
  378. (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
  379. (org-test-with-temp-text test-line
  380. (org-babel-execute-maybe)
  381. (should (string= (concat test-line " x")
  382. (buffer-string)))))
  383. (let ((test-line (concat " Some text prior to block "
  384. "src_emacs-lisp[ :results raw ]{ \"the\" }")))
  385. (org-test-with-temp-text (concat test-line " end")
  386. (re-search-forward "src_") (org-babel-execute-maybe)
  387. (should (string= (concat test-line " the end")
  388. (buffer-substring-no-properties
  389. (point-at-bol) (point-at-eol))))
  390. (re-search-forward "\" ") (org-babel-execute-maybe)
  391. (should (string= (concat test-line " the the end")
  392. (buffer-substring-no-properties
  393. (point-at-bol) (point-at-eol))))
  394. (forward-char 2)
  395. (should-error (org-ctrl-c-ctrl-c)))))
  396. (ert-deftest test-ob/inline-src_blk-results-file ()
  397. (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
  398. (org-test-with-temp-text
  399. test-line
  400. (org-babel-execute-maybe)
  401. (should (string= (concat test-line " {{{results([[file:~/test-file]])}}}")
  402. (buffer-substring-no-properties
  403. (point-min) (point-max)))))))
  404. (ert-deftest test-ob/inline-src_blk-results-scalar ()
  405. (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
  406. (org-babel-inline-result-wrap "=%s="))
  407. (org-test-with-temp-text
  408. test-line
  409. (org-babel-execute-maybe)
  410. (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
  411. (buffer-substring-no-properties
  412. (point-min) (point-max)))))))
  413. (ert-deftest test-ob/inline-src_blk-results-verbatim ()
  414. (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  415. (org-babel-inline-result-wrap "=%s="))
  416. (org-test-with-temp-text
  417. test-line
  418. (org-babel-execute-maybe)
  419. (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
  420. (buffer-substring-no-properties
  421. (point-min) (point-max)))))))
  422. (ert-deftest test-ob/combining-scalar-and-raw-result-types ()
  423. (org-test-with-temp-text-in-file "
  424. #+begin_src sh :results scalar
  425. echo \"[[file:./cv.cls]]\"
  426. #+end_src
  427. #+name:
  428. : [[file:./cv.cls]]
  429. #+begin_src sh :results raw scalar
  430. echo \"[[file:./cv.cls]]\"
  431. #+end_src
  432. "
  433. (cl-flet ((next-result ()
  434. (org-babel-next-src-block)
  435. (org-babel-execute-src-block)
  436. (goto-char (org-babel-where-is-src-block-result))
  437. (forward-line 1)))
  438. (goto-char (point-min))
  439. (next-result)
  440. (should (eq (org-element-type (org-element-at-point)) 'fixed-width))
  441. (next-result)
  442. (should-not
  443. (eq (org-element-type (org-element-at-point)) 'fixed-width)))))
  444. (ert-deftest test-ob/no-defaut-value-for-var ()
  445. "Test that the absence of a default value for a variable DOES THROW
  446. a proper error."
  447. (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
  448. (org-babel-next-src-block)
  449. (let ((err
  450. (should-error (org-babel-execute-src-block) :type 'error)))
  451. (should
  452. (equal
  453. '(error
  454. "Variable \"x\" must be assigned a default value")
  455. err)))))
  456. (ert-deftest test-ob/just-one-results-block ()
  457. "Test that evaluating two times the same code block does not result in a
  458. duplicate results block."
  459. (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
  460. (org-babel-execute-src-block)
  461. (org-babel-execute-src-block) ; second code block execution
  462. (should (search-forward "Hello")) ; the string inside the source code block
  463. (should (search-forward "Hello")) ; the same string in the results block
  464. (should-error (search-forward "Hello"))))
  465. (ert-deftest test-ob/nested-code-block ()
  466. "Test nested code blocks inside code blocks don't cause problems."
  467. (should
  468. (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
  469. (org-test-with-temp-text "#+begin_src org :results silent
  470. ,#+begin_src emacs-lisp
  471. 'foo
  472. ,#+end_src
  473. #+end_src"
  474. (let ((org-edit-src-content-indentation 2)
  475. (org-src-preserve-indentation nil))
  476. (org-babel-execute-src-block))))))
  477. (ert-deftest test-ob/partial-nested-code-block ()
  478. "Test nested code blocks inside code blocks don't cause problems."
  479. (org-test-with-temp-text "#+begin_src org :results silent
  480. ,#+begin_src emacs-lisp
  481. #+end_src"
  482. (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
  483. (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
  484. (org-test-with-temp-text "#+NAME: foo
  485. #+BEGIN_SRC emacs-lisp
  486. 'foo
  487. #+END_SRC\n"
  488. (org-babel-next-src-block 1)
  489. (should (eq 'foo (org-babel-execute-src-block)))
  490. (goto-char (point-min))
  491. (org-babel-next-src-block 1)
  492. (should (looking-at org-babel-src-block-regexp))))
  493. (ert-deftest test-ob/catches-all-references ()
  494. (org-test-with-temp-text "
  495. #+NAME: literal-example
  496. #+BEGIN_EXAMPLE
  497. A literal example
  498. on two lines
  499. #+END_EXAMPLE
  500. #+NAME: read-literal-example
  501. #+BEGIN_SRC emacs-lisp :var x=literal-example
  502. (concatenate 'string x \" for me.\")
  503. #+END_SRC"
  504. (org-babel-next-src-block 1)
  505. (should (string= (org-babel-execute-src-block)
  506. "A literal example\non two lines\n for me."))))
  507. (ert-deftest test-ob/ignore-reference-in-commented-headings ()
  508. (should
  509. (= 2
  510. (org-test-with-temp-text
  511. "
  512. * COMMENT H1
  513. #+NAME: n
  514. : 1
  515. * H2
  516. #+NAME: n
  517. : 2
  518. * Code
  519. <point>#+BEGIN_SRC emacs-lisp :var x=n
  520. x
  521. #+END_SRC"
  522. (org-babel-execute-src-block)))))
  523. (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
  524. (org-test-with-temp-text "
  525. #+name: base_plus
  526. | 1 |
  527. | 2 |
  528. #+name: 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. (cl-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. (ert-deftest test-ob/org-babel-insert-result ()
  682. "Test `org-babel-insert-result' specifications."
  683. ;; Do not error when output is an improper list.
  684. (should
  685. (org-test-with-temp-text
  686. "
  687. <point>#+BEGIN_SRC emacs-lisp
  688. '((1 . nil) (2 . 3))
  689. #+END_SRC
  690. "
  691. (org-babel-execute-maybe) t))
  692. ;; Escape headlines when producing an example block.
  693. (should
  694. (string-match-p
  695. ",\\* Not an headline"
  696. (org-test-with-temp-text
  697. "
  698. <point>#+BEGIN_SRC emacs-lisp
  699. \"* Not an headline\"
  700. #+END_SRC
  701. "
  702. (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
  703. (buffer-string))))
  704. ;; Escape special syntax in example blocks.
  705. (should
  706. (string-match-p
  707. ",#\\+END_SRC"
  708. (org-test-with-temp-text
  709. "
  710. <point>#+BEGIN_SRC emacs-lisp
  711. \"#+END_SRC\"
  712. #+END_SRC
  713. "
  714. (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
  715. (buffer-string))))
  716. ;; No escaping is done with other blocks or raw type.
  717. (should-not
  718. (string-match-p
  719. ",\\* Not an headline"
  720. (org-test-with-temp-text
  721. "
  722. <point>#+BEGIN_SRC emacs-lisp
  723. \"* Not an headline\"
  724. #+END_SRC
  725. "
  726. (let ((org-babel-min-lines-for-block-output 10))
  727. (org-babel-execute-maybe))
  728. (buffer-string))))
  729. (should-not
  730. (string-match-p
  731. ",\\* Not an headline"
  732. (org-test-with-temp-text
  733. "
  734. <point>#+BEGIN_SRC emacs-lisp :results raw
  735. \"* Not an headline\"
  736. #+END_SRC
  737. "
  738. (org-babel-execute-maybe)
  739. (buffer-string))))
  740. (should-not
  741. (string-match-p
  742. ",\\* Not an headline"
  743. (org-test-with-temp-text
  744. "
  745. <point>#+BEGIN_SRC emacs-lisp :results drawer
  746. \"* Not an headline\"
  747. #+END_SRC
  748. "
  749. (org-babel-execute-maybe)
  750. (buffer-string)))))
  751. (ert-deftest test-ob/remove-inline-result ()
  752. "Test `org-babel-remove-inline-result' honors whitespace."
  753. (let*
  754. ((inline-sb "src_emacs-lisp{(+ 1 2)}")
  755. (inline-res " {{{results(=3=)}}}")
  756. (inline-sb-dot (concat inline-sb "."))
  757. (inline-sb-res-dot (concat inline-sb inline-res ".")))
  758. (org-test-with-temp-text
  759. ;; Insert inline_src_block followed by dot.
  760. inline-sb-dot
  761. ;; Insert result before dot.
  762. (org-babel-execute-maybe)
  763. (should (string= inline-sb-res-dot
  764. (buffer-substring-no-properties
  765. (point-at-bol) (point-at-eol))))
  766. ;; Delete whitespace and result.
  767. (org-babel-remove-inline-result)
  768. (should (string= inline-sb-dot
  769. (buffer-substring-no-properties
  770. (point-at-bol) (point-at-eol))))
  771. ;; Add whitespace and result before dot.
  772. (search-forward inline-sb)
  773. (insert " " inline-res)
  774. (goto-char (point-at-bol))
  775. ;; Remove whitespace and result.
  776. (org-babel-remove-inline-result)
  777. (should (string= inline-sb-dot
  778. (buffer-substring-no-properties
  779. (point-at-bol) (point-at-eol))))
  780. ;; Add whitespace before dot.
  781. (search-forward inline-sb)
  782. (insert " ")
  783. (goto-char (point-at-bol))
  784. ;; Add result before whitespace.
  785. (org-babel-execute-maybe)
  786. ;; Remove result - leave trailing whitespace and dot.
  787. (org-babel-remove-inline-result)
  788. (should (string= (concat inline-sb " .")
  789. (buffer-substring-no-properties
  790. (point-at-bol) (point-at-eol)))))))
  791. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  792. "Test helper function to test `org-babel-remove-result'.
  793. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  794. and the result of execution is verified against RESULT.
  795. The block is actually executed /twice/ to ensure result
  796. replacement happens correctly."
  797. (org-test-with-temp-text
  798. buffer-text
  799. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  800. (should (re-search-forward "\\#\\+results:" nil t))
  801. (forward-line)
  802. (should (string= result
  803. (buffer-substring-no-properties
  804. (point-at-bol)
  805. (- (point-max) 16))))
  806. (org-babel-previous-src-block) (org-babel-remove-result)
  807. (should (string= buffer-text
  808. (buffer-substring-no-properties
  809. (point-min) (point-max))))))
  810. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  811. "Test `org-babel-remove-result' with default :results."
  812. (mapcar (lambda (language)
  813. (test-ob-verify-result-and-removed-result
  814. "\n"
  815. (concat
  816. "* org-babel-remove-result
  817. #+begin_src " language "
  818. #+end_src
  819. * next heading")))
  820. '("sh" "emacs-lisp")))
  821. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  822. "Test `org-babel-remove-result' with :results list."
  823. (test-ob-verify-result-and-removed-result
  824. "- 1
  825. - 2
  826. - 3
  827. - (quote (4 5))"
  828. "* org-babel-remove-result
  829. #+begin_src emacs-lisp :results list
  830. '(1 2 3 '(4 5))
  831. #+end_src
  832. * next heading"))
  833. (ert-deftest test-ob/org-babel-results-indented-wrap ()
  834. "Ensure that wrapped results are inserted correction when indented.
  835. If not inserted correctly then the second evaluation will fail
  836. trying to find the :END: marker."
  837. (org-test-with-temp-text
  838. "- indented
  839. #+begin_src sh :results file wrap
  840. echo test.txt
  841. #+end_src"
  842. (org-babel-next-src-block 1)
  843. (org-babel-execute-src-block)
  844. (org-babel-execute-src-block)))
  845. (ert-deftest test-ob/file-desc-header-argument ()
  846. "Test that the :file-desc header argument is used."
  847. (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  848. \"foo\"
  849. #+end_src
  850. #+begin_src emacs-lisp :results file :file-desc
  851. \"foo\"
  852. #+end_src"
  853. (org-babel-execute-src-block)
  854. (org-babel-next-src-block 1)
  855. (org-babel-execute-src-block)
  856. (goto-char (point-min))
  857. (should (search-forward "[[file:foo][bar]]" nil t))
  858. (should (search-forward "[[file:foo][foo]]" nil t))))
  859. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  860. "Test `org-babel-remove-result' with :results pp."
  861. (test-ob-verify-result-and-removed-result
  862. ": \"I /am/ working!\""
  863. "* org-babel-remove-result
  864. #+begin_src emacs-lisp :results pp
  865. \"I /am/ working!\")
  866. #+end_src
  867. * next heading"))
  868. (ert-deftest test-ob/inline-src_blk-preceded-punct-preceded-by-point ()
  869. (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  870. (org-babel-inline-result-wrap "=%s="))
  871. (org-test-with-temp-text
  872. test-line
  873. (forward-char 1)
  874. (org-babel-execute-maybe)
  875. (should (re-search-forward "=\"x\"=" nil t))
  876. (forward-line))))
  877. (ert-deftest test-ob/commented-last-block-line-with-var ()
  878. (org-test-with-temp-text-in-file "
  879. #+begin_src emacs-lisp :var a=1
  880. ;;
  881. #+end_src"
  882. (org-babel-next-src-block)
  883. (org-babel-execute-maybe)
  884. (re-search-forward "\\#\\+results:" nil t)
  885. (forward-line)
  886. (should (string=
  887. ""
  888. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  889. (org-test-with-temp-text-in-file "
  890. #+begin_src emacs-lisp :var a=2
  891. 2;;
  892. #+end_src"
  893. (org-babel-next-src-block)
  894. (org-babel-execute-maybe)
  895. (re-search-forward "\\#\\+results:" nil t)
  896. (forward-line)
  897. (should (string=
  898. ": 2"
  899. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  900. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  901. "Test helper function to test `org-babel-remove-result'.
  902. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  903. and the result of execution is verified against RESULT.
  904. The block is actually executed /twice/ to ensure result
  905. replacement happens correctly."
  906. (org-test-with-temp-text
  907. buffer-text
  908. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  909. (should (re-search-forward "\\#\\+results:" nil t))
  910. (forward-line)
  911. (should (string= result
  912. (buffer-substring-no-properties
  913. (point-at-bol)
  914. (- (point-max) 16))))
  915. (org-babel-previous-src-block) (org-babel-remove-result)
  916. (should (string= buffer-text
  917. (buffer-substring-no-properties
  918. (point-min) (point-max))))))
  919. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  920. "Test `org-babel-remove-result' with default :results."
  921. (mapcar (lambda (language)
  922. (test-ob-verify-result-and-removed-result
  923. "\n"
  924. (concat
  925. "* org-babel-remove-result
  926. #+begin_src " language "
  927. #+end_src
  928. * next heading")))
  929. '("sh" "emacs-lisp")))
  930. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  931. "Test `org-babel-remove-result' with :results list."
  932. (test-ob-verify-result-and-removed-result
  933. "- 1
  934. - 2
  935. - 3
  936. - (quote (4 5))"
  937. "* org-babel-remove-result
  938. #+begin_src emacs-lisp :results list
  939. '(1 2 3 '(4 5))
  940. #+end_src
  941. * next heading"))
  942. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  943. "Test `org-babel-remove-result' with :results wrap."
  944. (test-ob-verify-result-and-removed-result
  945. ":RESULTS:
  946. hello there
  947. :END:"
  948. "* org-babel-remove-result
  949. #+begin_src emacs-lisp :results wrap
  950. \"hello there\"
  951. #+end_src
  952. * next heading"))
  953. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  954. "Test `org-babel-remove-result' with :results org."
  955. (test-ob-verify-result-and-removed-result
  956. "#+BEGIN_SRC org
  957. ,* heading
  958. ,** subheading
  959. content
  960. #+END_SRC"
  961. "* org-babel-remove-result
  962. #+begin_src emacs-lisp :results org
  963. \"* heading
  964. ,** subheading
  965. content\"
  966. #+end_src
  967. * next heading"))
  968. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  969. "Test `org-babel-remove-result' with :results html."
  970. (test-ob-verify-result-and-removed-result
  971. "#+BEGIN_EXPORT html
  972. <head><body></body></head>
  973. #+END_EXPORT"
  974. "* org-babel-remove-result
  975. #+begin_src emacs-lisp :results html
  976. \"<head><body></body></head>\"
  977. #+end_src
  978. * next heading"))
  979. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  980. "Test `org-babel-remove-result' with :results latex."
  981. (test-ob-verify-result-and-removed-result
  982. "#+BEGIN_EXPORT latex
  983. Line 1
  984. Line 2
  985. Line 3
  986. #+END_EXPORT"
  987. "* org-babel-remove-result
  988. #+begin_src emacs-lisp :results latex
  989. \"Line 1
  990. Line 2
  991. Line 3\"
  992. #+end_src
  993. * next heading"))
  994. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  995. "Test `org-babel-remove-result' with :results code."
  996. (test-ob-verify-result-and-removed-result
  997. "#+BEGIN_SRC emacs-lisp
  998. \"I am working!\"
  999. #+END_SRC"
  1000. "* org-babel-remove-result
  1001. #+begin_src emacs-lisp :results code
  1002. (message \"I am working!\")
  1003. #+end_src
  1004. * next heading"))
  1005. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  1006. "Test `org-babel-remove-result' with :results pp."
  1007. (test-ob-verify-result-and-removed-result
  1008. ": \"I /am/ working!\""
  1009. "* org-babel-remove-result
  1010. #+begin_src emacs-lisp :results pp
  1011. \"I /am/ working!\")
  1012. #+end_src
  1013. * next heading"))
  1014. (ert-deftest test-ob/results-do-not-replace-code-blocks ()
  1015. (org-test-with-temp-text "Block two has a space after the name.
  1016. #+name: foo
  1017. #+begin_src emacs-lisp
  1018. 1
  1019. #+end_src
  1020. #+name: foo
  1021. #+begin_src emacs-lisp
  1022. 2
  1023. #+end_src
  1024. #+name: foo
  1025. #+begin_src emacs-lisp
  1026. 3
  1027. #+end_src
  1028. #+RESULTS: foo
  1029. : foo
  1030. "
  1031. (dolist (num '(1 2 3))
  1032. ;; execute the block
  1033. (goto-char (point-min))
  1034. (org-babel-next-src-block num) (org-babel-execute-src-block)
  1035. ;; check the results
  1036. (goto-char (point-max))
  1037. (move-beginning-of-line 0)
  1038. (should (looking-at (format ": %d" num))))))
  1039. (ert-deftest test-ob/blocks-with-spaces ()
  1040. "Test expansion of blocks followed by blank lines."
  1041. ;; Preserve number of blank lines after block.
  1042. (should
  1043. (equal "#+BEGIN_SRC emacs-lisp
  1044. \(+ 1 2)
  1045. #+END_SRC
  1046. #+RESULTS:
  1047. : 3\n\n\n"
  1048. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
  1049. \(+ 1 2)
  1050. #+END_SRC\n\n\n"
  1051. (let ((org-babel-next-src-block "RESULTS"))
  1052. (org-babel-execute-src-block))
  1053. (buffer-string))))
  1054. ;; Do not add spurious blank lines after results.
  1055. (should
  1056. (equal
  1057. "
  1058. - item 1
  1059. #+begin_src emacs-lisp
  1060. 0
  1061. #+end_src
  1062. #+RESULTS:
  1063. : 0
  1064. - item 2"
  1065. (org-test-with-temp-text "
  1066. - item 1
  1067. #+begin_src emacs-lisp<point>
  1068. 0
  1069. #+end_src
  1070. - item 2"
  1071. (org-babel-execute-src-block)
  1072. (buffer-string))))
  1073. (should
  1074. (equal
  1075. "
  1076. - item 1
  1077. #+begin_src emacs-lisp
  1078. 1
  1079. #+end_src
  1080. #+RESULTS:
  1081. : 1
  1082. - item 2"
  1083. (org-test-with-temp-text "
  1084. - item 1
  1085. #+begin_src emacs-lisp<point>
  1086. 1
  1087. #+end_src
  1088. #+RESULTS:
  1089. : 1
  1090. - item 2"
  1091. (org-babel-execute-src-block)
  1092. (buffer-string)))))
  1093. (ert-deftest test-ob/results-in-narrowed-buffer ()
  1094. "Test block execution in a narrowed buffer."
  1095. ;; If results don't exist, they should be inserted in visible part
  1096. ;; of the buffer.
  1097. (should
  1098. (equal
  1099. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
  1100. (org-test-with-temp-text
  1101. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1102. (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
  1103. (let ((org-babel-results-keyword "RESULTS"))
  1104. (org-babel-execute-src-block))
  1105. (org-trim (buffer-string)))))
  1106. (should
  1107. (equal
  1108. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
  1109. (org-test-with-temp-text
  1110. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1111. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1112. (let ((org-babel-results-keyword "RESULTS"))
  1113. (org-babel-execute-src-block))
  1114. (org-trim (buffer-string)))))
  1115. ;; Results in visible part of buffer, should be updated here.
  1116. (should
  1117. (equal
  1118. "#+NAME: test
  1119. #+BEGIN_SRC emacs-lisp
  1120. \(+ 1 2)
  1121. #+END_SRC
  1122. #+RESULTS: test
  1123. : 3"
  1124. (org-test-with-temp-text
  1125. "#+NAME: test
  1126. #+BEGIN_SRC emacs-lisp
  1127. \(+ 1 2)
  1128. #+END_SRC
  1129. #+RESULTS: test
  1130. : 4
  1131. Paragraph"
  1132. (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
  1133. (let ((org-babel-results-keyword "RESULTS"))
  1134. (org-babel-execute-src-block))
  1135. (org-trim (buffer-string)))))
  1136. ;; Results in invisible part of buffer, should be updated there.
  1137. (org-test-with-temp-text
  1138. "#+NAME: test
  1139. #+BEGIN_SRC emacs-lisp
  1140. \(+ 1 2)
  1141. #+END_SRC
  1142. #+RESULTS: test
  1143. : 4
  1144. Paragraph"
  1145. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1146. (let ((org-babel-results-keyword "RESULTS"))
  1147. (org-babel-execute-src-block))
  1148. (should-not (re-search-forward "^#\\+RESULTS:" nil t))
  1149. (widen)
  1150. (should (re-search-forward "^: 3" nil t))))
  1151. (ert-deftest test-ob/specific-colnames ()
  1152. "Test passing specific column names."
  1153. (should
  1154. (equal "#+name: input-table
  1155. | id | var1 |
  1156. |----+------|
  1157. | 1 | bar |
  1158. | 2 | baz |
  1159. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1160. echo \"$data\"
  1161. #+end_src
  1162. #+RESULTS:
  1163. | Rev | Author |
  1164. |-----+--------|
  1165. | 1 | bar |
  1166. | 2 | baz |
  1167. "
  1168. (org-test-with-temp-text
  1169. "#+name: input-table
  1170. | id | var1 |
  1171. |----+------|
  1172. | 1 | bar |
  1173. | 2 | baz |
  1174. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1175. echo \"$data\"
  1176. #+end_src
  1177. "
  1178. ;; we should find a code block
  1179. (should (re-search-forward org-babel-src-block-regexp nil t))
  1180. (goto-char (match-beginning 0))
  1181. ;; now that we've located the code block, it may be evaluated
  1182. (let ((org-babel-execute-src-block "RESULTS"))
  1183. (org-babel-execute-src-block))
  1184. (buffer-string)))))
  1185. (ert-deftest test-ob/location-of-header-arg-eval ()
  1186. "Test location of header argument evaluation."
  1187. (org-test-with-temp-text "
  1188. #+name: top-block
  1189. #+begin_src emacs-lisp :var pt=(point)
  1190. pt
  1191. #+end_src
  1192. #+name: bottom-block
  1193. #+begin_src emacs-lisp :var pt=top-block()
  1194. pt
  1195. #+end_src
  1196. "
  1197. ;; the value of the second block should be greater than the first
  1198. (should
  1199. (< (progn (re-search-forward org-babel-src-block-regexp nil t)
  1200. (goto-char (match-beginning 0))
  1201. (prog1 (save-match-data (org-babel-execute-src-block))
  1202. (goto-char (match-end 0))))
  1203. (progn (re-search-forward org-babel-src-block-regexp nil t)
  1204. (goto-char (match-beginning 0))
  1205. (org-babel-execute-src-block))))))
  1206. (ert-deftest test-ob/preserve-results-indentation ()
  1207. "Preserve indentation when executing a src block."
  1208. (should
  1209. (equal
  1210. '(2 2)
  1211. (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1212. (org-babel-execute-src-block)
  1213. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1214. (list (org-get-indentation)
  1215. (progn (forward-line) (org-get-indentation))))))
  1216. (should
  1217. (equal
  1218. '(2 2)
  1219. (org-test-with-temp-text
  1220. " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1221. (org-babel-execute-src-block)
  1222. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1223. (list (org-get-indentation)
  1224. (progn (forward-line) (org-get-indentation))))))
  1225. ;; Don't get fooled by TAB-based indentation.
  1226. (should
  1227. (equal
  1228. '(6 6)
  1229. (org-test-with-temp-text
  1230. "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
  1231. (setq tab-width 4)
  1232. (org-babel-execute-src-block)
  1233. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1234. (list (org-get-indentation)
  1235. (progn (forward-line) (org-get-indentation)))))))
  1236. (ert-deftest test-ob/safe-header-args ()
  1237. "Detect safe and unsafe header args."
  1238. (let ((safe-args '((:cache . "foo")
  1239. (:results . "output")
  1240. (:eval . "never")
  1241. (:eval . "query")))
  1242. (unsafe-args '((:eval . "yes")
  1243. (:results . "output file")
  1244. (:foo . "bar")))
  1245. (malformed-args '((bar . "foo")
  1246. ("foo" . "bar")
  1247. :foo))
  1248. (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
  1249. (dolist (arg safe-args)
  1250. (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
  1251. (dolist (arg unsafe-args)
  1252. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1253. (dolist (arg malformed-args)
  1254. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1255. (should (not (funcall safe-p (append safe-args unsafe-args))))))
  1256. (ert-deftest test-ob/noweb-expansions-in-cache ()
  1257. "Ensure that noweb expansions are expanded before caching."
  1258. (let ((noweb-expansions-in-cache-var 0))
  1259. (org-test-with-temp-text "
  1260. #+name: foo
  1261. #+begin_src emacs-lisp
  1262. \"I said\"
  1263. #+end_src
  1264. #+name: bar
  1265. #+begin_src emacs-lisp :noweb yes :cache yes
  1266. (setq noweb-expansions-in-cache-var
  1267. (+ 1 noweb-expansions-in-cache-var))
  1268. (concat <<foo>> \" check noweb expansions\")
  1269. #+end_src
  1270. "
  1271. ;; run the second block to create the cache
  1272. (goto-char (point-min))
  1273. (re-search-forward (regexp-quote "#+name: bar"))
  1274. (should (string= "I said check noweb expansions"
  1275. (org-babel-execute-src-block)))
  1276. (should (= noweb-expansions-in-cache-var 1))
  1277. ;; change the value of the first block
  1278. (goto-char (point-min))
  1279. (re-search-forward (regexp-quote "said"))
  1280. (goto-char (match-beginning 0))
  1281. (insert "haven't ")
  1282. (re-search-forward (regexp-quote "#+name: bar"))
  1283. (should (string= "I haven't said check noweb expansions"
  1284. (org-babel-execute-src-block)))
  1285. (should (= noweb-expansions-in-cache-var 2)))))
  1286. (ert-deftest test-ob/file-ext-and-output-dir ()
  1287. (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
  1288. (org-babel-next-src-block)
  1289. (should (equal "file-ext-basic.txt"
  1290. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1291. (org-babel-next-src-block)
  1292. (should (equal "foo/file-ext-dir-relative.txt"
  1293. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1294. (org-babel-next-src-block)
  1295. (should (equal "foo/file-ext-dir-relative-slash.txt"
  1296. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1297. (org-babel-next-src-block)
  1298. (should (equal "/tmp/file-ext-dir-absolute.txt"
  1299. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1300. (org-babel-next-src-block)
  1301. (should (equal "foo.bar"
  1302. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1303. (org-babel-next-src-block)
  1304. (should (equal "xxx/foo.bar"
  1305. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1306. ))
  1307. (ert-deftest test-ob/script-escape ()
  1308. ;; Delimited lists of numbers
  1309. (should (equal '(1 2 3)
  1310. (org-babel-script-escape "[1 2 3]")))
  1311. (should (equal '(1 2 3)
  1312. (org-babel-script-escape "{1 2 3}")))
  1313. (should (equal '(1 2 3)
  1314. (org-babel-script-escape "(1 2 3)")))
  1315. ;; Delimited lists of double-quoted strings
  1316. (should (equal '("foo" "bar")
  1317. (org-babel-script-escape "(\"foo\" \"bar\")")))
  1318. (should (equal '("foo" "bar")
  1319. (org-babel-script-escape "[\"foo\" \"bar\"]")))
  1320. (should (equal '("foo" "bar")
  1321. (org-babel-script-escape "{\"foo\" \"bar\"}")))
  1322. ;; ... with commas
  1323. (should (equal '("foo" "bar")
  1324. (org-babel-script-escape "(\"foo\", \"bar\")")))
  1325. (should (equal '("foo" "bar")
  1326. (org-babel-script-escape "[\"foo\", \"bar\"]")))
  1327. (should (equal '("foo" "bar")
  1328. (org-babel-script-escape "{\"foo\", \"bar\"}")))
  1329. ;; Delimited lists of single-quoted strings
  1330. (should (equal '("foo" "bar")
  1331. (org-babel-script-escape "('foo' 'bar')")))
  1332. (should (equal '("foo" "bar")
  1333. (org-babel-script-escape "['foo' 'bar']")))
  1334. (should (equal '("foo" "bar")
  1335. (org-babel-script-escape "{'foo' 'bar'}")))
  1336. ;; ... with commas
  1337. (should (equal '("foo" "bar")
  1338. (org-babel-script-escape "('foo', 'bar')")))
  1339. (should (equal '("foo" "bar")
  1340. (org-babel-script-escape "['foo', 'bar']")))
  1341. (should (equal '("foo" "bar")
  1342. (org-babel-script-escape "{'foo', 'bar'}")))
  1343. ;; Single quoted strings
  1344. (should (equal "foo"
  1345. (org-babel-script-escape "'foo'")))
  1346. ;; ... with internal double quote
  1347. (should (equal "foo\"bar"
  1348. (org-babel-script-escape "'foo\"bar'")))
  1349. ;; ... with internal backslash
  1350. (should (equal "foo\\bar"
  1351. (org-babel-script-escape "'foo\\bar'")))
  1352. ;; ... with internal escaped backslash
  1353. (should (equal "foo\\bar"
  1354. (org-babel-script-escape "'foo\\\\bar'")))
  1355. ;; ... with internal backslash-double quote
  1356. (should (equal "foo\\\"bar"
  1357. (org-babel-script-escape "'foo\\\"bar'")))
  1358. ;; ... with internal escaped backslash-double quote
  1359. (should (equal "foo\\\"bar"
  1360. (org-babel-script-escape "'foo\\\\\"bar'")))
  1361. ;; ... with internal escaped single quote
  1362. (should (equal "foo'bar"
  1363. (org-babel-script-escape "'foo\\'bar'")))
  1364. ;; ... with internal escaped backslash-escaped single quote
  1365. (should (equal "foo\\'bar"
  1366. (org-babel-script-escape "'foo\\\\\\'bar'")))
  1367. ;; Double quoted strings
  1368. (should (equal "foo"
  1369. (org-babel-script-escape "\"foo\"")))
  1370. ;; ... with internal single quote
  1371. (should (equal "foo'bar"
  1372. (org-babel-script-escape "\"foo'bar\"")))
  1373. ;; ... with internal backslash
  1374. (should (equal "foo\\bar"
  1375. (org-babel-script-escape "\"foo\\bar\"")))
  1376. ;; ... with internal escaped backslash
  1377. (should (equal "foo\\bar"
  1378. (org-babel-script-escape "\"foo\\\\bar\"")))
  1379. ;; ... with internal backslash-single quote
  1380. (should (equal "foo\\'bar"
  1381. (org-babel-script-escape "\"foo\\'bar\"")))
  1382. ;; ... with internal escaped backslash-single quote
  1383. (should (equal "foo\\'bar"
  1384. (org-babel-script-escape "\"foo\\\\'bar\"")))
  1385. ;; ... with internal escaped double quote
  1386. (should (equal "foo\"bar"
  1387. (org-babel-script-escape "\"foo\\\"bar\"")))
  1388. ;; ... with internal escaped backslash-escaped double quote
  1389. (should (equal "foo\\\"bar"
  1390. (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
  1391. (ert-deftest test-ob/process-params-no-duplicates ()
  1392. (should
  1393. (equal (org-babel-process-params '((:colname-names)
  1394. (:rowname-names)
  1395. (:result-params)
  1396. (:result-type)
  1397. (:var . "\"foo\"")))
  1398. '((:var)
  1399. (:colname-names)
  1400. (:rowname-names)
  1401. (:result-params)
  1402. (:result-type . value)))))
  1403. (defun org-test-babel-confirm-evaluate (eval-value)
  1404. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  1405. nil
  1406. #+end_src" eval-value)
  1407. (goto-char (point-min))
  1408. (let ((info (org-babel-get-src-block-info)))
  1409. (org-babel-check-confirm-evaluate info))))
  1410. (ert-deftest test-ob/check-eval ()
  1411. (let ((org-confirm-babel-evaluate t))
  1412. ;; Non-export tests
  1413. (dolist (pair '(("no" . nil)
  1414. ("never" . nil)
  1415. ("query" . query)
  1416. ("yes" . query)))
  1417. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1418. ;; Export tests
  1419. (let ((org-babel-exp-reference-buffer t))
  1420. (dolist (pair '(("no" . nil)
  1421. ("never" . nil)
  1422. ("query" . query)
  1423. ("yes" . query)
  1424. ("never-export" . nil)
  1425. ("no-export" . nil)
  1426. ("query-export" . query)))
  1427. (message (car pair))
  1428. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))))
  1429. (let ((org-confirm-babel-evaluate nil))
  1430. ;; Non-export tests
  1431. (dolist (pair '(("no" . nil)
  1432. ("never" . nil)
  1433. ("query" . query)
  1434. ("yes" . t)))
  1435. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1436. ;; Export tests
  1437. (let ((org-babel-exp-reference-buffer t))
  1438. (dolist (pair '(("no" . nil)
  1439. ("never" . nil)
  1440. ("query" . query)
  1441. ("yes" . t)
  1442. ("never-export" . nil)
  1443. ("no-export" . nil)
  1444. ("query-export" . query)))
  1445. (message (car pair))
  1446. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair)))))))
  1447. (defun org-test-ob/update-block-body ()
  1448. "Test `org-babel-update-block-body' specifications."
  1449. (should
  1450. (equal "#+begin_src elisp\n 2\n#+end_src"
  1451. (let ((org-edit-src-content-indentation 2))
  1452. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1453. (org-babel-update-block-body "2")
  1454. (buffer-string)))))
  1455. ;; Preserve block indentation.
  1456. (should
  1457. (equal " #+begin_src elisp\n 2\n #+end_src"
  1458. (let ((org-edit-src-content-indentation 1))
  1459. (org-test-with-temp-text
  1460. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1461. (org-babel-update-block-body "2")
  1462. (buffer-string)))))
  1463. ;; Ignore NEW-BODY global indentation.
  1464. (should
  1465. (equal "#+begin_src elisp\n 2\n#+end_src"
  1466. (let ((org-edit-src-content-indentation 2))
  1467. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1468. (org-babel-update-block-body " 2")
  1469. (buffer-string)))))
  1470. ;; When indentation should be preserved ignore the two rules above.
  1471. (should
  1472. (equal " #+begin_src elisp\n2\n #+end_src"
  1473. (let ((org-edit-src-content-indentation 1)
  1474. (org-src-preserve-indentation t))
  1475. (org-test-with-temp-text
  1476. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1477. (org-babel-update-block-body "2")
  1478. (buffer-string)))))
  1479. (should
  1480. (equal " #+begin_src elisp -i\n2\n #+end_src"
  1481. (let ((org-edit-src-content-indentation 1))
  1482. (org-test-with-temp-text
  1483. " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
  1484. (org-babel-update-block-body "2")
  1485. (buffer-string)))))
  1486. (should
  1487. (equal "#+begin_src elisp\n 2\n#+end_src"
  1488. (let ((org-edit-src-content-indentation 2)
  1489. (org-src-preserve-indentation t))
  1490. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1491. (org-babel-update-block-body " 2")
  1492. (buffer-string)))))
  1493. (should
  1494. (equal "#+begin_src elisp -i\n 2\n#+end_src"
  1495. (let ((org-edit-src-content-indentation 2)
  1496. (org-src-preserve-indentation t))
  1497. (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
  1498. (org-babel-update-block-body " 2")
  1499. (buffer-string))))))
  1500. (ert-deftest test-ob/find-named-result ()
  1501. "Test `org-babel-find-named-result' specifications."
  1502. (should
  1503. (= 1
  1504. (org-test-with-temp-text "#+results: foo\n: result"
  1505. (org-babel-find-named-result "foo"))))
  1506. (should-not
  1507. (org-test-with-temp-text "#+results: foo\n: result"
  1508. (org-babel-find-named-result "bar")))
  1509. (should-not
  1510. (org-test-with-temp-text "#+results: foobar\n: result"
  1511. (org-babel-find-named-result "foo")))
  1512. ;; Search is case insensitive.
  1513. (should
  1514. (org-test-with-temp-text "#+RESULTS: FOO\n: result"
  1515. (org-babel-find-named-result "foo")))
  1516. ;; Handle hash in results keyword.
  1517. (should
  1518. (org-test-with-temp-text "#+results[hash]: FOO\n: result"
  1519. (org-babel-find-named-result "foo")))
  1520. ;; Accept orphaned affiliated keywords.
  1521. (should
  1522. (org-test-with-temp-text "#+results: foo"
  1523. (org-babel-find-named-result "foo"))))
  1524. (ert-deftest test-ob/where-is-src-block-result ()
  1525. "Test `org-babel-where-is-src-block-result' specifications."
  1526. ;; Find anonymous results.
  1527. (should
  1528. (equal "#+RESULTS:"
  1529. (org-test-with-temp-text
  1530. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
  1531. (goto-char (org-babel-where-is-src-block-result))
  1532. (buffer-substring-no-properties (point) (line-end-position)))))
  1533. ;; Find named results. Those have priority over anonymous ones.
  1534. (should
  1535. (equal "#+RESULTS: example"
  1536. (org-test-with-temp-text
  1537. "
  1538. <point>#+NAME: example
  1539. #+BEGIN_SRC emacs-lisp
  1540. \(+ 1 1)
  1541. #+END_SRC
  1542. #+RESULTS: example
  1543. : 2"
  1544. (goto-char (org-babel-where-is-src-block-result))
  1545. (buffer-substring-no-properties (point) (line-end-position)))))
  1546. (should
  1547. (equal "#+RESULTS: example"
  1548. (org-test-with-temp-text
  1549. "
  1550. <point>#+NAME: example
  1551. #+BEGIN_SRC emacs-lisp
  1552. \(+ 1 1)
  1553. #+END_SRC
  1554. #+RESULTS:
  1555. : fake
  1556. #+RESULTS: example
  1557. : 2"
  1558. (goto-char (org-babel-where-is-src-block-result))
  1559. (buffer-substring-no-properties (point) (line-end-position)))))
  1560. ;; Return nil when no result is found.
  1561. (should-not
  1562. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1563. (org-babel-where-is-src-block-result)))
  1564. (should-not
  1565. (org-test-with-temp-text
  1566. "- item\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n"
  1567. (org-babel-where-is-src-block-result)))
  1568. ;; When optional argument INSERT is non-nil, add RESULTS keyword
  1569. ;; whenever no RESULTS can be found.
  1570. (should
  1571. (equal
  1572. "#+RESULTS:"
  1573. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1574. (let ((org-babel-results-keyword "RESULTS"))
  1575. (goto-char (org-babel-where-is-src-block-result t)))
  1576. (buffer-substring-no-properties (point) (line-end-position)))))
  1577. ;; Insert a named RESULTS keyword if possible.
  1578. (should
  1579. (equal
  1580. "#+RESULTS: e"
  1581. (org-test-with-temp-text
  1582. "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1583. (let ((org-babel-results-keyword "RESULTS"))
  1584. (goto-char (org-babel-where-is-src-block-result t)))
  1585. (buffer-substring-no-properties (point) (line-end-position)))))
  1586. ;; When optional argument HASH is provided, clear RESULTS keyword
  1587. ;; and related contents if they do not match it.
  1588. (should
  1589. (equal
  1590. "#+RESULTS[bbbb]:"
  1591. (org-test-with-temp-text
  1592. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 3"
  1593. (let ((org-babel-results-keyword "RESULTS"))
  1594. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1595. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1596. (should
  1597. (equal
  1598. "#+RESULTS[bbbb]:"
  1599. (org-test-with-temp-text
  1600. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
  1601. (let ((org-babel-results-keyword "RESULTS"))
  1602. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1603. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1604. ;; Handle hashes with times.
  1605. (should
  1606. (equal
  1607. "#+RESULTS[<2014-03-04 00:41:10> bbbb]:"
  1608. (org-test-with-temp-text
  1609. "
  1610. <point>#+BEGIN_SRC emacs-lisp
  1611. \(+ 1 1)
  1612. #+END_SRC
  1613. #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1614. (let ((org-babel-results-keyword "RESULTS")
  1615. (org-babel-hash-show-time t))
  1616. (cl-letf (((symbol-function 'format-time-string)
  1617. (lambda (&rest _) "<2014-03-04 00:41:10>")))
  1618. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb"))
  1619. (org-trim (buffer-substring-no-properties (point) (point-max))))))))
  1620. (should
  1621. (equal
  1622. "#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1623. (org-test-with-temp-text
  1624. "
  1625. <point>#+BEGIN_SRC emacs-lisp
  1626. \(+ 1 1)
  1627. #+END_SRC
  1628. #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1629. (let ((org-babel-results-keyword "RESULTS")
  1630. (org-babel-hash-show-time t))
  1631. (cl-letf (((symbol-function 'format-time-string)
  1632. (lambda (&rest _) "<2014-03-04 00:41:10>")))
  1633. (goto-char (org-babel-where-is-src-block-result nil nil "aaaa"))
  1634. (org-trim (buffer-substring-no-properties (point) (point-max))))))))
  1635. ;; RESULTS keyword may not be the last affiliated keyword.
  1636. (should
  1637. (equal
  1638. "#+RESULTS[bbbb]:"
  1639. (org-test-with-temp-text
  1640. "
  1641. <point>#+BEGIN_SRC emacs-lisp
  1642. \(+ 1 1)
  1643. #+END_SRC
  1644. #+RESULTS[aaaa]:
  1645. #+NAME: e
  1646. : 3"
  1647. (let ((org-babel-results-keyword "RESULTS"))
  1648. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1649. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1650. ;; HASH does nothing if no RESULTS can be found. However, if INSERT
  1651. ;; is also non-nil, RESULTS keyword is inserted along with the
  1652. ;; expected hash.
  1653. (should
  1654. (equal
  1655. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1656. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1657. (org-babel-where-is-src-block-result nil nil "bbbb")
  1658. (buffer-string))))
  1659. (should
  1660. (equal
  1661. "#+RESULTS[bbbb]:"
  1662. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1663. (let ((org-babel-results-keyword "RESULTS"))
  1664. (goto-char (org-babel-where-is-src-block-result t nil "bbbb")))
  1665. (org-trim (buffer-substring-no-properties (point) (point-max)))))))
  1666. (ert-deftest test-ob/goto-named-src-block ()
  1667. "Test interactive use of `org-babel-goto-named-src-block'."
  1668. (org-test-with-temp-text-in-file
  1669. "
  1670. #+NAME: abc
  1671. #+BEGIN_SRC emacs-lisp :results value
  1672. (1+ 1)
  1673. #+END_SRC
  1674. #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
  1675. #+BEGIN_SRC emacs-lisp
  1676. <<abc>>
  1677. #+END_SRC
  1678. abc
  1679. #+RESULTS: abc
  1680. : 2
  1681. "
  1682. ;; non-existent name
  1683. (should-not
  1684. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
  1685. ;; correct name
  1686. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
  1687. (should (= 14 (point)))
  1688. ;; call line - autocompletion
  1689. (forward-line 3)
  1690. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1691. (should (= 14 (point)))
  1692. ;; noweb reference - autocompletion
  1693. (forward-line 5)
  1694. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1695. (should (= 14 (point)))
  1696. ;; at symbol - autocompletion
  1697. (forward-line 7)
  1698. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1699. (should (= 14 (point)))
  1700. ;; in results - autocompletion
  1701. (forward-line 8)
  1702. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1703. (should (= 14 (point)))
  1704. (forward-line 9)
  1705. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1706. (should (= 14 (point)))))
  1707. (ert-deftest test-ob/evaluate-body-with-coderefs ()
  1708. (should
  1709. (= 2
  1710. (org-test-with-temp-text
  1711. "#+begin_src emacs-lisp -l \"#(ref:%s)\"\n2 #(ref:foo)\n#+end_src"
  1712. (org-babel-execute-src-block))))
  1713. (should
  1714. (= 3
  1715. (org-test-with-temp-text
  1716. "#+begin_src emacs-lisp\n3 #(ref:foo)\n#+end_src"
  1717. (let ((org-coderef-label-format "#(ref:%s)"))
  1718. (org-babel-execute-src-block))))))
  1719. (provide 'test-ob)
  1720. ;;; test-ob ends here