add fluid tests
[bpt/guile.git] / test-suite / tests / goops.test
CommitLineData
4ed29c73
MV
1;;;; goops.test --- test suite for GOOPS -*- scheme -*-
2;;;;
510d7877 3;;;; Copyright (C) 2001,2003,2004, 2006, 2008, 2009 Free Software Foundation, Inc.
4ed29c73 4;;;;
53befeb7
NJ
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.
4ed29c73 9;;;;
53befeb7 10;;;; This library is distributed in the hope that it will be useful,
4ed29c73 11;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
53befeb7
NJ
12;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13;;;; Lesser General Public License for more details.
4ed29c73 14;;;;
53befeb7
NJ
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
4ed29c73 18
62ed3710 19(define-module (test-suite test-goops)
82d8d6d9
LC
20 #:use-module (test-suite lib)
21 #:autoload (srfi srfi-1) (unfold))
4ed29c73 22
2aecf4cf
AW
23(define exception:no-applicable-method
24 '(goops-error . "^No applicable method"))
25
4ed29c73
MV
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...
bdd2c6f4
DH
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"
57b1d518
MD
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 )
9f045403 129
510d7877
LC
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"
288bbc44
LC
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-vtable "prprpr" 0)) <class>)))
510d7877
LC
151
152
9f045403
DH
153(with-test-prefix "defining classes"
154
155 (with-test-prefix "define-class"
156
157 (pass-if "creating a new binding"
62ed3710
DH
158 (if (eval '(defined? '<foo-0>) (current-module))
159 (throw 'unresolved))
160 (eval '(define-class <foo-0> ()) (current-module))
161 (eval '(is-a? <foo-0> <class>) (current-module)))
9f045403
DH
162
163 (pass-if "overwriting a binding to a non-class"
164 (eval '(define <foo> #f) (current-module))
165 (eval '(define-class <foo> ()) (current-module))
47cd67db
MD
166 (eval '(is-a? <foo> <class>) (current-module)))
167
168 (expect-fail "bad init-thunk"
5f161164 169 (begin
5f161164
AW
170 (catch #t
171 (lambda ()
172 (eval '(define-class <foo> ()
173 (x #:init-thunk (lambda (x) 1)))
174 (current-module))
175 #t)
176 (lambda args
177 #f))))
cac39600
LC
178
179 (pass-if "interaction with `struct-ref'"
180 (eval '(define-class <class-struct> ()
181 (foo #:init-keyword #:foo)
182 (bar #:init-keyword #:bar))
183 (current-module))
184 (eval '(let ((x (make <class-struct>
185 #:foo 'hello
186 #:bar 'world)))
187 (and (struct? x)
188 (eq? (struct-ref x 0) 'hello)
189 (eq? (struct-ref x 1) 'world)))
190 (current-module)))
191
192 (pass-if "interaction with `struct-set!'"
193 (eval '(define-class <class-struct-2> ()
194 (foo) (bar))
195 (current-module))
196 (eval '(let ((x (make <class-struct-2>)))
197 (struct-set! x 0 'hello)
198 (struct-set! x 1 'world)
199 (and (struct? x)
200 (eq? (struct-ref x 0) 'hello)
201 (eq? (struct-ref x 1) 'world)))
5192c9e8
AW
202 (current-module)))
203
204 (pass-if "with accessors"
205 (eval '(define-class <qux> ()
206 (x #:accessor x #:init-value 123)
207 (z #:accessor z #:init-value 789))
208 (current-module))
209 (eval '(equal? (x (make <qux>)) 123) (current-module)))))
210
33e04d54
DH
211
212(with-test-prefix "defining generics"
213
214 (with-test-prefix "define-generic"
215
216 (pass-if "creating a new top-level binding"
62ed3710
DH
217 (if (eval '(defined? 'foo-0) (current-module))
218 (throw 'unresolved))
219 (eval '(define-generic foo-0) (current-module))
220 (eval '(and (is-a? foo-0 <generic>)
221 (null? (generic-function-methods foo-0)))
33e04d54
DH
222 (current-module)))
223
224 (pass-if "overwriting a top-level binding to a non-generic"
225 (eval '(define (foo) #f) (current-module))
226 (eval '(define-generic foo) (current-module))
227 (eval '(and (is-a? foo <generic>)
228 (= 1 (length (generic-function-methods foo))))
229 (current-module)))
230
231 (pass-if "overwriting a top-level binding to a generic"
232 (eval '(define (foo) #f) (current-module))
233 (eval '(define-generic foo) (current-module))
234 (eval '(define-generic foo) (current-module))
235 (eval '(and (is-a? foo <generic>)
236 (null? (generic-function-methods foo)))
237 (current-module)))))
238
b1f57ea4
LC
239(with-test-prefix "defining methods"
240
241 (pass-if "define-method"
242 (let ((m (current-module)))
243 (eval '(define-method (my-plus (s1 <string>) (s2 <string>))
244 (string-append s1 s2))
245 m)
246 (eval '(define-method (my-plus (i1 <integer>) (i2 <integer>))
247 (+ i1 i2))
248 m)
249 (eval '(and (is-a? my-plus <generic>)
250 (= (length (generic-function-methods my-plus))
251 2))
252 m)))
253
254 (pass-if "method-more-specific?"
255 (eval '(let* ((m+ (generic-function-methods my-plus))
256 (m1 (car m+))
257 (m2 (cadr m+))
258 (arg-types (list <string> <string>)))
259 (if (memq <string> (method-specializers m1))
260 (method-more-specific? m1 m2 arg-types)
261 (method-more-specific? m2 m1 arg-types)))
262 (current-module)))
263
264 (pass-if-exception "method-more-specific? (failure)"
265 exception:wrong-type-arg
266 (eval '(let* ((m+ (generic-function-methods my-plus))
267 (m1 (car m+))
268 (m2 (cadr m+)))
269 (method-more-specific? m1 m2 '()))
270 (current-module))))
271
4bcc952d
AW
272(with-test-prefix "the method cache"
273 (pass-if "defining a method with a rest arg"
274 (let ((m (current-module)))
275 (eval '(define-method (foo bar . baz)
276 (cons bar baz))
277 m)
278 (eval '(foo 1)
279 m)
280 (eval '(foo 1 2)
281 m)
282 (eval '(equal? (foo 1 2) '(1 2))
283 m))))
284
33e04d54
DH
285(with-test-prefix "defining accessors"
286
287 (with-test-prefix "define-accessor"
288
289 (pass-if "creating a new top-level binding"
62ed3710
DH
290 (if (eval '(defined? 'foo-1) (current-module))
291 (throw 'unresolved))
292 (eval '(define-accessor foo-1) (current-module))
293 (eval '(and (is-a? foo-1 <generic-with-setter>)
294 (null? (generic-function-methods foo-1)))
33e04d54
DH
295 (current-module)))
296
297 (pass-if "overwriting a top-level binding to a non-accessor"
298 (eval '(define (foo) #f) (current-module))
299 (eval '(define-accessor foo) (current-module))
300 (eval '(and (is-a? foo <generic-with-setter>)
301 (= 1 (length (generic-function-methods foo))))
302 (current-module)))
303
304 (pass-if "overwriting a top-level binding to an accessor"
305 (eval '(define (foo) #f) (current-module))
306 (eval '(define-accessor foo) (current-module))
307 (eval '(define-accessor foo) (current-module))
308 (eval '(and (is-a? foo <generic-with-setter>)
309 (null? (generic-function-methods foo)))
310 (current-module)))))
58241edc
MD
311
312(with-test-prefix "object update"
313 (pass-if "defining class"
314 (eval '(define-class <foo> ()
315 (x #:accessor x #:init-value 123)
316 (z #:accessor z #:init-value 789))
317 (current-module))
318 (eval '(is-a? <foo> <class>) (current-module)))
319 (pass-if "making instance"
320 (eval '(define foo (make <foo>)) (current-module))
321 (eval '(and (is-a? foo <foo>) (= (x foo) 123)) (current-module)))
322 (pass-if "redefining class"
323 (eval '(define-class <foo> ()
324 (x #:accessor x #:init-value 123)
325 (y #:accessor y #:init-value 456)
326 (z #:accessor z #:init-value 789))
327 (current-module))
82d8d6d9
LC
328 (eval '(and (= (y foo) 456) (= (z foo) 789)) (current-module)))
329
330 (pass-if "changing class"
331 (let* ((c1 (class () (the-slot #:init-keyword #:value)))
332 (c2 (class () (the-slot #:init-keyword #:value)
333 (the-other-slot #:init-value 888)))
334 (o1 (make c1 #:value 777)))
335 (and (is-a? o1 c1)
336 (not (is-a? o1 c2))
337 (equal? (slot-ref o1 'the-slot) 777)
338 (let ((o2 (change-class o1 c2)))
339 (and (eq? o1 o2)
340 (is-a? o2 c2)
341 (not (is-a? o2 c1))
342 (equal? (slot-ref o2 'the-slot) 777))))))
343
344 (pass-if "`hell' in `goops.c' grows as expected"
345 ;; This snippet yielded a segfault prior to the 2008-08-19 `goops.c'
346 ;; fix (i.e., Guile 1.8.5 and earlier). The root of the problem was
347 ;; that `go_to_hell ()' would not reallocate enough room for the `hell'
348 ;; array, leading to out-of-bounds accesses.
349
350 (let* ((parent-class (class ()
351 #:name '<class-that-will-be-redefined>))
352 (classes
353 (unfold (lambda (i) (>= i 20))
354 (lambda (i)
355 (make-class (list parent-class)
356 '((the-slot #:init-value #:value)
357 (the-other-slot))
358 #:name (string->symbol
359 (string-append "<foo-to-redefine-"
360 (number->string i)
361 ">"))))
362 (lambda (i)
363 (+ 1 i))
364 0))
365 (objects
366 (map (lambda (class)
367 (make class #:value 777))
368 classes)))
369
370 (define-method (change-class (foo parent-class)
371 (new <class>))
372 ;; Called by `scm_change_object_class ()', via `purgatory ()'.
373 (if (null? classes)
374 (next-method)
375 (let ((class (car classes))
376 (object (car objects)))
377 (set! classes (cdr classes))
378 (set! objects (cdr objects))
379
380 ;; Redefine the class so that its instances are eventually
381 ;; passed to `scm_change_object_class ()'. This leads to
382 ;; nested `scm_change_object_class ()' calls, which increases
383 ;; the size of HELL and increments N_HELL.
384 (class-redefinition class
385 (make-class '() (class-slots class)
386 #:name (class-name class)))
387
388 ;; Use `slot-ref' to trigger the `scm_change_object_class ()'
389 ;; and `go_to_hell ()' calls.
390 (slot-ref object 'the-slot)
391
392 (next-method))))
393
394
395 ;; Initiate the whole `change-class' chain.
396 (let* ((class (car classes))
397 (object (change-class (car objects) class)))
398 (is-a? object class)))))
58241edc 399
47cd67db
MD
400(with-test-prefix "object comparison"
401 (pass-if "default method"
57b1d518
MD
402 (eval '(begin
403 (define-class <c> ()
404 (x #:accessor x #:init-keyword #:x)
405 (y #:accessor y #:init-keyword #:y))
47cd67db
MD
406 (define o1 (make <c> #:x '(1) #:y '(2)))
407 (define o2 (make <c> #:x '(1) #:y '(3)))
408 (define o3 (make <c> #:x '(4) #:y '(3)))
409 (define o4 (make <c> #:x '(4) #:y '(3)))
410 (not (eqv? o1 o2)))
411 (current-module)))
47cd67db
MD
412 (pass-if "equal?"
413 (eval '(begin
57b1d518
MD
414 (define-method (equal? (a <c>) (b <c>))
415 (equal? (y a) (y b)))
47cd67db 416 (equal? o2 o3))
57b1d518 417 (current-module)))
47cd67db
MD
418 (pass-if "not equal?"
419 (eval '(not (equal? o1 o2))
420 (current-module)))
421 (pass-if "="
422 (eval '(begin
423 (define-method (= (a <c>) (b <c>))
424 (and (equal? (x a) (x b))
425 (equal? (y a) (y b))))
426 (= o3 o4))
427 (current-module)))
428 (pass-if "not ="
429 (eval '(not (= o1 o2))
430 (current-module)))
431 )
57b1d518 432
58241edc
MD
433(use-modules (oop goops active-slot))
434
435(with-test-prefix "active-slot"
436 (pass-if "defining class with active slot"
437 (eval '(begin
438 (define z '())
439 (define-class <bar> ()
440 (x #:accessor x
441 #:init-value 1
442 #:allocation #:active
443 #:before-slot-ref
444 (lambda (o)
445 (set! z (cons 'before-ref z))
446 #t)
447 #:after-slot-ref
448 (lambda (o)
449 (set! z (cons 'after-ref z)))
450 #:before-slot-set!
451 (lambda (o v)
452 (set! z (cons* v 'before-set! z)))
453 #:after-slot-set!
454 (lambda (o v)
455 (set! z (cons* v (x o) 'after-set! z))))
456 #:metaclass <active-class>)
457 (define bar (make <bar>))
458 (x bar)
459 (set! (x bar) 2)
460 (equal? (reverse z)
461 '(before-ref before-set! 1 before-ref after-ref
462 after-set! 1 1 before-ref after-ref
463 before-set! 2 before-ref after-ref after-set! 2 2)))
464 (current-module))))
465
47cd67db
MD
466(use-modules (oop goops composite-slot))
467
468(with-test-prefix "composite-slot"
469 (pass-if "creating instance with propagated slot"
470 (eval '(begin
471 (define-class <a> ()
472 (x #:accessor x #:init-keyword #:x)
473 (y #:accessor y #:init-keyword #:y))
474 (define-class <c> ()
475 (o1 #:accessor o1 #:init-form (make <a> #:x 1 #:y 2))
476 (o2 #:accessor o2 #:init-form (make <a> #:x 3 #:y 4))
477 (x #:accessor x
478 #:allocation #:propagated
479 #:propagate-to '(o1 (o2 y)))
480 #:metaclass <composite-class>)
481 (define o (make <c>))
482 (is-a? o <c>))
483 (current-module)))
484 (pass-if "reading propagated slot"
485 (eval '(= (x o) 1) (current-module)))
486 (pass-if "writing propagated slot"
487 (eval '(begin
488 (set! (x o) 5)
489 (and (= (x (o1 o)) 5)
490 (= (y (o1 o)) 2)
491 (= (x (o2 o)) 3)
492 (= (y (o2 o)) 5)))
493 (current-module))))
2aecf4cf
AW
494
495(with-test-prefix "no-applicable-method"
496 (pass-if-exception "calling generic, no methods"
497 exception:no-applicable-method
498 (eval '(begin
499 (define-class <qux> ())
500 (define-generic quxy)
501 (quxy 1))
502 (current-module)))
503 (pass-if "calling generic, one method, applicable"
504 (eval '(begin
505 (define-method (quxy (q <qux>))
506 #t)
507 (define q (make <qux>))
508 (quxy q))
509 (current-module)))
510 (pass-if-exception "calling generic, one method, not applicable"
511 exception:no-applicable-method
512 (eval '(quxy 1)
513 (current-module))))