1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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 #:select (package-derivation %current-system))
26 #:use-module (gnu packages)
27 #:use-module (gnu packages bootstrap)
28 #:use-module ((gnu packages base) #:select (coreutils))
29 #:use-module (ice-9 match)
30 #:use-module (rnrs io ports)
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-26)
33 #:use-module (srfi srfi-64))
35 ;; Test the (guix monads) module.
38 (open-connection-for-tests))
41 (list %identity-monad %store-monad))
45 (cut run-with-store %store <>)))
51 (and (every monad? %monads)
52 (every (compose procedure? monad-bind) %monads)
53 (every (compose procedure? monad-return) %monads)))
55 ;; The 3 "monad laws": <http://www.haskell.org/haskellwiki/Monad_laws>.
57 (test-assert "left identity"
58 (every (lambda (monad run)
59 (let ((number (random 777)))
62 (return (* (1+ number) 2)))
64 (= (run (>>= (return number) f))
69 (test-assert "right identity"
70 (every (lambda (monad run)
72 (let ((number (return (random 777))))
73 (= (run (>>= number return))
78 (test-assert "associativity"
79 (every (lambda (monad run)
86 (let ((number (return (random 777))))
87 (= (run (>>= (>>= number f) g))
88 (run (>>= number (lambda (x) (>>= (f x) g))))))))
93 (every (lambda (monad run)
94 (let ((f (lift1 1+ monad)))
96 (let ((number (random 777)))
97 (= (run (>>= (return number) f))
102 (test-assert "mbegin"
103 (every (lambda (monad run)
105 (let* ((been-there? #f)
106 (number (mbegin monad
109 (set! been-there? #t)
112 (and (= (run number) 3)
117 (test-assert "mlet* + text-file + package-file"
118 (run-with-store %store
119 (mlet* %store-monad ((guile (package-file %bootstrap-guile "bin/guile"))
120 (file (text-file "monadic" guile)))
121 (return (equal? (call-with-input-file file get-string-all)
123 #:guile-for-build (package-derivation %store %bootstrap-guile)))
125 (test-assert "package-file, default system"
126 ;; The default system should be the one at '>>=' time, not the one at
127 ;; invocation time. See <http://bugs.gnu.org/18002>.
128 (run-with-store %store
130 ((system -> (%current-system))
131 (file (parameterize ((%current-system "foobar64-linux"))
132 (package-file coreutils "bin/ls")))
133 (cu (package->derivation coreutils)))
134 (return (string=? file
135 (string-append (derivation->output-path cu)
137 #:guile-for-build (package-derivation %store %bootstrap-guile)))
139 (test-assert "package-file + package->cross-derivation"
140 (run-with-store %store
141 (mlet* %store-monad ((target -> "mips64el-linux-gnu")
142 (file (package-file coreutils "bin/ls"
144 (xcu (package->cross-derivation coreutils target)))
145 (let ((output (derivation->output-path xcu)))
146 (return (string=? file (string-append output "/bin/ls")))))
147 #:guile-for-build (package-derivation %store %bootstrap-guile)))
149 (test-assert "interned-file"
150 (run-with-store %store
151 (mlet* %store-monad ((file -> (search-path %load-path "guix.scm"))
152 (a (interned-file file))
153 (b (interned-file file "b")))
154 (return (equal? (call-with-input-file file get-string-all)
155 (call-with-input-file a get-string-all)
156 (call-with-input-file b get-string-all))))
157 #:guile-for-build (package-derivation %store %bootstrap-guile)))
159 (define derivation-expression
160 (@@ (guix monads) derivation-expression))
162 (test-assert "mlet* + derivation-expression"
163 (run-with-store %store
164 (mlet* %store-monad ((guile (package-file %bootstrap-guile "bin/guile"))
165 (gdrv (package->derivation %bootstrap-guile))
166 (exp -> `(let ((out (assoc-ref %outputs "out")))
169 (string-append out "/guile-rocks"))))
170 (drv (derivation-expression "rocks" exp
173 (out -> (derivation->output-path drv))
174 (built? (built-derivations (list drv))))
177 (readlink (string-append out "/guile-rocks"))))))
178 #:guile-for-build (package-derivation %store %bootstrap-guile)))
180 (test-assert "text-file*"
181 (let ((references (store-lift references)))
182 (run-with-store %store
184 ((drv (package->derivation %bootstrap-guile))
185 (guile -> (derivation->output-path drv))
186 (file (text-file "bar" "This is bar."))
187 (text (text-file* "foo"
188 %bootstrap-guile "/bin/guile "
189 `(,%bootstrap-guile "out") "/bin/guile "
192 (done (built-derivations (list text)))
193 (out -> (derivation->output-path text))
194 (refs (references out)))
195 ;; Make sure we get the right references and the right content.
196 (return (and (lset= string=? refs (list guile file))
197 (equal? (call-with-input-file out get-string-all)
198 (string-append guile "/bin/guile "
202 #:guile-for-build (package-derivation %store %bootstrap-guile))))
205 (every (lambda (monad run)
207 (equal? (run (mapm monad (lift1 1+ monad) (map return (iota 10))))
208 (map 1+ (iota 10)))))
212 (test-assert "sequence"
213 (every (lambda (monad run)
214 (let* ((input (iota 100))
217 (mlet monad ((foo (return 'foo)))
218 ;; The side effect here is used to keep track of the order in
219 ;; which monadic values are bound. Perform the side effect
220 ;; within a '>>=' so that it is performed when the return
221 ;; value is actually bound.
222 (set! order (cons i order))
226 (run (sequence monad (map frob input))))
228 ;; Make sure this is from left to right.
229 (equal? order (reverse input)))))
234 (every (lambda (monad run)
235 (run (with-monad monad
236 (let ((lst (listm monad
237 (return 1) (return 2) (return 3))))
238 (mlet monad ((lst lst))
239 (return (equal? '(1 2 3) lst)))))))
244 (every (lambda (monad run)
245 (eq? (run (with-monad monad
246 (let ((lst (list (return 1) (return 2) (return 3))))
249 (and (odd? x) 'odd!))
258 (exit (= (test-runner-fail-count (test-runner-current)) 0))