Merge branch 'maint'
[org-mode.git] / testing / lisp / test-ob-lob.el
blob49f1f24fb1003bd9740669ec5ce1247abe1c4b5d
1 ;;; test-ob-lob.el --- test for ob-lob.el
3 ;; Copyright (c) 2010-2015 Eric Schulte
4 ;; Authors: Eric Schulte
6 ;; This file is not part of GNU Emacs.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Tests
23 (org-babel-lob-ingest
24 (expand-file-name
25 "library-of-babel.org"
26 (expand-file-name
27 "doc"
28 (expand-file-name
29 ".."
30 (expand-file-name
31 ".."
32 (file-name-directory
33 (or load-file-name buffer-file-name)))))))
35 (ert-deftest test-ob-lob/ingest ()
36 "Test the ingestion of an Org file."
37 (should (< 0 (org-babel-lob-ingest
38 (expand-file-name "babel.org" org-test-example-dir)))))
40 (ert-deftest test-ob-lob/call-with-header-arguments ()
41 "Test the evaluation of a library of babel #+call: line."
42 (letf (((symbol-function 'org-babel-insert-result)
43 (symbol-function 'ignore)))
44 (let ((org-babel-library-of-babel
45 (org-test-with-temp-text-in-file
47 #+name: echo
48 #+begin_src emacs-lisp :var input=\"echo'd\"
49 input
50 #+end_src
52 #+name: lob-minus
53 #+begin_src emacs-lisp :var a=0 :var b=0
54 (- a b)
55 #+end_src"
56 (org-babel-lob-ingest)
57 org-babel-library-of-babel)))
58 (org-test-at-id "fab7e291-fde6-45fc-bf6e-a485b8bca2f0"
59 (move-beginning-of-line 1)
60 (forward-line 6)
61 (message (buffer-substring (point-at-bol) (point-at-eol)))
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)
78 (should
79 (string= "testing" (org-babel-execute-src-block
80 nil (org-babel-lob-get-info))))
81 (forward-line 1)
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) (forward-char 27)
86 (should
87 (string= "testing" (org-babel-execute-src-block
88 nil (org-babel-lob-get-info))))
89 (forward-line 1) (beginning-of-line) (forward-char 27)
90 (should
91 (string= "testing" (caar (org-babel-execute-src-block
92 nil (org-babel-lob-get-info)))))
93 (forward-line 1) (beginning-of-line)
94 (should
95 (= 4 (org-babel-execute-src-block nil (org-babel-lob-get-info))))
96 (forward-line 1)
97 (should
98 (string= "testing" (org-babel-execute-src-block
99 nil (org-babel-lob-get-info))))
100 (forward-line 1)
101 (should (string= "123" (org-babel-execute-src-block
102 nil (org-babel-lob-get-info))))))))
104 (ert-deftest test-ob-lob/export-lob-lines ()
105 "Test the export of a variety of library babel call lines."
106 (let ((org-babel-inline-result-wrap "=%s=")
107 (org-export-use-babel t))
108 (org-test-at-id "72ddeed3-2d17-4c7f-8192-a575d535d3fc"
109 (org-narrow-to-subtree)
110 (let ((string (org-with-wide-buffer (buffer-string)))
111 (narrowing (list (point-min) (point-max))))
112 (with-temp-buffer
113 (org-mode)
114 (insert string)
115 (apply #'narrow-to-region narrowing)
116 (org-babel-exp-process-buffer)
117 (message (buffer-string))
118 (goto-char (point-min))
119 (should (re-search-forward "^: 0" nil t))
120 (should (re-search-forward "call {{{results(=2=)}}} stuck" nil t))
121 (should (re-search-forward
122 "exported =call_double(it=2)= because" nil t))
123 (should (re-search-forward "^{{{results(=6=)}}} because" nil t))
124 (should (re-search-forward "results 8 should" nil t))
125 (should (re-search-forward "following 2\\*5={{{results(=10=)}}} should" nil t)))))))
127 (ert-deftest test-ob-lob/do-not-eval-lob-lines-in-example-blocks-on-export ()
128 (require 'ox)
129 (org-test-with-temp-text-in-file "
130 for export
131 #+begin_example
132 #+call: rubbish()
133 #+end_example"
134 (should (progn (org-babel-exp-process-buffer) t))))
136 (ert-deftest test-ob-lob/caching-call-line ()
137 (let ((temporary-value-for-test 0))
138 (org-test-with-temp-text "
139 #+name: call-line-caching-example
140 #+begin_src emacs-lisp :var bar=\"baz\"
141 (setq temporary-value-for-test (+ 1 temporary-value-for-test))
142 #+end_src
144 <point>#+call: call-line-caching-example(\"qux\") :cache yes
146 ;; first execution should flip value to t
147 (should
148 (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1))
149 ;; if cached, second evaluation will retain the t value
150 (should
151 (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1)))))
153 (ert-deftest test-ob-lob/named-caching-call-line ()
154 (let ((temporary-value-for-test 0))
155 (org-test-with-temp-text "
156 #+name: call-line-caching-example
157 #+begin_src emacs-lisp :var bar=\"baz\"
158 (setq temporary-value-for-test (+ 1 temporary-value-for-test))
159 #+end_src
161 #+name: call-line-caching-called
162 <point>#+call: call-line-caching-example(\"qux\") :cache yes
164 ;; first execution should flip value to t
165 (should
166 (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1))
167 ;; if cached, second evaluation will retain the t value
168 (should
169 (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1)))))
171 (ert-deftest test-ob-lob/assignment-with-newline ()
172 "Test call lines with an argument containing a newline character."
173 (should
174 (equal " foo"
175 (org-test-with-temp-text "
176 #+name: test-newline
177 #+begin_src emacs-lisp :var x=\"a\"
178 'foo
179 #+end_src
181 call_test-newline[:eval yes :results raw](\"a\nb\")<point>"
182 (org-babel-execute-src-block nil (org-babel-lob-get-info))
183 (buffer-substring (point) (point-max)))))
184 (should
185 (equal " bar"
186 (org-test-with-temp-text "
187 #+name: test-newline
188 #+begin_src emacs-lisp :var x=\"a\"
189 'bar
190 #+end_src
192 call_test-newline[:eval yes :results raw]('(1\n2))<point>"
193 (org-babel-execute-src-block nil (org-babel-lob-get-info))
194 (buffer-substring (point) (point-max))))))
196 (provide 'test-ob-lob)
198 ;;; test-ob-lob.el ends here