test-ob-lob.el 8.3 KB

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