Debian Codegraph @

test/object.scm

raw
1 ;;
2 ;; Test object system
3 ;;
4
5 (use gauche.test)
6
7 (test-start "object system")
8
9 ;;----------------------------------------------------------------
10 (test-section "class definition")
11
12 (define-class <x> () (a b c))
13 (test* "define-class <x>" '<x> (class-name <x>))
14 (test* "define-class <x>" 3 (slot-ref <x> 'num-instance-slots))
15 (test* "define-class <x>" <class> (class-of <x>))
16 (test* "define-class <x>" '(<x> <object> <top>)
17 (map class-name (class-precedence-list <x>)))
18
19 (define-class <y> (<x>) (c d e))
20 (test* "define-class <y>" 5 (slot-ref <y> 'num-instance-slots))
21 (test* "define-class <y>" <class> (class-of <y>))
22 (test* "define-class <y>" '(<y> <x> <object> <top>)
23 (map class-name (class-precedence-list <y>)))
24
25 (define-class <z> (<object>) ())
26 (test* "define-class <z>" 0 (slot-ref <z> 'num-instance-slots))
27 (test* "define-class <z>" <class> (class-of <z>))
28 (test* "define-class <z>" '(<z> <object> <top>)
29 (map class-name (class-precedence-list <z>)))
30
31 (define-class <w> (<z> <y>) (e f))
32 (test* "define-class <w>" 6 (slot-ref <w> 'num-instance-slots))
33 (test* "define-class <w>" <class> (class-of <w>))
34 (test* "define-class <w>" '(<w> <z> <y> <x> <object> <top>)
35 (map class-name (class-precedence-list <w>)))
36
37 (define-class <w2> (<y> <z>) (e f))
38 (test* "define-class <w2>" '(<w2> <y> <x> <z> <object> <top>)
39 (map class-name (class-precedence-list <w2>)))
40
41 ;;----------------------------------------------------------------
42 (test-section "instancing")
43
44 (define x1 (make <x>))
45 (define x2 (make <x>))
46
47 (test* "make <x>" <x> (class-of x1))
48 (test* "make <x>" <x> (class-of x2))
49
50 (slot-set! x1 'a 4)
51 (slot-set! x1 'b 5)
52 (slot-set! x1 'c 6)
53 (slot-set! x2 'a 7)
54 (slot-set! x2 'b 8)
55 (slot-set! x2 'c 9)
56
57 (test* "slot-ref" '(4 5 6) (map (lambda (slot) (slot-ref x1 slot)) '(a b c)))
58 (test* "slot-ref" '(7 8 9) (map (lambda (slot) (slot-ref x2 slot)) '(a b c)))
59
60 (test* "slot-ref-using-class" '(4 5 6)
61 (map (lambda (slot) (slot-ref-using-class <x> x1 slot)) '(a b c)))
62 (test* "slot-ref-using-class" (test-error)
63 (slot-ref-using-class <y> x1 'a))
64
65 (test* "slot-ref-using-accessor" '(7 8 9)
66 (map (lambda (slot)
67 (let ((sa (class-slot-accessor <x> slot)))
68 (and sa (slot-ref-using-accessor x2 sa))))
69 '(a b c)))
70 (test* "slot-ref-using-accessor" (test-error)
71 (let ((sa (class-slot-accessor <y> slot)))
72 (and sa (slot-ref-using-accessor x2 sa))))
73
74 (test* "slot-set-using-class!" '(-4 -5 -6)
75 (map (lambda (slot)
76 (slot-set-using-class! <x> x1 slot
77 (- (slot-ref x1 slot)))
78 (slot-ref x1 slot))
79 '(a b c)))
80 (test* "slot-set-using-class!" (test-error)
81 (slot-set-using-class! <y> x1 'a 3))
82
83 (test* "slot-set-using-accessor!" '(-7 -8 -9)
84 (map (lambda (slot)
85 (let ((sa (class-slot-accessor <x> slot)))
86 (and sa
87 (slot-set-using-accessor! x2 sa (- (slot-ref x2 slot)))))
88 (slot-ref x2 slot))
89 '(a b c)))
90 (test* "slot-ref-using-accessor!" (test-error)
91 (let ((sa (class-slot-accessor <y> slot)))
92 (and sa (slot-set-using-accessor! x2 sa -1))))
93
94 (let ((x3 (make <x>)))
95 (test* "slot-pop! (unbound error)" (test-error)
96 (slot-pop! x3 'a))
97 (test* "slot-pop! (unbound but fallback)" 'ok
98 (slot-pop! x3 'a 'ok))
99 (test* "slot-push!" '(1)
100 (begin (slot-set! x3 'a '())
101 (slot-push! x3 'a 1)
102 (slot-ref x3 'a)))
103 (test* "slot-push!" '(2 1)
104 (begin (slot-push! x3 'a 2)
105 (slot-ref x3 'a)))
106 (test* "slot-pop!" 2 (slot-pop! x3 'a 'empty))
107 (test* "slot-pop!" 1 (slot-pop! x3 'a 'empty))
108 (test* "slot-pop!" 'empty (slot-pop! x3 'a 'empty))
109 (test* "slot-push!" '(1 . z)
110 (begin (slot-set! x3 'a 'z)
111 (slot-push! x3 'a 1)
112 (slot-ref x3 'a)))
113 (test* "slot-pop!" 1 (slot-pop! x3 'a))
114 (test* "slot-pop!" (test-error) (slot-pop! x3 'a))
115 (test* "slot-pop!" 'empty (slot-pop! x3 'a 'empty))
116 )
117
118 ;;----------------------------------------------------------------
119 (test-section "slot initialization")
120
121 (define-class <r> ()
122 ((a :init-keyword :a :initform 4)
123 (b :init-keyword :b :init-value 5)))
124
125 (define r1 (make <r>))
126 (define r2 (make <r> :a 9))
127 (define r3 (make <r> :b 100 :a 20))
128
129 (define-method slot-values ((obj <r>))
130 (map (^s (slot-ref obj s)) '(a b)))
131
132 (test* "make <r>" '(4 5) (slot-values r1))
133 (test* "make <r> :a" '(9 5) (slot-values r2))
134 (test* "make <r> :a :b" '(20 100) (slot-values r3))
135
136 ;;----------------------------------------------------------------
137 (test-section "slot allocations")
138
139 (define-class <s> ()
140 ((i :allocation :instance :init-keyword :i :init-value #\i)
141 (c :allocation :class :init-keyword :c :init-value #\c)
142 (s :allocation :each-subclass :init-keyword :s :init-value #\s)
143 (v :allocation :virtual :init-keyword :v
144 :slot-ref (^o (cons (slot-ref o 'i) (slot-ref o 'c)))
145 :slot-set! (lambda (o v)
146 (slot-set! o 'i (car v))
147 (slot-set! o 'c (cdr v))))
148 ))
149
150 (define-method slot-values ((obj <s>))
151 (map (^s (slot-ref obj s)) '(i c s v)))
152
153 (define s1 (make <s>))
154 (define s2 (make <s>))
155
156 (test* "make <s>" '(#\i #\c #\s (#\i . #\c)) (slot-values s1))
157 (test* "slot-set! :instance"
158 '((#\I #\c #\s (#\I . #\c)) (#\i #\c #\s (#\i . #\c)))
159 (begin
160 (slot-set! s1 'i #\I)
161 (list (slot-values s1) (slot-values s2))))
162 (test* "slot-set! :class"
163 '((#\I #\C #\s (#\I . #\C)) (#\i #\C #\s (#\i . #\C)))
164 (begin
165 (slot-set! s1 'c #\C)
166 (list (slot-values s1) (slot-values s2))))
167 (test* "slot-set! :each-subclass"
168 '((#\I #\C #\S (#\I . #\C)) (#\i #\C #\S (#\i . #\C)))
169 (begin
170 (slot-set! s1 's #\S)
171 (list (slot-values s1) (slot-values s2))))
172 (test* "slot-set! :virtual"
173 '((i c #\S (i . c)) (#\i c #\S (#\i . c)))
174 (begin
175 (slot-set! s1 'v '(i . c))
176 (list (slot-values s1) (slot-values s2))))
177
178 (define-class <ss> (<s>)
179 ())
180
181 (define s3 (make <ss> :i "i" :c "c" :s "s"))
182
183 (test* "make <ss>"
184 '(("i" "c" "s" ("i" . "c")) (i "c" #\S (i . "c")))
185 (list (slot-values s3) (slot-values s1)))
186 (test* "slot-set! :class"
187 '(("i" "C" "s" ("i" . "C")) (i "C" #\S (i . "C")))
188 (begin
189 (slot-set! s3 'c "C")
190 (list (slot-values s3) (slot-values s1))))
191 (test* "slot-set! :each-subclass"
192 '(("i" "C" "s" ("i" . "C")) (i "C" "S" (i . "C")))
193 (begin
194 (slot-set! s1 's "S")
195 (list (slot-values s3) (slot-values s1))))
196 (test* "slot-set! :each-subclass"
197 '(("i" "C" 5 ("i" . "C")) (i "C" "S" (i . "C")))
198 (begin
199 (slot-set! s3 's 5)
200 (list (slot-values s3) (slot-values s1))))
201
202 (define s4 (make <ss> :v '(1 . 0)))
203
204 (test* "make <ss> :v"
205 '((1 0 5 (1 . 0)) ("i" 0 5 ("i" . 0)))
206 (list (slot-values s4) (slot-values s3)))
207
208 (test* "class-slot-ref"
209 '(0 "S" 0 5)
210 (list (class-slot-ref <s> 'c) (class-slot-ref <s> 's)
211 (class-slot-ref <ss> 'c) (class-slot-ref <ss> 's)))
212 (test* "class-slot-set!"
213 '(100 99 100 5)
214 (begin
215 (class-slot-set! <s> 'c 100)
216 (class-slot-set! <s> 's 99)
217 (list (class-slot-ref <s> 'c) (class-slot-ref <s> 's)
218 (class-slot-ref <ss> 'c) (class-slot-ref <ss> 's))))
219 (test* "class-slot-set!"
220 '(101 99 101 55)
221 (begin
222 (class-slot-set! <ss> 'c 101)
223 (class-slot-set! <ss> 's 55)
224 (list (class-slot-ref <s> 'c) (class-slot-ref <s> 's)
225 (class-slot-ref <ss> 'c) (class-slot-ref <ss> 's))))
226
227 (define-class <sss> ()
228 ((v :allocation :virtual
229 :slot-ref (^o (slot-ref o 'vv))
230 :slot-set! (lambda (o v) (slot-set! o 'vv v))
231 :slot-bound? (^o (slot-bound? o 'vv)))
232 vv))
233
234 (define s5 (make <sss>))
235
236 (test* "slot-bound? protocol" #f
237 (slot-bound? s5 'v))
238
239 (test* "slot-bound? protocol" '(#t 8)
240 (begin (slot-set! s5 'v 8)
241 (list (slot-bound? s5 'v)
242 (slot-ref s5 'v))))
243
244 ;; Direct instance-slot access is only for under-the-hood work, and
245 ;; shouldn't be casually used. But that doesn't diminish the importance
246 ;; of tests!
247 (define-class <ssss> () ((a :init-keyword :a) b))
248
249 (let ((z (make <ssss> :a 1)))
250 (test* "instance-slot-ref a" 1 (instance-slot-ref z 0))
251 (test* "instance-slot-ref b" (test-error <error> #/unbound/)
252 (instance-slot-ref z 1))
253 (test* "instance-slot-ref b fallback" 2
254 (instance-slot-ref z 1 2))
255 (test* "instance-slot-set!" 3
256 (begin (instance-slot-set! z 1 3)
257 (instance-slot-ref z 1)))
258 (test* "instance-slot-ref oob" (test-error <error> #/out of bound/)
259 (instance-slot-ref z 2))
260 (test* "instance-slot-ref oob" (test-error <error> #/out of bound/)
261 (instance-slot-ref z -1))
262 (test* "instance-slot-set! oob" (test-error <error> #/out of bound/)
263 (instance-slot-set! z 2 0))
264 )
265
266 ;;----------------------------------------------------------------
267 (test-section "next method")
268
269 (define (nm obj) 'fallback)
270
271 (define-method nm ((obj <x>)) (list 'x-in (next-method) 'x-out))
272 (define-method nm ((obj <y>)) (list 'y-in (next-method) 'y-out))
273 (define-method nm ((obj <z>)) (list 'z-in (next-method) 'z-out))
274 (define-method nm ((obj <w>)) (list 'w-in (next-method) 'w-out))
275 (define-method nm ((obj <w2>)) (list 'w2-in (next-method) 'w2-out))
276
277 (test* "next method"
278 '(y-in (x-in fallback x-out) y-out)
279 (nm (make <y>)))
280 (test* "next-method"
281 '(w-in (z-in (y-in (x-in fallback x-out) y-out) z-out) w-out)
282 (nm (make <w>)))
283 (test* "next-method"
284 '(w2-in (y-in (x-in (z-in fallback z-out) x-out) y-out) w2-out)
285 (nm (make <w2>)))
286
287 (define-method nm (obj . a)
288 (if (null? a) (list 't*-in (next-method) 't*-out) 't*))
289 (define-method nm ((obj <y>) a) (list 'y1-in (next-method) 'y1-out))
290 (define-method nm ((obj <y>) . a) (list 'y*-in (next-method) 'y*-out))
291
292 (test* "next-method"
293 '(y1-in (y*-in t* y*-out) y1-out)
294 (nm (make <y>) 3))
295 (test* "next-method"
296 '(y-in (y*-in (x-in (t*-in fallback t*-out) x-out) y*-out) y-out)
297 (nm (make <y>)))
298
299 ;; check if method-leaf is properly set
300 (define-method nm-1 (obj) #f)
301 (define-method nm-2 (obj) (next-method))
302
303 (test* "method-leaf" '(#t #f)
304 (list (method-leaf? (car (slot-ref nm-1 'methods)))
305 (method-leaf? (car (slot-ref nm-2 'methods)))))
306
307 ;;----------------------------------------------------------------
308 (test-section "immutable slots")
309
310 (define-class <immutable-slot-class> ()
311 ((a :init-keyword :a :immutable #t)
312 (b :init-thunk (^[] *immutable-b*) :immutable #t)))
313 (define *immutable-b* 5)
314
315 (test* "immutable, initialization" 3
316 (let1 v (make <immutable-slot-class> :a 3)
317 (slot-ref v 'a)))
318
319 (test* "immutable, reject" (test-error)
320 (let1 v (make <immutable-slot-class> :a 3)
321 (slot-set! v 'a 5)
322 (slot-ref v 'a)))
323
324 (test* "immutable, delayed initialization" 3
325 (let1 v (make <immutable-slot-class>)
326 (slot-set! v 'a 3)
327 (slot-ref v 'a)))
328
329 (test* "immutable, initialization by a thunk" 5
330 (let1 v (make <immutable-slot-class>)
331 (slot-ref v 'b)))
332
333 ;;----------------------------------------------------------------
334 (test-section "method sorting")
335
336 ;; Corner cases of method sorting.
337
338 ;; 0.8.6 and before had a bug in method-more-specific? when both methods
339 ;; had optional arg.
340 (define-method ms-1 ((x <string>) . rest) 1)
341 (define-method ms-1 rest 0)
342 (define-method ms-1 ((x <string>) (y <string>) . rest) 2)
343
344 (test* "method sorting" 2 (ms-1 "a" "a"))
345 (test* "method sorting" 1 (ms-1 "a"))
346
347
348 ;;----------------------------------------------------------------
349 (test-section "setter method definition")
350
351 (define-method s-get-i ((self <s>)) (slot-ref self 'i))
352 (define-method (setter s-get-i) ((self <s>) v) (slot-set! self 'i v))
353 (define-method (setter s-get-i) ((self <ss>) v) (slot-set! self 'i (cons v v)))
354
355 (test* "setter of s-get-i(<s>)" '("i" "j")
356 (let* ((s (make <s> :i "i"))
357 (i (s-get-i s))
358 (j (begin (set! (s-get-i s) "j") (s-get-i s))))
359 (list i j)))
360 (test* "setter of s-get-i(<ss>)" '("i" ("j" . "j"))
361 (let* ((s (make <ss> :i "i"))
362 (i (s-get-i s))
363 (j (begin (set! (s-get-i s) "j") (s-get-i s))))
364 (list i j)))
365
366 ;;----------------------------------------------------------------
367 (test-section "method optional arguments")
368
369 (define-method optarg ((a <x>)) 'x)
370 (define-method optarg ((a <y>) :optional (x 0) (y 1) :key (z 2) :rest r)
371 (list x y z r))
372
373 (test* "method optarg (no optarg)" 'x (optarg (make <x>)))
374 (test* "method optarg (optarg)" '(0 1 2 ()) (optarg (make <y>)))
375 (test* "method optarg (optarg)" '(a b 2 ()) (optarg (make <y>) 'a 'b))
376 (test* "method optarg (optarg)" '(a b 4 (:z 4)) (optarg (make <y>) 'a 'b :z 4))
377
378 ;;----------------------------------------------------------------
379 (test-section "accelerated dispatch")
380
381 (define-generic acc-dis-1)
382 (define-generic acc-dis-2)
383 ((with-module gauche.object generic-build-dispatcher!) acc-dis-2 0)
384
385 (define-method acc-dis-1 ((a <top>)) 'top)
386 (define-method acc-dis-2 ((a <top>)) 'top)
387
388 (define *acc-dis-count* 25)
389
390 (define-macro (gen-acc-dis-classes&methods)
391 (define (gen-1 n)
392 (let1 cl (symbol-append '<acc-dis- n '>)
393 `(begin
394 (define-class ,cl () ())
395 (define-method acc-dis-1 ((a ,cl)) `(,',n 0))
396 (define-method acc-dis-1 ((a ,cl) x) `(,',n 1))
397 (define-method acc-dis-1 ((a ,cl) x y) `(,',n 2))
398 (define-method acc-dis-1 ((a ,cl) x y . z) `(,',n 3))
399 (define-method acc-dis-2 ((a ,cl)) `(,',n 0))
400 (define-method acc-dis-2 ((a ,cl) x) `(,',n 1))
401 (define-method acc-dis-2 ((a ,cl) x y) `(,',n 2))
402 (define-method acc-dis-2 ((a ,cl) x y . z) `(,',n 3))
403 )))
404 `(begin ,@(append-map gen-1 (iota *acc-dis-count*))))
405
406 (gen-acc-dis-classes&methods)
407
408 (define-macro (acc-dis-classes)
409 `(list ,@(map (^n (symbol-append '<acc-dis- n '>)) (iota *acc-dis-count*))))
410
411 (define (test-acc-dis name gf :optional (expect '()))
412 (test* name expect
413 (let1 r '()
414 (do ([cs (acc-dis-classes) (cdr cs)]
415 [n 0 (+ n 1)])
416 [(null? cs) r]
417 (do ([obj (make (car cs))]
418 [k 0 (+ k 1)]
419 [args '() (cons #f args)])
420 [(= k 4)]
421 (let* ([expected `(,n ,k)]
422 [actual (apply gf obj args)])
423 (unless (equal? expected actual)
424 (push! r `(:expected ,expected :actual ,actual)))))))))
425
426 ((with-module gauche.object generic-build-dispatcher!) acc-dis-1 0)
427 (test-acc-dis "batch build" acc-dis-1)
428
429 (test-acc-dis "incremental build" acc-dis-2)
430
431 (define-method acc-dis-1 ((c <acc-dis-0>)) (cons '(0 0) (next-method)))
432 (define-method acc-dis-1 ((c <acc-dis-1>)) 'redefined)
433
434 (test-acc-dis "redefinition 1" acc-dis-1
435 '((:expected (1 0) :actual redefined)
436 (:expected (0 0) :actual ((0 0) . top))))
437
438 (define-method acc-dis-1 ((c <acc-dis-0>)) '(0 0))
439 (define-method acc-dis-1 ((c <acc-dis-1>) (x <integer>)) (cons '(1 1) (next-method)))
440 (test-acc-dis "redefinition 2" acc-dis-1
441 '((:expected (1 0) :actual redefined)))
442 (test* "redefiniton 2'" '((1 1) 1 1)
443 (acc-dis-1 (make <acc-dis-1>) 2))
444 (define-method acc-dis-1 ((c <acc-dis-1>) (x <integer>)) 'leaf)
445 (test* "redefiniton 3" '((1 1) . leaf)
446 (cons (acc-dis-1 (make <acc-dis-1>) #f)
447 (acc-dis-1 (make <acc-dis-1>) 2)))
448
449
450 ;;----------------------------------------------------------------
451 (test-section "module and accessor")
452
453 ;; This test is a contrived example of the case where the
454 ;; superclass has a slot with a getter method whose name isn't
455 ;; exported. Gauche 0.8.5 and before doesn't handle
456 ;; this correctly, since the implicit accessor method of slot a
457 ;; of <ma-class-2> defined in MA doesn't share the generic function
458 ;; with MA.inner#ma-get. Thus, in MA.inner#ma-method, (ma-get <ma-class-1>)
459 ;; is called even when the passed object is <ma-class-2>.
460 ;;
461 ;; The root of the problem is the undesired interaction between
462 ;; module system and generic functions. In 0.8.6, we haven't still
463 ;; solved the root problem, but we fixed this particular problem by
464 ;; adding extra check in accessor methods.
465 (define-module MA.inner
466 (export <ma-class-1> ma-getter ma-setter)
467 (define-class <ma-class-1> ()
468 ((a :accessor ma-get :init-value 'a)))
469 (define-method ma-getter ((o <ma-class-1>))
470 (ma-get o))
471 (define-method ma-setter ((o <ma-class-1>) val)
472 (set! (ma-get o) val)))
473
474 (define-module MA
475 (import MA.inner)
476 (export <ma-class-2> ma-g ma-s)
477 (define-class <ma-class-2> (<ma-class-1>)
478 ((b :init-value 'b)))
479 (define (ma-g o) (ma-getter o))
480 (define (ma-s o v) (ma-setter o v)))
481
482 (define-module MA.user
483 (import MA))
484
485 (test* "module and accessor" 'a
486 (with-module MA.user
487 (ma-g (make <ma-class-2>))))
488
489 (test* "module and accessor" 'ei
490 (with-module MA.user
491 (let1 m (make <ma-class-2>)
492 (ma-s m 'ei)
493 (slot-ref m 'a))))
494
495 ;; Another tricky interference between modules and implicit setter.
496 ;; The :accessor slot option may create the setter generic function
497 ;; with the weird name |setter of <getter-name>|. The name isn't supposed
498 ;; to be visible from the user ever. However, until 0.9.6, when the
499 ;; MB.base below does not export the setter name, the derived class
500 ;; couldn't find the setter gf and created a separate setter gf, making it
501 ;; incompatible with base class and derived class.
502 (define-module MB.base
503 (export <mb-base> mb-a)
504 (define-class <mb-base> ()
505 ((a :accessor mb-a :init-value 0))))
506
507 (define-module MB.derived
508 (import MB.base)
509 (export <mb-base> <mb-derived> mb-a)
510 (define-class <mb-defived> (<mb-base>) ()))
511
512 (test* "module and implicit setter" 3
513 ;; This caused 'no applicable method for #<generic |setter of mb-a|>
514 ;; in 0.9.6 and before.
515 (with-module MB.derived
516 (let1 m (make <mb-base>)
517 (set! (mb-a m) 3)
518 (~ m 'a))))
519
520 ;;----------------------------------------------------------------
521 (test-section "class redefinition (part 1)")
522
523 ;; save original <x> and <y> defined above
524 (define <x>-orig <x>)
525 (define <y>-orig <y>)
526 (define <w>-orig <w>)
527 (define <w2>-orig <w2>)
528
529 ;; create some more instances
530 (define y1 (let ((o (make <y>)))
531 (for-each (lambda (s v) (slot-set! o s v))
532 '(a b c d e)
533 '(0 1 2 3 4))
534 o))
535 (define y2 (let ((o (make <y>)))
536 (for-each (lambda (s v) (slot-set! o s v))
537 '(a b c d e)
538 '(5 6 7 8 9))
539 o))
540 (define w1 (let ((o (make <w>)))
541 (for-each (lambda (s v) (slot-set! o s v))
542 '(a b c d e f)
543 '(100 101 102 103 104 105))
544 o))
545 (define w2 (make <w>))
546
547 ;; set several methods
548 (define-method redef-test1 ((x <x>)) 'x)
549 (define-method redef-test1 ((y <y>)) 'y)
550 (define-method redef-test1 ((w <w>)) 'w)
551 (define-method redef-test1 ((w2 <w2>)) 'w2)
552
553 (define-method redef-test2 ((x <x>) (y <y>)) 'xyz)
554 (define-method redef-test2 ((z <z>) (w <w>)) 'yw)
555
556 (test* "simple redefinition of <x>" #f
557 (begin
558 (eval '(define-class <x> () (a b c x)) (current-module))
559 (eval '(eq? <x> <x>-orig) (current-module))))
560
561 (test* "simple redefinition of <x>" '(#t #f #t #f)
562 (list (eq? (ref <x>-orig 'redefined) <x>)
563 (ref <x> 'redefined)
564 (eq? (ref <y>-orig 'redefined) <y>)
565 (ref <y> 'redefined)))
566
567 (test* "subclass redefinition <y> (links)"
568 '(#f #f #f)
569 (list (eq? <y> <y>-orig)
570 (not (memq <x> (ref <y> 'direct-supers)))
571 (not (memq <x>-orig (ref <y>-orig 'direct-supers)))))
572
573 (test* "subclass redefinition <y> (slots)"
574 '((a b c) (a b c x) (c d e a b) (c d e a b x))
575 (map (^c (map (lambda (s) (car s)) (class-slots c)))
576 (list <x>-orig <x> <y>-orig <y>)))
577
578 (test* "subclass redefinition <w> (links)"
579 '(#f #f #f)
580 (list (eq? <w> <w>-orig)
581 (not (memq <y> (ref <w> 'direct-supers)))
582 (not (memq <y>-orig (ref <w>-orig 'direct-supers)))))
583
584 (test* "subclass redefinition <w> (slots)"
585 '((e f c d a b) (e f c d a b x) (e f c d a b) (e f c d a b x))
586 (map (^c (map (lambda (s) (car s)) (class-slots c)))
587 (list <w>-orig <w> <w2>-orig <w2>)))
588
589 (test* "subclass redefinition (hierarchy)"
590 (list (list <x> <object> <top>)
591 (list <y> <x> <object> <top>)
592 (list <w> <z> <y> <x> <object> <top>)
593 (list <w2> <y> <x> <z> <object> <top>))
594 (map class-precedence-list (list <x> <y> <w> <w2>)))
595
596 (test* "subclass redefinition (hierarchy, orig)"
597 (list (list <x>-orig <object> <top>)
598 (list <y>-orig <x>-orig <object> <top>)
599 (list <w>-orig <z> <y>-orig <x>-orig <object> <top>)
600 (list <w2>-orig <y>-orig <x>-orig <z> <object> <top>))
601 (map class-precedence-list
602 (list <x>-orig <y>-orig <w>-orig <w2>-orig)))
603
604 ;; check link consistency between class-direct-methods and method-specializer.x
605 (define (method-link-check gf class)
606 (and (not (null? (class-direct-methods class)))
607 (let loop ((dmeths (class-direct-methods class)))
608 (cond ((null? dmeths) #t)
609 ((memq (car dmeths) (slot-ref gf 'methods))
610 => (lambda (meth)
611 (and (memq class (slot-ref (car meth) 'specializers))
612 (loop (cdr dmeths)))))
613 (else (loop (cdr dmeths)))))))
614
615 (test* "method link fix"
616 '(#t #t #t #t #t #t #t)
617 (list (method-link-check redef-test1 <x>)
618 (method-link-check redef-test1 <y>)
619 (method-link-check redef-test1 <w>)
620 (method-link-check redef-test1 <w2>)
621 (method-link-check redef-test2 <x>)
622 (method-link-check redef-test2 <y>)
623 (method-link-check redef-test2 <w>)))
624
625 (test* "instance update (x1)" '(#t -4 -5 -6 #f)
626 (list (is-a? x1 <x>)
627 (slot-ref x1 'a)
628 (slot-ref x1 'b)
629 (slot-ref x1 'c)
630 (slot-bound? x1 'x)))
631
632 (test* "instance update (y1)" '(#f 0 1 2 3 4)
633 (list (slot-bound? y1 'x)
634 (slot-ref y1 'a)
635 (slot-ref y1 'b)
636 (slot-ref y1 'c)
637 (slot-ref y1 'd)
638 (slot-ref y1 'e)))
639
640 (test* "redefine <x> again" '(a c x)
641 (begin
642 (eval '(define-class <x> () (a c (x :init-value 3))) (current-module))
643 (eval '(map car (class-slots <x>)) (current-module))))
644
645 (test* "instance update (x1)" '(1 #f -6 3)
646 (begin
647 (slot-set! x1 'a 1)
648 (list (slot-ref x1 'a)
649 (slot-exists? x1 'b)
650 (slot-ref x1 'c)
651 (slot-ref x1 'x))))
652
653 (test* "instance update (x2) - cascade" '(#t -7 #f -9 3)
654 (list (is-a? x2 <x>)
655 (slot-ref x2 'a)
656 (slot-exists? x2 'b)
657 (slot-ref x2 'c)
658 (slot-ref x2 'x)))
659
660 (test* "redefine <y>" '(a e c x)
661 (begin
662 (eval '(define-class <y> (<x>)
663 ((a :accessor a-of)
664 (e :init-value -200)))
665 (current-module))
666 (eval '(map car (class-slots <y>)) (current-module))))
667
668 (test* "instance update (y2) - cascade"
669 '(5 7 9 3)
670 (map (^s (slot-ref y2 s)) '(a c e x)))
671
672 (test* "redefine <y> without inheriting <x>" '(a e)
673 (begin
674 (eval '(define-class <y> ()
675 ((a :init-keyword :a :init-value -30)
676 (e :init-keyword :e :init-value -40)))
677 (current-module))
678 (eval '(map car (class-slots <y>)) (current-module))))
679
680 (test* "link consistency <y> vs <x>" '(#f #f #f)
681 (list (memq <y> (ref <x> 'direct-subclasses))
682 (memq <y>-orig (ref <x> 'direct-subclasses))
683 (memq <x> (ref <y> 'direct-supers))))
684
685 (test* "instance update (y1)" '(0 4)
686 (map (^s (slot-ref y1 s)) '(a e)))
687
688 (test* "subclass redefinition <w>" '(e f a)
689 (map car (class-slots <w>)))
690
691 (test* "instance update (w1)" '(#f #t #t 100 104 105)
692 (list (is-a? w1 <x>)
693 (is-a? w1 <y>)
694 (is-a? w1 <z>)
695 (slot-ref w1 'a)
696 (slot-ref w1 'e)
697 (slot-ref w1 'f)))
698
699 (test* "instance update (w2)" '(#f #t #t -30 #f #f)
700 (list (is-a? w2 <x>)
701 (is-a? w2 <y>)
702 (is-a? w2 <z>)
703 (slot-ref w2 'a)
704 (slot-bound? w2 'e)
705 (slot-bound? w2 'f)))
706
707 (test* "method link fix"
708 '(#t #t #t #t #t #t #t)
709 (list (method-link-check redef-test1 <x>)
710 (method-link-check redef-test1 <y>)
711 (method-link-check redef-test1 <w>)
712 (method-link-check redef-test1 <w2>)
713 (method-link-check redef-test2 <x>)
714 (method-link-check redef-test2 <y>)
715 (method-link-check redef-test2 <w>)))
716
717
718 ;;----------------------------------------------------------------
719 (test-section "object comparison protocol")
720
721 (define-class <cmp> () ((x :init-keyword :x)))
722
723 (define-method object-equal? ((x <cmp>) (y <cmp>))
724 (equal? (slot-ref x 'x) (slot-ref y 'x)))
725
726 (define-method object-compare ((x <cmp>) (y <cmp>))
727 (compare (slot-ref x 'x) (slot-ref y 'x)))
728
729 (test* "object-equal?" #t
730 (equal? (make <cmp> :x 3) (make <cmp> :x 3)))
731
732 (test* "object-equal?" #f
733 (equal? (make <cmp> :x 3) (make <cmp> :x 2)))
734
735 (test* "object-equal?" #t
736 (equal? (make <cmp> :x (list 1 2))
737 (make <cmp> :x (list 1 2))))
738
739 (test* "object-equal?" #f
740 (equal? (make <cmp> :x 5) 5))
741
742 (test* "object-compare" -1 (compare 0 1))
743 (test* "object-compare" 0 (compare 0 0))
744 (test* "object-compare" 1 (compare 1 0))
745 (test* "object-compare" -1 (compare "abc" "abd"))
746 (test* "object-compare" 0 (compare "abc" "abc"))
747 (test* "object-compare" 1 (compare "abd" "abc"))
748 (test* "object-compare" -1 (compare #\a #\b))
749 (test* "object-compare" 0 (compare #\a #\a))
750 (test* "object-compare" 1 (compare #\b #\a))
751 (test* "object-compare" -1 (compare (make <cmp> :x 3) (make <cmp> :x 4)))
752 (test* "object-compare" 0 (compare (make <cmp> :x 3) (make <cmp> :x 3)))
753 (test* "object-compare" 1 (compare (make <cmp> :x 4) (make <cmp> :x 3)))
754
755 ;;----------------------------------------------------------------
756 (test-section "object hash protocol")
757
758 ;; Without definining object-hash, it returns a constant value
759 (test* "object-hash" #t
760 (eqv? (hash (make <cmp> :x (list 1 2)))
761 (hash (make <cmp> :x (list 1 2)))))
762
763 (define-method object-hash ((obj <cmp>))
764 (+ (hash (slot-ref obj 'x)) 1))
765
766 (test* "object-hash" (+ (hash (list 1 2)) 1)
767 (hash (make <cmp> :x (list 1 2))))
768 (test* "object-hash" (hash (make <cmp> :x (list 1 2)))
769 (hash (make <cmp> :x (list 1 2))))
770 (test* "object-hash" (+ (hash (vector 'a 'b)) 1)
771 (hash (make <cmp> :x '#(a b))))
772 (test* "object-hash" (+ (hash "ab") 1)
773 (hash (make <cmp> :x "ab")))
774 ;; NB: the following test is not necessarily be false theoretically,
775 ;; but we know the two returns different values in our implementation.
776 (test* "object-hash" #f
777 (equal? (hash (make <cmp> :x (cons 1 2)))
778 (hash (make <cmp> :x (cons 2 1)))))
779
780 (use scheme.list)
781
782 (define xht (make-hash-table 'equal?))
783
784 (test* "a => 8" 8
785 (begin
786 (hash-table-put! xht (make <cmp> :x 'a) 8)
787 (hash-table-get xht (make <cmp> :x 'a))))
788
789 (test* "b => non" #t
790 (hash-table-get xht (make <cmp> :x 'b) #t))
791
792 (test* "b => error" (test-error)
793 (hash-table-get xht (make <cmp> :x 'b)))
794
795 (test* "b => \"b\"" "b"
796 (begin
797 (hash-table-put! xht (make <cmp> :x 'b) "b")
798 (hash-table-get xht (make <cmp> :x 'b))))
799
800 (test* "2.0 => #\C" #\C
801 (begin
802 (hash-table-put! xht (make <cmp> :x 2.0) #\C)
803 (hash-table-get xht (make <cmp> :x 2.0))))
804
805 (test* "2.0 => #\c" #\c
806 (begin
807 (hash-table-put! xht (make <cmp> :x 2.0) #\c)
808 (hash-table-get xht (make <cmp> :x 2.0))))
809
810 (test* "87592876592374659237845692374523694756 => 0" 0
811 (begin
812 (hash-table-put! xht
813 (make <cmp> :x 87592876592374659237845692374523694756) 0)
814 (hash-table-get xht
815 (make <cmp> :x 87592876592374659237845692374523694756))))
816
817 (test* "87592876592374659237845692374523694756 => -1" -1
818 (begin
819 (hash-table-put! xht
820 (make <cmp> :x 87592876592374659237845692374523694756) -1)
821 (hash-table-get xht
822 (make <cmp> :x 87592876592374659237845692374523694756))))
823
824 (test* "equal? test" 5
825 (begin
826 (hash-table-put! xht (make <cmp> :x (string #\d)) 4)
827 (hash-table-put! xht (make <cmp> :x (string #\d)) 5)
828 (length (hash-table-keys xht))))
829
830 (test* "equal? test" 6
831 (begin
832 (hash-table-put! xht (make <cmp> :x (cons 'a 'b)) 6)
833 (hash-table-put! xht (make <cmp> :x (cons 'a 'b)) 7)
834 (length (hash-table-keys xht))))
835
836 (test* "equal? test" 7
837 (begin
838 (hash-table-put! xht (make <cmp> :x (vector (cons 'a 'b) 3+3i)) 60)
839 (hash-table-put! xht (make <cmp> :x (vector (cons 'a 'b) 3+3i)) 61)
840 (length (hash-table-keys xht))))
841
842 (test* "hash-table-values" #t
843 (lset= equal? (hash-table-values xht) '(8 "b" #\c -1 5 7 61)))
844
845 (test* "delete!" #f
846 (begin
847 (hash-table-delete! xht (make <cmp> :x (vector (cons 'a 'b) 3+3i)))
848 (hash-table-get xht (make <cmp> :x (vector (cons 'a 'b) 3+3i)) #f)))
849
850 ;; Allowing to use user-defined objects in a hashtable with
851 ;; a comparator with default-hash.
852 ;; We use eq-/eqv-hash regardless of the compoarator's hash setting
853 ;; If the equality predicate is eq?/eqv?. See SRFI-125.
854 ;; Also https://github.com/shirok/Gauche/issues/708
855 (define-class <user-defined-hashed> ()
856 ((a :init-keyword :a)))
857
858 (let ([eqtab (make-hash-table (make-eq-comparator))]
859 [eqvtab (make-hash-table (make-eqv-comparator))]
860 [x0 (make <user-defined-hashed> :a 1)]
861 [x1 (make <user-defined-hashed> :a 2)])
862 (hash-table-put! eqtab x0 1)
863 (hash-table-put! eqtab x1 2)
864 (hash-table-put! eqvtab x0 3)
865 (hash-table-put! eqvtab x1 4)
866
867 (test* "user-defined object in eq/eqv comparator"
868 '(1 2)
869 (map (cut hash-table-get eqtab <>) (list x0 x1)))
870 (test* "user-defined object in eq/eqv comparator"
871 '(3 4)
872 (map (cut hash-table-get eqvtab <>) (list x0 x1)))
873 )
874
875 ;;----------------------------------------------------------------
876 (test-section "object-apply protocol")
877
878 (define-class <applicable> ()
879 ((v :initform (make-vector 5 #f))))
880
881 (define-method object-apply ((self <applicable>) (i <integer>))
882 (vector-ref (ref self 'v) i))
883
884 (define-method (setter object-apply) ((self <applicable>) (i <integer>) v)
885 (vector-set! (ref self 'v) i v))
886
887 (define-method object-apply ((self <applicable>) (s <symbol>))
888 (case s
889 ((list) (vector->list (ref self 'v)))
890 ((vector) (ref self 'v))
891 (else #f)))
892
893 (define applicable (make <applicable>))
894
895 (test* "object-apply" #f (applicable 2))
896 (test* "object-apply" 'a
897 (begin (set! (applicable 3) 'a) (applicable 3)))
898 (test* "object-apply" '(d b c a q)
899 (begin
900 (for-each (lambda (i v) (set! (applicable i) v))
901 '(2 4 1 0) '(c q b d))
902 (map applicable '(0 1 2 3 4))))
903 (test* "object-apply" '((d b c a q) #(d b c a q))
904 (map applicable '(list vector)))
905
906 ;;----------------------------------------------------------------
907 (test-section "metaclass")
908
909 (define-class <listing-class> (<class>)
910 ((classes :allocation :class :init-value '() :accessor classes-of))
911 )
912
913 (define-method initialize ((class <listing-class>) initargs)
914 (next-method)
915 (set! (classes-of class) (cons (class-name class) (classes-of class))))
916
917 (define-class <xx> ()
918 ()
919 :metaclass <listing-class>)
920
921 (define-class <yy> (<xx>)
922 ())
923
924 (test* "metaclass" '(<yy> <xx>)
925 (class-slot-ref <listing-class> 'classes))
926
927 (define-class <auto-accessor-class> (<class>)
928 ())
929
930 (define-method initialize ((class <auto-accessor-class>) initargs)
931 (let ((slots (map (lambda (slot)
932 (if (get-keyword :accessor (cdr slot) #f)
933 slot
934 (cons (car slot)
935 (list* :accessor
936 (string->symbol
937 (format #f "~a-of" (car slot)))
938 (cdr slot)))))
939 (get-keyword :slots initargs '()))))
940 (next-method class (list* :slots slots initargs))))
941
942 (define-class <zz> ()
943 (a b c)
944 :metaclass <auto-accessor-class>)
945
946 (test* "metaclass" '(1 2 3)
947 (let ((zz (make <zz>)))
948 (set! (a-of zz) 1)
949 (set! (b-of zz) 2)
950 (set! (c-of zz) 3)
951 (map (^s (slot-ref zz s)) '(a b c))))
952
953 (define-class <uu> (<zz>)
954 (d e f))
955
956 (test* "metaclass" '(1 2 3 4 5 6)
957 (let ((uu (make <uu>)))
958 (set! (a-of uu) 1)
959 (set! (b-of uu) 2)
960 (set! (c-of uu) 3)
961 (set! (d-of uu) 4)
962 (set! (e-of uu) 5)
963 (set! (f-of uu) 6)
964 (map (^s (slot-ref uu s)) '(a b c d e f))))
965
966 (define-class <vv> (<zz> <xx>)
967 ())
968
969 (test* "metaclass" '(1 2 3)
970 (let ((vv (make <vv>)))
971 (set! (a-of vv) 1)
972 (set! (b-of vv) 2)
973 (set! (c-of vv) 3)
974 (map (^s (slot-ref vv s)) '(a b c))))
975 (test "metaclass" '(<vv> <yy> <xx>)
976 (lambda () (class-slot-ref <listing-class> 'classes)))
977
978 (define-class <ww> (<uu> <yy>)
979 ())
980
981 (test* "metaclass" #t
982 (eq? (class-of <ww>) (class-of <vv>)))
983 (test* "metaclass" '(1 2 3 4 5 6)
984 (let ((ww (make <ww>)))
985 (set! (a-of ww) 1)
986 (set! (b-of ww) 2)
987 (set! (c-of ww) 3)
988 (set! (d-of ww) 4)
989 (set! (e-of ww) 5)
990 (set! (f-of ww) 6)
991 (map (^s (slot-ref ww s)) '(a b c d e f))))
992 (test* "metaclass" '(<ww> <vv> <yy> <xx>)
993 (class-slot-ref <listing-class> 'classes))
994
995 ;;----------------------------------------------------------------
996 (test-section "metaclass w/ slots")
997
998 (define-class <docu-meta> (<class>)
999 ((sub :accessor sub-of)
1000 (doc :init-keyword :doc :initform #f)))
1001
1002 (define-method initialize ((self <docu-meta>) initargs)
1003 (next-method)
1004 (set! (sub-of self) "sub"))
1005
1006 (define-class <xxx> ()
1007 (a b c)
1008 :metaclass <docu-meta>
1009 :doc "Doc doc")
1010
1011 (test* "class slot in meta" '("Doc doc" "sub")
1012 (list (slot-ref <xxx> 'doc)
1013 (slot-ref <xxx> 'sub)))
1014
1015 (define-class <docu-meta-sub> (<docu-meta>)
1016 ((xtra :init-value 'xtra)))
1017
1018 (define-class <xxx-sub> (<xxx>)
1019 (x y z)
1020 :metaclass <docu-meta-sub>)
1021
1022 (test* "class slot in meta (sub)" '(#f "sub" xtra)
1023 (list (slot-ref <xxx-sub> 'doc)
1024 (slot-ref <xxx-sub> 'sub)
1025 (slot-ref <xxx-sub> 'xtra)))
1026
1027 ;;----------------------------------------------------------------
1028 (test-section "metaclass redefintiion")
1029
1030 ;; Create and save the instances with the original classes
1031 (define *docu*
1032 (rlet1 obj (make <xxx>)
1033 (slot-set! obj 'a 'a)
1034 (slot-set! obj 'b 'B)
1035 (slot-set! obj 'c 'c)))
1036 (define *docu-sub*
1037 (rlet1 obj (make <xxx-sub>)
1038 (slot-set! obj 'a 'A)
1039 (slot-set! obj 'b 'b)
1040 (slot-set! obj 'c 'C)
1041 (slot-set! obj 'x 'x)
1042 (slot-set! obj 'y 'Y)
1043 (slot-set! obj 'z 'z)))
1044
1045 (define-class <docu-meta> (<class>)
1046 ((doc :init-keyword :doc :initform #t)
1047 (doc2 :init-value "no doc")
1048 (sub :allocation :virtual
1049 :slot-set! (lambda (o v) #f)
1050 :slot-ref (^o (slot-ref o 'doc2)))))
1051
1052 (test* "redefinition of metaclass" '("Doc doc" "no doc" "no doc")
1053 (list (slot-ref <xxx> 'doc)
1054 (slot-ref <xxx> 'doc2)
1055 (slot-ref <xxx> 'sub)))
1056
1057 (test* "redefinition of metaclass (sub)" '(#f "no doc" xtra)
1058 (list (slot-ref <xxx-sub> 'doc)
1059 (slot-ref <xxx-sub> 'sub)
1060 (slot-ref <xxx-sub> 'xtra)))
1061
1062 (test* "redefinition of metaclass and existing instance"
1063 '(a B c)
1064 (map (cut slot-ref *docu* <>) '(a b c)))
1065 (test* "redefinition of metaclass and existing instance"
1066 '(A b C x Y z)
1067 (map (cut slot-ref *docu-sub* <>) '(a b c x y z)))
1068
1069
1070 ;; https://github.com/shirok/Gauche/issues/845
1071 (define-class <issue845-meta> (<class>) ())
1072 (define-class <issue845> () () :metaclass <issue845-meta>)
1073 (define issue845 (make <issue845>))
1074 (define-class <issue845-meta> (<class>) ())
1075 (define-class <issue845> () () :metaclass <issue845-meta>)
1076 (test* "refefinition of metaclass" #t
1077 (is-a? issue845 <issue845>))
1078
1079 ;;----------------------------------------------------------------
1080 (test-section "metaclass/bound-slot")
1081
1082 (use gauche.mop.bound-slot)
1083 (test-module 'gauche.mop.bound-slot)
1084
1085 (define-class <bound-slot-A> (<bound-slot-mixin>)
1086 ((a :init-keyword :a)
1087 (b :init-keyword :b :init-value 1)))
1088
1089 (test* "<bound-slot-A> check" (test-error)
1090 (make <bound-slot-A>))
1091
1092 (test* "<bound-slot-A> check" #t
1093 (is-a? (make <bound-slot-A> :a 3) <bound-slot-A>))
1094
1095 ;;----------------------------------------------------------------
1096 (test-section "metaclass/typed-slot")
1097
1098 (use gauche.mop.typed-slot)
1099 (test-module 'gauche.mop.typed-slot)
1100
1101 (define-class <typed-slot-A> (<typed-slot-mixin>)
1102 ((a :init-keyword :a
1103 :type <integer>)
1104 (b :init-keyword :b
1105 :type (</> <string> <symbol>))
1106 (c :init-keyword :c
1107 :type (<?> <string>))
1108 ))
1109
1110 (test* "<typed-slot-A> ok" '(1 a #f)
1111 (let1 z (make <typed-slot-A> :a 1 :b 'a :c #f)
1112 (map (cut ~ z <>) '(a b c))))
1113
1114 (test* "<typed-slot-A> set! check" '(2 "b" "c")
1115 (let1 z (make <typed-slot-A>)
1116 (set! (~ z 'a) 2)
1117 (set! (~ z 'b) "b")
1118 (set! (~ z 'c) "c")
1119 (map (cut ~ z <>) '(a b c))))
1120
1121 (test* "<typed-slot-A> init error a"
1122 (test-error <error> #/Slot a of <typed-slot-A> must be of type/)
1123 (make <typed-slot-A> :a 3.4))
1124 (test* "<typed-slot-A> init error b"
1125 (test-error <error> #/Slot b of <typed-slot-A> must be of type/)
1126 (make <typed-slot-A> :b 3))
1127 (test* "<typed-slot-A> init error c"
1128 (test-error <error> #/Slot c of <typed-slot-A> must be of type/)
1129 (make <typed-slot-A> :c 'c))
1130
1131 (test* "<typed-slot-A> set! error a"
1132 (test-error <error> #/Slot a of <typed-slot-A> must be of type/)
1133 (set! (~ (make <typed-slot-A>) 'a) #f))
1134 (test* "<typed-slot-A> set! error b"
1135 (test-error <error> #/Slot b of <typed-slot-A> must be of type/)
1136 (set! (~ (make <typed-slot-A>) 'b) #f))
1137 (test* "<typed-slot-A> set! error c"
1138 (test-error <error> #/Slot c of <typed-slot-A> must be of type/)
1139 (set! (~ (make <typed-slot-A>) 'c) 5))
1140
1141 ;;----------------------------------------------------------------
1142 (test-section "metaclass/singleton")
1143
1144 (use gauche.mop.singleton)
1145
1146 (define-class <single> ()
1147 ((foo :init-keyword :foo :initform 4))
1148 :metaclass <singleton-meta>)
1149
1150 (define single-obj (make <single> :foo 5))
1151
1152 (test* "singleton" #t (eq? single-obj (make <single>)))
1153
1154 (test* "singleton" #t (eq? single-obj (instance-of <single>)))
1155
1156 (test* "singleton" 5 (slot-ref (make <single>) 'foo))
1157
1158 (define-class <single-2> () () :metaclass <singleton-meta>)
1159
1160 (test* "singleton" #f (eq? single-obj (make <single-2>)))
1161
1162 ;;----------------------------------------------------------------
1163 (test-section "metaclass/validator")
1164
1165 (use gauche.mop.validator)
1166
1167 (define-class <validator> ()
1168 ((a :accessor a-of
1169 :initform 'doo
1170 :validator (lambda (obj value) (x->string value)))
1171 (b :accessor b-of
1172 :initform 99
1173 :validator (lambda (obj value)
1174 (if (integer? value)
1175 value
1176 (error "integer required for slot b")))))
1177 :metaclass <validator-meta>)
1178
1179 (define v (make <validator>))
1180
1181 (test* "validator" "doo" (slot-ref v 'a))
1182 (test* "validator" "foo" (begin (slot-set! v 'a 'foo) (slot-ref v 'a)))
1183 (test* "validator" "1234" (begin (set! (a-of v) 1234) (a-of v)))
1184 (test* "validator" 99 (slot-ref v 'b))
1185 (test* "validator" 55 (begin (slot-set! v 'b 55) (slot-ref v 'b)))
1186
1187 (test* "validator" (test-error) (slot-set! v 'b 3.4))
1188 (test* "validator" (test-error) (set! (b-of v) 3.4))
1189
1190 (define-class <validator2> (<validator-mixin>)
1191 ((a :validator (lambda (o v)
1192 (if (slot-bound? o 'a) "oops" v)))
1193 ))
1194
1195 (define vv (make <validator2>))
1196 (test* "validator/slot-bound" #f
1197 (slot-bound? vv 'a))
1198 (test* "validator/slot-bound" 'foo
1199 (begin (slot-set! vv 'a 'foo) (slot-ref vv 'a)))
1200 (test* "validator/slot-bound" #t
1201 (slot-bound? vv 'a))
1202 (test* "validator/slot-bound" "oops"
1203 (begin (slot-set! vv 'a 'bar) (slot-ref vv 'a)))
1204
1205 ;;----------------------------------------------------------------
1206 (test-section "metaclass/propagate")
1207
1208 (use gauche.mop.propagate)
1209
1210 (define-class <propagate-x> ()
1211 ((value :init-keyword :value :init-value 3)))
1212
1213 (define-class <propagate-y> ()
1214 ((x0 :init-keyword :x0 :init-form (make <propagate-x> :value 0))
1215 (x1 :init-keyword :x1 :init-form (make <propagate-x> :value 1))
1216 (value :allocation :propagated :propagate 'x0 :init-keyword :value)
1217 (vv :allocation :propagated :propagate '(x1 value)))
1218 :metaclass <propagate-meta>)
1219
1220 (test* "propagate ref default" '(0 0)
1221 (let* ((y (make <propagate-y>))
1222 (x (slot-ref y 'x0)))
1223 (list (slot-ref y 'value)
1224 (slot-ref x 'value))))
1225 (test* "propagate ref init" '(3 1)
1226 (let ((y (make <propagate-y> :x0 (make <propagate-x>))))
1227 (list (slot-ref y 'value) (slot-ref y 'vv))))
1228 (test* "propagate ref init2" '(99 99)
1229 (let* ((y (make <propagate-y> :x0 (make <propagate-x>) :value 99))
1230 (x (slot-ref y 'x0)))
1231 (list (slot-ref y 'value)
1232 (slot-ref x 'value))))
1233
1234 (test* "propagate set" '(888 888)
1235 (let ((y (make <propagate-y>)))
1236 (set! (slot-ref y 'value) 888)
1237 (list (slot-ref y 'value) (slot-ref (slot-ref y 'x0) 'value))))
1238
1239 (test* "propagate set" '(999 999)
1240 (let ((y (make <propagate-y>)))
1241 (set! (slot-ref y 'vv) 999)
1242 (list (slot-ref y 'vv) (slot-ref (slot-ref y 'x1) 'value))))
1243
1244 (define-class <propagate-validator-meta> (<validator-meta>
1245 <propagate-meta>)
1246 ())
1247
1248 (define-class <propagate-validator> ()
1249 ((x :initform (make <propagate-x>))
1250 (value :allocation :propagated :propagate 'x
1251 :validator (lambda (o v) (x->string v)))
1252 )
1253 :metaclass <propagate-validator-meta>)
1254
1255 (test* "propagate-validator" "999"
1256 (let ((y (make <propagate-validator>)))
1257 (set! (slot-ref y 'value) 999)
1258 (slot-ref y 'value)))
1259
1260 ;;----------------------------------------------------------------
1261 (test-section "metaclass/instance-pool")
1262
1263 (use scheme.list)
1264 (use gauche.mop.instance-pool)
1265
1266 (define-class <pool-meta> (<instance-pool-meta>)
1267 (a b c))
1268
1269 (define-class <pool-x> (<instance-pool-mixin>) ())
1270 (define-class <pool-y> (<instance-pool-mixin>) () :metaclass <pool-meta>)
1271 (define-class <pool-z> (<pool-x> <pool-y>) ())
1272
1273 (define pool-x1 (make <pool-x>))
1274 (define pool-z1 (make <pool-z>))
1275 (define pool-y1 (make <pool-y>))
1276 (define pool-y2 (make <pool-y>))
1277 (define pool-x2 (make <pool-x>))
1278 (define pool-z2 (make <pool-z>))
1279
1280 (test* "instance-pool (pool)" #t
1281 (and (memq pool-x1 (instance-pool->list <pool-x>))
1282 (memq pool-x2 (instance-pool->list <pool-x>))
1283 (not (memq pool-y1 (instance-pool->list <pool-x>)))
1284 (not (memq pool-y2 (instance-pool->list <pool-x>)))
1285 (memq pool-y1 (instance-pool->list <pool-y>))
1286 (memq pool-y2 (instance-pool->list <pool-y>))
1287 (not (memq pool-x1 (instance-pool->list <pool-y>)))
1288 (not (memq pool-x2 (instance-pool->list <pool-y>)))
1289 (memq pool-z1 (instance-pool->list <pool-x>))
1290 (memq pool-z1 (instance-pool->list <pool-y>))
1291 #t
1292 ))
1293
1294 (test* "instance-pool-find" pool-x1
1295 (instance-pool-find <pool-x> (cut eq? pool-x1 <>)))
1296
1297 (test* "instance-pool-find" pool-z1
1298 (instance-pool-find <pool-x> (cut eq? pool-z1 <>)))
1299
1300 (test* "instance-pool-find" pool-y1
1301 (instance-pool-find <pool-y> (cut eq? pool-y1 <>)))
1302
1303 (test* "instance-pool-find" #f
1304 (instance-pool-find <pool-x> (cut eq? pool-y2 <>)))
1305
1306 (test* "instance-pool-fold" (list pool-x1 pool-z1 pool-x2 pool-z2)
1307 (instance-pool-fold <pool-x> cons '())
1308 (cut lset= eq? <> <>))
1309
1310 (test* "instance-pool-map" (list pool-x1 pool-z1 pool-x2 pool-z2)
1311 (instance-pool-map <pool-x> identity)
1312 (cut lset= eq? <> <>))
1313
1314 (test* "instance-pool-for-each" (list pool-x1 pool-z1 pool-x2 pool-z2)
1315 (let ((r '()))
1316 (instance-pool-for-each <pool-x> (^p (push! r p)))
1317 r)
1318 (cut lset= eq? <> <>))
1319
1320 (test* "instance-pool-remove!" #f
1321 (begin
1322 (instance-pool-remove! <pool-x> (cut eq? pool-z1 <>))
1323 (instance-pool-find <pool-x> (cut eq? pool-z1 <>))))
1324
1325 (test* "instance-pool-remove!" pool-z1
1326 (instance-pool-find <pool-y> (cut eq? pool-z1 <>)))
1327
1328 (test* "instance-pool-remove!" #f
1329 (begin
1330 (instance-pool-remove! <pool-z> (cut eq? pool-z2 <>))
1331 (or (instance-pool-find <pool-x> (cut eq? pool-z2 <>))
1332 (instance-pool-find <pool-y> (cut eq? pool-z2 <>)))))
1333
1334 ;;----------------------------------------------------------------
1335 (test-section "class redefinition (part 2)")
1336
1337 ;; Here, we test more involved cases of class redefinition.
1338 ;; This time, the class to be redefined contains not-a-simple instance slot.
1339
1340 ;; First version of <member>
1341 (define-class <member> ()
1342 ((name :init-keyword :name)
1343 (occupation :init-keyword :occupation)
1344 (cname :allocation :class :init-value "member")
1345 ))
1346
1347 (define *member-alan*
1348 (make <member> :name "Alan Pangborn" :occupation "County Sheriff"))
1349
1350 (define *member-mike*
1351 (make <member> :name "Mike Noonan" :occupation "Writer"))
1352
1353 (define *member-ted*
1354 (make <member> :name "Ted Brautigan" :occupation "Breaker"))
1355
1356 ;; Now, we redefine <member> with splitting name slot to
1357 ;; first-name and last-name, updating them using change-class method.
1358
1359 (define <old-member> <member>) ;; saving old class
1360
1361 (define-class <member> ()
1362 ((first-name :init-keyword :first-name)
1363 (last-name :init-keyword :last-name)
1364 (occupation :init-keyword :occupation)
1365 (name :allocation :virtual
1366 :slot-ref (lambda (o)
1367 (string-append (slot-ref o 'first-name)
1368 " "
1369 (slot-ref o 'last-name)))
1370 :slot-set! (lambda (o v) #f))
1371 (cname :allocation :class)
1372 ))
1373
1374 (define-method change-class ((obj <old-member>) new-class)
1375 (let* ((old-name (slot-ref-using-class (current-class-of obj) obj 'name))
1376 (first&last (string-split old-name #\space)))
1377 (next-method)
1378 (slot-set-using-class! new-class obj 'first-name (car first&last))
1379 (slot-set-using-class! new-class obj 'last-name (cadr first&last))
1380 obj))
1381
1382 ;; Let's see we have done it right.
1383 (test* "customizing change-class" '("Alan Pangborn" "Alan" "Pangborn"
1384 "County Sheriff" "member")
1385 (map (cut slot-ref *member-alan* <>)
1386 '(name first-name last-name occupation cname)))
1387
1388 ;; Now, we refactor part of <member> into <person>.
1389 ;; This also involves redefinition of the virtual slot.
1390 (define-class <person> ()
1391 ((first-name :init-keyword :first-name)
1392 (last-name :init-keyword :last-name)
1393 (name :allocation :virtual
1394 :slot-ref (lambda (o)
1395 (string-append (slot-ref o 'first-name)
1396 " "
1397 (slot-ref o 'last-name)))
1398 :slot-set! (lambda (o v) #f))
1399 (sex :init-keyword :sex)
1400 ))
1401
1402 (define-class <member> (<person>)
1403 ((occupation :init-keyword :occupation)
1404 (cname))) ;; changing class slot to an instance slot
1405
1406 ;; Let's see we have done it right.
1407 (test* "inserting parent class" '("Alan Pangborn" "Alan" "Pangborn"
1408 "County Sheriff" "member")
1409 (map (cut slot-ref *member-alan* <>)
1410 '(name first-name last-name occupation cname)))
1411
1412 (test* "inserting parent class" '("Mike Noonan" "Mike" "Noonan"
1413 "Writer" "member")
1414 (map (cut slot-ref *member-mike* <>)
1415 '(name first-name last-name occupation cname)))
1416
1417 (slot-set! *member-alan* 'sex 'what?)
1418 (slot-set! *member-alan* 'cname "alan")
1419
1420 ;; Now, we change <person> to <validator> class, and validates
1421 ;; the value of 'sex' slot.
1422 (define-class <person> (<validator-mixin>)
1423 ((first-name :init-keyword :first-name)
1424 (last-name :init-keyword :last-name)
1425 (name :allocation :virtual
1426 :slot-ref (lambda (o)
1427 (string-append (slot-ref o 'first-name)
1428 " "
1429 (slot-ref o 'last-name)))
1430 :slot-set! (lambda (o v) #f))
1431 (sex :init-keyword :sex
1432 :validator (lambda (o v)
1433 (if (memq v '(male female))
1434 v
1435 'unknown)))
1436 ))
1437
1438 ;; Let's see we have done it right.
1439 (test* "changing parent's metaclass" '("Alan Pangborn" "Alan" "Pangborn"
1440 "County Sheriff" unknown)
1441 (map (cut slot-ref *member-alan* <>)
1442 '(name first-name last-name occupation sex)))
1443
1444 (test* "changing parent's metaclass" '(#f male)
1445 (let* ((bound? (slot-bound? *member-mike* 'sex))
1446 (val (begin (slot-set! *member-mike* 'sex 'male)
1447 (slot-ref *member-mike* 'sex))))
1448 (list bound? val)))
1449
1450 ;; Now, we drop the first-name/last-name slots and make the name slot
1451 ;; to be real one again.
1452 ;; This also tests overriding change-class of superclass.
1453 (define <old-person> <person>)
1454
1455 (define-class <person> (<validator-mixin>)
1456 ((name :init-keyword :name)
1457 (sex :init-keyword :sex
1458 :validator (lambda (o v)
1459 (if (memq v '(male female))
1460 v
1461 'unknown)))
1462 ))
1463
1464 (define-method change-class ((obj <old-person>) new-class)
1465 (let ((fn (slot-ref-using-class (current-class-of obj) obj 'first-name))
1466 (ln (slot-ref-using-class (current-class-of obj) obj 'last-name)))
1467 (next-method)
1468 (slot-set-using-class! new-class obj 'name (string-append fn " " ln))
1469 obj))
1470
1471 ;; Let's see we have done it right.
1472 (test* "reincarnating name slot" '("Alan Pangborn" "County Sheriff"
1473 unknown "alan")
1474 (map (cut slot-ref *member-alan* <>)
1475 '(name occupation sex cname)))
1476
1477 (test* "reincarnating name slot" '("Mike Noonan" "Writer"
1478 male "member")
1479 (map (cut slot-ref *member-mike* <>)
1480 '(name occupation sex cname)))
1481
1482 ;; Now, we reorganize the whole structure.
1483 (define <old-member> <member>)
1484
1485 (define-class <member> (<propagate-mixin>)
1486 ((person :init-keyword :person)
1487 (full-name :allocation :propagated
1488 :propagate-to '(person name))
1489 (sex :allocation :propagated
1490 :propagate-to 'person)
1491 (occupation :init-keyword :occupation)
1492 (cname :allocation :class) ;; back to class slot
1493 ))
1494
1495 (define-class <person> ()
1496 ((name :init-keyword :name)
1497 (sex :init-keyword :sex)
1498 ))
1499
1500 (define-method change-class ((obj <old-member>) new-class)
1501 (let* ((old-class (current-class-of obj))
1502 (name (slot-ref-using-class old-class obj 'name))
1503 (sex (if (slot-bound-using-class? old-class obj 'sex)
1504 (slot-ref-using-class old-class obj 'sex)
1505 'unknown))
1506 (person (make <person> :name name :sex sex)))
1507 (change-object-class obj old-class new-class)
1508 (slot-set-using-class! new-class obj 'person person)
1509 obj))
1510
1511 ;; Let's see we have done it right.
1512 (test* "reorganizing structure" '("Alan Pangborn" "County Sheriff"
1513 unknown "alan")
1514 (map (cut slot-ref *member-alan* <>)
1515 '(full-name occupation sex cname)))
1516
1517 (test* "reorganizing structure" '("Mike Noonan" "Writer"
1518 male "alan")
1519 (map (cut slot-ref *member-mike* <>)
1520 '(full-name occupation sex cname)))
1521
1522 (test* "reorganizing structure" '("Ted Brautigan" "Breaker"
1523 unknown "alan")
1524 (map (cut slot-ref *member-ted* <>)
1525 '(full-name occupation sex cname)))
1526
1527 ;;----------------------------------------------------------------
1528 (test-section "class redefinition (part 3 - multiple inheritance)")
1529
1530 ;; http://chaton.practical-scheme.net/gauche/a/2020/11/22#entry-5fbab148-42ea5
1531 (define-module redefinition-3
1532 (use gauche.test)
1533 (define-class <foo> () ())
1534 (define-class <bar> () ())
1535 (define-class <foobar> (<foo> <bar>) ())
1536 (define <foobar>-save <foobar>)
1537
1538 ;; This triggers redefinition of <foo> AND <foobar>.
1539 (eval '(define-class <foo> () ()) (current-module))
1540
1541 ;; Make sure reference to the old <foobar> isn't
1542 (test* "multiple inheriance redefinition" '(#t #f)
1543 (list (boolean (memq <foobar> (~ <bar>'direct-subclasses)))
1544 (boolean (memq <foobar>-save (~ <bar>'direct-subclasses)))))
1545 )
1546
1547 ;;----------------------------------------------------------------
1548 (test-section "method application customization")
1549
1550 ;; The original example of <wrapper-generic> is presented by Alex Shinn.
1551
1552 (define-class <wrapper-generic> (<generic>) ())
1553
1554 (define-method write-object ((obj <wrapper-generic>) port)
1555 (format port "#<wrapper-generic ~a>" (ref obj 'name)))
1556
1557 (define-method object-unwrap (obj pass fail)
1558 (fail))
1559
1560 (define-method apply-generic ((gf <wrapper-generic>) args)
1561 (let ((methods (compute-applicable-methods gf args)))
1562 (if (pair? methods)
1563 (apply-methods gf (sort-applicable-methods gf methods args) args)
1564 (let loop ((ls args) (rev '()))
1565 (if (null? ls)
1566 (errorf "no applicable method for ~S with arguments ~S" gf args)
1567 (object-unwrap
1568 (car ls)
1569 (lambda (obj)
1570 (apply-generic gf (append (reverse rev) (list obj) (cdr ls))))
1571 (lambda () (loop (cdr ls) (cons (car ls) rev)))))))))
1572
1573 (define-class <ci-string> ()
1574 ((value :init-keyword :value)))
1575
1576 (define (ci-string str) (make <ci-string> :value str))
1577
1578 (define-method object-unwrap ((obj <ci-string>) pass fail)
1579 (pass (ref obj 'value)))
1580
1581 (define-generic concat :class <wrapper-generic>)
1582 (define-method concat () "")
1583 (define-method concat ((a <string>)) a)
1584 (define-method concat ((a <string>) (b <string>))
1585 (string-append a b))
1586 (define-method concat ((a <string>) (b <string>) c . rest)
1587 (string-append a b (apply concat c rest)))
1588
1589 (test* "<wrapper-generic>" "aBciIiXyZ"
1590 (concat (ci-string "aBc") "iIi" (ci-string "XyZ")))
1591
1592 (define-class <coercer-generic> (<generic>) ())
1593
1594 (define-method apply-generic ((gf <coercer-generic>) args)
1595 (let ((applicable-methods (compute-applicable-methods gf args)))
1596 (if (pair? applicable-methods)
1597 (apply-methods gf (sort-applicable-methods gf applicable-methods args) args)
1598 (let loop1 ((methods (slot-ref gf 'methods)))
1599 (if (null? methods)
1600 (errorf "no applicable method for ~S with arguments ~S" gf args)
1601 (let ((method (car methods)))
1602 (let loop2 ((specs (slot-ref method 'specializers))
1603 (a args)
1604 (rev '()))
1605 (if (null? specs)
1606 (if (null? a)
1607 (apply-methods gf (list method) (reverse rev))
1608 (if (slot-ref method 'optional)
1609 (apply-methods gf (list method) (append (reverse rev) a))
1610 (loop1 (cdr methods))))
1611 (if (null? a)
1612 (loop1 (cdr methods))
1613 (let ((class (car specs)) (arg (car a)))
1614 (if (is-a? arg class)
1615 (loop2 (cdr specs) (cdr a) (cons arg rev))
1616 (coerce-to
1617 class arg
1618 (lambda (obj)
1619 (loop2 (cdr specs) (cdr a) (cons obj rev)))
1620 (lambda ()
1621 (loop1 (cdr methods)))))))))))))))
1622
1623 (define-method coerce-to (class obj pass fail)
1624 (fail))
1625
1626 (define-method coerce-to (<string> obj pass fail)
1627 (pass (x->string obj)))
1628
1629 (define-method coerce-to (<number> (str <string>) pass fail)
1630 (cond ((string->number str) => pass) (else (fail))))
1631
1632 (define-generic add :class <coercer-generic>)
1633
1634 (define-method add ((a <number>) (b <number>))
1635 (+ a b))
1636
1637 (test* "<coercer-generic>" 3
1638 (add 1 "2"))
1639
1640 ;;----------------------------------------------------------------
1641 (test-section "apply special path")
1642
1643 ;; These tests are composed so that they go through the paths
1644 ;; "goto do_method_call" or "goto do_method_call_app".
1645
1646 (define-class <app-sp-path1> () ())
1647 (define-class <app-sp-path2> (<app-sp-path1>) ())
1648
1649 (define-method app-sp-path-test1 ((x <app-sp-path1>) a b c)
1650 (list c b a))
1651 (define-method app-sp-path-test1 ((x <app-sp-path2>) a b c)
1652 (cons 'z (next-method)))
1653
1654 (define-method app-sp-path-test2 ((x <app-sp-path1>) a b c)
1655 (list c b a))
1656 (define-method app-sp-path-test2 ((x <app-sp-path2>) a b c)
1657 (cons 'z (apply next-method '())))
1658
1659 (test* "apply special path (normal->apply)" '(z c b a)
1660 (apply app-sp-path-test1 (make <app-sp-path2>) '(a b c)))
1661 (test* "apply special path (apply->normal)" '(z c b a)
1662 (app-sp-path-test2 (make <app-sp-path2>) 'a 'b 'c))
1663
1664 ;;----------------------------------------------------------------
1665 (test-section "applicable?")
1666
1667 ;; The procedure applicable? depends on libobj's method-applicable-for-clases,
1668 ;; so we test it here.
1669
1670 (define-syntax atest*
1671 (syntax-rules (=>)
1672 [(_ fn (args => expected) ...)
1673 (begin (test* (format "applicable? ~a~s" 'fn 'args)
1674 expected (applicable? fn . args))
1675 ...)]))
1676
1677 (atest* cons [() => #f] [(<top>) => #f] [(<top> <top>) => #t]
1678 [(<top> <top> <top>) => #f])
1679 (atest* list [() => #t] [(<top>) => #t] [(<top> <top>) => #t])
1680 (atest* apply [() => #f] [(<top>) => #f] [(<top> <top>) => #t])
1681
1682 (atest* (case-lambda ([a] 1) ([a b c] 2) ([a b c d e . f] 3))
1683 [() => #f] [(<top>) => #t] [(<top> <top>) => #f]
1684 [(<top> <top> <top>) => #t] [(<top> <top> <top> <top>) => #f]
1685 [(<top> <top> <top> <top> <top>) => #t]
1686 [(<top> <top> <top> <top> <top> <top>) => #t]
1687 [(<top> <top> <top> <top> <top> <top> <top>) => #t])
1688
1689 ;; NB: The result may change if loaded module adds new method to 'ref'.
1690 (atest* ref [() => #f] [(<boolean>) => #f] [(<boolean> <top>) => #f]
1691 [(<top>) => #f] [(<top> <symbol>) => #t] [(<top> <symbol> <top>) => #t]
1692 [(<vector>) => #f] [(<vector> <integer>) => #t]
1693 [(<vector> <vector> <integer> <integer>) => #f])
1694
1695 (atest* #/a/ [() => #f] [(<string>) => #t] [(<integer>) => #f])
1696
1697 ;;----------------------------------------------------------------
1698 (test-section "expansion hygiene")
1699
1700 ;; define-class etc. are expanded in libobj.scm. we should make sure that
1701 ;; the expansion won't break even user env doesn't import gauche native stuff.
1702
1703 (define-module object-hygiene-1
1704 (define-syntax defclass (with-module gauche define-class))
1705 (define-syntax defmethod (with-module gauche define-method))
1706 (define ref (with-module gauche ref))
1707 (define-syntax quote (with-module gauche quote))
1708 (export <object-hygiene-1> object-hygiene-1-c)
1709 (extend)
1710 (defclass <object-hygiene-1> ()
1711 (a b (c :init-keyword ':c)))
1712 (defmethod object-hygiene-1-c ((self <object-hygiene-1>))
1713 (ref self 'c)))
1714
1715 (import object-hygiene-1)
1716 (test* "object-hygiene-1" '(#t (a) (b) (c :init-keyword :c))
1717 (cons (is-a? <object-hygiene-1> <class>)
1718 (class-slots <object-hygiene-1>)))
1719 (test* "object-hygiene-1-c" 4
1720 (let1 z (make <object-hygiene-1> :c 4)
1721 (object-hygiene-1-c z)))
1722
1723 ;; Hygienic keyword match.
1724 ;; http://chaton.practical-scheme.net/gauche/a/2020/11/15#entry-5fb18659-edde4
1725
1726 (define-module object-hygiene-2
1727 (export <foo-meta> <foo>)
1728 (define-class <foo-meta> (<class>) ())
1729 (define-syntax define-foo
1730 (syntax-rules ()
1731 ((_ name) (define-class name () () :metaclass <foo-meta>))))
1732 (define-foo <foo>)
1733 )
1734
1735 (import (object-hygiene-2 :prefix oh2:))
1736 (test* "hygienic keyword match" oh2:<foo-meta>
1737 (class-of oh2:<foo>))
1738
1739 (test-end)