File size: 15,674 Bytes
3dcad1f |
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
;;;; modules.test --- exercise some of guile's module stuff -*- scheme -*-
;;;; Copyright (C) 2006, 2007, 2009-2011, 2014, 2019 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 3 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) #:prefix s:) ; for test purposes
#:use-module (test-suite lib))
(define (every? . args)
(not (not (apply every args))))
;;;
;;; Foundations.
;;;
(with-test-prefix "foundations"
(pass-if "modules don't remain anonymous"
;; This is a requirement for `psyntax': it stores module names and relies
;; on being able to `resolve-module' them.
(let ((m (make-module)))
(and (module-name m)
(eq? m (resolve-module (module-name m))))))
(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)))
(pass-if "module-reverse-lookup [pre-module-obarray]"
(let ((var (module-variable (current-module) 'string?)))
(eq? 'string? (module-reverse-lookup #f var))))
(pass-if-exception "module-reverse-lookup [wrong-type-arg]"
exception:wrong-type-arg
(module-reverse-lookup (current-module) 'foo))
(pass-if "the-root-module"
(eq? (module-public-interface the-root-module) the-scm-module))
(pass-if "the-scm-module"
;; THE-SCM-MODULE is its own public interface. See
;; <https://savannah.gnu.org/bugs/index.php?30623>.
(eq? (module-public-interface the-scm-module) the-scm-module)))
;;;
;;; module-use! / module-use-interfaces!
;;;
(with-test-prefix "module-use"
(let ((m (make-module)))
(pass-if "no uses initially"
(null? (module-uses m)))
(pass-if "using ice-9 q"
(begin
(module-use! m (resolve-interface '(ice-9 q)))
(equal? (module-uses m)
(list (resolve-interface '(ice-9 q))))))
(pass-if "using ice-9 q again"
(begin
(module-use! m (resolve-interface '(ice-9 q)))
(equal? (module-uses m)
(list (resolve-interface '(ice-9 q))))))
(pass-if "using ice-9 ftw"
(begin
(module-use-interfaces! m (list (resolve-interface '(ice-9 ftw))))
(equal? (module-uses m)
(list (resolve-interface '(ice-9 q))
(resolve-interface '(ice-9 ftw))))))
(pass-if "using ice-9 ftw again"
(begin
(module-use-interfaces! m (list (resolve-interface '(ice-9 ftw))))
(equal? (module-uses m)
(list (resolve-interface '(ice-9 q))
(resolve-interface '(ice-9 ftw))))))
(pass-if "using ice-9 control twice"
(begin
(module-use-interfaces! m (list (resolve-interface '(ice-9 control))
(resolve-interface '(ice-9 control))))
(equal? (module-uses m)
(list (resolve-interface '(ice-9 q))
(resolve-interface '(ice-9 ftw))
(resolve-interface '(ice-9 control))))))))
;;;
;;; Resolve-module.
;;;
(with-test-prefix "resolve-module"
(pass-if "#:ensure #t by default"
(module? (resolve-module (list (gensym)))))
(pass-if "#:ensure #t explicitly"
(module? (resolve-module (list (gensym)) #:ensure #t)))
(pass-if "#:ensure #f"
(not (resolve-module (list (gensym)) #:ensure #f))))
;;;
;;; 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)
;; We expect both VAR and VAL to be #f, as there
;; is no previous binding for 'imported in M.
(if var (error "unexpected var" var))
(if val (error "unexpected val" 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?
(eqv? (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))))
;;;
;;; R6RS compatibility
;;;
(with-test-prefix "module versions"
(pass-if "version-matches? for matching versions"
(version-matches? '(1 2 3) '(1 2 3)))
(pass-if "version-matches? for non-matching versions"
(not (version-matches? '(3 2 1) '(1 2 3))))
(pass-if "version-matches? against more specified version"
(version-matches? '(1 2) '(1 2 3)))
(pass-if "version-matches? against less specified version"
(not (version-matches? '(1 2 3) '(1 2)))))
(with-test-prefix "circular imports"
(pass-if-equal "#:select" 1
(begin
(eval
'(begin
(define-module (test-circular-imports))
(define (init-module-a)
(eval '(begin
(define-module (test-circular-imports a)
#:use-module (test-circular-imports b)
#:export (from-a))
(define from-a 1))
(current-module)))
(define (init-module-b)
(eval '(begin
(define-module (test-circular-imports b)
#:use-module ((test-circular-imports a)
#:select (from-a))
#:export (from-b))
(define from-b 2))
(current-module)))
(define (submodule-binder mod name)
(let ((m (make-module)))
(set-module-kind! m 'directory)
(set-module-name! m (append (module-name mod) (list name)))
(module-define-submodule! mod name m)
(case name
((a) (init-module-a))
((b) (init-module-b))
((c) #t)
(else (error "unreachable")))
m))
(set-module-submodule-binder! (current-module) submodule-binder))
(current-module))
(eval '(begin
(define-module (test-circular-imports c))
(use-modules (test-circular-imports a))
from-a)
(current-module)))))
|