test-ob.el 74 KB

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