test-ob.el 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  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/replace-special-block-result ()
  495. (should-error
  496. (org-test-with-temp-text "
  497. #+begin_src emacs-lisp :wrap special<point>
  498. 'foo
  499. #+end_src"
  500. (org-babel-execute-src-block)
  501. (org-babel-execute-src-block)
  502. (buffer-string)
  503. (search-forward "#+begin_special" nil nil 2))))
  504. (ert-deftest test-ob/catches-all-references ()
  505. (org-test-with-temp-text "
  506. #+NAME: literal-example
  507. #+BEGIN_EXAMPLE
  508. A literal example
  509. on two lines
  510. #+END_EXAMPLE
  511. #+NAME: read-literal-example
  512. #+BEGIN_SRC emacs-lisp :var x=literal-example
  513. (cl-concatenate 'string x \" for me.\")
  514. #+END_SRC"
  515. (org-babel-next-src-block 1)
  516. (should (string= (org-babel-execute-src-block)
  517. "A literal example\non two lines\n for me."))))
  518. (ert-deftest test-ob/ignore-reference-in-commented-headings ()
  519. (should
  520. (= 2
  521. (org-test-with-temp-text
  522. "
  523. * COMMENT H1
  524. #+NAME: n
  525. : 1
  526. * H2
  527. #+NAME: n
  528. : 2
  529. * Code
  530. <point>#+BEGIN_SRC emacs-lisp :var x=n
  531. x
  532. #+END_SRC"
  533. (org-babel-execute-src-block)))))
  534. (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
  535. (org-test-with-temp-text "
  536. #+name: base_plus
  537. | 1 |
  538. | 2 |
  539. #+name: base
  540. | 3 |
  541. | 4 |
  542. #+begin_src emacs-lisp :var x=base
  543. x
  544. #+end_src"
  545. (org-babel-next-src-block 1)
  546. (should (equal (org-babel-execute-src-block) '((3) (4))))))
  547. (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
  548. (org-test-with-temp-text "
  549. #+name: base_plus
  550. #+begin_src emacs-lisp
  551. 'bar
  552. #+end_src
  553. #+name: base
  554. #+begin_src emacs-lisp
  555. 'foo
  556. #+end_src
  557. #+begin_src emacs-lisp :var x=base
  558. x
  559. #+end_src"
  560. (org-babel-next-src-block 3)
  561. (should (equal (org-babel-execute-src-block) "foo"))))
  562. (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
  563. (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
  564. (+ a b c d)
  565. #+end_src
  566. "
  567. (should (= 10 (org-babel-execute-src-block)))))
  568. (ert-deftest test-ob/org-babel-update-intermediate ()
  569. (org-test-with-temp-text "#+name: foo
  570. #+begin_src emacs-lisp
  571. 2
  572. #+end_src
  573. #+results: foo
  574. : 4
  575. #+begin_src emacs-lisp :var it=foo
  576. (+ it 1)
  577. #+end_src"
  578. (let ((org-babel-update-intermediate nil))
  579. (goto-char (point-min))
  580. (org-babel-next-src-block 2)
  581. (should (= 3 (org-babel-execute-src-block)))
  582. (goto-char (point-min))
  583. (forward-line 6)
  584. (should (looking-at ": 4")))
  585. (let ((org-babel-update-intermediate t))
  586. (goto-char (point-min))
  587. (org-babel-next-src-block 2)
  588. (should (= 3 (org-babel-execute-src-block)))
  589. (goto-char (point-min))
  590. (forward-line 6)
  591. (should (looking-at ": 2")))))
  592. (ert-deftest test-ob/eval-header-argument ()
  593. (cl-flet ((check-eval (eval runp)
  594. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  595. (setq foo :evald)
  596. #+end_src" eval)
  597. (let ((foo :not-run))
  598. (if runp
  599. (progn (should (org-babel-execute-src-block))
  600. (should (eq foo :evald)))
  601. (progn (should-not (org-babel-execute-src-block))
  602. (should-not (eq foo :evald))))))))
  603. (check-eval "never" nil)
  604. (check-eval "no" nil)
  605. (check-eval "never-export" t)
  606. (check-eval "no-export" t)
  607. (let ((org-babel-exp-reference-buffer (current-buffer)))
  608. (check-eval "never" nil)
  609. (check-eval "no" nil)
  610. (check-eval "never-export" nil)
  611. (check-eval "no-export" nil))))
  612. (ert-deftest test-ob/noweb-expansion ()
  613. ;; Standard test.
  614. (should
  615. (string=
  616. "bar"
  617. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  618. <<foo>>
  619. #+end_src
  620. #+name: foo
  621. #+begin_src sh
  622. bar
  623. #+end_src"
  624. (org-babel-expand-noweb-references))))
  625. ;; Handle :noweb-sep.
  626. (should
  627. (string=
  628. "barbaz"
  629. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  630. <<foo>>
  631. #+end_src
  632. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  633. bar
  634. #+end_src
  635. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  636. baz
  637. #+end_src"
  638. (org-babel-expand-noweb-references))))
  639. ;; :noweb-ref is extracted from definition, not point of call.
  640. (should
  641. (string=
  642. "(+ 1 1)"
  643. (org-test-with-temp-text
  644. "
  645. * Call
  646. :PROPERTIES:
  647. :header-args: :noweb-ref bar
  648. :END:
  649. <point>#+begin_src emacs-lisp :results output :tangle yes
  650. <<foo>>
  651. #+end_src
  652. * Evaluation
  653. :PROPERTIES:
  654. :header-args: :noweb-ref foo
  655. :END:
  656. #+begin_src sh :noweb-sep \"\"
  657. (+ 1 1)
  658. #+end_src"
  659. (org-babel-expand-noweb-references))))
  660. ;; Handle recursive expansion.
  661. (should
  662. (equal "baz"
  663. (org-test-with-temp-text "
  664. #+begin_src emacs-lisp :noweb yes<point>
  665. <<foo>>
  666. #+end_src
  667. #+name: foo
  668. #+begin_src emacs-lisp :noweb yes
  669. <<bar>>
  670. #+end_src
  671. #+name: bar
  672. #+begin_src emacs-lisp
  673. baz
  674. #+end_src"
  675. (org-babel-expand-noweb-references))))
  676. ;; During recursive expansion, obey to `:noweb' property.
  677. (should
  678. (equal "<<bar>>"
  679. (org-test-with-temp-text "
  680. #+begin_src emacs-lisp :noweb yes<point>
  681. <<foo>>
  682. #+end_src
  683. #+name: foo
  684. #+begin_src emacs-lisp :noweb no
  685. <<bar>>
  686. #+end_src
  687. #+name: bar
  688. #+begin_src emacs-lisp
  689. baz
  690. #+end_src"
  691. (org-babel-expand-noweb-references))))
  692. ;; Respect COMMENT headlines
  693. (should
  694. (equal "C"
  695. (org-test-with-temp-text "
  696. #+begin_src emacs-lisp :noweb yes<point>
  697. <<foo>>
  698. #+end_src
  699. * COMMENT A
  700. #+name: foo
  701. #+begin_src emacs-lisp
  702. A
  703. #+end_src
  704. * COMMENT B
  705. #+begin_src emacs-lisp :noweb-ref foo
  706. B
  707. #+end_src
  708. * C
  709. #+begin_src emacs-lisp :noweb-ref foo
  710. C
  711. #+end_src"
  712. (org-babel-expand-noweb-references)))))
  713. (ert-deftest test-ob/splitting-variable-lists-in-references ()
  714. (org-test-with-temp-text ""
  715. (should (= 1 (length (org-babel-ref-split-args
  716. "a=\"this, no work\""))))
  717. (should (= 2 (length (org-babel-ref-split-args
  718. "a=\"this, no work\", b=1"))))))
  719. (ert-deftest test-ob/balanced-split ()
  720. "Test `org-babel-balanced-split' specifications."
  721. (should (equal
  722. '(":a 1" "b [2 3]" "c (4 :d (5 6))")
  723. (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
  724. '((32 9) . 58))))
  725. ;; Handle un-balanced parens.
  726. (should
  727. (equal '(":foo ((6)" "bar 1")
  728. (org-babel-balanced-split ":foo ((6) :bar 1" '((32 9) . 58))))
  729. (should
  730. (equal '(":foo \"(foo\"" "bar 2")
  731. (org-babel-balanced-split ":foo \"(foo\" :bar 2" '((32 9) . 58))))
  732. ;; Handle un-balanced quotes.
  733. (should
  734. (equal '(":foo \"1" "bar 3")
  735. (org-babel-balanced-split ":foo \"1 :bar 3" '((32 9) . 58))))
  736. ;; Handle empty string.
  737. (should
  738. (equal '(":foo \"\"")
  739. (org-babel-balanced-split ":foo \"\"" '((32 9) . 58))))
  740. ;; Handle control characters within double quotes.
  741. (should
  742. (equal '(":foo \"\\n\"")
  743. (org-babel-balanced-split ":foo \"\\n\"" '((32 9) . 58)))))
  744. (ert-deftest test-ob/commented-last-block-line-no-var ()
  745. (org-test-with-temp-text-in-file "
  746. #+begin_src emacs-lisp
  747. ;;
  748. #+end_src"
  749. (org-babel-next-src-block)
  750. (org-babel-execute-maybe)
  751. (should (re-search-forward "\\#\\+results:" nil t))
  752. (forward-line)
  753. (should
  754. (string=
  755. ""
  756. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  757. (org-test-with-temp-text-in-file "
  758. #+begin_src emacs-lisp
  759. \"some text\";;
  760. #+end_src"
  761. (org-babel-next-src-block)
  762. (org-babel-execute-maybe)
  763. (should (re-search-forward "\\#\\+results:" nil t))
  764. (forward-line)
  765. (should
  766. (string=
  767. ": some text"
  768. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  769. (ert-deftest test-ob/commented-last-block-line-with-var ()
  770. (org-test-with-temp-text-in-file "
  771. #+begin_src emacs-lisp :var a=1
  772. ;;
  773. #+end_src"
  774. (org-babel-next-src-block)
  775. (org-babel-execute-maybe)
  776. (re-search-forward "\\#\\+results:" nil t)
  777. (forward-line)
  778. (should (string=
  779. ""
  780. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  781. (org-test-with-temp-text-in-file "
  782. #+begin_src emacs-lisp :var a=2
  783. 2;;
  784. #+end_src"
  785. (org-babel-next-src-block)
  786. (org-babel-execute-maybe)
  787. (re-search-forward "\\#\\+results:" nil t)
  788. (forward-line)
  789. (should (string=
  790. ": 2"
  791. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  792. (ert-deftest test-ob/org-babel-insert-result ()
  793. "Test `org-babel-insert-result' specifications."
  794. ;; Do not error when output is an improper list.
  795. (should
  796. (org-test-with-temp-text
  797. "
  798. <point>#+BEGIN_SRC emacs-lisp
  799. '((1 . nil) (2 . 3))
  800. #+END_SRC
  801. "
  802. (org-babel-execute-maybe) t))
  803. ;; Escape headlines when producing an example block.
  804. (should
  805. (string-match-p
  806. ",\\* Not an headline"
  807. (org-test-with-temp-text
  808. "
  809. <point>#+BEGIN_SRC emacs-lisp
  810. \"* Not an headline\"
  811. #+END_SRC
  812. "
  813. (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
  814. (buffer-string))))
  815. ;; Escape special syntax in example blocks.
  816. (should
  817. (string-match-p
  818. ",#\\+END_SRC"
  819. (org-test-with-temp-text
  820. "
  821. <point>#+BEGIN_SRC emacs-lisp
  822. \"#+END_SRC\"
  823. #+END_SRC
  824. "
  825. (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
  826. (buffer-string))))
  827. ;; No escaping is done with other blocks or raw type.
  828. (should-not
  829. (string-match-p
  830. ",\\* Not an headline"
  831. (org-test-with-temp-text
  832. "
  833. <point>#+BEGIN_SRC emacs-lisp
  834. \"* Not an headline\"
  835. #+END_SRC
  836. "
  837. (let ((org-babel-min-lines-for-block-output 10))
  838. (org-babel-execute-maybe))
  839. (buffer-string))))
  840. (should-not
  841. (string-match-p
  842. ",\\* Not an headline"
  843. (org-test-with-temp-text
  844. "
  845. <point>#+BEGIN_SRC emacs-lisp :results raw
  846. \"* Not an headline\"
  847. #+END_SRC
  848. "
  849. (org-babel-execute-maybe)
  850. (buffer-string))))
  851. (should-not
  852. (string-match-p
  853. ",\\* Not an headline"
  854. (org-test-with-temp-text
  855. "
  856. <point>#+BEGIN_SRC emacs-lisp :results drawer
  857. \"* Not an headline\"
  858. #+END_SRC
  859. "
  860. (org-babel-execute-maybe)
  861. (buffer-string)))))
  862. (ert-deftest test-ob/remove-inline-result ()
  863. "Test `org-babel-remove-inline-result' honors whitespace."
  864. (let*
  865. ((inline-sb "src_emacs-lisp{(+ 1 2)}")
  866. (inline-res " {{{results(=3=)}}}")
  867. (inline-sb-dot (concat inline-sb "."))
  868. (inline-sb-res-dot (concat inline-sb inline-res ".")))
  869. (org-test-with-temp-text
  870. ;; Insert inline_src_block followed by dot.
  871. inline-sb-dot
  872. ;; Insert result before dot.
  873. (org-babel-execute-maybe)
  874. (should (string= inline-sb-res-dot
  875. (buffer-substring-no-properties
  876. (point-at-bol) (point-at-eol))))
  877. ;; Delete whitespace and result.
  878. (org-babel-remove-inline-result)
  879. (should (string= inline-sb-dot
  880. (buffer-substring-no-properties
  881. (point-at-bol) (point-at-eol))))
  882. ;; Add whitespace and result before dot.
  883. (search-forward inline-sb)
  884. (insert " " inline-res)
  885. (goto-char (point-at-bol))
  886. ;; Remove whitespace and result.
  887. (org-babel-remove-inline-result)
  888. (should (string= inline-sb-dot
  889. (buffer-substring-no-properties
  890. (point-at-bol) (point-at-eol))))
  891. ;; Add whitespace before dot.
  892. (search-forward inline-sb)
  893. (insert " ")
  894. (goto-char (point-at-bol))
  895. ;; Add result before whitespace.
  896. (org-babel-execute-maybe)
  897. ;; Remove result - leave trailing whitespace and dot.
  898. (org-babel-remove-inline-result)
  899. (should (string= (concat inline-sb " .")
  900. (buffer-substring-no-properties
  901. (point-at-bol) (point-at-eol)))))))
  902. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  903. "Test `org-babel-remove-result' with default :results."
  904. (mapcar (lambda (language)
  905. (test-ob-verify-result-and-removed-result
  906. "\n"
  907. (concat
  908. "* org-babel-remove-result
  909. #+begin_src " language "
  910. #+end_src
  911. * next heading")))
  912. '("sh" "emacs-lisp")))
  913. (ert-deftest test-ob/org-babel-results-indented-wrap ()
  914. "Ensure that wrapped results are inserted correction when indented.
  915. If not inserted correctly then the second evaluation will fail
  916. trying to find the :END: marker."
  917. (org-test-with-temp-text
  918. "- indented
  919. #+begin_src sh :results file wrap
  920. echo test.txt
  921. #+end_src"
  922. (org-babel-next-src-block 1)
  923. (org-babel-execute-src-block)
  924. (org-babel-execute-src-block)))
  925. (ert-deftest test-ob/file-desc-header-argument ()
  926. "Test that the :file-desc header argument is used."
  927. (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  928. \"foo\"
  929. #+end_src
  930. #+begin_src emacs-lisp :results file :file-desc
  931. \"foo\"
  932. #+end_src"
  933. (org-babel-execute-src-block)
  934. (org-babel-next-src-block 1)
  935. (org-babel-execute-src-block)
  936. (goto-char (point-min))
  937. (should (search-forward "[[file:foo][bar]]" nil t))
  938. (should (search-forward "[[file:foo][foo]]" nil t))))
  939. (ert-deftest test-ob/result-file-link-type-header-argument ()
  940. "Ensure that the result is a link to a file.
  941. The file is just a link to `:file' value. Inhibit non-empty
  942. result write to `:file' value."
  943. (org-test-with-temp-text "
  944. <point>#+begin_src shell :results value file link :file \"/tmp/test.txt\"
  945. echo \"hello\" > /tmp/test.txt
  946. echo \"test\"
  947. #+end_src"
  948. (org-babel-execute-src-block)
  949. (should (search-forward "[[file:/tmp/test.txt]]" nil t))
  950. (should (with-temp-buffer
  951. (insert-file-contents "/tmp/test.txt")
  952. (string= "hello\n" (buffer-string)))))
  953. ;; Without "link" output type, the result is not a file.
  954. (should-not
  955. (org-test-with-temp-text "
  956. <point>#+begin_src shell :results value link :file \"/tmp/test.txt\"
  957. echo \"hello\" > /tmp/test.txt
  958. echo \"test\"
  959. #+end_src"
  960. (org-babel-execute-src-block)
  961. (search-forward "[[file:/tmp/test.txt]]" nil t))))
  962. (ert-deftest test-ob/result-graphics-link-type-header-argument ()
  963. "Ensure that the result is a link to a file.
  964. The file is just a link to `:file' value. Inhibit non-empty
  965. result write to `:file' value."
  966. (org-test-with-temp-text "
  967. <point>#+begin_src shell :results value file graphics :file \"/tmp/test.txt\"
  968. echo \"hello\" > /tmp/test.txt
  969. echo \"test\"
  970. #+end_src"
  971. (org-babel-execute-src-block)
  972. (should (search-forward "[[file:/tmp/test.txt]]" nil nil))
  973. (should (with-temp-buffer
  974. (insert-file-contents "/tmp/test.txt")
  975. (string= "hello\n" (buffer-string)))))
  976. ;; Without "link" output type, the result is not a file.
  977. (should-not
  978. (org-test-with-temp-text "
  979. <point>#+begin_src shell :results value graphics :file \"/tmp/test.txt\"
  980. echo \"hello\" > /tmp/test.txt
  981. echo \"test\"
  982. #+end_src"
  983. (org-babel-execute-src-block)
  984. (search-forward "[[file:/tmp/test.txt]]" nil t))))
  985. (ert-deftest test-ob/inline-src_blk-preceded-punct-preceded-by-point ()
  986. (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  987. (org-babel-inline-result-wrap "=%s="))
  988. (org-test-with-temp-text
  989. test-line
  990. (forward-char 1)
  991. (org-babel-execute-maybe)
  992. (should (re-search-forward "=\"x\"=" nil t))
  993. (forward-line))))
  994. (ert-deftest test-ob/commented-last-block-line-with-var ()
  995. (org-test-with-temp-text-in-file "
  996. #+begin_src emacs-lisp :var a=1
  997. ;;
  998. #+end_src"
  999. (org-babel-next-src-block)
  1000. (org-babel-execute-maybe)
  1001. (re-search-forward "\\#\\+results:" nil t)
  1002. (forward-line)
  1003. (should (string=
  1004. ""
  1005. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  1006. (org-test-with-temp-text-in-file "
  1007. #+begin_src emacs-lisp :var a=2
  1008. 2;;
  1009. #+end_src"
  1010. (org-babel-next-src-block)
  1011. (org-babel-execute-maybe)
  1012. (re-search-forward "\\#\\+results:" nil t)
  1013. (forward-line)
  1014. (should (string=
  1015. ": 2"
  1016. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  1017. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  1018. "Test helper function to test `org-babel-remove-result'.
  1019. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  1020. and the result of execution is verified against RESULT.
  1021. The block is actually executed /twice/ to ensure result
  1022. replacement happens correctly."
  1023. (org-test-with-temp-text
  1024. buffer-text
  1025. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  1026. (should (re-search-forward "\\#\\+results:" nil t))
  1027. (forward-line)
  1028. (should (string= result
  1029. (buffer-substring-no-properties
  1030. (point-at-bol)
  1031. (- (point-max) 16))))
  1032. (org-babel-previous-src-block) (org-babel-remove-result)
  1033. (should (string= buffer-text
  1034. (buffer-substring-no-properties
  1035. (point-min) (point-max))))))
  1036. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  1037. "Test `org-babel-remove-result' with :results list."
  1038. (test-ob-verify-result-and-removed-result
  1039. "- 1
  1040. - 2
  1041. - 3"
  1042. "* org-babel-remove-result
  1043. #+begin_src emacs-lisp :results list
  1044. '(1 2 3)
  1045. #+end_src
  1046. * next heading"))
  1047. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  1048. "Test `org-babel-remove-result' with :results wrap."
  1049. (test-ob-verify-result-and-removed-result
  1050. ":results:
  1051. hello there
  1052. :end:"
  1053. "* org-babel-remove-result
  1054. #+begin_src emacs-lisp :results wrap
  1055. \"hello there\"
  1056. #+end_src
  1057. * next heading"))
  1058. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  1059. "Test `org-babel-remove-result' with :results org."
  1060. (test-ob-verify-result-and-removed-result
  1061. "#+begin_src org
  1062. ,* heading
  1063. ,** subheading
  1064. content
  1065. #+end_src"
  1066. "* org-babel-remove-result
  1067. #+begin_src emacs-lisp :results org
  1068. \"* heading
  1069. ,** subheading
  1070. content\"
  1071. #+end_src
  1072. * next heading"))
  1073. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  1074. "Test `org-babel-remove-result' with :results html."
  1075. (test-ob-verify-result-and-removed-result
  1076. "#+begin_export html
  1077. <head><body></body></head>
  1078. #+end_export"
  1079. "* org-babel-remove-result
  1080. #+begin_src emacs-lisp :results html
  1081. \"<head><body></body></head>\"
  1082. #+end_src
  1083. * next heading"))
  1084. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  1085. "Test `org-babel-remove-result' with :results latex."
  1086. (test-ob-verify-result-and-removed-result
  1087. "#+begin_export latex
  1088. Line 1
  1089. Line 2
  1090. Line 3
  1091. #+end_export"
  1092. "* org-babel-remove-result
  1093. #+begin_src emacs-lisp :results latex
  1094. \"Line 1
  1095. Line 2
  1096. Line 3\"
  1097. #+end_src
  1098. * next heading"))
  1099. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  1100. "Test `org-babel-remove-result' with :results code."
  1101. (test-ob-verify-result-and-removed-result
  1102. "#+begin_src emacs-lisp
  1103. \"I am working!\"
  1104. #+end_src"
  1105. "* org-babel-remove-result
  1106. #+begin_src emacs-lisp :results code
  1107. (message \"I am working!\")
  1108. #+end_src
  1109. * next heading"))
  1110. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  1111. "Test `org-babel-remove-result' with :results pp."
  1112. (test-ob-verify-result-and-removed-result
  1113. ": \"I /am/ working!\""
  1114. "* org-babel-remove-result
  1115. #+begin_src emacs-lisp :results pp
  1116. \"I /am/ working!\")
  1117. #+end_src
  1118. * next heading"))
  1119. (ert-deftest test-ob/org-babel-remove-result--no-blank-line ()
  1120. "Test `org-babel-remove-result' without blank line between code and results."
  1121. (should
  1122. (equal "
  1123. #+begin_src emacs-lisp
  1124. (+ 1 1)
  1125. #+end_src
  1126. #+results:
  1127. : 2
  1128. * next heading"
  1129. (org-test-with-temp-text
  1130. "
  1131. <point>#+begin_src emacs-lisp
  1132. (+ 1 1)
  1133. #+end_src
  1134. #+results:
  1135. : 2
  1136. * next heading"
  1137. (org-babel-execute-maybe)
  1138. (buffer-string)))))
  1139. (ert-deftest test-ob/results-do-not-replace-code-blocks ()
  1140. (org-test-with-temp-text "Block two has a space after the name.
  1141. #+name: foo
  1142. #+begin_src emacs-lisp
  1143. 1
  1144. #+end_src
  1145. #+name: foo
  1146. #+begin_src emacs-lisp
  1147. 2
  1148. #+end_src
  1149. #+name: foo
  1150. #+begin_src emacs-lisp
  1151. 3
  1152. #+end_src
  1153. #+RESULTS: foo
  1154. : foo
  1155. "
  1156. (dolist (num '(1 2 3))
  1157. ;; execute the block
  1158. (goto-char (point-min))
  1159. (org-babel-next-src-block num) (org-babel-execute-src-block)
  1160. ;; check the results
  1161. (goto-char (point-max))
  1162. (move-beginning-of-line 0)
  1163. (should (looking-at (format ": %d" num))))))
  1164. (ert-deftest test-ob/blocks-with-spaces ()
  1165. "Test expansion of blocks followed by blank lines."
  1166. ;; Preserve number of blank lines after block.
  1167. (should
  1168. (equal "#+BEGIN_SRC emacs-lisp
  1169. \(+ 1 2)
  1170. #+END_SRC
  1171. #+RESULTS:
  1172. : 3\n\n\n"
  1173. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
  1174. \(+ 1 2)
  1175. #+END_SRC\n\n\n"
  1176. (let ((org-babel-next-src-block "RESULTS"))
  1177. (org-babel-execute-src-block))
  1178. (buffer-string))))
  1179. ;; Do not add spurious blank lines after results.
  1180. (should
  1181. (equal
  1182. "
  1183. - item 1
  1184. #+begin_src emacs-lisp
  1185. 0
  1186. #+end_src
  1187. #+RESULTS:
  1188. : 0
  1189. - item 2"
  1190. (org-test-with-temp-text "
  1191. - item 1
  1192. #+begin_src emacs-lisp<point>
  1193. 0
  1194. #+end_src
  1195. - item 2"
  1196. (org-babel-execute-src-block)
  1197. (buffer-string))))
  1198. (should
  1199. (equal
  1200. "
  1201. - item 1
  1202. #+begin_src emacs-lisp
  1203. 1
  1204. #+end_src
  1205. #+RESULTS:
  1206. : 1
  1207. - item 2"
  1208. (org-test-with-temp-text "
  1209. - item 1
  1210. #+begin_src emacs-lisp<point>
  1211. 1
  1212. #+end_src
  1213. #+RESULTS:
  1214. : 1
  1215. - item 2"
  1216. (org-babel-execute-src-block)
  1217. (buffer-string)))))
  1218. (ert-deftest test-ob/results-in-narrowed-buffer ()
  1219. "Test block execution in a narrowed buffer."
  1220. ;; If results don't exist, they should be inserted in visible part
  1221. ;; of the buffer.
  1222. (should
  1223. (equal
  1224. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
  1225. (org-test-with-temp-text
  1226. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1227. (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
  1228. (let ((org-babel-results-keyword "RESULTS"))
  1229. (org-babel-execute-src-block))
  1230. (org-trim (buffer-string)))))
  1231. (should
  1232. (equal
  1233. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
  1234. (org-test-with-temp-text
  1235. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1236. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1237. (let ((org-babel-results-keyword "RESULTS"))
  1238. (org-babel-execute-src-block))
  1239. (org-trim (buffer-string)))))
  1240. ;; Results in visible part of buffer, should be updated here.
  1241. (should
  1242. (equal
  1243. "#+NAME: test
  1244. #+BEGIN_SRC emacs-lisp
  1245. \(+ 1 2)
  1246. #+END_SRC
  1247. #+RESULTS: test
  1248. : 3"
  1249. (org-test-with-temp-text
  1250. "#+NAME: test
  1251. #+BEGIN_SRC emacs-lisp
  1252. \(+ 1 2)
  1253. #+END_SRC
  1254. #+RESULTS: test
  1255. : 4
  1256. <point>
  1257. Paragraph"
  1258. (narrow-to-region (point-min) (point))
  1259. (goto-char (point-min))
  1260. (let ((org-babel-results-keyword "RESULTS"))
  1261. (org-babel-execute-src-block))
  1262. (org-trim (buffer-string)))))
  1263. ;; Results in invisible part of buffer, should be updated there.
  1264. (org-test-with-temp-text
  1265. "#+NAME: test
  1266. #+BEGIN_SRC emacs-lisp
  1267. \(+ 1 2)
  1268. #+END_SRC
  1269. #+RESULTS: test
  1270. : 4
  1271. Paragraph"
  1272. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1273. (let ((org-babel-results-keyword "RESULTS"))
  1274. (org-babel-execute-src-block))
  1275. (should-not (re-search-forward "^#\\+RESULTS:" nil t))
  1276. (widen)
  1277. (should (re-search-forward "^: 3" nil t))))
  1278. (ert-deftest test-ob/specific-colnames ()
  1279. "Test passing specific column names."
  1280. (should
  1281. (equal "#+name: input-table
  1282. | id | var1 |
  1283. |----+------|
  1284. | 1 | bar |
  1285. | 2 | baz |
  1286. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1287. echo \"$data\"
  1288. #+end_src
  1289. #+RESULTS:
  1290. | Rev | Author |
  1291. |-----+--------|
  1292. | 1 | bar |
  1293. | 2 | baz |
  1294. "
  1295. (org-test-with-temp-text
  1296. "#+name: input-table
  1297. | id | var1 |
  1298. |----+------|
  1299. | 1 | bar |
  1300. | 2 | baz |
  1301. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1302. echo \"$data\"
  1303. #+end_src
  1304. "
  1305. ;; we should find a code block
  1306. (should (re-search-forward org-babel-src-block-regexp nil t))
  1307. (goto-char (match-beginning 0))
  1308. ;; now that we've located the code block, it may be evaluated
  1309. (let ((org-babel-execute-src-block "RESULTS"))
  1310. (org-babel-execute-src-block))
  1311. (buffer-string)))))
  1312. (ert-deftest test-ob/location-of-header-arg-eval ()
  1313. "Test location of header argument evaluation."
  1314. (org-test-with-temp-text "
  1315. #+name: top-block
  1316. #+begin_src emacs-lisp :var pt=(point)
  1317. pt
  1318. #+end_src
  1319. #+name: bottom-block
  1320. #+begin_src emacs-lisp :var pt=top-block()
  1321. pt
  1322. #+end_src
  1323. "
  1324. ;; the value of the second block should be greater than the first
  1325. (should
  1326. (< (progn (re-search-forward org-babel-src-block-regexp nil t)
  1327. (goto-char (match-beginning 0))
  1328. (prog1 (save-match-data (org-babel-execute-src-block))
  1329. (goto-char (match-end 0))))
  1330. (progn (re-search-forward org-babel-src-block-regexp nil t)
  1331. (goto-char (match-beginning 0))
  1332. (org-babel-execute-src-block))))))
  1333. (ert-deftest test-ob/preserve-results-indentation ()
  1334. "Preserve indentation when executing a source block."
  1335. (should
  1336. (equal
  1337. '(2 2)
  1338. (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1339. (org-babel-execute-src-block)
  1340. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1341. (list (org-get-indentation)
  1342. (progn (forward-line) (org-get-indentation))))))
  1343. (should
  1344. (equal
  1345. '(2 2)
  1346. (org-test-with-temp-text
  1347. " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1348. (org-babel-execute-src-block)
  1349. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1350. (list (org-get-indentation)
  1351. (progn (forward-line) (org-get-indentation))))))
  1352. ;; Don't get fooled by TAB-based indentation.
  1353. (should
  1354. (equal
  1355. '(6 6)
  1356. (org-test-with-temp-text
  1357. "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
  1358. (setq tab-width 4)
  1359. (org-babel-execute-src-block)
  1360. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1361. (list (org-get-indentation)
  1362. (progn (forward-line) (org-get-indentation))))))
  1363. ;; Properly indent examplified blocks.
  1364. (should
  1365. (equal
  1366. " #+begin_example
  1367. 0
  1368. 1
  1369. 2
  1370. 3
  1371. 4
  1372. 5
  1373. 6
  1374. 7
  1375. 8
  1376. 9
  1377. #+end_example
  1378. "
  1379. (org-test-with-temp-text
  1380. " #+begin_src emacs-lisp :results output
  1381. (dotimes (i 10) (princ i) (princ \"\\n\"))
  1382. #+end_src"
  1383. (org-babel-execute-src-block)
  1384. (search-forward "begin_example")
  1385. (downcase
  1386. (buffer-substring-no-properties (line-beginning-position)
  1387. (point-max))))))
  1388. ;; Properly indent "org" blocks.
  1389. (should
  1390. (equal
  1391. " #+begin_src org
  1392. 0
  1393. 1
  1394. 2
  1395. 3
  1396. 4
  1397. 5
  1398. 6
  1399. 7
  1400. 8
  1401. 9
  1402. #+end_src
  1403. "
  1404. (org-test-with-temp-text
  1405. " #+begin_src emacs-lisp :results output org
  1406. (dotimes (i 10) (princ i) (princ \"\\n\"))
  1407. #+end_src"
  1408. (org-babel-execute-src-block)
  1409. (search-forward "begin_src org")
  1410. (downcase
  1411. (buffer-substring-no-properties (line-beginning-position)
  1412. (point-max)))))))
  1413. (ert-deftest test-ob/preserve-comma-escape ()
  1414. "Preserve comma escapes when inserting results."
  1415. (should
  1416. (equal
  1417. "#+begin_example
  1418. line 1
  1419. ,* headline 2
  1420. ,* headline 3
  1421. ,* headline 4
  1422. ,* headline 5
  1423. #+end_example
  1424. "
  1425. (org-test-with-temp-text "#+begin_src emacs-lisp :wrap example
  1426. \"line 1
  1427. ,* headline 2
  1428. ,* headline 3
  1429. ,* headline 4
  1430. ,* headline 5
  1431. \"
  1432. #+end_src
  1433. "
  1434. (org-babel-execute-src-block)
  1435. (let ((case-fold-search t)) (search-forward "result" nil t))
  1436. (downcase (buffer-substring-no-properties (line-beginning-position 2)
  1437. (point-max)))))))
  1438. (ert-deftest test-ob/safe-header-args ()
  1439. "Detect safe and unsafe header args."
  1440. (let ((safe-args '((:cache . "foo")
  1441. (:results . "output")
  1442. (:eval . "never")
  1443. (:eval . "query")))
  1444. (unsafe-args '((:eval . "yes")
  1445. (:results . "output file")
  1446. (:foo . "bar")))
  1447. (malformed-args '((bar . "foo")
  1448. ("foo" . "bar")
  1449. :foo))
  1450. (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
  1451. (dolist (arg safe-args)
  1452. (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
  1453. (dolist (arg unsafe-args)
  1454. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1455. (dolist (arg malformed-args)
  1456. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1457. (should (not (funcall safe-p (append safe-args unsafe-args))))))
  1458. (ert-deftest test-ob/noweb-expansions-in-cache ()
  1459. "Ensure that noweb expansions are expanded before caching."
  1460. (let ((noweb-expansions-in-cache-var 0))
  1461. (org-test-with-temp-text "
  1462. #+name: foo
  1463. #+begin_src emacs-lisp
  1464. \"I said\"
  1465. #+end_src
  1466. #+name: bar
  1467. #+begin_src emacs-lisp :noweb yes :cache yes
  1468. (setq noweb-expansions-in-cache-var
  1469. (+ 1 noweb-expansions-in-cache-var))
  1470. (concat <<foo>> \" check noweb expansions\")
  1471. #+end_src
  1472. "
  1473. ;; run the second block to create the cache
  1474. (goto-char (point-min))
  1475. (re-search-forward (regexp-quote "#+name: bar"))
  1476. (should (string= "I said check noweb expansions"
  1477. (org-babel-execute-src-block)))
  1478. (should (= noweb-expansions-in-cache-var 1))
  1479. ;; change the value of the first block
  1480. (goto-char (point-min))
  1481. (re-search-forward (regexp-quote "said"))
  1482. (goto-char (match-beginning 0))
  1483. (insert "haven't ")
  1484. (re-search-forward (regexp-quote "#+name: bar"))
  1485. (should (string= "I haven't said check noweb expansions"
  1486. (org-babel-execute-src-block)))
  1487. (should (= noweb-expansions-in-cache-var 2)))))
  1488. (ert-deftest test-ob/file-ext-and-output-dir ()
  1489. (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
  1490. (org-babel-next-src-block)
  1491. (should (equal "file-ext-basic.txt"
  1492. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1493. (org-babel-next-src-block)
  1494. (should (equal "foo/file-ext-dir-relative.txt"
  1495. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1496. (org-babel-next-src-block)
  1497. (should (equal "foo/file-ext-dir-relative-slash.txt"
  1498. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1499. (org-babel-next-src-block)
  1500. (should (equal "/tmp/file-ext-dir-absolute.txt"
  1501. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1502. (org-babel-next-src-block)
  1503. (should (equal "foo.bar"
  1504. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1505. (org-babel-next-src-block)
  1506. (should (equal "xxx/foo.bar"
  1507. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1508. ))
  1509. (ert-deftest test-ob-core/dir-mkdirp ()
  1510. "Test :mkdirp with :dir header combination."
  1511. (should-not
  1512. (org-test-with-temp-text-in-file
  1513. "#+begin_src emacs-lisp :dir \"data/code\"
  1514. t
  1515. #+end_src"
  1516. (org-babel-execute-src-block)
  1517. (message default-directory)
  1518. (file-directory-p "data/code")))
  1519. (should-not
  1520. (org-test-with-temp-text-in-file
  1521. "#+begin_src emacs-lisp :mkdirp no :dir \"data/code\"
  1522. t
  1523. #+end_src"
  1524. (org-babel-execute-src-block)
  1525. (message default-directory)
  1526. (file-directory-p "data/code")))
  1527. (should
  1528. (org-test-with-temp-text-in-file
  1529. "#+begin_src emacs-lisp :mkdirp yes :dir \"data/code\"
  1530. t
  1531. #+end_src"
  1532. (org-babel-execute-src-block)
  1533. (message default-directory)
  1534. (prog1 (file-directory-p "data/code")
  1535. (delete-directory "data" t))))
  1536. (should
  1537. (equal "/tmp/test-dir-no-mkdirp/"
  1538. (org-test-with-temp-text-in-file
  1539. "#+begin_src emacs-lisp :dir /tmp/test-dir-no-mkdirp
  1540. default-directory
  1541. #+end_src"
  1542. (org-babel-execute-src-block)))))
  1543. (ert-deftest test-ob/script-escape ()
  1544. ;; Delimited lists of numbers
  1545. (should (equal '(1 2 3)
  1546. (org-babel-script-escape "[1 2 3]")))
  1547. (should (equal '(1 2 3)
  1548. (org-babel-script-escape "{1 2 3}")))
  1549. (should (equal '(1 2 3)
  1550. (org-babel-script-escape "(1 2 3)")))
  1551. ;; Delimited lists of double-quoted strings
  1552. (should (equal '("foo" "bar")
  1553. (org-babel-script-escape "(\"foo\" \"bar\")")))
  1554. (should (equal '("foo" "bar")
  1555. (org-babel-script-escape "[\"foo\" \"bar\"]")))
  1556. (should (equal '("foo" "bar")
  1557. (org-babel-script-escape "{\"foo\" \"bar\"}")))
  1558. ;; ... with commas
  1559. (should (equal '("foo" "bar")
  1560. (org-babel-script-escape "(\"foo\", \"bar\")")))
  1561. (should (equal '("foo" "bar")
  1562. (org-babel-script-escape "[\"foo\", \"bar\"]")))
  1563. (should (equal '("foo" "bar")
  1564. (org-babel-script-escape "{\"foo\", \"bar\"}")))
  1565. ;; Delimited lists of single-quoted strings
  1566. (should (equal '("foo" "bar")
  1567. (org-babel-script-escape "('foo' 'bar')")))
  1568. (should (equal '("foo" "bar")
  1569. (org-babel-script-escape "['foo' 'bar']")))
  1570. (should (equal '("foo" "bar")
  1571. (org-babel-script-escape "{'foo' 'bar'}")))
  1572. ;; ... with commas
  1573. (should (equal '("foo" "bar")
  1574. (org-babel-script-escape "('foo', 'bar')")))
  1575. (should (equal '("foo" "bar")
  1576. (org-babel-script-escape "['foo', 'bar']")))
  1577. (should (equal '("foo" "bar")
  1578. (org-babel-script-escape "{'foo', 'bar'}")))
  1579. ;; Single quoted strings
  1580. (should (equal "foo"
  1581. (org-babel-script-escape "'foo'")))
  1582. ;; ... with internal double quote
  1583. (should (equal "foo\"bar"
  1584. (org-babel-script-escape "'foo\"bar'")))
  1585. ;; ... with internal backslash
  1586. (should (equal "foo\\bar"
  1587. (org-babel-script-escape "'foo\\bar'")))
  1588. ;; ... with internal escaped backslash
  1589. (should (equal "foo\\bar"
  1590. (org-babel-script-escape "'foo\\\\bar'")))
  1591. ;; ... with internal backslash-double quote
  1592. (should (equal "foo\\\"bar"
  1593. (org-babel-script-escape "'foo\\\"bar'")))
  1594. ;; ... with internal escaped backslash-double quote
  1595. (should (equal "foo\\\"bar"
  1596. (org-babel-script-escape "'foo\\\\\"bar'")))
  1597. ;; ... with internal escaped single quote
  1598. (should (equal "foo'bar"
  1599. (org-babel-script-escape "'foo\\'bar'")))
  1600. ;; ... with internal escaped backslash-escaped single quote
  1601. (should (equal "foo\\'bar"
  1602. (org-babel-script-escape "'foo\\\\\\'bar'")))
  1603. ;; Double quoted strings
  1604. (should (equal "foo"
  1605. (org-babel-script-escape "\"foo\"")))
  1606. ;; ... with internal single quote
  1607. (should (equal "foo'bar"
  1608. (org-babel-script-escape "\"foo'bar\"")))
  1609. ;; ... with internal backslash
  1610. (should (equal "foo\\bar"
  1611. (org-babel-script-escape "\"foo\\bar\"")))
  1612. ;; ... with internal escaped backslash
  1613. (should (equal "foo\\bar"
  1614. (org-babel-script-escape "\"foo\\\\bar\"")))
  1615. ;; ... with internal backslash-single quote
  1616. (should (equal "foo\\'bar"
  1617. (org-babel-script-escape "\"foo\\'bar\"")))
  1618. ;; ... with internal escaped backslash-single quote
  1619. (should (equal "foo\\'bar"
  1620. (org-babel-script-escape "\"foo\\\\'bar\"")))
  1621. ;; ... with internal escaped double quote
  1622. (should (equal "foo\"bar"
  1623. (org-babel-script-escape "\"foo\\\"bar\"")))
  1624. ;; ... with internal escaped backslash-escaped double quote
  1625. (should (equal "foo\\\"bar"
  1626. (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
  1627. (ert-deftest test-ob/process-params-no-duplicates ()
  1628. (should
  1629. (equal (org-babel-process-params '((:colname-names)
  1630. (:rowname-names)
  1631. (:result-params)
  1632. (:result-type)
  1633. (:var . "\"foo\"")))
  1634. '((:var)
  1635. (:colname-names)
  1636. (:rowname-names)
  1637. (:result-params)
  1638. (:result-type . value)))))
  1639. (defun org-test-babel-confirm-evaluate (eval-value)
  1640. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  1641. nil
  1642. #+end_src" eval-value)
  1643. (goto-char (point-min))
  1644. (let ((info (org-babel-get-src-block-info)))
  1645. (org-babel-check-confirm-evaluate info))))
  1646. (ert-deftest test-ob/check-eval ()
  1647. (let ((org-confirm-babel-evaluate t))
  1648. ;; Non-export tests
  1649. (dolist (pair '(("no" . nil)
  1650. ("never" . nil)
  1651. ("query" . query)
  1652. ("yes" . query)))
  1653. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1654. ;; Export tests
  1655. (let ((org-babel-exp-reference-buffer t))
  1656. (dolist (pair '(("no" . nil)
  1657. ("never" . nil)
  1658. ("query" . query)
  1659. ("yes" . query)
  1660. ("never-export" . nil)
  1661. ("no-export" . nil)
  1662. ("query-export" . query)))
  1663. (message (car pair))
  1664. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))))
  1665. (let ((org-confirm-babel-evaluate nil))
  1666. ;; Non-export tests
  1667. (dolist (pair '(("no" . nil)
  1668. ("never" . nil)
  1669. ("query" . query)
  1670. ("yes" . t)))
  1671. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1672. ;; Export tests
  1673. (let ((org-babel-exp-reference-buffer t))
  1674. (dolist (pair '(("no" . nil)
  1675. ("never" . nil)
  1676. ("query" . query)
  1677. ("yes" . t)
  1678. ("never-export" . nil)
  1679. ("no-export" . nil)
  1680. ("query-export" . query)))
  1681. (message (car pair))
  1682. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair)))))))
  1683. (ert-deftest test-ob/check-eval-noweb-expanded ()
  1684. "`org-confirm-babel-evaluate' function receives expanded noweb refs."
  1685. (should
  1686. (equal t
  1687. (org-test-with-temp-text "
  1688. #+name: foo
  1689. #+begin_src emacs-lisp
  1690. :bar
  1691. #+end_src
  1692. <point>#+begin_src emacs-lisp :noweb yes
  1693. <<foo>>
  1694. #+end_src"
  1695. (let ((org-confirm-babel-evaluate
  1696. (lambda (_ body)
  1697. (not (string-match-p ":bar" body)))))
  1698. (org-babel-check-confirm-evaluate
  1699. (org-babel-get-src-block-info))))))
  1700. ;; The code block passed to `org-confirm-babel-evaluate' does not
  1701. ;; include coderefs.
  1702. (should
  1703. (equal t
  1704. (org-test-with-temp-text "
  1705. #+name: foo
  1706. #+begin_src emacs-lisp
  1707. :bar
  1708. #+end_src
  1709. <point>#+begin_src emacs-lisp :noweb yes
  1710. #(ref:foo)
  1711. <<foo>>
  1712. #+end_src"
  1713. (let ((org-coderef-label-format "#(ref:%s)")
  1714. (org-confirm-babel-evaluate
  1715. (lambda (_ body)
  1716. (string-match-p "ref:foo" body))))
  1717. (org-babel-check-confirm-evaluate
  1718. (org-babel-get-src-block-info)))))))
  1719. (defun org-test-ob/update-block-body ()
  1720. "Test `org-babel-update-block-body' specifications."
  1721. (should
  1722. (equal "#+begin_src elisp\n 2\n#+end_src"
  1723. (let ((org-edit-src-content-indentation 2))
  1724. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1725. (org-babel-update-block-body "2")
  1726. (buffer-string)))))
  1727. ;; Preserve block indentation.
  1728. (should
  1729. (equal " #+begin_src elisp\n 2\n #+end_src"
  1730. (let ((org-edit-src-content-indentation 1))
  1731. (org-test-with-temp-text
  1732. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1733. (org-babel-update-block-body "2")
  1734. (buffer-string)))))
  1735. ;; Ignore NEW-BODY global indentation.
  1736. (should
  1737. (equal "#+begin_src elisp\n 2\n#+end_src"
  1738. (let ((org-edit-src-content-indentation 2))
  1739. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1740. (org-babel-update-block-body " 2")
  1741. (buffer-string)))))
  1742. ;; When indentation should be preserved ignore the two rules above.
  1743. (should
  1744. (equal " #+begin_src elisp\n2\n #+end_src"
  1745. (let ((org-edit-src-content-indentation 1)
  1746. (org-src-preserve-indentation t))
  1747. (org-test-with-temp-text
  1748. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1749. (org-babel-update-block-body "2")
  1750. (buffer-string)))))
  1751. (should
  1752. (equal " #+begin_src elisp -i\n2\n #+end_src"
  1753. (let ((org-edit-src-content-indentation 1))
  1754. (org-test-with-temp-text
  1755. " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
  1756. (org-babel-update-block-body "2")
  1757. (buffer-string)))))
  1758. (should
  1759. (equal "#+begin_src elisp\n 2\n#+end_src"
  1760. (let ((org-edit-src-content-indentation 2)
  1761. (org-src-preserve-indentation t))
  1762. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1763. (org-babel-update-block-body " 2")
  1764. (buffer-string)))))
  1765. (should
  1766. (equal "#+begin_src elisp -i\n 2\n#+end_src"
  1767. (let ((org-edit-src-content-indentation 2)
  1768. (org-src-preserve-indentation t))
  1769. (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
  1770. (org-babel-update-block-body " 2")
  1771. (buffer-string))))))
  1772. (ert-deftest test-ob/find-named-result ()
  1773. "Test `org-babel-find-named-result' specifications."
  1774. (should
  1775. (= 1
  1776. (org-test-with-temp-text "#+results: foo\n: result"
  1777. (org-babel-find-named-result "foo"))))
  1778. (should-not
  1779. (org-test-with-temp-text "#+results: foo\n: result"
  1780. (org-babel-find-named-result "bar")))
  1781. (should-not
  1782. (org-test-with-temp-text "#+results: foobar\n: result"
  1783. (org-babel-find-named-result "foo")))
  1784. ;; Search is case insensitive.
  1785. (should
  1786. (org-test-with-temp-text "#+RESULTS: FOO\n: result"
  1787. (org-babel-find-named-result "foo")))
  1788. ;; Handle hash in results keyword.
  1789. (should
  1790. (org-test-with-temp-text "#+results[hash]: FOO\n: result"
  1791. (org-babel-find-named-result "foo")))
  1792. ;; Accept orphaned affiliated keywords.
  1793. (should
  1794. (org-test-with-temp-text "#+results: foo"
  1795. (org-babel-find-named-result "foo"))))
  1796. (ert-deftest test-ob/where-is-src-block-result ()
  1797. "Test `org-babel-where-is-src-block-result' specifications."
  1798. ;; Find anonymous results.
  1799. (should
  1800. (equal "#+RESULTS:"
  1801. (org-test-with-temp-text
  1802. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
  1803. (goto-char (org-babel-where-is-src-block-result))
  1804. (buffer-substring-no-properties (point) (line-end-position)))))
  1805. ;; Find named results. Those have priority over anonymous ones.
  1806. (should
  1807. (equal "#+RESULTS: example"
  1808. (org-test-with-temp-text
  1809. "
  1810. <point>#+NAME: example
  1811. #+BEGIN_SRC emacs-lisp
  1812. \(+ 1 1)
  1813. #+END_SRC
  1814. #+RESULTS: example
  1815. : 2"
  1816. (goto-char (org-babel-where-is-src-block-result))
  1817. (buffer-substring-no-properties (point) (line-end-position)))))
  1818. (should
  1819. (equal "#+RESULTS: example"
  1820. (org-test-with-temp-text
  1821. "
  1822. <point>#+NAME: example
  1823. #+BEGIN_SRC emacs-lisp
  1824. \(+ 1 1)
  1825. #+END_SRC
  1826. #+RESULTS:
  1827. : fake
  1828. #+RESULTS: example
  1829. : 2"
  1830. (goto-char (org-babel-where-is-src-block-result))
  1831. (buffer-substring-no-properties (point) (line-end-position)))))
  1832. ;; Return nil when no result is found.
  1833. (should-not
  1834. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1835. (org-babel-where-is-src-block-result)))
  1836. (should-not
  1837. (org-test-with-temp-text
  1838. "- item\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n"
  1839. (org-babel-where-is-src-block-result)))
  1840. ;; When optional argument INSERT is non-nil, add RESULTS keyword
  1841. ;; whenever no RESULTS can be found.
  1842. (should
  1843. (equal
  1844. "#+RESULTS:"
  1845. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1846. (let ((org-babel-results-keyword "RESULTS"))
  1847. (goto-char (org-babel-where-is-src-block-result t)))
  1848. (buffer-substring-no-properties (point) (line-end-position)))))
  1849. ;; Insert a named RESULTS keyword if possible.
  1850. (should
  1851. (equal
  1852. "#+RESULTS: e"
  1853. (org-test-with-temp-text
  1854. "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1855. (let ((org-babel-results-keyword "RESULTS"))
  1856. (goto-char (org-babel-where-is-src-block-result t)))
  1857. (buffer-substring-no-properties (point) (line-end-position)))))
  1858. ;; When optional argument HASH is provided, clear RESULTS keyword
  1859. ;; and related contents if they do not match it.
  1860. (should
  1861. (equal
  1862. "#+RESULTS[bbbb]:"
  1863. (org-test-with-temp-text
  1864. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 3"
  1865. (let ((org-babel-results-keyword "RESULTS"))
  1866. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1867. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1868. (should
  1869. (equal
  1870. "#+RESULTS[bbbb]:"
  1871. (org-test-with-temp-text
  1872. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
  1873. (let ((org-babel-results-keyword "RESULTS"))
  1874. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1875. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1876. ;; Handle hashes with times.
  1877. (should
  1878. (equal
  1879. "#+RESULTS[<2014-03-04 00:41:10> bbbb]:"
  1880. (org-test-with-temp-text
  1881. "
  1882. <point>#+BEGIN_SRC emacs-lisp
  1883. \(+ 1 1)
  1884. #+END_SRC
  1885. #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1886. (let ((org-babel-results-keyword "RESULTS")
  1887. (org-babel-hash-show-time t))
  1888. (cl-letf (((symbol-function 'format-time-string)
  1889. (lambda (&rest _) "<2014-03-04 00:41:10>")))
  1890. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb"))
  1891. (org-trim (buffer-substring-no-properties (point) (point-max))))))))
  1892. (should
  1893. (equal
  1894. "#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1895. (org-test-with-temp-text
  1896. "
  1897. <point>#+BEGIN_SRC emacs-lisp
  1898. \(+ 1 1)
  1899. #+END_SRC
  1900. #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1901. (let ((org-babel-results-keyword "RESULTS")
  1902. (org-babel-hash-show-time t))
  1903. (cl-letf (((symbol-function 'format-time-string)
  1904. (lambda (&rest _) "<2014-03-04 00:41:10>")))
  1905. (goto-char (org-babel-where-is-src-block-result nil nil "aaaa"))
  1906. (org-trim (buffer-substring-no-properties (point) (point-max))))))))
  1907. ;; RESULTS keyword may not be the last affiliated keyword.
  1908. (should
  1909. (equal
  1910. "#+RESULTS[bbbb]:"
  1911. (org-test-with-temp-text
  1912. "
  1913. <point>#+BEGIN_SRC emacs-lisp
  1914. \(+ 1 1)
  1915. #+END_SRC
  1916. #+RESULTS[aaaa]:
  1917. #+NAME: e
  1918. : 3"
  1919. (let ((org-babel-results-keyword "RESULTS"))
  1920. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1921. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1922. ;; HASH does nothing if no RESULTS can be found. However, if INSERT
  1923. ;; is also non-nil, RESULTS keyword is inserted along with the
  1924. ;; expected hash.
  1925. (should
  1926. (equal
  1927. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1928. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1929. (org-babel-where-is-src-block-result nil nil "bbbb")
  1930. (buffer-string))))
  1931. (should
  1932. (equal
  1933. "#+RESULTS[bbbb]:"
  1934. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1935. (let ((org-babel-results-keyword "RESULTS"))
  1936. (goto-char (org-babel-where-is-src-block-result t nil "bbbb")))
  1937. (org-trim (buffer-substring-no-properties (point) (point-max)))))))
  1938. (ert-deftest test-ob/goto-named-src-block ()
  1939. "Test interactive use of `org-babel-goto-named-src-block'."
  1940. (org-test-with-temp-text-in-file
  1941. "
  1942. #+NAME: abc
  1943. #+BEGIN_SRC emacs-lisp :results value
  1944. (1+ 1)
  1945. #+END_SRC
  1946. #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
  1947. #+BEGIN_SRC emacs-lisp
  1948. <<abc>>
  1949. #+END_SRC
  1950. abc
  1951. #+RESULTS: abc
  1952. : 2
  1953. "
  1954. ;; non-existent name
  1955. (should-not
  1956. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
  1957. ;; correct name
  1958. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
  1959. (should (= 14 (point)))
  1960. ;; call line - autocompletion
  1961. (forward-line 3)
  1962. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1963. (should (= 14 (point)))
  1964. ;; noweb reference - autocompletion
  1965. (forward-line 5)
  1966. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1967. (should (= 14 (point)))
  1968. ;; at symbol - autocompletion
  1969. (forward-line 7)
  1970. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1971. (should (= 14 (point)))
  1972. ;; in results - autocompletion
  1973. (forward-line 8)
  1974. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1975. (should (= 14 (point)))
  1976. (forward-line 9)
  1977. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1978. (should (= 14 (point)))))
  1979. (ert-deftest test-ob/evaluate-body-with-coderefs ()
  1980. (should
  1981. (= 2
  1982. (org-test-with-temp-text
  1983. "#+begin_src emacs-lisp -l \"#(ref:%s)\"\n2 #(ref:foo)\n#+end_src"
  1984. (org-babel-execute-src-block))))
  1985. (should
  1986. (= 3
  1987. (org-test-with-temp-text
  1988. "#+begin_src emacs-lisp\n3 #(ref:foo)\n#+end_src"
  1989. (let ((org-coderef-label-format "#(ref:%s)"))
  1990. (org-babel-execute-src-block))))))
  1991. (ert-deftest test-ob/string-to-number ()
  1992. (should (= 0 (org-babel--string-to-number "0")))
  1993. (should (= 1 (org-babel--string-to-number "1")))
  1994. (should (eq nil (org-babel--string-to-number "1 2")))
  1995. (should (= 1000.0 (org-babel--string-to-number "1e3")))
  1996. (should (eq 0 (org-babel--string-to-number "000")))
  1997. (should (eq 1 (org-babel--string-to-number "001")))
  1998. (should (eq 10 (org-babel--string-to-number "010")))
  1999. (should (= 100 (org-babel--string-to-number "100")))
  2000. (should (= 0.1 (org-babel--string-to-number "0.1")))
  2001. (should (= 1.0 (org-babel--string-to-number "1.0"))))
  2002. (provide 'test-ob)
  2003. ;;; test-ob ends here