test-ob-lob.el 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ;;; test-ob-lob.el --- test for ob-lob.el
  2. ;; Copyright (c) 2010-2015 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 <http://www.gnu.org/licenses/>.
  15. ;;; Tests
  16. (org-babel-lob-ingest
  17. (expand-file-name
  18. "library-of-babel.org"
  19. (expand-file-name
  20. "doc"
  21. (expand-file-name
  22. ".."
  23. (expand-file-name
  24. ".."
  25. (file-name-directory
  26. (or load-file-name buffer-file-name)))))))
  27. (ert-deftest test-ob-lob/ingest ()
  28. "Test the ingestion of an Org file."
  29. (should (< 0 (org-babel-lob-ingest
  30. (expand-file-name "babel.org" org-test-example-dir)))))
  31. (ert-deftest test-ob-lob/call-with-header-arguments ()
  32. "Test the evaluation of a library of babel #+call: line."
  33. (letf (((symbol-function 'org-babel-insert-result)
  34. (symbol-function 'ignore)))
  35. (let ((org-babel-library-of-babel
  36. (org-test-with-temp-text-in-file
  37. "
  38. #+name: echo
  39. #+begin_src emacs-lisp :var input=\"echo'd\"
  40. input
  41. #+end_src
  42. #+name: lob-minus
  43. #+begin_src emacs-lisp :var a=0 :var b=0
  44. (- a b)
  45. #+end_src"
  46. (org-babel-lob-ingest)
  47. org-babel-library-of-babel)))
  48. (org-test-at-id "fab7e291-fde6-45fc-bf6e-a485b8bca2f0"
  49. (move-beginning-of-line 1)
  50. (forward-line 6)
  51. (message (buffer-substring (point-at-bol) (point-at-eol)))
  52. (should
  53. (string= "testing" (org-babel-execute-src-block
  54. nil (org-babel-lob-get-info))))
  55. (forward-line 1)
  56. (should
  57. (string= "testing" (caar (org-babel-execute-src-block
  58. nil (org-babel-lob-get-info)))))
  59. (forward-line 1)
  60. (should
  61. (string= "testing" (org-babel-execute-src-block
  62. nil (org-babel-lob-get-info))))
  63. (forward-line 1)
  64. (should
  65. (string= "testing" (caar (org-babel-execute-src-block
  66. nil (org-babel-lob-get-info)))))
  67. (forward-line 1)
  68. (should
  69. (string= "testing" (org-babel-execute-src-block
  70. nil (org-babel-lob-get-info))))
  71. (forward-line 1)
  72. (should
  73. (string= "testing" (caar (org-babel-execute-src-block
  74. nil (org-babel-lob-get-info)))))
  75. (forward-line 1) (beginning-of-line) (forward-char 27)
  76. (should
  77. (string= "testing" (org-babel-execute-src-block
  78. nil (org-babel-lob-get-info))))
  79. (forward-line 1) (beginning-of-line) (forward-char 27)
  80. (should
  81. (string= "testing" (caar (org-babel-execute-src-block
  82. nil (org-babel-lob-get-info)))))
  83. (forward-line 1) (beginning-of-line)
  84. (should
  85. (= 4 (org-babel-execute-src-block nil (org-babel-lob-get-info))))
  86. (forward-line 1)
  87. (should
  88. (string= "testing" (org-babel-execute-src-block
  89. nil (org-babel-lob-get-info))))
  90. (forward-line 1)
  91. (should (string= "123" (org-babel-execute-src-block
  92. nil (org-babel-lob-get-info))))))))
  93. (ert-deftest test-ob-lob/export-lob-lines ()
  94. "Test the export of a variety of library babel call lines."
  95. (let ((org-babel-inline-result-wrap "=%s=")
  96. (org-export-use-babel t))
  97. (org-test-at-id "72ddeed3-2d17-4c7f-8192-a575d535d3fc"
  98. (org-narrow-to-subtree)
  99. (let ((string (org-with-wide-buffer (buffer-string)))
  100. (narrowing (list (point-min) (point-max))))
  101. (with-temp-buffer
  102. (org-mode)
  103. (insert string)
  104. (apply #'narrow-to-region narrowing)
  105. (org-babel-exp-process-buffer)
  106. (message (buffer-string))
  107. (goto-char (point-min))
  108. (should (re-search-forward "^: 0" nil t))
  109. (should (re-search-forward "call {{{results(=2=)}}} stuck" nil t))
  110. (should (re-search-forward
  111. "exported =call_double(it=2)= because" nil t))
  112. (should (re-search-forward "^{{{results(=6=)}}} because" nil t))
  113. (should (re-search-forward "results 8 should" nil t))
  114. (should (re-search-forward "following 2\\*5={{{results(=10=)}}} should" nil t)))))))
  115. (ert-deftest test-ob-lob/do-not-eval-lob-lines-in-example-blocks-on-export ()
  116. (require 'ox)
  117. (org-test-with-temp-text-in-file "
  118. for export
  119. #+begin_example
  120. #+call: rubbish()
  121. #+end_example"
  122. (should (progn (org-babel-exp-process-buffer) t))))
  123. (ert-deftest test-ob-lob/caching-call-line ()
  124. (let ((temporary-value-for-test 0))
  125. (org-test-with-temp-text "
  126. #+name: call-line-caching-example
  127. #+begin_src emacs-lisp :var bar=\"baz\"
  128. (setq temporary-value-for-test (+ 1 temporary-value-for-test))
  129. #+end_src
  130. <point>#+call: call-line-caching-example(\"qux\") :cache yes
  131. "
  132. ;; first execution should flip value to t
  133. (should
  134. (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1))
  135. ;; if cached, second evaluation will retain the t value
  136. (should
  137. (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1)))))
  138. (ert-deftest test-ob-lob/named-caching-call-line ()
  139. (let ((temporary-value-for-test 0))
  140. (org-test-with-temp-text "
  141. #+name: call-line-caching-example
  142. #+begin_src emacs-lisp :var bar=\"baz\"
  143. (setq temporary-value-for-test (+ 1 temporary-value-for-test))
  144. #+end_src
  145. #+name: call-line-caching-called
  146. <point>#+call: call-line-caching-example(\"qux\") :cache yes
  147. "
  148. ;; first execution should flip value to t
  149. (should
  150. (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1))
  151. ;; if cached, second evaluation will retain the t value
  152. (should
  153. (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1)))))
  154. (ert-deftest test-ob-lob/assignment-with-newline ()
  155. "Test call lines with an argument containing a newline character."
  156. (should
  157. (equal " foo"
  158. (org-test-with-temp-text "
  159. #+name: test-newline
  160. #+begin_src emacs-lisp :var x=\"a\"
  161. 'foo
  162. #+end_src
  163. call_test-newline[:eval yes :results raw](\"a\nb\")<point>"
  164. (org-babel-execute-src-block nil (org-babel-lob-get-info))
  165. (buffer-substring (point) (point-max)))))
  166. (should
  167. (equal " bar"
  168. (org-test-with-temp-text "
  169. #+name: test-newline
  170. #+begin_src emacs-lisp :var x=\"a\"
  171. 'bar
  172. #+end_src
  173. call_test-newline[:eval yes :results raw]('(1\n2))<point>"
  174. (org-babel-execute-src-block nil (org-babel-lob-get-info))
  175. (buffer-substring (point) (point-max))))))
  176. (provide 'test-ob-lob)
  177. ;;; test-ob-lob.el ends here