Debian Codegraph @

test-suite/tests/rnrs-libraries.test

raw
1 ;;;; rnrs-libraries.test --- test library and import forms -*- scheme -*-
2 ;;;; Copyright (C) 2010, 2012 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 (tests rnrs-libraries)
19 #:use-module (test-suite lib))
20
21 ;; First, check that Guile modules are r6rs modules.
22 ;;
23 (with-test-prefix "ice-9 receive"
24 (define iface #f)
25
26 (pass-if "import"
27 (eval '(begin
28 (import (ice-9 receive))
29 #t)
30 (current-module)))
31
32 (pass-if "resolve-interface"
33 (module? (resolve-interface '(ice-9 receive))))
34
35 (set! iface (resolve-interface '(ice-9 receive)))
36
37 (pass-if "resolve-r6rs-interface"
38 (eq? iface (resolve-r6rs-interface '(ice-9 receive))))
39
40 (pass-if "resolve-r6rs-interface (2)"
41 (eq? iface (resolve-r6rs-interface '(library (ice-9 receive)))))
42
43 (pass-if "module uses"
44 (and (memq iface (module-uses (current-module))) #t))
45
46 (pass-if "interface contents"
47 (equal? '(receive)
48 (hash-map->list (lambda (sym var) sym) (module-obarray iface))))
49
50 (pass-if "interface uses"
51 (null? (module-uses iface)))
52
53 (pass-if "version"
54 (or (not (module-version iface))
55 (null? (module-version iface))))
56
57 (pass-if "calling receive from current env"
58 (equal? (eval '(receive (a b) (values 10 32)
59 (+ a b))
60 (current-module))
61 42)))
62
63
64 ;; And check that r6rs modules are guile modules.
65 ;;
66 (with-test-prefix "rnrs-test-a"
67 (define iface #f)
68
69 (pass-if "no double"
70 (not (module-local-variable (current-module) 'double)))
71
72 (pass-if "import"
73 (eval '(begin
74 (import (tests rnrs-test-a))
75 #t)
76 (current-module)))
77
78 (pass-if "still no double"
79 (not (module-local-variable (current-module) 'double)))
80
81 (pass-if "resolve-interface"
82 (module? (resolve-interface '(tests rnrs-test-a))))
83
84 (set! iface (resolve-interface '(tests rnrs-test-a)))
85
86 (pass-if "resolve-interface (2)"
87 (eq? iface (resolve-interface '(tests rnrs-test-a))))
88
89 (pass-if "resolve-r6rs-interface"
90 (eq? iface (resolve-r6rs-interface '(tests rnrs-test-a))))
91
92 (pass-if "resolve-r6rs-interface (2)"
93 (eq? iface (resolve-r6rs-interface '(library (tests rnrs-test-a)))))
94
95 (pass-if "module uses"
96 (and (memq iface (module-uses (current-module))) #t))
97
98 (pass-if "interface contents"
99 (equal? '(double)
100 (hash-map->list (lambda (sym var) sym) (module-obarray iface))))
101
102 (pass-if "interface uses"
103 (null? (module-uses iface)))
104
105 (pass-if "version"
106 (or (not (module-version iface))
107 (null? (module-version iface))))
108
109 (pass-if "calling double"
110 (equal? ((module-ref iface 'double) 10)
111 20))
112
113 (pass-if "calling double from current env"
114 (equal? (eval '(double 20) (current-module))
115 40)))
116
117 ;; Guile should ignore explicit phase specifications
118 ;;
119 (with-test-prefix "implicit phasing"
120 (with-test-prefix "in library form"
121 (pass-if "explicit phasing ignored"
122 (import (for (guile) (meta -1))) #t))
123
124 (with-test-prefix "in library form"
125 (pass-if "explicit phasing ignored"
126 (save-module-excursion
127 (lambda ()
128 (library (test)
129 (export)
130 (import (for (guile) (meta -1))))
131 #t)))))
132
133 ;; Now import features.
134 ;;
135 (with-test-prefix "import features"
136 (define iface #f)
137
138 (with-test-prefix "only"
139 (pass-if "contents"
140 (equal? '(+)
141 (hash-map->list
142 (lambda (sym var) sym)
143 (module-obarray (resolve-r6rs-interface '(only (guile) +)))))))
144
145 (with-test-prefix "except"
146 ;; In Guile, interfaces can use other interfaces. For R6RS modules
147 ;; that are imported as-is (without `except', etc), Guile will just
148 ;; import them as-is. `(guile)' is one of those modules. For other
149 ;; import kinds like `except', the resolve-r6rs-interface code will
150 ;; go binding-by-binding and create a new flat interface. Anyway,
151 ;; that means to compare an except interface with (guile), we're
152 ;; comparing a flat interface with a deep interface, so we need to
153 ;; do more work to get the set of bindings in (guile), knowing also
154 ;; that some of those bindings could be duplicates.
155 (define (bound-name-count mod)
156 (define (module-for-each/nonlocal f mod)
157 (define (module-and-uses mod)
158 (let lp ((in (list mod)) (out '()))
159 (cond
160 ((null? in) (reverse out))
161 ((memq (car in) out) (lp (cdr in) out))
162 (else (lp (append (module-uses (car in)) (cdr in))
163 (cons (car in) out))))))
164 (for-each (lambda (mod)
165 (module-for-each f mod))
166 (module-and-uses mod)))
167 (hash-fold (lambda (sym var n) (1+ n))
168 0
169 (let ((t (make-hash-table)))
170 (module-for-each/nonlocal (lambda (sym var)
171 (hashq-set! t sym var))
172 mod)
173 t)))
174 (let ((except-+ (resolve-r6rs-interface '(except (guile) +))))
175 (pass-if "contains"
176 (equal? (bound-name-count except-+)
177 (1- (bound-name-count (resolve-interface '(guile))))))
178 (pass-if "does not contain"
179 (not (module-variable except-+ '+)))))
180
181 (with-test-prefix "prefix"
182 (let ((iface (resolve-r6rs-interface '(prefix (ice-9 q) q:))))
183 (pass-if "contains"
184 ((module-ref iface 'q:q?) ((module-ref iface 'q:make-q))))
185 (pass-if "does not contain"
186 (not (module-local-variable iface 'make-q)))))
187
188 (with-test-prefix "rename"
189 (let ((iface (resolve-r6rs-interface
190 '(rename (only (guile) cons car cdr)
191 (cons snoc)
192 (car rac)
193 (cdr rdc)))))
194 (pass-if "contents"
195 (equal? '("rac" "rdc" "snoc")
196 (sort
197 (hash-map->list
198 (lambda (sym var) (symbol->string sym))
199 (module-obarray iface))
200 string<)))
201 (pass-if "contains"
202 (equal? 3 ((module-ref iface 'rac)
203 ((module-ref iface 'snoc) 3 4))))))
204
205 (with-test-prefix "srfi"
206 (pass-if "renaming works"
207 (eq? (resolve-interface '(srfi srfi-1))
208 (resolve-r6rs-interface '(srfi :1)))
209 (eq? (resolve-interface '(srfi srfi-1))
210 (resolve-r6rs-interface '(srfi :1 lists)))))
211
212 (with-test-prefix "macro"
213 (pass-if "multiple clauses"
214 (eval '(begin
215 (import (rnrs) (for (rnrs) expand) (rnrs))
216 #t)
217 (current-module)))))