read.lsp 3.14 KB
Newer Older
pfdietz's avatar
pfdietz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Fri Dec 31 07:52:06 2004
;;;; Contains: Tests of READ

(in-package :cl-test)

;;; Input stream designators

(deftest read.1
  (block done
    (with-input-from-string
     (is "1 2 3")
     (with-output-to-string
       (os)
       (with-open-stream
sds's avatar
sds committed
17 18
        (*terminal-io* (make-two-way-stream is os))
        (return-from done (read t))))))
pfdietz's avatar
pfdietz committed
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 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  1)

(deftest read.2
  (with-input-from-string
   (*standard-input* "1 2 3")
   (read nil))
  1)

(deftest read.3
  (with-input-from-string
   (*standard-input* "1 2 3")
   (read))
  1)

(deftest read.4
  (with-input-from-string
   (s "1 2 3")
   (read s))
  1)

;;; eof handling

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

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

(deftest read.7
  (with-input-from-string (s "1") (read s))
  1)

(deftest read.8
  (let ((*package* (find-package "CL-TEST")))
    (with-input-from-string (s "X") (read s)))
  |X|)

(deftest read.9
  (with-input-from-string (s "1.2") (read s))
  1.2)

(deftest read.10
  (with-input-from-string (s "1.0s0") (read s))
  1.0s0)

(deftest read.11
  (with-input-from-string (s "1.0f0") (read s))
  1.0f0)

(deftest read.12
  (with-input-from-string (s "1.0d0") (read s))
  1.0d0)

(deftest read.13
  (with-input-from-string (s "1.0l0") (read s))
  1.0l0)

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

(deftest read.15
  (with-input-from-string (s "(1 2 3)") (read s))
  (1 2 3))

;;; Throwing away whitespace chars

(deftest read.16
  (with-standard-io-syntax
   (with-input-from-string
    (s ":ABC X")
    (assert (eq (read s) :|ABC|))
    (read-char s)))
  #\X)

(deftest read.17
  (with-standard-io-syntax
   (with-input-from-string
    (s ":ABC  X")
    (assert (eq (read s) :|ABC|))
    (read-char s)))
  #\Space)

(deftest read.18
  (with-standard-io-syntax
   (with-input-from-string
    (s ":ABC(")
    (assert (eq (read s) :|ABC|))
    (read-char s)))
  #\()

;;; eof value

(deftest read.19
  (with-input-from-string
   (s "")
   (read s nil 'foo))
sds's avatar
sds committed
118
  foo)
pfdietz's avatar
pfdietz committed
119 120 121 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 149 150 151 152 153 154 155 156 157 158 159 160 161

;;; Error tests

(deftest read.error.1
  (signals-error (with-input-from-string (s "") (read s)) end-of-file)
  t)

(deftest read.error.2
  (signals-error (with-input-from-string (s "") (read s)) stream-error)
  t)

(deftest read.error.3
  (signals-error (with-input-from-string (s "") (read s t)) stream-error)
  t)

(deftest read.error.4
  (signals-error (with-input-from-string (s "(") (read s nil)) end-of-file)
  t)

(deftest read.error.5
  (signals-error (with-input-from-string (s "(") (read s t)) end-of-file)
  t)

(deftest read.error.6
  (signals-error (with-input-from-string (s "#(") (read s t)) end-of-file)
  t)

(deftest read.error.7
  (signals-error (with-input-from-string (s "#S(") (read s t)) end-of-file)
  t)

;;; Note -- cannot easily test calls with RECURSIVE-P set to T
;;; These have to be done from reader macro functions so that READ is not
;;; called without having any requisite dynamic environment created
;;; around the call.

(deftest read.error.8
  (signals-error
   (with-input-from-string
    (s "1 2 3")
    (read s nil nil nil nil))
   program-error)
  t)