1 ;;;; reader.test --- Reader test. -*- coding: iso-8859-1; mode: scheme -*-
3 ;;;; Copyright (C) 1999, 2001, 2002, 2003, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 ;;;; Jim Blandy <jimb@red-bean.com>
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 (define-module (test-suite reader)
21 :use-module (srfi srfi-1)
22 :use-module (test-suite lib))
26 (cons 'read-error "end of file$"))
27 (define exception:unexpected-rparen
28 (cons 'read-error "unexpected \")\"$"))
29 (define exception:unterminated-block-comment
30 (cons 'read-error "unterminated `#. \\.\\.\\. .#' comment$"))
31 (define exception:unknown-character-name
32 (cons 'read-error "unknown character name .*$"))
33 (define exception:unknown-sharp-object
34 (cons 'read-error "Unknown # object: .*$"))
35 (define exception:eof-in-string
36 (cons 'read-error "end of file in string constant$"))
37 (define exception:illegal-escape
38 (cons 'read-error "illegal character in escape sequence: .*$"))
39 (define exception:missing-expression
40 (cons 'read-error "no expression after #;"))
43 (define (read-string s)
44 (with-fluids ((%default-port-encoding #f))
45 (with-input-from-string s (lambda () (read)))))
47 (define (with-read-options opts thunk)
48 (let ((saved-options (read-options)))
54 (read-options saved-options)))))
57 (with-test-prefix "reading"
59 (equal? (read-string "0") 0))
61 (equal? (read-string "1++i") '1++i))
63 (equal? (read-string "1+i+i") '1+i+i))
65 (equal? (read-string "1+e10000i") '1+e10000i))
67 ;; At one time the arg list for "Unknown # object: ~S" didn't make it out
68 ;; of read.c. Check that `format' can be applied to this error.
69 (pass-if "error message on bad #"
73 ;; oops, this # is supposed to be unrecognised
75 (lambda (key subr message args rest)
76 (apply format #f message args)
77 ;; message and args are ok
80 (pass-if "block comment"
82 (read-string "(+ 1 #! this is a\ncomment !# 2 3)")))
84 (pass-if "block comment finishing s-exp"
86 (read-string "(+ 2 #! a comment\n!#\n) ")))
88 (pass-if "R6RS/SRFI-30 block comment"
90 (read-string "(+ 1 #| this is a\ncomment |# 2 3)")))
92 (pass-if "R6RS/SRFI-30 nested block comment"
94 (read-string "(a b c #| d #| e |# f |#)")))
96 (pass-if "R6RS/SRFI-30 block comment syntax overridden"
97 ;; To be compatible with 1.8 and earlier, we should be able to override
99 (let ((rhp read-hash-procedures))
102 (read-hash-extend #\| (lambda args 'not)))
104 (fold (lambda (x y result)
105 (and result (eq? x y)))
107 (read-string "(this is #| a comment)")
108 `(this is not a comment)))
110 (set! read-hash-procedures rhp)))))
112 (pass-if "unprintable symbol"
113 ;; The reader tolerates unprintable characters for symbols.
114 (equal? (string->symbol "\x01\x02\x03")
115 (read-string "\x01\x02\x03")))
117 (pass-if "CR recognized as a token delimiter"
118 ;; In 1.8.3, character 0x0d was not recognized as a delimiter.
119 (equal? (read-string "one\x0dtwo") 'one))
121 (pass-if "returned strings are mutable"
122 ;; Per R5RS Section 3.4, "Storage Model", `read' is supposed to return
124 (let ((str (with-input-from-string "\"hello, world\"" read)))
125 (string-set! str 0 #\H)
126 (string=? str "Hello, world"))))
129 (pass-if-exception "radix passed to number->string can't be zero"
130 exception:out-of-range
131 (number->string 10 0))
132 (pass-if-exception "radix passed to number->string can't be one either"
133 exception:out-of-range
134 (number->string 10 1))
137 (with-test-prefix "mismatching parentheses"
138 (pass-if-exception "opening parenthesis"
141 (pass-if-exception "closing parenthesis following mismatched opening"
142 exception:unexpected-rparen
144 (pass-if-exception "opening vector parenthesis"
147 (pass-if-exception "closing parenthesis following mismatched vector opening"
148 exception:unexpected-rparen
152 (with-test-prefix "exceptions"
154 ;; Reader exceptions: although they are not documented, they may be relied
155 ;; on by some programs, hence these tests.
157 (pass-if-exception "unterminated block comment"
158 exception:unterminated-block-comment
159 (read-string "(+ 1 #! comment\n..."))
160 (pass-if-exception "R6RS/SRFI-30 unterminated nested block comment"
161 exception:unterminated-block-comment
162 (read-string "(foo #| bar #| |#)"))
163 (pass-if-exception "unknown character name"
164 exception:unknown-character-name
165 (read-string "#\\theunknowncharacter"))
166 (pass-if-exception "unknown sharp object"
167 exception:unknown-sharp-object
169 (pass-if-exception "eof in string"
170 exception:eof-in-string
171 (read-string "\"the string that never ends"))
172 (pass-if-exception "illegal escape in string"
173 exception:illegal-escape
174 (read-string "\"some string \\???\"")))
177 (with-test-prefix "read-options"
178 (pass-if "case-sensitive"
179 (not (eq? 'guile 'GuiLe)))
180 (pass-if "case-insensitive"
182 (with-read-options '(case-insensitive)
184 (read-string "GuiLe")))))
185 (pass-if "prefix keywords"
187 (with-read-options '(keywords prefix case-insensitive)
189 (read-string ":KeyWord")))))
190 (pass-if "prefix non-keywords"
191 (symbol? (with-read-options '(keywords prefix)
193 (read-string "srfi88-keyword:")))))
194 (pass-if "postfix keywords"
196 (with-read-options '(keywords postfix)
198 (read-string "keyword:")))))
199 (pass-if "long postfix keywords"
200 (eq? #:keyword0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
201 (with-read-options '(keywords postfix)
203 (read-string "keyword0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789:")))))
204 (pass-if "`:' is not a postfix keyword (per SRFI-88)"
206 (with-read-options '(keywords postfix)
208 (read-string ":")))))
209 (pass-if "no positions"
210 (let ((sexp (with-read-options '()
212 (read-string "(+ 1 2 3)")))))
213 (and (not (source-property sexp 'line))
214 (not (source-property sexp 'column)))))
216 (let ((sexp (with-read-options '(positions)
218 (read-string "(+ 1 2 3)")))))
219 (and (equal? (source-property sexp 'line) 0)
220 (equal? (source-property sexp 'column) 0))))
221 (pass-if "positions on quote"
222 (let ((sexp (with-read-options '(positions)
224 (read-string "'abcde")))))
225 (and (equal? (source-property sexp 'line) 0)
226 (equal? (source-property sexp 'column) 0))))
227 (with-test-prefix "r6rs-hex-escapes"
228 (pass-if-exception "non-hex char in two-digit hex-escape"
229 exception:illegal-escape
230 (with-read-options '(r6rs-hex-escapes)
232 (with-input-from-string "\"\\x0g;\"" read))))
234 (pass-if-exception "non-hex char in four-digit hex-escape"
235 exception:illegal-escape
236 (with-read-options '(r6rs-hex-escapes)
238 (with-input-from-string "\"\\x000g;\"" read))))
240 (pass-if-exception "non-hex char in six-digit hex-escape"
241 exception:illegal-escape
242 (with-read-options '(r6rs-hex-escapes)
244 (with-input-from-string "\"\\x00000g;\"" read))))
246 (pass-if-exception "no semicolon at termination of one-digit hex-escape"
247 exception:illegal-escape
248 (with-read-options '(r6rs-hex-escapes)
250 (with-input-from-string "\"\\x0\"" read))))
252 (pass-if-exception "no semicolon at termination of three-digit hex-escape"
253 exception:illegal-escape
254 (with-read-options '(r6rs-hex-escapes)
256 (with-input-from-string "\"\\x000\"" read))))
258 (pass-if "two-digit hex escape"
260 (with-read-options '(r6rs-hex-escapes)
262 (string-ref (with-input-from-string "\"--\\xff;--\"" read) 2)))
263 (integer->char #xff)))
265 (pass-if "four-digit hex escape"
267 (with-read-options '(r6rs-hex-escapes)
269 (string-ref (with-input-from-string "\"--\\x0100;--\"" read) 2)))
270 (integer->char #x0100)))
272 (pass-if "six-digit hex escape"
274 (with-read-options '(r6rs-hex-escapes)
276 (string-ref (with-input-from-string "\"--\\x010300;--\"" read) 2)))
277 (integer->char #x010300)))
279 (pass-if "escaped characters match non-escaped ASCII characters"
281 (with-read-options '(r6rs-hex-escapes)
283 (with-input-from-string "\"\\x41;\\x0042;\\x000043;\"" read)))
286 (pass-if "write R6RS string escapes"
287 (let* ((s1 (apply string
288 (map integer->char '(#x8 ; backspace
293 (s2 (with-read-options '(r6rs-hex-escapes)
295 (with-output-to-string
296 (lambda () (write s1)))))))
299 (list #\" #\\ #\x #\8 #\; #\space #\0 #\@ #\"))))
301 (pass-if "display R6RS string escapes"
303 (with-read-options '(r6rs-hex-escapes)
305 (let ((pt (open-output-string))
306 (s1 (apply string (map integer->char
307 '(#xFF #x100 #xFFF #x1000 #xFFFF #x10000)))))
308 (set-port-encoding! pt "ASCII")
309 (set-port-conversion-strategy! pt 'escape)
311 (get-output-string pt))))
312 "\\xff;\\x100;\\xfff;\\x1000;\\xffff;\\x10000;"))
314 (pass-if "one-digit hex escape"
315 (eqv? (with-read-options '(r6rs-hex-escapes)
317 (with-input-from-string "#\\xA" read)))
318 (integer->char #x0A)))
320 (pass-if "two-digit hex escape"
321 (eqv? (with-read-options '(r6rs-hex-escapes)
323 (with-input-from-string "#\\xFF" read)))
324 (integer->char #xFF)))
326 (pass-if "four-digit hex escape"
327 (eqv? (with-read-options '(r6rs-hex-escapes)
329 (with-input-from-string "#\\x00FF" read)))
330 (integer->char #xFF)))
332 (pass-if "eight-digit hex escape"
333 (eqv? (with-read-options '(r6rs-hex-escapes)
335 (with-input-from-string "#\\x00006587" read)))
336 (integer->char #x6587)))
337 (pass-if "write R6RS escapes"
339 (with-read-options '(r6rs-hex-escapes)
341 (with-output-to-string
343 (write (integer->char #x80))))))
348 (with-test-prefix "#;"
352 (equal? (with-input-from-string (car pair) read) (cdr pair))))
355 ("#;(10 20 30) foo" . foo)
356 ("#; (10 20 30) foo" . foo)
357 ("#;\n10\n20" . 20)))
360 (eof-object? (with-input-from-string "#;foo" read)))
362 (pass-if-exception "#;"
363 exception:missing-expression
364 (with-input-from-string "#;" read))
365 (pass-if-exception "#;("
367 (with-input-from-string "#;(" read)))
369 (with-test-prefix "#'"
373 (equal? (with-input-from-string (car pair) read) (cdr pair))))
375 '(("#'foo". (syntax foo))
376 ("#`foo" . (quasisyntax foo))
377 ("#,foo" . (unsyntax foo))
378 ("#,@foo" . (unsyntax-splicing foo)))))