test-ob.el 69 KB

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