1 ;;;; syncase.test --- test suite for (ice-9 syncase) -*- scheme -*-
3 ;;;; Copyright (C) 2001, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library 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 GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 ;; These tests are in a module so that the syntax transformer does not
20 ;; affect code outside of this file.
22 (define-module (test-suite test-syncase)
23 #:use-module (test-suite lib)
24 #:use-module (system base compile)
25 #:use-module ((srfi srfi-1) :select (member)))
29 ((plus x ...) (+ x ...))))
31 (pass-if "basic syncase macro"
32 (= (plus 1 2 3) (+ 1 2 3)))
34 (pass-if "@ works with syncase"
35 (eq? run-test (@ (test-suite lib) run-test)))
37 (define-syntax string-let
41 #`(let ((id #,(symbol->string
42 (syntax->datum #'id))))
45 (pass-if "macro using quasisyntax"
46 (equal? (string-let foo (list foo foo))
49 (define-syntax string-case
51 ((string-case expr ((string ...) clause-body ...) ... (else else-body ...))
53 (cond ((member value '(string ...) string=?)
58 ((string-case expr ((string ...) clause-body ...) ...)
60 (cond ((member value '(string ...) string=?)
66 ((alist ((key val) ... (tail expr)))
67 (cons* '(key . val) ... expr))
68 ((alist ((key val) ...))
69 (list '(key . val) ...))))
71 (with-test-prefix "with-syntax"
72 (pass-if "definitions allowed in body"
73 (equal? (with-syntax ((a 23))
78 (with-test-prefix "tail patterns"
79 (with-test-prefix "at the outermost level"
80 (pass-if "non-tail invocation"
81 (equal? (string-case "foo" (("foo") 'foo))
83 (pass-if "tail invocation"
84 (equal? (string-case "foo" (("bar") 'bar) (else 'else))
86 (with-test-prefix "at a nested level"
87 (pass-if "non-tail invocation"
88 (equal? (alist ((a 1) (b 2) (c 3)))
89 '((a . 1) (b . 2) (c . 3))))
90 (pass-if "tail invocation"
91 (equal? (alist ((foo 42) (tail '((bar . 66)))))
92 '((foo . 42) (bar . 66))))))
94 (with-test-prefix "serializable labels and marks"
96 (define-syntax duplicate-macro
98 ((_ new-name old-name)
99 (define-syntax new-name
102 (letrec-syntax ((apply (syntax-rules ()
105 (apply old-name vals))))))))
111 (duplicate-macro kwote* kwote))
112 #:env (current-module))
113 (pass-if "compiled macro-generating macro works"
114 (eq? (eval '(kwote* foo) (current-module))
117 (with-test-prefix "changes to expansion environment"
118 (pass-if "expander detects changes to current-module with @@ @@"
120 (define-module (new-module))
122 (define-syntax new-module-macro
125 ((_ arg) (syntax arg))))))
127 (new-module-macro #t)))
128 #:env (current-module))))
130 (define-module (test-suite test-syncase-2)
131 #:export (make-the-macro))
136 (define-syntax make-the-macro
143 (define-module (test-suite test-syncase)) ;; back to main module
144 (use-modules (test-suite test-syncase-2))
148 (with-test-prefix "macro-generating macro"
149 (pass-if "module hygiene"
152 (pass-if "_ is a placeholder"
153 (equal? (eval '(begin
165 (with-test-prefix "identifier-syntax"
167 (pass-if "global reference"
168 (let-syntax ((baz (identifier-syntax qux)))
171 (pass-if "lexical hygienic reference"
172 (let-syntax ((baz (identifier-syntax qux)))
177 (pass-if "lexical hygienic reference (bound)"
179 (let-syntax ((baz (identifier-syntax qux)))
183 (pass-if "global reference (settable)"
184 (let-syntax ((baz (identifier-syntax
186 ((set! id expr) (set! qux expr)))))
189 (pass-if "lexical hygienic reference (settable)"
190 (let-syntax ((baz (identifier-syntax
192 ((set! id expr) (set! qux expr)))))
197 (pass-if "lexical hygienic reference (bound, settable)"
199 (let-syntax ((baz (identifier-syntax
201 ((set! id expr) (set! qux expr)))))
205 (pass-if "global set!"
206 (let-syntax ((baz (identifier-syntax
208 ((set! id expr) (set! qux expr)))))
210 (equal? (+ baz qux) 20)))
212 (pass-if "lexical hygienic set!"
213 (let-syntax ((baz (identifier-syntax
215 ((set! id expr) (set! qux expr)))))
222 (pass-if "lexical hygienic set! (bound)"
224 (let-syntax ((baz (identifier-syntax
226 ((set! id expr) (set! qux expr)))))
231 (with-test-prefix "top-level expansions"
232 (pass-if "syntax definitions expanded before other expressions"
236 (or (= x 0) (odd? (- x 1)))))
239 ((odd? x) (not (even? x)))))