restart-bind.lsp 5.19 KB
Newer Older
pfdietz's avatar
pfdietz committed
1 2 3 4 5 6 7 8 9 10 11 12 13
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Fri Mar 21 22:28:53 2003
;;;; Contains: Tests for RESTART-BIND

(in-package :cl-test)

(deftest restart-bind.1
  (restart-bind () nil)
  nil)

(deftest restart-bind.2
  (restart-bind () (values)))
sds's avatar
sds committed
14

pfdietz's avatar
pfdietz committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
(deftest restart-bind.3
  (restart-bind () (values 'a 'b 'c 'd 'e 'f))
  a b c d e f)

(deftest restart-bind.4
  (block nil
    (restart-bind () (return 'good) 'bad))
  good)

(deftest restart-bind.5
  (block done
    (tagbody
     (restart-bind () (go 10) (return-from done 'bad))
     10
     (return-from done 'good)))
  good)

pfdietz's avatar
pfdietz committed
32 33 34 35 36 37 38
(deftest restart-bind.6
  (restart-bind ())
  nil)

(deftest restart-bind.7
  (block done
    (restart-bind ((foo #'(lambda () (return-from done 'good))))
sds's avatar
sds committed
39 40
                  (invoke-restart 'foo)
                  'bad))
pfdietz's avatar
pfdietz committed
41 42 43 44 45
  good)

(deftest restart-bind.8
  (block done
    (restart-bind ((foo #'(lambda () (return-from done 'good))))
sds's avatar
sds committed
46 47 48 49
                  (let ((restart (find-restart 'foo)))
                    (and (typep restart 'restart)
                         (invoke-restart restart)))
                  'bad))
pfdietz's avatar
pfdietz committed
50 51 52 53
  good)

(deftest restart-bind.9
  (restart-bind ((foo #'(lambda (a b c) (list c a b))))
sds's avatar
sds committed
54
                (invoke-restart 'foo 1 2 3))
pfdietz's avatar
pfdietz committed
55 56 57 58 59
  (3 1 2))

(deftest restart-bind.10
  (flet ((%f () (invoke-restart 'foo 'x 'y 'z)))
    (restart-bind ((foo #'(lambda (a b c) (list c a b))))
sds's avatar
sds committed
60
                  (%f)))
pfdietz's avatar
pfdietz committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  (z x y))

(deftest restart-bind.11
  (restart-bind
   ((foo #'(lambda () 'bad)))
   (restart-bind
    ((foo #'(lambda () 'good)))
    (invoke-restart 'foo)))
  good)

(deftest restart-bind.12
  (let ((*x* 'bad))
    (declare (special *x*))
    (restart-bind
     ((foo #'(lambda () (declare (special *x*)) *x*)))
     (let ((*x* 'good))
       (declare (special *x*))
       (invoke-restart 'foo))))
  good)

(deftest restart-bind.13
  (restart-bind
   ((foo #'(lambda () 'bad)))
   (flet ((%f () (invoke-restart 'foo)))
     (restart-bind
      ((foo #'(lambda () 'good)))
      (%f))))
  good)

pfdietz's avatar
pfdietz committed
90 91 92 93
(deftest restart-bind.14
  (let ((x 10) (y nil))
    (restart-bind
     ((foo #'(lambda ()
sds's avatar
sds committed
94 95 96 97 98
               (when (> x 0)
                 (push 'a y)
                 (decf x)
                 (invoke-restart 'foo))
               y)))
pfdietz's avatar
pfdietz committed
99 100 101 102 103 104 105
     (invoke-restart 'foo)))
  (a a a a a a a a a a))

(deftest restart-bind.15
  (block done
    (let ((i 0))
      (restart-bind ((foo (progn (incf i)
sds's avatar
sds committed
106 107 108
                                 #'(lambda () (return-from done i)))))
                    (invoke-restart 'foo)
                    'bad)))
pfdietz's avatar
pfdietz committed
109 110 111 112 113 114 115 116
  1)

(deftest restart-bind.16
  (let ((i 0))
    (values
     (with-output-to-string
       (s)
       (restart-bind
sds's avatar
sds committed
117 118 119 120 121
        ((foo #'(lambda () nil)
              :report-function (progn (incf i)
                                      #'(lambda (s) (format s "A report")))))
        (let ((*print-escape* nil))
          (format s "~A" (find-restart 'foo)))))
pfdietz's avatar
pfdietz committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
     i))
  "A report"
  1)

(deftest restart-bind.17
  (restart-bind
   ((foo #'(lambda () 'good))
    (foo #'(lambda () 'bad)))
   (invoke-restart 'foo))
  good)

(deftest restart-bind.18
  (restart-bind
   ((foo #'(lambda () 'good))
    (bar #'(lambda () 'bad)))
   (invoke-restart 'foo))
  good)

(deftest restart-bind.19
  (restart-bind
   ((foo #'(lambda () 'bad))
    (bar #'(lambda () 'good)))
   (invoke-restart 'bar))
  good)

;;; Using the :test-function to associate a restart with a condition

pfdietz's avatar
pfdietz committed
149 150 151
;;; This test is disabled until I figure out how to fix
;;; it.  See sbcl-devel mailing list, Oct 2005
#|
pfdietz's avatar
pfdietz committed
152 153 154 155
(deftest restart-bind.20
  (let ((c (make-condition 'error)))
    (restart-bind
     ((foo #'(lambda () 'bad)
sds's avatar
sds committed
156
           :test-function #'(lambda (c1) (not (eq c c1))))
pfdietz's avatar
pfdietz committed
157
      (foo #'(lambda () 'good)
sds's avatar
sds committed
158 159
           :test-function #'(lambda (c2) (or (null c2)
                                             (eq c c2)))))
pfdietz's avatar
pfdietz committed
160 161
     (invoke-restart (find-restart 'foo c))))
  good)
pfdietz's avatar
pfdietz committed
162
|#
pfdietz's avatar
pfdietz committed
163 164 165 166 167

(deftest restart-bind.21
  (let ((c (make-condition 'error)))
    (restart-bind
     ((foo #'(lambda () 'bad)
sds's avatar
sds committed
168
           :test-function #'(lambda (c1) nil))
pfdietz's avatar
pfdietz committed
169
      (foo #'(lambda () 'good)
sds's avatar
sds committed
170
           :test-function #'(lambda (c2) t)))
pfdietz's avatar
pfdietz committed
171 172 173 174 175
     (invoke-restart (find-restart 'foo c))))
  good)

(deftest restart-bind.22
  (let ((c (make-condition 'error))
sds's avatar
sds committed
176
        (i 0))
pfdietz's avatar
pfdietz committed
177 178 179
    (values
     (restart-bind
      ((foo #'(lambda () 'good)
sds's avatar
sds committed
180
            :test-function (progn (incf i) #'(lambda (c2) t))))
pfdietz's avatar
pfdietz committed
181 182 183 184 185 186 187 188
      (invoke-restart (find-restart 'foo c)))
     i))
  good
  1)

;;; Error tests

(deftest restart-bind.error.1
189
  (signals-error
pfdietz's avatar
pfdietz committed
190 191
   (restart-bind
    ((foo #'(lambda () t)))
192 193 194
    (invoke-restart 'foo 'a))
   program-error)
  t)
pfdietz's avatar
pfdietz committed
195 196

(deftest restart-bind.error.2
197
  (signals-error
pfdietz's avatar
pfdietz committed
198 199
   (restart-bind
    ((foo #'(lambda (x) x)))
200 201 202
    (invoke-restart 'foo))
   program-error)
  t)
pfdietz's avatar
pfdietz committed
203 204

(deftest restart-bind.error.3
205
  (signals-error
pfdietz's avatar
pfdietz committed
206 207
   (restart-bind
    ((foo #'identity))
208 209 210
    (invoke-restart 'foo))
   program-error)
  t)
pfdietz's avatar
pfdietz committed
211

212 213 214 215 216
(deftest restart-bind.23
  (restart-bind
   ((foo #'(lambda () 'good)))
   (invoke-restart-interactively 'foo))
  good)
pfdietz's avatar
pfdietz committed
217

218 219 220 221 222
(deftest restart-bind.24
  (let ((i 0))
    (values
     (restart-bind
      ((foo
sds's avatar
sds committed
223 224 225
        #'(lambda (x y z) (list z y x))
        :interactive-function (progn (incf i)
                                     #'(lambda () (list 'a 'b 'c)))))
226 227 228 229
      (invoke-restart-interactively 'foo))
     i))
  (c b a)
  1)
pfdietz's avatar
pfdietz committed
230