| 1 |
;;;; coding.test --- test suite for coding declarations. -*- mode: scheme -*- |
| 2 |
;;;; |
| 3 |
;;;; Copyright (C) 2011, 2013, 2014 Free Software Foundation, Inc. |
| 4 |
;;;; |
| 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. |
| 9 |
;;;; |
| 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. |
| 14 |
;;;; |
| 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 |
| 18 |
|
| 19 |
(define-module (test-coding) |
| 20 |
#:use-module (test-suite lib)) |
| 21 |
|
| 22 |
(define (with-temp-file proc) |
| 23 |
(let* ((tmpdir (or (getenv "TMPDIR") |
| 24 |
(getenv "TEMP") |
| 25 |
"/tmp")) |
| 26 |
(name (string-append tmpdir "/coding-test.XXXXXX")) |
| 27 |
(port (mkstemp! name))) |
| 28 |
(let ((res (with-throw-handler |
| 29 |
#t |
| 30 |
(lambda () |
| 31 |
(proc name port)) |
| 32 |
(lambda _ |
| 33 |
(delete-file name))))) |
| 34 |
(delete-file name) |
| 35 |
res))) |
| 36 |
|
| 37 |
(define (scan-coding str) |
| 38 |
(with-temp-file |
| 39 |
(lambda (name port) |
| 40 |
(display str port) |
| 41 |
(close port) |
| 42 |
;; We don't simply seek back and rescan, because the encoding scan |
| 43 |
;; relies on the opportunistic filling of the input buffer, which |
| 44 |
;; doesn't happen after a seek. |
| 45 |
(let* ((port (open-input-file name)) |
| 46 |
(res (file-encoding port))) |
| 47 |
(close-port port) |
| 48 |
res)))) |
| 49 |
|
| 50 |
(with-test-prefix "block comments" |
| 51 |
|
| 52 |
(pass-if-equal "first line" |
| 53 |
"ISO-8859-1" |
| 54 |
(scan-coding "#! coding: iso-8859-1 !#")) |
| 55 |
|
| 56 |
(pass-if-equal "first line no whitespace" |
| 57 |
"ISO-8859-1" |
| 58 |
(scan-coding "#!coding:iso-8859-1!#")) |
| 59 |
|
| 60 |
(pass-if-equal "second line" |
| 61 |
"ISO-8859-1" |
| 62 |
(scan-coding "#! \n coding: iso-8859-1 !#")) |
| 63 |
|
| 64 |
(pass-if-equal "second line no whitespace" |
| 65 |
"ISO-8859-1" |
| 66 |
(scan-coding "#!\ncoding:iso-8859-1!#")) |
| 67 |
|
| 68 |
(pass-if-equal "third line" |
| 69 |
"ISO-8859-1" |
| 70 |
(scan-coding "#! \n coding: iso-8859-1 \n !#")) |
| 71 |
|
| 72 |
(pass-if-equal "third line no whitespace" |
| 73 |
"ISO-8859-1" |
| 74 |
(scan-coding "#!\ncoding:iso-8859-1\n!#"))) |
| 75 |
|
| 76 |
(with-test-prefix "line comment" |
| 77 |
(pass-if-equal "first line, no whitespace, no nl" |
| 78 |
"ISO-8859-1" |
| 79 |
(scan-coding ";coding:iso-8859-1")) |
| 80 |
|
| 81 |
(pass-if-equal "first line, whitespace, no nl" |
| 82 |
"ISO-8859-1" |
| 83 |
(scan-coding "; coding: iso-8859-1 ")) |
| 84 |
|
| 85 |
(pass-if-equal "first line, no whitespace, nl" |
| 86 |
"ISO-8859-1" |
| 87 |
(scan-coding ";coding:iso-8859-1\n")) |
| 88 |
|
| 89 |
(pass-if-equal "first line, whitespace, nl" |
| 90 |
"ISO-8859-1" |
| 91 |
(scan-coding "; coding: iso-8859-1 \n")) |
| 92 |
|
| 93 |
(pass-if-equal "second line, no whitespace, no nl" |
| 94 |
"ISO-8859-1" |
| 95 |
(scan-coding "\n;coding:iso-8859-1")) |
| 96 |
|
| 97 |
(pass-if-equal "second line, whitespace, no nl" |
| 98 |
"ISO-8859-1" |
| 99 |
(scan-coding "\n; coding: iso-8859-1 ")) |
| 100 |
|
| 101 |
(pass-if-equal "second line, no whitespace, nl" |
| 102 |
"ISO-8859-1" |
| 103 |
(scan-coding "\n;coding:iso-8859-1\n")) |
| 104 |
|
| 105 |
(pass-if-equal "second line, whitespace, nl" |
| 106 |
"ISO-8859-1" |
| 107 |
(scan-coding "\n; coding: iso-8859-1 \n")) |
| 108 |
|
| 109 |
(pass-if-equal "http://bugs.gnu.org/16463" |
| 110 |
;; On Guile <= 2.0.9, this would return "ISO-8". |
| 111 |
"ISO-8859-1" |
| 112 |
(scan-coding (string-append (make-string 485 #\space) |
| 113 |
"; coding: ISO-8859-1")))) |