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 (assert (equal (mapcar #'slot-definition-allocation
202 (class-slots (find-class 'class-to-add-instance-slot)))
203 ;; FIXME: is the order really guaranteed?
204 '(:class :instance)))
205 (assert (typep (slot-definition-location
206 (cadr (class-slots (find-class 'class-to-add-instance-slot))))
208 #| (assert (typep (slot-definition-location (car ...)) '???)) |#
209 (let ((x (make-instance 'class-to-add-instance-slot)))
210 (assert (not (slot-boundp x 'x)))
211 (setf (slot-value x 'x) t)
212 (assert (not (slot-boundp x 'y)))
213 (setf (slot-value x 'y) 1)
214 (assert (= 1 (slot-value x 'y))))
215 (let ((x (make-instance 'class-to-add-instance-slot)))
216 (assert (slot-boundp x 'x))
217 (assert (eq t (slot-value x 'x)))
218 (assert (not (slot-boundp x 'y))))
220 ;;; b. adding an :allocation :class slot
221 (defclass class-to-add-class-slot (dummy-ctacs) ())
222 (defmethod compute-slots ((c (eql (find-class 'class-to-add-class-slot))))
223 (append (call-next-method)
224 (list (make-instance 'standard-effective-slot-definition
226 :allocation :class))))
227 (defclass dummy-ctacs () ((x :allocation :class)))
228 (assert (equal (mapcar #'slot-definition-allocation
229 (class-slots (find-class 'class-to-add-class-slot)))
231 (let ((x (make-instance 'class-to-add-class-slot)))
232 (assert (not (slot-boundp x 'x)))
233 (setf (slot-value x 'x) nil)
234 (assert (not (slot-boundp x 'y)))
235 (setf (slot-value x 'y) 1)
236 (assert (= 1 (slot-value x 'y))))
237 (let ((x (make-instance 'class-to-add-class-slot)))
238 (assert (slot-boundp x 'x))
239 (assert (eq nil (slot-value x 'x)))
240 (assert (slot-boundp x 'y))
241 (assert (= 1 (slot-value x 'y))))
242 ;;; extra paranoia: check that we haven't broken the instance-slot class
243 (let ((x (make-instance 'class-to-add-instance-slot)))
244 (assert (slot-boundp x 'x))
245 (assert (eq t (slot-value x 'x)))
246 (assert (not (slot-boundp x 'y))))
248 ;;;; the CTOR optimization was insufficiently careful about its
249 ;;;; assumptions: firstly, it failed with a failed AVER for
250 ;;;; non-standard-allocation slots:
251 (defclass class-with-frob-slot ()
252 ((frob-slot :initarg :frob-slot :allocation :frob)))
254 (funcall (compile nil '(lambda ()
255 (make-instance 'class-with-frob-slot
257 (sb-int:bug (c) (error c))
258 (error () "Probably OK: haven't implemented SLOT-BOUNDP-USING-CLASS"))
259 ;;; secondly, it failed to take account of the fact that we might wish
260 ;;; to customize (setf slot-value-using-class)
261 (defclass class-with-special-ssvuc ()
262 ((some-slot :initarg :some-slot)))
263 (defvar *special-ssvuc-counter* 0)
264 (defmethod (setf slot-value-using-class) :before
265 (new-value class (instance class-with-special-ssvuc) slotd)
266 (incf *special-ssvuc-counter*))
267 (let ((fun (compile nil '(lambda () (make-instance 'class-with-special-ssvuc
269 (assert (= *special-ssvuc-counter* 0))
271 (assert (= *special-ssvuc-counter* 1))
273 (assert (= *special-ssvuc-counter* 2)))
274 ;;; and now with the customization after running the function once
275 (defclass class-with-special-ssvuc-2 ()
276 ((some-slot :initarg :some-slot)))
277 (defvar *special-ssvuc-counter-2* 0)
278 (let ((fun (compile nil '(lambda () (make-instance 'class-with-special-ssvuc-2
280 (assert (= *special-ssvuc-counter-2* 0))
282 (assert (= *special-ssvuc-counter-2* 0))
283 (defmethod (setf slot-value-using-class) :before
284 (new-value class (instance class-with-special-ssvuc-2) slotd)
285 (incf *special-ssvuc-counter-2*))
287 (assert (= *special-ssvuc-counter-2* 1)))
289 ;;; vicious metacycle detection and resolution wasn't good enough: it
290 ;;; didn't take account that the slots (and hence the slot readers)
291 ;;; might be inherited from superclasses. This example, due to Bruno
292 ;;; Haible, also tests programmatic addition of accessors.
293 (defclass auto-accessors-direct-slot-definition-class (standard-class)
294 ((containing-class-name :initarg :containing-class-name)))
295 (defmethod validate-superclass
296 ((c1 auto-accessors-direct-slot-definition-class) (c2 standard-class))
298 (defclass auto-accessors-class (standard-class)
300 (defmethod direct-slot-definition-class ((class auto-accessors-class)
302 (let ((dsd-class-name (gensym)))
305 :metaclass 'auto-accessors-direct-slot-definition-class
306 :direct-superclasses (list (find-class 'standard-direct-slot-definition))
307 :containing-class-name (class-name class))
308 (eval `(defmethod initialize-instance :after ((dsd ,dsd-class-name)
310 (when (and (null (slot-definition-readers dsd))
311 (null (slot-definition-writers dsd)))
312 (let* ((containing-class-name
313 (slot-value (class-of dsd) 'containing-class-name))
317 (symbol-name containing-class-name)
319 (symbol-name (slot-definition-name dsd)))
320 (symbol-package containing-class-name))))
321 (setf (slot-definition-readers dsd) (list accessor-name))
322 (setf (slot-definition-writers dsd)
323 (list (list 'setf accessor-name)))))))
324 (find-class dsd-class-name)))
325 (defmethod validate-superclass ((c1 auto-accessors-class) (c2 standard-class))
327 (defclass testclass15 ()
328 ((x :initarg :x) (y))
329 (:metaclass auto-accessors-class))
330 (let ((inst (make-instance 'testclass15 :x 12)))
331 (assert (equal (list (testclass15-x inst) (setf (testclass15-y inst) 13))
334 ;;; bug reported by Bruno Haible on sbcl-devel 2004-11-17: incorrect
335 ;;; handling of multiple values for non-standard slot-options
337 (defclass option-slot-definition (sb-mop:standard-direct-slot-definition)
338 ((option :accessor sl-option :initarg :my-option)))
339 (defclass option-slot-class (standard-class)
341 (defmethod sb-mop:direct-slot-definition-class
342 ((c option-slot-class) &rest args)
343 (declare (ignore args))
344 (find-class 'option-slot-definition))
345 (defmethod sb-mop:validate-superclass
346 ((c1 option-slot-class) (c2 standard-class))
348 (eval '(defclass test-multiple-slot-option-bug ()
349 ((x :my-option bar :my-option baz))
350 (:metaclass option-slot-class)))
351 (assert (null (set-difference
353 (sl-option (first (sb-mop:class-direct-slots
354 (find-class 'test-multiple-slot-option-bug))))))))
356 ;;; bug reported by Bruno Haibel on sbcl-devel 2004-11-19: AMOP requires
357 ;;; that CLASS-PROTOYPE signals an error if the class is not yet finalized
358 (defclass prototype-not-finalized-sub (prototype-not-finalized-super) ())
359 (multiple-value-bind (val err)
360 (ignore-errors (sb-mop:class-prototype (find-class 'prototype-not-finalized-super)))
362 (assert (typep err 'error)))
365 (find-method (fdefinition 'sb-mop:allocate-instance) () '(built-in-class))
366 (dolist (class-name '(fixnum bignum symbol))
367 (let ((class (find-class class-name)))
368 (multiple-value-bind (value error) (ignore-errors (allocate-instance class))
369 (assert (null value))
370 (assert (typep error 'error)))))
372 ;;; bug reported by David Morse: direct-subclass update protocol was broken
373 (defclass vegetable () ())
374 (defclass tomato (vegetable) ())
375 (assert (equal (list (find-class 'tomato)) (sb-mop:class-direct-subclasses (find-class 'vegetable))))
376 (defclass tomato () ())
377 (assert (null (sb-mop:class-direct-subclasses (find-class 'vegetable))))
379 ;;; bug 331: lazy creation of clos classes for defstructs
380 (defstruct bug-331-super)
381 (defstruct (bug-331-sub (:include bug-331-super)))
382 (let ((subs (sb-mop:class-direct-subclasses (find-class 'bug-331-super))))
383 (assert (= 1 (length subs)))
384 (assert (eq (car subs) (find-class 'bug-331-sub))))
385 ;;; (addendum to test for #331: conditions suffered the same problem)
386 (define-condition condition-bug-331-super () ())
387 (define-condition condition-bug-331-sub (condition-bug-331-super) ())
388 (let ((subs (sb-mop:class-direct-subclasses
389 (find-class 'condition-bug-331-super))))
390 (assert (= 1 (length subs)))
391 (assert (eq (car subs) (find-class 'condition-bug-331-sub))))
392 ;;; (addendum to the addendum: the fix for this revealed breakage in
393 ;;; REINITIALIZE-INSTANCE)
394 (define-condition condition-bug-331a () ((slot331a :reader slot331a)))
395 (reinitialize-instance (find-class 'condition-bug-331a))
396 (let* ((gf #'slot331a)
397 (methods (sb-mop:generic-function-methods gf)))
398 (assert (= (length methods) 1))
399 (assert (eq (car methods)
400 (find-method #'slot331a nil
401 (list (find-class 'condition-bug-331a))))))
403 ;;; detection of multiple class options in defclass, reported by Bruno Haible
404 (defclass option-class (standard-class)
405 ((option :accessor cl-option :initarg :my-option)))
406 (defmethod sb-pcl:validate-superclass ((c1 option-class) (c2 standard-class))
408 (multiple-value-bind (result error)
409 (ignore-errors (eval '(defclass option-class-instance ()
413 (:metaclass option-class))))
414 (assert (not result))
417 ;;; class as :metaclass
419 (sb-mop:ensure-class-using-class
420 nil 'class-as-metaclass-test
421 :metaclass (find-class 'standard-class)
422 :name 'class-as-metaclass-test
423 :direct-superclasses (list (find-class 'standard-object)))
426 ;;; COMPUTE-DEFAULT-INITARGS protocol mismatch reported by Bruno
428 (defparameter *extra-initarg-value* 'extra)
429 (defclass custom-default-initargs-class (standard-class)
431 (defmethod compute-default-initargs ((class custom-default-initargs-class))
432 (let ((original-default-initargs
435 (mapcar #'class-direct-default-initargs
436 (class-precedence-list class)))
439 (cons (list ':extra '*extra-initarg-value* #'(lambda () *extra-initarg-value*))
440 (remove ':extra original-default-initargs :key #'car))))
441 (defmethod validate-superclass ((c1 custom-default-initargs-class)
444 (defclass extra-initarg ()
445 ((slot :initarg :extra))
446 (:metaclass custom-default-initargs-class))
447 (assert (eq (slot-value (make-instance 'extra-initarg) 'slot) 'extra))
449 ;;; STANDARD-CLASS valid as a superclass for FUNCALLABLE-STANDARD-CLASS
450 (defclass standard-class-for-fsc ()
451 ((scforfsc-slot :initarg :scforfsc-slot :accessor scforfsc-slot)))
452 (defvar *standard-class-for-fsc*
453 (make-instance 'standard-class-for-fsc :scforfsc-slot 1))
454 (defclass fsc-with-standard-class-superclass
455 (standard-class-for-fsc funcallable-standard-object)
456 ((fsc-slot :initarg :fsc-slot :accessor fsc-slot))
457 (:metaclass funcallable-standard-class))
459 (make-instance 'fsc-with-standard-class-superclass
462 (assert (= (scforfsc-slot *standard-class-for-fsc*) 1))
463 (assert (= (scforfsc-slot *fsc/scs*) 2))
464 (assert (= (fsc-slot *fsc/scs*) 3))
465 (assert (subtypep 'fsc-with-standard-class-superclass 'function))
466 (assert (not (subtypep 'standard-class-for-fsc 'function)))
468 ;;; also check that our sanity check for functionness is good
469 (assert (raises-error?
471 (defclass bad-standard-class (funcallable-standard-object)
473 (:metaclass standard-class))
474 (make-instance 'bad-standard-class))))
475 (assert (raises-error?
477 (defclass bad-funcallable-standard-class (standard-object)
479 (:metaclass funcallable-standard-class))
480 (make-instance 'bad-funcallable-standard-class))))