test-ob.el 64 KB

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