read-from-string.lsp 6.76 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Jan  1 14:50:09 2005
;;;; Contains: Tests of READ-FROM-STRING

(in-package :cl-test)

(deftest read-from-string.1
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s "123")
     (let ((vals (multiple-value-list (read-from-string s))))
       (assert (= (length vals) 2))
       (assert (eql (first vals) 123))
       (assert (member (second vals) '(3 4))))))
  nil)

(deftest read-from-string.2
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s "XYZ   ")
     (let ((vals (multiple-value-list (read-from-string s))))
       (assert (equal vals '(|XYZ| 4))))))
  nil)

(deftest read-from-string.3
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s "(1 2 3)X")
     (let ((vals (multiple-value-list (read-from-string s))))
       (assert (equal vals '((1 2 3) 7))))))
  nil)

(deftest read-from-string.4
  (do-special-strings
     (s "")
     (let ((vals (multiple-value-list (read-from-string s nil :good))))
       (assert (= (length vals) 2))
       (assert (equal (first vals) :good))
       (assert (member (second vals) '(0 1)))))
  nil)

(deftest read-from-string.5
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s "71235")
     (let ((vals (multiple-value-list
sds's avatar
sds committed
48
                  (read-from-string s t nil :start 1 :end 4))))
49
50
51
52
53
54
55
56
       (assert (equal vals '(123 4))))))
  nil)

(deftest read-from-string.6
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s "7123  ")
     (let ((vals (multiple-value-list
sds's avatar
sds committed
57
                  (read-from-string s t nil :start 1))))
58
59
60
61
62
63
64
65
66
       (assert (equal vals '(123 5))))))
  nil)


(deftest read-from-string.7
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s "7123  ")
     (let ((vals (multiple-value-list
sds's avatar
sds committed
67
                  (read-from-string s t nil :end 4))))
68
69
70
       (assert (equal vals '(7123 4))))))
  nil)

pfdietz's avatar
pfdietz committed
71
(deftest read-from-string.8
72
73
74
75
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s "7123")
     (let ((vals (multiple-value-list
sds's avatar
sds committed
76
                  (read-from-string s nil 'foo :start 2 :end 2))))
77
78
79
       (assert (equal vals '(foo 2))))))
  nil)

pfdietz's avatar
pfdietz committed
80
(deftest read-from-string.9
81
82
83
84
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s "123  ")
     (let ((vals (multiple-value-list
sds's avatar
sds committed
85
                  (read-from-string s t nil :preserve-whitespace t))))
86
87
88
       (assert (equal vals '(123 3))))))
  nil)

pfdietz's avatar
pfdietz committed
89
(deftest read-from-string.10
90
91
92
93
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s (concatenate 'string "( )" (string #\Newline)))
     (let ((vals (multiple-value-list
sds's avatar
sds committed
94
                  (read-from-string s t nil :preserve-whitespace t))))
95
96
97
98
99
       (assert (equal vals '(nil 3))))))
  nil)

;;; Multiple keywords

pfdietz's avatar
pfdietz committed
100
(deftest read-from-string.11
101
102
103
104
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s "7123  ")
     (let ((vals (multiple-value-list
sds's avatar
sds committed
105
                  (read-from-string s t nil :start 1 :start 2))))
106
107
108
       (assert (equal vals '(123 5))))))
  nil)

pfdietz's avatar
pfdietz committed
109
(deftest read-from-string.12
110
111
112
113
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s "7123  ")
     (let ((vals (multiple-value-list
sds's avatar
sds committed
114
                  (read-from-string s t nil :end 4 :end 2))))
115
116
117
       (assert (equal vals '(7123 4))))))
  nil)

pfdietz's avatar
pfdietz committed
118
(deftest read-from-string.13
119
120
121
122
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s (concatenate 'string "( )" (string #\Newline)))
     (let ((vals (multiple-value-list
sds's avatar
sds committed
123
124
                  (read-from-string s t nil :preserve-whitespace t
                                    :preserve-whitespace nil))))
125
126
127
128
129
       (assert (equal vals '(nil 3))))))
  nil)

;;; Allow other keys

pfdietz's avatar
pfdietz committed
130
(deftest read-from-string.14
131
132
133
134
135
  (with-standard-io-syntax
   (let ((*package* (find-package :cl-test)))
     (do-special-strings
      (s "abc   ")
      (let ((vals (multiple-value-list
sds's avatar
sds committed
136
137
                   (read-from-string s t nil :allow-other-keys nil))))
        (assert (equal vals '(|ABC| 4)) (vals) "VALS is ~A" vals)))))
138
139
  nil)

pfdietz's avatar
pfdietz committed
140
(deftest read-from-string.15
141
142
143
144
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s "123   ")
     (let ((vals (multiple-value-list
sds's avatar
sds committed
145
                  (read-from-string s t nil :foo 'bar :allow-other-keys t))))
146
147
148
       (assert (equal vals '(123 4)) (vals) "VALS is ~A" vals))))
  nil)

pfdietz's avatar
pfdietz committed
149
(deftest read-from-string.16
150
151
152
153
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s "123   ")
     (let ((vals (multiple-value-list
sds's avatar
sds committed
154
155
                  (read-from-string s t nil :allow-other-keys t
                                    :allow-other-keys nil :foo 'bar))))
156
157
158
159
160
       (assert (equal vals '(123 4)) (vals) "VALS is ~A" vals))))
  nil)

;;; default for :end

pfdietz's avatar
pfdietz committed
161
(deftest read-from-string.17
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  (let ((*package* (find-package :cl-test)))
    (do-special-strings
     (s "XYZ   ")
     (let ((vals (multiple-value-list (read-from-string s t nil :end nil))))
       (assert (equal vals '(|XYZ| 4))))))
  nil)

;;; TODO  Add tests for reading from strings containing non-base characters

;;; Error tests

(deftest read-from-string.error.1
  (signals-error (read-from-string "") error)
  t)

(deftest read-from-string.error.2
  (signals-error (read-from-string "(A B ") error)
  t)

(deftest read-from-string.error.3
  (signals-error (read-from-string "" t) error)
  t)

(deftest read-from-string.error.4
  (signals-error (read-from-string "" t nil) error)
  t)

(deftest read-from-string.error.5
  (signals-error (read-from-string "(A B " nil) error)
  t)

(deftest read-from-string.error.6
  (signals-error (read-from-string "(A B " t) error)
  t)

(deftest read-from-string.error.7
  (signals-error (read-from-string "123" t nil :start 0 :end 0) error)
  t)

(deftest read-from-string.error.8
  (signals-error (read-from-string) program-error)
  t)

(deftest read-from-string.error.9
  (signals-error (read-from-string "A" nil t :bad-keyword t) program-error)
  t)


(deftest read-from-string.error.10
  (signals-error (read-from-string "A" nil t
sds's avatar
sds committed
212
213
214
                                   :bad-keyword t
                                   :allow-other-keys nil)
                 program-error)
215
216
217
218
  t)

(deftest read-from-string.error.11
  (signals-error (read-from-string "A" nil t
sds's avatar
sds committed
219
220
221
222
                                   :bad-keyword t
                                   :allow-other-keys nil
                                   :allow-other-keys t)
                 program-error)
223
224
225
226
  t)

(deftest read-from-string.error.12
  (signals-error (read-from-string "A" nil t
sds's avatar
sds committed
227
228
229
230
                                   :allow-other-keys nil
                                   :allow-other-keys t
                                   :bad-keyword t)
                 program-error)
231
232
233
234
  t)

(deftest read-from-string.error.13
  (signals-error (read-from-string "A" nil t :start)
sds's avatar
sds committed
235
                 program-error)
236
237
238
  t)