test-ob.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. ;;; test-ob.el --- tests for ob.el
  2. ;; Copyright (c) 2010, 2011 Eric Schulte
  3. ;; Authors: Eric Schulte, Martyn Jago
  4. ;; Released under the GNU General Public License version 3
  5. ;; see: http://www.gnu.org/licenses/gpl-3.0.html
  6. (let ((load-path (cons (expand-file-name
  7. ".." (file-name-directory
  8. (or load-file-name buffer-file-name)))
  9. load-path)))
  10. (require 'org-test)
  11. (require 'org-test-ob-consts))
  12. (ert-deftest test-org-babel/multi-line-header-regexp ()
  13. (should(equal "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
  14. org-babel-multi-line-header-regexp))
  15. ;;TODO can be optimised - and what about blah4 blah5 blah6?
  16. (should (string-match
  17. org-babel-multi-line-header-regexp
  18. " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))
  19. (should
  20. (equal
  21. "blah1 blah2 blah3 \t"
  22. (match-string
  23. 1
  24. " \t #+headers: blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n")))
  25. ;;TODO Check - should this fail?
  26. (should
  27. (not (org-test-string-exact-match
  28. org-babel-multi-line-header-regexp
  29. " \t #+headers : blah1 blah2 blah3 \t\n\t\n blah4 blah5 blah6 \n"))))
  30. (ert-deftest test-org-babel/src-block-regexp ()
  31. (let ((test-block
  32. (concat
  33. "#+begin_src language -n-r-a-b -c :argument-1 yes :argument-2 no\n"
  34. "echo this is a test\n"
  35. "echo Currently in ' $PWD\n"
  36. "#+end_src"))
  37. (language "language")
  38. (flags "-n-r-a-b -c ")
  39. (arguments ":argument-1 yes :argument-2 no")
  40. (body "echo this is a test\necho Currently in ' $PWD\n"))
  41. (should (string-match org-babel-src-block-regexp test-block))
  42. (should (string-match org-babel-src-block-regexp (upcase test-block)))
  43. (should (equal language (match-string 2 test-block)))
  44. ;;TODO Consider refactoring
  45. (should (equal flags (match-string 3 test-block)))
  46. (should (equal arguments (match-string 4 test-block)))
  47. (should (equal body (match-string 5 test-block)))
  48. ;;no switches
  49. (should (org-test-string-exact-match
  50. org-babel-src-block-regexp
  51. (replace-regexp-in-string flags "" test-block)))
  52. ;;no header arguments
  53. (should (org-test-string-exact-match
  54. org-babel-src-block-regexp
  55. (replace-regexp-in-string arguments "" test-block)))
  56. ;; should be valid with no body
  57. (should (org-test-string-exact-match
  58. org-babel-src-block-regexp
  59. (replace-regexp-in-string body "" test-block)))))
  60. (ert-deftest test-org-babel/get-header ()
  61. (should (not (org-babel-get-header
  62. org-babel-default-header-args :doesnt-exist)))
  63. (should(equal '((:session . "none"))
  64. (org-babel-get-header
  65. org-babel-default-header-args :session)))
  66. (should(equal '((:session . "none"))
  67. (org-babel-get-header
  68. org-babel-default-header-args :session nil)))
  69. (should (not (org-babel-get-header
  70. org-babel-default-header-args :SESSION)))
  71. (should (equal '((:tangle . "no"))
  72. (org-babel-get-header
  73. org-babel-default-header-args :tangle)))
  74. ;; with OTHERS option
  75. (should (equal org-babel-default-header-args
  76. (org-babel-get-header
  77. org-babel-default-header-args :doesnt-exist 'others)))
  78. (should (equal org-babel-default-header-args
  79. (org-babel-get-header
  80. org-babel-default-header-args nil 'others)))
  81. (should (null
  82. (assoc :noweb
  83. (org-babel-get-header
  84. org-babel-default-header-args :noweb 'others)))))
  85. (ert-deftest test-org-babel/default-inline-header-args ()
  86. (should(equal
  87. '((:session . "none") (:results . "replace") (:exports . "results"))
  88. org-babel-default-inline-header-args)))
  89. ;;; ob-get-src-block-info
  90. (ert-deftest test-org-babel/get-src-block-info-language ()
  91. (org-test-at-marker nil org-test-file-ob-anchor
  92. (let ((info (org-babel-get-src-block-info)))
  93. (should (string= "emacs-lisp" (nth 0 info))))))
  94. (ert-deftest test-org-babel/get-src-block-info-body ()
  95. (org-test-at-marker nil org-test-file-ob-anchor
  96. (let ((info (org-babel-get-src-block-info)))
  97. (should (string-match (regexp-quote org-test-file-ob-anchor)
  98. (nth 1 info))))))
  99. (ert-deftest test-org-babel/get-src-block-info-tangle ()
  100. (org-test-at-marker nil org-test-file-ob-anchor
  101. (let ((info (org-babel-get-src-block-info)))
  102. (should (string= "no" (cdr (assoc :tangle (nth 2 info))))))))
  103. (ert-deftest test-org-babel/elisp-in-header-arguments ()
  104. "Test execution of elisp forms in header arguments."
  105. ;; at the babel.org:elisp-forms-in-header-arguments header
  106. (org-test-at-id "22d67284-bf14-4cdc-8319-f4bd876829d7"
  107. (org-babel-next-src-block)
  108. (let ((info (org-babel-get-src-block-info)))
  109. (should (= 4 (org-babel-execute-src-block))))))
  110. (ert-deftest test-org-babel/simple-named-code-block ()
  111. "Test that simple named code blocks can be evaluated."
  112. (org-test-with-temp-text-in-file "
  113. #+name: i-have-a-name
  114. #+begin_src emacs-lisp
  115. 42
  116. #+end_src"
  117. (progn
  118. (org-babel-next-src-block 1)
  119. (should (= 42 (org-babel-execute-src-block))))))
  120. (ert-deftest test-org-babel/simple-variable-resolution ()
  121. "Test that simple variable resolution is working."
  122. (org-test-at-id "f68821bc-7f49-4389-85b5-914791ee3718"
  123. (org-babel-next-src-block 2)
  124. (should (= 4 (org-babel-execute-src-block)))))
  125. (ert-deftest test-org-babel/multi-line-header-arguments ()
  126. "Test that multi-line header arguments and can be read."
  127. (org-test-at-id "b77c8857-6c76-4ea9-8a61-ddc2648d96c4"
  128. (org-babel-next-src-block)
  129. (let ((results (org-babel-execute-src-block)))
  130. (should(equal 'a (cadr (assoc 1 results))))
  131. (should(equal 'd (cadr (assoc 4 results)))))))
  132. (ert-deftest test-org-babel/parse-header-args ()
  133. (org-test-at-id "7eb0dc6e-1c53-4275-88b3-b22f3113b9c3"
  134. (org-babel-next-src-block)
  135. (let* ((info (org-babel-get-src-block-info))
  136. (params (nth 2 info)))
  137. (message "%S" params)
  138. (should(equal "example-lang" (nth 0 info)))
  139. (should(string= "the body" (org-babel-trim (nth 1 info))))
  140. (should-not (member '(:session\ \ \ \ ) params))
  141. (should(equal '(:session) (assoc :session params)))
  142. (should(equal '(:result-type . output) (assoc :result-type params)))
  143. (should(equal '(num . 9) (cdr (assoc :var params)))))))
  144. (ert-deftest test-org-babel/parse-header-args2 ()
  145. (org-test-at-id "2409e8ba-7b5f-4678-8888-e48aa02d8cb4"
  146. (should (string-match (regexp-quote "this is simple")
  147. (org-babel-ref-resolve "simple-subtree")))
  148. (org-babel-next-src-block)
  149. (should (= 14 (org-babel-execute-src-block)))))
  150. (ert-deftest test-org-babel/inline-src-blocks ()
  151. (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
  152. (macrolet ((at-next (&rest body)
  153. `(progn
  154. (move-end-of-line 1)
  155. (re-search-forward org-babel-inline-src-block-regexp nil t)
  156. (goto-char (match-beginning 1))
  157. (save-match-data ,@body))))
  158. (at-next (should (equal 1 (org-babel-execute-src-block))))
  159. (at-next (should (equal 2 (org-babel-execute-src-block))))
  160. (at-next (should (equal 3 (org-babel-execute-src-block)))))))
  161. (ert-deftest test-org-babel/org-babel-get-inline-src-block-matches ()
  162. (org-test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74"
  163. (let ((test-point (point)))
  164. (should (fboundp 'org-babel-get-inline-src-block-matches))
  165. (should (re-search-forward "src_" nil t)) ;; 1
  166. (should (org-babel-get-inline-src-block-matches))
  167. (should (re-search-forward "}" nil (point-at-bol))) ;; 1
  168. (should-not (org-babel-get-inline-src-block-matches))
  169. (should (re-search-forward "in" nil t)) ;; 2
  170. (should-not (org-babel-get-inline-src-block-matches))
  171. (should (re-search-forward "echo" nil t)) ;; 2
  172. (should (org-babel-get-inline-src-block-matches))
  173. (should (re-search-forward "blocks" nil t)) ;; 3
  174. (backward-char 8) ;; 3
  175. (should (org-babel-get-inline-src-block-matches))
  176. (forward-char 1) ;;3
  177. (should-not (org-babel-get-inline-src-block-matches))
  178. (should (re-search-forward ":results" nil t)) ;; 4
  179. (should (org-babel-get-inline-src-block-matches))
  180. (end-of-line)
  181. (should-not (org-babel-get-inline-src-block-matches))
  182. )))
  183. (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-1 ()
  184. (let ((test-line "src_sh{echo 1}"))
  185. ;; src_ at bol line 1...
  186. (org-test-with-temp-text
  187. test-line
  188. (goto-char (point-min)) (org-ctrl-c-ctrl-c)
  189. (should (string=
  190. (concat test-line " =1=")
  191. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  192. (forward-char) (org-ctrl-c-ctrl-c)
  193. (should (string=
  194. (concat test-line " =1= =1=")
  195. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  196. (re-search-forward "1}")
  197. (should-error (org-ctrl-c-ctrl-c))
  198. (backward-char) ;; last char of block body
  199. (org-ctrl-c-ctrl-c)
  200. (should (string=
  201. (concat test-line " =1= =1= =1=")
  202. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  203. ;; src_ follows space line 1...
  204. (let ((test-line " src_emacs-lisp{ 1 }"))
  205. (org-test-with-temp-text
  206. test-line
  207. (should-error (org-ctrl-c-ctrl-c))
  208. (forward-char) (org-ctrl-c-ctrl-c)
  209. (should (string=
  210. (concat test-line " =1=")
  211. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  212. (re-search-forward "{ 1 ") (org-ctrl-c-ctrl-c)
  213. (should (string=
  214. (concat test-line " =1= =1=")
  215. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  216. (forward-char)
  217. (should-error (org-ctrl-c-ctrl-c)))))
  218. (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-2 ()
  219. ;; src_ at bol line 2...
  220. (let ((test-line " src_emacs-lisp{ \"x\" }"))
  221. (org-test-with-temp-text
  222. (concat "\n" test-line)
  223. (should-error (org-ctrl-c-ctrl-c))
  224. (goto-char (point-min))
  225. (should-error (org-ctrl-c-ctrl-c))
  226. (forward-line)
  227. (should-error (org-ctrl-c-ctrl-c))
  228. (forward-char) (org-ctrl-c-ctrl-c)
  229. (should (string=
  230. (concat test-line " =x=")
  231. (buffer-substring-no-properties
  232. (point-at-bol) (point-at-eol))))))
  233. (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }"))
  234. (org-test-with-temp-text
  235. test-line
  236. (goto-char (point-max))
  237. (insert (concat "\n" test-line " end"))
  238. (re-search-backward "src") (org-ctrl-c-ctrl-c)
  239. (should (string=
  240. (concat test-line " =y= end")
  241. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  242. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  243. (should (string=
  244. (concat test-line " =y= =y= end")
  245. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  246. (forward-char)
  247. (should-error (org-ctrl-c-ctrl-c))))))
  248. (ert-deftest test-org-babel/inline-src_blk-manual-results-replace ()
  249. (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }"))
  250. (org-test-with-temp-text
  251. (concat "\n" test-line)
  252. (should-error (org-ctrl-c-ctrl-c))
  253. (goto-char (point-max))
  254. (should-error (org-ctrl-c-ctrl-c))
  255. (beginning-of-line)
  256. (should-error (org-ctrl-c-ctrl-c))
  257. (forward-char) (org-ctrl-c-ctrl-c)
  258. (should (string=
  259. (concat test-line " =x=")
  260. (buffer-substring-no-properties
  261. (point-at-bol) (point-at-eol))))))
  262. (let ((test-line (concat " Some text prior to block "
  263. "src_emacs-lisp[:results replace]{ \"y\" }")))
  264. (org-test-with-temp-text test-line
  265. (goto-char (point-max))
  266. (insert (concat "\n" test-line " end"))
  267. (re-search-backward "src") (org-ctrl-c-ctrl-c)
  268. (should (string=
  269. (concat test-line " =y= end")
  270. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  271. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  272. (should (string=
  273. (concat test-line " =y= =y= end")
  274. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  275. (forward-char)
  276. (should-error (org-ctrl-c-ctrl-c)))))
  277. (ert-deftest test-org-babel/inline-src_blk-results-silent ()
  278. (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
  279. (org-test-with-temp-text test-line
  280. (org-ctrl-c-ctrl-c)
  281. (should (string= test-line
  282. (buffer-substring-no-properties
  283. (point-at-bol) (point-at-eol))))
  284. (end-of-buffer)
  285. (should-error (org-ctrl-c-ctrl-c))))
  286. (let ((test-line (concat " Some text prior to block src_emacs-lisp"
  287. "[ :results silent ]{ \"y\" }")))
  288. (org-test-with-temp-text
  289. test-line
  290. (goto-char (point-max))
  291. (insert (concat "\n" test-line " end"))
  292. (re-search-backward "src_") (org-ctrl-c-ctrl-c)
  293. (should (string= (concat test-line " end")
  294. (buffer-substring-no-properties
  295. (point-at-bol) (point-at-eol))))
  296. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  297. (should (string= (concat test-line " end")
  298. (buffer-substring-no-properties
  299. (point-at-bol) (point-at-eol))))
  300. (forward-char)
  301. (should-error (org-ctrl-c-ctrl-c)))))
  302. (ert-deftest test-org-babel/inline-src_blk-results-raw ()
  303. (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
  304. (org-test-with-temp-text test-line
  305. (org-ctrl-c-ctrl-c)
  306. (should (string= (concat test-line " x")
  307. (buffer-string)))))
  308. (let ((test-line (concat " Some text prior to block "
  309. "src_emacs-lisp[ :results raw ]{ \"the\" }")))
  310. (org-test-with-temp-text (concat test-line " end")
  311. (re-search-forward "src_") (org-ctrl-c-ctrl-c)
  312. (should (string= (concat test-line " the end")
  313. (buffer-substring-no-properties
  314. (point-at-bol) (point-at-eol))))
  315. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  316. (should (string= (concat test-line " the the end")
  317. (buffer-substring-no-properties
  318. (point-at-bol) (point-at-eol))))
  319. (forward-char)
  320. (should-error (org-ctrl-c-ctrl-c)))))
  321. (ert-deftest test-org-babel/inline-src_blk-results-file ()
  322. (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
  323. (org-test-with-temp-text
  324. test-line
  325. (org-ctrl-c-ctrl-c)
  326. (should (string= (concat test-line " [[file:~/test-file]]")
  327. (buffer-substring-no-properties
  328. (point-min) (point-max)))))))
  329. (ert-deftest test-org-babel/inline-src_blk-results-scalar ()
  330. (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }"))
  331. (org-test-with-temp-text
  332. test-line
  333. (org-ctrl-c-ctrl-c)
  334. (should (string= (concat test-line " =\"x\"=")
  335. (buffer-substring-no-properties
  336. (point-min) (point-max)))))))
  337. (ert-deftest test-org-babel/inline-src_blk-results-verbatim ()
  338. (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
  339. (org-test-with-temp-text
  340. test-line
  341. (org-ctrl-c-ctrl-c)
  342. (should (string= (concat test-line " =\"x\"=")
  343. (buffer-substring-no-properties
  344. (point-min) (point-max)))))))
  345. (ert-deftest test-org-babel/combining-scalar-and-raw-result-types ()
  346. (flet ((next-result ()
  347. (org-babel-next-src-block)
  348. (org-babel-execute-src-block)
  349. (goto-char (org-babel-where-is-src-block-result))
  350. (forward-line 1)))
  351. (org-test-at-id "a73a2ab6-b8b2-4c0e-ae7f-23ad14eab7bc"
  352. (next-result)
  353. (should (org-babel-in-example-or-verbatim))
  354. (next-result)
  355. (should (not (org-babel-in-example-or-verbatim))))))
  356. (ert-deftest test-org-babel/no-defaut-value-for-var ()
  357. "Test that the absence of a default value for a variable DOES THROW
  358. a proper error."
  359. (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
  360. (org-babel-next-src-block)
  361. (let ((err
  362. (should-error (org-babel-execute-src-block) :type 'error)))
  363. (should
  364. (equal
  365. '(error
  366. "variable \"x\" in block \"carre\" must be assigned a default value")
  367. err)))))
  368. (ert-deftest test-org-babel/just-one-results-block ()
  369. "Test that evaluating two times the same code block does not result in a
  370. duplicate results block."
  371. (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
  372. (org-babel-execute-src-block)
  373. (org-babel-execute-src-block) ; second code block execution
  374. (should (search-forward "Hello")) ; the string inside the source code block
  375. (should (search-forward "Hello")) ; the same string in the results block
  376. (should-error (search-forward "Hello"))))
  377. (ert-deftest test-org-babel/nested-code-block ()
  378. "Test nested code blocks inside code blocks don't cause problems."
  379. (org-test-with-temp-text "#+begin_src org :results silent
  380. ,#+begin_src emacs-lisp
  381. , 'foo
  382. ,#+end_src
  383. #+end_src"
  384. (should (string= (org-babel-execute-src-block)
  385. "#+begin_src emacs-lisp\n 'foo\n#+end_src"))))
  386. (ert-deftest test-org-babel/partial-nested-code-block ()
  387. "Test nested code blocks inside code blocks don't cause problems."
  388. (org-test-with-temp-text "#+begin_src org :results silent
  389. ,#+begin_src emacs-lisp
  390. #+end_src"
  391. (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
  392. (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
  393. (org-test-with-temp-text "#+NAME: foo
  394. #+BEGIN_SRC emacs-lisp
  395. 'foo
  396. #+END_SRC\n"
  397. (org-babel-next-src-block 1)
  398. (should (eq 'foo (org-babel-execute-src-block)))
  399. (goto-char (point-min))
  400. (org-babel-next-src-block 1)
  401. (should (looking-at org-babel-src-block-regexp))))
  402. (ert-deftest test-ob/catches-all-references ()
  403. (org-test-with-temp-text "
  404. #+NAME: literal-example
  405. #+BEGIN_EXAMPLE
  406. A literal example
  407. on two lines
  408. #+END_EXAMPLE
  409. #+NAME: read-literal-example
  410. #+BEGIN_SRC emacs-lisp :var x=literal-example
  411. (concatenate 'string x \" for me.\")
  412. #+END_SRC"
  413. (org-babel-next-src-block 1)
  414. (should (string= (org-babel-execute-src-block)
  415. "A literal example\non two lines for me."))))
  416. (ert-deftest test-ob/resolve-code-blocks-before-data-blocks ()
  417. (org-test-with-temp-text "
  418. #+name: foo
  419. : bar
  420. #+name: foo
  421. #+begin_src emacs-lisp
  422. \"baz\"
  423. #+end_src
  424. #+begin_src emacs-lisp :var foo=foo
  425. foo
  426. #+end_src"
  427. (org-babel-next-src-block 2)
  428. (should (string= (org-babel-execute-src-block) "baz"))))
  429. (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
  430. (org-test-with-temp-text "
  431. #+tblname: base_plus
  432. | 1 |
  433. | 2 |
  434. #+tblname: base
  435. | 3 |
  436. | 4 |
  437. #+begin_src emacs-lisp :var x=base
  438. x
  439. #+end_src"
  440. (org-babel-next-src-block 1)
  441. (should (equal (org-babel-execute-src-block) '((3) (4))))))
  442. (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
  443. (org-test-with-temp-text "
  444. #+name: base_plus
  445. #+begin_src emacs-lisp
  446. 'bar
  447. #+end_src
  448. #+name: base
  449. #+begin_src emacs-lisp
  450. 'foo
  451. #+end_src
  452. #+begin_src emacs-lisp :var x=base
  453. x
  454. #+end_src"
  455. (org-babel-next-src-block 3)
  456. (should (equal (org-babel-execute-src-block) "foo"))))
  457. (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
  458. (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
  459. (+ a b c d)
  460. #+end_src
  461. "
  462. (should (= 10 (org-babel-execute-src-block)))))
  463. (ert-deftest test-ob/org-babel-update-intermediate ()
  464. (org-test-with-temp-text "#+name: foo
  465. #+begin_src emacs-lisp
  466. 2
  467. #+end_src
  468. #+results: foo
  469. : 4
  470. #+begin_src emacs-lisp :var it=foo
  471. (+ it 1)
  472. #+end_src"
  473. (let ((org-babel-update-intermediate nil))
  474. (goto-char (point-min))
  475. (org-babel-next-src-block 2)
  476. (should (= 3 (org-babel-execute-src-block)))
  477. (goto-char (point-min))
  478. (forward-line 6)
  479. (should (looking-at ": 4")))
  480. (let ((org-babel-update-intermediate t))
  481. (goto-char (point-min))
  482. (org-babel-next-src-block 2)
  483. (should (= 3 (org-babel-execute-src-block)))
  484. (goto-char (point-min))
  485. (forward-line 6)
  486. (should (looking-at ": 2")))))
  487. (ert-deftest test-ob/eval-header-argument ()
  488. (flet ((check-eval (eval runp)
  489. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  490. (setq foo :evald)
  491. #+end_src" eval)
  492. (let ((foo :not-run))
  493. (if runp
  494. (progn (should (org-babel-execute-src-block))
  495. (should (eq foo :evald)))
  496. (progn (should-not (org-babel-execute-src-block))
  497. (should-not (eq foo :evald))))))))
  498. (check-eval "never" nil)
  499. (check-eval "no" nil)
  500. (check-eval "never-export" t)
  501. (check-eval "no-export" t)
  502. (let ((org-current-export-file "something"))
  503. (check-eval "never" nil)
  504. (check-eval "no" nil)
  505. (check-eval "never-export" nil)
  506. (check-eval "no-export" nil))))
  507. (ert-deftest test-ob/noweb-expansion ()
  508. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  509. <<foo>>
  510. #+end_src
  511. #+name: foo
  512. #+begin_src sh
  513. bar
  514. #+end_src"
  515. (should (string= (org-babel-expand-noweb-references) "bar")))
  516. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  517. <<foo>>
  518. #+end_src
  519. #+name: foo
  520. #+begin_src sh :noweb-sep \"\"
  521. bar
  522. #+end_src
  523. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  524. baz
  525. #+end_src"
  526. (should (string= (org-babel-expand-noweb-references) "barbaz"))))
  527. (ert-deftest test-ob/splitting-variable-lists-in-references ()
  528. (org-test-with-temp-text ""
  529. (should (= 1 (length (org-babel-ref-split-args
  530. "a=\"this, no work\""))))
  531. (should (= 2 (length (org-babel-ref-split-args
  532. "a=\"this, no work\", b=1"))))))
  533. (ert-deftest test-ob/org-babel-balanced-split ()
  534. (should (equal
  535. '(":a 1" "b [2 3]" "c (4 :d (5 6))")
  536. (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
  537. '((32 9) . 58)))))
  538. (ert-deftest test-ob/commented-last-block-line-no-var ()
  539. (org-test-with-temp-text-in-file "
  540. #+begin_src emacs-lisp
  541. ;;
  542. #+end_src"
  543. (progn
  544. (org-babel-next-src-block)
  545. (org-ctrl-c-ctrl-c)
  546. (should (re-search-forward "\\#\\+results:" nil t))
  547. (forward-line)
  548. (should
  549. (string=
  550. ""
  551. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  552. (org-test-with-temp-text-in-file "
  553. #+begin_src emacs-lisp
  554. \"some text\";;
  555. #+end_src"
  556. (progn
  557. (org-babel-next-src-block)
  558. (org-ctrl-c-ctrl-c)
  559. (should (re-search-forward "\\#\\+results:" nil t))
  560. (forward-line)
  561. (should
  562. (string=
  563. ": some text"
  564. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
  565. (ert-deftest test-ob/commented-last-block-line-with-var ()
  566. (org-test-with-temp-text-in-file "
  567. #+begin_src emacs-lisp :var a=1
  568. ;;
  569. #+end_src"
  570. (progn
  571. (org-babel-next-src-block)
  572. (org-ctrl-c-ctrl-c)
  573. (re-search-forward "\\#\\+results:" nil t)
  574. (forward-line)
  575. (should (string=
  576. ""
  577. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  578. (org-test-with-temp-text-in-file "
  579. #+begin_src emacs-lisp :var a=2
  580. 2;;
  581. #+end_src"
  582. (progn
  583. (org-babel-next-src-block)
  584. (org-ctrl-c-ctrl-c)
  585. (re-search-forward "\\#\\+results:" nil t)
  586. (forward-line)
  587. (should (string=
  588. ": 2"
  589. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
  590. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  591. "Test helper function to test `org-babel-remove-result'.
  592. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  593. and the result of execution is verified against RESULT.
  594. The block is actually executed /twice/ to ensure result
  595. replacement happens correctly."
  596. (org-test-with-temp-text
  597. buffer-text
  598. (progn
  599. (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
  600. (should (re-search-forward "\\#\\+results:" nil t))
  601. (forward-line)
  602. (should (string= result
  603. (buffer-substring-no-properties
  604. (point-at-bol)
  605. (- (point-max) 16))))
  606. (org-babel-previous-src-block) (org-babel-remove-result)
  607. (should (string= buffer-text
  608. (buffer-substring-no-properties
  609. (point-min) (point-max)))))))
  610. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  611. "Test `org-babel-remove-result' with default :results."
  612. (mapcar (lambda (language)
  613. (test-ob-verify-result-and-removed-result
  614. "\n"
  615. (concat
  616. "* org-babel-remove-result
  617. #+begin_src " language "
  618. #+end_src
  619. * next heading")))
  620. '("sh" "emacs-lisp")))
  621. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  622. "Test `org-babel-remove-result' with :results list."
  623. (test-ob-verify-result-and-removed-result
  624. "- 1
  625. - 2
  626. - 3
  627. - (quote (4 5))"
  628. "* org-babel-remove-result
  629. #+begin_src emacs-lisp :results list
  630. '(1 2 3 '(4 5))
  631. #+end_src
  632. * next heading"))
  633. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  634. "Test `org-babel-remove-result' with :results wrap."
  635. (test-ob-verify-result-and-removed-result
  636. ":RESULTS:
  637. hello there
  638. :END:"
  639. "* org-babel-remove-result
  640. #+begin_src emacs-lisp :results wrap
  641. \"hello there\"
  642. #+end_src
  643. * next heading"))
  644. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  645. "Test `org-babel-remove-result' with :results org."
  646. (test-ob-verify-result-and-removed-result
  647. "#+BEGIN_ORG
  648. * heading
  649. ** subheading
  650. content
  651. #+END_ORG"
  652. "* org-babel-remove-result
  653. #+begin_src emacs-lisp :results org
  654. \"* heading
  655. ** subheading
  656. content\"
  657. #+end_src
  658. * next heading"))
  659. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  660. "Test `org-babel-remove-result' with :results html."
  661. (test-ob-verify-result-and-removed-result
  662. "#+BEGIN_HTML
  663. <head><body></body></head>
  664. #+END_HTML"
  665. "* org-babel-remove-result
  666. #+begin_src emacs-lisp :results html
  667. \"<head><body></body></head>\"
  668. #+end_src
  669. * next heading"))
  670. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  671. "Test `org-babel-remove-result' with :results latex."
  672. (test-ob-verify-result-and-removed-result
  673. "#+BEGIN_LaTeX
  674. Line 1
  675. Line 2
  676. Line 3
  677. #+END_LaTeX"
  678. "* org-babel-remove-result
  679. #+begin_src emacs-lisp :results latex
  680. \"Line 1
  681. Line 2
  682. Line 3\"
  683. #+end_src
  684. * next heading"))
  685. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  686. "Test `org-babel-remove-result' with :results code."
  687. (test-ob-verify-result-and-removed-result
  688. "#+BEGIN_SRC emacs-lisp
  689. \"I am working!\"
  690. #+END_SRC"
  691. "* org-babel-remove-result
  692. #+begin_src emacs-lisp :results code
  693. (message \"I am working!\")
  694. #+end_src
  695. * next heading"))
  696. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  697. "Test `org-babel-remove-result' with :results pp."
  698. (test-ob-verify-result-and-removed-result
  699. ": \"I /am/ working!\""
  700. "* org-babel-remove-result
  701. #+begin_src emacs-lisp :results pp
  702. \"I /am/ working!\")
  703. #+end_src
  704. * next heading"))
  705. (provide 'test-ob)
  706. ;;; test-ob ends here