print.test 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. ;;;; -*- coding: utf-8; mode: scheme; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2010, 2013, 2014 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-print)
  19. #:use-module (ice-9 pretty-print)
  20. #:use-module (test-suite lib))
  21. (define-syntax prints?
  22. ;; #t if EXP prints as RESULT.
  23. (syntax-rules ()
  24. ((_ exp result)
  25. (string=? result
  26. (with-output-to-string
  27. (lambda ()
  28. (pretty-print 'exp)))))))
  29. (define (with-print-options opts thunk)
  30. (let ((saved-options (print-options)))
  31. (dynamic-wind
  32. (lambda ()
  33. (print-options opts))
  34. thunk
  35. (lambda ()
  36. (print-options saved-options)))))
  37. (define-syntax-rule (write-with-options opts x)
  38. (with-print-options opts (lambda ()
  39. (with-output-to-string
  40. (lambda ()
  41. (write x))))))
  42. (with-test-prefix "write"
  43. (with-test-prefix "r7rs-symbols"
  44. (pass-if-equal "basic"
  45. "|foo bar|"
  46. (write-with-options '(r7rs-symbols)
  47. (string->symbol "foo bar")))
  48. (pass-if-equal "escapes"
  49. "|bar \\| backslash \\x5c; alarm \\a backspace \\b tab \\t newline \\n cr \\r null \\x0; del \\x7f;|"
  50. (write-with-options
  51. '(r7rs-symbols)
  52. (string->symbol
  53. (string-append
  54. "bar | backslash \\ alarm \a backspace \b tab \t newline \n cr \r null \0 del "
  55. (string #\del)))))
  56. (pass-if-equal "brackets"
  57. "|()[]{}|"
  58. (write-with-options '(r7rs-symbols)
  59. (string->symbol "()[]{}")))
  60. (pass-if-equal "starts with bar"
  61. "|\\|foo|"
  62. (write-with-options '(r7rs-symbols)
  63. (string->symbol "|foo")))
  64. (pass-if-equal "ends with bar"
  65. "|foo\\||"
  66. (write-with-options '(r7rs-symbols)
  67. (string->symbol "foo|")))
  68. (pass-if-equal "starts with backslash"
  69. "|\\x5c;foo|"
  70. (write-with-options '(r7rs-symbols)
  71. (string->symbol "\\foo")))
  72. (pass-if-equal "ends with backslash"
  73. "|foo\\x5c;|"
  74. (write-with-options '(r7rs-symbols)
  75. (string->symbol "foo\\")))))
  76. (with-test-prefix "pretty-print"
  77. (pass-if "pair"
  78. (prints? (a . b) "(a . b)\n"))
  79. (pass-if "list"
  80. (prints? (a b c) "(a b c)\n"))
  81. (pass-if "dotted list"
  82. (prints? (a b . c) "(a b . c)\n"))
  83. (pass-if "quote"
  84. (prints? 'foo "'foo\n"))
  85. (pass-if "non-starting quote"
  86. (prints? (foo 'bar) "(foo 'bar)\n"))
  87. (pass-if "nested quote"
  88. (prints? (''foo) "(''foo)\n"))
  89. (pass-if "quasiquote & co."
  90. (prints? (define foo `(bar ,(+ 2 3)))
  91. "(define foo `(bar ,(+ 2 3)))\n")))
  92. (with-test-prefix "truncated-print"
  93. (define exp '(a b #(c d e) f . g))
  94. (define (tprint x width encoding)
  95. (call-with-output-string
  96. (lambda (p)
  97. (set-port-encoding! p encoding)
  98. (truncated-print x p #:width width))))
  99. (pass-if-equal "(a b . #)"
  100. (tprint exp 10 "ISO-8859-1"))
  101. (pass-if-equal "(a b # f . g)"
  102. (tprint exp 15 "ISO-8859-1"))
  103. (pass-if-equal "(a b #(c ...) . #)"
  104. (tprint exp 18 "ISO-8859-1"))
  105. (pass-if-equal "(a b #(c d e) f . g)"
  106. (tprint exp 20 "ISO-8859-1"))
  107. (pass-if-equal "\"The quick brown...\""
  108. (tprint "The quick brown fox" 20 "ISO-8859-1"))
  109. (pass-if-equal "\"The quick brown f…\""
  110. (tprint "The quick brown fox" 20 "UTF-8"))
  111. (pass-if-equal "#<directory (tes...>"
  112. (tprint (current-module) 20 "ISO-8859-1"))
  113. (pass-if-equal "#<directory (test-…>"
  114. (tprint (current-module) 20 "UTF-8"))
  115. ;; bitvectors
  116. (let ((testv (bitvector #t #f #f #t #t #f #t #t)))
  117. (pass-if-equal "#*10011011"
  118. (tprint testv 11 "UTF-8"))
  119. (pass-if-equal "#*10011011"
  120. (tprint testv 11 "ISO-8859-1"))
  121. (pass-if-equal "#*10011…"
  122. (tprint testv 8 "UTF-8"))
  123. (pass-if-equal "#*100..."
  124. (tprint testv 8 "ISO-8859-1"))
  125. (pass-if-equal "#*10…"
  126. (tprint testv 5 "UTF-8"))
  127. (pass-if-equal "#*..."
  128. (tprint testv 5 "ISO-8859-1"))
  129. (pass-if-equal "#*1…"
  130. (tprint testv 4 "UTF-8"))
  131. (pass-if-equal "#"
  132. (tprint testv 4 "ISO-8859-1")))
  133. ;; rank 0 arrays
  134. (pass-if-equal "#0(#)"
  135. (tprint (make-typed-array #t 9.0) 6 "UTF-8"))
  136. (pass-if-equal "#0(9.0)"
  137. (tprint (make-typed-array #t 9.0) 7 "UTF-8"))
  138. (pass-if-equal "#0f64(#)"
  139. (tprint (make-typed-array 'f64 9.0) 8 "UTF-8"))
  140. (pass-if-equal "#0f64(9.0)"
  141. (tprint (make-typed-array 'f64 9.0) 10 "UTF-8"))
  142. (pass-if-equal "#"
  143. (tprint (make-typed-array 's32 0 20 20) 7 "UTF-8"))
  144. ;; higher dimensional arrays
  145. (let ((testa (make-typed-array 's32 0 20 20)))
  146. (pass-if-equal "#2s32(…)"
  147. (tprint testa 8 "UTF-8"))
  148. (pass-if-equal "#2s32(# …)"
  149. (tprint testa 10 "UTF-8"))
  150. (pass-if-equal "#2s32((…) …)"
  151. (tprint testa 12 "UTF-8"))
  152. (pass-if-equal "#2s32((0 …) …)"
  153. (tprint testa 14 "UTF-8")))
  154. ;; check that bounds are printed correctly
  155. (pass-if-equal "#2@-1@0((foo foo foo foo …) …)"
  156. (tprint (make-array 'foo '(-1 3) 5) 30 "UTF-8"))
  157. (pass-if-equal "#3@-1:5@0:0@0:5(() () () # #)"
  158. (tprint (make-array 'foo '(-1 3) 0 5) 30 "UTF-8"))
  159. ;; nested objects including arrays
  160. (pass-if-equal "#2((#(9 9) #(9 9)) (#(9 9) #(9 9)))"
  161. (tprint (make-typed-array #t (make-typed-array #t 9 2) 2 2) 40 "UTF-8"))
  162. (pass-if-equal "#(#2((9 9) (9 9)) #2((9 9) (9 9)))"
  163. (tprint (make-vector 2 (make-typed-array #t 9 2 2)) 40 "UTF-8"))
  164. (pass-if-equal "(#2((9 9) (9 9)) #2((9 9) (9 9)))"
  165. (tprint (make-list 2 (make-typed-array #t 9 2 2)) 40 "UTF-8"))
  166. (pass-if-equal "(#0(9) #0(9))"
  167. (tprint (make-list 2 (make-typed-array #t 9)) 20 "UTF-8"))
  168. (pass-if-equal "(#0(9) #)"
  169. (tprint (make-list 2 (make-typed-array #t 9)) 10 "UTF-8")))