1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015 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-records)
20 #:use-module (srfi srfi-64)
21 #:use-module (ice-9 match)
22 #:use-module (ice-9 regex)
23 #:use-module (guix records))
26 ;; A module in which to evaluate things that are known to fail.
27 (let ((module (make-fresh-user-module)))
28 (module-use! module (resolve-interface '(guix records)))
32 (test-begin "records")
34 (test-assert "define-record-type*"
36 (define-record-type* <foo> foo make-foo
39 (baz foo-baz (default (+ 40 2))))
40 (and (match (foo (bar 1) (baz 2))
42 (match (foo (baz 2) (bar 1))
45 (($ <foo> 1 42) #t)))))
47 (test-assert "define-record-type* with let* behavior"
48 ;; Make sure field initializers can refer to each other as if they were in
51 (define-record-type* <bar> bar make-bar
54 (y bar-y (default (+ 40 2)))
56 (and (match (bar (x 1) (y (+ x 1)) (z (* y 2)))
58 (match (bar (x 7) (z (* x 3)))
59 (($ <bar> 7 42 21) #t))
60 (match (bar (z 21) (x (/ z 3)))
61 (($ <bar> 7 42 21) #t)))))
63 (test-assert "define-record-type* & inherit"
65 (define-record-type* <foo> foo make-foo
68 (baz foo-baz (default (+ 40 2))))
69 (let* ((a (foo (bar 1)))
70 (b (foo (inherit a) (baz 2)))
71 (c (foo (inherit b) (bar -2)))
73 (e (foo (inherit (foo (bar 42))) (baz 77))))
74 (and (match a (($ <foo> 1 42) #t))
75 (match b (($ <foo> 1 2) #t))
76 (match c (($ <foo> -2 2) #t))
78 (match e (($ <foo> 42 77) #t))))))
80 (test-assert "define-record-type* & inherit & let* behavior"
82 (define-record-type* <foo> foo make-foo
85 (baz foo-baz (default (+ 40 2))))
86 (let* ((a (foo (bar 77)))
87 (b (foo (inherit a) (bar 1) (baz (+ bar 1))))
88 (c (foo (inherit b) (baz 2) (bar (- baz 1)))))
89 (and (match a (($ <foo> 77 42) #t))
90 (match b (($ <foo> 1 2) #t))
93 (test-assert "define-record-type* & inherit & innate"
95 (define-record-type* <foo> foo make-foo
97 (bar foo-bar (innate) (default 42)))
98 (let* ((a (foo (bar 1)))
100 (c (foo (inherit a) (bar 3)))
102 (and (match a (($ <foo> 1) #t))
103 (match b (($ <foo> 42) #t))
104 (match c (($ <foo> 3) #t))
105 (match d (($ <foo> 42) #t))))))
107 (test-assert "define-record-type* & thunked"
109 (define-record-type* <foo> foo make-foo
112 (baz foo-baz (thunked)))
116 (baz (begin (set! calls (1+ calls)) 3)))))
118 (equal? (foo-bar x) 2)
119 (equal? (foo-baz x) 3) (= 1 calls)
120 (equal? (foo-baz x) 3) (= 2 calls)))))
122 (test-assert "define-record-type* & thunked & default"
124 (define-record-type* <foo> foo make-foo
127 (baz foo-baz (thunked) (default 42)))
129 (let ((mark (make-parameter #f)))
130 (let ((x (foo (bar 2) (baz (mark))))
132 (and (equal? (foo-bar x) 2)
133 (parameterize ((mark (cons 'a 'b)))
134 (eq? (foo-baz x) (mark)))
135 (equal? (foo-bar y) 2)
136 (equal? (foo-baz y) 42))))))
138 (test-assert "define-record-type* & thunked & inherited"
140 (define-record-type* <foo> foo make-foo
142 (bar foo-bar (thunked))
143 (baz foo-baz (thunked) (default 42)))
145 (let ((mark (make-parameter #f)))
146 (let* ((x (foo (bar 2) (baz (mark))))
147 (y (foo (inherit x) (bar (mark)))))
148 (and (equal? (foo-bar x) 2)
149 (parameterize ((mark (cons 'a 'b)))
150 (eq? (foo-baz x) (mark)))
151 (parameterize ((mark (cons 'a 'b)))
152 (eq? (foo-bar y) (mark)))
153 (parameterize ((mark (cons 'a 'b)))
154 (eq? (foo-baz y) (mark))))))))
156 (test-assert "define-record-type* & thunked & innate"
157 (let ((mark (make-parameter #f)))
158 (define-record-type* <foo> foo make-foo
160 (bar foo-bar (thunked) (innate) (default (mark)))
161 (baz foo-baz (default #f)))
163 (let* ((x (foo (bar 42)))
164 (y (foo (inherit x) (baz 'unused))))
165 (and (procedure? (struct-ref x 0))
166 (equal? (foo-bar x) 42)
167 (parameterize ((mark (cons 'a 'b)))
168 (eq? (foo-bar y) (mark)))
169 (parameterize ((mark (cons 'a 'b)))
170 (eq? (foo-bar y) (mark)))))))
172 (test-assert "define-record-type* & delayed"
174 (define-record-type* <foo> foo make-foo
176 (bar foo-bar (delayed)))
179 (x (foo (bar (begin (set! calls (1+ calls)) 3)))))
181 (equal? (foo-bar x) 3) (= 1 calls)
182 (equal? (foo-bar x) 3) (= 1 calls)
183 (equal? (foo-bar x) 3) (= 1 calls)))))
185 (test-assert "define-record-type* & delayed & default"
187 (define-record-type* <foo> foo make-foo
189 (bar foo-bar (delayed) (default mark)))
193 (and (equal? (foo-bar x) 42)
196 (equal? (foo-bar x) 42))))))
198 (test-assert "define-record-type* & delayed & inherited"
200 (define-record-type* <foo> foo make-foo
202 (bar foo-bar (delayed))
203 (baz foo-baz (delayed)))
207 (x (foo (bar m) (baz n)))
208 (y (foo (inherit x) (baz 'b))))
210 (and (equal? (foo-bar x) 1)
214 (equal? (foo-bar y) 1)) ;promise was already forced
215 (eq? (foo-baz y) 'b)))))
217 (test-assert "define-record-type* & missing initializers"
221 (define-record-type* <foo> foo make-foo
223 (bar foo-bar (default 42))
229 (lambda (key proc message location form . args)
231 (string-match "missing .*initialize.*baz" message)
232 (equal? form '(foo))))))
234 (test-assert "define-record-type* & extra initializers"
238 (define-record-type* <foo> foo make-foo
240 (bar foo-bar (default 42)))
245 (lambda (key proc message location form . args)
246 (and (string-match "extra.*initializer.*baz" message)
249 (test-assert "define-record-type* & inherit & extra initializers"
253 (define-record-type* <foo> foo make-foo
255 (bar foo-bar (default 42)))
257 (foo (inherit (foo)) (baz 'what?)))
260 (lambda (key proc message location form . args)
261 (and (string-match "extra.*initializer.*baz" message)
264 (test-equal "recutils->alist"
267 ("Synopsis" . "foo bar")
268 ("Something_else" . "chbouib"))
270 ("Version" . "1.5")))
271 (let ((p (open-input-string "
272 # Comment following an empty line, and
273 # preceding a couple of empty lines, all of
274 # which should be silently consumed.
279 # Comment right in the middle,
280 # spanning two lines.
282 Something_else: chbouib
284 # Comment right before.
287 # Comment at the end.")))
288 (list (recutils->alist p)
289 (recutils->alist p))))
291 (test-equal "recutils->alist with + lines"
293 ("Description" . "1st line,\n2nd line,\n 3rd line with extra space,\n4th line without space."))
294 (recutils->alist (open-input-string "
296 Description: 1st line,
298 + 3rd line with extra space,
299 +4th line without space.")))
301 (test-equal "alist->record" '((1 2) b c)
302 (alist->record '(("a" . 1) ("b" . b) ("c" . c) ("a" . 2))
310 (exit (= (test-runner-fail-count (test-runner-current)) 0))