test-ob.el 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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. (org-babel-inline-result-wrap "=%s="))
  261. ;; src_ at bol line 1...
  262. (org-test-with-temp-text
  263. test-line
  264. (goto-char (point-min)) (org-ctrl-c-ctrl-c)
  265. (should (string=
  266. (concat test-line " =1=")
  267. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  268. (forward-char) (org-ctrl-c-ctrl-c)
  269. (should (string=
  270. (concat test-line " =1= =1=")
  271. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  272. (re-search-forward "1}")
  273. (should-error (org-ctrl-c-ctrl-c))
  274. (backward-char) ;; last char of block body
  275. (org-ctrl-c-ctrl-c)
  276. (should (string=
  277. (concat test-line " =1= =1= =1=")
  278. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  279. ;; src_ follows space line 1...
  280. (let ((test-line " src_emacs-lisp{ 1 }"))
  281. (org-test-with-temp-text
  282. test-line
  283. (should-error (org-ctrl-c-ctrl-c))
  284. (forward-char) (org-ctrl-c-ctrl-c)
  285. (should (string=
  286. (concat test-line " =1=")
  287. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  288. (re-search-forward "{ 1 ") (org-ctrl-c-ctrl-c)
  289. (should (string=
  290. (concat test-line " =1= =1=")
  291. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  292. (forward-char)
  293. (should-error (org-ctrl-c-ctrl-c))))))
  294. (ert-deftest test-org-babel/inline-src_blk-default-results-replace-line-2 ()
  295. ;; src_ at bol line 2...
  296. (let ((test-line " src_emacs-lisp{ \"x\" }")
  297. (org-babel-inline-result-wrap "=%s="))
  298. (org-test-with-temp-text
  299. (concat "\n" test-line)
  300. (should-error (org-ctrl-c-ctrl-c))
  301. (goto-char (point-min))
  302. (should-error (org-ctrl-c-ctrl-c))
  303. (forward-line)
  304. (should-error (org-ctrl-c-ctrl-c))
  305. (forward-char) (org-ctrl-c-ctrl-c)
  306. (should (string=
  307. (concat test-line " =x=")
  308. (buffer-substring-no-properties
  309. (point-at-bol) (point-at-eol))))))
  310. (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
  311. (org-babel-inline-result-wrap "=%s="))
  312. (org-test-with-temp-text
  313. test-line
  314. (goto-char (point-max))
  315. (insert (concat "\n" test-line " end"))
  316. (re-search-backward "src") (org-ctrl-c-ctrl-c)
  317. (should (string=
  318. (concat test-line " =y= end")
  319. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  320. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  321. (should (string=
  322. (concat test-line " =y= =y= end")
  323. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  324. (forward-char)
  325. (should-error (org-ctrl-c-ctrl-c)))))
  326. (ert-deftest test-org-babel/inline-src_blk-manual-results-replace ()
  327. (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
  328. (org-babel-inline-result-wrap "=%s="))
  329. (org-test-with-temp-text
  330. (concat "\n" test-line)
  331. (should-error (org-ctrl-c-ctrl-c))
  332. (goto-char (point-max))
  333. (should-error (org-ctrl-c-ctrl-c))
  334. (beginning-of-line)
  335. (should-error (org-ctrl-c-ctrl-c))
  336. (forward-char) (org-ctrl-c-ctrl-c)
  337. (should (string=
  338. (concat test-line " =x=")
  339. (buffer-substring-no-properties
  340. (point-at-bol) (point-at-eol))))))
  341. (let ((test-line (concat " Some text prior to block "
  342. "src_emacs-lisp[:results replace]{ \"y\" }"))
  343. (org-babel-inline-result-wrap "=%s="))
  344. (org-test-with-temp-text test-line
  345. (goto-char (point-max))
  346. (insert (concat "\n" test-line " end"))
  347. (re-search-backward "src") (org-ctrl-c-ctrl-c)
  348. (should (string=
  349. (concat test-line " =y= end")
  350. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  351. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  352. (should (string=
  353. (concat test-line " =y= =y= end")
  354. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  355. (forward-char)
  356. (should-error (org-ctrl-c-ctrl-c)))))
  357. (ert-deftest test-org-babel/inline-src_blk-results-silent ()
  358. (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
  359. (org-test-with-temp-text test-line
  360. (org-ctrl-c-ctrl-c)
  361. (should (string= test-line
  362. (buffer-substring-no-properties
  363. (point-at-bol) (point-at-eol))))
  364. (end-of-buffer)
  365. (should-error (org-ctrl-c-ctrl-c))))
  366. (let ((test-line (concat " Some text prior to block src_emacs-lisp"
  367. "[ :results silent ]{ \"y\" }")))
  368. (org-test-with-temp-text
  369. test-line
  370. (goto-char (point-max))
  371. (insert (concat "\n" test-line " end"))
  372. (re-search-backward "src_") (org-ctrl-c-ctrl-c)
  373. (should (string= (concat test-line " end")
  374. (buffer-substring-no-properties
  375. (point-at-bol) (point-at-eol))))
  376. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  377. (should (string= (concat test-line " end")
  378. (buffer-substring-no-properties
  379. (point-at-bol) (point-at-eol))))
  380. (forward-char)
  381. (should-error (org-ctrl-c-ctrl-c)))))
  382. (ert-deftest test-org-babel/inline-src_blk-results-raw ()
  383. (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
  384. (org-test-with-temp-text test-line
  385. (org-ctrl-c-ctrl-c)
  386. (should (string= (concat test-line " x")
  387. (buffer-string)))))
  388. (let ((test-line (concat " Some text prior to block "
  389. "src_emacs-lisp[ :results raw ]{ \"the\" }")))
  390. (org-test-with-temp-text (concat test-line " end")
  391. (re-search-forward "src_") (org-ctrl-c-ctrl-c)
  392. (should (string= (concat test-line " the end")
  393. (buffer-substring-no-properties
  394. (point-at-bol) (point-at-eol))))
  395. (re-search-forward "\" ") (org-ctrl-c-ctrl-c)
  396. (should (string= (concat test-line " the the end")
  397. (buffer-substring-no-properties
  398. (point-at-bol) (point-at-eol))))
  399. (forward-char)
  400. (should-error (org-ctrl-c-ctrl-c)))))
  401. (ert-deftest test-org-babel/inline-src_blk-results-file ()
  402. (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
  403. (org-test-with-temp-text
  404. test-line
  405. (org-ctrl-c-ctrl-c)
  406. (should (string= (concat test-line " [[file:~/test-file]]")
  407. (buffer-substring-no-properties
  408. (point-min) (point-max)))))))
  409. (ert-deftest test-org-babel/inline-src_blk-results-scalar ()
  410. (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
  411. (org-babel-inline-result-wrap "=%s="))
  412. (org-test-with-temp-text
  413. test-line
  414. (org-ctrl-c-ctrl-c)
  415. (should (string= (concat test-line " =\"x\"=")
  416. (buffer-substring-no-properties
  417. (point-min) (point-max)))))))
  418. (ert-deftest test-org-babel/inline-src_blk-results-verbatim ()
  419. (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  420. (org-babel-inline-result-wrap "=%s="))
  421. (org-test-with-temp-text
  422. test-line
  423. (org-ctrl-c-ctrl-c)
  424. (should (string= (concat test-line " =\"x\"=")
  425. (buffer-substring-no-properties
  426. (point-min) (point-max)))))))
  427. (ert-deftest test-org-babel/combining-scalar-and-raw-result-types ()
  428. (org-test-with-temp-text-in-file "
  429. #+begin_src sh :results scalar
  430. echo \"[[file:./cv.cls]]\"
  431. #+end_src
  432. #+name:
  433. : [[file:./cv.cls]]
  434. #+begin_src sh :results raw scalar
  435. echo \"[[file:./cv.cls]]\"
  436. #+end_src
  437. "
  438. (flet ((next-result ()
  439. (org-babel-next-src-block)
  440. (org-babel-execute-src-block)
  441. (goto-char (org-babel-where-is-src-block-result))
  442. (forward-line 1)))
  443. (goto-char (point-min))
  444. (next-result)
  445. (should (org-babel-in-example-or-verbatim))
  446. (next-result)
  447. (should (not (org-babel-in-example-or-verbatim))))))
  448. (ert-deftest test-org-babel/no-defaut-value-for-var ()
  449. "Test that the absence of a default value for a variable DOES THROW
  450. a proper error."
  451. (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
  452. (org-babel-next-src-block)
  453. (let ((err
  454. (should-error (org-babel-execute-src-block) :type 'error)))
  455. (should
  456. (equal
  457. '(error
  458. "Variable \"x\" must be assigned a default value")
  459. err)))))
  460. (ert-deftest test-org-babel/just-one-results-block ()
  461. "Test that evaluating two times the same code block does not result in a
  462. duplicate results block."
  463. (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
  464. (org-babel-execute-src-block)
  465. (org-babel-execute-src-block) ; second code block execution
  466. (should (search-forward "Hello")) ; the string inside the source code block
  467. (should (search-forward "Hello")) ; the same string in the results block
  468. (should-error (search-forward "Hello"))))
  469. (ert-deftest test-org-babel/nested-code-block ()
  470. "Test nested code blocks inside code blocks don't cause problems."
  471. (should
  472. (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
  473. (org-test-with-temp-text "#+begin_src org :results silent
  474. ,#+begin_src emacs-lisp
  475. , 'foo
  476. ,#+end_src
  477. #+end_src"
  478. (let ((org-edit-src-content-indentation 2)
  479. (org-src-preserve-indentation nil))
  480. (org-babel-execute-src-block))))))
  481. (ert-deftest test-org-babel/partial-nested-code-block ()
  482. "Test nested code blocks inside code blocks don't cause problems."
  483. (org-test-with-temp-text "#+begin_src org :results silent
  484. ,#+begin_src emacs-lisp
  485. #+end_src"
  486. (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
  487. (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
  488. (org-test-with-temp-text "#+NAME: foo
  489. #+BEGIN_SRC emacs-lisp
  490. 'foo
  491. #+END_SRC\n"
  492. (org-babel-next-src-block 1)
  493. (should (eq 'foo (org-babel-execute-src-block)))
  494. (goto-char (point-min))
  495. (org-babel-next-src-block 1)
  496. (should (looking-at org-babel-src-block-regexp))))
  497. (ert-deftest test-ob/catches-all-references ()
  498. (org-test-with-temp-text "
  499. #+NAME: literal-example
  500. #+BEGIN_EXAMPLE
  501. A literal example
  502. on two lines
  503. #+END_EXAMPLE
  504. #+NAME: read-literal-example
  505. #+BEGIN_SRC emacs-lisp :var x=literal-example
  506. (concatenate 'string x \" for me.\")
  507. #+END_SRC"
  508. (org-babel-next-src-block 1)
  509. (should (string= (org-babel-execute-src-block)
  510. "A literal example\non two lines\n for me."))))
  511. (ert-deftest test-ob/resolve-code-blocks-before-data-blocks ()
  512. (org-test-with-temp-text "
  513. #+name: foo
  514. : bar
  515. #+name: foo
  516. #+begin_src emacs-lisp
  517. \"baz\"
  518. #+end_src
  519. #+begin_src emacs-lisp :var foo=foo
  520. foo
  521. #+end_src"
  522. (org-babel-next-src-block 2)
  523. (should (string= (org-babel-execute-src-block) "baz"))))
  524. (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
  525. (org-test-with-temp-text "
  526. #+tblname: base_plus
  527. | 1 |
  528. | 2 |
  529. #+tblname: base
  530. | 3 |
  531. | 4 |
  532. #+begin_src emacs-lisp :var x=base
  533. x
  534. #+end_src"
  535. (org-babel-next-src-block 1)
  536. (should (equal (org-babel-execute-src-block) '((3) (4))))))
  537. (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
  538. (org-test-with-temp-text "
  539. #+name: base_plus
  540. #+begin_src emacs-lisp
  541. 'bar
  542. #+end_src
  543. #+name: base
  544. #+begin_src emacs-lisp
  545. 'foo
  546. #+end_src
  547. #+begin_src emacs-lisp :var x=base
  548. x
  549. #+end_src"
  550. (org-babel-next-src-block 3)
  551. (should (equal (org-babel-execute-src-block) "foo"))))
  552. (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
  553. (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
  554. (+ a b c d)
  555. #+end_src
  556. "
  557. (should (= 10 (org-babel-execute-src-block)))))
  558. (ert-deftest test-ob/org-babel-update-intermediate ()
  559. (org-test-with-temp-text "#+name: foo
  560. #+begin_src emacs-lisp
  561. 2
  562. #+end_src
  563. #+results: foo
  564. : 4
  565. #+begin_src emacs-lisp :var it=foo
  566. (+ it 1)
  567. #+end_src"
  568. (let ((org-babel-update-intermediate nil))
  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 ": 4")))
  575. (let ((org-babel-update-intermediate t))
  576. (goto-char (point-min))
  577. (org-babel-next-src-block 2)
  578. (should (= 3 (org-babel-execute-src-block)))
  579. (goto-char (point-min))
  580. (forward-line 6)
  581. (should (looking-at ": 2")))))
  582. (ert-deftest test-ob/eval-header-argument ()
  583. (flet ((check-eval (eval runp)
  584. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  585. (setq foo :evald)
  586. #+end_src" eval)
  587. (let ((foo :not-run))
  588. (if runp
  589. (progn (should (org-babel-execute-src-block))
  590. (should (eq foo :evald)))
  591. (progn (should-not (org-babel-execute-src-block))
  592. (should-not (eq foo :evald))))))))
  593. (check-eval "never" nil)
  594. (check-eval "no" nil)
  595. (check-eval "never-export" t)
  596. (check-eval "no-export" t)
  597. (let ((org-babel-exp-reference-buffer (current-buffer)))
  598. (check-eval "never" nil)
  599. (check-eval "no" nil)
  600. (check-eval "never-export" nil)
  601. (check-eval "no-export" nil))))
  602. (ert-deftest test-ob/noweb-expansion-1 ()
  603. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  604. <<foo>>
  605. #+end_src
  606. #+name: foo
  607. #+begin_src sh
  608. bar
  609. #+end_src"
  610. (should (string= (org-babel-expand-noweb-references) "bar"))))
  611. (ert-deftest test-ob/noweb-expansion-2 ()
  612. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  613. <<foo>>
  614. #+end_src
  615. #+name: foo
  616. #+begin_src sh :noweb-sep \"\"
  617. bar
  618. #+end_src
  619. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  620. baz
  621. #+end_src"
  622. (should (string= (org-babel-expand-noweb-references) "barbaz"))))
  623. (ert-deftest test-ob/splitting-variable-lists-in-references ()
  624. (org-test-with-temp-text ""
  625. (should (= 1 (length (org-babel-ref-split-args
  626. "a=\"this, no work\""))))
  627. (should (= 2 (length (org-babel-ref-split-args
  628. "a=\"this, no work\", b=1"))))))
  629. (ert-deftest test-ob/org-babel-balanced-split ()
  630. (should (equal
  631. '(":a 1" "b [2 3]" "c (4 :d (5 6))")
  632. (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
  633. '((32 9) . 58)))))
  634. (ert-deftest test-ob/commented-last-block-line-no-var ()
  635. (org-test-with-temp-text-in-file "
  636. #+begin_src emacs-lisp
  637. ;;
  638. #+end_src"
  639. (org-babel-next-src-block)
  640. (org-ctrl-c-ctrl-c)
  641. (should (re-search-forward "\\#\\+results:" nil t))
  642. (forward-line)
  643. (should
  644. (string=
  645. ""
  646. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  647. (org-test-with-temp-text-in-file "
  648. #+begin_src emacs-lisp
  649. \"some text\";;
  650. #+end_src"
  651. (org-babel-next-src-block)
  652. (org-ctrl-c-ctrl-c)
  653. (should (re-search-forward "\\#\\+results:" nil t))
  654. (forward-line)
  655. (should
  656. (string=
  657. ": some text"
  658. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  659. (ert-deftest test-ob/commented-last-block-line-with-var ()
  660. (org-test-with-temp-text-in-file "
  661. #+begin_src emacs-lisp :var a=1
  662. ;;
  663. #+end_src"
  664. (org-babel-next-src-block)
  665. (org-ctrl-c-ctrl-c)
  666. (re-search-forward "\\#\\+results:" nil t)
  667. (forward-line)
  668. (should (string=
  669. ""
  670. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  671. (org-test-with-temp-text-in-file "
  672. #+begin_src emacs-lisp :var a=2
  673. 2;;
  674. #+end_src"
  675. (org-babel-next-src-block)
  676. (org-ctrl-c-ctrl-c)
  677. (re-search-forward "\\#\\+results:" nil t)
  678. (forward-line)
  679. (should (string=
  680. ": 2"
  681. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  682. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  683. "Test helper function to test `org-babel-remove-result'.
  684. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  685. and the result of execution is verified against RESULT.
  686. The block is actually executed /twice/ to ensure result
  687. replacement happens correctly."
  688. (org-test-with-temp-text
  689. buffer-text
  690. (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
  691. (should (re-search-forward "\\#\\+results:" nil t))
  692. (forward-line)
  693. (should (string= result
  694. (buffer-substring-no-properties
  695. (point-at-bol)
  696. (- (point-max) 16))))
  697. (org-babel-previous-src-block) (org-babel-remove-result)
  698. (should (string= buffer-text
  699. (buffer-substring-no-properties
  700. (point-min) (point-max))))))
  701. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  702. "Test `org-babel-remove-result' with default :results."
  703. (mapcar (lambda (language)
  704. (test-ob-verify-result-and-removed-result
  705. "\n"
  706. (concat
  707. "* org-babel-remove-result
  708. #+begin_src " language "
  709. #+end_src
  710. * next heading")))
  711. '("sh" "emacs-lisp")))
  712. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  713. "Test `org-babel-remove-result' with :results list."
  714. (test-ob-verify-result-and-removed-result
  715. "- 1
  716. - 2
  717. - 3
  718. - (quote (4 5))"
  719. "* org-babel-remove-result
  720. #+begin_src emacs-lisp :results list
  721. '(1 2 3 '(4 5))
  722. #+end_src
  723. * next heading"))
  724. (ert-deftest test-ob/org-babel-results-indented-wrap ()
  725. "Ensure that wrapped results are inserted correction when indented.
  726. If not inserted correctly then the second evaluation will fail
  727. trying to find the :END: marker."
  728. (org-test-with-temp-text
  729. "- indented
  730. #+begin_src sh :results file wrap
  731. echo test.txt
  732. #+end_src"
  733. (org-babel-next-src-block 1)
  734. (org-babel-execute-src-block)
  735. (org-babel-execute-src-block)))
  736. (ert-deftest test-ob/file-desc-header-argument ()
  737. "Test that the :file-desc header argument is used."
  738. (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  739. \"foo\"
  740. #+end_src
  741. #+begin_src emacs-lisp :results file :file-desc
  742. \"foo\"
  743. #+end_src"
  744. (org-babel-execute-src-block)
  745. (org-babel-next-src-block 1)
  746. (org-babel-execute-src-block)
  747. (goto-char (point-min))
  748. (should (search-forward "[[file:foo][bar]]" nil t))
  749. (should (search-forward "[[file:foo][foo]]" nil t))))
  750. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  751. "Test `org-babel-remove-result' with :results pp."
  752. (test-ob-verify-result-and-removed-result
  753. ": \"I /am/ working!\""
  754. "* org-babel-remove-result
  755. #+begin_src emacs-lisp :results pp
  756. \"I /am/ working!\")
  757. #+end_src
  758. * next heading"))
  759. (ert-deftest test-org-babel/inline-src_blk-preceded-punct-preceded-by-point ()
  760. (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  761. (org-babel-inline-result-wrap "=%s="))
  762. (org-test-with-temp-text
  763. test-line
  764. (forward-char 1)
  765. (org-ctrl-c-ctrl-c)
  766. (should (re-search-forward "=\"x\"=" nil t))
  767. (forward-line))))
  768. (ert-deftest test-ob/commented-last-block-line-with-var ()
  769. (org-test-with-temp-text-in-file "
  770. #+begin_src emacs-lisp :var a=1
  771. ;;
  772. #+end_src"
  773. (org-babel-next-src-block)
  774. (org-ctrl-c-ctrl-c)
  775. (re-search-forward "\\#\\+results:" nil t)
  776. (forward-line)
  777. (should (string=
  778. ""
  779. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  780. (org-test-with-temp-text-in-file "
  781. #+begin_src emacs-lisp :var a=2
  782. 2;;
  783. #+end_src"
  784. (org-babel-next-src-block)
  785. (org-ctrl-c-ctrl-c)
  786. (re-search-forward "\\#\\+results:" nil t)
  787. (forward-line)
  788. (should (string=
  789. ": 2"
  790. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  791. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  792. "Test helper function to test `org-babel-remove-result'.
  793. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  794. and the result of execution is verified against RESULT.
  795. The block is actually executed /twice/ to ensure result
  796. replacement happens correctly."
  797. (org-test-with-temp-text
  798. buffer-text
  799. (org-babel-next-src-block) (org-ctrl-c-ctrl-c) (org-ctrl-c-ctrl-c)
  800. (should (re-search-forward "\\#\\+results:" nil t))
  801. (forward-line)
  802. (should (string= result
  803. (buffer-substring-no-properties
  804. (point-at-bol)
  805. (- (point-max) 16))))
  806. (org-babel-previous-src-block) (org-babel-remove-result)
  807. (should (string= buffer-text
  808. (buffer-substring-no-properties
  809. (point-min) (point-max))))))
  810. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  811. "Test `org-babel-remove-result' with default :results."
  812. (mapcar (lambda (language)
  813. (test-ob-verify-result-and-removed-result
  814. "\n"
  815. (concat
  816. "* org-babel-remove-result
  817. #+begin_src " language "
  818. #+end_src
  819. * next heading")))
  820. '("sh" "emacs-lisp")))
  821. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  822. "Test `org-babel-remove-result' with :results list."
  823. (test-ob-verify-result-and-removed-result
  824. "- 1
  825. - 2
  826. - 3
  827. - (quote (4 5))"
  828. "* org-babel-remove-result
  829. #+begin_src emacs-lisp :results list
  830. '(1 2 3 '(4 5))
  831. #+end_src
  832. * next heading"))
  833. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  834. "Test `org-babel-remove-result' with :results wrap."
  835. (test-ob-verify-result-and-removed-result
  836. ":RESULTS:
  837. hello there
  838. :END:"
  839. "* org-babel-remove-result
  840. #+begin_src emacs-lisp :results wrap
  841. \"hello there\"
  842. #+end_src
  843. * next heading"))
  844. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  845. "Test `org-babel-remove-result' with :results org."
  846. (test-ob-verify-result-and-removed-result
  847. "#+BEGIN_SRC org
  848. ,* heading
  849. ,** subheading
  850. content
  851. #+END_SRC"
  852. "* org-babel-remove-result
  853. #+begin_src emacs-lisp :results org
  854. \"* heading
  855. ,** subheading
  856. content\"
  857. #+end_src
  858. * next heading"))
  859. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  860. "Test `org-babel-remove-result' with :results html."
  861. (test-ob-verify-result-and-removed-result
  862. "#+BEGIN_HTML
  863. <head><body></body></head>
  864. #+END_HTML"
  865. "* org-babel-remove-result
  866. #+begin_src emacs-lisp :results html
  867. \"<head><body></body></head>\"
  868. #+end_src
  869. * next heading"))
  870. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  871. "Test `org-babel-remove-result' with :results latex."
  872. (test-ob-verify-result-and-removed-result
  873. "#+BEGIN_LaTeX
  874. Line 1
  875. Line 2
  876. Line 3
  877. #+END_LaTeX"
  878. "* org-babel-remove-result
  879. #+begin_src emacs-lisp :results latex
  880. \"Line 1
  881. Line 2
  882. Line 3\"
  883. #+end_src
  884. * next heading"))
  885. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  886. "Test `org-babel-remove-result' with :results code."
  887. (test-ob-verify-result-and-removed-result
  888. "#+BEGIN_SRC emacs-lisp
  889. \"I am working!\"
  890. #+END_SRC"
  891. "* org-babel-remove-result
  892. #+begin_src emacs-lisp :results code
  893. (message \"I am working!\")
  894. #+end_src
  895. * next heading"))
  896. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  897. "Test `org-babel-remove-result' with :results pp."
  898. (test-ob-verify-result-and-removed-result
  899. ": \"I /am/ working!\""
  900. "* org-babel-remove-result
  901. #+begin_src emacs-lisp :results pp
  902. \"I /am/ working!\")
  903. #+end_src
  904. * next heading"))
  905. (ert-deftest test-ob/results-do-not-replace-code-blocks ()
  906. (org-test-with-temp-text "Block two has a space after the name.
  907. #+name: foo
  908. #+begin_src emacs-lisp
  909. 1
  910. #+end_src
  911. #+name: foo
  912. #+begin_src emacs-lisp
  913. 2
  914. #+end_src
  915. #+name: foo
  916. #+begin_src emacs-lisp
  917. 3
  918. #+end_src
  919. #+RESULTS: foo
  920. : foo
  921. "
  922. (dolist (num '(1 2 3))
  923. ;; execute the block
  924. (goto-char (point-min))
  925. (org-babel-next-src-block num) (org-babel-execute-src-block)
  926. ;; check the results
  927. (goto-char (point-max))
  928. (move-beginning-of-line 0)
  929. (should (looking-at (format ": %d" num))))))
  930. (ert-deftest test-ob/blocks-with-spaces ()
  931. "Test expansion of blocks followed by blank lines."
  932. (should
  933. (equal "#+BEGIN_SRC emacs-lisp
  934. \(+ 1 2)
  935. #+END_SRC
  936. #+RESULTS:
  937. : 3\n\n\n"
  938. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
  939. \(+ 1 2)
  940. #+END_SRC\n\n\n"
  941. (let ((org-babel-next-src-block "RESULTS"))
  942. (org-babel-execute-src-block))
  943. (buffer-string)))))
  944. (ert-deftest test-ob/results-in-narrowed-buffer ()
  945. "Test block execution in a narrowed buffer."
  946. ;; If results don't exist, they should be inserted in visible part
  947. ;; of the buffer.
  948. (should
  949. (equal
  950. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
  951. (org-test-with-temp-text
  952. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  953. (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
  954. (let ((org-babel-results-keyword "RESULTS"))
  955. (org-babel-execute-src-block))
  956. (org-trim (buffer-string)))))
  957. (should
  958. (equal
  959. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
  960. (org-test-with-temp-text
  961. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  962. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  963. (let ((org-babel-results-keyword "RESULTS"))
  964. (org-babel-execute-src-block))
  965. (org-trim (buffer-string)))))
  966. ;; Results in visible part of buffer, should be updated here.
  967. (should
  968. (equal
  969. "#+NAME: test
  970. #+BEGIN_SRC emacs-lisp
  971. \(+ 1 2)
  972. #+END_SRC
  973. #+RESULTS: test
  974. : 3"
  975. (org-test-with-temp-text
  976. "#+NAME: test
  977. #+BEGIN_SRC emacs-lisp
  978. \(+ 1 2)
  979. #+END_SRC
  980. #+RESULTS: test
  981. : 4
  982. Paragraph"
  983. (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
  984. (let ((org-babel-results-keyword "RESULTS"))
  985. (org-babel-execute-src-block))
  986. (org-trim (buffer-string)))))
  987. ;; Results in invisible part of buffer, should be updated there.
  988. (org-test-with-temp-text
  989. "#+NAME: test
  990. #+BEGIN_SRC emacs-lisp
  991. \(+ 1 2)
  992. #+END_SRC
  993. #+RESULTS: test
  994. : 4
  995. Paragraph"
  996. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  997. (let ((org-babel-results-keyword "RESULTS"))
  998. (org-babel-execute-src-block))
  999. (should-not (re-search-forward "^#\\+RESULTS:" nil t))
  1000. (widen)
  1001. (should (should (re-search-forward "^: 3" nil t)))))
  1002. (ert-deftest test-ob/specific-colnames ()
  1003. "Test passing specific column names."
  1004. (should
  1005. (equal "#+name: input-table
  1006. | id | var1 |
  1007. |----+------|
  1008. | 1 | bar |
  1009. | 2 | baz |
  1010. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1011. echo \"$data\"
  1012. #+end_src
  1013. #+RESULTS:
  1014. | Rev | Author |
  1015. |-----+--------|
  1016. | 1 | bar |
  1017. | 2 | baz |
  1018. "
  1019. (org-test-with-temp-text
  1020. "#+name: input-table
  1021. | id | var1 |
  1022. |----+------|
  1023. | 1 | bar |
  1024. | 2 | baz |
  1025. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1026. echo \"$data\"
  1027. #+end_src
  1028. "
  1029. ;; we should find a code block
  1030. (should (re-search-forward org-babel-src-block-regexp nil t))
  1031. (goto-char (match-beginning 0))
  1032. ;; now that we've located the code block, it may be evaluated
  1033. (let ((org-babel-execute-src-block "RESULTS"))
  1034. (org-babel-execute-src-block))
  1035. (buffer-string)))))
  1036. (ert-deftest test-ob/location-of-header-arg-eval ()
  1037. "Test location of header argument evaluation."
  1038. (org-test-with-temp-text "
  1039. #+name: top-block
  1040. #+begin_src emacs-lisp :var pt=(point)
  1041. pt
  1042. #+end_src
  1043. #+name: bottom-block
  1044. #+begin_src emacs-lisp :var pt=top-block()
  1045. pt
  1046. #+end_src
  1047. "
  1048. ;; the value of the second block should be greater than the first
  1049. (should
  1050. (< (progn (re-search-forward org-babel-src-block-regexp nil t)
  1051. (goto-char (match-beginning 0))
  1052. (prog1 (save-match-data (org-babel-execute-src-block))
  1053. (goto-char (match-end 0))))
  1054. (progn (re-search-forward org-babel-src-block-regexp nil t)
  1055. (goto-char (match-beginning 0))
  1056. (org-babel-execute-src-block))))))
  1057. (ert-deftest test-ob/preserve-results-indentation ()
  1058. "Preserve indentation when executing a src block."
  1059. (should
  1060. (equal '(2 2)
  1061. (org-test-with-temp-text
  1062. " #+begin_src emacs-lisp\n (+ 1 1)\n #+end_src"
  1063. (org-babel-execute-src-block)
  1064. (buffer-string)
  1065. (let ((case-fold-search t)) (search-forward "#+results:"))
  1066. ;; Check if both #+RESULTS: keyword and actual results are
  1067. ;; indented by 2 columns.
  1068. (list (org-get-indentation)
  1069. (progn (forward-line) (org-get-indentation)))))))
  1070. (ert-deftest test-ob/safe-header-args ()
  1071. "Detect safe and unsafe header args."
  1072. (let ((safe-args '((:cache . "foo")
  1073. (:results . "output")
  1074. (:eval . "never")
  1075. (:eval . "query")))
  1076. (unsafe-args '((:eval . "yes")
  1077. (:results . "output file")
  1078. (:foo . "bar")))
  1079. (malformed-args '((bar . "foo")
  1080. ("foo" . "bar")
  1081. :foo))
  1082. (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
  1083. (dolist (arg safe-args)
  1084. (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
  1085. (dolist (arg unsafe-args)
  1086. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1087. (dolist (arg malformed-args)
  1088. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1089. (should (not (funcall safe-p (append safe-args unsafe-args))))))
  1090. (ert-deftest test-ob/noweb-expansions-in-cache ()
  1091. "Ensure that noweb expansions are expanded before caching."
  1092. (let ((noweb-expansions-in-cache-var 0))
  1093. (org-test-with-temp-text "
  1094. #+name: foo
  1095. #+begin_src emacs-lisp
  1096. \"I said\"
  1097. #+end_src
  1098. #+name: bar
  1099. #+begin_src emacs-lisp :noweb yes :cache yes
  1100. (setq noweb-expansions-in-cache-var
  1101. (+ 1 noweb-expansions-in-cache-var))
  1102. (concat <<foo>> \" check noweb expansions\")
  1103. #+end_src
  1104. "
  1105. ;; run the second block to create the cache
  1106. (goto-char (point-min))
  1107. (re-search-forward (regexp-quote "#+name: bar"))
  1108. (should (string= "I said check noweb expansions"
  1109. (org-babel-execute-src-block)))
  1110. (should (= noweb-expansions-in-cache-var 1))
  1111. ;; change the value of the first block
  1112. (goto-char (point-min))
  1113. (re-search-forward (regexp-quote "said"))
  1114. (goto-char (match-beginning 0))
  1115. (insert "haven't ")
  1116. (re-search-forward (regexp-quote "#+name: bar"))
  1117. (should (string= "I haven't said check noweb expansions"
  1118. (org-babel-execute-src-block)))
  1119. (should (= noweb-expansions-in-cache-var 2)))))
  1120. (ert-deftest test-org-babel/file-ext-and-output-dir ()
  1121. (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
  1122. (org-babel-next-src-block)
  1123. (should (equal "file-ext-basic.txt"
  1124. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1125. (org-babel-next-src-block)
  1126. (should (equal "foo/file-ext-dir-relative.txt"
  1127. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1128. (org-babel-next-src-block)
  1129. (should (equal "foo/file-ext-dir-relative-slash.txt"
  1130. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1131. (org-babel-next-src-block)
  1132. (should (equal "/tmp/file-ext-dir-absolute.txt"
  1133. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1134. (org-babel-next-src-block)
  1135. (should (equal "foo.bar"
  1136. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1137. (org-babel-next-src-block)
  1138. (should (equal "xxx/foo.bar"
  1139. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1140. ))
  1141. (provide 'test-ob)
  1142. ;;; test-ob ends here