encoding-escapes.test 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ;;;; encoding-escapes.test --- test suite for Guile's string encodings -*- mode: scheme; coding: utf-8 -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2009, 2010, 2011 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-strings)
  19. #:use-module (test-suite lib)
  20. #:use-module (srfi srfi-1))
  21. ;; Create a string from integer char values, eg. (string-ints 65) => "A"
  22. (define (string-ints . args)
  23. (apply string (map integer->char args)))
  24. (define s1 "última")
  25. (define s2 "cédula")
  26. (define s3 "años")
  27. (define s4 "羅生門")
  28. (with-test-prefix "internal encoding"
  29. (pass-if "ultima"
  30. (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
  31. (pass-if "cedula"
  32. (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
  33. (pass-if "anos"
  34. (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
  35. (pass-if "Rashomon"
  36. (string=? s4 (string-ints #x7f85 #x751f #x9580))))
  37. (with-test-prefix "chars"
  38. (pass-if "ultima"
  39. (list= eqv? (string->list s1)
  40. (list #\372 #\l #\t #\i #\m #\a)))
  41. (pass-if "cedula"
  42. (list= eqv? (string->list s2)
  43. (list #\c #\351 #\d #\u #\l #\a)))
  44. (pass-if "anos"
  45. (list= eqv? (string->list s3)
  46. (list #\a #\361 #\o #\s)))
  47. (pass-if "Rashomon"
  48. (list= eqv? (string->list s4)
  49. (list #\77605 #\72437 #\112600))))
  50. ;; Check that an error is flagged on display output when the output
  51. ;; error strategy is 'error
  52. (with-test-prefix "display output errors"
  53. (pass-if "ultima"
  54. (let ((pt (open-output-string)))
  55. (set-port-encoding! pt "ASCII")
  56. (set-port-conversion-strategy! pt 'error)
  57. (catch 'encoding-error
  58. (lambda ()
  59. (display s1 pt)
  60. #f)
  61. (lambda (key subr message errno port chr)
  62. (and (eq? port pt)
  63. (char=? chr (string-ref s1 0))
  64. (string=? (get-output-string pt) ""))))))
  65. (pass-if "Rashomon"
  66. (let ((pt (open-output-string)))
  67. (set-port-encoding! pt "ASCII")
  68. (set-port-conversion-strategy! pt 'error)
  69. (catch 'encoding-error
  70. (lambda ()
  71. (display s4 pt)
  72. #f)
  73. (lambda (key subr message errno port chr)
  74. (and (eq? port pt)
  75. (char=? chr (string-ref s4 0))
  76. (string=? (get-output-string pt) ""))))))
  77. (pass-if "tekniko"
  78. (let ((pt (open-output-string)))
  79. (set-port-encoding! pt "ASCII")
  80. (set-port-conversion-strategy! pt 'error)
  81. (catch 'encoding-error
  82. (lambda ()
  83. ;; This time encoding should fail on the 3rd character.
  84. (display "teĥniko" pt)
  85. #f)
  86. (lambda (key subr message errno port chr)
  87. (and (eq? port pt)
  88. (char=? chr #\ĥ)
  89. (string=? "te" (get-output-string pt))))))))
  90. ;; Check that questions marks or substitutions appear when the conversion
  91. ;; mode is substitute
  92. (with-test-prefix "display output substitutions"
  93. (pass-if "ultima"
  94. (let ((pt (open-output-string)))
  95. (set-port-encoding! pt "ASCII")
  96. (set-port-conversion-strategy! pt 'substitute)
  97. (display s1 pt)
  98. (string=? "?ltima"
  99. (get-output-string pt))))
  100. (pass-if "Rashomon"
  101. (let ((pt (open-output-string)))
  102. (set-port-encoding! pt "ASCII")
  103. (set-port-conversion-strategy! pt 'substitute)
  104. (display s4 pt)
  105. (string=? "???"
  106. (get-output-string pt)))))
  107. ;; Check that hex escapes appear in the write output and that no error
  108. ;; is thrown. The output error strategy should be irrelevant here.
  109. (with-test-prefix "display output escapes"
  110. (pass-if "ultima"
  111. (let ((pt (open-output-string)))
  112. (set-port-encoding! pt "ASCII")
  113. (set-port-conversion-strategy! pt 'escape)
  114. (display s1 pt)
  115. (string=? "\\xfaltima"
  116. (get-output-string pt))))
  117. (pass-if "Rashomon"
  118. (let ((pt (open-output-string)))
  119. (set-port-encoding! pt "ASCII")
  120. (set-port-conversion-strategy! pt 'escape)
  121. (display s4 pt)
  122. (string=? "\\u7f85\\u751f\\u9580"
  123. (get-output-string pt))))
  124. (pass-if "fake escape"
  125. ;; The input string below contains something that looks like
  126. ;; an escape in libunistring syntax, but which should be left
  127. ;; as is in the output. See
  128. ;; <http://lists.gnu.org/archive/html/bug-libunistring/2010-09/msg00004.html>
  129. ;; for background info.
  130. (let ((pt (open-output-string)))
  131. (set-port-encoding! pt "ASCII")
  132. (set-port-conversion-strategy! pt 'escape)
  133. (display "λ -- \\u0012" pt)
  134. (string=? "\\u03bb -- \\u0012"
  135. (get-output-string pt)))))
  136. (with-test-prefix "input escapes"
  137. (pass-if "última"
  138. (with-locale "en_US.utf8"
  139. (string=? "última"
  140. (with-input-from-string "\"\\xfaltima\"" read))))
  141. (pass-if "羅生門"
  142. (with-locale "en_US.utf8"
  143. (string=? "羅生門"
  144. (with-input-from-string
  145. "\"\\u7F85\\u751F\\u9580\"" read)))))