1 ;;;; encoding-iso88591.test --- test suite for Guile's string encodings -*- mode: scheme; coding: iso-8859-1 -*-
3 ;;;; Copyright (C) 2009, 2010, 2014 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)))
27 ;; Set locale to the environment's locale, so that the prints look OK.
28 (when (defined? 'setlocale)
29 (setlocale LC_ALL ""))
31 (define ascii-a (integer->char 65)) ; LATIN CAPITAL LETTER A
32 (define a-acute (integer->char #x00c1)) ; LATIN CAPITAL LETTER A WITH ACUTE
33 (define alpha (integer->char #x03b1)) ; GREEK SMALL LETTER ALPHA
34 (define cherokee-a (integer->char #x13a0)) ; CHEROKEE LETTER A
36 (with-test-prefix "characters"
40 (pass-if "input A acute"
44 (let ((pt (open-output-string)))
45 (set-port-encoding! pt "ISO-8859-1")
46 (set-port-conversion-strategy! pt 'escape)
49 (get-output-string pt))))
51 (pass-if "display A acute"
52 (let ((pt (open-output-string)))
53 (set-port-encoding! pt "ISO-8859-1")
54 (set-port-conversion-strategy! pt 'escape)
57 (get-output-string pt))))
59 (pass-if "display alpha"
60 (let ((pt (open-output-string)))
61 (set-port-encoding! pt "ISO-8859-1")
62 (set-port-conversion-strategy! pt 'escape)
64 (string-ci=? "\\u03b1"
65 (get-output-string pt))))
67 (pass-if "display Cherokee a"
68 (let ((pt (open-output-string)))
69 (set-port-encoding! pt "ISO-8859-1")
70 (set-port-conversion-strategy! pt 'escape)
71 (display cherokee-a pt)
72 (string-ci=? "\\u13a0"
73 (get-output-string pt))))
76 (let ((pt (open-output-string)))
77 (set-port-encoding! pt "ISO-8859-1")
78 (set-port-conversion-strategy! pt 'escape)
81 (get-output-string pt))))
83 (pass-if "write A acute"
84 (let ((pt (open-output-string)))
85 (set-port-encoding! pt "ISO-8859-1")
86 (set-port-conversion-strategy! pt 'escape)
89 (get-output-string pt))))
91 (pass-if "write A followed by combining accent"
92 (let ((pt (open-output-string)))
93 (set-port-encoding! pt "ISO-8859-1")
94 (set-port-conversion-strategy! pt 'escape)
95 (write (string #\A (integer->char #x030f)) pt)
96 (string-ci=? "\"A\\u030f\""
97 (get-output-string pt)))))
105 (with-test-prefix "string length"
108 (eqv? (string-length s1) 6))
111 (eqv? (string-length s2) 6))
114 (eqv? (string-length s3) 4))
117 (eqv? (string-length s4) 6)))
119 (with-test-prefix "internal encoding"
122 (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
125 (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
128 (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
131 (string=? s4 (string-ints #xbf #x43 #xf3 #x6d #x6f #x3f))))
133 (with-test-prefix "chars"
136 (list= eqv? (string->list s1)
137 (list #\ú #\l #\t #\i #\m #\a)))
140 (list= eqv? (string->list s2)
141 (list #\c #\é #\d #\u #\l #\a)))
144 (list= eqv? (string->list s3)
145 (list #\a #\ñ #\o #\s)))
148 (list= eqv? (string->list s4)
149 (list #\¿ #\C #\ó #\m #\o #\?))))
151 (with-test-prefix "symbols == strings"
154 (eq? (string->symbol s1) 'última))
157 (eq? (string->symbol s2) 'cédula))
160 (eq? (string->symbol s3) 'años))
163 (eq? (string->symbol s4) '¿Cómo?)))
165 (with-test-prefix "non-ascii variable names"
172 (with-test-prefix "output errors"
174 (pass-if-exception "char 256" exception:encoding-error
175 (let ((pt (open-output-string)))
176 (set-port-encoding! pt "ISO-8859-1")
177 (set-port-conversion-strategy! pt 'error)
178 (display (string-ints 256) pt))))