test-library-group.scm 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 #:features '(a b c d))
  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 1+
  180. (lambda (stx)
  181. (syntax-case stx ()
  182. ((_ x) #'(%+ x 1)))))
  183. (define (inc x) (1+ x)))
  184. (import (inc))
  185. (inc 42))
  186. ;; A very silly tree-il->scheme rendering, but it is correct.
  187. (let inc ((x 42))
  188. (+ x 1)))
  189. (test-library-group
  190. (library-group
  191. (include "foo")
  192. (import (foo))
  193. bar)
  194. (let ()
  195. (define bar 42)
  196. bar)
  197. ("foo" (library (foo)
  198. (export bar)
  199. (import (hoot primitives))
  200. (define bar 42))))
  201. ;; Basic guile module.
  202. (test-library-group
  203. (library-group
  204. (include "foo")
  205. (import (foo))
  206. bar)
  207. (let ()
  208. (define bar 42)
  209. bar)
  210. ("foo"
  211. (define-module (foo)
  212. #:use-module (hoot primitives)
  213. #:pure
  214. #:export (bar))
  215. (define bar 42)))
  216. ;; Renaming export.
  217. (test-library-group
  218. (library-group
  219. (include "foo")
  220. (import (foo))
  221. bar)
  222. (let ()
  223. (define baz 42)
  224. baz)
  225. ("foo"
  226. (define-module (foo)
  227. #:use-module (hoot primitives)
  228. #:pure
  229. #:export ((baz . bar)))
  230. (define baz 42)))
  231. ;; Selecting a specific imports.
  232. (test-library-group
  233. (library-group
  234. (include "foo")
  235. (import (foo))
  236. bar)
  237. (let ()
  238. (define bar (+ 42 69))
  239. bar)
  240. ("foo"
  241. (define-module (foo)
  242. #:use-module ((hoot primitives) #:select (define %+))
  243. #:pure
  244. #:export (bar))
  245. (define bar (%+ 42 69))))
  246. ;; Renaming a specific imports.
  247. (test-library-group
  248. (library-group
  249. (include "foo")
  250. (import (foo))
  251. bar)
  252. (let ()
  253. (define bar (+ 42 69))
  254. bar)
  255. ("foo"
  256. (define-module (foo)
  257. #:use-module ((hoot primitives) #:select (define (%+ . +)))
  258. #:pure
  259. #:export (bar))
  260. (define bar (+ 42 69))))
  261. ;; Prefix.
  262. (test-library-group
  263. (library-group
  264. (include "foo")
  265. (import (foo))
  266. bar)
  267. (let ()
  268. (define bar (+ 42 69))
  269. bar)
  270. ("foo"
  271. (define-module (foo)
  272. #:use-module ((hoot primitives) #:select (define (%+ . +)) #:prefix base:)
  273. #:pure
  274. #:export (bar))
  275. (base:define bar (base:+ 42 69))))
  276. ;; symbol-prefix-proc
  277. (test-library-group
  278. (library-group
  279. (include "foo")
  280. (import (foo))
  281. bar)
  282. (let ()
  283. (define bar (+ 42 69))
  284. bar)
  285. ("foo"
  286. (define-module (foo)
  287. #:use-module ((hoot primitives) #:select (define (%+ . +))
  288. #:renamer (symbol-prefix-proc 'base:))
  289. #:pure
  290. #:export (bar))
  291. (base:define bar (base:+ 42 69))))
  292. ;; Hiding definitions.
  293. (test-library-group
  294. (library-group
  295. (include "foo")
  296. (import (foo))
  297. bar)
  298. (let ()
  299. (define (%* n y)
  300. (if (eq? y 1) n (+ n (%* n (- y 1)))))
  301. (define bar (%* 42 10)) bar)
  302. ("foo"
  303. (define-module (foo)
  304. #:use-module ((hoot primitives) #:hide (%*))
  305. #:pure
  306. #:export (bar))
  307. (define (%* n y) (if (%eq? y 1) n (%+ n (%* n (%- y 1)))))
  308. (define bar (%* 42 10))))
  309. ;; The (guile) module, added for impure modules, is not yet supported.
  310. (test-invalid-library-group
  311. (library-group
  312. (include "foo")
  313. (import (foo))
  314. bar)
  315. ("foo"
  316. (define-module (foo)
  317. #:export (bar))
  318. (define bar 42)))
  319. ;; The (guile) module, added for impure modules, is not yet supported.
  320. (test-invalid-library-group
  321. (library-group
  322. (include "foo")
  323. (import (foo))
  324. bar)
  325. ("foo"
  326. (define-module (foo)
  327. #:export (bar))
  328. (define bar 42)))
  329. ;; R7RS libraries.
  330. (test-library-group
  331. (library-group
  332. (define-library (ct10)
  333. (export ten)
  334. (import (hoot primitives))
  335. (begin (define ten 10)))
  336. (define-library (ctplus10)
  337. (export ctplus10)
  338. (import (hoot primitives))
  339. (import (ct10))
  340. (begin (define-syntax ctplus10
  341. (lambda (stx)
  342. (syntax-case stx ()
  343. ((_ a)
  344. (%+ (%syntax->datum #'a) ten)))))))
  345. (import (ctplus10))
  346. (ctplus10 42))
  347. (let ()
  348. (define ten 10)
  349. (define _ (if #f #f)) ;; The ctplus10 binding, not residualized.
  350. 52))
  351. ;; R7RS libraries.
  352. (test-library-group
  353. (library-group
  354. (define-library (conditional)
  355. (export x)
  356. (import (hoot primitives))
  357. (cond-expand
  358. (q (begin (define x 42)))
  359. (a (begin (define x 69)))))
  360. (import (conditional))
  361. x)
  362. (let ()
  363. (define x 69)
  364. x))
  365. (test-library-group
  366. (library-group
  367. (define-library (foo)
  368. (export x)
  369. (import (hoot primitives))
  370. (begin (define x 42)))
  371. (define-library (bar)
  372. (export x)
  373. (import (hoot primitives))
  374. (begin (define x 69)))
  375. (define-library (conditional)
  376. (export y)
  377. (cond-expand
  378. (a (import (hoot primitives) (foo)))
  379. (q (import (hoot primitives) (bar))))
  380. (begin (define y x)))
  381. (import (conditional))
  382. y)
  383. (let ()
  384. (define x 42)
  385. (define x-1 69)
  386. (define y x)
  387. y))
  388. (test-library-group
  389. (library-group
  390. (define-library (conditional)
  391. (export x)
  392. (import (hoot primitives))
  393. (include-library-definitions "foo"))
  394. (import (conditional))
  395. x)
  396. (let ()
  397. (define x 69)
  398. x)
  399. ("foo"
  400. (cond-expand
  401. (q (begin (define x 42)))
  402. (a (begin (define x 69))))))
  403. ;; Duplicate definitions are allowed in Guile modules.
  404. (test-library-group
  405. (library-group
  406. (include "guile")
  407. (include "foo")
  408. (include "bar")
  409. (use-modules (foo) (bar))
  410. baz)
  411. (let ()
  412. (define _ (if #f #f))
  413. (define baz 42)
  414. (define baz 84)
  415. baz)
  416. ("guile"
  417. (define-module (guile)
  418. #:pure
  419. #:use-module ((hoot primitives) #:select (define))
  420. #:re-export (define)))
  421. ("foo"
  422. (define-module (foo)
  423. #:export (baz))
  424. (define baz 42))
  425. ("bar"
  426. (define-module (bar)
  427. #:export (baz))
  428. (define baz 84)))
  429. (test-end* "test-library-group")