test-ob.el 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  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-ob/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= (expand-file-name "test.txt")
  30. (org-babel-execute-src-block))))))
  31. (ert-deftest test-ob/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-ob/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-ob/default-inline-header-args ()
  80. (should(equal
  81. '((:session . "none")
  82. (:results . "replace")
  83. (:exports . "results")
  84. (:hlines . "yes"))
  85. org-babel-default-inline-header-args)))
  86. (ert-deftest ob-test/org-babel-combine-header-arg-lists ()
  87. (let ((results (org-babel-combine-header-arg-lists
  88. '((foo . :any)
  89. (bar)
  90. (baz . ((foo bar) (baz)))
  91. (qux . ((foo bar baz qux)))
  92. (quux . ((foo bar))))
  93. '((bar)
  94. (baz . ((baz)))
  95. (quux . :any)))))
  96. (dolist (pair '((foo . :any)
  97. (bar)
  98. (baz . ((baz)))
  99. (quux . :any)
  100. (qux . ((foo bar baz qux)))))
  101. (should (equal (cdr pair)
  102. (cdr (assoc (car pair) results)))))))
  103. ;;; ob-get-src-block-info
  104. (ert-deftest test-ob/get-src-block-info-language ()
  105. (org-test-at-marker nil org-test-file-ob-anchor
  106. (let ((info (org-babel-get-src-block-info)))
  107. (should (string= "emacs-lisp" (nth 0 info))))))
  108. (ert-deftest test-ob/get-src-block-info-body ()
  109. (org-test-at-marker nil org-test-file-ob-anchor
  110. (let ((info (org-babel-get-src-block-info)))
  111. (should (string-match (regexp-quote org-test-file-ob-anchor)
  112. (nth 1 info))))))
  113. (ert-deftest test-ob/get-src-block-info-tangle ()
  114. (org-test-at-marker nil org-test-file-ob-anchor
  115. (let ((info (org-babel-get-src-block-info)))
  116. (should (string= "no" (cdr (assq :tangle (nth 2 info))))))))
  117. (ert-deftest test-ob/elisp-in-header-arguments ()
  118. "Test execution of elisp forms in header arguments."
  119. (org-test-with-temp-text-in-file "
  120. * elisp forms in header arguments
  121. :PROPERTIES:
  122. :header-args: :var prop = (* 7 6)
  123. :END:
  124. #+begin_src emacs-lisp
  125. prop
  126. #+end_src"
  127. (goto-char (point-min))
  128. (org-babel-next-src-block)
  129. (should (= 42 (org-babel-execute-src-block)))))
  130. (ert-deftest test-ob/simple-named-code-block ()
  131. "Test that simple named code blocks can be evaluated."
  132. (org-test-with-temp-text-in-file "
  133. #+name: i-have-a-name
  134. #+begin_src emacs-lisp
  135. 42
  136. #+end_src"
  137. (org-babel-next-src-block 1)
  138. (should (= 42 (org-babel-execute-src-block)))))
  139. (ert-deftest test-ob/simple-variable-resolution ()
  140. "Test that simple variable resolution is working."
  141. (org-test-with-temp-text-in-file "
  142. #+name: four
  143. #+begin_src emacs-lisp
  144. (list 1 2 3 4)
  145. #+end_src
  146. #+begin_src emacs-lisp :var four=four
  147. (length four)
  148. #+end_src"
  149. (org-babel-next-src-block 2)
  150. (should (= 4 (org-babel-execute-src-block)))
  151. (forward-line 5)
  152. (should (string= ": 4" (buffer-substring
  153. (point-at-bol)
  154. (point-at-eol))))))
  155. (ert-deftest test-ob/multi-line-header-arguments ()
  156. "Test that multi-line header arguments and can be read."
  157. (org-test-with-temp-text-in-file "
  158. #+headers: :var letters='(a b c d e f g)
  159. #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
  160. (require 'cl-lib)
  161. (cl-map 'list #'list numbers letters)
  162. #+end_src"
  163. (org-babel-next-src-block)
  164. (let ((results (org-babel-execute-src-block)))
  165. (should (eq 'a (cadr (assoc 1 results))))
  166. (should (eq 'd (cadr (assoc 4 results)))))))
  167. (ert-deftest test-ob/parse-header-args ()
  168. (org-test-with-temp-text-in-file "
  169. #+begin_src example-lang :session :results output :var num=9
  170. the body
  171. #+end_src"
  172. (org-babel-next-src-block)
  173. (let* ((info (org-babel-get-src-block-info))
  174. (params (nth 2 info)))
  175. (message "%S" params)
  176. (should (equal "example-lang" (nth 0 info)))
  177. (should (string= "the body" (org-trim (nth 1 info))))
  178. (should-not (member '(:session\ \ \ \ ) params))
  179. (should (equal '(:session) (assq :session params)))
  180. (should (equal '(:result-type . output) (assq :result-type params)))
  181. (should (equal '(num . 9) (cdr (assq :var params)))))))
  182. (ert-deftest test-ob/parse-header-args2 ()
  183. (org-test-with-temp-text-in-file "
  184. * resolving sub-trees as references
  185. #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
  186. (length text)
  187. #+end_src
  188. #+begin_src org :noweb yes
  189. <<simple-subtree>>
  190. <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
  191. #+end_src
  192. ** simple subtree with custom ID
  193. :PROPERTIES:
  194. :CUSTOM_ID: simple-subtree
  195. :END:
  196. this is simple"
  197. (should (string-match (regexp-quote "this is simple")
  198. (org-babel-ref-resolve "simple-subtree")))
  199. (org-babel-next-src-block)
  200. (should (= 14 (org-babel-execute-src-block)))))
  201. (ert-deftest test-ob/inline-src-blocks ()
  202. (should
  203. (= 1
  204. (org-test-with-temp-text
  205. "In the middle <point>src_emacs-lisp{(+ 0 1)} of a line"
  206. (org-babel-execute-src-block))))
  207. (should
  208. (= 2
  209. (org-test-with-temp-text
  210. "One at the end of a line: <point>src_emacs-lisp{(+ 1 1)}"
  211. (org-babel-execute-src-block))))
  212. (should
  213. (= 3
  214. (org-test-with-temp-text
  215. "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
  216. (org-babel-execute-src-block))))
  217. (should
  218. (= 4
  219. (org-test-with-temp-text
  220. "In the middle <point>src_emacs-lisp[:results silent\
  221. :exports code]{(+ 3 1)} of a line"
  222. (org-babel-execute-src-block))))
  223. (should
  224. (= 5
  225. (org-test-with-temp-text
  226. "One at the end of a line: <point>src_emacs-lisp[:results silent\
  227. :exports code]{(+ 4 1)}"
  228. (org-babel-execute-src-block))))
  229. (should
  230. (= 6
  231. (org-test-with-temp-text
  232. "src_emacs-lisp[:results silent :exports code]{(+ 5 1)}\
  233. at the beginning of a line."
  234. (org-babel-execute-src-block))))
  235. (should
  236. (= 7
  237. (org-test-with-temp-text
  238. "One also evaluated: <point>src_emacs-lisp[:exports both\
  239. :results silent]{(+ 6 1)}"
  240. (org-babel-execute-src-block)))))
  241. (ert-deftest test-ob/inline-src_blk-default-results-replace-line-1 ()
  242. (let ((test-line "src_sh{echo 1}")
  243. (org-babel-inline-result-wrap "=%s="))
  244. ;; src_ at bol line 1...
  245. (org-test-with-temp-text
  246. test-line
  247. (goto-char (point-min)) (org-babel-execute-maybe)
  248. (should (string=
  249. (concat test-line " {{{results(=1=)}}}")
  250. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  251. (forward-char) (org-babel-execute-maybe)
  252. (should (string=
  253. (concat test-line " {{{results(=1=)}}}")
  254. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  255. (re-search-forward "{{{")
  256. ;;(should-error (org-ctrl-c-ctrl-c))
  257. (backward-char 4) ;; last char of block body
  258. (org-babel-execute-maybe)
  259. (should (string=
  260. (concat test-line " {{{results(=1=)}}}")
  261. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  262. ;; src_ follows space line 1...
  263. (let ((test-line " src_emacs-lisp{ 1 }"))
  264. (org-test-with-temp-text
  265. test-line
  266. (should-error (org-ctrl-c-ctrl-c))
  267. (forward-char) (org-babel-execute-maybe)
  268. (should (string=
  269. (concat test-line " {{{results(=1=)}}}")
  270. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  271. (re-search-forward "{ 1 ") (org-babel-execute-maybe)
  272. (should (string=
  273. (concat test-line " {{{results(=1=)}}}")
  274. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  275. (forward-char 6)
  276. (should-error (org-ctrl-c-ctrl-c))))
  277. ;; Results on a subsequent line are replaced.
  278. (should
  279. (equal
  280. "src_emacs-lisp{(+ 1 2)}\n {{{results(=3=)}}}"
  281. (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n {{{results(=2=)}}}"
  282. (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
  283. (buffer-string))))
  284. ;; Also handle results at the beginning of a line.
  285. (should
  286. (equal
  287. "src_emacs-lisp{(+ 1 2)}\n{{{results(=3=)}}}"
  288. (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n{{{results(=2=)}}}"
  289. (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
  290. (buffer-string))))))
  291. (ert-deftest test-ob/inline-src_blk-default-results-replace-line-2 ()
  292. ;; src_ at bol line 2...
  293. (let ((test-line " src_emacs-lisp{ \"x\" }")
  294. (org-babel-inline-result-wrap "=%s="))
  295. (org-test-with-temp-text
  296. (concat "\n" test-line)
  297. (should-error (org-ctrl-c-ctrl-c))
  298. (goto-char (point-min))
  299. (should-error (org-ctrl-c-ctrl-c))
  300. (forward-line)
  301. (should-error (org-ctrl-c-ctrl-c))
  302. (forward-char) (org-babel-execute-maybe)
  303. (should (string=
  304. (concat test-line " {{{results(=x=)}}}")
  305. (buffer-substring-no-properties
  306. (point-at-bol) (point-at-eol))))))
  307. (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
  308. (org-babel-inline-result-wrap "=%s="))
  309. (org-test-with-temp-text
  310. test-line
  311. (goto-char (point-max))
  312. (insert (concat "\n" test-line " end"))
  313. (re-search-backward "src") (org-babel-execute-maybe)
  314. (should (string=
  315. (concat test-line " {{{results(=y=)}}} end")
  316. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  317. (re-search-forward "\" ") (org-babel-execute-maybe)
  318. (should (string=
  319. (concat test-line " {{{results(=y=)}}} end")
  320. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  321. (forward-char 3)
  322. (should-error (org-ctrl-c-ctrl-c)))))
  323. (ert-deftest test-ob/inline-src_blk-manual-results-replace ()
  324. (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
  325. (org-babel-inline-result-wrap "=%s="))
  326. (org-test-with-temp-text
  327. (concat "\n" test-line)
  328. (should-error (org-ctrl-c-ctrl-c))
  329. (goto-char (point-max))
  330. (org-babel-execute-maybe)
  331. (beginning-of-line)
  332. (should-error (org-ctrl-c-ctrl-c))
  333. (forward-char) (org-babel-execute-maybe)
  334. (should (string=
  335. (concat test-line " {{{results(=x=)}}}")
  336. (buffer-substring-no-properties
  337. (point-at-bol) (point-at-eol))))))
  338. (let ((test-line (concat " Some text prior to block "
  339. "src_emacs-lisp[:results replace]{ \"y\" }"))
  340. (org-babel-inline-result-wrap "=%s="))
  341. (org-test-with-temp-text test-line
  342. (goto-char (point-max))
  343. (insert (concat "\n" test-line " end"))
  344. (re-search-backward "src") (org-babel-execute-maybe)
  345. (should (string=
  346. (concat test-line " {{{results(=y=)}}} end")
  347. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  348. (re-search-forward "\" ") (org-babel-execute-maybe)
  349. (should (string=
  350. (concat test-line " {{{results(=y=)}}} end")
  351. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  352. (forward-char 3)
  353. (should-error (org-ctrl-c-ctrl-c)))))
  354. (ert-deftest test-ob/inline-src_blk-results-silent ()
  355. (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
  356. (org-test-with-temp-text test-line
  357. (org-babel-execute-maybe)
  358. (should (string= test-line
  359. (buffer-substring-no-properties
  360. (point-at-bol) (point-at-eol))))))
  361. (let ((test-line (concat " Some text prior to block src_emacs-lisp"
  362. "[ :results silent ]{ \"y\" }")))
  363. (org-test-with-temp-text
  364. test-line
  365. (goto-char (point-max))
  366. (insert (concat "\n" test-line " end"))
  367. (re-search-backward "src_") (org-babel-execute-maybe)
  368. (should (string= (concat test-line " end")
  369. (buffer-substring-no-properties
  370. (point-at-bol) (point-at-eol))))
  371. (re-search-forward "\" ") (org-babel-execute-maybe)
  372. (should (string= (concat test-line " end")
  373. (buffer-substring-no-properties
  374. (point-at-bol) (point-at-eol))))
  375. (forward-char 2)
  376. (should-error (org-ctrl-c-ctrl-c)))))
  377. (ert-deftest test-ob/inline-src_blk-results-raw ()
  378. (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
  379. (org-test-with-temp-text test-line
  380. (org-babel-execute-maybe)
  381. (should (string= (concat test-line " x")
  382. (buffer-string)))))
  383. (let ((test-line (concat " Some text prior to block "
  384. "src_emacs-lisp[ :results raw ]{ \"the\" }")))
  385. (org-test-with-temp-text (concat test-line " end")
  386. (re-search-forward "src_") (org-babel-execute-maybe)
  387. (should (string= (concat test-line " the end")
  388. (buffer-substring-no-properties
  389. (point-at-bol) (point-at-eol))))
  390. (re-search-forward "\" ") (org-babel-execute-maybe)
  391. (should (string= (concat test-line " the the 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-ob/inline-src_blk-results-file ()
  397. (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
  398. (org-test-with-temp-text
  399. test-line
  400. (org-babel-execute-maybe)
  401. (should (string= (concat test-line " {{{results([[file:~/test-file]])}}}")
  402. (buffer-substring-no-properties
  403. (point-min) (point-max)))))))
  404. (ert-deftest test-ob/inline-src_blk-results-scalar ()
  405. (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
  406. (org-babel-inline-result-wrap "=%s="))
  407. (org-test-with-temp-text
  408. test-line
  409. (org-babel-execute-maybe)
  410. (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
  411. (buffer-substring-no-properties
  412. (point-min) (point-max)))))))
  413. (ert-deftest test-ob/inline-src_blk-results-verbatim ()
  414. (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  415. (org-babel-inline-result-wrap "=%s="))
  416. (org-test-with-temp-text
  417. test-line
  418. (org-babel-execute-maybe)
  419. (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
  420. (buffer-substring-no-properties
  421. (point-min) (point-max)))))))
  422. (ert-deftest test-ob/combining-scalar-and-raw-result-types ()
  423. (org-test-with-temp-text-in-file "
  424. #+begin_src sh :results scalar
  425. echo \"[[file:./cv.cls]]\"
  426. #+end_src
  427. #+name:
  428. : [[file:./cv.cls]]
  429. #+begin_src sh :results raw scalar
  430. echo \"[[file:./cv.cls]]\"
  431. #+end_src
  432. "
  433. (cl-flet ((next-result ()
  434. (org-babel-next-src-block)
  435. (org-babel-execute-src-block)
  436. (goto-char (org-babel-where-is-src-block-result))
  437. (forward-line 1)))
  438. (goto-char (point-min))
  439. (next-result)
  440. (should (eq (org-element-type (org-element-at-point)) 'fixed-width))
  441. (next-result)
  442. (should-not
  443. (eq (org-element-type (org-element-at-point)) 'fixed-width)))))
  444. (ert-deftest test-ob/no-defaut-value-for-var ()
  445. "Test that the absence of a default value for a variable DOES THROW
  446. a proper error."
  447. (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
  448. (org-babel-next-src-block)
  449. (let ((err
  450. (should-error (org-babel-execute-src-block) :type 'error)))
  451. (should
  452. (equal
  453. '(error
  454. "Variable \"x\" must be assigned a default value")
  455. err)))))
  456. (ert-deftest test-ob/just-one-results-block ()
  457. "Test that evaluating two times the same code block does not result in a
  458. duplicate results block."
  459. (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
  460. (org-babel-execute-src-block)
  461. (org-babel-execute-src-block) ; second code block execution
  462. (should (search-forward "Hello")) ; the string inside the source code block
  463. (should (search-forward "Hello")) ; the same string in the results block
  464. (should-error (search-forward "Hello"))))
  465. (ert-deftest test-ob/nested-code-block ()
  466. "Test nested code blocks inside code blocks don't cause problems."
  467. (should
  468. (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
  469. (org-test-with-temp-text "#+begin_src org :results silent
  470. ,#+begin_src emacs-lisp
  471. 'foo
  472. ,#+end_src
  473. #+end_src"
  474. (let ((org-edit-src-content-indentation 2)
  475. (org-src-preserve-indentation nil))
  476. (org-babel-execute-src-block))))))
  477. (ert-deftest test-ob/partial-nested-code-block ()
  478. "Test nested code blocks inside code blocks don't cause problems."
  479. (org-test-with-temp-text "#+begin_src org :results silent
  480. ,#+begin_src emacs-lisp
  481. #+end_src"
  482. (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
  483. (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
  484. (org-test-with-temp-text "#+NAME: foo
  485. #+BEGIN_SRC emacs-lisp
  486. 'foo
  487. #+END_SRC\n"
  488. (org-babel-next-src-block 1)
  489. (should (eq 'foo (org-babel-execute-src-block)))
  490. (goto-char (point-min))
  491. (org-babel-next-src-block 1)
  492. (should (looking-at org-babel-src-block-regexp))))
  493. (ert-deftest test-ob/catches-all-references ()
  494. (org-test-with-temp-text "
  495. #+NAME: literal-example
  496. #+BEGIN_EXAMPLE
  497. A literal example
  498. on two lines
  499. #+END_EXAMPLE
  500. #+NAME: read-literal-example
  501. #+BEGIN_SRC emacs-lisp :var x=literal-example
  502. (concatenate 'string x \" for me.\")
  503. #+END_SRC"
  504. (org-babel-next-src-block 1)
  505. (should (string= (org-babel-execute-src-block)
  506. "A literal example\non two lines\n for me."))))
  507. (ert-deftest test-ob/ignore-reference-in-commented-headings ()
  508. (should
  509. (= 2
  510. (org-test-with-temp-text
  511. "
  512. * COMMENT H1
  513. #+NAME: n
  514. : 1
  515. * H2
  516. #+NAME: n
  517. : 2
  518. * Code
  519. <point>#+BEGIN_SRC emacs-lisp :var x=n
  520. x
  521. #+END_SRC"
  522. (org-babel-execute-src-block)))))
  523. (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
  524. (org-test-with-temp-text "
  525. #+name: base_plus
  526. | 1 |
  527. | 2 |
  528. #+name: base
  529. | 3 |
  530. | 4 |
  531. #+begin_src emacs-lisp :var x=base
  532. x
  533. #+end_src"
  534. (org-babel-next-src-block 1)
  535. (should (equal (org-babel-execute-src-block) '((3) (4))))))
  536. (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
  537. (org-test-with-temp-text "
  538. #+name: base_plus
  539. #+begin_src emacs-lisp
  540. 'bar
  541. #+end_src
  542. #+name: base
  543. #+begin_src emacs-lisp
  544. 'foo
  545. #+end_src
  546. #+begin_src emacs-lisp :var x=base
  547. x
  548. #+end_src"
  549. (org-babel-next-src-block 3)
  550. (should (equal (org-babel-execute-src-block) "foo"))))
  551. (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
  552. (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
  553. (+ a b c d)
  554. #+end_src
  555. "
  556. (should (= 10 (org-babel-execute-src-block)))))
  557. (ert-deftest test-ob/org-babel-update-intermediate ()
  558. (org-test-with-temp-text "#+name: foo
  559. #+begin_src emacs-lisp
  560. 2
  561. #+end_src
  562. #+results: foo
  563. : 4
  564. #+begin_src emacs-lisp :var it=foo
  565. (+ it 1)
  566. #+end_src"
  567. (let ((org-babel-update-intermediate nil))
  568. (goto-char (point-min))
  569. (org-babel-next-src-block 2)
  570. (should (= 3 (org-babel-execute-src-block)))
  571. (goto-char (point-min))
  572. (forward-line 6)
  573. (should (looking-at ": 4")))
  574. (let ((org-babel-update-intermediate t))
  575. (goto-char (point-min))
  576. (org-babel-next-src-block 2)
  577. (should (= 3 (org-babel-execute-src-block)))
  578. (goto-char (point-min))
  579. (forward-line 6)
  580. (should (looking-at ": 2")))))
  581. (ert-deftest test-ob/eval-header-argument ()
  582. (cl-flet ((check-eval (eval runp)
  583. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  584. (setq foo :evald)
  585. #+end_src" eval)
  586. (let ((foo :not-run))
  587. (if runp
  588. (progn (should (org-babel-execute-src-block))
  589. (should (eq foo :evald)))
  590. (progn (should-not (org-babel-execute-src-block))
  591. (should-not (eq foo :evald))))))))
  592. (check-eval "never" nil)
  593. (check-eval "no" nil)
  594. (check-eval "never-export" t)
  595. (check-eval "no-export" t)
  596. (let ((org-babel-exp-reference-buffer (current-buffer)))
  597. (check-eval "never" nil)
  598. (check-eval "no" nil)
  599. (check-eval "never-export" nil)
  600. (check-eval "no-export" nil))))
  601. (ert-deftest test-ob/noweb-expansion ()
  602. ;; Standard test.
  603. (should
  604. (string=
  605. "bar"
  606. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  607. <<foo>>
  608. #+end_src
  609. #+name: foo
  610. #+begin_src sh
  611. bar
  612. #+end_src"
  613. (org-babel-expand-noweb-references))))
  614. ;; Handle :noweb-sep.
  615. (should
  616. (string=
  617. "barbaz"
  618. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  619. <<foo>>
  620. #+end_src
  621. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  622. bar
  623. #+end_src
  624. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  625. baz
  626. #+end_src"
  627. (org-babel-expand-noweb-references))))
  628. ;; :noweb-ref is extracted from definition, not point of call.
  629. (should
  630. (string=
  631. "(+ 1 1)"
  632. (org-test-with-temp-text
  633. "
  634. * Call
  635. :PROPERTIES:
  636. :header-args: :noweb-ref bar
  637. :END:
  638. <point>#+begin_src emacs-lisp :results output :tangle yes
  639. <<foo>>
  640. #+end_src
  641. * Evaluation
  642. :PROPERTIES:
  643. :header-args: :noweb-ref foo
  644. :END:
  645. #+begin_src sh :noweb-sep \"\"
  646. (+ 1 1)
  647. #+end_src"
  648. (org-babel-expand-noweb-references)))))
  649. (ert-deftest test-ob/splitting-variable-lists-in-references ()
  650. (org-test-with-temp-text ""
  651. (should (= 1 (length (org-babel-ref-split-args
  652. "a=\"this, no work\""))))
  653. (should (= 2 (length (org-babel-ref-split-args
  654. "a=\"this, no work\", b=1"))))))
  655. (ert-deftest test-ob/balanced-split ()
  656. "Test `org-babel-balanced-split' specifications."
  657. (should (equal
  658. '(":a 1" "b [2 3]" "c (4 :d (5 6))")
  659. (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
  660. '((32 9) . 58))))
  661. ;; Handle un-balanced parens.
  662. (should
  663. (equal '(":foo ((6)" "bar 1")
  664. (org-babel-balanced-split ":foo ((6) :bar 1" '((32 9) . 58))))
  665. (should
  666. (equal '(":foo \"(foo\"" "bar 2")
  667. (org-babel-balanced-split ":foo \"(foo\" :bar 2" '((32 9) . 58))))
  668. ;; Handle un-balanced quotes.
  669. (should
  670. (equal '(":foo \"1" "bar 3")
  671. (org-babel-balanced-split ":foo \"1 :bar 3" '((32 9) . 58))))
  672. ;; Handle empty string.
  673. (should
  674. (equal '(":foo \"\"")
  675. (org-babel-balanced-split ":foo \"\"" '((32 9) . 58))))
  676. ;; Handle control characters within double quotes.
  677. (should
  678. (equal '(":foo \"\\n\"")
  679. (org-babel-balanced-split ":foo \"\\n\"" '((32 9) . 58)))))
  680. (ert-deftest test-ob/commented-last-block-line-no-var ()
  681. (org-test-with-temp-text-in-file "
  682. #+begin_src emacs-lisp
  683. ;;
  684. #+end_src"
  685. (org-babel-next-src-block)
  686. (org-babel-execute-maybe)
  687. (should (re-search-forward "\\#\\+results:" nil t))
  688. (forward-line)
  689. (should
  690. (string=
  691. ""
  692. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  693. (org-test-with-temp-text-in-file "
  694. #+begin_src emacs-lisp
  695. \"some text\";;
  696. #+end_src"
  697. (org-babel-next-src-block)
  698. (org-babel-execute-maybe)
  699. (should (re-search-forward "\\#\\+results:" nil t))
  700. (forward-line)
  701. (should
  702. (string=
  703. ": some text"
  704. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  705. (ert-deftest test-ob/commented-last-block-line-with-var ()
  706. (org-test-with-temp-text-in-file "
  707. #+begin_src emacs-lisp :var a=1
  708. ;;
  709. #+end_src"
  710. (org-babel-next-src-block)
  711. (org-babel-execute-maybe)
  712. (re-search-forward "\\#\\+results:" nil t)
  713. (forward-line)
  714. (should (string=
  715. ""
  716. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  717. (org-test-with-temp-text-in-file "
  718. #+begin_src emacs-lisp :var a=2
  719. 2;;
  720. #+end_src"
  721. (org-babel-next-src-block)
  722. (org-babel-execute-maybe)
  723. (re-search-forward "\\#\\+results:" nil t)
  724. (forward-line)
  725. (should (string=
  726. ": 2"
  727. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  728. (ert-deftest test-ob/org-babel-insert-result ()
  729. "Test `org-babel-insert-result' specifications."
  730. ;; Do not error when output is an improper list.
  731. (should
  732. (org-test-with-temp-text
  733. "
  734. <point>#+BEGIN_SRC emacs-lisp
  735. '((1 . nil) (2 . 3))
  736. #+END_SRC
  737. "
  738. (org-babel-execute-maybe) t))
  739. ;; Escape headlines when producing an example block.
  740. (should
  741. (string-match-p
  742. ",\\* Not an headline"
  743. (org-test-with-temp-text
  744. "
  745. <point>#+BEGIN_SRC emacs-lisp
  746. \"* Not an headline\"
  747. #+END_SRC
  748. "
  749. (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
  750. (buffer-string))))
  751. ;; Escape special syntax in example blocks.
  752. (should
  753. (string-match-p
  754. ",#\\+END_SRC"
  755. (org-test-with-temp-text
  756. "
  757. <point>#+BEGIN_SRC emacs-lisp
  758. \"#+END_SRC\"
  759. #+END_SRC
  760. "
  761. (let ((org-babel-min-lines-for-block-output 1)) (org-babel-execute-maybe))
  762. (buffer-string))))
  763. ;; No escaping is done with other blocks or raw type.
  764. (should-not
  765. (string-match-p
  766. ",\\* Not an headline"
  767. (org-test-with-temp-text
  768. "
  769. <point>#+BEGIN_SRC emacs-lisp
  770. \"* Not an headline\"
  771. #+END_SRC
  772. "
  773. (let ((org-babel-min-lines-for-block-output 10))
  774. (org-babel-execute-maybe))
  775. (buffer-string))))
  776. (should-not
  777. (string-match-p
  778. ",\\* Not an headline"
  779. (org-test-with-temp-text
  780. "
  781. <point>#+BEGIN_SRC emacs-lisp :results raw
  782. \"* Not an headline\"
  783. #+END_SRC
  784. "
  785. (org-babel-execute-maybe)
  786. (buffer-string))))
  787. (should-not
  788. (string-match-p
  789. ",\\* Not an headline"
  790. (org-test-with-temp-text
  791. "
  792. <point>#+BEGIN_SRC emacs-lisp :results drawer
  793. \"* Not an headline\"
  794. #+END_SRC
  795. "
  796. (org-babel-execute-maybe)
  797. (buffer-string)))))
  798. (ert-deftest test-ob/remove-inline-result ()
  799. "Test `org-babel-remove-inline-result' honors whitespace."
  800. (let*
  801. ((inline-sb "src_emacs-lisp{(+ 1 2)}")
  802. (inline-res " {{{results(=3=)}}}")
  803. (inline-sb-dot (concat inline-sb "."))
  804. (inline-sb-res-dot (concat inline-sb inline-res ".")))
  805. (org-test-with-temp-text
  806. ;; Insert inline_src_block followed by dot.
  807. inline-sb-dot
  808. ;; Insert result before dot.
  809. (org-babel-execute-maybe)
  810. (should (string= inline-sb-res-dot
  811. (buffer-substring-no-properties
  812. (point-at-bol) (point-at-eol))))
  813. ;; Delete whitespace and result.
  814. (org-babel-remove-inline-result)
  815. (should (string= inline-sb-dot
  816. (buffer-substring-no-properties
  817. (point-at-bol) (point-at-eol))))
  818. ;; Add whitespace and result before dot.
  819. (search-forward inline-sb)
  820. (insert " " inline-res)
  821. (goto-char (point-at-bol))
  822. ;; Remove whitespace and result.
  823. (org-babel-remove-inline-result)
  824. (should (string= inline-sb-dot
  825. (buffer-substring-no-properties
  826. (point-at-bol) (point-at-eol))))
  827. ;; Add whitespace before dot.
  828. (search-forward inline-sb)
  829. (insert " ")
  830. (goto-char (point-at-bol))
  831. ;; Add result before whitespace.
  832. (org-babel-execute-maybe)
  833. ;; Remove result - leave trailing whitespace and dot.
  834. (org-babel-remove-inline-result)
  835. (should (string= (concat inline-sb " .")
  836. (buffer-substring-no-properties
  837. (point-at-bol) (point-at-eol)))))))
  838. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  839. "Test `org-babel-remove-result' with default :results."
  840. (mapcar (lambda (language)
  841. (test-ob-verify-result-and-removed-result
  842. "\n"
  843. (concat
  844. "* org-babel-remove-result
  845. #+begin_src " language "
  846. #+end_src
  847. * next heading")))
  848. '("sh" "emacs-lisp")))
  849. (ert-deftest test-ob/org-babel-results-indented-wrap ()
  850. "Ensure that wrapped results are inserted correction when indented.
  851. If not inserted correctly then the second evaluation will fail
  852. trying to find the :END: marker."
  853. (org-test-with-temp-text
  854. "- indented
  855. #+begin_src sh :results file wrap
  856. echo test.txt
  857. #+end_src"
  858. (org-babel-next-src-block 1)
  859. (org-babel-execute-src-block)
  860. (org-babel-execute-src-block)))
  861. (ert-deftest test-ob/file-desc-header-argument ()
  862. "Test that the :file-desc header argument is used."
  863. (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  864. \"foo\"
  865. #+end_src
  866. #+begin_src emacs-lisp :results file :file-desc
  867. \"foo\"
  868. #+end_src"
  869. (org-babel-execute-src-block)
  870. (org-babel-next-src-block 1)
  871. (org-babel-execute-src-block)
  872. (goto-char (point-min))
  873. (should (search-forward "[[file:foo][bar]]" nil t))
  874. (should (search-forward "[[file:foo][foo]]" nil t))))
  875. (ert-deftest test-ob/inline-src_blk-preceded-punct-preceded-by-point ()
  876. (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  877. (org-babel-inline-result-wrap "=%s="))
  878. (org-test-with-temp-text
  879. test-line
  880. (forward-char 1)
  881. (org-babel-execute-maybe)
  882. (should (re-search-forward "=\"x\"=" nil t))
  883. (forward-line))))
  884. (ert-deftest test-ob/commented-last-block-line-with-var ()
  885. (org-test-with-temp-text-in-file "
  886. #+begin_src emacs-lisp :var a=1
  887. ;;
  888. #+end_src"
  889. (org-babel-next-src-block)
  890. (org-babel-execute-maybe)
  891. (re-search-forward "\\#\\+results:" nil t)
  892. (forward-line)
  893. (should (string=
  894. ""
  895. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  896. (org-test-with-temp-text-in-file "
  897. #+begin_src emacs-lisp :var a=2
  898. 2;;
  899. #+end_src"
  900. (org-babel-next-src-block)
  901. (org-babel-execute-maybe)
  902. (re-search-forward "\\#\\+results:" nil t)
  903. (forward-line)
  904. (should (string=
  905. ": 2"
  906. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  907. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  908. "Test helper function to test `org-babel-remove-result'.
  909. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  910. and the result of execution is verified against RESULT.
  911. The block is actually executed /twice/ to ensure result
  912. replacement happens correctly."
  913. (org-test-with-temp-text
  914. buffer-text
  915. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  916. (should (re-search-forward "\\#\\+results:" nil t))
  917. (forward-line)
  918. (should (string= result
  919. (buffer-substring-no-properties
  920. (point-at-bol)
  921. (- (point-max) 16))))
  922. (org-babel-previous-src-block) (org-babel-remove-result)
  923. (should (string= buffer-text
  924. (buffer-substring-no-properties
  925. (point-min) (point-max))))))
  926. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  927. "Test `org-babel-remove-result' with :results list."
  928. (test-ob-verify-result-and-removed-result
  929. "- 1
  930. - 2
  931. - 3
  932. - (quote (4 5))"
  933. "* org-babel-remove-result
  934. #+begin_src emacs-lisp :results list
  935. '(1 2 3 '(4 5))
  936. #+end_src
  937. * next heading"))
  938. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  939. "Test `org-babel-remove-result' with :results wrap."
  940. (test-ob-verify-result-and-removed-result
  941. ":results:
  942. hello there
  943. :end:"
  944. "* org-babel-remove-result
  945. #+begin_src emacs-lisp :results wrap
  946. \"hello there\"
  947. #+end_src
  948. * next heading"))
  949. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  950. "Test `org-babel-remove-result' with :results org."
  951. (test-ob-verify-result-and-removed-result
  952. "#+begin_src org
  953. ,* heading
  954. ,** subheading
  955. content
  956. #+end_src"
  957. "* org-babel-remove-result
  958. #+begin_src emacs-lisp :results org
  959. \"* heading
  960. ,** subheading
  961. content\"
  962. #+end_src
  963. * next heading"))
  964. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  965. "Test `org-babel-remove-result' with :results html."
  966. (test-ob-verify-result-and-removed-result
  967. "#+begin_export html
  968. <head><body></body></head>
  969. #+end_export"
  970. "* org-babel-remove-result
  971. #+begin_src emacs-lisp :results html
  972. \"<head><body></body></head>\"
  973. #+end_src
  974. * next heading"))
  975. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  976. "Test `org-babel-remove-result' with :results latex."
  977. (test-ob-verify-result-and-removed-result
  978. "#+begin_export latex
  979. Line 1
  980. Line 2
  981. Line 3
  982. #+end_export"
  983. "* org-babel-remove-result
  984. #+begin_src emacs-lisp :results latex
  985. \"Line 1
  986. Line 2
  987. Line 3\"
  988. #+end_src
  989. * next heading"))
  990. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  991. "Test `org-babel-remove-result' with :results code."
  992. (test-ob-verify-result-and-removed-result
  993. "#+begin_src emacs-lisp
  994. \"I am working!\"
  995. #+end_src"
  996. "* org-babel-remove-result
  997. #+begin_src emacs-lisp :results code
  998. (message \"I am working!\")
  999. #+end_src
  1000. * next heading"))
  1001. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  1002. "Test `org-babel-remove-result' with :results pp."
  1003. (test-ob-verify-result-and-removed-result
  1004. ": \"I /am/ working!\""
  1005. "* org-babel-remove-result
  1006. #+begin_src emacs-lisp :results pp
  1007. \"I /am/ working!\")
  1008. #+end_src
  1009. * next heading"))
  1010. (ert-deftest test-ob/org-babel-remove-result--no-blank-line ()
  1011. "Test `org-babel-remove-result' without blank line between code and results."
  1012. (should
  1013. (equal "
  1014. #+begin_src emacs-lisp
  1015. (+ 1 1)
  1016. #+end_src
  1017. #+results:
  1018. : 2
  1019. * next heading"
  1020. (org-test-with-temp-text
  1021. "
  1022. <point>#+begin_src emacs-lisp
  1023. (+ 1 1)
  1024. #+end_src
  1025. #+results:
  1026. : 2
  1027. * next heading"
  1028. (org-babel-execute-maybe)
  1029. (buffer-string)))))
  1030. (ert-deftest test-ob/results-do-not-replace-code-blocks ()
  1031. (org-test-with-temp-text "Block two has a space after the name.
  1032. #+name: foo
  1033. #+begin_src emacs-lisp
  1034. 1
  1035. #+end_src
  1036. #+name: foo
  1037. #+begin_src emacs-lisp
  1038. 2
  1039. #+end_src
  1040. #+name: foo
  1041. #+begin_src emacs-lisp
  1042. 3
  1043. #+end_src
  1044. #+RESULTS: foo
  1045. : foo
  1046. "
  1047. (dolist (num '(1 2 3))
  1048. ;; execute the block
  1049. (goto-char (point-min))
  1050. (org-babel-next-src-block num) (org-babel-execute-src-block)
  1051. ;; check the results
  1052. (goto-char (point-max))
  1053. (move-beginning-of-line 0)
  1054. (should (looking-at (format ": %d" num))))))
  1055. (ert-deftest test-ob/blocks-with-spaces ()
  1056. "Test expansion of blocks followed by blank lines."
  1057. ;; Preserve number of blank lines after block.
  1058. (should
  1059. (equal "#+BEGIN_SRC emacs-lisp
  1060. \(+ 1 2)
  1061. #+END_SRC
  1062. #+RESULTS:
  1063. : 3\n\n\n"
  1064. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
  1065. \(+ 1 2)
  1066. #+END_SRC\n\n\n"
  1067. (let ((org-babel-next-src-block "RESULTS"))
  1068. (org-babel-execute-src-block))
  1069. (buffer-string))))
  1070. ;; Do not add spurious blank lines after results.
  1071. (should
  1072. (equal
  1073. "
  1074. - item 1
  1075. #+begin_src emacs-lisp
  1076. 0
  1077. #+end_src
  1078. #+RESULTS:
  1079. : 0
  1080. - item 2"
  1081. (org-test-with-temp-text "
  1082. - item 1
  1083. #+begin_src emacs-lisp<point>
  1084. 0
  1085. #+end_src
  1086. - item 2"
  1087. (org-babel-execute-src-block)
  1088. (buffer-string))))
  1089. (should
  1090. (equal
  1091. "
  1092. - item 1
  1093. #+begin_src emacs-lisp
  1094. 1
  1095. #+end_src
  1096. #+RESULTS:
  1097. : 1
  1098. - item 2"
  1099. (org-test-with-temp-text "
  1100. - item 1
  1101. #+begin_src emacs-lisp<point>
  1102. 1
  1103. #+end_src
  1104. #+RESULTS:
  1105. : 1
  1106. - item 2"
  1107. (org-babel-execute-src-block)
  1108. (buffer-string)))))
  1109. (ert-deftest test-ob/results-in-narrowed-buffer ()
  1110. "Test block execution in a narrowed buffer."
  1111. ;; If results don't exist, they should be inserted in visible part
  1112. ;; of the buffer.
  1113. (should
  1114. (equal
  1115. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
  1116. (org-test-with-temp-text
  1117. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1118. (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
  1119. (let ((org-babel-results-keyword "RESULTS"))
  1120. (org-babel-execute-src-block))
  1121. (org-trim (buffer-string)))))
  1122. (should
  1123. (equal
  1124. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
  1125. (org-test-with-temp-text
  1126. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1127. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1128. (let ((org-babel-results-keyword "RESULTS"))
  1129. (org-babel-execute-src-block))
  1130. (org-trim (buffer-string)))))
  1131. ;; Results in visible part of buffer, should be updated here.
  1132. (should
  1133. (equal
  1134. "#+NAME: test
  1135. #+BEGIN_SRC emacs-lisp
  1136. \(+ 1 2)
  1137. #+END_SRC
  1138. #+RESULTS: test
  1139. : 3"
  1140. (org-test-with-temp-text
  1141. "#+NAME: test
  1142. #+BEGIN_SRC emacs-lisp
  1143. \(+ 1 2)
  1144. #+END_SRC
  1145. #+RESULTS: test
  1146. : 4
  1147. <point>
  1148. Paragraph"
  1149. (narrow-to-region (point-min) (point))
  1150. (goto-char (point-min))
  1151. (let ((org-babel-results-keyword "RESULTS"))
  1152. (org-babel-execute-src-block))
  1153. (org-trim (buffer-string)))))
  1154. ;; Results in invisible part of buffer, should be updated there.
  1155. (org-test-with-temp-text
  1156. "#+NAME: test
  1157. #+BEGIN_SRC emacs-lisp
  1158. \(+ 1 2)
  1159. #+END_SRC
  1160. #+RESULTS: test
  1161. : 4
  1162. Paragraph"
  1163. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1164. (let ((org-babel-results-keyword "RESULTS"))
  1165. (org-babel-execute-src-block))
  1166. (should-not (re-search-forward "^#\\+RESULTS:" nil t))
  1167. (widen)
  1168. (should (re-search-forward "^: 3" nil t))))
  1169. (ert-deftest test-ob/specific-colnames ()
  1170. "Test passing specific column names."
  1171. (should
  1172. (equal "#+name: input-table
  1173. | id | var1 |
  1174. |----+------|
  1175. | 1 | bar |
  1176. | 2 | baz |
  1177. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1178. echo \"$data\"
  1179. #+end_src
  1180. #+RESULTS:
  1181. | Rev | Author |
  1182. |-----+--------|
  1183. | 1 | bar |
  1184. | 2 | baz |
  1185. "
  1186. (org-test-with-temp-text
  1187. "#+name: input-table
  1188. | id | var1 |
  1189. |----+------|
  1190. | 1 | bar |
  1191. | 2 | baz |
  1192. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1193. echo \"$data\"
  1194. #+end_src
  1195. "
  1196. ;; we should find a code block
  1197. (should (re-search-forward org-babel-src-block-regexp nil t))
  1198. (goto-char (match-beginning 0))
  1199. ;; now that we've located the code block, it may be evaluated
  1200. (let ((org-babel-execute-src-block "RESULTS"))
  1201. (org-babel-execute-src-block))
  1202. (buffer-string)))))
  1203. (ert-deftest test-ob/location-of-header-arg-eval ()
  1204. "Test location of header argument evaluation."
  1205. (org-test-with-temp-text "
  1206. #+name: top-block
  1207. #+begin_src emacs-lisp :var pt=(point)
  1208. pt
  1209. #+end_src
  1210. #+name: bottom-block
  1211. #+begin_src emacs-lisp :var pt=top-block()
  1212. pt
  1213. #+end_src
  1214. "
  1215. ;; the value of the second block should be greater than the first
  1216. (should
  1217. (< (progn (re-search-forward org-babel-src-block-regexp nil t)
  1218. (goto-char (match-beginning 0))
  1219. (prog1 (save-match-data (org-babel-execute-src-block))
  1220. (goto-char (match-end 0))))
  1221. (progn (re-search-forward org-babel-src-block-regexp nil t)
  1222. (goto-char (match-beginning 0))
  1223. (org-babel-execute-src-block))))))
  1224. (ert-deftest test-ob/preserve-results-indentation ()
  1225. "Preserve indentation when executing a src block."
  1226. (should
  1227. (equal
  1228. '(2 2)
  1229. (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1230. (org-babel-execute-src-block)
  1231. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1232. (list (org-get-indentation)
  1233. (progn (forward-line) (org-get-indentation))))))
  1234. (should
  1235. (equal
  1236. '(2 2)
  1237. (org-test-with-temp-text
  1238. " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1239. (org-babel-execute-src-block)
  1240. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1241. (list (org-get-indentation)
  1242. (progn (forward-line) (org-get-indentation))))))
  1243. ;; Don't get fooled by TAB-based indentation.
  1244. (should
  1245. (equal
  1246. '(6 6)
  1247. (org-test-with-temp-text
  1248. "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
  1249. (setq tab-width 4)
  1250. (org-babel-execute-src-block)
  1251. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1252. (list (org-get-indentation)
  1253. (progn (forward-line) (org-get-indentation))))))
  1254. ;; Properly indent examplified blocks.
  1255. (should
  1256. (equal
  1257. " #+begin_example
  1258. 0
  1259. 1
  1260. 2
  1261. 3
  1262. 4
  1263. 5
  1264. 6
  1265. 7
  1266. 8
  1267. 9
  1268. #+end_example
  1269. "
  1270. (org-test-with-temp-text
  1271. " #+begin_src emacs-lisp :results output
  1272. (dotimes (i 10) (princ i) (princ \"\\n\"))
  1273. #+end_src"
  1274. (org-babel-execute-src-block)
  1275. (search-forward "begin_example")
  1276. (downcase
  1277. (buffer-substring-no-properties (line-beginning-position)
  1278. (point-max))))))
  1279. ;; Properly indent "org" blocks.
  1280. (should
  1281. (equal
  1282. " #+begin_src org
  1283. 0
  1284. 1
  1285. 2
  1286. 3
  1287. 4
  1288. 5
  1289. 6
  1290. 7
  1291. 8
  1292. 9
  1293. #+end_src
  1294. "
  1295. (org-test-with-temp-text
  1296. " #+begin_src emacs-lisp :results output org
  1297. (dotimes (i 10) (princ i) (princ \"\\n\"))
  1298. #+end_src"
  1299. (org-babel-execute-src-block)
  1300. (search-forward "begin_src org")
  1301. (downcase
  1302. (buffer-substring-no-properties (line-beginning-position)
  1303. (point-max)))))))
  1304. (ert-deftest test-ob/safe-header-args ()
  1305. "Detect safe and unsafe header args."
  1306. (let ((safe-args '((:cache . "foo")
  1307. (:results . "output")
  1308. (:eval . "never")
  1309. (:eval . "query")))
  1310. (unsafe-args '((:eval . "yes")
  1311. (:results . "output file")
  1312. (:foo . "bar")))
  1313. (malformed-args '((bar . "foo")
  1314. ("foo" . "bar")
  1315. :foo))
  1316. (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
  1317. (dolist (arg safe-args)
  1318. (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
  1319. (dolist (arg unsafe-args)
  1320. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1321. (dolist (arg malformed-args)
  1322. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1323. (should (not (funcall safe-p (append safe-args unsafe-args))))))
  1324. (ert-deftest test-ob/noweb-expansions-in-cache ()
  1325. "Ensure that noweb expansions are expanded before caching."
  1326. (let ((noweb-expansions-in-cache-var 0))
  1327. (org-test-with-temp-text "
  1328. #+name: foo
  1329. #+begin_src emacs-lisp
  1330. \"I said\"
  1331. #+end_src
  1332. #+name: bar
  1333. #+begin_src emacs-lisp :noweb yes :cache yes
  1334. (setq noweb-expansions-in-cache-var
  1335. (+ 1 noweb-expansions-in-cache-var))
  1336. (concat <<foo>> \" check noweb expansions\")
  1337. #+end_src
  1338. "
  1339. ;; run the second block to create the cache
  1340. (goto-char (point-min))
  1341. (re-search-forward (regexp-quote "#+name: bar"))
  1342. (should (string= "I said check noweb expansions"
  1343. (org-babel-execute-src-block)))
  1344. (should (= noweb-expansions-in-cache-var 1))
  1345. ;; change the value of the first block
  1346. (goto-char (point-min))
  1347. (re-search-forward (regexp-quote "said"))
  1348. (goto-char (match-beginning 0))
  1349. (insert "haven't ")
  1350. (re-search-forward (regexp-quote "#+name: bar"))
  1351. (should (string= "I haven't said check noweb expansions"
  1352. (org-babel-execute-src-block)))
  1353. (should (= noweb-expansions-in-cache-var 2)))))
  1354. (ert-deftest test-ob/file-ext-and-output-dir ()
  1355. (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
  1356. (org-babel-next-src-block)
  1357. (should (equal "file-ext-basic.txt"
  1358. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1359. (org-babel-next-src-block)
  1360. (should (equal "foo/file-ext-dir-relative.txt"
  1361. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1362. (org-babel-next-src-block)
  1363. (should (equal "foo/file-ext-dir-relative-slash.txt"
  1364. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1365. (org-babel-next-src-block)
  1366. (should (equal "/tmp/file-ext-dir-absolute.txt"
  1367. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1368. (org-babel-next-src-block)
  1369. (should (equal "foo.bar"
  1370. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1371. (org-babel-next-src-block)
  1372. (should (equal "xxx/foo.bar"
  1373. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1374. ))
  1375. (ert-deftest test-ob/script-escape ()
  1376. ;; Delimited lists of numbers
  1377. (should (equal '(1 2 3)
  1378. (org-babel-script-escape "[1 2 3]")))
  1379. (should (equal '(1 2 3)
  1380. (org-babel-script-escape "{1 2 3}")))
  1381. (should (equal '(1 2 3)
  1382. (org-babel-script-escape "(1 2 3)")))
  1383. ;; Delimited lists of double-quoted strings
  1384. (should (equal '("foo" "bar")
  1385. (org-babel-script-escape "(\"foo\" \"bar\")")))
  1386. (should (equal '("foo" "bar")
  1387. (org-babel-script-escape "[\"foo\" \"bar\"]")))
  1388. (should (equal '("foo" "bar")
  1389. (org-babel-script-escape "{\"foo\" \"bar\"}")))
  1390. ;; ... with commas
  1391. (should (equal '("foo" "bar")
  1392. (org-babel-script-escape "(\"foo\", \"bar\")")))
  1393. (should (equal '("foo" "bar")
  1394. (org-babel-script-escape "[\"foo\", \"bar\"]")))
  1395. (should (equal '("foo" "bar")
  1396. (org-babel-script-escape "{\"foo\", \"bar\"}")))
  1397. ;; Delimited lists of single-quoted strings
  1398. (should (equal '("foo" "bar")
  1399. (org-babel-script-escape "('foo' 'bar')")))
  1400. (should (equal '("foo" "bar")
  1401. (org-babel-script-escape "['foo' 'bar']")))
  1402. (should (equal '("foo" "bar")
  1403. (org-babel-script-escape "{'foo' 'bar'}")))
  1404. ;; ... with commas
  1405. (should (equal '("foo" "bar")
  1406. (org-babel-script-escape "('foo', 'bar')")))
  1407. (should (equal '("foo" "bar")
  1408. (org-babel-script-escape "['foo', 'bar']")))
  1409. (should (equal '("foo" "bar")
  1410. (org-babel-script-escape "{'foo', 'bar'}")))
  1411. ;; Single quoted strings
  1412. (should (equal "foo"
  1413. (org-babel-script-escape "'foo'")))
  1414. ;; ... with internal double quote
  1415. (should (equal "foo\"bar"
  1416. (org-babel-script-escape "'foo\"bar'")))
  1417. ;; ... with internal backslash
  1418. (should (equal "foo\\bar"
  1419. (org-babel-script-escape "'foo\\bar'")))
  1420. ;; ... with internal escaped backslash
  1421. (should (equal "foo\\bar"
  1422. (org-babel-script-escape "'foo\\\\bar'")))
  1423. ;; ... with internal backslash-double quote
  1424. (should (equal "foo\\\"bar"
  1425. (org-babel-script-escape "'foo\\\"bar'")))
  1426. ;; ... with internal escaped backslash-double quote
  1427. (should (equal "foo\\\"bar"
  1428. (org-babel-script-escape "'foo\\\\\"bar'")))
  1429. ;; ... with internal escaped single quote
  1430. (should (equal "foo'bar"
  1431. (org-babel-script-escape "'foo\\'bar'")))
  1432. ;; ... with internal escaped backslash-escaped single quote
  1433. (should (equal "foo\\'bar"
  1434. (org-babel-script-escape "'foo\\\\\\'bar'")))
  1435. ;; Double quoted strings
  1436. (should (equal "foo"
  1437. (org-babel-script-escape "\"foo\"")))
  1438. ;; ... with internal single quote
  1439. (should (equal "foo'bar"
  1440. (org-babel-script-escape "\"foo'bar\"")))
  1441. ;; ... with internal backslash
  1442. (should (equal "foo\\bar"
  1443. (org-babel-script-escape "\"foo\\bar\"")))
  1444. ;; ... with internal escaped backslash
  1445. (should (equal "foo\\bar"
  1446. (org-babel-script-escape "\"foo\\\\bar\"")))
  1447. ;; ... with internal backslash-single quote
  1448. (should (equal "foo\\'bar"
  1449. (org-babel-script-escape "\"foo\\'bar\"")))
  1450. ;; ... with internal escaped backslash-single quote
  1451. (should (equal "foo\\'bar"
  1452. (org-babel-script-escape "\"foo\\\\'bar\"")))
  1453. ;; ... with internal escaped double quote
  1454. (should (equal "foo\"bar"
  1455. (org-babel-script-escape "\"foo\\\"bar\"")))
  1456. ;; ... with internal escaped backslash-escaped double quote
  1457. (should (equal "foo\\\"bar"
  1458. (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
  1459. (ert-deftest test-ob/process-params-no-duplicates ()
  1460. (should
  1461. (equal (org-babel-process-params '((:colname-names)
  1462. (:rowname-names)
  1463. (:result-params)
  1464. (:result-type)
  1465. (:var . "\"foo\"")))
  1466. '((:var)
  1467. (:colname-names)
  1468. (:rowname-names)
  1469. (:result-params)
  1470. (:result-type . value)))))
  1471. (defun org-test-babel-confirm-evaluate (eval-value)
  1472. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  1473. nil
  1474. #+end_src" eval-value)
  1475. (goto-char (point-min))
  1476. (let ((info (org-babel-get-src-block-info)))
  1477. (org-babel-check-confirm-evaluate info))))
  1478. (ert-deftest test-ob/check-eval ()
  1479. (let ((org-confirm-babel-evaluate t))
  1480. ;; Non-export tests
  1481. (dolist (pair '(("no" . nil)
  1482. ("never" . nil)
  1483. ("query" . query)
  1484. ("yes" . query)))
  1485. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1486. ;; Export tests
  1487. (let ((org-babel-exp-reference-buffer t))
  1488. (dolist (pair '(("no" . nil)
  1489. ("never" . nil)
  1490. ("query" . query)
  1491. ("yes" . query)
  1492. ("never-export" . nil)
  1493. ("no-export" . nil)
  1494. ("query-export" . query)))
  1495. (message (car pair))
  1496. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))))
  1497. (let ((org-confirm-babel-evaluate nil))
  1498. ;; Non-export tests
  1499. (dolist (pair '(("no" . nil)
  1500. ("never" . nil)
  1501. ("query" . query)
  1502. ("yes" . t)))
  1503. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1504. ;; Export tests
  1505. (let ((org-babel-exp-reference-buffer t))
  1506. (dolist (pair '(("no" . nil)
  1507. ("never" . nil)
  1508. ("query" . query)
  1509. ("yes" . t)
  1510. ("never-export" . nil)
  1511. ("no-export" . nil)
  1512. ("query-export" . query)))
  1513. (message (car pair))
  1514. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair)))))))
  1515. (defun org-test-ob/update-block-body ()
  1516. "Test `org-babel-update-block-body' specifications."
  1517. (should
  1518. (equal "#+begin_src elisp\n 2\n#+end_src"
  1519. (let ((org-edit-src-content-indentation 2))
  1520. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1521. (org-babel-update-block-body "2")
  1522. (buffer-string)))))
  1523. ;; Preserve block indentation.
  1524. (should
  1525. (equal " #+begin_src elisp\n 2\n #+end_src"
  1526. (let ((org-edit-src-content-indentation 1))
  1527. (org-test-with-temp-text
  1528. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1529. (org-babel-update-block-body "2")
  1530. (buffer-string)))))
  1531. ;; Ignore NEW-BODY global indentation.
  1532. (should
  1533. (equal "#+begin_src elisp\n 2\n#+end_src"
  1534. (let ((org-edit-src-content-indentation 2))
  1535. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1536. (org-babel-update-block-body " 2")
  1537. (buffer-string)))))
  1538. ;; When indentation should be preserved ignore the two rules above.
  1539. (should
  1540. (equal " #+begin_src elisp\n2\n #+end_src"
  1541. (let ((org-edit-src-content-indentation 1)
  1542. (org-src-preserve-indentation t))
  1543. (org-test-with-temp-text
  1544. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1545. (org-babel-update-block-body "2")
  1546. (buffer-string)))))
  1547. (should
  1548. (equal " #+begin_src elisp -i\n2\n #+end_src"
  1549. (let ((org-edit-src-content-indentation 1))
  1550. (org-test-with-temp-text
  1551. " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
  1552. (org-babel-update-block-body "2")
  1553. (buffer-string)))))
  1554. (should
  1555. (equal "#+begin_src elisp\n 2\n#+end_src"
  1556. (let ((org-edit-src-content-indentation 2)
  1557. (org-src-preserve-indentation t))
  1558. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1559. (org-babel-update-block-body " 2")
  1560. (buffer-string)))))
  1561. (should
  1562. (equal "#+begin_src elisp -i\n 2\n#+end_src"
  1563. (let ((org-edit-src-content-indentation 2)
  1564. (org-src-preserve-indentation t))
  1565. (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
  1566. (org-babel-update-block-body " 2")
  1567. (buffer-string))))))
  1568. (ert-deftest test-ob/find-named-result ()
  1569. "Test `org-babel-find-named-result' specifications."
  1570. (should
  1571. (= 1
  1572. (org-test-with-temp-text "#+results: foo\n: result"
  1573. (org-babel-find-named-result "foo"))))
  1574. (should-not
  1575. (org-test-with-temp-text "#+results: foo\n: result"
  1576. (org-babel-find-named-result "bar")))
  1577. (should-not
  1578. (org-test-with-temp-text "#+results: foobar\n: result"
  1579. (org-babel-find-named-result "foo")))
  1580. ;; Search is case insensitive.
  1581. (should
  1582. (org-test-with-temp-text "#+RESULTS: FOO\n: result"
  1583. (org-babel-find-named-result "foo")))
  1584. ;; Handle hash in results keyword.
  1585. (should
  1586. (org-test-with-temp-text "#+results[hash]: FOO\n: result"
  1587. (org-babel-find-named-result "foo")))
  1588. ;; Accept orphaned affiliated keywords.
  1589. (should
  1590. (org-test-with-temp-text "#+results: foo"
  1591. (org-babel-find-named-result "foo"))))
  1592. (ert-deftest test-ob/where-is-src-block-result ()
  1593. "Test `org-babel-where-is-src-block-result' specifications."
  1594. ;; Find anonymous results.
  1595. (should
  1596. (equal "#+RESULTS:"
  1597. (org-test-with-temp-text
  1598. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
  1599. (goto-char (org-babel-where-is-src-block-result))
  1600. (buffer-substring-no-properties (point) (line-end-position)))))
  1601. ;; Find named results. Those have priority over anonymous ones.
  1602. (should
  1603. (equal "#+RESULTS: example"
  1604. (org-test-with-temp-text
  1605. "
  1606. <point>#+NAME: example
  1607. #+BEGIN_SRC emacs-lisp
  1608. \(+ 1 1)
  1609. #+END_SRC
  1610. #+RESULTS: example
  1611. : 2"
  1612. (goto-char (org-babel-where-is-src-block-result))
  1613. (buffer-substring-no-properties (point) (line-end-position)))))
  1614. (should
  1615. (equal "#+RESULTS: example"
  1616. (org-test-with-temp-text
  1617. "
  1618. <point>#+NAME: example
  1619. #+BEGIN_SRC emacs-lisp
  1620. \(+ 1 1)
  1621. #+END_SRC
  1622. #+RESULTS:
  1623. : fake
  1624. #+RESULTS: example
  1625. : 2"
  1626. (goto-char (org-babel-where-is-src-block-result))
  1627. (buffer-substring-no-properties (point) (line-end-position)))))
  1628. ;; Return nil when no result is found.
  1629. (should-not
  1630. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1631. (org-babel-where-is-src-block-result)))
  1632. (should-not
  1633. (org-test-with-temp-text
  1634. "- item\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n"
  1635. (org-babel-where-is-src-block-result)))
  1636. ;; When optional argument INSERT is non-nil, add RESULTS keyword
  1637. ;; whenever no RESULTS can be found.
  1638. (should
  1639. (equal
  1640. "#+RESULTS:"
  1641. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1642. (let ((org-babel-results-keyword "RESULTS"))
  1643. (goto-char (org-babel-where-is-src-block-result t)))
  1644. (buffer-substring-no-properties (point) (line-end-position)))))
  1645. ;; Insert a named RESULTS keyword if possible.
  1646. (should
  1647. (equal
  1648. "#+RESULTS: e"
  1649. (org-test-with-temp-text
  1650. "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1651. (let ((org-babel-results-keyword "RESULTS"))
  1652. (goto-char (org-babel-where-is-src-block-result t)))
  1653. (buffer-substring-no-properties (point) (line-end-position)))))
  1654. ;; When optional argument HASH is provided, clear RESULTS keyword
  1655. ;; and related contents if they do not match it.
  1656. (should
  1657. (equal
  1658. "#+RESULTS[bbbb]:"
  1659. (org-test-with-temp-text
  1660. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 3"
  1661. (let ((org-babel-results-keyword "RESULTS"))
  1662. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1663. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1664. (should
  1665. (equal
  1666. "#+RESULTS[bbbb]:"
  1667. (org-test-with-temp-text
  1668. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
  1669. (let ((org-babel-results-keyword "RESULTS"))
  1670. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1671. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1672. ;; Handle hashes with times.
  1673. (should
  1674. (equal
  1675. "#+RESULTS[<2014-03-04 00:41:10> bbbb]:"
  1676. (org-test-with-temp-text
  1677. "
  1678. <point>#+BEGIN_SRC emacs-lisp
  1679. \(+ 1 1)
  1680. #+END_SRC
  1681. #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1682. (let ((org-babel-results-keyword "RESULTS")
  1683. (org-babel-hash-show-time t))
  1684. (cl-letf (((symbol-function 'format-time-string)
  1685. (lambda (&rest _) "<2014-03-04 00:41:10>")))
  1686. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb"))
  1687. (org-trim (buffer-substring-no-properties (point) (point-max))))))))
  1688. (should
  1689. (equal
  1690. "#+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1691. (org-test-with-temp-text
  1692. "
  1693. <point>#+BEGIN_SRC emacs-lisp
  1694. \(+ 1 1)
  1695. #+END_SRC
  1696. #+RESULTS[<2012-03-29 16:40:12> aaaa]:"
  1697. (let ((org-babel-results-keyword "RESULTS")
  1698. (org-babel-hash-show-time t))
  1699. (cl-letf (((symbol-function 'format-time-string)
  1700. (lambda (&rest _) "<2014-03-04 00:41:10>")))
  1701. (goto-char (org-babel-where-is-src-block-result nil nil "aaaa"))
  1702. (org-trim (buffer-substring-no-properties (point) (point-max))))))))
  1703. ;; RESULTS keyword may not be the last affiliated keyword.
  1704. (should
  1705. (equal
  1706. "#+RESULTS[bbbb]:"
  1707. (org-test-with-temp-text
  1708. "
  1709. <point>#+BEGIN_SRC emacs-lisp
  1710. \(+ 1 1)
  1711. #+END_SRC
  1712. #+RESULTS[aaaa]:
  1713. #+NAME: e
  1714. : 3"
  1715. (let ((org-babel-results-keyword "RESULTS"))
  1716. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  1717. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  1718. ;; HASH does nothing if no RESULTS can be found. However, if INSERT
  1719. ;; is also non-nil, RESULTS keyword is inserted along with the
  1720. ;; expected hash.
  1721. (should
  1722. (equal
  1723. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1724. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1725. (org-babel-where-is-src-block-result nil nil "bbbb")
  1726. (buffer-string))))
  1727. (should
  1728. (equal
  1729. "#+RESULTS[bbbb]:"
  1730. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  1731. (let ((org-babel-results-keyword "RESULTS"))
  1732. (goto-char (org-babel-where-is-src-block-result t nil "bbbb")))
  1733. (org-trim (buffer-substring-no-properties (point) (point-max)))))))
  1734. (ert-deftest test-ob/goto-named-src-block ()
  1735. "Test interactive use of `org-babel-goto-named-src-block'."
  1736. (org-test-with-temp-text-in-file
  1737. "
  1738. #+NAME: abc
  1739. #+BEGIN_SRC emacs-lisp :results value
  1740. (1+ 1)
  1741. #+END_SRC
  1742. #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
  1743. #+BEGIN_SRC emacs-lisp
  1744. <<abc>>
  1745. #+END_SRC
  1746. abc
  1747. #+RESULTS: abc
  1748. : 2
  1749. "
  1750. ;; non-existent name
  1751. (should-not
  1752. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
  1753. ;; correct name
  1754. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
  1755. (should (= 14 (point)))
  1756. ;; call line - autocompletion
  1757. (forward-line 3)
  1758. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1759. (should (= 14 (point)))
  1760. ;; noweb reference - autocompletion
  1761. (forward-line 5)
  1762. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1763. (should (= 14 (point)))
  1764. ;; at symbol - autocompletion
  1765. (forward-line 7)
  1766. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1767. (should (= 14 (point)))
  1768. ;; in results - autocompletion
  1769. (forward-line 8)
  1770. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1771. (should (= 14 (point)))
  1772. (forward-line 9)
  1773. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  1774. (should (= 14 (point)))))
  1775. (ert-deftest test-ob/evaluate-body-with-coderefs ()
  1776. (should
  1777. (= 2
  1778. (org-test-with-temp-text
  1779. "#+begin_src emacs-lisp -l \"#(ref:%s)\"\n2 #(ref:foo)\n#+end_src"
  1780. (org-babel-execute-src-block))))
  1781. (should
  1782. (= 3
  1783. (org-test-with-temp-text
  1784. "#+begin_src emacs-lisp\n3 #(ref:foo)\n#+end_src"
  1785. (let ((org-coderef-label-format "#(ref:%s)"))
  1786. (org-babel-execute-src-block))))))
  1787. (provide 'test-ob)
  1788. ;;; test-ob ends here