1 ;;;; encoding-escapes.test --- test suite for Guile's string encodings -*- mode: scheme; coding: utf-8 -*-
3 ;;;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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
19 (define-module (test-strings)
20 #:use-module (test-suite lib)
21 #:use-module (srfi srfi-1))
23 ;; Create a string from integer char values, eg. (string-ints 65) => "A"
24 (define (string-ints . args)
25 (apply string (map integer->char args)))
32 (with-test-prefix "internal encoding"
35 (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
38 (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
41 (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
44 (string=? s4 (string-ints #x7f85 #x751f #x9580))))
46 (with-test-prefix "chars"
49 (list= eqv? (string->list s1)
50 (list #\372 #\l #\t #\i #\m #\a)))
53 (list= eqv? (string->list s2)
54 (list #\c #\351 #\d #\u #\l #\a)))
57 (list= eqv? (string->list s3)
58 (list #\a #\361 #\o #\s)))
61 (list= eqv? (string->list s4)
62 (list #\77605 #\72437 #\112600))))
65 ;; Check that an error is flagged on display output when the output
66 ;; error strategy is 'error
68 (with-test-prefix "display output errors"
71 (let ((pt (open-output-string)))
72 (set-port-encoding! pt "ASCII")
73 (set-port-conversion-strategy! pt 'error)
74 (catch 'encoding-error
78 (lambda (key subr message errno port chr)
80 (char=? chr (string-ref s1 0))
81 (string=? (get-output-string pt) ""))))))
84 (let ((pt (open-output-string)))
85 (set-port-encoding! pt "ASCII")
86 (set-port-conversion-strategy! pt 'error)
87 (catch 'encoding-error
91 (lambda (key subr message errno port chr)
93 (char=? chr (string-ref s4 0))
94 (string=? (get-output-string pt) ""))))))
97 (let ((pt (open-output-string)))
98 (set-port-encoding! pt "ASCII")
99 (set-port-conversion-strategy! pt 'error)
100 (catch 'encoding-error
102 ;; This time encoding should fail on the 3rd character.
103 (display "teĥniko" pt)
105 (lambda (key subr message errno port chr)
108 (string=? "te" (get-output-string pt))))))))
110 ;; Check that questions marks or substitutions appear when the conversion
111 ;; mode is substitute
112 (with-test-prefix "display output substitutions"
115 (let ((pt (open-output-string)))
116 (set-port-encoding! pt "ASCII")
117 (set-port-conversion-strategy! pt 'substitute)
120 (get-output-string pt))))
123 (let ((pt (open-output-string)))
124 (set-port-encoding! pt "ASCII")
125 (set-port-conversion-strategy! pt 'substitute)
128 (get-output-string pt)))))
131 ;; Check that hex escapes appear in the write output and that no error
132 ;; is thrown. The output error strategy should be irrelevant here.
133 (with-test-prefix "display output escapes"
136 (let ((pt (open-output-string)))
137 (set-port-encoding! pt "ASCII")
138 (set-port-conversion-strategy! pt 'escape)
140 (string=? "\\xfaltima"
141 (get-output-string pt))))
143 (let ((pt (open-output-string)))
144 (set-port-encoding! pt "ASCII")
145 (set-port-conversion-strategy! pt 'escape)
147 (string=? "\\u7f85\\u751f\\u9580"
148 (get-output-string pt))))
150 (pass-if "fake escape"
151 ;; The input string below contains something that looks like
152 ;; an escape in libunistring syntax, but which should be left
153 ;; as is in the output. See
154 ;; <http://lists.gnu.org/archive/html/bug-libunistring/2010-09/msg00004.html>
155 ;; for background info.
156 (let ((pt (open-output-string)))
157 (set-port-encoding! pt "ASCII")
158 (set-port-conversion-strategy! pt 'escape)
159 (display "λ -- \\u0012" pt)
160 (string=? "\\u03bb -- \\u0012"
161 (get-output-string pt)))))
163 (with-test-prefix "input escapes"
166 (with-locale "en_US.utf8"
168 (with-input-from-string "\"\\xfaltima\"" read))))
171 (with-locale "en_US.utf8"
173 (with-input-from-string
174 "\"\\u7F85\\u751F\\u9580\"" read)))))