X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=tests%2Fmop.impure.lisp;h=01b2796b7b5b1f24665f1ee2df9a6df1a9920d18;hb=d7875c296a4988e9f27e2776237884deb1984c62;hp=287655af5ed5115e07f88c58a9706834a664be98;hpb=175c318c892b0627b36fa3c4db66f59680242204;p=sbcl.git diff --git a/tests/mop.impure.lisp b/tests/mop.impure.lisp index 287655a..01b2796 100644 --- a/tests/mop.impure.lisp +++ b/tests/mop.impure.lisp @@ -15,8 +15,10 @@ ;;;; However, this seems a good a way as any of ensuring that we have ;;;; no regressions. +(load "test-util.lisp") + (defpackage "MOP-TEST" - (:use "CL" "SB-MOP")) + (:use "CL" "SB-MOP" "ASSERTOID" "TEST-UTIL")) (in-package "MOP-TEST") @@ -198,6 +200,7 @@ :name 'y :allocation :instance)))) (defclass dummy-ctais () ((x :allocation :class))) +(finalize-inheritance (find-class 'class-to-add-instance-slot)) (assert (equal (mapcar #'slot-definition-allocation (class-slots (find-class 'class-to-add-instance-slot))) ;; FIXME: is the order really guaranteed? @@ -225,6 +228,7 @@ :name 'y :allocation :class)))) (defclass dummy-ctacs () ((x :allocation :class))) +(finalize-inheritance (find-class 'class-to-add-class-slot)) (assert (equal (mapcar #'slot-definition-allocation (class-slots (find-class 'class-to-add-class-slot))) '(:class :class))) @@ -382,6 +386,23 @@ (let ((subs (sb-mop:class-direct-subclasses (find-class 'bug-331-super)))) (assert (= 1 (length subs))) (assert (eq (car subs) (find-class 'bug-331-sub)))) +;;; (addendum to test for #331: conditions suffered the same problem) +(define-condition condition-bug-331-super () ()) +(define-condition condition-bug-331-sub (condition-bug-331-super) ()) +(let ((subs (sb-mop:class-direct-subclasses + (find-class 'condition-bug-331-super)))) + (assert (= 1 (length subs))) + (assert (eq (car subs) (find-class 'condition-bug-331-sub)))) +;;; (addendum to the addendum: the fix for this revealed breakage in +;;; REINITIALIZE-INSTANCE) +(define-condition condition-bug-331a () ((slot331a :reader slot331a))) +(reinitialize-instance (find-class 'condition-bug-331a)) +(let* ((gf #'slot331a) + (methods (sb-mop:generic-function-methods gf))) + (assert (= (length methods) 1)) + (assert (eq (car methods) + (find-method #'slot331a nil + (list (find-class 'condition-bug-331a)))))) ;;; detection of multiple class options in defclass, reported by Bruno Haible (defclass option-class (standard-class) @@ -429,4 +450,244 @@ (:metaclass custom-default-initargs-class)) (assert (eq (slot-value (make-instance 'extra-initarg) 'slot) 'extra)) +;;; STANDARD-CLASS valid as a superclass for FUNCALLABLE-STANDARD-CLASS +(defclass standard-class-for-fsc () + ((scforfsc-slot :initarg :scforfsc-slot :accessor scforfsc-slot))) +(defvar *standard-class-for-fsc* + (make-instance 'standard-class-for-fsc :scforfsc-slot 1)) +(defclass fsc-with-standard-class-superclass + (standard-class-for-fsc funcallable-standard-object) + ((fsc-slot :initarg :fsc-slot :accessor fsc-slot)) + (:metaclass funcallable-standard-class)) +(defvar *fsc/scs* + (make-instance 'fsc-with-standard-class-superclass + :scforfsc-slot 2 + :fsc-slot 3)) +(assert (= (scforfsc-slot *standard-class-for-fsc*) 1)) +(assert (= (scforfsc-slot *fsc/scs*) 2)) +(assert (= (fsc-slot *fsc/scs*) 3)) +(assert (subtypep 'fsc-with-standard-class-superclass 'function)) +(assert (not (subtypep 'standard-class-for-fsc 'function))) + +;;; also check that our sanity check for functionness is good +(assert (raises-error? + (progn + (defclass bad-standard-class (funcallable-standard-object) + () + (:metaclass standard-class)) + (make-instance 'bad-standard-class)))) +(assert (raises-error? + (progn + (defclass bad-funcallable-standard-class (standard-object) + () + (:metaclass funcallable-standard-class)) + (make-instance 'bad-funcallable-standard-class)))) + +;;; we should be able to make classes with silly names +(make-instance 'standard-class :name 3) +(defclass foo () ()) +(reinitialize-instance (find-class 'foo) :name '(a b)) + +;;; classes (including anonymous ones) and eql-specializers should be +;;; allowed to be specializers. +(defvar *anonymous-class* + (make-instance 'standard-class + :direct-superclasses (list (find-class 'standard-object)))) +(defvar *object-of-anonymous-class* + (make-instance *anonymous-class*)) +(eval `(defmethod method-on-anonymous-class ((obj ,*anonymous-class*)) 41)) +(assert (eql (method-on-anonymous-class *object-of-anonymous-class*) 41)) +(eval `(defmethod method-on-anonymous-class + ((obj ,(intern-eql-specializer *object-of-anonymous-class*))) + 42)) +(assert (eql (method-on-anonymous-class *object-of-anonymous-class*) 42)) + +;;; accessors can cause early finalization, which caused confusion in +;;; the system, leading to uncompileable TYPEP problems. +(defclass funcallable-class-for-typep () + ((some-slot-with-accessor :accessor some-slot-with-accessor)) + (:metaclass funcallable-standard-class)) +(compile nil '(lambda (x) (typep x 'funcallable-class-for-typep))) + +;;; even anonymous classes should be valid types +(let* ((class1 (make-instance 'standard-class :direct-superclasses (list (find-class 'standard-object)))) + (class2 (make-instance 'standard-class :direct-superclasses (list class1)))) + (assert (subtypep class2 class1)) + (assert (typep (make-instance class2) class1))) + +;;; ensure-class got its treatment of :metaclass wrong. +(ensure-class 'better-be-standard-class :direct-superclasses '(standard-object) + :metaclass 'standard-class + :metaclass 'funcallable-standard-class) +(assert (eq (class-of (find-class 'better-be-standard-class)) + (find-class 'standard-class))) + +;;; CLASS-SLOTS should signal an error for classes that are not yet +;;; finalized. Reported by Levente Meszaros on sbcl-devel. +(defclass has-slots-but-isnt-finalized () (a b c)) +(let ((class (find-class 'has-slots-but-isnt-finalized))) + (assert (not (sb-mop:class-finalized-p class))) + (assert (raises-error? (sb-mop:class-slots class) sb-kernel::reference-condition))) + +;;; Check that MAKE-METHOD-LAMBDA which wraps the original body doesn't +;;; break RETURN-FROM. +(defclass wrapped-generic (standard-generic-function) + () + (:metaclass sb-mop:funcallable-standard-class)) + +(defmethod sb-mop:make-method-lambda ((gf wrapped-generic) method lambda env) + (call-next-method gf method + `(lambda ,(second lambda) + (flet ((default () :default)) + ,@(cddr lambda))) + env)) + +(defgeneric wrapped (x) + (:generic-function-class wrapped-generic)) + +(defmethod wrapped ((x cons)) + (return-from wrapped (default))) + +(with-test (:name :make-method-lambda-wrapping+return-from) + (assert (eq :default (wrapped (cons t t))))) + +(with-test (:name :slow-method-is-fboundp) + (assert (fboundp '(sb-pcl::slow-method wrapped (cons)))) + (assert (eq :default (funcall #'(sb-pcl::slow-method wrapped (cons)) (list (cons t t)) nil)))) + +;;; Check that SLOT-BOUNDP-USING-CLASS doesn't confuse MAKE-INSTANCE +;;; optimizations. +(defclass sbuc-mio-test-class (standard-class) + ()) +(defmethod validate-superclass ((class sbuc-mio-test-class) + (superclass standard-class)) + t) +(defvar *sbuc-counter* 0) +(defmethod slot-boundp-using-class ((class sbuc-mio-test-class) + (object t) + (slot standard-effective-slot-definition)) + (incf *sbuc-counter*) + (call-next-method)) +(defclass sbuc-mio-test-object () + ((slot :initform 5 :accessor a-slot)) + (:metaclass sbuc-mio-test-class)) +(with-test (:name :sbuc-mio-test) + (assert (= 5 (funcall + (compile + nil + `(lambda () + (let ((object (make-instance 'sbuc-mio-test-object))) + (slot-value object 'slot))))))) + (assert (= 1 *sbuc-counter*))) + +;;; Redefining classes so that slot definition class changes. +(defclass func-slot-class (standard-class) + ()) + +(defmethod sb-mop:validate-superclass ((class func-slot-class) (super standard-class)) + t) + +(defclass func-slot-definition () + ((function :initform nil :initarg :function :reader slotd-function))) + +(defclass effective-func-slot-definition (sb-mop:standard-effective-slot-definition + func-slot-definition) + ()) + +(defclass direct-func-slot-definition (sb-mop:standard-direct-slot-definition + func-slot-definition) + ()) + +(defmethod sb-mop:slot-value-using-class ((class func-slot-class) + instance + (slotd effective-func-slot-definition)) + (funcall (slotd-function slotd) (call-next-method))) + +(defvar *func-slot*) + +(defmethod sb-mop:effective-slot-definition-class ((class func-slot-class) &key) + (if *func-slot* + (find-class 'effective-func-slot-definition) + (call-next-method))) + +(defmethod sb-mop:direct-slot-definition-class ((class func-slot-class) &key) + (find-class 'direct-func-slot-definition)) + +(defmethod sb-mop:compute-effective-slot-definition ((class func-slot-class) name dslotds) + (let* ((*func-slot* (some #'slotd-function dslotds)) + (slotd (call-next-method))) + (when *func-slot* + (setf (slot-value slotd 'function) (fdefinition *func-slot*))) + slotd)) + +(with-test (:name :class-redefinition-changes-custom-slot-type) + (eval `(defclass func-slot-object () + ((foo :initarg :foo :reader foofoo)) + (:metaclass func-slot-class))) + (let ((x (cons t t))) + (assert (eq x (foofoo (make-instance 'func-slot-object :foo x))))) + (eval `(defclass func-slot-object () + ((foo :initarg :foo :reader foofoo :function car)) + (:metaclass func-slot-class))) + (let* ((x (cons t t)) + (y (list x))) + (assert (eq x (foofoo (make-instance 'func-slot-object :foo y)))))) + +(with-test (:name :class-redefinition-changes-custom-slot-type-mio) + (eval `(defclass func-slot-object2 () + ((foo :initarg :foo :reader foofoo)) + (:metaclass func-slot-class))) + (let* ((x (cons t t)) + (y (cons x x)) + (o (make-instance 'func-slot-object2 :foo y))) + (assert (eq y (foofoo o))) + (eval `(defclass func-slot-object2 () + ((foo :initarg :foo :reader foofoo :function car)) + (:metaclass func-slot-class))) + (assert (eq x (foofoo o))))) + +(defclass class-slot-removal-test () + ((instance :initform 1) + (class :allocation :class :initform :ok))) + +(defmethod update-instance-for-redefined-class ((x class-slot-removal-test) added removed plist &rest inits) + (throw 'update-instance + (list added removed plist inits))) + +(with-test (:name :class-redefinition-removes-class-slot) + (let ((o (make-instance 'class-slot-removal-test))) + (assert (equal '(nil nil nil nil) + (catch 'update-instance + (eval `(defclass class-slot-removal-test () + ((instance :initform 2)))) + (slot-value o 'instance)))))) + +(defclass class-slot-add-test () + ((instance :initform 1))) + +(defmethod update-instance-for-redefined-class ((x class-slot-add-test) added removed plist &rest inits) + (throw 'update-instance + (list added removed plist inits))) + +(with-test (:name :class-redefinition-adds-class-slot) + (let ((o (make-instance 'class-slot-add-test))) + (assert (equal '(nil nil nil nil) + (catch 'update-instance + (eval `(defclass class-slot-add-test () + ((instance :initform 2) + (class :allocation :class :initform :ok)))) + (slot-value o 'instance)))))) + +(defgeneric definitely-a-funcallable-instance (x)) +(with-test (:name (set-funcallable-instance-function :typechecking)) + (assert (raises-error? (set-funcallable-instance-function + (lambda (y) nil) + #'definitely-a-funcallable-instance) + type-error))) + +(with-test (:name (defstruct :nil-slot-name :bug-633911)) + (defstruct nil-slot-name nil) + (let ((fun (compile nil '(lambda (x) (slot-value x 'nil))))) + (assert (= 3 (funcall fun (make-nil-slot-name :nil 3)))))) + ;;;; success