Debian Codegraph @

test-suite/tests/goops.test

raw
1 ;;;; goops.test --- test suite for GOOPS -*- scheme -*-
2 ;;;;
3 ;;;; Copyright (C) 2001,2003,2004, 2006, 2008, 2009, 2011, 2012, 2014, 2015, 2017, 2021 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 (define-module (test-suite test-goops)
20 #:use-module (test-suite lib)
21 #:autoload (srfi srfi-1) (unfold))
22
23 (define exception:no-applicable-method
24 '(goops-error . "^No applicable method"))
25
26 (pass-if "GOOPS loads"
27 (false-if-exception
28 (begin (resolve-module '(oop goops))
29 #t)))
30
31 (use-modules (oop goops))
32
33 ;;; more tests here...
34
35 (with-test-prefix "basic classes"
36
37 (with-test-prefix "<top>"
38
39 (pass-if "instance?"
40 (instance? <top>))
41
42 (pass-if "class-of"
43 (eq? (class-of <top>) <class>))
44
45 (pass-if "is a class?"
46 (is-a? <top> <class>))
47
48 (pass-if "class-name"
49 (eq? (class-name <top>) '<top>))
50
51 (pass-if "direct superclasses"
52 (equal? (class-direct-supers <top>) '()))
53
54 (pass-if "superclasses"
55 (equal? (class-precedence-list <top>) (list <top>)))
56
57 (pass-if "direct slots"
58 (equal? (class-direct-slots <top>) '()))
59
60 (pass-if "slots"
61 (equal? (class-slots <top>) '())))
62
63 (with-test-prefix "<object>"
64
65 (pass-if "instance?"
66 (instance? <object>))
67
68 (pass-if "class-of"
69 (eq? (class-of <object>) <class>))
70
71 (pass-if "is a class?"
72 (is-a? <object> <class>))
73
74 (pass-if "class-name"
75 (eq? (class-name <object>) '<object>))
76
77 (pass-if "direct superclasses"
78 (equal? (class-direct-supers <object>) (list <top>)))
79
80 (pass-if "superclasses"
81 (equal? (class-precedence-list <object>) (list <object> <top>)))
82
83 (pass-if "direct slots"
84 (equal? (class-direct-slots <object>) '()))
85
86 (pass-if "slots"
87 (equal? (class-slots <object>) '())))
88
89 (with-test-prefix "<class>"
90
91 (pass-if "instance?"
92 (instance? <class>))
93
94 (pass-if "class-of"
95 (eq? (class-of <class>) <class>))
96
97 (pass-if "is a class?"
98 (is-a? <class> <class>))
99
100 (pass-if "class-name"
101 (eq? (class-name <class>) '<class>))
102
103 (pass-if "direct superclass"
104 (equal? (class-direct-supers <class>) (list <object>))))
105
106 (with-test-prefix "class-precedence-list"
107 (for-each (lambda (class)
108 (run-test (if (slot-bound? class 'name)
109 (class-name class)
110 (with-output-to-string
111 (lambda ()
112 (display class))))
113 #t
114 (lambda ()
115 (catch #t
116 (lambda ()
117 (equal? (class-precedence-list class)
118 (compute-cpl class)))
119 (lambda args #t)))))
120 (let ((table (make-hash-table)))
121 (let rec ((class <top>))
122 (hash-create-handle! table class #f)
123 (for-each rec (class-direct-subclasses class)))
124 (hash-fold (lambda (class ignore classes)
125 (cons class classes))
126 '()
127 table))))
128 )
129
130 (with-test-prefix "classes for built-in types"
131
132 (pass-if "subr"
133 (eq? (class-of fluid-ref) <procedure>))
134
135 (pass-if "gsubr"
136 (eq? (class-of hashq-ref) <procedure>))
137
138 (pass-if "car"
139 (eq? (class-of car) <procedure>))
140
141 (pass-if "string"
142 (eq? (class-of "foo") <string>))
143
144 (pass-if "port"
145 (is-a? (%make-void-port "w") <port>))
146
147 (pass-if "struct vtable"
148 ;; Previously, `class-of' would fail for nameless structs, i.e., structs
149 ;; for which `struct-vtable-name' is #f.
150 (is-a? (class-of (make-vtable
151 (string-append standard-vtable-fields "pwpwpw")))
152 <class>))
153
154 ;; Two cases: one for structs created before goops, one after.
155 (pass-if "early vtable class cached"
156 (eq? (class-of (current-module))
157 (class-of (current-module))))
158 (pass-if "late vtable class cached"
159 (let ((vtable (make-vtable
160 (string-append standard-vtable-fields "pwpwpw"))))
161 (eq? (class-of vtable)
162 (class-of vtable)))))
163
164
165 (with-test-prefix "defining classes"
166
167 (with-test-prefix "define-class"
168
169 (pass-if "creating a new binding"
170 (if (eval '(defined? '<foo-0>) (current-module))
171 (throw 'unresolved))
172 (eval '(define-class <foo-0> ()) (current-module))
173 (eval '(is-a? <foo-0> <class>) (current-module)))
174
175 (pass-if "overwriting a binding to a non-class"
176 (eval '(define <foo> #f) (current-module))
177 (eval '(define-class <foo> ()) (current-module))
178 (eval '(is-a? <foo> <class>) (current-module)))
179
180 (pass-if "bad init-thunk"
181 (catch #t
182 (lambda ()
183 (eval '(define-class <foo> ()
184 (x #:init-thunk (lambda (x) 1)))
185 (current-module))
186 #f)
187 (lambda args
188 #t)))
189
190 (pass-if "interaction with `struct-ref'"
191 (eval '(define-class <class-struct> ()
192 (foo #:init-keyword #:foo)
193 (bar #:init-keyword #:bar))
194 (current-module))
195 (eval '(let ((x (make <class-struct>
196 #:foo 'hello
197 #:bar 'world)))
198 (and (struct? x)
199 (eq? (struct-ref x 0) 'hello)
200 (eq? (struct-ref x 1) 'world)))
201 (current-module)))
202
203 (pass-if "interaction with `struct-set!'"
204 (eval '(define-class <class-struct-2> ()
205 (foo) (bar))
206 (current-module))
207 (eval '(let ((x (make <class-struct-2>)))
208 (struct-set! x 0 'hello)
209 (struct-set! x 1 'world)
210 (and (struct? x)
211 (eq? (struct-ref x 0) 'hello)
212 (eq? (struct-ref x 1) 'world)))
213 (current-module)))
214
215 (pass-if "with accessors"
216 (eval '(define-class <qux> ()
217 (x #:accessor x #:init-value 123)
218 (z #:accessor z #:init-value 789))
219 (current-module))
220 (eval '(equal? (x (make <qux>)) 123) (current-module)))
221
222 (pass-if-exception "cannot redefine fields of <class>"
223 '(misc-error . "cannot be redefined")
224 (eval '(begin
225 (define-class <test-class> (<class>)
226 name)
227 (make <test-class>))
228 (current-module)))))
229
230 (with-test-prefix "defining generics"
231
232 (with-test-prefix "define-generic"
233
234 (pass-if "creating a new top-level binding"
235 (if (eval '(defined? 'foo-0) (current-module))
236 (throw 'unresolved))
237 (eval '(define-generic foo-0) (current-module))
238 (eval '(and (is-a? foo-0 <generic>)
239 (null? (generic-function-methods foo-0)))
240 (current-module)))
241
242 (pass-if "overwriting a top-level binding to a non-generic"
243 (eval '(define (foo) #f) (current-module))
244 (eval '(define-generic foo) (current-module))
245 (eval '(and (is-a? foo <generic>)
246 (= 1 (length (generic-function-methods foo))))
247 (current-module)))
248
249 (pass-if "overwriting a top-level binding to a generic"
250 (eval '(define (foo) #f) (current-module))
251 (eval '(define-generic foo) (current-module))
252 (eval '(define-generic foo) (current-module))
253 (eval '(and (is-a? foo <generic>)
254 (null? (generic-function-methods foo)))
255 (current-module)))
256
257 (pass-if-exception "getters do not have setters"
258 exception:wrong-type-arg
259 (eval '(setter foo) (current-module)))))
260
261 (with-test-prefix "defining methods"
262
263 (pass-if "define-method"
264 (let ((m (current-module)))
265 (eval '(define-method (my-plus (s1 <string>) (s2 <string>))
266 (string-append s1 s2))
267 m)
268 (eval '(define-method (my-plus (i1 <integer>) (i2 <integer>))
269 (+ i1 i2))
270 m)
271 (eval '(and (is-a? my-plus <generic>)
272 (= (length (generic-function-methods my-plus))
273 2))
274 m)))
275
276 (pass-if "method-more-specific?"
277 (eval '(let* ((m+ (generic-function-methods my-plus))
278 (m1 (car m+))
279 (m2 (cadr m+))
280 (arg-types (list <string> <string>)))
281 (if (memq <string> (method-specializers m1))
282 (method-more-specific? m1 m2 arg-types)
283 (method-more-specific? m2 m1 arg-types)))
284 (current-module)))
285
286 (pass-if-exception "method-more-specific? (failure)"
287 exception:wrong-type-arg
288 (eval '(let* ((m+ (generic-function-methods my-plus))
289 (m1 (car m+))
290 (m2 (cadr m+)))
291 (method-more-specific? m1 m2 '()))
292 (current-module))))
293
294 (with-test-prefix "the method cache"
295 (pass-if "defining a method with a rest arg"
296 (let ((m (current-module)))
297 (eval '(define-method (foo bar . baz)
298 (cons bar baz))
299 m)
300 (eval '(foo 1)
301 m)
302 (eval '(foo 1 2)
303 m)
304 (eval '(equal? (foo 1 2) '(1 2))
305 m))))
306
307 (with-test-prefix "defining accessors"
308
309 (with-test-prefix "define-accessor"
310
311 (pass-if "creating a new top-level binding"
312 (if (eval '(defined? 'foo-1) (current-module))
313 (throw 'unresolved))
314 (eval '(define-accessor foo-1) (current-module))
315 (eval '(and (is-a? foo-1 <generic-with-setter>)
316 (null? (generic-function-methods foo-1)))
317 (current-module)))
318
319 (pass-if "accessors have setters"
320 (procedure? (eval '(setter foo-1) (current-module))))
321
322 (pass-if "overwriting a top-level binding to a non-accessor"
323 (eval '(define (foo) #f) (current-module))
324 (eval '(define-accessor foo) (current-module))
325 (eval '(and (is-a? foo <generic-with-setter>)
326 (= 1 (length (generic-function-methods foo))))
327 (current-module)))
328
329 (pass-if "overwriting a top-level binding to an accessor"
330 (eval '(define (foo) #f) (current-module))
331 (eval '(define-accessor foo) (current-module))
332 (eval '(define-accessor foo) (current-module))
333 (eval '(and (is-a? foo <generic-with-setter>)
334 (null? (generic-function-methods foo)))
335 (current-module)))))
336
337 (with-test-prefix "object update"
338 (pass-if "defining class"
339 (eval '(define-class <foo> ()
340 (x #:accessor x #:init-value 123)
341 (z #:accessor z #:init-value 789)
342 #:metaclass <redefinable-class>)
343 (current-module))
344 (eval '(is-a? <foo> <class>) (current-module)))
345 (pass-if "making instance"
346 (eval '(define foo (make <foo>)) (current-module))
347 (eval '(and (is-a? foo <foo>) (= (x foo) 123)) (current-module)))
348 (pass-if "redefining class"
349 (eval '(define-class <foo> ()
350 (x #:accessor x #:init-value 123)
351 (y #:accessor y #:init-value 456)
352 (z #:accessor z #:init-value 789)
353 #:metaclass <redefinable-class>)
354 (current-module))
355 (eval '(and (= (y foo) 456) (= (z foo) 789)) (current-module)))
356
357 (pass-if "changing class"
358 (let* ((c1 (class ()
359 (the-slot #:init-keyword #:value)
360 #:metaclass <redefinable-class>))
361 (c2 (class ()
362 (the-slot #:init-keyword #:value)
363 (the-other-slot #:init-value 888)
364 #:metaclass <redefinable-class>))
365 (o1 (make c1 #:value 777)))
366 (and (is-a? o1 c1)
367 (not (is-a? o1 c2))
368 (equal? (slot-ref o1 'the-slot) 777)
369 (let ((o2 (change-class o1 c2)))
370 (and (eq? o1 o2)
371 (is-a? o2 c2)
372 (not (is-a? o2 c1))
373 (equal? (slot-ref o2 'the-slot) 777))))))
374
375 (pass-if "`hell' in `goops.c' grows as expected"
376 ;; This snippet yielded a segfault prior to the 2008-08-19 `goops.c'
377 ;; fix (i.e., Guile 1.8.5 and earlier). The root of the problem was
378 ;; that `go_to_hell ()' would not reallocate enough room for the `hell'
379 ;; array, leading to out-of-bounds accesses.
380
381 (let* ((parent-class (class ()
382 #:name '<class-that-will-be-redefined>
383 #:metaclass <redefinable-class>))
384 (classes
385 (unfold (lambda (i) (>= i 20))
386 (lambda (i)
387 (make-class (list parent-class)
388 '((the-slot #:init-value #:value)
389 (the-other-slot))
390 #:name (string->symbol
391 (string-append "<foo-to-redefine-"
392 (number->string i)
393 ">"))
394 #:metaclass <redefinable-class>))
395 (lambda (i)
396 (+ 1 i))
397 0))
398 (objects
399 (map (lambda (class)
400 (make class #:value 777))
401 classes)))
402
403 (define-method (change-class (foo parent-class)
404 (new <redefinable-class>))
405 ;; Called by `scm_change_object_class ()', via `purgatory ()'.
406 (if (null? classes)
407 (next-method)
408 (let ((class (car classes))
409 (object (car objects)))
410 (set! classes (cdr classes))
411 (set! objects (cdr objects))
412
413 ;; Redefine the class so that its instances are eventually
414 ;; passed to `scm_change_object_class ()'. This leads to
415 ;; nested `scm_change_object_class ()' calls, which increases
416 ;; the size of HELL and increments N_HELL.
417 (class-redefinition class
418 (make-class '() (class-direct-slots class)
419 #:name (class-name class)
420 #:metaclass <redefinable-class>))
421
422 ;; Use `slot-ref' to trigger the `scm_change_object_class ()'
423 ;; and `go_to_hell ()' calls.
424 (slot-ref object 'the-slot)
425
426 (next-method))))
427
428
429 ;; Initiate the whole `change-class' chain.
430 (let* ((class (car classes))
431 (object (change-class (car objects) class)))
432 (is-a? object class)))))
433
434 (with-test-prefix "object comparison"
435 (pass-if "default method"
436 (eval '(begin
437 (define-class <c> ()
438 (x #:accessor x #:init-keyword #:x)
439 (y #:accessor y #:init-keyword #:y))
440 (define o1 (make <c> #:x '(1) #:y '(2)))
441 (define o2 (make <c> #:x '(1) #:y '(3)))
442 (define o3 (make <c> #:x '(4) #:y '(3)))
443 (define o4 (make <c> #:x '(4) #:y '(3)))
444 (not (eqv? o1 o2)))
445 (current-module)))
446 (pass-if "equal?"
447 (eval '(begin
448 (define-method (equal? (a <c>) (b <c>))
449 (equal? (y a) (y b)))
450 (equal? o2 o3))
451 (current-module)))
452 (pass-if "not equal?"
453 (eval '(not (equal? o1 o2))
454 (current-module)))
455 (pass-if "="
456 (eval '(begin
457 (define-method (= (a <c>) (b <c>))
458 (and (equal? (x a) (x b))
459 (equal? (y a) (y b))))
460 (= o3 o4))
461 (current-module)))
462 (pass-if "not ="
463 (eval '(not (= o1 o2))
464 (current-module)))
465 )
466
467 (use-modules (oop goops active-slot))
468
469 (with-test-prefix "active-slot"
470 (pass-if "defining class with active slot"
471 (eval '(begin
472 (define z '())
473 (define-class <bar> ()
474 (x #:accessor x
475 #:init-value 1
476 #:allocation #:active
477 #:before-slot-ref
478 (lambda (o)
479 (set! z (cons 'before-ref z))
480 #t)
481 #:after-slot-ref
482 (lambda (o)
483 (set! z (cons 'after-ref z)))
484 #:before-slot-set!
485 (lambda (o v)
486 (set! z (cons* v 'before-set! z)))
487 #:after-slot-set!
488 (lambda (o v)
489 (set! z (cons* v (x o) 'after-set! z))))
490 #:metaclass <active-class>)
491 (define bar (make <bar>))
492 (x bar)
493 (set! (x bar) 2)
494 (equal? (reverse z)
495 '(before-set! 1 before-ref after-ref
496 after-set! 1 1 before-ref after-ref
497 before-set! 2 before-ref after-ref after-set! 2 2)))
498 (current-module))))
499
500 (use-modules (oop goops composite-slot))
501
502 (with-test-prefix "composite-slot"
503 (pass-if "creating instance with propagated slot"
504 (eval '(begin
505 (define-class <a> ()
506 (x #:accessor x #:init-keyword #:x)
507 (y #:accessor y #:init-keyword #:y))
508 (define-class <c> ()
509 (o1 #:accessor o1 #:init-form (make <a> #:x 1 #:y 2))
510 (o2 #:accessor o2 #:init-form (make <a> #:x 3 #:y 4))
511 (x #:accessor x
512 #:allocation #:propagated
513 #:propagate-to '(o1 (o2 y)))
514 #:metaclass <composite-class>)
515 (define o (make <c>))
516 (is-a? o <c>))
517 (current-module)))
518 (pass-if "reading propagated slot"
519 (eval '(= (x o) 1) (current-module)))
520 (pass-if "writing propagated slot"
521 (eval '(begin
522 (set! (x o) 5)
523 (and (= (x (o1 o)) 5)
524 (= (y (o1 o)) 2)
525 (= (x (o2 o)) 3)
526 (= (y (o2 o)) 5)))
527 (current-module))))
528
529 (with-test-prefix "no-applicable-method"
530 (pass-if-exception "calling generic, no methods"
531 exception:no-applicable-method
532 (eval '(begin
533 (define-class <qux> ())
534 (define-generic quxy)
535 (quxy 1))
536 (current-module)))
537 (pass-if "calling generic, one method, applicable"
538 (eval '(begin
539 (define-method (quxy (q <qux>))
540 #t)
541 (define q (make <qux>))
542 (quxy q))
543 (current-module)))
544 (pass-if-exception "calling generic, one method, not applicable"
545 exception:no-applicable-method
546 (eval '(quxy 1)
547 (current-module))))
548
549 (with-test-prefix "foreign slots"
550 (define-class <foreign-test> ()
551 (a #:init-keyword #:a #:class <foreign-slot>
552 #:accessor test-a)
553 (b #:init-keyword #:b #:init-form 3 #:class <foreign-slot>
554 #:accessor test-b))
555
556 (pass-if-equal "constructing, no initargs"
557 '(0 3)
558 (let ((x (make <foreign-test>)))
559 (list (slot-ref x 'a)
560 (slot-ref x 'b))))
561
562 (pass-if-equal "constructing, initargs"
563 '(1 2)
564 (let ((x (make <foreign-test> #:a 1 #:b 2)))
565 (list (slot-ref x 'a)
566 (slot-ref x 'b))))
567
568 (pass-if-equal "getters"
569 '(0 3)
570 (let ((x (make <foreign-test>)))
571 (list (test-a x) (test-b x))))
572
573 (pass-if-equal "setters"
574 '(10 20)
575 (let ((x (make <foreign-test>)))
576 (set! (test-a x) 10)
577 (set! (test-b x) 20)
578 (list (test-a x) (test-b x))))
579
580 (pass-if-exception "out of range"
581 exception:out-of-range
582 (make <foreign-test> #:a (ash 1 64))))
583
584 (with-test-prefix "#:class slot allocation"
585 (pass-if-equal "basic class slot allocation" #:class
586 (eval '(begin
587 (define-class <has-a-class-slot> ()
588 (bar #:allocation #:class #:init-value 'baz))
589 (slot-definition-allocation
590 (class-slot-definition <has-a-class-slot> 'bar)))
591 (current-module))))
592
593 (with-test-prefix "#:each-subclass"
594 (let* ((<subclass-allocation-test>
595 (class ()
596 (test #:init-value '() #:allocation #:each-subclass)
597 #:name '<subclass-allocation-test>))
598 (a (make <subclass-allocation-test>)))
599 (pass-if-equal '() (slot-ref a 'test))
600 (let ((b (make <subclass-allocation-test>)))
601 (pass-if-equal '() (slot-ref b 'test))
602 (slot-set! a 'test 100)
603 (pass-if-equal 100 (slot-ref a 'test))
604 (pass-if-equal 100 (slot-ref b 'test))
605
606 ;; #:init-value of the class shouldn't reinitialize slot when
607 ;; instances are allocated.
608 (make <subclass-allocation-test>)
609
610 (pass-if-equal 100 (slot-ref a 'test))
611 (pass-if-equal 100 (slot-ref b 'test))
612
613 (let ((<test-subclass>
614 (class (<subclass-allocation-test>))))
615 (pass-if-equal 100 (slot-ref a 'test))
616 (pass-if-equal 100 (slot-ref b 'test))
617 (let ((c (make <test-subclass>)))
618 (pass-if-equal 100 (slot-ref a 'test))
619 (pass-if-equal 100 (slot-ref b 'test))
620 (pass-if-equal '() (slot-ref c 'test))
621 (slot-set! c 'test 200)
622 (pass-if-equal 200 (slot-ref c 'test))
623
624 (make <test-subclass>)
625
626 (pass-if-equal 100 (slot-ref a 'test))
627 (pass-if-equal 100 (slot-ref b 'test))
628 (pass-if-equal 200 (slot-ref c 'test)))))))
629
630 (define-class <food> ())
631 (define-class <fruit> (<food>))
632 (define-class <spice> (<food>))
633 (define-class <apple> (<fruit>))
634 (define-class <cinnamon> (<spice>))
635 (define-class <pie> (<apple> <cinnamon>))
636
637 (define-class <d> ())
638 (define-class <e> ())
639 (define-class <f> ())
640 (define-class <b> (<d> <e>))
641 (define-class <c> (<e> <f>))
642 (define-class <a> (<b> <c>))
643
644 (with-test-prefix "compute-cpl"
645 (pass-if-equal "<pie>"
646 (list <pie> <apple> <fruit> <cinnamon> <spice> <food> <object> <top>)
647 (compute-cpl <pie>))
648
649 (pass-if-equal "<a>"
650 (list <a> <b> <d> <c> <e> <f> <object> <top>)
651 (compute-cpl <a>)))
652
653 (with-test-prefix "accessor slots"
654 (let* ((a-accessor (make-accessor 'a))
655 (b-accessor (make-accessor 'b))
656 (<a> (class ()
657 (a #:init-keyword #:a #:accessor a-accessor)
658 #:name '<a>))
659 (<b> (class ()
660 (b #:init-keyword #:b #:accessor b-accessor)
661 #:name '<b>))
662 (<ab> (class (<a> <b>) #:name '<ab>))
663 (<ba> (class (<b> <a>) #:name '<ba>))
664 (<cab> (class (<ab>)
665 (a #:init-keyword #:a)
666 #:name '<cab>))
667 (<cba> (class (<ba>)
668 (a #:init-keyword #:a)
669 #:name '<cba>))
670 (a (make <a> #:a 'a))
671 (b (make <b> #:b 'b))
672 (ab (make <ab> #:a 'a #:b 'b))
673 (ba (make <ba> #:a 'a #:b 'b))
674 (cab (make <cab> #:a 'a #:b 'b))
675 (cba (make <cba> #:a 'a #:b 'b)))
676 (pass-if-equal "a accessor on a" 'a (a-accessor a))
677 (pass-if-equal "a accessor on ab" 'a (a-accessor ab))
678 (pass-if-equal "a accessor on ba" 'a (a-accessor ba))
679 (pass-if-exception "a accessor on cab" exception:no-applicable-method
680 (a-accessor cab))
681 (pass-if-exception "a accessor on cba" exception:no-applicable-method
682 (a-accessor cba))
683 (pass-if-equal "b accessor on a" 'b (b-accessor b))
684 (pass-if-equal "b accessor on ab" 'b (b-accessor ab))
685 (pass-if-equal "b accessor on ba" 'b (b-accessor ba))
686 (pass-if-equal "b accessor on cab" 'b (b-accessor cab))
687 (pass-if-equal "b accessor on cba" 'b (b-accessor cba))))
688
689 (with-test-prefix "static slot allocation"
690 (let* ((<a> (class () (a) #:name '<a> #:static-slot-allocation? #t))
691 (<b> (class () (b) #:name '<b> #:static-slot-allocation? #t))
692 (<c> (class () (c) #:name '<c>))
693 (<ac> (class (<a> <c>) #:name '<ac>))
694 (<ca> (class (<c> <a>) #:name '<ca>)))
695 (pass-if-equal "slots of <ac>" '(a c)
696 (map slot-definition-name (class-slots <ac>)))
697 (pass-if-equal "slots of <ca>" '(a c)
698 (map slot-definition-name (class-slots <ca>)))
699 (pass-if-exception "can't make <ab>"
700 '(misc-error . "static slot")
701 (class (<a> <b>) #:name '<ab>))
702 ;; It should be possible to create subclasses of static classes
703 ;; whose slots are statically allocated, as long as there is no
704 ;; diamond inheritance among static superclasses, but for now we
705 ;; don't support it at all.
706 (pass-if-exception "static subclass"
707 '(misc-error . "static slot")
708 (class (<a>) (slot) #:name '<static-sub> #:static-slot-allocation? #t))
709 (pass-if-equal "non-static subclass" '(a d)
710 (map slot-definition-name (class-slots (class (<a>) (d) #:name '<ad>))))))
711
712 (with-test-prefix "dispatch"
713 (pass-if-equal "multi-arity dispatch" 0
714 (eval '(begin
715 (define-method (dispatch (x <number>) . args) 0)
716 (dispatch 1)
717 (dispatch 1 2)
718 ;; By now "dispatch" is forced into multi-arity mode. Test
719 ;; that the multi-arity dispatcher works:
720 (dispatch 1 2 3))
721 (current-module))))
722
723 ;; The defined? check in define-accessor prevents a local definition of
724 ;; get-the-bar, sadly!
725 (define-accessor get-the-bar)
726 (with-test-prefix "slot options on redefinable classes"
727 (let ((<meta> (class (<class>)))
728 (box make-variable)
729 (unbox variable-ref))
730 (define-class <meta> (<class>))
731
732 (define (boxed-slot? slot)
733 (get-keyword #:box? (slot-definition-options slot)))
734
735 (define-method (compute-getter-method (class <meta>) slot)
736 (if (boxed-slot? slot)
737 (make <method>
738 #:specializers (list class)
739 #:procedure (let ((slot-name (slot-definition-name slot)))
740 (lambda (obj)
741 (unbox (slot-ref obj slot-name)))))
742 (next-method)))
743
744 (define-method (compute-setter-method (class <meta>) slot)
745 (if (boxed-slot? slot)
746 (make <method>
747 #:specializers (list class <top>)
748 #:procedure (let ((slot-name (slot-definition-name slot)))
749 (lambda (obj value)
750 (set-box! (slot-ref obj slot-name) value))))
751 (next-method)))
752
753 (let* ((<redefinable-meta> (class (<meta> <redefinable-class>)))
754 (<foo>
755 (class ()
756 (bar #:accessor get-the-bar #:box? #t #:init-form (box 123))
757 #:metaclass <meta>))
758 (<redefinable-foo>
759 (class ()
760 (bar #:accessor get-the-bar #:box? #t #:init-form (box 123))
761 #:metaclass <redefinable-meta>)))
762 (pass-if-equal 123 (get-the-bar (make <foo>)))
763 (pass-if-equal 123 (get-the-bar (make <redefinable-foo>))))))
764
765 (with-test-prefix "keyword formals"
766
767 (define-class <A> ())
768 (define-class <B> (<A>))
769
770 (define a (make <A>))
771 (define b (make <B>))
772
773 (define-method* (test-opt (x <A>) #:optional y)
774 (list 'A x y))
775
776 (define-method* (test-opt (x <B>) #:optional y)
777 (append (list 'B x y)
778 (next-method)))
779
780 (pass-if-equal "optional without arg" `(B ,b #f A ,b #f) (test-opt b))
781 (pass-if-equal "optional with arg" `(B ,b 17 A ,b 17) (test-opt b 17))
782
783 (define-method* (test-key (x <A>) #:key (y 3))
784 (list 'A x y))
785
786 (define-method* (test-key (rest <B>) #:key y) ;`rest' checks impl hygiene
787 (append (list 'B rest y)
788 (next-method)))
789
790 (pass-if-equal "keyword without arg" `(B ,b #f A ,b 3) (test-key b))
791 (pass-if-equal "keyword with arg" `(B ,b 17 A ,b 17) (test-key b #:y 17))
792
793 (define-method* (test-rest (x <A>) #:optional class #:rest y) ;`class' -"-
794 (list 'A x class y))
795
796 (define-method* (test-rest (x <B>) . y)
797 (append (list 'B x y)
798 (next-method)))
799
800 (pass-if-equal "rest arg" `(B ,b (1 2 3) A ,b 1 (2 3)) (test-rest b 1 2 3))
801
802 (define-method* (test-next (x <A>) #:optional y)
803 (list 'A y))
804
805 (define-method* (test-next (x <B>) #:optional y)
806 (append (list 'B y)
807 (next-method x 2)))
808
809 (pass-if-equal "next-method arg" `(B 1 A 2) (test-next b 1))
810 )