records.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2018, 2019, 2020 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. (test-begin "records")
  30. (test-assert "define-record-type*"
  31. (begin
  32. (define-record-type* <foo> foo make-foo
  33. foo?
  34. (bar foo-bar)
  35. (baz foo-baz (default (+ 40 2))))
  36. (and (match (foo (bar 1) (baz 2))
  37. (($ <foo> 1 2) #t))
  38. (match (foo (baz 2) (bar 1))
  39. (($ <foo> 1 2) #t))
  40. (match (foo (bar 1))
  41. (($ <foo> 1 42) #t)))))
  42. (test-assert "define-record-type* with let* behavior"
  43. ;; Make sure field initializers can refer to each other as if they were in
  44. ;; a 'let*'.
  45. (begin
  46. (define-record-type* <bar> bar make-bar
  47. foo?
  48. (x bar-x)
  49. (y bar-y (default (+ 40 2)))
  50. (z bar-z))
  51. (and (match (bar (x 1) (y (+ x 1)) (z (* y 2)))
  52. (($ <bar> 1 2 4) #t))
  53. (match (bar (x 7) (z (* x 3)))
  54. (($ <bar> 7 42 21) #t))
  55. (match (bar (z 21) (x (/ z 3)))
  56. (($ <bar> 7 42 21) #t)))))
  57. (test-assert "define-record-type* & inherit"
  58. (begin
  59. (define-record-type* <foo> foo make-foo
  60. foo?
  61. (bar foo-bar)
  62. (baz foo-baz (default (+ 40 2))))
  63. (let* ((a (foo (bar 1)))
  64. (b (foo (inherit a) (baz 2)))
  65. (c (foo (inherit b) (bar -2)))
  66. (d (foo (inherit c)))
  67. (e (foo (inherit (foo (bar 42))) (baz 77))))
  68. (and (match a (($ <foo> 1 42) #t))
  69. (match b (($ <foo> 1 2) #t))
  70. (match c (($ <foo> -2 2) #t))
  71. (equal? c d)
  72. (match e (($ <foo> 42 77) #t))))))
  73. (test-assert "define-record-type* & inherit & let* behavior"
  74. (begin
  75. (define-record-type* <foo> foo make-foo
  76. foo?
  77. (bar foo-bar)
  78. (baz foo-baz (default (+ 40 2))))
  79. (let* ((a (foo (bar 77)))
  80. (b (foo (inherit a) (bar 1) (baz (+ bar 1))))
  81. (c (foo (inherit b) (baz 2) (bar (- baz 1)))))
  82. (and (match a (($ <foo> 77 42) #t))
  83. (match b (($ <foo> 1 2) #t))
  84. (equal? b c)))))
  85. (test-assert "define-record-type* & inherit & innate"
  86. (begin
  87. (define-record-type* <foo> foo make-foo
  88. foo?
  89. (bar foo-bar (innate) (default 42)))
  90. (let* ((a (foo (bar 1)))
  91. (b (foo (inherit a)))
  92. (c (foo (inherit a) (bar 3)))
  93. (d (foo)))
  94. (and (match a (($ <foo> 1) #t))
  95. (match b (($ <foo> 42) #t))
  96. (match c (($ <foo> 3) #t))
  97. (match d (($ <foo> 42) #t))))))
  98. (test-assert "define-record-type* & thunked"
  99. (begin
  100. (define-record-type* <foo> foo make-foo
  101. foo?
  102. (bar foo-bar)
  103. (baz foo-baz (thunked)))
  104. (let* ((calls 0)
  105. (x (foo (bar 2)
  106. (baz (begin (set! calls (1+ calls)) 3)))))
  107. (and (zero? calls)
  108. (equal? (foo-bar x) 2)
  109. (equal? (foo-baz x) 3) (= 1 calls)
  110. (equal? (foo-baz x) 3) (= 2 calls)))))
  111. (test-assert "define-record-type* & thunked & default"
  112. (begin
  113. (define-record-type* <foo> foo make-foo
  114. foo?
  115. (bar foo-bar)
  116. (baz foo-baz (thunked) (default 42)))
  117. (let ((mark (make-parameter #f)))
  118. (let ((x (foo (bar 2) (baz (mark))))
  119. (y (foo (bar 2))))
  120. (and (equal? (foo-bar x) 2)
  121. (parameterize ((mark (cons 'a 'b)))
  122. (eq? (foo-baz x) (mark)))
  123. (equal? (foo-bar y) 2)
  124. (equal? (foo-baz y) 42))))))
  125. (test-assert "define-record-type* & thunked & inherited"
  126. (begin
  127. (define-record-type* <foo> foo make-foo
  128. foo?
  129. (bar foo-bar (thunked))
  130. (baz foo-baz (thunked) (default 42)))
  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. (test-assert "define-record-type* & thunked & innate"
  142. (let ((mark (make-parameter #f)))
  143. (define-record-type* <foo> foo make-foo
  144. foo?
  145. (bar foo-bar (thunked) (innate) (default (mark)))
  146. (baz foo-baz (default #f)))
  147. (let* ((x (foo (bar 42)))
  148. (y (foo (inherit x) (baz 'unused))))
  149. (and (procedure? (struct-ref x 0))
  150. (equal? (foo-bar x) 42)
  151. (parameterize ((mark (cons 'a 'b)))
  152. (eq? (foo-bar y) (mark)))
  153. (parameterize ((mark (cons 'a 'b)))
  154. (eq? (foo-bar y) (mark)))))))
  155. (test-assert "define-record-type* & thunked & this-record"
  156. (begin
  157. (define-record-type* <foo> foo make-foo
  158. foo?
  159. (bar foo-bar)
  160. (baz foo-baz (thunked)))
  161. (let ((x (foo (bar 40)
  162. (baz (+ (foo-bar this-record) 2)))))
  163. (and (= 40 (foo-bar x))
  164. (= 42 (foo-baz x))))))
  165. (test-assert "define-record-type* & thunked & default & this-record"
  166. (begin
  167. (define-record-type* <foo> foo make-foo
  168. foo?
  169. (bar foo-bar)
  170. (baz foo-baz (thunked)
  171. (default (+ (foo-bar this-record) 2))))
  172. (let ((x (foo (bar 40))))
  173. (and (= 40 (foo-bar x))
  174. (= 42 (foo-baz x))))))
  175. (test-assert "define-record-type* & thunked & inherit & this-record"
  176. (begin
  177. (define-record-type* <foo> foo make-foo
  178. foo?
  179. (bar foo-bar)
  180. (baz foo-baz (thunked)
  181. (default (+ (foo-bar this-record) 2))))
  182. (let* ((x (foo (bar 40)))
  183. (y (foo (inherit x) (bar -2)))
  184. (z (foo (inherit x) (baz -2))))
  185. (and (= -2 (foo-bar y))
  186. (= 0 (foo-baz y))
  187. (= 40 (foo-bar z))
  188. (= -2 (foo-baz z))))))
  189. (test-assert "define-record-type* & thunked & inherit & custom this"
  190. (let ()
  191. (define-record-type* <foo> foo make-foo
  192. foo? this-foo
  193. (thing foo-thing (thunked)))
  194. (define-record-type* <bar> bar make-bar
  195. bar? this-bar
  196. (baz bar-baz (thunked)))
  197. ;; Nest records and test the two self references.
  198. (let* ((x (foo (thing (bar (baz (list this-bar this-foo))))))
  199. (y (foo-thing x)))
  200. (match (bar-baz y)
  201. ((first second)
  202. (and (eq? second x)
  203. (bar? first)
  204. (eq? first y)))))))
  205. (test-assert "define-record-type* & delayed"
  206. (begin
  207. (define-record-type* <foo> foo make-foo
  208. foo?
  209. (bar foo-bar (delayed)))
  210. (let* ((calls 0)
  211. (x (foo (bar (begin (set! calls (1+ calls)) 3)))))
  212. (and (zero? calls)
  213. (equal? (foo-bar x) 3) (= 1 calls)
  214. (equal? (foo-bar x) 3) (= 1 calls)
  215. (equal? (foo-bar x) 3) (= 1 calls)))))
  216. (test-assert "define-record-type* & delayed & default"
  217. (let ((mark #f))
  218. (define-record-type* <foo> foo make-foo
  219. foo?
  220. (bar foo-bar (delayed) (default mark)))
  221. (let ((x (foo)))
  222. (set! mark 42)
  223. (and (equal? (foo-bar x) 42)
  224. (begin
  225. (set! mark 7)
  226. (equal? (foo-bar x) 42))))))
  227. (test-assert "define-record-type* & delayed & inherited"
  228. (begin
  229. (define-record-type* <foo> foo make-foo
  230. foo?
  231. (bar foo-bar (delayed))
  232. (baz foo-baz (delayed)))
  233. (let* ((m 1)
  234. (n #f)
  235. (x (foo (bar m) (baz n)))
  236. (y (foo (inherit x) (baz 'b))))
  237. (set! n 'a)
  238. (and (equal? (foo-bar x) 1)
  239. (eq? (foo-baz x) 'a)
  240. (begin
  241. (set! m 777)
  242. (equal? (foo-bar y) 1)) ;promise was already forced
  243. (eq? (foo-baz y) 'b)))))
  244. (test-assert "define-record-type* & wrong field specifier"
  245. (let ((exp '(begin
  246. (define-record-type* <foo> foo make-foo
  247. foo?
  248. (bar foo-bar (default 42))
  249. (baz foo-baz))
  250. (foo (baz 1 2 3 4 5)))) ;syntax error
  251. (loc (current-source-location))) ;keep this alignment!
  252. (catch 'syntax-error
  253. (lambda ()
  254. (eval exp (test-module))
  255. #f)
  256. (lambda (key proc message location form subform . _)
  257. (and (eq? proc 'foo)
  258. (string-match "invalid field" message)
  259. (equal? subform '(baz 1 2 3 4 5))
  260. (equal? form '(foo (baz 1 2 3 4 5)))
  261. ;; Make sure the location is that of the field specifier.
  262. ;; See <http://bugs.gnu.org/23969>.
  263. (lset= equal?
  264. (pk 'expected-loc
  265. `((line . ,(- (assq-ref loc 'line) 1))
  266. ,@(alist-delete 'line loc)))
  267. (pk 'actual-loc location)))))))
  268. (test-assert "define-record-type* & wrong field specifier, identifier"
  269. (let ((exp '(begin
  270. (define-record-type* <foo> foo make-foo
  271. foo?
  272. (bar foo-bar (default 42))
  273. (baz foo-baz))
  274. (foo
  275. baz))) ;syntax error
  276. (loc (current-source-location))) ;keep this alignment!
  277. (catch 'syntax-error
  278. (lambda ()
  279. (eval exp (test-module))
  280. #f)
  281. (lambda (key proc message location form subform . _)
  282. (and (eq? proc 'foo)
  283. (string-match "invalid field" message)
  284. (equal? subform 'baz)
  285. (equal? form '(foo baz))
  286. ;; Here the location is that of the parent form.
  287. (lset= equal?
  288. (pk 'expected-loc
  289. `((line . ,(- (assq-ref loc 'line) 2))
  290. ,@(alist-delete 'line loc)))
  291. (pk 'actual-loc location)))))))
  292. (test-assert "define-record-type* & missing initializers"
  293. (catch 'syntax-error
  294. (lambda ()
  295. (eval '(begin
  296. (define-record-type* <foo> foo make-foo
  297. foo?
  298. (bar foo-bar (default 42))
  299. (baz foo-baz))
  300. (foo))
  301. (test-module))
  302. #f)
  303. (lambda (key proc message location form . args)
  304. (and (eq? proc 'foo)
  305. (string-match "missing .*initialize.*baz" message)
  306. (equal? form '(foo))))))
  307. (test-assert "define-record-type* & extra initializers"
  308. (catch 'syntax-error
  309. (lambda ()
  310. (eval '(begin
  311. (define-record-type* <foo> foo make-foo
  312. foo?
  313. (bar foo-bar (default 42)))
  314. (foo (baz 'what?)))
  315. (test-module))
  316. #f)
  317. (lambda (key proc message location form . args)
  318. (and (string-match "extra.*initializer.*baz" message)
  319. (eq? proc 'foo)))))
  320. (test-assert "define-record-type* & inherit & extra initializers"
  321. (catch 'syntax-error
  322. (lambda ()
  323. (eval '(begin
  324. (define-record-type* <foo> foo make-foo
  325. foo?
  326. (bar foo-bar (default 42)))
  327. (foo (inherit (foo)) (baz 'what?)))
  328. (test-module))
  329. #f)
  330. (lambda (key proc message location form . args)
  331. (and (string-match "extra.*initializer.*baz" message)
  332. (eq? proc 'foo)))))
  333. (test-assert "define-record-type* & duplicate initializers"
  334. (let ((exp '(begin
  335. (define-record-type* <foo> foo make-foo
  336. foo?
  337. (bar foo-bar (default 42)))
  338. (foo (bar 1)
  339. (bar 2))))
  340. (loc (current-source-location))) ;keep this alignment!
  341. (catch 'syntax-error
  342. (lambda ()
  343. (eval exp (test-module))
  344. #f)
  345. (lambda (key proc message location form . args)
  346. (and (string-match "duplicate.*initializer" message)
  347. (eq? proc 'foo)
  348. ;; Make sure the location is that of the field specifier.
  349. (lset= equal?
  350. (pk 'expected-loc
  351. `((line . ,(- (assq-ref loc 'line) 1))
  352. ,@(alist-delete 'line loc)))
  353. (pk 'actual-loc location)))))))
  354. (test-assert "ABI checks"
  355. (let ((module (test-module)))
  356. (eval '(begin
  357. (define-record-type* <foo> foo make-foo
  358. foo?
  359. (bar foo-bar (default 42)))
  360. (define (make-me-a-record) (foo)))
  361. module)
  362. (unless (eval '(foo? (make-me-a-record)) module)
  363. (error "what?" (eval '(make-me-a-record) module)))
  364. ;; Redefine <foo> with an additional field.
  365. (eval '(define-record-type* <foo> foo make-foo
  366. foo?
  367. (baz foo-baz)
  368. (bar foo-bar (default 42)))
  369. module)
  370. ;; Now 'make-me-a-record' is out of sync because it does an
  371. ;; 'allocate-struct' that corresponds to the previous definition of <foo>.
  372. (catch 'record-abi-mismatch-error
  373. (lambda ()
  374. (eval '(foo? (make-me-a-record)) module)
  375. #f)
  376. (match-lambda*
  377. ((key 'abi-check (? string? message) (rtd) . _)
  378. (eq? rtd (eval '<foo> module)))))))
  379. (test-equal "recutils->alist"
  380. '((("Name" . "foo")
  381. ("Version" . "0.1")
  382. ("Synopsis" . "foo bar")
  383. ("Something_else" . "chbouib"))
  384. (("Name" . "bar")
  385. ("Version" . "1.5")))
  386. (let ((p (open-input-string "
  387. # Comment following an empty line, and
  388. # preceding a couple of empty lines, all of
  389. # which should be silently consumed.
  390. Name: foo
  391. Version: 0.1
  392. # Comment right in the middle,
  393. # spanning two lines.
  394. Synopsis: foo bar
  395. Something_else: chbouib
  396. # Comment right before.
  397. Name: bar
  398. Version: 1.5
  399. # Comment at the end.")))
  400. (list (recutils->alist p)
  401. (recutils->alist p))))
  402. (test-equal "recutils->alist with + lines"
  403. '(("Name" . "foo")
  404. ("Description" . "1st line,\n2nd line,\n 3rd line with extra space,\n4th line without space."))
  405. (recutils->alist (open-input-string "
  406. Name: foo
  407. Description: 1st line,
  408. + 2nd line,
  409. + 3rd line with extra space,
  410. +4th line without space.")))
  411. (test-equal "alist->record" '((1 2) b c)
  412. (alist->record '(("a" . 1) ("b" . b) ("c" . c) ("a" . 2))
  413. list
  414. '("a" "b" "c")
  415. '("a")))
  416. (test-end)