test-ob.el 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  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-org-babel/indented-cached-org-bracket-link ()
  17. "When the result of a source block is a cached indented link it
  18. should still return the link."
  19. (should
  20. (let ((default-directory temporary-file-directory))
  21. (org-test-with-temp-text
  22. "
  23. * Test
  24. #+<point>BEGIN_SRC emacs-lisp :file test.txt :cache yes
  25. (message \"test\")
  26. #+END_SRC"
  27. ;; Execute twice as the first time creates the cache.
  28. (org-babel-execute-src-block)
  29. (string= (concat default-directory "test.txt")
  30. (org-babel-execute-src-block))))))
  31. (ert-deftest test-org-babel/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-org-babel/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-org-babel/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-org-babel/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-org-babel/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-org-babel/get-src-block-info-tangle ()
  114. (org-test-at-marker nil org-test-file-ob-anchor
  115. (let ((info (org-babel-get-src-block-info)))
  116. (should (string= "no" (cdr (assoc :tangle (nth 2 info))))))))
  117. (ert-deftest test-org-babel/elisp-in-header-arguments ()
  118. "Test execution of elisp forms in header arguments."
  119. (org-test-with-temp-text-in-file "
  120. * elisp forms in header arguments
  121. :PROPERTIES:
  122. :var: prop = (* 7 6)
  123. :END:
  124. #+begin_src emacs-lisp
  125. prop
  126. #+end_src"
  127. (goto-char (point-min))
  128. (org-babel-next-src-block)
  129. (let ((info (org-babel-get-src-block-info)))
  130. (should (= 42 (org-babel-execute-src-block))))))
  131. (ert-deftest test-org-babel/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-org-babel/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-org-babel/multi-line-header-arguments ()
  157. "Test that multi-line header arguments and can be read."
  158. (org-test-with-temp-text-in-file "
  159. #+headers: :var letters='(a b c d e f g)
  160. #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
  161. (require 'cl)
  162. (defalias 'my-map (if (org-version-check \"24.2.50\" \"cl\" :predicate)
  163. 'cl-map
  164. 'map))
  165. (my-map 'list #'list numbers letters)
  166. #+end_src"
  167. (org-babel-next-src-block)
  168. (let ((results (org-babel-execute-src-block)))
  169. (should(equal 'a (cadr (assoc 1 results))))
  170. (should(equal 'd (cadr (assoc 4 results)))))))
  171. (ert-deftest test-org-babel/parse-header-args ()
  172. (org-test-with-temp-text-in-file "
  173. #+begin_src example-lang :session :results output :var num=9
  174. the body
  175. #+end_src"
  176. (org-babel-next-src-block)
  177. (let* ((info (org-babel-get-src-block-info))
  178. (params (nth 2 info)))
  179. (message "%S" params)
  180. (should (equal "example-lang" (nth 0 info)))
  181. (should (string= "the body" (org-babel-trim (nth 1 info))))
  182. (should-not (member '(:session\ \ \ \ ) params))
  183. (should (equal '(:session) (assoc :session params)))
  184. (should (equal '(:result-type . output) (assoc :result-type params)))
  185. (should (equal '(num . 9) (cdr (assoc :var params)))))))
  186. (ert-deftest test-org-babel/parse-header-args2 ()
  187. (org-test-with-temp-text-in-file "
  188. * resolving sub-trees as references
  189. #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
  190. (length text)
  191. #+end_src
  192. #+begin_src org :noweb yes
  193. <<simple-subtree>>
  194. <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
  195. #+end_src
  196. ** simple subtree with custom ID
  197. :PROPERTIES:
  198. :CUSTOM_ID: simple-subtree
  199. :END:
  200. this is simple"
  201. (should (string-match (regexp-quote "this is simple")
  202. (org-babel-ref-resolve "simple-subtree")))
  203. (org-babel-next-src-block)
  204. (should (= 14 (org-babel-execute-src-block)))))
  205. (ert-deftest test-org-babel/inline-src-blocks ()
  206. (should
  207. (= 1
  208. (org-test-with-temp-text
  209. "In the middle <point>src_emacs-lisp{(+ 0 1)} of a line"
  210. (org-babel-execute-src-block))))
  211. (should
  212. (= 2
  213. (org-test-with-temp-text
  214. "One at the end of a line: <point>src_emacs-lisp{(+ 1 1)}"
  215. (org-babel-execute-src-block))))
  216. (should
  217. (= 3
  218. (org-test-with-temp-text
  219. "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
  220. (org-babel-execute-src-block))))
  221. (should
  222. (= 4
  223. (org-test-with-temp-text
  224. "In the middle <point>src_emacs-lisp[:results silent\
  225. :exports code]{(+ 3 1)} of a line"
  226. (org-babel-execute-src-block))))
  227. (should
  228. (= 5
  229. (org-test-with-temp-text
  230. "One at the end of a line: <point>src_emacs-lisp[:results silent\
  231. :exports code]{(+ 4 1)}"
  232. (org-babel-execute-src-block))))
  233. (should
  234. (= 6
  235. (org-test-with-temp-text
  236. "src_emacs-lisp[:results silent :exports code]{(+ 5 1)}\
  237. at the beginning of a line."
  238. (org-babel-execute-src-block))))
  239. (should
  240. (= 7
  241. (org-test-with-temp-text
  242. "One also evaluated: <point>src_emacs-lisp[:exports both\
  243. :results silent]{(+ 6 1)}"
  244. (org-babel-execute-src-block)))))
  245. (ert-deftest test-org-babel/org-babel-get-inline-src-block-matches ()
  246. (flet ((test-at-id (id)
  247. (org-test-at-id
  248. id
  249. (let ((test-point (point)))
  250. (should (fboundp 'org-babel-get-inline-src-block-matches))
  251. (should (re-search-forward "src_" nil t)) ;; 1
  252. (should (org-babel-get-inline-src-block-matches))
  253. (should (re-search-forward " b" nil (point-at-bol))) ;; 1
  254. (should-not (org-babel-get-inline-src-block-matches))
  255. (should (re-search-forward "in" nil t)) ;; 2
  256. (should-not (org-babel-get-inline-src-block-matches))
  257. (should (re-search-forward "echo" nil t)) ;; 2
  258. (should (org-babel-get-inline-src-block-matches))
  259. (should (re-search-forward "blocks" nil t)) ;; 3
  260. (backward-char 7) ;; 3
  261. (should (org-babel-get-inline-src-block-matches))
  262. (forward-char 1) ;;3
  263. (should-not (org-babel-get-inline-src-block-matches))
  264. (should (re-search-forward ":results" nil t)) ;; 4
  265. (should (org-babel-get-inline-src-block-matches))
  266. (end-of-line)
  267. (should-not (org-babel-get-inline-src-block-matches))))))
  268. (test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74")
  269. (test-at-id "d55dada7-de0e-4340-8061-787cccbedee5")))
  270. (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-1 ()
  271. (let ((test-line "src_sh{echo 1}")
  272. (org-babel-inline-result-wrap "=%s="))
  273. ;; src_ at bol line 1...
  274. (org-test-with-temp-text
  275. test-line
  276. (goto-char (point-min)) (org-babel-execute-maybe)
  277. (should (string=
  278. (concat test-line " {{{results(=1=)}}}")
  279. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  280. (forward-char) (org-babel-execute-maybe)
  281. (should (string=
  282. (concat test-line " {{{results(=1=)}}}")
  283. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  284. (re-search-forward "{{{")
  285. ;;(should-error (org-ctrl-c-ctrl-c))
  286. (backward-char 4) ;; last char of block body
  287. (org-babel-execute-maybe)
  288. (should (string=
  289. (concat test-line " {{{results(=1=)}}}")
  290. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  291. ;; src_ follows space line 1...
  292. (let ((test-line " src_emacs-lisp{ 1 }"))
  293. (org-test-with-temp-text
  294. test-line
  295. (should-error (org-ctrl-c-ctrl-c))
  296. (forward-char) (org-babel-execute-maybe)
  297. (should (string=
  298. (concat test-line " {{{results(=1=)}}}")
  299. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  300. (re-search-forward "{ 1 ") (org-babel-execute-maybe)
  301. (should (string=
  302. (concat test-line " {{{results(=1=)}}}")
  303. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  304. (forward-char 6)
  305. (should-error (org-ctrl-c-ctrl-c))
  306. ))))
  307. (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-2 ()
  308. ;; src_ at bol line 2...
  309. (let ((test-line " src_emacs-lisp{ \"x\" }")
  310. (org-babel-inline-result-wrap "=%s="))
  311. (org-test-with-temp-text
  312. (concat "\n" test-line)
  313. (should-error (org-ctrl-c-ctrl-c))
  314. (goto-char (point-min))
  315. (should-error (org-ctrl-c-ctrl-c))
  316. (forward-line)
  317. (should-error (org-ctrl-c-ctrl-c))
  318. (forward-char) (org-babel-execute-maybe)
  319. (should (string=
  320. (concat test-line " {{{results(=x=)}}}")
  321. (buffer-substring-no-properties
  322. (point-at-bol) (point-at-eol))))))
  323. (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
  324. (org-babel-inline-result-wrap "=%s="))
  325. (org-test-with-temp-text
  326. test-line
  327. (goto-char (point-max))
  328. (insert (concat "\n" test-line " end"))
  329. (re-search-backward "src") (org-babel-execute-maybe)
  330. (should (string=
  331. (concat test-line " {{{results(=y=)}}} end")
  332. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  333. (re-search-forward "\" ") (org-babel-execute-maybe)
  334. (should (string=
  335. (concat test-line " {{{results(=y=)}}} end")
  336. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  337. (forward-char 3)
  338. (should-error (org-ctrl-c-ctrl-c)))))
  339. (ert-deftest test-org-babel/inline-src_blk-manual-results-replace ()
  340. (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
  341. (org-babel-inline-result-wrap "=%s="))
  342. (org-test-with-temp-text
  343. (concat "\n" test-line)
  344. (should-error (org-ctrl-c-ctrl-c))
  345. (goto-char (point-max))
  346. (org-babel-execute-maybe)
  347. (beginning-of-line)
  348. (should-error (org-ctrl-c-ctrl-c))
  349. (forward-char) (org-babel-execute-maybe)
  350. (should (string=
  351. (concat test-line " {{{results(=x=)}}}")
  352. (buffer-substring-no-properties
  353. (point-at-bol) (point-at-eol))))))
  354. (let ((test-line (concat " Some text prior to block "
  355. "src_emacs-lisp[:results replace]{ \"y\" }"))
  356. (org-babel-inline-result-wrap "=%s="))
  357. (org-test-with-temp-text test-line
  358. (goto-char (point-max))
  359. (insert (concat "\n" test-line " end"))
  360. (re-search-backward "src") (org-babel-execute-maybe)
  361. (should (string=
  362. (concat test-line " {{{results(=y=)}}} end")
  363. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  364. (re-search-forward "\" ") (org-babel-execute-maybe)
  365. (should (string=
  366. (concat test-line " {{{results(=y=)}}} end")
  367. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  368. (forward-char 3)
  369. (should-error (org-ctrl-c-ctrl-c)))))
  370. (ert-deftest test-org-babel/inline-src_blk-results-silent ()
  371. (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
  372. (org-test-with-temp-text test-line
  373. (org-babel-execute-maybe)
  374. (should (string= test-line
  375. (buffer-substring-no-properties
  376. (point-at-bol) (point-at-eol))))))
  377. (let ((test-line (concat " Some text prior to block src_emacs-lisp"
  378. "[ :results silent ]{ \"y\" }")))
  379. (org-test-with-temp-text
  380. test-line
  381. (goto-char (point-max))
  382. (insert (concat "\n" test-line " end"))
  383. (re-search-backward "src_") (org-babel-execute-maybe)
  384. (should (string= (concat test-line " end")
  385. (buffer-substring-no-properties
  386. (point-at-bol) (point-at-eol))))
  387. (re-search-forward "\" ") (org-babel-execute-maybe)
  388. (should (string= (concat test-line " end")
  389. (buffer-substring-no-properties
  390. (point-at-bol) (point-at-eol))))
  391. (forward-char 2)
  392. (should-error (org-ctrl-c-ctrl-c)))))
  393. (ert-deftest test-org-babel/inline-src_blk-results-raw ()
  394. (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
  395. (org-test-with-temp-text test-line
  396. (org-babel-execute-maybe)
  397. (should (string= (concat test-line " x")
  398. (buffer-string)))))
  399. (let ((test-line (concat " Some text prior to block "
  400. "src_emacs-lisp[ :results raw ]{ \"the\" }")))
  401. (org-test-with-temp-text (concat test-line " end")
  402. (re-search-forward "src_") (org-babel-execute-maybe)
  403. (should (string= (concat test-line " the end")
  404. (buffer-substring-no-properties
  405. (point-at-bol) (point-at-eol))))
  406. (re-search-forward "\" ") (org-babel-execute-maybe)
  407. (should (string= (concat test-line " the the end")
  408. (buffer-substring-no-properties
  409. (point-at-bol) (point-at-eol))))
  410. (forward-char 2)
  411. (should-error (org-ctrl-c-ctrl-c)))))
  412. (ert-deftest test-org-babel/inline-src_blk-results-file ()
  413. (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
  414. (org-test-with-temp-text
  415. test-line
  416. (org-babel-execute-maybe)
  417. (should (string= (concat test-line " {{{results([[file:~/test-file]])}}}")
  418. (buffer-substring-no-properties
  419. (point-min) (point-max)))))))
  420. (ert-deftest test-org-babel/inline-src_blk-results-scalar ()
  421. (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
  422. (org-babel-inline-result-wrap "=%s="))
  423. (org-test-with-temp-text
  424. test-line
  425. (org-babel-execute-maybe)
  426. (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
  427. (buffer-substring-no-properties
  428. (point-min) (point-max)))))))
  429. (ert-deftest test-org-babel/inline-src_blk-results-verbatim ()
  430. (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  431. (org-babel-inline-result-wrap "=%s="))
  432. (org-test-with-temp-text
  433. test-line
  434. (org-babel-execute-maybe)
  435. (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
  436. (buffer-substring-no-properties
  437. (point-min) (point-max)))))))
  438. (ert-deftest test-org-babel/combining-scalar-and-raw-result-types ()
  439. (org-test-with-temp-text-in-file "
  440. #+begin_src sh :results scalar
  441. echo \"[[file:./cv.cls]]\"
  442. #+end_src
  443. #+name:
  444. : [[file:./cv.cls]]
  445. #+begin_src sh :results raw scalar
  446. echo \"[[file:./cv.cls]]\"
  447. #+end_src
  448. "
  449. (flet ((next-result ()
  450. (org-babel-next-src-block)
  451. (org-babel-execute-src-block)
  452. (goto-char (org-babel-where-is-src-block-result))
  453. (forward-line 1)))
  454. (goto-char (point-min))
  455. (next-result)
  456. (should (org-babel-in-example-or-verbatim))
  457. (next-result)
  458. (should (not (org-babel-in-example-or-verbatim))))))
  459. (ert-deftest test-org-babel/no-defaut-value-for-var ()
  460. "Test that the absence of a default value for a variable DOES THROW
  461. a proper error."
  462. (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
  463. (org-babel-next-src-block)
  464. (let ((err
  465. (should-error (org-babel-execute-src-block) :type 'error)))
  466. (should
  467. (equal
  468. '(error
  469. "Variable \"x\" must be assigned a default value")
  470. err)))))
  471. (ert-deftest test-org-babel/just-one-results-block ()
  472. "Test that evaluating two times the same code block does not result in a
  473. duplicate results block."
  474. (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
  475. (org-babel-execute-src-block)
  476. (org-babel-execute-src-block) ; second code block execution
  477. (should (search-forward "Hello")) ; the string inside the source code block
  478. (should (search-forward "Hello")) ; the same string in the results block
  479. (should-error (search-forward "Hello"))))
  480. (ert-deftest test-org-babel/nested-code-block ()
  481. "Test nested code blocks inside code blocks don't cause problems."
  482. (should
  483. (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
  484. (org-test-with-temp-text "#+begin_src org :results silent
  485. ,#+begin_src emacs-lisp
  486. , 'foo
  487. ,#+end_src
  488. #+end_src"
  489. (let ((org-edit-src-content-indentation 2)
  490. (org-src-preserve-indentation nil))
  491. (org-babel-execute-src-block))))))
  492. (ert-deftest test-org-babel/partial-nested-code-block ()
  493. "Test nested code blocks inside code blocks don't cause problems."
  494. (org-test-with-temp-text "#+begin_src org :results silent
  495. ,#+begin_src emacs-lisp
  496. #+end_src"
  497. (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
  498. (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
  499. (org-test-with-temp-text "#+NAME: foo
  500. #+BEGIN_SRC emacs-lisp
  501. 'foo
  502. #+END_SRC\n"
  503. (org-babel-next-src-block 1)
  504. (should (eq 'foo (org-babel-execute-src-block)))
  505. (goto-char (point-min))
  506. (org-babel-next-src-block 1)
  507. (should (looking-at org-babel-src-block-regexp))))
  508. (ert-deftest test-ob/catches-all-references ()
  509. (org-test-with-temp-text "
  510. #+NAME: literal-example
  511. #+BEGIN_EXAMPLE
  512. A literal example
  513. on two lines
  514. #+END_EXAMPLE
  515. #+NAME: read-literal-example
  516. #+BEGIN_SRC emacs-lisp :var x=literal-example
  517. (concatenate 'string x \" for me.\")
  518. #+END_SRC"
  519. (org-babel-next-src-block 1)
  520. (should (string= (org-babel-execute-src-block)
  521. "A literal example\non two lines\n for me."))))
  522. (ert-deftest test-ob/ignore-reference-in-commented-headings ()
  523. (should
  524. (= 2
  525. (org-test-with-temp-text
  526. "
  527. * COMMENT H1
  528. #+NAME: n
  529. : 1
  530. * H2
  531. #+NAME: n
  532. : 2
  533. * Code
  534. <point>#+BEGIN_SRC emacs-lisp :var x=n
  535. x
  536. #+END_SRC"
  537. (org-babel-execute-src-block)))))
  538. (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
  539. (org-test-with-temp-text "
  540. #+tblname: base_plus
  541. | 1 |
  542. | 2 |
  543. #+tblname: base
  544. | 3 |
  545. | 4 |
  546. #+begin_src emacs-lisp :var x=base
  547. x
  548. #+end_src"
  549. (org-babel-next-src-block 1)
  550. (should (equal (org-babel-execute-src-block) '((3) (4))))))
  551. (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
  552. (org-test-with-temp-text "
  553. #+name: base_plus
  554. #+begin_src emacs-lisp
  555. 'bar
  556. #+end_src
  557. #+name: base
  558. #+begin_src emacs-lisp
  559. 'foo
  560. #+end_src
  561. #+begin_src emacs-lisp :var x=base
  562. x
  563. #+end_src"
  564. (org-babel-next-src-block 3)
  565. (should (equal (org-babel-execute-src-block) "foo"))))
  566. (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
  567. (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
  568. (+ a b c d)
  569. #+end_src
  570. "
  571. (should (= 10 (org-babel-execute-src-block)))))
  572. (ert-deftest test-ob/org-babel-update-intermediate ()
  573. (org-test-with-temp-text "#+name: foo
  574. #+begin_src emacs-lisp
  575. 2
  576. #+end_src
  577. #+results: foo
  578. : 4
  579. #+begin_src emacs-lisp :var it=foo
  580. (+ it 1)
  581. #+end_src"
  582. (let ((org-babel-update-intermediate nil))
  583. (goto-char (point-min))
  584. (org-babel-next-src-block 2)
  585. (should (= 3 (org-babel-execute-src-block)))
  586. (goto-char (point-min))
  587. (forward-line 6)
  588. (should (looking-at ": 4")))
  589. (let ((org-babel-update-intermediate t))
  590. (goto-char (point-min))
  591. (org-babel-next-src-block 2)
  592. (should (= 3 (org-babel-execute-src-block)))
  593. (goto-char (point-min))
  594. (forward-line 6)
  595. (should (looking-at ": 2")))))
  596. (ert-deftest test-ob/eval-header-argument ()
  597. (flet ((check-eval (eval runp)
  598. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  599. (setq foo :evald)
  600. #+end_src" eval)
  601. (let ((foo :not-run))
  602. (if runp
  603. (progn (should (org-babel-execute-src-block))
  604. (should (eq foo :evald)))
  605. (progn (should-not (org-babel-execute-src-block))
  606. (should-not (eq foo :evald))))))))
  607. (check-eval "never" nil)
  608. (check-eval "no" nil)
  609. (check-eval "never-export" t)
  610. (check-eval "no-export" t)
  611. (let ((org-babel-exp-reference-buffer (current-buffer)))
  612. (check-eval "never" nil)
  613. (check-eval "no" nil)
  614. (check-eval "never-export" nil)
  615. (check-eval "no-export" nil))))
  616. (ert-deftest test-ob/noweb-expansion-1 ()
  617. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  618. <<foo>>
  619. #+end_src
  620. #+name: foo
  621. #+begin_src sh
  622. bar
  623. #+end_src"
  624. (should (string= (org-babel-expand-noweb-references) "bar"))))
  625. (ert-deftest test-ob/noweb-expansion-2 ()
  626. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  627. <<foo>>
  628. #+end_src
  629. #+name: foo
  630. #+begin_src sh :noweb-sep \"\"
  631. bar
  632. #+end_src
  633. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  634. baz
  635. #+end_src"
  636. (should (string= (org-babel-expand-noweb-references) "barbaz"))))
  637. (ert-deftest test-ob/splitting-variable-lists-in-references ()
  638. (org-test-with-temp-text ""
  639. (should (= 1 (length (org-babel-ref-split-args
  640. "a=\"this, no work\""))))
  641. (should (= 2 (length (org-babel-ref-split-args
  642. "a=\"this, no work\", b=1"))))))
  643. (ert-deftest test-ob/org-babel-balanced-split ()
  644. (should (equal
  645. '(":a 1" "b [2 3]" "c (4 :d (5 6))")
  646. (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
  647. '((32 9) . 58)))))
  648. (ert-deftest test-ob/commented-last-block-line-no-var ()
  649. (org-test-with-temp-text-in-file "
  650. #+begin_src emacs-lisp
  651. ;;
  652. #+end_src"
  653. (org-babel-next-src-block)
  654. (org-babel-execute-maybe)
  655. (should (re-search-forward "\\#\\+results:" nil t))
  656. (forward-line)
  657. (should
  658. (string=
  659. ""
  660. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  661. (org-test-with-temp-text-in-file "
  662. #+begin_src emacs-lisp
  663. \"some text\";;
  664. #+end_src"
  665. (org-babel-next-src-block)
  666. (org-babel-execute-maybe)
  667. (should (re-search-forward "\\#\\+results:" nil t))
  668. (forward-line)
  669. (should
  670. (string=
  671. ": some text"
  672. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  673. (ert-deftest test-ob/commented-last-block-line-with-var ()
  674. (org-test-with-temp-text-in-file "
  675. #+begin_src emacs-lisp :var a=1
  676. ;;
  677. #+end_src"
  678. (org-babel-next-src-block)
  679. (org-babel-execute-maybe)
  680. (re-search-forward "\\#\\+results:" nil t)
  681. (forward-line)
  682. (should (string=
  683. ""
  684. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  685. (org-test-with-temp-text-in-file "
  686. #+begin_src emacs-lisp :var a=2
  687. 2;;
  688. #+end_src"
  689. (org-babel-next-src-block)
  690. (org-babel-execute-maybe)
  691. (re-search-forward "\\#\\+results:" nil t)
  692. (forward-line)
  693. (should (string=
  694. ": 2"
  695. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  696. (ert-deftest test-ob/org-babel-insert-result--improper-lists ()
  697. "Test `org-babel-insert-result' with improper lists."
  698. ;; Do not error when output is an improper list.
  699. (should
  700. (org-test-with-temp-text
  701. "
  702. <point>#+BEGIN_SRC emacs-lisp
  703. '((1 . nil) (2 . 3))
  704. #+END_SRC
  705. "
  706. (org-babel-execute-maybe) t)))
  707. (ert-deftest test-ob/remove-inline-result ()
  708. "Test `org-babel-remove-inline-result' honors whitespace."
  709. (let*
  710. ((inline-sb "src_emacs-lisp{(+ 1 2)}")
  711. (inline-res " {{{results(=3=)}}}")
  712. (inline-sb-dot (concat inline-sb "."))
  713. (inline-sb-res-dot (concat inline-sb inline-res ".")))
  714. (org-test-with-temp-text
  715. ;; Insert inline_src_block followed by dot.
  716. inline-sb-dot
  717. ;; Insert result before dot.
  718. (org-babel-execute-maybe)
  719. (should (string= inline-sb-res-dot
  720. (buffer-substring-no-properties
  721. (point-at-bol) (point-at-eol))))
  722. ;; Delete whitespace and result.
  723. (org-babel-remove-inline-result)
  724. (should (string= inline-sb-dot
  725. (buffer-substring-no-properties
  726. (point-at-bol) (point-at-eol))))
  727. ;; Add whitespace and result before dot.
  728. (search-forward inline-sb)
  729. (insert " " inline-res)
  730. (goto-char (point-at-bol))
  731. ;; Remove whitespace and result.
  732. (org-babel-remove-inline-result)
  733. (should (string= inline-sb-dot
  734. (buffer-substring-no-properties
  735. (point-at-bol) (point-at-eol))))
  736. ;; Add whitespace before dot.
  737. (search-forward inline-sb)
  738. (insert " ")
  739. (goto-char (point-at-bol))
  740. ;; Add result before whitespace.
  741. (org-babel-execute-maybe)
  742. ;; Remove result - leave trailing whitespace and dot.
  743. (org-babel-remove-inline-result)
  744. (should (string= (concat inline-sb " .")
  745. (buffer-substring-no-properties
  746. (point-at-bol) (point-at-eol)))))))
  747. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  748. "Test helper function to test `org-babel-remove-result'.
  749. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  750. and the result of execution is verified against RESULT.
  751. The block is actually executed /twice/ to ensure result
  752. replacement happens correctly."
  753. (org-test-with-temp-text
  754. buffer-text
  755. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  756. (should (re-search-forward "\\#\\+results:" nil t))
  757. (forward-line)
  758. (should (string= result
  759. (buffer-substring-no-properties
  760. (point-at-bol)
  761. (- (point-max) 16))))
  762. (org-babel-previous-src-block) (org-babel-remove-result)
  763. (should (string= buffer-text
  764. (buffer-substring-no-properties
  765. (point-min) (point-max))))))
  766. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  767. "Test `org-babel-remove-result' with default :results."
  768. (mapcar (lambda (language)
  769. (test-ob-verify-result-and-removed-result
  770. "\n"
  771. (concat
  772. "* org-babel-remove-result
  773. #+begin_src " language "
  774. #+end_src
  775. * next heading")))
  776. '("sh" "emacs-lisp")))
  777. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  778. "Test `org-babel-remove-result' with :results list."
  779. (test-ob-verify-result-and-removed-result
  780. "- 1
  781. - 2
  782. - 3
  783. - (quote (4 5))"
  784. "* org-babel-remove-result
  785. #+begin_src emacs-lisp :results list
  786. '(1 2 3 '(4 5))
  787. #+end_src
  788. * next heading"))
  789. (ert-deftest test-ob/org-babel-results-indented-wrap ()
  790. "Ensure that wrapped results are inserted correction when indented.
  791. If not inserted correctly then the second evaluation will fail
  792. trying to find the :END: marker."
  793. (org-test-with-temp-text
  794. "- indented
  795. #+begin_src sh :results file wrap
  796. echo test.txt
  797. #+end_src"
  798. (org-babel-next-src-block 1)
  799. (org-babel-execute-src-block)
  800. (org-babel-execute-src-block)))
  801. (ert-deftest test-ob/file-desc-header-argument ()
  802. "Test that the :file-desc header argument is used."
  803. (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  804. \"foo\"
  805. #+end_src
  806. #+begin_src emacs-lisp :results file :file-desc
  807. \"foo\"
  808. #+end_src"
  809. (org-babel-execute-src-block)
  810. (org-babel-next-src-block 1)
  811. (org-babel-execute-src-block)
  812. (goto-char (point-min))
  813. (should (search-forward "[[file:foo][bar]]" nil t))
  814. (should (search-forward "[[file:foo][foo]]" nil t))))
  815. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  816. "Test `org-babel-remove-result' with :results pp."
  817. (test-ob-verify-result-and-removed-result
  818. ": \"I /am/ working!\""
  819. "* org-babel-remove-result
  820. #+begin_src emacs-lisp :results pp
  821. \"I /am/ working!\")
  822. #+end_src
  823. * next heading"))
  824. (ert-deftest test-org-babel/inline-src_blk-preceded-punct-preceded-by-point ()
  825. (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  826. (org-babel-inline-result-wrap "=%s="))
  827. (org-test-with-temp-text
  828. test-line
  829. (forward-char 1)
  830. (org-babel-execute-maybe)
  831. (should (re-search-forward "=\"x\"=" nil t))
  832. (forward-line))))
  833. (ert-deftest test-ob/commented-last-block-line-with-var ()
  834. (org-test-with-temp-text-in-file "
  835. #+begin_src emacs-lisp :var a=1
  836. ;;
  837. #+end_src"
  838. (org-babel-next-src-block)
  839. (org-babel-execute-maybe)
  840. (re-search-forward "\\#\\+results:" nil t)
  841. (forward-line)
  842. (should (string=
  843. ""
  844. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  845. (org-test-with-temp-text-in-file "
  846. #+begin_src emacs-lisp :var a=2
  847. 2;;
  848. #+end_src"
  849. (org-babel-next-src-block)
  850. (org-babel-execute-maybe)
  851. (re-search-forward "\\#\\+results:" nil t)
  852. (forward-line)
  853. (should (string=
  854. ": 2"
  855. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  856. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  857. "Test helper function to test `org-babel-remove-result'.
  858. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  859. and the result of execution is verified against RESULT.
  860. The block is actually executed /twice/ to ensure result
  861. replacement happens correctly."
  862. (org-test-with-temp-text
  863. buffer-text
  864. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  865. (should (re-search-forward "\\#\\+results:" nil t))
  866. (forward-line)
  867. (should (string= result
  868. (buffer-substring-no-properties
  869. (point-at-bol)
  870. (- (point-max) 16))))
  871. (org-babel-previous-src-block) (org-babel-remove-result)
  872. (should (string= buffer-text
  873. (buffer-substring-no-properties
  874. (point-min) (point-max))))))
  875. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  876. "Test `org-babel-remove-result' with default :results."
  877. (mapcar (lambda (language)
  878. (test-ob-verify-result-and-removed-result
  879. "\n"
  880. (concat
  881. "* org-babel-remove-result
  882. #+begin_src " language "
  883. #+end_src
  884. * next heading")))
  885. '("sh" "emacs-lisp")))
  886. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  887. "Test `org-babel-remove-result' with :results list."
  888. (test-ob-verify-result-and-removed-result
  889. "- 1
  890. - 2
  891. - 3
  892. - (quote (4 5))"
  893. "* org-babel-remove-result
  894. #+begin_src emacs-lisp :results list
  895. '(1 2 3 '(4 5))
  896. #+end_src
  897. * next heading"))
  898. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  899. "Test `org-babel-remove-result' with :results wrap."
  900. (test-ob-verify-result-and-removed-result
  901. ":RESULTS:
  902. hello there
  903. :END:"
  904. "* org-babel-remove-result
  905. #+begin_src emacs-lisp :results wrap
  906. \"hello there\"
  907. #+end_src
  908. * next heading"))
  909. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  910. "Test `org-babel-remove-result' with :results org."
  911. (test-ob-verify-result-and-removed-result
  912. "#+BEGIN_SRC org
  913. ,* heading
  914. ,** subheading
  915. content
  916. #+END_SRC"
  917. "* org-babel-remove-result
  918. #+begin_src emacs-lisp :results org
  919. \"* heading
  920. ,** subheading
  921. content\"
  922. #+end_src
  923. * next heading"))
  924. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  925. "Test `org-babel-remove-result' with :results html."
  926. (test-ob-verify-result-and-removed-result
  927. "#+BEGIN_EXPORT html
  928. <head><body></body></head>
  929. #+END_EXPORT"
  930. "* org-babel-remove-result
  931. #+begin_src emacs-lisp :results html
  932. \"<head><body></body></head>\"
  933. #+end_src
  934. * next heading"))
  935. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  936. "Test `org-babel-remove-result' with :results latex."
  937. (test-ob-verify-result-and-removed-result
  938. "#+BEGIN_EXPORT latex
  939. Line 1
  940. Line 2
  941. Line 3
  942. #+END_EXPORT"
  943. "* org-babel-remove-result
  944. #+begin_src emacs-lisp :results latex
  945. \"Line 1
  946. Line 2
  947. Line 3\"
  948. #+end_src
  949. * next heading"))
  950. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  951. "Test `org-babel-remove-result' with :results code."
  952. (test-ob-verify-result-and-removed-result
  953. "#+BEGIN_SRC emacs-lisp
  954. \"I am working!\"
  955. #+END_SRC"
  956. "* org-babel-remove-result
  957. #+begin_src emacs-lisp :results code
  958. (message \"I am working!\")
  959. #+end_src
  960. * next heading"))
  961. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  962. "Test `org-babel-remove-result' with :results pp."
  963. (test-ob-verify-result-and-removed-result
  964. ": \"I /am/ working!\""
  965. "* org-babel-remove-result
  966. #+begin_src emacs-lisp :results pp
  967. \"I /am/ working!\")
  968. #+end_src
  969. * next heading"))
  970. (ert-deftest test-ob/results-do-not-replace-code-blocks ()
  971. (org-test-with-temp-text "Block two has a space after the name.
  972. #+name: foo
  973. #+begin_src emacs-lisp
  974. 1
  975. #+end_src
  976. #+name: foo
  977. #+begin_src emacs-lisp
  978. 2
  979. #+end_src
  980. #+name: foo
  981. #+begin_src emacs-lisp
  982. 3
  983. #+end_src
  984. #+RESULTS: foo
  985. : foo
  986. "
  987. (dolist (num '(1 2 3))
  988. ;; execute the block
  989. (goto-char (point-min))
  990. (org-babel-next-src-block num) (org-babel-execute-src-block)
  991. ;; check the results
  992. (goto-char (point-max))
  993. (move-beginning-of-line 0)
  994. (should (looking-at (format ": %d" num))))))
  995. (ert-deftest test-ob/blocks-with-spaces ()
  996. "Test expansion of blocks followed by blank lines."
  997. (should
  998. (equal "#+BEGIN_SRC emacs-lisp
  999. \(+ 1 2)
  1000. #+END_SRC
  1001. #+RESULTS:
  1002. : 3\n\n\n"
  1003. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
  1004. \(+ 1 2)
  1005. #+END_SRC\n\n\n"
  1006. (let ((org-babel-next-src-block "RESULTS"))
  1007. (org-babel-execute-src-block))
  1008. (buffer-string)))))
  1009. (ert-deftest test-ob/results-in-narrowed-buffer ()
  1010. "Test block execution in a narrowed buffer."
  1011. ;; If results don't exist, they should be inserted in visible part
  1012. ;; of the buffer.
  1013. (should
  1014. (equal
  1015. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
  1016. (org-test-with-temp-text
  1017. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1018. (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
  1019. (let ((org-babel-results-keyword "RESULTS"))
  1020. (org-babel-execute-src-block))
  1021. (org-trim (buffer-string)))))
  1022. (should
  1023. (equal
  1024. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
  1025. (org-test-with-temp-text
  1026. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1027. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1028. (let ((org-babel-results-keyword "RESULTS"))
  1029. (org-babel-execute-src-block))
  1030. (org-trim (buffer-string)))))
  1031. ;; Results in visible part of buffer, should be updated here.
  1032. (should
  1033. (equal
  1034. "#+NAME: test
  1035. #+BEGIN_SRC emacs-lisp
  1036. \(+ 1 2)
  1037. #+END_SRC
  1038. #+RESULTS: test
  1039. : 3"
  1040. (org-test-with-temp-text
  1041. "#+NAME: test
  1042. #+BEGIN_SRC emacs-lisp
  1043. \(+ 1 2)
  1044. #+END_SRC
  1045. #+RESULTS: test
  1046. : 4
  1047. Paragraph"
  1048. (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
  1049. (let ((org-babel-results-keyword "RESULTS"))
  1050. (org-babel-execute-src-block))
  1051. (org-trim (buffer-string)))))
  1052. ;; Results in invisible part of buffer, should be updated there.
  1053. (org-test-with-temp-text
  1054. "#+NAME: test
  1055. #+BEGIN_SRC emacs-lisp
  1056. \(+ 1 2)
  1057. #+END_SRC
  1058. #+RESULTS: test
  1059. : 4
  1060. Paragraph"
  1061. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1062. (let ((org-babel-results-keyword "RESULTS"))
  1063. (org-babel-execute-src-block))
  1064. (should-not (re-search-forward "^#\\+RESULTS:" nil t))
  1065. (widen)
  1066. (should (should (re-search-forward "^: 3" nil t)))))
  1067. (ert-deftest test-ob/specific-colnames ()
  1068. "Test passing specific column names."
  1069. (should
  1070. (equal "#+name: input-table
  1071. | id | var1 |
  1072. |----+------|
  1073. | 1 | bar |
  1074. | 2 | baz |
  1075. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1076. echo \"$data\"
  1077. #+end_src
  1078. #+RESULTS:
  1079. | Rev | Author |
  1080. |-----+--------|
  1081. | 1 | bar |
  1082. | 2 | baz |
  1083. "
  1084. (org-test-with-temp-text
  1085. "#+name: input-table
  1086. | id | var1 |
  1087. |----+------|
  1088. | 1 | bar |
  1089. | 2 | baz |
  1090. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1091. echo \"$data\"
  1092. #+end_src
  1093. "
  1094. ;; we should find a code block
  1095. (should (re-search-forward org-babel-src-block-regexp nil t))
  1096. (goto-char (match-beginning 0))
  1097. ;; now that we've located the code block, it may be evaluated
  1098. (let ((org-babel-execute-src-block "RESULTS"))
  1099. (org-babel-execute-src-block))
  1100. (buffer-string)))))
  1101. (ert-deftest test-ob/location-of-header-arg-eval ()
  1102. "Test location of header argument evaluation."
  1103. (org-test-with-temp-text "
  1104. #+name: top-block
  1105. #+begin_src emacs-lisp :var pt=(point)
  1106. pt
  1107. #+end_src
  1108. #+name: bottom-block
  1109. #+begin_src emacs-lisp :var pt=top-block()
  1110. pt
  1111. #+end_src
  1112. "
  1113. ;; the value of the second block should be greater than the first
  1114. (should
  1115. (< (progn (re-search-forward org-babel-src-block-regexp nil t)
  1116. (goto-char (match-beginning 0))
  1117. (prog1 (save-match-data (org-babel-execute-src-block))
  1118. (goto-char (match-end 0))))
  1119. (progn (re-search-forward org-babel-src-block-regexp nil t)
  1120. (goto-char (match-beginning 0))
  1121. (org-babel-execute-src-block))))))
  1122. (ert-deftest test-ob/preserve-results-indentation ()
  1123. "Preserve indentation when executing a src block."
  1124. (should
  1125. (equal
  1126. '(2 2)
  1127. (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1128. (org-babel-execute-src-block)
  1129. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1130. (list (org-get-indentation)
  1131. (progn (forward-line) (org-get-indentation))))))
  1132. (should
  1133. (equal
  1134. '(2 2)
  1135. (org-test-with-temp-text
  1136. " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1137. (org-babel-execute-src-block)
  1138. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1139. (list (org-get-indentation)
  1140. (progn (forward-line) (org-get-indentation))))))
  1141. ;; Don't get fooled by TAB-based indentation.
  1142. (should
  1143. (equal
  1144. '(6 6)
  1145. (org-test-with-temp-text
  1146. "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
  1147. (setq tab-width 4)
  1148. (org-babel-execute-src-block)
  1149. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1150. (list (org-get-indentation)
  1151. (progn (forward-line) (org-get-indentation)))))))
  1152. (ert-deftest test-ob/safe-header-args ()
  1153. "Detect safe and unsafe header args."
  1154. (let ((safe-args '((:cache . "foo")
  1155. (:results . "output")
  1156. (:eval . "never")
  1157. (:eval . "query")))
  1158. (unsafe-args '((:eval . "yes")
  1159. (:results . "output file")
  1160. (:foo . "bar")))
  1161. (malformed-args '((bar . "foo")
  1162. ("foo" . "bar")
  1163. :foo))
  1164. (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
  1165. (dolist (arg safe-args)
  1166. (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
  1167. (dolist (arg unsafe-args)
  1168. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1169. (dolist (arg malformed-args)
  1170. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1171. (should (not (funcall safe-p (append safe-args unsafe-args))))))
  1172. (ert-deftest test-ob/noweb-expansions-in-cache ()
  1173. "Ensure that noweb expansions are expanded before caching."
  1174. (let ((noweb-expansions-in-cache-var 0))
  1175. (org-test-with-temp-text "
  1176. #+name: foo
  1177. #+begin_src emacs-lisp
  1178. \"I said\"
  1179. #+end_src
  1180. #+name: bar
  1181. #+begin_src emacs-lisp :noweb yes :cache yes
  1182. (setq noweb-expansions-in-cache-var
  1183. (+ 1 noweb-expansions-in-cache-var))
  1184. (concat <<foo>> \" check noweb expansions\")
  1185. #+end_src
  1186. "
  1187. ;; run the second block to create the cache
  1188. (goto-char (point-min))
  1189. (re-search-forward (regexp-quote "#+name: bar"))
  1190. (should (string= "I said check noweb expansions"
  1191. (org-babel-execute-src-block)))
  1192. (should (= noweb-expansions-in-cache-var 1))
  1193. ;; change the value of the first block
  1194. (goto-char (point-min))
  1195. (re-search-forward (regexp-quote "said"))
  1196. (goto-char (match-beginning 0))
  1197. (insert "haven't ")
  1198. (re-search-forward (regexp-quote "#+name: bar"))
  1199. (should (string= "I haven't said check noweb expansions"
  1200. (org-babel-execute-src-block)))
  1201. (should (= noweb-expansions-in-cache-var 2)))))
  1202. (ert-deftest test-org-babel/file-ext-and-output-dir ()
  1203. (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
  1204. (org-babel-next-src-block)
  1205. (should (equal "file-ext-basic.txt"
  1206. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1207. (org-babel-next-src-block)
  1208. (should (equal "foo/file-ext-dir-relative.txt"
  1209. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1210. (org-babel-next-src-block)
  1211. (should (equal "foo/file-ext-dir-relative-slash.txt"
  1212. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1213. (org-babel-next-src-block)
  1214. (should (equal "/tmp/file-ext-dir-absolute.txt"
  1215. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1216. (org-babel-next-src-block)
  1217. (should (equal "foo.bar"
  1218. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1219. (org-babel-next-src-block)
  1220. (should (equal "xxx/foo.bar"
  1221. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1222. ))
  1223. (ert-deftest test-org-babel/script-escape ()
  1224. ;; Delimited lists of numbers
  1225. (should (equal '(1 2 3)
  1226. (org-babel-script-escape "[1 2 3]")))
  1227. (should (equal '(1 2 3)
  1228. (org-babel-script-escape "{1 2 3}")))
  1229. (should (equal '(1 2 3)
  1230. (org-babel-script-escape "(1 2 3)")))
  1231. ;; Delimited lists of double-quoted strings
  1232. (should (equal '("foo" "bar")
  1233. (org-babel-script-escape "(\"foo\" \"bar\")")))
  1234. (should (equal '("foo" "bar")
  1235. (org-babel-script-escape "[\"foo\" \"bar\"]")))
  1236. (should (equal '("foo" "bar")
  1237. (org-babel-script-escape "{\"foo\" \"bar\"}")))
  1238. ;; ... with commas
  1239. (should (equal '("foo" "bar")
  1240. (org-babel-script-escape "(\"foo\", \"bar\")")))
  1241. (should (equal '("foo" "bar")
  1242. (org-babel-script-escape "[\"foo\", \"bar\"]")))
  1243. (should (equal '("foo" "bar")
  1244. (org-babel-script-escape "{\"foo\", \"bar\"}")))
  1245. ;; Delimited lists of single-quoted strings
  1246. (should (equal '("foo" "bar")
  1247. (org-babel-script-escape "('foo' 'bar')")))
  1248. (should (equal '("foo" "bar")
  1249. (org-babel-script-escape "['foo' 'bar']")))
  1250. (should (equal '("foo" "bar")
  1251. (org-babel-script-escape "{'foo' 'bar'}")))
  1252. ;; ... with commas
  1253. (should (equal '("foo" "bar")
  1254. (org-babel-script-escape "('foo', 'bar')")))
  1255. (should (equal '("foo" "bar")
  1256. (org-babel-script-escape "['foo', 'bar']")))
  1257. (should (equal '("foo" "bar")
  1258. (org-babel-script-escape "{'foo', 'bar'}")))
  1259. ;; Single quoted strings
  1260. (should (equal "foo"
  1261. (org-babel-script-escape "'foo'")))
  1262. ;; ... with internal double quote
  1263. (should (equal "foo\"bar"
  1264. (org-babel-script-escape "'foo\"bar'")))
  1265. ;; ... with internal backslash
  1266. (should (equal "foo\\bar"
  1267. (org-babel-script-escape "'foo\\bar'")))
  1268. ;; ... with internal escaped backslash
  1269. (should (equal "foo\\bar"
  1270. (org-babel-script-escape "'foo\\\\bar'")))
  1271. ;; ... with internal backslash-double quote
  1272. (should (equal "foo\\\"bar"
  1273. (org-babel-script-escape "'foo\\\"bar'")))
  1274. ;; ... with internal escaped backslash-double quote
  1275. (should (equal "foo\\\"bar"
  1276. (org-babel-script-escape "'foo\\\\\"bar'")))
  1277. ;; ... with internal escaped single quote
  1278. (should (equal "foo'bar"
  1279. (org-babel-script-escape "'foo\\'bar'")))
  1280. ;; ... with internal escaped backslash-escaped single quote
  1281. (should (equal "foo\\'bar"
  1282. (org-babel-script-escape "'foo\\\\\\'bar'")))
  1283. ;; Double quoted strings
  1284. (should (equal "foo"
  1285. (org-babel-script-escape "\"foo\"")))
  1286. ;; ... with internal single quote
  1287. (should (equal "foo'bar"
  1288. (org-babel-script-escape "\"foo'bar\"")))
  1289. ;; ... with internal backslash
  1290. (should (equal "foo\\bar"
  1291. (org-babel-script-escape "\"foo\\bar\"")))
  1292. ;; ... with internal escaped backslash
  1293. (should (equal "foo\\bar"
  1294. (org-babel-script-escape "\"foo\\\\bar\"")))
  1295. ;; ... with internal backslash-single quote
  1296. (should (equal "foo\\'bar"
  1297. (org-babel-script-escape "\"foo\\'bar\"")))
  1298. ;; ... with internal escaped backslash-single quote
  1299. (should (equal "foo\\'bar"
  1300. (org-babel-script-escape "\"foo\\\\'bar\"")))
  1301. ;; ... with internal escaped double quote
  1302. (should (equal "foo\"bar"
  1303. (org-babel-script-escape "\"foo\\\"bar\"")))
  1304. ;; ... with internal escaped backslash-escaped double quote
  1305. (should (equal "foo\\\"bar"
  1306. (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
  1307. (ert-deftest ob/process-params-no-duplicates ()
  1308. (should (equal (org-babel-process-params '((:colname-names . 1)
  1309. (:rowname-names . 1)
  1310. (:result-params . 1)
  1311. (:result-type . 1)
  1312. (:var . "\"foo\"")))
  1313. '((:var)
  1314. (:colname-names . 1)
  1315. (:rowname-names . 1)
  1316. (:result-params . 1)
  1317. (:result-type . value)))))
  1318. (defun org-test-babel-confirm-evaluate (eval-value)
  1319. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  1320. nil
  1321. #+end_src" eval-value)
  1322. (goto-char (point-min))
  1323. (let ((info (org-babel-get-src-block-info)))
  1324. (org-babel-check-confirm-evaluate info))))
  1325. (ert-deftest ob/check-eval ()
  1326. (let ((org-confirm-babel-evaluate t))
  1327. ;; Non-export tests
  1328. (dolist (pair '(("no" . nil)
  1329. ("never" . nil)
  1330. ("query" . query)
  1331. ("yes" . query)))
  1332. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1333. ;; Export tests
  1334. (let ((org-babel-exp-reference-buffer t))
  1335. (dolist (pair '(("no" . nil)
  1336. ("never" . nil)
  1337. ("query" . query)
  1338. ("yes" . query)
  1339. ("never-export" . nil)
  1340. ("no-export" . nil)
  1341. ("query-export" . query)))
  1342. (message (car pair))
  1343. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))))
  1344. (let ((org-confirm-babel-evaluate nil))
  1345. ;; Non-export tests
  1346. (dolist (pair '(("no" . nil)
  1347. ("never" . nil)
  1348. ("query" . query)
  1349. ("yes" . t)))
  1350. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1351. ;; Export tests
  1352. (let ((org-babel-exp-reference-buffer t))
  1353. (dolist (pair '(("no" . nil)
  1354. ("never" . nil)
  1355. ("query" . query)
  1356. ("yes" . t)
  1357. ("never-export" . nil)
  1358. ("no-export" . nil)
  1359. ("query-export" . query)))
  1360. (message (car pair))
  1361. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair)))))))
  1362. (provide 'test-ob)
  1363. ;;; test-ob ends here