records.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016 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* & delayed"
  156. (begin
  157. (define-record-type* <foo> foo make-foo
  158. foo?
  159. (bar foo-bar (delayed)))
  160. (let* ((calls 0)
  161. (x (foo (bar (begin (set! calls (1+ calls)) 3)))))
  162. (and (zero? calls)
  163. (equal? (foo-bar x) 3) (= 1 calls)
  164. (equal? (foo-bar x) 3) (= 1 calls)
  165. (equal? (foo-bar x) 3) (= 1 calls)))))
  166. (test-assert "define-record-type* & delayed & default"
  167. (let ((mark #f))
  168. (define-record-type* <foo> foo make-foo
  169. foo?
  170. (bar foo-bar (delayed) (default mark)))
  171. (let ((x (foo)))
  172. (set! mark 42)
  173. (and (equal? (foo-bar x) 42)
  174. (begin
  175. (set! mark 7)
  176. (equal? (foo-bar x) 42))))))
  177. (test-assert "define-record-type* & delayed & inherited"
  178. (begin
  179. (define-record-type* <foo> foo make-foo
  180. foo?
  181. (bar foo-bar (delayed))
  182. (baz foo-baz (delayed)))
  183. (let* ((m 1)
  184. (n #f)
  185. (x (foo (bar m) (baz n)))
  186. (y (foo (inherit x) (baz 'b))))
  187. (set! n 'a)
  188. (and (equal? (foo-bar x) 1)
  189. (eq? (foo-baz x) 'a)
  190. (begin
  191. (set! m 777)
  192. (equal? (foo-bar y) 1)) ;promise was already forced
  193. (eq? (foo-baz y) 'b)))))
  194. (test-assert "define-record-type* & wrong field specifier"
  195. (let ((exp '(begin
  196. (define-record-type* <foo> foo make-foo
  197. foo?
  198. (bar foo-bar (default 42))
  199. (baz foo-baz))
  200. (foo (baz 1 2 3 4 5)))) ;syntax error
  201. (loc (current-source-location))) ;keep this alignment!
  202. (catch 'syntax-error
  203. (lambda ()
  204. (eval exp (test-module))
  205. #f)
  206. (lambda (key proc message location form . args)
  207. (and (eq? proc 'foo)
  208. (string-match "invalid field" message)
  209. (equal? form '(baz 1 2 3 4 5))
  210. ;; Make sure the location is that of the field specifier.
  211. ;; See <http://bugs.gnu.org/23969>.
  212. (lset= equal?
  213. (pk 'expected-loc
  214. `((line . ,(- (assq-ref loc 'line) 1))
  215. ,@(alist-delete 'line loc)))
  216. (pk 'actual-loc location)))))))
  217. (test-assert "define-record-type* & missing initializers"
  218. (catch 'syntax-error
  219. (lambda ()
  220. (eval '(begin
  221. (define-record-type* <foo> foo make-foo
  222. foo?
  223. (bar foo-bar (default 42))
  224. (baz foo-baz))
  225. (foo))
  226. (test-module))
  227. #f)
  228. (lambda (key proc message location form . args)
  229. (and (eq? proc 'foo)
  230. (string-match "missing .*initialize.*baz" message)
  231. (equal? form '(foo))))))
  232. (test-assert "define-record-type* & extra initializers"
  233. (catch 'syntax-error
  234. (lambda ()
  235. (eval '(begin
  236. (define-record-type* <foo> foo make-foo
  237. foo?
  238. (bar foo-bar (default 42)))
  239. (foo (baz 'what?)))
  240. (test-module))
  241. #f)
  242. (lambda (key proc message location form . args)
  243. (and (string-match "extra.*initializer.*baz" message)
  244. (eq? proc 'foo)))))
  245. (test-assert "define-record-type* & inherit & extra initializers"
  246. (catch 'syntax-error
  247. (lambda ()
  248. (eval '(begin
  249. (define-record-type* <foo> foo make-foo
  250. foo?
  251. (bar foo-bar (default 42)))
  252. (foo (inherit (foo)) (baz 'what?)))
  253. (test-module))
  254. #f)
  255. (lambda (key proc message location form . args)
  256. (and (string-match "extra.*initializer.*baz" message)
  257. (eq? proc 'foo)))))
  258. (test-equal "recutils->alist"
  259. '((("Name" . "foo")
  260. ("Version" . "0.1")
  261. ("Synopsis" . "foo bar")
  262. ("Something_else" . "chbouib"))
  263. (("Name" . "bar")
  264. ("Version" . "1.5")))
  265. (let ((p (open-input-string "
  266. # Comment following an empty line, and
  267. # preceding a couple of empty lines, all of
  268. # which should be silently consumed.
  269. Name: foo
  270. Version: 0.1
  271. # Comment right in the middle,
  272. # spanning two lines.
  273. Synopsis: foo bar
  274. Something_else: chbouib
  275. # Comment right before.
  276. Name: bar
  277. Version: 1.5
  278. # Comment at the end.")))
  279. (list (recutils->alist p)
  280. (recutils->alist p))))
  281. (test-equal "recutils->alist with + lines"
  282. '(("Name" . "foo")
  283. ("Description" . "1st line,\n2nd line,\n 3rd line with extra space,\n4th line without space."))
  284. (recutils->alist (open-input-string "
  285. Name: foo
  286. Description: 1st line,
  287. + 2nd line,
  288. + 3rd line with extra space,
  289. +4th line without space.")))
  290. (test-equal "alist->record" '((1 2) b c)
  291. (alist->record '(("a" . 1) ("b" . b) ("c" . c) ("a" . 2))
  292. list
  293. '("a" "b" "c")
  294. '("a")))
  295. (test-end)