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