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/>.
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."
31 (goto-char (point-min))
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))
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 }"
72 (should (= (point) (point-min)))
74 (should (= (point) (+ (point-min) 3)))))
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) ?
{))))
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 (point)) ?
{))))
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))))
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))))
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
))))
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"
122 (json-read-keyword "false") :type
'json-unknown-keyword
))
123 (json-tests--with-temp-buffer "foo"
125 (json-read-keyword "foo") :type
'json-unknown-keyword
)))
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")))
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
)))
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")))
159 (ert-deftest test-json-read-escaped-char
()
160 (json-tests--with-temp-buffer "\\\""
161 (should (equal (json-read-escaped-char) ?
\"))))
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")))
171 (json-tests--with-temp-buffer "\"\\uD834\\uDD1E\""
172 (should (equal (json-read-string) "\U0001D11E")))
173 (json-tests--with-temp-buffer "foo"
174 (should-error (json-read-string) :type
'json-string-format
)))
176 (ert-deftest test-json-encode-string
()
177 (should (equal (json-encode-string "foo") "\"foo\""))
178 (should (equal (json-encode-string "a\n\fb") "\"a\\n\\fb\""))
179 (should (equal (json-encode-string "\nasdфыв\u001f\u007ffgh\t")
180 "\"\\nasdфыв\\u001f\u007ffgh\\t\"")))
182 (ert-deftest test-json-encode-key
()
183 (should (equal (json-encode-key "foo") "\"foo\""))
184 (should (equal (json-encode-key 'foo
) "\"foo\""))
185 (should (equal (json-encode-key :foo
) "\"foo\""))
186 (should-error (json-encode-key 5) :type
'json-key-format
)
187 (should-error (json-encode-key ["foo"]) :type
'json-key-format
)
188 (should-error (json-encode-key '("foo")) :type
'json-key-format
))
192 (ert-deftest test-json-new-object
()
193 (let ((json-object-type 'alist
))
194 (should (equal (json-new-object) '())))
195 (let ((json-object-type 'plist
))
196 (should (equal (json-new-object) '())))
197 (let* ((json-object-type 'hash-table
)
198 (json-object (json-new-object)))
199 (should (hash-table-p json-object
))
200 (should (= (hash-table-count json-object
) 0))))
202 (ert-deftest test-json-add-to-object
()
203 (let* ((json-object-type 'alist
)
205 (obj (json-new-object)))
206 (setq obj
(json-add-to-object obj
"a" 1))
207 (setq obj
(json-add-to-object obj
"b" 2))
208 (should (equal (assq 'a obj
) '(a .
1)))
209 (should (equal (assq 'b obj
) '(b .
2))))
210 (let* ((json-object-type 'plist
)
212 (obj (json-new-object)))
213 (setq obj
(json-add-to-object obj
"a" 1))
214 (setq obj
(json-add-to-object obj
"b" 2))
215 (should (= (plist-get obj
:a
) 1))
216 (should (= (plist-get obj
:b
) 2)))
217 (let* ((json-object-type 'hash-table
)
219 (obj (json-new-object)))
220 (setq obj
(json-add-to-object obj
"a" 1))
221 (setq obj
(json-add-to-object obj
"b" 2))
222 (should (= (gethash "a" obj
) 1))
223 (should (= (gethash "b" obj
) 2))))
225 (ert-deftest test-json-read-object
()
226 (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }"
227 (let ((json-object-type 'alist
))
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 'plist
))
231 (should (equal (json-read-object) '(:a
1 :b
2)))))
232 (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }"
233 (let* ((json-object-type 'hash-table
)
234 (hash-table (json-read-object)))
235 (should (= (gethash "a" hash-table
) 1))
236 (should (= (gethash "b" hash-table
) 2))))
237 (json-tests--with-temp-buffer "{ \"a\": 1 \"b\": 2 }"
238 (should-error (json-read-object) :type
'json-object-format
)))
240 (ert-deftest test-json-encode-hash-table
()
241 (let ((hash-table (make-hash-table))
242 (json-encoding-object-sort-predicate 'string
<)
243 (json-encoding-pretty-print nil
))
244 (puthash :a
1 hash-table
)
245 (puthash :b
2 hash-table
)
246 (puthash :c
3 hash-table
)
247 (should (equal (json-encode hash-table
)
248 "{\"a\":1,\"b\":2,\"c\":3}"))))
250 (ert-deftest json-encode-simple-alist
()
251 (let ((json-encoding-pretty-print nil
))
252 (should (equal (json-encode '((a .
1) (b .
2)))
253 "{\"a\":1,\"b\":2}"))))
255 (ert-deftest test-json-encode-plist
()
256 (let ((plist '(:a
1 :b
2))
257 (json-encoding-pretty-print nil
))
258 (should (equal (json-encode plist
) "{\"a\":1,\"b\":2}"))))
260 (ert-deftest test-json-encode-plist-with-sort-predicate
()
261 (let ((plist '(:c
3 :a
1 :b
2))
262 (json-encoding-object-sort-predicate 'string
<)
263 (json-encoding-pretty-print nil
))
264 (should (equal (json-encode plist
) "{\"a\":1,\"b\":2,\"c\":3}"))))
266 (ert-deftest test-json-encode-alist-with-sort-predicate
()
267 (let ((alist '((:c .
3) (:a .
1) (:b .
2)))
268 (json-encoding-object-sort-predicate 'string
<)
269 (json-encoding-pretty-print nil
))
270 (should (equal (json-encode alist
) "{\"a\":1,\"b\":2,\"c\":3}"))))
272 (ert-deftest test-json-encode-list
()
273 (let ((json-encoding-pretty-print nil
))
274 (should (equal (json-encode-list '(:a
1 :b
2))
275 "{\"a\":1,\"b\":2}"))
276 (should (equal (json-encode-list '((:a .
1) (:b .
2)))
277 "{\"a\":1,\"b\":2}"))
278 (should (equal (json-encode-list '(1 2 3 4)) "[1,2,3,4]"))))
282 (ert-deftest test-json-read-array
()
283 (let ((json-array-type 'vector
))
284 (json-tests--with-temp-buffer "[1, 2, \"a\", \"b\"]"
285 (should (equal (json-read-array) [1 2 "a" "b"]))))
286 (let ((json-array-type 'list
))
287 (json-tests--with-temp-buffer "[1, 2, \"a\", \"b\"]"
288 (should (equal (json-read-array) '(1 2 "a" "b")))))
289 (json-tests--with-temp-buffer "[1 2]"
290 (should-error (json-read-array) :type
'json-error
)))
292 (ert-deftest test-json-encode-array
()
293 (let ((json-encoding-pretty-print nil
))
294 (should (equal (json-encode-array [1 2 "a" "b"])
295 "[1,2,\"a\",\"b\"]"))))
299 (ert-deftest test-json-read
()
300 (json-tests--with-temp-buffer "{ \"a\": 1 }"
301 ;; We don't care exactly what the return value is (that is tested
302 ;; in `test-json-read-object'), but it should parse without error.
303 (should (json-read)))
304 (json-tests--with-temp-buffer ""
305 (should-error (json-read) :type
'json-end-of-file
))
306 (json-tests--with-temp-buffer "xxx"
307 (should-error (json-read) :type
'json-readtable-error
)))
309 (ert-deftest test-json-read-from-string
()
310 (let ((json-string "{ \"a\": 1 }"))
311 (json-tests--with-temp-buffer json-string
312 (should (equal (json-read-from-string json-string
)
317 (ert-deftest test-json-encode
()
318 (should (equal (json-encode "foo") "\"foo\""))
320 (should-error (json-encode (current-buffer)) :type
'json-error
)))
322 (provide 'json-tests
)
323 ;;; json-tests.el ends here