Commit | Line | Data |
---|---|---|
c0cd1b3e | 1 | ;;; GNU Guix --- Functional package management for GNU |
310b32a2 | 2 | ;;; Copyright © 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org> |
c0cd1b3e 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-records) | |
20 | #:use-module (srfi srfi-64) | |
21 | #:use-module (ice-9 match) | |
23e9a680 | 22 | #:use-module (ice-9 regex) |
c0cd1b3e LC |
23 | #:use-module (guix records)) |
24 | ||
23e9a680 LC |
25 | (define (test-module) |
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))) | |
29 | module)) | |
30 | ||
31 | \f | |
c0cd1b3e LC |
32 | (test-begin "records") |
33 | ||
34 | (test-assert "define-record-type*" | |
35 | (begin | |
36 | (define-record-type* <foo> foo make-foo | |
37 | foo? | |
38 | (bar foo-bar) | |
39 | (baz foo-baz (default (+ 40 2)))) | |
40 | (and (match (foo (bar 1) (baz 2)) | |
41 | (($ <foo> 1 2) #t)) | |
42 | (match (foo (baz 2) (bar 1)) | |
43 | (($ <foo> 1 2) #t)) | |
44 | (match (foo (bar 1)) | |
45 | (($ <foo> 1 42) #t))))) | |
46 | ||
59fbeb8c | 47 | (test-assert "define-record-type* with let* behavior" |
c0cd1b3e | 48 | ;; Make sure field initializers can refer to each other as if they were in |
59fbeb8c | 49 | ;; a 'let*'. |
c0cd1b3e LC |
50 | (begin |
51 | (define-record-type* <bar> bar make-bar | |
52 | foo? | |
53 | (x bar-x) | |
54 | (y bar-y (default (+ 40 2))) | |
55 | (z bar-z)) | |
56 | (and (match (bar (x 1) (y (+ x 1)) (z (* y 2))) | |
57 | (($ <bar> 1 2 4) #t)) | |
58 | (match (bar (x 7) (z (* x 3))) | |
59 | (($ <bar> 7 42 21))) | |
60 | (match (bar (z 21) (x (/ z 3))) | |
a1906758 | 61 | (($ <bar> 7 42 21) #t))))) |
c0cd1b3e LC |
62 | |
63 | (test-assert "define-record-type* & inherit" | |
64 | (begin | |
65 | (define-record-type* <foo> foo make-foo | |
66 | foo? | |
67 | (bar foo-bar) | |
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))) | |
72 | (d (foo (inherit c))) | |
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)) | |
77 | (equal? c d) | |
78 | (match e (($ <foo> 42 77) #t)))))) | |
79 | ||
59fbeb8c | 80 | (test-assert "define-record-type* & inherit & let* behavior" |
c0cd1b3e LC |
81 | (begin |
82 | (define-record-type* <foo> foo make-foo | |
83 | foo? | |
84 | (bar foo-bar) | |
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)) | |
91 | (equal? b c))))) | |
92 | ||
93 | (test-assert "define-record-type* & thunked" | |
94 | (begin | |
95 | (define-record-type* <foo> foo make-foo | |
96 | foo? | |
97 | (bar foo-bar) | |
98 | (baz foo-baz (thunked))) | |
99 | ||
100 | (let* ((calls 0) | |
101 | (x (foo (bar 2) | |
102 | (baz (begin (set! calls (1+ calls)) 3))))) | |
103 | (and (zero? calls) | |
104 | (equal? (foo-bar x) 2) | |
105 | (equal? (foo-baz x) 3) (= 1 calls) | |
106 | (equal? (foo-baz x) 3) (= 2 calls))))) | |
107 | ||
108 | (test-assert "define-record-type* & thunked & default" | |
109 | (begin | |
110 | (define-record-type* <foo> foo make-foo | |
111 | foo? | |
112 | (bar foo-bar) | |
113 | (baz foo-baz (thunked) (default 42))) | |
114 | ||
115 | (let ((mark (make-parameter #f))) | |
116 | (let ((x (foo (bar 2) (baz (mark)))) | |
117 | (y (foo (bar 2)))) | |
118 | (and (equal? (foo-bar x) 2) | |
119 | (parameterize ((mark (cons 'a 'b))) | |
120 | (eq? (foo-baz x) (mark))) | |
121 | (equal? (foo-bar y) 2) | |
122 | (equal? (foo-baz y) 42)))))) | |
123 | ||
124 | (test-assert "define-record-type* & thunked & inherited" | |
125 | (begin | |
126 | (define-record-type* <foo> foo make-foo | |
127 | foo? | |
128 | (bar foo-bar (thunked)) | |
129 | (baz foo-baz (thunked) (default 42))) | |
130 | ||
131 | (let ((mark (make-parameter #f))) | |
132 | (let* ((x (foo (bar 2) (baz (mark)))) | |
133 | (y (foo (inherit x) (bar (mark))))) | |
134 | (and (equal? (foo-bar x) 2) | |
135 | (parameterize ((mark (cons 'a 'b))) | |
136 | (eq? (foo-baz x) (mark))) | |
137 | (parameterize ((mark (cons 'a 'b))) | |
138 | (eq? (foo-bar y) (mark))) | |
139 | (parameterize ((mark (cons 'a 'b))) | |
140 | (eq? (foo-baz y) (mark)))))))) | |
141 | ||
310b32a2 LC |
142 | (test-assert "define-record-type* & delayed" |
143 | (begin | |
144 | (define-record-type* <foo> foo make-foo | |
145 | foo? | |
146 | (bar foo-bar (delayed))) | |
147 | ||
148 | (let* ((calls 0) | |
149 | (x (foo (bar (begin (set! calls (1+ calls)) 3))))) | |
150 | (and (zero? calls) | |
151 | (equal? (foo-bar x) 3) (= 1 calls) | |
152 | (equal? (foo-bar x) 3) (= 1 calls) | |
153 | (equal? (foo-bar x) 3) (= 1 calls))))) | |
154 | ||
155 | (test-assert "define-record-type* & delayed & default" | |
156 | (let ((mark #f)) | |
157 | (define-record-type* <foo> foo make-foo | |
158 | foo? | |
159 | (bar foo-bar (delayed) (default mark))) | |
160 | ||
161 | (let ((x (foo))) | |
162 | (set! mark 42) | |
163 | (and (equal? (foo-bar x) 42) | |
164 | (begin | |
165 | (set! mark 7) | |
166 | (equal? (foo-bar x) 42)))))) | |
167 | ||
168 | (test-assert "define-record-type* & delayed & inherited" | |
169 | (begin | |
170 | (define-record-type* <foo> foo make-foo | |
171 | foo? | |
172 | (bar foo-bar (delayed)) | |
173 | (baz foo-baz (delayed))) | |
174 | ||
175 | (let* ((m 1) | |
176 | (n #f) | |
177 | (x (foo (bar m) (baz n))) | |
178 | (y (foo (inherit x) (baz 'b)))) | |
179 | (set! n 'a) | |
180 | (and (equal? (foo-bar x) 1) | |
181 | (eq? (foo-baz x) 'a) | |
182 | (begin | |
183 | (set! m 777) | |
184 | (equal? (foo-bar y) 1)) ;promise was already forced | |
185 | (eq? (foo-baz y) 'b))))) | |
186 | ||
23e9a680 LC |
187 | (test-assert "define-record-type* & missing initializers" |
188 | (catch 'syntax-error | |
189 | (lambda () | |
190 | (eval '(begin | |
191 | (define-record-type* <foo> foo make-foo | |
192 | foo? | |
193 | (bar foo-bar (default 42)) | |
194 | (baz foo-baz)) | |
195 | ||
196 | (foo)) | |
197 | (test-module)) | |
198 | #f) | |
199 | (lambda (key proc message location form . args) | |
200 | (and (eq? proc 'foo) | |
201 | (string-match "missing .*initialize.*baz" message) | |
202 | (equal? form '(foo)))))) | |
203 | ||
204 | (test-assert "define-record-type* & extra initializers" | |
205 | (catch 'syntax-error | |
206 | (lambda () | |
207 | (eval '(begin | |
208 | (define-record-type* <foo> foo make-foo | |
209 | foo? | |
210 | (bar foo-bar (default 42))) | |
211 | ||
212 | (foo (baz 'what?))) | |
213 | (test-module)) | |
214 | #f) | |
215 | (lambda (key proc message location form . args) | |
216 | (and (string-match "extra.*initializer.*baz" message) | |
217 | (eq? proc 'foo))))) | |
218 | ||
a1906758 LC |
219 | (test-assert "define-record-type* & inherit & extra initializers" |
220 | (catch 'syntax-error | |
221 | (lambda () | |
222 | (eval '(begin | |
223 | (define-record-type* <foo> foo make-foo | |
224 | foo? | |
225 | (bar foo-bar (default 42))) | |
226 | ||
227 | (foo (inherit (foo)) (baz 'what?))) | |
228 | (test-module)) | |
229 | #f) | |
230 | (lambda (key proc message location form . args) | |
231 | (and (string-match "extra.*initializer.*baz" message) | |
232 | (eq? proc 'foo))))) | |
233 | ||
fdc1bf65 LC |
234 | (test-equal "recutils->alist" |
235 | '((("Name" . "foo") | |
236 | ("Version" . "0.1") | |
237 | ("Synopsis" . "foo bar") | |
238 | ("Something_else" . "chbouib")) | |
239 | (("Name" . "bar") | |
240 | ("Version" . "1.5"))) | |
b7b88288 LC |
241 | (let ((p (open-input-string " |
242 | # Comment following an empty line, and | |
243 | # preceding a couple of empty lines, all of | |
244 | # which should be silently consumed. | |
245 | ||
246 | ||
247 | Name: foo | |
fdc1bf65 | 248 | Version: 0.1 |
b7b88288 LC |
249 | # Comment right in the middle, |
250 | # spanning two lines. | |
fdc1bf65 LC |
251 | Synopsis: foo bar |
252 | Something_else: chbouib | |
253 | ||
b7b88288 | 254 | # Comment right before. |
fdc1bf65 | 255 | Name: bar |
b7b88288 LC |
256 | Version: 1.5 |
257 | # Comment at the end."))) | |
fdc1bf65 LC |
258 | (list (recutils->alist p) |
259 | (recutils->alist p)))) | |
260 | ||
836d10f1 LC |
261 | (test-equal "recutils->alist with + lines" |
262 | '(("Name" . "foo") | |
263 | ("Description" . "1st line,\n2nd line,\n 3rd line with extra space,\n4th line without space.")) | |
264 | (recutils->alist (open-input-string " | |
265 | Name: foo | |
266 | Description: 1st line, | |
267 | + 2nd line, | |
268 | + 3rd line with extra space, | |
269 | +4th line without space."))) | |
270 | ||
c8772a7a LC |
271 | (test-equal "alist->record" '((1 2) b c) |
272 | (alist->record '(("a" . 1) ("b" . b) ("c" . c) ("a" . 2)) | |
273 | list | |
274 | '("a" "b" "c") | |
275 | '("a"))) | |
276 | ||
c0cd1b3e LC |
277 | (test-end) |
278 | ||
279 | \f | |
280 | (exit (= (test-runner-fail-count (test-runner-current)) 0)) |