test-library-group.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. ;;; Copyright (C) 2024 Igalia, S.L.
  2. ;;;
  3. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;;; you may not use this file except in compliance with the License.
  5. ;;; You may obtain a copy of the License at
  6. ;;;
  7. ;;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;;
  9. ;;; Unless required by applicable law or agreed to in writing, software
  10. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;;; See the License for the specific language governing permissions and
  13. ;;; limitations under the License.
  14. ;;; Commentary:
  15. ;;;
  16. ;;; Tests for library-group.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (srfi srfi-64)
  20. (ice-9 match)
  21. (test utils)
  22. ((language tree-il) #:select (tree-il->scheme))
  23. (hoot library-group))
  24. (test-begin "test-library-group")
  25. (define (parse-and-expand exp)
  26. (tree-il->scheme
  27. (expand-library-group (parse-library-group exp)
  28. #:primitives '(hoot primitives))))
  29. (define-syntax-rule (test-library-group exp expanded)
  30. (test-equal 'exp 'expanded (parse-and-expand 'exp)))
  31. (define-syntax-rule (test-invalid-library-group exp)
  32. (test-assert 'exp
  33. (catch #t
  34. (lambda () (parse-and-expand 'exp) #f)
  35. (lambda _ #t))))
  36. (test-invalid-library-group 42)
  37. (test-invalid-library-group ())
  38. (test-invalid-library-group '())
  39. (test-library-group
  40. (library-group)
  41. (if #f #f))
  42. (test-library-group
  43. (library-group
  44. (library (foo)
  45. (export a)
  46. (import (only (hoot primitives) define))
  47. (define a 42))
  48. (import (foo))
  49. a)
  50. (let ()
  51. (define a 42)
  52. a))
  53. (test-library-group
  54. (library-group
  55. (library (foo)
  56. (export a)
  57. (import (only (hoot primitives) define))
  58. (define a 42))
  59. (library (bar)
  60. (export b)
  61. (import (only (hoot primitives) define))
  62. (define b 10))
  63. (import (foo) (bar)
  64. (rename (only (hoot primitives) %+)
  65. (%+ +)))
  66. (+ a b))
  67. (let ()
  68. (define a 42)
  69. (define b 10)
  70. (+ a b)))
  71. (test-library-group
  72. (library-group
  73. (library (foo)
  74. (export a)
  75. (import (only (hoot primitives) define))
  76. (define a 42))
  77. (library (bar)
  78. (export a)
  79. (import (only (hoot primitives) define))
  80. (define a 10))
  81. (import (foo)
  82. (rename (bar) (a b))
  83. (rename (only (hoot primitives) %+)
  84. (%+ +)))
  85. (+ a b))
  86. (let ()
  87. (define a-1 42)
  88. (define a 10)
  89. (+ a-1 a)))
  90. (test-invalid-library-group
  91. (library-group
  92. (library (foo)
  93. (export a)
  94. (import (only (hoot primitives) define))
  95. (define a 42))
  96. (library (bar)
  97. (export a)
  98. (import (only (hoot primitives) define))
  99. (define a 10))
  100. #:untrusted
  101. (import (foo)
  102. (rename (bar) (a b))
  103. (rename (only (hoot primitives) %+)
  104. (%+ +)))
  105. (+ a b)))
  106. (test-library-group
  107. (library-group
  108. (library (foo)
  109. (export a)
  110. (import (only (hoot primitives) define))
  111. (define a 42))
  112. (library (bar)
  113. (export a)
  114. (import (only (hoot primitives) define))
  115. (define a 10))
  116. (library (plus)
  117. (export +)
  118. (import (only (hoot primitives) define %+))
  119. (define (+ a b) (%+ a b)))
  120. #:untrusted
  121. (import (foo)
  122. (rename (bar) (a b))
  123. (plus))
  124. (+ a b))
  125. (let ()
  126. (define a-1 42)
  127. (define a 10)
  128. (define (+-1 a b) (+ a b))
  129. (+-1 a-1 a)))
  130. (test-library-group
  131. (library-group
  132. (library (ctplus)
  133. (export (rename ctplus +))
  134. (import (hoot primitives))
  135. (define-syntax ctplus
  136. (lambda (stx)
  137. (syntax-case stx ()
  138. ((_ a b)
  139. (%+ (syntax->datum #'a)
  140. (syntax->datum #'b)))))))
  141. (import (ctplus))
  142. (+ 42 10))
  143. (let ()
  144. (define _ (if #f #f)) ;; The ctplus binding, not residualized.
  145. 52))
  146. (test-library-group
  147. (library-group
  148. (library (ct10)
  149. (export ten)
  150. (import (hoot primitives))
  151. (define ten 10))
  152. (library (ctplus10)
  153. (export ctplus10)
  154. (import (hoot primitives) (ct10))
  155. (define-syntax ctplus10
  156. (lambda (stx)
  157. (syntax-case stx ()
  158. ((_ a)
  159. (%+ (syntax->datum #'a) ten))))))
  160. (import (ctplus10))
  161. (ctplus10 42))
  162. (let ()
  163. (define ten 10)
  164. (define _ (if #f #f)) ;; The ctplus10 binding, not residualized.
  165. 52))
  166. (test-library-group
  167. (library-group
  168. (library (inc)
  169. (export inc)
  170. (import (hoot primitives))
  171. (define-syntax-rule (1+ x)
  172. (%+ x 1))
  173. (define (inc x) (1+ x)))
  174. (import (inc))
  175. (inc 42))
  176. ;; A very silly tree-il->scheme rendering, but it is correct.
  177. (let inc ((x 42))
  178. (+ x 1)))
  179. (test-end* "test-library-group")