1 ;;;; encoding-escapes.test --- test suite for Guile's string encodings -*- mode: scheme; coding: utf-8 -*-
3 ;;;; Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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
20 (define-module (test-strings)
21 #:use-module (test-suite lib)
22 #:use-module (srfi srfi-1))
24 (define exception:conversion
25 (cons 'misc-error "^cannot convert to output locale"))
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)))
36 (with-test-prefix "internal encoding"
39 (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
42 (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
45 (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
48 (string=? s4 (string-ints #x7f85 #x751f #x9580))))
50 (with-test-prefix "chars"
53 (list= eqv? (string->list s1)
54 (list #\372 #\l #\t #\i #\m #\a)))
57 (list= eqv? (string->list s2)
58 (list #\c #\351 #\d #\u #\l #\a)))
61 (list= eqv? (string->list s3)
62 (list #\a #\361 #\o #\s)))
65 (list= eqv? (string->list s4)
66 (list #\77605 #\72437 #\112600))))
69 ;; Check that an error is flagged on display output when the output
70 ;; error strategy is 'error
72 (with-test-prefix "display output errors"
74 (pass-if-exception "ultima"
76 (let ((pt (open-output-string)))
77 (set-port-encoding! pt "ASCII")
78 (set-port-conversion-strategy! pt 'error)
81 (pass-if-exception "Rashomon"
83 (let ((pt (open-output-string)))
84 (set-port-encoding! pt "ASCII")
85 (set-port-conversion-strategy! pt 'error)
88 ;; Check that questions marks or substitutions appear when the conversion
90 (with-test-prefix "display output substitutions"
93 (let ((pt (open-output-string)))
94 (set-port-encoding! pt "ASCII")
95 (set-port-conversion-strategy! pt 'substitute)
98 (get-output-string pt))))
101 (let ((pt (open-output-string)))
102 (set-port-encoding! pt "ASCII")
103 (set-port-conversion-strategy! pt 'substitute)
106 (get-output-string pt)))))
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"
114 (let ((pt (open-output-string)))
115 (set-port-encoding! pt "ASCII")
116 (set-port-conversion-strategy! pt 'escape)
118 (string=? "\\xfaltima"
119 (get-output-string pt))))
121 (let ((pt (open-output-string)))
122 (set-port-encoding! pt "ASCII")
123 (set-port-conversion-strategy! pt 'escape)
125 (string=? "\\u7F85\\u751F\\u9580"
126 (get-output-string pt)))))
128 (with-test-prefix "input escapes"
131 (with-locale "en_US.utf8"
133 (with-input-from-string "\"\\xfaltima\"" read))))
136 (with-locale "en_US.utf8"
138 (with-input-from-string
139 "\"\\u7F85\\u751F\\u9580\"" read)))))