coding.test 3.6 KB

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