test-ob.el 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  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/balanced-split ()
  629. "Test `org-babel-balanced-split' specifications."
  630. (should (equal
  631. '(":a 1" "b [2 3]" "c (4 :d (5 6))")
  632. (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
  633. '((32 9) . 58))))
  634. ;; Handle un-balanced parens.
  635. (should
  636. (equal '(":foo ((6)" "bar 1")
  637. (org-babel-balanced-split ":foo ((6) :bar 1" '((32 9) . 58))))
  638. (should
  639. (equal '(":foo \"(foo\"" "bar 2")
  640. (org-babel-balanced-split ":foo \"(foo\" :bar 2" '((32 9) . 58))))
  641. ;; Handle un-balanced quotes.
  642. (should
  643. (equal '(":foo \"1" "bar 3")
  644. (org-babel-balanced-split ":foo \"1 :bar 3" '((32 9) . 58)))))
  645. (ert-deftest test-ob/commented-last-block-line-no-var ()
  646. (org-test-with-temp-text-in-file "
  647. #+begin_src emacs-lisp
  648. ;;
  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. ""
  657. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  658. (org-test-with-temp-text-in-file "
  659. #+begin_src emacs-lisp
  660. \"some text\";;
  661. #+end_src"
  662. (org-babel-next-src-block)
  663. (org-babel-execute-maybe)
  664. (should (re-search-forward "\\#\\+results:" nil t))
  665. (forward-line)
  666. (should
  667. (string=
  668. ": some text"
  669. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  670. (ert-deftest test-ob/commented-last-block-line-with-var ()
  671. (org-test-with-temp-text-in-file "
  672. #+begin_src emacs-lisp :var a=1
  673. ;;
  674. #+end_src"
  675. (org-babel-next-src-block)
  676. (org-babel-execute-maybe)
  677. (re-search-forward "\\#\\+results:" nil t)
  678. (forward-line)
  679. (should (string=
  680. ""
  681. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  682. (org-test-with-temp-text-in-file "
  683. #+begin_src emacs-lisp :var a=2
  684. 2;;
  685. #+end_src"
  686. (org-babel-next-src-block)
  687. (org-babel-execute-maybe)
  688. (re-search-forward "\\#\\+results:" nil t)
  689. (forward-line)
  690. (should (string=
  691. ": 2"
  692. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  693. (ert-deftest test-ob/org-babel-insert-result ()
  694. "Test `org-babel-insert-result' specifications."
  695. ;; Do not error when output is an improper list.
  696. (should
  697. (org-test-with-temp-text
  698. "
  699. <point>#+BEGIN_SRC emacs-lisp
  700. '((1 . nil) (2 . 3))
  701. #+END_SRC
  702. "
  703. (org-babel-execute-maybe) t))
  704. ;; Escape headlines when producing an example block.
  705. (should
  706. (string-match-p
  707. ",\\* Not an headline"
  708. (org-test-with-temp-text
  709. "
  710. <point>#+BEGIN_SRC emacs-lisp
  711. \"* Not an headline\"
  712. #+END_SRC
  713. "
  714. (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
  715. (buffer-string))))
  716. ;; Escape special syntax in example blocks.
  717. (should
  718. (string-match-p
  719. ",#\\+END_SRC"
  720. (org-test-with-temp-text
  721. "
  722. <point>#+BEGIN_SRC emacs-lisp
  723. \"#+END_SRC\"
  724. #+END_SRC
  725. "
  726. (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
  727. (buffer-string))))
  728. ;; No escaping is done with other blocks or raw type.
  729. (should-not
  730. (string-match-p
  731. ",\\* Not an headline"
  732. (org-test-with-temp-text
  733. "
  734. <point>#+BEGIN_SRC emacs-lisp
  735. \"* Not an headline\"
  736. #+END_SRC
  737. "
  738. (let ((org-babel-min-lines-for-block-output 10))
  739. (org-babel-execute-maybe))
  740. (buffer-string))))
  741. (should-not
  742. (string-match-p
  743. ",\\* Not an headline"
  744. (org-test-with-temp-text
  745. "
  746. <point>#+BEGIN_SRC emacs-lisp :results raw
  747. \"* Not an headline\"
  748. #+END_SRC
  749. "
  750. (org-babel-execute-maybe)
  751. (buffer-string))))
  752. (should-not
  753. (string-match-p
  754. ",\\* Not an headline"
  755. (org-test-with-temp-text
  756. "
  757. <point>#+BEGIN_SRC emacs-lisp :results drawer
  758. \"* Not an headline\"
  759. #+END_SRC
  760. "
  761. (org-babel-execute-maybe)
  762. (buffer-string)))))
  763. (ert-deftest test-ob/remove-inline-result ()
  764. "Test `org-babel-remove-inline-result' honors whitespace."
  765. (let*
  766. ((inline-sb "src_emacs-lisp{(+ 1 2)}")
  767. (inline-res " {{{results(=3=)}}}")
  768. (inline-sb-dot (concat inline-sb "."))
  769. (inline-sb-res-dot (concat inline-sb inline-res ".")))
  770. (org-test-with-temp-text
  771. ;; Insert inline_src_block followed by dot.
  772. inline-sb-dot
  773. ;; Insert result before dot.
  774. (org-babel-execute-maybe)
  775. (should (string= inline-sb-res-dot
  776. (buffer-substring-no-properties
  777. (point-at-bol) (point-at-eol))))
  778. ;; Delete whitespace and result.
  779. (org-babel-remove-inline-result)
  780. (should (string= inline-sb-dot
  781. (buffer-substring-no-properties
  782. (point-at-bol) (point-at-eol))))
  783. ;; Add whitespace and result before dot.
  784. (search-forward inline-sb)
  785. (insert " " inline-res)
  786. (goto-char (point-at-bol))
  787. ;; Remove whitespace and result.
  788. (org-babel-remove-inline-result)
  789. (should (string= inline-sb-dot
  790. (buffer-substring-no-properties
  791. (point-at-bol) (point-at-eol))))
  792. ;; Add whitespace before dot.
  793. (search-forward inline-sb)
  794. (insert " ")
  795. (goto-char (point-at-bol))
  796. ;; Add result before whitespace.
  797. (org-babel-execute-maybe)
  798. ;; Remove result - leave trailing whitespace and dot.
  799. (org-babel-remove-inline-result)
  800. (should (string= (concat inline-sb " .")
  801. (buffer-substring-no-properties
  802. (point-at-bol) (point-at-eol)))))))
  803. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  804. "Test helper function to test `org-babel-remove-result'.
  805. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  806. and the result of execution is verified against RESULT.
  807. The block is actually executed /twice/ to ensure result
  808. replacement happens correctly."
  809. (org-test-with-temp-text
  810. buffer-text
  811. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  812. (should (re-search-forward "\\#\\+results:" nil t))
  813. (forward-line)
  814. (should (string= result
  815. (buffer-substring-no-properties
  816. (point-at-bol)
  817. (- (point-max) 16))))
  818. (org-babel-previous-src-block) (org-babel-remove-result)
  819. (should (string= buffer-text
  820. (buffer-substring-no-properties
  821. (point-min) (point-max))))))
  822. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  823. "Test `org-babel-remove-result' with default :results."
  824. (mapcar (lambda (language)
  825. (test-ob-verify-result-and-removed-result
  826. "\n"
  827. (concat
  828. "* org-babel-remove-result
  829. #+begin_src " language "
  830. #+end_src
  831. * next heading")))
  832. '("sh" "emacs-lisp")))
  833. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  834. "Test `org-babel-remove-result' with :results list."
  835. (test-ob-verify-result-and-removed-result
  836. "- 1
  837. - 2
  838. - 3
  839. - (quote (4 5))"
  840. "* org-babel-remove-result
  841. #+begin_src emacs-lisp :results list
  842. '(1 2 3 '(4 5))
  843. #+end_src
  844. * next heading"))
  845. (ert-deftest test-ob/org-babel-results-indented-wrap ()
  846. "Ensure that wrapped results are inserted correction when indented.
  847. If not inserted correctly then the second evaluation will fail
  848. trying to find the :END: marker."
  849. (org-test-with-temp-text
  850. "- indented
  851. #+begin_src sh :results file wrap
  852. echo test.txt
  853. #+end_src"
  854. (org-babel-next-src-block 1)
  855. (org-babel-execute-src-block)
  856. (org-babel-execute-src-block)))
  857. (ert-deftest test-ob/file-desc-header-argument ()
  858. "Test that the :file-desc header argument is used."
  859. (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  860. \"foo\"
  861. #+end_src
  862. #+begin_src emacs-lisp :results file :file-desc
  863. \"foo\"
  864. #+end_src"
  865. (org-babel-execute-src-block)
  866. (org-babel-next-src-block 1)
  867. (org-babel-execute-src-block)
  868. (goto-char (point-min))
  869. (should (search-forward "[[file:foo][bar]]" nil t))
  870. (should (search-forward "[[file:foo][foo]]" nil t))))
  871. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  872. "Test `org-babel-remove-result' with :results pp."
  873. (test-ob-verify-result-and-removed-result
  874. ": \"I /am/ working!\""
  875. "* org-babel-remove-result
  876. #+begin_src emacs-lisp :results pp
  877. \"I /am/ working!\")
  878. #+end_src
  879. * next heading"))
  880. (ert-deftest test-ob/inline-src_blk-preceded-punct-preceded-by-point ()
  881. (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  882. (org-babel-inline-result-wrap "=%s="))
  883. (org-test-with-temp-text
  884. test-line
  885. (forward-char 1)
  886. (org-babel-execute-maybe)
  887. (should (re-search-forward "=\"x\"=" nil t))
  888. (forward-line))))
  889. (ert-deftest test-ob/commented-last-block-line-with-var ()
  890. (org-test-with-temp-text-in-file "
  891. #+begin_src emacs-lisp :var a=1
  892. ;;
  893. #+end_src"
  894. (org-babel-next-src-block)
  895. (org-babel-execute-maybe)
  896. (re-search-forward "\\#\\+results:" nil t)
  897. (forward-line)
  898. (should (string=
  899. ""
  900. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  901. (org-test-with-temp-text-in-file "
  902. #+begin_src emacs-lisp :var a=2
  903. 2;;
  904. #+end_src"
  905. (org-babel-next-src-block)
  906. (org-babel-execute-maybe)
  907. (re-search-forward "\\#\\+results:" nil t)
  908. (forward-line)
  909. (should (string=
  910. ": 2"
  911. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  912. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  913. "Test helper function to test `org-babel-remove-result'.
  914. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  915. and the result of execution is verified against RESULT.
  916. The block is actually executed /twice/ to ensure result
  917. replacement happens correctly."
  918. (org-test-with-temp-text
  919. buffer-text
  920. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  921. (should (re-search-forward "\\#\\+results:" nil t))
  922. (forward-line)
  923. (should (string= result
  924. (buffer-substring-no-properties
  925. (point-at-bol)
  926. (- (point-max) 16))))
  927. (org-babel-previous-src-block) (org-babel-remove-result)
  928. (should (string= buffer-text
  929. (buffer-substring-no-properties
  930. (point-min) (point-max))))))
  931. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  932. "Test `org-babel-remove-result' with default :results."
  933. (mapcar (lambda (language)
  934. (test-ob-verify-result-and-removed-result
  935. "\n"
  936. (concat
  937. "* org-babel-remove-result
  938. #+begin_src " language "
  939. #+end_src
  940. * next heading")))
  941. '("sh" "emacs-lisp")))
  942. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  943. "Test `org-babel-remove-result' with :results list."
  944. (test-ob-verify-result-and-removed-result
  945. "- 1
  946. - 2
  947. - 3
  948. - (quote (4 5))"
  949. "* org-babel-remove-result
  950. #+begin_src emacs-lisp :results list
  951. '(1 2 3 '(4 5))
  952. #+end_src
  953. * next heading"))
  954. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  955. "Test `org-babel-remove-result' with :results wrap."
  956. (test-ob-verify-result-and-removed-result
  957. ":RESULTS:
  958. hello there
  959. :END:"
  960. "* org-babel-remove-result
  961. #+begin_src emacs-lisp :results wrap
  962. \"hello there\"
  963. #+end_src
  964. * next heading"))
  965. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  966. "Test `org-babel-remove-result' with :results org."
  967. (test-ob-verify-result-and-removed-result
  968. "#+BEGIN_SRC org
  969. ,* heading
  970. ,** subheading
  971. content
  972. #+END_SRC"
  973. "* org-babel-remove-result
  974. #+begin_src emacs-lisp :results org
  975. \"* heading
  976. ,** subheading
  977. content\"
  978. #+end_src
  979. * next heading"))
  980. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  981. "Test `org-babel-remove-result' with :results html."
  982. (test-ob-verify-result-and-removed-result
  983. "#+BEGIN_EXPORT html
  984. <head><body></body></head>
  985. #+END_EXPORT"
  986. "* org-babel-remove-result
  987. #+begin_src emacs-lisp :results html
  988. \"<head><body></body></head>\"
  989. #+end_src
  990. * next heading"))
  991. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  992. "Test `org-babel-remove-result' with :results latex."
  993. (test-ob-verify-result-and-removed-result
  994. "#+BEGIN_EXPORT latex
  995. Line 1
  996. Line 2
  997. Line 3
  998. #+END_EXPORT"
  999. "* org-babel-remove-result
  1000. #+begin_src emacs-lisp :results latex
  1001. \"Line 1
  1002. Line 2
  1003. Line 3\"
  1004. #+end_src
  1005. * next heading"))
  1006. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  1007. "Test `org-babel-remove-result' with :results code."
  1008. (test-ob-verify-result-and-removed-result
  1009. "#+BEGIN_SRC emacs-lisp
  1010. \"I am working!\"
  1011. #+END_SRC"
  1012. "* org-babel-remove-result
  1013. #+begin_src emacs-lisp :results code
  1014. (message \"I am working!\")
  1015. #+end_src
  1016. * next heading"))
  1017. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  1018. "Test `org-babel-remove-result' with :results pp."
  1019. (test-ob-verify-result-and-removed-result
  1020. ": \"I /am/ working!\""
  1021. "* org-babel-remove-result
  1022. #+begin_src emacs-lisp :results pp
  1023. \"I /am/ working!\")
  1024. #+end_src
  1025. * next heading"))
  1026. (ert-deftest test-ob/results-do-not-replace-code-blocks ()
  1027. (org-test-with-temp-text "Block two has a space after the name.
  1028. #+name: foo
  1029. #+begin_src emacs-lisp
  1030. 1
  1031. #+end_src
  1032. #+name: foo
  1033. #+begin_src emacs-lisp
  1034. 2
  1035. #+end_src
  1036. #+name: foo
  1037. #+begin_src emacs-lisp
  1038. 3
  1039. #+end_src
  1040. #+RESULTS: foo
  1041. : foo
  1042. "
  1043. (dolist (num '(1 2 3))
  1044. ;; execute the block
  1045. (goto-char (point-min))
  1046. (org-babel-next-src-block num) (org-babel-execute-src-block)
  1047. ;; check the results
  1048. (goto-char (point-max))
  1049. (move-beginning-of-line 0)
  1050. (should (looking-at (format ": %d" num))))))
  1051. (ert-deftest test-ob/blocks-with-spaces ()
  1052. "Test expansion of blocks followed by blank lines."
  1053. ;; Preserve number of blank lines after block.
  1054. (should
  1055. (equal "#+BEGIN_SRC emacs-lisp
  1056. \(+ 1 2)
  1057. #+END_SRC
  1058. #+RESULTS:
  1059. : 3\n\n\n"
  1060. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
  1061. \(+ 1 2)
  1062. #+END_SRC\n\n\n"
  1063. (let ((org-babel-next-src-block "RESULTS"))
  1064. (org-babel-execute-src-block))
  1065. (buffer-string))))
  1066. ;; Do not add spurious blank lines after results.
  1067. (should
  1068. (equal
  1069. "
  1070. - item 1
  1071. #+begin_src emacs-lisp
  1072. 0
  1073. #+end_src
  1074. #+RESULTS:
  1075. : 0
  1076. - item 2"
  1077. (org-test-with-temp-text "
  1078. - item 1
  1079. #+begin_src emacs-lisp<point>
  1080. 0
  1081. #+end_src
  1082. - item 2"
  1083. (org-babel-execute-src-block)
  1084. (buffer-string))))
  1085. (should
  1086. (equal
  1087. "
  1088. - item 1
  1089. #+begin_src emacs-lisp
  1090. 1
  1091. #+end_src
  1092. #+RESULTS:
  1093. : 1
  1094. - item 2"
  1095. (org-test-with-temp-text "
  1096. - item 1
  1097. #+begin_src emacs-lisp<point>
  1098. 1
  1099. #+end_src
  1100. #+RESULTS:
  1101. : 1
  1102. - item 2"
  1103. (org-babel-execute-src-block)
  1104. (buffer-string)))))
  1105. (ert-deftest test-ob/results-in-narrowed-buffer ()
  1106. "Test block execution in a narrowed buffer."
  1107. ;; If results don't exist, they should be inserted in visible part
  1108. ;; of the buffer.
  1109. (should
  1110. (equal
  1111. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
  1112. (org-test-with-temp-text
  1113. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1114. (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
  1115. (let ((org-babel-results-keyword "RESULTS"))
  1116. (org-babel-execute-src-block))
  1117. (org-trim (buffer-string)))))
  1118. (should
  1119. (equal
  1120. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
  1121. (org-test-with-temp-text
  1122. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1123. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1124. (let ((org-babel-results-keyword "RESULTS"))
  1125. (org-babel-execute-src-block))
  1126. (org-trim (buffer-string)))))
  1127. ;; Results in visible part of buffer, should be updated here.
  1128. (should
  1129. (equal
  1130. "#+NAME: test
  1131. #+BEGIN_SRC emacs-lisp
  1132. \(+ 1 2)
  1133. #+END_SRC
  1134. #+RESULTS: test
  1135. : 3"
  1136. (org-test-with-temp-text
  1137. "#+NAME: test
  1138. #+BEGIN_SRC emacs-lisp
  1139. \(+ 1 2)
  1140. #+END_SRC
  1141. #+RESULTS: test
  1142. : 4
  1143. Paragraph"
  1144. (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
  1145. (let ((org-babel-results-keyword "RESULTS"))
  1146. (org-babel-execute-src-block))
  1147. (org-trim (buffer-string)))))
  1148. ;; Results in invisible part of buffer, should be updated there.
  1149. (org-test-with-temp-text
  1150. "#+NAME: test
  1151. #+BEGIN_SRC emacs-lisp
  1152. \(+ 1 2)
  1153. #+END_SRC
  1154. #+RESULTS: test
  1155. : 4
  1156. Paragraph"
  1157. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1158. (let ((org-babel-results-keyword "RESULTS"))
  1159. (org-babel-execute-src-block))
  1160. (should-not (re-search-forward "^#\\+RESULTS:" nil t))
  1161. (widen)
  1162. (should (re-search-forward "^: 3" nil t))))
  1163. (ert-deftest test-ob/specific-colnames ()
  1164. "Test passing specific column names."
  1165. (should
  1166. (equal "#+name: input-table
  1167. | id | var1 |
  1168. |----+------|
  1169. | 1 | bar |
  1170. | 2 | baz |
  1171. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1172. echo \"$data\"
  1173. #+end_src
  1174. #+RESULTS:
  1175. | Rev | Author |
  1176. |-----+--------|
  1177. | 1 | bar |
  1178. | 2 | baz |
  1179. "
  1180. (org-test-with-temp-text
  1181. "#+name: input-table
  1182. | id | var1 |
  1183. |----+------|
  1184. | 1 | bar |
  1185. | 2 | baz |
  1186. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1187. echo \"$data\"
  1188. #+end_src
  1189. "
  1190. ;; we should find a code block
  1191. (should (re-search-forward org-babel-src-block-regexp nil t))
  1192. (goto-char (match-beginning 0))
  1193. ;; now that we've located the code block, it may be evaluated
  1194. (let ((org-babel-execute-src-block "RESULTS"))
  1195. (org-babel-execute-src-block))
  1196. (buffer-string)))))
  1197. (ert-deftest test-ob/location-of-header-arg-eval ()
  1198. "Test location of header argument evaluation."
  1199. (org-test-with-temp-text "
  1200. #+name: top-block
  1201. #+begin_src emacs-lisp :var pt=(point)
  1202. pt
  1203. #+end_src
  1204. #+name: bottom-block
  1205. #+begin_src emacs-lisp :var pt=top-block()
  1206. pt
  1207. #+end_src
  1208. "
  1209. ;; the value of the second block should be greater than the first
  1210. (should
  1211. (< (progn (re-search-forward org-babel-src-block-regexp nil t)
  1212. (goto-char (match-beginning 0))
  1213. (prog1 (save-match-data (org-babel-execute-src-block))
  1214. (goto-char (match-end 0))))
  1215. (progn (re-search-forward org-babel-src-block-regexp nil t)
  1216. (goto-char (match-beginning 0))
  1217. (org-babel-execute-src-block))))))
  1218. (ert-deftest test-ob/preserve-results-indentation ()
  1219. "Preserve indentation when executing a src block."
  1220. (should
  1221. (equal
  1222. '(2 2)
  1223. (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1224. (org-babel-execute-src-block)
  1225. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1226. (list (org-get-indentation)
  1227. (progn (forward-line) (org-get-indentation))))))
  1228. (should
  1229. (equal
  1230. '(2 2)
  1231. (org-test-with-temp-text
  1232. " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1233. (org-babel-execute-src-block)
  1234. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1235. (list (org-get-indentation)
  1236. (progn (forward-line) (org-get-indentation))))))
  1237. ;; Don't get fooled by TAB-based indentation.
  1238. (should
  1239. (equal
  1240. '(6 6)
  1241. (org-test-with-temp-text
  1242. "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
  1243. (setq tab-width 4)
  1244. (org-babel-execute-src-block)
  1245. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1246. (list (org-get-indentation)
  1247. (progn (forward-line) (org-get-indentation)))))))
  1248. (ert-deftest test-ob/safe-header-args ()
  1249. "Detect safe and unsafe header args."
  1250. (let ((safe-args '((:cache . "foo")
  1251. (:results . "output")
  1252. (:eval . "never")
  1253. (:eval . "query")))
  1254. (unsafe-args '((:eval . "yes")
  1255. (:results . "output file")
  1256. (:foo . "bar")))
  1257. (malformed-args '((bar . "foo")
  1258. ("foo" . "bar")
  1259. :foo))
  1260. (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
  1261. (dolist (arg safe-args)
  1262. (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
  1263. (dolist (arg unsafe-args)
  1264. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1265. (dolist (arg malformed-args)
  1266. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1267. (should (not (funcall safe-p (append safe-args unsafe-args))))))
  1268. (ert-deftest test-ob/noweb-expansions-in-cache ()
  1269. "Ensure that noweb expansions are expanded before caching."
  1270. (let ((noweb-expansions-in-cache-var 0))
  1271. (org-test-with-temp-text "
  1272. #+name: foo
  1273. #+begin_src emacs-lisp
  1274. \"I said\"
  1275. #+end_src
  1276. #+name: bar
  1277. #+begin_src emacs-lisp :noweb yes :cache yes
  1278. (setq noweb-expansions-in-cache-var
  1279. (+ 1 noweb-expansions-in-cache-var))
  1280. (concat <<foo>> \" check noweb expansions\")
  1281. #+end_src
  1282. "
  1283. ;; run the second block to create the cache
  1284. (goto-char (point-min))
  1285. (re-search-forward (regexp-quote "#+name: bar"))
  1286. (should (string= "I said check noweb expansions"
  1287. (org-babel-execute-src-block)))
  1288. (should (= noweb-expansions-in-cache-var 1))
  1289. ;; change the value of the first block
  1290. (goto-char (point-min))
  1291. (re-search-forward (regexp-quote "said"))
  1292. (goto-char (match-beginning 0))
  1293. (insert "haven't ")
  1294. (re-search-forward (regexp-quote "#+name: bar"))
  1295. (should (string= "I haven't said check noweb expansions"
  1296. (org-babel-execute-src-block)))
  1297. (should (= noweb-expansions-in-cache-var 2)))))
  1298. (ert-deftest test-ob/file-ext-and-output-dir ()
  1299. (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
  1300. (org-babel-next-src-block)
  1301. (should (equal "file-ext-basic.txt"
  1302. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1303. (org-babel-next-src-block)
  1304. (should (equal "foo/file-ext-dir-relative.txt"
  1305. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1306. (org-babel-next-src-block)
  1307. (should (equal "foo/file-ext-dir-relative-slash.txt"
  1308. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1309. (org-babel-next-src-block)
  1310. (should (equal "/tmp/file-ext-dir-absolute.txt"
  1311. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1312. (org-babel-next-src-block)
  1313. (should (equal "foo.bar"
  1314. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1315. (org-babel-next-src-block)
  1316. (should (equal "xxx/foo.bar"
  1317. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1318. ))
  1319. (ert-deftest test-ob/script-escape ()
  1320. ;; Delimited lists of numbers
  1321. (should (equal '(1 2 3)
  1322. (org-babel-script-escape "[1 2 3]")))
  1323. (should (equal '(1 2 3)
  1324. (org-babel-script-escape "{1 2 3}")))
  1325. (should (equal '(1 2 3)
  1326. (org-babel-script-escape "(1 2 3)")))
  1327. ;; Delimited lists of double-quoted strings
  1328. (should (equal '("foo" "bar")
  1329. (org-babel-script-escape "(\"foo\" \"bar\")")))
  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. ;; ... with commas
  1335. (should (equal '("foo" "bar")
  1336. (org-babel-script-escape "(\"foo\", \"bar\")")))
  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. ;; Delimited lists of single-quoted strings
  1342. (should (equal '("foo" "bar")
  1343. (org-babel-script-escape "('foo' 'bar')")))
  1344. (should (equal '("foo" "bar")
  1345. (org-babel-script-escape "['foo' 'bar']")))
  1346. (should (equal '("foo" "bar")
  1347. (org-babel-script-escape "{'foo' 'bar'}")))
  1348. ;; ... with commas
  1349. (should (equal '("foo" "bar")
  1350. (org-babel-script-escape "('foo', 'bar')")))
  1351. (should (equal '("foo" "bar")
  1352. (org-babel-script-escape "['foo', 'bar']")))
  1353. (should (equal '("foo" "bar")
  1354. (org-babel-script-escape "{'foo', 'bar'}")))
  1355. ;; Single quoted strings
  1356. (should (equal "foo"
  1357. (org-babel-script-escape "'foo'")))
  1358. ;; ... with internal double quote
  1359. (should (equal "foo\"bar"
  1360. (org-babel-script-escape "'foo\"bar'")))
  1361. ;; ... with internal backslash
  1362. (should (equal "foo\\bar"
  1363. (org-babel-script-escape "'foo\\bar'")))
  1364. ;; ... with internal escaped backslash
  1365. (should (equal "foo\\bar"
  1366. (org-babel-script-escape "'foo\\\\bar'")))
  1367. ;; ... with internal backslash-double quote
  1368. (should (equal "foo\\\"bar"
  1369. (org-babel-script-escape "'foo\\\"bar'")))
  1370. ;; ... with internal escaped backslash-double quote
  1371. (should (equal "foo\\\"bar"
  1372. (org-babel-script-escape "'foo\\\\\"bar'")))
  1373. ;; ... with internal escaped single quote
  1374. (should (equal "foo'bar"
  1375. (org-babel-script-escape "'foo\\'bar'")))
  1376. ;; ... with internal escaped backslash-escaped single quote
  1377. (should (equal "foo\\'bar"
  1378. (org-babel-script-escape "'foo\\\\\\'bar'")))
  1379. ;; Double quoted strings
  1380. (should (equal "foo"
  1381. (org-babel-script-escape "\"foo\"")))
  1382. ;; ... with internal single quote
  1383. (should (equal "foo'bar"
  1384. (org-babel-script-escape "\"foo'bar\"")))
  1385. ;; ... with internal backslash
  1386. (should (equal "foo\\bar"
  1387. (org-babel-script-escape "\"foo\\bar\"")))
  1388. ;; ... with internal escaped backslash
  1389. (should (equal "foo\\bar"
  1390. (org-babel-script-escape "\"foo\\\\bar\"")))
  1391. ;; ... with internal backslash-single quote
  1392. (should (equal "foo\\'bar"
  1393. (org-babel-script-escape "\"foo\\'bar\"")))
  1394. ;; ... with internal escaped backslash-single quote
  1395. (should (equal "foo\\'bar"
  1396. (org-babel-script-escape "\"foo\\\\'bar\"")))
  1397. ;; ... with internal escaped double quote
  1398. (should (equal "foo\"bar"
  1399. (org-babel-script-escape "\"foo\\\"bar\"")))
  1400. ;; ... with internal escaped backslash-escaped double quote
  1401. (should (equal "foo\\\"bar"
  1402. (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
  1403. (ert-deftest test-ob/process-params-no-duplicates ()
  1404. (should
  1405. (equal (org-babel-process-params '((:colname-names)
  1406. (:rowname-names)
  1407. (:result-params)
  1408. (:result-type)
  1409. (:var . "\"foo\"")))
  1410. '((:var)
  1411. (:colname-names)
  1412. (:rowname-names)
  1413. (:result-params)
  1414. (:result-type . value)))))
  1415. (defun org-test-babel-confirm-evaluate (eval-value)
  1416. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  1417. nil
  1418. #+end_src" eval-value)
  1419. (goto-char (point-min))
  1420. (let ((info (org-babel-get-src-block-info)))
  1421. (org-babel-check-confirm-evaluate info))))
  1422. (ert-deftest test-ob/check-eval ()
  1423. (let ((org-confirm-babel-evaluate t))
  1424. ;; Non-export tests
  1425. (dolist (pair '(("no" . nil)
  1426. ("never" . nil)
  1427. ("query" . query)
  1428. ("yes" . query)))
  1429. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1430. ;; Export tests
  1431. (let ((org-babel-exp-reference-buffer t))
  1432. (dolist (pair '(("no" . nil)
  1433. ("never" . nil)
  1434. ("query" . query)
  1435. ("yes" . query)
  1436. ("never-export" . nil)
  1437. ("no-export" . nil)
  1438. ("query-export" . query)))
  1439. (message (car pair))
  1440. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))))
  1441. (let ((org-confirm-babel-evaluate nil))
  1442. ;; Non-export tests
  1443. (dolist (pair '(("no" . nil)
  1444. ("never" . nil)
  1445. ("query" . query)
  1446. ("yes" . t)))
  1447. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1448. ;; Export tests
  1449. (let ((org-babel-exp-reference-buffer t))
  1450. (dolist (pair '(("no" . nil)
  1451. ("never" . nil)
  1452. ("query" . query)
  1453. ("yes" . t)
  1454. ("never-export" . nil)
  1455. ("no-export" . nil)
  1456. ("query-export" . query)))
  1457. (message (car pair))
  1458. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair)))))))
  1459. (defun org-test-ob/update-block-body ()
  1460. "Test `org-babel-update-block-body' specifications."
  1461. (should
  1462. (equal "#+begin_src elisp\n 2\n#+end_src"
  1463. (let ((org-edit-src-content-indentation 2))
  1464. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1465. (org-babel-update-block-body "2")
  1466. (buffer-string)))))
  1467. ;; Preserve block indentation.
  1468. (should
  1469. (equal " #+begin_src elisp\n 2\n #+end_src"
  1470. (let ((org-edit-src-content-indentation 1))
  1471. (org-test-with-temp-text
  1472. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1473. (org-babel-update-block-body "2")
  1474. (buffer-string)))))
  1475. ;; Ignore NEW-BODY global indentation.
  1476. (should
  1477. (equal "#+begin_src elisp\n 2\n#+end_src"
  1478. (let ((org-edit-src-content-indentation 2))
  1479. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1480. (org-babel-update-block-body " 2")
  1481. (buffer-string)))))
  1482. ;; When indentation should be preserved ignore the two rules above.
  1483. (should
  1484. (equal " #+begin_src elisp\n2\n #+end_src"
  1485. (let ((org-edit-src-content-indentation 1)
  1486. (org-src-preserve-indentation t))
  1487. (org-test-with-temp-text
  1488. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1489. (org-babel-update-block-body "2")
  1490. (buffer-string)))))
  1491. (should
  1492. (equal " #+begin_src elisp -i\n2\n #+end_src"
  1493. (let ((org-edit-src-content-indentation 1))
  1494. (org-test-with-temp-text
  1495. " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
  1496. (org-babel-update-block-body "2")
  1497. (buffer-string)))))
  1498. (should
  1499. (equal "#+begin_src elisp\n 2\n#+end_src"
  1500. (let ((org-edit-src-content-indentation 2)
  1501. (org-src-preserve-indentation t))
  1502. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1503. (org-babel-update-block-body " 2")
  1504. (buffer-string)))))
  1505. (should
  1506. (equal "#+begin_src elisp -i\n 2\n#+end_src"
  1507. (let ((org-edit-src-content-indentation 2)
  1508. (org-src-preserve-indentation t))
  1509. (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
  1510. (org-babel-update-block-body " 2")
  1511. (buffer-string))))))
  1512. (ert-deftest test-ob/find-named-result ()
  1513. "Test `org-babel-find-named-result' specifications."
  1514. (should
  1515. (= 1
  1516. (org-test-with-temp-text "#+results: foo\n: result"
  1517. (org-babel-find-named-result "foo"))))
  1518. (should-not
  1519. (org-test-with-temp-text "#+results: foo\n: result"
  1520. (org-babel-find-named-result "bar")))
  1521. (should-not
  1522. (org-test-with-temp-text "#+results: foobar\n: result"
  1523. (org-babel-find-named-result "foo")))
  1524. ;; Search is case insensitive.
  1525. (should
  1526. (org-test-with-temp-text "#+RESULTS: FOO\n: result"
  1527. (org-babel-find-named-result "foo")))
  1528. ;; Handle hash in results keyword.
  1529. (should
  1530. (org-test-with-temp-text "#+results[hash]: FOO\n: result"
  1531. (org-babel-find-named-result "foo")))
  1532. ;; Accept orphaned affiliated keywords.
  1533. (should
  1534. (org-test-with-temp-text "#+results: foo"
  1535. (org-babel-find-named-result "foo"))))
  1536. (ert-deftest test-ob/where-is-src-block-result ()
  1537. "Test `org-babel-where-is-src-block-result' specifications."
  1538. ;; Find anonymous results.
  1539. (should
  1540. (equal "#+RESULTS:"
  1541. (org-test-with-temp-text
  1542. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
  1543. (goto-char (org-babel-where-is-src-block-result))
  1544. (buffer-substring-no-properties (point) (line-end-position)))))
  1545. ;; Find named results. Those have priority over anonymous ones.
  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: example
  1555. : 2"
  1556. (goto-char (org-babel-where-is-src-block-result))
  1557. (buffer-substring-no-properties (point) (line-end-position)))))
  1558. (should
  1559. (equal "#+RESULTS: example"
  1560. (org-test-with-temp-text
  1561. "
  1562. <point>#+NAME: example
  1563. #+BEGIN_SRC emacs-lisp
  1564. \(+ 1 1)
  1565. #+END_SRC
  1566. #+RESULTS:
  1567. : fake
  1568. #+RESULTS: example
  1569. : 2"
  1570. (goto-char (org-babel-where-is-src-block-result))
  1571. (buffer-substring-no-properties (point) (line-end-position)))))
  1572. ;; Return nil when no result is found.
  1573. (should-not
  1574. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1575. (org-babel-where-is-src-block-result)))
  1576. (should-not
  1577. (org-test-with-temp-text
  1578. "- item\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n"
  1579. (org-babel-where-is-src-block-result)))
  1580. ;; When optional argument INSERT is non-nil, add RESULTS keyword
  1581. ;; whenever no RESULTS can be found.
  1582. (should
  1583. (equal
  1584. "#+RESULTS:"
  1585. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1586. (let ((org-babel-results-keyword "RESULTS"))
  1587. (goto-char (org-babel-where-is-src-block-result t)))
  1588. (buffer-substring-no-properties (point) (line-end-position)))))
  1589. ;; Insert a named RESULTS keyword if possible.
  1590. (should
  1591. (equal
  1592. "#+RESULTS: e"
  1593. (org-test-with-temp-text
  1594. "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1595. (let ((org-babel-results-keyword "RESULTS"))
  1596. (goto-char (org-babel-where-is-src-block-result t)))
  1597. (buffer-substring-no-properties (point) (line-end-position)))))
  1598. ;; When optional argument HASH is provided, clear RESULTS keyword
  1599. ;; and related contents if they do not match it.
  1600. (should
  1601. (equal
  1602. "#+RESULTS[bbbb]:"
  1603. (org-test-with-temp-text
  1604. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 3"
  1605. (let ((org-babel-results-keyword "RESULTS"))
  1606. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1607. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1608. (should
  1609. (equal
  1610. "#+RESULTS[bbbb]:"
  1611. (org-test-with-temp-text
  1612. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
  1613. (let ((org-babel-results-keyword "RESULTS"))
  1614. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1615. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1616. ;; Handle hashes with times.
  1617. (should
  1618. (equal
  1619. "#+RESULTS[<2014-03-04 00:41:10> bbbb]:"
  1620. (org-test-with-temp-text
  1621. "
  1622. <point>#+BEGIN_SRC emacs-lisp
  1623. \(+ 1 1)
  1624. #+END_SRC
  1625. #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1626. (let ((org-babel-results-keyword "RESULTS")
  1627. (org-babel-hash-show-time t))
  1628. (cl-letf (((symbol-function 'format-time-string)
  1629. (lambda (&rest _) "<2014-03-04 00:41:10>")))
  1630. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb"))
  1631. (org-trim (buffer-substring-no-properties (point) (point-max))))))))
  1632. (should
  1633. (equal
  1634. "#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1635. (org-test-with-temp-text
  1636. "
  1637. <point>#+BEGIN_SRC emacs-lisp
  1638. \(+ 1 1)
  1639. #+END_SRC
  1640. #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1641. (let ((org-babel-results-keyword "RESULTS")
  1642. (org-babel-hash-show-time t))
  1643. (cl-letf (((symbol-function 'format-time-string)
  1644. (lambda (&rest _) "<2014-03-04 00:41:10>")))
  1645. (goto-char (org-babel-where-is-src-block-result nil nil "aaaa"))
  1646. (org-trim (buffer-substring-no-properties (point) (point-max))))))))
  1647. ;; RESULTS keyword may not be the last affiliated keyword.
  1648. (should
  1649. (equal
  1650. "#+RESULTS[bbbb]:"
  1651. (org-test-with-temp-text
  1652. "
  1653. <point>#+BEGIN_SRC emacs-lisp
  1654. \(+ 1 1)
  1655. #+END_SRC
  1656. #+RESULTS[aaaa]:
  1657. #+NAME: e
  1658. : 3"
  1659. (let ((org-babel-results-keyword "RESULTS"))
  1660. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1661. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1662. ;; HASH does nothing if no RESULTS can be found. However, if INSERT
  1663. ;; is also non-nil, RESULTS keyword is inserted along with the
  1664. ;; expected hash.
  1665. (should
  1666. (equal
  1667. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1668. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1669. (org-babel-where-is-src-block-result nil nil "bbbb")
  1670. (buffer-string))))
  1671. (should
  1672. (equal
  1673. "#+RESULTS[bbbb]:"
  1674. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1675. (let ((org-babel-results-keyword "RESULTS"))
  1676. (goto-char (org-babel-where-is-src-block-result t nil "bbbb")))
  1677. (org-trim (buffer-substring-no-properties (point) (point-max)))))))
  1678. (ert-deftest test-ob/goto-named-src-block ()
  1679. "Test interactive use of `org-babel-goto-named-src-block'."
  1680. (org-test-with-temp-text-in-file
  1681. "
  1682. #+NAME: abc
  1683. #+BEGIN_SRC emacs-lisp :results value
  1684. (1+ 1)
  1685. #+END_SRC
  1686. #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
  1687. #+BEGIN_SRC emacs-lisp
  1688. <<abc>>
  1689. #+END_SRC
  1690. abc
  1691. #+RESULTS: abc
  1692. : 2
  1693. "
  1694. ;; non-existent name
  1695. (should-not
  1696. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
  1697. ;; correct name
  1698. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
  1699. (should (= 14 (point)))
  1700. ;; call line - autocompletion
  1701. (forward-line 3)
  1702. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1703. (should (= 14 (point)))
  1704. ;; noweb reference - autocompletion
  1705. (forward-line 5)
  1706. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1707. (should (= 14 (point)))
  1708. ;; at symbol - autocompletion
  1709. (forward-line 7)
  1710. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1711. (should (= 14 (point)))
  1712. ;; in results - autocompletion
  1713. (forward-line 8)
  1714. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1715. (should (= 14 (point)))
  1716. (forward-line 9)
  1717. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1718. (should (= 14 (point)))))
  1719. (ert-deftest test-ob/evaluate-body-with-coderefs ()
  1720. (should
  1721. (= 2
  1722. (org-test-with-temp-text
  1723. "#+begin_src emacs-lisp -l \"#(ref:%s)\"\n2 #(ref:foo)\n#+end_src"
  1724. (org-babel-execute-src-block))))
  1725. (should
  1726. (= 3
  1727. (org-test-with-temp-text
  1728. "#+begin_src emacs-lisp\n3 #(ref:foo)\n#+end_src"
  1729. (let ((org-coderef-label-format "#(ref:%s)"))
  1730. (org-babel-execute-src-block))))))
  1731. (provide 'test-ob)
  1732. ;;; test-ob ends here