aboutsummaryrefslogtreecommitdiffstats
path: root/test/src
diff options
context:
space:
mode:
authorPhilipp Stephani2017-09-18 10:51:39 +0200
committerPhilipp Stephani2017-09-18 16:34:47 +0200
commitcb99cf5a99680af7dc2c49fdf5b840d1ff4dd928 (patch)
tree8b6d73ffc577bc69fc6833c4dea4b5e555b9598b /test/src
parent61a5c30e70926f48480b03b79f4f531c8d64418e (diff)
downloademacs-cb99cf5a99680af7dc2c49fdf5b840d1ff4dd928.tar.gz
emacs-cb99cf5a99680af7dc2c49fdf5b840d1ff4dd928.zip
Implement native JSON support using Jansson
* configure.ac: New option --with-json. * src/json.c (Fjson_serialize, Fjson_insert, Fjson_parse_string) (Fjson_parse_buffer): New defuns. (json_out_of_memory, json_parse_error, json_release_object) (check_string_without_embedded_nulls, json_check, lisp_to_json) (json_insert, json_insert_callback, json_to_lisp) (json_read_buffer_callback, Fjson_parse_buffer, define_error): New helper function. (syms_of_json): New file. * src/lisp.h: Declaration for syms_of_json. * src/emacs.c (main): Enable JSON functions. * src/Makefile.in (JSON_LIBS, JSON_CFLAGS, JSON_OBJ, EMACS_CFLAGS) (base_obj, LIBES): Compile json.c if --with-json is enabled. * test/src/json-tests.el (json-serialize/roundtrip) (json-serialize/object, json-parse-string/object): New unit tests.
Diffstat (limited to 'test/src')
-rw-r--r--test/src/json-tests.el61
1 files changed, 61 insertions, 0 deletions
diff --git a/test/src/json-tests.el b/test/src/json-tests.el
new file mode 100644
index 00000000000..1d8f9a490ba
--- /dev/null
+++ b/test/src/json-tests.el
@@ -0,0 +1,61 @@
1;;; json-tests.el --- unit tests for json.c -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2017 Free Software Foundation, Inc.
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs 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;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
19
20;;; Commentary:
21
22;; Unit tests for src/json.c.
23
24;;; Code:
25
26(require 'cl-lib)
27(require 'map)
28
29(ert-deftest json-serialize/roundtrip ()
30 (let ((lisp [nil :json-false t 0 123 -456 3.75 "foo"])
31 (json "[null,false,true,0,123,-456,3.75,\"foo\"]"))
32 (should (equal (json-serialize lisp) json))
33 (with-temp-buffer
34 (json-insert lisp)
35 (should (equal (buffer-string) json))
36 (should (eobp)))
37 (should (equal (json-parse-string json) lisp))
38 (with-temp-buffer
39 (insert json)
40 (goto-char 1)
41 (should (equal (json-parse-buffer) lisp))
42 (should (eobp)))))
43
44(ert-deftest json-serialize/object ()
45 (let ((table (make-hash-table :test #'equal)))
46 (puthash "abc" [1 2 t] table)
47 (puthash "def" nil table)
48 (should (equal (json-serialize table)
49 "{\"abc\":[1,2,true],\"def\":null}"))))
50
51(ert-deftest json-parse-string/object ()
52 (let ((actual
53 (json-parse-string
54 "{ \"abc\" : [1, 2, true], \"def\" : null, \"abc\" : [9, false] }\n")))
55 (should (hash-table-p actual))
56 (should (equal (hash-table-count actual) 2))
57 (should (equal (cl-sort (map-pairs actual) #'string< :key #'car)
58 '(("abc" . [9 :json-false]) ("def"))))))
59
60(provide 'json-tests)
61;;; json-tests.el ends here