fix style-warnings for condition slot-accessors used in :REPORT
[sbcl.git] / tests / condition.impure.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; While most of SBCL is derived from the CMU CL system, the test
5 ;;;; files (like this one) were written from scratch after the fork
6 ;;;; from CMU CL.
7 ;;;;
8 ;;;; This software is in the public domain and is provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
10 ;;;; more information.
11
12 (cl:in-package :cl-user)
13
14 (use-package :test-util)
15
16 ;;; Bug from CLOCC.
17 (defpackage :p1
18   (:use :cl)
19   (:export #:code #:code-msg #:%code-msg))
20 (in-package :p1)
21 (define-condition code ()
22   ((msg :reader code-msg :reader %code-msg :initarg :msg)))
23
24 (defpackage :p2
25   (:use :cl :p1))
26 (in-package :p2)
27 (define-condition code1 (code)
28   ((msg :accessor code-msg :initarg :msg)))
29
30 (let ((code (make-condition 'code :msg 1)))
31   (assert (typep code 'code))
32   (assert (eql (code-msg code) 1))
33   (assert (eql (%code-msg code) 1)))
34 (let ((code (make-condition 'code1 :msg 1)))
35   (assert (typep code 'code))
36   (assert (eql (code-msg code) 1))
37   (assert (eql (%code-msg code) 1))
38   (setf (code-msg code) 2)
39   (assert (eql (code-msg code) 2))
40   (assert (eql (%code-msg code) 1)))
41
42 (in-package :cl-user)
43
44 ;;; Check that initializing the condition class metaobject doesn't create
45 ;;; any instances. Reported by Marco Baringer on sbcl-devel Mon, 05 Jul 2004.
46 (defvar *condition-count* 0)
47 (define-condition counted-condition () ((slot :initform (incf *condition-count*))))
48 (defmethod frob-counted-condition ((x counted-condition)) x)
49 (assert (= 0 *condition-count*))
50 (assert (typep (sb-mop:class-prototype (find-class 'counted-condition))
51                '(and condition counted-condition)))
52
53 (define-condition picky-condition () ())
54 (restart-case
55     (handler-case
56         (error 'picky-condition)
57       (picky-condition (c)
58         (assert (eq (car (compute-restarts)) (car (compute-restarts c))))))
59   (picky-restart ()
60     :report "Do nothing."
61     :test (lambda (c)
62             (typep c '(or null picky-condition)))
63     'ok))
64
65 ;;; adapted from Helmut Eller on cmucl-imp
66 (assert (eq 'it
67             (restart-case
68                 (handler-case
69                     (error 'picky-condition)
70                   (picky-condition (c)
71                     (invoke-restart (find-restart 'give-it c))))
72               (give-it ()
73                 :test (lambda (c) (typep c 'picky-condition))
74                 'it))))
75
76 ;;; In sbcl-1.0.9, a condition derived from CL:STREAM-ERROR (or
77 ;;; CL:READER-ERROR or or CL:PARSE-ERROR) didn't inherit a usable
78 ;;; PRINT-OBJECT method --- the PRINT-OBJECT code implicitly assumed
79 ;;; that CL:STREAM-ERROR was like a SIMPLE-CONDITION, with args and
80 ;;; format control, which seems to be a preANSIism.
81 ;;;
82 ;;; (The spec for DEFINE-CONDITION says that if :REPORT is not
83 ;;; supplied, "information about how to report this type of condition
84 ;;; is inherited from the PARENT-TYPE." The spec doesn't explicitly
85 ;;; forbid the inherited printer from trying to read slots which
86 ;;; aren't portably specified for the condition, but it doesn't seem
87 ;;; reasonable for the inherited printer to do so. It does seem
88 ;;; reasonable for app code to derive a new condition from
89 ;;; CL:READER-ERROR (perhaps for an error in a readmacro) or
90 ;;; CL:PARSE-ERROR (perhaps for an error in an operator
91 ;;; READ-MY-FAVORITE-DATA-STRUCTURE) or CL:STREAM-ERROR (dunno why
92 ;;; offhand, but perhaps for some Gray-stream-ish reason), not define
93 ;;; a :REPORT method for its new condition, and expect to inherit from
94 ;;; the application's printer all the cruft required for describing
95 ;;; the location of the error in the input.)
96 (define-condition my-stream-error-1-0-9 (stream-error) ())
97 (define-condition parse-foo-error-1-0-9 (parse-error) ())
98 (define-condition read-bar-error-1-0-9 (reader-error) ())
99 (let (;; instances created initializing all the slots specified in
100       ;; ANSI CL
101       (parse-foo-error-1-0-9 (make-condition 'parse-foo-error-1-0-9
102                                              :stream *standard-input*))
103       (read-foo-error-1-0-9 (make-condition 'read-bar-error-1-0-9
104                                             :stream *standard-input*))
105       (my-stream-error-1-0-9 (make-condition 'my-stream-error-1-0-9
106                                              :stream *standard-input*)))
107   ;; should be printable
108   (dolist (c (list
109               my-stream-error-1-0-9
110               parse-foo-error-1-0-9
111               read-foo-error-1-0-9))
112     ;; whether escaped or not
113     (dolist (*print-escape* '(nil t))
114       (write c :stream (make-string-output-stream)))))
115
116 ;;; Reported by Michael Weber: restart computation in :TEST-FUNCTION used to
117 ;;; cause infinite recursion.
118 (defun restart-test-finds-restarts ()
119   (restart-bind
120       ((bar (lambda ()
121               (return-from restart-test-finds-restarts 42))
122          :test-function
123          (lambda (condition)
124            (find-restart 'qux))))
125     (when (find-restart 'bar)
126       (invoke-restart 'bar))))
127 (assert (not (restart-test-finds-restarts)))
128
129 (with-test (:name :bug-896379)
130   (let ((*evaluator-mode* :compile))
131     (handler-bind ((style-warning #'error))
132       (let ((reader (gensym "READER"))
133             (name (gensym "FOO-ERROR")))
134         (eval `(define-condition ,name (error)
135                  ((slot :initarg :slot :reader ,reader))
136                  (:report (lambda (c stream)
137                             (format stream "Oops: ~S" (,reader c))))))))))