diff options
| author | Andrea Corallo | 2020-05-24 10:20:23 +0100 |
|---|---|---|
| committer | Andrea Corallo | 2020-05-24 10:20:23 +0100 |
| commit | 9daffe9cfe82d3b1e1e9fa8929dbb40cfed60f0f (patch) | |
| tree | c9e78cbb4e151dc3c3996a65cf1eedab19248fb4 /test | |
| parent | f5dceed09a8234548d5b3acb76d443569533cab9 (diff) | |
| parent | e021c2dc2279e0fd3a5331f9ea661e4d39c2e840 (diff) | |
| download | emacs-9daffe9cfe82d3b1e1e9fa8929dbb40cfed60f0f.tar.gz emacs-9daffe9cfe82d3b1e1e9fa8929dbb40cfed60f0f.zip | |
Merge remote-tracking branch 'savannah/master' into HEAD
Diffstat (limited to 'test')
| -rw-r--r-- | test/lisp/electric-tests.el | 18 | ||||
| -rw-r--r-- | test/lisp/emacs-lisp/syntax-tests.el | 67 | ||||
| -rw-r--r-- | test/lisp/json-tests.el | 865 | ||||
| -rw-r--r-- | test/lisp/net/webjump-tests.el | 73 | ||||
| -rw-r--r-- | test/lisp/xml-tests.el | 10 | ||||
| -rw-r--r-- | test/src/buffer-tests.el | 6 |
6 files changed, 896 insertions, 143 deletions
diff --git a/test/lisp/electric-tests.el b/test/lisp/electric-tests.el index 56d1bdb110e..67f474cbd52 100644 --- a/test/lisp/electric-tests.el +++ b/test/lisp/electric-tests.el | |||
| @@ -547,6 +547,24 @@ baz\"\"" | |||
| 547 | (should (equal "" (buffer-string)))))) | 547 | (should (equal "" (buffer-string)))))) |
| 548 | 548 | ||
| 549 | 549 | ||
| 550 | ;;; Undoing | ||
| 551 | (ert-deftest electric-pair-undo-unrelated-state () | ||
| 552 | "Make sure `electric-pair-mode' does not confuse `undo' (bug#39680)." | ||
| 553 | (with-temp-buffer | ||
| 554 | (buffer-enable-undo) | ||
| 555 | (electric-pair-local-mode) | ||
| 556 | (let ((last-command-event ?\()) | ||
| 557 | (ert-simulate-command '(self-insert-command 1))) | ||
| 558 | (undo-boundary) | ||
| 559 | (let ((last-command-event ?a)) | ||
| 560 | (ert-simulate-command '(self-insert-command 1))) | ||
| 561 | (undo-boundary) | ||
| 562 | (ert-simulate-command '(undo)) | ||
| 563 | (let ((last-command-event ?\()) | ||
| 564 | (ert-simulate-command '(self-insert-command 1))) | ||
| 565 | (should (string= (buffer-string) "(())")))) | ||
| 566 | |||
| 567 | |||
| 550 | ;;; Electric newlines between pairs | 568 | ;;; Electric newlines between pairs |
| 551 | ;;; TODO: better tests | 569 | ;;; TODO: better tests |
| 552 | (ert-deftest electric-pair-open-extra-newline () | 570 | (ert-deftest electric-pair-open-extra-newline () |
diff --git a/test/lisp/emacs-lisp/syntax-tests.el b/test/lisp/emacs-lisp/syntax-tests.el new file mode 100644 index 00000000000..9d4c4113fdd --- /dev/null +++ b/test/lisp/emacs-lisp/syntax-tests.el | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | ;;; syntax-tests.el --- tests for syntax.el -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2020 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 | ;;; Code: | ||
| 21 | |||
| 22 | (require 'ert) | ||
| 23 | (require 'syntax) | ||
| 24 | |||
| 25 | (ert-deftest syntax-propertize--shift-groups-and-backrefs () | ||
| 26 | "Test shifting of numbered groups and back-references in regexps." | ||
| 27 | ;; A numbered group must be shifted. | ||
| 28 | (should | ||
| 29 | (string= | ||
| 30 | (syntax-propertize--shift-groups-and-backrefs | ||
| 31 | "\\(?2:[abc]+\\)foobar" 2) | ||
| 32 | "\\(?4:[abc]+\\)foobar")) | ||
| 33 | ;; A back-reference \1 on a normal sub-regexp context must be | ||
| 34 | ;; shifted. | ||
| 35 | (should | ||
| 36 | (string= | ||
| 37 | (syntax-propertize--shift-groups-and-backrefs "\\(a\\)\\1" 2) | ||
| 38 | "\\(a\\)\\3")) | ||
| 39 | ;; Shifting must not happen if the \1 appears in a character class, | ||
| 40 | ;; or in a \{\} repetition construct (although \1 isn't valid there | ||
| 41 | ;; anyway). | ||
| 42 | (let ((rx-with-class "\\(a\\)[\\1-2]") | ||
| 43 | (rx-with-rep "\\(a\\)\\{1,\\1\\}")) | ||
| 44 | (should | ||
| 45 | (string= | ||
| 46 | (syntax-propertize--shift-groups-and-backrefs rx-with-class 2) | ||
| 47 | rx-with-class)) | ||
| 48 | (should | ||
| 49 | (string= | ||
| 50 | (syntax-propertize--shift-groups-and-backrefs rx-with-rep 2) | ||
| 51 | rx-with-rep))) | ||
| 52 | ;; Now numbered groups and back-references in combination. | ||
| 53 | (should | ||
| 54 | (string= | ||
| 55 | (syntax-propertize--shift-groups-and-backrefs | ||
| 56 | "\\(?2:[abc]+\\)foo\\(\\2\\)" 2) | ||
| 57 | "\\(?4:[abc]+\\)foo\\(\\4\\)")) | ||
| 58 | ;; Emacs supports only the back-references \1,...,\9, so when a | ||
| 59 | ;; shift would result in \10 or more, an error must be signalled. | ||
| 60 | (should-error | ||
| 61 | (syntax-propertize--shift-groups-and-backrefs "\\(a\\)\\3" 7))) | ||
| 62 | |||
| 63 | ;; Local Variables: | ||
| 64 | ;; no-byte-compile: t | ||
| 65 | ;; End: | ||
| 66 | |||
| 67 | ;;; syntax-tests.el ends here. | ||
diff --git a/test/lisp/json-tests.el b/test/lisp/json-tests.el index ac9706a8ae7..a0e8c87c7b3 100644 --- a/test/lisp/json-tests.el +++ b/test/lisp/json-tests.el | |||
| @@ -21,11 +21,16 @@ | |||
| 21 | 21 | ||
| 22 | (require 'ert) | 22 | (require 'ert) |
| 23 | (require 'json) | 23 | (require 'json) |
| 24 | (require 'map) | ||
| 25 | (require 'seq) | ||
| 26 | |||
| 27 | (eval-when-compile | ||
| 28 | (require 'cl-lib)) | ||
| 24 | 29 | ||
| 25 | (defmacro json-tests--with-temp-buffer (content &rest body) | 30 | (defmacro json-tests--with-temp-buffer (content &rest body) |
| 26 | "Create a temporary buffer with CONTENT and evaluate BODY there. | 31 | "Create a temporary buffer with CONTENT and evaluate BODY there. |
| 27 | Point is moved to beginning of the buffer." | 32 | Point is moved to beginning of the buffer." |
| 28 | (declare (indent 1)) | 33 | (declare (debug t) (indent 1)) |
| 29 | `(with-temp-buffer | 34 | `(with-temp-buffer |
| 30 | (insert ,content) | 35 | (insert ,content) |
| 31 | (goto-char (point-min)) | 36 | (goto-char (point-min)) |
| @@ -33,66 +38,107 @@ Point is moved to beginning of the buffer." | |||
| 33 | 38 | ||
| 34 | ;;; Utilities | 39 | ;;; Utilities |
| 35 | 40 | ||
| 36 | (ert-deftest test-json-join () | ||
| 37 | (should (equal (json-join '() ", ") "")) | ||
| 38 | (should (equal (json-join '("a" "b" "c") ", ") "a, b, c"))) | ||
| 39 | |||
| 40 | (ert-deftest test-json-alist-p () | 41 | (ert-deftest test-json-alist-p () |
| 41 | (should (json-alist-p '())) | 42 | (should (json-alist-p '())) |
| 42 | (should (json-alist-p '((a 1) (b 2) (c 3)))) | 43 | (should (json-alist-p '((())))) |
| 43 | (should (json-alist-p '((:a 1) (:b 2) (:c 3)))) | 44 | (should (json-alist-p '((a)))) |
| 44 | (should (json-alist-p '(("a" 1) ("b" 2) ("c" 3)))) | 45 | (should (json-alist-p '((a . 1)))) |
| 46 | (should (json-alist-p '((a . 1) (b 2) (c)))) | ||
| 47 | (should (json-alist-p '((:a) (:b 2) (:c . 3)))) | ||
| 48 | (should (json-alist-p '(("a" . 1) ("b" 2) ("c")))) | ||
| 49 | (should-not (json-alist-p '(()))) | ||
| 50 | (should-not (json-alist-p '(a))) | ||
| 51 | (should-not (json-alist-p '(a . 1))) | ||
| 52 | (should-not (json-alist-p '((a . 1) . []))) | ||
| 53 | (should-not (json-alist-p '((a . 1) []))) | ||
| 45 | (should-not (json-alist-p '(:a :b :c))) | 54 | (should-not (json-alist-p '(:a :b :c))) |
| 46 | (should-not (json-alist-p '(:a 1 :b 2 :c 3))) | 55 | (should-not (json-alist-p '(:a 1 :b 2 :c 3))) |
| 47 | (should-not (json-alist-p '((:a 1) (:b 2) 3)))) | 56 | (should-not (json-alist-p '((:a 1) (:b 2) 3))) |
| 57 | (should-not (json-alist-p '((:a 1) (:b 2) ()))) | ||
| 58 | (should-not (json-alist-p '(((a) 1) (b 2) (c 3)))) | ||
| 59 | (should-not (json-alist-p [])) | ||
| 60 | (should-not (json-alist-p [(a . 1)])) | ||
| 61 | (should-not (json-alist-p #s(hash-table)))) | ||
| 48 | 62 | ||
| 49 | (ert-deftest test-json-plist-p () | 63 | (ert-deftest test-json-plist-p () |
| 50 | (should (json-plist-p '())) | 64 | (should (json-plist-p '())) |
| 65 | (should (json-plist-p '(:a 1))) | ||
| 51 | (should (json-plist-p '(:a 1 :b 2 :c 3))) | 66 | (should (json-plist-p '(:a 1 :b 2 :c 3))) |
| 67 | (should (json-plist-p '(:a :b))) | ||
| 68 | (should (json-plist-p '(:a :b :c :d))) | ||
| 69 | (should-not (json-plist-p '(a))) | ||
| 70 | (should-not (json-plist-p '(a 1))) | ||
| 52 | (should-not (json-plist-p '(a 1 b 2 c 3))) | 71 | (should-not (json-plist-p '(a 1 b 2 c 3))) |
| 53 | (should-not (json-plist-p '("a" 1 "b" 2 "c" 3))) | 72 | (should-not (json-plist-p '("a" 1 "b" 2 "c" 3))) |
| 73 | (should-not (json-plist-p '(:a))) | ||
| 54 | (should-not (json-plist-p '(:a :b :c))) | 74 | (should-not (json-plist-p '(:a :b :c))) |
| 55 | (should-not (json-plist-p '((:a 1) (:b 2) (:c 3))))) | 75 | (should-not (json-plist-p '(:a 1 :b 2 :c))) |
| 56 | 76 | (should-not (json-plist-p '((:a 1)))) | |
| 57 | (ert-deftest test-json-plist-reverse () | 77 | (should-not (json-plist-p '((:a 1) (:b 2) (:c 3)))) |
| 58 | (should (equal (json--plist-reverse '()) '())) | 78 | (should-not (json-plist-p [])) |
| 59 | (should (equal (json--plist-reverse '(:a 1)) '(:a 1))) | 79 | (should-not (json-plist-p [:a 1])) |
| 60 | (should (equal (json--plist-reverse '(:a 1 :b 2 :c 3)) | 80 | (should-not (json-plist-p #s(hash-table)))) |
| 81 | |||
| 82 | (ert-deftest test-json-plist-nreverse () | ||
| 83 | (should (equal (json--plist-nreverse '()) '())) | ||
| 84 | (should (equal (json--plist-nreverse (list :a 1)) '(:a 1))) | ||
| 85 | (should (equal (json--plist-nreverse (list :a 1 :b 2)) '(:b 2 :a 1))) | ||
| 86 | (should (equal (json--plist-nreverse (list :a 1 :b 2 :c 3)) | ||
| 61 | '(:c 3 :b 2 :a 1)))) | 87 | '(:c 3 :b 2 :a 1)))) |
| 62 | 88 | ||
| 63 | (ert-deftest test-json-plist-to-alist () | ||
| 64 | (should (equal (json--plist-to-alist '()) '())) | ||
| 65 | (should (equal (json--plist-to-alist '(:a 1)) '((:a . 1)))) | ||
| 66 | (should (equal (json--plist-to-alist '(:a 1 :b 2 :c 3)) | ||
| 67 | '((:a . 1) (:b . 2) (:c . 3))))) | ||
| 68 | |||
| 69 | (ert-deftest test-json-advance () | 89 | (ert-deftest test-json-advance () |
| 70 | (json-tests--with-temp-buffer "{ \"a\": 1 }" | 90 | (json-tests--with-temp-buffer "{ \"a\": 1 }" |
| 71 | (json-advance 0) | 91 | (json-advance 0) |
| 72 | (should (= (point) (point-min))) | 92 | (should (bobp)) |
| 93 | (json-advance) | ||
| 94 | (should (= (point) (1+ (point-min)))) | ||
| 95 | (json-advance 0) | ||
| 96 | (should (= (point) (1+ (point-min)))) | ||
| 97 | (json-advance 1) | ||
| 98 | (should (= (point) (+ (point-min) 2))) | ||
| 73 | (json-advance 3) | 99 | (json-advance 3) |
| 74 | (should (= (point) (+ (point-min) 3))))) | 100 | (should (= (point) (+ (point-min) 5))))) |
| 75 | 101 | ||
| 76 | (ert-deftest test-json-peek () | 102 | (ert-deftest test-json-peek () |
| 77 | (json-tests--with-temp-buffer "" | 103 | (json-tests--with-temp-buffer "" |
| 78 | (should (zerop (json-peek)))) | 104 | (should (zerop (json-peek)))) |
| 79 | (json-tests--with-temp-buffer "{ \"a\": 1 }" | 105 | (json-tests--with-temp-buffer "{ \"a\": 1 }" |
| 80 | (should (equal (json-peek) ?{)))) | 106 | (should (= (json-peek) ?\{)) |
| 107 | (goto-char (1- (point-max))) | ||
| 108 | (should (= (json-peek) ?\})) | ||
| 109 | (json-advance) | ||
| 110 | (should (zerop (json-peek))))) | ||
| 81 | 111 | ||
| 82 | (ert-deftest test-json-pop () | 112 | (ert-deftest test-json-pop () |
| 83 | (json-tests--with-temp-buffer "" | 113 | (json-tests--with-temp-buffer "" |
| 84 | (should-error (json-pop) :type 'json-end-of-file)) | 114 | (should-error (json-pop) :type 'json-end-of-file)) |
| 85 | (json-tests--with-temp-buffer "{ \"a\": 1 }" | 115 | (json-tests--with-temp-buffer "{ \"a\": 1 }" |
| 86 | (should (equal (json-pop) ?{)) | 116 | (should (= (json-pop) ?\{)) |
| 87 | (should (= (point) (+ (point-min) 1))))) | 117 | (should (= (point) (1+ (point-min)))) |
| 118 | (goto-char (1- (point-max))) | ||
| 119 | (should (= (json-pop) ?\})) | ||
| 120 | (should-error (json-pop) :type 'json-end-of-file))) | ||
| 88 | 121 | ||
| 89 | (ert-deftest test-json-skip-whitespace () | 122 | (ert-deftest test-json-skip-whitespace () |
| 123 | (json-tests--with-temp-buffer "" | ||
| 124 | (json-skip-whitespace) | ||
| 125 | (should (bobp)) | ||
| 126 | (should (eobp))) | ||
| 127 | (json-tests--with-temp-buffer "{}" | ||
| 128 | (json-skip-whitespace) | ||
| 129 | (should (bobp)) | ||
| 130 | (json-advance) | ||
| 131 | (json-skip-whitespace) | ||
| 132 | (should (= (point) (1+ (point-min)))) | ||
| 133 | (json-advance) | ||
| 134 | (json-skip-whitespace) | ||
| 135 | (should (eobp))) | ||
| 90 | (json-tests--with-temp-buffer "\t\r\n\f\b { \"a\": 1 }" | 136 | (json-tests--with-temp-buffer "\t\r\n\f\b { \"a\": 1 }" |
| 91 | (json-skip-whitespace) | 137 | (json-skip-whitespace) |
| 92 | (should (equal (char-after) ?\f))) | 138 | (should (= (json-peek) ?\f))) |
| 93 | (json-tests--with-temp-buffer "\t\r\n\t { \"a\": 1 }" | 139 | (json-tests--with-temp-buffer "\t\r\n\t { \"a\": 1 }" |
| 94 | (json-skip-whitespace) | 140 | (json-skip-whitespace) |
| 95 | (should (equal (char-after) ?{)))) | 141 | (should (= (json-peek) ?\{)))) |
| 96 | 142 | ||
| 97 | ;;; Paths | 143 | ;;; Paths |
| 98 | 144 | ||
| @@ -113,59 +159,243 @@ Point is moved to beginning of the buffer." | |||
| 113 | (ert-deftest test-json-path-to-position-no-match () | 159 | (ert-deftest test-json-path-to-position-no-match () |
| 114 | (let* ((json-string "{\"foo\": {\"bar\": \"baz\"}}") | 160 | (let* ((json-string "{\"foo\": {\"bar\": \"baz\"}}") |
| 115 | (matched-path (json-path-to-position 5 json-string))) | 161 | (matched-path (json-path-to-position 5 json-string))) |
| 116 | (should (null matched-path)))) | 162 | (should-not matched-path))) |
| 117 | 163 | ||
| 118 | ;;; Keywords | 164 | ;;; Keywords |
| 119 | 165 | ||
| 120 | (ert-deftest test-json-read-keyword () | 166 | (ert-deftest test-json-read-keyword () |
| 121 | (json-tests--with-temp-buffer "true" | 167 | (json-tests--with-temp-buffer "true" |
| 122 | (should (json-read-keyword "true"))) | 168 | (should (eq (json-read-keyword "true") t)) |
| 169 | (should (eobp))) | ||
| 170 | (json-tests--with-temp-buffer "true " | ||
| 171 | (should (eq (json-read-keyword "true") t)) | ||
| 172 | (should (eobp))) | ||
| 173 | (json-tests--with-temp-buffer "true}" | ||
| 174 | (should (eq (json-read-keyword "true") t)) | ||
| 175 | (should (= (point) (+ (point-min) 4)))) | ||
| 176 | (json-tests--with-temp-buffer "true false" | ||
| 177 | (should (eq (json-read-keyword "true") t)) | ||
| 178 | (should (= (point) (+ (point-min) 5)))) | ||
| 179 | (json-tests--with-temp-buffer "true }" | ||
| 180 | (should (eq (json-read-keyword "true") t)) | ||
| 181 | (should (= (point) (+ (point-min) 5)))) | ||
| 182 | (json-tests--with-temp-buffer "true |" | ||
| 183 | (should (eq (json-read-keyword "true") t)) | ||
| 184 | (should (= (point) (+ (point-min) 5)))) | ||
| 185 | (json-tests--with-temp-buffer "false" | ||
| 186 | (let ((json-false 'false)) | ||
| 187 | (should (eq (json-read-keyword "false") 'false))) | ||
| 188 | (should (eobp))) | ||
| 189 | (json-tests--with-temp-buffer "null" | ||
| 190 | (let ((json-null 'null)) | ||
| 191 | (should (eq (json-read-keyword "null") 'null))) | ||
| 192 | (should (eobp)))) | ||
| 193 | |||
| 194 | (ert-deftest test-json-read-keyword-invalid () | ||
| 195 | (json-tests--with-temp-buffer "" | ||
| 196 | (should (equal (should-error (json-read-keyword "")) | ||
| 197 | '(json-unknown-keyword ""))) | ||
| 198 | (should (equal (should-error (json-read-keyword "true")) | ||
| 199 | '(json-unknown-keyword ())))) | ||
| 123 | (json-tests--with-temp-buffer "true" | 200 | (json-tests--with-temp-buffer "true" |
| 124 | (should-error | 201 | (should (equal (should-error (json-read-keyword "false")) |
| 125 | (json-read-keyword "false") :type 'json-unknown-keyword)) | 202 | '(json-unknown-keyword "true")))) |
| 126 | (json-tests--with-temp-buffer "foo" | 203 | (json-tests--with-temp-buffer "foo" |
| 127 | (should-error | 204 | (should (equal (should-error (json-read-keyword "foo")) |
| 128 | (json-read-keyword "foo") :type 'json-unknown-keyword))) | 205 | '(json-unknown-keyword "foo"))) |
| 206 | (should (equal (should-error (json-read-keyword "bar")) | ||
| 207 | '(json-unknown-keyword "bar")))) | ||
| 208 | (json-tests--with-temp-buffer " true" | ||
| 209 | (should (equal (should-error (json-read-keyword "true")) | ||
| 210 | '(json-unknown-keyword ())))) | ||
| 211 | (json-tests--with-temp-buffer "truefalse" | ||
| 212 | (should (equal (should-error (json-read-keyword "true")) | ||
| 213 | '(json-unknown-keyword "truefalse")))) | ||
| 214 | (json-tests--with-temp-buffer "true|" | ||
| 215 | (should (equal (should-error (json-read-keyword "true")) | ||
| 216 | '(json-unknown-keyword "true"))))) | ||
| 129 | 217 | ||
| 130 | (ert-deftest test-json-encode-keyword () | 218 | (ert-deftest test-json-encode-keyword () |
| 131 | (should (equal (json-encode-keyword t) "true")) | 219 | (should (equal (json-encode-keyword t) "true")) |
| 132 | (should (equal (json-encode-keyword json-false) "false")) | 220 | (let ((json-false 'false)) |
| 133 | (should (equal (json-encode-keyword json-null) "null"))) | 221 | (should (equal (json-encode-keyword 'false) "false")) |
| 222 | (should (equal (json-encode-keyword json-false) "false"))) | ||
| 223 | (let ((json-null 'null)) | ||
| 224 | (should (equal (json-encode-keyword 'null) "null")) | ||
| 225 | (should (equal (json-encode-keyword json-null) "null")))) | ||
| 134 | 226 | ||
| 135 | ;;; Numbers | 227 | ;;; Numbers |
| 136 | 228 | ||
| 137 | (ert-deftest test-json-read-number () | 229 | (ert-deftest test-json-read-integer () |
| 138 | (json-tests--with-temp-buffer "3" | 230 | (json-tests--with-temp-buffer "0 " |
| 139 | (should (= (json-read-number) 3))) | 231 | (should (= (json-read-number) 0)) |
| 140 | (json-tests--with-temp-buffer "-5" | 232 | (should (eobp))) |
| 141 | (should (= (json-read-number) -5))) | 233 | (json-tests--with-temp-buffer "-0 " |
| 142 | (json-tests--with-temp-buffer "123.456" | 234 | (should (= (json-read-number) 0)) |
| 143 | (should (= (json-read-number) 123.456))) | 235 | (should (eobp))) |
| 144 | (json-tests--with-temp-buffer "1e3" | 236 | (json-tests--with-temp-buffer "3 " |
| 145 | (should (= (json-read-number) 1e3))) | 237 | (should (= (json-read-number) 3)) |
| 146 | (json-tests--with-temp-buffer "2e+3" | 238 | (should (eobp))) |
| 147 | (should (= (json-read-number) 2e3))) | 239 | (json-tests--with-temp-buffer "-10 " |
| 148 | (json-tests--with-temp-buffer "3E3" | 240 | (should (= (json-read-number) -10)) |
| 149 | (should (= (json-read-number) 3e3))) | 241 | (should (eobp))) |
| 150 | (json-tests--with-temp-buffer "1e-7" | 242 | (json-tests--with-temp-buffer (format "%d " (1+ most-positive-fixnum)) |
| 151 | (should (= (json-read-number) 1e-7))) | 243 | (should (= (json-read-number) (1+ most-positive-fixnum))) |
| 152 | (json-tests--with-temp-buffer "abc" | 244 | (should (eobp))) |
| 153 | (should-error (json-read-number) :type 'json-number-format))) | 245 | (json-tests--with-temp-buffer (format "%d " (1- most-negative-fixnum)) |
| 246 | (should (= (json-read-number) (1- most-negative-fixnum))) | ||
| 247 | (should (eobp)))) | ||
| 248 | |||
| 249 | (ert-deftest test-json-read-fraction () | ||
| 250 | (json-tests--with-temp-buffer "0.0 " | ||
| 251 | (should (= (json-read-number) 0.0)) | ||
| 252 | (should (eobp))) | ||
| 253 | (json-tests--with-temp-buffer "-0.0 " | ||
| 254 | (should (= (json-read-number) 0.0)) | ||
| 255 | (should (eobp))) | ||
| 256 | (json-tests--with-temp-buffer "0.01 " | ||
| 257 | (should (= (json-read-number) 0.01)) | ||
| 258 | (should (eobp))) | ||
| 259 | (json-tests--with-temp-buffer "-0.01 " | ||
| 260 | (should (= (json-read-number) -0.01)) | ||
| 261 | (should (eobp))) | ||
| 262 | (json-tests--with-temp-buffer "123.456 " | ||
| 263 | (should (= (json-read-number) 123.456)) | ||
| 264 | (should (eobp))) | ||
| 265 | (json-tests--with-temp-buffer "-123.456 " | ||
| 266 | (should (= (json-read-number) -123.456)) | ||
| 267 | (should (eobp)))) | ||
| 268 | |||
| 269 | (ert-deftest test-json-read-exponent () | ||
| 270 | (json-tests--with-temp-buffer "0e0 " | ||
| 271 | (should (= (json-read-number) 0e0)) | ||
| 272 | (should (eobp))) | ||
| 273 | (json-tests--with-temp-buffer "-0E0 " | ||
| 274 | (should (= (json-read-number) 0e0)) | ||
| 275 | (should (eobp))) | ||
| 276 | (json-tests--with-temp-buffer "-0E+0 " | ||
| 277 | (should (= (json-read-number) 0e0)) | ||
| 278 | (should (eobp))) | ||
| 279 | (json-tests--with-temp-buffer "0e-0 " | ||
| 280 | (should (= (json-read-number) 0e0)) | ||
| 281 | (should (eobp))) | ||
| 282 | (json-tests--with-temp-buffer "12e34 " | ||
| 283 | (should (= (json-read-number) 12e34)) | ||
| 284 | (should (eobp))) | ||
| 285 | (json-tests--with-temp-buffer "-12E34 " | ||
| 286 | (should (= (json-read-number) -12e34)) | ||
| 287 | (should (eobp))) | ||
| 288 | (json-tests--with-temp-buffer "-12E+34 " | ||
| 289 | (should (= (json-read-number) -12e34)) | ||
| 290 | (should (eobp))) | ||
| 291 | (json-tests--with-temp-buffer "12e-34 " | ||
| 292 | (should (= (json-read-number) 12e-34)) | ||
| 293 | (should (eobp)))) | ||
| 294 | |||
| 295 | (ert-deftest test-json-read-fraction-exponent () | ||
| 296 | (json-tests--with-temp-buffer "0.0e0 " | ||
| 297 | (should (= (json-read-number) 0.0e0)) | ||
| 298 | (should (eobp))) | ||
| 299 | (json-tests--with-temp-buffer "-0.0E0 " | ||
| 300 | (should (= (json-read-number) 0.0e0)) | ||
| 301 | (should (eobp))) | ||
| 302 | (json-tests--with-temp-buffer "0.12E-0 " | ||
| 303 | (should (= (json-read-number) 0.12e0)) | ||
| 304 | (should (eobp))) | ||
| 305 | (json-tests--with-temp-buffer "-12.34e+56 " | ||
| 306 | (should (= (json-read-number) -12.34e+56)) | ||
| 307 | (should (eobp)))) | ||
| 308 | |||
| 309 | (ert-deftest test-json-read-number-invalid () | ||
| 310 | (cl-flet ((read (str) | ||
| 311 | ;; Return error and point resulting from reading STR. | ||
| 312 | (json-tests--with-temp-buffer str | ||
| 313 | (cons (should-error (json-read-number)) (point))))) | ||
| 314 | ;; POS is where each of its STRINGS becomes invalid. | ||
| 315 | (pcase-dolist (`(,pos . ,strings) | ||
| 316 | '((1 "" "+" "-" "." "e" "e1" "abc" "++0" "++1" | ||
| 317 | "+0" "+0.0" "+12" "+12.34" "+12.34e56" | ||
| 318 | ".0" "+.0" "-.0" ".12" "+.12" "-.12" | ||
| 319 | ".e0" "+.e0" "-.e0" ".0e0" "+.0e0" "-.0e0") | ||
| 320 | (2 "01" "1ee1" "1e++1") | ||
| 321 | (3 "-01") | ||
| 322 | (4 "0.0.0" "1.1.1" "1e1e1") | ||
| 323 | (5 "-0.0.0" "-1.1.1"))) | ||
| 324 | ;; Expected error and point. | ||
| 325 | (let ((res `((json-number-format ,pos) . ,pos))) | ||
| 326 | (dolist (str strings) | ||
| 327 | (should (equal (read str) res))))))) | ||
| 154 | 328 | ||
| 155 | (ert-deftest test-json-encode-number () | 329 | (ert-deftest test-json-encode-number () |
| 330 | (should (equal (json-encode-number 0) "0")) | ||
| 331 | (should (equal (json-encode-number -0) "0")) | ||
| 156 | (should (equal (json-encode-number 3) "3")) | 332 | (should (equal (json-encode-number 3) "3")) |
| 157 | (should (equal (json-encode-number -5) "-5")) | 333 | (should (equal (json-encode-number -5) "-5")) |
| 158 | (should (equal (json-encode-number 123.456) "123.456"))) | 334 | (should (equal (json-encode-number 123.456) "123.456")) |
| 335 | (let ((bignum (1+ most-positive-fixnum))) | ||
| 336 | (should (equal (json-encode-number bignum) | ||
| 337 | (number-to-string bignum))))) | ||
| 159 | 338 | ||
| 160 | ;; Strings | 339 | ;;; Strings |
| 161 | 340 | ||
| 162 | (ert-deftest test-json-read-escaped-char () | 341 | (ert-deftest test-json-read-escaped-char () |
| 163 | (json-tests--with-temp-buffer "\\\"" | 342 | (json-tests--with-temp-buffer "\\\"" |
| 164 | (should (equal (json-read-escaped-char) ?\")))) | 343 | (should (= (json-read-escaped-char) ?\")) |
| 344 | (should (eobp))) | ||
| 345 | (json-tests--with-temp-buffer "\\\\ " | ||
| 346 | (should (= (json-read-escaped-char) ?\\)) | ||
| 347 | (should (= (point) (+ (point-min) 2)))) | ||
| 348 | (json-tests--with-temp-buffer "\\b " | ||
| 349 | (should (= (json-read-escaped-char) ?\b)) | ||
| 350 | (should (= (point) (+ (point-min) 2)))) | ||
| 351 | (json-tests--with-temp-buffer "\\f " | ||
| 352 | (should (= (json-read-escaped-char) ?\f)) | ||
| 353 | (should (= (point) (+ (point-min) 2)))) | ||
| 354 | (json-tests--with-temp-buffer "\\n " | ||
| 355 | (should (= (json-read-escaped-char) ?\n)) | ||
| 356 | (should (= (point) (+ (point-min) 2)))) | ||
| 357 | (json-tests--with-temp-buffer "\\r " | ||
| 358 | (should (= (json-read-escaped-char) ?\r)) | ||
| 359 | (should (= (point) (+ (point-min) 2)))) | ||
| 360 | (json-tests--with-temp-buffer "\\t " | ||
| 361 | (should (= (json-read-escaped-char) ?\t)) | ||
| 362 | (should (= (point) (+ (point-min) 2)))) | ||
| 363 | (json-tests--with-temp-buffer "\\x " | ||
| 364 | (should (= (json-read-escaped-char) ?x)) | ||
| 365 | (should (= (point) (+ (point-min) 2)))) | ||
| 366 | (json-tests--with-temp-buffer "\\ud800\\uDC00 " | ||
| 367 | (should (= (json-read-escaped-char) #x10000)) | ||
| 368 | (should (= (point) (+ (point-min) 12)))) | ||
| 369 | (json-tests--with-temp-buffer "\\ud7ff\\udc00 " | ||
| 370 | (should (= (json-read-escaped-char) #xd7ff)) | ||
| 371 | (should (= (point) (+ (point-min) 6)))) | ||
| 372 | (json-tests--with-temp-buffer "\\uffff " | ||
| 373 | (should (= (json-read-escaped-char) #xffff)) | ||
| 374 | (should (= (point) (+ (point-min) 6)))) | ||
| 375 | (json-tests--with-temp-buffer "\\ufffff " | ||
| 376 | (should (= (json-read-escaped-char) #xffff)) | ||
| 377 | (should (= (point) (+ (point-min) 6))))) | ||
| 378 | |||
| 379 | (ert-deftest test-json-read-escaped-char-invalid () | ||
| 380 | (json-tests--with-temp-buffer "" | ||
| 381 | (should-error (json-read-escaped-char))) | ||
| 382 | (json-tests--with-temp-buffer "\\" | ||
| 383 | (should-error (json-read-escaped-char) :type 'json-end-of-file)) | ||
| 384 | (json-tests--with-temp-buffer "\\ufff " | ||
| 385 | (should (equal (should-error (json-read-escaped-char)) | ||
| 386 | (list 'json-string-escape (+ (point-min) 2))))) | ||
| 387 | (json-tests--with-temp-buffer "\\ufffg " | ||
| 388 | (should (equal (should-error (json-read-escaped-char)) | ||
| 389 | (list 'json-string-escape (+ (point-min) 2)))))) | ||
| 165 | 390 | ||
| 166 | (ert-deftest test-json-read-string () | 391 | (ert-deftest test-json-read-string () |
| 392 | (json-tests--with-temp-buffer "" | ||
| 393 | (should-error (json-read-string))) | ||
| 167 | (json-tests--with-temp-buffer "\"formfeed\f\"" | 394 | (json-tests--with-temp-buffer "\"formfeed\f\"" |
| 168 | (should-error (json-read-string) :type 'json-string-format)) | 395 | (should (equal (should-error (json-read-string)) |
| 396 | '(json-string-format ?\f)))) | ||
| 397 | (json-tests--with-temp-buffer "\"\"" | ||
| 398 | (should (equal (json-read-string) ""))) | ||
| 169 | (json-tests--with-temp-buffer "\"foo \\\"bar\\\"\"" | 399 | (json-tests--with-temp-buffer "\"foo \\\"bar\\\"\"" |
| 170 | (should (equal (json-read-string) "foo \"bar\""))) | 400 | (should (equal (json-read-string) "foo \"bar\""))) |
| 171 | (json-tests--with-temp-buffer "\"abcαβγ\"" | 401 | (json-tests--with-temp-buffer "\"abcαβγ\"" |
| @@ -175,57 +405,117 @@ Point is moved to beginning of the buffer." | |||
| 175 | ;; Bug#24784 | 405 | ;; Bug#24784 |
| 176 | (json-tests--with-temp-buffer "\"\\uD834\\uDD1E\"" | 406 | (json-tests--with-temp-buffer "\"\\uD834\\uDD1E\"" |
| 177 | (should (equal (json-read-string) "\U0001D11E"))) | 407 | (should (equal (json-read-string) "\U0001D11E"))) |
| 408 | (json-tests--with-temp-buffer "f" | ||
| 409 | (should-error (json-read-string) :type 'json-end-of-file)) | ||
| 178 | (json-tests--with-temp-buffer "foo" | 410 | (json-tests--with-temp-buffer "foo" |
| 179 | (should-error (json-read-string) :type 'json-string-format))) | 411 | (should-error (json-read-string) :type 'json-end-of-file))) |
| 180 | 412 | ||
| 181 | (ert-deftest test-json-encode-string () | 413 | (ert-deftest test-json-encode-string () |
| 414 | (should (equal (json-encode-string "") "\"\"")) | ||
| 415 | (should (equal (json-encode-string "a") "\"a\"")) | ||
| 182 | (should (equal (json-encode-string "foo") "\"foo\"")) | 416 | (should (equal (json-encode-string "foo") "\"foo\"")) |
| 183 | (should (equal (json-encode-string "a\n\fb") "\"a\\n\\fb\"")) | 417 | (should (equal (json-encode-string "a\n\fb") "\"a\\n\\fb\"")) |
| 184 | (should (equal (json-encode-string "\nasdфыв\u001f\u007ffgh\t") | 418 | (should (equal (json-encode-string "\nasdфыв\u001f\u007ffgh\t") |
| 185 | "\"\\nasdфыв\\u001f\u007ffgh\\t\""))) | 419 | "\"\\nasdфыв\\u001f\u007ffgh\\t\""))) |
| 186 | 420 | ||
| 187 | (ert-deftest test-json-encode-key () | 421 | (ert-deftest test-json-encode-key () |
| 422 | (should (equal (json-encode-key "") "\"\"")) | ||
| 423 | (should (equal (json-encode-key '##) "\"\"")) | ||
| 424 | (should (equal (json-encode-key :) "\"\"")) | ||
| 188 | (should (equal (json-encode-key "foo") "\"foo\"")) | 425 | (should (equal (json-encode-key "foo") "\"foo\"")) |
| 189 | (should (equal (json-encode-key 'foo) "\"foo\"")) | 426 | (should (equal (json-encode-key 'foo) "\"foo\"")) |
| 190 | (should (equal (json-encode-key :foo) "\"foo\"")) | 427 | (should (equal (json-encode-key :foo) "\"foo\"")) |
| 191 | (should-error (json-encode-key 5) :type 'json-key-format) | 428 | (should (equal (should-error (json-encode-key 5)) |
| 192 | (should-error (json-encode-key ["foo"]) :type 'json-key-format) | 429 | '(json-key-format 5))) |
| 193 | (should-error (json-encode-key '("foo")) :type 'json-key-format)) | 430 | (should (equal (should-error (json-encode-key ["foo"])) |
| 431 | '(json-key-format ["foo"]))) | ||
| 432 | (should (equal (should-error (json-encode-key '("foo"))) | ||
| 433 | '(json-key-format ("foo"))))) | ||
| 194 | 434 | ||
| 195 | ;;; Objects | 435 | ;;; Objects |
| 196 | 436 | ||
| 197 | (ert-deftest test-json-new-object () | 437 | (ert-deftest test-json-new-object () |
| 198 | (let ((json-object-type 'alist)) | 438 | (let ((json-object-type 'alist)) |
| 199 | (should (equal (json-new-object) '()))) | 439 | (should-not (json-new-object))) |
| 200 | (let ((json-object-type 'plist)) | 440 | (let ((json-object-type 'plist)) |
| 201 | (should (equal (json-new-object) '()))) | 441 | (should-not (json-new-object))) |
| 202 | (let* ((json-object-type 'hash-table) | 442 | (let* ((json-object-type 'hash-table) |
| 203 | (json-object (json-new-object))) | 443 | (json-object (json-new-object))) |
| 204 | (should (hash-table-p json-object)) | 444 | (should (hash-table-p json-object)) |
| 205 | (should (= (hash-table-count json-object) 0)))) | 445 | (should (map-empty-p json-object)) |
| 446 | (should (eq (hash-table-test json-object) #'equal)))) | ||
| 206 | 447 | ||
| 207 | (ert-deftest test-json-add-to-object () | 448 | (ert-deftest test-json-add-to-alist () |
| 208 | (let* ((json-object-type 'alist) | 449 | (let* ((json-object-type 'alist) |
| 209 | (json-key-type nil) | ||
| 210 | (obj (json-new-object))) | 450 | (obj (json-new-object))) |
| 211 | (setq obj (json-add-to-object obj "a" 1)) | 451 | (let ((json-key-type nil)) |
| 212 | (setq obj (json-add-to-object obj "b" 2)) | 452 | (setq obj (json-add-to-object obj "a" 1)) |
| 213 | (should (equal (assq 'a obj) '(a . 1))) | 453 | (setq obj (json-add-to-object obj "b" 2)) |
| 214 | (should (equal (assq 'b obj) '(b . 2)))) | 454 | (should (equal (assq 'a obj) '(a . 1))) |
| 455 | (should (equal (assq 'b obj) '(b . 2)))) | ||
| 456 | (let ((json-key-type 'symbol)) | ||
| 457 | (setq obj (json-add-to-object obj "c" 3)) | ||
| 458 | (setq obj (json-add-to-object obj "d" 4)) | ||
| 459 | (should (equal (assq 'c obj) '(c . 3))) | ||
| 460 | (should (equal (assq 'd obj) '(d . 4)))) | ||
| 461 | (let ((json-key-type 'keyword)) | ||
| 462 | (setq obj (json-add-to-object obj "e" 5)) | ||
| 463 | (setq obj (json-add-to-object obj "f" 6)) | ||
| 464 | (should (equal (assq :e obj) '(:e . 5))) | ||
| 465 | (should (equal (assq :f obj) '(:f . 6)))) | ||
| 466 | (let ((json-key-type 'string)) | ||
| 467 | (setq obj (json-add-to-object obj "g" 7)) | ||
| 468 | (setq obj (json-add-to-object obj "h" 8)) | ||
| 469 | (should (equal (assoc "g" obj) '("g" . 7))) | ||
| 470 | (should (equal (assoc "h" obj) '("h" . 8)))))) | ||
| 471 | |||
| 472 | (ert-deftest test-json-add-to-plist () | ||
| 215 | (let* ((json-object-type 'plist) | 473 | (let* ((json-object-type 'plist) |
| 216 | (json-key-type nil) | ||
| 217 | (obj (json-new-object))) | 474 | (obj (json-new-object))) |
| 218 | (setq obj (json-add-to-object obj "a" 1)) | 475 | (let ((json-key-type nil)) |
| 219 | (setq obj (json-add-to-object obj "b" 2)) | 476 | (setq obj (json-add-to-object obj "a" 1)) |
| 220 | (should (= (plist-get obj :a) 1)) | 477 | (setq obj (json-add-to-object obj "b" 2)) |
| 221 | (should (= (plist-get obj :b) 2))) | 478 | (should (= (plist-get obj :a) 1)) |
| 479 | (should (= (plist-get obj :b) 2))) | ||
| 480 | (let ((json-key-type 'keyword)) | ||
| 481 | (setq obj (json-add-to-object obj "c" 3)) | ||
| 482 | (setq obj (json-add-to-object obj "d" 4)) | ||
| 483 | (should (= (plist-get obj :c) 3)) | ||
| 484 | (should (= (plist-get obj :d) 4))) | ||
| 485 | (let ((json-key-type 'symbol)) | ||
| 486 | (setq obj (json-add-to-object obj "e" 5)) | ||
| 487 | (setq obj (json-add-to-object obj "f" 6)) | ||
| 488 | (should (= (plist-get obj 'e) 5)) | ||
| 489 | (should (= (plist-get obj 'f) 6))) | ||
| 490 | (let ((json-key-type 'string)) | ||
| 491 | (setq obj (json-add-to-object obj "g" 7)) | ||
| 492 | (setq obj (json-add-to-object obj "h" 8)) | ||
| 493 | (should (= (lax-plist-get obj "g") 7)) | ||
| 494 | (should (= (lax-plist-get obj "h") 8))))) | ||
| 495 | |||
| 496 | (ert-deftest test-json-add-to-hash-table () | ||
| 222 | (let* ((json-object-type 'hash-table) | 497 | (let* ((json-object-type 'hash-table) |
| 223 | (json-key-type nil) | ||
| 224 | (obj (json-new-object))) | 498 | (obj (json-new-object))) |
| 225 | (setq obj (json-add-to-object obj "a" 1)) | 499 | (let ((json-key-type nil)) |
| 226 | (setq obj (json-add-to-object obj "b" 2)) | 500 | (setq obj (json-add-to-object obj "a" 1)) |
| 227 | (should (= (gethash "a" obj) 1)) | 501 | (setq obj (json-add-to-object obj "b" 2)) |
| 228 | (should (= (gethash "b" obj) 2)))) | 502 | (should (= (gethash "a" obj) 1)) |
| 503 | (should (= (gethash "b" obj) 2))) | ||
| 504 | (let ((json-key-type 'string)) | ||
| 505 | (setq obj (json-add-to-object obj "c" 3)) | ||
| 506 | (setq obj (json-add-to-object obj "d" 4)) | ||
| 507 | (should (= (gethash "c" obj) 3)) | ||
| 508 | (should (= (gethash "d" obj) 4))) | ||
| 509 | (let ((json-key-type 'symbol)) | ||
| 510 | (setq obj (json-add-to-object obj "e" 5)) | ||
| 511 | (setq obj (json-add-to-object obj "f" 6)) | ||
| 512 | (should (= (gethash 'e obj) 5)) | ||
| 513 | (should (= (gethash 'f obj) 6))) | ||
| 514 | (let ((json-key-type 'keyword)) | ||
| 515 | (setq obj (json-add-to-object obj "g" 7)) | ||
| 516 | (setq obj (json-add-to-object obj "h" 8)) | ||
| 517 | (should (= (gethash :g obj) 7)) | ||
| 518 | (should (= (gethash :h obj) 8))))) | ||
| 229 | 519 | ||
| 230 | (ert-deftest test-json-read-object () | 520 | (ert-deftest test-json-read-object () |
| 231 | (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }" | 521 | (json-tests--with-temp-buffer "{ \"a\": 1, \"b\": 2 }" |
| @@ -238,94 +528,384 @@ Point is moved to beginning of the buffer." | |||
| 238 | (let* ((json-object-type 'hash-table) | 528 | (let* ((json-object-type 'hash-table) |
| 239 | (hash-table (json-read-object))) | 529 | (hash-table (json-read-object))) |
| 240 | (should (= (gethash "a" hash-table) 1)) | 530 | (should (= (gethash "a" hash-table) 1)) |
| 241 | (should (= (gethash "b" hash-table) 2)))) | 531 | (should (= (gethash "b" hash-table) 2))))) |
| 532 | |||
| 533 | (ert-deftest test-json-read-object-empty () | ||
| 534 | (json-tests--with-temp-buffer "{}" | ||
| 535 | (let ((json-object-type 'alist)) | ||
| 536 | (should-not (save-excursion (json-read-object)))) | ||
| 537 | (let ((json-object-type 'plist)) | ||
| 538 | (should-not (save-excursion (json-read-object)))) | ||
| 539 | (let* ((json-object-type 'hash-table) | ||
| 540 | (hash-table (json-read-object))) | ||
| 541 | (should (hash-table-p hash-table)) | ||
| 542 | (should (map-empty-p hash-table))))) | ||
| 543 | |||
| 544 | (ert-deftest test-json-read-object-invalid () | ||
| 545 | (json-tests--with-temp-buffer "{ \"a\" 1, \"b\": 2 }" | ||
| 546 | (should (equal (should-error (json-read-object)) | ||
| 547 | '(json-object-format ":" ?1)))) | ||
| 242 | (json-tests--with-temp-buffer "{ \"a\": 1 \"b\": 2 }" | 548 | (json-tests--with-temp-buffer "{ \"a\": 1 \"b\": 2 }" |
| 243 | (should-error (json-read-object) :type 'json-object-format))) | 549 | (should (equal (should-error (json-read-object)) |
| 550 | '(json-object-format "," ?\"))))) | ||
| 551 | |||
| 552 | (ert-deftest test-json-read-object-function () | ||
| 553 | (let* ((pre nil) | ||
| 554 | (post nil) | ||
| 555 | (keys '("b" "a")) | ||
| 556 | (json-pre-element-read-function | ||
| 557 | (lambda (key) | ||
| 558 | (setq pre 'pre) | ||
| 559 | (should (equal key (pop keys))))) | ||
| 560 | (json-post-element-read-function | ||
| 561 | (lambda () (setq post 'post)))) | ||
| 562 | (json-tests--with-temp-buffer "{ \"b\": 2, \"a\": 1 }" | ||
| 563 | (json-read-object) | ||
| 564 | (should (eq pre 'pre)) | ||
| 565 | (should (eq post 'post))))) | ||
| 244 | 566 | ||
| 245 | (ert-deftest test-json-encode-hash-table () | 567 | (ert-deftest test-json-encode-hash-table () |
| 246 | (let ((hash-table (make-hash-table)) | 568 | (let ((json-encoding-object-sort-predicate nil) |
| 247 | (json-encoding-object-sort-predicate 'string<) | ||
| 248 | (json-encoding-pretty-print nil)) | 569 | (json-encoding-pretty-print nil)) |
| 249 | (puthash :a 1 hash-table) | 570 | (should (equal (json-encode-hash-table #s(hash-table)) "{}")) |
| 250 | (puthash :b 2 hash-table) | 571 | (should (equal (json-encode-hash-table #s(hash-table data (a 1))) |
| 251 | (puthash :c 3 hash-table) | 572 | "{\"a\":1}")) |
| 252 | (should (equal (json-encode hash-table) | 573 | (should (member (json-encode-hash-table #s(hash-table data (b 2 a 1))) |
| 253 | "{\"a\":1,\"b\":2,\"c\":3}")))) | 574 | '("{\"a\":1,\"b\":2}" "{\"b\":2,\"a\":1}"))) |
| 254 | 575 | (should (member (json-encode-hash-table #s(hash-table data (c 3 b 2 a 1))) | |
| 255 | (ert-deftest json-encode-simple-alist () | 576 | '("{\"a\":1,\"b\":2,\"c\":3}" |
| 256 | (let ((json-encoding-pretty-print nil)) | 577 | "{\"a\":1,\"c\":3,\"b\":2}" |
| 257 | (should (equal (json-encode '((a . 1) (b . 2))) | 578 | "{\"b\":2,\"a\":1,\"c\":3}" |
| 258 | "{\"a\":1,\"b\":2}")))) | 579 | "{\"b\":2,\"c\":3,\"a\":1}" |
| 259 | 580 | "{\"c\":3,\"a\":1,\"b\":2}" | |
| 260 | (ert-deftest test-json-encode-plist () | 581 | "{\"c\":3,\"b\":2,\"a\":1}"))))) |
| 261 | (let ((plist '(:a 1 :b 2)) | 582 | |
| 583 | (ert-deftest test-json-encode-hash-table-pretty () | ||
| 584 | (let ((json-encoding-object-sort-predicate nil) | ||
| 585 | (json-encoding-pretty-print t) | ||
| 586 | (json-encoding-default-indentation " ") | ||
| 587 | (json-encoding-lisp-style-closings nil)) | ||
| 588 | (should (equal (json-encode-hash-table #s(hash-table)) "{}")) | ||
| 589 | (should (equal (json-encode-hash-table #s(hash-table data (a 1))) | ||
| 590 | "{\n \"a\": 1\n}")) | ||
| 591 | (should (member (json-encode-hash-table #s(hash-table data (b 2 a 1))) | ||
| 592 | '("{\n \"a\": 1,\n \"b\": 2\n}" | ||
| 593 | "{\n \"b\": 2,\n \"a\": 1\n}"))) | ||
| 594 | (should (member (json-encode-hash-table #s(hash-table data (c 3 b 2 a 1))) | ||
| 595 | '("{\n \"a\": 1,\n \"b\": 2,\n \"c\": 3\n}" | ||
| 596 | "{\n \"a\": 1,\n \"c\": 3,\n \"b\": 2\n}" | ||
| 597 | "{\n \"b\": 2,\n \"a\": 1,\n \"c\": 3\n}" | ||
| 598 | "{\n \"b\": 2,\n \"c\": 3,\n \"a\": 1\n}" | ||
| 599 | "{\n \"c\": 3,\n \"a\": 1,\n \"b\": 2\n}" | ||
| 600 | "{\n \"c\": 3,\n \"b\": 2,\n \"a\": 1\n}"))))) | ||
| 601 | |||
| 602 | (ert-deftest test-json-encode-hash-table-lisp-style () | ||
| 603 | (let ((json-encoding-object-sort-predicate nil) | ||
| 604 | (json-encoding-pretty-print t) | ||
| 605 | (json-encoding-default-indentation " ") | ||
| 606 | (json-encoding-lisp-style-closings t)) | ||
| 607 | (should (equal (json-encode-hash-table #s(hash-table)) "{}")) | ||
| 608 | (should (equal (json-encode-hash-table #s(hash-table data (a 1))) | ||
| 609 | "{\n \"a\": 1}")) | ||
| 610 | (should (member (json-encode-hash-table #s(hash-table data (b 2 a 1))) | ||
| 611 | '("{\n \"a\": 1,\n \"b\": 2}" | ||
| 612 | "{\n \"b\": 2,\n \"a\": 1}"))) | ||
| 613 | (should (member (json-encode-hash-table #s(hash-table data (c 3 b 2 a 1))) | ||
| 614 | '("{\n \"a\": 1,\n \"b\": 2,\n \"c\": 3}" | ||
| 615 | "{\n \"a\": 1,\n \"c\": 3,\n \"b\": 2}" | ||
| 616 | "{\n \"b\": 2,\n \"a\": 1,\n \"c\": 3}" | ||
| 617 | "{\n \"b\": 2,\n \"c\": 3,\n \"a\": 1}" | ||
| 618 | "{\n \"c\": 3,\n \"a\": 1,\n \"b\": 2}" | ||
| 619 | "{\n \"c\": 3,\n \"b\": 2,\n \"a\": 1}"))))) | ||
| 620 | |||
| 621 | (ert-deftest test-json-encode-hash-table-sort () | ||
| 622 | (let ((json-encoding-object-sort-predicate #'string<) | ||
| 262 | (json-encoding-pretty-print nil)) | 623 | (json-encoding-pretty-print nil)) |
| 263 | (should (equal (json-encode plist) "{\"a\":1,\"b\":2}")))) | 624 | (pcase-dolist (`(,in . ,out) |
| 264 | 625 | '((#s(hash-table) . "{}") | |
| 265 | (ert-deftest test-json-encode-plist-with-sort-predicate () | 626 | (#s(hash-table data (a 1)) . "{\"a\":1}") |
| 266 | (let ((plist '(:c 3 :a 1 :b 2)) | 627 | (#s(hash-table data (b 2 a 1)) . "{\"a\":1,\"b\":2}") |
| 267 | (json-encoding-object-sort-predicate 'string<) | 628 | (#s(hash-table data (c 3 b 2 a 1)) |
| 629 | . "{\"a\":1,\"b\":2,\"c\":3}"))) | ||
| 630 | (let ((copy (map-pairs in))) | ||
| 631 | (should (equal (json-encode-hash-table in) out)) | ||
| 632 | ;; Ensure sorting isn't destructive. | ||
| 633 | (should (seq-set-equal-p (map-pairs in) copy)))))) | ||
| 634 | |||
| 635 | (ert-deftest test-json-encode-alist () | ||
| 636 | (let ((json-encoding-object-sort-predicate nil) | ||
| 268 | (json-encoding-pretty-print nil)) | 637 | (json-encoding-pretty-print nil)) |
| 269 | (should (equal (json-encode plist) "{\"a\":1,\"b\":2,\"c\":3}")))) | 638 | (should (equal (json-encode-alist ()) "{}")) |
| 639 | (should (equal (json-encode-alist '((a . 1))) "{\"a\":1}")) | ||
| 640 | (should (equal (json-encode-alist '((b . 2) (a . 1))) "{\"b\":2,\"a\":1}")) | ||
| 641 | (should (equal (json-encode-alist '((c . 3) (b . 2) (a . 1))) | ||
| 642 | "{\"c\":3,\"b\":2,\"a\":1}")))) | ||
| 643 | |||
| 644 | (ert-deftest test-json-encode-alist-pretty () | ||
| 645 | (let ((json-encoding-object-sort-predicate nil) | ||
| 646 | (json-encoding-pretty-print t) | ||
| 647 | (json-encoding-default-indentation " ") | ||
| 648 | (json-encoding-lisp-style-closings nil)) | ||
| 649 | (should (equal (json-encode-alist ()) "{}")) | ||
| 650 | (should (equal (json-encode-alist '((a . 1))) "{\n \"a\": 1\n}")) | ||
| 651 | (should (equal (json-encode-alist '((b . 2) (a . 1))) | ||
| 652 | "{\n \"b\": 2,\n \"a\": 1\n}")) | ||
| 653 | (should (equal (json-encode-alist '((c . 3) (b . 2) (a . 1))) | ||
| 654 | "{\n \"c\": 3,\n \"b\": 2,\n \"a\": 1\n}")))) | ||
| 655 | |||
| 656 | (ert-deftest test-json-encode-alist-lisp-style () | ||
| 657 | (let ((json-encoding-object-sort-predicate nil) | ||
| 658 | (json-encoding-pretty-print t) | ||
| 659 | (json-encoding-default-indentation " ") | ||
| 660 | (json-encoding-lisp-style-closings t)) | ||
| 661 | (should (equal (json-encode-alist ()) "{}")) | ||
| 662 | (should (equal (json-encode-alist '((a . 1))) "{\n \"a\": 1}")) | ||
| 663 | (should (equal (json-encode-alist '((b . 2) (a . 1))) | ||
| 664 | "{\n \"b\": 2,\n \"a\": 1}")) | ||
| 665 | (should (equal (json-encode-alist '((c . 3) (b . 2) (a . 1))) | ||
| 666 | "{\n \"c\": 3,\n \"b\": 2,\n \"a\": 1}")))) | ||
| 667 | |||
| 668 | (ert-deftest test-json-encode-alist-sort () | ||
| 669 | (let ((json-encoding-object-sort-predicate #'string<) | ||
| 670 | (json-encoding-pretty-print nil)) | ||
| 671 | (pcase-dolist (`(,in . ,out) | ||
| 672 | '((() . "{}") | ||
| 673 | (((a . 1)) . "{\"a\":1}") | ||
| 674 | (((b . 2) (a . 1)) . "{\"a\":1,\"b\":2}") | ||
| 675 | (((c . 3) (b . 2) (a . 1)) | ||
| 676 | . "{\"a\":1,\"b\":2,\"c\":3}"))) | ||
| 677 | (let ((copy (copy-alist in))) | ||
| 678 | (should (equal (json-encode-alist in) out)) | ||
| 679 | ;; Ensure sorting isn't destructive (bug#40693). | ||
| 680 | (should (equal in copy)))))) | ||
| 270 | 681 | ||
| 271 | (ert-deftest test-json-encode-alist-with-sort-predicate () | 682 | (ert-deftest test-json-encode-plist () |
| 272 | (let ((alist '((:c . 3) (:a . 1) (:b . 2))) | 683 | (let ((json-encoding-object-sort-predicate nil) |
| 273 | (json-encoding-object-sort-predicate 'string<) | ||
| 274 | (json-encoding-pretty-print nil)) | 684 | (json-encoding-pretty-print nil)) |
| 275 | (should (equal (json-encode alist) "{\"a\":1,\"b\":2,\"c\":3}")))) | 685 | (should (equal (json-encode-plist ()) "{}")) |
| 686 | (should (equal (json-encode-plist '(:a 1)) "{\"a\":1}")) | ||
| 687 | (should (equal (json-encode-plist '(:b 2 :a 1)) "{\"b\":2,\"a\":1}")) | ||
| 688 | (should (equal (json-encode-plist '(:c 3 :b 2 :a 1)) | ||
| 689 | "{\"c\":3,\"b\":2,\"a\":1}")))) | ||
| 690 | |||
| 691 | (ert-deftest test-json-encode-plist-pretty () | ||
| 692 | (let ((json-encoding-object-sort-predicate nil) | ||
| 693 | (json-encoding-pretty-print t) | ||
| 694 | (json-encoding-default-indentation " ") | ||
| 695 | (json-encoding-lisp-style-closings nil)) | ||
| 696 | (should (equal (json-encode-plist ()) "{}")) | ||
| 697 | (should (equal (json-encode-plist '(:a 1)) "{\n \"a\": 1\n}")) | ||
| 698 | (should (equal (json-encode-plist '(:b 2 :a 1)) | ||
| 699 | "{\n \"b\": 2,\n \"a\": 1\n}")) | ||
| 700 | (should (equal (json-encode-plist '(:c 3 :b 2 :a 1)) | ||
| 701 | "{\n \"c\": 3,\n \"b\": 2,\n \"a\": 1\n}")))) | ||
| 702 | |||
| 703 | (ert-deftest test-json-encode-plist-lisp-style () | ||
| 704 | (let ((json-encoding-object-sort-predicate nil) | ||
| 705 | (json-encoding-pretty-print t) | ||
| 706 | (json-encoding-default-indentation " ") | ||
| 707 | (json-encoding-lisp-style-closings t)) | ||
| 708 | (should (equal (json-encode-plist ()) "{}")) | ||
| 709 | (should (equal (json-encode-plist '(:a 1)) "{\n \"a\": 1}")) | ||
| 710 | (should (equal (json-encode-plist '(:b 2 :a 1)) | ||
| 711 | "{\n \"b\": 2,\n \"a\": 1}")) | ||
| 712 | (should (equal (json-encode-plist '(:c 3 :b 2 :a 1)) | ||
| 713 | "{\n \"c\": 3,\n \"b\": 2,\n \"a\": 1}")))) | ||
| 714 | |||
| 715 | (ert-deftest test-json-encode-plist-sort () | ||
| 716 | (let ((json-encoding-object-sort-predicate #'string<) | ||
| 717 | (json-encoding-pretty-print nil)) | ||
| 718 | (pcase-dolist (`(,in . ,out) | ||
| 719 | '((() . "{}") | ||
| 720 | ((:a 1) . "{\"a\":1}") | ||
| 721 | ((:b 2 :a 1) . "{\"a\":1,\"b\":2}") | ||
| 722 | ((:c 3 :b 2 :a 1) . "{\"a\":1,\"b\":2,\"c\":3}"))) | ||
| 723 | (let ((copy (copy-sequence in))) | ||
| 724 | (should (equal (json-encode-plist in) out)) | ||
| 725 | ;; Ensure sorting isn't destructive. | ||
| 726 | (should (equal in copy)))))) | ||
| 276 | 727 | ||
| 277 | (ert-deftest test-json-encode-list () | 728 | (ert-deftest test-json-encode-list () |
| 278 | (let ((json-encoding-pretty-print nil)) | 729 | (let ((json-encoding-object-sort-predicate nil) |
| 279 | (should (equal (json-encode-list '(:a 1 :b 2)) | 730 | (json-encoding-pretty-print nil)) |
| 280 | "{\"a\":1,\"b\":2}")) | 731 | (should (equal (json-encode-list ()) "{}")) |
| 281 | (should (equal (json-encode-list '((:a . 1) (:b . 2))) | 732 | (should (equal (json-encode-list '(a)) "[\"a\"]")) |
| 282 | "{\"a\":1,\"b\":2}")) | 733 | (should (equal (json-encode-list '(:a)) "[\"a\"]")) |
| 283 | (should (equal (json-encode-list '(1 2 3 4)) "[1,2,3,4]")))) | 734 | (should (equal (json-encode-list '("a")) "[\"a\"]")) |
| 735 | (should (equal (json-encode-list '(a 1)) "[\"a\",1]")) | ||
| 736 | (should (equal (json-encode-list '("a" 1)) "[\"a\",1]")) | ||
| 737 | (should (equal (json-encode-list '(:a 1)) "{\"a\":1}")) | ||
| 738 | (should (equal (json-encode-list '((a . 1))) "{\"a\":1}")) | ||
| 739 | (should (equal (json-encode-list '((:a . 1))) "{\"a\":1}")) | ||
| 740 | (should (equal (json-encode-list '(:b 2 :a)) "[\"b\",2,\"a\"]")) | ||
| 741 | (should (equal (json-encode-list '(4 3 2 1)) "[4,3,2,1]")) | ||
| 742 | (should (equal (json-encode-list '(b 2 a 1)) "[\"b\",2,\"a\",1]")) | ||
| 743 | (should (equal (json-encode-list '(:b 2 :a 1)) "{\"b\":2,\"a\":1}")) | ||
| 744 | (should (equal (json-encode-list '((b . 2) (a . 1))) "{\"b\":2,\"a\":1}")) | ||
| 745 | (should (equal (json-encode-list '((:b . 2) (:a . 1))) | ||
| 746 | "{\"b\":2,\"a\":1}")) | ||
| 747 | (should (equal (json-encode-list '((a) 1)) "[[\"a\"],1]")) | ||
| 748 | (should (equal (json-encode-list '((:a) 1)) "[[\"a\"],1]")) | ||
| 749 | (should (equal (json-encode-list '(("a") 1)) "[[\"a\"],1]")) | ||
| 750 | (should (equal (json-encode-list '((a 1) 2)) "[[\"a\",1],2]")) | ||
| 751 | (should (equal (json-encode-list '((:a 1) 2)) "[{\"a\":1},2]")) | ||
| 752 | (should (equal (json-encode-list '(((a . 1)) 2)) "[{\"a\":1},2]")) | ||
| 753 | (should (equal (json-encode-list '(:a 1 :b (2))) "{\"a\":1,\"b\":[2]}")) | ||
| 754 | (should (equal (json-encode-list '((a . 1) (b 2))) "{\"a\":1,\"b\":[2]}")) | ||
| 755 | (should-error (json-encode-list '(a . 1)) :type 'wrong-type-argument) | ||
| 756 | (should-error (json-encode-list '((a . 1) 2)) :type 'wrong-type-argument) | ||
| 757 | (should (equal (should-error (json-encode-list [])) | ||
| 758 | '(json-error []))) | ||
| 759 | (should (equal (should-error (json-encode-list [a])) | ||
| 760 | '(json-error [a]))))) | ||
| 284 | 761 | ||
| 285 | ;;; Arrays | 762 | ;;; Arrays |
| 286 | 763 | ||
| 287 | (ert-deftest test-json-read-array () | 764 | (ert-deftest test-json-read-array () |
| 288 | (let ((json-array-type 'vector)) | 765 | (let ((json-array-type 'vector)) |
| 766 | (json-tests--with-temp-buffer "[]" | ||
| 767 | (should (equal (json-read-array) []))) | ||
| 768 | (json-tests--with-temp-buffer "[ ]" | ||
| 769 | (should (equal (json-read-array) []))) | ||
| 770 | (json-tests--with-temp-buffer "[1]" | ||
| 771 | (should (equal (json-read-array) [1]))) | ||
| 289 | (json-tests--with-temp-buffer "[1, 2, \"a\", \"b\"]" | 772 | (json-tests--with-temp-buffer "[1, 2, \"a\", \"b\"]" |
| 290 | (should (equal (json-read-array) [1 2 "a" "b"])))) | 773 | (should (equal (json-read-array) [1 2 "a" "b"])))) |
| 291 | (let ((json-array-type 'list)) | 774 | (let ((json-array-type 'list)) |
| 775 | (json-tests--with-temp-buffer "[]" | ||
| 776 | (should-not (json-read-array))) | ||
| 777 | (json-tests--with-temp-buffer "[ ]" | ||
| 778 | (should-not (json-read-array))) | ||
| 779 | (json-tests--with-temp-buffer "[1]" | ||
| 780 | (should (equal (json-read-array) '(1)))) | ||
| 292 | (json-tests--with-temp-buffer "[1, 2, \"a\", \"b\"]" | 781 | (json-tests--with-temp-buffer "[1, 2, \"a\", \"b\"]" |
| 293 | (should (equal (json-read-array) '(1 2 "a" "b"))))) | 782 | (should (equal (json-read-array) '(1 2 "a" "b"))))) |
| 294 | (json-tests--with-temp-buffer "[1 2]" | 783 | (json-tests--with-temp-buffer "[1 2]" |
| 295 | (should-error (json-read-array) :type 'json-error))) | 784 | (should (equal (should-error (json-read-array)) |
| 785 | '(json-array-format "," ?2))))) | ||
| 786 | |||
| 787 | (ert-deftest test-json-read-array-function () | ||
| 788 | (let* ((pre nil) | ||
| 789 | (post nil) | ||
| 790 | (keys '(0 1)) | ||
| 791 | (json-pre-element-read-function | ||
| 792 | (lambda (key) | ||
| 793 | (setq pre 'pre) | ||
| 794 | (should (equal key (pop keys))))) | ||
| 795 | (json-post-element-read-function | ||
| 796 | (lambda () (setq post 'post)))) | ||
| 797 | (json-tests--with-temp-buffer "[1, 0]" | ||
| 798 | (json-read-array) | ||
| 799 | (should (eq pre 'pre)) | ||
| 800 | (should (eq post 'post))))) | ||
| 296 | 801 | ||
| 297 | (ert-deftest test-json-encode-array () | 802 | (ert-deftest test-json-encode-array () |
| 298 | (let ((json-encoding-pretty-print nil)) | 803 | (let ((json-encoding-object-sort-predicate nil) |
| 299 | (should (equal (json-encode-array [1 2 "a" "b"]) | 804 | (json-encoding-pretty-print nil)) |
| 300 | "[1,2,\"a\",\"b\"]")))) | 805 | (should (equal (json-encode-array ()) "[]")) |
| 806 | (should (equal (json-encode-array []) "[]")) | ||
| 807 | (should (equal (json-encode-array '(1)) "[1]")) | ||
| 808 | (should (equal (json-encode-array '[1]) "[1]")) | ||
| 809 | (should (equal (json-encode-array '(2 1)) "[2,1]")) | ||
| 810 | (should (equal (json-encode-array '[2 1]) "[2,1]")) | ||
| 811 | (should (equal (json-encode-array '[:b a 2 1]) "[\"b\",\"a\",2,1]")))) | ||
| 812 | |||
| 813 | (ert-deftest test-json-encode-array-pretty () | ||
| 814 | (let ((json-encoding-object-sort-predicate nil) | ||
| 815 | (json-encoding-pretty-print t) | ||
| 816 | (json-encoding-default-indentation " ") | ||
| 817 | (json-encoding-lisp-style-closings nil)) | ||
| 818 | (should (equal (json-encode-array ()) "[]")) | ||
| 819 | (should (equal (json-encode-array []) "[]")) | ||
| 820 | (should (equal (json-encode-array '(1)) "[\n 1\n]")) | ||
| 821 | (should (equal (json-encode-array '[1]) "[\n 1\n]")) | ||
| 822 | (should (equal (json-encode-array '(2 1)) "[\n 2,\n 1\n]")) | ||
| 823 | (should (equal (json-encode-array '[2 1]) "[\n 2,\n 1\n]")) | ||
| 824 | (should (equal (json-encode-array '[:b a 2 1]) | ||
| 825 | "[\n \"b\",\n \"a\",\n 2,\n 1\n]")))) | ||
| 826 | |||
| 827 | (ert-deftest test-json-encode-array-lisp-style () | ||
| 828 | (let ((json-encoding-object-sort-predicate nil) | ||
| 829 | (json-encoding-pretty-print t) | ||
| 830 | (json-encoding-default-indentation " ") | ||
| 831 | (json-encoding-lisp-style-closings t)) | ||
| 832 | (should (equal (json-encode-array ()) "[]")) | ||
| 833 | (should (equal (json-encode-array []) "[]")) | ||
| 834 | (should (equal (json-encode-array '(1)) "[\n 1]")) | ||
| 835 | (should (equal (json-encode-array '[1]) "[\n 1]")) | ||
| 836 | (should (equal (json-encode-array '(2 1)) "[\n 2,\n 1]")) | ||
| 837 | (should (equal (json-encode-array '[2 1]) "[\n 2,\n 1]")) | ||
| 838 | (should (equal (json-encode-array '[:b a 2 1]) | ||
| 839 | "[\n \"b\",\n \"a\",\n 2,\n 1]")))) | ||
| 301 | 840 | ||
| 302 | ;;; Reader | 841 | ;;; Reader |
| 303 | 842 | ||
| 304 | (ert-deftest test-json-read () | 843 | (ert-deftest test-json-read () |
| 305 | (json-tests--with-temp-buffer "{ \"a\": 1 }" | 844 | (pcase-dolist (`(,fn . ,contents) |
| 306 | ;; We don't care exactly what the return value is (that is tested | 845 | '((json-read-string "\"\"" "\"a\"") |
| 307 | ;; in `test-json-read-object'), but it should parse without error. | 846 | (json-read-array "[]" "[1]") |
| 308 | (should (json-read))) | 847 | (json-read-object "{}" "{\"a\":1}") |
| 848 | (json-read-keyword "null" "false" "true") | ||
| 849 | (json-read-number | ||
| 850 | "-0" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))) | ||
| 851 | (dolist (content contents) | ||
| 852 | ;; Check that leading whitespace is skipped. | ||
| 853 | (dolist (str (list content (concat " " content))) | ||
| 854 | (cl-letf* ((called nil) | ||
| 855 | ((symbol-function fn) | ||
| 856 | (lambda (&rest _) (setq called t)))) | ||
| 857 | (json-tests--with-temp-buffer str | ||
| 858 | ;; We don't care exactly what the return value is (that is | ||
| 859 | ;; tested elsewhere), but it should parse without error. | ||
| 860 | (should (json-read)) | ||
| 861 | (should called))))))) | ||
| 862 | |||
| 863 | (ert-deftest test-json-read-invalid () | ||
| 309 | (json-tests--with-temp-buffer "" | 864 | (json-tests--with-temp-buffer "" |
| 310 | (should-error (json-read) :type 'json-end-of-file)) | 865 | (should-error (json-read) :type 'json-end-of-file)) |
| 311 | (json-tests--with-temp-buffer "xxx" | 866 | (json-tests--with-temp-buffer " " |
| 312 | (let ((err (should-error (json-read) :type 'json-readtable-error))) | 867 | (should-error (json-read) :type 'json-end-of-file)) |
| 313 | (should (equal (cdr err) '(?x)))))) | 868 | (json-tests--with-temp-buffer "x" |
| 869 | (should (equal (should-error (json-read)) | ||
| 870 | '(json-readtable-error ?x)))) | ||
| 871 | (json-tests--with-temp-buffer " x" | ||
| 872 | (should (equal (should-error (json-read)) | ||
| 873 | '(json-readtable-error ?x))))) | ||
| 314 | 874 | ||
| 315 | (ert-deftest test-json-read-from-string () | 875 | (ert-deftest test-json-read-from-string () |
| 316 | (let ((json-string "{ \"a\": 1 }")) | 876 | (dolist (str '("\"\"" "\"a\"" "[]" "[1]" "{}" "{\"a\":1}" |
| 317 | (json-tests--with-temp-buffer json-string | 877 | "null" "false" "true" "0" "123")) |
| 318 | (should (equal (json-read-from-string json-string) | 878 | (json-tests--with-temp-buffer str |
| 879 | (should (equal (json-read-from-string str) | ||
| 319 | (json-read)))))) | 880 | (json-read)))))) |
| 320 | 881 | ||
| 321 | ;;; JSON encoder | 882 | ;;; Encoder |
| 322 | 883 | ||
| 323 | (ert-deftest test-json-encode () | 884 | (ert-deftest test-json-encode () |
| 885 | (should (equal (json-encode t) "true")) | ||
| 886 | (let ((json-null 'null)) | ||
| 887 | (should (equal (json-encode json-null) "null"))) | ||
| 888 | (let ((json-false 'false)) | ||
| 889 | (should (equal (json-encode json-false) "false"))) | ||
| 890 | (should (equal (json-encode "") "\"\"")) | ||
| 324 | (should (equal (json-encode "foo") "\"foo\"")) | 891 | (should (equal (json-encode "foo") "\"foo\"")) |
| 892 | (should (equal (json-encode :) "\"\"")) | ||
| 893 | (should (equal (json-encode :foo) "\"foo\"")) | ||
| 894 | (should (equal (json-encode '(1)) "[1]")) | ||
| 895 | (should (equal (json-encode 'foo) "\"foo\"")) | ||
| 896 | (should (equal (json-encode 0) "0")) | ||
| 897 | (should (equal (json-encode 123) "123")) | ||
| 898 | (let ((json-encoding-object-sort-predicate nil) | ||
| 899 | (json-encoding-pretty-print nil)) | ||
| 900 | (should (equal (json-encode []) "[]")) | ||
| 901 | (should (equal (json-encode [1]) "[1]")) | ||
| 902 | (should (equal (json-encode #s(hash-table)) "{}")) | ||
| 903 | (should (equal (json-encode #s(hash-table data (a 1))) "{\"a\":1}"))) | ||
| 325 | (with-temp-buffer | 904 | (with-temp-buffer |
| 326 | (should-error (json-encode (current-buffer)) :type 'json-error))) | 905 | (should (equal (should-error (json-encode (current-buffer))) |
| 906 | (list 'json-error (current-buffer)))))) | ||
| 327 | 907 | ||
| 328 | ;;; Pretty-print | 908 | ;;; Pretty printing & minimizing |
| 329 | 909 | ||
| 330 | (defun json-tests-equal-pretty-print (original &optional expected) | 910 | (defun json-tests-equal-pretty-print (original &optional expected) |
| 331 | "Abort current test if pretty-printing ORIGINAL does not yield EXPECTED. | 911 | "Abort current test if pretty-printing ORIGINAL does not yield EXPECTED. |
| @@ -351,46 +931,45 @@ nil, ORIGINAL should stay unchanged by pretty-printing." | |||
| 351 | (json-tests-equal-pretty-print "0.123")) | 931 | (json-tests-equal-pretty-print "0.123")) |
| 352 | 932 | ||
| 353 | (ert-deftest test-json-pretty-print-object () | 933 | (ert-deftest test-json-pretty-print-object () |
| 354 | ;; empty (regression test for bug#24252) | 934 | ;; Empty (regression test for bug#24252). |
| 355 | (json-tests-equal-pretty-print | 935 | (json-tests-equal-pretty-print "{}") |
| 356 | "{}" | 936 | ;; One pair. |
| 357 | "{\n}") | ||
| 358 | ;; one pair | ||
| 359 | (json-tests-equal-pretty-print | 937 | (json-tests-equal-pretty-print |
| 360 | "{\"key\":1}" | 938 | "{\"key\":1}" |
| 361 | "{\n \"key\": 1\n}") | 939 | "{\n \"key\": 1\n}") |
| 362 | ;; two pairs | 940 | ;; Two pairs. |
| 363 | (json-tests-equal-pretty-print | 941 | (json-tests-equal-pretty-print |
| 364 | "{\"key1\":1,\"key2\":2}" | 942 | "{\"key1\":1,\"key2\":2}" |
| 365 | "{\n \"key1\": 1,\n \"key2\": 2\n}") | 943 | "{\n \"key1\": 1,\n \"key2\": 2\n}") |
| 366 | ;; embedded object | 944 | ;; Nested object. |
| 367 | (json-tests-equal-pretty-print | 945 | (json-tests-equal-pretty-print |
| 368 | "{\"foo\":{\"key\":1}}" | 946 | "{\"foo\":{\"key\":1}}" |
| 369 | "{\n \"foo\": {\n \"key\": 1\n }\n}") | 947 | "{\n \"foo\": {\n \"key\": 1\n }\n}") |
| 370 | ;; embedded array | 948 | ;; Nested array. |
| 371 | (json-tests-equal-pretty-print | 949 | (json-tests-equal-pretty-print |
| 372 | "{\"key\":[1,2]}" | 950 | "{\"key\":[1,2]}" |
| 373 | "{\n \"key\": [\n 1,\n 2\n ]\n}")) | 951 | "{\n \"key\": [\n 1,\n 2\n ]\n}")) |
| 374 | 952 | ||
| 375 | (ert-deftest test-json-pretty-print-array () | 953 | (ert-deftest test-json-pretty-print-array () |
| 376 | ;; empty | 954 | ;; Empty. |
| 377 | (json-tests-equal-pretty-print "[]") | 955 | (json-tests-equal-pretty-print "[]") |
| 378 | ;; one item | 956 | ;; One item. |
| 379 | (json-tests-equal-pretty-print | 957 | (json-tests-equal-pretty-print |
| 380 | "[1]" | 958 | "[1]" |
| 381 | "[\n 1\n]") | 959 | "[\n 1\n]") |
| 382 | ;; two items | 960 | ;; Two items. |
| 383 | (json-tests-equal-pretty-print | 961 | (json-tests-equal-pretty-print |
| 384 | "[1,2]" | 962 | "[1,2]" |
| 385 | "[\n 1,\n 2\n]") | 963 | "[\n 1,\n 2\n]") |
| 386 | ;; embedded object | 964 | ;; Nested object. |
| 387 | (json-tests-equal-pretty-print | 965 | (json-tests-equal-pretty-print |
| 388 | "[{\"key\":1}]" | 966 | "[{\"key\":1}]" |
| 389 | "[\n {\n \"key\": 1\n }\n]") | 967 | "[\n {\n \"key\": 1\n }\n]") |
| 390 | ;; embedded array | 968 | ;; Nested array. |
| 391 | (json-tests-equal-pretty-print | 969 | (json-tests-equal-pretty-print |
| 392 | "[[1,2]]" | 970 | "[[1,2]]" |
| 393 | "[\n [\n 1,\n 2\n ]\n]")) | 971 | "[\n [\n 1,\n 2\n ]\n]")) |
| 394 | 972 | ||
| 395 | (provide 'json-tests) | 973 | (provide 'json-tests) |
| 974 | |||
| 396 | ;;; json-tests.el ends here | 975 | ;;; json-tests.el ends here |
diff --git a/test/lisp/net/webjump-tests.el b/test/lisp/net/webjump-tests.el new file mode 100644 index 00000000000..47569c948f5 --- /dev/null +++ b/test/lisp/net/webjump-tests.el | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | ;;; webjump-tests.el --- Tests for webjump.el -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2020 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Author: Simen Heggestøyl <simenheg@gmail.com> | ||
| 6 | ;; Keywords: | ||
| 7 | |||
| 8 | ;; This file is part of GNU Emacs. | ||
| 9 | |||
| 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 11 | ;; it under the terms of the GNU General Public License as published by | ||
| 12 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 13 | ;; (at your option) any later version. | ||
| 14 | |||
| 15 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 18 | ;; GNU General Public License for more details. | ||
| 19 | |||
| 20 | ;; You should have received a copy of the GNU General Public License | ||
| 21 | ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. | ||
| 22 | |||
| 23 | ;;; Commentary: | ||
| 24 | |||
| 25 | ;; | ||
| 26 | |||
| 27 | ;;; Code: | ||
| 28 | |||
| 29 | (require 'ert) | ||
| 30 | (require 'webjump) | ||
| 31 | |||
| 32 | (ert-deftest webjump-tests-builtin () | ||
| 33 | (should (equal (webjump-builtin '[name] "gnu.org") "gnu.org"))) | ||
| 34 | |||
| 35 | (ert-deftest webjump-tests-builtin-check-args () | ||
| 36 | (should (webjump-builtin-check-args [1 2 3] "Foo" 2)) | ||
| 37 | (should-error (webjump-builtin-check-args [1 2 3] "Foo" 3))) | ||
| 38 | |||
| 39 | (ert-deftest webjump-tests-mirror-default () | ||
| 40 | (should (equal (webjump-mirror-default | ||
| 41 | '("https://ftp.gnu.org/pub/gnu/" | ||
| 42 | "https://ftpmirror.gnu.org")) | ||
| 43 | "https://ftp.gnu.org/pub/gnu/"))) | ||
| 44 | |||
| 45 | (ert-deftest webjump-tests-null-or-blank-string-p () | ||
| 46 | (should (webjump-null-or-blank-string-p nil)) | ||
| 47 | (should (webjump-null-or-blank-string-p "")) | ||
| 48 | (should (webjump-null-or-blank-string-p " ")) | ||
| 49 | (should-not (webjump-null-or-blank-string-p " . "))) | ||
| 50 | |||
| 51 | (ert-deftest webjump-tests-url-encode () | ||
| 52 | (should (equal (webjump-url-encode "") "")) | ||
| 53 | (should (equal (webjump-url-encode "a b c") "a+b+c")) | ||
| 54 | (should (equal (webjump-url-encode "foo?") "foo%3F")) | ||
| 55 | (should (equal (webjump-url-encode "/foo\\") "/foo%5C")) | ||
| 56 | (should (equal (webjump-url-encode "f&o") "f%26o"))) | ||
| 57 | |||
| 58 | (ert-deftest webjump-tests-url-fix () | ||
| 59 | (should (equal (webjump-url-fix nil) "")) | ||
| 60 | (should (equal (webjump-url-fix "/tmp/") "file:///tmp/")) | ||
| 61 | (should (equal (webjump-url-fix "gnu.org") "http://gnu.org/")) | ||
| 62 | (should (equal (webjump-url-fix "ftp.x.org") "ftp://ftp.x.org/")) | ||
| 63 | (should (equal (webjump-url-fix "https://gnu.org") | ||
| 64 | "https://gnu.org/"))) | ||
| 65 | |||
| 66 | (ert-deftest webjump-tests-url-fix-trailing-slash () | ||
| 67 | (should (equal (webjump-url-fix-trailing-slash "https://gnu.org") | ||
| 68 | "https://gnu.org/")) | ||
| 69 | (should (equal (webjump-url-fix-trailing-slash "https://gnu.org/") | ||
| 70 | "https://gnu.org/"))) | ||
| 71 | |||
| 72 | (provide 'webjump-tests) | ||
| 73 | ;;; webjump-tests.el ends here | ||
diff --git a/test/lisp/xml-tests.el b/test/lisp/xml-tests.el index 57e685cd347..72c78d00e3e 100644 --- a/test/lisp/xml-tests.el +++ b/test/lisp/xml-tests.el | |||
| @@ -164,6 +164,16 @@ Parser is called with and without 'symbol-qnames argument.") | |||
| 164 | (should (equal (cdr xml-parse-test--namespace-attribute-qnames) | 164 | (should (equal (cdr xml-parse-test--namespace-attribute-qnames) |
| 165 | (xml-parse-region nil nil nil nil 'symbol-qnames))))) | 165 | (xml-parse-region nil nil nil nil 'symbol-qnames))))) |
| 166 | 166 | ||
| 167 | (ert-deftest xml-print-invalid-cdata () | ||
| 168 | "Check that Bug#41094 is fixed." | ||
| 169 | (with-temp-buffer | ||
| 170 | (should (equal (should-error (xml-print '((foo () "\0"))) | ||
| 171 | :type 'xml-invalid-character) | ||
| 172 | '(xml-invalid-character 0 1))) | ||
| 173 | (should (equal (should-error (xml-print '((foo () "\u00FF \xFF"))) | ||
| 174 | :type 'xml-invalid-character) | ||
| 175 | '(xml-invalid-character #x3FFFFF 3))))) | ||
| 176 | |||
| 167 | ;; Local Variables: | 177 | ;; Local Variables: |
| 168 | ;; no-byte-compile: t | 178 | ;; no-byte-compile: t |
| 169 | ;; End: | 179 | ;; End: |
diff --git a/test/src/buffer-tests.el b/test/src/buffer-tests.el index 6e87cb94897..6e9764625a9 100644 --- a/test/src/buffer-tests.el +++ b/test/src/buffer-tests.el | |||
| @@ -1327,4 +1327,10 @@ with parameters from the *Messages* buffer modification." | |||
| 1327 | (set-buffer-multibyte t) | 1327 | (set-buffer-multibyte t) |
| 1328 | (buffer-string))))))) | 1328 | (buffer-string))))))) |
| 1329 | 1329 | ||
| 1330 | ;; https://debbugs.gnu.org/33492 | ||
| 1331 | (ert-deftest buffer-tests-buffer-local-variables-undo () | ||
| 1332 | "Test that `buffer-undo-list' appears in `buffer-local-variables'." | ||
| 1333 | (with-temp-buffer | ||
| 1334 | (should (assq 'buffer-undo-list (buffer-local-variables))))) | ||
| 1335 | |||
| 1330 | ;;; buffer-tests.el ends here | 1336 | ;;; buffer-tests.el ends here |