records.scm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012-2016, 2018-2022 Ludovic Courtès <ludo@gnu.org>
  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. (define-module (test-records)
  19. #:use-module (srfi srfi-1)
  20. #:use-module (srfi srfi-64)
  21. #:use-module (ice-9 match)
  22. #:use-module (ice-9 regex)
  23. #:use-module (guix records))
  24. (define (test-module)
  25. ;; A module in which to evaluate things that are known to fail.
  26. (let ((module (make-fresh-user-module)))
  27. (module-use! module (resolve-interface '(guix records)))
  28. module))
  29. (define (location-alist loc)
  30. ;; Return a location alist. In Guile < 3.0.6, LOC is always an alist, but
  31. ;; starting with 3.0.6, LOC is a vector (at least when it comes from
  32. ;; 'syntax-error' exceptions), hence this conversion.
  33. (match loc
  34. (#(file line column)
  35. `((line . ,line) (column . ,column)
  36. (filename . ,file)))
  37. (_ loc)))
  38. (test-begin "records")
  39. (test-assert "define-record-type*"
  40. (begin
  41. (define-record-type* <foo> foo make-foo
  42. foo?
  43. (bar foo-bar)
  44. (baz foo-baz (default (+ 40 2))))
  45. (and (match (foo (bar 1) (baz 2))
  46. (($ <foo> 1 2) #t))
  47. (match (foo (baz 2) (bar 1))
  48. (($ <foo> 1 2) #t))
  49. (match (foo (bar 1))
  50. (($ <foo> 1 42) #t)))))
  51. (test-assert "define-record-type* with let* behavior"
  52. ;; Make sure field initializers can refer to each other as if they were in
  53. ;; a 'let*'.
  54. (begin
  55. (define-record-type* <bar> bar make-bar
  56. foo?
  57. (x bar-x)
  58. (y bar-y (default (+ 40 2)))
  59. (z bar-z))
  60. (and (match (bar (x 1) (y (+ x 1)) (z (* y 2)))
  61. (($ <bar> 1 2 4) #t))
  62. (match (bar (x 7) (z (* x 3)))
  63. (($ <bar> 7 42 21) #t))
  64. (match (bar (z 21) (x (/ z 3)))
  65. (($ <bar> 7 42 21) #t)))))
  66. (test-assert "define-record-type* & inherit"
  67. (begin
  68. (define-record-type* <foo> foo make-foo
  69. foo?
  70. (bar foo-bar)
  71. (baz foo-baz (default (+ 40 2))))
  72. (let* ((a (foo (bar 1)))
  73. (b (foo (inherit a) (baz 2)))
  74. (c (foo (inherit b) (bar -2)))
  75. (d (foo (inherit c)))
  76. (e (foo (inherit (foo (bar 42))) (baz 77))))
  77. (and (match a (($ <foo> 1 42) #t))
  78. (match b (($ <foo> 1 2) #t))
  79. (match c (($ <foo> -2 2) #t))
  80. (equal? c d)
  81. (match e (($ <foo> 42 77) #t))))))
  82. (test-assert "define-record-type* & inherit & let* behavior"
  83. (begin
  84. (define-record-type* <foo> foo make-foo
  85. foo?
  86. (bar foo-bar)
  87. (baz foo-baz (default (+ 40 2))))
  88. (let* ((a (foo (bar 77)))
  89. (b (foo (inherit a) (bar 1) (baz (+ bar 1))))
  90. (c (foo (inherit b) (baz 2) (bar (- baz 1)))))
  91. (and (match a (($ <foo> 77 42) #t))
  92. (match b (($ <foo> 1 2) #t))
  93. (equal? b c)))))
  94. (test-assert "define-record-type* & inherit & innate"
  95. (begin
  96. (define-record-type* <foo> foo make-foo
  97. 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)))
  102. (d (foo)))
  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))))))
  107. (test-assert "define-record-type* & thunked"
  108. (begin
  109. (define-record-type* <foo> foo make-foo
  110. foo?
  111. (bar foo-bar)
  112. (baz foo-baz (thunked)))
  113. (let* ((calls 0)
  114. (x (foo (bar 2)
  115. (baz (begin (set! calls (1+ calls)) 3)))))
  116. (and (zero? calls)
  117. (equal? (foo-bar x) 2)
  118. (equal? (foo-baz x) 3) (= 1 calls)
  119. (equal? (foo-baz x) 3) (= 2 calls)))))
  120. (test-assert "define-record-type* & thunked & default"
  121. (begin
  122. (define-record-type* <foo> foo make-foo
  123. foo?
  124. (bar foo-bar)
  125. (baz foo-baz (thunked) (default 42)))
  126. (let ((mark (make-parameter #f)))
  127. (let ((x (foo (bar 2) (baz (mark))))
  128. (y (foo (bar 2))))
  129. (and (equal? (foo-bar x) 2)
  130. (parameterize ((mark (cons 'a 'b)))
  131. (eq? (foo-baz x) (mark)))
  132. (equal? (foo-bar y) 2)
  133. (equal? (foo-baz y) 42))))))
  134. (test-assert "define-record-type* & thunked & inherited"
  135. (begin
  136. (define-record-type* <foo> foo make-foo
  137. foo?
  138. (bar foo-bar (thunked))
  139. (baz foo-baz (thunked) (default 42)))
  140. (let ((mark (make-parameter #f)))
  141. (let* ((x (foo (bar 2) (baz (mark))))
  142. (y (foo (inherit x) (bar (mark)))))
  143. (and (equal? (foo-bar x) 2)
  144. (parameterize ((mark (cons 'a 'b)))
  145. (eq? (foo-baz x) (mark)))
  146. (parameterize ((mark (cons 'a 'b)))
  147. (eq? (foo-bar y) (mark)))
  148. (parameterize ((mark (cons 'a 'b)))
  149. (eq? (foo-baz y) (mark))))))))
  150. (test-assert "define-record-type* & thunked & innate"
  151. (let ((mark (make-parameter #f)))
  152. (define-record-type* <foo> foo make-foo
  153. foo?
  154. (bar foo-bar (thunked) (innate) (default (mark)))
  155. (baz foo-baz (default #f)))
  156. (let* ((x (foo (bar 42)))
  157. (y (foo (inherit x) (baz 'unused))))
  158. (and (procedure? (struct-ref x 0))
  159. (equal? (foo-bar x) 42)
  160. (parameterize ((mark (cons 'a 'b)))
  161. (eq? (foo-bar y) (mark)))
  162. (parameterize ((mark (cons 'a 'b)))
  163. (eq? (foo-bar y) (mark)))))))
  164. (test-assert "define-record-type* & thunked & this-record"
  165. (begin
  166. (define-record-type* <foo> foo make-foo
  167. foo?
  168. (bar foo-bar)
  169. (baz foo-baz (thunked)))
  170. (let ((x (foo (bar 40)
  171. (baz (+ (foo-bar this-record) 2)))))
  172. (and (= 40 (foo-bar x))
  173. (= 42 (foo-baz x))))))
  174. (test-assert "define-record-type* & thunked & default & this-record"
  175. (begin
  176. (define-record-type* <foo> foo make-foo
  177. foo?
  178. (bar foo-bar)
  179. (baz foo-baz (thunked)
  180. (default (+ (foo-bar this-record) 2))))
  181. (let ((x (foo (bar 40))))
  182. (and (= 40 (foo-bar x))
  183. (= 42 (foo-baz x))))))
  184. (test-assert "define-record-type* & thunked & inherit & this-record"
  185. (begin
  186. (define-record-type* <foo> foo make-foo
  187. foo?
  188. (bar foo-bar)
  189. (baz foo-baz (thunked)
  190. (default (+ (foo-bar this-record) 2))))
  191. (let* ((x (foo (bar 40)))
  192. (y (foo (inherit x) (bar -2)))
  193. (z (foo (inherit x) (baz -2))))
  194. (and (= -2 (foo-bar y))
  195. (= 0 (foo-baz y))
  196. (= 40 (foo-bar z))
  197. (= -2 (foo-baz z))))))
  198. (test-assert "define-record-type* & thunked & inherit & custom this"
  199. (let ()
  200. (define-record-type* <foo> foo make-foo
  201. foo? this-foo
  202. (thing foo-thing (thunked)))
  203. (define-record-type* <bar> bar make-bar
  204. bar? this-bar
  205. (baz bar-baz (thunked)))
  206. ;; Nest records and test the two self references.
  207. (let* ((x (foo (thing (bar (baz (list this-bar this-foo))))))
  208. (y (foo-thing x)))
  209. (match (bar-baz y)
  210. ((first second)
  211. (and (eq? second x)
  212. (bar? first)
  213. (eq? first y)))))))
  214. (test-assert "define-record-type* & delayed"
  215. (begin
  216. (define-record-type* <foo> foo make-foo
  217. foo?
  218. (bar foo-bar (delayed)))
  219. (let* ((calls 0)
  220. (x (foo (bar (begin (set! calls (1+ calls)) 3)))))
  221. (and (zero? calls)
  222. (equal? (foo-bar x) 3) (= 1 calls)
  223. (equal? (foo-bar x) 3) (= 1 calls)
  224. (equal? (foo-bar x) 3) (= 1 calls)))))
  225. (test-assert "define-record-type* & delayed & default"
  226. (let ((mark #f))
  227. (define-record-type* <foo> foo make-foo
  228. foo?
  229. (bar foo-bar (delayed) (default mark)))
  230. (let ((x (foo)))
  231. (set! mark 42)
  232. (and (equal? (foo-bar x) 42)
  233. (begin
  234. (set! mark 7)
  235. (equal? (foo-bar x) 42))))))
  236. (test-assert "define-record-type* & delayed & inherited"
  237. (begin
  238. (define-record-type* <foo> foo make-foo
  239. foo?
  240. (bar foo-bar (delayed))
  241. (baz foo-baz (delayed)))
  242. (let* ((m 1)
  243. (n #f)
  244. (x (foo (bar m) (baz n)))
  245. (y (foo (inherit x) (baz 'b))))
  246. (set! n 'a)
  247. (and (equal? (foo-bar x) 1)
  248. (eq? (foo-baz x) 'a)
  249. (begin
  250. (set! m 777)
  251. (equal? (foo-bar y) 1)) ;promise was already forced
  252. (eq? (foo-baz y) 'b)))))
  253. (test-assert "define-record-type* & sanitize"
  254. (begin
  255. (define-record-type* <foo> foo make-foo
  256. foo?
  257. (bar foo-bar
  258. (default "bar")
  259. (sanitize (lambda (x) (string-append x "!")))))
  260. (let* ((p (foo))
  261. (q (foo (inherit p)))
  262. (r (foo (inherit p) (bar "baz")))
  263. (s (foo (bar "baz"))))
  264. (and (string=? (foo-bar p) "bar!")
  265. (equal? q p)
  266. (string=? (foo-bar r) "baz!")
  267. (equal? s r)))))
  268. (test-equal "define-record-type* & sanitize without default value"
  269. 42
  270. (begin
  271. (define-record-type* <foo> foo make-foo
  272. foo?
  273. (bar foo-bar (sanitize 1+)))
  274. (foo-bar (foo (bar 41)))))
  275. (test-assert "define-record-type* & sanitize & thunked"
  276. (let ((sanitized 0))
  277. (define-record-type* <foo> foo make-foo
  278. foo?
  279. (bar foo-bar
  280. (default "bar")
  281. (sanitize (lambda (x)
  282. (set! sanitized (+ 1 sanitized))
  283. (string-append x "!")))))
  284. (let ((p (foo)))
  285. (and (string=? (foo-bar p) "bar!")
  286. (string=? (foo-bar p) "bar!") ;twice
  287. (= sanitized 1) ;sanitizer was called at init time only
  288. (let ((q (foo (bar "baz"))))
  289. (and (string=? (foo-bar q) "baz!")
  290. (string=? (foo-bar q) "baz!") ;twice
  291. (= sanitized 2)
  292. (let ((r (foo (inherit q))))
  293. (and (string=? (foo-bar r) "baz!")
  294. (= sanitized 2))))))))) ;no re-sanitization
  295. (test-assert "define-record-type* & wrong field specifier"
  296. (let ((exp '(begin
  297. (define-record-type* <foo> foo make-foo
  298. foo?
  299. (bar foo-bar (default 42))
  300. (baz foo-baz))
  301. (foo (baz 1 2 3 4 5)))) ;syntax error
  302. (loc (current-source-location))) ;keep this alignment!
  303. (catch 'syntax-error
  304. (lambda ()
  305. (eval exp (test-module))
  306. #f)
  307. (lambda (key proc message location form subform . _)
  308. (and (eq? proc 'foo)
  309. (string-match "invalid field" message)
  310. (equal? subform '(baz 1 2 3 4 5))
  311. (equal? form '(foo (baz 1 2 3 4 5)))
  312. ;; Make sure the location is that of the field specifier.
  313. ;; See <http://bugs.gnu.org/23969>.
  314. (lset= equal?
  315. (pk 'expected-loc
  316. `((line . ,(- (assq-ref loc 'line) 1))
  317. ,@(alist-delete 'line loc)))
  318. (pk 'actual-loc (location-alist location))))))))
  319. (test-assert "define-record-type* & wrong field specifier, identifier"
  320. (let ((exp '(begin
  321. (define-record-type* <foo> foo make-foo
  322. foo?
  323. (bar foo-bar (default 42))
  324. (baz foo-baz))
  325. (foo
  326. baz))) ;syntax error
  327. (loc (current-source-location))) ;keep this alignment!
  328. (catch 'syntax-error
  329. (lambda ()
  330. (eval exp (test-module))
  331. #f)
  332. (lambda (key proc message location form subform . _)
  333. (and (eq? proc 'foo)
  334. (string-match "invalid field" message)
  335. (equal? subform 'baz)
  336. (equal? form '(foo baz))
  337. ;; Here the location is that of the parent form.
  338. (lset= equal?
  339. (pk 'expected-loc
  340. `((line . ,(- (assq-ref loc 'line) 2))
  341. ,@(alist-delete 'line loc)))
  342. (pk 'actual-loc (location-alist location))))))))
  343. (test-assert "define-record-type* & missing initializers"
  344. (catch 'syntax-error
  345. (lambda ()
  346. (eval '(begin
  347. (define-record-type* <foo> foo make-foo
  348. foo?
  349. (bar foo-bar (default 42))
  350. (baz foo-baz))
  351. (foo))
  352. (test-module))
  353. #f)
  354. (lambda (key proc message location form . args)
  355. (and (eq? proc 'foo)
  356. (string-match "missing .*initialize.*baz" message)
  357. (equal? form '(foo))))))
  358. (test-assert "define-record-type* & extra initializers"
  359. (catch 'syntax-error
  360. (lambda ()
  361. (eval '(begin
  362. (define-record-type* <foo> foo make-foo
  363. foo?
  364. (bar foo-bar (default 42)))
  365. (foo (baz 'what?)))
  366. (test-module))
  367. #f)
  368. (lambda (key proc message location form . args)
  369. (and (string-match "extra.*initializer.*baz" message)
  370. (eq? proc 'foo)))))
  371. (test-assert "define-record-type* & inherit & extra initializers"
  372. (catch 'syntax-error
  373. (lambda ()
  374. (eval '(begin
  375. (define-record-type* <foo> foo make-foo
  376. foo?
  377. (bar foo-bar (default 42)))
  378. (foo (inherit (foo)) (baz 'what?)))
  379. (test-module))
  380. #f)
  381. (lambda (key proc message location form . args)
  382. (and (string-match "extra.*initializer.*baz" message)
  383. (eq? proc 'foo)))))
  384. (test-assert "define-record-type* & duplicate initializers"
  385. (let ((exp '(begin
  386. (define-record-type* <foo> foo make-foo
  387. foo?
  388. (bar foo-bar (default 42)))
  389. (foo (bar 1)
  390. (bar 2))))
  391. (loc (current-source-location))) ;keep this alignment!
  392. (catch 'syntax-error
  393. (lambda ()
  394. (eval exp (test-module))
  395. #f)
  396. (lambda (key proc message location form . args)
  397. (and (string-match "duplicate.*initializer" message)
  398. (eq? proc 'foo)
  399. ;; Make sure the location is that of the field specifier.
  400. (lset= equal?
  401. (pk 'expected-loc
  402. `((line . ,(- (assq-ref loc 'line) 1))
  403. ,@(alist-delete 'line loc)))
  404. (pk 'actual-loc (location-alist location))))))))
  405. (test-assert "ABI checks"
  406. (let ((module (test-module)))
  407. (eval '(begin
  408. (define-record-type* <foo> foo make-foo
  409. foo?
  410. (bar foo-bar (default 42)))
  411. (define (make-me-a-record) (foo)))
  412. module)
  413. (unless (eval '(foo? (make-me-a-record)) module)
  414. (error "what?" (eval '(make-me-a-record) module)))
  415. ;; Redefine <foo> with an additional field.
  416. (eval '(define-record-type* <foo> foo make-foo
  417. foo?
  418. (baz foo-baz)
  419. (bar foo-bar (default 42)))
  420. module)
  421. ;; Now 'make-me-a-record' is out of sync because it does an
  422. ;; 'allocate-struct' that corresponds to the previous definition of <foo>.
  423. (catch 'record-abi-mismatch-error
  424. (lambda ()
  425. (eval '(foo? (make-me-a-record)) module)
  426. #f)
  427. (match-lambda*
  428. ((key 'abi-check (? string? message) (rtd) . _)
  429. (eq? rtd (eval '<foo> module)))))))
  430. (test-equal "recutils->alist"
  431. '((("Name" . "foo")
  432. ("Version" . "0.1")
  433. ("Synopsis" . "foo bar")
  434. ("Something_else" . "chbouib"))
  435. (("Name" . "bar")
  436. ("Version" . "1.5")))
  437. (let ((p (open-input-string "
  438. # Comment following an empty line, and
  439. # preceding a couple of empty lines, all of
  440. # which should be silently consumed.
  441. Name: foo
  442. Version: 0.1
  443. # Comment right in the middle,
  444. # spanning two lines.
  445. Synopsis: foo bar
  446. Something_else: chbouib
  447. # Comment right before.
  448. Name: bar
  449. Version: 1.5
  450. # Comment at the end.")))
  451. (list (recutils->alist p)
  452. (recutils->alist p))))
  453. (test-equal "recutils->alist with + lines"
  454. '(("Name" . "foo")
  455. ("Description" . "1st line,\n2nd line,\n 3rd line with extra space,\n4th line without space."))
  456. (recutils->alist (open-input-string "
  457. Name: foo
  458. Description: 1st line,
  459. + 2nd line,
  460. + 3rd line with extra space,
  461. +4th line without space.")))
  462. (test-equal "alist->record" '((1 2) b c)
  463. (alist->record '(("a" . 1) ("b" . b) ("c" . c) ("a" . 2))
  464. list
  465. '("a" "b" "c")
  466. '("a")))
  467. (test-equal "match-record, simple"
  468. '((1 2) (a b))
  469. (let ()
  470. (define-record-type* <foo> foo make-foo
  471. foo?
  472. (first foo-first (default 1))
  473. (second foo-second))
  474. (list (match-record (foo (second 2)) <foo>
  475. (first second)
  476. (list first second))
  477. (match-record (foo (first 'a) (second 'b)) <foo>
  478. (second (first first/new-var))
  479. (list first/new-var second)))))
  480. (test-equal "match-record, unknown field"
  481. 'syntax-error
  482. (catch 'syntax-error
  483. (lambda ()
  484. (eval '(begin
  485. (use-modules (guix records))
  486. (define-record-type* <foo> foo make-foo
  487. foo?
  488. (first foo-first (default 1))
  489. (second foo-second))
  490. (match-record (foo (second 2)) <foo>
  491. (one two)
  492. #f))
  493. (make-fresh-user-module)))
  494. (lambda (key . args) key)))
  495. (test-equal "match-record, delayed field"
  496. "foo bar bar foo"
  497. (begin
  498. (define-record-type* <with-delayed> with-delayed make-with-delayed
  499. with-delayed?
  500. (delayed with-delayed-delayed
  501. (delayed)))
  502. (let ((rec (with-delayed
  503. (delayed "foo bar bar foo"))))
  504. (match-record rec <with-delayed> (delayed)
  505. delayed))))
  506. (test-equal "match-record, thunked field"
  507. '("foo" "foobar")
  508. (begin
  509. (define-record-type* <with-thunked> with-thunked make-with-thunked
  510. with-thunked?
  511. (normal with-thunked-normal)
  512. (thunked with-thunked-thunked
  513. (thunked)))
  514. (let ((rec (with-thunked
  515. (normal "foo")
  516. (thunked (string-append (with-thunked-normal this-record)
  517. "bar")))))
  518. (match-record rec <with-thunked> (normal thunked)
  519. (list normal thunked)))))
  520. (test-equal "match-record, ellipsis in body"
  521. #t
  522. (begin
  523. (define-record-type* <foo> foo make-foo foo?
  524. (value foo-value))
  525. (define bar (foo (value '(1 2 3))))
  526. (match-record bar <foo> (value)
  527. (match value
  528. ((one two ...)
  529. #t)
  530. (_
  531. #f)))))
  532. (test-equal "match-record-lambda"
  533. '("thing: foo" "thing: bar")
  534. (begin
  535. (define-record-type* <with-text> with-text make-with-text
  536. with-text?
  537. (text with-text-text))
  538. (map (match-record-lambda <with-text> (text)
  539. (string-append "thing: " text))
  540. (list (with-text (text "foo"))
  541. (with-text (text "bar"))))))
  542. (test-end)