]> code.delx.au - gnu-emacs/blob - test/lisp/json-tests.el
Rename all test files to reflect source layout.
[gnu-emacs] / test / lisp / 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 (ert-deftest test-json-plist-reverse ()
26 (should (equal (json--plist-reverse '()) '()))
27 (should (equal (json--plist-reverse '(:a 1)) '(:a 1)))
28 (should (equal (json--plist-reverse '(:a 1 :b 2 :c 3))
29 '(:c 3 :b 2 :a 1))))
30
31 (ert-deftest test-json-plist-to-alist ()
32 (should (equal (json--plist-to-alist '()) '()))
33 (should (equal (json--plist-to-alist '(:a 1)) '((:a . 1))))
34 (should (equal (json--plist-to-alist '(:a 1 :b 2 :c 3))
35 '((:a . 1) (:b . 2) (:c . 3)))))
36
37 (ert-deftest test-json-encode-plist ()
38 (let ((plist '(:a 1 :b 2)))
39 (should (equal (json-encode plist) "{\"a\":1,\"b\":2}"))))
40
41 (ert-deftest json-encode-simple-alist ()
42 (should (equal (json-encode '((a . 1)
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
60 (ert-deftest test-json-encode-plist-with-sort-predicate ()
61 (let ((plist '(:c 3 :a 1 :b 2))
62 (json-encoding-object-sort-predicate 'string<))
63 (should (equal (json-encode plist) "{\"a\":1,\"b\":2,\"c\":3}"))))
64
65 (ert-deftest json-read-simple-alist ()
66 (let ((json-object-type 'alist))
67 (should (equal (json-read-from-string "{\"a\": 1, \"b\": 2}")
68 '((a . 1)
69 (b . 2))))))
70
71 (ert-deftest json-encode-string-with-special-chars ()
72 (should (equal (json-encode-string "a\n\fb")
73 "\"a\\n\\fb\""))
74 (should (equal (json-encode-string "\nasdфыв\u001f\u007ffgh\t")
75 "\"\\nasdфыв\\u001f\u007ffgh\\t\"")))
76
77 (ert-deftest json-read-string-with-special-chars ()
78 (should (equal (json-read-from-string "\"\\nasd\\u0444\\u044b\\u0432fgh\\t\"")
79 "\nasdфывfgh\t")))
80
81 (ert-deftest test-json-path-to-position-with-objects ()
82 (let* ((json-string "{\"foo\": {\"bar\": {\"baz\": \"value\"}}}")
83 (matched-path (json-path-to-position 32 json-string)))
84 (should (equal (plist-get matched-path :path) '("foo" "bar" "baz")))
85 (should (equal (plist-get matched-path :match-start) 25))
86 (should (equal (plist-get matched-path :match-end) 32))))
87
88 (ert-deftest test-json-path-to-position-with-arrays ()
89 (let* ((json-string "{\"foo\": [\"bar\", [\"baz\"]]}")
90 (matched-path (json-path-to-position 20 json-string)))
91 (should (equal (plist-get matched-path :path) '("foo" 1 0)))
92 (should (equal (plist-get matched-path :match-start) 18))
93 (should (equal (plist-get matched-path :match-end) 23))))
94
95 (ert-deftest test-json-path-to-position-no-match ()
96 (let* ((json-string "{\"foo\": {\"bar\": \"baz\"}}")
97 (matched-path (json-path-to-position 5 json-string)))
98 (should (null matched-path))))
99
100 (provide 'json-tests)
101 ;;; json-tests.el ends here