check-type.lsp 2.33 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:  Sat Feb 15 20:12:04 2003
;;;; Contains: Tests of CHECK-TYPE

(in-package :cl-test)

(deftest check-type.1
  (let ((x 'a))
    (values (check-type x symbol) x))
  nil a)

(deftest check-type.2
14
  (signals-type-error x 'a (check-type x integer))
15
  t)
pfdietz's avatar
pfdietz committed
16 17 18 19

(deftest check-type.3
  (let ((x 'a))
    (handler-bind
20
     ((type-error #'(lambda (c)
sds's avatar
sds committed
21 22 23 24
                      (assert (eql (type-error-datum c) x))
                      (assert (not (typep x (type-error-expected-type c))))
                      ;; Can we assume the expected-type is NUMBER?
                      (store-value 15 c))))
pfdietz's avatar
pfdietz committed
25 26 27 28 29 30 31 32 33 34 35
     (values (check-type x number) x)))
  nil 15)

(deftest check-type.4
  (let ((x 'a))
    (values (check-type x symbol "a symbol") x))
  nil a)

(deftest check-type.5
  (let ((x 'a))
    (handler-bind
36
     ((type-error #'(lambda (c)
sds's avatar
sds committed
37 38 39 40
                      (assert (eql (type-error-datum c) x))
                      (assert (not (typep x (type-error-expected-type c))))
                      ;; Can we assume the expected-type is STRING?
                      (store-value "abc" c))))
pfdietz's avatar
pfdietz committed
41 42 43 44 45 46
     (values (check-type x string "a string") x)))
  nil "abc")

(deftest check-type.6
  (let ((x 'a))
    (handler-bind
47
     ((type-error #'(lambda (c)
sds's avatar
sds committed
48 49 50 51
                      (assert (eql (type-error-datum c) x))
                      (assert (not (typep x (type-error-expected-type c))))
                      ;; Can we assume the expected-type is NUMBER?
                      (store-value 15 nil))))
pfdietz's avatar
pfdietz committed
52 53 54 55 56 57
     (values (check-type x number) x)))
  nil 15)

(deftest check-type.7
  (let ((x 'a))
    (handler-bind
58
     ((type-error #'(lambda (c)
sds's avatar
sds committed
59 60 61 62
                      (assert (eql (type-error-datum c) x))
                      (assert (not (typep x (type-error-expected-type c))))
                      ;; Can we assume the expected-type is NUMBER?
                      (store-value 15))))
pfdietz's avatar
pfdietz committed
63 64
     (values (check-type x number) x)))
  nil 15)
65 66 67 68 69 70 71 72 73

;;; Test that explicit calls to macroexpand in subforms
;;; are done in the correct environment

(deftest check-type.8
  (let ((x 10))
    (macrolet
     ((%m (z) z))
     (check-type (expand-in-current-env (%m x))
sds's avatar
sds committed
74
                 (integer 8 13))))
75 76 77 78 79 80 81 82 83
  nil)

(deftest check-type.9
  (let ((x 10))
    (macrolet
     ((%m (z) z))
     (check-type x (integer 9 12) (expand-in-current-env (%m "Foo!")))))
  nil)