test-ob.el 73 KB

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