summaryrefslogtreecommitdiff
path: root/test-suite/tests/modules.test
blob: 43e35d8b718458371a0299fb75108d5072f72c8c (plain)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
;;;; modules.test --- exercise some of guile's module stuff -*- scheme -*-

;;;; Copyright (C) 2006, 2007 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 2.1 of the License, or (at your option) any later version.
;;;;
;;;; This library 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
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (test-suite test-modules)
  :use-module (srfi srfi-1)
  :use-module ((ice-9 streams)  ;; for test purposes
               #:renamer (symbol-prefix-proc 's:))
  :use-module (test-suite lib))


(define (every? . args)
  (not (not (apply every args))))



;;;
;;; Foundations.
;;;

(with-test-prefix "foundations"

  (pass-if "module-add!"
    (let ((m (make-module))
          (value (cons 'x 'y)))
      (module-add! m 'something (make-variable value))
      (eq? (module-ref m 'something) value)))

  (pass-if "module-define!"
    (let ((m (make-module))
          (value (cons 'x 'y)))
      (module-define! m 'something value)
      (eq? (module-ref m 'something) value)))

  (pass-if "module-use!"
    (let ((m (make-module))
          (import (make-module)))
      (module-define! m 'something 'something)
      (module-define! import 'imported 'imported)
      (module-use! m import)
      (and (eq? (module-ref m 'something) 'something)
           (eq? (module-ref m 'imported)  'imported)
           (module-local-variable m 'something)
           (not (module-local-variable m 'imported))
           #t)))

  (pass-if "module-use! (duplicates local binding)"
    ;; Imported bindings can't override locale bindings.
    (let ((m (make-module))
          (import (make-module)))
      (module-define! m 'something 'something)
      (module-define! import 'something 'imported)
      (module-use! m import)
      (eq? (module-ref m 'something) 'something)))

  (pass-if "module-locally-bound?"
     (let ((m (make-module))
           (import (make-module)))
       (module-define! m 'something #t)
       (module-define! import 'imported #t)
       (module-use! m import)
       (and (module-locally-bound? m 'something)
            (not (module-locally-bound? m 'imported)))))

  (pass-if "module-{local-,}variable"
     (let ((m (make-module))
           (import (make-module)))
       (module-define! m 'local #t)
       (module-define! import 'imported #t)
       (module-use! m import)
       (and (module-local-variable m 'local)
            (not (module-local-variable m 'imported))
            (eq? (module-variable m 'local)
                 (module-local-variable m 'local))
            (eq? (module-local-variable import 'imported)
                 (module-variable m 'imported)))))

  (pass-if "module-import-interface"
    (and (every? (lambda (sym iface)
                   (eq? (module-import-interface (current-module) sym)
                        iface))
                 '(current-module exception:bad-variable every)
                 (cons the-scm-module
                       (map resolve-interface
                            '((test-suite lib) (srfi srfi-1)))))

         ;; For renamed bindings, a custom interface is used so we can't
         ;; check for equality with `eq?'.
         (every? (lambda (sym iface)
                   (let ((import
                          (module-import-interface (current-module) sym)))
                     (equal? (module-name import)
                             (module-name iface))))
                 '(s:make-stream s:stream-car s:stream-cdr)
                 (make-list 3 (resolve-interface '(ice-9 streams))))))

  (pass-if "module-reverse-lookup"
    (let ((mods   '((srfi srfi-1) (test-suite lib)      (ice-9 streams)))
          (syms   '(every         exception:bad-variable make-stream))
          (locals '(every         exception:bad-variable s:make-stream)))
      (every? (lambda (var sym)
                (eq? (module-reverse-lookup (current-module) var)
                     sym))
              (map module-variable
                   (map resolve-interface mods)
                   syms)
              locals))))



;;;
;;; Observers.
;;;

(with-test-prefix "observers"

  (pass-if "weak observer invoked"
    (let* ((m (make-module))
           (invoked 0))
      (module-observe-weak m (lambda (mod)
                               (if (eq? mod m)
                                   (set! invoked (+ invoked 1)))))
      (module-define! m 'something 2)
      (module-define! m 'something-else 1)
      (= invoked 2)))

  (pass-if "all weak observers invoked"
    ;; With the two-argument `module-observe-weak' available in previous
    ;; versions, the observer would get unregistered as soon as the observing
    ;; closure gets GC'd, making it impossible to use an anonymous lambda as
    ;; the observing procedure.

    (let* ((m (make-module))
           (observer-count 500)
           (observer-ids (let loop ((i observer-count)
                                    (ids '()))
                           (if (= i 0)
                               ids
                               (loop (- i 1) (cons (make-module) ids)))))
           (observers-invoked (make-hash-table observer-count)))

      ;; register weak observers
      (for-each (lambda (id)
                  (module-observe-weak m id
                                       (lambda (m)
                                         (hashq-set! observers-invoked
                                                     id #t))))
                observer-ids)

      (gc)

      ;; invoke them
      (module-call-observers m)

      ;; make sure all of them were invoked
      (->bool (every (lambda (id)
                       (hashq-ref observers-invoked id))
                     observer-ids))))

  (pass-if "imported bindings updated"
    (let ((m (make-module))
          (imported (make-module)))
      ;; Beautify them, notably adding them a public interface.
      (beautify-user-module! m)
      (beautify-user-module! imported)

      (module-use! m (module-public-interface imported))
      (module-define! imported 'imported-binding #t)

      ;; At this point, `imported-binding' is local to IMPORTED.
      (and (not (module-variable m 'imported-binding))
           (begin
             ;; Export `imported-binding' from IMPORTED.
             (module-export! imported '(imported-binding))

             ;; Make sure it is now visible from M.
             (module-ref m 'imported-binding))))))



;;;
;;; Duplicate bindings handling.
;;;

(with-test-prefix "duplicate bindings"

  (pass-if "simple duplicate handler"
    ;; Import the same binding twice.
    (let* ((m (make-module))
           (import1 (make-module))
           (import2 (make-module))
           (handler-invoked? #f)
           (handler (lambda (module name int1 val1 int2 val2 var val)
                      (set! handler-invoked? #t)
                      ;; Keep the first binding.
                      (or var (module-local-variable int1 name)))))

      (set-module-duplicates-handlers! m (list handler))
      (module-define! m 'something 'something)
      (set-module-name! import1 'imported-module-1)
      (set-module-name! import2 'imported-module-2)
      (module-define! import1 'imported 'imported-1)
      (module-define! import2 'imported 'imported-2)
      (module-use! m import1)
      (module-use! m import2)
      (and (eq? (module-ref m 'imported) 'imported-1)
           handler-invoked?))))


;;;
;;; Lazy binder.
;;;

(with-test-prefix "lazy binder"

  (pass-if "not invoked"
    (let ((m (make-module))
          (invoked? #f))
      (module-define! m 'something 2)
      (set-module-binder! m (lambda args (set! invoked? #t) #f))
      (and (module-ref m 'something)
           (not invoked?))))

  (pass-if "not invoked (module-add!)"
    (let ((m (make-module))
          (invoked? #f))
      (set-module-binder! m (lambda args (set! invoked? #t) #f))
      (module-add! m 'something (make-variable 2))
      (and (module-ref m 'something)
           (not invoked?))))

  (pass-if "invoked (module-ref)"
    (let ((m (make-module))
          (invoked? #f))
      (set-module-binder! m (lambda args (set! invoked? #t) #f))
      (false-if-exception (module-ref m 'something))
      invoked?))

  (pass-if "invoked (module-define!)"
    (let ((m (make-module))
          (invoked? #f))
      (set-module-binder! m (lambda args (set! invoked? #t) #f))
      (module-define! m 'something 2)
      (and invoked?
           (eq? (module-ref m 'something) 2))))

  (pass-if "honored (ref)"
    (let ((m (make-module))
          (invoked? #f)
          (value (cons 'x 'y)))
      (set-module-binder! m
                          (lambda (mod sym define?)
                            (set! invoked? #t)
                            (cond ((not (eq? m mod))
                                   (error "invalid module" mod))
                                  (define?
                                   (error "DEFINE? shouldn't be set"))
                                  (else
                                   (make-variable value)))))
      (and (eq? (module-ref m 'something) value)
           invoked?))))



;;;
;;; Higher-level features.
;;;

(with-test-prefix "autoload"

  (pass-if "module-autoload!"
     (let ((m (make-module)))
       (module-autoload! m '(ice-9 q) '(make-q))
       (not (not (module-ref m 'make-q)))))

  (pass-if "autoloaded"
     (catch #t
       (lambda ()
	 ;; Simple autoloading.
	 (eval '(begin
		  (define-module (test-autoload-one)
		    :autoload (ice-9 q) (make-q))
		  (not (not make-q)))
	       (current-module)))
	(lambda (key . args)
	  #f)))

  ;; In Guile 1.8.0 this failed because the binder in
  ;; `make-autoload-interface' would try to remove the autoload interface
  ;; from the module's "uses" without making sure it is still part of these
  ;; "uses".
  ;;
  (pass-if "autoloaded+used"
     (catch #t
       (lambda ()
	 (eval '(begin
		  (define-module (test-autoload-two)
		    :autoload (ice-9 q) (make-q)
		    :use-module (ice-9 q))
		  (not (not make-q)))
	       (current-module)))
	(lambda (key . args)
	  #f))))