1 ;;; parser.el --- Tests of the js2-mode's parser
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
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.
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.
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/>.
24 (defun js2-test-string-to-ast (s)
25 (ert-with-test-buffer (:name 'origin)
28 (should (null js2-mode-buffer-dirty-p))
31 (defun js2-test-parse-string (code-string &key syntax-error)
32 (let ((ast (js2-test-string-to-ast code-string)))
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)
42 (skip-chars-backward " \t\n")
43 (should (string= code-string (buffer-substring-no-properties
44 (point-min) (point))))))))
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))))
56 (put 'js2-deftest-parse 'lisp-indent-function 'defun)
58 ;;; Callers of `js2-valid-prop-name-token'.
60 (js2-deftest-parse parse-property-access-when-not-keyword
63 (js2-deftest-parse parse-property-access-when-keyword
65 :bind ((js2-allow-keywords-as-property-names t)))
67 (js2-deftest-parse parse-property-access-when-keyword-no-xml
69 :bind ((js2-allow-keywords-as-property-names t)
70 (js2-compiler-xml-available nil)))
72 (js2-deftest-parse parse-array-literal-when-not-keyword
75 (js2-deftest-parse parse-array-literal-when-keyword
77 :bind ((js2-allow-keywords-as-property-names t)))
79 ;;; 'of' contextual keyword.
81 (js2-deftest-parse parse-array-comp-loop-with-of
84 (js2-deftest-parse parse-for-of
85 "for (var a of []) {\n}")
87 (js2-deftest-parse of-can-be-var-name
90 (js2-deftest-parse of-can-be-function-name
93 ;;; Destructuring binding.
95 (js2-deftest-parse destruct-in-declaration
96 "var {a, b} = {a: 1, b: 2};")
98 (js2-deftest-parse destruct-in-arguments
99 "function f({a: aa, b: bb}) {\n}")
101 (js2-deftest-parse destruct-in-array-comp-loop
102 "[a + b for ([a, b] in [[0, 1], [1, 2]])];")
104 (js2-deftest-parse destruct-in-catch-clause
105 "try {\n} catch ({a, b}) {\n a + b;\n}")
107 ;;; Function parameters.
109 (js2-deftest-parse function-with-default-parameters
110 "function foo(a = 1, b = a + 1) {\n}")
112 (js2-deftest-parse function-with-no-default-after-default
113 "function foo(a = 1, b) {\n}"
116 (js2-deftest-parse function-with-destruct-after-default
117 "function foo(a = 1, {b, c}) {\n}"
120 (js2-deftest-parse function-with-rest-parameter
121 "function foo(a, b, ...rest) {\n}")
123 (js2-deftest-parse function-with-param-after-rest-parameter
124 "function foo(a, ...b, rest) {\n}"
125 :syntax-error "rest")
127 (js2-deftest-parse function-with-destruct-after-rest-parameter
128 "function foo(a, ...b, {}) {\n}"
131 (js2-deftest-parse function-with-rest-after-default-parameter
132 "function foo(a = 1, ...rest) {\n}")