aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDamien Cassou2018-05-19 08:36:32 +0200
committerNicolas Petton2018-06-14 11:01:49 +0200
commit8cb9beb32163fa3ce3b052ced646fd673814ddc6 (patch)
tree57f2140a14a50c59d7b1caa875351e66448f4336 /test
parent967d2c55ef3908fd378e05b2a0070663ae45f6de (diff)
downloademacs-8cb9beb32163fa3ce3b052ced646fd673814ddc6.tar.gz
emacs-8cb9beb32163fa3ce3b052ced646fd673814ddc6.zip
Fix pretty-printing empty objects as null
* lisp/json.el (json-pretty-print): Force distinction between empty objects and null. (json-encode-list): Remove responsibility to print "null" as this value is not a list. (json-encode): Give higher precedence to lists so that an empty list is printed as an empty object, not as "null". * test/lisp/json-tests.el (test-json-encode): Add many tests to check the behavior of pretty-printing.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/json-tests.el67
1 files changed, 67 insertions, 0 deletions
diff --git a/test/lisp/json-tests.el b/test/lisp/json-tests.el
index ea562e8b134..84039c09cee 100644
--- a/test/lisp/json-tests.el
+++ b/test/lisp/json-tests.el
@@ -325,5 +325,72 @@ Point is moved to beginning of the buffer."
325 (with-temp-buffer 325 (with-temp-buffer
326 (should-error (json-encode (current-buffer)) :type 'json-error))) 326 (should-error (json-encode (current-buffer)) :type 'json-error)))
327 327
328;;; Pretty-print
329
330(defun json-tests-equal-pretty-print (original &optional expected)
331 "Abort current test if pretty-printing ORIGINAL does not yield EXPECTED.
332
333Both ORIGINAL and EXPECTED should be strings. If EXPECTED is
334nil, ORIGINAL should stay unchanged by pretty-printing."
335 (with-temp-buffer
336 (insert original)
337 (json-pretty-print-buffer)
338 (should (equal (buffer-string) (or expected original)))))
339
340(ert-deftest test-json-pretty-print-string ()
341 (json-tests-equal-pretty-print "\"\"")
342 (json-tests-equal-pretty-print "\"foo\""))
343
344(ert-deftest test-json-pretty-print-atom ()
345 (json-tests-equal-pretty-print "true")
346 (json-tests-equal-pretty-print "false")
347 (json-tests-equal-pretty-print "null"))
348
349(ert-deftest test-json-pretty-print-number ()
350 (json-tests-equal-pretty-print "123")
351 (json-tests-equal-pretty-print "0.123"))
352
353(ert-deftest test-json-pretty-print-object ()
354 ;; empty (regression test for bug#24252)
355 (json-tests-equal-pretty-print
356 "{}"
357 "{\n}")
358 ;; one pair
359 (json-tests-equal-pretty-print
360 "{\"key\":1}"
361 "{\n \"key\": 1\n}")
362 ;; two pairs
363 (json-tests-equal-pretty-print
364 "{\"key1\":1,\"key2\":2}"
365 "{\n \"key1\": 1,\n \"key2\": 2\n}")
366 ;; embedded object
367 (json-tests-equal-pretty-print
368 "{\"foo\":{\"key\":1}}"
369 "{\n \"foo\": {\n \"key\": 1\n }\n}")
370 ;; embedded array
371 (json-tests-equal-pretty-print
372 "{\"key\":[1,2]}"
373 "{\n \"key\": [\n 1,\n 2\n ]\n}"))
374
375(ert-deftest test-json-pretty-print-array ()
376 ;; empty
377 (json-tests-equal-pretty-print "[]")
378 ;; one item
379 (json-tests-equal-pretty-print
380 "[1]"
381 "[\n 1\n]")
382 ;; two items
383 (json-tests-equal-pretty-print
384 "[1,2]"
385 "[\n 1,\n 2\n]")
386 ;; embedded object
387 (json-tests-equal-pretty-print
388 "[{\"key\":1}]"
389 "[\n {\n \"key\": 1\n }\n]")
390 ;; embedded array
391 (json-tests-equal-pretty-print
392 "[[1,2]]"
393 "[\n [\n 1,\n 2\n ]\n]"))
394
328(provide 'json-tests) 395(provide 'json-tests)
329;;; json-tests.el ends here 396;;; json-tests.el ends here