]> code.delx.au - gnu-emacs-elpa/blob - packages/js2-mode/tests/json-path.el
Merge commit '0cda39255827f283e7578cd469ae42daad9556a2' from js2-mode
[gnu-emacs-elpa] / packages / js2-mode / tests / json-path.el
1 ;;; tests/json-path.el --- Test of using js2-mode AST to print JSON path.
2
3 ;; Copyright (C) 2015 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 <http://www.gnu.org/licenses/>.
19
20 ;;; Code:
21
22 (require 'ert)
23 (require 'js2-mode)
24
25 (ert-deftest js2-json-path-with-actual-array-index ()
26 (with-temp-buffer
27 (insert "var a = { hello: [1, 2, [1,3,3,4, { world: { hell: { yes: [1,2, 'test'] } } }]] };")
28 (js2-mode)
29 (goto-char 0)
30 (search-forward "test")
31 (should (string= (js2-print-json-path) "hello[2][4].world.hell.yes[2]"))))
32
33 (ert-deftest js2-json-path-pure-arrays ()
34 (with-temp-buffer
35 (insert "var a = [5, 1, 4, [ 4, [1, 2, [1, 3.9, 4, [1, 2, 'test',3]]]], 9, 9];")
36 (js2-mode)
37 (goto-char 0)
38 (search-forward "test")
39 (should (string= (js2-print-json-path) "[3][1][2][3][2]"))))
40
41 (ert-deftest js2-json-path-key-is-numeric ()
42 (with-temp-buffer
43 (insert "var b = {hello: {3 : {world: {2: 'test'}}}};")
44 (js2-mode)
45 (goto-char 0)
46 (search-forward "test")
47 (should (string= (js2-print-json-path) "hello.3.world.2"))))
48
49 (ert-deftest js2-json-path-not-found ()
50 (with-temp-buffer
51 (insert "console.log('test');")
52 (js2-mode)
53 (goto-char 0)
54 (search-forward "test")
55 (should (eq (js2-print-json-path) nil))))
56
57 (ert-deftest js2-json-path-with-array-index-hardcoded ()
58 (with-temp-buffer
59 (insert "var a = { hello: [1, 2, [1,3,3,4, { world: { hell: { yes: [1,2, 'test'] } } }]] };")
60 (js2-mode)
61 (goto-char 0)
62 (search-forward "test")
63 (should (string= (js2-print-json-path 1) "hello[1][1].world.hell.yes[1]"))
64 (should (string= (js2-print-json-path 0) "hello[0][0].world.hell.yes[0]"))))