records.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2018, 2019, 2020, 2021 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-assert "define-record-type* & sanitize & thunked"
  269. (let ((sanitized 0))
  270. (define-record-type* <foo> foo make-foo
  271. foo?
  272. (bar foo-bar
  273. (default "bar")
  274. (sanitize (lambda (x)
  275. (set! sanitized (+ 1 sanitized))
  276. (string-append x "!")))))
  277. (let ((p (foo)))
  278. (and (string=? (foo-bar p) "bar!")
  279. (string=? (foo-bar p) "bar!") ;twice
  280. (= sanitized 1) ;sanitizer was called at init time only
  281. (let ((q (foo (bar "baz"))))
  282. (and (string=? (foo-bar q) "baz!")
  283. (string=? (foo-bar q) "baz!") ;twice
  284. (= sanitized 2)
  285. (let ((r (foo (inherit q))))
  286. (and (string=? (foo-bar r) "baz!")
  287. (= sanitized 2))))))))) ;no re-sanitization
  288. (test-assert "define-record-type* & wrong field specifier"
  289. (let ((exp '(begin
  290. (define-record-type* <foo> foo make-foo
  291. foo?
  292. (bar foo-bar (default 42))
  293. (baz foo-baz))
  294. (foo (baz 1 2 3 4 5)))) ;syntax error
  295. (loc (current-source-location))) ;keep this alignment!
  296. (catch 'syntax-error
  297. (lambda ()
  298. (eval exp (test-module))
  299. #f)
  300. (lambda (key proc message location form subform . _)
  301. (and (eq? proc 'foo)
  302. (string-match "invalid field" message)
  303. (equal? subform '(baz 1 2 3 4 5))
  304. (equal? form '(foo (baz 1 2 3 4 5)))
  305. ;; Make sure the location is that of the field specifier.
  306. ;; See <http://bugs.gnu.org/23969>.
  307. (lset= equal?
  308. (pk 'expected-loc
  309. `((line . ,(- (assq-ref loc 'line) 1))
  310. ,@(alist-delete 'line loc)))
  311. (pk 'actual-loc (location-alist location))))))))
  312. (test-assert "define-record-type* & wrong field specifier, identifier"
  313. (let ((exp '(begin
  314. (define-record-type* <foo> foo make-foo
  315. foo?
  316. (bar foo-bar (default 42))
  317. (baz foo-baz))
  318. (foo
  319. baz))) ;syntax error
  320. (loc (current-source-location))) ;keep this alignment!
  321. (catch 'syntax-error
  322. (lambda ()
  323. (eval exp (test-module))
  324. #f)
  325. (lambda (key proc message location form subform . _)
  326. (and (eq? proc 'foo)
  327. (string-match "invalid field" message)
  328. (equal? subform 'baz)
  329. (equal? form '(foo baz))
  330. ;; Here the location is that of the parent form.
  331. (lset= equal?
  332. (pk 'expected-loc
  333. `((line . ,(- (assq-ref loc 'line) 2))
  334. ,@(alist-delete 'line loc)))
  335. (pk 'actual-loc (location-alist location))))))))
  336. (test-assert "define-record-type* & missing initializers"
  337. (catch 'syntax-error
  338. (lambda ()
  339. (eval '(begin
  340. (define-record-type* <foo> foo make-foo
  341. foo?
  342. (bar foo-bar (default 42))
  343. (baz foo-baz))
  344. (foo))
  345. (test-module))
  346. #f)
  347. (lambda (key proc message location form . args)
  348. (and (eq? proc 'foo)
  349. (string-match "missing .*initialize.*baz" message)
  350. (equal? form '(foo))))))
  351. (test-assert "define-record-type* & extra initializers"
  352. (catch 'syntax-error
  353. (lambda ()
  354. (eval '(begin
  355. (define-record-type* <foo> foo make-foo
  356. foo?
  357. (bar foo-bar (default 42)))
  358. (foo (baz 'what?)))
  359. (test-module))
  360. #f)
  361. (lambda (key proc message location form . args)
  362. (and (string-match "extra.*initializer.*baz" message)
  363. (eq? proc 'foo)))))
  364. (test-assert "define-record-type* & inherit & extra initializers"
  365. (catch 'syntax-error
  366. (lambda ()
  367. (eval '(begin
  368. (define-record-type* <foo> foo make-foo
  369. foo?
  370. (bar foo-bar (default 42)))
  371. (foo (inherit (foo)) (baz 'what?)))
  372. (test-module))
  373. #f)
  374. (lambda (key proc message location form . args)
  375. (and (string-match "extra.*initializer.*baz" message)
  376. (eq? proc 'foo)))))
  377. (test-assert "define-record-type* & duplicate initializers"
  378. (let ((exp '(begin
  379. (define-record-type* <foo> foo make-foo
  380. foo?
  381. (bar foo-bar (default 42)))
  382. (foo (bar 1)
  383. (bar 2))))
  384. (loc (current-source-location))) ;keep this alignment!
  385. (catch 'syntax-error
  386. (lambda ()
  387. (eval exp (test-module))
  388. #f)
  389. (lambda (key proc message location form . args)
  390. (and (string-match "duplicate.*initializer" message)
  391. (eq? proc 'foo)
  392. ;; Make sure the location is that of the field specifier.
  393. (lset= equal?
  394. (pk 'expected-loc
  395. `((line . ,(- (assq-ref loc 'line) 1))
  396. ,@(alist-delete 'line loc)))
  397. (pk 'actual-loc (location-alist location))))))))
  398. (test-assert "ABI checks"
  399. (let ((module (test-module)))
  400. (eval '(begin
  401. (define-record-type* <foo> foo make-foo
  402. foo?
  403. (bar foo-bar (default 42)))
  404. (define (make-me-a-record) (foo)))
  405. module)
  406. (unless (eval '(foo? (make-me-a-record)) module)
  407. (error "what?" (eval '(make-me-a-record) module)))
  408. ;; Redefine <foo> with an additional field.
  409. (eval '(define-record-type* <foo> foo make-foo
  410. foo?
  411. (baz foo-baz)
  412. (bar foo-bar (default 42)))
  413. module)
  414. ;; Now 'make-me-a-record' is out of sync because it does an
  415. ;; 'allocate-struct' that corresponds to the previous definition of <foo>.
  416. (catch 'record-abi-mismatch-error
  417. (lambda ()
  418. (eval '(foo? (make-me-a-record)) module)
  419. #f)
  420. (match-lambda*
  421. ((key 'abi-check (? string? message) (rtd) . _)
  422. (eq? rtd (eval '<foo> module)))))))
  423. (test-equal "recutils->alist"
  424. '((("Name" . "foo")
  425. ("Version" . "0.1")
  426. ("Synopsis" . "foo bar")
  427. ("Something_else" . "chbouib"))
  428. (("Name" . "bar")
  429. ("Version" . "1.5")))
  430. (let ((p (open-input-string "
  431. # Comment following an empty line, and
  432. # preceding a couple of empty lines, all of
  433. # which should be silently consumed.
  434. Name: foo
  435. Version: 0.1
  436. # Comment right in the middle,
  437. # spanning two lines.
  438. Synopsis: foo bar
  439. Something_else: chbouib
  440. # Comment right before.
  441. Name: bar
  442. Version: 1.5
  443. # Comment at the end.")))
  444. (list (recutils->alist p)
  445. (recutils->alist p))))
  446. (test-equal "recutils->alist with + lines"
  447. '(("Name" . "foo")
  448. ("Description" . "1st line,\n2nd line,\n 3rd line with extra space,\n4th line without space."))
  449. (recutils->alist (open-input-string "
  450. Name: foo
  451. Description: 1st line,
  452. + 2nd line,
  453. + 3rd line with extra space,
  454. +4th line without space.")))
  455. (test-equal "alist->record" '((1 2) b c)
  456. (alist->record '(("a" . 1) ("b" . b) ("c" . c) ("a" . 2))
  457. list
  458. '("a" "b" "c")
  459. '("a")))
  460. (test-end)