| 1 |
;; Copyright (C) Marc Nieper-Wißkirchen (2016). All Rights Reserved. |
| 2 |
|
| 3 |
;; Permission is hereby granted, free of charge, to any person |
| 4 |
;; obtaining a copy of this software and associated documentation |
| 5 |
;; files (the "Software"), to deal in the Software without |
| 6 |
;; restriction, including without limitation the rights to use, copy, |
| 7 |
;; modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
;; of the Software, and to permit persons to whom the Software is |
| 9 |
;; furnished to do so, subject to the following conditions: |
| 10 |
|
| 11 |
;; The above copyright notice and this permission notice shall be |
| 12 |
;; included in all copies or substantial portions of the Software. |
| 13 |
|
| 14 |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 |
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 |
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 18 |
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 19 |
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 |
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
;; SOFTWARE. |
| 22 |
|
| 23 |
(define-library (srfi 147 test) |
| 24 |
(export run-tests) |
| 25 |
(include-library-declarations "../../custom-macro-transformers.scm") |
| 26 |
(import (srfi 64)) |
| 27 |
(begin |
| 28 |
(define (run-tests) |
| 29 |
(test-begin "SRFI 147") |
| 30 |
|
| 31 |
(test-group "R7RS macros" |
| 32 |
(define-syntax foo |
| 33 |
(syntax-rules () |
| 34 |
((foo) |
| 35 |
42))) |
| 36 |
|
| 37 |
(test-equal 42 (foo)) |
| 38 |
|
| 39 |
(test-equal 42 (let-syntax |
| 40 |
((foo |
| 41 |
(syntax-rules () |
| 42 |
((foo) |
| 43 |
42)))) |
| 44 |
(foo))) |
| 45 |
|
| 46 |
(test-equal 42 (letrec-syntax |
| 47 |
((foo |
| 48 |
(syntax-rules () |
| 49 |
((foo) |
| 50 |
42))) |
| 51 |
(bar |
| 52 |
(syntax-rules () |
| 53 |
((bar) |
| 54 |
(foo))))) |
| 55 |
(bar)))) |
| 56 |
|
| 57 |
(test-group "Custom macro transformers" |
| 58 |
(define-syntax simple-syntax-rules |
| 59 |
(syntax-rules () |
| 60 |
((simple-syntax-rules . rules) |
| 61 |
(syntax-rules () . rules)))) |
| 62 |
|
| 63 |
(define-syntax bar-rules |
| 64 |
(simple-syntax-rules |
| 65 |
((bar-rules (pattern template) ...) |
| 66 |
(simple-syntax-rules (pattern '(bar template)) ...)))) |
| 67 |
|
| 68 |
(define-syntax foo |
| 69 |
(simple-syntax-rules |
| 70 |
((foo) |
| 71 |
42))) |
| 72 |
|
| 73 |
(define-syntax bar |
| 74 |
(bar-rules |
| 75 |
((bar x) x))) |
| 76 |
|
| 77 |
(test-equal 42 (foo)) |
| 78 |
|
| 79 |
(test-equal 42 (let-syntax |
| 80 |
((foo |
| 81 |
(simple-syntax-rules |
| 82 |
((foo) |
| 83 |
42)))) |
| 84 |
(foo))) |
| 85 |
|
| 86 |
(test-equal '(bar 42) (bar 42))) |
| 87 |
|
| 88 |
;; NB: This doesn't work in Gauche (yet) |
| 89 |
'(test-group "Auxiliary definitions in custom macro transformers" |
| 90 |
(define-syntax my-macro-transformer |
| 91 |
(syntax-rules () |
| 92 |
((my-macro-transformer) |
| 93 |
(begin (define foo 2) |
| 94 |
(syntax-rules () |
| 95 |
((_) foo)))))) |
| 96 |
|
| 97 |
(test-equal 42 (* 21 (letrec-syntax ((foo (my-macro-transformer))) |
| 98 |
(foo))))) |
| 99 |
|
| 100 |
(test-group "Scoping of expansion" |
| 101 |
(define-syntax simple-syntax-rules |
| 102 |
(syntax-rules () |
| 103 |
((simple-syntax-rules . rules) |
| 104 |
(syntax-rules () . rules)))) |
| 105 |
|
| 106 |
(test-equal 'foo (let-syntax |
| 107 |
((simple-syntax-rules |
| 108 |
(simple-syntax-rules ((_) 'foo)))) |
| 109 |
(simple-syntax-rules)))) |
| 110 |
|
| 111 |
(test-group "Custom ellipsis" |
| 112 |
(define-syntax my-syntax-rules |
| 113 |
(syntax-rules !!! () |
| 114 |
((my-syntax-rules e l* rule !!!) |
| 115 |
(syntax-rules e l* rule !!!)))) |
| 116 |
|
| 117 |
(define-syntax foo |
| 118 |
(my-syntax-rules ::: () |
| 119 |
((foo a) 'a) |
| 120 |
((foo a b) '(a . b)) |
| 121 |
((foo a :::) (list 'a :::)))) |
| 122 |
|
| 123 |
(test-equal '(a b c) (foo a b c))) |
| 124 |
|
| 125 |
(test-group "Aliases for keywords" |
| 126 |
(define-syntax λ lambda) |
| 127 |
(define foo (λ () 'baz)) |
| 128 |
(test-equal 'baz (foo))) |
| 129 |
|
| 130 |
(test-group "Example from specification" |
| 131 |
(define-syntax syntax-rules* |
| 132 |
(syntax-rules () |
| 133 |
((syntax-rules* (literal ...) (pattern . templates) ...) |
| 134 |
(syntax-rules (literal ...) (pattern (begin . templates)) ...)) |
| 135 |
((syntax-rules* ellipsis (literal ...) (pattern . templates) ...) |
| 136 |
(syntax-rules ellipsis (literal ...) (pattern (begin . templates)) ...)))) |
| 137 |
|
| 138 |
(test-equal '(1 2) (let-syntax |
| 139 |
((foo |
| 140 |
(syntax-rules* () |
| 141 |
((foo a b) |
| 142 |
(define a 1) |
| 143 |
(define b 2))))) |
| 144 |
(foo x y) |
| 145 |
(list x y)))) |
| 146 |
|
| 147 |
(test-end))) |
| 148 |
) |