2
;;;; Author: Paul Dietz
3
;;;; Created: Mon May 12 21:19:36 2003
4
;;;; Contains: Tests of MAKE-INSTANCE
8
;;; MAKE-INSTANCE is used in many other tests as well
10
(deftest make-instance.error.1
11
(signals-error (make-instance) program-error)
14
(defclass make-instance-class-01 ()
15
((a :initarg :a) (b :initarg :b)))
17
(deftest make-instance.error.2
18
(signals-error (make-instance 'make-instance-class-01 :a)
22
(deftest make-instance.error.3
23
(handler-case (progn (eval '(make-instance 'make-instance-class-01 :z 1))
28
(deftest make-instance.error.4
29
(handler-case (progn (eval '(make-instance
30
(find-class 'make-instance-class-01)
36
(deftest make-instance.error.5
37
(signals-error (let () (make-instance) nil)
41
;; Definitions of methods
43
(defmethod make-instance ((x make-instance-class-01)
44
&rest initargs &key &allow-other-keys)
47
(deftest make-instance.1
48
(make-instance (make-instance 'make-instance-class-01))
51
(deftest make-instance.2
52
(make-instance (make-instance 'make-instance-class-01) :a 1 :b 2)
56
(when *can-define-metaclasses*
58
(defclass make-instance-class-02 ()
60
(:metaclass substandard-class))
62
(defmethod make-instance ((class (eql (find-class 'make-instance-class-02)))
63
&rest initargs &key (x nil) (y nil) (z nil)
65
(declare (ignore initargs))
66
(let ((obj (allocate-instance class)))
67
(setf (slot-value obj 'a) x
69
(slot-value obj 'c) z)
72
(deftest make-instance.3
73
(let ((obj (make-instance 'make-instance-class-02)))
75
(eqt (class-of obj) (find-class 'make-instance-class-02))
81
(deftest make-instance.4
82
(let ((obj (make-instance 'make-instance-class-02 :z 10 :y 45 :x 'd)))
84
(eqt (class-of obj) (find-class 'make-instance-class-02))
91
(deftest make-instance.5
92
(let ((obj (make-instance (find-class 'make-instance-class-02) :y 'g)))
94
(eqt (class-of obj) (find-class 'make-instance-class-02))
100
(deftest make-instance.6
101
(eq (make-instance 'make-instance-class-02)
102
(make-instance 'make-instance-class-02))
105
;; Customization of make-instance
107
(defclass make-instance-class-03 ()
108
((a :initform 1) (b :initarg :b) c)
109
(:metaclass substandard-class))
111
(defmethod make-instance ((class (eql (find-class 'make-instance-class-03)))
113
&key (x nil x-p) (y nil y-p) (z nil z-p)
115
(declare (ignore initargs))
116
(let ((obj (allocate-instance (find-class 'make-instance-class-03))))
117
(when x-p (setf (slot-value obj 'a) x))
118
(when y-p (setf (slot-value obj 'b) y))
119
(when z-p (setf (slot-value obj 'c) z))
122
(deftest make-instance.7
123
(let ((obj (make-instance 'make-instance-class-03)))
126
(find-class 'make-instance-class-03))
127
(map-slot-boundp* obj '(a b c))))
130
(deftest make-instance.8
131
(let* ((class (find-class 'make-instance-class-03))
132
(obj (make-instance class :b 10)))
134
(eqt (class-of obj) class)
135
(map-slot-boundp* obj '(a b c))))
138
(deftest make-instance.9
139
(let* ((class (find-class 'make-instance-class-03))
140
(obj (make-instance class :x 'g :z 'i :y 'k :foo t :x 'bad)))
142
(eqt (class-of obj) class)
143
(map-slot-boundp* obj '(a b c))
144
(map-slot-value obj '(a b c))))
147
;; After method combination
149
(defparameter *make-instance-class-04-var* 0)
151
(defclass make-instance-class-04 ()
152
((a :initform *make-instance-class-04-var*))
153
(:metaclass substandard-class))
155
(defmethod make-instance :after
156
((class (eql (find-class 'make-instance-class-04)))
157
&rest initargs &key &allow-other-keys)
158
(declare (ignore initargs))
159
(incf *make-instance-class-04-var* 10))
161
(deftest make-instance.10
162
(let* ((*make-instance-class-04-var* 0)
163
(obj (make-instance 'make-instance-class-04)))
166
*make-instance-class-04-var*))
169
;; Around method combination
171
(defclass make-instance-class-05 ()
172
((a :initarg :a) (b :initarg :b :initform 'foo) c)
173
(:metaclass substandard-class))
175
(defmethod make-instance :around
176
((class (eql (find-class 'make-instance-class-05)))
177
&rest initargs &key &allow-other-keys)
178
(declare (ignore initargs))
179
(let ((obj (call-next-method)))
180
(setf (slot-value obj 'c) 'bar)
183
(deftest make-instance.11
184
(let ((obj (make-instance 'make-instance-class-05)))
186
(map-slot-boundp* obj '(a b c))
187
(map-slot-value obj '(b c))))
193
;;; Order of argument evaluation
195
(deftest make-instance.order.1
197
(obj (make-instance 'make-instance-class-01
199
:b (setf y (incf i)))))
201
(map-slot-value obj '(a b))
205
(deftest make-instance.order.2
207
(obj (make-instance 'make-instance-class-01
211
:a (setf w (incf i)))))
213
(map-slot-value obj '(a b))
217
(deftest make-instance.order.3
218
(let* ((i 0) u x y z w
219
(obj (make-instance (prog1 'make-instance-class-01
224
:a (setf w (incf i)))))
226
(map-slot-value obj '(a b))