rnrs-libraries.test 7.3 KB

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