test-ob.el 54 KB

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