test-ob.el 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  1. ;;; test-ob.el --- tests for ob.el
  2. ;; Copyright (c) 2010-2015, 2019 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. (eval-and-compile (require 'cl-lib))
  17. (ert-deftest test-ob/indented-cached-org-bracket-link ()
  18. "When the result of a source block is a cached indented link it
  19. should still return the link."
  20. (should
  21. (let ((default-directory temporary-file-directory))
  22. (org-test-with-temp-text
  23. "
  24. * Test
  25. #+<point>BEGIN_SRC emacs-lisp :results file :file test.txt :cache yes
  26. (message \"test\")
  27. #+END_SRC"
  28. ;; Execute twice as the first time creates the cache.
  29. (org-babel-execute-src-block)
  30. (string= (expand-file-name "test.txt")
  31. (org-babel-execute-src-block))))))
  32. (ert-deftest test-ob/multi-line-header-regexp ()
  33. (should(equal "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
  34. org-babel-multi-line-header-regexp))
  35. ;;TODO can be optimised - and what about blah4 blah5 blah6?
  36. (should (string-match
  37. org-babel-multi-line-header-regexp
  38. " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
  39. (should
  40. (equal
  41. "blah1 blah2 blah3 \t"
  42. (match-string
  43. 1
  44. " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
  45. ;;TODO Check - should this fail?
  46. (should
  47. (not (org-test-string-exact-match
  48. org-babel-multi-line-header-regexp
  49. " \t #+headers : blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))))
  50. (ert-deftest test-ob/src-block-regexp ()
  51. (let ((test-block
  52. (concat
  53. "#+begin_src language -n-r-a-b -c :argument-1 yes :argument-2 no\n"
  54. "echo this is a test\n"
  55. "echo Currently in ' $PWD\n"
  56. "#+end_src"))
  57. (language "language")
  58. (flags "-n-r-a-b -c ")
  59. (arguments ":argument-1 yes :argument-2 no")
  60. (body "echo this is a test\necho Currently in ' $PWD\n"))
  61. (should (string-match org-babel-src-block-regexp test-block))
  62. (should (string-match org-babel-src-block-regexp (upcase test-block)))
  63. (should (equal language (match-string 2 test-block)))
  64. ;;TODO Consider refactoring
  65. (should (equal flags (match-string 3 test-block)))
  66. (should (equal arguments (match-string 4 test-block)))
  67. (should (equal body (match-string 5 test-block)))
  68. ;;no switches
  69. (should (org-test-string-exact-match
  70. org-babel-src-block-regexp
  71. (replace-regexp-in-string flags "" test-block)))
  72. ;;no header arguments
  73. (should (org-test-string-exact-match
  74. org-babel-src-block-regexp
  75. (replace-regexp-in-string arguments "" test-block)))
  76. ;; should be valid with no body
  77. (should (org-test-string-exact-match
  78. org-babel-src-block-regexp
  79. (replace-regexp-in-string body "" test-block)))))
  80. (ert-deftest test-ob/default-inline-header-args ()
  81. (should(equal
  82. '((:session . "none")
  83. (:results . "replace")
  84. (:exports . "results")
  85. (:hlines . "yes"))
  86. org-babel-default-inline-header-args)))
  87. (ert-deftest ob-test/org-babel-combine-header-arg-lists ()
  88. (let ((results (org-babel-combine-header-arg-lists
  89. '((foo . :any)
  90. (bar)
  91. (baz . ((foo bar) (baz)))
  92. (qux . ((foo bar baz qux)))
  93. (quux . ((foo bar))))
  94. '((bar)
  95. (baz . ((baz)))
  96. (quux . :any)))))
  97. (dolist (pair '((foo . :any)
  98. (bar)
  99. (baz . ((baz)))
  100. (quux . :any)
  101. (qux . ((foo bar baz qux)))))
  102. (should (equal (cdr pair)
  103. (cdr (assoc (car pair) results)))))))
  104. ;;; ob-get-src-block-info
  105. (ert-deftest test-ob/get-src-block-info-language ()
  106. (org-test-at-marker nil org-test-file-ob-anchor
  107. (let ((info (org-babel-get-src-block-info)))
  108. (should (string= "emacs-lisp" (nth 0 info))))))
  109. (ert-deftest test-ob/get-src-block-info-body ()
  110. (org-test-at-marker nil org-test-file-ob-anchor
  111. (let ((info (org-babel-get-src-block-info)))
  112. (should (string-match (regexp-quote org-test-file-ob-anchor)
  113. (nth 1 info))))))
  114. (ert-deftest test-ob/get-src-block-info-tangle ()
  115. (org-test-at-marker nil org-test-file-ob-anchor
  116. (let ((info (org-babel-get-src-block-info)))
  117. (should (string= "no" (cdr (assq :tangle (nth 2 info))))))))
  118. (ert-deftest test-ob/elisp-in-header-arguments ()
  119. "Test execution of elisp forms in header arguments."
  120. (org-test-with-temp-text-in-file "
  121. * elisp forms in header arguments
  122. :PROPERTIES:
  123. :header-args: :var prop = (* 7 6)
  124. :END:
  125. #+begin_src emacs-lisp
  126. prop
  127. #+end_src"
  128. (goto-char (point-min))
  129. (org-babel-next-src-block)
  130. (should (= 42 (org-babel-execute-src-block)))))
  131. (ert-deftest test-ob/simple-named-code-block ()
  132. "Test that simple named code blocks can be evaluated."
  133. (org-test-with-temp-text-in-file "
  134. #+name: i-have-a-name
  135. #+begin_src emacs-lisp
  136. 42
  137. #+end_src"
  138. (org-babel-next-src-block 1)
  139. (should (= 42 (org-babel-execute-src-block)))))
  140. (ert-deftest test-ob/simple-variable-resolution ()
  141. "Test that simple variable resolution is working."
  142. (org-test-with-temp-text-in-file "
  143. #+name: four
  144. #+begin_src emacs-lisp
  145. (list 1 2 3 4)
  146. #+end_src
  147. #+begin_src emacs-lisp :var four=four
  148. (length four)
  149. #+end_src"
  150. (org-babel-next-src-block 2)
  151. (should (= 4 (org-babel-execute-src-block)))
  152. (forward-line 5)
  153. (should (string= ": 4" (buffer-substring
  154. (point-at-bol)
  155. (point-at-eol))))))
  156. (ert-deftest test-ob/multi-line-header-arguments ()
  157. "Test that multi-line header arguments and can be read."
  158. (org-test-with-temp-text-in-file "
  159. #+headers: :var letters='(a b c d e f g)
  160. #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
  161. (require 'cl-lib)
  162. (cl-map 'list #'list numbers letters)
  163. #+end_src"
  164. (org-babel-next-src-block)
  165. (let ((results (org-babel-execute-src-block)))
  166. (should (eq 'a (cadr (assoc 1 results))))
  167. (should (eq 'd (cadr (assoc 4 results)))))))
  168. (ert-deftest test-ob/parse-header-args ()
  169. (org-test-with-temp-text-in-file "
  170. #+begin_src example-lang :session :results output :var num=9
  171. the body
  172. #+end_src"
  173. (org-babel-next-src-block)
  174. (let* ((info (org-babel-get-src-block-info))
  175. (params (nth 2 info)))
  176. (message "%S" params)
  177. (should (equal "example-lang" (nth 0 info)))
  178. (should (string= "the body" (org-trim (nth 1 info))))
  179. (should-not (member '(:session\ \ \ \ ) params))
  180. (should (equal '(:session) (assq :session params)))
  181. (should (equal '(:result-type . output) (assq :result-type params)))
  182. (should (equal '(num . 9) (cdr (assq :var params)))))))
  183. (ert-deftest test-ob/parse-header-args2 ()
  184. (org-test-with-temp-text-in-file "
  185. * resolving sub-trees as references
  186. #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
  187. (length text)
  188. #+end_src
  189. #+begin_src org :noweb yes
  190. <<simple-subtree>>
  191. <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
  192. #+end_src
  193. ** simple subtree with custom ID
  194. :PROPERTIES:
  195. :CUSTOM_ID: simple-subtree
  196. :END:
  197. this is simple"
  198. (should (string-match (regexp-quote "this is simple")
  199. (org-babel-ref-resolve "simple-subtree")))
  200. (org-babel-next-src-block)
  201. (should (= 14 (org-babel-execute-src-block)))))
  202. (ert-deftest test-ob/inline-src-blocks ()
  203. (should
  204. (= 1
  205. (org-test-with-temp-text
  206. "In the middle <point>src_emacs-lisp{(+ 0 1)} of a line"
  207. (org-babel-execute-src-block))))
  208. (should
  209. (= 2
  210. (org-test-with-temp-text
  211. "One at the end of a line: <point>src_emacs-lisp{(+ 1 1)}"
  212. (org-babel-execute-src-block))))
  213. (should
  214. (= 3
  215. (org-test-with-temp-text
  216. "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
  217. (org-babel-execute-src-block))))
  218. (should
  219. (= 4
  220. (org-test-with-temp-text
  221. "In the middle <point>src_emacs-lisp[:results silent\
  222. :exports code]{(+ 3 1)} of a line"
  223. (org-babel-execute-src-block))))
  224. (should
  225. (= 5
  226. (org-test-with-temp-text
  227. "One at the end of a line: <point>src_emacs-lisp[:results silent\
  228. :exports code]{(+ 4 1)}"
  229. (org-babel-execute-src-block))))
  230. (should
  231. (= 6
  232. (org-test-with-temp-text
  233. "src_emacs-lisp[:results silent :exports code]{(+ 5 1)}\
  234. at the beginning of a line."
  235. (org-babel-execute-src-block))))
  236. (should
  237. (= 7
  238. (org-test-with-temp-text
  239. "One also evaluated: <point>src_emacs-lisp[:exports both\
  240. :results silent]{(+ 6 1)}"
  241. (org-babel-execute-src-block)))))
  242. (ert-deftest test-ob/inline-src_blk-default-results-replace-line-1 ()
  243. (let ((test-line "src_sh{echo 1}")
  244. (org-babel-inline-result-wrap "=%s="))
  245. ;; src_ at bol line 1...
  246. (org-test-with-temp-text
  247. test-line
  248. (goto-char (point-min)) (org-babel-execute-maybe)
  249. (should (string=
  250. (concat test-line " {{{results(=1=)}}}")
  251. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  252. (forward-char) (org-babel-execute-maybe)
  253. (should (string=
  254. (concat test-line " {{{results(=1=)}}}")
  255. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  256. (re-search-forward "{{{")
  257. ;;(should-error (org-ctrl-c-ctrl-c))
  258. (backward-char 4) ;; last char of block body
  259. (org-babel-execute-maybe)
  260. (should (string=
  261. (concat test-line " {{{results(=1=)}}}")
  262. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  263. ;; src_ follows space line 1...
  264. (let ((test-line " src_emacs-lisp{ 1 }"))
  265. (org-test-with-temp-text
  266. test-line
  267. (should-error (org-ctrl-c-ctrl-c))
  268. (forward-char) (org-babel-execute-maybe)
  269. (should (string=
  270. (concat test-line " {{{results(=1=)}}}")
  271. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  272. (re-search-forward "{ 1 ") (org-babel-execute-maybe)
  273. (should (string=
  274. (concat test-line " {{{results(=1=)}}}")
  275. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  276. (forward-char 6)
  277. (should-error (org-ctrl-c-ctrl-c))))
  278. ;; Results on a subsequent line are replaced.
  279. (should
  280. (equal
  281. "src_emacs-lisp{(+ 1 2)}\n {{{results(=3=)}}}"
  282. (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n {{{results(=2=)}}}"
  283. (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
  284. (buffer-string))))
  285. ;; Also handle results at the beginning of a line.
  286. (should
  287. (equal
  288. "src_emacs-lisp{(+ 1 2)}\n{{{results(=3=)}}}"
  289. (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n{{{results(=2=)}}}"
  290. (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
  291. (buffer-string))))))
  292. (ert-deftest test-ob/inline-src_blk-default-results-replace-line-2 ()
  293. ;; src_ at bol line 2...
  294. (let ((test-line " src_emacs-lisp{ \"x\" }")
  295. (org-babel-inline-result-wrap "=%s="))
  296. (org-test-with-temp-text
  297. (concat "\n" test-line)
  298. (should-error (org-ctrl-c-ctrl-c))
  299. (goto-char (point-min))
  300. (should-error (org-ctrl-c-ctrl-c))
  301. (forward-line)
  302. (should-error (org-ctrl-c-ctrl-c))
  303. (forward-char) (org-babel-execute-maybe)
  304. (should (string=
  305. (concat test-line " {{{results(=x=)}}}")
  306. (buffer-substring-no-properties
  307. (point-at-bol) (point-at-eol))))))
  308. (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
  309. (org-babel-inline-result-wrap "=%s="))
  310. (org-test-with-temp-text
  311. test-line
  312. (goto-char (point-max))
  313. (insert (concat "\n" test-line " end"))
  314. (re-search-backward "src") (org-babel-execute-maybe)
  315. (should (string=
  316. (concat test-line " {{{results(=y=)}}} end")
  317. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  318. (re-search-forward "\" ") (org-babel-execute-maybe)
  319. (should (string=
  320. (concat test-line " {{{results(=y=)}}} end")
  321. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  322. (forward-char 3)
  323. (should-error (org-ctrl-c-ctrl-c)))))
  324. (ert-deftest test-ob/inline-src_blk-manual-results-replace ()
  325. (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
  326. (org-babel-inline-result-wrap "=%s="))
  327. (org-test-with-temp-text
  328. (concat "\n" test-line)
  329. (should-error (org-ctrl-c-ctrl-c))
  330. (goto-char (point-max))
  331. (org-babel-execute-maybe)
  332. (beginning-of-line)
  333. (should-error (org-ctrl-c-ctrl-c))
  334. (forward-char) (org-babel-execute-maybe)
  335. (should (string=
  336. (concat test-line " {{{results(=x=)}}}")
  337. (buffer-substring-no-properties
  338. (point-at-bol) (point-at-eol))))))
  339. (let ((test-line (concat " Some text prior to block "
  340. "src_emacs-lisp[:results replace]{ \"y\" }"))
  341. (org-babel-inline-result-wrap "=%s="))
  342. (org-test-with-temp-text test-line
  343. (goto-char (point-max))
  344. (insert (concat "\n" test-line " end"))
  345. (re-search-backward "src") (org-babel-execute-maybe)
  346. (should (string=
  347. (concat test-line " {{{results(=y=)}}} end")
  348. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  349. (re-search-forward "\" ") (org-babel-execute-maybe)
  350. (should (string=
  351. (concat test-line " {{{results(=y=)}}} end")
  352. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  353. (forward-char 3)
  354. (should-error (org-ctrl-c-ctrl-c)))))
  355. (ert-deftest test-ob/inline-src_blk-results-silent ()
  356. (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
  357. (org-test-with-temp-text test-line
  358. (org-babel-execute-maybe)
  359. (should (string= test-line
  360. (buffer-substring-no-properties
  361. (point-at-bol) (point-at-eol))))))
  362. (let ((test-line (concat " Some text prior to block src_emacs-lisp"
  363. "[ :results silent ]{ \"y\" }")))
  364. (org-test-with-temp-text
  365. test-line
  366. (goto-char (point-max))
  367. (insert (concat "\n" test-line " end"))
  368. (re-search-backward "src_") (org-babel-execute-maybe)
  369. (should (string= (concat test-line " end")
  370. (buffer-substring-no-properties
  371. (point-at-bol) (point-at-eol))))
  372. (re-search-forward "\" ") (org-babel-execute-maybe)
  373. (should (string= (concat test-line " end")
  374. (buffer-substring-no-properties
  375. (point-at-bol) (point-at-eol))))
  376. (forward-char 2)
  377. (should-error (org-ctrl-c-ctrl-c)))))
  378. (ert-deftest test-ob/inline-src_blk-results-raw ()
  379. (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
  380. (org-test-with-temp-text test-line
  381. (org-babel-execute-maybe)
  382. (should (string= (concat test-line " x")
  383. (buffer-string)))))
  384. (let ((test-line (concat " Some text prior to block "
  385. "src_emacs-lisp[ :results raw ]{ \"the\" }")))
  386. (org-test-with-temp-text (concat test-line " end")
  387. (re-search-forward "src_") (org-babel-execute-maybe)
  388. (should (string= (concat test-line " the end")
  389. (buffer-substring-no-properties
  390. (point-at-bol) (point-at-eol))))
  391. (re-search-forward "\" ") (org-babel-execute-maybe)
  392. (should (string= (concat test-line " the the end")
  393. (buffer-substring-no-properties
  394. (point-at-bol) (point-at-eol))))
  395. (forward-char 2)
  396. (should-error (org-ctrl-c-ctrl-c)))))
  397. (ert-deftest test-ob/inline-src_blk-results-file ()
  398. (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
  399. (org-test-with-temp-text
  400. test-line
  401. (org-babel-execute-maybe)
  402. (should (string= (concat test-line " {{{results([[file:~/test-file]])}}}")
  403. (buffer-substring-no-properties
  404. (point-min) (point-max)))))))
  405. (ert-deftest test-ob/inline-src_blk-results-scalar ()
  406. (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
  407. (org-babel-inline-result-wrap "=%s="))
  408. (org-test-with-temp-text
  409. test-line
  410. (org-babel-execute-maybe)
  411. (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
  412. (buffer-substring-no-properties
  413. (point-min) (point-max)))))))
  414. (ert-deftest test-ob/inline-src_blk-results-verbatim ()
  415. (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  416. (org-babel-inline-result-wrap "=%s="))
  417. (org-test-with-temp-text
  418. test-line
  419. (org-babel-execute-maybe)
  420. (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
  421. (buffer-substring-no-properties
  422. (point-min) (point-max)))))))
  423. (ert-deftest test-ob/combining-scalar-and-raw-result-types ()
  424. (org-test-with-temp-text-in-file "
  425. #+begin_src sh :results scalar
  426. echo \"[[file:./cv.cls]]\"
  427. #+end_src
  428. #+name:
  429. : [[file:./cv.cls]]
  430. #+begin_src sh :results raw scalar
  431. echo \"[[file:./cv.cls]]\"
  432. #+end_src
  433. "
  434. (cl-flet ((next-result ()
  435. (org-babel-next-src-block)
  436. (org-babel-execute-src-block)
  437. (goto-char (org-babel-where-is-src-block-result))
  438. (forward-line 1)))
  439. (goto-char (point-min))
  440. (next-result)
  441. (should (eq (org-element-type (org-element-at-point)) 'fixed-width))
  442. (next-result)
  443. (should-not
  444. (eq (org-element-type (org-element-at-point)) 'fixed-width)))))
  445. (ert-deftest test-ob/no-defaut-value-for-var ()
  446. "Test that the absence of a default value for a variable DOES THROW
  447. a proper error."
  448. (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
  449. (org-babel-next-src-block)
  450. (let ((err
  451. (should-error (org-babel-execute-src-block) :type 'error)))
  452. (should
  453. (equal
  454. '(error
  455. "Variable \"x\" must be assigned a default value")
  456. err)))))
  457. (ert-deftest test-ob/just-one-results-block ()
  458. "Test that evaluating two times the same code block does not result in a
  459. duplicate results block."
  460. (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
  461. (org-babel-execute-src-block)
  462. (org-babel-execute-src-block) ; second code block execution
  463. (should (search-forward "Hello")) ; the string inside the source code block
  464. (should (search-forward "Hello")) ; the same string in the results block
  465. (should-error (search-forward "Hello"))))
  466. (ert-deftest test-ob/nested-code-block ()
  467. "Test nested code blocks inside code blocks don't cause problems."
  468. (should
  469. (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
  470. (org-test-with-temp-text "#+begin_src org :results silent
  471. ,#+begin_src emacs-lisp
  472. 'foo
  473. ,#+end_src
  474. #+end_src"
  475. (let ((org-edit-src-content-indentation 2)
  476. (org-src-preserve-indentation nil))
  477. (org-babel-execute-src-block))))))
  478. (ert-deftest test-ob/partial-nested-code-block ()
  479. "Test nested code blocks inside code blocks don't cause problems."
  480. (org-test-with-temp-text "#+begin_src org :results silent
  481. ,#+begin_src emacs-lisp
  482. #+end_src"
  483. (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
  484. (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
  485. (org-test-with-temp-text "#+NAME: foo
  486. #+BEGIN_SRC emacs-lisp
  487. 'foo
  488. #+END_SRC\n"
  489. (org-babel-next-src-block 1)
  490. (should (eq 'foo (org-babel-execute-src-block)))
  491. (goto-char (point-min))
  492. (org-babel-next-src-block 1)
  493. (should (looking-at org-babel-src-block-regexp))))
  494. (ert-deftest test-ob/catches-all-references ()
  495. (org-test-with-temp-text "
  496. #+NAME: literal-example
  497. #+BEGIN_EXAMPLE
  498. A literal example
  499. on two lines
  500. #+END_EXAMPLE
  501. #+NAME: read-literal-example
  502. #+BEGIN_SRC emacs-lisp :var x=literal-example
  503. (cl-concatenate 'string x \" for me.\")
  504. #+END_SRC"
  505. (org-babel-next-src-block 1)
  506. (should (string= (org-babel-execute-src-block)
  507. "A literal example\non two lines\n for me."))))
  508. (ert-deftest test-ob/ignore-reference-in-commented-headings ()
  509. (should
  510. (= 2
  511. (org-test-with-temp-text
  512. "
  513. * COMMENT H1
  514. #+NAME: n
  515. : 1
  516. * H2
  517. #+NAME: n
  518. : 2
  519. * Code
  520. <point>#+BEGIN_SRC emacs-lisp :var x=n
  521. x
  522. #+END_SRC"
  523. (org-babel-execute-src-block)))))
  524. (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
  525. (org-test-with-temp-text "
  526. #+name: base_plus
  527. | 1 |
  528. | 2 |
  529. #+name: base
  530. | 3 |
  531. | 4 |
  532. #+begin_src emacs-lisp :var x=base
  533. x
  534. #+end_src"
  535. (org-babel-next-src-block 1)
  536. (should (equal (org-babel-execute-src-block) '((3) (4))))))
  537. (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
  538. (org-test-with-temp-text "
  539. #+name: base_plus
  540. #+begin_src emacs-lisp
  541. 'bar
  542. #+end_src
  543. #+name: base
  544. #+begin_src emacs-lisp
  545. 'foo
  546. #+end_src
  547. #+begin_src emacs-lisp :var x=base
  548. x
  549. #+end_src"
  550. (org-babel-next-src-block 3)
  551. (should (equal (org-babel-execute-src-block) "foo"))))
  552. (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
  553. (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
  554. (+ a b c d)
  555. #+end_src
  556. "
  557. (should (= 10 (org-babel-execute-src-block)))))
  558. (ert-deftest test-ob/org-babel-update-intermediate ()
  559. (org-test-with-temp-text "#+name: foo
  560. #+begin_src emacs-lisp
  561. 2
  562. #+end_src
  563. #+results: foo
  564. : 4
  565. #+begin_src emacs-lisp :var it=foo
  566. (+ it 1)
  567. #+end_src"
  568. (let ((org-babel-update-intermediate nil))
  569. (goto-char (point-min))
  570. (org-babel-next-src-block 2)
  571. (should (= 3 (org-babel-execute-src-block)))
  572. (goto-char (point-min))
  573. (forward-line 6)
  574. (should (looking-at ": 4")))
  575. (let ((org-babel-update-intermediate t))
  576. (goto-char (point-min))
  577. (org-babel-next-src-block 2)
  578. (should (= 3 (org-babel-execute-src-block)))
  579. (goto-char (point-min))
  580. (forward-line 6)
  581. (should (looking-at ": 2")))))
  582. (ert-deftest test-ob/eval-header-argument ()
  583. (cl-flet ((check-eval (eval runp)
  584. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  585. (setq foo :evald)
  586. #+end_src" eval)
  587. (let ((foo :not-run))
  588. (if runp
  589. (progn (should (org-babel-execute-src-block))
  590. (should (eq foo :evald)))
  591. (progn (should-not (org-babel-execute-src-block))
  592. (should-not (eq foo :evald))))))))
  593. (check-eval "never" nil)
  594. (check-eval "no" nil)
  595. (check-eval "never-export" t)
  596. (check-eval "no-export" t)
  597. (let ((org-babel-exp-reference-buffer (current-buffer)))
  598. (check-eval "never" nil)
  599. (check-eval "no" nil)
  600. (check-eval "never-export" nil)
  601. (check-eval "no-export" nil))))
  602. (ert-deftest test-ob/noweb-expansion ()
  603. ;; Standard test.
  604. (should
  605. (string=
  606. "bar"
  607. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  608. <<foo>>
  609. #+end_src
  610. #+name: foo
  611. #+begin_src sh
  612. bar
  613. #+end_src"
  614. (org-babel-expand-noweb-references))))
  615. ;; Handle :noweb-sep.
  616. (should
  617. (string=
  618. "barbaz"
  619. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  620. <<foo>>
  621. #+end_src
  622. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  623. bar
  624. #+end_src
  625. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  626. baz
  627. #+end_src"
  628. (org-babel-expand-noweb-references))))
  629. ;; :noweb-ref is extracted from definition, not point of call.
  630. (should
  631. (string=
  632. "(+ 1 1)"
  633. (org-test-with-temp-text
  634. "
  635. * Call
  636. :PROPERTIES:
  637. :header-args: :noweb-ref bar
  638. :END:
  639. <point>#+begin_src emacs-lisp :results output :tangle yes
  640. <<foo>>
  641. #+end_src
  642. * Evaluation
  643. :PROPERTIES:
  644. :header-args: :noweb-ref foo
  645. :END:
  646. #+begin_src sh :noweb-sep \"\"
  647. (+ 1 1)
  648. #+end_src"
  649. (org-babel-expand-noweb-references))))
  650. ;; Handle recursive expansion.
  651. (should
  652. (equal "baz"
  653. (org-test-with-temp-text "
  654. #+begin_src emacs-lisp :noweb yes<point>
  655. <<foo>>
  656. #+end_src
  657. #+name: foo
  658. #+begin_src emacs-lisp :noweb yes
  659. <<bar>>
  660. #+end_src
  661. #+name: bar
  662. #+begin_src emacs-lisp
  663. baz
  664. #+end_src"
  665. (org-babel-expand-noweb-references))))
  666. ;; During recursive expansion, obey to `:noweb' property.
  667. (should
  668. (equal "<<bar>>"
  669. (org-test-with-temp-text "
  670. #+begin_src emacs-lisp :noweb yes<point>
  671. <<foo>>
  672. #+end_src
  673. #+name: foo
  674. #+begin_src emacs-lisp :noweb no
  675. <<bar>>
  676. #+end_src
  677. #+name: bar
  678. #+begin_src emacs-lisp
  679. baz
  680. #+end_src"
  681. (org-babel-expand-noweb-references)))))
  682. (ert-deftest test-ob/splitting-variable-lists-in-references ()
  683. (org-test-with-temp-text ""
  684. (should (= 1 (length (org-babel-ref-split-args
  685. "a=\"this, no work\""))))
  686. (should (= 2 (length (org-babel-ref-split-args
  687. "a=\"this, no work\", b=1"))))))
  688. (ert-deftest test-ob/balanced-split ()
  689. "Test `org-babel-balanced-split' specifications."
  690. (should (equal
  691. '(":a 1" "b [2 3]" "c (4 :d (5 6))")
  692. (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
  693. '((32 9) . 58))))
  694. ;; Handle un-balanced parens.
  695. (should
  696. (equal '(":foo ((6)" "bar 1")
  697. (org-babel-balanced-split ":foo ((6) :bar 1" '((32 9) . 58))))
  698. (should
  699. (equal '(":foo \"(foo\"" "bar 2")
  700. (org-babel-balanced-split ":foo \"(foo\" :bar 2" '((32 9) . 58))))
  701. ;; Handle un-balanced quotes.
  702. (should
  703. (equal '(":foo \"1" "bar 3")
  704. (org-babel-balanced-split ":foo \"1 :bar 3" '((32 9) . 58))))
  705. ;; Handle empty string.
  706. (should
  707. (equal '(":foo \"\"")
  708. (org-babel-balanced-split ":foo \"\"" '((32 9) . 58))))
  709. ;; Handle control characters within double quotes.
  710. (should
  711. (equal '(":foo \"\\n\"")
  712. (org-babel-balanced-split ":foo \"\\n\"" '((32 9) . 58)))))
  713. (ert-deftest test-ob/commented-last-block-line-no-var ()
  714. (org-test-with-temp-text-in-file "
  715. #+begin_src emacs-lisp
  716. ;;
  717. #+end_src"
  718. (org-babel-next-src-block)
  719. (org-babel-execute-maybe)
  720. (should (re-search-forward "\\#\\+results:" nil t))
  721. (forward-line)
  722. (should
  723. (string=
  724. ""
  725. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  726. (org-test-with-temp-text-in-file "
  727. #+begin_src emacs-lisp
  728. \"some text\";;
  729. #+end_src"
  730. (org-babel-next-src-block)
  731. (org-babel-execute-maybe)
  732. (should (re-search-forward "\\#\\+results:" nil t))
  733. (forward-line)
  734. (should
  735. (string=
  736. ": some text"
  737. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  738. (ert-deftest test-ob/commented-last-block-line-with-var ()
  739. (org-test-with-temp-text-in-file "
  740. #+begin_src emacs-lisp :var a=1
  741. ;;
  742. #+end_src"
  743. (org-babel-next-src-block)
  744. (org-babel-execute-maybe)
  745. (re-search-forward "\\#\\+results:" nil t)
  746. (forward-line)
  747. (should (string=
  748. ""
  749. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  750. (org-test-with-temp-text-in-file "
  751. #+begin_src emacs-lisp :var a=2
  752. 2;;
  753. #+end_src"
  754. (org-babel-next-src-block)
  755. (org-babel-execute-maybe)
  756. (re-search-forward "\\#\\+results:" nil t)
  757. (forward-line)
  758. (should (string=
  759. ": 2"
  760. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  761. (ert-deftest test-ob/org-babel-insert-result ()
  762. "Test `org-babel-insert-result' specifications."
  763. ;; Do not error when output is an improper list.
  764. (should
  765. (org-test-with-temp-text
  766. "
  767. <point>#+BEGIN_SRC emacs-lisp
  768. '((1 . nil) (2 . 3))
  769. #+END_SRC
  770. "
  771. (org-babel-execute-maybe) t))
  772. ;; Escape headlines when producing an example block.
  773. (should
  774. (string-match-p
  775. ",\\* Not an headline"
  776. (org-test-with-temp-text
  777. "
  778. <point>#+BEGIN_SRC emacs-lisp
  779. \"* Not an headline\"
  780. #+END_SRC
  781. "
  782. (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
  783. (buffer-string))))
  784. ;; Escape special syntax in example blocks.
  785. (should
  786. (string-match-p
  787. ",#\\+END_SRC"
  788. (org-test-with-temp-text
  789. "
  790. <point>#+BEGIN_SRC emacs-lisp
  791. \"#+END_SRC\"
  792. #+END_SRC
  793. "
  794. (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
  795. (buffer-string))))
  796. ;; No escaping is done with other blocks or raw type.
  797. (should-not
  798. (string-match-p
  799. ",\\* Not an headline"
  800. (org-test-with-temp-text
  801. "
  802. <point>#+BEGIN_SRC emacs-lisp
  803. \"* Not an headline\"
  804. #+END_SRC
  805. "
  806. (let ((org-babel-min-lines-for-block-output 10))
  807. (org-babel-execute-maybe))
  808. (buffer-string))))
  809. (should-not
  810. (string-match-p
  811. ",\\* Not an headline"
  812. (org-test-with-temp-text
  813. "
  814. <point>#+BEGIN_SRC emacs-lisp :results raw
  815. \"* Not an headline\"
  816. #+END_SRC
  817. "
  818. (org-babel-execute-maybe)
  819. (buffer-string))))
  820. (should-not
  821. (string-match-p
  822. ",\\* Not an headline"
  823. (org-test-with-temp-text
  824. "
  825. <point>#+BEGIN_SRC emacs-lisp :results drawer
  826. \"* Not an headline\"
  827. #+END_SRC
  828. "
  829. (org-babel-execute-maybe)
  830. (buffer-string)))))
  831. (ert-deftest test-ob/remove-inline-result ()
  832. "Test `org-babel-remove-inline-result' honors whitespace."
  833. (let*
  834. ((inline-sb "src_emacs-lisp{(+ 1 2)}")
  835. (inline-res " {{{results(=3=)}}}")
  836. (inline-sb-dot (concat inline-sb "."))
  837. (inline-sb-res-dot (concat inline-sb inline-res ".")))
  838. (org-test-with-temp-text
  839. ;; Insert inline_src_block followed by dot.
  840. inline-sb-dot
  841. ;; Insert result before dot.
  842. (org-babel-execute-maybe)
  843. (should (string= inline-sb-res-dot
  844. (buffer-substring-no-properties
  845. (point-at-bol) (point-at-eol))))
  846. ;; Delete whitespace and result.
  847. (org-babel-remove-inline-result)
  848. (should (string= inline-sb-dot
  849. (buffer-substring-no-properties
  850. (point-at-bol) (point-at-eol))))
  851. ;; Add whitespace and result before dot.
  852. (search-forward inline-sb)
  853. (insert " " inline-res)
  854. (goto-char (point-at-bol))
  855. ;; Remove whitespace and result.
  856. (org-babel-remove-inline-result)
  857. (should (string= inline-sb-dot
  858. (buffer-substring-no-properties
  859. (point-at-bol) (point-at-eol))))
  860. ;; Add whitespace before dot.
  861. (search-forward inline-sb)
  862. (insert " ")
  863. (goto-char (point-at-bol))
  864. ;; Add result before whitespace.
  865. (org-babel-execute-maybe)
  866. ;; Remove result - leave trailing whitespace and dot.
  867. (org-babel-remove-inline-result)
  868. (should (string= (concat inline-sb " .")
  869. (buffer-substring-no-properties
  870. (point-at-bol) (point-at-eol)))))))
  871. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  872. "Test `org-babel-remove-result' with default :results."
  873. (mapcar (lambda (language)
  874. (test-ob-verify-result-and-removed-result
  875. "\n"
  876. (concat
  877. "* org-babel-remove-result
  878. #+begin_src " language "
  879. #+end_src
  880. * next heading")))
  881. '("sh" "emacs-lisp")))
  882. (ert-deftest test-ob/org-babel-results-indented-wrap ()
  883. "Ensure that wrapped results are inserted correction when indented.
  884. If not inserted correctly then the second evaluation will fail
  885. trying to find the :END: marker."
  886. (org-test-with-temp-text
  887. "- indented
  888. #+begin_src sh :results file wrap
  889. echo test.txt
  890. #+end_src"
  891. (org-babel-next-src-block 1)
  892. (org-babel-execute-src-block)
  893. (org-babel-execute-src-block)))
  894. (ert-deftest test-ob/file-desc-header-argument ()
  895. "Test that the :file-desc header argument is used."
  896. (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  897. \"foo\"
  898. #+end_src
  899. #+begin_src emacs-lisp :results file :file-desc
  900. \"foo\"
  901. #+end_src"
  902. (org-babel-execute-src-block)
  903. (org-babel-next-src-block 1)
  904. (org-babel-execute-src-block)
  905. (goto-char (point-min))
  906. (should (search-forward "[[file:foo][bar]]" nil t))
  907. (should (search-forward "[[file:foo][foo]]" nil t))))
  908. (ert-deftest test-ob/result-file-link-type-header-argument ()
  909. "Ensure that the result is a link to a file.
  910. The file is just a link to `:file' value. Inhibit non-empty
  911. result write to `:file' value."
  912. (org-test-with-temp-text "
  913. <point>#+begin_src shell :results value file link :file \"/tmp/test.txt\"
  914. echo \"hello\" > /tmp/test.txt
  915. echo \"test\"
  916. #+end_src"
  917. (org-babel-execute-src-block)
  918. (should (search-forward "[[file:/tmp/test.txt]]" nil t))
  919. (should (with-temp-buffer
  920. (insert-file-contents "/tmp/test.txt")
  921. (string= "hello\n" (buffer-string)))))
  922. ;; Without "link" output type, the result is not a file.
  923. (should-not
  924. (org-test-with-temp-text "
  925. <point>#+begin_src shell :results value link :file \"/tmp/test.txt\"
  926. echo \"hello\" > /tmp/test.txt
  927. echo \"test\"
  928. #+end_src"
  929. (org-babel-execute-src-block)
  930. (search-forward "[[file:/tmp/test.txt]]" nil t))))
  931. (ert-deftest test-ob/result-graphics-link-type-header-argument ()
  932. "Ensure that the result is a link to a file.
  933. The file is just a link to `:file' value. Inhibit non-empty
  934. result write to `:file' value."
  935. (org-test-with-temp-text "
  936. <point>#+begin_src shell :results value file graphics :file \"/tmp/test.txt\"
  937. echo \"hello\" > /tmp/test.txt
  938. echo \"test\"
  939. #+end_src"
  940. (org-babel-execute-src-block)
  941. (should (search-forward "[[file:/tmp/test.txt]]" nil nil))
  942. (should (with-temp-buffer
  943. (insert-file-contents "/tmp/test.txt")
  944. (string= "hello\n" (buffer-string)))))
  945. ;; Without "link" output type, the result is not a file.
  946. (should-not
  947. (org-test-with-temp-text "
  948. <point>#+begin_src shell :results value graphics :file \"/tmp/test.txt\"
  949. echo \"hello\" > /tmp/test.txt
  950. echo \"test\"
  951. #+end_src"
  952. (org-babel-execute-src-block)
  953. (search-forward "[[file:/tmp/test.txt]]" nil t))))
  954. (ert-deftest test-ob/inline-src_blk-preceded-punct-preceded-by-point ()
  955. (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  956. (org-babel-inline-result-wrap "=%s="))
  957. (org-test-with-temp-text
  958. test-line
  959. (forward-char 1)
  960. (org-babel-execute-maybe)
  961. (should (re-search-forward "=\"x\"=" nil t))
  962. (forward-line))))
  963. (ert-deftest test-ob/commented-last-block-line-with-var ()
  964. (org-test-with-temp-text-in-file "
  965. #+begin_src emacs-lisp :var a=1
  966. ;;
  967. #+end_src"
  968. (org-babel-next-src-block)
  969. (org-babel-execute-maybe)
  970. (re-search-forward "\\#\\+results:" nil t)
  971. (forward-line)
  972. (should (string=
  973. ""
  974. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  975. (org-test-with-temp-text-in-file "
  976. #+begin_src emacs-lisp :var a=2
  977. 2;;
  978. #+end_src"
  979. (org-babel-next-src-block)
  980. (org-babel-execute-maybe)
  981. (re-search-forward "\\#\\+results:" nil t)
  982. (forward-line)
  983. (should (string=
  984. ": 2"
  985. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  986. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  987. "Test helper function to test `org-babel-remove-result'.
  988. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  989. and the result of execution is verified against RESULT.
  990. The block is actually executed /twice/ to ensure result
  991. replacement happens correctly."
  992. (org-test-with-temp-text
  993. buffer-text
  994. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  995. (should (re-search-forward "\\#\\+results:" nil t))
  996. (forward-line)
  997. (should (string= result
  998. (buffer-substring-no-properties
  999. (point-at-bol)
  1000. (- (point-max) 16))))
  1001. (org-babel-previous-src-block) (org-babel-remove-result)
  1002. (should (string= buffer-text
  1003. (buffer-substring-no-properties
  1004. (point-min) (point-max))))))
  1005. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  1006. "Test `org-babel-remove-result' with :results list."
  1007. (test-ob-verify-result-and-removed-result
  1008. "- 1
  1009. - 2
  1010. - 3"
  1011. "* org-babel-remove-result
  1012. #+begin_src emacs-lisp :results list
  1013. '(1 2 3)
  1014. #+end_src
  1015. * next heading"))
  1016. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  1017. "Test `org-babel-remove-result' with :results wrap."
  1018. (test-ob-verify-result-and-removed-result
  1019. ":results:
  1020. hello there
  1021. :end:"
  1022. "* org-babel-remove-result
  1023. #+begin_src emacs-lisp :results wrap
  1024. \"hello there\"
  1025. #+end_src
  1026. * next heading"))
  1027. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  1028. "Test `org-babel-remove-result' with :results org."
  1029. (test-ob-verify-result-and-removed-result
  1030. "#+begin_src org
  1031. ,* heading
  1032. ,** subheading
  1033. content
  1034. #+end_src"
  1035. "* org-babel-remove-result
  1036. #+begin_src emacs-lisp :results org
  1037. \"* heading
  1038. ,** subheading
  1039. content\"
  1040. #+end_src
  1041. * next heading"))
  1042. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  1043. "Test `org-babel-remove-result' with :results html."
  1044. (test-ob-verify-result-and-removed-result
  1045. "#+begin_export html
  1046. <head><body></body></head>
  1047. #+end_export"
  1048. "* org-babel-remove-result
  1049. #+begin_src emacs-lisp :results html
  1050. \"<head><body></body></head>\"
  1051. #+end_src
  1052. * next heading"))
  1053. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  1054. "Test `org-babel-remove-result' with :results latex."
  1055. (test-ob-verify-result-and-removed-result
  1056. "#+begin_export latex
  1057. Line 1
  1058. Line 2
  1059. Line 3
  1060. #+end_export"
  1061. "* org-babel-remove-result
  1062. #+begin_src emacs-lisp :results latex
  1063. \"Line 1
  1064. Line 2
  1065. Line 3\"
  1066. #+end_src
  1067. * next heading"))
  1068. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  1069. "Test `org-babel-remove-result' with :results code."
  1070. (test-ob-verify-result-and-removed-result
  1071. "#+begin_src emacs-lisp
  1072. \"I am working!\"
  1073. #+end_src"
  1074. "* org-babel-remove-result
  1075. #+begin_src emacs-lisp :results code
  1076. (message \"I am working!\")
  1077. #+end_src
  1078. * next heading"))
  1079. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  1080. "Test `org-babel-remove-result' with :results pp."
  1081. (test-ob-verify-result-and-removed-result
  1082. ": \"I /am/ working!\""
  1083. "* org-babel-remove-result
  1084. #+begin_src emacs-lisp :results pp
  1085. \"I /am/ working!\")
  1086. #+end_src
  1087. * next heading"))
  1088. (ert-deftest test-ob/org-babel-remove-result--no-blank-line ()
  1089. "Test `org-babel-remove-result' without blank line between code and results."
  1090. (should
  1091. (equal "
  1092. #+begin_src emacs-lisp
  1093. (+ 1 1)
  1094. #+end_src
  1095. #+results:
  1096. : 2
  1097. * next heading"
  1098. (org-test-with-temp-text
  1099. "
  1100. <point>#+begin_src emacs-lisp
  1101. (+ 1 1)
  1102. #+end_src
  1103. #+results:
  1104. : 2
  1105. * next heading"
  1106. (org-babel-execute-maybe)
  1107. (buffer-string)))))
  1108. (ert-deftest test-ob/results-do-not-replace-code-blocks ()
  1109. (org-test-with-temp-text "Block two has a space after the name.
  1110. #+name: foo
  1111. #+begin_src emacs-lisp
  1112. 1
  1113. #+end_src
  1114. #+name: foo
  1115. #+begin_src emacs-lisp
  1116. 2
  1117. #+end_src
  1118. #+name: foo
  1119. #+begin_src emacs-lisp
  1120. 3
  1121. #+end_src
  1122. #+RESULTS: foo
  1123. : foo
  1124. "
  1125. (dolist (num '(1 2 3))
  1126. ;; execute the block
  1127. (goto-char (point-min))
  1128. (org-babel-next-src-block num) (org-babel-execute-src-block)
  1129. ;; check the results
  1130. (goto-char (point-max))
  1131. (move-beginning-of-line 0)
  1132. (should (looking-at (format ": %d" num))))))
  1133. (ert-deftest test-ob/blocks-with-spaces ()
  1134. "Test expansion of blocks followed by blank lines."
  1135. ;; Preserve number of blank lines after block.
  1136. (should
  1137. (equal "#+BEGIN_SRC emacs-lisp
  1138. \(+ 1 2)
  1139. #+END_SRC
  1140. #+RESULTS:
  1141. : 3\n\n\n"
  1142. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
  1143. \(+ 1 2)
  1144. #+END_SRC\n\n\n"
  1145. (let ((org-babel-next-src-block "RESULTS"))
  1146. (org-babel-execute-src-block))
  1147. (buffer-string))))
  1148. ;; Do not add spurious blank lines after results.
  1149. (should
  1150. (equal
  1151. "
  1152. - item 1
  1153. #+begin_src emacs-lisp
  1154. 0
  1155. #+end_src
  1156. #+RESULTS:
  1157. : 0
  1158. - item 2"
  1159. (org-test-with-temp-text "
  1160. - item 1
  1161. #+begin_src emacs-lisp<point>
  1162. 0
  1163. #+end_src
  1164. - item 2"
  1165. (org-babel-execute-src-block)
  1166. (buffer-string))))
  1167. (should
  1168. (equal
  1169. "
  1170. - item 1
  1171. #+begin_src emacs-lisp
  1172. 1
  1173. #+end_src
  1174. #+RESULTS:
  1175. : 1
  1176. - item 2"
  1177. (org-test-with-temp-text "
  1178. - item 1
  1179. #+begin_src emacs-lisp<point>
  1180. 1
  1181. #+end_src
  1182. #+RESULTS:
  1183. : 1
  1184. - item 2"
  1185. (org-babel-execute-src-block)
  1186. (buffer-string)))))
  1187. (ert-deftest test-ob/results-in-narrowed-buffer ()
  1188. "Test block execution in a narrowed buffer."
  1189. ;; If results don't exist, they should be inserted in visible part
  1190. ;; of the buffer.
  1191. (should
  1192. (equal
  1193. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
  1194. (org-test-with-temp-text
  1195. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1196. (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
  1197. (let ((org-babel-results-keyword "RESULTS"))
  1198. (org-babel-execute-src-block))
  1199. (org-trim (buffer-string)))))
  1200. (should
  1201. (equal
  1202. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
  1203. (org-test-with-temp-text
  1204. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1205. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1206. (let ((org-babel-results-keyword "RESULTS"))
  1207. (org-babel-execute-src-block))
  1208. (org-trim (buffer-string)))))
  1209. ;; Results in visible part of buffer, should be updated here.
  1210. (should
  1211. (equal
  1212. "#+NAME: test
  1213. #+BEGIN_SRC emacs-lisp
  1214. \(+ 1 2)
  1215. #+END_SRC
  1216. #+RESULTS: test
  1217. : 3"
  1218. (org-test-with-temp-text
  1219. "#+NAME: test
  1220. #+BEGIN_SRC emacs-lisp
  1221. \(+ 1 2)
  1222. #+END_SRC
  1223. #+RESULTS: test
  1224. : 4
  1225. <point>
  1226. Paragraph"
  1227. (narrow-to-region (point-min) (point))
  1228. (goto-char (point-min))
  1229. (let ((org-babel-results-keyword "RESULTS"))
  1230. (org-babel-execute-src-block))
  1231. (org-trim (buffer-string)))))
  1232. ;; Results in invisible part of buffer, should be updated there.
  1233. (org-test-with-temp-text
  1234. "#+NAME: test
  1235. #+BEGIN_SRC emacs-lisp
  1236. \(+ 1 2)
  1237. #+END_SRC
  1238. #+RESULTS: test
  1239. : 4
  1240. Paragraph"
  1241. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1242. (let ((org-babel-results-keyword "RESULTS"))
  1243. (org-babel-execute-src-block))
  1244. (should-not (re-search-forward "^#\\+RESULTS:" nil t))
  1245. (widen)
  1246. (should (re-search-forward "^: 3" nil t))))
  1247. (ert-deftest test-ob/specific-colnames ()
  1248. "Test passing specific column names."
  1249. (should
  1250. (equal "#+name: input-table
  1251. | id | var1 |
  1252. |----+------|
  1253. | 1 | bar |
  1254. | 2 | baz |
  1255. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1256. echo \"$data\"
  1257. #+end_src
  1258. #+RESULTS:
  1259. | Rev | Author |
  1260. |-----+--------|
  1261. | 1 | bar |
  1262. | 2 | baz |
  1263. "
  1264. (org-test-with-temp-text
  1265. "#+name: input-table
  1266. | id | var1 |
  1267. |----+------|
  1268. | 1 | bar |
  1269. | 2 | baz |
  1270. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1271. echo \"$data\"
  1272. #+end_src
  1273. "
  1274. ;; we should find a code block
  1275. (should (re-search-forward org-babel-src-block-regexp nil t))
  1276. (goto-char (match-beginning 0))
  1277. ;; now that we've located the code block, it may be evaluated
  1278. (let ((org-babel-execute-src-block "RESULTS"))
  1279. (org-babel-execute-src-block))
  1280. (buffer-string)))))
  1281. (ert-deftest test-ob/location-of-header-arg-eval ()
  1282. "Test location of header argument evaluation."
  1283. (org-test-with-temp-text "
  1284. #+name: top-block
  1285. #+begin_src emacs-lisp :var pt=(point)
  1286. pt
  1287. #+end_src
  1288. #+name: bottom-block
  1289. #+begin_src emacs-lisp :var pt=top-block()
  1290. pt
  1291. #+end_src
  1292. "
  1293. ;; the value of the second block should be greater than the first
  1294. (should
  1295. (< (progn (re-search-forward org-babel-src-block-regexp nil t)
  1296. (goto-char (match-beginning 0))
  1297. (prog1 (save-match-data (org-babel-execute-src-block))
  1298. (goto-char (match-end 0))))
  1299. (progn (re-search-forward org-babel-src-block-regexp nil t)
  1300. (goto-char (match-beginning 0))
  1301. (org-babel-execute-src-block))))))
  1302. (ert-deftest test-ob/preserve-results-indentation ()
  1303. "Preserve indentation when executing a source block."
  1304. (should
  1305. (equal
  1306. '(2 2)
  1307. (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1308. (org-babel-execute-src-block)
  1309. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1310. (list (org-get-indentation)
  1311. (progn (forward-line) (org-get-indentation))))))
  1312. (should
  1313. (equal
  1314. '(2 2)
  1315. (org-test-with-temp-text
  1316. " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1317. (org-babel-execute-src-block)
  1318. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1319. (list (org-get-indentation)
  1320. (progn (forward-line) (org-get-indentation))))))
  1321. ;; Don't get fooled by TAB-based indentation.
  1322. (should
  1323. (equal
  1324. '(6 6)
  1325. (org-test-with-temp-text
  1326. "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
  1327. (setq tab-width 4)
  1328. (org-babel-execute-src-block)
  1329. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1330. (list (org-get-indentation)
  1331. (progn (forward-line) (org-get-indentation))))))
  1332. ;; Properly indent examplified blocks.
  1333. (should
  1334. (equal
  1335. " #+begin_example
  1336. 0
  1337. 1
  1338. 2
  1339. 3
  1340. 4
  1341. 5
  1342. 6
  1343. 7
  1344. 8
  1345. 9
  1346. #+end_example
  1347. "
  1348. (org-test-with-temp-text
  1349. " #+begin_src emacs-lisp :results output
  1350. (dotimes (i 10) (princ i) (princ \"\\n\"))
  1351. #+end_src"
  1352. (org-babel-execute-src-block)
  1353. (search-forward "begin_example")
  1354. (downcase
  1355. (buffer-substring-no-properties (line-beginning-position)
  1356. (point-max))))))
  1357. ;; Properly indent "org" blocks.
  1358. (should
  1359. (equal
  1360. " #+begin_src org
  1361. 0
  1362. 1
  1363. 2
  1364. 3
  1365. 4
  1366. 5
  1367. 6
  1368. 7
  1369. 8
  1370. 9
  1371. #+end_src
  1372. "
  1373. (org-test-with-temp-text
  1374. " #+begin_src emacs-lisp :results output org
  1375. (dotimes (i 10) (princ i) (princ \"\\n\"))
  1376. #+end_src"
  1377. (org-babel-execute-src-block)
  1378. (search-forward "begin_src org")
  1379. (downcase
  1380. (buffer-substring-no-properties (line-beginning-position)
  1381. (point-max)))))))
  1382. (ert-deftest test-ob/preserve-comma-escape ()
  1383. "Preserve comma escapes when inserting results."
  1384. (should
  1385. (equal
  1386. "#+begin_example
  1387. line 1
  1388. ,* headline 2
  1389. ,* headline 3
  1390. ,* headline 4
  1391. ,* headline 5
  1392. #+end_example
  1393. "
  1394. (org-test-with-temp-text "#+begin_src emacs-lisp :wrap example
  1395. \"line 1
  1396. ,* headline 2
  1397. ,* headline 3
  1398. ,* headline 4
  1399. ,* headline 5
  1400. \"
  1401. #+end_src
  1402. "
  1403. (org-babel-execute-src-block)
  1404. (let ((case-fold-search t)) (search-forward "result" nil t))
  1405. (downcase (buffer-substring-no-properties (line-beginning-position 2)
  1406. (point-max)))))))
  1407. (ert-deftest test-ob/safe-header-args ()
  1408. "Detect safe and unsafe header args."
  1409. (let ((safe-args '((:cache . "foo")
  1410. (:results . "output")
  1411. (:eval . "never")
  1412. (:eval . "query")))
  1413. (unsafe-args '((:eval . "yes")
  1414. (:results . "output file")
  1415. (:foo . "bar")))
  1416. (malformed-args '((bar . "foo")
  1417. ("foo" . "bar")
  1418. :foo))
  1419. (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
  1420. (dolist (arg safe-args)
  1421. (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
  1422. (dolist (arg unsafe-args)
  1423. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1424. (dolist (arg malformed-args)
  1425. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1426. (should (not (funcall safe-p (append safe-args unsafe-args))))))
  1427. (ert-deftest test-ob/noweb-expansions-in-cache ()
  1428. "Ensure that noweb expansions are expanded before caching."
  1429. (let ((noweb-expansions-in-cache-var 0))
  1430. (org-test-with-temp-text "
  1431. #+name: foo
  1432. #+begin_src emacs-lisp
  1433. \"I said\"
  1434. #+end_src
  1435. #+name: bar
  1436. #+begin_src emacs-lisp :noweb yes :cache yes
  1437. (setq noweb-expansions-in-cache-var
  1438. (+ 1 noweb-expansions-in-cache-var))
  1439. (concat <<foo>> \" check noweb expansions\")
  1440. #+end_src
  1441. "
  1442. ;; run the second block to create the cache
  1443. (goto-char (point-min))
  1444. (re-search-forward (regexp-quote "#+name: bar"))
  1445. (should (string= "I said check noweb expansions"
  1446. (org-babel-execute-src-block)))
  1447. (should (= noweb-expansions-in-cache-var 1))
  1448. ;; change the value of the first block
  1449. (goto-char (point-min))
  1450. (re-search-forward (regexp-quote "said"))
  1451. (goto-char (match-beginning 0))
  1452. (insert "haven't ")
  1453. (re-search-forward (regexp-quote "#+name: bar"))
  1454. (should (string= "I haven't said check noweb expansions"
  1455. (org-babel-execute-src-block)))
  1456. (should (= noweb-expansions-in-cache-var 2)))))
  1457. (ert-deftest test-ob/file-ext-and-output-dir ()
  1458. (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
  1459. (org-babel-next-src-block)
  1460. (should (equal "file-ext-basic.txt"
  1461. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1462. (org-babel-next-src-block)
  1463. (should (equal "foo/file-ext-dir-relative.txt"
  1464. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1465. (org-babel-next-src-block)
  1466. (should (equal "foo/file-ext-dir-relative-slash.txt"
  1467. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1468. (org-babel-next-src-block)
  1469. (should (equal "/tmp/file-ext-dir-absolute.txt"
  1470. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1471. (org-babel-next-src-block)
  1472. (should (equal "foo.bar"
  1473. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1474. (org-babel-next-src-block)
  1475. (should (equal "xxx/foo.bar"
  1476. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1477. ))
  1478. (ert-deftest test-ob-core/dir-mkdirp ()
  1479. "Test :mkdirp with :dir header combination."
  1480. (should-not
  1481. (org-test-with-temp-text-in-file
  1482. "#+begin_src emacs-lisp :dir \"data/code\"
  1483. t
  1484. #+end_src"
  1485. (org-babel-execute-src-block)
  1486. (message default-directory)
  1487. (file-directory-p "data/code")))
  1488. (should-not
  1489. (org-test-with-temp-text-in-file
  1490. "#+begin_src emacs-lisp :mkdirp no :dir \"data/code\"
  1491. t
  1492. #+end_src"
  1493. (org-babel-execute-src-block)
  1494. (message default-directory)
  1495. (file-directory-p "data/code")))
  1496. (should
  1497. (org-test-with-temp-text-in-file
  1498. "#+begin_src emacs-lisp :mkdirp yes :dir \"data/code\"
  1499. t
  1500. #+end_src"
  1501. (org-babel-execute-src-block)
  1502. (message default-directory)
  1503. (prog1 (file-directory-p "data/code")
  1504. (delete-directory "data" t))))
  1505. (should
  1506. (equal "/tmp/test-dir-no-mkdirp/"
  1507. (org-test-with-temp-text-in-file
  1508. "#+begin_src emacs-lisp :dir /tmp/test-dir-no-mkdirp
  1509. default-directory
  1510. #+end_src"
  1511. (org-babel-execute-src-block)))))
  1512. (ert-deftest test-ob/script-escape ()
  1513. ;; Delimited lists of numbers
  1514. (should (equal '(1 2 3)
  1515. (org-babel-script-escape "[1 2 3]")))
  1516. (should (equal '(1 2 3)
  1517. (org-babel-script-escape "{1 2 3}")))
  1518. (should (equal '(1 2 3)
  1519. (org-babel-script-escape "(1 2 3)")))
  1520. ;; Delimited lists of double-quoted strings
  1521. (should (equal '("foo" "bar")
  1522. (org-babel-script-escape "(\"foo\" \"bar\")")))
  1523. (should (equal '("foo" "bar")
  1524. (org-babel-script-escape "[\"foo\" \"bar\"]")))
  1525. (should (equal '("foo" "bar")
  1526. (org-babel-script-escape "{\"foo\" \"bar\"}")))
  1527. ;; ... with commas
  1528. (should (equal '("foo" "bar")
  1529. (org-babel-script-escape "(\"foo\", \"bar\")")))
  1530. (should (equal '("foo" "bar")
  1531. (org-babel-script-escape "[\"foo\", \"bar\"]")))
  1532. (should (equal '("foo" "bar")
  1533. (org-babel-script-escape "{\"foo\", \"bar\"}")))
  1534. ;; Delimited lists of single-quoted strings
  1535. (should (equal '("foo" "bar")
  1536. (org-babel-script-escape "('foo' 'bar')")))
  1537. (should (equal '("foo" "bar")
  1538. (org-babel-script-escape "['foo' 'bar']")))
  1539. (should (equal '("foo" "bar")
  1540. (org-babel-script-escape "{'foo' 'bar'}")))
  1541. ;; ... with commas
  1542. (should (equal '("foo" "bar")
  1543. (org-babel-script-escape "('foo', 'bar')")))
  1544. (should (equal '("foo" "bar")
  1545. (org-babel-script-escape "['foo', 'bar']")))
  1546. (should (equal '("foo" "bar")
  1547. (org-babel-script-escape "{'foo', 'bar'}")))
  1548. ;; Single quoted strings
  1549. (should (equal "foo"
  1550. (org-babel-script-escape "'foo'")))
  1551. ;; ... with internal double quote
  1552. (should (equal "foo\"bar"
  1553. (org-babel-script-escape "'foo\"bar'")))
  1554. ;; ... with internal backslash
  1555. (should (equal "foo\\bar"
  1556. (org-babel-script-escape "'foo\\bar'")))
  1557. ;; ... with internal escaped backslash
  1558. (should (equal "foo\\bar"
  1559. (org-babel-script-escape "'foo\\\\bar'")))
  1560. ;; ... with internal backslash-double quote
  1561. (should (equal "foo\\\"bar"
  1562. (org-babel-script-escape "'foo\\\"bar'")))
  1563. ;; ... with internal escaped backslash-double quote
  1564. (should (equal "foo\\\"bar"
  1565. (org-babel-script-escape "'foo\\\\\"bar'")))
  1566. ;; ... with internal escaped single quote
  1567. (should (equal "foo'bar"
  1568. (org-babel-script-escape "'foo\\'bar'")))
  1569. ;; ... with internal escaped backslash-escaped single quote
  1570. (should (equal "foo\\'bar"
  1571. (org-babel-script-escape "'foo\\\\\\'bar'")))
  1572. ;; Double quoted strings
  1573. (should (equal "foo"
  1574. (org-babel-script-escape "\"foo\"")))
  1575. ;; ... with internal single quote
  1576. (should (equal "foo'bar"
  1577. (org-babel-script-escape "\"foo'bar\"")))
  1578. ;; ... with internal backslash
  1579. (should (equal "foo\\bar"
  1580. (org-babel-script-escape "\"foo\\bar\"")))
  1581. ;; ... with internal escaped backslash
  1582. (should (equal "foo\\bar"
  1583. (org-babel-script-escape "\"foo\\\\bar\"")))
  1584. ;; ... with internal backslash-single quote
  1585. (should (equal "foo\\'bar"
  1586. (org-babel-script-escape "\"foo\\'bar\"")))
  1587. ;; ... with internal escaped backslash-single quote
  1588. (should (equal "foo\\'bar"
  1589. (org-babel-script-escape "\"foo\\\\'bar\"")))
  1590. ;; ... with internal escaped double quote
  1591. (should (equal "foo\"bar"
  1592. (org-babel-script-escape "\"foo\\\"bar\"")))
  1593. ;; ... with internal escaped backslash-escaped double quote
  1594. (should (equal "foo\\\"bar"
  1595. (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
  1596. (ert-deftest test-ob/process-params-no-duplicates ()
  1597. (should
  1598. (equal (org-babel-process-params '((:colname-names)
  1599. (:rowname-names)
  1600. (:result-params)
  1601. (:result-type)
  1602. (:var . "\"foo\"")))
  1603. '((:var)
  1604. (:colname-names)
  1605. (:rowname-names)
  1606. (:result-params)
  1607. (:result-type . value)))))
  1608. (defun org-test-babel-confirm-evaluate (eval-value)
  1609. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  1610. nil
  1611. #+end_src" eval-value)
  1612. (goto-char (point-min))
  1613. (let ((info (org-babel-get-src-block-info)))
  1614. (org-babel-check-confirm-evaluate info))))
  1615. (ert-deftest test-ob/check-eval ()
  1616. (let ((org-confirm-babel-evaluate t))
  1617. ;; Non-export tests
  1618. (dolist (pair '(("no" . nil)
  1619. ("never" . nil)
  1620. ("query" . query)
  1621. ("yes" . query)))
  1622. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1623. ;; Export tests
  1624. (let ((org-babel-exp-reference-buffer t))
  1625. (dolist (pair '(("no" . nil)
  1626. ("never" . nil)
  1627. ("query" . query)
  1628. ("yes" . query)
  1629. ("never-export" . nil)
  1630. ("no-export" . nil)
  1631. ("query-export" . query)))
  1632. (message (car pair))
  1633. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))))
  1634. (let ((org-confirm-babel-evaluate nil))
  1635. ;; Non-export tests
  1636. (dolist (pair '(("no" . nil)
  1637. ("never" . nil)
  1638. ("query" . query)
  1639. ("yes" . t)))
  1640. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1641. ;; Export tests
  1642. (let ((org-babel-exp-reference-buffer t))
  1643. (dolist (pair '(("no" . nil)
  1644. ("never" . nil)
  1645. ("query" . query)
  1646. ("yes" . t)
  1647. ("never-export" . nil)
  1648. ("no-export" . nil)
  1649. ("query-export" . query)))
  1650. (message (car pair))
  1651. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair)))))))
  1652. (defun org-test-ob/update-block-body ()
  1653. "Test `org-babel-update-block-body' specifications."
  1654. (should
  1655. (equal "#+begin_src elisp\n 2\n#+end_src"
  1656. (let ((org-edit-src-content-indentation 2))
  1657. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1658. (org-babel-update-block-body "2")
  1659. (buffer-string)))))
  1660. ;; Preserve block indentation.
  1661. (should
  1662. (equal " #+begin_src elisp\n 2\n #+end_src"
  1663. (let ((org-edit-src-content-indentation 1))
  1664. (org-test-with-temp-text
  1665. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1666. (org-babel-update-block-body "2")
  1667. (buffer-string)))))
  1668. ;; Ignore NEW-BODY global indentation.
  1669. (should
  1670. (equal "#+begin_src elisp\n 2\n#+end_src"
  1671. (let ((org-edit-src-content-indentation 2))
  1672. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1673. (org-babel-update-block-body " 2")
  1674. (buffer-string)))))
  1675. ;; When indentation should be preserved ignore the two rules above.
  1676. (should
  1677. (equal " #+begin_src elisp\n2\n #+end_src"
  1678. (let ((org-edit-src-content-indentation 1)
  1679. (org-src-preserve-indentation t))
  1680. (org-test-with-temp-text
  1681. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1682. (org-babel-update-block-body "2")
  1683. (buffer-string)))))
  1684. (should
  1685. (equal " #+begin_src elisp -i\n2\n #+end_src"
  1686. (let ((org-edit-src-content-indentation 1))
  1687. (org-test-with-temp-text
  1688. " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
  1689. (org-babel-update-block-body "2")
  1690. (buffer-string)))))
  1691. (should
  1692. (equal "#+begin_src elisp\n 2\n#+end_src"
  1693. (let ((org-edit-src-content-indentation 2)
  1694. (org-src-preserve-indentation t))
  1695. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1696. (org-babel-update-block-body " 2")
  1697. (buffer-string)))))
  1698. (should
  1699. (equal "#+begin_src elisp -i\n 2\n#+end_src"
  1700. (let ((org-edit-src-content-indentation 2)
  1701. (org-src-preserve-indentation t))
  1702. (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
  1703. (org-babel-update-block-body " 2")
  1704. (buffer-string))))))
  1705. (ert-deftest test-ob/find-named-result ()
  1706. "Test `org-babel-find-named-result' specifications."
  1707. (should
  1708. (= 1
  1709. (org-test-with-temp-text "#+results: foo\n: result"
  1710. (org-babel-find-named-result "foo"))))
  1711. (should-not
  1712. (org-test-with-temp-text "#+results: foo\n: result"
  1713. (org-babel-find-named-result "bar")))
  1714. (should-not
  1715. (org-test-with-temp-text "#+results: foobar\n: result"
  1716. (org-babel-find-named-result "foo")))
  1717. ;; Search is case insensitive.
  1718. (should
  1719. (org-test-with-temp-text "#+RESULTS: FOO\n: result"
  1720. (org-babel-find-named-result "foo")))
  1721. ;; Handle hash in results keyword.
  1722. (should
  1723. (org-test-with-temp-text "#+results[hash]: FOO\n: result"
  1724. (org-babel-find-named-result "foo")))
  1725. ;; Accept orphaned affiliated keywords.
  1726. (should
  1727. (org-test-with-temp-text "#+results: foo"
  1728. (org-babel-find-named-result "foo"))))
  1729. (ert-deftest test-ob/where-is-src-block-result ()
  1730. "Test `org-babel-where-is-src-block-result' specifications."
  1731. ;; Find anonymous results.
  1732. (should
  1733. (equal "#+RESULTS:"
  1734. (org-test-with-temp-text
  1735. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
  1736. (goto-char (org-babel-where-is-src-block-result))
  1737. (buffer-substring-no-properties (point) (line-end-position)))))
  1738. ;; Find named results. Those have priority over anonymous ones.
  1739. (should
  1740. (equal "#+RESULTS: example"
  1741. (org-test-with-temp-text
  1742. "
  1743. <point>#+NAME: example
  1744. #+BEGIN_SRC emacs-lisp
  1745. \(+ 1 1)
  1746. #+END_SRC
  1747. #+RESULTS: example
  1748. : 2"
  1749. (goto-char (org-babel-where-is-src-block-result))
  1750. (buffer-substring-no-properties (point) (line-end-position)))))
  1751. (should
  1752. (equal "#+RESULTS: example"
  1753. (org-test-with-temp-text
  1754. "
  1755. <point>#+NAME: example
  1756. #+BEGIN_SRC emacs-lisp
  1757. \(+ 1 1)
  1758. #+END_SRC
  1759. #+RESULTS:
  1760. : fake
  1761. #+RESULTS: example
  1762. : 2"
  1763. (goto-char (org-babel-where-is-src-block-result))
  1764. (buffer-substring-no-properties (point) (line-end-position)))))
  1765. ;; Return nil when no result is found.
  1766. (should-not
  1767. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1768. (org-babel-where-is-src-block-result)))
  1769. (should-not
  1770. (org-test-with-temp-text
  1771. "- item\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n"
  1772. (org-babel-where-is-src-block-result)))
  1773. ;; When optional argument INSERT is non-nil, add RESULTS keyword
  1774. ;; whenever no RESULTS can be found.
  1775. (should
  1776. (equal
  1777. "#+RESULTS:"
  1778. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1779. (let ((org-babel-results-keyword "RESULTS"))
  1780. (goto-char (org-babel-where-is-src-block-result t)))
  1781. (buffer-substring-no-properties (point) (line-end-position)))))
  1782. ;; Insert a named RESULTS keyword if possible.
  1783. (should
  1784. (equal
  1785. "#+RESULTS: e"
  1786. (org-test-with-temp-text
  1787. "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1788. (let ((org-babel-results-keyword "RESULTS"))
  1789. (goto-char (org-babel-where-is-src-block-result t)))
  1790. (buffer-substring-no-properties (point) (line-end-position)))))
  1791. ;; When optional argument HASH is provided, clear RESULTS keyword
  1792. ;; and related contents if they do not match it.
  1793. (should
  1794. (equal
  1795. "#+RESULTS[bbbb]:"
  1796. (org-test-with-temp-text
  1797. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 3"
  1798. (let ((org-babel-results-keyword "RESULTS"))
  1799. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1800. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1801. (should
  1802. (equal
  1803. "#+RESULTS[bbbb]:"
  1804. (org-test-with-temp-text
  1805. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
  1806. (let ((org-babel-results-keyword "RESULTS"))
  1807. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1808. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1809. ;; Handle hashes with times.
  1810. (should
  1811. (equal
  1812. "#+RESULTS[<2014-03-04 00:41:10> bbbb]:"
  1813. (org-test-with-temp-text
  1814. "
  1815. <point>#+BEGIN_SRC emacs-lisp
  1816. \(+ 1 1)
  1817. #+END_SRC
  1818. #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1819. (let ((org-babel-results-keyword "RESULTS")
  1820. (org-babel-hash-show-time t))
  1821. (cl-letf (((symbol-function 'format-time-string)
  1822. (lambda (&rest _) "<2014-03-04 00:41:10>")))
  1823. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb"))
  1824. (org-trim (buffer-substring-no-properties (point) (point-max))))))))
  1825. (should
  1826. (equal
  1827. "#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1828. (org-test-with-temp-text
  1829. "
  1830. <point>#+BEGIN_SRC emacs-lisp
  1831. \(+ 1 1)
  1832. #+END_SRC
  1833. #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1834. (let ((org-babel-results-keyword "RESULTS")
  1835. (org-babel-hash-show-time t))
  1836. (cl-letf (((symbol-function 'format-time-string)
  1837. (lambda (&rest _) "<2014-03-04 00:41:10>")))
  1838. (goto-char (org-babel-where-is-src-block-result nil nil "aaaa"))
  1839. (org-trim (buffer-substring-no-properties (point) (point-max))))))))
  1840. ;; RESULTS keyword may not be the last affiliated keyword.
  1841. (should
  1842. (equal
  1843. "#+RESULTS[bbbb]:"
  1844. (org-test-with-temp-text
  1845. "
  1846. <point>#+BEGIN_SRC emacs-lisp
  1847. \(+ 1 1)
  1848. #+END_SRC
  1849. #+RESULTS[aaaa]:
  1850. #+NAME: e
  1851. : 3"
  1852. (let ((org-babel-results-keyword "RESULTS"))
  1853. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1854. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1855. ;; HASH does nothing if no RESULTS can be found. However, if INSERT
  1856. ;; is also non-nil, RESULTS keyword is inserted along with the
  1857. ;; expected hash.
  1858. (should
  1859. (equal
  1860. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1861. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1862. (org-babel-where-is-src-block-result nil nil "bbbb")
  1863. (buffer-string))))
  1864. (should
  1865. (equal
  1866. "#+RESULTS[bbbb]:"
  1867. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1868. (let ((org-babel-results-keyword "RESULTS"))
  1869. (goto-char (org-babel-where-is-src-block-result t nil "bbbb")))
  1870. (org-trim (buffer-substring-no-properties (point) (point-max)))))))
  1871. (ert-deftest test-ob/goto-named-src-block ()
  1872. "Test interactive use of `org-babel-goto-named-src-block'."
  1873. (org-test-with-temp-text-in-file
  1874. "
  1875. #+NAME: abc
  1876. #+BEGIN_SRC emacs-lisp :results value
  1877. (1+ 1)
  1878. #+END_SRC
  1879. #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
  1880. #+BEGIN_SRC emacs-lisp
  1881. <<abc>>
  1882. #+END_SRC
  1883. abc
  1884. #+RESULTS: abc
  1885. : 2
  1886. "
  1887. ;; non-existent name
  1888. (should-not
  1889. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
  1890. ;; correct name
  1891. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
  1892. (should (= 14 (point)))
  1893. ;; call line - autocompletion
  1894. (forward-line 3)
  1895. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1896. (should (= 14 (point)))
  1897. ;; noweb reference - autocompletion
  1898. (forward-line 5)
  1899. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1900. (should (= 14 (point)))
  1901. ;; at symbol - autocompletion
  1902. (forward-line 7)
  1903. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1904. (should (= 14 (point)))
  1905. ;; in results - autocompletion
  1906. (forward-line 8)
  1907. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1908. (should (= 14 (point)))
  1909. (forward-line 9)
  1910. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1911. (should (= 14 (point)))))
  1912. (ert-deftest test-ob/evaluate-body-with-coderefs ()
  1913. (should
  1914. (= 2
  1915. (org-test-with-temp-text
  1916. "#+begin_src emacs-lisp -l \"#(ref:%s)\"\n2 #(ref:foo)\n#+end_src"
  1917. (org-babel-execute-src-block))))
  1918. (should
  1919. (= 3
  1920. (org-test-with-temp-text
  1921. "#+begin_src emacs-lisp\n3 #(ref:foo)\n#+end_src"
  1922. (let ((org-coderef-label-format "#(ref:%s)"))
  1923. (org-babel-execute-src-block))))))
  1924. (ert-deftest test-ob/string-to-number ()
  1925. (should (= 0 (org-babel--string-to-number "0")))
  1926. (should (= 1 (org-babel--string-to-number "1")))
  1927. (should (eq nil (org-babel--string-to-number "000")))
  1928. (should (eq nil (org-babel--string-to-number "001")))
  1929. (should (eq nil (org-babel--string-to-number "010")))
  1930. (should (= 100 (org-babel--string-to-number "100")))
  1931. (should (= 0.1 (org-babel--string-to-number "0.1")))
  1932. (should (= 1.0 (org-babel--string-to-number "1.0"))))
  1933. (provide 'test-ob)
  1934. ;;; test-ob ends here