emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob 66dfd0eab29cebcdb3053214d7e28a25ac068b50 8597 bytes (raw)
name: testing/lisp/test-ob-lob.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
 
;;; test-ob-lob.el --- test for ob-lob.el  -*- lexical-binding: t; -*-

;; Copyright (c) 2010-2015, 2019 Eric Schulte
;; Authors: Eric Schulte

;; This file is not part of GNU Emacs.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(eval-and-compile (require 'cl-lib))
(require 'ob-lob)

\f
;;; Tests
(org-babel-lob-ingest
 (expand-file-name
  "library-of-babel.org"
  (expand-file-name
   "doc"
    (expand-file-name
     ".."
     (expand-file-name
      ".."
      (file-name-directory
       (or load-file-name buffer-file-name)))))))

(ert-deftest test-ob-lob/ingest ()
  "Test the ingestion of an Org file."
  (should (< 0 (org-babel-lob-ingest
		(expand-file-name "babel.org" org-test-example-dir)))))

(ert-deftest test-ob-lob/call-with-header-arguments ()
  "Test the evaluation of a library of babel #+call: line."
  (cl-letf (((symbol-function 'org-babel-insert-result)
	  (symbol-function 'ignore)))
    (let ((org-babel-library-of-babel
	   (org-test-with-temp-text-in-file
	       "
#+name: echo
#+begin_src emacs-lisp :var input=\"echo'd\"
  input
#+end_src

#+name: lob-minus
#+begin_src emacs-lisp :var a=0 :var b=0
  (- a b)
#+end_src"
	     (org-babel-lob-ingest)
	     org-babel-library-of-babel)))
      (org-test-at-id "fab7e291-fde6-45fc-bf6e-a485b8bca2f0"
	(move-beginning-of-line 1)
	(forward-line 6)
	(message (buffer-substring (point-at-bol) (point-at-eol)))
	(should
	 (string= "testing" (org-babel-execute-src-block
			     nil (org-babel-lob-get-info))))
	(forward-line 1)
	(should
	 (string= "testing" (caar (org-babel-execute-src-block
				   nil (org-babel-lob-get-info)))))
	(forward-line 1)
	(should
	 (string= "testing" (org-babel-execute-src-block
			     nil (org-babel-lob-get-info))))
	(forward-line 1)
	(should
	 (string= "testing" (caar (org-babel-execute-src-block
				   nil (org-babel-lob-get-info)))))
	(forward-line 1)
	(should
	 (string= "testing" (org-babel-execute-src-block
			     nil (org-babel-lob-get-info))))
	(forward-line 1)
	(should
	 (string= "testing" (caar (org-babel-execute-src-block
				   nil (org-babel-lob-get-info)))))
	(forward-line 1) (beginning-of-line) (forward-char 27)
	(should
	 (string= "testing" (org-babel-execute-src-block
			     nil (org-babel-lob-get-info))))
	(forward-line 1) (beginning-of-line) (forward-char 27)
	(should
	 (string= "testing" (caar (org-babel-execute-src-block
				   nil (org-babel-lob-get-info)))))
	(forward-line 1) (beginning-of-line)
	(should
	 (= 4 (org-babel-execute-src-block nil (org-babel-lob-get-info))))
	(forward-line 1)
	(should
	 (string= "testing" (org-babel-execute-src-block
			     nil (org-babel-lob-get-info))))
	(forward-line 1)
	(should (string= "123" (org-babel-execute-src-block
				nil (org-babel-lob-get-info))))))))

(ert-deftest test-ob-lob/export-lob-lines ()
  "Test the export of a variety of library babel call lines."
  (let ((org-babel-inline-result-wrap "=%s=")
	(org-export-use-babel t))
    (org-test-at-id "72ddeed3-2d17-4c7f-8192-a575d535d3fc"
      (org-narrow-to-subtree)
      (let ((string (org-with-wide-buffer (buffer-string)))
	    (narrowing (list (point-min) (point-max))))
	(with-temp-buffer
	  (org-mode)
	  (insert string)
	  (apply #'narrow-to-region narrowing)
	  (org-babel-exp-process-buffer)
	  (message (buffer-string))
	  (goto-char (point-min))
	  (should (re-search-forward "^: 0" nil t))
	  (should (re-search-forward "call {{{results(=2=)}}} stuck" nil t))
	  (should (re-search-forward
		   "exported =call_double(it=2)= because" nil t))
	  (should (re-search-forward "^{{{results(=6=)}}} because" nil t))
	  (should (re-search-forward "results 8 should" nil t))
	  (should (re-search-forward "following 2\\*5={{{results(=10=)}}} should" nil t)))))))

(ert-deftest test-ob-lob/do-not-eval-lob-lines-in-example-blocks-on-export ()
  (require 'ox)
  (org-test-with-temp-text-in-file "
for export
#+begin_example
#+call: rubbish()
#+end_example"
    (should (progn (org-babel-exp-process-buffer) t))))

(defvar temporary-value-for-test)

(ert-deftest test-ob-lob/caching-call-line ()
  (let ((temporary-value-for-test 0))
    (org-test-with-temp-text "
#+name: call-line-caching-example
#+begin_src emacs-lisp :var bar=\"baz\"
  (setq temporary-value-for-test (+ 1 temporary-value-for-test))
#+end_src

<point>#+call: call-line-caching-example(\"qux\") :cache yes
"
      ;; first execution should flip value to t
      (should
       (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1))
      ;; if cached, second evaluation will retain the t value
      (org-test-ignore-duplicate
        (should
         (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1))))))

(ert-deftest test-ob-lob/named-caching-call-line ()
  (let ((temporary-value-for-test 0))
    (org-test-with-temp-text "
#+name: call-line-caching-example
#+begin_src emacs-lisp :var bar=\"baz\"
  (setq temporary-value-for-test (+ 1 temporary-value-for-test))
#+end_src

#+name: call-line-caching-called
<point>#+call: call-line-caching-example(\"qux\") :cache yes
"
      ;; first execution should flip value to t
      (should
       (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1))
      ;; if cached, second evaluation will retain the t value
      (org-test-ignore-duplicate
        (should
         (eq (org-babel-execute-src-block nil (org-babel-lob-get-info)) 1))))))

(ert-deftest test-ob-lob/assignment-with-newline ()
  "Test call lines with an argument containing a newline character."
  (should
   (equal " foo"
	  (org-test-with-temp-text "
#+name: test-newline
#+begin_src emacs-lisp :var x=\"a\"
'foo
#+end_src

call_test-newline[:eval yes :results raw](\"a\nb\")<point>"
	    (org-babel-execute-src-block nil (org-babel-lob-get-info))
	    (buffer-substring (point) (point-max)))))
  (should
   (equal " bar"
	  (org-test-with-temp-text "
#+name: test-newline
#+begin_src emacs-lisp :var x=\"a\"
'bar
#+end_src

call_test-newline[:eval yes :results raw]('(1\n2))<point>"
	    (org-babel-execute-src-block nil (org-babel-lob-get-info))
	    (buffer-substring (point) (point-max))))))

(ert-deftest test-ob-lob/external-reference-syntax ()
  "Test external reference syntax for Babel calls."
  (should
   (= 2
      (org-test-with-temp-text-in-file
	  "#+name: foo\n#+begin_src emacs-lisp\n(+ 1 1)\n#+end_src"
	(let ((file (buffer-file-name)))
	  (org-test-with-temp-text (format "#+call: %s:foo()" file)
	    (org-babel-execute-src-block nil (org-babel-lob-get-info))))))))

(ert-deftest test-ob-lob/call-with-indirection ()
  "Test calling code with indirection."
  (should
   (= 2
      (org-test-with-temp-text
	  "
#+name: foo
#+begin_src emacs-lisp
\(+ 1 1)
#+end_src

#+name: bar
#+call: foo()

<point>#+call: bar()"
	(org-babel-execute-src-block nil (org-babel-lob-get-info)))))
  (should
   (= 10
      (org-test-with-temp-text
	  "
#+name: foo
#+begin_src emacs-lisp :var x=1
\(* 2 x)
#+end_src

#+name: bar
#+call: foo(x=3)

<point>#+call: bar(x=5)"
	(org-babel-execute-src-block nil (org-babel-lob-get-info)))))
  (should
   (= 6
      (org-test-with-temp-text
	  "
#+name: foo
#+begin_src emacs-lisp :var x=1
\(* 2 x)
#+end_src

#+name: bar
#+call: foo(x=3)

<point>#+call: bar()"
	(org-babel-execute-src-block nil (org-babel-lob-get-info))))))

(ert-deftest test-ob-lob/confirm-evaluate ()
  "Test confirmation when exporting lob calls."
  ;; With the default `org-confirm-babel-evaluate' of t, the caller is
  ;; queried one time.
  (should
   (= 1
      (let ((org-export-use-babel t)
	    (org-confirm-babel-evaluate t)
	    (confirm-evaluate-calls 0))
	(cl-letf (((symbol-function 'yes-or-no-p)
		   (lambda (&rest _ignore)
		     (cl-incf confirm-evaluate-calls)
		     t)))
	  (org-test-with-temp-text
	      "
#+name: foo
#+begin_src emacs-lisp
  nil
#+end_src

#+call: foo()"
	    (let ((string (buffer-string)))
	      (with-temp-buffer
		(org-mode)
		(insert string)
		(org-babel-exp-process-buffer)
		confirm-evaluate-calls))))))))

(provide 'test-ob-lob)

;;; test-ob-lob.el ends here

debug log:

solving 66dfd0eab ...
found 66dfd0eab in https://list.orgmode.org/orgmode/87sf5cr7na.fsf@gmail.com/
found 188fee4c0 in https://git.savannah.gnu.org/cgit/emacs/org-mode.git
preparing index
index prepared:
100644 188fee4c00e9b6ba05338b585f2c9ecc4a275c2b	testing/lisp/test-ob-lob.el

applying [1/1] https://list.orgmode.org/orgmode/87sf5cr7na.fsf@gmail.com/
diff --git a/testing/lisp/test-ob-lob.el b/testing/lisp/test-ob-lob.el
index 188fee4c0..66dfd0eab 100644

Checking patch testing/lisp/test-ob-lob.el...
Applied patch testing/lisp/test-ob-lob.el cleanly.

index at:
100644 66dfd0eab29cebcdb3053214d7e28a25ac068b50	testing/lisp/test-ob-lob.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).