test-ob.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. ;;; test-ob.el --- tests for ob.el
  2. ;; Copyright (c) 2010-2012 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. (org-test-with-temp-text-in-file "
  106. * elisp forms in header arguments
  107. :PROPERTIES:
  108. :var: prop = (* 7 6)
  109. :END:
  110. #+begin_src emacs-lisp
  111. prop
  112. #+end_src"
  113. (progn
  114. (goto-char (point-min))
  115. (org-babel-next-src-block)
  116. (let ((info (org-babel-get-src-block-info)))
  117. (should (= 42 (org-babel-execute-src-block)))))))
  118. (ert-deftest test-org-babel/simple-named-code-block ()
  119. "Test that simple named code blocks can be evaluated."
  120. (org-test-with-temp-text-in-file "
  121. #+name: i-have-a-name
  122. #+begin_src emacs-lisp
  123. 42
  124. #+end_src"
  125. (progn
  126. (org-babel-next-src-block 1)
  127. (should (= 42 (org-babel-execute-src-block))))))
  128. (ert-deftest test-org-babel/simple-variable-resolution ()
  129. "Test that simple variable resolution is working."
  130. (org-test-with-temp-text-in-file "
  131. #+name: four
  132. #+begin_src emacs-lisp
  133. (list 1 2 3 4)
  134. #+end_src
  135. #+begin_src emacs-lisp :var four=four
  136. (length four)
  137. #+end_src"
  138. (progn
  139. (org-babel-next-src-block 2)
  140. (should (= 4 (org-babel-execute-src-block)))
  141. (forward-line 5)
  142. (should (string= ": 4" (buffer-substring
  143. (point-at-bol)
  144. (point-at-eol)))))))
  145. (ert-deftest test-org-babel/multi-line-header-arguments ()
  146. "Test that multi-line header arguments and can be read."
  147. (org-test-with-temp-text-in-file "
  148. #+headers: :var letters='(a b c d e f g)
  149. #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
  150. (map 'list #'list numbers letters)
  151. #+end_src"
  152. (progn
  153. (org-babel-next-src-block)
  154. (let ((results (org-babel-execute-src-block)))
  155. (should(equal 'a (cadr (assoc 1 results))))
  156. (should(equal 'd (cadr (assoc 4 results))))))))
  157. (ert-deftest test-org-babel/parse-header-args ()
  158. (org-test-with-temp-text-in-file "
  159. #+begin_src example-lang :session :results output :var num=9
  160. the body
  161. #+end_src"
  162. (progn
  163. (org-babel-next-src-block)
  164. (let* ((info (org-babel-get-src-block-info))
  165. (params (nth 2 info)))
  166. (message "%S" params)
  167. (should(equal "example-lang" (nth 0 info)))
  168. (should(string= "the body" (org-babel-trim (nth 1 info))))
  169. (should-not (member '(:session\ \ \ \ ) params))
  170. (should(equal '(:session) (assoc :session params)))
  171. (should(equal '(:result-type . output) (assoc :result-type params)))
  172. (should(equal '(num . 9) (cdr (assoc :var params))))))))
  173. (ert-deftest test-org-babel/parse-header-args2 ()
  174. (org-test-with-temp-text-in-file "
  175. * resolving sub-trees as references
  176. #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
  177. (length text)
  178. #+end_src
  179. #+begin_src org :noweb yes
  180. <<simple-subtree>>
  181. <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
  182. #+end_src
  183. ** simple subtree with custom ID
  184. :PROPERTIES:
  185. :CUSTOM_ID: simple-subtree
  186. :END:
  187. this is simple"
  188. (progn
  189. (should (string-match (regexp-quote "this is simple")
  190. (org-babel-ref-resolve "simple-subtree")))
  191. (org-babel-next-src-block)
  192. (should (= 14 (org-babel-execute-src-block))))))
  193. (ert-deftest test-org-babel/inline-src-blocks ()
  194. (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
  195. (macrolet ((at-next (&rest body)
  196. `(progn
  197. (move-end-of-line 1)
  198. (re-search-forward org-babel-inline-src-block-regexp nil t)
  199. (goto-char (match-beginning 1))
  200. (save-match-data ,@body))))
  201. (at-next (should (equal 1 (org-babel-execute-src-block))))
  202. (at-next (should (equal 2 (org-babel-execute-src-block))))
  203. (at-next (should (equal 3 (org-babel-execute-src-block)))))))
  204. (ert-deftest test-org-babel/org-babel-get-inline-src-block-matches ()
  205. (org-test-at-id "0D0983D4-DE33-400A-8A05-A225A567BC74"
  206. (let ((test-point (point)))
  207. (should (fboundp 'org-babel-get-inline-src-block-matches))
  208. (should (re-search-forward "src_" nil t)) ;; 1
  209. (should (= (+ test-point 138) (match-end 0)))
  210. (should (org-babel-get-inline-src-block-matches))
  211. (should (re-search-forward "}" nil (point-at-bol))) ;; 1
  212. (should-not (org-babel-get-inline-src-block-matches))
  213. (should (re-search-forward "in" nil t)) ;; 2
  214. (should-not (org-babel-get-inline-src-block-matches))
  215. (should (re-search-forward "echo" nil t)) ;; 2
  216. (should (org-babel-get-inline-src-block-matches))
  217. (should (re-search-forward "blocks" nil t)) ;; 3
  218. (backward-char 8) ;; 3
  219. (should (org-babel-get-inline-src-block-matches))
  220. (forward-char 1) ;;3
  221. (should-not (org-babel-get-inline-src-block-matches))
  222. (should (re-search-forward ":results" nil t)) ;; 4
  223. (should (org-babel-get-inline-src-block-matches))
  224. (end-of-line)
  225. (should-not (org-babel-get-inline-src-block-matches)))))
  226. (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-1 ()
  227. (let ((test-line "src_sh{echo 1}"))
  228. ;; src_ at bol line 1...
  229. (org-test-with-temp-text
  230. test-line
  231. (goto-char (point-min)) (org-ctrl-c-ctrl-c)
  232. (should (string=
  233. (concat test-line " =1=")
  234. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  235. (forward-char) (org-ctrl-c-ctrl-c)
  236. (should (string=
  237. (concat test-line " =1= =1=")
  238. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  239. (re-search-forward "1}")
  240. (should-error (org-ctrl-c-ctrl-c))
  241. (backward-char) ;; last char of block body
  242. (org-ctrl-c-ctrl-c)
  243. (should (string=
  244. (concat test-line " =1= =1= =1=")
  245. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  246. ;; src_ follows space line 1...
  247. (let ((test-line " src_emacs-lisp{ 1 }"))
  248. (org-test-with-temp-text
  249. test-line
  250. (should-error (org-ctrl-c-ctrl-c))
  251. (forward-char) (org-ctrl-c-ctrl-c)
  252. (should (string=
  253. (concat test-line " =1=")
  254. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  255. (re-search-forward "{ 1 ") (org-ctrl-c-ctrl-c)
  256. (should (string=
  257. (concat test-line " =1= =1=")
  258. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  259. (forward-char)
  260. (should-error (org-ctrl-c-ctrl-c)))))
  261. (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-2 ()
  262. ;; src_ at bol line 2...
  263. (let ((test-line " src_emacs-lisp{ \"x\" }"))
  264. (org-test-with-temp-text
  265. (concat "\n" test-line)
  266. (should-error (org-ctrl-c-ctrl-c))
  267. (goto-char (point-min))
  268. (should-error (org-ctrl-c-ctrl-c))
  269. (forward-line)
  270. (should-error (org-ctrl-c-ctrl-c))
  271. (forward-char) (org-ctrl-c-ctrl-c)
  272. (should (string=
  273. (concat test-line " =x=")
  274. (buffer-substring-no-properties
  275. (point-at-bol) (point-at-eol))))))
  276. (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }"))
  277. (org-test-with-temp-text
  278. test-line
  279. (goto-char (point-max))
  280. (insert (concat "\n" test-line " end"))
  281. (re-search-backward "src") (org-ctrl-c-ctrl-c)
  282. (should (string=
  283. (concat test-line " =y= end")
  284. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  285. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  286. (should (string=
  287. (concat test-line " =y= =y= end")
  288. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  289. (forward-char)
  290. (should-error (org-ctrl-c-ctrl-c))))))
  291. (ert-deftest test-org-babel/inline-src_blk-manual-results-replace ()
  292. (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }"))
  293. (org-test-with-temp-text
  294. (concat "\n" test-line)
  295. (should-error (org-ctrl-c-ctrl-c))
  296. (goto-char (point-max))
  297. (should-error (org-ctrl-c-ctrl-c))
  298. (beginning-of-line)
  299. (should-error (org-ctrl-c-ctrl-c))
  300. (forward-char) (org-ctrl-c-ctrl-c)
  301. (should (string=
  302. (concat test-line " =x=")
  303. (buffer-substring-no-properties
  304. (point-at-bol) (point-at-eol))))))
  305. (let ((test-line (concat " Some text prior to block "
  306. "src_emacs-lisp[:results replace]{ \"y\" }")))
  307. (org-test-with-temp-text test-line
  308. (goto-char (point-max))
  309. (insert (concat "\n" test-line " end"))
  310. (re-search-backward "src") (org-ctrl-c-ctrl-c)
  311. (should (string=
  312. (concat test-line " =y= end")
  313. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  314. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  315. (should (string=
  316. (concat test-line " =y= =y= end")
  317. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  318. (forward-char)
  319. (should-error (org-ctrl-c-ctrl-c)))))
  320. (ert-deftest test-org-babel/inline-src_blk-results-silent ()
  321. (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
  322. (org-test-with-temp-text test-line
  323. (org-ctrl-c-ctrl-c)
  324. (should (string= test-line
  325. (buffer-substring-no-properties
  326. (point-at-bol) (point-at-eol))))
  327. (end-of-buffer)
  328. (should-error (org-ctrl-c-ctrl-c))))
  329. (let ((test-line (concat " Some text prior to block src_emacs-lisp"
  330. "[ :results silent ]{ \"y\" }")))
  331. (org-test-with-temp-text
  332. test-line
  333. (goto-char (point-max))
  334. (insert (concat "\n" test-line " end"))
  335. (re-search-backward "src_") (org-ctrl-c-ctrl-c)
  336. (should (string= (concat test-line " end")
  337. (buffer-substring-no-properties
  338. (point-at-bol) (point-at-eol))))
  339. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  340. (should (string= (concat test-line " end")
  341. (buffer-substring-no-properties
  342. (point-at-bol) (point-at-eol))))
  343. (forward-char)
  344. (should-error (org-ctrl-c-ctrl-c)))))
  345. (ert-deftest test-org-babel/inline-src_blk-results-raw ()
  346. (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
  347. (org-test-with-temp-text test-line
  348. (org-ctrl-c-ctrl-c)
  349. (should (string= (concat test-line " x")
  350. (buffer-string)))))
  351. (let ((test-line (concat " Some text prior to block "
  352. "src_emacs-lisp[ :results raw ]{ \"the\" }")))
  353. (org-test-with-temp-text (concat test-line " end")
  354. (re-search-forward "src_") (org-ctrl-c-ctrl-c)
  355. (should (string= (concat test-line " the end")
  356. (buffer-substring-no-properties
  357. (point-at-bol) (point-at-eol))))
  358. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  359. (should (string= (concat test-line " the the end")
  360. (buffer-substring-no-properties
  361. (point-at-bol) (point-at-eol))))
  362. (forward-char)
  363. (should-error (org-ctrl-c-ctrl-c)))))
  364. (ert-deftest test-org-babel/inline-src_blk-results-file ()
  365. (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
  366. (org-test-with-temp-text
  367. test-line
  368. (org-ctrl-c-ctrl-c)
  369. (should (string= (concat test-line " [[file:~/test-file]]")
  370. (buffer-substring-no-properties
  371. (point-min) (point-max)))))))
  372. (ert-deftest test-org-babel/inline-src_blk-results-scalar ()
  373. (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }"))
  374. (org-test-with-temp-text
  375. test-line
  376. (org-ctrl-c-ctrl-c)
  377. (should (string= (concat test-line " =\"x\"=")
  378. (buffer-substring-no-properties
  379. (point-min) (point-max)))))))
  380. (ert-deftest test-org-babel/inline-src_blk-results-verbatim ()
  381. (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }"))
  382. (org-test-with-temp-text
  383. test-line
  384. (org-ctrl-c-ctrl-c)
  385. (should (string= (concat test-line " =\"x\"=")
  386. (buffer-substring-no-properties
  387. (point-min) (point-max)))))))
  388. (ert-deftest test-org-babel/combining-scalar-and-raw-result-types ()
  389. (org-test-with-temp-text-in-file "
  390. #+begin_src sh :results scalar
  391. echo \"[[file:./cv.cls]]\"
  392. #+end_src
  393. #+name:
  394. : [[file:./cv.cls]]
  395. #+begin_src sh :results raw scalar
  396. echo \"[[file:./cv.cls]]\"
  397. #+end_src
  398. "
  399. (flet ((next-result ()
  400. (org-babel-next-src-block)
  401. (org-babel-execute-src-block)
  402. (goto-char (org-babel-where-is-src-block-result))
  403. (forward-line 1)))
  404. (goto-char (point-min))
  405. (next-result)
  406. (should (org-babel-in-example-or-verbatim))
  407. (next-result)
  408. (should (not (org-babel-in-example-or-verbatim))))))
  409. (ert-deftest test-org-babel/no-defaut-value-for-var ()
  410. "Test that the absence of a default value for a variable DOES THROW
  411. a proper error."
  412. (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
  413. (org-babel-next-src-block)
  414. (let ((err
  415. (should-error (org-babel-execute-src-block) :type 'error)))
  416. (should
  417. (equal
  418. '(error
  419. "variable \"x\" in block \"carre\" must be assigned a default value")
  420. err)))))
  421. (ert-deftest test-org-babel/just-one-results-block ()
  422. "Test that evaluating two times the same code block does not result in a
  423. duplicate results block."
  424. (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
  425. (org-babel-execute-src-block)
  426. (org-babel-execute-src-block) ; second code block execution
  427. (should (search-forward "Hello")) ; the string inside the source code block
  428. (should (search-forward "Hello")) ; the same string in the results block
  429. (should-error (search-forward "Hello"))))
  430. (ert-deftest test-org-babel/nested-code-block ()
  431. "Test nested code blocks inside code blocks don't cause problems."
  432. (org-test-with-temp-text "#+begin_src org :results silent
  433. ,#+begin_src emacs-lisp
  434. , 'foo
  435. ,#+end_src
  436. #+end_src"
  437. (should (string= (org-babel-execute-src-block)
  438. "#+begin_src emacs-lisp\n 'foo\n#+end_src"))))
  439. (ert-deftest test-org-babel/partial-nested-code-block ()
  440. "Test nested code blocks inside code blocks don't cause problems."
  441. (org-test-with-temp-text "#+begin_src org :results silent
  442. ,#+begin_src emacs-lisp
  443. #+end_src"
  444. (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
  445. (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
  446. (org-test-with-temp-text "#+NAME: foo
  447. #+BEGIN_SRC emacs-lisp
  448. 'foo
  449. #+END_SRC\n"
  450. (org-babel-next-src-block 1)
  451. (should (eq 'foo (org-babel-execute-src-block)))
  452. (goto-char (point-min))
  453. (org-babel-next-src-block 1)
  454. (should (looking-at org-babel-src-block-regexp))))
  455. (ert-deftest test-ob/catches-all-references ()
  456. (org-test-with-temp-text "
  457. #+NAME: literal-example
  458. #+BEGIN_EXAMPLE
  459. A literal example
  460. on two lines
  461. #+END_EXAMPLE
  462. #+NAME: read-literal-example
  463. #+BEGIN_SRC emacs-lisp :var x=literal-example
  464. (concatenate 'string x \" for me.\")
  465. #+END_SRC"
  466. (org-babel-next-src-block 1)
  467. (should (string= (org-babel-execute-src-block)
  468. "A literal example\non two lines for me."))))
  469. (ert-deftest test-ob/resolve-code-blocks-before-data-blocks ()
  470. (org-test-with-temp-text "
  471. #+name: foo
  472. : bar
  473. #+name: foo
  474. #+begin_src emacs-lisp
  475. \"baz\"
  476. #+end_src
  477. #+begin_src emacs-lisp :var foo=foo
  478. foo
  479. #+end_src"
  480. (org-babel-next-src-block 2)
  481. (should (string= (org-babel-execute-src-block) "baz"))))
  482. (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
  483. (org-test-with-temp-text "
  484. #+tblname: base_plus
  485. | 1 |
  486. | 2 |
  487. #+tblname: base
  488. | 3 |
  489. | 4 |
  490. #+begin_src emacs-lisp :var x=base
  491. x
  492. #+end_src"
  493. (org-babel-next-src-block 1)
  494. (should (equal (org-babel-execute-src-block) '((3) (4))))))
  495. (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
  496. (org-test-with-temp-text "
  497. #+name: base_plus
  498. #+begin_src emacs-lisp
  499. 'bar
  500. #+end_src
  501. #+name: base
  502. #+begin_src emacs-lisp
  503. 'foo
  504. #+end_src
  505. #+begin_src emacs-lisp :var x=base
  506. x
  507. #+end_src"
  508. (org-babel-next-src-block 3)
  509. (should (equal (org-babel-execute-src-block) "foo"))))
  510. (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
  511. (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
  512. (+ a b c d)
  513. #+end_src
  514. "
  515. (should (= 10 (org-babel-execute-src-block)))))
  516. (ert-deftest test-ob/org-babel-update-intermediate ()
  517. (org-test-with-temp-text "#+name: foo
  518. #+begin_src emacs-lisp
  519. 2
  520. #+end_src
  521. #+results: foo
  522. : 4
  523. #+begin_src emacs-lisp :var it=foo
  524. (+ it 1)
  525. #+end_src"
  526. (let ((org-babel-update-intermediate nil))
  527. (goto-char (point-min))
  528. (org-babel-next-src-block 2)
  529. (should (= 3 (org-babel-execute-src-block)))
  530. (goto-char (point-min))
  531. (forward-line 6)
  532. (should (looking-at ": 4")))
  533. (let ((org-babel-update-intermediate t))
  534. (goto-char (point-min))
  535. (org-babel-next-src-block 2)
  536. (should (= 3 (org-babel-execute-src-block)))
  537. (goto-char (point-min))
  538. (forward-line 6)
  539. (should (looking-at ": 2")))))
  540. (ert-deftest test-ob/eval-header-argument ()
  541. (flet ((check-eval (eval runp)
  542. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  543. (setq foo :evald)
  544. #+end_src" eval)
  545. (let ((foo :not-run))
  546. (if runp
  547. (progn (should (org-babel-execute-src-block))
  548. (should (eq foo :evald)))
  549. (progn (should-not (org-babel-execute-src-block))
  550. (should-not (eq foo :evald))))))))
  551. (check-eval "never" nil)
  552. (check-eval "no" nil)
  553. (check-eval "never-export" t)
  554. (check-eval "no-export" t)
  555. (let ((org-current-export-file "something"))
  556. (check-eval "never" nil)
  557. (check-eval "no" nil)
  558. (check-eval "never-export" nil)
  559. (check-eval "no-export" nil))))
  560. (ert-deftest test-ob/noweb-expansion-1 ()
  561. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  562. <<foo>>
  563. #+end_src
  564. #+name: foo
  565. #+begin_src sh
  566. bar
  567. #+end_src"
  568. (should (string= (org-babel-expand-noweb-references) "bar"))))
  569. (ert-deftest test-ob/noweb-expansion-2 ()
  570. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  571. <<foo>>
  572. #+end_src
  573. #+name: foo
  574. #+begin_src sh
  575. bar
  576. #+end_src
  577. #+begin_src sh :noweb-ref foo
  578. baz
  579. #+end_src"
  580. (should (string= (org-babel-expand-noweb-references) "barbaz"))))
  581. (ert-deftest test-ob/splitting-variable-lists-in-references ()
  582. (org-test-with-temp-text ""
  583. (should (= 1 (length (org-babel-ref-split-args
  584. "a=\"this, no work\""))))
  585. (should (= 2 (length (org-babel-ref-split-args
  586. "a=\"this, no work\", b=1"))))))
  587. (ert-deftest test-ob/org-babel-balanced-split ()
  588. (should (equal
  589. '(":a 1" "b [2 3]" "c (4 :d (5 6))")
  590. (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
  591. '((32 9) . 58)))))
  592. (ert-deftest test-ob/commented-last-block-line-no-var ()
  593. (org-test-with-temp-text-in-file "
  594. #+begin_src emacs-lisp
  595. ;;
  596. #+end_src"
  597. (progn
  598. (org-babel-next-src-block)
  599. (org-ctrl-c-ctrl-c)
  600. (should (re-search-forward "\\#\\+results:" nil t))
  601. (forward-line)
  602. (should
  603. (string=
  604. ""
  605. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  606. (org-test-with-temp-text-in-file "
  607. #+begin_src emacs-lisp
  608. \"some text\";;
  609. #+end_src"
  610. (progn
  611. (org-babel-next-src-block)
  612. (org-ctrl-c-ctrl-c)
  613. (should (re-search-forward "\\#\\+results:" nil t))
  614. (forward-line)
  615. (should
  616. (string=
  617. ": some text"
  618. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
  619. (ert-deftest test-ob/commented-last-block-line-with-var ()
  620. (org-test-with-temp-text-in-file "
  621. #+begin_src emacs-lisp :var a=1
  622. ;;
  623. #+end_src"
  624. (progn
  625. (org-babel-next-src-block)
  626. (org-ctrl-c-ctrl-c)
  627. (re-search-forward "\\#\\+results:" nil t)
  628. (forward-line)
  629. (should (string=
  630. ""
  631. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  632. (org-test-with-temp-text-in-file "
  633. #+begin_src emacs-lisp :var a=2
  634. 2;;
  635. #+end_src"
  636. (progn
  637. (org-babel-next-src-block)
  638. (org-ctrl-c-ctrl-c)
  639. (re-search-forward "\\#\\+results:" nil t)
  640. (forward-line)
  641. (should (string=
  642. ": 2"
  643. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))))
  644. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  645. "Test helper function to test `org-babel-remove-result'.
  646. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  647. and the result of execution is verified against RESULT.
  648. The block is actually executed /twice/ to ensure result
  649. replacement happens correctly."
  650. (org-test-with-temp-text
  651. buffer-text
  652. (progn
  653. (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
  654. (should (re-search-forward "\\#\\+results:" nil t))
  655. (forward-line)
  656. (should (string= result
  657. (buffer-substring-no-properties
  658. (point-at-bol)
  659. (- (point-max) 16))))
  660. (org-babel-previous-src-block) (org-babel-remove-result)
  661. (should (string= buffer-text
  662. (buffer-substring-no-properties
  663. (point-min) (point-max)))))))
  664. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  665. "Test `org-babel-remove-result' with default :results."
  666. (mapcar (lambda (language)
  667. (test-ob-verify-result-and-removed-result
  668. "\n"
  669. (concat
  670. "* org-babel-remove-result
  671. #+begin_src " language "
  672. #+end_src
  673. * next heading")))
  674. '("sh" "emacs-lisp")))
  675. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  676. "Test `org-babel-remove-result' with :results list."
  677. (test-ob-verify-result-and-removed-result
  678. "- 1
  679. - 2
  680. - 3
  681. - (quote (4 5))"
  682. "* org-babel-remove-result
  683. #+begin_src emacs-lisp :results list
  684. '(1 2 3 '(4 5))
  685. #+end_src
  686. * next heading"))
  687. ;; TODO FIXME Activate when Eric's trailing newline fix has been committed
  688. ;; (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  689. ;; (test-ob-verify-result-and-removed-result
  690. ;; ":RESULTS:
  691. ;; hello there
  692. ;; :END:"
  693. ;;
  694. ;; "* org-babel-remove-result
  695. ;;
  696. ;; +begin_src emacs-lisp :results wrap
  697. ;; \"hello there\"
  698. ;; #+end_src
  699. ;;
  700. ;; * next heading"))
  701. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  702. "Test `org-babel-remove-result' with :results org."
  703. (test-ob-verify-result-and-removed-result
  704. "#+BEGIN_ORG
  705. * heading
  706. ** subheading
  707. content
  708. #+END_ORG"
  709. "* org-babel-remove-result
  710. #+begin_src emacs-lisp :results org
  711. \"* heading
  712. ** subheading
  713. content\"
  714. #+end_src
  715. * next heading"))
  716. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  717. "Test `org-babel-remove-result' with :results html."
  718. (test-ob-verify-result-and-removed-result
  719. "#+BEGIN_HTML
  720. <head><body></body></head>
  721. #+END_HTML"
  722. "* org-babel-remove-result
  723. #+begin_src emacs-lisp :results html
  724. \"<head><body></body></head>\"
  725. #+end_src
  726. * next heading"))
  727. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  728. "Test `org-babel-remove-result' with :results latex."
  729. (test-ob-verify-result-and-removed-result
  730. "#+BEGIN_LaTeX
  731. Line 1
  732. Line 2
  733. Line 3
  734. #+END_LaTeX"
  735. "* org-babel-remove-result
  736. #+begin_src emacs-lisp :results latex
  737. \"Line 1
  738. Line 2
  739. Line 3\"
  740. #+end_src
  741. * next heading"))
  742. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  743. "Test `org-babel-remove-result' with :results code."
  744. (test-ob-verify-result-and-removed-result
  745. "#+BEGIN_SRC emacs-lisp
  746. \"I am working!\"
  747. #+END_SRC"
  748. "* org-babel-remove-result
  749. #+begin_src emacs-lisp :results code
  750. (message \"I am working!\")
  751. #+end_src
  752. * next heading"))
  753. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  754. "Test `org-babel-remove-result' with :results pp."
  755. (test-ob-verify-result-and-removed-result
  756. ": \"I /am/ working!\""
  757. "* org-babel-remove-result
  758. #+begin_src emacs-lisp :results pp
  759. \"I /am/ working!\")
  760. #+end_src
  761. * next heading"))
  762. (provide 'test-ob)
  763. ;;; test-ob ends here