rnrs-libraries.test 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. ;;;; rnrs-libraries.test --- test library and import forms -*- scheme -*-
  2. ;;;; Copyright (C) 2010 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 (test-suite 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 (test-suite 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 '(test-suite tests rnrs-test-a))))
  65. (set! iface (resolve-interface '(test-suite tests rnrs-test-a)))
  66. (pass-if "resolve-interface (2)"
  67. (eq? iface (resolve-interface '(test-suite tests rnrs-test-a))))
  68. (pass-if "resolve-r6rs-interface"
  69. (eq? iface (resolve-r6rs-interface '(test-suite tests rnrs-test-a))))
  70. (pass-if "resolve-r6rs-interface (2)"
  71. (eq? iface (resolve-r6rs-interface '(library (test-suite 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. (let ((bindings (hash-map->list
  114. (lambda (sym var) sym)
  115. (module-obarray
  116. (resolve-r6rs-interface '(except (guile) +))))))
  117. (pass-if "contains"
  118. (equal? (length bindings)
  119. (1- (hash-fold
  120. (lambda (sym var n) (1+ n))
  121. 0
  122. (module-obarray (resolve-interface '(guile)))))))
  123. (pass-if "does not contain"
  124. (not (memq '+ bindings)))))
  125. (with-test-prefix "prefix"
  126. (let ((iface (resolve-r6rs-interface '(prefix (ice-9 q) q:))))
  127. (pass-if "contains"
  128. ((module-ref iface 'q:q?) ((module-ref iface 'q:make-q))))
  129. (pass-if "does not contain"
  130. (not (module-local-variable iface 'make-q)))))
  131. (with-test-prefix "rename"
  132. (let ((iface (resolve-r6rs-interface
  133. '(rename (only (guile) cons car cdr)
  134. (cons snoc)
  135. (car rac)
  136. (cdr rdc)))))
  137. (pass-if "contents"
  138. (equal? '("rac" "rdc" "snoc")
  139. (sort
  140. (hash-map->list
  141. (lambda (sym var) (symbol->string sym))
  142. (module-obarray iface))
  143. string<)))
  144. (pass-if "contains"
  145. (equal? 3 ((module-ref iface 'rac)
  146. ((module-ref iface 'snoc) 3 4))))))
  147. (with-test-prefix "srfi"
  148. (pass-if "renaming works"
  149. (eq? (resolve-interface '(srfi srfi-1))
  150. (resolve-r6rs-interface '(srfi :1)))))
  151. (with-test-prefix "macro"
  152. (pass-if "multiple clauses"
  153. (eval '(begin
  154. (import (rnrs) (for (rnrs) expand) (rnrs))
  155. #t)
  156. (current-module)))))