test-ob.el 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  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. :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. (let ((info (org-babel-get-src-block-info)))
  130. (should (= 42 (org-babel-execute-src-block))))))
  131. (ert-deftest test-ob/simple-named-code-block ()
  132. "Test that simple named code blocks can be evaluated."
  133. (org-test-with-temp-text-in-file "
  134. #+name: i-have-a-name
  135. #+begin_src emacs-lisp
  136. 42
  137. #+end_src"
  138. (org-babel-next-src-block 1)
  139. (should (= 42 (org-babel-execute-src-block)))))
  140. (ert-deftest test-ob/simple-variable-resolution ()
  141. "Test that simple variable resolution is working."
  142. (org-test-with-temp-text-in-file "
  143. #+name: four
  144. #+begin_src emacs-lisp
  145. (list 1 2 3 4)
  146. #+end_src
  147. #+begin_src emacs-lisp :var four=four
  148. (length four)
  149. #+end_src"
  150. (org-babel-next-src-block 2)
  151. (should (= 4 (org-babel-execute-src-block)))
  152. (forward-line 5)
  153. (should (string= ": 4" (buffer-substring
  154. (point-at-bol)
  155. (point-at-eol))))))
  156. (ert-deftest test-ob/multi-line-header-arguments ()
  157. "Test that multi-line header arguments and can be read."
  158. (org-test-with-temp-text-in-file "
  159. #+headers: :var letters='(a b c d e f g)
  160. #+begin_src emacs-lisp :var numbers='(1 2 3 4 5 6 7)
  161. (require 'cl)
  162. (defalias 'my-map (if (org-version-check \"24.2.50\" \"cl\" :predicate)
  163. 'cl-map
  164. 'map))
  165. (my-map 'list #'list numbers letters)
  166. #+end_src"
  167. (org-babel-next-src-block)
  168. (let ((results (org-babel-execute-src-block)))
  169. (should(equal 'a (cadr (assoc 1 results))))
  170. (should(equal 'd (cadr (assoc 4 results)))))))
  171. (ert-deftest test-ob/parse-header-args ()
  172. (org-test-with-temp-text-in-file "
  173. #+begin_src example-lang :session :results output :var num=9
  174. the body
  175. #+end_src"
  176. (org-babel-next-src-block)
  177. (let* ((info (org-babel-get-src-block-info))
  178. (params (nth 2 info)))
  179. (message "%S" params)
  180. (should (equal "example-lang" (nth 0 info)))
  181. (should (string= "the body" (org-babel-trim (nth 1 info))))
  182. (should-not (member '(:session\ \ \ \ ) params))
  183. (should (equal '(:session) (assoc :session params)))
  184. (should (equal '(:result-type . output) (assoc :result-type params)))
  185. (should (equal '(num . 9) (cdr (assoc :var params)))))))
  186. (ert-deftest test-ob/parse-header-args2 ()
  187. (org-test-with-temp-text-in-file "
  188. * resolving sub-trees as references
  189. #+begin_src emacs-lisp :var text=d4faa7b3-072b-4dcf-813c-dd7141c633f3
  190. (length text)
  191. #+end_src
  192. #+begin_src org :noweb yes
  193. <<simple-subtree>>
  194. <<d4faa7b3-072b-4dcf-813c-dd7141c633f3>>
  195. #+end_src
  196. ** simple subtree with custom ID
  197. :PROPERTIES:
  198. :CUSTOM_ID: simple-subtree
  199. :END:
  200. this is simple"
  201. (should (string-match (regexp-quote "this is simple")
  202. (org-babel-ref-resolve "simple-subtree")))
  203. (org-babel-next-src-block)
  204. (should (= 14 (org-babel-execute-src-block)))))
  205. (ert-deftest test-ob/inline-src-blocks ()
  206. (should
  207. (= 1
  208. (org-test-with-temp-text
  209. "In the middle <point>src_emacs-lisp{(+ 0 1)} of a line"
  210. (org-babel-execute-src-block))))
  211. (should
  212. (= 2
  213. (org-test-with-temp-text
  214. "One at the end of a line: <point>src_emacs-lisp{(+ 1 1)}"
  215. (org-babel-execute-src-block))))
  216. (should
  217. (= 3
  218. (org-test-with-temp-text
  219. "src_emacs-lisp{(+ 2 1)} at the beginning of a line."
  220. (org-babel-execute-src-block))))
  221. (should
  222. (= 4
  223. (org-test-with-temp-text
  224. "In the middle <point>src_emacs-lisp[:results silent\
  225. :exports code]{(+ 3 1)} of a line"
  226. (org-babel-execute-src-block))))
  227. (should
  228. (= 5
  229. (org-test-with-temp-text
  230. "One at the end of a line: <point>src_emacs-lisp[:results silent\
  231. :exports code]{(+ 4 1)}"
  232. (org-babel-execute-src-block))))
  233. (should
  234. (= 6
  235. (org-test-with-temp-text
  236. "src_emacs-lisp[:results silent :exports code]{(+ 5 1)}\
  237. at the beginning of a line."
  238. (org-babel-execute-src-block))))
  239. (should
  240. (= 7
  241. (org-test-with-temp-text
  242. "One also evaluated: <point>src_emacs-lisp[:exports both\
  243. :results silent]{(+ 6 1)}"
  244. (org-babel-execute-src-block)))))
  245. (ert-deftest test-ob/inline-src_blk-default-results-replace-line-1 ()
  246. (let ((test-line "src_sh{echo 1}")
  247. (org-babel-inline-result-wrap "=%s="))
  248. ;; src_ at bol line 1...
  249. (org-test-with-temp-text
  250. test-line
  251. (goto-char (point-min)) (org-babel-execute-maybe)
  252. (should (string=
  253. (concat test-line " {{{results(=1=)}}}")
  254. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  255. (forward-char) (org-babel-execute-maybe)
  256. (should (string=
  257. (concat test-line " {{{results(=1=)}}}")
  258. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  259. (re-search-forward "{{{")
  260. ;;(should-error (org-ctrl-c-ctrl-c))
  261. (backward-char 4) ;; last char of block body
  262. (org-babel-execute-maybe)
  263. (should (string=
  264. (concat test-line " {{{results(=1=)}}}")
  265. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  266. ;; src_ follows space line 1...
  267. (let ((test-line " src_emacs-lisp{ 1 }"))
  268. (org-test-with-temp-text
  269. test-line
  270. (should-error (org-ctrl-c-ctrl-c))
  271. (forward-char) (org-babel-execute-maybe)
  272. (should (string=
  273. (concat test-line " {{{results(=1=)}}}")
  274. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  275. (re-search-forward "{ 1 ") (org-babel-execute-maybe)
  276. (should (string=
  277. (concat test-line " {{{results(=1=)}}}")
  278. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  279. (forward-char 6)
  280. (should-error (org-ctrl-c-ctrl-c))))
  281. ;; Results on a subsequent line are replaced.
  282. (should
  283. (equal
  284. "src_emacs-lisp{(+ 1 2)}\n {{{results(=3=)}}}"
  285. (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n {{{results(=2=)}}}"
  286. (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
  287. (buffer-string))))
  288. ;; Also handle results at the beginning of a line.
  289. (should
  290. (equal
  291. "src_emacs-lisp{(+ 1 2)}\n{{{results(=3=)}}}"
  292. (org-test-with-temp-text "src_emacs-lisp{(+ 1 2)}\n{{{results(=2=)}}}"
  293. (let ((org-babel-inline-result-wrap "=%s=")) (org-babel-execute-maybe))
  294. (buffer-string))))))
  295. (ert-deftest test-ob/inline-src_blk-default-results-replace-line-2 ()
  296. ;; src_ at bol line 2...
  297. (let ((test-line " src_emacs-lisp{ \"x\" }")
  298. (org-babel-inline-result-wrap "=%s="))
  299. (org-test-with-temp-text
  300. (concat "\n" test-line)
  301. (should-error (org-ctrl-c-ctrl-c))
  302. (goto-char (point-min))
  303. (should-error (org-ctrl-c-ctrl-c))
  304. (forward-line)
  305. (should-error (org-ctrl-c-ctrl-c))
  306. (forward-char) (org-babel-execute-maybe)
  307. (should (string=
  308. (concat test-line " {{{results(=x=)}}}")
  309. (buffer-substring-no-properties
  310. (point-at-bol) (point-at-eol))))))
  311. (let ((test-line "Some text prior to block src_emacs-lisp{ \"y\" }")
  312. (org-babel-inline-result-wrap "=%s="))
  313. (org-test-with-temp-text
  314. test-line
  315. (goto-char (point-max))
  316. (insert (concat "\n" test-line " end"))
  317. (re-search-backward "src") (org-babel-execute-maybe)
  318. (should (string=
  319. (concat test-line " {{{results(=y=)}}} end")
  320. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  321. (re-search-forward "\" ") (org-babel-execute-maybe)
  322. (should (string=
  323. (concat test-line " {{{results(=y=)}}} end")
  324. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  325. (forward-char 3)
  326. (should-error (org-ctrl-c-ctrl-c)))))
  327. (ert-deftest test-ob/inline-src_blk-manual-results-replace ()
  328. (let ((test-line " src_emacs-lisp[:results replace]{ \"x\" }")
  329. (org-babel-inline-result-wrap "=%s="))
  330. (org-test-with-temp-text
  331. (concat "\n" test-line)
  332. (should-error (org-ctrl-c-ctrl-c))
  333. (goto-char (point-max))
  334. (org-babel-execute-maybe)
  335. (beginning-of-line)
  336. (should-error (org-ctrl-c-ctrl-c))
  337. (forward-char) (org-babel-execute-maybe)
  338. (should (string=
  339. (concat test-line " {{{results(=x=)}}}")
  340. (buffer-substring-no-properties
  341. (point-at-bol) (point-at-eol))))))
  342. (let ((test-line (concat " Some text prior to block "
  343. "src_emacs-lisp[:results replace]{ \"y\" }"))
  344. (org-babel-inline-result-wrap "=%s="))
  345. (org-test-with-temp-text test-line
  346. (goto-char (point-max))
  347. (insert (concat "\n" test-line " end"))
  348. (re-search-backward "src") (org-babel-execute-maybe)
  349. (should (string=
  350. (concat test-line " {{{results(=y=)}}} end")
  351. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  352. (re-search-forward "\" ") (org-babel-execute-maybe)
  353. (should (string=
  354. (concat test-line " {{{results(=y=)}}} end")
  355. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  356. (forward-char 3)
  357. (should-error (org-ctrl-c-ctrl-c)))))
  358. (ert-deftest test-ob/inline-src_blk-results-silent ()
  359. (let ((test-line "src_emacs-lisp[ :results silent ]{ \"x\" }"))
  360. (org-test-with-temp-text test-line
  361. (org-babel-execute-maybe)
  362. (should (string= test-line
  363. (buffer-substring-no-properties
  364. (point-at-bol) (point-at-eol))))))
  365. (let ((test-line (concat " Some text prior to block src_emacs-lisp"
  366. "[ :results silent ]{ \"y\" }")))
  367. (org-test-with-temp-text
  368. test-line
  369. (goto-char (point-max))
  370. (insert (concat "\n" test-line " end"))
  371. (re-search-backward "src_") (org-babel-execute-maybe)
  372. (should (string= (concat test-line " end")
  373. (buffer-substring-no-properties
  374. (point-at-bol) (point-at-eol))))
  375. (re-search-forward "\" ") (org-babel-execute-maybe)
  376. (should (string= (concat test-line " end")
  377. (buffer-substring-no-properties
  378. (point-at-bol) (point-at-eol))))
  379. (forward-char 2)
  380. (should-error (org-ctrl-c-ctrl-c)))))
  381. (ert-deftest test-ob/inline-src_blk-results-raw ()
  382. (let ((test-line "src_emacs-lisp[ :results raw ]{ \"x\" }"))
  383. (org-test-with-temp-text test-line
  384. (org-babel-execute-maybe)
  385. (should (string= (concat test-line " x")
  386. (buffer-string)))))
  387. (let ((test-line (concat " Some text prior to block "
  388. "src_emacs-lisp[ :results raw ]{ \"the\" }")))
  389. (org-test-with-temp-text (concat test-line " end")
  390. (re-search-forward "src_") (org-babel-execute-maybe)
  391. (should (string= (concat test-line " the end")
  392. (buffer-substring-no-properties
  393. (point-at-bol) (point-at-eol))))
  394. (re-search-forward "\" ") (org-babel-execute-maybe)
  395. (should (string= (concat test-line " the the end")
  396. (buffer-substring-no-properties
  397. (point-at-bol) (point-at-eol))))
  398. (forward-char 2)
  399. (should-error (org-ctrl-c-ctrl-c)))))
  400. (ert-deftest test-ob/inline-src_blk-results-file ()
  401. (let ((test-line "src_emacs-lisp[ :results file ]{ \"~/test-file\" }"))
  402. (org-test-with-temp-text
  403. test-line
  404. (org-babel-execute-maybe)
  405. (should (string= (concat test-line " {{{results([[file:~/test-file]])}}}")
  406. (buffer-substring-no-properties
  407. (point-min) (point-max)))))))
  408. (ert-deftest test-ob/inline-src_blk-results-scalar ()
  409. (let ((test-line "src_emacs-lisp[ :results scalar ]{ \"x\" }")
  410. (org-babel-inline-result-wrap "=%s="))
  411. (org-test-with-temp-text
  412. test-line
  413. (org-babel-execute-maybe)
  414. (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
  415. (buffer-substring-no-properties
  416. (point-min) (point-max)))))))
  417. (ert-deftest test-ob/inline-src_blk-results-verbatim ()
  418. (let ((test-line "src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  419. (org-babel-inline-result-wrap "=%s="))
  420. (org-test-with-temp-text
  421. test-line
  422. (org-babel-execute-maybe)
  423. (should (string= (concat test-line " {{{results(=\"x\"=)}}}")
  424. (buffer-substring-no-properties
  425. (point-min) (point-max)))))))
  426. (ert-deftest test-ob/combining-scalar-and-raw-result-types ()
  427. (org-test-with-temp-text-in-file "
  428. #+begin_src sh :results scalar
  429. echo \"[[file:./cv.cls]]\"
  430. #+end_src
  431. #+name:
  432. : [[file:./cv.cls]]
  433. #+begin_src sh :results raw scalar
  434. echo \"[[file:./cv.cls]]\"
  435. #+end_src
  436. "
  437. (flet ((next-result ()
  438. (org-babel-next-src-block)
  439. (org-babel-execute-src-block)
  440. (goto-char (org-babel-where-is-src-block-result))
  441. (forward-line 1)))
  442. (goto-char (point-min))
  443. (next-result)
  444. (should (eq (org-element-type (org-element-at-point)) 'fixed-width))
  445. (next-result)
  446. (should-not
  447. (eq (org-element-type (org-element-at-point)) 'fixed-width)))))
  448. (ert-deftest test-ob/no-defaut-value-for-var ()
  449. "Test that the absence of a default value for a variable DOES THROW
  450. a proper error."
  451. (org-test-at-id "f2df5ba6-75fa-4e6b-8441-65ed84963627"
  452. (org-babel-next-src-block)
  453. (let ((err
  454. (should-error (org-babel-execute-src-block) :type 'error)))
  455. (should
  456. (equal
  457. '(error
  458. "Variable \"x\" must be assigned a default value")
  459. err)))))
  460. (ert-deftest test-ob/just-one-results-block ()
  461. "Test that evaluating two times the same code block does not result in a
  462. duplicate results block."
  463. (org-test-with-temp-text "#+begin_src sh\necho Hello\n#+end_src\n"
  464. (org-babel-execute-src-block)
  465. (org-babel-execute-src-block) ; second code block execution
  466. (should (search-forward "Hello")) ; the string inside the source code block
  467. (should (search-forward "Hello")) ; the same string in the results block
  468. (should-error (search-forward "Hello"))))
  469. (ert-deftest test-ob/nested-code-block ()
  470. "Test nested code blocks inside code blocks don't cause problems."
  471. (should
  472. (string= "#+begin_src emacs-lisp\n 'foo\n#+end_src"
  473. (org-test-with-temp-text "#+begin_src org :results silent
  474. ,#+begin_src emacs-lisp
  475. 'foo
  476. ,#+end_src
  477. #+end_src"
  478. (let ((org-edit-src-content-indentation 2)
  479. (org-src-preserve-indentation nil))
  480. (org-babel-execute-src-block))))))
  481. (ert-deftest test-ob/partial-nested-code-block ()
  482. "Test nested code blocks inside code blocks don't cause problems."
  483. (org-test-with-temp-text "#+begin_src org :results silent
  484. ,#+begin_src emacs-lisp
  485. #+end_src"
  486. (should (string= "#+begin_src emacs-lisp" (org-babel-execute-src-block)))))
  487. (ert-deftest test-ob/does-not-replace-a-block-with-the-results ()
  488. (org-test-with-temp-text "#+NAME: foo
  489. #+BEGIN_SRC emacs-lisp
  490. 'foo
  491. #+END_SRC\n"
  492. (org-babel-next-src-block 1)
  493. (should (eq 'foo (org-babel-execute-src-block)))
  494. (goto-char (point-min))
  495. (org-babel-next-src-block 1)
  496. (should (looking-at org-babel-src-block-regexp))))
  497. (ert-deftest test-ob/catches-all-references ()
  498. (org-test-with-temp-text "
  499. #+NAME: literal-example
  500. #+BEGIN_EXAMPLE
  501. A literal example
  502. on two lines
  503. #+END_EXAMPLE
  504. #+NAME: read-literal-example
  505. #+BEGIN_SRC emacs-lisp :var x=literal-example
  506. (concatenate 'string x \" for me.\")
  507. #+END_SRC"
  508. (org-babel-next-src-block 1)
  509. (should (string= (org-babel-execute-src-block)
  510. "A literal example\non two lines\n for me."))))
  511. (ert-deftest test-ob/ignore-reference-in-commented-headings ()
  512. (should
  513. (= 2
  514. (org-test-with-temp-text
  515. "
  516. * COMMENT H1
  517. #+NAME: n
  518. : 1
  519. * H2
  520. #+NAME: n
  521. : 2
  522. * Code
  523. <point>#+BEGIN_SRC emacs-lisp :var x=n
  524. x
  525. #+END_SRC"
  526. (org-babel-execute-src-block)))))
  527. (ert-deftest test-ob/do-not-resolve-to-partial-names-data ()
  528. (org-test-with-temp-text "
  529. #+name: base_plus
  530. | 1 |
  531. | 2 |
  532. #+name: base
  533. | 3 |
  534. | 4 |
  535. #+begin_src emacs-lisp :var x=base
  536. x
  537. #+end_src"
  538. (org-babel-next-src-block 1)
  539. (should (equal (org-babel-execute-src-block) '((3) (4))))))
  540. (ert-deftest test-ob/do-not-resolve-to-partial-names-code ()
  541. (org-test-with-temp-text "
  542. #+name: base_plus
  543. #+begin_src emacs-lisp
  544. 'bar
  545. #+end_src
  546. #+name: base
  547. #+begin_src emacs-lisp
  548. 'foo
  549. #+end_src
  550. #+begin_src emacs-lisp :var x=base
  551. x
  552. #+end_src"
  553. (org-babel-next-src-block 3)
  554. (should (equal (org-babel-execute-src-block) "foo"))))
  555. (ert-deftest test-ob/allow-spaces-around-=-in-var-specs ()
  556. (org-test-with-temp-text "#+begin_src emacs-lisp :var a = 1 b = 2 c= 3 d =4
  557. (+ a b c d)
  558. #+end_src
  559. "
  560. (should (= 10 (org-babel-execute-src-block)))))
  561. (ert-deftest test-ob/org-babel-update-intermediate ()
  562. (org-test-with-temp-text "#+name: foo
  563. #+begin_src emacs-lisp
  564. 2
  565. #+end_src
  566. #+results: foo
  567. : 4
  568. #+begin_src emacs-lisp :var it=foo
  569. (+ it 1)
  570. #+end_src"
  571. (let ((org-babel-update-intermediate nil))
  572. (goto-char (point-min))
  573. (org-babel-next-src-block 2)
  574. (should (= 3 (org-babel-execute-src-block)))
  575. (goto-char (point-min))
  576. (forward-line 6)
  577. (should (looking-at ": 4")))
  578. (let ((org-babel-update-intermediate t))
  579. (goto-char (point-min))
  580. (org-babel-next-src-block 2)
  581. (should (= 3 (org-babel-execute-src-block)))
  582. (goto-char (point-min))
  583. (forward-line 6)
  584. (should (looking-at ": 2")))))
  585. (ert-deftest test-ob/eval-header-argument ()
  586. (flet ((check-eval (eval runp)
  587. (org-test-with-temp-text (format "#+begin_src emacs-lisp :eval %s
  588. (setq foo :evald)
  589. #+end_src" eval)
  590. (let ((foo :not-run))
  591. (if runp
  592. (progn (should (org-babel-execute-src-block))
  593. (should (eq foo :evald)))
  594. (progn (should-not (org-babel-execute-src-block))
  595. (should-not (eq foo :evald))))))))
  596. (check-eval "never" nil)
  597. (check-eval "no" nil)
  598. (check-eval "never-export" t)
  599. (check-eval "no-export" t)
  600. (let ((org-babel-exp-reference-buffer (current-buffer)))
  601. (check-eval "never" nil)
  602. (check-eval "no" nil)
  603. (check-eval "never-export" nil)
  604. (check-eval "no-export" nil))))
  605. (ert-deftest test-ob/noweb-expansion-1 ()
  606. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  607. <<foo>>
  608. #+end_src
  609. #+name: foo
  610. #+begin_src sh
  611. bar
  612. #+end_src"
  613. (should (string= (org-babel-expand-noweb-references) "bar"))))
  614. (ert-deftest test-ob/noweb-expansion-2 ()
  615. (org-test-with-temp-text "#+begin_src sh :results output :tangle yes
  616. <<foo>>
  617. #+end_src
  618. #+name: foo
  619. #+begin_src sh :noweb-sep \"\"
  620. bar
  621. #+end_src
  622. #+begin_src sh :noweb-ref foo :noweb-sep \"\"
  623. baz
  624. #+end_src"
  625. (should (string= (org-babel-expand-noweb-references) "barbaz"))))
  626. (ert-deftest test-ob/splitting-variable-lists-in-references ()
  627. (org-test-with-temp-text ""
  628. (should (= 1 (length (org-babel-ref-split-args
  629. "a=\"this, no work\""))))
  630. (should (= 2 (length (org-babel-ref-split-args
  631. "a=\"this, no work\", b=1"))))))
  632. (ert-deftest test-ob/org-babel-balanced-split ()
  633. (should (equal
  634. '(":a 1" "b [2 3]" "c (4 :d (5 6))")
  635. (org-babel-balanced-split ":a 1 :b [2 3] :c (4 :d (5 6))"
  636. '((32 9) . 58)))))
  637. (ert-deftest test-ob/commented-last-block-line-no-var ()
  638. (org-test-with-temp-text-in-file "
  639. #+begin_src emacs-lisp
  640. ;;
  641. #+end_src"
  642. (org-babel-next-src-block)
  643. (org-babel-execute-maybe)
  644. (should (re-search-forward "\\#\\+results:" nil t))
  645. (forward-line)
  646. (should
  647. (string=
  648. ""
  649. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  650. (org-test-with-temp-text-in-file "
  651. #+begin_src emacs-lisp
  652. \"some text\";;
  653. #+end_src"
  654. (org-babel-next-src-block)
  655. (org-babel-execute-maybe)
  656. (should (re-search-forward "\\#\\+results:" nil t))
  657. (forward-line)
  658. (should
  659. (string=
  660. ": some text"
  661. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  662. (ert-deftest test-ob/commented-last-block-line-with-var ()
  663. (org-test-with-temp-text-in-file "
  664. #+begin_src emacs-lisp :var a=1
  665. ;;
  666. #+end_src"
  667. (org-babel-next-src-block)
  668. (org-babel-execute-maybe)
  669. (re-search-forward "\\#\\+results:" nil t)
  670. (forward-line)
  671. (should (string=
  672. ""
  673. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  674. (org-test-with-temp-text-in-file "
  675. #+begin_src emacs-lisp :var a=2
  676. 2;;
  677. #+end_src"
  678. (org-babel-next-src-block)
  679. (org-babel-execute-maybe)
  680. (re-search-forward "\\#\\+results:" nil t)
  681. (forward-line)
  682. (should (string=
  683. ": 2"
  684. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  685. (ert-deftest test-ob/org-babel-insert-result--improper-lists ()
  686. "Test `org-babel-insert-result' with improper lists."
  687. ;; Do not error when output is an improper list.
  688. (should
  689. (org-test-with-temp-text
  690. "
  691. <point>#+BEGIN_SRC emacs-lisp
  692. '((1 . nil) (2 . 3))
  693. #+END_SRC
  694. "
  695. (org-babel-execute-maybe) t)))
  696. (ert-deftest test-ob/remove-inline-result ()
  697. "Test `org-babel-remove-inline-result' honors whitespace."
  698. (let*
  699. ((inline-sb "src_emacs-lisp{(+ 1 2)}")
  700. (inline-res " {{{results(=3=)}}}")
  701. (inline-sb-dot (concat inline-sb "."))
  702. (inline-sb-res-dot (concat inline-sb inline-res ".")))
  703. (org-test-with-temp-text
  704. ;; Insert inline_src_block followed by dot.
  705. inline-sb-dot
  706. ;; Insert result before dot.
  707. (org-babel-execute-maybe)
  708. (should (string= inline-sb-res-dot
  709. (buffer-substring-no-properties
  710. (point-at-bol) (point-at-eol))))
  711. ;; Delete whitespace and result.
  712. (org-babel-remove-inline-result)
  713. (should (string= inline-sb-dot
  714. (buffer-substring-no-properties
  715. (point-at-bol) (point-at-eol))))
  716. ;; Add whitespace and result before dot.
  717. (search-forward inline-sb)
  718. (insert " " inline-res)
  719. (goto-char (point-at-bol))
  720. ;; Remove whitespace and result.
  721. (org-babel-remove-inline-result)
  722. (should (string= inline-sb-dot
  723. (buffer-substring-no-properties
  724. (point-at-bol) (point-at-eol))))
  725. ;; Add whitespace before dot.
  726. (search-forward inline-sb)
  727. (insert " ")
  728. (goto-char (point-at-bol))
  729. ;; Add result before whitespace.
  730. (org-babel-execute-maybe)
  731. ;; Remove result - leave trailing whitespace and dot.
  732. (org-babel-remove-inline-result)
  733. (should (string= (concat inline-sb " .")
  734. (buffer-substring-no-properties
  735. (point-at-bol) (point-at-eol)))))))
  736. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  737. "Test helper function to test `org-babel-remove-result'.
  738. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  739. and the result of execution is verified against RESULT.
  740. The block is actually executed /twice/ to ensure result
  741. replacement happens correctly."
  742. (org-test-with-temp-text
  743. buffer-text
  744. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  745. (should (re-search-forward "\\#\\+results:" nil t))
  746. (forward-line)
  747. (should (string= result
  748. (buffer-substring-no-properties
  749. (point-at-bol)
  750. (- (point-max) 16))))
  751. (org-babel-previous-src-block) (org-babel-remove-result)
  752. (should (string= buffer-text
  753. (buffer-substring-no-properties
  754. (point-min) (point-max))))))
  755. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  756. "Test `org-babel-remove-result' with default :results."
  757. (mapcar (lambda (language)
  758. (test-ob-verify-result-and-removed-result
  759. "\n"
  760. (concat
  761. "* org-babel-remove-result
  762. #+begin_src " language "
  763. #+end_src
  764. * next heading")))
  765. '("sh" "emacs-lisp")))
  766. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  767. "Test `org-babel-remove-result' with :results list."
  768. (test-ob-verify-result-and-removed-result
  769. "- 1
  770. - 2
  771. - 3
  772. - (quote (4 5))"
  773. "* org-babel-remove-result
  774. #+begin_src emacs-lisp :results list
  775. '(1 2 3 '(4 5))
  776. #+end_src
  777. * next heading"))
  778. (ert-deftest test-ob/org-babel-results-indented-wrap ()
  779. "Ensure that wrapped results are inserted correction when indented.
  780. If not inserted correctly then the second evaluation will fail
  781. trying to find the :END: marker."
  782. (org-test-with-temp-text
  783. "- indented
  784. #+begin_src sh :results file wrap
  785. echo test.txt
  786. #+end_src"
  787. (org-babel-next-src-block 1)
  788. (org-babel-execute-src-block)
  789. (org-babel-execute-src-block)))
  790. (ert-deftest test-ob/file-desc-header-argument ()
  791. "Test that the :file-desc header argument is used."
  792. (org-test-with-temp-text "#+begin_src emacs-lisp :results file :file-desc bar
  793. \"foo\"
  794. #+end_src
  795. #+begin_src emacs-lisp :results file :file-desc
  796. \"foo\"
  797. #+end_src"
  798. (org-babel-execute-src-block)
  799. (org-babel-next-src-block 1)
  800. (org-babel-execute-src-block)
  801. (goto-char (point-min))
  802. (should (search-forward "[[file:foo][bar]]" nil t))
  803. (should (search-forward "[[file:foo][foo]]" nil t))))
  804. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  805. "Test `org-babel-remove-result' with :results pp."
  806. (test-ob-verify-result-and-removed-result
  807. ": \"I /am/ working!\""
  808. "* org-babel-remove-result
  809. #+begin_src emacs-lisp :results pp
  810. \"I /am/ working!\")
  811. #+end_src
  812. * next heading"))
  813. (ert-deftest test-ob/inline-src_blk-preceded-punct-preceded-by-point ()
  814. (let ((test-line ".src_emacs-lisp[ :results verbatim ]{ \"x\" }")
  815. (org-babel-inline-result-wrap "=%s="))
  816. (org-test-with-temp-text
  817. test-line
  818. (forward-char 1)
  819. (org-babel-execute-maybe)
  820. (should (re-search-forward "=\"x\"=" nil t))
  821. (forward-line))))
  822. (ert-deftest test-ob/commented-last-block-line-with-var ()
  823. (org-test-with-temp-text-in-file "
  824. #+begin_src emacs-lisp :var a=1
  825. ;;
  826. #+end_src"
  827. (org-babel-next-src-block)
  828. (org-babel-execute-maybe)
  829. (re-search-forward "\\#\\+results:" nil t)
  830. (forward-line)
  831. (should (string=
  832. ""
  833. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  834. (org-test-with-temp-text-in-file "
  835. #+begin_src emacs-lisp :var a=2
  836. 2;;
  837. #+end_src"
  838. (org-babel-next-src-block)
  839. (org-babel-execute-maybe)
  840. (re-search-forward "\\#\\+results:" nil t)
  841. (forward-line)
  842. (should (string=
  843. ": 2"
  844. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  845. (defun test-ob-verify-result-and-removed-result (result buffer-text)
  846. "Test helper function to test `org-babel-remove-result'.
  847. A temp buffer is populated with BUFFER-TEXT, the first block is executed,
  848. and the result of execution is verified against RESULT.
  849. The block is actually executed /twice/ to ensure result
  850. replacement happens correctly."
  851. (org-test-with-temp-text
  852. buffer-text
  853. (org-babel-next-src-block) (org-babel-execute-maybe) (org-babel-execute-maybe)
  854. (should (re-search-forward "\\#\\+results:" nil t))
  855. (forward-line)
  856. (should (string= result
  857. (buffer-substring-no-properties
  858. (point-at-bol)
  859. (- (point-max) 16))))
  860. (org-babel-previous-src-block) (org-babel-remove-result)
  861. (should (string= buffer-text
  862. (buffer-substring-no-properties
  863. (point-min) (point-max))))))
  864. (ert-deftest test-ob/org-babel-remove-result--results-default ()
  865. "Test `org-babel-remove-result' with default :results."
  866. (mapcar (lambda (language)
  867. (test-ob-verify-result-and-removed-result
  868. "\n"
  869. (concat
  870. "* org-babel-remove-result
  871. #+begin_src " language "
  872. #+end_src
  873. * next heading")))
  874. '("sh" "emacs-lisp")))
  875. (ert-deftest test-ob/org-babel-remove-result--results-list ()
  876. "Test `org-babel-remove-result' with :results list."
  877. (test-ob-verify-result-and-removed-result
  878. "- 1
  879. - 2
  880. - 3
  881. - (quote (4 5))"
  882. "* org-babel-remove-result
  883. #+begin_src emacs-lisp :results list
  884. '(1 2 3 '(4 5))
  885. #+end_src
  886. * next heading"))
  887. (ert-deftest test-ob/org-babel-remove-result--results-wrap ()
  888. "Test `org-babel-remove-result' with :results wrap."
  889. (test-ob-verify-result-and-removed-result
  890. ":RESULTS:
  891. hello there
  892. :END:"
  893. "* org-babel-remove-result
  894. #+begin_src emacs-lisp :results wrap
  895. \"hello there\"
  896. #+end_src
  897. * next heading"))
  898. (ert-deftest test-ob/org-babel-remove-result--results-org ()
  899. "Test `org-babel-remove-result' with :results org."
  900. (test-ob-verify-result-and-removed-result
  901. "#+BEGIN_SRC org
  902. ,* heading
  903. ,** subheading
  904. content
  905. #+END_SRC"
  906. "* org-babel-remove-result
  907. #+begin_src emacs-lisp :results org
  908. \"* heading
  909. ,** subheading
  910. content\"
  911. #+end_src
  912. * next heading"))
  913. (ert-deftest test-ob/org-babel-remove-result--results-html ()
  914. "Test `org-babel-remove-result' with :results html."
  915. (test-ob-verify-result-and-removed-result
  916. "#+BEGIN_EXPORT html
  917. <head><body></body></head>
  918. #+END_EXPORT"
  919. "* org-babel-remove-result
  920. #+begin_src emacs-lisp :results html
  921. \"<head><body></body></head>\"
  922. #+end_src
  923. * next heading"))
  924. (ert-deftest test-ob/org-babel-remove-result--results-latex ()
  925. "Test `org-babel-remove-result' with :results latex."
  926. (test-ob-verify-result-and-removed-result
  927. "#+BEGIN_EXPORT latex
  928. Line 1
  929. Line 2
  930. Line 3
  931. #+END_EXPORT"
  932. "* org-babel-remove-result
  933. #+begin_src emacs-lisp :results latex
  934. \"Line 1
  935. Line 2
  936. Line 3\"
  937. #+end_src
  938. * next heading"))
  939. (ert-deftest test-ob/org-babel-remove-result--results-code ()
  940. "Test `org-babel-remove-result' with :results code."
  941. (test-ob-verify-result-and-removed-result
  942. "#+BEGIN_SRC emacs-lisp
  943. \"I am working!\"
  944. #+END_SRC"
  945. "* org-babel-remove-result
  946. #+begin_src emacs-lisp :results code
  947. (message \"I am working!\")
  948. #+end_src
  949. * next heading"))
  950. (ert-deftest test-ob/org-babel-remove-result--results-pp ()
  951. "Test `org-babel-remove-result' with :results pp."
  952. (test-ob-verify-result-and-removed-result
  953. ": \"I /am/ working!\""
  954. "* org-babel-remove-result
  955. #+begin_src emacs-lisp :results pp
  956. \"I /am/ working!\")
  957. #+end_src
  958. * next heading"))
  959. (ert-deftest test-ob/results-do-not-replace-code-blocks ()
  960. (org-test-with-temp-text "Block two has a space after the name.
  961. #+name: foo
  962. #+begin_src emacs-lisp
  963. 1
  964. #+end_src
  965. #+name: foo
  966. #+begin_src emacs-lisp
  967. 2
  968. #+end_src
  969. #+name: foo
  970. #+begin_src emacs-lisp
  971. 3
  972. #+end_src
  973. #+RESULTS: foo
  974. : foo
  975. "
  976. (dolist (num '(1 2 3))
  977. ;; execute the block
  978. (goto-char (point-min))
  979. (org-babel-next-src-block num) (org-babel-execute-src-block)
  980. ;; check the results
  981. (goto-char (point-max))
  982. (move-beginning-of-line 0)
  983. (should (looking-at (format ": %d" num))))))
  984. (ert-deftest test-ob/blocks-with-spaces ()
  985. "Test expansion of blocks followed by blank lines."
  986. (should
  987. (equal "#+BEGIN_SRC emacs-lisp
  988. \(+ 1 2)
  989. #+END_SRC
  990. #+RESULTS:
  991. : 3\n\n\n"
  992. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp
  993. \(+ 1 2)
  994. #+END_SRC\n\n\n"
  995. (let ((org-babel-next-src-block "RESULTS"))
  996. (org-babel-execute-src-block))
  997. (buffer-string)))))
  998. (ert-deftest test-ob/results-in-narrowed-buffer ()
  999. "Test block execution in a narrowed buffer."
  1000. ;; If results don't exist, they should be inserted in visible part
  1001. ;; of the buffer.
  1002. (should
  1003. (equal
  1004. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS:\n: 3"
  1005. (org-test-with-temp-text
  1006. "#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1007. (narrow-to-region (point) (save-excursion (forward-line 3) (point)))
  1008. (let ((org-babel-results-keyword "RESULTS"))
  1009. (org-babel-execute-src-block))
  1010. (org-trim (buffer-string)))))
  1011. (should
  1012. (equal
  1013. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\n#+RESULTS: test\n: 3"
  1014. (org-test-with-temp-text
  1015. "#+NAME: test\n#+BEGIN_SRC emacs-lisp\n(+ 1 2)\n#+END_SRC\n\nParagraph"
  1016. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1017. (let ((org-babel-results-keyword "RESULTS"))
  1018. (org-babel-execute-src-block))
  1019. (org-trim (buffer-string)))))
  1020. ;; Results in visible part of buffer, should be updated here.
  1021. (should
  1022. (equal
  1023. "#+NAME: test
  1024. #+BEGIN_SRC emacs-lisp
  1025. \(+ 1 2)
  1026. #+END_SRC
  1027. #+RESULTS: test
  1028. : 3"
  1029. (org-test-with-temp-text
  1030. "#+NAME: test
  1031. #+BEGIN_SRC emacs-lisp
  1032. \(+ 1 2)
  1033. #+END_SRC
  1034. #+RESULTS: test
  1035. : 4
  1036. Paragraph"
  1037. (narrow-to-region (point) (save-excursion (forward-line 7) (point)))
  1038. (let ((org-babel-results-keyword "RESULTS"))
  1039. (org-babel-execute-src-block))
  1040. (org-trim (buffer-string)))))
  1041. ;; Results in invisible part of buffer, should be updated there.
  1042. (org-test-with-temp-text
  1043. "#+NAME: test
  1044. #+BEGIN_SRC emacs-lisp
  1045. \(+ 1 2)
  1046. #+END_SRC
  1047. #+RESULTS: test
  1048. : 4
  1049. Paragraph"
  1050. (narrow-to-region (point) (save-excursion (forward-line 4) (point)))
  1051. (let ((org-babel-results-keyword "RESULTS"))
  1052. (org-babel-execute-src-block))
  1053. (should-not (re-search-forward "^#\\+RESULTS:" nil t))
  1054. (widen)
  1055. (should (re-search-forward "^: 3" nil t))))
  1056. (ert-deftest test-ob/specific-colnames ()
  1057. "Test passing specific column names."
  1058. (should
  1059. (equal "#+name: input-table
  1060. | id | var1 |
  1061. |----+------|
  1062. | 1 | bar |
  1063. | 2 | baz |
  1064. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1065. echo \"$data\"
  1066. #+end_src
  1067. #+RESULTS:
  1068. | Rev | Author |
  1069. |-----+--------|
  1070. | 1 | bar |
  1071. | 2 | baz |
  1072. "
  1073. (org-test-with-temp-text
  1074. "#+name: input-table
  1075. | id | var1 |
  1076. |----+------|
  1077. | 1 | bar |
  1078. | 2 | baz |
  1079. #+begin_src sh :var data=input-table :exports results :colnames '(Rev Author)
  1080. echo \"$data\"
  1081. #+end_src
  1082. "
  1083. ;; we should find a code block
  1084. (should (re-search-forward org-babel-src-block-regexp nil t))
  1085. (goto-char (match-beginning 0))
  1086. ;; now that we've located the code block, it may be evaluated
  1087. (let ((org-babel-execute-src-block "RESULTS"))
  1088. (org-babel-execute-src-block))
  1089. (buffer-string)))))
  1090. (ert-deftest test-ob/location-of-header-arg-eval ()
  1091. "Test location of header argument evaluation."
  1092. (org-test-with-temp-text "
  1093. #+name: top-block
  1094. #+begin_src emacs-lisp :var pt=(point)
  1095. pt
  1096. #+end_src
  1097. #+name: bottom-block
  1098. #+begin_src emacs-lisp :var pt=top-block()
  1099. pt
  1100. #+end_src
  1101. "
  1102. ;; the value of the second block should be greater than the first
  1103. (should
  1104. (< (progn (re-search-forward org-babel-src-block-regexp nil t)
  1105. (goto-char (match-beginning 0))
  1106. (prog1 (save-match-data (org-babel-execute-src-block))
  1107. (goto-char (match-end 0))))
  1108. (progn (re-search-forward org-babel-src-block-regexp nil t)
  1109. (goto-char (match-beginning 0))
  1110. (org-babel-execute-src-block))))))
  1111. (ert-deftest test-ob/preserve-results-indentation ()
  1112. "Preserve indentation when executing a src block."
  1113. (should
  1114. (equal
  1115. '(2 2)
  1116. (org-test-with-temp-text " #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1117. (org-babel-execute-src-block)
  1118. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1119. (list (org-get-indentation)
  1120. (progn (forward-line) (org-get-indentation))))))
  1121. (should
  1122. (equal
  1123. '(2 2)
  1124. (org-test-with-temp-text
  1125. " #+name: block\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1126. (org-babel-execute-src-block)
  1127. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1128. (list (org-get-indentation)
  1129. (progn (forward-line) (org-get-indentation))))))
  1130. ;; Don't get fooled by TAB-based indentation.
  1131. (should
  1132. (equal
  1133. '(6 6)
  1134. (org-test-with-temp-text
  1135. "\t #+begin_src emacs-lisp\n\t (+ 1 1)\n\t #+end_src"
  1136. (setq tab-width 4)
  1137. (org-babel-execute-src-block)
  1138. (let ((case-fold-search t)) (search-forward "RESULTS"))
  1139. (list (org-get-indentation)
  1140. (progn (forward-line) (org-get-indentation)))))))
  1141. (ert-deftest test-ob/safe-header-args ()
  1142. "Detect safe and unsafe header args."
  1143. (let ((safe-args '((:cache . "foo")
  1144. (:results . "output")
  1145. (:eval . "never")
  1146. (:eval . "query")))
  1147. (unsafe-args '((:eval . "yes")
  1148. (:results . "output file")
  1149. (:foo . "bar")))
  1150. (malformed-args '((bar . "foo")
  1151. ("foo" . "bar")
  1152. :foo))
  1153. (safe-p (org-babel-header-args-safe-fn org-babel-safe-header-args)))
  1154. (dolist (arg safe-args)
  1155. (should (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args)))
  1156. (dolist (arg unsafe-args)
  1157. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1158. (dolist (arg malformed-args)
  1159. (should (not (org-babel-one-header-arg-safe-p arg org-babel-safe-header-args))))
  1160. (should (not (funcall safe-p (append safe-args unsafe-args))))))
  1161. (ert-deftest test-ob/noweb-expansions-in-cache ()
  1162. "Ensure that noweb expansions are expanded before caching."
  1163. (let ((noweb-expansions-in-cache-var 0))
  1164. (org-test-with-temp-text "
  1165. #+name: foo
  1166. #+begin_src emacs-lisp
  1167. \"I said\"
  1168. #+end_src
  1169. #+name: bar
  1170. #+begin_src emacs-lisp :noweb yes :cache yes
  1171. (setq noweb-expansions-in-cache-var
  1172. (+ 1 noweb-expansions-in-cache-var))
  1173. (concat <<foo>> \" check noweb expansions\")
  1174. #+end_src
  1175. "
  1176. ;; run the second block to create the cache
  1177. (goto-char (point-min))
  1178. (re-search-forward (regexp-quote "#+name: bar"))
  1179. (should (string= "I said check noweb expansions"
  1180. (org-babel-execute-src-block)))
  1181. (should (= noweb-expansions-in-cache-var 1))
  1182. ;; change the value of the first block
  1183. (goto-char (point-min))
  1184. (re-search-forward (regexp-quote "said"))
  1185. (goto-char (match-beginning 0))
  1186. (insert "haven't ")
  1187. (re-search-forward (regexp-quote "#+name: bar"))
  1188. (should (string= "I haven't said check noweb expansions"
  1189. (org-babel-execute-src-block)))
  1190. (should (= noweb-expansions-in-cache-var 2)))))
  1191. (ert-deftest test-ob/file-ext-and-output-dir ()
  1192. (org-test-at-id "93573e1d-6486-442e-b6d0-3fedbdc37c9b"
  1193. (org-babel-next-src-block)
  1194. (should (equal "file-ext-basic.txt"
  1195. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1196. (org-babel-next-src-block)
  1197. (should (equal "foo/file-ext-dir-relative.txt"
  1198. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1199. (org-babel-next-src-block)
  1200. (should (equal "foo/file-ext-dir-relative-slash.txt"
  1201. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1202. (org-babel-next-src-block)
  1203. (should (equal "/tmp/file-ext-dir-absolute.txt"
  1204. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1205. (org-babel-next-src-block)
  1206. (should (equal "foo.bar"
  1207. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1208. (org-babel-next-src-block)
  1209. (should (equal "xxx/foo.bar"
  1210. (cdr (assq :file (nth 2 (org-babel-get-src-block-info t))))))
  1211. ))
  1212. (ert-deftest test-ob/script-escape ()
  1213. ;; Delimited lists of numbers
  1214. (should (equal '(1 2 3)
  1215. (org-babel-script-escape "[1 2 3]")))
  1216. (should (equal '(1 2 3)
  1217. (org-babel-script-escape "{1 2 3}")))
  1218. (should (equal '(1 2 3)
  1219. (org-babel-script-escape "(1 2 3)")))
  1220. ;; Delimited lists of double-quoted strings
  1221. (should (equal '("foo" "bar")
  1222. (org-babel-script-escape "(\"foo\" \"bar\")")))
  1223. (should (equal '("foo" "bar")
  1224. (org-babel-script-escape "[\"foo\" \"bar\"]")))
  1225. (should (equal '("foo" "bar")
  1226. (org-babel-script-escape "{\"foo\" \"bar\"}")))
  1227. ;; ... with commas
  1228. (should (equal '("foo" "bar")
  1229. (org-babel-script-escape "(\"foo\", \"bar\")")))
  1230. (should (equal '("foo" "bar")
  1231. (org-babel-script-escape "[\"foo\", \"bar\"]")))
  1232. (should (equal '("foo" "bar")
  1233. (org-babel-script-escape "{\"foo\", \"bar\"}")))
  1234. ;; Delimited lists of single-quoted strings
  1235. (should (equal '("foo" "bar")
  1236. (org-babel-script-escape "('foo' 'bar')")))
  1237. (should (equal '("foo" "bar")
  1238. (org-babel-script-escape "['foo' 'bar']")))
  1239. (should (equal '("foo" "bar")
  1240. (org-babel-script-escape "{'foo' 'bar'}")))
  1241. ;; ... with commas
  1242. (should (equal '("foo" "bar")
  1243. (org-babel-script-escape "('foo', 'bar')")))
  1244. (should (equal '("foo" "bar")
  1245. (org-babel-script-escape "['foo', 'bar']")))
  1246. (should (equal '("foo" "bar")
  1247. (org-babel-script-escape "{'foo', 'bar'}")))
  1248. ;; Single quoted strings
  1249. (should (equal "foo"
  1250. (org-babel-script-escape "'foo'")))
  1251. ;; ... with internal double quote
  1252. (should (equal "foo\"bar"
  1253. (org-babel-script-escape "'foo\"bar'")))
  1254. ;; ... with internal backslash
  1255. (should (equal "foo\\bar"
  1256. (org-babel-script-escape "'foo\\bar'")))
  1257. ;; ... with internal escaped backslash
  1258. (should (equal "foo\\bar"
  1259. (org-babel-script-escape "'foo\\\\bar'")))
  1260. ;; ... with internal backslash-double quote
  1261. (should (equal "foo\\\"bar"
  1262. (org-babel-script-escape "'foo\\\"bar'")))
  1263. ;; ... with internal escaped backslash-double quote
  1264. (should (equal "foo\\\"bar"
  1265. (org-babel-script-escape "'foo\\\\\"bar'")))
  1266. ;; ... with internal escaped single quote
  1267. (should (equal "foo'bar"
  1268. (org-babel-script-escape "'foo\\'bar'")))
  1269. ;; ... with internal escaped backslash-escaped single quote
  1270. (should (equal "foo\\'bar"
  1271. (org-babel-script-escape "'foo\\\\\\'bar'")))
  1272. ;; Double quoted strings
  1273. (should (equal "foo"
  1274. (org-babel-script-escape "\"foo\"")))
  1275. ;; ... with internal single quote
  1276. (should (equal "foo'bar"
  1277. (org-babel-script-escape "\"foo'bar\"")))
  1278. ;; ... with internal backslash
  1279. (should (equal "foo\\bar"
  1280. (org-babel-script-escape "\"foo\\bar\"")))
  1281. ;; ... with internal escaped backslash
  1282. (should (equal "foo\\bar"
  1283. (org-babel-script-escape "\"foo\\\\bar\"")))
  1284. ;; ... with internal backslash-single quote
  1285. (should (equal "foo\\'bar"
  1286. (org-babel-script-escape "\"foo\\'bar\"")))
  1287. ;; ... with internal escaped backslash-single quote
  1288. (should (equal "foo\\'bar"
  1289. (org-babel-script-escape "\"foo\\\\'bar\"")))
  1290. ;; ... with internal escaped double quote
  1291. (should (equal "foo\"bar"
  1292. (org-babel-script-escape "\"foo\\\"bar\"")))
  1293. ;; ... with internal escaped backslash-escaped double quote
  1294. (should (equal "foo\\\"bar"
  1295. (org-babel-script-escape "\"foo\\\\\\\"bar\""))))
  1296. (ert-deftest test-ob/process-params-no-duplicates ()
  1297. (should (equal (org-babel-process-params '((:colname-names . 1)
  1298. (:rowname-names . 1)
  1299. (:result-params . 1)
  1300. (:result-type . 1)
  1301. (:var . "\"foo\"")))
  1302. '((:var)
  1303. (:colname-names . 1)
  1304. (:rowname-names . 1)
  1305. (:result-params . 1)
  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. (provide 'test-ob)
  1540. ;;; test-ob ends here