]> code.delx.au - gnu-emacs-elpa/blob - packages/js2-mode/tests/parser.el
3c05c4b5ef9e5042ee0eeb046da2ccf74462e3cf
[gnu-emacs-elpa] / packages / js2-mode / tests / parser.el
1 (require 'ert)
2 (require 'ert-x)
3 (require 'js2-mode)
4
5 (defun js2-test-string-to-ast (s)
6 (ert-with-test-buffer (:name 'origin)
7 (insert s)
8 (js2-mode)
9 (should (null js2-mode-buffer-dirty-p))
10 js2-mode-ast))
11
12 (defun js2-test-parse-string (code-string &key syntax-error)
13 (let ((ast (js2-test-string-to-ast code-string)))
14 (if syntax-error
15 (let ((errors (js2-ast-root-errors ast)))
16 (should (= 1 (length errors)))
17 (destructuring-bind (_ pos len) (first errors)
18 (should (string= syntax-error (substring code-string
19 (1- pos) (+ pos len -1))))))
20 (should (= 0 (length (js2-ast-root-errors ast))))
21 (ert-with-test-buffer (:name 'copy)
22 (js2-print-tree ast)
23 (skip-chars-backward " \t\n")
24 (should (string= code-string (buffer-substring-no-properties
25 (point-min) (point))))))))
26
27 (defmacro* js2-deftest-parse (name code-string &key bind syntax-error)
28 "Parse CODE-STRING. If SYNTAX-ERROR is nil, print syntax tree
29 with `js2-print-tree' and assert the result to be equal to the
30 original string. If SYNTAX-ERROR is passed, expect syntax error
31 highlighting substring equal to SYNTAX-ERROR value.
32 BIND defines bindings to apply them around the test."
33 `(ert-deftest ,(intern (format "js2-%s" name)) ()
34 (let ,(append bind '((js2-basic-offset 2)))
35 (js2-test-parse-string ,code-string :syntax-error ,syntax-error))))
36
37 (put 'js2-deftest-parse 'lisp-indent-function 'defun)
38
39 ;;; Callers of `js2-valid-prop-name-token'.
40
41 (js2-deftest-parse parse-property-access-when-not-keyword
42 "A.foo = 3;")
43
44 (js2-deftest-parse parse-property-access-when-keyword
45 "A.in = 3;"
46 :bind ((js2-allow-keywords-as-property-names t)))
47
48 (js2-deftest-parse parse-property-access-when-keyword-no-xml
49 "A.in = 3;"
50 :bind ((js2-allow-keywords-as-property-names t)
51 (js2-compiler-xml-available nil)))
52
53 (js2-deftest-parse parse-array-literal-when-not-keyword
54 "a = {b: 1};")
55
56 (js2-deftest-parse parse-array-literal-when-keyword
57 "a = {in: 1};"
58 :bind ((js2-allow-keywords-as-property-names t)))
59
60 ;;; 'of' contextual keyword.
61
62 (js2-deftest-parse parse-array-comp-loop-with-of
63 "[a for (a of [])];")
64
65 (js2-deftest-parse parse-for-of
66 "for (var a of []) {\n}")
67
68 (js2-deftest-parse of-can-be-var-name
69 "var of = 3;")
70
71 (js2-deftest-parse of-can-be-function-name
72 "function of() {\n}")
73
74 ;;; Destructuring binding.
75
76 (js2-deftest-parse destruct-in-declaration
77 "var {a, b} = {a: 1, b: 2};")
78
79 (js2-deftest-parse destruct-in-arguments
80 "function f({a: aa, b: bb}) {\n}")
81
82 (js2-deftest-parse destruct-in-array-comp-loop
83 "[a + b for ([a, b] in [[0, 1], [1, 2]])];")
84
85 (js2-deftest-parse destruct-in-catch-clause
86 "try {\n} catch ({a, b}) {\n a + b;\n}")
87
88 ;;; Function parameters.
89
90 (js2-deftest-parse function-with-default-parameters
91 "function foo(a = 1, b = a + 1) {\n}")
92
93 (js2-deftest-parse function-with-no-default-after-default
94 "function foo(a = 1, b) {\n}"
95 :syntax-error "b")
96
97 (js2-deftest-parse function-with-destruct-after-default
98 "function foo(a = 1, {b, c}) {\n}"
99 :syntax-error "{")
100
101 (js2-deftest-parse function-with-rest-parameter
102 "function foo(a, b, ...rest) {\n}")
103
104 (js2-deftest-parse function-with-param-after-rest-parameter
105 "function foo(a, ...b, rest) {\n}"
106 :syntax-error "rest")
107
108 (js2-deftest-parse function-with-destruct-after-rest-parameter
109 "function foo(a, ...b, {}) {\n}"
110 :syntax-error "{}")
111
112 (js2-deftest-parse function-with-rest-after-default-parameter
113 "function foo(a = 1, ...rest) {\n}")