Debian Codegraph @

test/load.scm

raw
1 ;;
2 ;; Tests for subtle effects of loading and autoloading
3 ;;
4
5 (use gauche.test)
6
7 (test-start "load")
8
9 (add-load-path ".")
10
11 ;; Some abstraction for win32... The file.util module provides
12 ;; higher abstraction, but we're not at the stage of using it yet.
13 (define (P path) (sys-normalize-pathname path))
14
15 (define (rmrf . files)
16 (dolist [f files]
17 (cond-expand
18 [gauche.os.windows
19 (sys-system #"rmdir /q /s ~(P f) > NUL 2>&1")
20 (sys-system #"del /q ~(P f) > NUL 2>&1")]
21 [else
22 (sys-system #"rm -rf ~f > /dev/null")])))
23
24 ;;----------------------------------------------------------------
25 (test-section "require and provide")
26
27 (rmrf "test.o")
28 (sys-mkdir "test.o" #o777)
29 (with-output-to-file "test.o/a.scm"
30 (^[]
31 (write '(provide "test.o/a"))
32 (newline)))
33
34 (test* "double require"
35 #t
36 (begin
37 (eval '(require "test.o/a") (interaction-environment))
38 (sys-unlink "test.o/a.scm")
39 (eval '(require "test.o/a") (interaction-environment))
40 #t))
41
42 (rmrf "test.o")
43 (sys-mkdir "test.o" #o777)
44 (with-output-to-file "test.o/b.scm"
45 (^[]
46 (write '(require "test.o/c"))
47 (write '(provide "test.o/b"))
48 (newline)))
49 (with-output-to-file "test.o/c.scm"
50 (^[]
51 (write '(require "test.o/b"))
52 (write '(provide "test.o/c"))
53 (newline)))
54
55 (test* "detecting loop of require"
56 (test-error)
57 (eval '(require "test.o/b") (interaction-environment)))
58
59 (rmrf "test.o")
60 (sys-mkdir "test.o" #o777)
61 (with-output-to-file "test.o/d.scm"
62 (^[]
63 (display "(define z 0)(")
64 (newline)))
65
66 (test "reload after error"
67 1
68 (^[]
69 (let ((m (make-module 'reload-after-error)))
70 (with-error-handler
71 (^e #t)
72 (^[] (eval '(require "test.o/d") (interaction-environment))))
73 (with-output-to-file "test.o/d.scm"
74 (^[]
75 (write '(select-module reload-after-error))
76 (write '(define z 1))
77 (write '(provide "tset.o/d"))))
78 (eval '(require "test.o/d") (interaction-environment))
79 (eval 'z m))))
80
81 ;; :environment arg -------------------------------------
82 (test-section "load environment")
83
84 (with-output-to-file "test.o/d.scm"
85 (^[] (display "(define foo 3)")))
86 (define-module load.test )
87 (define foo 8)
88
89 (test* ":environment argument"
90 3
91 (begin
92 (load "test.o/d" :environment (find-module 'load.test))
93 (with-module load.test foo)))
94
95 ;; a compicated case involving eval, load and restoration of environment.
96 ;; this is actually testing code in Scm_VMEval, but I put it here
97 ;; since the 'eval' test is done before i/o.
98 (with-output-to-file "test.o/d.scm"
99 (^[] (display "(define foo 6)")))
100
101 (test* "eval & load & environment" 6
102 (begin
103 (eval '(load "test.o/d") (find-module 'load.test))
104 (with-module load.test foo)))
105
106 ;; current-load-* --------------------------------------
107 (test-section "current-load-* info")
108
109 (with-output-to-file "test.o/c1.scm"
110 (^[]
111 (write '(print (current-load-history)))
112 (write '(print (current-load-next)))
113 (write '(print (current-load-port)))
114 (write '(print (current-load-path)))
115 (write '(load "./test.o/c2.scm"))
116 (write '(print (current-load-history)))
117 (write '(print (current-load-next)))
118 (write '(print (current-load-port)))
119 (write '(print (current-load-path)))))
120
121 (with-output-to-file "test.o/c2.scm"
122 (^[]
123 (write '(print (current-load-history)))
124 (write '(print (current-load-next)))
125 (write '(print (current-load-port)))
126 (write '(print (current-load-path)))))
127
128 (with-output-to-file "test.o/c.out"
129 (^[] (load "./test.o/c1.scm")))
130
131 (with-input-from-file "test.o/c.out"
132 (^[]
133 (test* "current-load-history (1)" #/^\(\(#<iport [^>]*test\/load\.scm/
134 (read-line) rxmatch)
135 (test* "current-load-next (1)" "()" (read-line))
136 (test* "current-load-port (1)" #/^#<iport [^>]*\/test\.o\/c1\.scm/
137 (read-line) rxmatch)
138 (test* "current-load-path (1)" "./test.o/c1.scm"
139 (read-line) rxmatch)
140
141 (test* "current-load-history (2)"
142 #/^\(\(#<iport [^>]*test\.o\/c1\.scm [^\)]*\) \(#<iport [^>]*test\/load\.scm [^\)]*\)/
143 (read-line) rxmatch)
144 (test* "current-load-next (2)" "()" (read-line))
145 (test* "current-load-port (2)" #/^#<iport [^>]*\/test\.o\/c2\.scm/
146 (read-line) rxmatch)
147 (test* "current-load-path (2)" "./test.o/c2.scm"
148 (read-line) rxmatch)
149
150 (test* "current-load-history (3)" #/^\(\(#<iport [^>]*test\/load\.scm/
151 (read-line) rxmatch)
152 (test* "current-load-next (3)" "()" (read-line))
153 (test* "current-load-port (3)" #/^#<iport [^>]*\/test\.o\/c1\.scm/
154 (read-line) rxmatch)
155 (test* "current-load-path (3)" "./test.o/c1.scm"
156 (read-line) rxmatch)
157 ))
158
159 ;; include --------------------------------------
160 (test-section "include")
161
162 (with-output-to-file "test.o/inc0.scm"
163 (^[]
164 (write '(define inc-var 4))
165 (write 'inc-var)))
166 (with-output-to-file "test.o/inc1.scm"
167 (^[]
168 (write '(define inc-var 10))
169 (write '(let ((x inc-var))
170 (include "inc0.scm")
171 (+ x inc-var)))))
172 (with-output-to-file "test.o/inc2.scm"
173 (^[]
174 (write '(define inc-var2 (let () (include "inc0"))))))
175
176 (define (include-into-toplevel filename)
177 (eval `(begin (define inc-var 0)
178 (include ,filename))
179 (current-module)))
180
181 (test* "include (relative to current, expands into toplevel)" 4
182 (include-into-toplevel "test.o/inc0"))
183 (test* "include (relative to current, expands inside let)" 4
184 (let ((inc-var 2))
185 (include "test.o/inc0")
186 inc-var))
187 (test* "include (relative to current, dots, expands inside let)" 4
188 (let ((inc-var 2))
189 (include "../src/test.o/inc0")
190 inc-var))
191 (test* "include (relative to current, dots, expands inside let)" 4
192 (let ((inc-var 2))
193 (include "./test.o/inc0")
194 inc-var))
195 (test* "include (absolute, expands into toplevel)" 4
196 (include-into-toplevel
197 (sys-normalize-pathname "test.o/inc0" :absolute #t)))
198 (test* "include (nested)" 14
199 (include-into-toplevel "test.o/inc1"))
200 (test* "include (postcondition)" 10
201 (eval 'inc-var (current-module)))
202 (test* "include (within init part)" 4
203 (let () (include "test.o/inc2.scm") inc-var2))
204
205 ;; autoloading -----------------------------------------
206 (test-section "autoload")
207
208 (with-output-to-file "test.o/l0.scm"
209 (^[] (write '(define foo 0))))
210 (autoload "test.o/l0" foo)
211 (test* "autoload (file)" 0 foo)
212
213 (with-output-to-file "test.o/l1.scm"
214 (^[] (write '(define foo 0))))
215 (autoload "test.o/l1" foo1)
216 (test* "autoload (file/error)" (test-error) foo1)
217
218 (with-output-to-file "test.o/l0.scm"
219 (^[]
220 (write '(define-module foo (extend scheme)))
221 (write '(load "./test.o/l1.scm" :environment (find-module 'foo)))))
222 (with-output-to-file "test.o/l1.scm"
223 (^[] (write '(expt 2 3))))
224
225 (test* "autoload environment" #t
226 (boolean (load "./test.o/l0.scm")))
227
228 (rmrf "test.o")
229
230 ;; library utilities -----------------------------------
231
232 (test-section "libutil")
233
234 (sys-system "mkdir test.o")
235 (sys-system #"mkdir ~(P \"test.o/_test\")")
236 (sys-system #"mkdir ~(P \"test.o/_tset\")")
237
238 (with-output-to-file "test.o/_test.scm"
239 (^[]
240 (write '(define-module _test ))
241 (write '(provide "_test"))))
242
243 (with-output-to-file "test.o/_test/_test.scm"
244 (^[]
245 (write '(define-module _test._test ))
246 (write '(provide "_test/_test"))))
247
248 (with-output-to-file "test.o/_test/_test1.scm"
249 (^[]
250 (write '(define-module _test._test1 ))
251 (write '(provide "_test/_test2"))))
252
253 (with-output-to-file "test.o/_tset/_test.scm"
254 (^[]
255 (write '(define-module _tset._test ))
256 (write '(provide "_tset/_test"))))
257
258 (with-output-to-file "test.o/_tset/_test1"
259 (^[]
260 (write '(define-module dummy ))))
261
262 (with-output-to-file "test.o/_tset/_test2.scm"
263 (^[]
264 (write '(provide "_tset/_test2"))))
265
266 (test* "library-fold _test" `((_test . ,(P "test.o/_test.scm")))
267 (library-fold '_test acons '() :paths '("./test.o")))
268
269 (test* "library-fold _test" `(("_test" . ,(P "test.o/_test.scm")))
270 (library-fold "_test" acons '() :paths '("./test.o")))
271
272 (define paths-a (map P '("./test.o" "./test.o/_test" "./test.o/_tset")))
273 (define paths-b (map P '("./test.o/_test" "./test.o" "./test.o/_tset")))
274
275 (test* "library-fold _test (multi)" `((_test . ,(P "test.o/_test.scm")))
276 (library-fold '_test acons '() :paths paths-a))
277 (test* "library-fold _test (multi)" `((_test . ,(P "test.o/_test.scm")))
278 (library-fold '_test acons '() :paths paths-b))
279 (test* "library-fold _test (multi)"
280 `(("_test" . ,(P "test.o/_test/_test.scm")))
281 (library-fold "_test" acons '() :paths paths-b))
282 (test* "library-fold _test (multi)"
283 `(("_test" . ,(P "test.o/_tset/_test.scm"))
284 ("_test" . ,(P "test.o/_test.scm"))
285 ("_test" . ,(P "test.o/_test/_test.scm")))
286 (library-fold "_test" acons '() :paths paths-b
287 :allow-duplicates? #t))
288 (test* "library-fold _test (non-strict)"
289 `((_test . ,(P "test.o/_tset/_test.scm"))
290 (_test . ,(P "test.o/_test.scm"))
291 (_test . ,(P "test.o/_test/_test.scm")))
292 (library-fold '_test acons '() :paths paths-b
293 :strict? #f :allow-duplicates? #t))
294
295 (test* "library-fold _test._test"
296 `((_test._test . ,(P "test.o/_test/_test.scm")))
297 (library-fold '_test._test acons '() :paths paths-b))
298 (test* "library-fold _test/_test"
299 `(("_test/_test" . ,(P "test.o/_test/_test.scm")))
300 (library-fold "_test/_test" acons '() :paths paths-b))
301
302 ;; needs sort the result, for the order library-fold returns depends on
303 ;; readdir(), which may be system dependent.
304 (test* "library-fold _test.*"
305 `((_test._test . ,(P "test.o/_test/_test.scm"))
306 (_test._test1 . ,(P "test.o/_test/_test1.scm")))
307 (sort (library-fold '_test.* acons '() :paths paths-b)
308 (lambda (a b) (string<? (cdr a) (cdr b)))))
309 (test* "library-fold _tset.*"
310 `((_tset._test . ,(P "test.o/_tset/_test.scm")))
311 (sort (library-fold '_tset.* acons '() :paths paths-b)
312 (lambda (a b) (string<? (cdr a) (cdr b)))))
313 (test* "library-fold _tset/*"
314 `(("_tset/_test" . ,(P "test.o/_tset/_test.scm"))
315 ("_tset/_test2" . ,(P "test.o/_tset/_test2.scm")))
316 (sort (library-fold "_tset/*" acons '() :paths paths-b)
317 (lambda (a b) (string<? (cdr a) (cdr b)))))
318
319 (test* "library-fold _test.*1"
320 `((_test._test1 . ,(P "test.o/_test/_test1.scm")))
321 (sort (library-fold '_test.*1 acons '() :paths paths-b)
322 (lambda (a b) (string<? (cdr a) (cdr b)))))
323 (test* "library-fold _*t._te*"
324 `((_test._test . ,(P "test.o/_test/_test.scm"))
325 (_test._test1 . ,(P "test.o/_test/_test1.scm"))
326 (_tset._test . ,(P "test.o/_tset/_test.scm")))
327 (sort (library-fold '_*t._te* acons '() :paths paths-b)
328 (lambda (a b) (string<? (cdr a) (cdr b)))))
329 (test* "library-fold */*"
330 `(("_test/_test" . ,(P "test.o/_test/_test.scm"))
331 ("_test/_test1" . ,(P "test.o/_test/_test1.scm"))
332 ("_tset/_test" . ,(P "test.o/_tset/_test.scm"))
333 ("_tset/_test2" . ,(P "test.o/_tset/_test2.scm")))
334 (sort (library-fold "*/*" acons '() :paths paths-b)
335 (lambda (a b) (string<? (cdr a) (cdr b)))))
336
337 (test* "library-fold _t??t._test?"
338 `((_test._test1 . ,(P "test.o/_test/_test1.scm")))
339 (sort (library-fold '_t??t._test? acons '() :paths paths-b)
340 (lambda (a b) (string<? (cdr a) (cdr b)))))
341 (test* "library-fold ?test.?test"
342 `((_test._test . ,(P "test.o/_test/_test.scm")))
343 (sort (library-fold '?test.?test acons '() :paths paths-b)
344 (lambda (a b) (string<? (cdr a) (cdr b)))))
345 (test* "library-fold _t??t._test?"
346 `((_test._test1 . ,(P "test.o/_test/_test1.scm"))
347 (_tset._test2 . ,(P "test.o/_tset/_test2.scm")))
348 (sort (library-fold '_t??t._test? acons '() :paths paths-b :strict? #f)
349 (lambda (a b) (string<? (cdr a) (cdr b)))))
350 (test* "library-fold _t??t/_test?"
351 `(("_test/_test1" . ,(P "test.o/_test/_test1.scm"))
352 ("_tset/_test2" . ,(P "test.o/_tset/_test2.scm")))
353 (sort (library-fold "_t??t/_test?" acons '() :paths paths-b)
354 (lambda (a b) (string<? (cdr a) (cdr b)))))
355 (test* "library-fold _t??t?/_test?"
356 '()
357 (sort (library-fold "_t??t?/_test?" acons '() :paths paths-b)
358 (lambda (a b) (string<? (cdr a) (cdr b)))))
359
360 (test* "library-map" `((_test._test . ,(P "test.o/_test/_test.scm"))
361 (_test._test1 . ,(P "test.o/_test/_test1.scm")))
362 (sort (library-map '_test.* cons :paths paths-b)
363 (lambda (a b) (string<? (cdr a) (cdr b)))))
364 (test* "library-for-each" `((_test._test . ,(P "test.o/_test/_test.scm"))
365 (_test._test1 . ,(P "test.o/_test/_test1.scm")))
366 (let ((p '()))
367 (library-for-each '_test.*
368 (lambda (x y) (push! p (cons x y)))
369 :paths paths-b)
370 (sort p (lambda (a b) (string<? (cdr a) (cdr b))))))
371
372 (test* "library-exists? _test" #t
373 (not (not (library-exists? '_test :paths paths-b))))
374 (test* "library-exists? _test1" #f
375 (not (not (library-exists? '_test1 :paths paths-b))))
376 (test* "library-exists? _test1, non-strict" #t
377 (not (not (library-exists? '_test1 :paths paths-b :strict? #f))))
378 (test* "library-exists? _tset._test" #t
379 (not (not (library-exists? '_tset._test :paths paths-b :strict? #f))))
380 (test* "library-exists? \"_test1\"" #t
381 (not (not (library-exists? "_test1" :paths paths-b))))
382 (test* "library-exists? \"_tset/_test2\"" #t
383 (not (not (library-exists? "_tset/_test2" :paths paths-b))))
384 (test* "library-exists? \"_test9\"" #f
385 (not (not (library-exists? "_test9" :paths paths-b))))
386
387 (test* "library-exists? gauche" #t
388 (not (not (library-exists? 'gauche :paths paths-b))))
389 (test* "library-exists? gauche, force-search" #f
390 (not (not (library-exists? 'gauche :paths paths-b :force-search? #t))))
391 (test* "library-exists? gauche" #t
392 (not (not (library-exists? "gauche" :paths paths-b))))
393 ;;NB: this no longer work since gauche.object is compiled in.
394 ;(test* "library-exists? gauche/object" #t
395 ; (not (not (library-exists? "gauche/object" :paths paths-b))))
396
397 ;; we check module here, since gauche.libutil is autoloaded.
398 (test-module 'gauche.libutil)
399
400 (rmrf "test.o")
401
402 ;; Load-path hook -----------------------------------
403
404 (test-section "load-path hook")
405
406 (define (dummy-load-path-hook archive relpath suffixes)
407 (and (equal? (sys-basename archive) "test.o")
408 (member ".scm" suffixes)
409 (cons #"~|archive|/~|relpath|.scm"
410 (^[path]
411 (open-input-string
412 (format "(define *path* ~s)" path))))))
413
414 (test* "dummy-load-path-hook" #"~(sys-getcwd)/test.o/non/existent/file.scm"
415 (unwind-protect
416 (begin
417 ((with-module gauche.internal %add-load-path-hook!)
418 dummy-load-path-hook)
419 ;; just create a dummy file - content doesn't matter.
420 (with-output-to-file "test.o"
421 (^[] (print)))
422 (load "non/existent/file" :paths `(,#"~(sys-getcwd)/test.o"))
423 (module-binding-ref (current-module) '*path*))
424 ((with-module gauche.internal %delete-load-path-hook!)
425 dummy-load-path-hook)))
426
427 (test-end)