1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016 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-1)
21 #:use-module (srfi srfi-64)
22 #:use-module (ice-9 match)
23 #:use-module (ice-9 regex)
24 #:use-module (guix records))
27 ;; A module in which to evaluate things that are known to fail.
28 (let ((module (make-fresh-user-module)))
29 (module-use! module (resolve-interface '(guix records)))
33 (test-begin "records")
35 (test-assert "define-record-type*"
37 (define-record-type* <foo> foo make-foo
40 (baz foo-baz (default (+ 40 2))))
41 (and (match (foo (bar 1) (baz 2))
43 (match (foo (baz 2) (bar 1))
46 (($ <foo> 1 42) #t)))))
48 (test-assert "define-record-type* with let* behavior"
49 ;; Make sure field initializers can refer to each other as if they were in
52 (define-record-type* <bar> bar make-bar
55 (y bar-y (default (+ 40 2)))
57 (and (match (bar (x 1) (y (+ x 1)) (z (* y 2)))
59 (match (bar (x 7) (z (* x 3)))
60 (($ <bar> 7 42 21) #t))
61 (match (bar (z 21) (x (/ z 3)))
62 (($ <bar> 7 42 21) #t)))))
64 (test-assert "define-record-type* & inherit"
66 (define-record-type* <foo> foo make-foo
69 (baz foo-baz (default (+ 40 2))))
70 (let* ((a (foo (bar 1)))
71 (b (foo (inherit a) (baz 2)))
72 (c (foo (inherit b) (bar -2)))
74 (e (foo (inherit (foo (bar 42))) (baz 77))))
75 (and (match a (($ <foo> 1 42) #t))
76 (match b (($ <foo> 1 2) #t))
77 (match c (($ <foo> -2 2) #t))
79 (match e (($ <foo> 42 77) #t))))))
81 (test-assert "define-record-type* & inherit & let* behavior"
83 (define-record-type* <foo> foo make-foo
86 (baz foo-baz (default (+ 40 2))))
87 (let* ((a (foo (bar 77)))
88 (b (foo (inherit a) (bar 1) (baz (+ bar 1))))
89 (c (foo (inherit b) (baz 2) (bar (- baz 1)))))
90 (and (match a (($ <foo> 77 42) #t))
91 (match b (($ <foo> 1 2) #t))
94 (test-assert "define-record-type* & inherit & innate"
96 (define-record-type* <foo> foo make-foo
98 (bar foo-bar (innate) (default 42)))
99 (let* ((a (foo (bar 1)))
100 (b (foo (inherit a)))
101 (c (foo (inherit a) (bar 3)))
103 (and (match a (($ <foo> 1) #t))
104 (match b (($ <foo> 42) #t))
105 (match c (($ <foo> 3) #t))
106 (match d (($ <foo> 42) #t))))))
108 (test-assert "define-record-type* & thunked"
110 (define-record-type* <foo> foo make-foo
113 (baz foo-baz (thunked)))
117 (baz (begin (set! calls (1+ calls)) 3)))))
119 (equal? (foo-bar x) 2)
120 (equal? (foo-baz x) 3) (= 1 calls)
121 (equal? (foo-baz x) 3) (= 2 calls)))))
123 (test-assert "define-record-type* & thunked & default"
125 (define-record-type* <foo> foo make-foo
128 (baz foo-baz (thunked) (default 42)))
130 (let ((mark (make-parameter #f)))
131 (let ((x (foo (bar 2) (baz (mark))))
133 (and (equal? (foo-bar x) 2)
134 (parameterize ((mark (cons 'a 'b)))
135 (eq? (foo-baz x) (mark)))
136 (equal? (foo-bar y) 2)
137 (equal? (foo-baz y) 42))))))
139 (test-assert "define-record-type* & thunked & inherited"
141 (define-record-type* <foo> foo make-foo
143 (bar foo-bar (thunked))
144 (baz foo-baz (thunked) (default 42)))
146 (let ((mark (make-parameter #f)))
147 (let* ((x (foo (bar 2) (baz (mark))))
148 (y (foo (inherit x) (bar (mark)))))
149 (and (equal? (foo-bar x) 2)
150 (parameterize ((mark (cons 'a 'b)))
151 (eq? (foo-baz x) (mark)))
152 (parameterize ((mark (cons 'a 'b)))
153 (eq? (foo-bar y) (mark)))
154 (parameterize ((mark (cons 'a 'b)))
155 (eq? (foo-baz y) (mark))))))))
157 (test-assert "define-record-type* & thunked & innate"
158 (let ((mark (make-parameter #f)))
159 (define-record-type* <foo> foo make-foo
161 (bar foo-bar (thunked) (innate) (default (mark)))
162 (baz foo-baz (default #f)))
164 (let* ((x (foo (bar 42)))
165 (y (foo (inherit x) (baz 'unused))))
166 (and (procedure? (struct-ref x 0))
167 (equal? (foo-bar x) 42)
168 (parameterize ((mark (cons 'a 'b)))
169 (eq? (foo-bar y) (mark)))
170 (parameterize ((mark (cons 'a 'b)))
171 (eq? (foo-bar y) (mark)))))))
173 (test-assert "define-record-type* & delayed"
175 (define-record-type* <foo> foo make-foo
177 (bar foo-bar (delayed)))
180 (x (foo (bar (begin (set! calls (1+ calls)) 3)))))
182 (equal? (foo-bar x) 3) (= 1 calls)
183 (equal? (foo-bar x) 3) (= 1 calls)
184 (equal? (foo-bar x) 3) (= 1 calls)))))
186 (test-assert "define-record-type* & delayed & default"
188 (define-record-type* <foo> foo make-foo
190 (bar foo-bar (delayed) (default mark)))
194 (and (equal? (foo-bar x) 42)
197 (equal? (foo-bar x) 42))))))
199 (test-assert "define-record-type* & delayed & inherited"
201 (define-record-type* <foo> foo make-foo
203 (bar foo-bar (delayed))
204 (baz foo-baz (delayed)))
208 (x (foo (bar m) (baz n)))
209 (y (foo (inherit x) (baz 'b))))
211 (and (equal? (foo-bar x) 1)
215 (equal? (foo-bar y) 1)) ;promise was already forced
216 (eq? (foo-baz y) 'b)))))
218 (test-assert "define-record-type* & wrong field specifier"
220 (define-record-type* <foo> foo make-foo
222 (bar foo-bar (default 42))
225 (foo (baz 1 2 3 4 5)))) ;syntax error
226 (loc (current-source-location))) ;keep this alignment!
229 (eval exp (test-module))
231 (lambda (key proc message location form . args)
233 (string-match "invalid field" message)
234 (equal? form '(baz 1 2 3 4 5))
236 ;; Make sure the location is that of the field specifier.
237 ;; See <http://bugs.gnu.org/23969>.
240 `((line . ,(- (assq-ref loc 'line) 1))
241 ,@(alist-delete 'line loc)))
242 (pk 'actual-loc location)))))))
244 (test-assert "define-record-type* & missing initializers"
248 (define-record-type* <foo> foo make-foo
250 (bar foo-bar (default 42))
256 (lambda (key proc message location form . args)
258 (string-match "missing .*initialize.*baz" message)
259 (equal? form '(foo))))))
261 (test-assert "define-record-type* & extra initializers"
265 (define-record-type* <foo> foo make-foo
267 (bar foo-bar (default 42)))
272 (lambda (key proc message location form . args)
273 (and (string-match "extra.*initializer.*baz" message)
276 (test-assert "define-record-type* & inherit & extra initializers"
280 (define-record-type* <foo> foo make-foo
282 (bar foo-bar (default 42)))
284 (foo (inherit (foo)) (baz 'what?)))
287 (lambda (key proc message location form . args)
288 (and (string-match "extra.*initializer.*baz" message)
291 (test-equal "recutils->alist"
294 ("Synopsis" . "foo bar")
295 ("Something_else" . "chbouib"))
297 ("Version" . "1.5")))
298 (let ((p (open-input-string "
299 # Comment following an empty line, and
300 # preceding a couple of empty lines, all of
301 # which should be silently consumed.
306 # Comment right in the middle,
307 # spanning two lines.
309 Something_else: chbouib
311 # Comment right before.
314 # Comment at the end.")))
315 (list (recutils->alist p)
316 (recutils->alist p))))
318 (test-equal "recutils->alist with + lines"
320 ("Description" . "1st line,\n2nd line,\n 3rd line with extra space,\n4th line without space."))
321 (recutils->alist (open-input-string "
323 Description: 1st line,
325 + 3rd line with extra space,
326 +4th line without space.")))
328 (test-equal "alist->record" '((1 2) b c)
329 (alist->record '(("a" . 1) ("b" . b) ("c" . c) ("a" . 2))