1 ;;; json-tests.el --- Test suite for json.el
3 ;; Copyright (C) 2015-2016 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")))
170 (json-tests--with-temp-buffer "foo"
171 (should-error (json-read-string) :type
'json-string-format
)))
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\"")))
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
))
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))))
199 (ert-deftest test-json-add-to-object
()
200 (let* ((json-object-type 'alist
)
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
)
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
)
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))))
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
)))
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}"))))
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}"))))
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}"))))
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}"))))
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}"))))
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]"))))
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
)))
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\"]"))))
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
)))
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
)
314 (ert-deftest test-json-encode
()
315 (should (equal (json-encode "foo") "\"foo\""))
317 (should-error (json-encode (current-buffer)) :type
'json-error
)))
319 (provide 'json-tests
)
320 ;;; json-tests.el ends here