1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-18 01:22:24 +02:00
guile/test-suite/tests/encoding-escapes.test
Ludovic Courtès a5229ee822 Switch the `encoding*.test' files to LGPLv3+.
* test-suite/tests/encoding-escapes.test,
  test-suite/tests/encoding-iso88591.test,
  test-suite/tests/encoding-iso88597.test,
  test-suite/tests/encoding-utf8.test: Switch to LGPLv3+ for the sake of
  consistency.
2009-09-14 00:42:25 +02:00

139 lines
4.3 KiB
Scheme

;;;; encoding-escapes.test --- test suite for Guile's string encodings -*- mode: scheme; coding: utf-8 -*-
;;;;
;;;; Copyright (C) 2009 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
(define-module (test-strings)
#:use-module (test-suite lib)
#:use-module (srfi srfi-1))
(define exception:conversion
(cons 'misc-error "^cannot convert to output locale"))
;; Create a string from integer char values, eg. (string-ints 65) => "A"
(define (string-ints . args)
(apply string (map integer->char args)))
(define s1 "última")
(define s2 "cédula")
(define s3 "años")
(define s4 "羅生門")
(with-test-prefix "internal encoding"
(pass-if "ultima"
(string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
(pass-if "cedula"
(string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
(pass-if "anos"
(string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
(pass-if "Rashomon"
(string=? s4 (string-ints #x7f85 #x751f #x9580))))
(with-test-prefix "chars"
(pass-if "ultima"
(list= eqv? (string->list s1)
(list #\372 #\l #\t #\i #\m #\a)))
(pass-if "cedula"
(list= eqv? (string->list s2)
(list #\c #\351 #\d #\u #\l #\a)))
(pass-if "anos"
(list= eqv? (string->list s3)
(list #\a #\361 #\o #\s)))
(pass-if "Rashomon"
(list= eqv? (string->list s4)
(list #\77605 #\72437 #\112600))))
;; Check that an error is flagged on display output when the output
;; error strategy is 'error
(with-test-prefix "display output errors"
(pass-if-exception "ultima"
exception:conversion
(let ((pt (open-output-string)))
(set-port-encoding! pt "ASCII")
(set-port-conversion-strategy! pt 'error)
(display s1 pt)))
(pass-if-exception "Rashomon"
exception:conversion
(let ((pt (open-output-string)))
(set-port-encoding! pt "ASCII")
(set-port-conversion-strategy! pt 'error)
(display s4 pt))))
;; Check that questions marks or substitutions appear when the conversion
;; mode is substitute
(with-test-prefix "display output substitutions"
(pass-if "ultima"
(let ((pt (open-output-string)))
(set-port-encoding! pt "ASCII")
(set-port-conversion-strategy! pt 'substitute)
(display s1 pt)
(string=? "?ltima"
(get-output-string pt))))
(pass-if "Rashomon"
(let ((pt (open-output-string)))
(set-port-encoding! pt "ASCII")
(set-port-conversion-strategy! pt 'substitute)
(display s4 pt)
(string=? "???"
(get-output-string pt)))))
;; Check that hex escapes appear in the write output and that no error
;; is thrown. The output error strategy should be irrelevant here.
(with-test-prefix "display output escapes"
(pass-if "ultima"
(let ((pt (open-output-string)))
(set-port-encoding! pt "ASCII")
(set-port-conversion-strategy! pt 'escape)
(display s1 pt)
(string=? "\\xfaltima"
(get-output-string pt))))
(pass-if "Rashomon"
(let ((pt (open-output-string)))
(set-port-encoding! pt "ASCII")
(set-port-conversion-strategy! pt 'escape)
(display s4 pt)
(string=? "\\u7F85\\u751F\\u9580"
(get-output-string pt)))))
(with-test-prefix "input escapes"
(pass-if "última"
(with-locale "en_US.utf8"
(string=? "última"
(with-input-from-string "\"\\xfaltima\"" read))))
(pass-if "羅生門"
(with-locale "en_US.utf8"
(string=? "羅生門"
(with-input-from-string
"\"\\u7F85\\u751F\\u9580\"" read)))))