test-library-group.scm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 #:key (includes '()))
  26. (define (include-file file)
  27. (or (assoc-ref includes file)
  28. (error "library-group include clause forbidden" file)))
  29. (tree-il->scheme
  30. (expand-library-group
  31. (parse-library-group exp #:include-file include-file)
  32. #:primitives '(hoot primitives))))
  33. (define-syntax-rule (test-library-group exp expanded (file form ...) ...)
  34. (test-equal 'exp 'expanded
  35. (parse-and-expand 'exp #:includes '((file form ...) ...))))
  36. (define-syntax-rule (test-invalid-library-group exp (file form ...) ...)
  37. (test-assert
  38. 'exp
  39. (catch #t
  40. (lambda ()
  41. (parse-and-expand 'exp #:includes '((file form ...) ...))
  42. #f)
  43. (lambda _ #t))))
  44. (test-invalid-library-group 42)
  45. (test-invalid-library-group ())
  46. (test-invalid-library-group '())
  47. (test-library-group
  48. (library-group)
  49. (if #f #f))
  50. (test-library-group
  51. (library-group
  52. (library (foo)
  53. (export a)
  54. (import (only (hoot primitives) define))
  55. (define a 42))
  56. (import (foo))
  57. a)
  58. (let ()
  59. (define a 42)
  60. a))
  61. (test-library-group
  62. (library-group
  63. (library (foo)
  64. (export a)
  65. (import (only (hoot primitives) define))
  66. (define a 42))
  67. (library (bar)
  68. (export b)
  69. (import (only (hoot primitives) define))
  70. (define b 10))
  71. (import (foo) (bar)
  72. (rename (only (hoot primitives) %+)
  73. (%+ +)))
  74. (+ a b))
  75. (let ()
  76. (define a 42)
  77. (define b 10)
  78. (+ a b)))
  79. (test-library-group
  80. (library-group
  81. (library (foo)
  82. (export a)
  83. (import (only (hoot primitives) define))
  84. (define a 42))
  85. (library (bar)
  86. (export a)
  87. (import (only (hoot primitives) define))
  88. (define a 10))
  89. (import (foo)
  90. (rename (bar) (a b))
  91. (rename (only (hoot primitives) %+)
  92. (%+ +)))
  93. (+ a b))
  94. (let ()
  95. (define a-1 42)
  96. (define a 10)
  97. (+ a-1 a)))
  98. (test-invalid-library-group
  99. (library-group
  100. (library (foo)
  101. (export a)
  102. (import (only (hoot primitives) define))
  103. (define a 42))
  104. (library (bar)
  105. (export a)
  106. (import (only (hoot primitives) define))
  107. (define a 10))
  108. #:untrusted
  109. (import (foo)
  110. (rename (bar) (a b))
  111. (rename (only (hoot primitives) %+)
  112. (%+ +)))
  113. (+ a b)))
  114. (test-library-group
  115. (library-group
  116. (library (foo)
  117. (export a)
  118. (import (only (hoot primitives) define))
  119. (define a 42))
  120. (library (bar)
  121. (export a)
  122. (import (only (hoot primitives) define))
  123. (define a 10))
  124. (library (plus)
  125. (export +)
  126. (import (only (hoot primitives) define %+))
  127. (define (+ a b) (%+ a b)))
  128. #:untrusted
  129. (import (foo)
  130. (rename (bar) (a b))
  131. (plus))
  132. (+ a b))
  133. (let ()
  134. (define a-1 42)
  135. (define a 10)
  136. (define (+-1 a b) (+ a b))
  137. (+-1 a-1 a)))
  138. (test-library-group
  139. (library-group
  140. (library (ctplus)
  141. (export (rename ctplus +))
  142. (import (hoot primitives))
  143. (define-syntax ctplus
  144. (lambda (stx)
  145. (syntax-case stx ()
  146. ((_ a b)
  147. (%+ (syntax->datum #'a)
  148. (syntax->datum #'b)))))))
  149. (import (ctplus))
  150. (+ 42 10))
  151. (let ()
  152. (define _ (if #f #f)) ;; The ctplus binding, not residualized.
  153. 52))
  154. (test-library-group
  155. (library-group
  156. (library (ct10)
  157. (export ten)
  158. (import (hoot primitives))
  159. (define ten 10))
  160. (library (ctplus10)
  161. (export ctplus10)
  162. (import (hoot primitives) (ct10))
  163. (define-syntax ctplus10
  164. (lambda (stx)
  165. (syntax-case stx ()
  166. ((_ a)
  167. (%+ (syntax->datum #'a) ten))))))
  168. (import (ctplus10))
  169. (ctplus10 42))
  170. (let ()
  171. (define ten 10)
  172. (define _ (if #f #f)) ;; The ctplus10 binding, not residualized.
  173. 52))
  174. (test-library-group
  175. (library-group
  176. (library (inc)
  177. (export inc)
  178. (import (hoot primitives))
  179. (define-syntax-rule (1+ x)
  180. (%+ x 1))
  181. (define (inc x) (1+ x)))
  182. (import (inc))
  183. (inc 42))
  184. ;; A very silly tree-il->scheme rendering, but it is correct.
  185. (let inc ((x 42))
  186. (+ x 1)))
  187. (test-library-group
  188. (library-group
  189. (include "foo")
  190. (import (foo))
  191. bar)
  192. (let ()
  193. (define bar 42)
  194. bar)
  195. ("foo" (library (foo)
  196. (export bar)
  197. (import (hoot primitives))
  198. (define bar 42))))
  199. ;; Basic guile module.
  200. (test-library-group
  201. (library-group
  202. (include "foo")
  203. (import (foo))
  204. bar)
  205. (let ()
  206. (define bar 42)
  207. bar)
  208. ("foo"
  209. (define-module (foo)
  210. #:use-module (hoot primitives)
  211. #:pure
  212. #:export (bar))
  213. (define bar 42)))
  214. ;; Renaming export.
  215. (test-library-group
  216. (library-group
  217. (include "foo")
  218. (import (foo))
  219. bar)
  220. (let ()
  221. (define baz 42)
  222. baz)
  223. ("foo"
  224. (define-module (foo)
  225. #:use-module (hoot primitives)
  226. #:pure
  227. #:export ((baz . bar)))
  228. (define baz 42)))
  229. ;; Selecting a specific imports.
  230. (test-library-group
  231. (library-group
  232. (include "foo")
  233. (import (foo))
  234. bar)
  235. (let ()
  236. (define bar (+ 42 69))
  237. bar)
  238. ("foo"
  239. (define-module (foo)
  240. #:use-module ((hoot primitives) #:select (define %+))
  241. #:pure
  242. #:export (bar))
  243. (define bar (%+ 42 69))))
  244. ;; Renaming a specific imports.
  245. (test-library-group
  246. (library-group
  247. (include "foo")
  248. (import (foo))
  249. bar)
  250. (let ()
  251. (define bar (+ 42 69))
  252. bar)
  253. ("foo"
  254. (define-module (foo)
  255. #:use-module ((hoot primitives) #:select (define (%+ . +)))
  256. #:pure
  257. #:export (bar))
  258. (define bar (+ 42 69))))
  259. ;; Prefix.
  260. (test-library-group
  261. (library-group
  262. (include "foo")
  263. (import (foo))
  264. bar)
  265. (let ()
  266. (define bar (+ 42 69))
  267. bar)
  268. ("foo"
  269. (define-module (foo)
  270. #:use-module ((hoot primitives) #:select (define (%+ . +)) #:prefix base:)
  271. #:pure
  272. #:export (bar))
  273. (base:define bar (base:+ 42 69))))
  274. ;; symbol-prefix-proc
  275. (test-library-group
  276. (library-group
  277. (include "foo")
  278. (import (foo))
  279. bar)
  280. (let ()
  281. (define bar (+ 42 69))
  282. bar)
  283. ("foo"
  284. (define-module (foo)
  285. #:use-module ((hoot primitives) #:select (define (%+ . +))
  286. #:renamer (symbol-prefix-proc 'base:))
  287. #:pure
  288. #:export (bar))
  289. (base:define bar (base:+ 42 69))))
  290. ;; Hiding definitions.
  291. (test-library-group
  292. (library-group
  293. (include "foo")
  294. (import (foo))
  295. bar)
  296. (let ()
  297. (define (%* n y)
  298. (if (eq? y 1) n (+ n (%* n (- y 1)))))
  299. (define bar (%* 42 10)) bar)
  300. ("foo"
  301. (define-module (foo)
  302. #:use-module ((hoot primitives) #:hide (%*))
  303. #:pure
  304. #:export (bar))
  305. (define (%* n y) (if (%eq? y 1) n (%+ n (%* n (%- y 1)))))
  306. (define bar (%* 42 10))))
  307. ;; The (guile) module, added for impure modules, is not yet supported.
  308. (test-invalid-library-group
  309. (library-group
  310. (include "foo")
  311. (import (foo))
  312. bar)
  313. ("foo"
  314. (define-module (foo)
  315. #:export (bar))
  316. (define bar 42)))
  317. (test-end* "test-library-group")