records.test 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. ;;;; records.test --- Test suite for Guile's records. -*- mode: scheme; coding: utf-8 -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (test-records)
  19. #:use-module (ice-9 format)
  20. #:use-module (test-suite lib))
  21. ;; ascii names and symbols, custom printer
  22. (define rtd-foo (make-record-type "foo" '(x y)
  23. (lambda (s p)
  24. (display "#<it is a foo>" p))))
  25. (define make-foo (record-constructor rtd-foo))
  26. (define foo? (record-predicate rtd-foo))
  27. (define get-foo-x (record-accessor rtd-foo 'x))
  28. (define get-foo-y (record-accessor rtd-foo 'y))
  29. (define set-foo-x! (record-modifier rtd-foo 'x))
  30. (define set-foo-y! (record-modifier rtd-foo 'y))
  31. ;; non-Latin-1 names and symbols, default printer
  32. (define rtd-fŏŏ (make-record-type "fŏŏ" '(x ȳ)))
  33. (define make-fŏŏ (record-constructor rtd-fŏŏ))
  34. (define fŏŏ? (record-predicate rtd-fŏŏ))
  35. (define get-fŏŏ-x (record-accessor rtd-fŏŏ 'x))
  36. (define get-fŏŏ-ȳ (record-accessor rtd-fŏŏ 'ȳ))
  37. (define set-fŏŏ-x! (record-modifier rtd-fŏŏ 'x))
  38. (define set-fŏŏ-ȳ! (record-modifier rtd-fŏŏ 'ȳ))
  39. (with-test-prefix "records"
  40. (with-test-prefix "constructor"
  41. (pass-if-exception "0 args (2 required)" exception:wrong-num-args
  42. (make-foo))
  43. (pass-if-exception "1 arg (2 required)" exception:wrong-num-args
  44. (make-foo 1))
  45. (pass-if "2 args (2 required)" exception:wrong-num-args
  46. (foo? (make-foo 1 2)))
  47. (pass-if "non-latin-1" exception:wrong-num-args
  48. (fŏŏ? (make-fŏŏ 1 2))))
  49. (with-test-prefix "modifier and getter"
  50. (pass-if "set"
  51. (let ((r (make-foo 1 2)))
  52. (set-foo-x! r 3)
  53. (eqv? (get-foo-x r) 3)))
  54. (pass-if "set 2"
  55. (let ((r (make-fŏŏ 1 2)))
  56. (set-fŏŏ-ȳ! r 3)
  57. (eqv? (get-fŏŏ-ȳ r) 3))))
  58. (with-test-prefix "record type name"
  59. (pass-if "foo"
  60. (string=? "foo" (record-type-name rtd-foo)))
  61. (pass-if "fŏŏ"
  62. (string=? "fŏŏ" (record-type-name rtd-fŏŏ))))
  63. (with-test-prefix "printer"
  64. (pass-if "foo"
  65. (string=? "#<it is a foo>"
  66. (with-output-to-string
  67. (lambda () (display (make-foo 1 2))))))
  68. (pass-if "fŏŏ"
  69. (with-locale "en_US.utf8"
  70. (string-prefix? "#<fŏŏ"
  71. (with-output-to-string
  72. (lambda () (display (make-fŏŏ 1 2)))))))))