test-ob.el 46 KB

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