PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/test/src/lread-tests.el

https://gitlab.com/RobertCochran/emacs
Emacs Lisp | 226 lines | 172 code | 34 blank | 20 comment | 1 complexity | 3ce7f5d03267759ff1d45f7f1b138de9 MD5 | raw file
  1. ;;; lread-tests.el --- tests for lread.c -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. ;; Author: Philipp Stephani <phst@google.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; Unit tests for code in src/lread.c.
  17. ;;; Code:
  18. (ert-deftest lread-char-number ()
  19. (should (equal (read "?\\N{U+A817}") #xA817)))
  20. (ert-deftest lread-char-name-1 ()
  21. (should (equal (read "?\\N{SYLOTI NAGRI LETTER \n DHO}")
  22. #xA817)))
  23. (ert-deftest lread-char-name-2 ()
  24. (should (equal (read "?\\N{BED}") #x1F6CF)))
  25. (ert-deftest lread-char-name-3 ()
  26. (should (equal (read "?\\N{U+BED}") #xBED)))
  27. (ert-deftest lread-char-name-4 ()
  28. (should (equal (read "?\\N{VARIATION SELECTOR-1}") #xFE00)))
  29. (ert-deftest lread-char-name-5 ()
  30. (should (equal (read "?\\N{VARIATION SELECTOR-16}") #xFE0F)))
  31. (ert-deftest lread-char-name-6 ()
  32. (should (equal (read "?\\N{VARIATION SELECTOR-17}") #xE0100)))
  33. (ert-deftest lread-char-name-7 ()
  34. (should (equal (read "?\\N{VARIATION SELECTOR-256}") #xE01EF)))
  35. (ert-deftest lread-char-name-8 ()
  36. (should (equal (read "?\\N{CJK COMPATIBILITY IDEOGRAPH-F900}") #xF900)))
  37. (ert-deftest lread-char-name-9 ()
  38. (should (equal (read "?\\N{CJK COMPATIBILITY IDEOGRAPH-FAD9}") #xFAD9)))
  39. (ert-deftest lread-char-name-10 ()
  40. (should (equal (read "?\\N{CJK COMPATIBILITY IDEOGRAPH-2F800}") #x2F800)))
  41. (ert-deftest lread-char-name-11 ()
  42. (should (equal (read "?\\N{CJK COMPATIBILITY IDEOGRAPH-2FA1D}") #x2FA1D)))
  43. (ert-deftest lread-char-invalid-number ()
  44. (should-error (read "?\\N{U+110000}") :type 'invalid-read-syntax))
  45. (ert-deftest lread-char-invalid-name-1 ()
  46. (should-error (read "?\\N{DOES NOT EXIST}")) :type 'invalid-read-syntax)
  47. (ert-deftest lread-char-invalid-name-2 ()
  48. (should-error (read "?\\N{VARIATION SELECTOR-0}")) :type 'invalid-read-syntax)
  49. (ert-deftest lread-char-invalid-name-3 ()
  50. (should-error (read "?\\N{VARIATION SELECTOR-257}"))
  51. :type 'invalid-read-syntax)
  52. (ert-deftest lread-char-invalid-name-4 ()
  53. (should-error (read "?\\N{VARIATION SELECTOR--0}"))
  54. :type 'invalid-read-syntax)
  55. (ert-deftest lread-char-invalid-name-5 ()
  56. (should-error (read "?\\N{CJK COMPATIBILITY IDEOGRAPH-F8FF}"))
  57. :type 'invalid-read-syntax)
  58. (ert-deftest lread-char-invalid-name-6 ()
  59. (should-error (read "?\\N{CJK COMPATIBILITY IDEOGRAPH-FADA}"))
  60. :type 'invalid-read-syntax)
  61. (ert-deftest lread-char-invalid-name-7 ()
  62. (should-error (read "?\\N{CJK COMPATIBILITY IDEOGRAPH-2F7FF}"))
  63. :type 'invalid-read-syntax)
  64. (ert-deftest lread-char-invalid-name-8 ()
  65. (should-error (read "?\\N{CJK COMPATIBILITY IDEOGRAPH-2FA1E}"))
  66. :type 'invalid-read-syntax)
  67. (ert-deftest lread-char-non-ascii-name ()
  68. (should-error (read "?\\N{LATIN CAPITAL LETTER Ø}")
  69. :type 'invalid-read-syntax))
  70. (ert-deftest lread-char-empty-name ()
  71. (should-error (read "?\\N{}") :type 'invalid-read-syntax))
  72. (ert-deftest lread-char-surrogate-1 ()
  73. (should-error (read "?\\N{U+D800}") :type 'invalid-read-syntax))
  74. (ert-deftest lread-char-surrogate-2 ()
  75. (should-error (read "?\\N{U+D801}") :type 'invalid-read-syntax))
  76. (ert-deftest lread-char-surrogate-3 ()
  77. (should-error (read "?\\N{U+Dffe}") :type 'invalid-read-syntax))
  78. (ert-deftest lread-char-surrogate-4 ()
  79. (should-error (read "?\\N{U+DFFF}") :type 'invalid-read-syntax))
  80. (ert-deftest lread-string-char-number-1 ()
  81. (should (equal (read "\"a\\N{U+A817}b\"") "a\uA817b")))
  82. (ert-deftest lread-string-char-number-2 ()
  83. (should-error (read "?\\N{0.5}") :type 'invalid-read-syntax))
  84. (ert-deftest lread-string-char-number-3 ()
  85. (should-error (read "?\\N{U+-0}") :type 'invalid-read-syntax))
  86. (ert-deftest lread-string-char-name ()
  87. (should (equal (read "\"a\\N{SYLOTI NAGRI LETTER DHO}b\"") "a\uA817b")))
  88. (ert-deftest lread-empty-int-literal ()
  89. "Check that Bug#25120 is fixed."
  90. (should-error (read "#b") :type 'invalid-read-syntax)
  91. (should-error (read "#o") :type 'invalid-read-syntax)
  92. (should-error (read "#x") :type 'invalid-read-syntax)
  93. (should-error (read "#24r") :type 'invalid-read-syntax)
  94. (should-error (read "#") :type 'invalid-read-syntax))
  95. (ert-deftest lread-record-1 ()
  96. (should (equal '(#s(foo) #s(foo))
  97. (read "(#1=#s(foo) #1#)"))))
  98. (defmacro lread-tests--with-temp-file (file-name-var &rest body)
  99. (declare (indent 1))
  100. (cl-check-type file-name-var symbol)
  101. `(let ((,file-name-var (make-temp-file "emacs")))
  102. (unwind-protect
  103. (progn ,@body)
  104. (delete-file ,file-name-var))))
  105. (defun lread-tests--last-message ()
  106. (with-current-buffer "*Messages*"
  107. (save-excursion
  108. (goto-char (point-max))
  109. (skip-chars-backward "\n")
  110. (buffer-substring (line-beginning-position) (point)))))
  111. (ert-deftest lread-tests--unescaped-char-literals ()
  112. "Check that loading warns about unescaped character
  113. literals (Bug#20852)."
  114. (lread-tests--with-temp-file file-name
  115. (write-region "?) ?( ?; ?\" ?[ ?]" nil file-name)
  116. (should (equal (load file-name nil :nomessage :nosuffix) t))
  117. (should (equal (lread-tests--last-message)
  118. (concat (format-message "Loading `%s': " file-name)
  119. "unescaped character literals "
  120. "`?\"', `?(', `?)', `?;', `?[', `?]' detected, "
  121. "`?\\\"', `?\\(', `?\\)', `?\\;', `?\\[', `?\\]' "
  122. "expected!")))))
  123. (ert-deftest lread-tests--funny-quote-symbols ()
  124. "Check that 'smart quotes' or similar trigger errors in symbol names."
  125. (dolist (quote-char
  126. '(#x2018 ;; LEFT SINGLE QUOTATION MARK
  127. #x2019 ;; RIGHT SINGLE QUOTATION MARK
  128. #x201B ;; SINGLE HIGH-REVERSED-9 QUOTATION MARK
  129. #x201C ;; LEFT DOUBLE QUOTATION MARK
  130. #x201D ;; RIGHT DOUBLE QUOTATION MARK
  131. #x201F ;; DOUBLE HIGH-REVERSED-9 QUOTATION MARK
  132. #x301E ;; DOUBLE PRIME QUOTATION MARK
  133. #xFF02 ;; FULLWIDTH QUOTATION MARK
  134. #xFF07 ;; FULLWIDTH APOSTROPHE
  135. ))
  136. (let ((str (format "%cfoo" quote-char)))
  137. (should-error (read str) :type 'invalid-read-syntax)
  138. (should (eq (read (concat "\\" str)) (intern str))))))
  139. (ert-deftest lread-test-bug26837 ()
  140. "Test for https://debbugs.gnu.org/26837 ."
  141. (let ((load-path (cons
  142. (file-name-as-directory
  143. (expand-file-name "data" (getenv "EMACS_TEST_DIRECTORY")))
  144. load-path)))
  145. (load "somelib" nil t)
  146. (should (string-suffix-p "/somelib.el" (caar load-history)))
  147. (load "somelib2" nil t)
  148. (should (string-suffix-p "/somelib2.el" (caar load-history)))
  149. (load "somelib" nil t)
  150. (should (string-suffix-p "/somelib.el" (caar load-history)))))
  151. (ert-deftest lread-tests--old-style-backquotes ()
  152. "Check that loading doesn't accept old-style backquotes."
  153. (lread-tests--with-temp-file file-name
  154. (write-region "(` (a b))" nil file-name)
  155. (let ((data (should-error (load file-name nil :nomessage :nosuffix))))
  156. (should (equal (cdr data)
  157. (list (concat (format-message "Loading `%s': " file-name)
  158. "old-style backquotes detected!")))))))
  159. (ert-deftest lread-tests--force-new-style-backquotes ()
  160. (let ((data (should-error (read "(` (a b))"))))
  161. (should (equal (cdr data) '("Old-style backquotes detected!"))))
  162. (should (equal (let ((force-new-style-backquotes t))
  163. (read "(` (a b))"))
  164. '(`(a b)))))
  165. (ert-deftest lread-lread--substitute-object-in-subtree ()
  166. (let ((x (cons 0 1)))
  167. (setcar x x)
  168. (lread--substitute-object-in-subtree x 1 t)
  169. (should (eq x (cdr x)))))
  170. (ert-deftest lread-long-hex-integer ()
  171. (should (bignump (read "#xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"))))
  172. (ert-deftest lread-test-bug-31186 ()
  173. (with-temp-buffer
  174. (insert ";; -*- -:*-")
  175. (should-not
  176. ;; This used to crash in lisp_file_lexically_bound_p before the
  177. ;; bug was fixed.
  178. (eval-buffer))))
  179. (ert-deftest lread-invalid-bytecodes ()
  180. (should-error
  181. (let ((load-force-doc-strings t)) (read "#[0 \"\"]"))))
  182. (ert-deftest lread-string-to-number-trailing-dot ()
  183. (dolist (n (list (* most-negative-fixnum most-negative-fixnum)
  184. (1- most-negative-fixnum) most-negative-fixnum
  185. (1+ most-negative-fixnum) -1 0 1
  186. (1- most-positive-fixnum) most-positive-fixnum
  187. (1+ most-positive-fixnum)
  188. (* most-positive-fixnum most-positive-fixnum)))
  189. (should (= n (string-to-number (format "%d." n))))))
  190. (ert-deftest lread-circular-hash ()
  191. (should-error (read "#s(hash-table data #0=(#0# . #0#))")))
  192. ;;; lread-tests.el ends here