1 ;;;; miscellaneous side-effectful tests of the MOP
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 ;;;; Note that the MOP is not in an entirely supported state.
15 ;;;; However, this seems a good a way as any of ensuring that we have
18 (defpackage "MOP-TEST"
19 (:use
"CL" "SB-MOP" "ASSERTOID"))
21 (in-package "MOP-TEST")
23 ;;; Readers for Class Metaobjects (pp. 212--214 of AMOP)
24 (defclass red-herring
(forward-ref) ())
26 (assert (null (class-direct-slots (find-class 'forward-ref
))))
27 (assert (null (class-direct-default-initargs
28 (find-class 'forward-ref
))))
30 ;;; Readers for Generic Function Metaobjects (pp. 216--218 of AMOP)
31 (defgeneric fn-with-odd-arg-precedence
(a b c
)
32 (:argument-precedence-order b c a
))
35 (generic-function-lambda-list #'fn-with-odd-arg-precedence
)
38 (generic-function-argument-precedence-order #'fn-with-odd-arg-precedence
)
40 ;;; Test for DOCUMENTATION's order, which was wrong until sbcl-0.7.8.39
42 (generic-function-argument-precedence-order #'documentation
)
43 (let ((ll (generic-function-lambda-list #'documentation
)))
44 (list (nth 1 ll
) (nth 0 ll
)))))
47 (generic-function-declarations #'fn-with-odd-arg-precedence
)))
48 (defgeneric gf-with-declarations
(x)
49 (declare (optimize (speed 3)))
50 (declare (optimize (safety 0))))
51 (let ((decls (generic-function-declarations #'gf-with-declarations
)))
52 (assert (= (length decls
) 2))
53 (assert (member '(optimize (speed 3)) decls
:test
#'equal
))
54 (assert (member '(optimize (safety 0)) decls
:test
#'equal
)))
56 ;;; Readers for Slot Definition Metaobjects (pp. 221--224 of AMOP)
58 ;;; Ensure that SLOT-DEFINITION-ALLOCATION returns :INSTANCE/:CLASS as
60 (defclass sdm-test-class
()
61 ((an-instance-slot :accessor an-instance-slot
)
62 (a-class-slot :allocation
:class
:accessor a-class-slot
)))
63 (dolist (m (list (list #'an-instance-slot
:instance
)
64 (list #'a-class-slot
:class
)))
65 (let ((methods (generic-function-methods (car m
))))
66 (assert (= (length methods
) 1))
67 (assert (eq (slot-definition-allocation
68 (accessor-method-slot-definition
72 ;;; Class Finalization Protocol (see section 5.5.2 of AMOP)
73 (let ((finalized-count 0))
74 (defmethod finalize-inheritance :after
((x standard-class
))
75 (incf finalized-count
))
76 (defun get-count () finalized-count
))
77 (defclass finalization-test-1
() ())
78 (make-instance 'finalization-test-1
)
79 (assert (= (get-count) 1))
80 (defclass finalization-test-2
(finalization-test-3) ())
81 (assert (= (get-count) 1))
82 (defclass finalization-test-3
() ())
83 (make-instance 'finalization-test-3
)
84 (assert (or (= (get-count) 2) (= (get-count) 3)))
85 (make-instance 'finalization-test-2
)
86 (assert (= (get-count) 3))
88 ;;; Bits of FUNCALLABLE-STANDARD-CLASS are easy to break; make sure
89 ;;; that it is at least possible to define classes with that as a
91 (defclass gf-class
(standard-generic-function) ()
92 (:metaclass funcallable-standard-class
))
94 (:generic-function-class gf-class
))
96 ;;; until sbcl-0.7.12.47, PCL wasn't aware of some direct class
97 ;;; relationships. These aren't necessarily true, but are probably
98 ;;; not going to change often.
99 (dolist (x '(number array sequence character symbol
))
100 (assert (eq (car (class-direct-superclasses (find-class x
)))
102 (assert (member (find-class x
)
103 (class-direct-subclasses (find-class t
)))))
105 ;;; the class-prototype of the NULL class used to be some weird
106 ;;; standard-instance-like thing. Make sure it's actually NIL.
108 ;;; (and FIXME: eventually turn this into asserting that the prototype
109 ;;; of all built-in-classes is of the relevant type)
110 (assert (null (class-prototype (find-class 'null
))))
112 ;;; simple consistency checks for the SB-MOP package: all of the
113 ;;; functionality specified in AMOP is in functions and classes:
114 (assert (null (loop for x being each external-symbol in
"SB-MOP"
115 unless
(or (fboundp x
) (find-class x
)) collect x
)))
116 ;;; and all generic functions in SB-MOP have at least one specified
117 ;;; method, except for UPDATE-DEPENDENT
118 (assert (null (loop for x being each external-symbol in
"SB-MOP"
119 unless
(or (not (fboundp x
))
120 (eq x
'update-dependent
)
121 (not (typep (fdefinition x
) 'generic-function
))
122 (> (length (generic-function-methods
127 ;;; make sure that ENSURE-CLASS-USING-CLASS's arguments are the right
129 (defvar *e-c-u-c-arg-order
* nil
)
130 (defmethod ensure-class-using-class :after
131 (class (name (eql 'e-c-u-c-arg-order
)) &key
&allow-other-keys
)
132 (setf *e-c-u-c-arg-order
* t
))
133 (defclass e-c-u-c-arg-orderoid
() ())
134 (assert (null *e-c-u-c-arg-order
*))
135 (defclass e-c-u-c-arg-order
() ())
136 (assert (eq *e-c-u-c-arg-order
* t
))
138 ;;; verify that FIND-CLASS works after FINALIZE-INHERITANCE
139 (defclass automethod-class
(standard-class) ())
140 (defmethod validate-superclass ((c1 automethod-class
) (c2 standard-class
))
142 (defmethod finalize-inheritance :after
((x automethod-class
))
143 (format t
"~&~S ~S~%" x
(find-class (class-name x
))))
144 (defclass automethod-object
() ()
145 (:metaclass automethod-class
))
146 (defvar *automethod-object
* (make-instance 'automethod-object
))
147 (assert (typep *automethod-object
* 'automethod-object
))
149 ;;; COMPUTE-EFFECTIVE-SLOT-DEFINITION should take three arguments, one
150 ;;; of which is the name of the slot.
151 (defvar *compute-effective-slot-definition-count
* 0)
152 (defmethod compute-effective-slot-definition :before
153 (class (name (eql 'foo
)) dsds
)
154 (incf *compute-effective-slot-definition-count
*))
155 (defclass cesd-test-class
()
156 ((foo :initarg
:foo
)))
157 (make-instance 'cesd-test-class
:foo
3)
158 ;;; FIXME: this assertion seems a little weak. I don't know why
159 ;;; COMPUTE-EFFECTIVE-SLOT-DEFINITION gets called twice in this
160 ;;; sequence, nor whether that's compliant with AMOP. -- CSR,
162 (assert (> *compute-effective-slot-definition-count
* 0))
164 ;;; this used to cause a nasty uncaught metacircularity in PCL.
165 (defclass substandard-method
(standard-method) ())
166 (defgeneric substandard-defgeneric
(x y
)
167 (:method-class substandard-method
)
168 (:method
((x number
) (y number
)) (+ x y
))
169 (:method
((x string
) (y string
)) (concatenate 'string x y
)))
170 (assert (= (substandard-defgeneric 1 2) 3))
171 (assert (string= (substandard-defgeneric "1" "2") "12"))
173 (let* ((x (find-class 'pathname
))
174 (xs (class-direct-subclasses x
)))
175 (assert (>= (length xs
) 1))
176 (assert (member (find-class 'logical-pathname
) xs
)))
178 ;;; BUG 338: "MOP specializers as type specifiers"
179 ;;; (reported by Bruno Haible sbcl-devel 2004-06-11)
180 (let* ((m (defmethod eql-specialized-method ((x (eql 4.0))) 3.0))
181 (spec (first (sb-mop:method-specializers m
))))
182 (assert (not (typep 1 spec
)))
183 (assert (typep 4.0 spec
)))
185 ;;; BUG #334, relating to programmatic addition of slots to a class
186 ;;; with COMPUTE-SLOTS.
188 ;;; FIXME: the DUMMY classes here are to prevent class finalization
189 ;;; before the compute-slots method is around. This should probably
190 ;;; be done by defining the COMPUTE-SLOTS methods on a metaclass,
191 ;;; which can be defined before.
193 ;;; a. adding an :allocation :instance slot
194 (defclass class-to-add-instance-slot
(dummy-ctais) ())
195 (defmethod compute-slots ((c (eql (find-class 'class-to-add-instance-slot
))))
196 (append (call-next-method)
197 (list (make-instance 'standard-effective-slot-definition
199 :allocation
:instance
))))
200 (defclass dummy-ctais
() ((x :allocation
:class
)))
201 (finalize-inheritance (find-class 'class-to-add-instance-slot
))
202 (assert (equal (mapcar #'slot-definition-allocation
203 (class-slots (find-class 'class-to-add-instance-slot
)))
204 ;; FIXME: is the order really guaranteed?
205 '(:class
:instance
)))
206 (assert (typep (slot-definition-location
207 (cadr (class-slots (find-class 'class-to-add-instance-slot
))))
209 #|
(assert (typep (slot-definition-location (car ...
)) '???
)) |
#
210 (let ((x (make-instance 'class-to-add-instance-slot
)))
211 (assert (not (slot-boundp x
'x
)))
212 (setf (slot-value x
'x
) t
)
213 (assert (not (slot-boundp x
'y
)))
214 (setf (slot-value x
'y
) 1)
215 (assert (= 1 (slot-value x
'y
))))
216 (let ((x (make-instance 'class-to-add-instance-slot
)))
217 (assert (slot-boundp x
'x
))
218 (assert (eq t
(slot-value x
'x
)))
219 (assert (not (slot-boundp x
'y
))))
221 ;;; b. adding an :allocation :class slot
222 (defclass class-to-add-class-slot
(dummy-ctacs) ())
223 (defmethod compute-slots ((c (eql (find-class 'class-to-add-class-slot
))))
224 (append (call-next-method)
225 (list (make-instance 'standard-effective-slot-definition
227 :allocation
:class
))))
228 (defclass dummy-ctacs
() ((x :allocation
:class
)))
229 (finalize-inheritance (find-class 'class-to-add-class-slot
))
230 (assert (equal (mapcar #'slot-definition-allocation
231 (class-slots (find-class 'class-to-add-class-slot
)))
233 (let ((x (make-instance 'class-to-add-class-slot
)))
234 (assert (not (slot-boundp x
'x
)))
235 (setf (slot-value x
'x
) nil
)
236 (assert (not (slot-boundp x
'y
)))
237 (setf (slot-value x
'y
) 1)
238 (assert (= 1 (slot-value x
'y
))))
239 (let ((x (make-instance 'class-to-add-class-slot
)))
240 (assert (slot-boundp x
'x
))
241 (assert (eq nil
(slot-value x
'x
)))
242 (assert (slot-boundp x
'y
))
243 (assert (= 1 (slot-value x
'y
))))
244 ;;; extra paranoia: check that we haven't broken the instance-slot class
245 (let ((x (make-instance 'class-to-add-instance-slot
)))
246 (assert (slot-boundp x
'x
))
247 (assert (eq t
(slot-value x
'x
)))
248 (assert (not (slot-boundp x
'y
))))
250 ;;;; the CTOR optimization was insufficiently careful about its
251 ;;;; assumptions: firstly, it failed with a failed AVER for
252 ;;;; non-standard-allocation slots:
253 (defclass class-with-frob-slot
()
254 ((frob-slot :initarg
:frob-slot
:allocation
:frob
)))
256 (funcall (compile nil
'(lambda ()
257 (make-instance 'class-with-frob-slot
259 (sb-int:bug
(c) (error c
))
260 (error () "Probably OK: haven't implemented SLOT-BOUNDP-USING-CLASS"))
261 ;;; secondly, it failed to take account of the fact that we might wish
262 ;;; to customize (setf slot-value-using-class)
263 (defclass class-with-special-ssvuc
()
264 ((some-slot :initarg
:some-slot
)))
265 (defvar *special-ssvuc-counter
* 0)
266 (defmethod (setf slot-value-using-class
) :before
267 (new-value class
(instance class-with-special-ssvuc
) slotd
)
268 (incf *special-ssvuc-counter
*))
269 (let ((fun (compile nil
'(lambda () (make-instance 'class-with-special-ssvuc
271 (assert (= *special-ssvuc-counter
* 0))
273 (assert (= *special-ssvuc-counter
* 1))
275 (assert (= *special-ssvuc-counter
* 2)))
276 ;;; and now with the customization after running the function once
277 (defclass class-with-special-ssvuc-2
()
278 ((some-slot :initarg
:some-slot
)))
279 (defvar *special-ssvuc-counter-2
* 0)
280 (let ((fun (compile nil
'(lambda () (make-instance 'class-with-special-ssvuc-2
282 (assert (= *special-ssvuc-counter-2
* 0))
284 (assert (= *special-ssvuc-counter-2
* 0))
285 (defmethod (setf slot-value-using-class
) :before
286 (new-value class
(instance class-with-special-ssvuc-2
) slotd
)
287 (incf *special-ssvuc-counter-2
*))
289 (assert (= *special-ssvuc-counter-2
* 1)))
291 ;;; vicious metacycle detection and resolution wasn't good enough: it
292 ;;; didn't take account that the slots (and hence the slot readers)
293 ;;; might be inherited from superclasses. This example, due to Bruno
294 ;;; Haible, also tests programmatic addition of accessors.
295 (defclass auto-accessors-direct-slot-definition-class
(standard-class)
296 ((containing-class-name :initarg
:containing-class-name
)))
297 (defmethod validate-superclass
298 ((c1 auto-accessors-direct-slot-definition-class
) (c2 standard-class
))
300 (defclass auto-accessors-class
(standard-class)
302 (defmethod direct-slot-definition-class ((class auto-accessors-class
)
304 (let ((dsd-class-name (gensym)))
307 :metaclass
'auto-accessors-direct-slot-definition-class
308 :direct-superclasses
(list (find-class 'standard-direct-slot-definition
))
309 :containing-class-name
(class-name class
))
310 (eval `(defmethod initialize-instance :after
((dsd ,dsd-class-name
)
312 (when (and (null (slot-definition-readers dsd
))
313 (null (slot-definition-writers dsd
)))
314 (let* ((containing-class-name
315 (slot-value (class-of dsd
) 'containing-class-name
))
319 (symbol-name containing-class-name
)
321 (symbol-name (slot-definition-name dsd
)))
322 (symbol-package containing-class-name
))))
323 (setf (slot-definition-readers dsd
) (list accessor-name
))
324 (setf (slot-definition-writers dsd
)
325 (list (list 'setf accessor-name
)))))))
326 (find-class dsd-class-name
)))
327 (defmethod validate-superclass ((c1 auto-accessors-class
) (c2 standard-class
))
329 (defclass testclass15
()
330 ((x :initarg
:x
) (y))
331 (:metaclass auto-accessors-class
))
332 (let ((inst (make-instance 'testclass15
:x
12)))
333 (assert (equal (list (testclass15-x inst
) (setf (testclass15-y inst
) 13))
336 ;;; bug reported by Bruno Haible on sbcl-devel 2004-11-17: incorrect
337 ;;; handling of multiple values for non-standard slot-options
339 (defclass option-slot-definition
(sb-mop:standard-direct-slot-definition
)
340 ((option :accessor sl-option
:initarg
:my-option
)))
341 (defclass option-slot-class
(standard-class)
343 (defmethod sb-mop:direct-slot-definition-class
344 ((c option-slot-class
) &rest args
)
345 (declare (ignore args
))
346 (find-class 'option-slot-definition
))
347 (defmethod sb-mop:validate-superclass
348 ((c1 option-slot-class
) (c2 standard-class
))
350 (eval '(defclass test-multiple-slot-option-bug
()
351 ((x :my-option bar
:my-option baz
))
352 (:metaclass option-slot-class
)))
353 (assert (null (set-difference
355 (sl-option (first (sb-mop:class-direct-slots
356 (find-class 'test-multiple-slot-option-bug
))))))))
358 ;;; bug reported by Bruno Haibel on sbcl-devel 2004-11-19: AMOP requires
359 ;;; that CLASS-PROTOYPE signals an error if the class is not yet finalized
360 (defclass prototype-not-finalized-sub
(prototype-not-finalized-super) ())
361 (multiple-value-bind (val err
)
362 (ignore-errors (sb-mop:class-prototype
(find-class 'prototype-not-finalized-super
)))
364 (assert (typep err
'error
)))
367 (find-method (fdefinition 'sb-mop
:allocate-instance
) () '(built-in-class))
368 (dolist (class-name '(fixnum bignum symbol
))
369 (let ((class (find-class class-name
)))
370 (multiple-value-bind (value error
) (ignore-errors (allocate-instance class
))
371 (assert (null value
))
372 (assert (typep error
'error
)))))
374 ;;; bug reported by David Morse: direct-subclass update protocol was broken
375 (defclass vegetable
() ())
376 (defclass tomato
(vegetable) ())
377 (assert (equal (list (find-class 'tomato
)) (sb-mop:class-direct-subclasses
(find-class 'vegetable
))))
378 (defclass tomato
() ())
379 (assert (null (sb-mop:class-direct-subclasses
(find-class 'vegetable
))))
381 ;;; bug 331: lazy creation of clos classes for defstructs
382 (defstruct bug-331-super
)
383 (defstruct (bug-331-sub (:include bug-331-super
)))
384 (let ((subs (sb-mop:class-direct-subclasses
(find-class 'bug-331-super
))))
385 (assert (= 1 (length subs
)))
386 (assert (eq (car subs
) (find-class 'bug-331-sub
))))
387 ;;; (addendum to test for #331: conditions suffered the same problem)
388 (define-condition condition-bug-331-super
() ())
389 (define-condition condition-bug-331-sub
(condition-bug-331-super) ())
390 (let ((subs (sb-mop:class-direct-subclasses
391 (find-class 'condition-bug-331-super
))))
392 (assert (= 1 (length subs
)))
393 (assert (eq (car subs
) (find-class 'condition-bug-331-sub
))))
394 ;;; (addendum to the addendum: the fix for this revealed breakage in
395 ;;; REINITIALIZE-INSTANCE)
396 (define-condition condition-bug-331a
() ((slot331a :reader slot331a
)))
397 (reinitialize-instance (find-class 'condition-bug-331a
))
398 (let* ((gf #'slot331a
)
399 (methods (sb-mop:generic-function-methods gf
)))
400 (assert (= (length methods
) 1))
401 (assert (eq (car methods
)
402 (find-method #'slot331a nil
403 (list (find-class 'condition-bug-331a
))))))
405 ;;; detection of multiple class options in defclass, reported by Bruno Haible
406 (defclass option-class
(standard-class)
407 ((option :accessor cl-option
:initarg
:my-option
)))
408 (defmethod sb-pcl:validate-superclass
((c1 option-class
) (c2 standard-class
))
410 (multiple-value-bind (result error
)
411 (ignore-errors (eval '(defclass option-class-instance
()
415 (:metaclass option-class
))))
416 (assert (not result
))
419 ;;; class as :metaclass
421 (sb-mop:ensure-class-using-class
422 nil
'class-as-metaclass-test
423 :metaclass
(find-class 'standard-class
)
424 :name
'class-as-metaclass-test
425 :direct-superclasses
(list (find-class 'standard-object
)))
428 ;;; COMPUTE-DEFAULT-INITARGS protocol mismatch reported by Bruno
430 (defparameter *extra-initarg-value
* 'extra
)
431 (defclass custom-default-initargs-class
(standard-class)
433 (defmethod compute-default-initargs ((class custom-default-initargs-class
))
434 (let ((original-default-initargs
437 (mapcar #'class-direct-default-initargs
438 (class-precedence-list class
)))
441 (cons (list ':extra
'*extra-initarg-value
* #'(lambda () *extra-initarg-value
*))
442 (remove ':extra original-default-initargs
:key
#'car
))))
443 (defmethod validate-superclass ((c1 custom-default-initargs-class
)
446 (defclass extra-initarg
()
447 ((slot :initarg
:extra
))
448 (:metaclass custom-default-initargs-class
))
449 (assert (eq (slot-value (make-instance 'extra-initarg
) 'slot
) 'extra
))
451 ;;; STANDARD-CLASS valid as a superclass for FUNCALLABLE-STANDARD-CLASS
452 (defclass standard-class-for-fsc
()
453 ((scforfsc-slot :initarg
:scforfsc-slot
:accessor scforfsc-slot
)))
454 (defvar *standard-class-for-fsc
*
455 (make-instance 'standard-class-for-fsc
:scforfsc-slot
1))
456 (defclass fsc-with-standard-class-superclass
457 (standard-class-for-fsc funcallable-standard-object
)
458 ((fsc-slot :initarg
:fsc-slot
:accessor fsc-slot
))
459 (:metaclass funcallable-standard-class
))
461 (make-instance 'fsc-with-standard-class-superclass
464 (assert (= (scforfsc-slot *standard-class-for-fsc
*) 1))
465 (assert (= (scforfsc-slot *fsc
/scs
*) 2))
466 (assert (= (fsc-slot *fsc
/scs
*) 3))
467 (assert (subtypep 'fsc-with-standard-class-superclass
'function
))
468 (assert (not (subtypep 'standard-class-for-fsc
'function
)))
470 ;;; also check that our sanity check for functionness is good
471 (assert (raises-error?
473 (defclass bad-standard-class
(funcallable-standard-object)
475 (:metaclass standard-class
))
476 (make-instance 'bad-standard-class
))))
477 (assert (raises-error?
479 (defclass bad-funcallable-standard-class
(standard-object)
481 (:metaclass funcallable-standard-class
))
482 (make-instance 'bad-funcallable-standard-class
))))
484 ;;; we should be able to make classes with silly names
485 (make-instance 'standard-class
:name
3)
487 (reinitialize-instance (find-class 'foo
) :name
'(a b
))
489 ;;; classes (including anonymous ones) and eql-specializers should be
490 ;;; allowed to be specializers.
491 (defvar *anonymous-class
*
492 (make-instance 'standard-class
493 :direct-superclasses
(list (find-class 'standard-object
))))
494 (defvar *object-of-anonymous-class
*
495 (make-instance *anonymous-class
*))
496 (eval `(defmethod method-on-anonymous-class ((obj ,*anonymous-class
*)) 41))
497 (assert (eql (method-on-anonymous-class *object-of-anonymous-class
*) 41))
498 (eval `(defmethod method-on-anonymous-class
499 ((obj ,(intern-eql-specializer *object-of-anonymous-class
*)))
501 (assert (eql (method-on-anonymous-class *object-of-anonymous-class
*) 42))
503 ;;; accessors can cause early finalization, which caused confusion in
504 ;;; the system, leading to uncompileable TYPEP problems.
505 (defclass funcallable-class-for-typep
()
506 ((some-slot-with-accessor :accessor some-slot-with-accessor
))
507 (:metaclass funcallable-standard-class
))
508 (compile nil
'(lambda (x) (typep x
'funcallable-class-for-typep
)))
510 ;;; even anonymous classes should be valid types
511 (let* ((class1 (make-instance 'standard-class
:direct-superclasses
(list (find-class 'standard-object
))))
512 (class2 (make-instance 'standard-class
:direct-superclasses
(list class1
))))
513 (assert (subtypep class2 class1
))
514 (assert (typep (make-instance class2
) class1
)))
516 ;;; ensure-class got its treatment of :metaclass wrong.
517 (ensure-class 'better-be-standard-class
:direct-superclasses
'(standard-object)
518 :metaclass
'standard-class
519 :metaclass
'funcallable-standard-class
)
520 (assert (eq (class-of (find-class 'better-be-standard-class
))
521 (find-class 'standard-class
)))