all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Simen Heggestøyl" <simenheg@gmail.com>
To: Dmitry Gutov <dgutov@yandex.ru>
Cc: 21985@debbugs.gnu.org
Subject: bug#21985: 25.0.50; PATCH] Extend the test suite for json.el
Date: Tue, 24 Nov 2015 21:25:29 +0100	[thread overview]
Message-ID: <1448396729.6004.1@smtp.gmail.com> (raw)
In-Reply-To: <56524C7B.9010500@yandex.ru>


[-- Attachment #1.1: Type: text/plain, Size: 501 bytes --]

On Mon, Nov 23, 2015 at 12:15 AM, Dmitry Gutov <dgutov@yandex.ru> wrote:
> IMO, there's no too much need to exhaustively test `json-encode', 
> aside from one encode test (with any value), one decode test, and 
> maybe one try-to-encode-something-unusual-and-end-up-with-an-error 
> test.
> 
> Testing all contents of json-readtable would seem excessive to me. 
> But hey, if we miscalculate, we can add more tests as we fix 
> regressions, later.

Alright!

I've attached an updated patch.

-- Simen

[-- Attachment #1.2: Type: text/html, Size: 646 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Extend-the-test-suite-for-json.el.patch --]
[-- Type: text/x-patch, Size: 14701 bytes --]

From 10576e0204fc16ba165a6a5d6fc46d96cf7be081 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simen=20Heggest=C3=B8yl?= <simenheg@gmail.com>
Date: Sat, 14 Nov 2015 17:54:38 +0100
Subject: [PATCH] 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'.
---
 lisp/json.el                 |   2 +-
 test/automated/json-tests.el | 297 +++++++++++++++++++++++++++++++++++++------
 2 files changed, 259 insertions(+), 40 deletions(-)

diff --git a/lisp/json.el b/lisp/json.el
index 0214a3e..85827b5 100644
--- a/lisp/json.el
+++ b/lisp/json.el
@@ -149,7 +149,7 @@ json-alist-p
   (null list))
 
 (defun json-plist-p (list)
-  "Non-null if and only if LIST is a plist."
+  "Non-null if and only if LIST is a plist with keyword keys."
   (while (consp list)
     (setq list (if (and (keywordp (car list))
                         (consp (cdr list)))
diff --git a/test/automated/json-tests.el b/test/automated/json-tests.el
index 8f0cd6f..bb043dc 100644
--- a/test/automated/json-tests.el
+++ b/test/automated/json-tests.el
@@ -22,6 +22,38 @@
 (require 'ert)
 (require 'json)
 
+(defmacro json-tests--with-temp-buffer (content &rest body)
+  "Create a temporary buffer with CONTENT and evaluate BODY there.
+Point is moved to beginning of the buffer."
+  (declare (indent 1))
+  `(with-temp-buffer
+     (insert ,content)
+     (goto-char (point-min))
+     ,@body))
+
+;;; Utilities
+
+(ert-deftest test-json-join ()
+  (should (equal (json-join '() ", ")  ""))
+  (should (equal (json-join '("a" "b" "c") ", ")  "a, b, c")))
+
+(ert-deftest test-json-alist-p ()
+  (should (json-alist-p '()))
+  (should (json-alist-p '((a 1) (b 2) (c 3))))
+  (should (json-alist-p '((:a 1) (:b 2) (:c 3))))
+  (should (json-alist-p '(("a" 1) ("b" 2) ("c" 3))))
+  (should-not (json-alist-p '(:a :b :c)))
+  (should-not (json-alist-p '(:a 1 :b 2 :c 3)))
+  (should-not (json-alist-p '((:a 1) (:b 2) 3))))
+
+(ert-deftest test-json-plist-p ()
+  (should (json-plist-p '()))
+  (should (json-plist-p '(:a 1 :b 2 :c 3)))
+  (should-not (json-plist-p '(a 1 b 2 c 3)))
+  (should-not (json-plist-p '("a" 1 "b" 2 "c" 3)))
+  (should-not (json-plist-p '(:a :b :c)))
+  (should-not (json-plist-p '((:a 1) (:b 2) (:c 3)))))
+
 (ert-deftest test-json-plist-reverse ()
   (should (equal (json--plist-reverse '()) '()))
   (should (equal (json--plist-reverse '(:a 1)) '(:a 1)))
@@ -34,49 +66,32 @@
   (should (equal (json--plist-to-alist '(:a 1 :b 2 :c 3))
                  '((:a . 1) (:b . 2) (:c . 3)))))
 
-(ert-deftest test-json-encode-plist ()
-  (let ((plist '(:a 1 :b 2)))
-    (should (equal (json-encode plist) "{\"a\":1,\"b\":2}"))))
-
-(ert-deftest json-encode-simple-alist ()
-  (should (equal (json-encode '((a . 1)
-                                (b . 2)))
-                 "{\"a\":1,\"b\":2}")))
-
-(ert-deftest test-json-encode-hash-table ()
-  (let ((hash-table (make-hash-table))
-        (json-encoding-object-sort-predicate 'string<))
-    (puthash :a 1 hash-table)
-    (puthash :b 2 hash-table)
-    (puthash :c 3 hash-table)
-    (should (equal (json-encode hash-table)
-                   "{\"a\":1,\"b\":2,\"c\":3}"))))
-
-(ert-deftest test-json-encode-alist-with-sort-predicate ()
-  (let ((alist '((:c . 3) (:a . 1) (:b . 2)))
-        (json-encoding-object-sort-predicate 'string<))
-    (should (equal (json-encode alist) "{\"a\":1,\"b\":2,\"c\":3}"))))
+(ert-deftest test-json-advance ()
+  (json-tests--with-temp-buffer "{ \"a\": 1 }"
+    (json-advance 0)
+    (should (= (point) (point-min)))
+    (json-advance 3)
+    (should (= (point) (+ (point-min) 3)))))
 
-(ert-deftest test-json-encode-plist-with-sort-predicate ()
-  (let ((plist '(:c 3 :a 1 :b 2))
-        (json-encoding-object-sort-predicate 'string<))
-    (should (equal (json-encode plist) "{\"a\":1,\"b\":2,\"c\":3}"))))
+(ert-deftest test-json-peek ()
+  (json-tests--with-temp-buffer ""
+    (should (eq (json-peek) :json-eof)))
+  (json-tests--with-temp-buffer "{ \"a\": 1 }"
+    (should (equal (json-peek) ?{))))
 
-(ert-deftest json-read-simple-alist ()
-  (let ((json-object-type 'alist))
-    (should (equal (json-read-from-string "{\"a\": 1, \"b\": 2}")
-                   '((a . 1)
-                     (b . 2))))))
+(ert-deftest test-json-pop ()
+  (json-tests--with-temp-buffer ""
+    (should-error (json-pop) :type 'json-end-of-file))
+  (json-tests--with-temp-buffer "{ \"a\": 1 }"
+    (should (equal (json-pop) ?{))
+    (should (= (point) (+ (point-min) 1)))))
 
-(ert-deftest json-encode-string-with-special-chars ()
-  (should (equal (json-encode-string "a\n\fb")
-                 "\"a\\n\\fb\""))
-  (should (equal (json-encode-string "\nasdфыв\u001f\u007ffgh\t")
-                 "\"\\nasdфыв\\u001f\u007ffgh\\t\"")))
+(ert-deftest test-json-skip-whitespace ()
+  (json-tests--with-temp-buffer "\t\r\n\f\b { \"a\": 1 }"
+    (json-skip-whitespace)
+    (should (equal (char-after (point)) ?{))))
 
-(ert-deftest json-read-string-with-special-chars ()
-  (should (equal (json-read-from-string "\"\\nasd\\u0444\\u044b\\u0432fgh\\t\"")
-                 "\nasdфывfgh\t")))
+;;; Paths
 
 (ert-deftest test-json-path-to-position-with-objects ()
   (let* ((json-string "{\"foo\": {\"bar\": {\"baz\": \"value\"}}}")
@@ -97,5 +112,209 @@
          (matched-path (json-path-to-position 5 json-string)))
     (should (null matched-path))))
 
+;;; Keywords
+
+(ert-deftest test-json-read-keyword ()
+  (json-tests--with-temp-buffer "true"
+    (should (json-read-keyword "true")))
+  (json-tests--with-temp-buffer "true"
+    (should-error
+     (json-read-keyword "false") :type 'json-unknown-keyword))
+  (json-tests--with-temp-buffer "foo"
+    (should-error
+     (json-read-keyword "foo") :type 'json-unknown-keyword)))
+
+(ert-deftest test-json-encode-keyword ()
+  (should (equal (json-encode-keyword t) "true"))
+  (should (equal (json-encode-keyword json-false) "false"))
+  (should (equal (json-encode-keyword json-null) "null")))
+
+;;; Numbers
+
+(ert-deftest test-json-read-number ()
+  (json-tests--with-temp-buffer "3"
+    (should (= (json-read-number) 3)))
+  (json-tests--with-temp-buffer "-5"
+    (should (= (json-read-number) -5)))
+  (json-tests--with-temp-buffer "123.456"
+    (should (= (json-read-number) 123.456)))
+  (json-tests--with-temp-buffer "1e3"
+    (should (= (json-read-number) 1e3)))
+  (json-tests--with-temp-buffer "2e+3"
+    (should (= (json-read-number) 2e3)))
+  (json-tests--with-temp-buffer "3E3"
+    (should (= (json-read-number) 3e3)))
+  (json-tests--with-temp-buffer "1e-7"
+    (should (= (json-read-number) 1e-7)))
+  (json-tests--with-temp-buffer "abc"
+    (should-error (json-read-number) :type 'json-number-format)))
+
+(ert-deftest test-json-encode-number ()
+  (should (equal (json-encode-number 3) "3"))
+  (should (equal (json-encode-number -5) "-5"))
+  (should (equal (json-encode-number 123.456) "123.456")))
+
+;; Strings
+
+(ert-deftest test-json-read-escaped-char ()
+  (json-tests--with-temp-buffer "\\\""
+    (should (equal (json-read-escaped-char) ?\"))))
+
+(ert-deftest test-json-read-string ()
+  (json-tests--with-temp-buffer "\"foo \\\"bar\\\"\""
+    (should (equal (json-read-string) "foo \"bar\"")))
+  (json-tests--with-temp-buffer "\"abcαβγ\""
+    (should (equal (json-read-string) "abcαβγ")))
+  (json-tests--with-temp-buffer "\"\\nasd\\u0444\\u044b\\u0432fgh\\t\""
+    (should (equal (json-read-string) "\nasdфывfgh\t")))
+  (json-tests--with-temp-buffer "foo"
+    (should-error (json-read-string) :type 'json-string-format)))
+
+(ert-deftest test-json-encode-string ()
+  (should (equal (json-encode-string "foo") "\"foo\""))
+  (should (equal (json-encode-string "a\n\fb") "\"a\\n\\fb\""))
+  (should (equal (json-encode-string "\nasdфыв\u001f\u007ffgh\t")
+                 "\"\\nasdфыв\\u001f\u007ffgh\\t\"")))
+
+(ert-deftest test-json-encode-key ()
+  (should (equal (json-encode-key "foo") "\"foo\""))
+  (should (equal (json-encode-key 'foo) "\"foo\""))
+  (should (equal (json-encode-key :foo) "\"foo\""))
+  (should-error (json-encode-key 5) :type 'json-key-format)
+  (should-error (json-encode-key ["foo"]) :type 'json-key-format)
+  (should-error (json-encode-key '("foo")) :type 'json-key-format))
+
+;;; Objects
+
+(ert-deftest test-json-new-object ()
+  (let ((json-object-type 'alist))
+    (should (equal (json-new-object) '())))
+  (let ((json-object-type 'plist))
+    (should (equal (json-new-object) '())))
+  (let* ((json-object-type 'hash-table)
+         (json-object (json-new-object)))
+    (should (hash-table-p json-object))
+    (should (= (hash-table-count json-object) 0))))
+
+(ert-deftest test-json-add-to-object ()
+  (let* ((json-object-type 'alist)
+         (json-key-type nil)
+         (obj (json-new-object)))
+    (setq obj (json-add-to-object obj "a" 1))
+    (setq obj (json-add-to-object obj "b" 2))
+    (should (equal (assq 'a obj) '(a . 1)))
+    (should (equal (assq 'b obj) '(b . 2))))
+  (let* ((json-object-type 'plist)
+         (json-key-type nil)
+         (obj (json-new-object)))
+    (setq obj (json-add-to-object obj "a" 1))
+    (setq obj (json-add-to-object obj "b" 2))
+    (should (= (plist-get obj :a) 1))
+    (should (= (plist-get obj :b) 2)))
+  (let* ((json-object-type 'hash-table)
+         (json-key-type nil)
+         (obj (json-new-object)))
+    (setq obj (json-add-to-object obj "a" 1))
+    (setq obj (json-add-to-object obj "b" 2))
+    (should (= (gethash "a" obj) 1))
+    (should (= (gethash "b" obj) 2))))
+
+(ert-deftest test-json-read-object ()
+  (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }"
+    (let ((json-object-type 'alist))
+      (should (equal (json-read-object) '((a . 1) (b . 2))))))
+  (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }"
+    (let ((json-object-type 'plist))
+      (should (equal (json-read-object) '(:a 1 :b 2)))))
+  (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }"
+    (let* ((json-object-type 'hash-table)
+           (hash-table (json-read-object)))
+      (should (= (gethash "a" hash-table) 1))
+      (should (= (gethash "b" hash-table) 2))))
+  (json-tests--with-temp-buffer "{ \"a\": 1 \"b\": 2 }"
+    (should-error (json-read-object) :type 'json-object-format)))
+
+(ert-deftest test-json-encode-hash-table ()
+  (let ((hash-table (make-hash-table))
+        (json-encoding-object-sort-predicate 'string<)
+        (json-encoding-pretty-print nil))
+    (puthash :a 1 hash-table)
+    (puthash :b 2 hash-table)
+    (puthash :c 3 hash-table)
+    (should (equal (json-encode hash-table)
+                   "{\"a\":1,\"b\":2,\"c\":3}"))))
+
+(ert-deftest json-encode-simple-alist ()
+  (let ((json-encoding-pretty-print nil))
+    (should (equal (json-encode '((a . 1) (b . 2)))
+                   "{\"a\":1,\"b\":2}"))))
+
+(ert-deftest test-json-encode-plist ()
+  (let ((plist '(:a 1 :b 2))
+        (json-encoding-pretty-print nil))
+    (should (equal (json-encode plist) "{\"a\":1,\"b\":2}"))))
+
+(ert-deftest test-json-encode-plist-with-sort-predicate ()
+  (let ((plist '(:c 3 :a 1 :b 2))
+        (json-encoding-object-sort-predicate 'string<)
+        (json-encoding-pretty-print nil))
+    (should (equal (json-encode plist) "{\"a\":1,\"b\":2,\"c\":3}"))))
+
+(ert-deftest test-json-encode-alist-with-sort-predicate ()
+  (let ((alist '((:c . 3) (:a . 1) (:b . 2)))
+        (json-encoding-object-sort-predicate 'string<)
+        (json-encoding-pretty-print nil))
+    (should (equal (json-encode alist) "{\"a\":1,\"b\":2,\"c\":3}"))))
+
+(ert-deftest test-json-encode-list ()
+  (let ((json-encoding-pretty-print nil))
+    (should (equal (json-encode-list '(:a 1 :b 2))
+                   "{\"a\":1,\"b\":2}"))
+    (should (equal (json-encode-list '((:a . 1) (:b . 2)))
+                   "{\"a\":1,\"b\":2}"))
+    (should (equal (json-encode-list '(1 2 3 4)) "[1,2,3,4]"))))
+
+;;; Arrays
+
+(ert-deftest test-json-read-array ()
+  (let ((json-array-type 'vector))
+    (json-tests--with-temp-buffer "[1, 2, \"a\", \"b\"]"
+      (should (equal (json-read-array) [1 2 "a" "b"]))))
+  (let ((json-array-type 'list))
+    (json-tests--with-temp-buffer "[1, 2, \"a\", \"b\"]"
+      (should (equal (json-read-array) '(1 2 "a" "b")))))
+  (json-tests--with-temp-buffer "[1 2]"
+    (should-error (json-read-array) :type 'json-error)))
+
+(ert-deftest test-json-encode-array ()
+  (let ((json-encoding-pretty-print nil))
+    (should (equal (json-encode-array [1 2 "a" "b"])
+                   "[1,2,\"a\",\"b\"]"))))
+
+;;; Reader
+
+(ert-deftest test-json-read ()
+  (json-tests--with-temp-buffer "{ \"a\": 1 }"
+    ;; We don't care exactly what the return value is (that is tested
+    ;; in `test-json-read-object'), but it should parse without error.
+    (should (json-read)))
+  (json-tests--with-temp-buffer ""
+    (should-error (json-read) :type 'json-end-of-file))
+  (json-tests--with-temp-buffer "xxx"
+    (should-error (json-read) :type 'json-readtable-error)))
+
+(ert-deftest test-json-read-from-string ()
+  (let ((json-string "{ \"a\": 1 }"))
+    (json-tests--with-temp-buffer json-string
+      (should (equal (json-read-from-string json-string)
+                     (json-read))))))
+
+;;; JSON encoder
+
+(ert-deftest test-json-encode ()
+  (should (equal (json-encode "foo") "\"foo\""))
+  (with-temp-buffer
+    (should-error (json-encode (current-buffer)) :type 'json-error)))
+
 (provide 'json-tests)
 ;;; json-tests.el ends here
-- 
2.6.2


  reply	other threads:[~2015-11-24 20:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-22 21:30 bug#21985: 25.0.50; PATCH] Extend the test suite for json.el Simen Heggestøyl
2015-11-22 23:15 ` Dmitry Gutov
2015-11-24 20:25   ` Simen Heggestøyl [this message]
2015-11-24 20:53     ` Dmitry Gutov
2015-11-24 22:16       ` Simen Heggestøyl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1448396729.6004.1@smtp.gmail.com \
    --to=simenheg@gmail.com \
    --cc=21985@debbugs.gnu.org \
    --cc=dgutov@yandex.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.