test-ob-lob.el 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. ;;; test-ob-lob.el --- test for ob-lob.el
  2. ;; Copyright (c) 2010-2015, 2019 Eric Schulte
  3. ;; Authors: Eric Schulte
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. (eval-and-compile (require 'cl-lib))
  16. ;;; Tests
  17. (org-babel-lob-ingest
  18. (expand-file-name
  19. "library-of-babel.org"
  20. (expand-file-name
  21. "doc"
  22. (expand-file-name
  23. ".."
  24. (expand-file-name
  25. ".."
  26. (file-name-directory
  27. (or load-file-name buffer-file-name)))))))
  28. (ert-deftest test-ob-lob/ingest ()
  29. "Test the ingestion of an Org file."
  30. (should (< 0 (org-babel-lob-ingest
  31. (expand-file-name "babel.org" org-test-example-dir)))))
  32. (ert-deftest test-ob-lob/call-with-header-arguments ()
  33. "Test the evaluation of a library of babel #+call: line."
  34. (cl-letf (((symbol-function 'org-babel-insert-result)
  35. (symbol-function 'ignore)))
  36. (let ((org-babel-library-of-babel
  37. (org-test-with-temp-text-in-file
  38. "
  39. #+name: echo
  40. #+begin_src emacs-lisp :var input=\"echo'd\"
  41. input
  42. #+end_src
  43. #+name: lob-minus
  44. #+begin_src emacs-lisp :var a=0 :var b=0
  45. (- a b)
  46. #+end_src"
  47. (org-babel-lob-ingest)
  48. org-babel-library-of-babel)))
  49. (org-test-at-id "fab7e291-fde6-45fc-bf6e-a485b8bca2f0"
  50. (move-beginning-of-line 1)
  51. (forward-line 6)
  52. (message (buffer-substring (point-at-bol) (point-at-eol)))
  53. (should
  54. (string= "testing" (org-babel-execute-src-block
  55. nil (org-babel-lob-get-info))))
  56. (forward-line 1)
  57. (should
  58. (string= "testing" (caar (org-babel-execute-src-block
  59. nil (org-babel-lob-get-info)))))
  60. (forward-line 1)
  61. (should
  62. (string= "testing" (org-babel-execute-src-block
  63. nil (org-babel-lob-get-info))))
  64. (forward-line 1)
  65. (should
  66. (string= "testing" (caar (org-babel-execute-src-block
  67. nil (org-babel-lob-get-info)))))
  68. (forward-line 1)
  69. (should
  70. (string= "testing" (org-babel-execute-src-block
  71. nil (org-babel-lob-get-info))))
  72. (forward-line 1)
  73. (should
  74. (string= "testing" (caar (org-babel-execute-src-block
  75. nil (org-babel-lob-get-info)))))
  76. (forward-line 1) (beginning-of-line) (forward-char 27)
  77. (should
  78. (string= "testing" (org-babel-execute-src-block
  79. nil (org-babel-lob-get-info))))
  80. (forward-line 1) (beginning-of-line) (forward-char 27)
  81. (should
  82. (string= "testing" (caar (org-babel-execute-src-block
  83. nil (org-babel-lob-get-info)))))
  84. (forward-line 1) (beginning-of-line)
  85. (should
  86. (= 4 (org-babel-execute-src-block nil (org-babel-lob-get-info))))
  87. (forward-line 1)
  88. (should
  89. (string= "testing" (org-babel-execute-src-block
  90. nil (org-babel-lob-get-info))))
  91. (forward-line 1)
  92. (should (string= "123" (org-babel-execute-src-block
  93. nil (org-babel-lob-get-info))))))))
  94. (ert-deftest test-ob-lob/export-lob-lines ()
  95. "Test the export of a variety of library babel call lines."
  96. (let ((org-babel-inline-result-wrap "=%s=")
  97. (org-export-use-babel t))
  98. (org-test-at-id "72ddeed3-2d17-4c7f-8192-a575d535d3fc"
  99. (org-narrow-to-subtree)
  100. (let ((string (org-with-wide-buffer (buffer-string)))
  101. (narrowing (list (point-min) (point-max))))
  102. (with-temp-buffer
  103. (org-mode)
  104. (insert string)
  105. (apply #'narrow-to-region narrowing)
  106. (org-babel-exp-process-buffer)
  107. (message (buffer-string))
  108. (goto-char (point-min))
  109. (should (re-search-forward "^: 0" nil t))
  110. (should (re-search-forward "call {{{results(=2=)}}} stuck" nil t))
  111. (should (re-search-forward
  112. "exported =call_double(it=2)= because" nil t))
  113. (should (re-search-forward "^{{{results(=6=)}}} because" nil t))
  114. (should (re-search-forward "results 8 should" nil t))
  115. (should (re-search-forward "following 2\\*5={{{results(=10=)}}} should" nil t)))))))
  116. (ert-deftest test-ob-lob/do-not-eval-lob-lines-in-example-blocks-on-export ()
  117. (require 'ox)
  118. (org-test-with-temp-text-in-file "
  119. for export
  120. #+begin_example
  121. #+call: rubbish()
  122. #+end_example"
  123. (should (progn (org-babel-exp-process-buffer) t))))
  124. (ert-deftest test-ob-lob/caching-call-line ()
  125. (let ((temporary-value-for-test 0))
  126. (org-test-with-temp-text "
  127. #+name: call-line-caching-example
  128. #+begin_src emacs-lisp :var bar=\"baz\"
  129. (setq temporary-value-for-test (+ 1 temporary-value-for-test))
  130. #+end_src
  131. <point>#+call: call-line-caching-example(\"qux\") :cache yes
  132. "
  133. ;; first execution should flip value to t
  134. (should
  135. (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1))
  136. ;; if cached, second evaluation will retain the t value
  137. (should
  138. (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1)))))
  139. (ert-deftest test-ob-lob/named-caching-call-line ()
  140. (let ((temporary-value-for-test 0))
  141. (org-test-with-temp-text "
  142. #+name: call-line-caching-example
  143. #+begin_src emacs-lisp :var bar=\"baz\"
  144. (setq temporary-value-for-test (+ 1 temporary-value-for-test))
  145. #+end_src
  146. #+name: call-line-caching-called
  147. <point>#+call: call-line-caching-example(\"qux\") :cache yes
  148. "
  149. ;; first execution should flip value to t
  150. (should
  151. (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1))
  152. ;; if cached, second evaluation will retain the t value
  153. (should
  154. (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1)))))
  155. (ert-deftest test-ob-lob/assignment-with-newline ()
  156. "Test call lines with an argument containing a newline character."
  157. (should
  158. (equal " foo"
  159. (org-test-with-temp-text "
  160. #+name: test-newline
  161. #+begin_src emacs-lisp :var x=\"a\"
  162. 'foo
  163. #+end_src
  164. call_test-newline[:eval yes :results raw](\"a\nb\")<point>"
  165. (org-babel-execute-src-block nil (org-babel-lob-get-info))
  166. (buffer-substring (point) (point-max)))))
  167. (should
  168. (equal " bar"
  169. (org-test-with-temp-text "
  170. #+name: test-newline
  171. #+begin_src emacs-lisp :var x=\"a\"
  172. 'bar
  173. #+end_src
  174. call_test-newline[:eval yes :results raw]('(1\n2))<point>"
  175. (org-babel-execute-src-block nil (org-babel-lob-get-info))
  176. (buffer-substring (point) (point-max))))))
  177. (ert-deftest test-ob-lob/external-reference-syntax ()
  178. "Test external reference syntax for Babel calls."
  179. (should
  180. (= 2
  181. (org-test-with-temp-text-in-file
  182. "#+name: foo\n#+begin_src emacs-lisp\n(+ 1 1)\n#+end_src"
  183. (let ((file (buffer-file-name)))
  184. (org-test-with-temp-text (format "#+call: %s:foo()" file)
  185. (org-babel-execute-src-block nil (org-babel-lob-get-info))))))))
  186. (ert-deftest test-ob-lob/call-with-indirection ()
  187. "Test calling code with indirection."
  188. (should
  189. (= 2
  190. (org-test-with-temp-text
  191. "
  192. #+name: foo
  193. #+begin_src emacs-lisp
  194. \(+ 1 1)
  195. #+end_src
  196. #+name: bar
  197. #+call: foo()
  198. <point>#+call: bar()"
  199. (org-babel-execute-src-block nil (org-babel-lob-get-info)))))
  200. (should
  201. (= 10
  202. (org-test-with-temp-text
  203. "
  204. #+name: foo
  205. #+begin_src emacs-lisp :var x=1
  206. \(* 2 x)
  207. #+end_src
  208. #+name: bar
  209. #+call: foo(x=3)
  210. <point>#+call: bar(x=5)"
  211. (org-babel-execute-src-block nil (org-babel-lob-get-info)))))
  212. (should
  213. (= 6
  214. (org-test-with-temp-text
  215. "
  216. #+name: foo
  217. #+begin_src emacs-lisp :var x=1
  218. \(* 2 x)
  219. #+end_src
  220. #+name: bar
  221. #+call: foo(x=3)
  222. <point>#+call: bar()"
  223. (org-babel-execute-src-block nil (org-babel-lob-get-info))))))
  224. (ert-deftest test-ob-lob/confirm-evaluate ()
  225. "Test confirmation when exporting lob calls."
  226. ;; With the default `org-confirm-babel-evaluate' of t, the caller is
  227. ;; queried one time.
  228. (should
  229. (= 1
  230. (let ((org-export-use-babel t)
  231. (org-confirm-babel-evaluate t)
  232. (confirm-evaluate-calls 0))
  233. (cl-letf (((symbol-function 'yes-or-no-p)
  234. (lambda (&rest _ignore)
  235. (cl-incf confirm-evaluate-calls)
  236. t)))
  237. (org-test-with-temp-text
  238. "
  239. #+name: foo
  240. #+begin_src emacs-lisp
  241. nil
  242. #+end_src
  243. #+call: foo()"
  244. (let ((string (buffer-string)))
  245. (with-temp-buffer
  246. (org-mode)
  247. (insert string)
  248. (org-babel-exp-process-buffer)
  249. confirm-evaluate-calls))))))))
  250. (provide 'test-ob-lob)
  251. ;;; test-ob-lob.el ends here