1 ;;;; syntax.test --- test suite for Guile's syntactic forms -*- scheme -*-
3 ;;;; Copyright (C) 2001 Free Software Foundation, Inc.
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
10 ;;;; This program is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 ;;;; Boston, MA 02111-1307 USA
21 (define exception:bad-bindings
22 (cons 'misc-error "^bad bindings"))
23 (define exception:duplicate-bindings
24 (cons 'misc-error "^duplicate bindings"))
25 (define exception:bad-body
26 (cons 'misc-error "^bad body"))
27 (define exception:bad-formals
28 (cons 'misc-error "^bad formals"))
29 (define exception:duplicate-formals
30 (cons 'misc-error "^duplicate formals"))
31 (define exception:bad-var
32 (cons 'misc-error "^bad variable"))
33 (define exception:bad/missing-clauses
34 (cons 'misc-error "^bad or missing clauses"))
35 (define exception:missing/extra-expr
36 (cons 'misc-error "^missing or extra expression"))
39 (with-test-prefix "expressions"
41 (with-test-prefix "missing or extra expression"
44 ;; *Note:* In many dialects of Lisp, the empty combination, (),
45 ;; is a legitimate expression. In Scheme, combinations must
46 ;; have at least one subexpression, so () is not a syntactically
50 (pass-if-exception "empty parentheses \"()\""
51 exception:missing/extra-expr
54 (with-test-prefix "lambda"
56 (with-test-prefix "bad formals"
58 (pass-if-exception "(lambda)"
62 (pass-if-exception "(lambda . \"foo\")"
66 (pass-if-exception "(lambda ())"
70 (pass-if-exception "(lambda \"foo\")"
74 (pass-if-exception "(lambda \"foo\" #f)"
78 (pass-if-exception "(lambda (x 1) 2)"
82 (pass-if-exception "(lambda (1 x) 2)"
86 (pass-if-exception "(lambda (x \"a\") 2)"
90 (pass-if-exception "(lambda (\"a\" x) 2)"
94 (with-test-prefix "duplicate formals"
97 (pass-if-exception "(lambda (x x) 1)"
98 exception:duplicate-formals
102 (pass-if-exception "(lambda (x x x) 1)"
103 exception:duplicate-formals
104 (lambda (x x x) 1))))
106 (with-test-prefix "let"
108 (with-test-prefix "bindings"
110 (pass-if-exception "late binding"
111 exception:unbound-var
112 (let ((x 1) (y x)) y)))
114 (with-test-prefix "bad body"
116 (pass-if-exception "(let ())"
120 (pass-if-exception "(let ((x 1)))"
124 ;; FIXME: Wouldn't one rather expect a 'bad bindings' error?
125 ;; Hmm, the body is bad as well, isn't it?
126 (pass-if-exception "(let)"
130 ;; FIXME: Wouldn't one rather expect a 'bad bindings' error?
131 ;; Hmm, the body is bad as well, isn't it?
132 (pass-if-exception "(let 1)"
136 ;; FIXME: Wouldn't one rather expect a 'bad bindings' error?
137 ;; Hmm, the body is bad as well, isn't it?
138 (pass-if-exception "(let (x))"
142 (with-test-prefix "bad bindings"
144 (pass-if-exception "(let (x) 1)"
145 exception:bad-bindings
148 (pass-if-exception "(let ((x)) 3)"
149 exception:bad-bindings
152 (pass-if-exception "(let ((x 1) y) x)"
153 exception:bad-bindings
156 (pass-if-exception "(let ((1 2)) 3)"
160 (with-test-prefix "duplicate bindings"
162 (pass-if-exception "(let ((x 1) (x 2)) x)"
163 exception:duplicate-bindings
164 (let ((x 1) (x 2)) x))))
166 (with-test-prefix "named let"
168 (with-test-prefix "bad body"
170 (pass-if-exception "(let x ())"
174 (pass-if-exception "(let x ((y 1)))"
178 ;; FIXME: Wouldn't one rather expect a 'bad bindings' error?
179 ;; Hmm, the body is bad as well, isn't it?
180 (pass-if-exception "(let x (y))"
184 (with-test-prefix "let*"
186 (with-test-prefix "bindings"
188 (pass-if "(let* ((x 1) (x 2)) ...)"
192 (pass-if "(let* ((x 1) (x x)) ...)"
196 (with-test-prefix "bad body"
198 (pass-if-exception "(let* ())"
202 (pass-if-exception "(let* ((x 1)))"
206 ;; FIXME: Wouldn't one rather expect a 'bad bindings' error?
207 ;; Hmm, the body is bad as well, isn't it?
208 (pass-if-exception "(let*)"
212 ;; FIXME: Wouldn't one rather expect a 'bad bindings' error?
213 ;; Hmm, the body is bad as well, isn't it?
214 (pass-if-exception "(let* 1)"
218 ;; FIXME: Wouldn't one rather expect a 'bad bindings' error?
219 ;; Hmm, the body is bad as well, isn't it?
220 (pass-if-exception "(let* (x))"
224 (with-test-prefix "bad bindings"
226 (pass-if-exception "(let* (x) 1)"
227 exception:bad-bindings
230 (pass-if-exception "(let* ((x)) 3)"
231 exception:bad-bindings
234 (pass-if-exception "(let* ((x 1) y) x)"
235 exception:bad-bindings
238 (pass-if-exception "(let* x ())"
239 exception:bad-bindings
242 (pass-if-exception "(let* x (y))"
243 exception:bad-bindings
246 (pass-if-exception "(let* ((1 2)) 3)"
250 (with-test-prefix "letrec"
252 (with-test-prefix "bindings"
254 (pass-if-exception "initial bindings are undefined"
255 exception:unbound-var
257 (letrec ((x 1) (y x)) y))))
259 (with-test-prefix "bad body"
261 (pass-if-exception "(letrec ())"
265 (pass-if-exception "(letrec ((x 1)))"
269 ;; FIXME: Wouldn't one rather expect a 'bad bindings' error?
270 ;; Hmm, the body is bad as well, isn't it?
271 (pass-if-exception "(letrec)"
275 ;; FIXME: Wouldn't one rather expect a 'bad bindings' error?
276 ;; Hmm, the body is bad as well, isn't it?
277 (pass-if-exception "(letrec 1)"
281 ;; FIXME: Wouldn't one rather expect a 'bad bindings' error?
282 ;; Hmm, the body is bad as well, isn't it?
283 (pass-if-exception "(letrec (x))"
287 (with-test-prefix "bad bindings"
289 (pass-if-exception "(letrec (x) 1)"
290 exception:bad-bindings
293 (pass-if-exception "(letrec ((x)) 3)"
294 exception:bad-bindings
297 (pass-if-exception "(letrec ((x 1) y) x)"
298 exception:bad-bindings
299 (letrec ((x 1) y) x))
301 (pass-if-exception "(letrec x ())"
302 exception:bad-bindings
305 (pass-if-exception "(letrec x (y))"
306 exception:bad-bindings
309 (pass-if-exception "(letrec ((1 2)) 3)"
313 (with-test-prefix "duplicate bindings"
315 (pass-if-exception "(letrec ((x 1) (x 2)) x)"
316 exception:duplicate-bindings
317 (letrec ((x 1) (x 2)) x))))
319 (with-test-prefix "if"
321 (with-test-prefix "missing or extra expressions"
323 (pass-if-exception "(if)"
324 exception:missing/extra-expr
327 (pass-if-exception "(if 1 2 3 4)"
328 exception:missing/extra-expr
331 (with-test-prefix "cond"
333 (with-test-prefix "bad or missing clauses"
335 (pass-if-exception "(cond)"
336 exception:bad/missing-clauses
339 (pass-if-exception "(cond #t)"
340 exception:bad/missing-clauses
343 (pass-if-exception "(cond 1)"
344 exception:bad/missing-clauses
347 (pass-if-exception "(cond 1 2)"
348 exception:bad/missing-clauses
351 (pass-if-exception "(cond 1 2 3)"
352 exception:bad/missing-clauses
355 (pass-if-exception "(cond 1 2 3 4)"
356 exception:bad/missing-clauses
359 (pass-if-exception "(cond ())"
360 exception:bad/missing-clauses
363 (pass-if-exception "(cond () 1)"
364 exception:bad/missing-clauses
367 (pass-if-exception "(cond (1) 1)"
368 exception:bad/missing-clauses
371 (with-test-prefix "cond =>"
373 (with-test-prefix "else is handled correctly"
377 (eq? (cond (else =>)) 'foo)))
379 (pass-if "else => identity"
381 (eq? (cond (else => identity)) identity))))
383 (with-test-prefix "bad formals"
385 (pass-if-exception "=> (lambda (x 1) 2)"
386 exception:bad-formals
387 (cond (1 => (lambda (x 1) 2))))))
389 (with-test-prefix "case"
391 (with-test-prefix "bad or missing clauses"
393 (pass-if-exception "(case)"
394 exception:bad/missing-clauses
397 (pass-if-exception "(case . \"foo\")"
398 exception:bad/missing-clauses
401 (pass-if-exception "(case 1)"
402 exception:bad/missing-clauses
405 (pass-if-exception "(case 1 . \"foo\")"
406 exception:bad/missing-clauses
409 (pass-if-exception "(case 1 \"foo\")"
410 exception:bad/missing-clauses
413 (pass-if-exception "(case 1 ())"
414 exception:bad/missing-clauses
417 (pass-if-exception "(case 1 (\"foo\"))"
418 exception:bad/missing-clauses
421 (pass-if-exception "(case 1 (\"foo\" \"bar\"))"
422 exception:bad/missing-clauses
423 (case 1 ("foo" "bar")))
425 ;; According to R5RS, the following one is syntactically correct.
426 ;; (pass-if-exception "(case 1 (() \"bar\"))"
427 ;; exception:bad/missing-clauses
428 ;; (case 1 (() "bar")))
430 (pass-if-exception "(case 1 ((2) \"bar\") . \"foo\")"
431 exception:bad/missing-clauses
432 (case 1 ((2) "bar") . "foo"))
434 (pass-if-exception "(case 1 (else #f) ((1) #t))"
435 exception:bad/missing-clauses
436 (case 1 ((2) "bar") (else)))
438 (pass-if-exception "(case 1 (else #f) . \"foo\")"
439 exception:bad/missing-clauses
440 (case 1 (else #f) . "foo"))
442 (pass-if-exception "(case 1 (else #f) ((1) #t))"
443 exception:bad/missing-clauses
444 (case 1 (else #f) ((1) #t)))))
446 (with-test-prefix "define"
448 (with-test-prefix "missing or extra expressions"
450 (pass-if-exception "(define)"
451 exception:missing/extra-expr
454 (with-test-prefix "set!"
456 (with-test-prefix "missing or extra expressions"
458 (pass-if-exception "(set!)"
459 exception:missing/extra-expr
462 (pass-if-exception "(set! 1)"
463 exception:missing/extra-expr
466 (pass-if-exception "(set! 1 2 3)"
467 exception:missing/extra-expr
470 (with-test-prefix "bad variable"
472 (pass-if-exception "(set! \"\" #t)"
476 (pass-if-exception "(set! 1 #t)"
480 (pass-if-exception "(set! #t #f)"
484 (pass-if-exception "(set! #f #t)"
488 (pass-if-exception "(set! #\space #f)"
492 (with-test-prefix "generalized set! (SRFI 17)"
494 (with-test-prefix "target is not procedure with setter"
496 (pass-if-exception "(set! (symbol->string 'x) 1)"
497 exception:wrong-type-arg
498 (set! (symbol->string 'x) 1))
500 (pass-if-exception "(set! '#f 1)"
501 exception:wrong-type-arg
504 (with-test-prefix "quote"
506 (with-test-prefix "missing or extra expression"
508 (pass-if-exception "(quote)"
509 exception:missing/extra-expr
512 (pass-if-exception "(quote a b)"
513 exception:missing/extra-expr