test-ob.el 68 KB

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