1.0.11.3: belatedly recognizing STREAM-ERROR and PARSE-ERROR printing is OK
[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 ;;; Bug from CLOCC.
15 (defpackage :p1
16   (:use :cl)
17   (:export #:code #:code-msg #:%code-msg))
18 (in-package :p1)
19 (define-condition code ()
20   ((msg :reader code-msg :reader %code-msg :initarg :msg)))
21
22 (defpackage :p2
23   (:use :cl :p1))
24 (in-package :p2)
25 (define-condition code1 (code)
26   ((msg :accessor code-msg :initarg :msg)))
27
28 (let ((code (make-condition 'code :msg 1)))
29   (assert (typep code 'code))
30   (assert (eql (code-msg code) 1))
31   (assert (eql (%code-msg code) 1)))
32 (let ((code (make-condition 'code1 :msg 1)))
33   (assert (typep code 'code))
34   (assert (eql (code-msg code) 1))
35   (assert (eql (%code-msg code) 1))
36   (setf (code-msg code) 2)
37   (assert (eql (code-msg code) 2))
38   (assert (eql (%code-msg code) 1)))
39
40 ;;; Check that initializing the condition class metaobject doesn't create
41 ;;; any instances. Reported by Marco Baringer on sbcl-devel Mon, 05 Jul 2004.
42 (defvar *condition-count* 0)
43 (define-condition counted-condition () ((slot :initform (incf *condition-count*))))
44 (defmethod frob-counted-condition ((x counted-condition)) x)
45 (assert (= 0 *condition-count*))
46 (assert (typep (sb-mop:class-prototype (find-class 'counted-condition))
47                '(and condition counted-condition)))
48
49 (define-condition picky-condition () ())
50 (restart-case
51     (handler-case
52         (error 'picky-condition)
53       (picky-condition (c)
54         (assert (eq (car (compute-restarts)) (car (compute-restarts c))))))
55   (picky-restart ()
56     :report "Do nothing."
57     :test (lambda (c)
58             (typep c '(or null picky-condition)))
59     'ok))
60
61 ;;; adapted from Helmut Eller on cmucl-imp
62 (assert (eq 'it
63             (restart-case
64                 (handler-case
65                     (error 'picky-condition)
66                   (picky-condition (c)
67                     (invoke-restart (find-restart 'give-it c))))
68               (give-it ()
69                 :test (lambda (c) (typep c 'picky-condition))
70                 'it))))
71
72 ;;; In sbcl-1.0.9, a condition derived from CL:STREAM-ERROR (or
73 ;;; CL:READER-ERROR or or CL:PARSE-ERROR) didn't inherit a usable
74 ;;; PRINT-OBJECT method --- the PRINT-OBJECT code implicitly assumed
75 ;;; that CL:STREAM-ERROR was like a SIMPLE-CONDITION, with args and
76 ;;; format control, which seems to be a preANSIism.
77 ;;;
78 ;;; (The spec for DEFINE-CONDITION says that if :REPORT is not
79 ;;; supplied, "information about how to report this type of condition
80 ;;; is inherited from the PARENT-TYPE." The spec doesn't explicitly
81 ;;; forbid the inherited printer from trying to read slots which
82 ;;; aren't portably specified for the condition, but it doesn't seem
83 ;;; reasonable for the inherited printer to do so. It does seem
84 ;;; reasonable for app code to derive a new condition from
85 ;;; CL:READER-ERROR (perhaps for an error in a readmacro) or
86 ;;; CL:PARSE-ERROR (perhaps for an error in an operator
87 ;;; READ-MY-FAVORITE-DATA-STRUCTURE) or CL:STREAM-ERROR (dunno why
88 ;;; offhand, but perhaps for some Gray-stream-ish reason), not define
89 ;;; a :REPORT method for its new condition, and expect to inherit from
90 ;;; the application's printer all the cruft required for describing
91 ;;; the location of the error in the input.)
92 (define-condition my-stream-error-1-0-9 (stream-error) ())
93 (define-condition parse-foo-error-1-0-9 (parse-error) ())
94 (define-condition read-bar-error-1-0-9 (reader-error) ())
95 (let (;; instances created initializing all the slots specified in
96       ;; ANSI CL
97       (parse-foo-error-1-0-9 (make-condition 'parse-foo-error-1-0-9
98                                              :stream *standard-input*))
99       (read-foo-error-1-0-9 (make-condition 'read-bar-error-1-0-9
100                                             :stream *standard-input*))
101       (my-stream-error-1-0-9 (make-condition 'my-stream-error-1-0-9
102                                              :stream *standard-input*)))
103   ;; should be printable
104   (dolist (c (list
105               my-stream-error-1-0-9
106               parse-foo-error-1-0-9
107               read-foo-error-1-0-9))
108     ;; whether escaped or not
109     (dolist (*print-escape* '(nil t))
110       (write c :stream (make-string-output-stream)))))