handler-case.lsp 4.33 KB
Newer Older
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Mar  1 14:08:07 2003
;;;; Contains: Tests of HANDLER-CASE

6

7

pfdietz's avatar
pfdietz committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
(deftest handler-case.1
  (handler-case
   (error "an error")
   (error () t))
  t)

(deftest handler-case.2
  (handler-case
   (error "an error")
   (warning () nil)
   (error () t))
  t)

(deftest handler-case.3
  (handler-case
   (error "an error")
   (error (c) (and (typep c 'error) t))
   (error () 'bad)
   (condition () 'bad2))
  t)

(deftest handler-case.4
  (handler-case
   (error "an error")
   (warning (c) c)
   (error (c) (and (typep c 'error) t))
   (error () 'bad)
   (condition () 'bad2))
  t)

(deftest handler-case.5
  (handler-case
   (error "an error")
   (#.(find-class 'error) (c) (and (typep c 'error) t))
   (error () 'bad))
  t)

(deftest handler-case.6
  (handler-case (values)
sds's avatar
sds committed
47
                (error () nil)))
pfdietz's avatar
pfdietz committed
48 49 50 51 52

(deftest handler-case.7
  (handler-case 'foo (condition () 'bar))
  foo)

53 54 55
;;; (deftest handler-case.8
;;;  (handler-case 'foo (t () 'bar))
;;;  foo)
pfdietz's avatar
pfdietz committed
56 57 58 59 60

(deftest handler-case.9
  (handler-case (values 1 2 3 4 5 6 7 8) (condition () nil))
  1 2 3 4 5 6 7 8)

61 62 63 64 65
;;; (deftest handler-case.10
;;;  (handler-case
;;;   (error "foo")
;;;   (t () 'good))
;;;  good)
pfdietz's avatar
pfdietz committed
66 67 68

(deftest handler-case.11
  (labels ((%f () (declare (special *c*))
sds's avatar
sds committed
69 70 71 72 73 74 75 76 77 78
               (and (typep *c* 'condition) t))
           (%g ()
               (let ((*c* nil))
                 (declare (special *c*))
                 (%h)))
           (%h ()
            (handler-case
             (error "foo")
             (error (*c*) (declare (special *c*))
                    (%f)))))
pfdietz's avatar
pfdietz committed
79 80 81 82 83
    (%g))
  t)

(deftest handler-case.12
  (handler-case (error "foo")
sds's avatar
sds committed
84 85
                (nil () nil)
                (error (c) (notnot-mv (typep c 'simple-error))))
pfdietz's avatar
pfdietz committed
86 87 88 89
  t)

(deftest handler-case.13
  (handler-case (error "foo")
sds's avatar
sds committed
90
                (error (c) (values))))
pfdietz's avatar
pfdietz committed
91 92 93

(deftest handler-case.14
  (handler-case (error "foo")
sds's avatar
sds committed
94 95
                (error (c)
                       (values 1 2 3 4 5 6 7 8)))
pfdietz's avatar
pfdietz committed
96 97 98 99 100
  1 2 3 4 5 6 7 8)

(deftest handler-case.15
  (handler-case
   (handler-case (error "foo")
sds's avatar
sds committed
101
                 (warning () 'bad))
pfdietz's avatar
pfdietz committed
102 103 104 105 106 107
   (error () 'good))
  good)

(deftest handler-case.16
  (handler-case
   (handler-case (error "foo")
sds's avatar
sds committed
108
                 (error () 'good))
pfdietz's avatar
pfdietz committed
109 110 111 112 113 114 115 116
   (error () 'bad))
  good)

(deftest handler-case.17
  (let ((i 0))
    (values
     (handler-case
      (handler-case (error "foo")
sds's avatar
sds committed
117
                    (error () (incf i) (error "bar")))
pfdietz's avatar
pfdietz committed
118 119 120 121 122 123 124 125 126
      (error () 'good))
     i))
  good 1)

(deftest handler-case.18
  (let ((i 0))
    (values
     (handler-case
      (handler-case (error "foo")
sds's avatar
sds committed
127
                    (error (c) (incf i) (error c)))
pfdietz's avatar
pfdietz committed
128 129 130 131 132 133 134 135
      (error () 'good))
     i))
  good 1)

(deftest handler-case.19
  (handler-case
   (error "foo")
   (error (c)
sds's avatar
sds committed
136 137 138 139 140
          ;; Test that declarations can go here
          (declare (optimize (safety 3)))
          (declare (type condition c))
          (declare (ignore c))
          t))
pfdietz's avatar
pfdietz committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  t)

(deftest handler-case.20
  (handler-case
   10
   (:no-error (x) (+ x 3)))
  13)

(deftest handler-case.21
  (handler-case
   (values)
   (:no-error () 'foo))
  foo)

(deftest handler-case.22
  (handler-case
   (values 1 2 3 4 5)
   (:no-error (a b c d e) (list e d c b a)))
  (5 4 3 2 1))

(deftest handler-case.23
162 163 164 165
  (signals-error
   (handler-case (values 1 2) (:no-error (x) x))
   program-error)
  t)
pfdietz's avatar
pfdietz committed
166 167

(deftest handler-case.24
168 169 170 171
  (signals-error
   (handler-case (values) (:no-error (x) x))
   program-error)
  t)
pfdietz's avatar
pfdietz committed
172 173 174 175 176 177 178 179 180 181

(deftest handler-case.25
  (handler-case
   (handler-case
    (values)
    (error () 'bad)
    (:no-error () (error "foo")))
   (error () 'good))
  good)

182 183 184 185 186
(deftest handler-case.26
  (handler-case
   (values 1 'a 1.0)
   (error () 'bad)
   (:no-error (a b c)
sds's avatar
sds committed
187 188 189 190 191 192
              ;; Test that declarations can go here
              (declare (type integer a))
              (declare (type symbol b))
              (declare (type number c))
              (declare (ignore a c))
              b))
193 194 195 196 197
  a)

(deftest handler-case.27
  (handler-case (error "foo") (error ()))
  nil)
198 199 200 201

(deftest handler-case.28
  (handler-case (error "foo") (error () (declare (optimize speed))))
  nil)
202 203 204 205 206 207 208 209

;;; Free declaration scope

(deftest handler-case.29
  (let ((x :bad))
    (declare (special x))
    (let ((x :good))
      (handler-case nil
sds's avatar
sds committed
210 211 212
                    (:no-error (z &aux (y x))
                               (declare (special x) (ignore z))
                               y))))
213
  :good)