1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 11:50:28 +02:00
guile/test-suite/tests/rdelim.test
Ludovic Courtès 96128014bf Make sure binary ports pass `binary-port?' regardless of the locale.
* libguile/r6rs-ports.c (make_bip, make_cbip, make_bop, make_cbop):
  Set `c_port->encoding' to NULL.

* test-suite/tests/r6rs-ports.test ("7.2.7 Input
  Ports")["bytevector-input-port is binary"]: New test.
  ("7.2.7 Input Ports")["make-custom-binary-input-port"]: Make sure PORT
  passes `binary-port?' and `input-port?'.
  ("8.2.10 Output ports")["bytevector-output-port is binary"]: New test.
  ["make-custom-binary-output"]: Rename to...
  ["make-custom-binary-output-port"]: ... this.

* test-suite/tests/ports.test ("string ports")["read-char, wrong
  encoding, error", "read-char, wrong encoding, escape", "read-char,
  wrong encoding, substitute", "peek-char, wrong encoding, error"]: Use
  `set-port-encoding!' instead of `%default-port-encoding' to set the
  encoding of bytevector input ports.

* test-suite/tests/rdelim.test ("read-line")["decoding error", "decoding
  error, substitute"]: Likewise.

* doc/ref/api-io.texi (R6RS Port Manipulation): Document `binary-port?'
  and `textual-port?'.

* doc/ref/r6rs.texi (R6RS Incompatibilities): Mention the soft
  distinction between textual and binary ports.
2011-04-22 23:58:00 +02:00

197 lines
7.3 KiB
Scheme
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;;; rdelim.test --- Delimited I/O. -*- mode: scheme; coding: utf-8; -*-
;;;; Ludovic Courtès <ludo@gnu.org>
;;;;
;;;; Copyright (C) 2011 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-suite test-rdelim)
#:use-module (ice-9 rdelim)
#:use-module ((rnrs io ports) #:select (open-bytevector-input-port))
#:use-module (test-suite lib))
(with-fluids ((%default-port-encoding "UTF-8"))
(with-test-prefix "read-line"
(pass-if "one line"
(let* ((s "hello, world")
(p (open-input-string s)))
(and (string=? s (read-line p))
(eof-object? (read-line p)))))
(pass-if "two lines, trim"
(let* ((s "foo\nbar\n")
(p (open-input-string s)))
(and (equal? (string-tokenize s)
(list (read-line p) (read-line p)))
(eof-object? (read-line p)))))
(pass-if "two lines, concat"
(let* ((s "foo\nbar\n")
(p (open-input-string s)))
(and (equal? '("foo\n" "bar\n")
(list (read-line p 'concat)
(read-line p 'concat)))
(eof-object? (read-line p)))))
(pass-if "two lines, peek"
(let* ((s "foo\nbar\n")
(p (open-input-string s)))
(and (equal? '("foo" #\newline "bar" #\newline)
(list (read-line p 'peek) (read-char p)
(read-line p 'peek) (read-char p)))
(eof-object? (read-line p)))))
(pass-if "two lines, split"
(let* ((s "foo\nbar\n")
(p (open-input-string s)))
(and (equal? '(("foo" . #\newline)
("bar" . #\newline))
(list (read-line p 'split)
(read-line p 'split)))
(eof-object? (read-line p)))))
(pass-if "two Greek lines, trim"
(let* ((s "λαμβδα\nμυ\n")
(p (open-input-string s)))
(and (equal? (string-tokenize s)
(list (read-line p) (read-line p)))
(eof-object? (read-line p)))))
(pass-if "decoding error"
(let ((p (open-bytevector-input-port #vu8(65 255 66 67 68))))
(set-port-encoding! p "UTF-8")
(set-port-conversion-strategy! p 'error)
(catch 'decoding-error
(lambda ()
(read-line p)
#f)
(lambda (key subr message err port)
(and (eq? port p)
;; PORT should now point past the error.
(string=? (read-line p) "BCD")
(eof-object? (read-line p)))))))
(pass-if "decoding error, substitute"
(let ((p (open-bytevector-input-port #vu8(65 255 66 67 68))))
(set-port-encoding! p "UTF-8")
(set-port-conversion-strategy! p 'substitute)
(and (string=? (read-line p) "A?BCD")
(eof-object? (read-line p))))))
(with-test-prefix "read-delimited"
(pass-if "delimiter hit"
(let ((p (open-input-string "hello, world!")))
(and (string=? "hello" (read-delimited ",.;" p))
(string=? " world!" (read-delimited ",.;" p))
(eof-object? (read-delimited ",.;" p)))))
(pass-if "delimiter hit, split"
(equal? '("hello" . #\,)
(read-delimited ",.;"
(open-input-string "hello, world!")
'split)))
(pass-if "delimiter hit, concat"
(equal? '"hello,"
(read-delimited ",.;" (open-input-string "hello, world!")
'concat)))
(pass-if "delimiter hit, peek"
(let ((p (open-input-string "hello, world!")))
(and (string=? "hello" (read-delimited ",.;" p 'peek))
(char=? #\, (peek-char p)))))
(pass-if "eof"
(eof-object? (read-delimited "}{" (open-input-string "")))))
(with-test-prefix "read-delimited!"
(pass-if "delimiter hit"
(let ((s (make-string 123))
(p (open-input-string "hello, world!")))
(and (= 5 (read-delimited! ",.;" s p))
(string=? (substring s 0 5) "hello")
(= 7 (read-delimited! ",.;" s p))
(string=? (substring s 0 7) " world!")
(eof-object? (read-delimited! ",.;" s p)))))
(pass-if "delimiter hit, start+end"
(let ((s (make-string 123))
(p (open-input-string "hello, world!")))
(and (= 5 (read-delimited! ",.;" s p 'trim 10 30))
(string=? (substring s 10 15) "hello"))))
(pass-if "delimiter hit, split"
(let ((s (make-string 123)))
(and (equal? '(5 . #\,)
(read-delimited! ",.;" s
(open-input-string "hello, world!")
'split))
(string=? (substring s 0 5) "hello"))))
(pass-if "delimiter hit, concat"
(let ((s (make-string 123)))
(and (= 6 (read-delimited! ",.;" s
(open-input-string "hello, world!")
'concat))
(string=? (substring s 0 6) "hello,"))))
(pass-if "delimiter hit, peek"
(let ((s (make-string 123))
(p (open-input-string "hello, world!")))
(and (= 5 (read-delimited! ",.;" s p 'peek))
(string=? (substring s 0 5) "hello")
(char=? #\, (peek-char p)))))
(pass-if "string too small"
(let ((s (make-string 7)))
(and (= 7 (read-delimited! "}{" s
(open-input-string "hello, world!")))
(string=? s "hello, "))))
(pass-if "string too small, start+end"
(let ((s (make-string 123)))
(and (= 7 (read-delimited! "}{" s
(open-input-string "hello, world!")
'trim
70 77))
(string=? (substring s 70 77) "hello, "))))
(pass-if "string too small, split"
(let ((s (make-string 7)))
(and (equal? '(7 . #f)
(read-delimited! "}{" s
(open-input-string "hello, world!")
'split))
(string=? s "hello, "))))
(pass-if "eof"
(eof-object? (read-delimited! ":" (make-string 7)
(open-input-string ""))))
(pass-if "eof, split"
(eof-object? (read-delimited! ":" (make-string 7)
(open-input-string ""))))))
;;; Local Variables:
;;; eval: (put 'with-test-prefix 'scheme-indent-function 1)
;;; eval: (put 'pass-if 'scheme-indent-function 1)
;;; End: