Don't mix definitions and expressions in SRFI-9
[bpt/guile.git] / test-suite / tests / srfi-9.test
1 ;;;; srfi-9.test --- Test suite for Guile's SRFI-9 functions. -*- scheme -*-
2 ;;;; Martin Grabmueller, 2001-05-10
3 ;;;;
4 ;;;; Copyright (C) 2001, 2006, 2007, 2010, 2011 Free Software Foundation, Inc.
5 ;;;;
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;;
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 (define-module (test-suite test-numbers)
21 #:use-module (test-suite lib)
22 #:use-module ((system base compile) #:select (compile))
23 #:use-module (srfi srfi-9))
24
25
26 (define-record-type :qux (make-qux) qux?)
27
28 (define-record-type :foo (make-foo x) foo?
29 (x get-x) (y get-y set-y!))
30
31 (define-record-type :bar (make-bar i j) bar?
32 (i get-i) (i get-j set-j!))
33
34 (define f (make-foo 1))
35 (set-y! f 2)
36
37 (define b (make-bar 123 456))
38
39 (with-test-prefix "constructor"
40
41 ;; Constructors are defined using `define-integrable', meaning that direct
42 ;; calls as in `(make-foo)' lead to a compile-time psyntax error, hence the
43 ;; distinction below.
44
45 (pass-if-exception "foo 0 args (inline)" exception:syntax-pattern-unmatched
46 (compile '(make-foo) #:env (current-module)))
47 (pass-if-exception "foo 2 args (inline)" exception:syntax-pattern-unmatched
48 (compile '(make-foo 1 2) #:env (current-module)))
49
50 (pass-if-exception "foo 0 args" exception:wrong-num-args
51 (let ((make-foo make-foo))
52 (make-foo)))
53 (pass-if-exception "foo 2 args" exception:wrong-num-args
54 (let ((make-foo make-foo))
55 (make-foo 1 2))))
56
57 (with-test-prefix "predicate"
58
59 (pass-if "pass"
60 (foo? f))
61 (pass-if "fail wrong record type"
62 (eq? #f (foo? b)))
63 (pass-if "fail number"
64 (eq? #f (foo? 123))))
65
66 (with-test-prefix "accessor"
67
68 (pass-if "get-x"
69 (= 1 (get-x f)))
70 (pass-if "get-y"
71 (= 2 (get-y f)))
72
73 (pass-if-exception "get-x on number" exception:wrong-type-arg
74 (get-x 999))
75 (pass-if-exception "get-y on number" exception:wrong-type-arg
76 (get-y 999))
77
78 ;; prior to guile 1.6.9 and 1.8.1 this wan't enforced
79 (pass-if-exception "get-x on bar" exception:wrong-type-arg
80 (get-x b))
81 (pass-if-exception "get-y on bar" exception:wrong-type-arg
82 (get-y b)))
83
84 (with-test-prefix "modifier"
85
86 (pass-if "set-y!"
87 (set-y! f #t)
88 (eq? #t (get-y f)))
89
90 (pass-if-exception "set-y! on number" exception:wrong-type-arg
91 (set-y! 999 #t))
92
93 ;; prior to guile 1.6.9 and 1.8.1 this wan't enforced
94 (pass-if-exception "set-y! on bar" exception:wrong-type-arg
95 (set-y! b 99)))
96
97 (with-test-prefix "non-toplevel"
98
99 (define-record-type :frotz (make-frotz a b) frotz?
100 (a frotz-a) (b frotz-b set-frotz-b!))
101
102 (pass-if "construction"
103 (let ((frotz (make-frotz 1 2)))
104 (and (= (frotz-a frotz) 1)
105 (= (frotz-b frotz) 2)))))