| 1 |
;; |
| 2 |
;; testing module system |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
|
| 7 |
(test-start "module") |
| 8 |
|
| 9 |
;;------------------------------------------------------------------ |
| 10 |
;; basic test |
| 11 |
|
| 12 |
(define-module M |
| 13 |
(define a 3) |
| 14 |
(define cons +)) |
| 15 |
|
| 16 |
(define a 100) |
| 17 |
(define b 200) |
| 18 |
|
| 19 |
(test "define-module" #t (lambda () (module? (find-module 'M)))) |
| 20 |
(test "with-module" 3 (lambda () (with-module M a))) |
| 21 |
(test "with-module" 5 (lambda () (with-module M (cons a 2)))) |
| 22 |
(test "with-module" '(3 . 2) (lambda () (cons (with-module M a) 2))) |
| 23 |
(test "with-module" 5 |
| 24 |
(lambda () |
| 25 |
(eval '(with-module M (define b 2) (cons a b)) |
| 26 |
(interaction-environment)))) |
| 27 |
(test "with-module" 2 (lambda () (with-module M b))) |
| 28 |
(test "with-module" 300 |
| 29 |
(lambda () (with-module M |
| 30 |
(cons (with-module user a) (with-module user b))))) |
| 31 |
|
| 32 |
(test "with-module (error)" (test-error) |
| 33 |
(lambda () (eval '(with-module MM 4) (interaction-environment)))) |
| 34 |
|
| 35 |
(with-module M |
| 36 |
(define + list) |
| 37 |
(define if list)) |
| 38 |
|
| 39 |
(test "with-module in head position" '(2 3) |
| 40 |
(lambda () |
| 41 |
((with-module M +) 2 3))) |
| 42 |
|
| 43 |
(test "with-module in head position" '(3 5 6) |
| 44 |
(lambda () |
| 45 |
(with-module M |
| 46 |
(if ((with-module scheme if) 2 3 4) 5 6)))) |
| 47 |
|
| 48 |
(define-module MA |
| 49 |
(export with-module) |
| 50 |
(define-syntax with-module |
| 51 |
(syntax-rules () |
| 52 |
((_ a b) list)))) |
| 53 |
|
| 54 |
(test "with-module in head position (shadowed)" '(1 2 3) |
| 55 |
(lambda () |
| 56 |
(with-module MA |
| 57 |
((with-module x y) 1 2 3)))) |
| 58 |
|
| 59 |
(define-module MB |
| 60 |
(import MA) |
| 61 |
(export oops) |
| 62 |
(define-syntax oops |
| 63 |
(syntax-rules () |
| 64 |
((_ a) (define a 3))))) |
| 65 |
|
| 66 |
(test "with-module in head position (shadowed)" '(1 2 3) |
| 67 |
(lambda () |
| 68 |
(with-module MB |
| 69 |
((with-module x y) 1 2 3)))) |
| 70 |
|
| 71 |
(test "with-module in head position (in lambda body)" 6 |
| 72 |
(lambda () |
| 73 |
(let ((x 1)) |
| 74 |
((with-module MB oops) x) |
| 75 |
(+ x x)))) |
| 76 |
|
| 77 |
;;------------------------------------------------------------------ |
| 78 |
;; define-in-module |
| 79 |
|
| 80 |
(test "define-in-module" 99 |
| 81 |
(lambda () |
| 82 |
(eval '(define-in-module M aa 99) (interaction-environment)) |
| 83 |
(eval '(with-module M aa) (interaction-environment)))) |
| 84 |
|
| 85 |
(test "define-in-module" (test-error) |
| 86 |
(lambda () |
| 87 |
(eval '(define-in-module MM aa 99) (interaction-environment)) |
| 88 |
(eval '(with-module MM aa) (interaction-environment)))) |
| 89 |
|
| 90 |
;;------------------------------------------------------------------ |
| 91 |
;; import, export |
| 92 |
|
| 93 |
(define-module N |
| 94 |
(export push-result get-result reset-result |
| 95 |
shared-gloc0 shared-gloc1) |
| 96 |
|
| 97 |
(define result '()) |
| 98 |
(define (get-result) (reverse result)) |
| 99 |
(define (push-result r) (set! result (cons r result))) |
| 100 |
(define (reset-result) (set! result '())) |
| 101 |
|
| 102 |
;; for testing %alias-binding via :only and :rename |
| 103 |
(define shared-gloc0 0) |
| 104 |
(define shared-gloc1 1)) |
| 105 |
|
| 106 |
(define-module O |
| 107 |
(import N) |
| 108 |
|
| 109 |
(define + *) |
| 110 |
) |
| 111 |
|
| 112 |
(test "import/export" '(56 72) |
| 113 |
(lambda () |
| 114 |
(eval '(with-module O |
| 115 |
(reset-result) |
| 116 |
(define a 7) |
| 117 |
(define b 8) |
| 118 |
(define c 9) |
| 119 |
(push-result (+ a b)) |
| 120 |
(push-result (+ b c)) |
| 121 |
(get-result)) |
| 122 |
(interaction-environment)))) |
| 123 |
|
| 124 |
(test "import (error)" (test-error) |
| 125 |
(lambda () (eval '(import MM) (interaction-environment)))) |
| 126 |
|
| 127 |
(define-module OO |
| 128 |
(import (N :prefix N:)) |
| 129 |
(define + *)) |
| 130 |
|
| 131 |
(test "import w/prefix" '(56 72) |
| 132 |
(lambda () |
| 133 |
(eval '(with-module OO |
| 134 |
(N:reset-result) |
| 135 |
(define a 7) |
| 136 |
(define b 8) |
| 137 |
(define c 9) |
| 138 |
(N:push-result (+ a b)) |
| 139 |
(N:push-result (+ b c)) |
| 140 |
(N:get-result)) |
| 141 |
(interaction-environment)))) |
| 142 |
|
| 143 |
(test "import w/prefix (error)" (test-error) |
| 144 |
(lambda () |
| 145 |
(eval '(with-module OO (reset-result)) |
| 146 |
(interaction-environment)))) |
| 147 |
|
| 148 |
(test "import w/prefix (insertion)" 99 |
| 149 |
(lambda () |
| 150 |
(eval '(with-module N (export new-binding) (define (new-binding) 99)) |
| 151 |
(interaction-environment)) |
| 152 |
(eval '(with-module OO (N:new-binding)) (interaction-environment)))) |
| 153 |
|
| 154 |
(define-module O1 |
| 155 |
(import (N :only (reset-result get-result shared-gloc0)))) |
| 156 |
(test "import w/only" '() |
| 157 |
(lambda () (eval '(begin (reset-result) (get-result)) (find-module 'O1)))) |
| 158 |
(test "import w/only (error)" (test-error) |
| 159 |
(lambda () (eval '(push-result 'a) (find-module 'O1)))) |
| 160 |
(test "import w/only propagation" '(0 10) |
| 161 |
(lambda () |
| 162 |
(let ((pre (eval 'shared-gloc0 (find-module 'O1)))) |
| 163 |
(with-module N (set! shared-gloc0 10)) |
| 164 |
(list pre (eval 'shared-gloc0 (find-module 'O1)))))) |
| 165 |
|
| 166 |
(define-module O2 |
| 167 |
(import (N :only (reset-result get-result) :prefix N:))) |
| 168 |
(test "import w/only-prefix" '() |
| 169 |
(lambda () |
| 170 |
(eval '(begin (N:reset-result) (N:get-result)) (find-module 'O2)))) |
| 171 |
(test "import w/only-prefix (error)" (test-error) |
| 172 |
(lambda () (eval '(N:push-result 'a) (find-module 'O2)))) |
| 173 |
|
| 174 |
(define-module O3 |
| 175 |
(import (N :prefix N: :only (N:reset-result N:get-result)))) |
| 176 |
(test "import w/prefix-only" '() |
| 177 |
(lambda () |
| 178 |
(eval '(begin (N:reset-result) (N:get-result)) (find-module 'O3)))) |
| 179 |
(test "import w/prefix-only (error)" (test-error) |
| 180 |
(lambda () (eval '(N:push-result 'a) (find-module 'O3)))) |
| 181 |
(test "import w/prefix-only (nonexistent error)" (test-error) |
| 182 |
(lambda () (eval '(define-module O3bis |
| 183 |
(import N :prefix N: :only (reset-result))) |
| 184 |
(current-module)))) |
| 185 |
|
| 186 |
(define-module O4 |
| 187 |
(import (N :except (push-result)))) |
| 188 |
(test "import w/except" '() |
| 189 |
(lambda () (eval '(begin (reset-result) (get-result)) (find-module 'O4)))) |
| 190 |
(test "import w/except (error)" (test-error) |
| 191 |
(lambda () (eval 'push-result (find-module 'O4)))) |
| 192 |
|
| 193 |
(define-module O5 |
| 194 |
(import (N :except (push-result) :prefix N:))) |
| 195 |
(test "import w/except-prefix" '() |
| 196 |
(lambda () |
| 197 |
(eval '(begin (N:reset-result) (N:get-result)) (find-module 'O5)))) |
| 198 |
(test "import w/except-prefix (error)" (test-error) |
| 199 |
(lambda () (eval 'N:push-result (find-module 'O5)))) |
| 200 |
|
| 201 |
(define-module O6 |
| 202 |
(import (N :prefix N: :except (N:push-result)))) |
| 203 |
(test "import w/prefix-except" '() |
| 204 |
(lambda () |
| 205 |
(eval '(begin (N:reset-result) (N:get-result)) (find-module 'O6)))) |
| 206 |
(test "import w/prefix-except (error)" (test-error) |
| 207 |
(lambda () |
| 208 |
(eval 'N:push-result (find-module 'O6)))) |
| 209 |
(test "import w/prefix-except (nonexistent error)" (test-error) |
| 210 |
(lambda () |
| 211 |
(eval '(define-module O6bis |
| 212 |
(import N :prefix N: :except (reset-result))) |
| 213 |
(current-module)))) |
| 214 |
|
| 215 |
(define-module O7 |
| 216 |
(import (N :rename ((reset-result reset) (get-result get))))) |
| 217 |
(test "import w/rename" '(1 2) |
| 218 |
(lambda () |
| 219 |
(eval '(begin (reset) (push-result 1) (push-result 2) (get)) |
| 220 |
(find-module 'O7)))) |
| 221 |
(test "import w/rename (make sure old binding is removed)" (test-error) |
| 222 |
(lambda () |
| 223 |
(eval '(reset-result) (find-module 'O7)))) |
| 224 |
|
| 225 |
(define-module O8 |
| 226 |
;; swapping names |
| 227 |
(import (N :rename ((reset-result get-result) (get-result reset-result))))) |
| 228 |
(test "import w/rename (swap)" '(1 2) |
| 229 |
(lambda () |
| 230 |
(eval '(begin |
| 231 |
(get-result) (push-result 1) (push-result 2) (reset-result)) |
| 232 |
(find-module 'O8)))) |
| 233 |
|
| 234 |
(define-module O9 |
| 235 |
(import (N :rename ((reset-result r) (get-result g)) :prefix n:))) |
| 236 |
(test "import w/rename-prefix" '(1 2) |
| 237 |
(lambda () |
| 238 |
(eval '(begin (n:r) (n:push-result 1) (n:push-result 2) (n:g)) |
| 239 |
(find-module 'O9)))) |
| 240 |
(test "import w/rename-prefix (hiding)" (test-error) |
| 241 |
(lambda () |
| 242 |
(eval 'n:reset-result (find-module 'O9)))) |
| 243 |
(test "import w/rename-prefix (hiding)" (test-error) |
| 244 |
(lambda () |
| 245 |
(eval 'n:get-result (find-module 'O9)))) |
| 246 |
|
| 247 |
(define-module Oa |
| 248 |
(import (N :prefix n: :rename ((n:reset-result r) (n:get-result g))))) |
| 249 |
(test "import w/prefix-rename" '(1 2) |
| 250 |
(lambda () |
| 251 |
(eval '(begin (r) (n:push-result 1) (n:push-result 2) (g)) |
| 252 |
(find-module 'Oa)))) |
| 253 |
(test "import w/prefix-rename (hiding)" (test-error) |
| 254 |
(lambda () (eval 'n:reset-result (find-module 'Oa)))) |
| 255 |
(test "import w/prefix-rename (hiding)" (test-error) |
| 256 |
(lambda () (eval 'n:get-result (find-module 'Oa)))) |
| 257 |
(test "import w/prefix-rename (hiding)" (test-error) |
| 258 |
(lambda () (eval 'reset-result (find-module 'Oa)))) |
| 259 |
(test "import w/prefix-rename (hiding)" (test-error) |
| 260 |
(lambda () (eval 'n:r (find-module 'Oa)))) |
| 261 |
(test "import w/prefix-rename (hiding)" (test-error) |
| 262 |
(lambda () (eval 'n:g (find-module 'Oa)))) |
| 263 |
|
| 264 |
(define-module Ob |
| 265 |
(import (N :prefix n: :rename ((n:push-result p)) |
| 266 |
:prefix nn: :rename ((nn:n:get-result g))))) |
| 267 |
(test "import w/prefix-rename-prefix-rename" '(1 2) |
| 268 |
(lambda () |
| 269 |
(eval '(begin (nn:n:reset-result) (nn:p 1) (nn:p 2) (g)) |
| 270 |
(find-module 'Ob)))) |
| 271 |
|
| 272 |
(define-module Oc |
| 273 |
(export ichi (rename shi yon) go) |
| 274 |
(define ichi 1) |
| 275 |
(define ni 2) ; not exported |
| 276 |
(define shi 4) |
| 277 |
(define go (+ ichi shi)) |
| 278 |
(define kyu (+ shi go)) |
| 279 |
(export (rename kyu ku))) |
| 280 |
(define-module Oc-1 |
| 281 |
(import Oc)) |
| 282 |
|
| 283 |
(test "export-time renaming" '(1 4 5 9 #f #f) |
| 284 |
(lambda () |
| 285 |
(let [(m (find-module 'Oc-1))] |
| 286 |
(list (module-binding-ref m 'ichi #f) |
| 287 |
(module-binding-ref m 'yon #f) |
| 288 |
(module-binding-ref m 'go #f) |
| 289 |
(module-binding-ref m 'ku #f) |
| 290 |
(module-binding-ref m 'shi #f) |
| 291 |
(module-binding-ref m 'kyu #f))))) |
| 292 |
|
| 293 |
(test "export-time renaming and defines?/exports? (0c)" |
| 294 |
'((ichi #t #t) (ni #t #f) (shi #t #f) (yon #f #t) (go #t #t) (kyu #t #f) (ku #f #t)) |
| 295 |
(lambda () |
| 296 |
(let [(m (find-module 'Oc))] |
| 297 |
(map (lambda (n) |
| 298 |
(list n |
| 299 |
(module-binds? m n) |
| 300 |
(module-exports? m n))) |
| 301 |
'(ichi ni shi yon go kyu ku))))) |
| 302 |
(test "export-time renaming and defines?/exports? (0c-1)" |
| 303 |
'((ichi #t #t) (ni #f #f) (shi #f #f) (yon #t #t) (go #t #t) (kyu #f #f) (ku #t #t)) |
| 304 |
(lambda () |
| 305 |
(let [(m (find-module 'Oc-1))] |
| 306 |
(map (lambda (n) |
| 307 |
(list n |
| 308 |
(module-binds? m n) |
| 309 |
(module-exports? m n))) |
| 310 |
'(ichi ni shi yon go kyu ku))))) |
| 311 |
|
| 312 |
(define-module Od |
| 313 |
(import (Oc :rename ((ku kokono)) :prefix mm:))) |
| 314 |
|
| 315 |
(test "export-time renaming plus import renaming" '(#f 9) |
| 316 |
(lambda () |
| 317 |
(let [(m (find-module 'Od))] |
| 318 |
(list (module-binding-ref m 'mm:ku #f) |
| 319 |
(module-binding-ref m 'mm:kokono #f))))) |
| 320 |
|
| 321 |
(define-module Oe |
| 322 |
(export (rename a x) (rename x a)) |
| 323 |
(define a 'A) |
| 324 |
(define x 'X)) |
| 325 |
(define-module Oe-1 |
| 326 |
(import Oe)) |
| 327 |
|
| 328 |
(test "export-time renaming with swapping" '(X A) |
| 329 |
(lambda () |
| 330 |
(let [(m (find-module 'Oe-1))] |
| 331 |
(list (module-binding-ref m 'a #f) |
| 332 |
(module-binding-ref m 'x #f))))) |
| 333 |
|
| 334 |
(define-module Of |
| 335 |
(export foo) |
| 336 |
(define foo 1) |
| 337 |
(define bar 2)) |
| 338 |
(define-module Of-1 |
| 339 |
(import Of) |
| 340 |
(export foo)) |
| 341 |
|
| 342 |
(test "transitive export" 1 |
| 343 |
(lambda () (module-binding-ref (find-module 'Of-1) 'foo #f))) |
| 344 |
|
| 345 |
(define-module Of-2 |
| 346 |
(import Of) |
| 347 |
(export (rename foo foo-alias))) |
| 348 |
(define-module Of-3 |
| 349 |
(import Of-2)) |
| 350 |
|
| 351 |
(test "export-time renaming and transitive export" '(1 #f) |
| 352 |
(lambda () |
| 353 |
(list |
| 354 |
(module-binding-ref 'Of-3 'foo-alias #f) |
| 355 |
(module-binding-ref 'Of-3 'foo #f)))) |
| 356 |
|
| 357 |
(define-module Of-4 |
| 358 |
(import (Of-2 :rename ((foo-alias foo-newname))))) |
| 359 |
|
| 360 |
(test "export-time renaming, transitive export, plus import renaming" |
| 361 |
'(1 #f #f) |
| 362 |
(lambda () |
| 363 |
(list |
| 364 |
(module-binding-ref 'Of-4 'foo-newname #f) |
| 365 |
(module-binding-ref 'Of-4 'foo-alias #f) |
| 366 |
(module-binding-ref 'Of-4 'foo #f)))) |
| 367 |
|
| 368 |
(define-module Of-5 |
| 369 |
(extend Of) |
| 370 |
(export (rename foo foo-alias) |
| 371 |
(rename bar bar-alias))) |
| 372 |
(define-module Of-6 |
| 373 |
(import (Of-5 :except (foo-alias) :rename ((bar-alias bah))))) |
| 374 |
(define-module Of-7 |
| 375 |
(import (Of-5 :except (foo) :rename ((foo-alias who))))) |
| 376 |
|
| 377 |
(test "export-time renaming, wrapper module and import renaming" |
| 378 |
'((#f 1) (#f #f) (1 #f) (2 #f) (#f 2) (#f #f)) |
| 379 |
(lambda () |
| 380 |
(map (lambda (s) |
| 381 |
(list (module-binding-ref 'Of-6 s #f) |
| 382 |
(module-binding-ref 'Of-7 s #f))) |
| 383 |
'(who foo-alias foo bah bar-alias bar)))) |
| 384 |
|
| 385 |
;;------------------------------------------------------------------ |
| 386 |
;; select-module, and restoration in load(). |
| 387 |
|
| 388 |
(test "select-module" '(O O N O) |
| 389 |
(lambda () |
| 390 |
(eval |
| 391 |
'(with-module O |
| 392 |
(define load-data '((select-module O) |
| 393 |
(push-result (module-name (current-module))) |
| 394 |
(select-module N) |
| 395 |
(push-result (module-name (current-module))))) |
| 396 |
(reset-result) |
| 397 |
(push-result (module-name (current-module))) |
| 398 |
(with-output-to-file "tmp.t" |
| 399 |
(lambda () (for-each write load-data))) |
| 400 |
(load "./tmp.t") |
| 401 |
(push-result (module-name (current-module))) |
| 402 |
(sys-unlink "tmp.t") |
| 403 |
(get-result) |
| 404 |
) |
| 405 |
(interaction-environment)))) |
| 406 |
|
| 407 |
(test "select-module" 'user (lambda () (module-name (current-module)))) |
| 408 |
|
| 409 |
(test "select-module (error)" (test-error) |
| 410 |
(lambda () (eval '(select-moulde MM) (interaction-environment)))) |
| 411 |
|
| 412 |
;;------------------------------------------------------------------ |
| 413 |
;; module inheritance |
| 414 |
|
| 415 |
(define-module P |
| 416 |
(export a b) |
| 417 |
(define a 'alpha) |
| 418 |
(define b 'beta)) |
| 419 |
(define-module Q |
| 420 |
(export a b d) |
| 421 |
(define a 'ei) |
| 422 |
(define b 'bee) |
| 423 |
(define d 'dee)) |
| 424 |
(define-module R |
| 425 |
(export c) |
| 426 |
(extend P) |
| 427 |
(define c 'gamma)) |
| 428 |
(define-module S |
| 429 |
(export c) |
| 430 |
(extend Q P) |
| 431 |
(define c 'delta)) |
| 432 |
(define-module T |
| 433 |
(export c) |
| 434 |
(extend Q) |
| 435 |
(define c 'delta)) |
| 436 |
(define-module U |
| 437 |
(extend R T) |
| 438 |
) |
| 439 |
(define-module V |
| 440 |
(import U) |
| 441 |
) |
| 442 |
|
| 443 |
(test "module inheritance" 'alpha (lambda () (with-module R a))) |
| 444 |
(test "module inheritance" 'ei (lambda () (with-module S a))) |
| 445 |
(test "module inheritance" '(gamma beta) |
| 446 |
(lambda () |
| 447 |
(with-module U (list c b)))) |
| 448 |
(test "module inheritance" '(alpha beta gamma dee) |
| 449 |
(lambda () |
| 450 |
(with-module V (list a b c d)))) |
| 451 |
|
| 452 |
(test "moduel inheritance (error)" (test-error) |
| 453 |
(lambda () |
| 454 |
(eval '(with-module V (extend Q MM)) (interaction-environment)))) |
| 455 |
|
| 456 |
(test "module-binding-ref" 'gamma |
| 457 |
(lambda () |
| 458 |
(module-binding-ref 'U 'c))) |
| 459 |
|
| 460 |
(test "module-binding-ref" (test-error) |
| 461 |
(lambda () |
| 462 |
(module-binding-ref 'U 'e))) |
| 463 |
|
| 464 |
(test "module-binding-ref" 'huh? |
| 465 |
(lambda () |
| 466 |
(module-binding-ref 'U 'e 'huh?))) |
| 467 |
|
| 468 |
(test "module-binding-ref" 'huh? |
| 469 |
(lambda () |
| 470 |
(module-binding-ref 'U 'c 'huh? #t))) |
| 471 |
|
| 472 |
;;------------------------------------------------------------------ |
| 473 |
;; mpl search bug in 0.9.1 reported by Ryo Akagi |
| 474 |
;; http://sourceforge.jp/projects/gauche/lists/archive/devel-jp/2010-December/001909.html |
| 475 |
|
| 476 |
(define-module mplbug-x (export x) (define x 0)) |
| 477 |
(define-module mplbug-a (export a) (define a 1)) |
| 478 |
(define-module mplbug-b (export b) (define b 2)) |
| 479 |
(define-module mplbug-A (extend mplbug-x mplbug-a)) |
| 480 |
(define-module mplbug-B (extend mplbug-x mplbug-b)) |
| 481 |
|
| 482 |
(define-module mplbug-user1 (import mplbug-A) (import mplbug-B)) |
| 483 |
(define-module mplbug-user2 (import mplbug-B) (import mplbug-A)) |
| 484 |
|
| 485 |
(define (mplbug-test mod var) |
| 486 |
(test* #"mpl search (~mod,~var)" #t |
| 487 |
(module-binds? (find-module mod) var))) |
| 488 |
|
| 489 |
(mplbug-test 'mplbug-user1 'a) |
| 490 |
(mplbug-test 'mplbug-user1 'b) |
| 491 |
(mplbug-test 'mplbug-user1 'x) |
| 492 |
(mplbug-test 'mplbug-user2 'a) |
| 493 |
(mplbug-test 'mplbug-user2 'b) |
| 494 |
(mplbug-test 'mplbug-user2 'x) |
| 495 |
|
| 496 |
;; module inheritance with prefixing |
| 497 |
(define-module mpl-prefix-a (export-all) (define a 0)) |
| 498 |
(define-module mpl-prefix-b (export b) (define b 1) (extend mpl-prefix-a)) |
| 499 |
(define-module mpl-prefix-c (export c) (define c 2) (extend mpl-prefix-a)) |
| 500 |
|
| 501 |
(define-module mpl-prefix-1 |
| 502 |
(import (mpl-prefix-b :prefix X:))) |
| 503 |
(define-module mpl-prefix-2 |
| 504 |
(import (mpl-prefix-b :prefix X:)) |
| 505 |
(import mpl-prefix-a)) |
| 506 |
(define-module mpl-prefix-3 |
| 507 |
(import mpl-prefix-a) |
| 508 |
(import (mpl-prefix-b :prefix X:))) |
| 509 |
(define-module mpl-prefix-4 |
| 510 |
(import (mpl-prefix-b :prefix X:)) |
| 511 |
(import (mpl-prefix-a :prefix Y:))) |
| 512 |
(define-module mpl-prefix-5 |
| 513 |
(import (mpl-prefix-a :prefix Y:)) |
| 514 |
(import mpl-prefix-b)) |
| 515 |
(define-module mpl-prefix-6 |
| 516 |
(import (mpl-prefix-b :prefix X:)) |
| 517 |
(import (mpl-prefix-c :prefix Y:))) |
| 518 |
(define-module mpl-prefix-7 |
| 519 |
(import (mpl-prefix-b :prefix X:)) |
| 520 |
(import (mpl-prefix-c :prefix Y:)) |
| 521 |
(import mpl-prefix-a)) |
| 522 |
(define-module mpl-prefix-8 |
| 523 |
(import (mpl-prefix-b :prefix X:)) |
| 524 |
(import (mpl-prefix-b :prefix Y:)) |
| 525 |
(import mpl-prefix-b)) |
| 526 |
|
| 527 |
|
| 528 |
(let () |
| 529 |
(define (check mod . syms) |
| 530 |
(map (lambda (sym) |
| 531 |
(list sym (module-binding-ref mod sym 'undef))) |
| 532 |
syms)) |
| 533 |
(test* "mpl prefix simple" '((a undef) (b undef) (X:a 0) (X:b 1)) |
| 534 |
(check 'mpl-prefix-1 'a 'b 'X:a 'X:b)) |
| 535 |
|
| 536 |
(test* "mpl prefix simple dupe (w/ prefix first)" |
| 537 |
'((a 0) (b undef) (X:a 0) (X:b 1)) |
| 538 |
(check 'mpl-prefix-2 'a 'b 'X:a 'X:b)) |
| 539 |
|
| 540 |
(test* "mpl prefix simple dupe (w/o prefix first)" |
| 541 |
'((a 0) (b undef) (X:a 0) (X:b 1)) |
| 542 |
(check 'mpl-prefix-3 'a 'b 'X:a 'X:b)) |
| 543 |
|
| 544 |
(test* "mpl prefix simple dupe (both prefix)" |
| 545 |
'((a undef) (b undef) (X:a 0) (X:b 1) (Y:a 0) (Y:b undef)) |
| 546 |
(check 'mpl-prefix-4 'a 'b 'X:a 'X:b 'Y:a 'Y:b)) |
| 547 |
|
| 548 |
(test* "mpl prefix simple dupe (parent prefix)" |
| 549 |
'((a 0) (b 1) (Y:a 0) (Y:b undef)) |
| 550 |
(check 'mpl-prefix-5 'a 'b 'Y:a 'Y:b)) |
| 551 |
|
| 552 |
(test* "mpl prefix simple merge (different prefix)" |
| 553 |
'((a undef) (b undef) (c undef) |
| 554 |
(X:a 0) (X:b 1) (X:c undef) |
| 555 |
(Y:a 0) (Y:b undef) (Y:c 2)) |
| 556 |
(check 'mpl-prefix-6 'a 'b 'c 'X:a 'X:b 'X:c 'Y:a 'Y:b 'Y:c)) |
| 557 |
|
| 558 |
(test* "mpl prefix simple merge/dupe (different prefix)" |
| 559 |
'((a 0) (b undef) (c undef) |
| 560 |
(X:a 0) (X:b 1) (X:c undef) |
| 561 |
(Y:a 0) (Y:b undef) (Y:c 2)) |
| 562 |
(check 'mpl-prefix-7 'a 'b 'c 'X:a 'X:b 'X:c 'Y:a 'Y:b 'Y:c)) |
| 563 |
|
| 564 |
(test* "mpl prefix simple dupe (different prefix)" |
| 565 |
'((a 0) (b 1) |
| 566 |
(X:a 0) (X:b 1) |
| 567 |
(Y:a 0) (Y:b 1)) |
| 568 |
(check 'mpl-prefix-8 'a 'b 'X:a 'X:b 'Y:a 'Y:b)) |
| 569 |
) |
| 570 |
|
| 571 |
;;------------------------------------------------------------------ |
| 572 |
;; transitive export issue |
| 573 |
;; https://github.com/shirok/Gauche/issues/472 |
| 574 |
|
| 575 |
(define-module tre1-A |
| 576 |
(export a) |
| 577 |
(define a 1)) |
| 578 |
(define-module tre1-B |
| 579 |
(import tre1-A) |
| 580 |
(export a)) |
| 581 |
(define-module tre1-C |
| 582 |
(extend tre1-B)) |
| 583 |
|
| 584 |
(test* "transitive export with only" 1 |
| 585 |
(let1 m (make-module #f) |
| 586 |
(eval '(import (tre1-C :only (a))) m) |
| 587 |
(eval 'a m))) |
| 588 |
|
| 589 |
(define-module tre2-A |
| 590 |
(export a) |
| 591 |
(define a 1)) |
| 592 |
(define-module tre2-B |
| 593 |
(import tre2-A) |
| 594 |
(export (rename a aaa))) |
| 595 |
(define-module tre2-C |
| 596 |
(extend tre2-B)) |
| 597 |
|
| 598 |
(test* "transitive export with only" 1 |
| 599 |
(let1 m (make-module #f) |
| 600 |
(eval '(import (tre2-C :only (aaa))) m) |
| 601 |
(eval 'aaa m))) |
| 602 |
|
| 603 |
(define-module tre3-A |
| 604 |
(export a) |
| 605 |
(define a 1)) |
| 606 |
(define-module tre3-B |
| 607 |
(import tre3-A) |
| 608 |
(export a)) |
| 609 |
(define-module tre3-C |
| 610 |
(extend tre3-B)) |
| 611 |
|
| 612 |
(test* "transitive export with only" 1 |
| 613 |
(let1 m (make-module #f) |
| 614 |
(eval '(import (tre3-C :rename ((a aaa)))) m) |
| 615 |
(eval 'aaa m))) |
| 616 |
|
| 617 |
;;------------------------------------------------------------------ |
| 618 |
;; creates modules on-the-fly |
| 619 |
|
| 620 |
(test "make-module" #t |
| 621 |
(lambda () |
| 622 |
(make-module 'foo) |
| 623 |
(module? (find-module 'foo)))) |
| 624 |
|
| 625 |
(test "make-module (duplicate name)" (test-error) |
| 626 |
(lambda () |
| 627 |
(make-module 'foo))) |
| 628 |
|
| 629 |
(test "make-module (duplicate name)" (test-error) |
| 630 |
(lambda () |
| 631 |
(make-module 'foo :if-exists :error))) |
| 632 |
|
| 633 |
(test "make-module (duplicate name)" #f |
| 634 |
(lambda () |
| 635 |
(make-module 'foo :if-exists #f))) |
| 636 |
|
| 637 |
(test "anynomous module" #t |
| 638 |
(lambda () |
| 639 |
(let ((m0 (make-module #f)) |
| 640 |
(m1 (make-module #f))) |
| 641 |
(and (module? m0) (module? m1) (not (eq? m0 m1)))))) |
| 642 |
|
| 643 |
(test "anonymous module" 13 |
| 644 |
(lambda () |
| 645 |
(let ((m0 (make-module #f))) |
| 646 |
(eval '(define x 13) m0) |
| 647 |
(eval 'x m0)))) |
| 648 |
|
| 649 |
(test "anonymous module" (test-error) |
| 650 |
(lambda () |
| 651 |
(let ((m0 (make-module #f)) |
| 652 |
(m1 (make-module #f))) |
| 653 |
(eval '(define x 13) m0) |
| 654 |
(eval 'x m1)))) |
| 655 |
|
| 656 |
;;------------------------------------------------------------------- |
| 657 |
;; null and scheme module bindings (R5RS) |
| 658 |
|
| 659 |
(test "null module bindings" '() |
| 660 |
(lambda () |
| 661 |
(let loop ((names '(and begin cond define define-syntax delay do |
| 662 |
if lambda let let* let-syntax |
| 663 |
letrec letrec-syntax or quasiquote quote |
| 664 |
set! syntax-rules else =>)) |
| 665 |
(missing '())) |
| 666 |
(cond [(null? names) missing] |
| 667 |
[(module-binds? 'null (car names)) (loop (cdr names) missing)] |
| 668 |
[else (loop (cdr names) (cons (car names) missing))])))) |
| 669 |
|
| 670 |
(test "scheme module bindings" '() |
| 671 |
(lambda () |
| 672 |
(let loop ((names '(* + - / < <= = >= > |
| 673 |
abs acos angle append apply asin assoc assq |
| 674 |
assv atan boolean? call-with-current-continuation |
| 675 |
call-with-input-file call-with-output-file |
| 676 |
call-with-values car cdr caar cadr cdar cddr |
| 677 |
caaar caadr cadar caddr cdaar cdadr cddar cdddr |
| 678 |
caaaar caaadr caadar caaddr cadaar cadadr |
| 679 |
caddar cadddr cdaaar cdaadr cdadar cdaddr |
| 680 |
cddaar cddadr cdddar cddddr ceiling char->integer |
| 681 |
char-alphabetic? char-ci<=? char-ci<? char-ci=? |
| 682 |
char-ci>=? char-ci>? char-downcase char-lower-case? |
| 683 |
char-numeric? char-ready? char-upcase |
| 684 |
char-upper-case? char-whitespace? |
| 685 |
char<? char<=? char=? char>? char>=? char? |
| 686 |
close-input-port close-output-port complex? |
| 687 |
cons cos current-input-port current-output-port |
| 688 |
denominator display dynamic-wind eof-object? |
| 689 |
eq? equal? eqv? eval even? exact->inexact |
| 690 |
exact? exp expt floor for-each force gcd |
| 691 |
imag-part inexact->exact inexact? input-port? |
| 692 |
integer->char integer? interaction-environment |
| 693 |
lcm length list list->string list->vector |
| 694 |
list-ref list-tail list? load log magnitude |
| 695 |
make-polar make-rectangular make-string |
| 696 |
make-vector map max member memq memv min modulo |
| 697 |
negative? newline not null-environment null? |
| 698 |
number->string number? numerator odd? |
| 699 |
open-input-file open-output-file or output-port? |
| 700 |
pair? peek-char port? positive? procedure? |
| 701 |
quotient rational? rationalize read read-char |
| 702 |
real-part real? remainder reverse round |
| 703 |
scheme-report-environment set-car! set-cdr! |
| 704 |
sin sqrt string string->list string->number |
| 705 |
string->symbol string-append string-ci<=? |
| 706 |
string-ci<? string-ci=? string-ci>? string-ci>=? |
| 707 |
string-copy string-fill! string-length string-ref |
| 708 |
string-set! string<=? string<? string=? string>? |
| 709 |
string>=? substring symbol->string symbol? |
| 710 |
tan truncate vector vector->list vector-fill! |
| 711 |
vector-length vector-ref vector-set! vector? |
| 712 |
with-input-from-file with-output-to-file |
| 713 |
write write-char zero?)) |
| 714 |
(missing '())) |
| 715 |
(cond [(null? names) missing] |
| 716 |
[(module-binds? 'scheme (car names)) (loop (cdr names) missing)] |
| 717 |
[else (loop (cdr names) (cons (car names) missing))])))) |
| 718 |
|
| 719 |
;;------------------------------------------------------------------- |
| 720 |
;; Module exports |
| 721 |
|
| 722 |
(define-module exports-test-A |
| 723 |
(export a b c) |
| 724 |
(define a 0) |
| 725 |
(define b 1) |
| 726 |
(define c 2)) |
| 727 |
|
| 728 |
(define-module exports-test-B |
| 729 |
(extend exports-test-A) |
| 730 |
(define a 3) |
| 731 |
(define d 4) |
| 732 |
(export d)) |
| 733 |
|
| 734 |
(define-module exports-test-C |
| 735 |
(extend exports-test-A) |
| 736 |
(export-all) |
| 737 |
(define e 5) |
| 738 |
(define f 6)) |
| 739 |
|
| 740 |
(define-module exports-test-D |
| 741 |
(extend exports-test-B exports-test-C) |
| 742 |
(export g) |
| 743 |
(define g 7)) |
| 744 |
|
| 745 |
(test "module-exports" |
| 746 |
'(a b c d e f g) |
| 747 |
(lambda () |
| 748 |
;; NB: We haven't tested 'sort', so we need to roll our own |
| 749 |
(define (sym<? a b) |
| 750 |
(string<? (symbol->string a) (symbol->string b))) |
| 751 |
(define (sort-sym xs) |
| 752 |
(if (or (null? xs) (null? (cdr xs))) |
| 753 |
xs |
| 754 |
(let loop ((pivot (car xs)) |
| 755 |
(xs (cdr xs)) |
| 756 |
(lt '()) |
| 757 |
(ge '())) |
| 758 |
(if (null? xs) |
| 759 |
(append (sort-sym lt) (list pivot) (sort-sym ge)) |
| 760 |
(if (sym<? (car xs) pivot) |
| 761 |
(loop pivot (cdr xs) (cons (car xs) lt) ge) |
| 762 |
(loop pivot (cdr xs) lt (cons (car xs) ge))))))) |
| 763 |
(sort-sym (module-exports (find-module 'exports-test-D))))) |
| 764 |
|
| 765 |
;;------------------------------------------------------------------- |
| 766 |
;; Macro and builtin inliner |
| 767 |
;; https://twitter.com/tk_riple/status/647865265154326528 |
| 768 |
|
| 769 |
(define-module builtin-inliner-bug-A |
| 770 |
(export ash-1) |
| 771 |
(define-inline ash-1 ash)) |
| 772 |
(define-module builtin-inliner-bug-B |
| 773 |
(import builtin-inliner-bug-A) |
| 774 |
(export ash-2) |
| 775 |
(define-syntax ash-2 |
| 776 |
(syntax-rules () |
| 777 |
[(_ a b) (ash-1 (+ a 1) (+ b 1))]))) |
| 778 |
(define-module builtin-inliner-bug-C |
| 779 |
(import builtin-inliner-bug-B) |
| 780 |
(define ash +) |
| 781 |
(define (ash-3 x y) (ash-2 x y))) |
| 782 |
|
| 783 |
(test* "builtin inliner bug" 64 |
| 784 |
((with-module builtin-inliner-bug-C ash-3) 7 2)) |
| 785 |
|
| 786 |
;;------------------------------------------------------------------- |
| 787 |
;; Import qualifier & binding check |
| 788 |
;; https://github.com/shirok/Gauche/issues/549 |
| 789 |
|
| 790 |
(test* "import qualifier binding check" 1 |
| 791 |
(eval '(begin |
| 792 |
(define-module imp-A |
| 793 |
(export x) |
| 794 |
(define (x) 1)) |
| 795 |
(define-module imp-B |
| 796 |
(import (imp-A :only (x))) |
| 797 |
(x))) |
| 798 |
(current-module))) |
| 799 |
|
| 800 |
(test-end) |