diff options
| author | Philipp Stephani | 2017-09-18 10:51:39 +0200 |
|---|---|---|
| committer | Philipp Stephani | 2017-12-10 13:49:08 +0100 |
| commit | ab203e36d5f84a99b6d4b04f1a22ba028be750e3 (patch) | |
| tree | 660ed9e9cf32973808ace1c5aed572885bafd110 /test/src | |
| parent | 402e790ad4cff87d0e40e516a15553c408f12de1 (diff) | |
| download | emacs-ab203e36d5f84a99b6d4b04f1a22ba028be750e3.tar.gz emacs-ab203e36d5f84a99b6d4b04f1a22ba028be750e3.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_malloc, json_free, json_has_prefix, json_has_suffix)
(json_make_string, json_build_string, json_encode)
(json_out_of_memory, json_parse_error)
(json_release_object, check_string_without_embedded_nulls, json_check)
(lisp_to_json, lisp_to_json_toplevel, lisp_to_json_toplevel_1)
(json_insert, json_insert_callback, json_to_lisp)
(json_read_buffer_callback, Fjson_parse_buffer, define_error): New
helper functions.
(init_json, syms_of_json): New file.
* src/lisp.h: Declaration for init_json and syms_of_json.
* src/emacs.c (main): Enable JSON functions.
* src/eval.c (internal_catch_all, internal_catch_all_1): New helper
functions to catch all signals.
(syms_of_eval): Add uninterned symbol to signify out of memory.
* 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)
(json-parse-string/string, json-serialize/string)
(json-parse-string/incomplete, json-parse-string/trailing)
(json-parse-buffer/incomplete, json-parse-buffer/trailing): New unit
tests.
* doc/lispref/text.texi (Parsing JSON): New manual section.
Diffstat (limited to 'test/src')
| -rw-r--r-- | test/src/json-tests.el | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/test/src/json-tests.el b/test/src/json-tests.el new file mode 100644 index 00000000000..5d3c84a136c --- /dev/null +++ b/test/src/json-tests.el | |||
| @@ -0,0 +1,97 @@ | |||
| 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 [:null :false t 0 123 -456 3.75 "abcαβγ"]) | ||
| 31 | (json "[null,false,true,0,123,-456,3.75,\"abcαβγ\"]")) | ||
| 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" :null 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 :false]) ("def" . :null)))))) | ||
| 59 | |||
| 60 | (ert-deftest json-parse-string/string () | ||
| 61 | (should-error (json-parse-string "[\"formfeed\f\"]") :type 'json-parse-error) | ||
| 62 | (should (equal (json-parse-string "[\"foo \\\"bar\\\"\"]") ["foo \"bar\""])) | ||
| 63 | (should (equal (json-parse-string "[\"abcαβγ\"]") ["abcαβγ"])) | ||
| 64 | (should (equal (json-parse-string "[\"\\nasd\\u0444\\u044b\\u0432fgh\\t\"]") | ||
| 65 | ["\nasdфывfgh\t"])) | ||
| 66 | (should (equal (json-parse-string "[\"\\uD834\\uDD1E\"]") ["\U0001D11E"])) | ||
| 67 | (should-error (json-parse-string "foo") :type 'json-parse-error)) | ||
| 68 | |||
| 69 | (ert-deftest json-serialize/string () | ||
| 70 | (should (equal (json-serialize ["foo"]) "[\"foo\"]")) | ||
| 71 | (should (equal (json-serialize ["a\n\fb"]) "[\"a\\n\\fb\"]")) | ||
| 72 | (should (equal (json-serialize ["\nasdфыв\u001f\u007ffgh\t"]) | ||
| 73 | "[\"\\nasdфыв\\u001F\u007ffgh\\t\"]"))) | ||
| 74 | |||
| 75 | (ert-deftest json-parse-string/incomplete () | ||
| 76 | (should-error (json-parse-string "[123") :type 'json-end-of-file)) | ||
| 77 | |||
| 78 | (ert-deftest json-parse-string/trailing () | ||
| 79 | (should-error (json-parse-string "[123] [456]") :type 'json-trailing-content)) | ||
| 80 | |||
| 81 | (ert-deftest json-parse-buffer/incomplete () | ||
| 82 | (with-temp-buffer | ||
| 83 | (insert "[123") | ||
| 84 | (goto-char 1) | ||
| 85 | (should-error (json-parse-buffer) :type 'json-end-of-file) | ||
| 86 | (should (bobp)))) | ||
| 87 | |||
| 88 | (ert-deftest json-parse-buffer/trailing () | ||
| 89 | (with-temp-buffer | ||
| 90 | (insert "[123] [456]") | ||
| 91 | (goto-char 1) | ||
| 92 | (should (equal (json-parse-buffer) [123])) | ||
| 93 | (should-not (bobp)) | ||
| 94 | (should (looking-at-p (rx " [456]" eos))))) | ||
| 95 | |||
| 96 | (provide 'json-tests) | ||
| 97 | ;;; json-tests.el ends here | ||