Debian Codegraph @

src/tests/normal-tests/metaobject-protocol.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;; Author: Juan Jose Garcia-Ripoll
5 ;;;; Author: Daniel Kochmański
6 ;;;; Created: Fri Apr 14 11:13:17 CEST 2006
7 ;;;; Contains: Metaobject Protocol tests
8
9 (in-package #:cl-test)
10
11 (suite 'mop)
12
13
14 ;; mop-001
15
16 (defun delete-class (&rest class-names)
17 ;;; do nothing. We will figure out later what to do.
18 (values))
19
20 ;;; Fixed: 14/04/2006 (juanjo)
21 ;;; Description:
22 ;;;
23 ;;; The slot definitions from some classes did not get converted.
24 ;;; Besides, metaobject CLASS had the same list for direct and effective
25 ;;; slots.
26 ;;;
27 (test mop.0001.fixup
28 (is-false
29 (block top
30 (labels ((test-class (class-object)
31 (let ((x (find-if-not #'(lambda (x)
32 (typep x 'clos:standard-direct-slot-definition))
33 (clos:class-direct-slots class-object))))
34 (when x
35 (format t "Class ~a has as direct slot ~a" class-object x)
36 (return-from top (class-name class-object))))
37 (let ((x (find-if-not #'(lambda (x)
38 (typep x 'clos:standard-effective-slot-definition))
39 (clos:class-slots class-object))))
40 (when x
41 (format t "Class ~a has as effective slot ~a" class-object x)
42 (return-from top (class-name class-object))))
43 (mapc #'test-class (clos:class-direct-subclasses class-object))))
44 (test-class (find-class 't))
45 nil))))
46
47 ;;; Date: 13/02/2006
48 ;;; From: Dan Debertin
49 ;;; Fixed: 24-02-2006 (juanjo)
50 ;;; Description:
51 ;;;
52 ;;; Subclasses of STANDARD-CLASS would not inherit all their slots
53 ;;; and thus would cause runtime errors when creating instances.
54 ;;;
55
56 (test mop.0002.metaclasses
57 (is
58 (= 3
59 (eval '(progn
60 (defclass foo-metaclass (standard-class) ())
61 (defmethod mop:validate-superclass ((c1 foo-metaclass) (c2 standard-class))
62 t)
63 (defclass faa () ((a :initform 2 :initarg :a)) (:metaclass foo-metaclass))
64 (prog1 (slot-value (make-instance 'faa :a 3) 'a)
65 (cl-test::delete-class 'foo-metaclass 'faa)))))))
66
67 ;;; Date: 02/03/2006
68 ;;; From: Pascal Costanza
69 ;;; Fixed: 07/03/2006 (juanjo)
70 ;;; Description:
71 ;;;
72 ;;; CLOS should export the symbols from the AMOP.
73 ;;;
74
75
76 (defconstant +mop-symbols+ '("DIRECT-SLOT-DEFINITION"
77 "EFFECTIVE-SLOT-DEFINITION" "EQL-SPECIALIZER" "FORWARD-REFERENCED-CLASS"
78 "FUNCALLABLE-STANDARD-CLASS" "FUNCALLABLE-STANDARD-OBJECT" "METAOBJECT"
79 "SLOT-DEFINITION" "SPECIALIZER" "STANDARD-ACCESSOR-METHOD"
80 "STANDARD-DIRECT-SLOT-DEFINITION" "STANDARD-EFFECTIVE-SLOT-DEFINITION"
81 "STANDARD-READER-METHOD" "STANDARD-SLOT-DEFINITION" "STANDARD-WRITER-METHOD"
82 "ACCESSOR-METHOD-SLOT-DEFINITION" "ADD-DEPENDENT" "ADD-DIRECT-METHOD"
83 "ADD-DIRECT-SUBCLASS" "CLASS-DEFAULT-INITARGS"
84 "CLASS-DIRECT-DEFAULT-INITARGS" "CLASS-DIRECT-SLOTS"
85 "CLASS-DIRECT-SUBCLASSES" "CLASS-DIRECT-SUPERCLASSES" "CLASS-FINALIZED-P"
86 "CLASS-PRECEDENCE-LIST" "CLASS-PROTOTYPE" "CLASS-SLOTS"
87 "COMPUTE-APPLICABLE-METHODS-USING-CLASSES" "COMPUTE-CLASS-PRECEDENCE-LIST"
88 "COMPUTE-DEFAULT-INITARGS" "COMPUTE-DISCRIMINATING-FUNCTION"
89 "COMPUTE-EFFECTIVE-METHOD" "COMPUTE-EFFECTIVE-SLOT-DEFINITION"
90 "COMPUTE-SLOTS" "DIRECT-SLOT-DEFINITION-CLASS"
91 "EFFECTIVE-SLOT-DEFINITION-CLASS" "ENSURE-CLASS" "ENSURE-CLASS-USING-CLASS"
92 "ENSURE-GENERIC-FUNCTION-USING-CLASS" "EQL-SPECIALIZER-OBJECT"
93 "EXTRACT-LAMBDA-LIST" "EXTRACT-SPECIALIZER-NAMES" "FINALIZE-INHERITANCE"
94 "FIND-METHOD-COMBINATION" "FUNCALLABLE-STANDARD-INSTANCE-ACCESS"
95 "GENERIC-FUNCTION-ARGUMENT-PRECEDENCE-ORDER"
96 "GENERIC-FUNCTION-DECLARATIONS" "GENERIC-FUNCTION-LAMBDA-LIST"
97 "GENERIC-FUNCTION-METHOD-CLASS" "GENERIC-FUNCTION-METHOD-COMBINATION"
98 "GENERIC-FUNCTION-METHODS" "GENERIC-FUNCTION-NAME" "INTERN-EQL-SPECIALIZER"
99 "MAKE-METHOD-LAMBDA" "MAP-DEPENDENTS" "METHOD-FUNCTION"
100 "METHOD-GENERIC-FUNCTION" "METHOD-LAMBDA-LIST" "METHOD-SPECIALIZERS"
101 "READER-METHOD-CLASS" "REMOVE-DEPENDENT" "REMOVE-DIRECT-METHOD"
102 "REMOVE-DIRECT-SUBCLASS" "SET-FUNCALLABLE-INSTANCE-FUNCTION"
103 "SLOT-BOUNDP-USING-CLASS" "SLOT-DEFINITION-ALLOCATION"
104 "SLOT-DEFINITION-INITARGS" "SLOT-DEFINITION-INITFORM"
105 "SLOT-DEFINITION-INITFUNCTION" "SLOT-DEFINITION-LOCATION"
106 "SLOT-DEFINITION-NAME" "SLOT-DEFINITION-READERS" "SLOT-DEFINITION-WRITERS"
107 "SLOT-DEFINITION-TYPE" "SLOT-MAKUNBOUND-USING-CLASS"
108 "SLOT-VALUE-USING-CLASS" "SPECIALIZER-DIRECT-GENERIC-FUNCTIONS"
109 "SPECIALIZER-DIRECT-METHODS" "STANDARD-INSTANCE-ACCESS" "UPDATE-DEPENDENT"
110 "VALIDATE-SUPERCLASS" "WRITER-METHOD-CLASS"))
111
112 (test mop.0003.amop-symbols
113 (is-false
114 (let ((*package* (find-package "CLOS")))
115 (remove-if #'(lambda (x)
116 (multiple-value-bind (s to)
117 (find-symbol x *package*)
118 (and s (eq to :external))))
119 +mop-symbols+))))
120
121 ;;; Date: 02/03/2006
122 ;;; From: Dank Corkill
123 ;;; Fixed: 02-03-2006 (Dan Corkill)
124 ;;; Description:
125 ;;;
126 ;;; DEFCLASS allows additional options which should be handled by the
127 ;;; metaclass.
128 ;;;
129 ;;; Description:
130 ;;;
131 ;;; Readers and writers for slot documentation.
132 ;;;
133
134 (test mop.0004.defclass-options
135 (is
136 (equal
137 '(T)
138 (eval '(let ((*aux* 5))
139 (declare (special *aux*))
140 (defclass foo-metaclass (standard-class) ())
141 (defmethod mop:validate-superclass ((c1 foo-metaclass) (c2 standard-class))
142 t)
143 (defmethod shared-initialize ((class foo-metaclass) slot-names
144 &rest initargs &key option)
145 (prog1 (call-next-method)
146 (setf *aux* option)))
147 (defclass faa ()
148 ((a :initform *aux* :initarg :a))
149 (:metaclass foo-metaclass)
150 (:option t))
151 (prog1 (slot-value (make-instance 'faa) 'a)
152 (cl-test::delete-class 'foo-metaclass 'faa)))))
153 "DEFCLASS allows additional options which should be handled by ~
154 the metaclass")
155 (is
156 (equal (eval '(let ((*aux* 5))
157 (declare (special *aux*))
158 (defclass fee ()
159 ((a :initform *aux* :initarg :a)))
160 (make-instance 'faa)
161 (setf (documentation (first (clos:class-slots (find-class 'fee))) t)
162 #1="hola")
163 (documentation (first (clos:class-slots (find-class 'fee))) t)))
164 #1#)
165 "Readers and writers for slot documentation"))
166
167 ;;; Date: 25/03/2006
168 ;;; From: Pascal Costanza
169 ;;; Fixed: 03/04/2006 (juanjo)
170 ;;; Description:
171 ;;;
172 ;;; The default slot setter methods had the first argument
173 ;;; (i.e. the new value) specialized to NIL. This makes it
174 ;;; impossible to write further specializations.
175 ;;;
176 (test mop.0005.setf-specializer
177 (defclass fee ()
178 ((a :accessor fee-a)))
179 (is
180 (equal '(t fee)
181 (mapcar #'class-name
182 (clos:method-specializers
183 (first (clos:generic-function-methods #'(setf fee-a)))))))
184 (is
185 (equal '(fee)
186 (mapcar #'class-name
187 (clos:method-specializers
188 (first (clos:generic-function-methods #'fee-a))))))
189 (delete-class 'fee))
190
191 ;;; Date: 06/04/2006
192 ;;; From: Pascal Costanza
193 ;;; Fixed: ---
194 ;;; Description:
195 ;;;
196 ;;; When a required argument in a method is not explicitely given
197 ;;; an specializer, the specializer should be T. Thus
198 ;;; (defmethod foo (a))
199 ;;; is equivalent to
200 ;;; (defmethod foo ((a t)))
201 ;;;
202 (ext:with-clean-symbols (test-method)
203 (test mop.0006.method-specializer
204 (defmethod test-method (a))
205 (is (equal
206 (mop:method-specializers
207 (first (mop:generic-function-methods #'test-method)))
208 (list (find-class t))))
209 (fmakunbound 'test-method)))
210
211 ;;; Date: 22/04/2006
212 ;;; From: M. Goffioul
213 ;;; Fixed: 23/04/2006 (juanjo)
214 ;;; Description:
215 ;;;
216 ;;; When a class inherits from two other classes which have a slot
217 ;;; with the same name, the new class should inherit the accessors
218 ;;; from both classes.
219 ;;;
220
221 (ext:with-clean-symbols (fee-1 fee-2 fee-3 c-slot-0)
222 (test mop.0007.slot-inheritance
223 (defclass fee-1 ()
224 ((slot-0 :initform 0 :reader slot-0)
225 (slot-1 :initform 1 :reader slot-1)))
226 (defclass fee-2 ()
227 ((slot-0 :initform 2 :reader slot-2)))
228 (defclass fee-3 (fee-1 fee-2)
229 ((slot-0 :initform 3 :accessor c-slot-0)))
230 (make-instance 'fee-3) ; finalizes inheritance
231 (flet ((accessors (class)
232 (list (class-name class)
233 (mapcar #'clos:slot-definition-readers (clos:class-slots class))
234 (mapcar #'clos:slot-definition-readers (clos:class-slots class)))))
235 (is (equal (accessors (find-class 'fee-1))
236 '(fee-1 ((slot-0) (slot-1)) ((slot-0) (slot-1)))))
237 (is (equal (accessors (find-class 'fee-2))
238 '(fee-2 ((slot-2)) ((slot-2)))))
239 (is (equal (accessors (find-class 'fee-3))
240 '(fee-3 ((c-slot-0 slot-0 slot-2) (slot-1))
241 ((c-slot-0 slot-0 slot-2) (slot-1)))))
242 (is (equal (mapcar #'(lambda (o)
243 (mapcar #'(lambda (method)
244 (handler-case (funcall method o)
245 (error (c) nil)))
246 '(slot-0 slot-2 c-slot-0)))
247 (mapcar #'make-instance '(fee-1 fee-2 fee-3)))
248 '((0 nil nil)
249 (nil 2 nil)
250 (3 3 3))))
251 (delete-class 'fee-1 'fee-2 'fee-3))))
252
253
254 ;;; Date: 28/04/2006
255 ;;; From: P. Costanza
256 ;;; Fixed: 05/05/2006 (P. Costanza)
257 ;;; Description:
258 ;;;
259 ;;; Option names from classes and generic functions which are not
260 ;;; in the keyword package should be quoted. This test is
261 ;;; essentially like mop.0004... because our DEFGENERIC does not
262 ;;; support non-keyword options.
263 ;;;
264 (test mop.0008.defclass-option-quote
265 (eval '(let ((*aux* 5))
266 (declare (special *aux*))
267 (defclass foo-metaclass (standard-class) ())
268 (defmethod mop:validate-superclass ((c1 foo-metaclass) (c2 standard-class))
269 t)
270 (defmethod shared-initialize ((class foo-metaclass) slot-names
271 &rest initargs &key ((cl-user::option option)))
272 (prog1 (call-next-method)
273 (setf *aux* option)))
274 (defclass faa ()
275 ((a :initform *aux* :initarg :a))
276 (:metaclass foo-metaclass)
277 (cl-user::option t))
278 (is (equal '(t)
279 (slot-value (make-instance 'faa) 'a)))
280 (cl-test::delete-class 'foo-metaclass 'faa))))
281
282
283 ;;; Date: 05/10/2006
284 ;;; From: Rick Taube
285 ;;; Fixed: 10/10/2006 (juanjo)
286 ;;; Description:
287 ;;;
288 ;;; :INITFORM arguments do not get properly expanded when the form
289 ;;; is a constant variable.
290 ;;;
291 ;;; (defclass a () ((a :initform most-positive-fixnum)))
292 ;;; (slot-value (make-instance a) 'a) => most-positive-fixnum
293 ;;;
294 (test mop.0009.defclass-initform
295 (is (equal
296 (loop for quoting in '(nil t)
297 collect
298 (loop for f in '(most-positive-fixnum #1=#.(lambda () 1) 12 "hola" :a t nil)
299 collect (prog1 (eval `(progn
300 (defclass foo ()
301 ((a :initform ,(if quoting (list 'quote f) f))))
302 (slot-value (make-instance 'foo) 'a)))
303 (cl-test::delete-class 'foo))))
304 '((#.most-positive-fixnum #1# 12 "hola" :a t nil)
305 (most-positive-fixnum #1# 12 "hola" :a t nil)))))
306
307
308 ;; Test MOP dependents
309 (defclass mop-dependent-object ()
310 ((log :initform nil :initarg :log :accessor mop-dependent-object-log)))
311
312 (defmethod clos:update-dependent ((object t) (dep mop-dependent-object) &rest initargs)
313 (push (list* object initargs) (mop-dependent-object-log dep)))
314
315 ;;; Date: 23/04/2012
316 ;;; Description:
317 ;;;
318 ;;; ADD-DEPENDENT uses pushnew
319 ;;;
320 (test mop.0010.gf-add/non-redundant
321 (is-true
322 (let* ((dep (make-instance 'mop-dependent-object))
323 l1 l2)
324 (fmakunbound 'mop-gf-add/remove-dependent)
325 (defgeneric mop-gf-add/remove-dependent (a))
326 (let ((f #'mop-gf-add/remove-dependent))
327 (clos:add-dependent f dep)
328 (setf l1 (clos::generic-function-dependents f))
329 (clos:add-dependent f dep)
330 (setf l2 (clos::generic-function-dependents f))
331 (and (eq l1 l2)
332 (equalp l1 (list dep)))))))
333
334 ;;; Date: 23/04/2012
335 ;;; Description:
336 ;;;
337 ;;; Generic functions have dependents and are activated
338 ;;;
339 (test mop.0011.gf-add/remove-dependent
340 (let* ((dep (make-instance 'mop-dependent-object))
341 l1 l2 l3 l4 l5 l6)
342 (fmakunbound 'mop-gf-add/remove-dependent)
343 (defgeneric mop-gf-add/remove-dependent (a))
344 (let ((f #'mop-gf-add/remove-dependent)
345 m1 m2)
346 ;;
347 ;; * ADD-DEPENDENT registers the object with the function
348 ;;
349 (clos:add-dependent f dep)
350 (setf l1 (clos::generic-function-dependents f))
351 ;;
352 ;; * ADD-METHOD invokes UPDATE-DEPENDENT
353 ;;
354 (defmethod mop-gf-add/remove-dependent ((a number)) (cos a))
355 (setf l2 (mop-dependent-object-log dep))
356 ;;
357 ;; * REMOVE-METHOD invokes UPDATE-DEPENDENT
358 ;;
359 (setf m1 (first (compute-applicable-methods f (list 1.0))))
360 (remove-method f m1)
361 (setf l3 (mop-dependent-object-log dep))
362 ;;
363 ;; * REMOVE-DEPENDENT eliminates all dependencies
364 ;;
365 (clos:remove-dependent f dep)
366 (setf l4 (clos::generic-function-dependents f))
367 ;;
368 ;; * ADD-METHOD invokes UPDATE-DEPENDENT but has no effect
369 ;;
370 (defmethod mop-gf-add/remove-dependent ((a symbol)) a)
371 (setf l5 (mop-dependent-object-log dep))
372 ;;
373 ;; * REMOVE-METHOD invokes UPDATE-DEPENDENT but has no effect
374 ;;
375 (setf m2 (first (compute-applicable-methods f (list 'a))))
376 (setf l6 (mop-dependent-object-log dep))
377 ;; the first call to defmethod adds two entries: one for the
378 ;; add-method and another one for a reinitialize-instance with
379 ;; the name of the function
380 (is-true (equalp l1 (list dep)))
381 (is-true (eq l2 (rest l3)))
382 (is-true (equalp l3
383 (list (list f 'remove-method m1)
384 (list f 'add-method m1)
385 (list f))))
386 (is-true (null l4))
387 (is-true (eq l5 l3))
388 (is-true (eq l6 l3)))))
389
390 ;;; Date: 23/04/2012
391 ;;; Description:
392 ;;;
393 ;;; ADD-DEPENDENT does not duplicate elements
394 ;;;
395 (test mop.0012.class-add/remove-dependent
396 (let* ((dep (make-instance 'mop-dependent-object))
397 l1 l2)
398 (when (find-class 'mop-class-add/remove-dependent nil)
399 (setf (class-name (find-class 'mop-class-add/remove-dependent)) nil))
400 (defclass mop-class-add/remove-dependent () ())
401 (let ((f (find-class 'mop-class-add/remove-dependent)))
402 (clos:add-dependent f dep)
403 (setf l1 (clos::class-dependents f))
404 (clos:add-dependent f dep)
405 (setf l2 (clos::class-dependents f))
406 (is-true
407 (and (eq l1 l2)
408 (equalp l1 (list dep)))))))
409
410 ;;; Date: 23/04/2012
411 ;;; Description:
412 ;;;
413 ;;; Standard classes have dependents and are activated
414 ;;;
415 (test mop.0013.class-add/remove-dependent
416 (let* ((dep (make-instance 'mop-dependent-object))
417 l1 l2 l3 l4 l5)
418 (when (find-class 'mop-class-add/remove-dependent nil)
419 (setf (class-name (find-class 'mop-class-add/remove-dependent)) nil))
420 (defclass mop-class-add/remove-dependent () ())
421 (let ((f (find-class 'mop-class-add/remove-dependent)))
422 ;;
423 ;; * ADD-DEPENDENT registers the object with the class
424 ;;
425 (clos:add-dependent f dep)
426 (setf l1 (clos::class-dependents f))
427 ;;
428 ;; * SHARED-INITIALIZE invokes UPDATE-DEPENDENT
429 ;;
430 (defclass mop-class-add/remove-dependent () (a))
431 (setf l2 (clos::class-dependents f))
432 (setf l3 (mop-dependent-object-log dep))
433 ;;
434 ;; * REMOVE-DEPENDENT eliminates object from list
435 ;;
436 (clos:remove-dependent f dep)
437 (setf l4 (clos::class-dependents f))
438 ;;
439 ;; * SHARED-INITIALIZE invokes UPDATE-DEPENDENT without effect
440 ;;
441 (defclass mop-class-add/remove-dependent () ())
442 (setf l5 (mop-dependent-object-log dep))
443 ;;
444 ;; the first call to defclass adds one entry with the reinitialization
445 ;; of the class both in name and list of slots
446 (is-true
447 (and (equalp l1 (list dep))
448 (eq l1 l2)
449 (equalp l3
450 (list (list f :name 'mop-class-add/remove-dependent
451 :direct-superclasses nil
452 :direct-slots '((:name a)))))
453 (null l4)
454 (eq l5 l3))))))
455
456
457 ;; Test MOP dispatch
458
459 ;;; Date: 23/04/2012
460 ;;; Description:
461 ;;;
462 ;;; COMPUTE-APPLICABLE-METHODS-USING-CLASSES works with one and
463 ;;; two methods and no EQL.
464 ;;;
465 (test mop.0014.c-a-m-u-c-two-methods
466 (fmakunbound 'mop-fn)
467 (defgeneric mop-fn (a)
468 (:method ((a number)) (cos a))
469 (:method ((a symbol)) a))
470 (let ((m1 (compute-applicable-methods #'mop-fn (list 1.0)))
471 (m2 (compute-applicable-methods #'mop-fn (list 'a))))
472 (flet ((f (class)
473 (multiple-value-list (clos:compute-applicable-methods-using-classes
474 #'mop-fn (list (find-class class))))))
475 (is-true
476 (and (equalp (f 'number) (list m1 t))
477 (equalp (f 'real) (list m1 t))
478 (equalp (f 'symbol) (list m2 t))
479 (equalp (f 'cons) '(nil t)))))))
480
481 ;;; Date: 23/04/2012
482 ;;; Description:
483 ;;;
484 ;;; COMPUTE-APPLICABLE-METHODS-USING-CLASSES fails with EQL
485 ;;; specializers when one of the specializers is covered by the
486 ;;; classes.
487 ;;;
488 (test mop.0015.-c-a-m-u-c-fails-with-eql
489 (fmakunbound 'mop-fn)
490 (defgeneric mop-fn (a)
491 (:method ((a (eql 1))) 1)
492 (:method ((a (eql 'a))) 2)
493 (:method ((a float)) 3))
494 (let ((m1 (compute-applicable-methods #'mop-fn (list 1)))
495 (m2 (compute-applicable-methods #'mop-fn (list 'a)))
496 (m3 (compute-applicable-methods #'mop-fn (list 1.0))))
497 (flet ((f (class)
498 (multiple-value-list (clos:compute-applicable-methods-using-classes
499 #'mop-fn (list (find-class class))))))
500 (is-true
501 (and (equalp (f 'integer) (list nil nil))
502 (equalp (f 'number) (list nil nil))
503 (equalp (f 'symbol) (list nil nil))
504 (equalp (f 'float) (list m3 t))
505 (= (length m1) 1)
506 (= (length m2) 1)
507 (= (length m3) 1))))))
508
509 ;;; Date: 24/04/2012
510 ;;; Description:
511 ;;;
512 ;;; COMPUTE-DISCRIMINATING-FUNCTION is invoked and honored by ECL.
513 ;;;
514 (test mop.0016.discriminator
515 (fmakunbound 'foo)
516 (defclass my-generic-function (standard-generic-function)
517 ())
518 (defmethod clos:compute-discriminating-function ((gf my-generic-function))
519 ;; We compute the invocaions of c-d-f. Note that it is invoked
520 ;; quite often -- we could probably optimize this.
521 #'(lambda (&rest args)
522 args))
523 (defgeneric foo (a)
524 (:generic-function-class my-generic-function))
525 (is (equal '(2)
526 (unwind-protect
527 (foo 2)
528 (fmakunbound 'foo)))))
529
530 ;;; Date: 24/04/2012
531 ;;; Description:
532 ;;;
533 ;;; COMPUTE-DISCRIMINATING-FUNCTION is invoked on ADD-METHOD, REMOVE-METHOD,
534 ;;; DEFGENERIC, INITIALIZE-INSTANCE and REINITIALIZE-INSTANCE acting on
535 ;;; generic functions.
536 ;;;
537 (ext:with-clean-symbols (*mop-discriminator-recomputation* foo my-generic-function)
538 (test mop.0017.discriminator-recomputation
539 (defparameter *mop-discriminator-recomputation* 0)
540 (defclass my-generic-function (standard-generic-function) ())
541 (defmethod clos:compute-discriminating-function ((gf my-generic-function))
542 ;; We compute the invocaions of c-d-f. Note that it is invoked
543 ;; quite often -- we could probably optimize this.
544 (incf *mop-discriminator-recomputation*)
545 (call-next-method))
546 (is-true
547 (and (progn
548 (setf *mop-discriminator-recomputation* 0)
549 (eval '(defgeneric foo (a)
550 (:generic-function-class my-generic-function)))
551 (plusp *mop-discriminator-recomputation* ))
552 (typep #'foo 'my-generic-function)
553 (progn
554 (setf *mop-discriminator-recomputation* 0)
555 (eval '(defmethod foo ((a number)) (print a)))
556 (plusp *mop-discriminator-recomputation*))
557 (progn
558 (setf *mop-discriminator-recomputation* 0)
559 (eval '(remove-method #'foo (first (compute-applicable-methods
560 #'foo
561 (list 1.0)))))
562 (plusp *mop-discriminator-recomputation*))))))
563
564 ;;; Date: 24/04/2012
565 ;;; Description:
566 ;;;
567 ;;; Verify ECL calls COMPUTE-APPLICABLE-METHODS-USING-CLASSES for
568 ;;; user-defined generic function classes.
569 ;;;
570 (ext:with-clean-symbols (*mop-dispatch-used* my-generic-function foo)
571 (test mop.0018.c-a-m-u-c-is-honored
572 (defparameter *mop-dispatch-used* 0)
573 (defclass my-generic-function (standard-generic-function) ())
574 (defmethod clos:compute-applicable-methods-using-classes
575 ((gf my-generic-function) classes)
576 (incf *mop-dispatch-used*)
577 (call-next-method))
578 (defgeneric foo (a)
579 (:generic-function-class my-generic-function)
580 (:method ((a number)) (cos 1.0)))
581 (is-true
582 (and (zerop *mop-dispatch-used*)
583 (progn (foo 1.0) (plusp *mop-dispatch-used*))))))
584
585 ;;; Date: 24/04/2012
586 ;;; Description:
587 ;;;
588 ;;; Verify ECL calls COMPUTE-APPLICABLE-METHODS for
589 ;;; user-defined generic function classes.
590 ;;;
591 (ext:with-clean-symbols (*mop-dispatch-used* my-generic-function foo)
592 (test mop.0019.compute-applicable-methods-is-honored
593 (defparameter *mop-dispatch-used* 0)
594 (defclass my-generic-function (standard-generic-function) ())
595 (defmethod clos:compute-applicable-methods-using-classes
596 ((gf my-generic-function) classes)
597 (incf *mop-dispatch-used*)
598 (values nil nil))
599 (defmethod compute-applicable-methods
600 ((gf my-generic-function) args)
601 (incf *mop-dispatch-used*)
602 (call-next-method))
603 (defgeneric foo (a)
604 (:generic-function-class my-generic-function)
605 (:method ((a number)) (cos 1.0)))
606 (is-true
607 (and (zerop *mop-dispatch-used*)
608 (progn (foo 1.0) (= *mop-dispatch-used* 2))))))
609
610 ;;; From: Pascal Costanza
611 ;;; Description:
612 ;;;
613 ;;; sort-applicable-methods is invoked by two methods and one
614 ;;; invocation triggers a disambiguation error:
615 ;;;
616 ;;; Condition of type: SIMPLE-ERROR
617 ;;;
618 ;;; The type specifiers #<The STANDARD-CLASS COMMON-LISP-USER::B>
619 ;;; and #<The STANDARD-CLASS COMMON-LISP-USER::A> can not be
620 ;;; disambiguated with respect to the argument specializer:
621 ;;; #<The STANDARD-CLASS STANDARD-CLASS>
622 (ext:with-clean-symbols (a b c f)
623 (defclass a () ())
624 (defclass b () ())
625 (defclass c (a b) ())
626 (defmethod f ((o a)))
627 (defmethod f ((o b)))
628 (make-instance 'c) ; finalizes inheritance
629 (test mop.0020.c-a-m-disambiguation
630 (finishes
631 (clos:compute-applicable-methods-using-classes
632 #'f (list (find-class 'c))))))
633
634 ;;; Bug #46
635 ;;;
636 ;;; Reported 2016-05-30
637 ;;;
638 ;;; Description: DEFGENERIC doesn't create methods on same pass as creating
639 ;;; generics.
640 (test mop.0021.ensure-generic
641 (is (progn (fmakunbound 'mop.0021.ensure-generic.fun)
642 (defun mop.0021.ensure-generic.fun () 'hi)
643 (with-temporary-file (input-file "
644 (fmakunbound 'mop.0021.ensure-generic.fun)
645 (defmethod mop.0021.ensure-generic.fun () (print 'bye))
646 ")
647 (compile-file input-file)))))
648
649 ;;; Bug #426
650 ;;;
651 ;;; CLOS::*CLOS-BOOTED* wasn't set correctly because of gray-streams bootstrap
652 ;;; (fundamental-stream subclasses stream).
653 (ext:with-clean-symbols (foo)
654 (test mop.0022.subclass-builtin
655 (signals error (defclass foo (stream) ()))))
656
657 ;;; Bug #425
658 ;;;
659 ;;; update-dependent didn't invalidate initarg caches when new methods were
660 ;;; defined.
661 (ext:with-clean-symbols (foo)
662 (test mop.0023.update-dependent
663 (defclass foo () ())
664 (finishes (make-instance 'foo))
665 (signals error (make-instance 'foo :test "hi"))
666 (defmethod initialize-instance :after ((obj foo) &key test) test)
667 (finishes (make-instance 'foo :test "hi"))
668 (signals error (make-instance 'foo :test "hi" :bam "bye"))))
669
670 ;;; Ensure that forward-referenced classes work as expected.
671 (ext:with-clean-symbols (foo1 foo2)
672 (test mop.0024.frc
673 (finishes (defclass foo1 (foo2) ()))
674 (signals error (make-instance 'foo1))
675 (finishes (defclass foo2 () ()))
676 (finishes (make-instance 'foo1))))
677
678 ;;; Date 2020-04-05
679 ;;; URL: https://gitlab.com/embeddable-common-lisp/ecl/-/issues/568
680 ;;; Fixed: 557541f3
681 ;;; Description
682 ;;;
683 ;;; Finalizing inheritance of a standard-effective-slot-definition
684 ;;; lead to the infinite recursion by making all its instances
685 ;;; obsolete (including its own slots!).
686 (test mop.0025.xxx
687 (finishes
688 (clos:finalize-inheritance
689 (find-class 'clos:standard-effective-slot-definition))))
690
691 ;;; Date 2020-04-07
692 ;;; URL: https://gitlab.com/embeddable-common-lisp/ecl/-/merge_requests/194
693 ;;; Description
694 ;;;
695 ;;; Some implementations extend the lambda list congruency for
696 ;;; generic functions and methods by allowing methods not accept
697 ;;; keys mentioned in the generic function lambda list if it has
698 ;;; &allow-other-keys. We follow the suit for compatibility with a
699 ;;; bad code.
700 (ext:with-clean-symbols (foo bar)
701 (test mop.0026.congruent-ll
702 (defgeneric foo (arg1 &key arg2 &allow-other-keys))
703 (finishes (defmethod foo ((arg1 integer) &key arg3)))
704 (finishes (defmethod foo ((arg1 string) &key arg3 &allow-other-keys)))
705 (defgeneric bar (arg1 &key arg2))
706 (finishes (defmethod bar ((arg1 string) &key arg3 &allow-other-keys)))
707 (signals error (defmethod bar ((arg1 integer) &key arg3)))))
708
709 ;;; Date 2020-04-10
710 ;;; URL: https://gitlab.com/embeddable-common-lisp/ecl/-/issues/572
711 ;;; Fixed: 6ea255fe
712 ;;; Description
713 ;;;
714 ;;; Fix in 557541f3 (see the test mop.0025.xxx) introduced a
715 ;;; regression for custom compute-class-precedence-list methods.
716 ;;; This issue is triggered only by a non-conforming code which
717 ;;; defines the method which returns a sequence which first
718 ;;; element *is not* the class passes as the argument. See:
719 ;;;
720 ;;; http://metamodular.com/CLOS-MOP/compute-class-precedence-list.html
721 (ext:with-clean-symbols (meta hack test-class)
722 (test mop.0027.finalize-inheritance
723 (finishes
724 (progn (defclass meta (standard-class) ())
725 (defclass hack () ())
726 (defmethod clos:validate-superclass ((class meta) (super standard-class)) t)
727 (defmethod clos:validate-superclass ((class standard-class) (super meta)) t)
728 (defmethod clos:compute-class-precedence-list ((class meta))
729 (cons (find-class 'hack) (call-next-method)))
730 (defclass test-class () () (:metaclass meta))))))
731
732 ;;; Date 2020-04-14
733 ;;; Description
734 ;;;
735 ;;; This is a local regression (never commited to the repository),
736 ;;; which signals "function class-a-c undefined" when redefining a
737 ;;; class after removing a slot.
738 (ext:with-clean-symbols (class-a class-a-b class-a-c)
739 (test mop.0028.local-regression
740 (defclass class-a ()
741 ((b :accessor class-a-b :initarg :b)
742 (c :accessor class-a-c :initarg :c)))
743 (defclass class-a ()
744 ((b :accessor class-a-b :initarg :b)))
745 (defclass class-a ()
746 ((b :accessor class-a-b :initarg :b)
747 (c :accessor class-a-c :initarg :c)))
748 (defclass class-a ()
749 ((b :accessor class-a-b :initarg :b)
750 (c :accessor class-a-c :initarg :c)))))
751
752 ;;; Date 2020-04-16
753 ;;; Description
754 ;;;
755 ;;; When finializing the standard-method metaclass we had
756 ;;; encountered a metastability issue when regenerating the
757 ;;; standard accessors (which implies removing them first as
758 ;;; specified in MOP), where METHOD-GENERIC-FUNCTION is invoked
759 ;;; from ADD-DIRECT-METHOD after specialization on the
760 ;;; STANDARD-METHOD is removed.
761 (test mop.0029.standard-method-metastability
762 (finishes (clos:finalize-inheritance (find-class 'standard-method))))