Commit | Line | Data |
---|---|---|
4ed29c73 MV |
1 | ;;;; syncase.test --- test suite for (ice-9 syncase) -*- scheme -*- |
2 | ;;;; | |
cb65f76c | 3 | ;;;; Copyright (C) 2001, 2006, 2009 Free Software Foundation, Inc. |
4ed29c73 | 4 | ;;;; |
53befeb7 NJ |
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. | |
4ed29c73 | 9 | ;;;; |
53befeb7 | 10 | ;;;; This library is distributed in the hope that it will be useful, |
4ed29c73 | 11 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
53befeb7 NJ |
12 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | ;;;; Lesser General Public License for more details. | |
4ed29c73 | 14 | ;;;; |
53befeb7 NJ |
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 | |
4ed29c73 MV |
18 | |
19 | ;; These tests are in a module so that the syntax transformer does not | |
20 | ;; affect code outside of this file. | |
21 | ;; | |
d6e04e7c | 22 | (define-module (test-suite test-syncase) |
fd598527 AW |
23 | #:use-module (test-suite lib) |
24 | #:use-module (system base compile) | |
25 | #:use-module ((srfi srfi-1) :select (member))) | |
4ed29c73 | 26 | |
0b1b772f MV |
27 | (define-syntax plus |
28 | (syntax-rules () | |
29 | ((plus x ...) (+ x ...)))) | |
30 | ||
31 | (pass-if "basic syncase macro" | |
d6e04e7c | 32 | (= (plus 1 2 3) (+ 1 2 3))) |
ad5f5ada NJ |
33 | |
34 | (pass-if "@ works with syncase" | |
35 | (eq? run-test (@ (test-suite lib) run-test))) | |
cb65f76c AR |
36 | |
37 | (define-syntax string-let | |
38 | (lambda (stx) | |
39 | (syntax-case stx () | |
40 | ((_ id body ...) | |
41 | #`(let ((id #,(symbol->string | |
42 | (syntax->datum #'id)))) | |
43 | body ...))))) | |
44 | ||
45 | (pass-if "macro using quasisyntax" | |
46 | (equal? (string-let foo (list foo foo)) | |
47 | '("foo" "foo"))) | |
aa3819aa AR |
48 | |
49 | (define-syntax string-case | |
50 | (syntax-rules (else) | |
51 | ((string-case expr ((string ...) clause-body ...) ... (else else-body ...)) | |
52 | (let ((value expr)) | |
53 | (cond ((member value '(string ...) string=?) | |
54 | clause-body ...) | |
55 | ... | |
56 | (else | |
57 | else-body ...)))) | |
58 | ((string-case expr ((string ...) clause-body ...) ...) | |
59 | (let ((value expr)) | |
60 | (cond ((member value '(string ...) string=?) | |
61 | clause-body ...) | |
62 | ...))))) | |
63 | ||
64 | (define-syntax alist | |
65 | (syntax-rules (tail) | |
66 | ((alist ((key val) ... (tail expr))) | |
67 | (cons* '(key . val) ... expr)) | |
68 | ((alist ((key val) ...)) | |
69 | (list '(key . val) ...)))) | |
70 | ||
71 | (with-test-prefix "tail patterns" | |
72 | (with-test-prefix "at the outermost level" | |
73 | (pass-if "non-tail invocation" | |
74 | (equal? (string-case "foo" (("foo") 'foo)) | |
75 | 'foo)) | |
76 | (pass-if "tail invocation" | |
77 | (equal? (string-case "foo" (("bar") 'bar) (else 'else)) | |
78 | 'else))) | |
79 | (with-test-prefix "at a nested level" | |
80 | (pass-if "non-tail invocation" | |
81 | (equal? (alist ((a 1) (b 2) (c 3))) | |
82 | '((a . 1) (b . 2) (c . 3)))) | |
83 | (pass-if "tail invocation" | |
84 | (equal? (alist ((foo 42) (tail '((bar . 66))))) | |
85 | '((foo . 42) (bar . 66)))))) | |
fd598527 AW |
86 | |
87 | (with-test-prefix "serializable labels and marks" | |
88 | (compile '(begin | |
89 | (define-syntax duplicate-macro | |
90 | (syntax-rules () | |
91 | ((_ new-name old-name) | |
92 | (define-syntax new-name | |
93 | (syntax-rules () | |
94 | ((_ . vals) | |
95 | (letrec-syntax ((apply (syntax-rules () | |
96 | ((_ macro args) | |
97 | (macro . args))))) | |
98 | (apply old-name vals)))))))) | |
99 | ||
100 | (define-syntax kwote | |
101 | (syntax-rules () | |
102 | ((_ arg1) 'arg1))) | |
103 | ||
104 | (duplicate-macro kwote* kwote)) | |
105 | #:env (current-module)) | |
106 | (pass-if "compiled macro-generating macro works" | |
107 | (eq? (eval '(kwote* foo) (current-module)) | |
108 | 'foo))) | |
a2f7536d JG |
109 | |
110 | (with-test-prefix "changes to expansion environment" | |
111 | (pass-if "expander detects changes to current-module" | |
112 | (false-if-exception | |
113 | (compile '(begin | |
114 | (define-module (new-module)) | |
115 | (define-syntax new-module-macro | |
116 | (lambda (stx) | |
117 | (syntax-case stx () | |
118 | ((_ arg) (syntax arg))))) | |
119 | (new-module-macro #t)) | |
120 | #:env (current-module))))) |