]> code.delx.au - gnu-emacs-elpa/blob - tests/parser.el
Fix (and rename) the ts-set-state test
[gnu-emacs-elpa] / tests / parser.el
1 (require 'ert)
2 (require 'ert-x)
3 (require 'js2-mode)
4
5 (defmacro js2-deftest (name buffer-contents &rest body)
6 `(ert-deftest ,(intern (format "js2-%s" name)) ()
7 (with-temp-buffer
8 (save-excursion
9 (insert ,buffer-contents))
10 (unwind-protect
11 (progn
12 ,@body)
13 (fundamental-mode)))))
14
15 (put 'js2-deftest 'lisp-indent-function 'defun)
16
17 (defun js2-test-string-to-ast (s)
18 (ert-with-test-buffer (:name 'origin)
19 (insert s)
20 (js2-mode)
21 (should (null js2-mode-buffer-dirty-p))
22 js2-mode-ast))
23
24 (defun* js2-test-parse-string (code-string &key syntax-error errors-count
25 reference)
26 (let ((ast (js2-test-string-to-ast code-string)))
27 (if syntax-error
28 (let ((errors (js2-ast-root-errors ast)))
29 (should (= (or errors-count 1) (length errors)))
30 (destructuring-bind (_ pos len) (first errors)
31 (should (string= syntax-error (substring code-string
32 (1- pos) (+ pos len -1))))))
33 (should (= 0 (length (js2-ast-root-errors ast))))
34 (ert-with-test-buffer (:name 'copy)
35 (js2-print-tree ast)
36 (skip-chars-backward " \t\n")
37 (should (string= (or reference code-string)
38 (buffer-substring-no-properties
39 (point-min) (point))))))))
40
41 (defmacro* js2-deftest-parse (name code-string &key bind syntax-error errors-count
42 reference)
43 "Parse CODE-STRING. If SYNTAX-ERROR is nil, print syntax tree
44 with `js2-print-tree' and assert the result to be equal to
45 REFERENCE, if present, or the original string. If SYNTAX-ERROR
46 is passed, expect syntax error highlighting substring equal to
47 SYNTAX-ERROR value. BIND defines bindings to apply them around
48 the test."
49 `(ert-deftest ,(intern (format "js2-%s" name)) ()
50 (let ,(append bind '((js2-basic-offset 2)))
51 (js2-test-parse-string ,code-string
52 :syntax-error ,syntax-error
53 :errors-count ,errors-count
54 :reference ,reference))))
55
56 (put 'js2-deftest-parse 'lisp-indent-function 'defun)
57
58 ;;; Basics
59
60 (js2-deftest-parse variable-assignment
61 "a = 1;")
62
63 (js2-deftest-parse empty-object-literal
64 "b = {};")
65
66 (js2-deftest-parse empty-array-literal
67 "c = [];")
68
69 (js2-deftest-parse comma-after-regexp
70 "d = /eee/, 42;")
71
72 (js2-deftest-parse return-statement
73 "function foo() {\n return 2;\n}")
74
75 (js2-deftest-parse function-statement
76 "function foo() {\n}")
77
78 (js2-deftest-parse function-expression-statements-are-verboten
79 "function() {}" :syntax-error "function")
80
81 (js2-deftest-parse member-expr-as-function-name
82 "function a.b.c[2](x, y) {\n}"
83 :bind ((js2-allow-member-expr-as-function-name t)))
84
85 (js2-deftest-parse named-function-expression
86 "a = function b() {};")
87
88 ;;; Callers of `js2-valid-prop-name-token'.
89
90 (js2-deftest-parse parse-property-access-when-not-keyword
91 "A.foo = 3;")
92
93 (js2-deftest-parse parse-property-access-when-keyword
94 "A.in = 3;"
95 :bind ((js2-allow-keywords-as-property-names t)))
96
97 (js2-deftest-parse parse-property-access-when-keyword-no-xml
98 "A.in = 3;"
99 :bind ((js2-allow-keywords-as-property-names t)
100 (js2-compiler-xml-available nil)))
101
102 (js2-deftest-parse parse-object-literal-when-not-keyword
103 "a = {b: 1};")
104
105 (js2-deftest-parse parse-object-literal-when-keyword
106 "a = {in: 1};"
107 :bind ((js2-allow-keywords-as-property-names t)))
108
109 ;;; 'of' contextual keyword.
110
111 (js2-deftest-parse parse-array-comp-loop-with-of
112 "[a for (a of [])];")
113
114 (js2-deftest-parse parse-for-of
115 "for (var a of []) {\n}")
116
117 (js2-deftest-parse of-can-be-var-name
118 "var of = 3;")
119
120 (js2-deftest-parse of-can-be-function-name
121 "function of() {\n}")
122
123 ;;; Destructuring binding.
124
125 (js2-deftest-parse destruct-in-declaration
126 "var {a, b} = {a: 1, b: 2};")
127
128 (js2-deftest-parse destruct-in-arguments
129 "function f({a: aa, b: bb}) {\n}")
130
131 (js2-deftest-parse destruct-in-array-comp-loop
132 "[a + b for ([a, b] in [[0, 1], [1, 2]])];")
133
134 (js2-deftest-parse destruct-in-catch-clause
135 "try {\n} catch ({a, b}) {\n a + b;\n}")
136
137 ;;; Function parameters.
138
139 (js2-deftest-parse function-with-default-parameters
140 "function foo(a = 1, b = a + 1) {\n}")
141
142 (js2-deftest-parse function-with-no-default-after-default
143 "function foo(a = 1, b) {\n}"
144 :syntax-error "b")
145
146 (js2-deftest-parse function-with-destruct-after-default
147 "function foo(a = 1, {b, c}) {\n}"
148 :syntax-error "{")
149
150 (js2-deftest-parse function-with-rest-parameter
151 "function foo(a, b, ...rest) {\n}")
152
153 (js2-deftest-parse function-with-param-after-rest-parameter
154 "function foo(a, ...b, rest) {\n}"
155 :syntax-error "rest")
156
157 (js2-deftest-parse function-with-destruct-after-rest-parameter
158 "function foo(a, ...b, {}) {\n}"
159 :syntax-error "{}")
160
161 (js2-deftest-parse function-with-rest-after-default-parameter
162 "function foo(a = 1, ...rest) {\n}")
163
164 ;;; Arrow functions.
165
166 (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies
167 "() => false;" :reference "() => {false};")
168
169 (js2-deftest-parse arrow-function-with-args-and-curlies
170 "(a, b = 1, ...c) => { c;\n};")
171
172 (js2-deftest-parse parenless-arrow-function-prohibits-rest
173 "...b => {b + 1;};" :syntax-error "b" :errors-count 2)
174
175 (js2-deftest-parse parenless-arrow-function-prohibits-destructuring
176 "[a, b] => {a + b;};" :syntax-error "]" :errors-count 5)
177
178 ;;; Automatic semicolon insertion.
179
180 (js2-deftest-parse no-auto-semi-insertion-after-if
181 "if (true) {\n}")
182
183 (js2-deftest-parse auto-semi-insertion-after-function
184 "a = function() {}" :reference "a = function() {};")
185
186 (js2-deftest-parse auto-semi-one-variable-per-line
187 "x\ny" :reference "x;\ny;")
188
189 ;;; Labels
190
191 (js2-deftest-parse labeled-stmt-node
192 "foo:\nbar:\n x = y + 1;")
193
194 (js2-deftest no-label-node-inside-expr "x = y:"
195 (let (js2-parse-interruptable-p)
196 (js2-mode))
197 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
198 (should (js2-name-node-p (js2-assign-node-right assignment)))))
199
200 ;;; Tokenizer
201
202 (js2-deftest get-token "(1+1)"
203 (js2-init-scanner)
204 (should (eq js2-LP (js2-next-token)))
205 (should (eq js2-NUMBER (js2-next-token)))
206 (should (eq js2-ADD (js2-next-token)))
207 (should (eq js2-NUMBER (js2-next-token)))
208 (should (eq js2-RP (js2-next-token))))
209
210 (js2-deftest unget-token "()"
211 (js2-init-scanner)
212 (should (eq js2-LP (js2-next-token)))
213 (js2-unget-token)
214 (should (eq js2-LP (js2-next-token)))
215 (should (eq js2-RP (js2-next-token))))
216
217 (js2-deftest get-token-or-eol "x\n++;"
218 (js2-init-scanner)
219 (should (eq js2-NAME (js2-next-token)))
220 (should (eq js2-EOL (js2-peek-token-or-eol)))
221 (should (eq js2-INC (js2-next-token)))
222 (should (eq js2-SEMI (js2-peek-token-or-eol))))
223
224 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
225 (js2-init-scanner)
226 (should (eq js2-NAME (js2-next-token)))
227 (should (eq js2-NAME (js2-next-token)))
228 (should (equal "y" (js2-current-token-string)))
229 (js2-unget-token)
230 (should (eq js2-NAME (js2-current-token-type)))
231 (should (equal "x" (js2-current-token-string))))
232
233 (js2-deftest ts-seek "(1+2)"
234 (js2-init-scanner)
235 (should (eq js2-LP (js2-next-token)))
236 (should (eq js2-NUMBER (js2-next-token)))
237 (js2-unget-token)
238 (let ((state (make-js2-ts-state)))
239 (should (eq js2-NUMBER (js2-next-token)))
240 (should (eq js2-ADD (js2-next-token)))
241 (js2-ts-seek state)
242 (should (eq 1 js2-ti-lookahead))
243 (should (eq js2-NUMBER (js2-next-token)))
244 (should (eq 1 (js2-token-number
245 (js2-current-token))))))