test-ob.el 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499
  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/org-babel-results-indented-list ()
  1060. "Test that :results value list indents multi-line items correctly."
  1061. (should
  1062. (string= "- Foo1
  1063. Bar1
  1064. - Foo2
  1065. Bar2
  1066. "
  1067. (org-test-with-temp-text
  1068. "#+begin_src emacs-lisp :results value list
  1069. '(\"Foo1
  1070. Bar1\"
  1071. \"Foo2
  1072. Bar2\")
  1073. #+end_src"
  1074. (org-babel-execute-src-block)
  1075. (org-forward-element)
  1076. (org-narrow-to-element)
  1077. (delete-trailing-whitespace)
  1078. (buffer-string)))))
  1079. (ert-deftest test-ob/file-desc-header-argument ()
  1080. "Test that the :file-desc header argument is used."
  1081. (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  1082. \"foo\"
  1083. #+end_src
  1084. #+begin_src emacs-lisp :results file :file-desc
  1085. \"foo\"
  1086. #+end_src"
  1087. (org-babel-execute-src-block)
  1088. (org-babel-next-src-block 1)
  1089. (org-babel-execute-src-block)
  1090. (goto-char (point-min))
  1091. (should (search-forward "[[file:foo][bar]]" nil t))
  1092. (should (search-forward "[[file:foo][foo]]" nil t)))
  1093. (should (string-match-p
  1094. (regexp-quote "[[file:foo]]")
  1095. (org-test-with-temp-text "
  1096. #+begin_src emacs-lisp :results file :file-desc []
  1097. \"foo\"
  1098. #+end_src"
  1099. (org-babel-next-src-block)
  1100. (org-babel-execute-src-block)
  1101. (buffer-substring-no-properties (point-min) (point-max)))))
  1102. (should (string-match-p
  1103. (regexp-quote "[[file:foo][foo]]")
  1104. (org-test-with-temp-text "
  1105. #+begin_src emacs-lisp :results file :file-desc
  1106. \"foo\"
  1107. #+end_src"
  1108. (org-babel-next-src-block)
  1109. (org-babel-execute-src-block)
  1110. (buffer-substring-no-properties (point-min) (point-max))))))
  1111. (ert-deftest test-ob/result-file-link-type-header-argument ()
  1112. "Ensure that the result is a link to a file.
  1113. The file is just a link to `:file' value. Inhibit non-empty
  1114. result write to `:file' value."
  1115. (org-test-with-temp-text "
  1116. <point>#+begin_src shell :results value file link :file \"/tmp/test.txt\"
  1117. echo \"hello\" > /tmp/test.txt
  1118. echo \"test\"
  1119. #+end_src"
  1120. (org-babel-execute-src-block)
  1121. (should (search-forward "[[file:/tmp/test.txt]]" nil t))
  1122. (should (with-temp-buffer
  1123. (insert-file-contents "/tmp/test.txt")
  1124. (string= "hello\n" (buffer-string)))))
  1125. ;; Without "link" output type, the result is not a file.
  1126. (should-not
  1127. (org-test-with-temp-text "
  1128. <point>#+begin_src shell :results value link :file \"/tmp/test.txt\"
  1129. echo \"hello\" > /tmp/test.txt
  1130. echo \"test\"
  1131. #+end_src"
  1132. (org-babel-execute-src-block)
  1133. (search-forward "[[file:/tmp/test.txt]]" nil t))))
  1134. (ert-deftest test-ob/result-graphics-link-type-header-argument ()
  1135. "Ensure that the result is a link to a file.
  1136. The file is just a link to `:file' value. Inhibit non-empty
  1137. result write to `:file' value."
  1138. (org-test-with-temp-text "
  1139. <point>#+begin_src shell :results value file graphics :file \"/tmp/test.txt\"
  1140. echo \"hello\" > /tmp/test.txt
  1141. echo \"test\"
  1142. #+end_src"
  1143. (org-babel-execute-src-block)
  1144. (should (search-forward "[[file:/tmp/test.txt]]" nil nil))
  1145. (should (with-temp-buffer
  1146. (insert-file-contents "/tmp/test.txt")
  1147. (string= "hello\n" (buffer-string)))))
  1148. ;; Without "link" output type, the result is not a file.
  1149. (should-not
  1150. (org-test-with-temp-text "
  1151. <point>#+begin_src shell :results value graphics :file \"/tmp/test.txt\"
  1152. echo \"hello\" > /tmp/test.txt
  1153. echo \"test\"
  1154. #+end_src"
  1155. (org-babel-execute-src-block)
  1156. (search-forward "[[file:/tmp/test.txt]]" nil t))))
  1157. (ert-deftest test-ob/inline-src_blk-preceded-punct-preceded-by-point ()
  1158. (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  1159. (org-babel-inline-result-wrap "=%s="))
  1160. (org-test-with-temp-text
  1161. test-line
  1162. (forward-char 1)
  1163. (org-babel-execute-maybe)
  1164. (should (re-search-forward "=\"x\"=" nil t))
  1165. (forward-line))))
  1166. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  1167. "Test helper function to test `org-babel-remove-result'.
  1168. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  1169. and the result of execution is verified against RESULT.
  1170. The block is actually executed /twice/ to ensure result
  1171. replacement happens correctly."
  1172. (org-test-with-temp-text
  1173. buffer-text
  1174. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  1175. (should (re-search-forward "\\#\\+results:" nil t))
  1176. (forward-line)
  1177. (should (string= result
  1178. (buffer-substring-no-properties
  1179. (point-at-bol)
  1180. (- (point-max) 16))))
  1181. (org-babel-previous-src-block) (org-babel-remove-result)
  1182. (should (string= buffer-text
  1183. (buffer-substring-no-properties
  1184. (point-min) (point-max))))))
  1185. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  1186. "Test `org-babel-remove-result' with :results list."
  1187. (test-ob-verify-result-and-removed-result
  1188. "- 1
  1189. - 2
  1190. - 3"
  1191. "* org-babel-remove-result
  1192. #+begin_src emacs-lisp :results list
  1193. '(1 2 3)
  1194. #+end_src
  1195. * next heading"))
  1196. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  1197. "Test `org-babel-remove-result' with :results wrap."
  1198. (test-ob-verify-result-and-removed-result
  1199. ":results:
  1200. hello there
  1201. :end:"
  1202. "* org-babel-remove-result
  1203. #+begin_src emacs-lisp :results wrap
  1204. \"hello there\"
  1205. #+end_src
  1206. * next heading"))
  1207. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  1208. "Test `org-babel-remove-result' with :results org."
  1209. (test-ob-verify-result-and-removed-result
  1210. "#+begin_src org
  1211. ,* heading
  1212. ,** subheading
  1213. content
  1214. #+end_src"
  1215. "* org-babel-remove-result
  1216. #+begin_src emacs-lisp :results org
  1217. \"* heading
  1218. ,** subheading
  1219. content\"
  1220. #+end_src
  1221. * next heading"))
  1222. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  1223. "Test `org-babel-remove-result' with :results html."
  1224. (test-ob-verify-result-and-removed-result
  1225. "#+begin_export html
  1226. <head><body></body></head>
  1227. #+end_export"
  1228. "* org-babel-remove-result
  1229. #+begin_src emacs-lisp :results html
  1230. \"<head><body></body></head>\"
  1231. #+end_src
  1232. * next heading"))
  1233. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  1234. "Test `org-babel-remove-result' with :results latex."
  1235. (test-ob-verify-result-and-removed-result
  1236. "#+begin_export latex
  1237. Line 1
  1238. Line 2
  1239. Line 3
  1240. #+end_export"
  1241. "* org-babel-remove-result
  1242. #+begin_src emacs-lisp :results latex
  1243. \"Line 1
  1244. Line 2
  1245. Line 3\"
  1246. #+end_src
  1247. * next heading"))
  1248. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  1249. "Test `org-babel-remove-result' with :results code."
  1250. (test-ob-verify-result-and-removed-result
  1251. "#+begin_src emacs-lisp
  1252. \"I am working!\"
  1253. #+end_src"
  1254. "* org-babel-remove-result
  1255. #+begin_src emacs-lisp :results code
  1256. (message \"I am working!\")
  1257. #+end_src
  1258. * next heading"))
  1259. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  1260. "Test `org-babel-remove-result' with :results pp."
  1261. (test-ob-verify-result-and-removed-result
  1262. ": \"I /am/ working!\""
  1263. "* org-babel-remove-result
  1264. #+begin_src emacs-lisp :results pp
  1265. \"I /am/ working!\")
  1266. #+end_src
  1267. * next heading"))
  1268. (ert-deftest test-ob/org-babel-remove-result--no-blank-line ()
  1269. "Test `org-babel-remove-result' without blank line between code and results."
  1270. (should
  1271. (equal "
  1272. #+begin_src emacs-lisp
  1273. (+ 1 1)
  1274. #+end_src
  1275. #+results:
  1276. : 2
  1277. * next heading"
  1278. (org-test-with-temp-text
  1279. "
  1280. <point>#+begin_src emacs-lisp
  1281. (+ 1 1)
  1282. #+end_src
  1283. #+results:
  1284. : 2
  1285. * next heading"
  1286. (org-babel-execute-maybe)
  1287. (buffer-string)))))
  1288. (ert-deftest test-ob/results-do-not-replace-code-blocks ()
  1289. (org-test-with-temp-text "Block two has a space after the name.
  1290. #+name: foo
  1291. #+begin_src emacs-lisp
  1292. 1
  1293. #+end_src
  1294. #+name: foo
  1295. #+begin_src emacs-lisp
  1296. 2
  1297. #+end_src
  1298. #+name: foo
  1299. #+begin_src emacs-lisp
  1300. 3
  1301. #+end_src
  1302. #+RESULTS: foo
  1303. : foo
  1304. "
  1305. (dolist (num '(1 2 3))
  1306. ;; execute the block
  1307. (goto-char (point-min))
  1308. (org-babel-next-src-block num) (org-babel-execute-src-block)
  1309. ;; check the results
  1310. (goto-char (point-max))
  1311. (move-beginning-of-line 0)
  1312. (should (looking-at (format ": %d" num))))))
  1313. (ert-deftest test-ob/blocks-with-spaces ()
  1314. "Test expansion of blocks followed by blank lines."
  1315. ;; Preserve number of blank lines after block.
  1316. (should
  1317. (equal "#+BEGIN_SRC emacs-lisp
  1318. \(+ 1 2)
  1319. #+END_SRC
  1320. #+RESULTS:
  1321. : 3\n\n\n"
  1322. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
  1323. \(+ 1 2)
  1324. #+END_SRC\n\n\n"
  1325. (let ((org-babel-results-keyword "RESULTS"))
  1326. (org-babel-execute-src-block))
  1327. (buffer-string))))
  1328. ;; Do not add spurious blank lines after results.
  1329. (should
  1330. (equal
  1331. "
  1332. - item 1
  1333. #+begin_src emacs-lisp
  1334. 0
  1335. #+end_src
  1336. #+RESULTS:
  1337. : 0
  1338. - item 2"
  1339. (org-test-with-temp-text "
  1340. - item 1
  1341. #+begin_src emacs-lisp<point>
  1342. 0
  1343. #+end_src
  1344. - item 2"
  1345. (org-babel-execute-src-block)
  1346. (buffer-string))))
  1347. (should
  1348. (equal
  1349. "
  1350. - item 1
  1351. #+begin_src emacs-lisp
  1352. 1
  1353. #+end_src
  1354. #+RESULTS:
  1355. : 1
  1356. - item 2"
  1357. (org-test-with-temp-text "
  1358. - item 1
  1359. #+begin_src emacs-lisp<point>
  1360. 1
  1361. #+end_src
  1362. #+RESULTS:
  1363. : 1
  1364. - item 2"
  1365. (org-babel-execute-src-block)
  1366. (buffer-string)))))
  1367. (ert-deftest test-ob/results-in-narrowed-buffer ()
  1368. "Test block execution in a narrowed buffer."
  1369. ;; If results don't exist, they should be inserted in visible part
  1370. ;; of the buffer.
  1371. (should
  1372. (equal
  1373. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
  1374. (org-test-with-temp-text
  1375. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1376. (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
  1377. (let ((org-babel-results-keyword "RESULTS"))
  1378. (org-babel-execute-src-block))
  1379. (org-trim (buffer-string)))))
  1380. (should
  1381. (equal
  1382. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
  1383. (org-test-with-temp-text
  1384. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1385. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1386. (let ((org-babel-results-keyword "RESULTS"))
  1387. (org-babel-execute-src-block))
  1388. (org-trim (buffer-string)))))
  1389. ;; Results in visible part of buffer, should be updated here.
  1390. (should
  1391. (equal
  1392. "#+NAME: test
  1393. #+BEGIN_SRC emacs-lisp
  1394. \(+ 1 2)
  1395. #+END_SRC
  1396. #+RESULTS: test
  1397. : 3"
  1398. (org-test-with-temp-text
  1399. "#+NAME: test
  1400. #+BEGIN_SRC emacs-lisp
  1401. \(+ 1 2)
  1402. #+END_SRC
  1403. #+RESULTS: test
  1404. : 4
  1405. <point>
  1406. Paragraph"
  1407. (narrow-to-region (point-min) (point))
  1408. (goto-char (point-min))
  1409. (let ((org-babel-results-keyword "RESULTS"))
  1410. (org-babel-execute-src-block))
  1411. (org-trim (buffer-string)))))
  1412. ;; Results in invisible part of buffer, should be updated there.
  1413. (org-test-with-temp-text
  1414. "#+NAME: test
  1415. #+BEGIN_SRC emacs-lisp
  1416. \(+ 1 2)
  1417. #+END_SRC
  1418. #+RESULTS: test
  1419. : 4
  1420. Paragraph"
  1421. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1422. (let ((org-babel-results-keyword "RESULTS"))
  1423. (org-babel-execute-src-block))
  1424. (should-not (re-search-forward "^#\\+RESULTS:" nil t))
  1425. (widen)
  1426. (should (re-search-forward "^: 3" nil t))))
  1427. (ert-deftest test-ob/specific-colnames ()
  1428. "Test passing specific column names."
  1429. (should
  1430. (equal "#+name: input-table
  1431. | id | var1 |
  1432. |----+------|
  1433. | 1 | bar |
  1434. | 2 | baz |
  1435. #+begin_src sh :var data=input-table :results table :exports results :colnames '(Rev Author)
  1436. echo \"$data\"
  1437. #+end_src
  1438. #+RESULTS:
  1439. | Rev | Author |
  1440. |-----+--------|
  1441. | 1 | bar |
  1442. | 2 | baz |
  1443. "
  1444. (org-test-with-temp-text
  1445. "#+name: input-table
  1446. | id | var1 |
  1447. |----+------|
  1448. | 1 | bar |
  1449. | 2 | baz |
  1450. #+begin_src sh :var data=input-table :results table :exports results :colnames '(Rev Author)
  1451. echo \"$data\"
  1452. #+end_src
  1453. "
  1454. ;; we should find a code block
  1455. (should (re-search-forward org-babel-src-block-regexp nil t))
  1456. (goto-char (match-beginning 0))
  1457. ;; now that we've located the code block, it may be evaluated
  1458. (let ((org-babel-results-keyword "RESULTS"))
  1459. (org-babel-execute-src-block))
  1460. (buffer-string)))))
  1461. (ert-deftest test-ob/location-of-header-arg-eval ()
  1462. "Test location of header argument evaluation."
  1463. (org-test-with-temp-text "
  1464. #+name: top-block
  1465. #+begin_src emacs-lisp :var pt=(point)
  1466. pt
  1467. #+end_src
  1468. #+name: bottom-block
  1469. #+begin_src emacs-lisp :var pt=top-block()
  1470. pt
  1471. #+end_src
  1472. "
  1473. ;; the value of the second block should be greater than the first
  1474. (should
  1475. (< (progn (re-search-forward org-babel-src-block-regexp nil t)
  1476. (goto-char (match-beginning 0))
  1477. (prog1 (save-match-data (org-babel-execute-src-block))
  1478. (goto-char (match-end 0))))
  1479. (progn (re-search-forward org-babel-src-block-regexp nil t)
  1480. (goto-char (match-beginning 0))
  1481. (org-babel-execute-src-block))))))
  1482. (ert-deftest test-ob/preserve-results-indentation ()
  1483. "Preserve indentation when executing a source block."
  1484. (should
  1485. (equal
  1486. '(2 2)
  1487. (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  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. (should
  1493. (equal
  1494. '(2 2)
  1495. (org-test-with-temp-text
  1496. " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1497. (org-babel-execute-src-block)
  1498. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1499. (list (current-indentation)
  1500. (progn (forward-line) (current-indentation))))))
  1501. ;; Don't get fooled by TAB-based indentation.
  1502. (should
  1503. (equal
  1504. '(6 6)
  1505. (org-test-with-temp-text
  1506. "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
  1507. (setq tab-width 4)
  1508. (org-babel-execute-src-block)
  1509. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1510. (list (current-indentation)
  1511. (progn (forward-line) (current-indentation))))))
  1512. ;; Properly indent examplified blocks.
  1513. (should
  1514. (equal
  1515. " #+begin_example
  1516. 0
  1517. 1
  1518. 2
  1519. 3
  1520. 4
  1521. 5
  1522. 6
  1523. 7
  1524. 8
  1525. 9
  1526. #+end_example
  1527. "
  1528. (org-test-with-temp-text
  1529. " #+begin_src emacs-lisp :results output
  1530. (dotimes (i 10) (princ i) (princ \"\\n\"))
  1531. #+end_src"
  1532. (org-babel-execute-src-block)
  1533. (search-forward "begin_example")
  1534. (downcase
  1535. (buffer-substring-no-properties (line-beginning-position)
  1536. (point-max))))))
  1537. ;; Properly indent "org" blocks.
  1538. (should
  1539. (equal
  1540. " #+begin_src org
  1541. 0
  1542. 1
  1543. 2
  1544. 3
  1545. 4
  1546. 5
  1547. 6
  1548. 7
  1549. 8
  1550. 9
  1551. #+end_src
  1552. "
  1553. (org-test-with-temp-text
  1554. " #+begin_src emacs-lisp :results output org
  1555. (dotimes (i 10) (princ i) (princ \"\\n\"))
  1556. #+end_src"
  1557. (org-babel-execute-src-block)
  1558. (search-forward "begin_src org")
  1559. (downcase
  1560. (buffer-substring-no-properties (line-beginning-position)
  1561. (point-max)))))))
  1562. (ert-deftest test-ob/preserve-comma-escape ()
  1563. "Preserve comma escapes when inserting results."
  1564. (should
  1565. (equal
  1566. "#+begin_example
  1567. line 1
  1568. ,* headline 2
  1569. ,* headline 3
  1570. ,* headline 4
  1571. ,* headline 5
  1572. #+end_example
  1573. "
  1574. (org-test-with-temp-text "#+begin_src emacs-lisp :wrap example
  1575. \"line 1
  1576. ,* headline 2
  1577. ,* headline 3
  1578. ,* headline 4
  1579. ,* headline 5
  1580. \"
  1581. #+end_src
  1582. "
  1583. (org-babel-execute-src-block)
  1584. (let ((case-fold-search t)) (search-forward "result" nil t))
  1585. (downcase (buffer-substring-no-properties (line-beginning-position 2)
  1586. (point-max))))))
  1587. (should
  1588. (string-match-p
  1589. ",#"
  1590. (org-test-with-temp-text "#+begin_src emacs-lisp :wrap export foo
  1591. \"#+keyword: value\"
  1592. #+end_src"
  1593. (org-babel-execute-src-block)
  1594. (buffer-string))))
  1595. (should
  1596. (string-match-p
  1597. ",#"
  1598. (org-test-with-temp-text "#+begin_src emacs-lisp :wrap src foo
  1599. \"#+keyword: value\"
  1600. #+end_src"
  1601. (org-babel-execute-src-block)
  1602. (buffer-string))))
  1603. ;; Do not comma-escape when the block is not verbatim.
  1604. (should-not
  1605. (string-match-p
  1606. ",#"
  1607. (org-test-with-temp-text "#+begin_src emacs-lisp :wrap special
  1608. \"#+keyword: value\"
  1609. #+end_src"
  1610. (org-babel-execute-src-block)
  1611. (buffer-string)))))
  1612. (ert-deftest test-ob/safe-header-args ()
  1613. "Detect safe and unsafe header args."
  1614. (let ((safe-args '((:cache . "foo")
  1615. (:results . "output")
  1616. (:eval . "never")
  1617. (:eval . "query")))
  1618. (unsafe-args '((:eval . "yes")
  1619. (:results . "output file")
  1620. (:foo . "bar")))
  1621. (malformed-args '((bar . "foo")
  1622. ("foo" . "bar")
  1623. :foo))
  1624. (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
  1625. (dolist (arg safe-args)
  1626. (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
  1627. (dolist (arg unsafe-args)
  1628. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1629. (dolist (arg malformed-args)
  1630. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1631. (should (not (funcall safe-p (append safe-args unsafe-args))))))
  1632. (ert-deftest test-ob/noweb-expansions-in-cache ()
  1633. "Ensure that noweb expansions are expanded before caching."
  1634. (defvar noweb-expansions-in-cache-var)
  1635. (let ((noweb-expansions-in-cache-var 0))
  1636. (org-test-with-temp-text "
  1637. #+name: foo
  1638. #+begin_src emacs-lisp
  1639. \"I said\"
  1640. #+end_src
  1641. #+name: bar
  1642. #+begin_src emacs-lisp :noweb yes :cache yes
  1643. (setq noweb-expansions-in-cache-var
  1644. (+ 1 noweb-expansions-in-cache-var))
  1645. (concat <<foo>> \" check noweb expansions\")
  1646. #+end_src
  1647. "
  1648. ;; run the second block to create the cache
  1649. (goto-char (point-min))
  1650. (re-search-forward (regexp-quote "#+name: bar"))
  1651. (should (string= "I said check noweb expansions"
  1652. (org-babel-execute-src-block)))
  1653. (should (= noweb-expansions-in-cache-var 1))
  1654. ;; change the value of the first block
  1655. (goto-char (point-min))
  1656. (re-search-forward (regexp-quote "said"))
  1657. (goto-char (match-beginning 0))
  1658. (insert "haven't ")
  1659. (re-search-forward (regexp-quote "#+name: bar"))
  1660. (should (string= "I haven't said check noweb expansions"
  1661. (org-babel-execute-src-block)))
  1662. (should (= noweb-expansions-in-cache-var 2)))))
  1663. (ert-deftest test-ob/file-ext-and-output-dir ()
  1664. (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
  1665. (org-babel-next-src-block)
  1666. (should (equal "file-ext-basic.txt"
  1667. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1668. (org-babel-next-src-block)
  1669. (should (equal "foo/file-ext-dir-relative.txt"
  1670. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1671. (org-babel-next-src-block)
  1672. (should (equal "foo/file-ext-dir-relative-slash.txt"
  1673. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1674. (org-babel-next-src-block)
  1675. (should (equal "/tmp/file-ext-dir-absolute.txt"
  1676. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1677. (org-babel-next-src-block)
  1678. (should (equal "foo.bar"
  1679. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1680. (org-babel-next-src-block)
  1681. (should (equal "xxx/foo.bar"
  1682. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1683. ))
  1684. (ert-deftest test-ob/file-mode ()
  1685. "Ensure that :file-mode results in expected permissions."
  1686. (should
  1687. (equal #o755
  1688. (org-test-with-temp-text-in-file "
  1689. #+begin_src emacs-lisp :results file :file t.sh :file-mode (identity #o755)
  1690. nil
  1691. #+end_src"
  1692. (org-babel-next-src-block)
  1693. (org-babel-execute-src-block)
  1694. (unwind-protect
  1695. (file-modes "t.sh")
  1696. (delete-file "t.sh"))))))
  1697. (ert-deftest test-ob-core/dir-attach ()
  1698. "Test :dir header using special 'attach value"
  1699. (should
  1700. (org-test-with-temp-text-in-file
  1701. "* 'attach Symbol
  1702. <point>#+begin_src elisp :dir 'attach :results file
  1703. (with-temp-file \"test.txt\" (insert \"attachment testing\n\"))
  1704. \"test.txt\"
  1705. #+end_src"
  1706. (org-id-get-create)
  1707. (org-babel-execute-src-block)
  1708. (goto-char (org-babel-where-is-src-block-result))
  1709. (forward-line)
  1710. (and
  1711. (file-exists-p (format "%s/test.txt" (org-attach-dir nil t)))
  1712. (string= (buffer-substring-no-properties (point) (line-end-position))
  1713. "[[attachment:test.txt]]"))))
  1714. (should
  1715. (org-test-with-temp-text-in-file
  1716. "* 'attach String
  1717. <point>#+begin_src elisp :dir \"'attach\" :results file
  1718. (with-temp-file \"test.txt\" (insert \"attachment testing\n\"))
  1719. \"test.txt\"
  1720. #+end_src"
  1721. (org-id-get-create)
  1722. (org-babel-execute-src-block)
  1723. (goto-char (org-babel-where-is-src-block-result))
  1724. (forward-line)
  1725. (and
  1726. (file-exists-p (format "%s/test.txt" (org-attach-dir nil t)))
  1727. (string= (buffer-substring-no-properties (point) (line-end-position))
  1728. "[[attachment:test.txt]]"))))
  1729. (should
  1730. (org-test-with-temp-text-in-file
  1731. "* 'attach with Existing DIR property
  1732. :PROPERTIES:
  1733. :DIR: custom-attach-dir
  1734. :END:
  1735. <point>#+begin_src elisp :dir 'attach :results file
  1736. (with-temp-file \"test.txt\" (insert \"attachment testing\n\"))
  1737. \"test.txt\"
  1738. #+end_src"
  1739. (message "DIR: %s" (org-attach-dir t))
  1740. (org-babel-execute-src-block)
  1741. (goto-char (org-babel-where-is-src-block-result))
  1742. (forward-line)
  1743. (and
  1744. (file-exists-p (format "%s/test.txt" (org-attach-dir nil t)))
  1745. (string= (buffer-substring-no-properties (point) (line-end-position))
  1746. "[[attachment:test.txt]]"))))
  1747. (should-error
  1748. (org-test-with-temp-text-in-file
  1749. "* 'attach with no ID or DIR
  1750. <point>#+begin_src elisp :dir 'attach :results file
  1751. (with-temp-file \"test.txt\" (insert \"attachment testing\n\"))
  1752. \"test.txt\"
  1753. #+end_src"
  1754. (org-babel-execute-src-block)
  1755. (goto-char (org-babel-where-is-src-block-result))
  1756. (forward-line)
  1757. (and
  1758. (file-exists-p (format "%s/test.txt" (org-attach-dir nil t)))
  1759. (string= (buffer-substring-no-properties (point) (line-end-position))
  1760. "[[attachment:test.txt]]")))))
  1761. (ert-deftest test-ob-core/dir-mkdirp ()
  1762. "Test :mkdirp with :dir header combination."
  1763. (should-not
  1764. (org-test-with-temp-text-in-file
  1765. "#+begin_src emacs-lisp :dir \"data/code\"
  1766. t
  1767. #+end_src"
  1768. (org-babel-execute-src-block)
  1769. (message default-directory)
  1770. (file-directory-p "data/code")))
  1771. (should-not
  1772. (org-test-with-temp-text-in-file
  1773. "#+begin_src emacs-lisp :mkdirp no :dir \"data/code\"
  1774. t
  1775. #+end_src"
  1776. (org-babel-execute-src-block)
  1777. (message default-directory)
  1778. (file-directory-p "data/code")))
  1779. (should
  1780. (org-test-with-temp-text-in-file
  1781. "#+begin_src emacs-lisp :mkdirp yes :dir \"data/code\"
  1782. t
  1783. #+end_src"
  1784. (org-babel-execute-src-block)
  1785. (message default-directory)
  1786. (prog1 (file-directory-p "data/code")
  1787. (delete-directory "data" t))))
  1788. (should
  1789. (equal "/tmp/test-dir-no-mkdirp/"
  1790. (org-test-with-temp-text-in-file
  1791. "#+begin_src emacs-lisp :dir /tmp/test-dir-no-mkdirp
  1792. default-directory
  1793. #+end_src"
  1794. (org-babel-execute-src-block)))))
  1795. (ert-deftest test-ob/script-escape ()
  1796. ;; Empty list.
  1797. (should (equal nil (org-babel-script-escape "[]")))
  1798. (should (equal nil (org-babel-script-escape "()")))
  1799. (should (equal nil (org-babel-script-escape "'()")))
  1800. (should (equal nil (org-babel-script-escape "{}")))
  1801. ;; Delimited lists of numbers
  1802. (should (equal '(1 2 3)
  1803. (org-babel-script-escape "[1 2 3]")))
  1804. (should (equal '(1 2 3)
  1805. (org-babel-script-escape "{1 2 3}")))
  1806. (should (equal '(1 2 3)
  1807. (org-babel-script-escape "(1 2 3)")))
  1808. ;; Delimited lists of double-quoted strings
  1809. (should (equal '("foo" "bar")
  1810. (org-babel-script-escape "(\"foo\" \"bar\")")))
  1811. (should (equal '("foo" "bar")
  1812. (org-babel-script-escape "[\"foo\" \"bar\"]")))
  1813. (should (equal '("foo" "bar")
  1814. (org-babel-script-escape "{\"foo\" \"bar\"}")))
  1815. ;; ... with commas
  1816. (should (equal '("foo" "bar")
  1817. (org-babel-script-escape "(\"foo\", \"bar\")")))
  1818. (should (equal '("foo" "bar")
  1819. (org-babel-script-escape "[\"foo\", \"bar\"]")))
  1820. (should (equal '("foo" "bar")
  1821. (org-babel-script-escape "{\"foo\", \"bar\"}")))
  1822. ;; Delimited lists of single-quoted strings
  1823. (should (equal '("foo" "bar")
  1824. (org-babel-script-escape "('foo' 'bar')")))
  1825. (should (equal '("foo" "bar")
  1826. (org-babel-script-escape "['foo' 'bar']")))
  1827. (should (equal '("foo" "bar")
  1828. (org-babel-script-escape "{'foo' 'bar'}")))
  1829. ;; ... with commas
  1830. (should (equal '("foo" "bar")
  1831. (org-babel-script-escape "('foo', 'bar')")))
  1832. (should (equal '("foo" "bar")
  1833. (org-babel-script-escape "['foo', 'bar']")))
  1834. (should (equal '("foo" "bar")
  1835. (org-babel-script-escape "{'foo', 'bar'}")))
  1836. ;; Single quoted strings
  1837. (should (equal "foo"
  1838. (org-babel-script-escape "'foo'")))
  1839. ;; ... with internal double quote
  1840. (should (equal "foo\"bar"
  1841. (org-babel-script-escape "'foo\"bar'")))
  1842. ;; ... with internal backslash
  1843. (should (equal "foo\\bar"
  1844. (org-babel-script-escape "'foo\\bar'")))
  1845. ;; ... with internal escaped backslash
  1846. (should (equal "foo\\bar"
  1847. (org-babel-script-escape "'foo\\\\bar'")))
  1848. ;; ... with internal backslash-double quote
  1849. (should (equal "foo\\\"bar"
  1850. (org-babel-script-escape "'foo\\\"bar'")))
  1851. ;; ... with internal escaped backslash-double quote
  1852. (should (equal "foo\\\"bar"
  1853. (org-babel-script-escape "'foo\\\\\"bar'")))
  1854. ;; ... with internal escaped single quote
  1855. (should (equal "foo'bar"
  1856. (org-babel-script-escape "'foo\\'bar'")))
  1857. ;; ... with internal escaped backslash-escaped single quote
  1858. (should (equal "foo\\'bar"
  1859. (org-babel-script-escape "'foo\\\\\\'bar'")))
  1860. ;; Double quoted strings
  1861. (should (equal "foo"
  1862. (org-babel-script-escape "\"foo\"")))
  1863. ;; ... with internal single quote
  1864. (should (equal "foo'bar"
  1865. (org-babel-script-escape "\"foo'bar\"")))
  1866. ;; ... with internal backslash
  1867. (should (equal "foo\\bar"
  1868. (org-babel-script-escape "\"foo\\bar\"")))
  1869. ;; ... with internal escaped backslash
  1870. (should (equal "foo\\bar"
  1871. (org-babel-script-escape "\"foo\\\\bar\"")))
  1872. ;; ... with internal backslash-single quote
  1873. (should (equal "foo\\'bar"
  1874. (org-babel-script-escape "\"foo\\'bar\"")))
  1875. ;; ... with internal escaped backslash-single quote
  1876. (should (equal "foo\\'bar"
  1877. (org-babel-script-escape "\"foo\\\\'bar\"")))
  1878. ;; ... with internal escaped double quote
  1879. (should (equal "foo\"bar"
  1880. (org-babel-script-escape "\"foo\\\"bar\"")))
  1881. ;; ... with internal escaped backslash-escaped double quote
  1882. (should (equal "foo\\\"bar"
  1883. (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
  1884. (ert-deftest test-ob/process-params-no-duplicates ()
  1885. (should
  1886. (equal (org-babel-process-params '((:colname-names)
  1887. (:rowname-names)
  1888. (:result-params)
  1889. (:result-type)
  1890. (:var . "\"foo\"")))
  1891. '((:var)
  1892. (:colname-names)
  1893. (:rowname-names)
  1894. (:result-params)
  1895. (:result-type . value)))))
  1896. (defun org-test-babel-confirm-evaluate (eval-value)
  1897. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  1898. nil
  1899. #+end_src" eval-value)
  1900. (goto-char (point-min))
  1901. (let ((info (org-babel-get-src-block-info)))
  1902. (org-babel-check-confirm-evaluate info))))
  1903. (ert-deftest test-ob/check-eval ()
  1904. (let ((org-confirm-babel-evaluate t))
  1905. ;; Non-export tests
  1906. (dolist (pair '(("no" . nil)
  1907. ("never" . nil)
  1908. ("query" . query)
  1909. ("yes" . query)))
  1910. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1911. ;; Export tests
  1912. (let ((org-babel-exp-reference-buffer t))
  1913. (dolist (pair '(("no" . nil)
  1914. ("never" . nil)
  1915. ("query" . query)
  1916. ("yes" . query)
  1917. ("never-export" . nil)
  1918. ("no-export" . nil)
  1919. ("query-export" . query)))
  1920. (message (car pair))
  1921. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))))
  1922. (let ((org-confirm-babel-evaluate nil))
  1923. ;; Non-export tests
  1924. (dolist (pair '(("no" . nil)
  1925. ("never" . nil)
  1926. ("query" . query)
  1927. ("yes" . t)))
  1928. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair))))
  1929. ;; Export tests
  1930. (let ((org-babel-exp-reference-buffer t))
  1931. (dolist (pair '(("no" . nil)
  1932. ("never" . nil)
  1933. ("query" . query)
  1934. ("yes" . t)
  1935. ("never-export" . nil)
  1936. ("no-export" . nil)
  1937. ("query-export" . query)))
  1938. (message (car pair))
  1939. (should (eq (org-test-babel-confirm-evaluate (car pair)) (cdr pair)))))))
  1940. (ert-deftest test-ob/check-eval-noweb-expanded ()
  1941. "`org-confirm-babel-evaluate' function receives expanded noweb refs."
  1942. (should
  1943. (equal t
  1944. (org-test-with-temp-text "
  1945. #+name: foo
  1946. #+begin_src emacs-lisp
  1947. :bar
  1948. #+end_src
  1949. <point>#+begin_src emacs-lisp :noweb yes
  1950. <<foo>>
  1951. #+end_src"
  1952. (let ((org-confirm-babel-evaluate
  1953. (lambda (_ body)
  1954. (not (string-match-p ":bar" body)))))
  1955. (org-babel-check-confirm-evaluate
  1956. (org-babel-get-src-block-info))))))
  1957. ;; The code block passed to `org-confirm-babel-evaluate' does not
  1958. ;; include coderefs.
  1959. (should
  1960. (equal t
  1961. (org-test-with-temp-text "
  1962. #+name: foo
  1963. #+begin_src emacs-lisp
  1964. :bar
  1965. #+end_src
  1966. <point>#+begin_src emacs-lisp :noweb yes
  1967. #(ref:foo)
  1968. <<foo>>
  1969. #+end_src"
  1970. (let ((org-coderef-label-format "#(ref:%s)")
  1971. (org-confirm-babel-evaluate
  1972. (lambda (_ body)
  1973. (string-match-p "ref:foo" body))))
  1974. (org-babel-check-confirm-evaluate
  1975. (org-babel-get-src-block-info)))))))
  1976. (defun org-test-ob/update-block-body ()
  1977. "Test `org-babel-update-block-body' specifications."
  1978. (should
  1979. (equal "#+begin_src elisp\n 2\n#+end_src"
  1980. (let ((org-edit-src-content-indentation 2))
  1981. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1982. (org-babel-update-block-body "2")
  1983. (buffer-string)))))
  1984. ;; Preserve block indentation.
  1985. (should
  1986. (equal " #+begin_src elisp\n 2\n #+end_src"
  1987. (let ((org-edit-src-content-indentation 1))
  1988. (org-test-with-temp-text
  1989. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  1990. (org-babel-update-block-body "2")
  1991. (buffer-string)))))
  1992. ;; Ignore NEW-BODY global indentation.
  1993. (should
  1994. (equal "#+begin_src elisp\n 2\n#+end_src"
  1995. (let ((org-edit-src-content-indentation 2))
  1996. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  1997. (org-babel-update-block-body " 2")
  1998. (buffer-string)))))
  1999. ;; When indentation should be preserved ignore the two rules above.
  2000. (should
  2001. (equal " #+begin_src elisp\n2\n #+end_src"
  2002. (let ((org-edit-src-content-indentation 1)
  2003. (org-src-preserve-indentation t))
  2004. (org-test-with-temp-text
  2005. " #+begin_src elisp\n (+ 1 1)\n #+end_src"
  2006. (org-babel-update-block-body "2")
  2007. (buffer-string)))))
  2008. (should
  2009. (equal " #+begin_src elisp -i\n2\n #+end_src"
  2010. (let ((org-edit-src-content-indentation 1))
  2011. (org-test-with-temp-text
  2012. " #+begin_src elisp -i\n (+ 1 1)\n #+end_src"
  2013. (org-babel-update-block-body "2")
  2014. (buffer-string)))))
  2015. (should
  2016. (equal "#+begin_src elisp\n 2\n#+end_src"
  2017. (let ((org-edit-src-content-indentation 2)
  2018. (org-src-preserve-indentation t))
  2019. (org-test-with-temp-text "#+begin_src elisp\n(+ 1 1)\n#+end_src"
  2020. (org-babel-update-block-body " 2")
  2021. (buffer-string)))))
  2022. (should
  2023. (equal "#+begin_src elisp -i\n 2\n#+end_src"
  2024. (let ((org-edit-src-content-indentation 2)
  2025. (org-src-preserve-indentation t))
  2026. (org-test-with-temp-text "#+begin_src elisp -i\n(+ 1 1)\n#+end_src"
  2027. (org-babel-update-block-body " 2")
  2028. (buffer-string))))))
  2029. (ert-deftest test-ob/find-named-result ()
  2030. "Test `org-babel-find-named-result' specifications."
  2031. (should
  2032. (= 1
  2033. (org-test-with-temp-text "#+results: foo\n: result"
  2034. (org-babel-find-named-result "foo"))))
  2035. (should-not
  2036. (org-test-with-temp-text "#+results: foo\n: result"
  2037. (org-babel-find-named-result "bar")))
  2038. (should-not
  2039. (org-test-with-temp-text "#+results: foobar\n: result"
  2040. (org-babel-find-named-result "foo")))
  2041. ;; Search is case insensitive.
  2042. (should
  2043. (org-test-with-temp-text "#+RESULTS: FOO\n: result"
  2044. (org-babel-find-named-result "foo")))
  2045. ;; Handle hash in results keyword.
  2046. (should
  2047. (org-test-with-temp-text "#+results[hash]: FOO\n: result"
  2048. (org-babel-find-named-result "foo")))
  2049. ;; Accept orphaned affiliated keywords.
  2050. (should
  2051. (org-test-with-temp-text "#+results: foo"
  2052. (org-babel-find-named-result "foo"))))
  2053. (ert-deftest test-ob/where-is-src-block-result ()
  2054. "Test `org-babel-where-is-src-block-result' specifications."
  2055. ;; Find anonymous results.
  2056. (should
  2057. (equal "#+RESULTS:"
  2058. (org-test-with-temp-text
  2059. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS:\n: 2"
  2060. (goto-char (org-babel-where-is-src-block-result))
  2061. (buffer-substring-no-properties (point) (line-end-position)))))
  2062. ;; Find named results. Those have priority over anonymous ones.
  2063. (should
  2064. (equal "#+RESULTS: example"
  2065. (org-test-with-temp-text
  2066. "
  2067. <point>#+NAME: example
  2068. #+BEGIN_SRC emacs-lisp
  2069. \(+ 1 1)
  2070. #+END_SRC
  2071. #+RESULTS: example
  2072. : 2"
  2073. (goto-char (org-babel-where-is-src-block-result))
  2074. (buffer-substring-no-properties (point) (line-end-position)))))
  2075. (should
  2076. (equal "#+RESULTS: example"
  2077. (org-test-with-temp-text
  2078. "
  2079. <point>#+NAME: example
  2080. #+BEGIN_SRC emacs-lisp
  2081. \(+ 1 1)
  2082. #+END_SRC
  2083. #+RESULTS:
  2084. : fake
  2085. #+RESULTS: example
  2086. : 2"
  2087. (goto-char (org-babel-where-is-src-block-result))
  2088. (buffer-substring-no-properties (point) (line-end-position)))))
  2089. ;; Return nil when no result is found.
  2090. (should-not
  2091. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2092. (org-babel-where-is-src-block-result)))
  2093. (should-not
  2094. (org-test-with-temp-text
  2095. "- item\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n"
  2096. (org-babel-where-is-src-block-result)))
  2097. ;; When optional argument INSERT is non-nil, add RESULTS keyword
  2098. ;; whenever no RESULTS can be found.
  2099. (should
  2100. (equal
  2101. "#+RESULTS:"
  2102. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2103. (let ((org-babel-results-keyword "RESULTS"))
  2104. (goto-char (org-babel-where-is-src-block-result t)))
  2105. (buffer-substring-no-properties (point) (line-end-position)))))
  2106. ;; Insert a named RESULTS keyword if possible.
  2107. (should
  2108. (equal
  2109. "#+RESULTS: e"
  2110. (org-test-with-temp-text
  2111. "#+NAME: e\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2112. (let ((org-babel-results-keyword "RESULTS"))
  2113. (goto-char (org-babel-where-is-src-block-result t)))
  2114. (buffer-substring-no-properties (point) (line-end-position)))))
  2115. ;; When optional argument HASH is provided, clear RESULTS keyword
  2116. ;; and related contents if they do not match it.
  2117. (should
  2118. (equal
  2119. "#+RESULTS[bbbb]:"
  2120. (org-test-with-temp-text
  2121. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:\n: 3"
  2122. (let ((org-babel-results-keyword "RESULTS"))
  2123. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  2124. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  2125. (should
  2126. (equal
  2127. "#+RESULTS[bbbb]:"
  2128. (org-test-with-temp-text
  2129. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n\n#+RESULTS[aaaa]:"
  2130. (let ((org-babel-results-keyword "RESULTS"))
  2131. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  2132. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  2133. ;; Handle hashes with times.
  2134. (should
  2135. (equal
  2136. "#+RESULTS[(2014-03-04 00:41:10) bbbb]:"
  2137. (org-test-with-temp-text
  2138. "#+BEGIN_SRC emacs-lisp
  2139. \(+ 1 1)
  2140. #+END_SRC
  2141. #+RESULTS[(2012-03-29 16:40:12) aaaa]:"
  2142. (let ((org-babel-results-keyword "RESULTS")
  2143. (org-babel-hash-show-time t))
  2144. (cl-letf (((symbol-function 'format-time-string)
  2145. (lambda (&rest _) "(2014-03-04 00:41:10)")))
  2146. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb"))
  2147. (org-trim (buffer-substring-no-properties (point) (point-max))))))))
  2148. (should
  2149. (equal
  2150. "#+RESULTS[(2012-03-29 16:40:12) aaaa]:"
  2151. (org-test-with-temp-text
  2152. "
  2153. <point>#+BEGIN_SRC emacs-lisp
  2154. \(+ 1 1)
  2155. #+END_SRC
  2156. #+RESULTS[(2012-03-29 16:40:12) aaaa]:"
  2157. (let ((org-babel-results-keyword "RESULTS")
  2158. (org-babel-hash-show-time t))
  2159. (cl-letf (((symbol-function 'format-time-string)
  2160. (lambda (&rest _) "(2014-03-04 00:41:10)")))
  2161. (goto-char (org-babel-where-is-src-block-result nil nil "aaaa"))
  2162. (org-trim (buffer-substring-no-properties (point) (point-max))))))))
  2163. ;; RESULTS keyword may not be the last affiliated keyword.
  2164. (should
  2165. (equal
  2166. "#+RESULTS[bbbb]:"
  2167. (org-test-with-temp-text
  2168. "
  2169. <point>#+BEGIN_SRC emacs-lisp
  2170. \(+ 1 1)
  2171. #+END_SRC
  2172. #+RESULTS[aaaa]:
  2173. #+NAME: e
  2174. : 3"
  2175. (let ((org-babel-results-keyword "RESULTS"))
  2176. (goto-char (org-babel-where-is-src-block-result nil nil "bbbb")))
  2177. (org-trim (buffer-substring-no-properties (point) (point-max))))))
  2178. ;; HASH does nothing if no RESULTS can be found. However, if INSERT
  2179. ;; is also non-nil, RESULTS keyword is inserted along with the
  2180. ;; expected hash.
  2181. (should
  2182. (equal
  2183. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2184. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2185. (org-babel-where-is-src-block-result nil nil "bbbb")
  2186. (buffer-string))))
  2187. (should
  2188. (equal
  2189. "#+RESULTS[bbbb]:"
  2190. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2191. (let ((org-babel-results-keyword "RESULTS"))
  2192. (goto-char (org-babel-where-is-src-block-result t nil "bbbb")))
  2193. (org-trim (buffer-substring-no-properties (point) (point-max)))))))
  2194. (ert-deftest test-ob/goto-named-src-block ()
  2195. "Test interactive use of `org-babel-goto-named-src-block'."
  2196. (org-test-with-temp-text-in-file
  2197. "
  2198. #+NAME: abc
  2199. #+BEGIN_SRC emacs-lisp :results value
  2200. (1+ 1)
  2201. #+END_SRC
  2202. #+CALL: abc( lorem() ) :results raw :wrap EXAMPLE
  2203. #+BEGIN_SRC emacs-lisp
  2204. <<abc>>
  2205. #+END_SRC
  2206. abc
  2207. #+RESULTS: abc
  2208. : 2
  2209. "
  2210. ;; non-existent name
  2211. (should-not
  2212. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nno-name\n"))
  2213. ;; correct name
  2214. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\nabc\n")
  2215. (should (= 14 (point)))
  2216. ;; call line - autocompletion
  2217. (forward-line 3)
  2218. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  2219. (should (= 14 (point)))
  2220. ;; noweb reference - autocompletion
  2221. (forward-line 5)
  2222. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  2223. (should (= 14 (point)))
  2224. ;; at symbol - autocompletion
  2225. (forward-line 7)
  2226. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  2227. (should (= 14 (point)))
  2228. ;; in results - autocompletion
  2229. (forward-line 8)
  2230. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  2231. (should (= 14 (point)))
  2232. (forward-line 9)
  2233. (execute-kbd-macro "\M-xorg-babel-goto-named-src-block\n\n")
  2234. (should (= 14 (point)))))
  2235. (ert-deftest test-ob/evaluate-body-with-coderefs ()
  2236. (should
  2237. (= 2
  2238. (org-test-with-temp-text
  2239. "#+begin_src emacs-lisp -l \"#(ref:%s)\"\n2 #(ref:foo)\n#+end_src"
  2240. (org-babel-execute-src-block))))
  2241. (should
  2242. (= 3
  2243. (org-test-with-temp-text
  2244. "#+begin_src emacs-lisp\n3 #(ref:foo)\n#+end_src"
  2245. (let ((org-coderef-label-format "#(ref:%s)"))
  2246. (org-babel-execute-src-block))))))
  2247. (ert-deftest test-ob/string-to-number ()
  2248. (should (= 0 (org-babel--string-to-number "0")))
  2249. (should (= 1 (org-babel--string-to-number "1")))
  2250. (should (eq nil (org-babel--string-to-number "1 2")))
  2251. (should (= 1000.0 (org-babel--string-to-number "1e3")))
  2252. (should (eq 0 (org-babel--string-to-number "000")))
  2253. (should (eq 1 (org-babel--string-to-number "001")))
  2254. (should (eq 10 (org-babel--string-to-number "010")))
  2255. (should (= 100 (org-babel--string-to-number "100")))
  2256. (should (= 0.1 (org-babel--string-to-number "0.1")))
  2257. (should (= 1.0 (org-babel--string-to-number "1.0"))))
  2258. (ert-deftest test-ob/import-elisp-from-file ()
  2259. "Test `org-babel-import-elisp-from-file'."
  2260. (should
  2261. (equal
  2262. (org-test-with-temp-text-in-file "line 1\nline 2\n"
  2263. (cl-letf (((symbol-function 'display-warning)
  2264. (lambda (&rest _) (error "No warnings should occur"))
  2265. (org-table-convert-region-max-lines 2)))
  2266. (org-babel-import-elisp-from-file (buffer-file-name))))
  2267. '(("line" 1)
  2268. ("line" 2))))
  2269. ;; If an error occurs during table conversion, it is shown with
  2270. ;; `display-warning' rather than as a message to make sure the
  2271. ;; caller sees it.
  2272. (should-error
  2273. (org-test-with-temp-text-in-file "line 1\nline 2\n"
  2274. (cl-letf (((symbol-function 'display-warning)
  2275. (lambda (&rest _) (error "Warning should be displayed")))
  2276. (org-table-convert-region-max-lines 1))
  2277. (org-babel-import-elisp-from-file (buffer-file-name)))))
  2278. ;; But an empty file (as is the case when there are no execution
  2279. ;; results) does not trigger a warning.
  2280. (should-not
  2281. (org-test-with-temp-text-in-file ""
  2282. (cl-letf (((symbol-function 'display-warning)
  2283. (lambda (&rest _) (error "No warnings should occur"))))
  2284. (org-babel-import-elisp-from-file (buffer-file-name))))))
  2285. (provide 'test-ob)
  2286. ;;; test-ob ends here