test-ob.el 72 KB

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