test-ob.el 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  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= (concat default-directory "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 (assoc :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. :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. (let ((info (org-babel-get-src-block-info)))
  130. (should (= 42 (org-babel-execute-src-block))))))
  131. (ert-deftest test-ob/simple-named-code-block ()
  132. "Test that simple named code blocks can be evaluated."
  133. (org-test-with-temp-text-in-file "
  134. #+name: i-have-a-name
  135. #+begin_src emacs-lisp
  136. 42
  137. #+end_src"
  138. (org-babel-next-src-block 1)
  139. (should (= 42 (org-babel-execute-src-block)))))
  140. (ert-deftest test-ob/simple-variable-resolution ()
  141. "Test that simple variable resolution is working."
  142. (org-test-with-temp-text-in-file "
  143. #+name: four
  144. #+begin_src emacs-lisp
  145. (list 1 2 3 4)
  146. #+end_src
  147. #+begin_src emacs-lisp :var four=four
  148. (length four)
  149. #+end_src"
  150. (org-babel-next-src-block 2)
  151. (should (= 4 (org-babel-execute-src-block)))
  152. (forward-line 5)
  153. (should (string= ": 4" (buffer-substring
  154. (point-at-bol)
  155. (point-at-eol))))))
  156. (ert-deftest test-ob/multi-line-header-arguments ()
  157. "Test that multi-line header arguments and can be read."
  158. (org-test-with-temp-text-in-file "
  159. #+headers: :var letters='(a b c d e f g)
  160. #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
  161. (require 'cl)
  162. (defalias 'my-map (if (org-version-check \"24.2.50\" \"cl\" :predicate)
  163. 'cl-map
  164. 'map))
  165. (my-map 'list #'list numbers letters)
  166. #+end_src"
  167. (org-babel-next-src-block)
  168. (let ((results (org-babel-execute-src-block)))
  169. (should(equal 'a (cadr (assoc 1 results))))
  170. (should(equal 'd (cadr (assoc 4 results)))))))
  171. (ert-deftest test-ob/parse-header-args ()
  172. (org-test-with-temp-text-in-file "
  173. #+begin_src example-lang :session :results output :var num=9
  174. the body
  175. #+end_src"
  176. (org-babel-next-src-block)
  177. (let* ((info (org-babel-get-src-block-info))
  178. (params (nth 2 info)))
  179. (message "%S" params)
  180. (should (equal "example-lang" (nth 0 info)))
  181. (should (string= "the body" (org-babel-trim (nth 1 info))))
  182. (should-not (member '(:session\ \ \ \ ) params))
  183. (should (equal '(:session) (assoc :session params)))
  184. (should (equal '(:result-type . output) (assoc :result-type params)))
  185. (should (equal '(num . 9) (cdr (assoc :var params)))))))
  186. (ert-deftest test-ob/parse-header-args2 ()
  187. (org-test-with-temp-text-in-file "
  188. * resolving sub-trees as references
  189. #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
  190. (length text)
  191. #+end_src
  192. #+begin_src org :noweb yes
  193. <<simple-subtree>>
  194. <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
  195. #+end_src
  196. ** simple subtree with custom ID
  197. :PROPERTIES:
  198. :CUSTOM_ID: simple-subtree
  199. :END:
  200. this is simple"
  201. (should (string-match (regexp-quote "this is simple")
  202. (org-babel-ref-resolve "simple-subtree")))
  203. (org-babel-next-src-block)
  204. (should (= 14 (org-babel-execute-src-block)))))
  205. (ert-deftest test-ob/inline-src-blocks ()
  206. (should
  207. (= 1
  208. (org-test-with-temp-text
  209. "In the middle <point>src_emacs-lisp{(+ 0 1)} of a line"
  210. (org-babel-execute-src-block))))
  211. (should
  212. (= 2
  213. (org-test-with-temp-text
  214. "One at the end of a line: <point>src_emacs-lisp{(+ 1 1)}"
  215. (org-babel-execute-src-block))))
  216. (should
  217. (= 3
  218. (org-test-with-temp-text
  219. "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
  220. (org-babel-execute-src-block))))
  221. (should
  222. (= 4
  223. (org-test-with-temp-text
  224. "In the middle <point>src_emacs-lisp[:results silent\
  225. :exports code]{(+ 3 1)} of a line"
  226. (org-babel-execute-src-block))))
  227. (should
  228. (= 5
  229. (org-test-with-temp-text
  230. "One at the end of a line: <point>src_emacs-lisp[:results silent\
  231. :exports code]{(+ 4 1)}"
  232. (org-babel-execute-src-block))))
  233. (should
  234. (= 6
  235. (org-test-with-temp-text
  236. "src_emacs-lisp[:results silent :exports code]{(+ 5 1)}\
  237. at the beginning of a line."
  238. (org-babel-execute-src-block))))
  239. (should
  240. (= 7
  241. (org-test-with-temp-text
  242. "One also evaluated: <point>src_emacs-lisp[:exports both\
  243. :results silent]{(+ 6 1)}"
  244. (org-babel-execute-src-block)))))
  245. (ert-deftest test-ob/inline-src_blk-default-results-replace-line-1 ()
  246. (let ((test-line "src_sh{echo 1}")
  247. (org-babel-inline-result-wrap "=%s="))
  248. ;; src_ at bol line 1...
  249. (org-test-with-temp-text
  250. test-line
  251. (goto-char (point-min)) (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. (forward-char) (org-babel-execute-maybe)
  256. (should (string=
  257. (concat test-line " {{{results(=1=)}}}")
  258. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  259. (re-search-forward "{{{")
  260. ;;(should-error (org-ctrl-c-ctrl-c))
  261. (backward-char 4) ;; last char of block body
  262. (org-babel-execute-maybe)
  263. (should (string=
  264. (concat test-line " {{{results(=1=)}}}")
  265. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  266. ;; src_ follows space line 1...
  267. (let ((test-line " src_emacs-lisp{ 1 }"))
  268. (org-test-with-temp-text
  269. test-line
  270. (should-error (org-ctrl-c-ctrl-c))
  271. (forward-char) (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. (re-search-forward "{ 1 ") (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. (forward-char 6)
  280. (should-error (org-ctrl-c-ctrl-c))))
  281. ;; Results on a subsequent line are replaced.
  282. (should
  283. (equal
  284. "src_emacs-lisp{(+ 1 2)}\n {{{results(=3=)}}}"
  285. (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n {{{results(=2=)}}}"
  286. (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
  287. (buffer-string))))
  288. ;; Also handle results at the beginning of a line.
  289. (should
  290. (equal
  291. "src_emacs-lisp{(+ 1 2)}\n{{{results(=3=)}}}"
  292. (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n{{{results(=2=)}}}"
  293. (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
  294. (buffer-string))))))
  295. (ert-deftest test-ob/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-ob/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-ob/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-ob/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-ob/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-ob/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-ob/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-ob/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. (cl-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 (eq (org-element-type (org-element-at-point)) 'fixed-width))
  445. (next-result)
  446. (should-not
  447. (eq (org-element-type (org-element-at-point)) 'fixed-width)))))
  448. (ert-deftest test-ob/no-defaut-value-for-var ()
  449. "Test that the absence of a default value for a variable DOES THROW
  450. a proper error."
  451. (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
  452. (org-babel-next-src-block)
  453. (let ((err
  454. (should-error (org-babel-execute-src-block) :type 'error)))
  455. (should
  456. (equal
  457. '(error
  458. "Variable \"x\" must be assigned a default value")
  459. err)))))
  460. (ert-deftest test-ob/just-one-results-block ()
  461. "Test that evaluating two times the same code block does not result in a
  462. duplicate results block."
  463. (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
  464. (org-babel-execute-src-block)
  465. (org-babel-execute-src-block) ; second code block execution
  466. (should (search-forward "Hello")) ; the string inside the source code block
  467. (should (search-forward "Hello")) ; the same string in the results block
  468. (should-error (search-forward "Hello"))))
  469. (ert-deftest test-ob/nested-code-block ()
  470. "Test nested code blocks inside code blocks don't cause problems."
  471. (should
  472. (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
  473. (org-test-with-temp-text "#+begin_src org :results silent
  474. ,#+begin_src emacs-lisp
  475. 'foo
  476. ,#+end_src
  477. #+end_src"
  478. (let ((org-edit-src-content-indentation 2)
  479. (org-src-preserve-indentation nil))
  480. (org-babel-execute-src-block))))))
  481. (ert-deftest test-ob/partial-nested-code-block ()
  482. "Test nested code blocks inside code blocks don't cause problems."
  483. (org-test-with-temp-text "#+begin_src org :results silent
  484. ,#+begin_src emacs-lisp
  485. #+end_src"
  486. (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
  487. (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
  488. (org-test-with-temp-text "#+NAME: foo
  489. #+BEGIN_SRC emacs-lisp
  490. 'foo
  491. #+END_SRC\n"
  492. (org-babel-next-src-block 1)
  493. (should (eq 'foo (org-babel-execute-src-block)))
  494. (goto-char (point-min))
  495. (org-babel-next-src-block 1)
  496. (should (looking-at org-babel-src-block-regexp))))
  497. (ert-deftest test-ob/catches-all-references ()
  498. (org-test-with-temp-text "
  499. #+NAME: literal-example
  500. #+BEGIN_EXAMPLE
  501. A literal example
  502. on two lines
  503. #+END_EXAMPLE
  504. #+NAME: read-literal-example
  505. #+BEGIN_SRC emacs-lisp :var x=literal-example
  506. (concatenate 'string x \" for me.\")
  507. #+END_SRC"
  508. (org-babel-next-src-block 1)
  509. (should (string= (org-babel-execute-src-block)
  510. "A literal example\non two lines\n for me."))))
  511. (ert-deftest test-ob/ignore-reference-in-commented-headings ()
  512. (should
  513. (= 2
  514. (org-test-with-temp-text
  515. "
  516. * COMMENT H1
  517. #+NAME: n
  518. : 1
  519. * H2
  520. #+NAME: n
  521. : 2
  522. * Code
  523. <point>#+BEGIN_SRC emacs-lisp :var x=n
  524. x
  525. #+END_SRC"
  526. (org-babel-execute-src-block)))))
  527. (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
  528. (org-test-with-temp-text "
  529. #+name: base_plus
  530. | 1 |
  531. | 2 |
  532. #+name: base
  533. | 3 |
  534. | 4 |
  535. #+begin_src emacs-lisp :var x=base
  536. x
  537. #+end_src"
  538. (org-babel-next-src-block 1)
  539. (should (equal (org-babel-execute-src-block) '((3) (4))))))
  540. (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
  541. (org-test-with-temp-text "
  542. #+name: base_plus
  543. #+begin_src emacs-lisp
  544. 'bar
  545. #+end_src
  546. #+name: base
  547. #+begin_src emacs-lisp
  548. 'foo
  549. #+end_src
  550. #+begin_src emacs-lisp :var x=base
  551. x
  552. #+end_src"
  553. (org-babel-next-src-block 3)
  554. (should (equal (org-babel-execute-src-block) "foo"))))
  555. (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
  556. (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
  557. (+ a b c d)
  558. #+end_src
  559. "
  560. (should (= 10 (org-babel-execute-src-block)))))
  561. (ert-deftest test-ob/org-babel-update-intermediate ()
  562. (org-test-with-temp-text "#+name: foo
  563. #+begin_src emacs-lisp
  564. 2
  565. #+end_src
  566. #+results: foo
  567. : 4
  568. #+begin_src emacs-lisp :var it=foo
  569. (+ it 1)
  570. #+end_src"
  571. (let ((org-babel-update-intermediate nil))
  572. (goto-char (point-min))
  573. (org-babel-next-src-block 2)
  574. (should (= 3 (org-babel-execute-src-block)))
  575. (goto-char (point-min))
  576. (forward-line 6)
  577. (should (looking-at ": 4")))
  578. (let ((org-babel-update-intermediate t))
  579. (goto-char (point-min))
  580. (org-babel-next-src-block 2)
  581. (should (= 3 (org-babel-execute-src-block)))
  582. (goto-char (point-min))
  583. (forward-line 6)
  584. (should (looking-at ": 2")))))
  585. (ert-deftest test-ob/eval-header-argument ()
  586. (cl-flet ((check-eval (eval runp)
  587. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  588. (setq foo :evald)
  589. #+end_src" eval)
  590. (let ((foo :not-run))
  591. (if runp
  592. (progn (should (org-babel-execute-src-block))
  593. (should (eq foo :evald)))
  594. (progn (should-not (org-babel-execute-src-block))
  595. (should-not (eq foo :evald))))))))
  596. (check-eval "never" nil)
  597. (check-eval "no" nil)
  598. (check-eval "never-export" t)
  599. (check-eval "no-export" t)
  600. (let ((org-babel-exp-reference-buffer (current-buffer)))
  601. (check-eval "never" nil)
  602. (check-eval "no" nil)
  603. (check-eval "never-export" nil)
  604. (check-eval "no-export" nil))))
  605. (ert-deftest test-ob/noweb-expansion-1 ()
  606. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  607. <<foo>>
  608. #+end_src
  609. #+name: foo
  610. #+begin_src sh
  611. bar
  612. #+end_src"
  613. (should (string= (org-babel-expand-noweb-references) "bar"))))
  614. (ert-deftest test-ob/noweb-expansion-2 ()
  615. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  616. <<foo>>
  617. #+end_src
  618. #+name: foo
  619. #+begin_src sh :noweb-sep \"\"
  620. bar
  621. #+end_src
  622. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  623. baz
  624. #+end_src"
  625. (should (string= (org-babel-expand-noweb-references) "barbaz"))))
  626. (ert-deftest test-ob/splitting-variable-lists-in-references ()
  627. (org-test-with-temp-text ""
  628. (should (= 1 (length (org-babel-ref-split-args
  629. "a=\"this, no work\""))))
  630. (should (= 2 (length (org-babel-ref-split-args
  631. "a=\"this, no work\", b=1"))))))
  632. (ert-deftest test-ob/org-babel-balanced-split ()
  633. (should (equal
  634. '(":a 1" "b [2 3]" "c (4 :d (5 6))")
  635. (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
  636. '((32 9) . 58)))))
  637. (ert-deftest test-ob/commented-last-block-line-no-var ()
  638. (org-test-with-temp-text-in-file "
  639. #+begin_src emacs-lisp
  640. ;;
  641. #+end_src"
  642. (org-babel-next-src-block)
  643. (org-babel-execute-maybe)
  644. (should (re-search-forward "\\#\\+results:" nil t))
  645. (forward-line)
  646. (should
  647. (string=
  648. ""
  649. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  650. (org-test-with-temp-text-in-file "
  651. #+begin_src emacs-lisp
  652. \"some text\";;
  653. #+end_src"
  654. (org-babel-next-src-block)
  655. (org-babel-execute-maybe)
  656. (should (re-search-forward "\\#\\+results:" nil t))
  657. (forward-line)
  658. (should
  659. (string=
  660. ": some text"
  661. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  662. (ert-deftest test-ob/commented-last-block-line-with-var ()
  663. (org-test-with-temp-text-in-file "
  664. #+begin_src emacs-lisp :var a=1
  665. ;;
  666. #+end_src"
  667. (org-babel-next-src-block)
  668. (org-babel-execute-maybe)
  669. (re-search-forward "\\#\\+results:" nil t)
  670. (forward-line)
  671. (should (string=
  672. ""
  673. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  674. (org-test-with-temp-text-in-file "
  675. #+begin_src emacs-lisp :var a=2
  676. 2;;
  677. #+end_src"
  678. (org-babel-next-src-block)
  679. (org-babel-execute-maybe)
  680. (re-search-forward "\\#\\+results:" nil t)
  681. (forward-line)
  682. (should (string=
  683. ": 2"
  684. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  685. (ert-deftest test-ob/org-babel-insert-result--improper-lists ()
  686. "Test `org-babel-insert-result' with improper lists."
  687. ;; Do not error when output is an improper list.
  688. (should
  689. (org-test-with-temp-text
  690. "
  691. <point>#+BEGIN_SRC emacs-lisp
  692. '((1 . nil) (2 . 3))
  693. #+END_SRC
  694. "
  695. (org-babel-execute-maybe) t)))
  696. (ert-deftest test-ob/remove-inline-result ()
  697. "Test `org-babel-remove-inline-result' honors whitespace."
  698. (let*
  699. ((inline-sb "src_emacs-lisp{(+ 1 2)}")
  700. (inline-res " {{{results(=3=)}}}")
  701. (inline-sb-dot (concat inline-sb "."))
  702. (inline-sb-res-dot (concat inline-sb inline-res ".")))
  703. (org-test-with-temp-text
  704. ;; Insert inline_src_block followed by dot.
  705. inline-sb-dot
  706. ;; Insert result before dot.
  707. (org-babel-execute-maybe)
  708. (should (string= inline-sb-res-dot
  709. (buffer-substring-no-properties
  710. (point-at-bol) (point-at-eol))))
  711. ;; Delete whitespace and result.
  712. (org-babel-remove-inline-result)
  713. (should (string= inline-sb-dot
  714. (buffer-substring-no-properties
  715. (point-at-bol) (point-at-eol))))
  716. ;; Add whitespace and result before dot.
  717. (search-forward inline-sb)
  718. (insert " " inline-res)
  719. (goto-char (point-at-bol))
  720. ;; Remove whitespace and result.
  721. (org-babel-remove-inline-result)
  722. (should (string= inline-sb-dot
  723. (buffer-substring-no-properties
  724. (point-at-bol) (point-at-eol))))
  725. ;; Add whitespace before dot.
  726. (search-forward inline-sb)
  727. (insert " ")
  728. (goto-char (point-at-bol))
  729. ;; Add result before whitespace.
  730. (org-babel-execute-maybe)
  731. ;; Remove result - leave trailing whitespace and dot.
  732. (org-babel-remove-inline-result)
  733. (should (string= (concat inline-sb " .")
  734. (buffer-substring-no-properties
  735. (point-at-bol) (point-at-eol)))))))
  736. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  737. "Test helper function to test `org-babel-remove-result'.
  738. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  739. and the result of execution is verified against RESULT.
  740. The block is actually executed /twice/ to ensure result
  741. replacement happens correctly."
  742. (org-test-with-temp-text
  743. buffer-text
  744. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  745. (should (re-search-forward "\\#\\+results:" nil t))
  746. (forward-line)
  747. (should (string= result
  748. (buffer-substring-no-properties
  749. (point-at-bol)
  750. (- (point-max) 16))))
  751. (org-babel-previous-src-block) (org-babel-remove-result)
  752. (should (string= buffer-text
  753. (buffer-substring-no-properties
  754. (point-min) (point-max))))))
  755. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  756. "Test `org-babel-remove-result' with default :results."
  757. (mapcar (lambda (language)
  758. (test-ob-verify-result-and-removed-result
  759. "\n"
  760. (concat
  761. "* org-babel-remove-result
  762. #+begin_src " language "
  763. #+end_src
  764. * next heading")))
  765. '("sh" "emacs-lisp")))
  766. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  767. "Test `org-babel-remove-result' with :results list."
  768. (test-ob-verify-result-and-removed-result
  769. "- 1
  770. - 2
  771. - 3
  772. - (quote (4 5))"
  773. "* org-babel-remove-result
  774. #+begin_src emacs-lisp :results list
  775. '(1 2 3 '(4 5))
  776. #+end_src
  777. * next heading"))
  778. (ert-deftest test-ob/org-babel-results-indented-wrap ()
  779. "Ensure that wrapped results are inserted correction when indented.
  780. If not inserted correctly then the second evaluation will fail
  781. trying to find the :END: marker."
  782. (org-test-with-temp-text
  783. "- indented
  784. #+begin_src sh :results file wrap
  785. echo test.txt
  786. #+end_src"
  787. (org-babel-next-src-block 1)
  788. (org-babel-execute-src-block)
  789. (org-babel-execute-src-block)))
  790. (ert-deftest test-ob/file-desc-header-argument ()
  791. "Test that the :file-desc header argument is used."
  792. (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  793. \"foo\"
  794. #+end_src
  795. #+begin_src emacs-lisp :results file :file-desc
  796. \"foo\"
  797. #+end_src"
  798. (org-babel-execute-src-block)
  799. (org-babel-next-src-block 1)
  800. (org-babel-execute-src-block)
  801. (goto-char (point-min))
  802. (should (search-forward "[[file:foo][bar]]" nil t))
  803. (should (search-forward "[[file:foo][foo]]" nil t))))
  804. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  805. "Test `org-babel-remove-result' with :results pp."
  806. (test-ob-verify-result-and-removed-result
  807. ": \"I /am/ working!\""
  808. "* org-babel-remove-result
  809. #+begin_src emacs-lisp :results pp
  810. \"I /am/ working!\")
  811. #+end_src
  812. * next heading"))
  813. (ert-deftest test-ob/inline-src_blk-preceded-punct-preceded-by-point ()
  814. (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  815. (org-babel-inline-result-wrap "=%s="))
  816. (org-test-with-temp-text
  817. test-line
  818. (forward-char 1)
  819. (org-babel-execute-maybe)
  820. (should (re-search-forward "=\"x\"=" nil t))
  821. (forward-line))))
  822. (ert-deftest test-ob/commented-last-block-line-with-var ()
  823. (org-test-with-temp-text-in-file "
  824. #+begin_src emacs-lisp :var a=1
  825. ;;
  826. #+end_src"
  827. (org-babel-next-src-block)
  828. (org-babel-execute-maybe)
  829. (re-search-forward "\\#\\+results:" nil t)
  830. (forward-line)
  831. (should (string=
  832. ""
  833. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  834. (org-test-with-temp-text-in-file "
  835. #+begin_src emacs-lisp :var a=2
  836. 2;;
  837. #+end_src"
  838. (org-babel-next-src-block)
  839. (org-babel-execute-maybe)
  840. (re-search-forward "\\#\\+results:" nil t)
  841. (forward-line)
  842. (should (string=
  843. ": 2"
  844. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  845. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  846. "Test helper function to test `org-babel-remove-result'.
  847. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  848. and the result of execution is verified against RESULT.
  849. The block is actually executed /twice/ to ensure result
  850. replacement happens correctly."
  851. (org-test-with-temp-text
  852. buffer-text
  853. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  854. (should (re-search-forward "\\#\\+results:" nil t))
  855. (forward-line)
  856. (should (string= result
  857. (buffer-substring-no-properties
  858. (point-at-bol)
  859. (- (point-max) 16))))
  860. (org-babel-previous-src-block) (org-babel-remove-result)
  861. (should (string= buffer-text
  862. (buffer-substring-no-properties
  863. (point-min) (point-max))))))
  864. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  865. "Test `org-babel-remove-result' with default :results."
  866. (mapcar (lambda (language)
  867. (test-ob-verify-result-and-removed-result
  868. "\n"
  869. (concat
  870. "* org-babel-remove-result
  871. #+begin_src " language "
  872. #+end_src
  873. * next heading")))
  874. '("sh" "emacs-lisp")))
  875. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  876. "Test `org-babel-remove-result' with :results list."
  877. (test-ob-verify-result-and-removed-result
  878. "- 1
  879. - 2
  880. - 3
  881. - (quote (4 5))"
  882. "* org-babel-remove-result
  883. #+begin_src emacs-lisp :results list
  884. '(1 2 3 '(4 5))
  885. #+end_src
  886. * next heading"))
  887. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  888. "Test `org-babel-remove-result' with :results wrap."
  889. (test-ob-verify-result-and-removed-result
  890. ":RESULTS:
  891. hello there
  892. :END:"
  893. "* org-babel-remove-result
  894. #+begin_src emacs-lisp :results wrap
  895. \"hello there\"
  896. #+end_src
  897. * next heading"))
  898. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  899. "Test `org-babel-remove-result' with :results org."
  900. (test-ob-verify-result-and-removed-result
  901. "#+BEGIN_SRC org
  902. ,* heading
  903. ,** subheading
  904. content
  905. #+END_SRC"
  906. "* org-babel-remove-result
  907. #+begin_src emacs-lisp :results org
  908. \"* heading
  909. ,** subheading
  910. content\"
  911. #+end_src
  912. * next heading"))
  913. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  914. "Test `org-babel-remove-result' with :results html."
  915. (test-ob-verify-result-and-removed-result
  916. "#+BEGIN_EXPORT html
  917. <head><body></body></head>
  918. #+END_EXPORT"
  919. "* org-babel-remove-result
  920. #+begin_src emacs-lisp :results html
  921. \"<head><body></body></head>\"
  922. #+end_src
  923. * next heading"))
  924. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  925. "Test `org-babel-remove-result' with :results latex."
  926. (test-ob-verify-result-and-removed-result
  927. "#+BEGIN_EXPORT latex
  928. Line 1
  929. Line 2
  930. Line 3
  931. #+END_EXPORT"
  932. "* org-babel-remove-result
  933. #+begin_src emacs-lisp :results latex
  934. \"Line 1
  935. Line 2
  936. Line 3\"
  937. #+end_src
  938. * next heading"))
  939. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  940. "Test `org-babel-remove-result' with :results code."
  941. (test-ob-verify-result-and-removed-result
  942. "#+BEGIN_SRC emacs-lisp
  943. \"I am working!\"
  944. #+END_SRC"
  945. "* org-babel-remove-result
  946. #+begin_src emacs-lisp :results code
  947. (message \"I am working!\")
  948. #+end_src
  949. * next heading"))
  950. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  951. "Test `org-babel-remove-result' with :results pp."
  952. (test-ob-verify-result-and-removed-result
  953. ": \"I /am/ working!\""
  954. "* org-babel-remove-result
  955. #+begin_src emacs-lisp :results pp
  956. \"I /am/ working!\")
  957. #+end_src
  958. * next heading"))
  959. (ert-deftest test-ob/results-do-not-replace-code-blocks ()
  960. (org-test-with-temp-text "Block two has a space after the name.
  961. #+name: foo
  962. #+begin_src emacs-lisp
  963. 1
  964. #+end_src
  965. #+name: foo
  966. #+begin_src emacs-lisp
  967. 2
  968. #+end_src
  969. #+name: foo
  970. #+begin_src emacs-lisp
  971. 3
  972. #+end_src
  973. #+RESULTS: foo
  974. : foo
  975. "
  976. (dolist (num '(1 2 3))
  977. ;; execute the block
  978. (goto-char (point-min))
  979. (org-babel-next-src-block num) (org-babel-execute-src-block)
  980. ;; check the results
  981. (goto-char (point-max))
  982. (move-beginning-of-line 0)
  983. (should (looking-at (format ": %d" num))))))
  984. (ert-deftest test-ob/blocks-with-spaces ()
  985. "Test expansion of blocks followed by blank lines."
  986. (should
  987. (equal "#+BEGIN_SRC emacs-lisp
  988. \(+ 1 2)
  989. #+END_SRC
  990. #+RESULTS:
  991. : 3\n\n\n"
  992. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
  993. \(+ 1 2)
  994. #+END_SRC\n\n\n"
  995. (let ((org-babel-next-src-block "RESULTS"))
  996. (org-babel-execute-src-block))
  997. (buffer-string)))))
  998. (ert-deftest test-ob/results-in-narrowed-buffer ()
  999. "Test block execution in a narrowed buffer."
  1000. ;; If results don't exist, they should be inserted in visible part
  1001. ;; of the buffer.
  1002. (should
  1003. (equal
  1004. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
  1005. (org-test-with-temp-text
  1006. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1007. (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
  1008. (let ((org-babel-results-keyword "RESULTS"))
  1009. (org-babel-execute-src-block))
  1010. (org-trim (buffer-string)))))
  1011. (should
  1012. (equal
  1013. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
  1014. (org-test-with-temp-text
  1015. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1016. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1017. (let ((org-babel-results-keyword "RESULTS"))
  1018. (org-babel-execute-src-block))
  1019. (org-trim (buffer-string)))))
  1020. ;; Results in visible part of buffer, should be updated here.
  1021. (should
  1022. (equal
  1023. "#+NAME: test
  1024. #+BEGIN_SRC emacs-lisp
  1025. \(+ 1 2)
  1026. #+END_SRC
  1027. #+RESULTS: test
  1028. : 3"
  1029. (org-test-with-temp-text
  1030. "#+NAME: test
  1031. #+BEGIN_SRC emacs-lisp
  1032. \(+ 1 2)
  1033. #+END_SRC
  1034. #+RESULTS: test
  1035. : 4
  1036. Paragraph"
  1037. (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
  1038. (let ((org-babel-results-keyword "RESULTS"))
  1039. (org-babel-execute-src-block))
  1040. (org-trim (buffer-string)))))
  1041. ;; Results in invisible part of buffer, should be updated there.
  1042. (org-test-with-temp-text
  1043. "#+NAME: test
  1044. #+BEGIN_SRC emacs-lisp
  1045. \(+ 1 2)
  1046. #+END_SRC
  1047. #+RESULTS: test
  1048. : 4
  1049. Paragraph"
  1050. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1051. (let ((org-babel-results-keyword "RESULTS"))
  1052. (org-babel-execute-src-block))
  1053. (should-not (re-search-forward "^#\\+RESULTS:" nil t))
  1054. (widen)
  1055. (should (re-search-forward "^: 3" nil t))))
  1056. (ert-deftest test-ob/specific-colnames ()
  1057. "Test passing specific column names."
  1058. (should
  1059. (equal "#+name: input-table
  1060. | id | var1 |
  1061. |----+------|
  1062. | 1 | bar |
  1063. | 2 | baz |
  1064. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1065. echo \"$data\"
  1066. #+end_src
  1067. #+RESULTS:
  1068. | Rev | Author |
  1069. |-----+--------|
  1070. | 1 | bar |
  1071. | 2 | baz |
  1072. "
  1073. (org-test-with-temp-text
  1074. "#+name: input-table
  1075. | id | var1 |
  1076. |----+------|
  1077. | 1 | bar |
  1078. | 2 | baz |
  1079. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1080. echo \"$data\"
  1081. #+end_src
  1082. "
  1083. ;; we should find a code block
  1084. (should (re-search-forward org-babel-src-block-regexp nil t))
  1085. (goto-char (match-beginning 0))
  1086. ;; now that we've located the code block, it may be evaluated
  1087. (let ((org-babel-execute-src-block "RESULTS"))
  1088. (org-babel-execute-src-block))
  1089. (buffer-string)))))
  1090. (ert-deftest test-ob/location-of-header-arg-eval ()
  1091. "Test location of header argument evaluation."
  1092. (org-test-with-temp-text "
  1093. #+name: top-block
  1094. #+begin_src emacs-lisp :var pt=(point)
  1095. pt
  1096. #+end_src
  1097. #+name: bottom-block
  1098. #+begin_src emacs-lisp :var pt=top-block()
  1099. pt
  1100. #+end_src
  1101. "
  1102. ;; the value of the second block should be greater than the first
  1103. (should
  1104. (< (progn (re-search-forward org-babel-src-block-regexp nil t)
  1105. (goto-char (match-beginning 0))
  1106. (prog1 (save-match-data (org-babel-execute-src-block))
  1107. (goto-char (match-end 0))))
  1108. (progn (re-search-forward org-babel-src-block-regexp nil t)
  1109. (goto-char (match-beginning 0))
  1110. (org-babel-execute-src-block))))))
  1111. (ert-deftest test-ob/preserve-results-indentation ()
  1112. "Preserve indentation when executing a src block."
  1113. (should
  1114. (equal
  1115. '(2 2)
  1116. (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1117. (org-babel-execute-src-block)
  1118. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1119. (list (org-get-indentation)
  1120. (progn (forward-line) (org-get-indentation))))))
  1121. (should
  1122. (equal
  1123. '(2 2)
  1124. (org-test-with-temp-text
  1125. " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1126. (org-babel-execute-src-block)
  1127. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1128. (list (org-get-indentation)
  1129. (progn (forward-line) (org-get-indentation))))))
  1130. ;; Don't get fooled by TAB-based indentation.
  1131. (should
  1132. (equal
  1133. '(6 6)
  1134. (org-test-with-temp-text
  1135. "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
  1136. (setq tab-width 4)
  1137. (org-babel-execute-src-block)
  1138. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1139. (list (org-get-indentation)
  1140. (progn (forward-line) (org-get-indentation)))))))
  1141. (ert-deftest test-ob/safe-header-args ()
  1142. "Detect safe and unsafe header args."
  1143. (let ((safe-args '((:cache . "foo")
  1144. (:results . "output")
  1145. (:eval . "never")
  1146. (:eval . "query")))
  1147. (unsafe-args '((:eval . "yes")
  1148. (:results . "output file")
  1149. (:foo . "bar")))
  1150. (malformed-args '((bar . "foo")
  1151. ("foo" . "bar")
  1152. :foo))
  1153. (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
  1154. (dolist (arg safe-args)
  1155. (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
  1156. (dolist (arg unsafe-args)
  1157. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1158. (dolist (arg malformed-args)
  1159. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1160. (should (not (funcall safe-p (append safe-args unsafe-args))))))
  1161. (ert-deftest test-ob/noweb-expansions-in-cache ()
  1162. "Ensure that noweb expansions are expanded before caching."
  1163. (let ((noweb-expansions-in-cache-var 0))
  1164. (org-test-with-temp-text "
  1165. #+name: foo
  1166. #+begin_src emacs-lisp
  1167. \"I said\"
  1168. #+end_src
  1169. #+name: bar
  1170. #+begin_src emacs-lisp :noweb yes :cache yes
  1171. (setq noweb-expansions-in-cache-var
  1172. (+ 1 noweb-expansions-in-cache-var))
  1173. (concat <<foo>> \" check noweb expansions\")
  1174. #+end_src
  1175. "
  1176. ;; run the second block to create the cache
  1177. (goto-char (point-min))
  1178. (re-search-forward (regexp-quote "#+name: bar"))
  1179. (should (string= "I said check noweb expansions"
  1180. (org-babel-execute-src-block)))
  1181. (should (= noweb-expansions-in-cache-var 1))
  1182. ;; change the value of the first block
  1183. (goto-char (point-min))
  1184. (re-search-forward (regexp-quote "said"))
  1185. (goto-char (match-beginning 0))
  1186. (insert "haven't ")
  1187. (re-search-forward (regexp-quote "#+name: bar"))
  1188. (should (string= "I haven't said check noweb expansions"
  1189. (org-babel-execute-src-block)))
  1190. (should (= noweb-expansions-in-cache-var 2)))))
  1191. (ert-deftest test-ob/file-ext-and-output-dir ()
  1192. (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
  1193. (org-babel-next-src-block)
  1194. (should (equal "file-ext-basic.txt"
  1195. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1196. (org-babel-next-src-block)
  1197. (should (equal "foo/file-ext-dir-relative.txt"
  1198. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1199. (org-babel-next-src-block)
  1200. (should (equal "foo/file-ext-dir-relative-slash.txt"
  1201. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1202. (org-babel-next-src-block)
  1203. (should (equal "/tmp/file-ext-dir-absolute.txt"
  1204. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1205. (org-babel-next-src-block)
  1206. (should (equal "foo.bar"
  1207. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1208. (org-babel-next-src-block)
  1209. (should (equal "xxx/foo.bar"
  1210. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1211. ))
  1212. (ert-deftest test-ob/script-escape ()
  1213. ;; Delimited lists of numbers
  1214. (should (equal '(1 2 3)
  1215. (org-babel-script-escape "[1 2 3]")))
  1216. (should (equal '(1 2 3)
  1217. (org-babel-script-escape "{1 2 3}")))
  1218. (should (equal '(1 2 3)
  1219. (org-babel-script-escape "(1 2 3)")))
  1220. ;; Delimited lists of double-quoted strings
  1221. (should (equal '("foo" "bar")
  1222. (org-babel-script-escape "(\"foo\" \"bar\")")))
  1223. (should (equal '("foo" "bar")
  1224. (org-babel-script-escape "[\"foo\" \"bar\"]")))
  1225. (should (equal '("foo" "bar")
  1226. (org-babel-script-escape "{\"foo\" \"bar\"}")))
  1227. ;; ... with commas
  1228. (should (equal '("foo" "bar")
  1229. (org-babel-script-escape "(\"foo\", \"bar\")")))
  1230. (should (equal '("foo" "bar")
  1231. (org-babel-script-escape "[\"foo\", \"bar\"]")))
  1232. (should (equal '("foo" "bar")
  1233. (org-babel-script-escape "{\"foo\", \"bar\"}")))
  1234. ;; Delimited lists of single-quoted strings
  1235. (should (equal '("foo" "bar")
  1236. (org-babel-script-escape "('foo' 'bar')")))
  1237. (should (equal '("foo" "bar")
  1238. (org-babel-script-escape "['foo' 'bar']")))
  1239. (should (equal '("foo" "bar")
  1240. (org-babel-script-escape "{'foo' 'bar'}")))
  1241. ;; ... with commas
  1242. (should (equal '("foo" "bar")
  1243. (org-babel-script-escape "('foo', 'bar')")))
  1244. (should (equal '("foo" "bar")
  1245. (org-babel-script-escape "['foo', 'bar']")))
  1246. (should (equal '("foo" "bar")
  1247. (org-babel-script-escape "{'foo', 'bar'}")))
  1248. ;; Single quoted strings
  1249. (should (equal "foo"
  1250. (org-babel-script-escape "'foo'")))
  1251. ;; ... with internal double quote
  1252. (should (equal "foo\"bar"
  1253. (org-babel-script-escape "'foo\"bar'")))
  1254. ;; ... with internal backslash
  1255. (should (equal "foo\\bar"
  1256. (org-babel-script-escape "'foo\\bar'")))
  1257. ;; ... with internal escaped backslash
  1258. (should (equal "foo\\bar"
  1259. (org-babel-script-escape "'foo\\\\bar'")))
  1260. ;; ... with internal backslash-double quote
  1261. (should (equal "foo\\\"bar"
  1262. (org-babel-script-escape "'foo\\\"bar'")))
  1263. ;; ... with internal escaped backslash-double quote
  1264. (should (equal "foo\\\"bar"
  1265. (org-babel-script-escape "'foo\\\\\"bar'")))
  1266. ;; ... with internal escaped single quote
  1267. (should (equal "foo'bar"
  1268. (org-babel-script-escape "'foo\\'bar'")))
  1269. ;; ... with internal escaped backslash-escaped single quote
  1270. (should (equal "foo\\'bar"
  1271. (org-babel-script-escape "'foo\\\\\\'bar'")))
  1272. ;; Double quoted strings
  1273. (should (equal "foo"
  1274. (org-babel-script-escape "\"foo\"")))
  1275. ;; ... with internal single quote
  1276. (should (equal "foo'bar"
  1277. (org-babel-script-escape "\"foo'bar\"")))
  1278. ;; ... with internal backslash
  1279. (should (equal "foo\\bar"
  1280. (org-babel-script-escape "\"foo\\bar\"")))
  1281. ;; ... with internal escaped backslash
  1282. (should (equal "foo\\bar"
  1283. (org-babel-script-escape "\"foo\\\\bar\"")))
  1284. ;; ... with internal backslash-single quote
  1285. (should (equal "foo\\'bar"
  1286. (org-babel-script-escape "\"foo\\'bar\"")))
  1287. ;; ... with internal escaped backslash-single quote
  1288. (should (equal "foo\\'bar"
  1289. (org-babel-script-escape "\"foo\\\\'bar\"")))
  1290. ;; ... with internal escaped double quote
  1291. (should (equal "foo\"bar"
  1292. (org-babel-script-escape "\"foo\\\"bar\"")))
  1293. ;; ... with internal escaped backslash-escaped double quote
  1294. (should (equal "foo\\\"bar"
  1295. (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
  1296. (ert-deftest test-ob/process-params-no-duplicates ()
  1297. (should
  1298. (equal (org-babel-process-params '((:colname-names)
  1299. (:rowname-names)
  1300. (:result-params)
  1301. (:result-type)
  1302. (:var . "\"foo\"")))
  1303. '((:var)
  1304. (:colname-names)
  1305. (:rowname-names)
  1306. (:result-params)
  1307. (:result-type . value)))))
  1308. (defun org-test-babel-confirm-evaluate (eval-value)
  1309. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  1310. nil
  1311. #+end_src" eval-value)
  1312. (goto-char (point-min))
  1313. (let ((info (org-babel-get-src-block-info)))
  1314. (org-babel-check-confirm-evaluate info))))
  1315. (ert-deftest test-ob/check-eval ()
  1316. (let ((org-confirm-babel-evaluate t))
  1317. ;; Non-export tests
  1318. (dolist (pair '(("no" . nil)
  1319. ("never" . nil)
  1320. ("query" . query)
  1321. ("yes" . query)))
  1322. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1323. ;; Export tests
  1324. (let ((org-babel-exp-reference-buffer t))
  1325. (dolist (pair '(("no" . nil)
  1326. ("never" . nil)
  1327. ("query" . query)
  1328. ("yes" . query)
  1329. ("never-export" . nil)
  1330. ("no-export" . nil)
  1331. ("query-export" . query)))
  1332. (message (car pair))
  1333. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))))
  1334. (let ((org-confirm-babel-evaluate nil))
  1335. ;; Non-export tests
  1336. (dolist (pair '(("no" . nil)
  1337. ("never" . nil)
  1338. ("query" . query)
  1339. ("yes" . t)))
  1340. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1341. ;; Export tests
  1342. (let ((org-babel-exp-reference-buffer t))
  1343. (dolist (pair '(("no" . nil)
  1344. ("never" . nil)
  1345. ("query" . query)
  1346. ("yes" . t)
  1347. ("never-export" . nil)
  1348. ("no-export" . nil)
  1349. ("query-export" . query)))
  1350. (message (car pair))
  1351. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair)))))))
  1352. (defun org-test-ob/update-block-body ()
  1353. "Test `org-babel-update-block-body' specifications."
  1354. (should
  1355. (equal "#+begin_src elisp\n 2\n#+end_src"
  1356. (let ((org-edit-src-content-indentation 2))
  1357. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1358. (org-babel-update-block-body "2")
  1359. (buffer-string)))))
  1360. ;; Preserve block indentation.
  1361. (should
  1362. (equal " #+begin_src elisp\n 2\n #+end_src"
  1363. (let ((org-edit-src-content-indentation 1))
  1364. (org-test-with-temp-text
  1365. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1366. (org-babel-update-block-body "2")
  1367. (buffer-string)))))
  1368. ;; Ignore NEW-BODY global indentation.
  1369. (should
  1370. (equal "#+begin_src elisp\n 2\n#+end_src"
  1371. (let ((org-edit-src-content-indentation 2))
  1372. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1373. (org-babel-update-block-body " 2")
  1374. (buffer-string)))))
  1375. ;; When indentation should be preserved ignore the two rules above.
  1376. (should
  1377. (equal " #+begin_src elisp\n2\n #+end_src"
  1378. (let ((org-edit-src-content-indentation 1)
  1379. (org-src-preserve-indentation t))
  1380. (org-test-with-temp-text
  1381. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1382. (org-babel-update-block-body "2")
  1383. (buffer-string)))))
  1384. (should
  1385. (equal " #+begin_src elisp -i\n2\n #+end_src"
  1386. (let ((org-edit-src-content-indentation 1))
  1387. (org-test-with-temp-text
  1388. " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
  1389. (org-babel-update-block-body "2")
  1390. (buffer-string)))))
  1391. (should
  1392. (equal "#+begin_src elisp\n 2\n#+end_src"
  1393. (let ((org-edit-src-content-indentation 2)
  1394. (org-src-preserve-indentation t))
  1395. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1396. (org-babel-update-block-body " 2")
  1397. (buffer-string)))))
  1398. (should
  1399. (equal "#+begin_src elisp -i\n 2\n#+end_src"
  1400. (let ((org-edit-src-content-indentation 2)
  1401. (org-src-preserve-indentation t))
  1402. (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
  1403. (org-babel-update-block-body " 2")
  1404. (buffer-string))))))
  1405. (ert-deftest test-ob/find-named-result ()
  1406. "Test `org-babel-find-named-result' specifications."
  1407. (should
  1408. (= 1
  1409. (org-test-with-temp-text "#+results: foo\n: result"
  1410. (org-babel-find-named-result "foo"))))
  1411. (should-not
  1412. (org-test-with-temp-text "#+results: foo\n: result"
  1413. (org-babel-find-named-result "bar")))
  1414. (should-not
  1415. (org-test-with-temp-text "#+results: foobar\n: result"
  1416. (org-babel-find-named-result "foo")))
  1417. ;; Search is case insensitive.
  1418. (should
  1419. (org-test-with-temp-text "#+RESULTS: FOO\n: result"
  1420. (org-babel-find-named-result "foo")))
  1421. ;; Handle hash in results keyword.
  1422. (should
  1423. (org-test-with-temp-text "#+results[hash]: FOO\n: result"
  1424. (org-babel-find-named-result "foo")))
  1425. ;; Accept orphaned affiliated keywords.
  1426. (should
  1427. (org-test-with-temp-text "#+results: foo"
  1428. (org-babel-find-named-result "foo"))))
  1429. (ert-deftest test-ob/where-is-src-block-result ()
  1430. "Test `org-babel-where-is-src-block-result' specifications."
  1431. ;; Find anonymous results.
  1432. (should
  1433. (equal "#+RESULTS:"
  1434. (org-test-with-temp-text
  1435. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
  1436. (goto-char (org-babel-where-is-src-block-result))
  1437. (buffer-substring-no-properties (point) (line-end-position)))))
  1438. ;; Find named results. Those have priority over anonymous ones.
  1439. (should
  1440. (equal "#+RESULTS: example"
  1441. (org-test-with-temp-text
  1442. "
  1443. <point>#+NAME: example
  1444. #+BEGIN_SRC emacs-lisp
  1445. \(+ 1 1)
  1446. #+END_SRC
  1447. #+RESULTS: example
  1448. : 2"
  1449. (goto-char (org-babel-where-is-src-block-result))
  1450. (buffer-substring-no-properties (point) (line-end-position)))))
  1451. (should
  1452. (equal "#+RESULTS: example"
  1453. (org-test-with-temp-text
  1454. "
  1455. <point>#+NAME: example
  1456. #+BEGIN_SRC emacs-lisp
  1457. \(+ 1 1)
  1458. #+END_SRC
  1459. #+RESULTS:
  1460. : fake
  1461. #+RESULTS: example
  1462. : 2"
  1463. (goto-char (org-babel-where-is-src-block-result))
  1464. (buffer-substring-no-properties (point) (line-end-position)))))
  1465. ;; Return nil when no result is found.
  1466. (should-not
  1467. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1468. (org-babel-where-is-src-block-result)))
  1469. (should-not
  1470. (org-test-with-temp-text
  1471. "- item\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n"
  1472. (org-babel-where-is-src-block-result)))
  1473. ;; When optional argument INSERT is non-nil, add RESULTS keyword
  1474. ;; whenever no RESULTS can be found.
  1475. (should
  1476. (equal
  1477. "#+RESULTS:"
  1478. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1479. (let ((org-babel-results-keyword "RESULTS"))
  1480. (goto-char (org-babel-where-is-src-block-result t)))
  1481. (buffer-substring-no-properties (point) (line-end-position)))))
  1482. ;; Insert a named RESULTS keyword if possible.
  1483. (should
  1484. (equal
  1485. "#+RESULTS: e"
  1486. (org-test-with-temp-text
  1487. "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1488. (let ((org-babel-results-keyword "RESULTS"))
  1489. (goto-char (org-babel-where-is-src-block-result t)))
  1490. (buffer-substring-no-properties (point) (line-end-position)))))
  1491. ;; When optional argument HASH is provided, clear RESULTS keyword
  1492. ;; and related contents if they do not match it.
  1493. (should
  1494. (equal
  1495. "#+RESULTS[bbbb]:"
  1496. (org-test-with-temp-text
  1497. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 3"
  1498. (let ((org-babel-results-keyword "RESULTS"))
  1499. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1500. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1501. (should
  1502. (equal
  1503. "#+RESULTS[bbbb]:"
  1504. (org-test-with-temp-text
  1505. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
  1506. (let ((org-babel-results-keyword "RESULTS"))
  1507. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1508. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1509. ;; RESULTS keyword may not be the last affiliated keyword.
  1510. (should
  1511. (equal
  1512. "#+RESULTS[bbbb]:"
  1513. (org-test-with-temp-text
  1514. "
  1515. <point>#+BEGIN_SRC emacs-lisp
  1516. \(+ 1 1)
  1517. #+END_SRC
  1518. #+RESULTS[aaaa]:
  1519. #+NAME: e
  1520. : 3"
  1521. (let ((org-babel-results-keyword "RESULTS"))
  1522. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1523. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1524. ;; HASH does nothing if no RESULTS can be found. However, if INSERT
  1525. ;; is also non-nil, RESULTS keyword is inserted along with the
  1526. ;; expected hash.
  1527. (should
  1528. (equal
  1529. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1530. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1531. (org-babel-where-is-src-block-result nil nil "bbbb")
  1532. (buffer-string))))
  1533. (should
  1534. (equal
  1535. "#+RESULTS[bbbb]:"
  1536. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1537. (let ((org-babel-results-keyword "RESULTS"))
  1538. (goto-char (org-babel-where-is-src-block-result t nil "bbbb")))
  1539. (org-trim (buffer-substring-no-properties (point) (point-max)))))))
  1540. (ert-deftest test-ob/goto-named-src-block ()
  1541. "Test interactive use of `org-babel-goto-named-src-block'."
  1542. (org-test-with-temp-text-in-file
  1543. "
  1544. #+NAME: abc
  1545. #+BEGIN_SRC emacs-lisp :results value
  1546. (1+ 1)
  1547. #+END_SRC
  1548. #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
  1549. #+BEGIN_SRC emacs-lisp
  1550. <<abc>>
  1551. #+END_SRC
  1552. abc
  1553. #+RESULTS: abc
  1554. : 2
  1555. "
  1556. ;; non-existent name
  1557. (should-not
  1558. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
  1559. ;; correct name
  1560. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
  1561. (should (= 14 (point)))
  1562. ;; call line - autocompletion
  1563. (forward-line 3)
  1564. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1565. (should (= 14 (point)))
  1566. ;; noweb reference - autocompletion
  1567. (forward-line 5)
  1568. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1569. (should (= 14 (point)))
  1570. ;; at symbol - autocompletion
  1571. (forward-line 7)
  1572. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1573. (should (= 14 (point)))
  1574. ;; in results - autocompletion
  1575. (forward-line 8)
  1576. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1577. (should (= 14 (point)))
  1578. (forward-line 9)
  1579. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1580. (should (= 14 (point)))))
  1581. (provide 'test-ob)
  1582. ;;; test-ob ends here