1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012-2016, 2018-2022 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)))
32 (define (location-alist loc)
33 ;; Return a location alist. In Guile < 3.0.6, LOC is always an alist, but
34 ;; starting with 3.0.6, LOC is a vector (at least when it comes from
35 ;; 'syntax-error' exceptions), hence this conversion.
38 `((line . ,line) (column . ,column)
43 (test-begin "records")
45 (test-assert "define-record-type*"
47 (define-record-type* <foo> foo make-foo
50 (baz foo-baz (default (+ 40 2))))
51 (and (match (foo (bar 1) (baz 2))
53 (match (foo (baz 2) (bar 1))
56 (($ <foo> 1 42) #t)))))
58 (test-assert "define-record-type* with let* behavior"
59 ;; Make sure field initializers can refer to each other as if they were in
62 (define-record-type* <bar> bar make-bar
65 (y bar-y (default (+ 40 2)))
67 (and (match (bar (x 1) (y (+ x 1)) (z (* y 2)))
69 (match (bar (x 7) (z (* x 3)))
70 (($ <bar> 7 42 21) #t))
71 (match (bar (z 21) (x (/ z 3)))
72 (($ <bar> 7 42 21) #t)))))
74 (test-assert "define-record-type* & inherit"
76 (define-record-type* <foo> foo make-foo
79 (baz foo-baz (default (+ 40 2))))
80 (let* ((a (foo (bar 1)))
81 (b (foo (inherit a) (baz 2)))
82 (c (foo (inherit b) (bar -2)))
84 (e (foo (inherit (foo (bar 42))) (baz 77))))
85 (and (match a (($ <foo> 1 42) #t))
86 (match b (($ <foo> 1 2) #t))
87 (match c (($ <foo> -2 2) #t))
89 (match e (($ <foo> 42 77) #t))))))
91 (test-assert "define-record-type* & inherit & let* behavior"
93 (define-record-type* <foo> foo make-foo
96 (baz foo-baz (default (+ 40 2))))
97 (let* ((a (foo (bar 77)))
98 (b (foo (inherit a) (bar 1) (baz (+ bar 1))))
99 (c (foo (inherit b) (baz 2) (bar (- baz 1)))))
100 (and (match a (($ <foo> 77 42) #t))
101 (match b (($ <foo> 1 2) #t))
104 (test-assert "define-record-type* & inherit & innate"
106 (define-record-type* <foo> foo make-foo
108 (bar foo-bar (innate) (default 42)))
109 (let* ((a (foo (bar 1)))
110 (b (foo (inherit a)))
111 (c (foo (inherit a) (bar 3)))
113 (and (match a (($ <foo> 1) #t))
114 (match b (($ <foo> 42) #t))
115 (match c (($ <foo> 3) #t))
116 (match d (($ <foo> 42) #t))))))
118 (test-assert "define-record-type* & thunked"
120 (define-record-type* <foo> foo make-foo
123 (baz foo-baz (thunked)))
127 (baz (begin (set! calls (1+ calls)) 3)))))
129 (equal? (foo-bar x) 2)
130 (equal? (foo-baz x) 3) (= 1 calls)
131 (equal? (foo-baz x) 3) (= 2 calls)))))
133 (test-assert "define-record-type* & thunked & default"
135 (define-record-type* <foo> foo make-foo
138 (baz foo-baz (thunked) (default 42)))
140 (let ((mark (make-parameter #f)))
141 (let ((x (foo (bar 2) (baz (mark))))
143 (and (equal? (foo-bar x) 2)
144 (parameterize ((mark (cons 'a 'b)))
145 (eq? (foo-baz x) (mark)))
146 (equal? (foo-bar y) 2)
147 (equal? (foo-baz y) 42))))))
149 (test-assert "define-record-type* & thunked & inherited"
151 (define-record-type* <foo> foo make-foo
153 (bar foo-bar (thunked))
154 (baz foo-baz (thunked) (default 42)))
156 (let ((mark (make-parameter #f)))
157 (let* ((x (foo (bar 2) (baz (mark))))
158 (y (foo (inherit x) (bar (mark)))))
159 (and (equal? (foo-bar x) 2)
160 (parameterize ((mark (cons 'a 'b)))
161 (eq? (foo-baz x) (mark)))
162 (parameterize ((mark (cons 'a 'b)))
163 (eq? (foo-bar y) (mark)))
164 (parameterize ((mark (cons 'a 'b)))
165 (eq? (foo-baz y) (mark))))))))
167 (test-assert "define-record-type* & thunked & innate"
168 (let ((mark (make-parameter #f)))
169 (define-record-type* <foo> foo make-foo
171 (bar foo-bar (thunked) (innate) (default (mark)))
172 (baz foo-baz (default #f)))
174 (let* ((x (foo (bar 42)))
175 (y (foo (inherit x) (baz 'unused))))
176 (and (procedure? (struct-ref x 0))
177 (equal? (foo-bar x) 42)
178 (parameterize ((mark (cons 'a 'b)))
179 (eq? (foo-bar y) (mark)))
180 (parameterize ((mark (cons 'a 'b)))
181 (eq? (foo-bar y) (mark)))))))
183 (test-assert "define-record-type* & thunked & this-record"
185 (define-record-type* <foo> foo make-foo
188 (baz foo-baz (thunked)))
190 (let ((x (foo (bar 40)
191 (baz (+ (foo-bar this-record) 2)))))
192 (and (= 40 (foo-bar x))
193 (= 42 (foo-baz x))))))
195 (test-assert "define-record-type* & thunked & default & this-record"
197 (define-record-type* <foo> foo make-foo
200 (baz foo-baz (thunked)
201 (default (+ (foo-bar this-record) 2))))
203 (let ((x (foo (bar 40))))
204 (and (= 40 (foo-bar x))
205 (= 42 (foo-baz x))))))
207 (test-assert "define-record-type* & thunked & inherit & this-record"
209 (define-record-type* <foo> foo make-foo
212 (baz foo-baz (thunked)
213 (default (+ (foo-bar this-record) 2))))
215 (let* ((x (foo (bar 40)))
216 (y (foo (inherit x) (bar -2)))
217 (z (foo (inherit x) (baz -2))))
218 (and (= -2 (foo-bar y))
221 (= -2 (foo-baz z))))))
223 (test-assert "define-record-type* & thunked & inherit & custom this"
225 (define-record-type* <foo> foo make-foo
227 (thing foo-thing (thunked)))
228 (define-record-type* <bar> bar make-bar
230 (baz bar-baz (thunked)))
232 ;; Nest records and test the two self references.
233 (let* ((x (foo (thing (bar (baz (list this-bar this-foo))))))
241 (test-assert "define-record-type* & delayed"
243 (define-record-type* <foo> foo make-foo
245 (bar foo-bar (delayed)))
248 (x (foo (bar (begin (set! calls (1+ calls)) 3)))))
250 (equal? (foo-bar x) 3) (= 1 calls)
251 (equal? (foo-bar x) 3) (= 1 calls)
252 (equal? (foo-bar x) 3) (= 1 calls)))))
254 (test-assert "define-record-type* & delayed & default"
256 (define-record-type* <foo> foo make-foo
258 (bar foo-bar (delayed) (default mark)))
262 (and (equal? (foo-bar x) 42)
265 (equal? (foo-bar x) 42))))))
267 (test-assert "define-record-type* & delayed & inherited"
269 (define-record-type* <foo> foo make-foo
271 (bar foo-bar (delayed))
272 (baz foo-baz (delayed)))
276 (x (foo (bar m) (baz n)))
277 (y (foo (inherit x) (baz 'b))))
279 (and (equal? (foo-bar x) 1)
283 (equal? (foo-bar y) 1)) ;promise was already forced
284 (eq? (foo-baz y) 'b)))))
286 (test-assert "define-record-type* & sanitize"
288 (define-record-type* <foo> foo make-foo
292 (sanitize (lambda (x) (string-append x "!")))))
295 (q (foo (inherit p)))
296 (r (foo (inherit p) (bar "baz")))
297 (s (foo (bar "baz"))))
298 (and (string=? (foo-bar p) "bar!")
300 (string=? (foo-bar r) "baz!")
303 (test-equal "define-record-type* & sanitize without default value"
306 (define-record-type* <foo> foo make-foo
308 (bar foo-bar (sanitize 1+)))
310 (foo-bar (foo (bar 41)))))
312 (test-assert "define-record-type* & sanitize & thunked"
314 (define-record-type* <foo> foo make-foo
318 (sanitize (lambda (x)
319 (set! sanitized (+ 1 sanitized))
320 (string-append x "!")))))
323 (and (string=? (foo-bar p) "bar!")
324 (string=? (foo-bar p) "bar!") ;twice
325 (= sanitized 1) ;sanitizer was called at init time only
326 (let ((q (foo (bar "baz"))))
327 (and (string=? (foo-bar q) "baz!")
328 (string=? (foo-bar q) "baz!") ;twice
330 (let ((r (foo (inherit q))))
331 (and (string=? (foo-bar r) "baz!")
332 (= sanitized 2))))))))) ;no re-sanitization
334 (test-assert "define-record-type* & wrong field specifier"
336 (define-record-type* <foo> foo make-foo
338 (bar foo-bar (default 42))
341 (foo (baz 1 2 3 4 5)))) ;syntax error
342 (loc (current-source-location))) ;keep this alignment!
345 (eval exp (test-module))
347 (lambda (key proc message location form subform . _)
349 (string-match "invalid field" message)
350 (equal? subform '(baz 1 2 3 4 5))
351 (equal? form '(foo (baz 1 2 3 4 5)))
353 ;; Make sure the location is that of the field specifier.
354 ;; See <http://bugs.gnu.org/23969>.
357 `((line . ,(- (assq-ref loc 'line) 1))
358 ,@(alist-delete 'line loc)))
359 (pk 'actual-loc (location-alist location))))))))
361 (test-assert "define-record-type* & wrong field specifier, identifier"
363 (define-record-type* <foo> foo make-foo
365 (bar foo-bar (default 42))
370 (loc (current-source-location))) ;keep this alignment!
373 (eval exp (test-module))
375 (lambda (key proc message location form subform . _)
377 (string-match "invalid field" message)
378 (equal? subform 'baz)
379 (equal? form '(foo baz))
381 ;; Here the location is that of the parent form.
384 `((line . ,(- (assq-ref loc 'line) 2))
385 ,@(alist-delete 'line loc)))
386 (pk 'actual-loc (location-alist location))))))))
388 (test-assert "define-record-type* & missing initializers"
392 (define-record-type* <foo> foo make-foo
394 (bar foo-bar (default 42))
400 (lambda (key proc message location form . args)
402 (string-match "missing .*initialize.*baz" message)
403 (equal? form '(foo))))))
405 (test-assert "define-record-type* & extra initializers"
409 (define-record-type* <foo> foo make-foo
411 (bar foo-bar (default 42)))
416 (lambda (key proc message location form . args)
417 (and (string-match "extra.*initializer.*baz" message)
420 (test-assert "define-record-type* & inherit & extra initializers"
424 (define-record-type* <foo> foo make-foo
426 (bar foo-bar (default 42)))
428 (foo (inherit (foo)) (baz 'what?)))
431 (lambda (key proc message location form . args)
432 (and (string-match "extra.*initializer.*baz" message)
435 (test-assert "define-record-type* & duplicate initializers"
437 (define-record-type* <foo> foo make-foo
439 (bar foo-bar (default 42)))
443 (loc (current-source-location))) ;keep this alignment!
446 (eval exp (test-module))
448 (lambda (key proc message location form . args)
449 (and (string-match "duplicate.*initializer" message)
452 ;; Make sure the location is that of the field specifier.
455 `((line . ,(- (assq-ref loc 'line) 1))
456 ,@(alist-delete 'line loc)))
457 (pk 'actual-loc (location-alist location))))))))
459 (test-assert "ABI checks"
460 (let ((module (test-module)))
462 (define-record-type* <foo> foo make-foo
464 (bar foo-bar (default 42)))
466 (define (make-me-a-record) (foo)))
468 (unless (eval '(foo? (make-me-a-record)) module)
469 (error "what?" (eval '(make-me-a-record) module)))
471 ;; Redefine <foo> with an additional field.
472 (eval '(define-record-type* <foo> foo make-foo
475 (bar foo-bar (default 42)))
478 ;; Now 'make-me-a-record' is out of sync because it does an
479 ;; 'allocate-struct' that corresponds to the previous definition of <foo>.
480 (catch 'record-abi-mismatch-error
482 (eval '(foo? (make-me-a-record)) module)
485 ((key 'abi-check (? string? message) (rtd) . _)
486 (eq? rtd (eval '<foo> module)))))))
488 (test-equal "recutils->alist"
491 ("Synopsis" . "foo bar")
492 ("Something_else" . "chbouib"))
494 ("Version" . "1.5")))
495 (let ((p (open-input-string "
496 # Comment following an empty line, and
497 # preceding a couple of empty lines, all of
498 # which should be silently consumed.
503 # Comment right in the middle,
504 # spanning two lines.
506 Something_else: chbouib
508 # Comment right before.
511 # Comment at the end.")))
512 (list (recutils->alist p)
513 (recutils->alist p))))
515 (test-equal "recutils->alist with + lines"
517 ("Description" . "1st line,\n2nd line,\n 3rd line with extra space,\n4th line without space."))
518 (recutils->alist (open-input-string "
520 Description: 1st line,
522 + 3rd line with extra space,
523 +4th line without space.")))
525 (test-equal "alist->record" '((1 2) b c)
526 (alist->record '(("a" . 1) ("b" . b) ("c" . c) ("a" . 2))