]> code.delx.au - gnu-emacs/blob - test/automated/json-tests.el
bb043dc4e054f5c61e643882d8b69623ec1e9e09
[gnu-emacs] / test / automated / json-tests.el
1 ;;; json-tests.el --- Test suite for json.el
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
6
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.
11
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.
16
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/>.
19
20 ;;; Code:
21
22 (require 'ert)
23 (require 'json)
24
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))
33
34 ;;; Utilities
35
36 (ert-deftest test-json-join ()
37 (should (equal (json-join '() ", ") ""))
38 (should (equal (json-join '("a" "b" "c") ", ") "a, b, c")))
39
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))))
48
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)))))
56
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))))
62
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)))))
68
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)))))
75
76 (ert-deftest test-json-peek ()
77 (json-tests--with-temp-buffer ""
78 (should (eq (json-peek) :json-eof)))
79 (json-tests--with-temp-buffer "{ \"a\": 1 }"
80 (should (equal (json-peek) ?{))))
81
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)))))
88
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 (point)) ?{))))
93
94 ;;; Paths
95
96 (ert-deftest test-json-path-to-position-with-objects ()
97 (let* ((json-string "{\"foo\": {\"bar\": {\"baz\": \"value\"}}}")
98 (matched-path (json-path-to-position 32 json-string)))
99 (should (equal (plist-get matched-path :path) '("foo" "bar" "baz")))
100 (should (equal (plist-get matched-path :match-start) 25))
101 (should (equal (plist-get matched-path :match-end) 32))))
102
103 (ert-deftest test-json-path-to-position-with-arrays ()
104 (let* ((json-string "{\"foo\": [\"bar\", [\"baz\"]]}")
105 (matched-path (json-path-to-position 20 json-string)))
106 (should (equal (plist-get matched-path :path) '("foo" 1 0)))
107 (should (equal (plist-get matched-path :match-start) 18))
108 (should (equal (plist-get matched-path :match-end) 23))))
109
110 (ert-deftest test-json-path-to-position-no-match ()
111 (let* ((json-string "{\"foo\": {\"bar\": \"baz\"}}")
112 (matched-path (json-path-to-position 5 json-string)))
113 (should (null matched-path))))
114
115 ;;; Keywords
116
117 (ert-deftest test-json-read-keyword ()
118 (json-tests--with-temp-buffer "true"
119 (should (json-read-keyword "true")))
120 (json-tests--with-temp-buffer "true"
121 (should-error
122 (json-read-keyword "false") :type 'json-unknown-keyword))
123 (json-tests--with-temp-buffer "foo"
124 (should-error
125 (json-read-keyword "foo") :type 'json-unknown-keyword)))
126
127 (ert-deftest test-json-encode-keyword ()
128 (should (equal (json-encode-keyword t) "true"))
129 (should (equal (json-encode-keyword json-false) "false"))
130 (should (equal (json-encode-keyword json-null) "null")))
131
132 ;;; Numbers
133
134 (ert-deftest test-json-read-number ()
135 (json-tests--with-temp-buffer "3"
136 (should (= (json-read-number) 3)))
137 (json-tests--with-temp-buffer "-5"
138 (should (= (json-read-number) -5)))
139 (json-tests--with-temp-buffer "123.456"
140 (should (= (json-read-number) 123.456)))
141 (json-tests--with-temp-buffer "1e3"
142 (should (= (json-read-number) 1e3)))
143 (json-tests--with-temp-buffer "2e+3"
144 (should (= (json-read-number) 2e3)))
145 (json-tests--with-temp-buffer "3E3"
146 (should (= (json-read-number) 3e3)))
147 (json-tests--with-temp-buffer "1e-7"
148 (should (= (json-read-number) 1e-7)))
149 (json-tests--with-temp-buffer "abc"
150 (should-error (json-read-number) :type 'json-number-format)))
151
152 (ert-deftest test-json-encode-number ()
153 (should (equal (json-encode-number 3) "3"))
154 (should (equal (json-encode-number -5) "-5"))
155 (should (equal (json-encode-number 123.456) "123.456")))
156
157 ;; Strings
158
159 (ert-deftest test-json-read-escaped-char ()
160 (json-tests--with-temp-buffer "\\\""
161 (should (equal (json-read-escaped-char) ?\"))))
162
163 (ert-deftest test-json-read-string ()
164 (json-tests--with-temp-buffer "\"foo \\\"bar\\\"\""
165 (should (equal (json-read-string) "foo \"bar\"")))
166 (json-tests--with-temp-buffer "\"abcαβγ\""
167 (should (equal (json-read-string) "abcαβγ")))
168 (json-tests--with-temp-buffer "\"\\nasd\\u0444\\u044b\\u0432fgh\\t\""
169 (should (equal (json-read-string) "\nasdфывfgh\t")))
170 (json-tests--with-temp-buffer "foo"
171 (should-error (json-read-string) :type 'json-string-format)))
172
173 (ert-deftest test-json-encode-string ()
174 (should (equal (json-encode-string "foo") "\"foo\""))
175 (should (equal (json-encode-string "a\n\fb") "\"a\\n\\fb\""))
176 (should (equal (json-encode-string "\nasdфыв\u001f\u007ffgh\t")
177 "\"\\nasdфыв\\u001f\u007ffgh\\t\"")))
178
179 (ert-deftest test-json-encode-key ()
180 (should (equal (json-encode-key "foo") "\"foo\""))
181 (should (equal (json-encode-key 'foo) "\"foo\""))
182 (should (equal (json-encode-key :foo) "\"foo\""))
183 (should-error (json-encode-key 5) :type 'json-key-format)
184 (should-error (json-encode-key ["foo"]) :type 'json-key-format)
185 (should-error (json-encode-key '("foo")) :type 'json-key-format))
186
187 ;;; Objects
188
189 (ert-deftest test-json-new-object ()
190 (let ((json-object-type 'alist))
191 (should (equal (json-new-object) '())))
192 (let ((json-object-type 'plist))
193 (should (equal (json-new-object) '())))
194 (let* ((json-object-type 'hash-table)
195 (json-object (json-new-object)))
196 (should (hash-table-p json-object))
197 (should (= (hash-table-count json-object) 0))))
198
199 (ert-deftest test-json-add-to-object ()
200 (let* ((json-object-type 'alist)
201 (json-key-type nil)
202 (obj (json-new-object)))
203 (setq obj (json-add-to-object obj "a" 1))
204 (setq obj (json-add-to-object obj "b" 2))
205 (should (equal (assq 'a obj) '(a . 1)))
206 (should (equal (assq 'b obj) '(b . 2))))
207 (let* ((json-object-type 'plist)
208 (json-key-type nil)
209 (obj (json-new-object)))
210 (setq obj (json-add-to-object obj "a" 1))
211 (setq obj (json-add-to-object obj "b" 2))
212 (should (= (plist-get obj :a) 1))
213 (should (= (plist-get obj :b) 2)))
214 (let* ((json-object-type 'hash-table)
215 (json-key-type nil)
216 (obj (json-new-object)))
217 (setq obj (json-add-to-object obj "a" 1))
218 (setq obj (json-add-to-object obj "b" 2))
219 (should (= (gethash "a" obj) 1))
220 (should (= (gethash "b" obj) 2))))
221
222 (ert-deftest test-json-read-object ()
223 (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }"
224 (let ((json-object-type 'alist))
225 (should (equal (json-read-object) '((a . 1) (b . 2))))))
226 (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }"
227 (let ((json-object-type 'plist))
228 (should (equal (json-read-object) '(:a 1 :b 2)))))
229 (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }"
230 (let* ((json-object-type 'hash-table)
231 (hash-table (json-read-object)))
232 (should (= (gethash "a" hash-table) 1))
233 (should (= (gethash "b" hash-table) 2))))
234 (json-tests--with-temp-buffer "{ \"a\": 1 \"b\": 2 }"
235 (should-error (json-read-object) :type 'json-object-format)))
236
237 (ert-deftest test-json-encode-hash-table ()
238 (let ((hash-table (make-hash-table))
239 (json-encoding-object-sort-predicate 'string<)
240 (json-encoding-pretty-print nil))
241 (puthash :a 1 hash-table)
242 (puthash :b 2 hash-table)
243 (puthash :c 3 hash-table)
244 (should (equal (json-encode hash-table)
245 "{\"a\":1,\"b\":2,\"c\":3}"))))
246
247 (ert-deftest json-encode-simple-alist ()
248 (let ((json-encoding-pretty-print nil))
249 (should (equal (json-encode '((a . 1) (b . 2)))
250 "{\"a\":1,\"b\":2}"))))
251
252 (ert-deftest test-json-encode-plist ()
253 (let ((plist '(:a 1 :b 2))
254 (json-encoding-pretty-print nil))
255 (should (equal (json-encode plist) "{\"a\":1,\"b\":2}"))))
256
257 (ert-deftest test-json-encode-plist-with-sort-predicate ()
258 (let ((plist '(:c 3 :a 1 :b 2))
259 (json-encoding-object-sort-predicate 'string<)
260 (json-encoding-pretty-print nil))
261 (should (equal (json-encode plist) "{\"a\":1,\"b\":2,\"c\":3}"))))
262
263 (ert-deftest test-json-encode-alist-with-sort-predicate ()
264 (let ((alist '((:c . 3) (:a . 1) (:b . 2)))
265 (json-encoding-object-sort-predicate 'string<)
266 (json-encoding-pretty-print nil))
267 (should (equal (json-encode alist) "{\"a\":1,\"b\":2,\"c\":3}"))))
268
269 (ert-deftest test-json-encode-list ()
270 (let ((json-encoding-pretty-print nil))
271 (should (equal (json-encode-list '(:a 1 :b 2))
272 "{\"a\":1,\"b\":2}"))
273 (should (equal (json-encode-list '((:a . 1) (:b . 2)))
274 "{\"a\":1,\"b\":2}"))
275 (should (equal (json-encode-list '(1 2 3 4)) "[1,2,3,4]"))))
276
277 ;;; Arrays
278
279 (ert-deftest test-json-read-array ()
280 (let ((json-array-type 'vector))
281 (json-tests--with-temp-buffer "[1, 2, \"a\", \"b\"]"
282 (should (equal (json-read-array) [1 2 "a" "b"]))))
283 (let ((json-array-type 'list))
284 (json-tests--with-temp-buffer "[1, 2, \"a\", \"b\"]"
285 (should (equal (json-read-array) '(1 2 "a" "b")))))
286 (json-tests--with-temp-buffer "[1 2]"
287 (should-error (json-read-array) :type 'json-error)))
288
289 (ert-deftest test-json-encode-array ()
290 (let ((json-encoding-pretty-print nil))
291 (should (equal (json-encode-array [1 2 "a" "b"])
292 "[1,2,\"a\",\"b\"]"))))
293
294 ;;; Reader
295
296 (ert-deftest test-json-read ()
297 (json-tests--with-temp-buffer "{ \"a\": 1 }"
298 ;; We don't care exactly what the return value is (that is tested
299 ;; in `test-json-read-object'), but it should parse without error.
300 (should (json-read)))
301 (json-tests--with-temp-buffer ""
302 (should-error (json-read) :type 'json-end-of-file))
303 (json-tests--with-temp-buffer "xxx"
304 (should-error (json-read) :type 'json-readtable-error)))
305
306 (ert-deftest test-json-read-from-string ()
307 (let ((json-string "{ \"a\": 1 }"))
308 (json-tests--with-temp-buffer json-string
309 (should (equal (json-read-from-string json-string)
310 (json-read))))))
311
312 ;;; JSON encoder
313
314 (ert-deftest test-json-encode ()
315 (should (equal (json-encode "foo") "\"foo\""))
316 (with-temp-buffer
317 (should-error (json-encode (current-buffer)) :type 'json-error)))
318
319 (provide 'json-tests)
320 ;;; json-tests.el ends here