]> code.delx.au - gnu-emacs-elpa/blob - packages/js2-mode/tests/parser.el
Fix up copyrights and the checking code
[gnu-emacs-elpa] / packages / js2-mode / tests / parser.el
1 ;;; parser.el --- Tests of the js2-mode's parser
2
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
4
5 ;; This program is free software: you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
9
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program If not, see <http://www.gnu.org/licenses/>.
17
18 ;;; Code:
19
20 (require 'ert)
21 (require 'ert-x)
22 (require 'js2-mode)
23
24 (defun js2-test-string-to-ast (s)
25 (ert-with-test-buffer (:name 'origin)
26 (insert s)
27 (js2-mode)
28 (should (null js2-mode-buffer-dirty-p))
29 js2-mode-ast))
30
31 (defun js2-test-parse-string (code-string &key syntax-error)
32 (let ((ast (js2-test-string-to-ast code-string)))
33 (if syntax-error
34 (let ((errors (js2-ast-root-errors ast)))
35 (should (= 1 (length errors)))
36 (destructuring-bind (_ pos len) (first errors)
37 (should (string= syntax-error (substring code-string
38 (1- pos) (+ pos len -1))))))
39 (should (= 0 (length (js2-ast-root-errors ast))))
40 (ert-with-test-buffer (:name 'copy)
41 (js2-print-tree ast)
42 (skip-chars-backward " \t\n")
43 (should (string= code-string (buffer-substring-no-properties
44 (point-min) (point))))))))
45
46 (defmacro* js2-deftest-parse (name code-string &key bind syntax-error)
47 "Parse CODE-STRING. If SYNTAX-ERROR is nil, print syntax tree
48 with `js2-print-tree' and assert the result to be equal to the
49 original string. If SYNTAX-ERROR is passed, expect syntax error
50 highlighting substring equal to SYNTAX-ERROR value.
51 BIND defines bindings to apply them around the test."
52 `(ert-deftest ,(intern (format "js2-%s" name)) ()
53 (let ,(append bind '((js2-basic-offset 2)))
54 (js2-test-parse-string ,code-string :syntax-error ,syntax-error))))
55
56 (put 'js2-deftest-parse 'lisp-indent-function 'defun)
57
58 ;;; Callers of `js2-valid-prop-name-token'.
59
60 (js2-deftest-parse parse-property-access-when-not-keyword
61 "A.foo = 3;")
62
63 (js2-deftest-parse parse-property-access-when-keyword
64 "A.in = 3;"
65 :bind ((js2-allow-keywords-as-property-names t)))
66
67 (js2-deftest-parse parse-property-access-when-keyword-no-xml
68 "A.in = 3;"
69 :bind ((js2-allow-keywords-as-property-names t)
70 (js2-compiler-xml-available nil)))
71
72 (js2-deftest-parse parse-array-literal-when-not-keyword
73 "a = {b: 1};")
74
75 (js2-deftest-parse parse-array-literal-when-keyword
76 "a = {in: 1};"
77 :bind ((js2-allow-keywords-as-property-names t)))
78
79 ;;; 'of' contextual keyword.
80
81 (js2-deftest-parse parse-array-comp-loop-with-of
82 "[a for (a of [])];")
83
84 (js2-deftest-parse parse-for-of
85 "for (var a of []) {\n}")
86
87 (js2-deftest-parse of-can-be-var-name
88 "var of = 3;")
89
90 (js2-deftest-parse of-can-be-function-name
91 "function of() {\n}")
92
93 ;;; Destructuring binding.
94
95 (js2-deftest-parse destruct-in-declaration
96 "var {a, b} = {a: 1, b: 2};")
97
98 (js2-deftest-parse destruct-in-arguments
99 "function f({a: aa, b: bb}) {\n}")
100
101 (js2-deftest-parse destruct-in-array-comp-loop
102 "[a + b for ([a, b] in [[0, 1], [1, 2]])];")
103
104 (js2-deftest-parse destruct-in-catch-clause
105 "try {\n} catch ({a, b}) {\n a + b;\n}")
106
107 ;;; Function parameters.
108
109 (js2-deftest-parse function-with-default-parameters
110 "function foo(a = 1, b = a + 1) {\n}")
111
112 (js2-deftest-parse function-with-no-default-after-default
113 "function foo(a = 1, b) {\n}"
114 :syntax-error "b")
115
116 (js2-deftest-parse function-with-destruct-after-default
117 "function foo(a = 1, {b, c}) {\n}"
118 :syntax-error "{")
119
120 (js2-deftest-parse function-with-rest-parameter
121 "function foo(a, b, ...rest) {\n}")
122
123 (js2-deftest-parse function-with-param-after-rest-parameter
124 "function foo(a, ...b, rest) {\n}"
125 :syntax-error "rest")
126
127 (js2-deftest-parse function-with-destruct-after-rest-parameter
128 "function foo(a, ...b, {}) {\n}"
129 :syntax-error "{}")
130
131 (js2-deftest-parse function-with-rest-after-default-parameter
132 "function foo(a = 1, ...rest) {\n}")