1 ;;;; encoding-iso88591.test --- test suite for Guile's string encodings -*- mode: scheme; coding: iso-8859-1 -*-
3 ;;;; Copyright (C) 2009 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 (define exception:conversion
24 (cons 'misc-error "^cannot convert to output locale"))
26 ;; Create a string from integer char values, eg. (string-ints 65) => "A"
27 (define (string-ints . args)
28 (apply string (map integer->char args)))
30 ;; Set locale to the environment's locale, so that the prints look OK.
32 (if (defined? 'setlocale)
33 (set! oldlocale (setlocale LC_ALL "")))
35 (define ascii-a (integer->char 65)) ; LATIN CAPITAL LETTER A
36 (define a-acute (integer->char #x00c1)) ; LATIN CAPITAL LETTER A WITH ACUTE
37 (define alpha (integer->char #x03b1)) ; GREEK SMALL LETTER ALPHA
38 (define cherokee-a (integer->char #x13a0)) ; CHEROKEE LETTER A
40 (with-test-prefix "characters"
44 (pass-if "input A acute"
48 (let ((pt (open-output-string)))
49 (set-port-encoding! pt "ISO-8859-1")
50 (set-port-conversion-strategy! pt 'escape)
53 (get-output-string pt))))
55 (pass-if "display A acute"
56 (let ((pt (open-output-string)))
57 (set-port-encoding! pt "ISO-8859-1")
58 (set-port-conversion-strategy! pt 'escape)
61 (get-output-string pt))))
63 (pass-if "display alpha"
64 (let ((pt (open-output-string)))
65 (set-port-encoding! pt "ISO-8859-1")
66 (set-port-conversion-strategy! pt 'escape)
68 (string-ci=? "\\u03b1"
69 (get-output-string pt))))
71 (pass-if "display Cherokee a"
72 (let ((pt (open-output-string)))
73 (set-port-encoding! pt "ISO-8859-1")
74 (set-port-conversion-strategy! pt 'escape)
75 (display cherokee-a pt)
76 (string-ci=? "\\u13a0"
77 (get-output-string pt))))
80 (let ((pt (open-output-string)))
81 (set-port-encoding! pt "ISO-8859-1")
82 (set-port-conversion-strategy! pt 'escape)
85 (get-output-string pt))))
87 (pass-if "write A acute"
88 (let ((pt (open-output-string)))
89 (set-port-encoding! pt "ISO-8859-1")
90 (set-port-conversion-strategy! pt 'escape)
93 (get-output-string pt)))))
101 (with-test-prefix "string length"
104 (eq? (string-length s1) 6))
107 (eq? (string-length s2) 6))
110 (eq? (string-length s3) 4))
113 (eq? (string-length s4) 6)))
115 (with-test-prefix "internal encoding"
118 (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
121 (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
124 (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
127 (string=? s4 (string-ints #xbf #x43 #xf3 #x6d #x6f #x3f))))
129 (with-test-prefix "chars"
132 (list= eqv? (string->list s1)
133 (list #\ú #\l #\t #\i #\m #\a)))
136 (list= eqv? (string->list s2)
137 (list #\c #\é #\d #\u #\l #\a)))
140 (list= eqv? (string->list s3)
141 (list #\a #\ñ #\o #\s)))
144 (list= eqv? (string->list s4)
145 (list #\¿ #\C #\ó #\m #\o #\?))))
147 (with-test-prefix "symbols == strings"
150 (eq? (string->symbol s1) 'última))
153 (eq? (string->symbol s2) 'cédula))
156 (eq? (string->symbol s3) 'años))
159 (eq? (string->symbol s4) '¿Cómo?)))
161 (with-test-prefix "non-ascii variable names"
168 (with-test-prefix "output errors"
170 (pass-if-exception "char 256" exception:conversion
171 (let ((pt (open-output-string)))
172 (set-port-encoding! pt "ISO-8859-1")
173 (set-port-conversion-strategy! pt 'error)
174 (display (string-ints 256) pt))))
177 (if (defined? 'setlocale)
178 (setlocale LC_ALL oldlocale))