]> code.delx.au - gnu-emacs-elpa/blobdiff - packages/js2-mode/tests/parser.el
Mark merge point of js2-mode.
[gnu-emacs-elpa] / packages / js2-mode / tests / parser.el
diff --git a/packages/js2-mode/tests/parser.el b/packages/js2-mode/tests/parser.el
new file mode 100644 (file)
index 0000000..3c05c4b
--- /dev/null
@@ -0,0 +1,113 @@
+(require 'ert)\r
+(require 'ert-x)\r
+(require 'js2-mode)\r
+\r
+(defun js2-test-string-to-ast (s)\r
+  (ert-with-test-buffer (:name 'origin)\r
+    (insert s)\r
+    (js2-mode)\r
+    (should (null js2-mode-buffer-dirty-p))\r
+    js2-mode-ast))\r
+\r
+(defun js2-test-parse-string (code-string &key syntax-error)\r
+  (let ((ast (js2-test-string-to-ast code-string)))\r
+    (if syntax-error\r
+        (let ((errors (js2-ast-root-errors ast)))\r
+          (should (= 1 (length errors)))\r
+          (destructuring-bind (_ pos len) (first errors)\r
+            (should (string= syntax-error (substring code-string\r
+                                                     (1- pos) (+ pos len -1))))))\r
+      (should (= 0 (length (js2-ast-root-errors ast))))\r
+      (ert-with-test-buffer (:name 'copy)\r
+        (js2-print-tree ast)\r
+        (skip-chars-backward " \t\n")\r
+        (should (string= code-string (buffer-substring-no-properties\r
+                                      (point-min) (point))))))))\r
+\r
+(defmacro* js2-deftest-parse (name code-string &key bind syntax-error)\r
+  "Parse CODE-STRING.  If SYNTAX-ERROR is nil, print syntax tree\r
+with `js2-print-tree' and assert the result to be equal to the\r
+original string.  If SYNTAX-ERROR is passed, expect syntax error\r
+highlighting substring equal to SYNTAX-ERROR value.\r
+BIND defines bindings to apply them around the test."\r
+  `(ert-deftest ,(intern (format "js2-%s" name)) ()\r
+     (let ,(append bind '((js2-basic-offset 2)))\r
+       (js2-test-parse-string ,code-string :syntax-error ,syntax-error))))\r
+\r
+(put 'js2-deftest-parse 'lisp-indent-function 'defun)\r
+\r
+;;; Callers of `js2-valid-prop-name-token'.\r
+\r
+(js2-deftest-parse parse-property-access-when-not-keyword\r
+  "A.foo = 3;")\r
+\r
+(js2-deftest-parse parse-property-access-when-keyword\r
+  "A.in = 3;"\r
+  :bind ((js2-allow-keywords-as-property-names t)))\r
+\r
+(js2-deftest-parse parse-property-access-when-keyword-no-xml\r
+  "A.in = 3;"\r
+  :bind ((js2-allow-keywords-as-property-names t)\r
+         (js2-compiler-xml-available nil)))\r
+\r
+(js2-deftest-parse parse-array-literal-when-not-keyword\r
+  "a = {b: 1};")\r
+\r
+(js2-deftest-parse parse-array-literal-when-keyword\r
+  "a = {in: 1};"\r
+  :bind ((js2-allow-keywords-as-property-names t)))\r
+\r
+;;; 'of' contextual keyword.\r
+\r
+(js2-deftest-parse parse-array-comp-loop-with-of\r
+  "[a for (a of [])];")\r
+\r
+(js2-deftest-parse parse-for-of\r
+  "for (var a of []) {\n}")\r
+\r
+(js2-deftest-parse of-can-be-var-name\r
+  "var of = 3;")\r
+\r
+(js2-deftest-parse of-can-be-function-name\r
+  "function of() {\n}")\r
+\r
+;;; Destructuring binding.\r
+\r
+(js2-deftest-parse destruct-in-declaration\r
+  "var {a, b} = {a: 1, b: 2};")\r
+\r
+(js2-deftest-parse destruct-in-arguments\r
+  "function f({a: aa, b: bb}) {\n}")\r
+\r
+(js2-deftest-parse destruct-in-array-comp-loop\r
+  "[a + b for ([a, b] in [[0, 1], [1, 2]])];")\r
+\r
+(js2-deftest-parse destruct-in-catch-clause\r
+  "try {\n} catch ({a, b}) {\n  a + b;\n}")\r
+\r
+;;; Function parameters.\r
+\r
+(js2-deftest-parse function-with-default-parameters\r
+  "function foo(a = 1, b = a + 1) {\n}")\r
+\r
+(js2-deftest-parse function-with-no-default-after-default\r
+  "function foo(a = 1, b) {\n}"\r
+  :syntax-error "b")\r
+\r
+(js2-deftest-parse function-with-destruct-after-default\r
+  "function foo(a = 1, {b, c}) {\n}"\r
+  :syntax-error "{")\r
+\r
+(js2-deftest-parse function-with-rest-parameter\r
+  "function foo(a, b, ...rest) {\n}")\r
+\r
+(js2-deftest-parse function-with-param-after-rest-parameter\r
+  "function foo(a, ...b, rest) {\n}"\r
+  :syntax-error "rest")\r
+\r
+(js2-deftest-parse function-with-destruct-after-rest-parameter\r
+  "function foo(a, ...b, {}) {\n}"\r
+  :syntax-error "{}")\r
+\r
+(js2-deftest-parse function-with-rest-after-default-parameter\r
+  "function foo(a = 1, ...rest) {\n}")\r