records.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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* & wrong field specifier"
  254. (let ((exp '(begin
  255. (define-record-type* <foo> foo make-foo
  256. foo?
  257. (bar foo-bar (default 42))
  258. (baz foo-baz))
  259. (foo (baz 1 2 3 4 5)))) ;syntax error
  260. (loc (current-source-location))) ;keep this alignment!
  261. (catch 'syntax-error
  262. (lambda ()
  263. (eval exp (test-module))
  264. #f)
  265. (lambda (key proc message location form subform . _)
  266. (and (eq? proc 'foo)
  267. (string-match "invalid field" message)
  268. (equal? subform '(baz 1 2 3 4 5))
  269. (equal? form '(foo (baz 1 2 3 4 5)))
  270. ;; Make sure the location is that of the field specifier.
  271. ;; See <http://bugs.gnu.org/23969>.
  272. (lset= equal?
  273. (pk 'expected-loc
  274. `((line . ,(- (assq-ref loc 'line) 1))
  275. ,@(alist-delete 'line loc)))
  276. (pk 'actual-loc (location-alist location))))))))
  277. (test-assert "define-record-type* & wrong field specifier, identifier"
  278. (let ((exp '(begin
  279. (define-record-type* <foo> foo make-foo
  280. foo?
  281. (bar foo-bar (default 42))
  282. (baz foo-baz))
  283. (foo
  284. baz))) ;syntax error
  285. (loc (current-source-location))) ;keep this alignment!
  286. (catch 'syntax-error
  287. (lambda ()
  288. (eval exp (test-module))
  289. #f)
  290. (lambda (key proc message location form subform . _)
  291. (and (eq? proc 'foo)
  292. (string-match "invalid field" message)
  293. (equal? subform 'baz)
  294. (equal? form '(foo baz))
  295. ;; Here the location is that of the parent form.
  296. (lset= equal?
  297. (pk 'expected-loc
  298. `((line . ,(- (assq-ref loc 'line) 2))
  299. ,@(alist-delete 'line loc)))
  300. (pk 'actual-loc (location-alist location))))))))
  301. (test-assert "define-record-type* & missing initializers"
  302. (catch 'syntax-error
  303. (lambda ()
  304. (eval '(begin
  305. (define-record-type* <foo> foo make-foo
  306. foo?
  307. (bar foo-bar (default 42))
  308. (baz foo-baz))
  309. (foo))
  310. (test-module))
  311. #f)
  312. (lambda (key proc message location form . args)
  313. (and (eq? proc 'foo)
  314. (string-match "missing .*initialize.*baz" message)
  315. (equal? form '(foo))))))
  316. (test-assert "define-record-type* & extra initializers"
  317. (catch 'syntax-error
  318. (lambda ()
  319. (eval '(begin
  320. (define-record-type* <foo> foo make-foo
  321. foo?
  322. (bar foo-bar (default 42)))
  323. (foo (baz 'what?)))
  324. (test-module))
  325. #f)
  326. (lambda (key proc message location form . args)
  327. (and (string-match "extra.*initializer.*baz" message)
  328. (eq? proc 'foo)))))
  329. (test-assert "define-record-type* & inherit & extra initializers"
  330. (catch 'syntax-error
  331. (lambda ()
  332. (eval '(begin
  333. (define-record-type* <foo> foo make-foo
  334. foo?
  335. (bar foo-bar (default 42)))
  336. (foo (inherit (foo)) (baz 'what?)))
  337. (test-module))
  338. #f)
  339. (lambda (key proc message location form . args)
  340. (and (string-match "extra.*initializer.*baz" message)
  341. (eq? proc 'foo)))))
  342. (test-assert "define-record-type* & duplicate initializers"
  343. (let ((exp '(begin
  344. (define-record-type* <foo> foo make-foo
  345. foo?
  346. (bar foo-bar (default 42)))
  347. (foo (bar 1)
  348. (bar 2))))
  349. (loc (current-source-location))) ;keep this alignment!
  350. (catch 'syntax-error
  351. (lambda ()
  352. (eval exp (test-module))
  353. #f)
  354. (lambda (key proc message location form . args)
  355. (and (string-match "duplicate.*initializer" message)
  356. (eq? proc 'foo)
  357. ;; Make sure the location is that of the field specifier.
  358. (lset= equal?
  359. (pk 'expected-loc
  360. `((line . ,(- (assq-ref loc 'line) 1))
  361. ,@(alist-delete 'line loc)))
  362. (pk 'actual-loc (location-alist location))))))))
  363. (test-assert "ABI checks"
  364. (let ((module (test-module)))
  365. (eval '(begin
  366. (define-record-type* <foo> foo make-foo
  367. foo?
  368. (bar foo-bar (default 42)))
  369. (define (make-me-a-record) (foo)))
  370. module)
  371. (unless (eval '(foo? (make-me-a-record)) module)
  372. (error "what?" (eval '(make-me-a-record) module)))
  373. ;; Redefine <foo> with an additional field.
  374. (eval '(define-record-type* <foo> foo make-foo
  375. foo?
  376. (baz foo-baz)
  377. (bar foo-bar (default 42)))
  378. module)
  379. ;; Now 'make-me-a-record' is out of sync because it does an
  380. ;; 'allocate-struct' that corresponds to the previous definition of <foo>.
  381. (catch 'record-abi-mismatch-error
  382. (lambda ()
  383. (eval '(foo? (make-me-a-record)) module)
  384. #f)
  385. (match-lambda*
  386. ((key 'abi-check (? string? message) (rtd) . _)
  387. (eq? rtd (eval '<foo> module)))))))
  388. (test-equal "recutils->alist"
  389. '((("Name" . "foo")
  390. ("Version" . "0.1")
  391. ("Synopsis" . "foo bar")
  392. ("Something_else" . "chbouib"))
  393. (("Name" . "bar")
  394. ("Version" . "1.5")))
  395. (let ((p (open-input-string "
  396. # Comment following an empty line, and
  397. # preceding a couple of empty lines, all of
  398. # which should be silently consumed.
  399. Name: foo
  400. Version: 0.1
  401. # Comment right in the middle,
  402. # spanning two lines.
  403. Synopsis: foo bar
  404. Something_else: chbouib
  405. # Comment right before.
  406. Name: bar
  407. Version: 1.5
  408. # Comment at the end.")))
  409. (list (recutils->alist p)
  410. (recutils->alist p))))
  411. (test-equal "recutils->alist with + lines"
  412. '(("Name" . "foo")
  413. ("Description" . "1st line,\n2nd line,\n 3rd line with extra space,\n4th line without space."))
  414. (recutils->alist (open-input-string "
  415. Name: foo
  416. Description: 1st line,
  417. + 2nd line,
  418. + 3rd line with extra space,
  419. +4th line without space.")))
  420. (test-equal "alist->record" '((1 2) b c)
  421. (alist->record '(("a" . 1) ("b" . b) ("c" . c) ("a" . 2))
  422. list
  423. '("a" "b" "c")
  424. '("a")))
  425. (test-end)