test-ob.el 61 KB

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