r6rs-records-syntactic.test 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ;;; -*- scheme -*-
  2. ;;; r6rs-records-syntactic.test --- Test suite for R6RS (rnrs records syntactic)
  3. ;; Copyright (C) 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-suite test-rnrs-records-syntactic)
  19. #:use-module ((rnrs records syntactic) #:version (6))
  20. #:use-module ((rnrs records procedural) #:version (6))
  21. #:use-module ((rnrs records inspection) #:version (6))
  22. #:use-module ((rnrs conditions) #:version (6))
  23. #:use-module ((rnrs exceptions) #:version (6))
  24. #:use-module ((system base compile) #:select (compile))
  25. #:use-module (test-suite lib))
  26. (define-record-type simple-rtd)
  27. (define-record-type
  28. (specified-rtd specified-rtd-constructor specified-rtd-predicate))
  29. ;; Can't be named as `parent-rtd', as that shadows the `parent-rtd'
  30. ;; literal.
  31. (define-record-type *parent-rtd (fields x y))
  32. (define-record-type child-parent-rtd-rtd
  33. (parent-rtd (record-type-descriptor *parent-rtd)
  34. (record-constructor-descriptor *parent-rtd))
  35. (fields z))
  36. (define-record-type child-parent-rtd (parent *parent-rtd) (fields z))
  37. (define-record-type mutable-fields-rtd
  38. (fields (mutable mutable-bar)
  39. (mutable mutable-baz mutable-baz-accessor mutable-baz-mutator)))
  40. (define-record-type immutable-fields-rtd
  41. (fields immutable-foo
  42. (immutable immutable-bar)
  43. (immutable immutable-baz immutable-baz-accessor)))
  44. (define-record-type protocol-rtd
  45. (fields (immutable x) (immutable y))
  46. (protocol (lambda (p) (lambda (x y) (p (+ x 1) (+ y 1))))))
  47. (define-record-type sealed-rtd (sealed #t))
  48. (define-record-type opaque-rtd (opaque #t))
  49. (define-record-type nongenerative-rtd (nongenerative))
  50. (define-record-type nongenerative-uid-rtd (nongenerative foo))
  51. (with-test-prefix "simple record names"
  52. (pass-if "define-record-type defines record type"
  53. (defined? 'simple-rtd))
  54. (pass-if "define-record-type defines record predicate"
  55. (defined? 'simple-rtd?))
  56. (pass-if "define-record-type defines record-constructor"
  57. (defined? 'make-simple-rtd)))
  58. (with-test-prefix "fully-specified record names"
  59. (pass-if "define-record-type defines named predicate"
  60. (defined? 'specified-rtd-predicate))
  61. (pass-if "define-record-type defines named constructor"
  62. (defined? 'specified-rtd-constructor)))
  63. (pass-if "parent-rtd clause includes specified parent"
  64. (eq? (record-type-parent child-parent-rtd-rtd) *parent-rtd))
  65. (pass-if "parent clause includes specified parent"
  66. (eq? (record-type-parent child-parent-rtd) *parent-rtd))
  67. (pass-if "protocol clause includes specified protocol"
  68. (let ((protocol-record (make-protocol-rtd 1 2)))
  69. (and (eqv? (protocol-rtd-x protocol-record) 2)
  70. (eqv? (protocol-rtd-y protocol-record) 3))))
  71. (pass-if "sealed clause produces sealed type"
  72. (record-type-sealed? sealed-rtd))
  73. (pass-if "opaque clause produces opaque type"
  74. (record-type-opaque? opaque-rtd))
  75. (with-test-prefix "nongenerative"
  76. (pass-if "nongenerative clause produces nongenerative type"
  77. (not (record-type-generative? nongenerative-rtd)))
  78. (pass-if "nongenerative clause preserves specified uid"
  79. (and (not (record-type-generative? nongenerative-uid-rtd))
  80. (eq? (record-type-uid nongenerative-uid-rtd) 'foo))))
  81. (with-test-prefix "fields"
  82. (pass-if "raw symbol produces accessor only"
  83. (and (defined? 'immutable-fields-rtd-immutable-foo)
  84. (not (defined? 'immutable-fields-rtd-immutable-foo-set!))))
  85. (pass-if "(immutable x) form produces accessor only"
  86. (and (defined? 'immutable-fields-rtd-immutable-bar)
  87. (not (defined? 'immutable-fields-rtd-immutable-bar-set!))))
  88. (pass-if "(immutable x y) form produces named accessor"
  89. (defined? 'immutable-baz-accessor))
  90. (pass-if "(mutable x) form produces accessor and mutator"
  91. (and (defined? 'mutable-fields-rtd-mutable-bar)
  92. (defined? 'mutable-fields-rtd-mutable-bar-set!)))
  93. (pass-if "(mutable x y) form produces named accessor and mutator"
  94. (and (defined? 'mutable-baz-accessor)
  95. (defined? 'mutable-baz-mutator))))
  96. (pass-if "record-type-descriptor returns rtd"
  97. (eq? (record-type-descriptor simple-rtd) simple-rtd))
  98. (pass-if "record-constructor-descriptor returns rcd"
  99. (procedure? (record-constructor (record-constructor-descriptor simple-rtd))))
  100. (with-test-prefix "record hygiene"
  101. (pass-if-exception "using shadowed record keywords fails" exception:syntax-pattern-unmatched
  102. (compile '(let ((fields #f))
  103. (define-record-type foo (fields bar))
  104. #t)
  105. #:env (current-module)))
  106. (pass-if "using shadowed record keywords fails 2"
  107. (guard (condition ((syntax-violation? condition) #t))
  108. (compile '(let ((immutable #f))
  109. (define-record-type foo (fields (immutable bar)))
  110. #t)
  111. #:env (current-module))
  112. #f))
  113. (pass-if "hygiene preserved when using macros"
  114. (compile '(begin
  115. (define pass #t)
  116. (define-syntax define-record
  117. (syntax-rules ()
  118. ((define-record name field)
  119. (define-record-type name
  120. (protocol
  121. (lambda (x)
  122. (lambda ()
  123. ;; pass refers to pass in scope of macro not use
  124. (x pass))))
  125. (fields field)))))
  126. (let ((pass #f))
  127. (define-record foo bar)
  128. (foo-bar (make-foo))))
  129. #:env (current-module))))