test-ob.el 47 KB

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