Treat control characters in JSON strings as invalid
[emacs.git] / test / lisp / json-tests.el
blob1d13ccf074f5abb3cd6c588c0c0c292182400fbc
1 ;;; json-tests.el --- Test suite for json.el
3 ;; Copyright (C) 2015-2017 Free Software Foundation, Inc.
5 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ;;; Code:
22 (require 'ert)
23 (require 'json)
25 (defmacro json-tests--with-temp-buffer (content &rest body)
26 "Create a temporary buffer with CONTENT and evaluate BODY there.
27 Point is moved to beginning of the buffer."
28 (declare (indent 1))
29 `(with-temp-buffer
30 (insert ,content)
31 (goto-char (point-min))
32 ,@body))
34 ;;; Utilities
36 (ert-deftest test-json-join ()
37 (should (equal (json-join '() ", ") ""))
38 (should (equal (json-join '("a" "b" "c") ", ") "a, b, c")))
40 (ert-deftest test-json-alist-p ()
41 (should (json-alist-p '()))
42 (should (json-alist-p '((a 1) (b 2) (c 3))))
43 (should (json-alist-p '((:a 1) (:b 2) (:c 3))))
44 (should (json-alist-p '(("a" 1) ("b" 2) ("c" 3))))
45 (should-not (json-alist-p '(:a :b :c)))
46 (should-not (json-alist-p '(:a 1 :b 2 :c 3)))
47 (should-not (json-alist-p '((:a 1) (:b 2) 3))))
49 (ert-deftest test-json-plist-p ()
50 (should (json-plist-p '()))
51 (should (json-plist-p '(:a 1 :b 2 :c 3)))
52 (should-not (json-plist-p '(a 1 b 2 c 3)))
53 (should-not (json-plist-p '("a" 1 "b" 2 "c" 3)))
54 (should-not (json-plist-p '(:a :b :c)))
55 (should-not (json-plist-p '((:a 1) (:b 2) (:c 3)))))
57 (ert-deftest test-json-plist-reverse ()
58 (should (equal (json--plist-reverse '()) '()))
59 (should (equal (json--plist-reverse '(:a 1)) '(:a 1)))
60 (should (equal (json--plist-reverse '(:a 1 :b 2 :c 3))
61 '(:c 3 :b 2 :a 1))))
63 (ert-deftest test-json-plist-to-alist ()
64 (should (equal (json--plist-to-alist '()) '()))
65 (should (equal (json--plist-to-alist '(:a 1)) '((:a . 1))))
66 (should (equal (json--plist-to-alist '(:a 1 :b 2 :c 3))
67 '((:a . 1) (:b . 2) (:c . 3)))))
69 (ert-deftest test-json-advance ()
70 (json-tests--with-temp-buffer "{ \"a\": 1 }"
71 (json-advance 0)
72 (should (= (point) (point-min)))
73 (json-advance 3)
74 (should (= (point) (+ (point-min) 3)))))
76 (ert-deftest test-json-peek ()
77 (json-tests--with-temp-buffer ""
78 (should (zerop (json-peek))))
79 (json-tests--with-temp-buffer "{ \"a\": 1 }"
80 (should (equal (json-peek) ?{))))
82 (ert-deftest test-json-pop ()
83 (json-tests--with-temp-buffer ""
84 (should-error (json-pop) :type 'json-end-of-file))
85 (json-tests--with-temp-buffer "{ \"a\": 1 }"
86 (should (equal (json-pop) ?{))
87 (should (= (point) (+ (point-min) 1)))))
89 (ert-deftest test-json-skip-whitespace ()
90 (json-tests--with-temp-buffer "\t\r\n\f\b { \"a\": 1 }"
91 (json-skip-whitespace)
92 (should (equal (char-after) ?\f)))
93 (json-tests--with-temp-buffer "\t\r\n\t { \"a\": 1 }"
94 (json-skip-whitespace)
95 (should (equal (char-after) ?{))))
97 ;;; Paths
99 (ert-deftest test-json-path-to-position-with-objects ()
100 (let* ((json-string "{\"foo\": {\"bar\": {\"baz\": \"value\"}}}")
101 (matched-path (json-path-to-position 32 json-string)))
102 (should (equal (plist-get matched-path :path) '("foo" "bar" "baz")))
103 (should (equal (plist-get matched-path :match-start) 25))
104 (should (equal (plist-get matched-path :match-end) 32))))
106 (ert-deftest test-json-path-to-position-with-arrays ()
107 (let* ((json-string "{\"foo\": [\"bar\", [\"baz\"]]}")
108 (matched-path (json-path-to-position 20 json-string)))
109 (should (equal (plist-get matched-path :path) '("foo" 1 0)))
110 (should (equal (plist-get matched-path :match-start) 18))
111 (should (equal (plist-get matched-path :match-end) 23))))
113 (ert-deftest test-json-path-to-position-no-match ()
114 (let* ((json-string "{\"foo\": {\"bar\": \"baz\"}}")
115 (matched-path (json-path-to-position 5 json-string)))
116 (should (null matched-path))))
118 ;;; Keywords
120 (ert-deftest test-json-read-keyword ()
121 (json-tests--with-temp-buffer "true"
122 (should (json-read-keyword "true")))
123 (json-tests--with-temp-buffer "true"
124 (should-error
125 (json-read-keyword "false") :type 'json-unknown-keyword))
126 (json-tests--with-temp-buffer "foo"
127 (should-error
128 (json-read-keyword "foo") :type 'json-unknown-keyword)))
130 (ert-deftest test-json-encode-keyword ()
131 (should (equal (json-encode-keyword t) "true"))
132 (should (equal (json-encode-keyword json-false) "false"))
133 (should (equal (json-encode-keyword json-null) "null")))
135 ;;; Numbers
137 (ert-deftest test-json-read-number ()
138 (json-tests--with-temp-buffer "3"
139 (should (= (json-read-number) 3)))
140 (json-tests--with-temp-buffer "-5"
141 (should (= (json-read-number) -5)))
142 (json-tests--with-temp-buffer "123.456"
143 (should (= (json-read-number) 123.456)))
144 (json-tests--with-temp-buffer "1e3"
145 (should (= (json-read-number) 1e3)))
146 (json-tests--with-temp-buffer "2e+3"
147 (should (= (json-read-number) 2e3)))
148 (json-tests--with-temp-buffer "3E3"
149 (should (= (json-read-number) 3e3)))
150 (json-tests--with-temp-buffer "1e-7"
151 (should (= (json-read-number) 1e-7)))
152 (json-tests--with-temp-buffer "abc"
153 (should-error (json-read-number) :type 'json-number-format)))
155 (ert-deftest test-json-encode-number ()
156 (should (equal (json-encode-number 3) "3"))
157 (should (equal (json-encode-number -5) "-5"))
158 (should (equal (json-encode-number 123.456) "123.456")))
160 ;; Strings
162 (ert-deftest test-json-read-escaped-char ()
163 (json-tests--with-temp-buffer "\\\""
164 (should (equal (json-read-escaped-char) ?\"))))
166 (ert-deftest test-json-read-string ()
167 (json-tests--with-temp-buffer "\"formfeed\f\""
168 (should-error (json-read-string) :type 'json-string-format))
169 (json-tests--with-temp-buffer "\"foo \\\"bar\\\"\""
170 (should (equal (json-read-string) "foo \"bar\"")))
171 (json-tests--with-temp-buffer "\"abcαβγ\""
172 (should (equal (json-read-string) "abcαβγ")))
173 (json-tests--with-temp-buffer "\"\\nasd\\u0444\\u044b\\u0432fgh\\t\""
174 (should (equal (json-read-string) "\nasdфывfgh\t")))
175 ;; Bug#24784
176 (json-tests--with-temp-buffer "\"\\uD834\\uDD1E\""
177 (should (equal (json-read-string) "\U0001D11E")))
178 (json-tests--with-temp-buffer "foo"
179 (should-error (json-read-string) :type 'json-string-format)))
181 (ert-deftest test-json-encode-string ()
182 (should (equal (json-encode-string "foo") "\"foo\""))
183 (should (equal (json-encode-string "a\n\fb") "\"a\\n\\fb\""))
184 (should (equal (json-encode-string "\nasdфыв\u001f\u007ffgh\t")
185 "\"\\nasdфыв\\u001f\u007ffgh\\t\"")))
187 (ert-deftest test-json-encode-key ()
188 (should (equal (json-encode-key "foo") "\"foo\""))
189 (should (equal (json-encode-key 'foo) "\"foo\""))
190 (should (equal (json-encode-key :foo) "\"foo\""))
191 (should-error (json-encode-key 5) :type 'json-key-format)
192 (should-error (json-encode-key ["foo"]) :type 'json-key-format)
193 (should-error (json-encode-key '("foo")) :type 'json-key-format))
195 ;;; Objects
197 (ert-deftest test-json-new-object ()
198 (let ((json-object-type 'alist))
199 (should (equal (json-new-object) '())))
200 (let ((json-object-type 'plist))
201 (should (equal (json-new-object) '())))
202 (let* ((json-object-type 'hash-table)
203 (json-object (json-new-object)))
204 (should (hash-table-p json-object))
205 (should (= (hash-table-count json-object) 0))))
207 (ert-deftest test-json-add-to-object ()
208 (let* ((json-object-type 'alist)
209 (json-key-type nil)
210 (obj (json-new-object)))
211 (setq obj (json-add-to-object obj "a" 1))
212 (setq obj (json-add-to-object obj "b" 2))
213 (should (equal (assq 'a obj) '(a . 1)))
214 (should (equal (assq 'b obj) '(b . 2))))
215 (let* ((json-object-type 'plist)
216 (json-key-type nil)
217 (obj (json-new-object)))
218 (setq obj (json-add-to-object obj "a" 1))
219 (setq obj (json-add-to-object obj "b" 2))
220 (should (= (plist-get obj :a) 1))
221 (should (= (plist-get obj :b) 2)))
222 (let* ((json-object-type 'hash-table)
223 (json-key-type nil)
224 (obj (json-new-object)))
225 (setq obj (json-add-to-object obj "a" 1))
226 (setq obj (json-add-to-object obj "b" 2))
227 (should (= (gethash "a" obj) 1))
228 (should (= (gethash "b" obj) 2))))
230 (ert-deftest test-json-read-object ()
231 (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }"
232 (let ((json-object-type 'alist))
233 (should (equal (json-read-object) '((a . 1) (b . 2))))))
234 (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }"
235 (let ((json-object-type 'plist))
236 (should (equal (json-read-object) '(:a 1 :b 2)))))
237 (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }"
238 (let* ((json-object-type 'hash-table)
239 (hash-table (json-read-object)))
240 (should (= (gethash "a" hash-table) 1))
241 (should (= (gethash "b" hash-table) 2))))
242 (json-tests--with-temp-buffer "{ \"a\": 1 \"b\": 2 }"
243 (should-error (json-read-object) :type 'json-object-format)))
245 (ert-deftest test-json-encode-hash-table ()
246 (let ((hash-table (make-hash-table))
247 (json-encoding-object-sort-predicate 'string<)
248 (json-encoding-pretty-print nil))
249 (puthash :a 1 hash-table)
250 (puthash :b 2 hash-table)
251 (puthash :c 3 hash-table)
252 (should (equal (json-encode hash-table)
253 "{\"a\":1,\"b\":2,\"c\":3}"))))
255 (ert-deftest json-encode-simple-alist ()
256 (let ((json-encoding-pretty-print nil))
257 (should (equal (json-encode '((a . 1) (b . 2)))
258 "{\"a\":1,\"b\":2}"))))
260 (ert-deftest test-json-encode-plist ()
261 (let ((plist '(:a 1 :b 2))
262 (json-encoding-pretty-print nil))
263 (should (equal (json-encode plist) "{\"a\":1,\"b\":2}"))))
265 (ert-deftest test-json-encode-plist-with-sort-predicate ()
266 (let ((plist '(:c 3 :a 1 :b 2))
267 (json-encoding-object-sort-predicate 'string<)
268 (json-encoding-pretty-print nil))
269 (should (equal (json-encode plist) "{\"a\":1,\"b\":2,\"c\":3}"))))
271 (ert-deftest test-json-encode-alist-with-sort-predicate ()
272 (let ((alist '((:c . 3) (:a . 1) (:b . 2)))
273 (json-encoding-object-sort-predicate 'string<)
274 (json-encoding-pretty-print nil))
275 (should (equal (json-encode alist) "{\"a\":1,\"b\":2,\"c\":3}"))))
277 (ert-deftest test-json-encode-list ()
278 (let ((json-encoding-pretty-print nil))
279 (should (equal (json-encode-list '(:a 1 :b 2))
280 "{\"a\":1,\"b\":2}"))
281 (should (equal (json-encode-list '((:a . 1) (:b . 2)))
282 "{\"a\":1,\"b\":2}"))
283 (should (equal (json-encode-list '(1 2 3 4)) "[1,2,3,4]"))))
285 ;;; Arrays
287 (ert-deftest test-json-read-array ()
288 (let ((json-array-type 'vector))
289 (json-tests--with-temp-buffer "[1, 2, \"a\", \"b\"]"
290 (should (equal (json-read-array) [1 2 "a" "b"]))))
291 (let ((json-array-type 'list))
292 (json-tests--with-temp-buffer "[1, 2, \"a\", \"b\"]"
293 (should (equal (json-read-array) '(1 2 "a" "b")))))
294 (json-tests--with-temp-buffer "[1 2]"
295 (should-error (json-read-array) :type 'json-error)))
297 (ert-deftest test-json-encode-array ()
298 (let ((json-encoding-pretty-print nil))
299 (should (equal (json-encode-array [1 2 "a" "b"])
300 "[1,2,\"a\",\"b\"]"))))
302 ;;; Reader
304 (ert-deftest test-json-read ()
305 (json-tests--with-temp-buffer "{ \"a\": 1 }"
306 ;; We don't care exactly what the return value is (that is tested
307 ;; in `test-json-read-object'), but it should parse without error.
308 (should (json-read)))
309 (json-tests--with-temp-buffer ""
310 (should-error (json-read) :type 'json-end-of-file))
311 (json-tests--with-temp-buffer "xxx"
312 (should-error (json-read) :type 'json-readtable-error)))
314 (ert-deftest test-json-read-from-string ()
315 (let ((json-string "{ \"a\": 1 }"))
316 (json-tests--with-temp-buffer json-string
317 (should (equal (json-read-from-string json-string)
318 (json-read))))))
320 ;;; JSON encoder
322 (ert-deftest test-json-encode ()
323 (should (equal (json-encode "foo") "\"foo\""))
324 (with-temp-buffer
325 (should-error (json-encode (current-buffer)) :type 'json-error)))
327 (provide 'json-tests)
328 ;;; json-tests.el ends here