1 ;;;; goops.test --- test suite for GOOPS -*- scheme -*-
3 ;;;; Copyright (C) 2001,2003,2004, 2006, 2008, 2009, 2011, 2012, 2014, 2015 Free Software Foundation, Inc.
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.
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.
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
19 (define-module (test-suite test-goops)
20 #:use-module (test-suite lib)
21 #:autoload (srfi srfi-1) (unfold))
23 (define exception:no-applicable-method
24 '(goops-error . "^No applicable method"))
26 (pass-if "GOOPS loads"
28 (begin (resolve-module '(oop goops))
31 (use-modules (oop goops))
33 ;;; more tests here...
35 (with-test-prefix "basic classes"
37 (with-test-prefix "<top>"
43 (eq? (class-of <top>) <class>))
45 (pass-if "is a class?"
46 (is-a? <top> <class>))
49 (eq? (class-name <top>) '<top>))
51 (pass-if "direct superclasses"
52 (equal? (class-direct-supers <top>) '()))
54 (pass-if "superclasses"
55 (equal? (class-precedence-list <top>) (list <top>)))
57 (pass-if "direct slots"
58 (equal? (class-direct-slots <top>) '()))
61 (equal? (class-slots <top>) '())))
63 (with-test-prefix "<object>"
69 (eq? (class-of <object>) <class>))
71 (pass-if "is a class?"
72 (is-a? <object> <class>))
75 (eq? (class-name <object>) '<object>))
77 (pass-if "direct superclasses"
78 (equal? (class-direct-supers <object>) (list <top>)))
80 (pass-if "superclasses"
81 (equal? (class-precedence-list <object>) (list <object> <top>)))
83 (pass-if "direct slots"
84 (equal? (class-direct-slots <object>) '()))
87 (equal? (class-slots <object>) '())))
89 (with-test-prefix "<class>"
95 (eq? (class-of <class>) <class>))
97 (pass-if "is a class?"
98 (is-a? <class> <class>))
100 (pass-if "class-name"
101 (eq? (class-name <class>) '<class>))
103 (pass-if "direct superclass"
104 (equal? (class-direct-supers <class>) (list <object>))))
106 (with-test-prefix "class-precedence-list"
107 (for-each (lambda (class)
108 (run-test (if (slot-bound? class 'name)
110 (with-output-to-string
117 (equal? (class-precedence-list class)
118 (compute-cpl class)))
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))
130 (with-test-prefix "classes for built-in types"
133 (eq? (class-of fluid-ref) <procedure>))
136 (eq? (class-of hashq-ref) <procedure>))
139 (eq? (class-of car) <procedure>))
142 (eq? (class-of "foo") <string>))
145 (is-a? (%make-void-port "w") <port>))
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 "prprpr")))
155 (with-test-prefix "defining classes"
157 (with-test-prefix "define-class"
159 (pass-if "creating a new binding"
160 (if (eval '(defined? '<foo-0>) (current-module))
162 (eval '(define-class <foo-0> ()) (current-module))
163 (eval '(is-a? <foo-0> <class>) (current-module)))
165 (pass-if "overwriting a binding to a non-class"
166 (eval '(define <foo> #f) (current-module))
167 (eval '(define-class <foo> ()) (current-module))
168 (eval '(is-a? <foo> <class>) (current-module)))
170 (pass-if "bad init-thunk"
173 (eval '(define-class <foo> ()
174 (x #:init-thunk (lambda (x) 1)))
180 (pass-if "interaction with `struct-ref'"
181 (eval '(define-class <class-struct> ()
182 (foo #:init-keyword #:foo)
183 (bar #:init-keyword #:bar))
185 (eval '(let ((x (make <class-struct>
189 (eq? (struct-ref x 0) 'hello)
190 (eq? (struct-ref x 1) 'world)))
193 (pass-if "interaction with `struct-set!'"
194 (eval '(define-class <class-struct-2> ()
197 (eval '(let ((x (make <class-struct-2>)))
198 (struct-set! x 0 'hello)
199 (struct-set! x 1 'world)
201 (eq? (struct-ref x 0) 'hello)
202 (eq? (struct-ref x 1) 'world)))
205 (pass-if "with accessors"
206 (eval '(define-class <qux> ()
207 (x #:accessor x #:init-value 123)
208 (z #:accessor z #:init-value 789))
210 (eval '(equal? (x (make <qux>)) 123) (current-module)))
212 (pass-if-exception "cannot redefine fields of <class>"
213 '(misc-error . "cannot be redefined")
215 (define-class <test-class> (<class>)
220 (with-test-prefix "defining generics"
222 (with-test-prefix "define-generic"
224 (pass-if "creating a new top-level binding"
225 (if (eval '(defined? 'foo-0) (current-module))
227 (eval '(define-generic foo-0) (current-module))
228 (eval '(and (is-a? foo-0 <generic>)
229 (null? (generic-function-methods foo-0)))
232 (pass-if "overwriting a top-level binding to a non-generic"
233 (eval '(define (foo) #f) (current-module))
234 (eval '(define-generic foo) (current-module))
235 (eval '(and (is-a? foo <generic>)
236 (= 1 (length (generic-function-methods foo))))
239 (pass-if "overwriting a top-level binding to a generic"
240 (eval '(define (foo) #f) (current-module))
241 (eval '(define-generic foo) (current-module))
242 (eval '(define-generic foo) (current-module))
243 (eval '(and (is-a? foo <generic>)
244 (null? (generic-function-methods foo)))
247 (pass-if-exception "getters do not have setters"
248 exception:wrong-type-arg
249 (eval '(setter foo) (current-module)))))
251 (with-test-prefix "defining methods"
253 (pass-if "define-method"
254 (let ((m (current-module)))
255 (eval '(define-method (my-plus (s1 <string>) (s2 <string>))
256 (string-append s1 s2))
258 (eval '(define-method (my-plus (i1 <integer>) (i2 <integer>))
261 (eval '(and (is-a? my-plus <generic>)
262 (= (length (generic-function-methods my-plus))
266 (pass-if "method-more-specific?"
267 (eval '(let* ((m+ (generic-function-methods my-plus))
270 (arg-types (list <string> <string>)))
271 (if (memq <string> (method-specializers m1))
272 (method-more-specific? m1 m2 arg-types)
273 (method-more-specific? m2 m1 arg-types)))
276 (pass-if-exception "method-more-specific? (failure)"
277 exception:wrong-type-arg
278 (eval '(let* ((m+ (generic-function-methods my-plus))
281 (method-more-specific? m1 m2 '()))
284 (with-test-prefix "the method cache"
285 (pass-if "defining a method with a rest arg"
286 (let ((m (current-module)))
287 (eval '(define-method (foo bar . baz)
294 (eval '(equal? (foo 1 2) '(1 2))
297 (with-test-prefix "defining accessors"
299 (with-test-prefix "define-accessor"
301 (pass-if "creating a new top-level binding"
302 (if (eval '(defined? 'foo-1) (current-module))
304 (eval '(define-accessor foo-1) (current-module))
305 (eval '(and (is-a? foo-1 <generic-with-setter>)
306 (null? (generic-function-methods foo-1)))
309 (pass-if "accessors have setters"
310 (procedure? (eval '(setter foo-1) (current-module))))
312 (pass-if "overwriting a top-level binding to a non-accessor"
313 (eval '(define (foo) #f) (current-module))
314 (eval '(define-accessor foo) (current-module))
315 (eval '(and (is-a? foo <generic-with-setter>)
316 (= 1 (length (generic-function-methods foo))))
319 (pass-if "overwriting a top-level binding to an accessor"
320 (eval '(define (foo) #f) (current-module))
321 (eval '(define-accessor foo) (current-module))
322 (eval '(define-accessor foo) (current-module))
323 (eval '(and (is-a? foo <generic-with-setter>)
324 (null? (generic-function-methods foo)))
327 (with-test-prefix "object update"
328 (pass-if "defining class"
329 (eval '(define-class <foo> ()
330 (x #:accessor x #:init-value 123)
331 (z #:accessor z #:init-value 789))
333 (eval '(is-a? <foo> <class>) (current-module)))
334 (pass-if "making instance"
335 (eval '(define foo (make <foo>)) (current-module))
336 (eval '(and (is-a? foo <foo>) (= (x foo) 123)) (current-module)))
337 (pass-if "redefining class"
338 (eval '(define-class <foo> ()
339 (x #:accessor x #:init-value 123)
340 (y #:accessor y #:init-value 456)
341 (z #:accessor z #:init-value 789))
343 (eval '(and (= (y foo) 456) (= (z foo) 789)) (current-module)))
345 (pass-if "changing class"
346 (let* ((c1 (class () (the-slot #:init-keyword #:value)))
347 (c2 (class () (the-slot #:init-keyword #:value)
348 (the-other-slot #:init-value 888)))
349 (o1 (make c1 #:value 777)))
352 (equal? (slot-ref o1 'the-slot) 777)
353 (let ((o2 (change-class o1 c2)))
357 (equal? (slot-ref o2 'the-slot) 777))))))
359 (pass-if "`hell' in `goops.c' grows as expected"
360 ;; This snippet yielded a segfault prior to the 2008-08-19 `goops.c'
361 ;; fix (i.e., Guile 1.8.5 and earlier). The root of the problem was
362 ;; that `go_to_hell ()' would not reallocate enough room for the `hell'
363 ;; array, leading to out-of-bounds accesses.
365 (let* ((parent-class (class ()
366 #:name '<class-that-will-be-redefined>))
368 (unfold (lambda (i) (>= i 20))
370 (make-class (list parent-class)
371 '((the-slot #:init-value #:value)
373 #:name (string->symbol
374 (string-append "<foo-to-redefine-"
382 (make class #:value 777))
385 (define-method (change-class (foo parent-class)
387 ;; Called by `scm_change_object_class ()', via `purgatory ()'.
390 (let ((class (car classes))
391 (object (car objects)))
392 (set! classes (cdr classes))
393 (set! objects (cdr objects))
395 ;; Redefine the class so that its instances are eventually
396 ;; passed to `scm_change_object_class ()'. This leads to
397 ;; nested `scm_change_object_class ()' calls, which increases
398 ;; the size of HELL and increments N_HELL.
399 (class-redefinition class
400 (make-class '() (class-slots class)
401 #:name (class-name class)))
403 ;; Use `slot-ref' to trigger the `scm_change_object_class ()'
404 ;; and `go_to_hell ()' calls.
405 (slot-ref object 'the-slot)
410 ;; Initiate the whole `change-class' chain.
411 (let* ((class (car classes))
412 (object (change-class (car objects) class)))
413 (is-a? object class)))))
415 (with-test-prefix "object comparison"
416 (pass-if "default method"
419 (x #:accessor x #:init-keyword #:x)
420 (y #:accessor y #:init-keyword #:y))
421 (define o1 (make <c> #:x '(1) #:y '(2)))
422 (define o2 (make <c> #:x '(1) #:y '(3)))
423 (define o3 (make <c> #:x '(4) #:y '(3)))
424 (define o4 (make <c> #:x '(4) #:y '(3)))
429 (define-method (equal? (a <c>) (b <c>))
430 (equal? (y a) (y b)))
433 (pass-if "not equal?"
434 (eval '(not (equal? o1 o2))
438 (define-method (= (a <c>) (b <c>))
439 (and (equal? (x a) (x b))
440 (equal? (y a) (y b))))
444 (eval '(not (= o1 o2))
448 (use-modules (oop goops active-slot))
450 (with-test-prefix "active-slot"
451 (pass-if "defining class with active slot"
454 (define-class <bar> ()
457 #:allocation #:active
460 (set! z (cons 'before-ref z))
464 (set! z (cons 'after-ref z)))
467 (set! z (cons* v 'before-set! z)))
470 (set! z (cons* v (x o) 'after-set! z))))
471 #:metaclass <active-class>)
472 (define bar (make <bar>))
476 '(before-set! 1 before-ref after-ref
477 after-set! 1 1 before-ref after-ref
478 before-set! 2 before-ref after-ref after-set! 2 2)))
481 (use-modules (oop goops composite-slot))
483 (with-test-prefix "composite-slot"
484 (pass-if "creating instance with propagated slot"
487 (x #:accessor x #:init-keyword #:x)
488 (y #:accessor y #:init-keyword #:y))
490 (o1 #:accessor o1 #:init-form (make <a> #:x 1 #:y 2))
491 (o2 #:accessor o2 #:init-form (make <a> #:x 3 #:y 4))
493 #:allocation #:propagated
494 #:propagate-to '(o1 (o2 y)))
495 #:metaclass <composite-class>)
496 (define o (make <c>))
499 (pass-if "reading propagated slot"
500 (eval '(= (x o) 1) (current-module)))
501 (pass-if "writing propagated slot"
504 (and (= (x (o1 o)) 5)
510 (with-test-prefix "no-applicable-method"
511 (pass-if-exception "calling generic, no methods"
512 exception:no-applicable-method
514 (define-class <qux> ())
515 (define-generic quxy)
518 (pass-if "calling generic, one method, applicable"
520 (define-method (quxy (q <qux>))
522 (define q (make <qux>))
525 (pass-if-exception "calling generic, one method, not applicable"
526 exception:no-applicable-method
530 (with-test-prefix "foreign slots"
531 (define-class <foreign-test> ()
532 (a #:init-keyword #:a #:class <foreign-slot>
534 (b #:init-keyword #:b #:init-form 3 #:class <foreign-slot>
537 (pass-if-equal "constructing, no initargs"
539 (let ((x (make <foreign-test>)))
540 (list (slot-ref x 'a)
543 (pass-if-equal "constructing, initargs"
545 (let ((x (make <foreign-test> #:a 1 #:b 2)))
546 (list (slot-ref x 'a)
549 (pass-if-equal "getters"
551 (let ((x (make <foreign-test>)))
552 (list (test-a x) (test-b x))))
554 (pass-if-equal "setters"
556 (let ((x (make <foreign-test>)))
559 (list (test-a x) (test-b x))))
561 (pass-if-exception "out of range"
562 exception:out-of-range
563 (make <foreign-test> #:a (ash 1 64))))
565 (with-test-prefix "#:each-subclass"
566 (let* ((<subclass-allocation-test>
568 (test #:init-value '() #:allocation #:each-subclass)
569 #:name '<subclass-allocation-test>))
570 (a (make <subclass-allocation-test>)))
571 (pass-if-equal '() (slot-ref a 'test))
572 (let ((b (make <subclass-allocation-test>)))
573 (pass-if-equal '() (slot-ref b 'test))
574 (slot-set! a 'test 100)
575 (pass-if-equal 100 (slot-ref a 'test))
576 (pass-if-equal 100 (slot-ref b 'test))
578 ;; #:init-value of the class shouldn't reinitialize slot when
579 ;; instances are allocated.
580 (make <subclass-allocation-test>)
582 (pass-if-equal 100 (slot-ref a 'test))
583 (pass-if-equal 100 (slot-ref b 'test))
585 (let ((<test-subclass>
586 (class (<subclass-allocation-test>))))
587 (pass-if-equal 100 (slot-ref a 'test))
588 (pass-if-equal 100 (slot-ref b 'test))
589 (let ((c (make <test-subclass>)))
590 (pass-if-equal 100 (slot-ref a 'test))
591 (pass-if-equal 100 (slot-ref b 'test))
592 (pass-if-equal '() (slot-ref c 'test))
593 (slot-set! c 'test 200)
594 (pass-if-equal 200 (slot-ref c 'test))
596 (make <test-subclass>)
598 (pass-if-equal 100 (slot-ref a 'test))
599 (pass-if-equal 100 (slot-ref b 'test))
600 (pass-if-equal 200 (slot-ref c 'test)))))))
602 (define-class <food> ())
603 (define-class <fruit> (<food>))
604 (define-class <spice> (<food>))
605 (define-class <apple> (<fruit>))
606 (define-class <cinnamon> (<spice>))
607 (define-class <pie> (<apple> <cinnamon>))
609 (define-class <d> ())
610 (define-class <e> ())
611 (define-class <f> ())
612 (define-class <b> (<d> <e>))
613 (define-class <c> (<e> <f>))
614 (define-class <a> (<b> <c>))
616 (with-test-prefix "compute-cpl"
617 (pass-if-equal "<pie>"
618 (list <pie> <apple> <fruit> <cinnamon> <spice> <food> <object> <top>)
622 (list <a> <b> <d> <c> <e> <f> <object> <top>)
625 (with-test-prefix "accessor slots"
626 (let* ((a-accessor (make-accessor 'a))
627 (b-accessor (make-accessor 'b))
629 (a #:init-keyword #:a #:accessor a-accessor)
632 (b #:init-keyword #:b #:accessor b-accessor)
634 (<ab> (class (<a> <b>) #:name '<ab>))
635 (<ba> (class (<b> <a>) #:name '<ba>))
637 (a #:init-keyword #:a)
640 (a #:init-keyword #:a)
642 (a (make <a> #:a 'a))
643 (b (make <b> #:b 'b))
644 (ab (make <ab> #:a 'a #:b 'b))
645 (ba (make <ba> #:a 'a #:b 'b))
646 (cab (make <cab> #:a 'a #:b 'b))
647 (cba (make <cba> #:a 'a #:b 'b)))
648 (pass-if-equal "a accessor on a" 'a (a-accessor a))
649 (pass-if-equal "a accessor on ab" 'a (a-accessor ab))
650 (pass-if-equal "a accessor on ba" 'a (a-accessor ba))
651 (pass-if-exception "a accessor on cab" exception:no-applicable-method
653 (pass-if-exception "a accessor on cba" exception:no-applicable-method
655 (pass-if-equal "b accessor on a" 'b (b-accessor b))
656 (pass-if-equal "b accessor on ab" 'b (b-accessor ab))
657 (pass-if-equal "b accessor on ba" 'b (b-accessor ba))
658 (pass-if-equal "b accessor on cab" 'b (b-accessor cab))
659 (pass-if-equal "b accessor on cba" 'b (b-accessor cba))))