json-tests.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. ;;; json-tests.el --- Test suite for json.el
  2. ;; Copyright (C) 2015-2017 Free Software Foundation, Inc.
  3. ;; Author: Dmitry Gutov <dgutov@yandex.ru>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (require 'ert)
  16. (require 'json)
  17. (defmacro json-tests--with-temp-buffer (content &rest body)
  18. "Create a temporary buffer with CONTENT and evaluate BODY there.
  19. Point is moved to beginning of the buffer."
  20. (declare (indent 1))
  21. `(with-temp-buffer
  22. (insert ,content)
  23. (goto-char (point-min))
  24. ,@body))
  25. ;;; Utilities
  26. (ert-deftest test-json-join ()
  27. (should (equal (json-join '() ", ") ""))
  28. (should (equal (json-join '("a" "b" "c") ", ") "a, b, c")))
  29. (ert-deftest test-json-alist-p ()
  30. (should (json-alist-p '()))
  31. (should (json-alist-p '((a 1) (b 2) (c 3))))
  32. (should (json-alist-p '((:a 1) (:b 2) (:c 3))))
  33. (should (json-alist-p '(("a" 1) ("b" 2) ("c" 3))))
  34. (should-not (json-alist-p '(:a :b :c)))
  35. (should-not (json-alist-p '(:a 1 :b 2 :c 3)))
  36. (should-not (json-alist-p '((:a 1) (:b 2) 3))))
  37. (ert-deftest test-json-plist-p ()
  38. (should (json-plist-p '()))
  39. (should (json-plist-p '(:a 1 :b 2 :c 3)))
  40. (should-not (json-plist-p '(a 1 b 2 c 3)))
  41. (should-not (json-plist-p '("a" 1 "b" 2 "c" 3)))
  42. (should-not (json-plist-p '(:a :b :c)))
  43. (should-not (json-plist-p '((:a 1) (:b 2) (:c 3)))))
  44. (ert-deftest test-json-plist-reverse ()
  45. (should (equal (json--plist-reverse '()) '()))
  46. (should (equal (json--plist-reverse '(:a 1)) '(:a 1)))
  47. (should (equal (json--plist-reverse '(:a 1 :b 2 :c 3))
  48. '(:c 3 :b 2 :a 1))))
  49. (ert-deftest test-json-plist-to-alist ()
  50. (should (equal (json--plist-to-alist '()) '()))
  51. (should (equal (json--plist-to-alist '(:a 1)) '((:a . 1))))
  52. (should (equal (json--plist-to-alist '(:a 1 :b 2 :c 3))
  53. '((:a . 1) (:b . 2) (:c . 3)))))
  54. (ert-deftest test-json-advance ()
  55. (json-tests--with-temp-buffer "{ \"a\": 1 }"
  56. (json-advance 0)
  57. (should (= (point) (point-min)))
  58. (json-advance 3)
  59. (should (= (point) (+ (point-min) 3)))))
  60. (ert-deftest test-json-peek ()
  61. (json-tests--with-temp-buffer ""
  62. (should (eq (json-peek) :json-eof)))
  63. (json-tests--with-temp-buffer "{ \"a\": 1 }"
  64. (should (equal (json-peek) ?{))))
  65. (ert-deftest test-json-pop ()
  66. (json-tests--with-temp-buffer ""
  67. (should-error (json-pop) :type 'json-end-of-file))
  68. (json-tests--with-temp-buffer "{ \"a\": 1 }"
  69. (should (equal (json-pop) ?{))
  70. (should (= (point) (+ (point-min) 1)))))
  71. (ert-deftest test-json-skip-whitespace ()
  72. (json-tests--with-temp-buffer "\t\r\n\f\b { \"a\": 1 }"
  73. (json-skip-whitespace)
  74. (should (equal (char-after (point)) ?{))))
  75. ;;; Paths
  76. (ert-deftest test-json-path-to-position-with-objects ()
  77. (let* ((json-string "{\"foo\": {\"bar\": {\"baz\": \"value\"}}}")
  78. (matched-path (json-path-to-position 32 json-string)))
  79. (should (equal (plist-get matched-path :path) '("foo" "bar" "baz")))
  80. (should (equal (plist-get matched-path :match-start) 25))
  81. (should (equal (plist-get matched-path :match-end) 32))))
  82. (ert-deftest test-json-path-to-position-with-arrays ()
  83. (let* ((json-string "{\"foo\": [\"bar\", [\"baz\"]]}")
  84. (matched-path (json-path-to-position 20 json-string)))
  85. (should (equal (plist-get matched-path :path) '("foo" 1 0)))
  86. (should (equal (plist-get matched-path :match-start) 18))
  87. (should (equal (plist-get matched-path :match-end) 23))))
  88. (ert-deftest test-json-path-to-position-no-match ()
  89. (let* ((json-string "{\"foo\": {\"bar\": \"baz\"}}")
  90. (matched-path (json-path-to-position 5 json-string)))
  91. (should (null matched-path))))
  92. ;;; Keywords
  93. (ert-deftest test-json-read-keyword ()
  94. (json-tests--with-temp-buffer "true"
  95. (should (json-read-keyword "true")))
  96. (json-tests--with-temp-buffer "true"
  97. (should-error
  98. (json-read-keyword "false") :type 'json-unknown-keyword))
  99. (json-tests--with-temp-buffer "foo"
  100. (should-error
  101. (json-read-keyword "foo") :type 'json-unknown-keyword)))
  102. (ert-deftest test-json-encode-keyword ()
  103. (should (equal (json-encode-keyword t) "true"))
  104. (should (equal (json-encode-keyword json-false) "false"))
  105. (should (equal (json-encode-keyword json-null) "null")))
  106. ;;; Numbers
  107. (ert-deftest test-json-read-number ()
  108. (json-tests--with-temp-buffer "3"
  109. (should (= (json-read-number) 3)))
  110. (json-tests--with-temp-buffer "-5"
  111. (should (= (json-read-number) -5)))
  112. (json-tests--with-temp-buffer "123.456"
  113. (should (= (json-read-number) 123.456)))
  114. (json-tests--with-temp-buffer "1e3"
  115. (should (= (json-read-number) 1e3)))
  116. (json-tests--with-temp-buffer "2e+3"
  117. (should (= (json-read-number) 2e3)))
  118. (json-tests--with-temp-buffer "3E3"
  119. (should (= (json-read-number) 3e3)))
  120. (json-tests--with-temp-buffer "1e-7"
  121. (should (= (json-read-number) 1e-7)))
  122. (json-tests--with-temp-buffer "abc"
  123. (should-error (json-read-number) :type 'json-number-format)))
  124. (ert-deftest test-json-encode-number ()
  125. (should (equal (json-encode-number 3) "3"))
  126. (should (equal (json-encode-number -5) "-5"))
  127. (should (equal (json-encode-number 123.456) "123.456")))
  128. ;; Strings
  129. (ert-deftest test-json-read-escaped-char ()
  130. (json-tests--with-temp-buffer "\\\""
  131. (should (equal (json-read-escaped-char) ?\"))))
  132. (ert-deftest test-json-read-string ()
  133. (json-tests--with-temp-buffer "\"foo \\\"bar\\\"\""
  134. (should (equal (json-read-string) "foo \"bar\"")))
  135. (json-tests--with-temp-buffer "\"abcαβγ\""
  136. (should (equal (json-read-string) "abcαβγ")))
  137. (json-tests--with-temp-buffer "\"\\nasd\\u0444\\u044b\\u0432fgh\\t\""
  138. (should (equal (json-read-string) "\nasdфывfgh\t")))
  139. ;; Bug#24784
  140. (json-tests--with-temp-buffer "\"\\uD834\\uDD1E\""
  141. (should (equal (json-read-string) "\U0001D11E")))
  142. (json-tests--with-temp-buffer "foo"
  143. (should-error (json-read-string) :type 'json-string-format)))
  144. (ert-deftest test-json-encode-string ()
  145. (should (equal (json-encode-string "foo") "\"foo\""))
  146. (should (equal (json-encode-string "a\n\fb") "\"a\\n\\fb\""))
  147. (should (equal (json-encode-string "\nasdфыв\u001f\u007ffgh\t")
  148. "\"\\nasdфыв\\u001f\u007ffgh\\t\"")))
  149. (ert-deftest test-json-encode-key ()
  150. (should (equal (json-encode-key "foo") "\"foo\""))
  151. (should (equal (json-encode-key 'foo) "\"foo\""))
  152. (should (equal (json-encode-key :foo) "\"foo\""))
  153. (should-error (json-encode-key 5) :type 'json-key-format)
  154. (should-error (json-encode-key ["foo"]) :type 'json-key-format)
  155. (should-error (json-encode-key '("foo")) :type 'json-key-format))
  156. ;;; Objects
  157. (ert-deftest test-json-new-object ()
  158. (let ((json-object-type 'alist))
  159. (should (equal (json-new-object) '())))
  160. (let ((json-object-type 'plist))
  161. (should (equal (json-new-object) '())))
  162. (let* ((json-object-type 'hash-table)
  163. (json-object (json-new-object)))
  164. (should (hash-table-p json-object))
  165. (should (= (hash-table-count json-object) 0))))
  166. (ert-deftest test-json-add-to-object ()
  167. (let* ((json-object-type 'alist)
  168. (json-key-type nil)
  169. (obj (json-new-object)))
  170. (setq obj (json-add-to-object obj "a" 1))
  171. (setq obj (json-add-to-object obj "b" 2))
  172. (should (equal (assq 'a obj) '(a . 1)))
  173. (should (equal (assq 'b obj) '(b . 2))))
  174. (let* ((json-object-type 'plist)
  175. (json-key-type nil)
  176. (obj (json-new-object)))
  177. (setq obj (json-add-to-object obj "a" 1))
  178. (setq obj (json-add-to-object obj "b" 2))
  179. (should (= (plist-get obj :a) 1))
  180. (should (= (plist-get obj :b) 2)))
  181. (let* ((json-object-type 'hash-table)
  182. (json-key-type nil)
  183. (obj (json-new-object)))
  184. (setq obj (json-add-to-object obj "a" 1))
  185. (setq obj (json-add-to-object obj "b" 2))
  186. (should (= (gethash "a" obj) 1))
  187. (should (= (gethash "b" obj) 2))))
  188. (ert-deftest test-json-read-object ()
  189. (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }"
  190. (let ((json-object-type 'alist))
  191. (should (equal (json-read-object) '((a . 1) (b . 2))))))
  192. (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }"
  193. (let ((json-object-type 'plist))
  194. (should (equal (json-read-object) '(:a 1 :b 2)))))
  195. (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }"
  196. (let* ((json-object-type 'hash-table)
  197. (hash-table (json-read-object)))
  198. (should (= (gethash "a" hash-table) 1))
  199. (should (= (gethash "b" hash-table) 2))))
  200. (json-tests--with-temp-buffer "{ \"a\": 1 \"b\": 2 }"
  201. (should-error (json-read-object) :type 'json-object-format)))
  202. (ert-deftest test-json-encode-hash-table ()
  203. (let ((hash-table (make-hash-table))
  204. (json-encoding-object-sort-predicate 'string<)
  205. (json-encoding-pretty-print nil))
  206. (puthash :a 1 hash-table)
  207. (puthash :b 2 hash-table)
  208. (puthash :c 3 hash-table)
  209. (should (equal (json-encode hash-table)
  210. "{\"a\":1,\"b\":2,\"c\":3}"))))
  211. (ert-deftest json-encode-simple-alist ()
  212. (let ((json-encoding-pretty-print nil))
  213. (should (equal (json-encode '((a . 1) (b . 2)))
  214. "{\"a\":1,\"b\":2}"))))
  215. (ert-deftest test-json-encode-plist ()
  216. (let ((plist '(:a 1 :b 2))
  217. (json-encoding-pretty-print nil))
  218. (should (equal (json-encode plist) "{\"a\":1,\"b\":2}"))))
  219. (ert-deftest test-json-encode-plist-with-sort-predicate ()
  220. (let ((plist '(:c 3 :a 1 :b 2))
  221. (json-encoding-object-sort-predicate 'string<)
  222. (json-encoding-pretty-print nil))
  223. (should (equal (json-encode plist) "{\"a\":1,\"b\":2,\"c\":3}"))))
  224. (ert-deftest test-json-encode-alist-with-sort-predicate ()
  225. (let ((alist '((:c . 3) (:a . 1) (:b . 2)))
  226. (json-encoding-object-sort-predicate 'string<)
  227. (json-encoding-pretty-print nil))
  228. (should (equal (json-encode alist) "{\"a\":1,\"b\":2,\"c\":3}"))))
  229. (ert-deftest test-json-encode-list ()
  230. (let ((json-encoding-pretty-print nil))
  231. (should (equal (json-encode-list '(:a 1 :b 2))
  232. "{\"a\":1,\"b\":2}"))
  233. (should (equal (json-encode-list '((:a . 1) (:b . 2)))
  234. "{\"a\":1,\"b\":2}"))
  235. (should (equal (json-encode-list '(1 2 3 4)) "[1,2,3,4]"))))
  236. ;;; Arrays
  237. (ert-deftest test-json-read-array ()
  238. (let ((json-array-type 'vector))
  239. (json-tests--with-temp-buffer "[1, 2, \"a\", \"b\"]"
  240. (should (equal (json-read-array) [1 2 "a" "b"]))))
  241. (let ((json-array-type 'list))
  242. (json-tests--with-temp-buffer "[1, 2, \"a\", \"b\"]"
  243. (should (equal (json-read-array) '(1 2 "a" "b")))))
  244. (json-tests--with-temp-buffer "[1 2]"
  245. (should-error (json-read-array) :type 'json-error)))
  246. (ert-deftest test-json-encode-array ()
  247. (let ((json-encoding-pretty-print nil))
  248. (should (equal (json-encode-array [1 2 "a" "b"])
  249. "[1,2,\"a\",\"b\"]"))))
  250. ;;; Reader
  251. (ert-deftest test-json-read ()
  252. (json-tests--with-temp-buffer "{ \"a\": 1 }"
  253. ;; We don't care exactly what the return value is (that is tested
  254. ;; in `test-json-read-object'), but it should parse without error.
  255. (should (json-read)))
  256. (json-tests--with-temp-buffer ""
  257. (should-error (json-read) :type 'json-end-of-file))
  258. (json-tests--with-temp-buffer "xxx"
  259. (should-error (json-read) :type 'json-readtable-error)))
  260. (ert-deftest test-json-read-from-string ()
  261. (let ((json-string "{ \"a\": 1 }"))
  262. (json-tests--with-temp-buffer json-string
  263. (should (equal (json-read-from-string json-string)
  264. (json-read))))))
  265. ;;; JSON encoder
  266. (ert-deftest test-json-encode ()
  267. (should (equal (json-encode "foo") "\"foo\""))
  268. (with-temp-buffer
  269. (should-error (json-encode (current-buffer)) :type 'json-error)))
  270. (provide 'json-tests)
  271. ;;; json-tests.el ends here