test-ob.el 59 KB

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