5 (defmacro js2-deftest (name buffer-contents &rest body)
6 `(ert-deftest ,(intern (format "js2-%s" name)) ()
9 (insert ,buffer-contents))
13 (fundamental-mode)))))
15 (put 'js2-deftest 'lisp-indent-function 'defun)
17 (defun js2-test-string-to-ast (s)
18 (ert-with-test-buffer (:name 'origin)
21 (should (null js2-mode-buffer-dirty-p))
24 (defun* js2-test-parse-string (code-string &key syntax-error errors-count
26 (let ((ast (js2-test-string-to-ast code-string)))
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)
36 (skip-chars-backward " \t\n")
37 (should (string= (or reference code-string)
38 (buffer-substring-no-properties
39 (point-min) (point))))))))
41 (defmacro* js2-deftest-parse (name code-string &key bind syntax-error errors-count
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
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))))
56 (put 'js2-deftest-parse 'lisp-indent-function 'defun)
60 (js2-deftest-parse variable-assignment
63 (js2-deftest-parse empty-object-literal
66 (js2-deftest-parse empty-array-literal
69 (js2-deftest-parse comma-after-regexp
72 (js2-deftest-parse return-statement
73 "function() {\n return 2;\n}")
75 ;;; Callers of `js2-valid-prop-name-token'.
77 (js2-deftest-parse parse-property-access-when-not-keyword
80 (js2-deftest-parse parse-property-access-when-keyword
82 :bind ((js2-allow-keywords-as-property-names t)))
84 (js2-deftest-parse parse-property-access-when-keyword-no-xml
86 :bind ((js2-allow-keywords-as-property-names t)
87 (js2-compiler-xml-available nil)))
89 (js2-deftest-parse parse-object-literal-when-not-keyword
92 (js2-deftest-parse parse-object-literal-when-keyword
94 :bind ((js2-allow-keywords-as-property-names t)))
96 ;;; 'of' contextual keyword.
98 (js2-deftest-parse parse-array-comp-loop-with-of
101 (js2-deftest-parse parse-for-of
102 "for (var a of []) {\n}")
104 (js2-deftest-parse of-can-be-var-name
107 (js2-deftest-parse of-can-be-function-name
108 "function of() {\n}")
110 ;;; Destructuring binding.
112 (js2-deftest-parse destruct-in-declaration
113 "var {a, b} = {a: 1, b: 2};")
115 (js2-deftest-parse destruct-in-arguments
116 "function f({a: aa, b: bb}) {\n}")
118 (js2-deftest-parse destruct-in-array-comp-loop
119 "[a + b for ([a, b] in [[0, 1], [1, 2]])];")
121 (js2-deftest-parse destruct-in-catch-clause
122 "try {\n} catch ({a, b}) {\n a + b;\n}")
124 ;;; Function parameters.
126 (js2-deftest-parse function-with-default-parameters
127 "function foo(a = 1, b = a + 1) {\n}")
129 (js2-deftest-parse function-with-no-default-after-default
130 "function foo(a = 1, b) {\n}"
133 (js2-deftest-parse function-with-destruct-after-default
134 "function foo(a = 1, {b, c}) {\n}"
137 (js2-deftest-parse function-with-rest-parameter
138 "function foo(a, b, ...rest) {\n}")
140 (js2-deftest-parse function-with-param-after-rest-parameter
141 "function foo(a, ...b, rest) {\n}"
142 :syntax-error "rest")
144 (js2-deftest-parse function-with-destruct-after-rest-parameter
145 "function foo(a, ...b, {}) {\n}"
148 (js2-deftest-parse function-with-rest-after-default-parameter
149 "function foo(a = 1, ...rest) {\n}")
151 ;;; Automatic semicolon insertion.
153 (js2-deftest-parse no-auto-semi-insertion-after-if
156 (js2-deftest-parse auto-semi-insertion-after-function
157 "a = function() {}" :reference "a = function() {};")
159 (js2-deftest-parse auto-semi-one-variable-per-line
160 "x\ny" :reference "x;\ny;")
164 (js2-deftest-parse labeled-stmt-node
165 "foo:\nbar:\n x = y + 1;")
167 (js2-deftest no-label-node-inside-expr "x = y:"
168 (let (js2-parse-interruptable-p)
170 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
171 (should (js2-name-node-p (js2-assign-node-right assignment)))))
175 (js2-deftest get-token "(1+1)"
177 (should (eq js2-LP (js2-next-token)))
178 (should (eq js2-NUMBER (js2-next-token)))
179 (should (eq js2-ADD (js2-next-token)))
180 (should (eq js2-NUMBER (js2-next-token)))
181 (should (eq js2-RP (js2-next-token))))
183 (js2-deftest unget-token "()"
185 (should (eq js2-LP (js2-next-token)))
187 (should (eq js2-LP (js2-next-token)))
188 (should (eq js2-RP (js2-next-token))))
190 (js2-deftest get-token-or-eol "x\n++;"
192 (should (eq js2-NAME (js2-next-token)))
193 (should (eq js2-EOL (js2-peek-token-or-eol)))
194 (should (eq js2-INC (js2-next-token)))
195 (should (eq js2-SEMI (js2-peek-token-or-eol))))
197 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
199 (should (eq js2-NAME (js2-next-token)))
200 (should (eq js2-NAME (js2-next-token)))
201 (should (equal "y" (js2-current-token-string)))
203 (should (eq js2-NAME (js2-current-token-type)))
204 (should (equal "x" (js2-current-token-string))))
206 (js2-deftest ts-set-state "(1+2)"
208 (should (eq js2-LP (js2-next-token)))
209 (should (eq js2-NUMBER (js2-next-token)))
211 (let ((state (make-js2-ts-state)))
212 (should (eq js2-NUMBER (js2-next-token)))
213 (should (eq js2-ADD (js2-next-token)))
214 (js2-ts-set-state state)
215 (should (eq 1 js2-ti-lookahead))
216 (should (eq js2-NUMBER (js2-next-token)))
217 (should (eq 1 (js2-token-number
218 (js2-current-token))))))