1 ;;;; miscellaneous side-effectful tests of CLOS
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.
18 ;;; It should be possible to do DEFGENERIC and DEFMETHOD referring to
19 ;;; structure types defined earlier in the file.
20 (defstruct struct-a x y)
21 (defstruct struct-b x y z)
22 (defmethod wiggle ((a struct-a))
25 (defgeneric jiggle ((arg t)))
26 (defmethod jiggle ((a struct-a))
29 (defmethod jiggle ((b struct-b))
33 (assert (= (wiggle (make-struct-a :x 6 :y 5))
34 (jiggle (make-struct-b :x 19 :y 6 :z 2))))
36 ;;; Compiling DEFGENERIC should prevent "undefined function" style
37 ;;; warnings from code within the same file.
38 (defgeneric gf-defined-in-this-file ((x number) (y number)))
39 (defun function-using-gf-defined-in-this-file (x y n)
41 (gf-defined-in-this-file x y)))
43 ;;; Until Martin Atzmueller ported Pierre Mai's CMU CL fixes in
44 ;;; sbcl-0.6.12.25, the implementation of NO-APPLICABLE-METHOD was
45 ;;; broken in such a way that the code here would signal an error.
46 (defgeneric zut-n-a-m (a b c))
47 (defmethod no-applicable-method ((zut-n-a-m (eql #'zut-n-a-m)) &rest args)
48 (format t "~&No applicable method for ZUT-N-A-M ~S, yet.~%" args))
51 ;;; bug reported and fixed by Alexey Dejneka sbcl-devel 2001-09-10:
52 ;;; This DEFGENERIC shouldn't cause an error.
53 (defgeneric ad-gf (a) (:method :around (x) x))
55 ;;; DEFGENERIC and DEFMETHOD shouldn't accept &REST when it's not
56 ;;; followed by a variable:
57 ;;; e.g. (DEFMETHOD FOO ((X T) &REST) NIL) should signal an error.
58 (eval-when (:load-toplevel :compile-toplevel :execute)
59 (defmacro expect-error (&body body)
60 `(multiple-value-bind (res condition)
61 (ignore-errors (progn ,@body))
62 (declare (ignore res))
63 (typep condition 'error))))
67 '(defmethod foo0 ((x t) &rest) nil))))
69 (assert (expect-error (defgeneric foo1 (x &rest))))
70 (assert (expect-error (defgeneric foo2 (x a &rest))))
72 (defgeneric foo3 (x &rest y))
73 (defmethod foo3 ((x t) &rest y) nil)
74 (defmethod foo4 ((x t) &key y &rest z) nil)
75 (defgeneric foo4 (x &key y &rest z))
77 (assert (expect-error (defgeneric foo5 (x &rest))))
78 (assert (expect-error (macroexpand-1 '(defmethod foo6 (x &rest)))))
80 ;;; structure-class tests setup
81 (defclass structure-class-foo1 () () (:metaclass cl:structure-class))
82 (defclass structure-class-foo2 (structure-class-foo1)
83 () (:metaclass cl:structure-class))
85 ;;; standard-class tests setup
86 (defclass standard-class-foo1 () () (:metaclass cl:standard-class))
87 (defclass standard-class-foo2 (standard-class-foo1)
88 () (:metaclass cl:standard-class))
90 (assert (typep (class-of (make-instance 'structure-class-foo1))
92 (assert (typep (make-instance 'structure-class-foo1) 'structure-class-foo1))
93 (assert (typep (make-instance 'standard-class-foo1) 'standard-class-foo1))
95 ;;; DEFGENERIC's blow-away-old-methods behavior is specified to have
96 ;;; special hacks to distinguish between defined-with-DEFGENERIC-:METHOD
97 ;;; methods and defined-with-DEFMETHOD methods, so that reLOADing
98 ;;; DEFGENERIC-containing files does the right thing instead of
99 ;;; randomly slicing your generic functions. (APD made this work
100 ;;; in sbcl-0.7.0.2.)
101 (defgeneric born-to-be-redefined (x)
102 (:method ((x integer))
104 (defmethod born-to-be-redefined ((x real))
106 (assert (eq (born-to-be-redefined 1) 'integer))
107 (defgeneric born-to-be-redefined (x))
108 (assert (eq (born-to-be-redefined 1) 'real)) ; failed until sbcl-0.7.0.2
109 (defgeneric born-to-be-redefined (x)
110 (:method ((x integer))
112 (defmethod born-to-be-redefined ((x integer))
114 (assert (eq (born-to-be-redefined 1) 'int))
115 (defgeneric born-to-be-redefined (x))
116 (assert (eq (born-to-be-redefined 1) 'int))
120 (sb-ext:quit :unix-status 104)