projects/guix·blob javascript-team · refs

270 lines · 9.3 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2013, 2014, 2015, 2016, 2022 Ludovic Courtès <[email protected]>
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (test-monads)
20 #:use-module (guix tests)
21 #:use-module (guix store)
22 #:use-module (guix monads)
23 #:use-module (guix derivations)
24 #:use-module (guix packages)
25 #:use-module (gnu packages)
26 #:use-module (gnu packages bootstrap)
27 #:use-module ((gnu packages base) #:select (coreutils))
28 #:use-module (ice-9 match)
29 #:use-module (rnrs io ports)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-26)
32 #:use-module (srfi srfi-64))
33
34;; Test the (guix monads) module.
35
36(define %store
37 (open-connection-for-tests))
38
39;; Globally disable grafts because they can trigger early builds.
40(%graft? #f)
41
42(define %monads
43 (list %identity-monad %store-monad %state-monad))
44
45(define %monad-run
46 (list identity
47 (cut run-with-store %store <>)
48 (cut run-with-state <> '())))
49
50(define-syntax-rule (values->list exp)
51 (call-with-values (lambda () exp)
52 list))
53
54
55(test-begin "monads")
56
57(test-assert "monad?"
58 (and (every monad? %monads)
59 (every (compose procedure? monad-bind) %monads)
60 (every (compose procedure? monad-return) %monads)))
61
62;; The 3 "monad laws": <http://www.haskell.org/haskellwiki/Monad_laws>.
63
64(test-assert "left identity"
65 (every (lambda (monad run)
66 (let ((number (random 777)))
67 (with-monad monad
68 (define (f x)
69 (return (* (1+ number) 2)))
70
71 (= (run (>>= (return number) f))
72 (run (f number))))))
73 %monads
74 %monad-run))
75
76(test-assert "right identity"
77 (every (lambda (monad run)
78 (with-monad monad
79 (let ((number (return (random 777))))
80 (= (run (>>= number return))
81 (run number)))))
82 %monads
83 %monad-run))
84
85(test-assert "associativity"
86 (every (lambda (monad run)
87 (with-monad monad
88 (define (f x)
89 (return (+ 1 x)))
90 (define (g x)
91 (return (* 2 x)))
92
93 (let ((number (return (random 777))))
94 (= (run (>>= (>>= number f) g))
95 (run (>>= number (lambda (x) (>>= (f x) g))))))))
96 %monads
97 %monad-run))
98
99(test-assert "lift"
100 (every (lambda (monad run)
101 (let ((f (lift1 1+ monad))
102 (g (apply lift1 1+ (list monad))))
103 (with-monad monad
104 (let ((number (random 777)))
105 (= (run (>>= (return number) f))
106 (run (>>= (return number) g))
107 (1+ number))))))
108 %monads
109 %monad-run))
110
111(test-assert ">>= with more than two arguments"
112 (every (lambda (monad run)
113 (let ((1+ (lift1 1+ monad))
114 (2* (lift1 (cut * 2 <>) monad)))
115 (with-monad monad
116 (let ((number (random 777)))
117 (= (run (>>= (return number)
118 1+ 1+ 1+
119 2* 2* 2*))
120 (* 8 (+ number 3)))))))
121 %monads
122 %monad-run))
123
124(test-assert "mbegin"
125 (every (lambda (monad run)
126 (with-monad monad
127 (let* ((been-there? #f)
128 (number (mbegin monad
129 (return 1)
130 (begin
131 (set! been-there? #t)
132 (return 2))
133 (return 3))))
134 (and (= (run number) 3)
135 been-there?))))
136 %monads
137 %monad-run))
138
139(test-assert "state-parameterize"
140 (let ((parameter (make-parameter 'outside)))
141 (equal?
142 (run-with-state
143 (mlet %state-monad ((outer (return (parameter)))
144 (inner
145 (state-parameterize ((parameter 'inside))
146 (return (parameter)))))
147 (return (list outer inner (parameter)))))
148 '(outside inside outside))))
149
150(test-assert "mlet* + text-file + package-file"
151 (run-with-store %store
152 (mlet* %store-monad ((guile (package-file %bootstrap-guile "bin/guile"))
153 (file (text-file "monadic" guile)))
154 (return (equal? (call-with-input-file file get-string-all)
155 guile)))
156 #:guile-for-build (package-derivation %store %bootstrap-guile)))
157
158(test-assert "package-file, default system"
159 ;; The default system should be the one at '>>=' time, not the one at
160 ;; invocation time. See <http://bugs.gnu.org/18002>.
161 (run-with-store %store
162 (mlet* %store-monad
163 ((system -> (%current-system))
164 (file (parameterize ((%current-system "foobar64-linux"))
165 (package-file coreutils "bin/ls")))
166 (cu (package->derivation coreutils)))
167 (return (string=? file
168 (string-append (derivation->output-path cu)
169 "/bin/ls"))))
170 #:guile-for-build (package-derivation %store %bootstrap-guile)))
171
172(test-assert "package-file + package->cross-derivation"
173 (run-with-store %store
174 (mlet* %store-monad ((target -> "mips64el-linux-gnu")
175 (file (package-file coreutils "bin/ls"
176 #:target target))
177 (xcu (package->cross-derivation coreutils target)))
178 (let ((output (derivation->output-path xcu)))
179 (return (string=? file (string-append output "/bin/ls")))))
180 #:guile-for-build (package-derivation %store %bootstrap-guile)))
181
182(test-assert "interned-file"
183 (run-with-store %store
184 (mlet* %store-monad ((file -> (search-path %load-path "guix.scm"))
185 (a (interned-file file))
186 (b (interned-file file "b")))
187 (return (equal? (call-with-input-file file get-string-all)
188 (call-with-input-file a get-string-all)
189 (call-with-input-file b get-string-all))))
190 #:guile-for-build (package-derivation %store %bootstrap-guile)))
191
192(test-assert "mapm"
193 (every (lambda (monad run)
194 (with-monad monad
195 (equal? (run (mapm monad (lift1 1+ monad) (iota 10)))
196 (map 1+ (iota 10)))))
197 %monads
198 %monad-run))
199
200(test-assert "sequence"
201 (every (lambda (monad run)
202 (let* ((input (iota 100))
203 (order '()))
204 (define (frob i)
205 (mlet monad ((foo (return 'foo)))
206 ;; The side effect here is used to keep track of the order in
207 ;; which monadic values are bound. Perform the side effect
208 ;; within a '>>=' so that it is performed when the return
209 ;; value is actually bound.
210 (set! order (cons i order))
211 (return i)))
212
213 (and (equal? input
214 (run (sequence monad (map frob input))))
215
216 ;; Make sure this is from left to right.
217 (equal? order (reverse input)))))
218 %monads
219 %monad-run))
220
221(test-assert "listm"
222 (every (lambda (monad run)
223 (run (with-monad monad
224 (let ((lst (listm monad
225 (return 1) (return 2) (return 3))))
226 (mlet monad ((lst lst))
227 (return (equal? '(1 2 3) lst)))))))
228 %monads
229 %monad-run))
230
231(test-assert "anym"
232 (every (lambda (monad run)
233 (eq? (run (with-monad monad
234 (anym monad
235 (lift1 (lambda (x)
236 (and (odd? x) 'odd!))
237 monad)
238 (append (make-list 1000 0)
239 (list 1 2)))))
240 'odd!))
241 %monads
242 %monad-run))
243
244(test-equal "set-current-state"
245 (list '(a a d) 'd)
246 (values->list
247 (run-with-state
248 (mlet* %state-monad ((init (current-state))
249 (init2 (set-current-state 'b)))
250 (mbegin %state-monad
251 (set-current-state 'c)
252 (set-current-state 'd)
253 (mlet %state-monad ((last (current-state)))
254 (return (list init init2 last)))))
255 'a)))
256
257(test-equal "state-push etc."
258 (list '((z . 2) (p . (1)) (a . (1))) '(2 1))
259 (values->list
260 (run-with-state
261 (mbegin %state-monad
262 (state-push 1) ;(1)
263 (state-push 2) ;(2 1)
264 (mlet* %state-monad ((z (state-pop)) ;(1)
265 (p (current-state))
266 (a (state-push z))) ;(2 1)
267 (return `((z . ,z) (p . ,p) (a . ,a)))))
268 '())))
269
270(test-end "monads")