test-ob.el 40 KB

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