test-ob.el 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  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= (concat default-directory "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 (assoc :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. :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. (let ((info (org-babel-get-src-block-info)))
  130. (should (= 42 (org-babel-execute-src-block))))))
  131. (ert-deftest test-ob/simple-named-code-block ()
  132. "Test that simple named code blocks can be evaluated."
  133. (org-test-with-temp-text-in-file "
  134. #+name: i-have-a-name
  135. #+begin_src emacs-lisp
  136. 42
  137. #+end_src"
  138. (org-babel-next-src-block 1)
  139. (should (= 42 (org-babel-execute-src-block)))))
  140. (ert-deftest test-ob/simple-variable-resolution ()
  141. "Test that simple variable resolution is working."
  142. (org-test-with-temp-text-in-file "
  143. #+name: four
  144. #+begin_src emacs-lisp
  145. (list 1 2 3 4)
  146. #+end_src
  147. #+begin_src emacs-lisp :var four=four
  148. (length four)
  149. #+end_src"
  150. (org-babel-next-src-block 2)
  151. (should (= 4 (org-babel-execute-src-block)))
  152. (forward-line 5)
  153. (should (string= ": 4" (buffer-substring
  154. (point-at-bol)
  155. (point-at-eol))))))
  156. (ert-deftest test-ob/multi-line-header-arguments ()
  157. "Test that multi-line header arguments and can be read."
  158. (org-test-with-temp-text-in-file "
  159. #+headers: :var letters='(a b c d e f g)
  160. #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
  161. (require 'cl)
  162. (defalias 'my-map (if (org-version-check \"24.2.50\" \"cl\" :predicate)
  163. 'cl-map
  164. 'map))
  165. (my-map 'list #'list numbers letters)
  166. #+end_src"
  167. (org-babel-next-src-block)
  168. (let ((results (org-babel-execute-src-block)))
  169. (should(equal 'a (cadr (assoc 1 results))))
  170. (should(equal 'd (cadr (assoc 4 results)))))))
  171. (ert-deftest test-ob/parse-header-args ()
  172. (org-test-with-temp-text-in-file "
  173. #+begin_src example-lang :session :results output :var num=9
  174. the body
  175. #+end_src"
  176. (org-babel-next-src-block)
  177. (let* ((info (org-babel-get-src-block-info))
  178. (params (nth 2 info)))
  179. (message "%S" params)
  180. (should (equal "example-lang" (nth 0 info)))
  181. (should (string= "the body" (org-babel-trim (nth 1 info))))
  182. (should-not (member '(:session\ \ \ \ ) params))
  183. (should (equal '(:session) (assoc :session params)))
  184. (should (equal '(:result-type . output) (assoc :result-type params)))
  185. (should (equal '(num . 9) (cdr (assoc :var params)))))))
  186. (ert-deftest test-ob/parse-header-args2 ()
  187. (org-test-with-temp-text-in-file "
  188. * resolving sub-trees as references
  189. #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
  190. (length text)
  191. #+end_src
  192. #+begin_src org :noweb yes
  193. <<simple-subtree>>
  194. <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
  195. #+end_src
  196. ** simple subtree with custom ID
  197. :PROPERTIES:
  198. :CUSTOM_ID: simple-subtree
  199. :END:
  200. this is simple"
  201. (should (string-match (regexp-quote "this is simple")
  202. (org-babel-ref-resolve "simple-subtree")))
  203. (org-babel-next-src-block)
  204. (should (= 14 (org-babel-execute-src-block)))))
  205. (ert-deftest test-ob/inline-src-blocks ()
  206. (should
  207. (= 1
  208. (org-test-with-temp-text
  209. "In the middle <point>src_emacs-lisp{(+ 0 1)} of a line"
  210. (org-babel-execute-src-block))))
  211. (should
  212. (= 2
  213. (org-test-with-temp-text
  214. "One at the end of a line: <point>src_emacs-lisp{(+ 1 1)}"
  215. (org-babel-execute-src-block))))
  216. (should
  217. (= 3
  218. (org-test-with-temp-text
  219. "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
  220. (org-babel-execute-src-block))))
  221. (should
  222. (= 4
  223. (org-test-with-temp-text
  224. "In the middle <point>src_emacs-lisp[:results silent\
  225. :exports code]{(+ 3 1)} of a line"
  226. (org-babel-execute-src-block))))
  227. (should
  228. (= 5
  229. (org-test-with-temp-text
  230. "One at the end of a line: <point>src_emacs-lisp[:results silent\
  231. :exports code]{(+ 4 1)}"
  232. (org-babel-execute-src-block))))
  233. (should
  234. (= 6
  235. (org-test-with-temp-text
  236. "src_emacs-lisp[:results silent :exports code]{(+ 5 1)}\
  237. at the beginning of a line."
  238. (org-babel-execute-src-block))))
  239. (should
  240. (= 7
  241. (org-test-with-temp-text
  242. "One also evaluated: <point>src_emacs-lisp[:exports both\
  243. :results silent]{(+ 6 1)}"
  244. (org-babel-execute-src-block)))))
  245. (ert-deftest test-ob/inline-src_blk-default-results-replace-line-1 ()
  246. (let ((test-line "src_sh{echo 1}")
  247. (org-babel-inline-result-wrap "=%s="))
  248. ;; src_ at bol line 1...
  249. (org-test-with-temp-text
  250. test-line
  251. (goto-char (point-min)) (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. (forward-char) (org-babel-execute-maybe)
  256. (should (string=
  257. (concat test-line " {{{results(=1=)}}}")
  258. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  259. (re-search-forward "{{{")
  260. ;;(should-error (org-ctrl-c-ctrl-c))
  261. (backward-char 4) ;; last char of block body
  262. (org-babel-execute-maybe)
  263. (should (string=
  264. (concat test-line " {{{results(=1=)}}}")
  265. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  266. ;; src_ follows space line 1...
  267. (let ((test-line " src_emacs-lisp{ 1 }"))
  268. (org-test-with-temp-text
  269. test-line
  270. (should-error (org-ctrl-c-ctrl-c))
  271. (forward-char) (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. (re-search-forward "{ 1 ") (org-babel-execute-maybe)
  276. (should (string=
  277. (concat test-line " {{{results(=1=)}}}")
  278. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  279. (forward-char 6)
  280. (should-error (org-ctrl-c-ctrl-c))))
  281. ;; Results on a subsequent line are replaced.
  282. (should
  283. (equal
  284. "src_emacs-lisp{(+ 1 2)}\n {{{results(=3=)}}}"
  285. (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n {{{results(=2=)}}}"
  286. (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
  287. (buffer-string))))
  288. ;; Also handle results at the beginning of a line.
  289. (should
  290. (equal
  291. "src_emacs-lisp{(+ 1 2)}\n{{{results(=3=)}}}"
  292. (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n{{{results(=2=)}}}"
  293. (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
  294. (buffer-string))))))
  295. (ert-deftest test-ob/inline-src_blk-default-results-replace-line-2 ()
  296. ;; src_ at bol line 2...
  297. (let ((test-line " src_emacs-lisp{ \"x\" }")
  298. (org-babel-inline-result-wrap "=%s="))
  299. (org-test-with-temp-text
  300. (concat "\n" test-line)
  301. (should-error (org-ctrl-c-ctrl-c))
  302. (goto-char (point-min))
  303. (should-error (org-ctrl-c-ctrl-c))
  304. (forward-line)
  305. (should-error (org-ctrl-c-ctrl-c))
  306. (forward-char) (org-babel-execute-maybe)
  307. (should (string=
  308. (concat test-line " {{{results(=x=)}}}")
  309. (buffer-substring-no-properties
  310. (point-at-bol) (point-at-eol))))))
  311. (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
  312. (org-babel-inline-result-wrap "=%s="))
  313. (org-test-with-temp-text
  314. test-line
  315. (goto-char (point-max))
  316. (insert (concat "\n" test-line " end"))
  317. (re-search-backward "src") (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. (re-search-forward "\" ") (org-babel-execute-maybe)
  322. (should (string=
  323. (concat test-line " {{{results(=y=)}}} end")
  324. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  325. (forward-char 3)
  326. (should-error (org-ctrl-c-ctrl-c)))))
  327. (ert-deftest test-ob/inline-src_blk-manual-results-replace ()
  328. (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
  329. (org-babel-inline-result-wrap "=%s="))
  330. (org-test-with-temp-text
  331. (concat "\n" test-line)
  332. (should-error (org-ctrl-c-ctrl-c))
  333. (goto-char (point-max))
  334. (org-babel-execute-maybe)
  335. (beginning-of-line)
  336. (should-error (org-ctrl-c-ctrl-c))
  337. (forward-char) (org-babel-execute-maybe)
  338. (should (string=
  339. (concat test-line " {{{results(=x=)}}}")
  340. (buffer-substring-no-properties
  341. (point-at-bol) (point-at-eol))))))
  342. (let ((test-line (concat " Some text prior to block "
  343. "src_emacs-lisp[:results replace]{ \"y\" }"))
  344. (org-babel-inline-result-wrap "=%s="))
  345. (org-test-with-temp-text test-line
  346. (goto-char (point-max))
  347. (insert (concat "\n" test-line " end"))
  348. (re-search-backward "src") (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. (re-search-forward "\" ") (org-babel-execute-maybe)
  353. (should (string=
  354. (concat test-line " {{{results(=y=)}}} end")
  355. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  356. (forward-char 3)
  357. (should-error (org-ctrl-c-ctrl-c)))))
  358. (ert-deftest test-ob/inline-src_blk-results-silent ()
  359. (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
  360. (org-test-with-temp-text test-line
  361. (org-babel-execute-maybe)
  362. (should (string= test-line
  363. (buffer-substring-no-properties
  364. (point-at-bol) (point-at-eol))))))
  365. (let ((test-line (concat " Some text prior to block src_emacs-lisp"
  366. "[ :results silent ]{ \"y\" }")))
  367. (org-test-with-temp-text
  368. test-line
  369. (goto-char (point-max))
  370. (insert (concat "\n" test-line " end"))
  371. (re-search-backward "src_") (org-babel-execute-maybe)
  372. (should (string= (concat test-line " end")
  373. (buffer-substring-no-properties
  374. (point-at-bol) (point-at-eol))))
  375. (re-search-forward "\" ") (org-babel-execute-maybe)
  376. (should (string= (concat test-line " end")
  377. (buffer-substring-no-properties
  378. (point-at-bol) (point-at-eol))))
  379. (forward-char 2)
  380. (should-error (org-ctrl-c-ctrl-c)))))
  381. (ert-deftest test-ob/inline-src_blk-results-raw ()
  382. (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
  383. (org-test-with-temp-text test-line
  384. (org-babel-execute-maybe)
  385. (should (string= (concat test-line " x")
  386. (buffer-string)))))
  387. (let ((test-line (concat " Some text prior to block "
  388. "src_emacs-lisp[ :results raw ]{ \"the\" }")))
  389. (org-test-with-temp-text (concat test-line " end")
  390. (re-search-forward "src_") (org-babel-execute-maybe)
  391. (should (string= (concat test-line " the end")
  392. (buffer-substring-no-properties
  393. (point-at-bol) (point-at-eol))))
  394. (re-search-forward "\" ") (org-babel-execute-maybe)
  395. (should (string= (concat test-line " the the end")
  396. (buffer-substring-no-properties
  397. (point-at-bol) (point-at-eol))))
  398. (forward-char 2)
  399. (should-error (org-ctrl-c-ctrl-c)))))
  400. (ert-deftest test-ob/inline-src_blk-results-file ()
  401. (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
  402. (org-test-with-temp-text
  403. test-line
  404. (org-babel-execute-maybe)
  405. (should (string= (concat test-line " {{{results([[file:~/test-file]])}}}")
  406. (buffer-substring-no-properties
  407. (point-min) (point-max)))))))
  408. (ert-deftest test-ob/inline-src_blk-results-scalar ()
  409. (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
  410. (org-babel-inline-result-wrap "=%s="))
  411. (org-test-with-temp-text
  412. test-line
  413. (org-babel-execute-maybe)
  414. (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
  415. (buffer-substring-no-properties
  416. (point-min) (point-max)))))))
  417. (ert-deftest test-ob/inline-src_blk-results-verbatim ()
  418. (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  419. (org-babel-inline-result-wrap "=%s="))
  420. (org-test-with-temp-text
  421. test-line
  422. (org-babel-execute-maybe)
  423. (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
  424. (buffer-substring-no-properties
  425. (point-min) (point-max)))))))
  426. (ert-deftest test-ob/combining-scalar-and-raw-result-types ()
  427. (org-test-with-temp-text-in-file "
  428. #+begin_src sh :results scalar
  429. echo \"[[file:./cv.cls]]\"
  430. #+end_src
  431. #+name:
  432. : [[file:./cv.cls]]
  433. #+begin_src sh :results raw scalar
  434. echo \"[[file:./cv.cls]]\"
  435. #+end_src
  436. "
  437. (flet ((next-result ()
  438. (org-babel-next-src-block)
  439. (org-babel-execute-src-block)
  440. (goto-char (org-babel-where-is-src-block-result))
  441. (forward-line 1)))
  442. (goto-char (point-min))
  443. (next-result)
  444. (should (eq (org-element-type (org-element-at-point)) 'fixed-width))
  445. (next-result)
  446. (should-not
  447. (eq (org-element-type (org-element-at-point)) 'fixed-width)))))
  448. (ert-deftest test-ob/no-defaut-value-for-var ()
  449. "Test that the absence of a default value for a variable DOES THROW
  450. a proper error."
  451. (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
  452. (org-babel-next-src-block)
  453. (let ((err
  454. (should-error (org-babel-execute-src-block) :type 'error)))
  455. (should
  456. (equal
  457. '(error
  458. "Variable \"x\" must be assigned a default value")
  459. err)))))
  460. (ert-deftest test-ob/just-one-results-block ()
  461. "Test that evaluating two times the same code block does not result in a
  462. duplicate results block."
  463. (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
  464. (org-babel-execute-src-block)
  465. (org-babel-execute-src-block) ; second code block execution
  466. (should (search-forward "Hello")) ; the string inside the source code block
  467. (should (search-forward "Hello")) ; the same string in the results block
  468. (should-error (search-forward "Hello"))))
  469. (ert-deftest test-ob/nested-code-block ()
  470. "Test nested code blocks inside code blocks don't cause problems."
  471. (should
  472. (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
  473. (org-test-with-temp-text "#+begin_src org :results silent
  474. ,#+begin_src emacs-lisp
  475. 'foo
  476. ,#+end_src
  477. #+end_src"
  478. (let ((org-edit-src-content-indentation 2)
  479. (org-src-preserve-indentation nil))
  480. (org-babel-execute-src-block))))))
  481. (ert-deftest test-ob/partial-nested-code-block ()
  482. "Test nested code blocks inside code blocks don't cause problems."
  483. (org-test-with-temp-text "#+begin_src org :results silent
  484. ,#+begin_src emacs-lisp
  485. #+end_src"
  486. (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
  487. (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
  488. (org-test-with-temp-text "#+NAME: foo
  489. #+BEGIN_SRC emacs-lisp
  490. 'foo
  491. #+END_SRC\n"
  492. (org-babel-next-src-block 1)
  493. (should (eq 'foo (org-babel-execute-src-block)))
  494. (goto-char (point-min))
  495. (org-babel-next-src-block 1)
  496. (should (looking-at org-babel-src-block-regexp))))
  497. (ert-deftest test-ob/catches-all-references ()
  498. (org-test-with-temp-text "
  499. #+NAME: literal-example
  500. #+BEGIN_EXAMPLE
  501. A literal example
  502. on two lines
  503. #+END_EXAMPLE
  504. #+NAME: read-literal-example
  505. #+BEGIN_SRC emacs-lisp :var x=literal-example
  506. (concatenate 'string x \" for me.\")
  507. #+END_SRC"
  508. (org-babel-next-src-block 1)
  509. (should (string= (org-babel-execute-src-block)
  510. "A literal example\non two lines\n for me."))))
  511. (ert-deftest test-ob/ignore-reference-in-commented-headings ()
  512. (should
  513. (= 2
  514. (org-test-with-temp-text
  515. "
  516. * COMMENT H1
  517. #+NAME: n
  518. : 1
  519. * H2
  520. #+NAME: n
  521. : 2
  522. * Code
  523. <point>#+BEGIN_SRC emacs-lisp :var x=n
  524. x
  525. #+END_SRC"
  526. (org-babel-execute-src-block)))))
  527. (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
  528. (org-test-with-temp-text "
  529. #+name: base_plus
  530. | 1 |
  531. | 2 |
  532. #+name: base
  533. | 3 |
  534. | 4 |
  535. #+begin_src emacs-lisp :var x=base
  536. x
  537. #+end_src"
  538. (org-babel-next-src-block 1)
  539. (should (equal (org-babel-execute-src-block) '((3) (4))))))
  540. (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
  541. (org-test-with-temp-text "
  542. #+name: base_plus
  543. #+begin_src emacs-lisp
  544. 'bar
  545. #+end_src
  546. #+name: base
  547. #+begin_src emacs-lisp
  548. 'foo
  549. #+end_src
  550. #+begin_src emacs-lisp :var x=base
  551. x
  552. #+end_src"
  553. (org-babel-next-src-block 3)
  554. (should (equal (org-babel-execute-src-block) "foo"))))
  555. (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
  556. (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
  557. (+ a b c d)
  558. #+end_src
  559. "
  560. (should (= 10 (org-babel-execute-src-block)))))
  561. (ert-deftest test-ob/org-babel-update-intermediate ()
  562. (org-test-with-temp-text "#+name: foo
  563. #+begin_src emacs-lisp
  564. 2
  565. #+end_src
  566. #+results: foo
  567. : 4
  568. #+begin_src emacs-lisp :var it=foo
  569. (+ it 1)
  570. #+end_src"
  571. (let ((org-babel-update-intermediate nil))
  572. (goto-char (point-min))
  573. (org-babel-next-src-block 2)
  574. (should (= 3 (org-babel-execute-src-block)))
  575. (goto-char (point-min))
  576. (forward-line 6)
  577. (should (looking-at ": 4")))
  578. (let ((org-babel-update-intermediate t))
  579. (goto-char (point-min))
  580. (org-babel-next-src-block 2)
  581. (should (= 3 (org-babel-execute-src-block)))
  582. (goto-char (point-min))
  583. (forward-line 6)
  584. (should (looking-at ": 2")))))
  585. (ert-deftest test-ob/eval-header-argument ()
  586. (flet ((check-eval (eval runp)
  587. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  588. (setq foo :evald)
  589. #+end_src" eval)
  590. (let ((foo :not-run))
  591. (if runp
  592. (progn (should (org-babel-execute-src-block))
  593. (should (eq foo :evald)))
  594. (progn (should-not (org-babel-execute-src-block))
  595. (should-not (eq foo :evald))))))))
  596. (check-eval "never" nil)
  597. (check-eval "no" nil)
  598. (check-eval "never-export" t)
  599. (check-eval "no-export" t)
  600. (let ((org-babel-exp-reference-buffer (current-buffer)))
  601. (check-eval "never" nil)
  602. (check-eval "no" nil)
  603. (check-eval "never-export" nil)
  604. (check-eval "no-export" nil))))
  605. (ert-deftest test-ob/noweb-expansion-1 ()
  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. (should (string= (org-babel-expand-noweb-references) "bar"))))
  614. (ert-deftest test-ob/noweb-expansion-2 ()
  615. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  616. <<foo>>
  617. #+end_src
  618. #+name: foo
  619. #+begin_src sh :noweb-sep \"\"
  620. bar
  621. #+end_src
  622. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  623. baz
  624. #+end_src"
  625. (should (string= (org-babel-expand-noweb-references) "barbaz"))))
  626. (ert-deftest test-ob/splitting-variable-lists-in-references ()
  627. (org-test-with-temp-text ""
  628. (should (= 1 (length (org-babel-ref-split-args
  629. "a=\"this, no work\""))))
  630. (should (= 2 (length (org-babel-ref-split-args
  631. "a=\"this, no work\", b=1"))))))
  632. (ert-deftest test-ob/org-babel-balanced-split ()
  633. (should (equal
  634. '(":a 1" "b [2 3]" "c (4 :d (5 6))")
  635. (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
  636. '((32 9) . 58)))))
  637. (ert-deftest test-ob/commented-last-block-line-no-var ()
  638. (org-test-with-temp-text-in-file "
  639. #+begin_src emacs-lisp
  640. ;;
  641. #+end_src"
  642. (org-babel-next-src-block)
  643. (org-babel-execute-maybe)
  644. (should (re-search-forward "\\#\\+results:" nil t))
  645. (forward-line)
  646. (should
  647. (string=
  648. ""
  649. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  650. (org-test-with-temp-text-in-file "
  651. #+begin_src emacs-lisp
  652. \"some text\";;
  653. #+end_src"
  654. (org-babel-next-src-block)
  655. (org-babel-execute-maybe)
  656. (should (re-search-forward "\\#\\+results:" nil t))
  657. (forward-line)
  658. (should
  659. (string=
  660. ": some text"
  661. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  662. (ert-deftest test-ob/commented-last-block-line-with-var ()
  663. (org-test-with-temp-text-in-file "
  664. #+begin_src emacs-lisp :var a=1
  665. ;;
  666. #+end_src"
  667. (org-babel-next-src-block)
  668. (org-babel-execute-maybe)
  669. (re-search-forward "\\#\\+results:" nil t)
  670. (forward-line)
  671. (should (string=
  672. ""
  673. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  674. (org-test-with-temp-text-in-file "
  675. #+begin_src emacs-lisp :var a=2
  676. 2;;
  677. #+end_src"
  678. (org-babel-next-src-block)
  679. (org-babel-execute-maybe)
  680. (re-search-forward "\\#\\+results:" nil t)
  681. (forward-line)
  682. (should (string=
  683. ": 2"
  684. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  685. (ert-deftest test-ob/org-babel-insert-result--improper-lists ()
  686. "Test `org-babel-insert-result' with improper lists."
  687. ;; Do not error when output is an improper list.
  688. (should
  689. (org-test-with-temp-text
  690. "
  691. <point>#+BEGIN_SRC emacs-lisp
  692. '((1 . nil) (2 . 3))
  693. #+END_SRC
  694. "
  695. (org-babel-execute-maybe) t)))
  696. (ert-deftest test-ob/remove-inline-result ()
  697. "Test `org-babel-remove-inline-result' honors whitespace."
  698. (let*
  699. ((inline-sb "src_emacs-lisp{(+ 1 2)}")
  700. (inline-res " {{{results(=3=)}}}")
  701. (inline-sb-dot (concat inline-sb "."))
  702. (inline-sb-res-dot (concat inline-sb inline-res ".")))
  703. (org-test-with-temp-text
  704. ;; Insert inline_src_block followed by dot.
  705. inline-sb-dot
  706. ;; Insert result before dot.
  707. (org-babel-execute-maybe)
  708. (should (string= inline-sb-res-dot
  709. (buffer-substring-no-properties
  710. (point-at-bol) (point-at-eol))))
  711. ;; Delete whitespace and result.
  712. (org-babel-remove-inline-result)
  713. (should (string= inline-sb-dot
  714. (buffer-substring-no-properties
  715. (point-at-bol) (point-at-eol))))
  716. ;; Add whitespace and result before dot.
  717. (search-forward inline-sb)
  718. (insert " " inline-res)
  719. (goto-char (point-at-bol))
  720. ;; Remove whitespace and result.
  721. (org-babel-remove-inline-result)
  722. (should (string= inline-sb-dot
  723. (buffer-substring-no-properties
  724. (point-at-bol) (point-at-eol))))
  725. ;; Add whitespace before dot.
  726. (search-forward inline-sb)
  727. (insert " ")
  728. (goto-char (point-at-bol))
  729. ;; Add result before whitespace.
  730. (org-babel-execute-maybe)
  731. ;; Remove result - leave trailing whitespace and dot.
  732. (org-babel-remove-inline-result)
  733. (should (string= (concat inline-sb " .")
  734. (buffer-substring-no-properties
  735. (point-at-bol) (point-at-eol)))))))
  736. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  737. "Test helper function to test `org-babel-remove-result'.
  738. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  739. and the result of execution is verified against RESULT.
  740. The block is actually executed /twice/ to ensure result
  741. replacement happens correctly."
  742. (org-test-with-temp-text
  743. buffer-text
  744. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  745. (should (re-search-forward "\\#\\+results:" nil t))
  746. (forward-line)
  747. (should (string= result
  748. (buffer-substring-no-properties
  749. (point-at-bol)
  750. (- (point-max) 16))))
  751. (org-babel-previous-src-block) (org-babel-remove-result)
  752. (should (string= buffer-text
  753. (buffer-substring-no-properties
  754. (point-min) (point-max))))))
  755. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  756. "Test `org-babel-remove-result' with default :results."
  757. (mapcar (lambda (language)
  758. (test-ob-verify-result-and-removed-result
  759. "\n"
  760. (concat
  761. "* org-babel-remove-result
  762. #+begin_src " language "
  763. #+end_src
  764. * next heading")))
  765. '("sh" "emacs-lisp")))
  766. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  767. "Test `org-babel-remove-result' with :results list."
  768. (test-ob-verify-result-and-removed-result
  769. "- 1
  770. - 2
  771. - 3
  772. - (quote (4 5))"
  773. "* org-babel-remove-result
  774. #+begin_src emacs-lisp :results list
  775. '(1 2 3 '(4 5))
  776. #+end_src
  777. * next heading"))
  778. (ert-deftest test-ob/org-babel-results-indented-wrap ()
  779. "Ensure that wrapped results are inserted correction when indented.
  780. If not inserted correctly then the second evaluation will fail
  781. trying to find the :END: marker."
  782. (org-test-with-temp-text
  783. "- indented
  784. #+begin_src sh :results file wrap
  785. echo test.txt
  786. #+end_src"
  787. (org-babel-next-src-block 1)
  788. (org-babel-execute-src-block)
  789. (org-babel-execute-src-block)))
  790. (ert-deftest test-ob/file-desc-header-argument ()
  791. "Test that the :file-desc header argument is used."
  792. (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  793. \"foo\"
  794. #+end_src
  795. #+begin_src emacs-lisp :results file :file-desc
  796. \"foo\"
  797. #+end_src"
  798. (org-babel-execute-src-block)
  799. (org-babel-next-src-block 1)
  800. (org-babel-execute-src-block)
  801. (goto-char (point-min))
  802. (should (search-forward "[[file:foo][bar]]" nil t))
  803. (should (search-forward "[[file:foo][foo]]" nil t))))
  804. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  805. "Test `org-babel-remove-result' with :results pp."
  806. (test-ob-verify-result-and-removed-result
  807. ": \"I /am/ working!\""
  808. "* org-babel-remove-result
  809. #+begin_src emacs-lisp :results pp
  810. \"I /am/ working!\")
  811. #+end_src
  812. * next heading"))
  813. (ert-deftest test-ob/inline-src_blk-preceded-punct-preceded-by-point ()
  814. (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  815. (org-babel-inline-result-wrap "=%s="))
  816. (org-test-with-temp-text
  817. test-line
  818. (forward-char 1)
  819. (org-babel-execute-maybe)
  820. (should (re-search-forward "=\"x\"=" nil t))
  821. (forward-line))))
  822. (ert-deftest test-ob/commented-last-block-line-with-var ()
  823. (org-test-with-temp-text-in-file "
  824. #+begin_src emacs-lisp :var a=1
  825. ;;
  826. #+end_src"
  827. (org-babel-next-src-block)
  828. (org-babel-execute-maybe)
  829. (re-search-forward "\\#\\+results:" nil t)
  830. (forward-line)
  831. (should (string=
  832. ""
  833. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  834. (org-test-with-temp-text-in-file "
  835. #+begin_src emacs-lisp :var a=2
  836. 2;;
  837. #+end_src"
  838. (org-babel-next-src-block)
  839. (org-babel-execute-maybe)
  840. (re-search-forward "\\#\\+results:" nil t)
  841. (forward-line)
  842. (should (string=
  843. ": 2"
  844. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  845. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  846. "Test helper function to test `org-babel-remove-result'.
  847. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  848. and the result of execution is verified against RESULT.
  849. The block is actually executed /twice/ to ensure result
  850. replacement happens correctly."
  851. (org-test-with-temp-text
  852. buffer-text
  853. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  854. (should (re-search-forward "\\#\\+results:" nil t))
  855. (forward-line)
  856. (should (string= result
  857. (buffer-substring-no-properties
  858. (point-at-bol)
  859. (- (point-max) 16))))
  860. (org-babel-previous-src-block) (org-babel-remove-result)
  861. (should (string= buffer-text
  862. (buffer-substring-no-properties
  863. (point-min) (point-max))))))
  864. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  865. "Test `org-babel-remove-result' with default :results."
  866. (mapcar (lambda (language)
  867. (test-ob-verify-result-and-removed-result
  868. "\n"
  869. (concat
  870. "* org-babel-remove-result
  871. #+begin_src " language "
  872. #+end_src
  873. * next heading")))
  874. '("sh" "emacs-lisp")))
  875. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  876. "Test `org-babel-remove-result' with :results list."
  877. (test-ob-verify-result-and-removed-result
  878. "- 1
  879. - 2
  880. - 3
  881. - (quote (4 5))"
  882. "* org-babel-remove-result
  883. #+begin_src emacs-lisp :results list
  884. '(1 2 3 '(4 5))
  885. #+end_src
  886. * next heading"))
  887. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  888. "Test `org-babel-remove-result' with :results wrap."
  889. (test-ob-verify-result-and-removed-result
  890. ":RESULTS:
  891. hello there
  892. :END:"
  893. "* org-babel-remove-result
  894. #+begin_src emacs-lisp :results wrap
  895. \"hello there\"
  896. #+end_src
  897. * next heading"))
  898. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  899. "Test `org-babel-remove-result' with :results org."
  900. (test-ob-verify-result-and-removed-result
  901. "#+BEGIN_SRC org
  902. ,* heading
  903. ,** subheading
  904. content
  905. #+END_SRC"
  906. "* org-babel-remove-result
  907. #+begin_src emacs-lisp :results org
  908. \"* heading
  909. ,** subheading
  910. content\"
  911. #+end_src
  912. * next heading"))
  913. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  914. "Test `org-babel-remove-result' with :results html."
  915. (test-ob-verify-result-and-removed-result
  916. "#+BEGIN_EXPORT html
  917. <head><body></body></head>
  918. #+END_EXPORT"
  919. "* org-babel-remove-result
  920. #+begin_src emacs-lisp :results html
  921. \"<head><body></body></head>\"
  922. #+end_src
  923. * next heading"))
  924. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  925. "Test `org-babel-remove-result' with :results latex."
  926. (test-ob-verify-result-and-removed-result
  927. "#+BEGIN_EXPORT latex
  928. Line 1
  929. Line 2
  930. Line 3
  931. #+END_EXPORT"
  932. "* org-babel-remove-result
  933. #+begin_src emacs-lisp :results latex
  934. \"Line 1
  935. Line 2
  936. Line 3\"
  937. #+end_src
  938. * next heading"))
  939. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  940. "Test `org-babel-remove-result' with :results code."
  941. (test-ob-verify-result-and-removed-result
  942. "#+BEGIN_SRC emacs-lisp
  943. \"I am working!\"
  944. #+END_SRC"
  945. "* org-babel-remove-result
  946. #+begin_src emacs-lisp :results code
  947. (message \"I am working!\")
  948. #+end_src
  949. * next heading"))
  950. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  951. "Test `org-babel-remove-result' with :results pp."
  952. (test-ob-verify-result-and-removed-result
  953. ": \"I /am/ working!\""
  954. "* org-babel-remove-result
  955. #+begin_src emacs-lisp :results pp
  956. \"I /am/ working!\")
  957. #+end_src
  958. * next heading"))
  959. (ert-deftest test-ob/results-do-not-replace-code-blocks ()
  960. (org-test-with-temp-text "Block two has a space after the name.
  961. #+name: foo
  962. #+begin_src emacs-lisp
  963. 1
  964. #+end_src
  965. #+name: foo
  966. #+begin_src emacs-lisp
  967. 2
  968. #+end_src
  969. #+name: foo
  970. #+begin_src emacs-lisp
  971. 3
  972. #+end_src
  973. #+RESULTS: foo
  974. : foo
  975. "
  976. (dolist (num '(1 2 3))
  977. ;; execute the block
  978. (goto-char (point-min))
  979. (org-babel-next-src-block num) (org-babel-execute-src-block)
  980. ;; check the results
  981. (goto-char (point-max))
  982. (move-beginning-of-line 0)
  983. (should (looking-at (format ": %d" num))))))
  984. (ert-deftest test-ob/blocks-with-spaces ()
  985. "Test expansion of blocks followed by blank lines."
  986. (should
  987. (equal "#+BEGIN_SRC emacs-lisp
  988. \(+ 1 2)
  989. #+END_SRC
  990. #+RESULTS:
  991. : 3\n\n\n"
  992. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
  993. \(+ 1 2)
  994. #+END_SRC\n\n\n"
  995. (let ((org-babel-next-src-block "RESULTS"))
  996. (org-babel-execute-src-block))
  997. (buffer-string)))))
  998. (ert-deftest test-ob/results-in-narrowed-buffer ()
  999. "Test block execution in a narrowed buffer."
  1000. ;; If results don't exist, they should be inserted in visible part
  1001. ;; of the buffer.
  1002. (should
  1003. (equal
  1004. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
  1005. (org-test-with-temp-text
  1006. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1007. (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
  1008. (let ((org-babel-results-keyword "RESULTS"))
  1009. (org-babel-execute-src-block))
  1010. (org-trim (buffer-string)))))
  1011. (should
  1012. (equal
  1013. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
  1014. (org-test-with-temp-text
  1015. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1016. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1017. (let ((org-babel-results-keyword "RESULTS"))
  1018. (org-babel-execute-src-block))
  1019. (org-trim (buffer-string)))))
  1020. ;; Results in visible part of buffer, should be updated here.
  1021. (should
  1022. (equal
  1023. "#+NAME: test
  1024. #+BEGIN_SRC emacs-lisp
  1025. \(+ 1 2)
  1026. #+END_SRC
  1027. #+RESULTS: test
  1028. : 3"
  1029. (org-test-with-temp-text
  1030. "#+NAME: test
  1031. #+BEGIN_SRC emacs-lisp
  1032. \(+ 1 2)
  1033. #+END_SRC
  1034. #+RESULTS: test
  1035. : 4
  1036. Paragraph"
  1037. (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
  1038. (let ((org-babel-results-keyword "RESULTS"))
  1039. (org-babel-execute-src-block))
  1040. (org-trim (buffer-string)))))
  1041. ;; Results in invisible part of buffer, should be updated there.
  1042. (org-test-with-temp-text
  1043. "#+NAME: test
  1044. #+BEGIN_SRC emacs-lisp
  1045. \(+ 1 2)
  1046. #+END_SRC
  1047. #+RESULTS: test
  1048. : 4
  1049. Paragraph"
  1050. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1051. (let ((org-babel-results-keyword "RESULTS"))
  1052. (org-babel-execute-src-block))
  1053. (should-not (re-search-forward "^#\\+RESULTS:" nil t))
  1054. (widen)
  1055. (should (re-search-forward "^: 3" nil t))))
  1056. (ert-deftest test-ob/specific-colnames ()
  1057. "Test passing specific column names."
  1058. (should
  1059. (equal "#+name: input-table
  1060. | id | var1 |
  1061. |----+------|
  1062. | 1 | bar |
  1063. | 2 | baz |
  1064. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1065. echo \"$data\"
  1066. #+end_src
  1067. #+RESULTS:
  1068. | Rev | Author |
  1069. |-----+--------|
  1070. | 1 | bar |
  1071. | 2 | baz |
  1072. "
  1073. (org-test-with-temp-text
  1074. "#+name: input-table
  1075. | id | var1 |
  1076. |----+------|
  1077. | 1 | bar |
  1078. | 2 | baz |
  1079. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1080. echo \"$data\"
  1081. #+end_src
  1082. "
  1083. ;; we should find a code block
  1084. (should (re-search-forward org-babel-src-block-regexp nil t))
  1085. (goto-char (match-beginning 0))
  1086. ;; now that we've located the code block, it may be evaluated
  1087. (let ((org-babel-execute-src-block "RESULTS"))
  1088. (org-babel-execute-src-block))
  1089. (buffer-string)))))
  1090. (ert-deftest test-ob/location-of-header-arg-eval ()
  1091. "Test location of header argument evaluation."
  1092. (org-test-with-temp-text "
  1093. #+name: top-block
  1094. #+begin_src emacs-lisp :var pt=(point)
  1095. pt
  1096. #+end_src
  1097. #+name: bottom-block
  1098. #+begin_src emacs-lisp :var pt=top-block()
  1099. pt
  1100. #+end_src
  1101. "
  1102. ;; the value of the second block should be greater than the first
  1103. (should
  1104. (< (progn (re-search-forward org-babel-src-block-regexp nil t)
  1105. (goto-char (match-beginning 0))
  1106. (prog1 (save-match-data (org-babel-execute-src-block))
  1107. (goto-char (match-end 0))))
  1108. (progn (re-search-forward org-babel-src-block-regexp nil t)
  1109. (goto-char (match-beginning 0))
  1110. (org-babel-execute-src-block))))))
  1111. (ert-deftest test-ob/preserve-results-indentation ()
  1112. "Preserve indentation when executing a src block."
  1113. (should
  1114. (equal
  1115. '(2 2)
  1116. (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1117. (org-babel-execute-src-block)
  1118. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1119. (list (org-get-indentation)
  1120. (progn (forward-line) (org-get-indentation))))))
  1121. (should
  1122. (equal
  1123. '(2 2)
  1124. (org-test-with-temp-text
  1125. " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1126. (org-babel-execute-src-block)
  1127. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1128. (list (org-get-indentation)
  1129. (progn (forward-line) (org-get-indentation))))))
  1130. ;; Don't get fooled by TAB-based indentation.
  1131. (should
  1132. (equal
  1133. '(6 6)
  1134. (org-test-with-temp-text
  1135. "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
  1136. (setq tab-width 4)
  1137. (org-babel-execute-src-block)
  1138. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1139. (list (org-get-indentation)
  1140. (progn (forward-line) (org-get-indentation)))))))
  1141. (ert-deftest test-ob/safe-header-args ()
  1142. "Detect safe and unsafe header args."
  1143. (let ((safe-args '((:cache . "foo")
  1144. (:results . "output")
  1145. (:eval . "never")
  1146. (:eval . "query")))
  1147. (unsafe-args '((:eval . "yes")
  1148. (:results . "output file")
  1149. (:foo . "bar")))
  1150. (malformed-args '((bar . "foo")
  1151. ("foo" . "bar")
  1152. :foo))
  1153. (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
  1154. (dolist (arg safe-args)
  1155. (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
  1156. (dolist (arg unsafe-args)
  1157. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1158. (dolist (arg malformed-args)
  1159. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1160. (should (not (funcall safe-p (append safe-args unsafe-args))))))
  1161. (ert-deftest test-ob/noweb-expansions-in-cache ()
  1162. "Ensure that noweb expansions are expanded before caching."
  1163. (let ((noweb-expansions-in-cache-var 0))
  1164. (org-test-with-temp-text "
  1165. #+name: foo
  1166. #+begin_src emacs-lisp
  1167. \"I said\"
  1168. #+end_src
  1169. #+name: bar
  1170. #+begin_src emacs-lisp :noweb yes :cache yes
  1171. (setq noweb-expansions-in-cache-var
  1172. (+ 1 noweb-expansions-in-cache-var))
  1173. (concat <<foo>> \" check noweb expansions\")
  1174. #+end_src
  1175. "
  1176. ;; run the second block to create the cache
  1177. (goto-char (point-min))
  1178. (re-search-forward (regexp-quote "#+name: bar"))
  1179. (should (string= "I said check noweb expansions"
  1180. (org-babel-execute-src-block)))
  1181. (should (= noweb-expansions-in-cache-var 1))
  1182. ;; change the value of the first block
  1183. (goto-char (point-min))
  1184. (re-search-forward (regexp-quote "said"))
  1185. (goto-char (match-beginning 0))
  1186. (insert "haven't ")
  1187. (re-search-forward (regexp-quote "#+name: bar"))
  1188. (should (string= "I haven't said check noweb expansions"
  1189. (org-babel-execute-src-block)))
  1190. (should (= noweb-expansions-in-cache-var 2)))))
  1191. (ert-deftest test-ob/file-ext-and-output-dir ()
  1192. (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
  1193. (org-babel-next-src-block)
  1194. (should (equal "file-ext-basic.txt"
  1195. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1196. (org-babel-next-src-block)
  1197. (should (equal "foo/file-ext-dir-relative.txt"
  1198. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1199. (org-babel-next-src-block)
  1200. (should (equal "foo/file-ext-dir-relative-slash.txt"
  1201. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1202. (org-babel-next-src-block)
  1203. (should (equal "/tmp/file-ext-dir-absolute.txt"
  1204. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1205. (org-babel-next-src-block)
  1206. (should (equal "foo.bar"
  1207. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1208. (org-babel-next-src-block)
  1209. (should (equal "xxx/foo.bar"
  1210. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1211. ))
  1212. (ert-deftest test-ob/script-escape ()
  1213. ;; Delimited lists of numbers
  1214. (should (equal '(1 2 3)
  1215. (org-babel-script-escape "[1 2 3]")))
  1216. (should (equal '(1 2 3)
  1217. (org-babel-script-escape "{1 2 3}")))
  1218. (should (equal '(1 2 3)
  1219. (org-babel-script-escape "(1 2 3)")))
  1220. ;; Delimited lists of double-quoted strings
  1221. (should (equal '("foo" "bar")
  1222. (org-babel-script-escape "(\"foo\" \"bar\")")))
  1223. (should (equal '("foo" "bar")
  1224. (org-babel-script-escape "[\"foo\" \"bar\"]")))
  1225. (should (equal '("foo" "bar")
  1226. (org-babel-script-escape "{\"foo\" \"bar\"}")))
  1227. ;; ... with commas
  1228. (should (equal '("foo" "bar")
  1229. (org-babel-script-escape "(\"foo\", \"bar\")")))
  1230. (should (equal '("foo" "bar")
  1231. (org-babel-script-escape "[\"foo\", \"bar\"]")))
  1232. (should (equal '("foo" "bar")
  1233. (org-babel-script-escape "{\"foo\", \"bar\"}")))
  1234. ;; Delimited lists of single-quoted strings
  1235. (should (equal '("foo" "bar")
  1236. (org-babel-script-escape "('foo' 'bar')")))
  1237. (should (equal '("foo" "bar")
  1238. (org-babel-script-escape "['foo' 'bar']")))
  1239. (should (equal '("foo" "bar")
  1240. (org-babel-script-escape "{'foo' 'bar'}")))
  1241. ;; ... with commas
  1242. (should (equal '("foo" "bar")
  1243. (org-babel-script-escape "('foo', 'bar')")))
  1244. (should (equal '("foo" "bar")
  1245. (org-babel-script-escape "['foo', 'bar']")))
  1246. (should (equal '("foo" "bar")
  1247. (org-babel-script-escape "{'foo', 'bar'}")))
  1248. ;; Single quoted strings
  1249. (should (equal "foo"
  1250. (org-babel-script-escape "'foo'")))
  1251. ;; ... with internal double quote
  1252. (should (equal "foo\"bar"
  1253. (org-babel-script-escape "'foo\"bar'")))
  1254. ;; ... with internal backslash
  1255. (should (equal "foo\\bar"
  1256. (org-babel-script-escape "'foo\\bar'")))
  1257. ;; ... with internal escaped backslash
  1258. (should (equal "foo\\bar"
  1259. (org-babel-script-escape "'foo\\\\bar'")))
  1260. ;; ... with internal backslash-double quote
  1261. (should (equal "foo\\\"bar"
  1262. (org-babel-script-escape "'foo\\\"bar'")))
  1263. ;; ... with internal escaped backslash-double quote
  1264. (should (equal "foo\\\"bar"
  1265. (org-babel-script-escape "'foo\\\\\"bar'")))
  1266. ;; ... with internal escaped single quote
  1267. (should (equal "foo'bar"
  1268. (org-babel-script-escape "'foo\\'bar'")))
  1269. ;; ... with internal escaped backslash-escaped single quote
  1270. (should (equal "foo\\'bar"
  1271. (org-babel-script-escape "'foo\\\\\\'bar'")))
  1272. ;; Double quoted strings
  1273. (should (equal "foo"
  1274. (org-babel-script-escape "\"foo\"")))
  1275. ;; ... with internal single quote
  1276. (should (equal "foo'bar"
  1277. (org-babel-script-escape "\"foo'bar\"")))
  1278. ;; ... with internal backslash
  1279. (should (equal "foo\\bar"
  1280. (org-babel-script-escape "\"foo\\bar\"")))
  1281. ;; ... with internal escaped backslash
  1282. (should (equal "foo\\bar"
  1283. (org-babel-script-escape "\"foo\\\\bar\"")))
  1284. ;; ... with internal backslash-single quote
  1285. (should (equal "foo\\'bar"
  1286. (org-babel-script-escape "\"foo\\'bar\"")))
  1287. ;; ... with internal escaped backslash-single quote
  1288. (should (equal "foo\\'bar"
  1289. (org-babel-script-escape "\"foo\\\\'bar\"")))
  1290. ;; ... with internal escaped double quote
  1291. (should (equal "foo\"bar"
  1292. (org-babel-script-escape "\"foo\\\"bar\"")))
  1293. ;; ... with internal escaped backslash-escaped double quote
  1294. (should (equal "foo\\\"bar"
  1295. (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
  1296. (ert-deftest test-ob/process-params-no-duplicates ()
  1297. (should (equal (org-babel-process-params '((:colname-names . 1)
  1298. (:rowname-names . 1)
  1299. (:result-params . 1)
  1300. (:result-type . 1)
  1301. (:var . "\"foo\"")))
  1302. '((:var)
  1303. (:colname-names . 1)
  1304. (:rowname-names . 1)
  1305. (:result-params . 1)
  1306. (:result-type . value)))))
  1307. (defun org-test-babel-confirm-evaluate (eval-value)
  1308. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  1309. nil
  1310. #+end_src" eval-value)
  1311. (goto-char (point-min))
  1312. (let ((info (org-babel-get-src-block-info)))
  1313. (org-babel-check-confirm-evaluate info))))
  1314. (ert-deftest test-ob/check-eval ()
  1315. (let ((org-confirm-babel-evaluate t))
  1316. ;; Non-export tests
  1317. (dolist (pair '(("no" . nil)
  1318. ("never" . nil)
  1319. ("query" . query)
  1320. ("yes" . query)))
  1321. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1322. ;; Export tests
  1323. (let ((org-babel-exp-reference-buffer t))
  1324. (dolist (pair '(("no" . nil)
  1325. ("never" . nil)
  1326. ("query" . query)
  1327. ("yes" . query)
  1328. ("never-export" . nil)
  1329. ("no-export" . nil)
  1330. ("query-export" . query)))
  1331. (message (car pair))
  1332. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))))
  1333. (let ((org-confirm-babel-evaluate nil))
  1334. ;; Non-export tests
  1335. (dolist (pair '(("no" . nil)
  1336. ("never" . nil)
  1337. ("query" . query)
  1338. ("yes" . t)))
  1339. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1340. ;; Export tests
  1341. (let ((org-babel-exp-reference-buffer t))
  1342. (dolist (pair '(("no" . nil)
  1343. ("never" . nil)
  1344. ("query" . query)
  1345. ("yes" . t)
  1346. ("never-export" . nil)
  1347. ("no-export" . nil)
  1348. ("query-export" . query)))
  1349. (message (car pair))
  1350. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair)))))))
  1351. (defun org-test-ob/update-block-body ()
  1352. "Test `org-babel-update-block-body' specifications."
  1353. (should
  1354. (equal "#+begin_src elisp\n 2\n#+end_src"
  1355. (let ((org-edit-src-content-indentation 2))
  1356. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1357. (org-babel-update-block-body "2")
  1358. (buffer-string)))))
  1359. ;; Preserve block indentation.
  1360. (should
  1361. (equal " #+begin_src elisp\n 2\n #+end_src"
  1362. (let ((org-edit-src-content-indentation 1))
  1363. (org-test-with-temp-text
  1364. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1365. (org-babel-update-block-body "2")
  1366. (buffer-string)))))
  1367. ;; Ignore NEW-BODY global indentation.
  1368. (should
  1369. (equal "#+begin_src elisp\n 2\n#+end_src"
  1370. (let ((org-edit-src-content-indentation 2))
  1371. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1372. (org-babel-update-block-body " 2")
  1373. (buffer-string)))))
  1374. ;; When indentation should be preserved ignore the two rules above.
  1375. (should
  1376. (equal " #+begin_src elisp\n2\n #+end_src"
  1377. (let ((org-edit-src-content-indentation 1)
  1378. (org-src-preserve-indentation t))
  1379. (org-test-with-temp-text
  1380. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1381. (org-babel-update-block-body "2")
  1382. (buffer-string)))))
  1383. (should
  1384. (equal " #+begin_src elisp -i\n2\n #+end_src"
  1385. (let ((org-edit-src-content-indentation 1))
  1386. (org-test-with-temp-text
  1387. " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
  1388. (org-babel-update-block-body "2")
  1389. (buffer-string)))))
  1390. (should
  1391. (equal "#+begin_src elisp\n 2\n#+end_src"
  1392. (let ((org-edit-src-content-indentation 2)
  1393. (org-src-preserve-indentation t))
  1394. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1395. (org-babel-update-block-body " 2")
  1396. (buffer-string)))))
  1397. (should
  1398. (equal "#+begin_src elisp -i\n 2\n#+end_src"
  1399. (let ((org-edit-src-content-indentation 2)
  1400. (org-src-preserve-indentation t))
  1401. (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
  1402. (org-babel-update-block-body " 2")
  1403. (buffer-string))))))
  1404. (ert-deftest test-ob/find-named-result ()
  1405. "Test `org-babel-find-named-result' specifications."
  1406. (should
  1407. (= 1
  1408. (org-test-with-temp-text "#+results: foo\n: result"
  1409. (org-babel-find-named-result "foo"))))
  1410. (should-not
  1411. (org-test-with-temp-text "#+results: foo\n: result"
  1412. (org-babel-find-named-result "bar")))
  1413. (should-not
  1414. (org-test-with-temp-text "#+results: foobar\n: result"
  1415. (org-babel-find-named-result "foo")))
  1416. ;; Search is case insensitive.
  1417. (should
  1418. (org-test-with-temp-text "#+RESULTS: FOO\n: result"
  1419. (org-babel-find-named-result "foo")))
  1420. ;; Handle hash in results keyword.
  1421. (should
  1422. (org-test-with-temp-text "#+results[hash]: FOO\n: result"
  1423. (org-babel-find-named-result "foo")))
  1424. ;; Accept orphaned affiliated keywords.
  1425. (should
  1426. (org-test-with-temp-text "#+results: foo"
  1427. (org-babel-find-named-result "foo"))))
  1428. (ert-deftest test-ob/where-is-src-block-result ()
  1429. "Test `org-babel-where-is-src-block-result' specifications."
  1430. ;; Find anonymous results.
  1431. (should
  1432. (equal "#+RESULTS:"
  1433. (org-test-with-temp-text
  1434. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
  1435. (goto-char (org-babel-where-is-src-block-result))
  1436. (buffer-substring-no-properties (point) (line-end-position)))))
  1437. ;; Find named results. Those have priority over anonymous ones.
  1438. (should
  1439. (equal "#+RESULTS: example"
  1440. (org-test-with-temp-text
  1441. "
  1442. <point>#+NAME: example
  1443. #+BEGIN_SRC emacs-lisp
  1444. \(+ 1 1)
  1445. #+END_SRC
  1446. #+RESULTS: example
  1447. : 2"
  1448. (goto-char (org-babel-where-is-src-block-result))
  1449. (buffer-substring-no-properties (point) (line-end-position)))))
  1450. (should
  1451. (equal "#+RESULTS: example"
  1452. (org-test-with-temp-text
  1453. "
  1454. <point>#+NAME: example
  1455. #+BEGIN_SRC emacs-lisp
  1456. \(+ 1 1)
  1457. #+END_SRC
  1458. #+RESULTS:
  1459. : fake
  1460. #+RESULTS: example
  1461. : 2"
  1462. (goto-char (org-babel-where-is-src-block-result))
  1463. (buffer-substring-no-properties (point) (line-end-position)))))
  1464. ;; Return nil when no result is found.
  1465. (should-not
  1466. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1467. (org-babel-where-is-src-block-result)))
  1468. ;; When optional argument INSERT is non-nil, add RESULTS keyword
  1469. ;; whenever no RESULTS can be found.
  1470. (should
  1471. (equal
  1472. "#+RESULTS:"
  1473. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1474. (let ((org-babel-results-keyword "RESULTS"))
  1475. (goto-char (org-babel-where-is-src-block-result t)))
  1476. (buffer-substring-no-properties (point) (line-end-position)))))
  1477. ;; Insert a named RESULTS keyword if possible.
  1478. (should
  1479. (equal
  1480. "#+RESULTS: e"
  1481. (org-test-with-temp-text
  1482. "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1483. (let ((org-babel-results-keyword "RESULTS"))
  1484. (goto-char (org-babel-where-is-src-block-result t)))
  1485. (buffer-substring-no-properties (point) (line-end-position)))))
  1486. ;; When optional argument HASH is provided, clear RESULTS keyword
  1487. ;; and related contents if they do not match it.
  1488. (should
  1489. (equal
  1490. "#+RESULTS[bbbb]:"
  1491. (org-test-with-temp-text
  1492. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 3"
  1493. (let ((org-babel-results-keyword "RESULTS"))
  1494. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1495. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1496. (should
  1497. (equal
  1498. "#+RESULTS[bbbb]:"
  1499. (org-test-with-temp-text
  1500. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
  1501. (let ((org-babel-results-keyword "RESULTS"))
  1502. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1503. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1504. ;; RESULTS keyword may not be the last affiliated keyword.
  1505. (should
  1506. (equal
  1507. "#+RESULTS[bbbb]:"
  1508. (org-test-with-temp-text
  1509. "
  1510. <point>#+BEGIN_SRC emacs-lisp
  1511. \(+ 1 1)
  1512. #+END_SRC
  1513. #+RESULTS[aaaa]:
  1514. #+NAME: e
  1515. : 3"
  1516. (let ((org-babel-results-keyword "RESULTS"))
  1517. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1518. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1519. ;; HASH does nothing if no RESULTS can be found. However, if INSERT
  1520. ;; is also non-nil, RESULTS keyword is inserted along with the
  1521. ;; expected hash.
  1522. (should
  1523. (equal
  1524. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1525. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1526. (org-babel-where-is-src-block-result nil nil "bbbb")
  1527. (buffer-string))))
  1528. (should
  1529. (equal
  1530. "#+RESULTS[bbbb]:"
  1531. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1532. (let ((org-babel-results-keyword "RESULTS"))
  1533. (goto-char (org-babel-where-is-src-block-result t nil "bbbb")))
  1534. (org-trim (buffer-substring-no-properties (point) (point-max)))))))
  1535. (provide 'test-ob)
  1536. ;;; test-ob ends here