test-ob.el 74 KB

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