1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 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-utils)
20 #:use-module (guix utils)
21 #:use-module ((guix store) #:select (%store-prefix store-path-package-name))
22 #:use-module (srfi srfi-1)
23 #:use-module (srfi srfi-11)
24 #:use-module (srfi srfi-64)
25 #:use-module (rnrs bytevectors)
26 #:use-module (ice-9 match))
30 (test-assert "bytevector->base16-string->bytevector"
32 (equal? (base16-string->bytevector
33 (bytevector->base16-string bv))
35 (map string->utf8 '("" "f" "fo" "foo" "foob" "fooba" "foobar"))))
37 (test-assert "gnu-triplet->nix-system"
38 (let ((samples '(("i586-gnu0.3" "i686-gnu")
39 ("x86_64-unknown-linux-gnu" "x86_64-linux")
40 ("i386-pc-linux-gnu" "i686-linux")
41 ("x86_64-unknown-freebsd8.2" "x86_64-freebsd")
42 ("x86_64-apple-darwin10.8.0" "x86_64-darwin")
43 ("i686-pc-cygwin" "i686-cygwin"))))
44 (let-values (((gnu nix) (unzip2 samples)))
45 (every (lambda (gnu nix)
46 (equal? nix (gnu-triplet->nix-system gnu)))
49 (test-assert "package-name->name+version"
52 (let*-values (((full-name)
54 (string-append name "-" version)
57 (package-name->name+version full-name)))
58 (and (equal? name* name)
59 (equal? version* version)))))
63 ("guile" "2.0.6.65-134c9") ; as produced by `git-version-gen'
64 ("nixpkgs" "1.0pre22125_a28fe19")
67 (test-assert "define-record-type*"
69 (define-record-type* <foo> foo make-foo
72 (baz foo-baz (default (+ 40 2))))
73 (and (match (foo (bar 1) (baz 2))
75 (match (foo (baz 2) (bar 1))
78 (($ <foo> 1 42) #t)))))
80 (test-assert "define-record-type* with letrec* behavior"
81 ;; Make sure field initializers can refer to each other as if they were in
84 (define-record-type* <bar> bar make-bar
87 (y bar-y (default (+ 40 2)))
89 (and (match (bar (x 1) (y (+ x 1)) (z (* y 2)))
91 (match (bar (x 7) (z (* x 3)))
93 (match (bar (z 21) (x (/ z 3)))
94 (($ <bar> 7 42 21))))))
96 (test-assert "define-record-type* & inherit"
98 (define-record-type* <foo> foo make-foo
101 (baz foo-baz (default (+ 40 2))))
102 (let* ((a (foo (bar 1)))
103 (b (foo (inherit a) (baz 2)))
104 (c (foo (inherit b) (bar -2)))
105 (d (foo (inherit c)))
106 (e (foo (inherit (foo (bar 42))) (baz 77))))
107 (and (match a (($ <foo> 1 42) #t))
108 (match b (($ <foo> 1 2) #t))
109 (match c (($ <foo> -2 2) #t))
111 (match e (($ <foo> 42 77) #t))))))
113 (test-assert "define-record-type* & inherit & letrec* behavior"
115 (define-record-type* <foo> foo make-foo
118 (baz foo-baz (default (+ 40 2))))
119 (let* ((a (foo (bar 77)))
120 (b (foo (inherit a) (bar 1) (baz (+ bar 1))))
121 (c (foo (inherit b) (baz 2) (bar (- baz 1)))))
122 (and (match a (($ <foo> 77 42) #t))
123 (match b (($ <foo> 1 2) #t))
126 (test-assert "define-record-type* & thunked"
128 (define-record-type* <foo> foo make-foo
131 (baz foo-baz (thunked)))
135 (baz (begin (set! calls (1+ calls)) 3)))))
137 (equal? (foo-bar x) 2)
138 (equal? (foo-baz x) 3) (= 1 calls)
139 (equal? (foo-baz x) 3) (= 2 calls)))))
141 (test-assert "define-record-type* & thunked & default"
143 (define-record-type* <foo> foo make-foo
146 (baz foo-baz (thunked) (default 42)))
148 (let ((mark (make-parameter #f)))
149 (let ((x (foo (bar 2) (baz (mark))))
151 (and (equal? (foo-bar x) 2)
152 (parameterize ((mark (cons 'a 'b)))
153 (eq? (foo-baz x) (mark)))
154 (equal? (foo-bar y) 2)
155 (equal? (foo-baz y) 42))))))
157 (test-assert "define-record-type* & thunked & inherited"
159 (define-record-type* <foo> foo make-foo
161 (bar foo-bar (thunked))
162 (baz foo-baz (thunked) (default 42)))
164 (let ((mark (make-parameter #f)))
165 (let* ((x (foo (bar 2) (baz (mark))))
166 (y (foo (inherit x) (bar (mark)))))
167 (and (equal? (foo-bar x) 2)
168 (parameterize ((mark (cons 'a 'b)))
169 (eq? (foo-baz x) (mark)))
170 (parameterize ((mark (cons 'a 'b)))
171 (eq? (foo-bar y) (mark)))
172 (parameterize ((mark (cons 'a 'b)))
173 (eq? (foo-baz y) (mark))))))))
175 ;; This is actually in (guix store).
176 (test-equal "store-path-package-name"
178 (store-path-package-name
179 (string-append (%store-prefix)
180 "/qvs2rj2ia5vci3wsdb7qvydrmacig4pg-bash-4.2-p24")))
185 (exit (= (test-runner-fail-count (test-runner-current)) 0))
188 ;;; eval: (put 'test-assert 'scheme-indent-function 1)