summaryrefslogtreecommitdiff
path: root/test-suite/tests/records.test
blob: c2ea06ed776a23dbc21dc0fef0040ae93515e311 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
;;;; records.test --- Test suite for Guile's records. -*- mode: scheme; coding: utf-8 -*-
;;;;
;;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
;;;; 
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;; 
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (test-records)
  #:use-module (ice-9 format)
  #:use-module (test-suite lib))

;; ascii names and symbols, custom printer
(define rtd-foo (make-record-type "foo" '(x y)
                                  (lambda (s p)
                                    (display "#<it is a foo>" p))))
(define make-foo (record-constructor rtd-foo))
(define foo? (record-predicate rtd-foo))
(define get-foo-x (record-accessor rtd-foo 'x))
(define get-foo-y (record-accessor rtd-foo 'y))
(define set-foo-x! (record-modifier rtd-foo 'x))
(define set-foo-y! (record-modifier rtd-foo 'y))

;; non-Latin-1 names and symbols, default printer
(define rtd-fŏŏ (make-record-type "fŏŏ" '(x ȳ)))
(define make-fŏŏ (record-constructor rtd-fŏŏ))
(define fŏŏ? (record-predicate rtd-fŏŏ))
(define get-fŏŏ-x (record-accessor rtd-fŏŏ 'x))
(define get-fŏŏ-ȳ (record-accessor rtd-fŏŏ 'ȳ))
(define set-fŏŏ-x! (record-modifier rtd-fŏŏ 'x))
(define set-fŏŏ-ȳ! (record-modifier rtd-fŏŏ 'ȳ))

(with-test-prefix "records"
  
  (with-test-prefix "constructor"

    (pass-if-exception "0 args (2 required)" exception:wrong-num-args
      (make-foo))

    (pass-if-exception "1 arg (2 required)" exception:wrong-num-args
      (make-foo 1))

    (pass-if "2 args (2 required)" exception:wrong-num-args
      (foo? (make-foo 1 2)))

    (pass-if "non-latin-1" exception:wrong-num-args
      (fŏŏ? (make-fŏŏ 1 2))))

  (with-test-prefix "modifier and getter"

    (pass-if "set"
      (let ((r (make-foo 1 2)))
        (set-foo-x! r 3)
        (eqv? (get-foo-x r) 3)))

    (pass-if "set 2"
      (let ((r (make-fŏŏ 1 2)))
        (set-fŏŏ-ȳ! r 3)
        (eqv? (get-fŏŏ-ȳ r) 3))))

  (with-test-prefix "record type name"
    
    (pass-if "foo"
      (string=? "foo" (record-type-name rtd-foo)))

    (pass-if "fŏŏ"
      (string=? "fŏŏ" (record-type-name rtd-fŏŏ))))

  (with-test-prefix "printer"

    (pass-if "foo"
      (string=? "#<it is a foo>"
                (with-output-to-string
                  (lambda () (display (make-foo 1 2))))))

    (pass-if "fŏŏ"
      (with-locale "en_US.utf8"
        (string-prefix? "#<fŏŏ"
                        (with-output-to-string
                          (lambda () (display (make-fŏŏ 1 2)))))))))