test-ob.el 72 KB

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