1 ;;;; goops.test --- test suite for GOOPS -*- scheme -*-
3 ;;;; Copyright (C) 2001,2003,2004, 2006, 2008, 2009 Free Software Foundation, Inc.
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
10 ;;;; This program 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
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 ;;;; Boston, MA 02110-1301 USA
20 (define-module (test-suite test-goops)
21 #:use-module (test-suite lib)
22 #:autoload (srfi srfi-1) (unfold))
24 (pass-if "GOOPS loads"
26 (begin (resolve-module '(oop goops))
29 (use-modules (oop goops))
31 ;;; more tests here...
33 (with-test-prefix "basic classes"
35 (with-test-prefix "<top>"
41 (eq? (class-of <top>) <class>))
43 (pass-if "is a class?"
44 (is-a? <top> <class>))
47 (eq? (class-name <top>) '<top>))
49 (pass-if "direct superclasses"
50 (equal? (class-direct-supers <top>) '()))
52 (pass-if "superclasses"
53 (equal? (class-precedence-list <top>) (list <top>)))
55 (pass-if "direct slots"
56 (equal? (class-direct-slots <top>) '()))
59 (equal? (class-slots <top>) '())))
61 (with-test-prefix "<object>"
67 (eq? (class-of <object>) <class>))
69 (pass-if "is a class?"
70 (is-a? <object> <class>))
73 (eq? (class-name <object>) '<object>))
75 (pass-if "direct superclasses"
76 (equal? (class-direct-supers <object>) (list <top>)))
78 (pass-if "superclasses"
79 (equal? (class-precedence-list <object>) (list <object> <top>)))
81 (pass-if "direct slots"
82 (equal? (class-direct-slots <object>) '()))
85 (equal? (class-slots <object>) '())))
87 (with-test-prefix "<class>"
93 (eq? (class-of <class>) <class>))
95 (pass-if "is a class?"
96 (is-a? <class> <class>))
99 (eq? (class-name <class>) '<class>))
101 (pass-if "direct superclass"
102 (equal? (class-direct-supers <class>) (list <object>))))
104 (with-test-prefix "class-precedence-list"
105 (for-each (lambda (class)
106 (run-test (if (slot-bound? class 'name)
108 (with-output-to-string
115 (equal? (class-precedence-list class)
116 (compute-cpl class)))
118 (let ((table (make-hash-table)))
119 (let rec ((class <top>))
120 (hash-create-handle! table class #f)
121 (for-each rec (class-direct-subclasses class)))
122 (hash-fold (lambda (class ignore classes)
123 (cons class classes))
128 (with-test-prefix "classes for built-in types"
131 (eq? (class-of fluid-ref) <procedure>))
134 (eq? (class-of hashq-ref) <procedure>))
137 (eq? (class-of car) <procedure>))
140 (eq? (class-of "foo") <string>))
143 (is-a? (%make-void-port "w") <port>)))
146 (with-test-prefix "defining classes"
148 (with-test-prefix "define-class"
150 (pass-if "creating a new binding"
151 (if (eval '(defined? '<foo-0>) (current-module))
153 (eval '(define-class <foo-0> ()) (current-module))
154 (eval '(is-a? <foo-0> <class>) (current-module)))
156 (pass-if "overwriting a binding to a non-class"
157 (eval '(define <foo> #f) (current-module))
158 (eval '(define-class <foo> ()) (current-module))
159 (eval '(is-a? <foo> <class>) (current-module)))
161 (expect-fail "bad init-thunk"
164 (eval '(define-class <foo> ()
165 (x #:init-thunk (lambda (x) 1)))
171 (pass-if "interaction with `struct-ref'"
172 (eval '(define-class <class-struct> ()
173 (foo #:init-keyword #:foo)
174 (bar #:init-keyword #:bar))
176 (eval '(let ((x (make <class-struct>
180 (eq? (struct-ref x 0) 'hello)
181 (eq? (struct-ref x 1) 'world)))
184 (pass-if "interaction with `struct-set!'"
185 (eval '(define-class <class-struct-2> ()
188 (eval '(let ((x (make <class-struct-2>)))
189 (struct-set! x 0 'hello)
190 (struct-set! x 1 'world)
192 (eq? (struct-ref x 0) 'hello)
193 (eq? (struct-ref x 1) 'world)))
196 (with-test-prefix "defining generics"
198 (with-test-prefix "define-generic"
200 (pass-if "creating a new top-level binding"
201 (if (eval '(defined? 'foo-0) (current-module))
203 (eval '(define-generic foo-0) (current-module))
204 (eval '(and (is-a? foo-0 <generic>)
205 (null? (generic-function-methods foo-0)))
208 (pass-if "overwriting a top-level binding to a non-generic"
209 (eval '(define (foo) #f) (current-module))
210 (eval '(define-generic foo) (current-module))
211 (eval '(and (is-a? foo <generic>)
212 (= 1 (length (generic-function-methods foo))))
215 (pass-if "overwriting a top-level binding to a generic"
216 (eval '(define (foo) #f) (current-module))
217 (eval '(define-generic foo) (current-module))
218 (eval '(define-generic foo) (current-module))
219 (eval '(and (is-a? foo <generic>)
220 (null? (generic-function-methods foo)))
223 (with-test-prefix "defining methods"
225 (pass-if "define-method"
226 (let ((m (current-module)))
227 (eval '(define-method (my-plus (s1 <string>) (s2 <string>))
228 (string-append s1 s2))
230 (eval '(define-method (my-plus (i1 <integer>) (i2 <integer>))
233 (eval '(and (is-a? my-plus <generic>)
234 (= (length (generic-function-methods my-plus))
238 (pass-if "method-more-specific?"
239 (eval '(let* ((m+ (generic-function-methods my-plus))
242 (arg-types (list <string> <string>)))
243 (if (memq <string> (method-specializers m1))
244 (method-more-specific? m1 m2 arg-types)
245 (method-more-specific? m2 m1 arg-types)))
248 (pass-if-exception "method-more-specific? (failure)"
249 exception:wrong-type-arg
250 (eval '(let* ((m+ (generic-function-methods my-plus))
253 (method-more-specific? m1 m2 '()))
256 (with-test-prefix "defining accessors"
258 (with-test-prefix "define-accessor"
260 (pass-if "creating a new top-level binding"
261 (if (eval '(defined? 'foo-1) (current-module))
263 (eval '(define-accessor foo-1) (current-module))
264 (eval '(and (is-a? foo-1 <generic-with-setter>)
265 (null? (generic-function-methods foo-1)))
268 (pass-if "overwriting a top-level binding to a non-accessor"
269 (eval '(define (foo) #f) (current-module))
270 (eval '(define-accessor foo) (current-module))
271 (eval '(and (is-a? foo <generic-with-setter>)
272 (= 1 (length (generic-function-methods foo))))
275 (pass-if "overwriting a top-level binding to an accessor"
276 (eval '(define (foo) #f) (current-module))
277 (eval '(define-accessor foo) (current-module))
278 (eval '(define-accessor foo) (current-module))
279 (eval '(and (is-a? foo <generic-with-setter>)
280 (null? (generic-function-methods foo)))
283 (with-test-prefix "object update"
284 (pass-if "defining class"
285 (eval '(define-class <foo> ()
286 (x #:accessor x #:init-value 123)
287 (z #:accessor z #:init-value 789))
289 (eval '(is-a? <foo> <class>) (current-module)))
290 (pass-if "making instance"
291 (eval '(define foo (make <foo>)) (current-module))
292 (eval '(and (is-a? foo <foo>) (= (x foo) 123)) (current-module)))
293 (pass-if "redefining class"
294 (eval '(define-class <foo> ()
295 (x #:accessor x #:init-value 123)
296 (y #:accessor y #:init-value 456)
297 (z #:accessor z #:init-value 789))
299 (eval '(and (= (y foo) 456) (= (z foo) 789)) (current-module)))
301 (pass-if "changing class"
302 (let* ((c1 (class () (the-slot #:init-keyword #:value)))
303 (c2 (class () (the-slot #:init-keyword #:value)
304 (the-other-slot #:init-value 888)))
305 (o1 (make c1 #:value 777)))
308 (equal? (slot-ref o1 'the-slot) 777)
309 (let ((o2 (change-class o1 c2)))
313 (equal? (slot-ref o2 'the-slot) 777))))))
315 (pass-if "`hell' in `goops.c' grows as expected"
316 ;; This snippet yielded a segfault prior to the 2008-08-19 `goops.c'
317 ;; fix (i.e., Guile 1.8.5 and earlier). The root of the problem was
318 ;; that `go_to_hell ()' would not reallocate enough room for the `hell'
319 ;; array, leading to out-of-bounds accesses.
321 (let* ((parent-class (class ()
322 #:name '<class-that-will-be-redefined>))
324 (unfold (lambda (i) (>= i 20))
326 (make-class (list parent-class)
327 '((the-slot #:init-value #:value)
329 #:name (string->symbol
330 (string-append "<foo-to-redefine-"
338 (make class #:value 777))
341 (define-method (change-class (foo parent-class)
343 ;; Called by `scm_change_object_class ()', via `purgatory ()'.
346 (let ((class (car classes))
347 (object (car objects)))
348 (set! classes (cdr classes))
349 (set! objects (cdr objects))
351 ;; Redefine the class so that its instances are eventually
352 ;; passed to `scm_change_object_class ()'. This leads to
353 ;; nested `scm_change_object_class ()' calls, which increases
354 ;; the size of HELL and increments N_HELL.
355 (class-redefinition class
356 (make-class '() (class-slots class)
357 #:name (class-name class)))
359 ;; Use `slot-ref' to trigger the `scm_change_object_class ()'
360 ;; and `go_to_hell ()' calls.
361 (slot-ref object 'the-slot)
366 ;; Initiate the whole `change-class' chain.
367 (let* ((class (car classes))
368 (object (change-class (car objects) class)))
369 (is-a? object class)))))
371 (with-test-prefix "object comparison"
372 (pass-if "default method"
375 (x #:accessor x #:init-keyword #:x)
376 (y #:accessor y #:init-keyword #:y))
377 (define o1 (make <c> #:x '(1) #:y '(2)))
378 (define o2 (make <c> #:x '(1) #:y '(3)))
379 (define o3 (make <c> #:x '(4) #:y '(3)))
380 (define o4 (make <c> #:x '(4) #:y '(3)))
385 (define-method (eqv? (a <c>) (b <c>))
386 (equal? (x a) (x b)))
390 (eval '(not (eqv? o2 o3))
392 (pass-if "transfer eqv? => equal?"
393 (eval '(equal? o1 o2)
397 (define-method (equal? (a <c>) (b <c>))
398 (equal? (y a) (y b)))
401 (pass-if "not equal?"
402 (eval '(not (equal? o1 o2))
406 (define-method (= (a <c>) (b <c>))
407 (and (equal? (x a) (x b))
408 (equal? (y a) (y b))))
412 (eval '(not (= o1 o2))
416 (use-modules (oop goops active-slot))
418 (with-test-prefix "active-slot"
419 (pass-if "defining class with active slot"
422 (define-class <bar> ()
425 #:allocation #:active
428 (set! z (cons 'before-ref z))
432 (set! z (cons 'after-ref z)))
435 (set! z (cons* v 'before-set! z)))
438 (set! z (cons* v (x o) 'after-set! z))))
439 #:metaclass <active-class>)
440 (define bar (make <bar>))
444 '(before-ref before-set! 1 before-ref after-ref
445 after-set! 1 1 before-ref after-ref
446 before-set! 2 before-ref after-ref after-set! 2 2)))
449 (use-modules (oop goops composite-slot))
451 (with-test-prefix "composite-slot"
452 (pass-if "creating instance with propagated slot"
455 (x #:accessor x #:init-keyword #:x)
456 (y #:accessor y #:init-keyword #:y))
458 (o1 #:accessor o1 #:init-form (make <a> #:x 1 #:y 2))
459 (o2 #:accessor o2 #:init-form (make <a> #:x 3 #:y 4))
461 #:allocation #:propagated
462 #:propagate-to '(o1 (o2 y)))
463 #:metaclass <composite-class>)
464 (define o (make <c>))
467 (pass-if "reading propagated slot"
468 (eval '(= (x o) 1) (current-module)))
469 (pass-if "writing propagated slot"
472 (and (= (x (o1 o)) 5)