aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorSimen Heggestøyl2015-11-24 23:13:30 +0100
committerSimen Heggestøyl2015-11-24 23:13:30 +0100
commit0805069ae5709dc1b97f7349c4f9a7651f3078e3 (patch)
tree7c47b46457bc54b3e75a8a090ab104efc92fdac2 /test
parent9f94c709b1bb29e275fe6546d22c58a78072672f (diff)
downloademacs-0805069ae5709dc1b97f7349c4f9a7651f3078e3.tar.gz
emacs-0805069ae5709dc1b97f7349c4f9a7651f3078e3.zip
Extend the test suite for json.el
* lisp/json.el (json-plist-p): Clarify docstring. * test/automated/json-tests.el (json-tests--with-temp-buffer): New macro. (test-json-join, test-json-alist-p) (test-json-plist-p, test-json-advance, test-json-peek) (test-json-pop, test-json-skip-whitespace) (test-json-read-keyword, test-json-encode-keyword) (test-json-read-number, test-json-encode-number) (test-json-read-escaped-char, test-json-read-string) (test-json-encode-string, test-json-encode-key) (test-json-new-object, test-json-add-to-object) (test-json-read-object, test-json-encode-list) (test-json-read-array, test-json-encode-array) (test-json-read, test-json-read-from-string) (test-json-encode): New tests. (json-read-simple-alist): Merged into `test-json-read-object'. (json-encode-string-with-special-chars): Merged into `test-json-encode-string'. (json-read-string-with-special-chars): Split into `test-json-encode-string' and `test-json-read-from-string'.
Diffstat (limited to 'test')
-rw-r--r--test/automated/json-tests.el297
1 files changed, 258 insertions, 39 deletions
diff --git a/test/automated/json-tests.el b/test/automated/json-tests.el
index 8f0cd6f0857..bb043dc4e05 100644
--- a/test/automated/json-tests.el
+++ b/test/automated/json-tests.el
@@ -22,6 +22,38 @@
22(require 'ert) 22(require 'ert)
23(require 'json) 23(require 'json)
24 24
25(defmacro json-tests--with-temp-buffer (content &rest body)
26 "Create a temporary buffer with CONTENT and evaluate BODY there.
27Point 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
25(ert-deftest test-json-plist-reverse () 57(ert-deftest test-json-plist-reverse ()
26 (should (equal (json--plist-reverse '()) '())) 58 (should (equal (json--plist-reverse '()) '()))
27 (should (equal (json--plist-reverse '(:a 1)) '(:a 1))) 59 (should (equal (json--plist-reverse '(:a 1)) '(:a 1)))
@@ -34,49 +66,32 @@
34 (should (equal (json--plist-to-alist '(:a 1 :b 2 :c 3)) 66 (should (equal (json--plist-to-alist '(:a 1 :b 2 :c 3))
35 '((:a . 1) (:b . 2) (:c . 3))))) 67 '((:a . 1) (:b . 2) (:c . 3)))))
36 68
37(ert-deftest test-json-encode-plist () 69(ert-deftest test-json-advance ()
38 (let ((plist '(:a 1 :b 2))) 70 (json-tests--with-temp-buffer "{ \"a\": 1 }"
39 (should (equal (json-encode plist) "{\"a\":1,\"b\":2}")))) 71 (json-advance 0)
40 72 (should (= (point) (point-min)))
41(ert-deftest json-encode-simple-alist () 73 (json-advance 3)
42 (should (equal (json-encode '((a . 1) 74 (should (= (point) (+ (point-min) 3)))))
43 (b . 2)))
44 "{\"a\":1,\"b\":2}")))
45
46(ert-deftest test-json-encode-hash-table ()
47 (let ((hash-table (make-hash-table))
48 (json-encoding-object-sort-predicate 'string<))
49 (puthash :a 1 hash-table)
50 (puthash :b 2 hash-table)
51 (puthash :c 3 hash-table)
52 (should (equal (json-encode hash-table)
53 "{\"a\":1,\"b\":2,\"c\":3}"))))
54
55(ert-deftest test-json-encode-alist-with-sort-predicate ()
56 (let ((alist '((:c . 3) (:a . 1) (:b . 2)))
57 (json-encoding-object-sort-predicate 'string<))
58 (should (equal (json-encode alist) "{\"a\":1,\"b\":2,\"c\":3}"))))
59 75
60(ert-deftest test-json-encode-plist-with-sort-predicate () 76(ert-deftest test-json-peek ()
61 (let ((plist '(:c 3 :a 1 :b 2)) 77 (json-tests--with-temp-buffer ""
62 (json-encoding-object-sort-predicate 'string<)) 78 (should (eq (json-peek) :json-eof)))
63 (should (equal (json-encode plist) "{\"a\":1,\"b\":2,\"c\":3}")))) 79 (json-tests--with-temp-buffer "{ \"a\": 1 }"
80 (should (equal (json-peek) ?{))))
64 81
65(ert-deftest json-read-simple-alist () 82(ert-deftest test-json-pop ()
66 (let ((json-object-type 'alist)) 83 (json-tests--with-temp-buffer ""
67 (should (equal (json-read-from-string "{\"a\": 1, \"b\": 2}") 84 (should-error (json-pop) :type 'json-end-of-file))
68 '((a . 1) 85 (json-tests--with-temp-buffer "{ \"a\": 1 }"
69 (b . 2)))))) 86 (should (equal (json-pop) ?{))
87 (should (= (point) (+ (point-min) 1)))))
70 88
71(ert-deftest json-encode-string-with-special-chars () 89(ert-deftest test-json-skip-whitespace ()
72 (should (equal (json-encode-string "a\n\fb") 90 (json-tests--with-temp-buffer "\t\r\n\f\b { \"a\": 1 }"
73 "\"a\\n\\fb\"")) 91 (json-skip-whitespace)
74 (should (equal (json-encode-string "\nasdфыв\u001f\u007ffgh\t") 92 (should (equal (char-after (point)) ?{))))
75 "\"\\nasdфыв\\u001f\u007ffgh\\t\"")))
76 93
77(ert-deftest json-read-string-with-special-chars () 94;;; Paths
78 (should (equal (json-read-from-string "\"\\nasd\\u0444\\u044b\\u0432fgh\\t\"")
79 "\nasdфывfgh\t")))
80 95
81(ert-deftest test-json-path-to-position-with-objects () 96(ert-deftest test-json-path-to-position-with-objects ()
82 (let* ((json-string "{\"foo\": {\"bar\": {\"baz\": \"value\"}}}") 97 (let* ((json-string "{\"foo\": {\"bar\": {\"baz\": \"value\"}}}")
@@ -97,5 +112,209 @@
97 (matched-path (json-path-to-position 5 json-string))) 112 (matched-path (json-path-to-position 5 json-string)))
98 (should (null matched-path)))) 113 (should (null matched-path))))
99 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
100(provide 'json-tests) 319(provide 'json-tests)
101;;; json-tests.el ends here 320;;; json-tests.el ends here