test-ob.el 55 KB

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