Don't presume existence or success of setlocale in test-suite
[bpt/guile.git] / test-suite / tests / encoding-escapes.test
1 ;;;; encoding-escapes.test --- test suite for Guile's string encodings -*- mode: scheme; coding: utf-8 -*-
2 ;;;;
3 ;;;; Copyright (C) 2009 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
9 ;;;;
10 ;;;; This program 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
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 ;;;; Boston, MA 02110-1301 USA
19
20 (define-module (test-strings)
21 #:use-module (test-suite lib)
22 #:use-module (srfi srfi-1))
23
24 (define exception:conversion
25 (cons 'misc-error "^cannot convert to output locale"))
26
27 ;; Create a string from integer char values, eg. (string-ints 65) => "A"
28 (define (string-ints . args)
29 (apply string (map integer->char args)))
30
31 (define s1 "última")
32 (define s2 "cédula")
33 (define s3 "años")
34 (define s4 "羅生門")
35
36 (with-test-prefix "internal encoding"
37
38 (pass-if "ultima"
39 (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
40
41 (pass-if "cedula"
42 (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
43
44 (pass-if "anos"
45 (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
46
47 (pass-if "Rashomon"
48 (string=? s4 (string-ints #x7f85 #x751f #x9580))))
49
50 (with-test-prefix "chars"
51
52 (pass-if "ultima"
53 (list= eqv? (string->list s1)
54 (list #\372 #\l #\t #\i #\m #\a)))
55
56 (pass-if "cedula"
57 (list= eqv? (string->list s2)
58 (list #\c #\351 #\d #\u #\l #\a)))
59
60 (pass-if "anos"
61 (list= eqv? (string->list s3)
62 (list #\a #\361 #\o #\s)))
63
64 (pass-if "Rashomon"
65 (list= eqv? (string->list s4)
66 (list #\77605 #\72437 #\112600))))
67
68
69 ;; Check that an error is flagged on display output when the output
70 ;; error strategy is 'error
71
72 (with-test-prefix "display output errors"
73
74 (pass-if-exception "ultima"
75 exception:conversion
76 (let ((pt (open-output-string)))
77 (set-port-encoding! pt "ASCII")
78 (set-port-conversion-strategy! pt 'error)
79 (display s1 pt)))
80
81 (pass-if-exception "Rashomon"
82 exception:conversion
83 (let ((pt (open-output-string)))
84 (set-port-encoding! pt "ASCII")
85 (set-port-conversion-strategy! pt 'error)
86 (display s4 pt))))
87
88 ;; Check that questions marks or substitutions appear when the conversion
89 ;; mode is substitute
90 (with-test-prefix "display output substitutions"
91
92 (pass-if "ultima"
93 (let ((pt (open-output-string)))
94 (set-port-encoding! pt "ASCII")
95 (set-port-conversion-strategy! pt 'substitute)
96 (display s1 pt)
97 (string=? "?ltima"
98 (get-output-string pt))))
99
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
108
109 ;; Check that hex escapes appear in the write output and that no error
110 ;; is thrown. The output error strategy should be irrelevant here.
111 (with-test-prefix "display output escapes"
112
113 (pass-if "ultima"
114 (let ((pt (open-output-string)))
115 (set-port-encoding! pt "ASCII")
116 (set-port-conversion-strategy! pt 'escape)
117 (display s1 pt)
118 (string=? "\\xfaltima"
119 (get-output-string pt))))
120 (pass-if "Rashomon"
121 (let ((pt (open-output-string)))
122 (set-port-encoding! pt "ASCII")
123 (set-port-conversion-strategy! pt 'escape)
124 (display s4 pt)
125 (string=? "\\u7F85\\u751F\\u9580"
126 (get-output-string pt)))))
127
128 (with-test-prefix "input escapes"
129
130 (pass-if "última"
131 (with-locale "en_US.utf8"
132 (string=? "última"
133 (with-input-from-string "\"\\xfaltima\"" read))))
134
135 (pass-if "羅生門"
136 (with-locale "en_US.utf8"
137 (string=? "羅生門"
138 (with-input-from-string
139 "\"\\u7F85\\u751F\\u9580\"" read)))))
140