]> code.delx.au - gnu-emacs-elpa/blobdiff - packages/js2-mode/tests/parser.el
Merge remote-tracking branch 'js2-mode/master'
[gnu-emacs-elpa] / packages / js2-mode / tests / parser.el
index 2f020a679b2c3307558940a7bff925efac8330cd..fd8c4c4e5de38ceb3fd7f85c825b3643399bdcad 100644 (file)
@@ -1,26 +1,19 @@
-;;; parser.el --- Tests of the js2-mode's parser\r
-\r
-;; Copyright (C) 2013  Free Software Foundation, Inc.\r
-\r
-;; This program is free software: you can redistribute it and/or modify\r
-;; it under the terms of the GNU General Public License as published by\r
-;; the Free Software Foundation, either version 3 of the License, or\r
-;; (at your option) any later version.\r
-\r
-;; This program is distributed in the hope that it will be useful,\r
-;; but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-;; GNU General Public License for more details.\r
-\r
-;; You should have received a copy of the GNU General Public License\r
-;; along with this program  If not, see <http://www.gnu.org/licenses/>.\r
-\r
-;;; Code:\r
-\r
 (require 'ert)\r
 (require 'ert-x)\r
 (require 'js2-mode)\r
 \r
+(defmacro js2-deftest (name buffer-contents &rest body)\r
+  `(ert-deftest ,(intern (format "js2-%s" name)) ()\r
+     (with-temp-buffer\r
+       (save-excursion\r
+         (insert ,buffer-contents))\r
+       (unwind-protect\r
+           (progn\r
+             ,@body)\r
+         (fundamental-mode)))))\r
+\r
+(put 'js2-deftest 'lisp-indent-function 'defun)\r
+\r
 (defun js2-test-string-to-ast (s)\r
   (ert-with-test-buffer (:name 'origin)\r
     (insert s)\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
+(defun* js2-test-parse-string (code-string &key syntax-error errors-count\r
+                                                reference)\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
+          (should (= (or errors-count 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
       (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
+        (should (string= (or reference code-string)\r
+                         (buffer-substring-no-properties\r
+                          (point-min) (point))))))))\r
 \r
-(defmacro* js2-deftest-parse (name code-string &key bind syntax-error)\r
+(defmacro* js2-deftest-parse (name code-string &key bind syntax-error errors-count\r
+                                                    reference)\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
+with `js2-print-tree' and assert the result to be equal to\r
+REFERENCE, if present, or the original string.  If SYNTAX-ERROR\r
+is passed, expect syntax error highlighting substring equal to\r
+SYNTAX-ERROR value.  BIND defines bindings to apply them around\r
+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
+       (js2-test-parse-string ,code-string\r
+                              :syntax-error ,syntax-error\r
+                              :errors-count ,errors-count\r
+                              :reference ,reference))))\r
 \r
 (put 'js2-deftest-parse 'lisp-indent-function 'defun)\r
 \r
-;;; Callers of `js2-valid-prop-name-token'.\r
+;;; Basics\r
+\r
+(js2-deftest-parse variable-assignment\r
+  "a = 1;")\r
+\r
+(js2-deftest-parse empty-object-literal\r
+  "b = {};")\r
+\r
+(js2-deftest-parse empty-array-literal\r
+  "c = [];")\r
+\r
+(js2-deftest-parse comma-after-regexp\r
+  "d = /eee/, 42;")\r
+\r
+(js2-deftest-parse return-statement\r
+  "function foo() {\n  return 2;\n}")\r
+\r
+(js2-deftest-parse function-statement\r
+  "function foo() {\n}")\r
+\r
+(js2-deftest-parse function-expression-statements-are-verboten\r
+  "function() {}" :syntax-error "function")\r
+\r
+(js2-deftest-parse member-expr-as-function-name\r
+  "function a.b.c[2](x, y) {\n}"\r
+  :bind ((js2-allow-member-expr-as-function-name t)))\r
+\r
+(js2-deftest-parse named-function-expression\r
+  "a = function b() {};")\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
@@ -69,14 +99,14 @@ BIND defines bindings to apply them around the test."
   :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
+(js2-deftest-parse parse-object-literal-when-not-keyword\r
   "a = {b: 1};")\r
 \r
-(js2-deftest-parse parse-array-literal-when-keyword\r
+(js2-deftest-parse parse-object-literal-when-keyword\r
   "a = {in: 1};"\r
   :bind ((js2-allow-keywords-as-property-names t)))\r
 \r
-;;; 'of' contextual keyword.\r
+;;; 'of' contextual keyword\r
 \r
 (js2-deftest-parse parse-array-comp-loop-with-of\r
   "[a for (a of [])];")\r
@@ -90,7 +120,7 @@ BIND defines bindings to apply them around the test."
 (js2-deftest-parse of-can-be-function-name\r
   "function of() {\n}")\r
 \r
-;;; Destructuring binding.\r
+;;; Destructuring binding\r
 \r
 (js2-deftest-parse destruct-in-declaration\r
   "var {a, b} = {a: 1, b: 2};")\r
@@ -104,7 +134,7 @@ BIND defines bindings to apply them around the test."
 (js2-deftest-parse destruct-in-catch-clause\r
   "try {\n} catch ({a, b}) {\n  a + b;\n}")\r
 \r
-;;; Function parameters.\r
+;;; Function parameters\r
 \r
 (js2-deftest-parse function-with-default-parameters\r
   "function foo(a = 1, b = a + 1) {\n}")\r
@@ -130,3 +160,144 @@ BIND defines bindings to apply them around the test."
 \r
 (js2-deftest-parse function-with-rest-after-default-parameter\r
   "function foo(a = 1, ...rest) {\n}")\r
+\r
+;;; Spread operator\r
+\r
+(js2-deftest-parse spread-in-array-literal\r
+  "[1, ...[2, 3], 4, ...[5, 6]];")\r
+\r
+(js2-deftest-parse spread-in-function-call\r
+  "f(3, ...[t(2), t(3)], 42, ...[t(4)]);")\r
+\r
+;;; Arrow functions\r
+\r
+(js2-deftest-parse arrow-function-with-empty-args-and-no-curlies\r
+  "() => false;" :reference "() => {false};")\r
+\r
+(js2-deftest-parse arrow-function-with-args-and-curlies\r
+  "(a, b = 1, ...c) => {  c;\n};")\r
+\r
+(js2-deftest-parse parenless-arrow-function-prohibits-rest\r
+  "...b => {b + 1;};" :syntax-error "b" :errors-count 2)\r
+\r
+(js2-deftest-parse parenless-arrow-function-prohibits-destructuring\r
+  "[a, b] => {a + b;};" :syntax-error "]" :errors-count 5)\r
+\r
+;;; Automatic semicolon insertion\r
+\r
+(js2-deftest-parse no-auto-semi-insertion-after-if\r
+  "if (true) {\n}")\r
+\r
+(js2-deftest-parse auto-semi-insertion-after-function\r
+  "a = function() {}" :reference "a = function() {};")\r
+\r
+(js2-deftest-parse auto-semi-one-variable-per-line\r
+  "x\ny" :reference "x;\ny;")\r
+\r
+;;; Labels\r
+\r
+(js2-deftest-parse labeled-stmt-node\r
+  "foo:\nbar:\nx = y + 1;")\r
+\r
+(js2-deftest no-label-node-inside-expr "x = y:"\r
+  (let (js2-parse-interruptable-p)\r
+    (js2-mode))\r
+  (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))\r
+    (should (js2-name-node-p (js2-assign-node-right assignment)))))\r
+\r
+(js2-deftest-parse label-and-loops "for (; ; ) {\r
+  loop:\r
+  for (; ; ) {\r
+    continue loop;\r
+  }\r
+}")\r
+\r
+;;; Generators\r
+\r
+(js2-deftest-parse legacy-generator "function foo() {\n  yield 1;\n}")\r
+\r
+(js2-deftest-parse legacy-generator-cannot-return\r
+  "function foo() {\n  yield 1;\n return 2;\n}" :syntax-error "return 2")\r
+\r
+(js2-deftest-parse harmony-generator "function* bar() {\n  yield 2;\n  return 3;\n}")\r
+\r
+;;; Scopes\r
+\r
+(js2-deftest ast-symbol-table-includes-fn-node "function foo() {}"\r
+  (js2-mode)\r
+  (let ((entry (js2-scope-get-symbol js2-mode-ast 'foo)))\r
+    (should (= (js2-symbol-decl-type entry) js2-FUNCTION))\r
+    (should (equal (js2-symbol-name entry) "foo"))\r
+    (should (js2-function-node-p (js2-symbol-ast-node entry)))))\r
+\r
+(js2-deftest fn-symbol-table-includes-nested-fn "function foo() {\r
+  function bar() {}\r
+  var x;\r
+}"\r
+  (js2-mode)\r
+  (let* ((scope (js2-node-at-point (point-min)))\r
+         (fn-entry (js2-scope-get-symbol scope 'bar))\r
+         (var-entry (js2-scope-get-symbol scope 'x)))\r
+    (should (string= (js2-name-node-name (js2-function-node-name scope)) "foo"))\r
+    (should (= (js2-symbol-decl-type fn-entry) js2-FUNCTION))\r
+    (should (js2-function-node-p (js2-symbol-ast-node fn-entry)))\r
+    (should (= (js2-symbol-decl-type var-entry) js2-VAR))\r
+    (should (js2-name-node-p (js2-symbol-ast-node var-entry)))))\r
+\r
+;;; Tokenizer\r
+\r
+(js2-deftest get-token "(1+1)"\r
+  (js2-init-scanner)\r
+  (should (eq js2-LP (js2-next-token)))\r
+  (should (eq js2-NUMBER (js2-next-token)))\r
+  (should (eq js2-ADD (js2-next-token)))\r
+  (should (eq js2-NUMBER (js2-next-token)))\r
+  (should (eq js2-RP (js2-next-token))))\r
+\r
+(js2-deftest unget-token "()"\r
+  (js2-init-scanner)\r
+  (should (eq js2-LP (js2-next-token)))\r
+  (js2-unget-token)\r
+  (should (eq js2-LP (js2-next-token)))\r
+  (should (eq js2-RP (js2-next-token))))\r
+\r
+(js2-deftest get-token-or-eol "x\n++;"\r
+  (js2-init-scanner)\r
+  (should (eq js2-NAME (js2-next-token)))\r
+  (should (eq js2-EOL (js2-peek-token-or-eol)))\r
+  (should (eq js2-INC (js2-next-token)))\r
+  (should (eq js2-SEMI (js2-peek-token-or-eol))))\r
+\r
+(js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"\r
+  (js2-init-scanner)\r
+  (should (eq js2-NAME (js2-next-token)))\r
+  (should (eq js2-NAME (js2-next-token)))\r
+  (should (equal "y" (js2-current-token-string)))\r
+  (js2-unget-token)\r
+  (should (eq js2-NAME (js2-current-token-type)))\r
+  (should (equal "x" (js2-current-token-string))))\r
+\r
+(js2-deftest ts-seek "(1+2)"\r
+  (js2-init-scanner)\r
+  (should (eq js2-LP (js2-next-token)))\r
+  (should (eq js2-NUMBER (js2-next-token)))\r
+  (js2-unget-token)\r
+  (let ((state (make-js2-ts-state)))\r
+    (should (eq js2-NUMBER (js2-next-token)))\r
+    (should (eq js2-ADD (js2-next-token)))\r
+    (js2-ts-seek state)\r
+    (should (eq 1 js2-ti-lookahead))\r
+    (should (eq js2-NUMBER (js2-next-token)))\r
+    (should (eq 1 (js2-token-number\r
+                   (js2-current-token))))))\r
+\r
+;;; Error handling\r
+\r
+(js2-deftest for-node-with-error-len "for "\r
+  (js2-mode)\r
+  (let ((node (js2-node-at-point (point-min))))\r
+    (should (= (js2-node-len (js2-node-parent node)) 4))))\r
+\r
+(js2-deftest function-without-parens-error "function b {}"\r
+  ;; Should finish the parse.\r
+  (js2-mode))\r