syncase.test 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. ;;;; syncase.test --- test suite for (ice-9 syncase) -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2001, 2006, 2009, 2010, 2011, 2013, 2015 Free Software Foundation, Inc.
  4. ;;;;
  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.
  9. ;;;;
  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.
  14. ;;;;
  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
  18. ;; These tests are in a module so that the syntax transformer does not
  19. ;; affect code outside of this file.
  20. ;;
  21. (define-module (test-suite test-syncase)
  22. #:use-module (test-suite lib)
  23. #:use-module (system base compile)
  24. #:use-module (ice-9 regex)
  25. #:use-module ((srfi srfi-1) :select (member)))
  26. (define-syntax plus
  27. (syntax-rules ()
  28. ((plus x ...) (+ x ...))))
  29. (pass-if "basic syncase macro"
  30. (= (plus 1 2 3) (+ 1 2 3)))
  31. (pass-if "@ works with syncase"
  32. (eq? run-test (@ (test-suite lib) run-test)))
  33. (define-syntax string-let
  34. (lambda (stx)
  35. (syntax-case stx ()
  36. ((_ id body ...)
  37. #`(let ((id #,(symbol->string
  38. (syntax->datum #'id))))
  39. body ...)))))
  40. (pass-if "macro using quasisyntax"
  41. (equal? (string-let foo (list foo foo))
  42. '("foo" "foo")))
  43. (define-syntax string-case
  44. (syntax-rules (else)
  45. ((string-case expr ((string ...) clause-body ...) ... (else else-body ...))
  46. (let ((value expr))
  47. (cond ((member value '(string ...) string=?)
  48. clause-body ...)
  49. ...
  50. (else
  51. else-body ...))))
  52. ((string-case expr ((string ...) clause-body ...) ...)
  53. (let ((value expr))
  54. (cond ((member value '(string ...) string=?)
  55. clause-body ...)
  56. ...)))))
  57. (define-syntax alist
  58. (syntax-rules (tail)
  59. ((alist ((key val) ... (tail expr)))
  60. (cons* '(key . val) ... expr))
  61. ((alist ((key val) ...))
  62. (list '(key . val) ...))))
  63. (with-test-prefix "with-syntax"
  64. (pass-if "definitions allowed in body"
  65. (equal? (with-syntax ((a 23))
  66. (define b #'a)
  67. (syntax->datum b))
  68. 23)))
  69. (with-test-prefix "tail patterns"
  70. (with-test-prefix "at the outermost level"
  71. (pass-if "non-tail invocation"
  72. (equal? (string-case "foo" (("foo") 'foo))
  73. 'foo))
  74. (pass-if "tail invocation"
  75. (equal? (string-case "foo" (("bar") 'bar) (else 'else))
  76. 'else)))
  77. (with-test-prefix "at a nested level"
  78. (pass-if "non-tail invocation"
  79. (equal? (alist ((a 1) (b 2) (c 3)))
  80. '((a . 1) (b . 2) (c . 3))))
  81. (pass-if "tail invocation"
  82. (equal? (alist ((foo 42) (tail '((bar . 66)))))
  83. '((foo . 42) (bar . 66))))))
  84. (with-test-prefix "serializable labels and marks"
  85. (compile '(begin
  86. (define-syntax duplicate-macro
  87. (syntax-rules ()
  88. ((_ new-name old-name)
  89. (define-syntax new-name
  90. (syntax-rules ()
  91. ((_ . vals)
  92. (letrec-syntax ((apply (syntax-rules ()
  93. ((_ macro args)
  94. (macro . args)))))
  95. (apply old-name vals))))))))
  96. (define-syntax kwote
  97. (syntax-rules ()
  98. ((_ arg1) 'arg1)))
  99. (duplicate-macro kwote* kwote))
  100. #:env (current-module))
  101. (pass-if "compiled macro-generating macro works"
  102. (eq? (eval '(kwote* foo) (current-module))
  103. 'foo)))
  104. (with-test-prefix "changes to expansion environment"
  105. (pass-if "expander detects changes to current-module with @@ @@"
  106. (compile '(begin
  107. (define-module (new-module))
  108. (@@ @@ (new-module)
  109. (define-syntax new-module-macro
  110. (lambda (stx)
  111. (syntax-case stx ()
  112. ((_ arg) (syntax arg))))))
  113. (@@ @@ (new-module)
  114. (new-module-macro #t)))
  115. #:env (current-module))))
  116. (define-module (test-suite test-syncase-2)
  117. #:export (make-the-macro))
  118. (define (hello)
  119. 'hello)
  120. (define-syntax make-the-macro
  121. (syntax-rules ()
  122. ((_ name)
  123. (define-syntax name
  124. (syntax-rules ()
  125. ((_) (hello)))))))
  126. (define-module (test-suite test-syncase)) ;; back to main module
  127. (use-modules (test-suite test-syncase-2))
  128. (make-the-macro foo)
  129. (with-test-prefix "macro-generating macro"
  130. (pass-if "module hygiene"
  131. (eq? (foo) 'hello)))
  132. (pass-if "_ is a placeholder"
  133. (equal? (eval '(begin
  134. (define-syntax ciao
  135. (lambda (stx)
  136. (syntax-case stx ()
  137. ((_ _)
  138. "ciao"))))
  139. (ciao 1))
  140. (current-module))
  141. "ciao"))
  142. (define qux 30)
  143. (with-test-prefix "identifier-syntax"
  144. (pass-if "global reference"
  145. (let-syntax ((baz (identifier-syntax qux)))
  146. (equal? baz qux)))
  147. (pass-if "lexical hygienic reference"
  148. (let-syntax ((baz (identifier-syntax qux)))
  149. (let ((qux 20))
  150. (equal? (+ baz qux)
  151. 50))))
  152. (pass-if "lexical hygienic reference (bound)"
  153. (let ((qux 20))
  154. (let-syntax ((baz (identifier-syntax qux)))
  155. (equal? (+ baz qux)
  156. 40))))
  157. (pass-if "global reference (settable)"
  158. (let-syntax ((baz (identifier-syntax
  159. (id qux)
  160. ((set! id expr) (set! qux expr)))))
  161. (equal? baz qux)))
  162. (pass-if "lexical hygienic reference (settable)"
  163. (let-syntax ((baz (identifier-syntax
  164. (id qux)
  165. ((set! id expr) (set! qux expr)))))
  166. (let ((qux 20))
  167. (equal? (+ baz qux)
  168. 50))))
  169. (pass-if "lexical hygienic reference (bound, settable)"
  170. (let ((qux 20))
  171. (let-syntax ((baz (identifier-syntax
  172. (id qux)
  173. ((set! id expr) (set! qux expr)))))
  174. (equal? (+ baz qux)
  175. 40))))
  176. (pass-if "global set!"
  177. (let-syntax ((baz (identifier-syntax
  178. (id qux)
  179. ((set! id expr) (set! qux expr)))))
  180. (set! baz 10)
  181. (equal? (+ baz qux) 20)))
  182. (pass-if "lexical hygienic set!"
  183. (let-syntax ((baz (identifier-syntax
  184. (id qux)
  185. ((set! id expr) (set! qux expr)))))
  186. (and (let ((qux 20))
  187. (set! baz 5)
  188. (equal? (+ baz qux)
  189. 25))
  190. (equal? qux 5))))
  191. (pass-if "lexical hygienic set! (bound)"
  192. (let ((qux 20))
  193. (let-syntax ((baz (identifier-syntax
  194. (id qux)
  195. ((set! id expr) (set! qux expr)))))
  196. (set! baz 50)
  197. (equal? (+ baz qux)
  198. 100)))))
  199. (with-test-prefix "top-level expansions"
  200. (pass-if "syntax definitions expanded before other expressions"
  201. (eval '(begin
  202. (define even?
  203. (lambda (x)
  204. (or (= x 0) (odd? (- x 1)))))
  205. (define-syntax odd?
  206. (syntax-rules ()
  207. ((odd? x) (not (even? x)))))
  208. (even? 10))
  209. (current-module))))
  210. (define-module (test-suite test-syncase-3)
  211. #:autoload (test-syncase-3-does-not-exist) (baz))
  212. (define-module (test-suite test-syncase)) ;; back to main module
  213. (pass-if "missing autoloads do not foil psyntax"
  214. (parameterize ((current-warning-port (%make-void-port "w")))
  215. (eval '(if #f (baz) #t)
  216. (resolve-module '(test-suite test-syncase-3)))))
  217. (use-modules (system syntax))
  218. (with-test-prefix "syntax-local-binding"
  219. (define-syntax syntax-type
  220. (lambda (x)
  221. (syntax-case x ()
  222. ((_ id resolve?)
  223. (call-with-values
  224. (lambda ()
  225. (syntax-local-binding
  226. #'id
  227. #:resolve-syntax-parameters? (syntax->datum #'resolve?)))
  228. (lambda (type value)
  229. (with-syntax ((type (datum->syntax #'id type)))
  230. #''type)))))))
  231. (define-syntax-parameter foo
  232. (syntax-rules ()))
  233. (pass-if "syntax-parameters (resolved)"
  234. (equal? (syntax-type foo #t) 'macro))
  235. (pass-if "syntax-parameters (unresolved)"
  236. (equal? (syntax-type foo #f) 'syntax-parameter)))
  237. ;; (put 'pass-if-syntax-error 'scheme-indent-function 1)
  238. (define-syntax pass-if-syntax-error
  239. (syntax-rules ()
  240. ((_ name pat exp)
  241. (pass-if name
  242. (catch 'syntax-error
  243. (lambda () exp (error "expected syntax-error exception"))
  244. (lambda (k who what where form . maybe-subform)
  245. (if (if (pair? pat)
  246. (and (eq? who (car pat))
  247. (string-match (cdr pat) what))
  248. (string-match pat what))
  249. #t
  250. (error "unexpected syntax-error exception" what pat))))))))
  251. (with-test-prefix "primitives"
  252. (pass-if-syntax-error "primref in default module"
  253. "failed to match"
  254. (macroexpand '(@@ primitive cons)))
  255. (pass-if-syntax-error "primcall in default module"
  256. "failed to match"
  257. (macroexpand '((@@ primitive cons) 1 2)))
  258. (pass-if-equal "primcall in (guile)"
  259. '(1 . 2)
  260. (@@ @@ (guile) ((@@ primitive cons) 1 2)))
  261. (pass-if-syntax-error "primref in (guile)"
  262. "not in operator position"
  263. (macroexpand '(@@ @@ (guile) (@@ primitive cons)))))
  264. (pass-if "infinite loop bug"
  265. (begin
  266. (macroexpand
  267. '(let-syntax
  268. ((define-foo
  269. (syntax-rules ()
  270. ((define-foo a b)
  271. (begin
  272. (define a '())
  273. ;; Oddly, the "*" in the define* seems to be
  274. ;; important in triggering this bug.
  275. (define* (b) (set! a a)))))))
  276. (define-foo a c)))
  277. #t))