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 (expect-fail "bad init-thunk"
174 (eval '(define-class <foo> ()
175 (x #:init-thunk (lambda (x) 1)))
181 (pass-if "interaction with `struct-ref'"
182 (eval '(define-class <class-struct> ()
183 (foo #:init-keyword #:foo)
184 (bar #:init-keyword #:bar))
186 (eval '(let ((x (make <class-struct>
190 (eq? (struct-ref x 0) 'hello)
191 (eq? (struct-ref x 1) 'world)))
194 (pass-if "interaction with `struct-set!'"
195 (eval '(define-class <class-struct-2> ()
198 (eval '(let ((x (make <class-struct-2>)))
199 (struct-set! x 0 'hello)
200 (struct-set! x 1 'world)
202 (eq? (struct-ref x 0) 'hello)
203 (eq? (struct-ref x 1) 'world)))
206 (pass-if "with accessors"
207 (eval '(define-class <qux> ()
208 (x #:accessor x #:init-value 123)
209 (z #:accessor z #:init-value 789))
211 (eval '(equal? (x (make <qux>)) 123) (current-module)))
213 (pass-if-exception "cannot redefine fields of <class>"
214 '(misc-error . "cannot be redefined")
216 (define-class <test-class> (<class>)
221 (with-test-prefix "defining generics"
223 (with-test-prefix "define-generic"
225 (pass-if "creating a new top-level binding"
226 (if (eval '(defined? 'foo-0) (current-module))
228 (eval '(define-generic foo-0) (current-module))
229 (eval '(and (is-a? foo-0 <generic>)
230 (null? (generic-function-methods foo-0)))
233 (pass-if "overwriting a top-level binding to a non-generic"
234 (eval '(define (foo) #f) (current-module))
235 (eval '(define-generic foo) (current-module))
236 (eval '(and (is-a? foo <generic>)
237 (= 1 (length (generic-function-methods foo))))
240 (pass-if "overwriting a top-level binding to a generic"
241 (eval '(define (foo) #f) (current-module))
242 (eval '(define-generic foo) (current-module))
243 (eval '(define-generic foo) (current-module))
244 (eval '(and (is-a? foo <generic>)
245 (null? (generic-function-methods foo)))
248 (pass-if-exception "getters do not have setters"
249 exception:wrong-type-arg
250 (eval '(setter foo) (current-module)))))
252 (with-test-prefix "defining methods"
254 (pass-if "define-method"
255 (let ((m (current-module)))
256 (eval '(define-method (my-plus (s1 <string>) (s2 <string>))
257 (string-append s1 s2))
259 (eval '(define-method (my-plus (i1 <integer>) (i2 <integer>))
262 (eval '(and (is-a? my-plus <generic>)
263 (= (length (generic-function-methods my-plus))
267 (pass-if "method-more-specific?"
268 (eval '(let* ((m+ (generic-function-methods my-plus))
271 (arg-types (list <string> <string>)))
272 (if (memq <string> (method-specializers m1))
273 (method-more-specific? m1 m2 arg-types)
274 (method-more-specific? m2 m1 arg-types)))
277 (pass-if-exception "method-more-specific? (failure)"
278 exception:wrong-type-arg
279 (eval '(let* ((m+ (generic-function-methods my-plus))
282 (method-more-specific? m1 m2 '()))
285 (with-test-prefix "the method cache"
286 (pass-if "defining a method with a rest arg"
287 (let ((m (current-module)))
288 (eval '(define-method (foo bar . baz)
295 (eval '(equal? (foo 1 2) '(1 2))
298 (with-test-prefix "defining accessors"
300 (with-test-prefix "define-accessor"
302 (pass-if "creating a new top-level binding"
303 (if (eval '(defined? 'foo-1) (current-module))
305 (eval '(define-accessor foo-1) (current-module))
306 (eval '(and (is-a? foo-1 <generic-with-setter>)
307 (null? (generic-function-methods foo-1)))
310 (pass-if "accessors have setters"
311 (procedure? (eval '(setter foo-1) (current-module))))
313 (pass-if "overwriting a top-level binding to a non-accessor"
314 (eval '(define (foo) #f) (current-module))
315 (eval '(define-accessor foo) (current-module))
316 (eval '(and (is-a? foo <generic-with-setter>)
317 (= 1 (length (generic-function-methods foo))))
320 (pass-if "overwriting a top-level binding to an accessor"
321 (eval '(define (foo) #f) (current-module))
322 (eval '(define-accessor foo) (current-module))
323 (eval '(define-accessor foo) (current-module))
324 (eval '(and (is-a? foo <generic-with-setter>)
325 (null? (generic-function-methods foo)))
328 (with-test-prefix "object update"
329 (pass-if "defining class"
330 (eval '(define-class <foo> ()
331 (x #:accessor x #:init-value 123)
332 (z #:accessor z #:init-value 789))
334 (eval '(is-a? <foo> <class>) (current-module)))
335 (pass-if "making instance"
336 (eval '(define foo (make <foo>)) (current-module))
337 (eval '(and (is-a? foo <foo>) (= (x foo) 123)) (current-module)))
338 (pass-if "redefining class"
339 (eval '(define-class <foo> ()
340 (x #:accessor x #:init-value 123)
341 (y #:accessor y #:init-value 456)
342 (z #:accessor z #:init-value 789))
344 (eval '(and (= (y foo) 456) (= (z foo) 789)) (current-module)))
346 (pass-if "changing class"
347 (let* ((c1 (class () (the-slot #:init-keyword #:value)))
348 (c2 (class () (the-slot #:init-keyword #:value)
349 (the-other-slot #:init-value 888)))
350 (o1 (make c1 #:value 777)))
353 (equal? (slot-ref o1 'the-slot) 777)
354 (let ((o2 (change-class o1 c2)))
358 (equal? (slot-ref o2 'the-slot) 777))))))
360 (pass-if "`hell' in `goops.c' grows as expected"
361 ;; This snippet yielded a segfault prior to the 2008-08-19 `goops.c'
362 ;; fix (i.e., Guile 1.8.5 and earlier). The root of the problem was
363 ;; that `go_to_hell ()' would not reallocate enough room for the `hell'
364 ;; array, leading to out-of-bounds accesses.
366 (let* ((parent-class (class ()
367 #:name '<class-that-will-be-redefined>))
369 (unfold (lambda (i) (>= i 20))
371 (make-class (list parent-class)
372 '((the-slot #:init-value #:value)
374 #:name (string->symbol
375 (string-append "<foo-to-redefine-"
383 (make class #:value 777))
386 (define-method (change-class (foo parent-class)
388 ;; Called by `scm_change_object_class ()', via `purgatory ()'.
391 (let ((class (car classes))
392 (object (car objects)))
393 (set! classes (cdr classes))
394 (set! objects (cdr objects))
396 ;; Redefine the class so that its instances are eventually
397 ;; passed to `scm_change_object_class ()'. This leads to
398 ;; nested `scm_change_object_class ()' calls, which increases
399 ;; the size of HELL and increments N_HELL.
400 (class-redefinition class
401 (make-class '() (class-slots class)
402 #:name (class-name class)))
404 ;; Use `slot-ref' to trigger the `scm_change_object_class ()'
405 ;; and `go_to_hell ()' calls.
406 (slot-ref object 'the-slot)
411 ;; Initiate the whole `change-class' chain.
412 (let* ((class (car classes))
413 (object (change-class (car objects) class)))
414 (is-a? object class)))))
416 (with-test-prefix "object comparison"
417 (pass-if "default method"
420 (x #:accessor x #:init-keyword #:x)
421 (y #:accessor y #:init-keyword #:y))
422 (define o1 (make <c> #:x '(1) #:y '(2)))
423 (define o2 (make <c> #:x '(1) #:y '(3)))
424 (define o3 (make <c> #:x '(4) #:y '(3)))
425 (define o4 (make <c> #:x '(4) #:y '(3)))
430 (define-method (equal? (a <c>) (b <c>))
431 (equal? (y a) (y b)))
434 (pass-if "not equal?"
435 (eval '(not (equal? o1 o2))
439 (define-method (= (a <c>) (b <c>))
440 (and (equal? (x a) (x b))
441 (equal? (y a) (y b))))
445 (eval '(not (= o1 o2))
449 (use-modules (oop goops active-slot))
451 (with-test-prefix "active-slot"
452 (pass-if "defining class with active slot"
455 (define-class <bar> ()
458 #:allocation #:active
461 (set! z (cons 'before-ref z))
465 (set! z (cons 'after-ref z)))
468 (set! z (cons* v 'before-set! z)))
471 (set! z (cons* v (x o) 'after-set! z))))
472 #:metaclass <active-class>)
473 (define bar (make <bar>))
477 '(before-set! 1 before-ref after-ref
478 after-set! 1 1 before-ref after-ref
479 before-set! 2 before-ref after-ref after-set! 2 2)))
482 (use-modules (oop goops composite-slot))
484 (with-test-prefix "composite-slot"
485 (pass-if "creating instance with propagated slot"
488 (x #:accessor x #:init-keyword #:x)
489 (y #:accessor y #:init-keyword #:y))
491 (o1 #:accessor o1 #:init-form (make <a> #:x 1 #:y 2))
492 (o2 #:accessor o2 #:init-form (make <a> #:x 3 #:y 4))
494 #:allocation #:propagated
495 #:propagate-to '(o1 (o2 y)))
496 #:metaclass <composite-class>)
497 (define o (make <c>))
500 (pass-if "reading propagated slot"
501 (eval '(= (x o) 1) (current-module)))
502 (pass-if "writing propagated slot"
505 (and (= (x (o1 o)) 5)
511 (with-test-prefix "no-applicable-method"
512 (pass-if-exception "calling generic, no methods"
513 exception:no-applicable-method
515 (define-class <qux> ())
516 (define-generic quxy)
519 (pass-if "calling generic, one method, applicable"
521 (define-method (quxy (q <qux>))
523 (define q (make <qux>))
526 (pass-if-exception "calling generic, one method, not applicable"
527 exception:no-applicable-method
531 (with-test-prefix "foreign slots"
532 (define-class <foreign-test> ()
533 (a #:init-keyword #:a #:class <foreign-slot>
535 (b #:init-keyword #:b #:init-form 3 #:class <foreign-slot>
538 (pass-if-equal "constructing, no initargs"
540 (let ((x (make <foreign-test>)))
541 (list (slot-ref x 'a)
544 (pass-if-equal "constructing, initargs"
546 (let ((x (make <foreign-test> #:a 1 #:b 2)))
547 (list (slot-ref x 'a)
550 (pass-if-equal "getters"
552 (let ((x (make <foreign-test>)))
553 (list (test-a x) (test-b x))))
555 (pass-if-equal "setters"
557 (let ((x (make <foreign-test>)))
560 (list (test-a x) (test-b x))))
562 (pass-if-exception "out of range"
563 exception:out-of-range
564 (make <foreign-test> #:a (ash 1 64))))
566 (with-test-prefix "#:each-subclass"
567 (let* ((<subclass-allocation-test>
569 (test #:init-value '() #:allocation #:each-subclass)
570 #:name '<subclass-allocation-test>))
571 (a (make <subclass-allocation-test>)))
572 (pass-if-equal '() (slot-ref a 'test))
573 (let ((b (make <subclass-allocation-test>)))
574 (pass-if-equal '() (slot-ref b 'test))
575 (slot-set! a 'test 100)
576 (pass-if-equal 100 (slot-ref a 'test))
577 (pass-if-equal 100 (slot-ref b 'test))
579 ;; #:init-value of the class shouldn't reinitialize slot when
580 ;; instances are allocated.
581 (make <subclass-allocation-test>)
583 (pass-if-equal 100 (slot-ref a 'test))
584 (pass-if-equal 100 (slot-ref b 'test))
586 (let ((<test-subclass>
587 (class (<subclass-allocation-test>))))
588 (pass-if-equal 100 (slot-ref a 'test))
589 (pass-if-equal 100 (slot-ref b 'test))
590 (let ((c (make <test-subclass>)))
591 (pass-if-equal 100 (slot-ref a 'test))
592 (pass-if-equal 100 (slot-ref b 'test))
593 (pass-if-equal '() (slot-ref c 'test))
594 (slot-set! c 'test 200)
595 (pass-if-equal 200 (slot-ref c 'test))
597 (make <test-subclass>)
599 (pass-if-equal 100 (slot-ref a 'test))
600 (pass-if-equal 100 (slot-ref b 'test))
601 (pass-if-equal 200 (slot-ref c 'test)))))))
603 (with-test-prefix "accessor slots"
604 (let* ((a-accessor (make-accessor 'a))
605 (b-accessor (make-accessor 'b))
607 (a #:init-keyword #:a #:accessor a-accessor)
610 (b #:init-keyword #:b #:accessor b-accessor)
612 (<ab> (class (<a> <b>) #:name '<ab>))
613 (<ba> (class (<b> <a>) #:name '<ba>))
615 (a #:init-keyword #:a)
618 (a #:init-keyword #:a)
620 (a (make <a> #:a 'a))
621 (b (make <b> #:b 'b))
622 (ab (make <ab> #:a 'a #:b 'b))
623 (ba (make <ba> #:a 'a #:b 'b))
624 (cab (make <cab> #:a 'a #:b 'b))
625 (cba (make <cba> #:a 'a #:b 'b)))
626 (pass-if-equal "a accessor on a" 'a (a-accessor a))
627 (pass-if-equal "a accessor on ab" 'a (a-accessor ab))
628 (pass-if-equal "a accessor on ba" 'a (a-accessor ba))
629 (pass-if-exception "a accessor on cab" exception:no-applicable-method
631 (pass-if-exception "a accessor on cba" exception:no-applicable-method
633 (pass-if-equal "b accessor on a" 'b (b-accessor b))
634 (pass-if-equal "b accessor on ab" 'b (b-accessor ab))
635 (pass-if-equal "b accessor on ba" 'b (b-accessor ba))
636 (pass-if-equal "b accessor on cab" 'b (b-accessor cab))
637 (pass-if-equal "b accessor on cba" 'b (b-accessor cba))))