srfi-109-test.scm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. (test-begin "srfi-109")
  2. (cond-expand
  3. (kawa
  4. ;; Using 3-operand datum->syntax enables line numbers in reporting.
  5. (define-syntax strtest
  6. (lambda (form)
  7. (syntax-case form ()
  8. ;; We need to use the rest1 and rest2 variables since the Kawa reader
  9. ;; currently only attaches line-numbers to pairs, and the quoted and
  10. ;; evaluated sub-forms aren't guaranteed to be lists.
  11. ((strtest value . rest1)
  12. (syntax-case #'rest1 ()
  13. ((quoted . rest2)
  14. (syntax-case #'rest2 ()
  15. ((evaluated)
  16. #`(begin
  17. #,(datum->syntax form #'(test-equal quoted (quote value))
  18. #'rest1)
  19. #,(datum->syntax form #'(test-equal evaluated value)
  20. #'rest2)))))))))))
  21. (else
  22. (define-syntax strtest
  23. (syntax-rules ()
  24. ((strtest string quoted evaluated)
  25. (begin
  26. (test-equal quoted (quote string))
  27. (test-equal evaluated string)))))))
  28. (strtest &{abc}
  29. '($string$ "abc")
  30. "abc")
  31. (strtest &{ab&(+ 3 4)xz}
  32. '($string$ "ab" $<<$ (+ 3 4) $>>$ "xz")
  33. "ab7xz")
  34. (strtest &{ab&[(+ 3 4)]xz}
  35. '($string$ "ab" $<<$ (+ 3 4) $>>$ "xz")
  36. "ab7xz")
  37. ;; Literal nested braces.
  38. (strtest &{ab{x}{}c{{d}}}
  39. '($string$ "ab{x}{}c{{d}}")
  40. "ab{x}{}c{{d}}")
  41. ;; Literal nested braces with enclosed expression.
  42. (strtest &{ab{&[(+ 5 7)]c}z}
  43. '($string$ "ab{" $<<$ (+ 5 7) $>>$ "c}z")
  44. "ab{12c}z")
  45. (strtest &{ab&[3 4]xzy}
  46. '($string$ "ab" $<<$ 3 4 $>>$ "xzy")
  47. "ab34xzy")
  48. (strtest &{_&lbrace;_&rbrace;_&gt;_&lt;_&quot;_&apos;_}
  49. '($string$ "_" $entity$:lbrace "_" $entity$:rbrace "_" $entity$:gt
  50. "_" $entity$:lt "_" $entity$:quot "_" $entity$:apos "_")
  51. "_{_}_>_<_\"_'_")
  52. (strtest &{_&alarm;_&backspace;_&delete;_&escape;_&newline;_&null;_&return;_&space;_&tab;_}
  53. '($string$ "_" $entity$:alarm "_" $entity$:backspace
  54. "_" $entity$:delete "_" $entity$:escape "_" $entity$:newline
  55. "_" $entity$:null "_" $entity$:return "_" $entity$:space
  56. "_" $entity$:tab "_")
  57. "_\a_\b_\x7f;_\x1b;_\n_\x0;_\r_ _\t_")
  58. (strtest &{a
  59. b}
  60. '($string$ "a\nb")
  61. "a\nb")
  62. (strtest &{_&#64;_&#x3f;_&#125;_}
  63. '($string$ "_@_?_}_")
  64. "_@_?_}_")
  65. (strtest &{abc&#|comment|#xyz} '($string$ "abcxyz") "abcxyz")
  66. (strtest &{abc
  67. &|def
  68. &| klm}
  69. '($string$ "abc\ndef\n klm")
  70. "abc\ndef\n klm")
  71. (strtest &{
  72. &|def
  73. &| klm}
  74. '($string$ "def\n klm")
  75. "def\n klm")
  76. ;; Next line is supposed to have trailing whitespace - should be ignored.
  77. (strtest &{
  78. &|def
  79. &| klm}
  80. '($string$ "def\n klm")
  81. "def\n klm")
  82. (test-equal
  83. "\n ab\n cd\n"
  84. (test-read-eval-string "&{\n ab\n cd\n}"))
  85. (test-equal
  86. " ab\n cd\n"
  87. (test-read-eval-string "&{\n &| ab\n &| cd\n}"))
  88. (test-equal
  89. "\n\n ab\n cd\n"
  90. (test-read-eval-string "&{\n\n &| ab\n &| cd\n}"))
  91. (test-equal
  92. "\n ab\n cd\n"
  93. (test-read-eval-string "&{&#||#\n &| ab\n &| cd\n}"))
  94. (test-equal
  95. "\n ab\n cd\n"
  96. (test-read-eval-string "&{&[]\n &| ab\n &| cd\n}"))
  97. (test-equal
  98. " ab\n cd\n"
  99. (test-read-eval-string "&{ \n &| ab\n &| cd\n}"))
  100. (test-equal
  101. "line1\nline2\n"
  102. (test-read-eval-string "&{
  103. &|line1
  104. &|line2
  105. &|}"))
  106. (test-equal
  107. "line1\nline2\n"
  108. (test-read-eval-string "&{\n &|line1\n &|line2\n}"))
  109. (test-equal
  110. " k \n ab\n cd\n"
  111. (test-read-eval-string "&{ k \n &| ab\n &| cd\n}"))
  112. (test-equal
  113. " \n ab\n cd\n"
  114. (test-read-eval-string "&{ &space; \n &| ab\n &| cd\n}"))
  115. (strtest &{&space;
  116. &|def
  117. &| klm}
  118. '($string$ $entity$:space "\ndef\n klm")
  119. " \ndef\n klm")
  120. (strtest &{abc&-
  121. def&-
  122. &| klm}
  123. '($string$ "abc def klm")
  124. "abc def klm")
  125. (strtest &{<&[(string-length "a/b/c")]>}
  126. '($string$ "<" $<<$ (string-length "a/b/c") $>>$ ">")
  127. "<5>")
  128. (strtest &{m&[3]&[4]n}
  129. '($string$ "m" $<<$ 3 $>>$ $<<$ 4 $>>$ "n")
  130. "m34n")
  131. ;; Some tests using format
  132. (strtest &{abc&~3d(+ 4 5)z}
  133. '($string$ "abc" ($format$ "~3d" (+ 4 5)) "z")
  134. "abc 9z")
  135. (strtest &{A&~{[]<&[[5 6 7]]>&~}[]Z}
  136. '($string$ "A" ($format$ "~{") "<" $<<$ ($bracket-list$ 5 6 7)
  137. $>>$ ">" ($format$ "~}") "Z")
  138. "A<5><6><7>Z")
  139. ;; Same as above, but with ellided empty []
  140. (strtest &{A&~{<&[[5 6 7]]>&~}Z}
  141. '($string$ "A" ($format$ "~{") "<" $<<$ ($bracket-list$ 5 6 7)
  142. $>>$ ">" ($format$ "~}") "Z")
  143. "A<5><6><7>Z")
  144. (strtest &{[&~{&[[5 6 7]]&~^_&~}]}
  145. '($string$ "[" ($format$ "~{") $<<$ ($bracket-list$ 5 6 7)
  146. $>>$ ($format$ "~^") "_" ($format$ "~}") "]")
  147. "[5_6_7]")
  148. (strtest &{[&~{&[[]]&~^_&~}]}
  149. '($string$ "[" ($format$ "~{") $<<$ ($bracket-list$)
  150. $>>$ ($format$ "~^") "_" ($format$ "~}") "]")
  151. "[]")
  152. (strtest &{_&~4t~w["qwerty"]_}
  153. '($string$ "_" ($format$ "~4t~w" "qwerty") "_")
  154. &{_ "qwerty"_})
  155. (cond-expand (kawa
  156. (strtest &{X&[@(list 3 4)]Y}
  157. '($string$ "X" $<<$ ($splice$ (list 3 4)) $>>$ "Y")
  158. "X34Y")
  159. (strtest &{X&~w[@(list "x" "y")]Y}
  160. '($string$ "X" ($format$ "~w" ($splice$ (list "x" "y")))
  161. "Y")
  162. &{X"x"Y})
  163. (strtest &{X&~w[@(list "x" "y")]&~w[]Y}
  164. '($string$ "X" ($format$ "~w" ($splice$ (list "x" "y")))
  165. ($format$ "~w") "Y")
  166. &{X"x""y"Y})
  167. ))
  168. (test-end)