test-ob.el 59 KB

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