read-char.lsp 2.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sun Jan 18 08:53:56 2004
;;;; Contains: Tests of READ-CHAR

(in-package :cl-test)

(deftest read-char.1
  (with-input-from-string
   (*standard-input* "a")
   (read-char))
  #\a)

(deftest read-char.2
  (with-input-from-string
   (*standard-input* "abc")
   (values
    (read-char)
    (read-char)
    (read-char)))
  #\a #\b #\c)

(when (code-char 0)
  (deftest read-char.3
    (with-input-from-string
     (*standard-input* (concatenate 'string
sds's avatar
sds committed
27 28 29
                                    "a"
                                    (string (code-char 0))
                                    "b"))
30 31 32 33 34
     (values
      (read-char)
      (read-char)
      (read-char)))
    #\a #.(code-char 0) #\b))
pfdietz's avatar
pfdietz committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

(deftest read-char.4
  (with-input-from-string
   (s "abc")
   (values
    (read-char s)
    (read-char s)
    (read-char s)))
  #\a #\b #\c)

(deftest read-char.5
  (with-input-from-string
   (s "")
   (read-char s nil))
  nil)

(deftest read-char.6
  (with-input-from-string
   (s "")
   (read-char s nil 'foo))
  foo)

(deftest read-char.7
  (with-input-from-string
   (s "abc")
   (values
    (read-char s nil nil)
    (read-char s nil nil)
    (read-char s nil nil)))
  #\a #\b #\c)

(deftest read-char.8
  (with-input-from-string
   (s "abc")
   (values
    (read-char s nil t)
    (read-char s nil t)
    (read-char s nil t)))
  #\a #\b #\c)

75 76 77
(deftest read-char.9
  (with-input-from-string
   (is "!?*")
pfdietz's avatar
pfdietz committed
78
   (let ((*terminal-io* (make-two-way-stream is (make-string-output-stream))))
79 80 81 82 83 84 85 86 87 88
     (read-char t)))
  #\!)

(deftest read-char.10
  (with-input-from-string
   (*standard-input* "345")
   (read-char nil))
  #\3)


pfdietz's avatar
pfdietz committed
89 90 91 92 93 94 95 96 97 98 99
;;; Error tests

(deftest read-char.error.1
  (signals-error
   (with-input-from-string
    (s "abc")
    (read-char s nil nil nil nil))
   program-error)
  t)

(deftest read-char.error.2
100
  (signals-error-always
pfdietz's avatar
pfdietz committed
101 102 103 104
   (with-input-from-string
    (s "")
    (read-char s))
   end-of-file)
105
  t t)
pfdietz's avatar
pfdietz committed
106 107

(deftest read-char.error.3
108
  (signals-error-always
pfdietz's avatar
pfdietz committed
109 110 111 112
   (with-input-from-string
    (s "")
    (read-char s t))
   end-of-file)
113
  t t)
pfdietz's avatar
pfdietz committed
114 115

(deftest read-char.error.4
116
  (signals-error-always
pfdietz's avatar
pfdietz committed
117 118 119 120
   (with-input-from-string
    (s "")
    (read-char s t t))
   end-of-file)
121
  t t)