test-ob.el 70 KB

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