Test the interaction of GOOPS objects with `struct-{ref,set!}'.
[bpt/guile.git] / test-suite / tests / reader.test
CommitLineData
7337d56d
LC
1;;;; reader.test --- Exercise the reader. -*- Scheme -*-
2;;;;
3;;;; Copyright (C) 1999, 2001, 2002, 2003, 2007 Free Software Foundation, Inc.
4;;;; Jim Blandy <jimb@red-bean.com>
5;;;;
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 2.1 of the License, or (at your option) any later version.
10;;;;
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.
15;;;;
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
19
20(define-module (test-suite reader)
21 :use-module (test-suite lib))
22
0c76ebbd 23
ef9709da 24(define exception:eof
ba1b2226 25 (cons 'read-error "end of file$"))
ef9709da 26(define exception:unexpected-rparen
ba1b2226 27 (cons 'read-error "unexpected \")\"$"))
7337d56d
LC
28(define exception:unterminated-block-comment
29 (cons 'read-error "unterminated `#! ... !#' comment$"))
30(define exception:unknown-character-name
31 (cons 'read-error "unknown character name .*$"))
32(define exception:unknown-sharp-object
33 (cons 'read-error "Unknown # object: .*$"))
34(define exception:eof-in-string
35 (cons 'read-error "end of file in string constant$"))
36(define exception:illegal-escape
37 (cons 'read-error "illegal character in escape sequence: .*$"))
38
ef9709da 39
6b4113af
DH
40(define (read-string s)
41 (with-input-from-string s (lambda () (read))))
0c76ebbd 42
7337d56d
LC
43(define (with-read-options opts thunk)
44 (let ((saved-options (read-options)))
45 (dynamic-wind
46 (lambda ()
47 (read-options opts))
48 thunk
49 (lambda ()
50 (read-options saved-options)))))
51
52\f
6b4113af
DH
53(with-test-prefix "reading"
54 (pass-if "0"
55 (equal? (read-string "0") 0))
56 (pass-if "1++i"
57 (equal? (read-string "1++i") '1++i))
58 (pass-if "1+i+i"
59 (equal? (read-string "1+i+i") '1+i+i))
60 (pass-if "1+e10000i"
b7d22e03
KR
61 (equal? (read-string "1+e10000i") '1+e10000i))
62
63 ;; At one time the arg list for "Unknown # object: ~S" didn't make it out
64 ;; of read.c. Check that `format' can be applied to this error.
65 (pass-if "error message on bad #"
66 (catch #t
67 (lambda ()
68 (read-string "#ZZZ")
69 ;; oops, this # is supposed to be unrecognised
70 #f)
71 (lambda (key subr message args rest)
72 (apply format #f message args)
73 ;; message and args are ok
7337d56d
LC
74 #t)))
75
76 (pass-if "block comment"
77 (equal? '(+ 1 2 3)
78 (read-string "(+ 1 #! this is a\ncomment !# 2 3)")))
06974184 79
454866e0
LC
80 (pass-if "block comment finishing s-exp"
81 (equal? '(+ 2)
82 (read-string "(+ 2 #! a comment\n!#\n) ")))
83
7337d56d
LC
84 (pass-if "unprintable symbol"
85 ;; The reader tolerates unprintable characters for symbols.
86 (equal? (string->symbol "\001\002\003")
d41668fa
LC
87 (read-string "\001\002\003")))
88
89 (pass-if "CR recognized as a token delimiter"
90 ;; In 1.8.3, character 0x0d was not recognized as a delimiter.
91 (equal? (read-string "one\x0dtwo") 'one)))
7337d56d
LC
92
93\f
6b4113af
DH
94(pass-if-exception "radix passed to number->string can't be zero"
95 exception:out-of-range
96 (number->string 10 0))
97(pass-if-exception "radix passed to number->string can't be one either"
98 exception:out-of-range
99 (number->string 10 1))
ef9709da 100
7337d56d 101\f
ef9709da
DH
102(with-test-prefix "mismatching parentheses"
103 (pass-if-exception "opening parenthesis"
104 exception:eof
105 (read-string "("))
106 (pass-if-exception "closing parenthesis following mismatched opening"
107 exception:unexpected-rparen
108 (read-string ")"))
109 (pass-if-exception "opening vector parenthesis"
110 exception:eof
111 (read-string "#("))
112 (pass-if-exception "closing parenthesis following mismatched vector opening"
113 exception:unexpected-rparen
114 (read-string ")")))
7337d56d
LC
115
116\f
117(with-test-prefix "exceptions"
118
119 ;; Reader exceptions: although they are not documented, they may be relied
120 ;; on by some programs, hence these tests.
121
122 (pass-if-exception "unterminated block comment"
123 exception:unterminated-block-comment
124 (read-string "(+ 1 #! comment\n..."))
125 (pass-if-exception "unknown character name"
126 exception:unknown-character-name
127 (read-string "#\\theunknowncharacter"))
128 (pass-if-exception "unknown sharp object"
129 exception:unknown-sharp-object
130 (read-string "#?"))
131 (pass-if-exception "eof in string"
132 exception:eof-in-string
133 (read-string "\"the string that never ends"))
134 (pass-if-exception "illegal escape in string"
135 exception:illegal-escape
136 (read-string "\"some string \\???\"")))
137
138\f
139(with-test-prefix "read-options"
140 (pass-if "case-sensitive"
141 (not (eq? 'guile 'GuiLe)))
142 (pass-if "case-insensitive"
143 (eq? 'guile
144 (with-read-options '(case-insensitive)
145 (lambda ()
146 (read-string "GuiLe")))))
147 (pass-if "prefix keywords"
148 (eq? #:keyword
149 (with-read-options '(keywords prefix case-insensitive)
150 (lambda ()
151 (read-string ":KeyWord")))))
152 (pass-if "no positions"
153 (let ((sexp (with-read-options '()
154 (lambda ()
155 (read-string "(+ 1 2 3)")))))
156 (and (not (source-property sexp 'line))
157 (not (source-property sexp 'column)))))
158 (pass-if "positions"
159 (let ((sexp (with-read-options '(positions)
160 (lambda ()
161 (read-string "(+ 1 2 3)")))))
492faee1
LC
162 (and (equal? (source-property sexp 'line) 0)
163 (equal? (source-property sexp 'column) 0))))
164 (pass-if "positions on quote"
165 (let ((sexp (with-read-options '(positions)
166 (lambda ()
167 (read-string "'abcde")))))
7337d56d
LC
168 (and (equal? (source-property sexp 'line) 0)
169 (equal? (source-property sexp 'column) 0)))))
170