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