Commit | Line | Data |
---|---|---|
b860f382 | 1 | ;;; GNU Guix --- Functional package management for GNU |
45adbd62 | 2 | ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org> |
b860f382 LC |
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 store) | |
21 | #:use-module (guix monads) | |
22 | #:use-module (guix derivations) | |
23 | #:use-module ((guix packages) | |
24 | #:select (package-derivation %current-system)) | |
25 | #:use-module (gnu packages) | |
26 | #:use-module (gnu packages bootstrap) | |
4231f05b | 27 | #:use-module ((gnu packages base) #:select (coreutils)) |
b860f382 LC |
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 store) module. | |
35 | ||
36 | (define %store | |
37 | (open-connection)) | |
38 | ||
39 | ;; Make sure we build everything by ourselves. | |
40 | (set-build-options %store #:use-substitutes? #f) | |
41 | ||
42 | (define %monads | |
43 | (list %identity-monad %store-monad)) | |
44 | ||
45 | (define %monad-run | |
46 | (list identity | |
47 | (cut run-with-store %store <>))) | |
48 | ||
49 | \f | |
50 | (test-begin "monads") | |
51 | ||
aeb7ec5c LC |
52 | (test-assert "monad?" |
53 | (and (every monad? %monads) | |
54 | (every (compose procedure? monad-bind) %monads) | |
55 | (every (compose procedure? monad-return) %monads))) | |
56 | ||
b860f382 LC |
57 | ;; The 3 "monad laws": <http://www.haskell.org/haskellwiki/Monad_laws>. |
58 | ||
59 | (test-assert "left identity" | |
60 | (every (lambda (monad run) | |
61 | (let ((number (random 777))) | |
62 | (with-monad monad | |
63 | (define (f x) | |
64 | (return (* (1+ number) 2))) | |
65 | ||
66 | (= (run (>>= (return number) f)) | |
67 | (run (f number)))))) | |
68 | %monads | |
69 | %monad-run)) | |
70 | ||
71 | (test-assert "right identity" | |
72 | (every (lambda (monad run) | |
73 | (with-monad monad | |
74 | (let ((number (return (random 777)))) | |
75 | (= (run (>>= number return)) | |
76 | (run number))))) | |
77 | %monads | |
78 | %monad-run)) | |
79 | ||
80 | (test-assert "associativity" | |
81 | (every (lambda (monad run) | |
82 | (with-monad monad | |
83 | (define (f x) | |
84 | (return (+ 1 x))) | |
85 | (define (g x) | |
86 | (return (* 2 x))) | |
87 | ||
88 | (let ((number (return (random 777)))) | |
89 | (= (run (>>= (>>= number f) g)) | |
90 | (run (>>= number (lambda (x) (>>= (f x) g)))))))) | |
91 | %monads | |
92 | %monad-run)) | |
93 | ||
94 | (test-assert "lift" | |
95 | (every (lambda (monad run) | |
96 | (let ((f (lift1 1+ monad))) | |
97 | (with-monad monad | |
98 | (let ((number (random 777))) | |
99 | (= (run (>>= (return number) f)) | |
100 | (1+ number)))))) | |
101 | %monads | |
102 | %monad-run)) | |
103 | ||
104 | (test-assert "mlet* + text-file + package-file" | |
105 | (run-with-store %store | |
106 | (mlet* %store-monad ((guile (package-file %bootstrap-guile "bin/guile")) | |
107 | (file (text-file "monadic" guile))) | |
108 | (return (equal? (call-with-input-file file get-string-all) | |
109 | guile))) | |
110 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
111 | ||
c90ddc8f LC |
112 | (test-assert "package-file, default system" |
113 | ;; The default system should be the one at '>>=' time, not the one at | |
114 | ;; invocation time. See <http://bugs.gnu.org/18002>. | |
115 | (run-with-store %store | |
116 | (mlet* %store-monad | |
117 | ((system -> (%current-system)) | |
118 | (file (parameterize ((%current-system "foobar64-linux")) | |
119 | (package-file coreutils "bin/ls"))) | |
120 | (cu (package->derivation coreutils))) | |
121 | (return (string=? file | |
122 | (string-append (derivation->output-path cu) | |
123 | "/bin/ls")))) | |
124 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
125 | ||
4231f05b LC |
126 | (test-assert "package-file + package->cross-derivation" |
127 | (run-with-store %store | |
128 | (mlet* %store-monad ((file (package-file coreutils "bin/ls" | |
129 | #:target "foo64-gnu")) | |
130 | (xcu (package->cross-derivation coreutils | |
131 | "foo64-gnu"))) | |
132 | (let ((output (derivation->output-path xcu))) | |
133 | (return (string=? file (string-append output "/bin/ls"))))) | |
134 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
135 | ||
0a90af15 LC |
136 | (test-assert "interned-file" |
137 | (run-with-store %store | |
138 | (mlet* %store-monad ((file -> (search-path %load-path "guix.scm")) | |
139 | (a (interned-file file)) | |
140 | (b (interned-file file "b"))) | |
141 | (return (equal? (call-with-input-file file get-string-all) | |
142 | (call-with-input-file a get-string-all) | |
143 | (call-with-input-file b get-string-all)))) | |
144 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
145 | ||
ada3df03 LC |
146 | (define derivation-expression |
147 | (@@ (guix monads) derivation-expression)) | |
148 | ||
b860f382 LC |
149 | (test-assert "mlet* + derivation-expression" |
150 | (run-with-store %store | |
151 | (mlet* %store-monad ((guile (package-file %bootstrap-guile "bin/guile")) | |
152 | (gdrv (package->derivation %bootstrap-guile)) | |
153 | (exp -> `(let ((out (assoc-ref %outputs "out"))) | |
154 | (mkdir out) | |
155 | (symlink ,guile | |
156 | (string-append out "/guile-rocks")))) | |
dd1a5a15 LC |
157 | (drv (derivation-expression "rocks" exp |
158 | #:inputs | |
159 | `(("g" ,gdrv)))) | |
b860f382 LC |
160 | (out -> (derivation->output-path drv)) |
161 | (built? (built-derivations (list drv)))) | |
162 | (return (and built? | |
163 | (equal? guile | |
164 | (readlink (string-append out "/guile-rocks")))))) | |
165 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
166 | ||
45adbd62 LC |
167 | (test-assert "text-file*" |
168 | (let ((references (store-lift references))) | |
169 | (run-with-store %store | |
170 | (mlet* %store-monad | |
171 | ((drv (package->derivation %bootstrap-guile)) | |
172 | (guile -> (derivation->output-path drv)) | |
173 | (file (text-file "bar" "This is bar.")) | |
174 | (text (text-file* "foo" | |
175 | %bootstrap-guile "/bin/guile " | |
176 | `(,%bootstrap-guile "out") "/bin/guile " | |
177 | drv "/bin/guile " | |
178 | file)) | |
179 | (done (built-derivations (list text))) | |
180 | (out -> (derivation->output-path text)) | |
181 | (refs (references out))) | |
182 | ;; Make sure we get the right references and the right content. | |
183 | (return (and (lset= string=? refs (list guile file)) | |
184 | (equal? (call-with-input-file out get-string-all) | |
185 | (string-append guile "/bin/guile " | |
186 | guile "/bin/guile " | |
187 | guile "/bin/guile " | |
188 | file))))) | |
189 | #:guile-for-build (package-derivation %store %bootstrap-guile)))) | |
190 | ||
b860f382 LC |
191 | (test-assert "mapm" |
192 | (every (lambda (monad run) | |
193 | (with-monad monad | |
194 | (equal? (run (mapm monad (lift1 1+ monad) (map return (iota 10)))) | |
195 | (map 1+ (iota 10))))) | |
196 | %monads | |
197 | %monad-run)) | |
198 | ||
199 | (test-assert "sequence" | |
200 | (every (lambda (monad run) | |
201 | (let* ((input (iota 100)) | |
202 | (order '())) | |
203 | (define (frob i) | |
f62435e2 LC |
204 | (mlet monad ((foo (return 'foo))) |
205 | ;; The side effect here is used to keep track of the order in | |
206 | ;; which monadic values are bound. Perform the side effect | |
207 | ;; within a '>>=' so that it is performed when the return | |
208 | ;; value is actually bound. | |
209 | (set! order (cons i order)) | |
210 | (return i))) | |
b860f382 LC |
211 | |
212 | (and (equal? input | |
f62435e2 | 213 | (run (sequence monad (map frob input)))) |
b860f382 LC |
214 | |
215 | ;; Make sure this is from left to right. | |
216 | (equal? order (reverse input))))) | |
217 | %monads | |
218 | %monad-run)) | |
219 | ||
220 | (test-assert "listm" | |
221 | (every (lambda (monad run) | |
222 | (run (with-monad monad | |
223 | (let ((lst (listm monad | |
224 | (return 1) (return 2) (return 3)))) | |
225 | (mlet monad ((lst lst)) | |
226 | (return (equal? '(1 2 3) lst))))))) | |
227 | %monads | |
228 | %monad-run)) | |
229 | ||
593c3fe6 LC |
230 | (test-assert "anym" |
231 | (every (lambda (monad run) | |
232 | (eq? (run (with-monad monad | |
233 | (let ((lst (list (return 1) (return 2) (return 3)))) | |
234 | (anym monad | |
235 | (lambda (x) | |
236 | (and (odd? x) 'odd!)) | |
237 | lst)))) | |
238 | 'odd!)) | |
239 | %monads | |
240 | %monad-run)) | |
241 | ||
b860f382 LC |
242 | (test-end "monads") |
243 | ||
244 | \f | |
245 | (exit (= (test-runner-fail-count (test-runner-current)) 0)) |