Honor and confine expansion-time side-effects to `current-reader'.
[bpt/guile.git] / test-suite / tests / compiler.test
1 ;;;; compiler.test --- tests for the compiler -*- scheme -*-
2 ;;;; Copyright (C) 2008, 2009 Free Software Foundation, Inc.
3 ;;;;
4 ;;;; This library is free software; you can redistribute it and/or
5 ;;;; modify it under the terms of the GNU Lesser General Public
6 ;;;; License as published by the Free Software Foundation; either
7 ;;;; version 3 of the License, or (at your option) any later version.
8 ;;;;
9 ;;;; This library is distributed in the hope that it will be useful,
10 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ;;;; Lesser General Public License for more details.
13 ;;;;
14 ;;;; You should have received a copy of the GNU Lesser General Public
15 ;;;; License along with this library; if not, write to the Free Software
16 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18 (define-module (test-suite tests compiler)
19 :use-module (test-suite lib)
20 :use-module (test-suite guile-test)
21 :use-module (system base compile)
22 :use-module ((system vm vm) #:select (the-vm vm-load)))
23
24 (define read-and-compile
25 (@@ (system base compile) read-and-compile))
26
27
28 \f
29 (with-test-prefix "basic"
30
31 (pass-if "compile to value"
32 (equal? (compile 1) 1)))
33
34 \f
35 (with-test-prefix "psyntax"
36
37 (pass-if "compile uses a fresh module by default"
38 (begin
39 (compile '(define + -))
40 (eq? (compile '+) +)))
41
42 (pass-if "compile-time definitions are isolated"
43 (begin
44 (compile '(define foo-bar #t))
45 (not (module-variable (current-module) 'foo-bar))))
46
47 (pass-if "compile in current module"
48 (let ((o (begin
49 (compile '(define-macro (foo) 'bar)
50 #:env (current-module))
51 (compile '(let ((bar 'ok)) (foo))
52 #:env (current-module)))))
53 (and (macro? (module-ref (current-module) 'foo))
54 (eq? o 'ok))))
55
56 (pass-if "compile in fresh module"
57 (let* ((m (let ((m (make-module)))
58 (beautify-user-module! m)
59 m))
60 (o (begin
61 (compile '(define-macro (foo) 'bar) #:env m)
62 (compile '(let ((bar 'ok)) (foo)) #:env m))))
63 (and (module-ref m 'foo)
64 (eq? o 'ok))))
65
66 (pass-if "redefinition"
67 ;; In this case the locally-bound `round' must have the same value as the
68 ;; imported `round'. See the same test in `syntax.test' for details.
69 (let ((m (make-module)))
70 (beautify-user-module! m)
71 (compile '(define round round) #:env m)
72 (eq? round (module-ref m 'round)))))
73
74 \f
75 (with-test-prefix "current-reader"
76
77 (pass-if "default compile-time current-reader differs"
78 (not (eq? (compile 'current-reader)
79 current-reader)))
80
81 (pass-if "compile-time changes are honored and isolated"
82 ;; Make sure changing `current-reader' as the side-effect of a defmacro
83 ;; actually works.
84 (let ((r (fluid-ref current-reader))
85 (input (open-input-string
86 "(define-macro (install-reader!)
87 ;;(format #t \"current-reader = ~A~%\" current-reader)
88 (fluid-set! current-reader
89 (let ((first? #t))
90 (lambda args
91 (if first?
92 (begin
93 (set! first? #f)
94 ''ok)
95 (read (open-input-string \"\"))))))
96 #f)
97 (install-reader!)
98 this-should-be-ignored")))
99 (and (eq? (vm-load (the-vm) (read-and-compile input))
100 'ok)
101 (eq? r (fluid-ref current-reader))))))