[Pkg-javascript-commits] [node-acorn-jsx] 320/484: Allow specifying expected comments/tokens arrays with `onComment`/`onToken`.

Bastien Roucariès rouca at moszumanska.debian.org
Sat Aug 19 14:20:49 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-acorn-jsx.

commit ac8bdc608d49911c6e33d66ba9ef7e2bba5c0bdd
Author: Ingvar Stepanyan <me at rreverser.com>
Date:   Sun Oct 26 20:54:37 2014 +0200

    Allow specifying expected comments/tokens arrays with `onComment`/`onToken`.
    
    Allows to avoid both custom `testAssert` and adding extra arguments in `test`.
---
 test/driver.js |  38 ++++----
 test/tests.js  | 282 ++++++++++++++++++++++++++++-----------------------------
 2 files changed, 159 insertions(+), 161 deletions(-)

diff --git a/test/driver.js b/test/driver.js
index d56f3b5..5444cc5 100644
--- a/test/driver.js
+++ b/test/driver.js
@@ -1,8 +1,8 @@
 (function(exports) {
   var tests = [];
 
-  exports.test = function(code, ast, options, comments) {
-    tests.push({code: code, ast: ast, options: options, comments: comments});
+  exports.test = function(code, ast, options) {
+    tests.push({code: code, ast: ast, options: options});
   };
   exports.testFail = function(code, message, options) {
     tests.push({code: code, error: message, options: options});
@@ -12,27 +12,19 @@
   };
 
   exports.runTests = function(config, callback) {
-    var parse = config.parse, comments;
-
-    function onComment(block, text, start, end, startLoc, endLoc) {
-      comments.push({
-        block: block,
-        text: text,
-        start: start,
-        end: end,
-        startLoc: { line: startLoc.line, column: startLoc.column },
-        endLoc: { line: endLoc.line, column: endLoc.column }
-      });
-    }
-
-    var opts = {locations: true, onComment: onComment};
+    var parse = config.parse;
 
     for (var i = 0; i < tests.length; ++i) {
       var test = tests[i];
       try {
-        comments = [];
-        var testOpts = test.options || opts;
-        testOpts.onComment = onComment;
+        var testOpts = test.options || {locations: true};
+        var expected = {};
+        if (expected.onComment = testOpts.onComment) {
+          testOpts.onComment = []
+        }
+        if (expected.onToken = testOpts.onToken) {
+          testOpts.onToken = [];
+        }
         var ast = parse(test.code, testOpts);
         if (test.error) {
           if (config.loose) {
@@ -48,7 +40,13 @@
           else callback("ok", test.code);
         } else {
           var mis = misMatch(test.ast, ast);
-          if (!mis && test.comments) mis = misMatch(test.comments, comments);
+          for (var name in expected) {
+            if (mis) break;
+            if (expected[name]) {
+              mis = misMatch(expected[name], testOpts[name]);
+              testOpts[name] = expected[name];
+            }
+          }
           if (mis) callback("fail", test.code, mis);
           else callback("ok", test.code);
         }
diff --git a/test/tests.js b/test/tests.js
index 8d17a89..f1d9467 100644
--- a/test/tests.js
+++ b/test/tests.js
@@ -28685,152 +28685,152 @@ test(function TestComments() {
     Cat.prototype.roar = function(message) {
       return 'MEOOWW: ' + /*stuff*/ message;
     };
-}.toString().replace(/\r\n/g, '\n'), {}, {}, [
-  {block: false, text: " Bear class"},
-  {block: false, text: " Whatever"},
-  {block: true,  text: [
-          " 1",
-    "       2",
-    "       3"
-  ].join('\n')},
-  {block: true, text: "stuff"}
-]);
+}.toString().replace(/\r\n/g, '\n'), {}, {
+  onComment: [
+    {type: "Line", value: " Bear class"},
+    {type: "Line", value: " Whatever"},
+    {type: "Block",  value: [
+            " 1",
+      "       2",
+      "       3"
+    ].join('\n')},
+    {type: "Block", value: "stuff"}
+  ]
+});
 
 test("<!--\n;", {
   type: "Program",
-  body: [
-    {
-      type: "EmptyStatement"
+  body: [{
+    type: "EmptyStatement"
+  }]
+});
+
+test("\nfunction plop() {\n'use strict';\n/* Comment */\n}", {}, {
+  locations: true,
+  onComment: [{
+    type: "Block",
+    value: " Comment ",
+    loc: {
+      start: { line: 4, column: 0 },
+      end: { line: 4, column: 13 }
     }
-  ]
-}
-);
-
-(function() {
-  test("\nfunction plop() {\n'use strict';\n/* Comment */\n}", {}, {locations: true},
-  [{
-    block: true,
-    text: " Comment ",
-    startLoc: { line: 4, column: 0 },
-    endLoc: { line: 4, column: 13 }
-  }]);
-
-  test("// line comment", {}, {locations: true},
-  [{
-    block: false,
-    text: " line comment",
-    startLoc: { line: 1, column: 0 },
-    endLoc: { line: 1, column: 15 }
-  }]);
-
-  test("<!-- HTML comment", {}, {locations: true},
-  [{
-    block: false,
-    text: " HTML comment",
-    startLoc: { line: 1, column: 0 },
-    endLoc: { line: 1, column: 17 }
-  }]);
-
-  test(";\n--> HTML comment", {}, {locations: true},
-  [{
-    block: false,
-    text: " HTML comment",
-    startLoc: { line: 2, column: 0 },
-    endLoc: { line: 2, column: 16 }
-  }]);
-})();
-
-(function() {
-  var tokTypes = acorn.tokTypes;
-
-  var actualTokens = [],
-      expectedTokens = [
-        {
-          type: tokTypes._var,
-          value: "var",
-          loc: {
-            start: {line: 1, column: 0},
-            end: {line: 1, column: 3}
-          }
-        },
-        {
-          type: tokTypes.name,
-          value: "x",
-          loc: {
-            start: {line: 1, column: 4},
-            end: {line: 1, column: 5}
-          }
-        },
-        {
-          type: tokTypes.eq,
-          value: "=",
-          loc: {
-            start: {line: 1, column: 6},
-            end: {line: 1, column: 7}
-          }
-        },
-        {
-          type: tokTypes.parenL,
-          value: undefined,
-          loc: {
-            start: {line: 1, column: 8},
-            end: {line: 1, column: 9}
-          }
-        },
-        {
-          type: tokTypes.num,
-          value: 1,
-          loc: {
-            start: {line: 1, column: 9},
-            end: {line: 1, column: 10}
-          }
-        },
-        {
-          type: {binop: 9, prefix: true, beforeExpr: true},
-          value: "+",
-          loc: {
-            start: {line: 1, column: 11},
-            end: {line: 1, column: 12}
-          }
-        },
-        {
-          type: tokTypes.num,
-          value: 2,
-          loc: {
-            start: {line: 1, column: 13},
-            end: {line: 1, column: 14}
-          }
-        },
-        {
-          type: tokTypes.parenR,
-          value: undefined,
-          loc: {
-            start: {line: 1, column: 14},
-            end: {line: 1, column: 15}
-          }
-        },
-        {
-          type: tokTypes.eof,
-          value: undefined,
-          loc: {
-            start: {line: 1, column: 15},
-            end: {line: 1, column: 15}
-          }
-        }
-      ];
-  testAssert('var x = (1 + 2)', function assert(ast) {
-    if (actualTokens.length !== expectedTokens.length) {
-      return "Bad token stream length: expected " + expectedTokens.length + ", got " + actualTokens.length;
-    } else {
-      for (var i=0, n=actualTokens.length; i < n; i++) {
-        var mis = misMatch(expectedTokens[i], actualTokens[i]);
-        if (mis) return mis;
+  }]
+});
+
+test("// line comment", {}, {
+  locations: true,
+  onComment: [{
+    type: "Line",
+    value: " line comment",
+    loc: {
+      start: { line: 1, column: 0 },
+      end: { line: 1, column: 15 }
+    }
+  }]
+});
+
+test("<!-- HTML comment", {}, {
+  locations: true,
+  onComment: [{
+    type: "Line",
+    value: " HTML comment",
+    loc: {
+      start: { line: 1, column: 0 },
+      end: { line: 1, column: 17 }
+    }
+  }]
+});
+
+test(";\n--> HTML comment", {}, {
+  locations: true,
+  onComment: [{
+    type: "Line",
+    value: " HTML comment",
+    loc: {
+      start: { line: 2, column: 0 },
+      end: { line: 2, column: 16 }
+    }
+  }]
+});
+
+var tokTypes = acorn.tokTypes;
+
+test('var x = (1 + 2)', {}, {
+  locations: true,
+  onToken: [
+    {
+      type: tokTypes._var,
+      value: "var",
+      loc: {
+        start: {line: 1, column: 0},
+        end: {line: 1, column: 3}
+      }
+    },
+    {
+      type: tokTypes.name,
+      value: "x",
+      loc: {
+        start: {line: 1, column: 4},
+        end: {line: 1, column: 5}
+      }
+    },
+    {
+      type: tokTypes.eq,
+      value: "=",
+      loc: {
+        start: {line: 1, column: 6},
+        end: {line: 1, column: 7}
+      }
+    },
+    {
+      type: tokTypes.parenL,
+      value: undefined,
+      loc: {
+        start: {line: 1, column: 8},
+        end: {line: 1, column: 9}
+      }
+    },
+    {
+      type: tokTypes.num,
+      value: 1,
+      loc: {
+        start: {line: 1, column: 9},
+        end: {line: 1, column: 10}
+      }
+    },
+    {
+      type: {binop: 9, prefix: true, beforeExpr: true},
+      value: "+",
+      loc: {
+        start: {line: 1, column: 11},
+        end: {line: 1, column: 12}
+      }
+    },
+    {
+      type: tokTypes.num,
+      value: 2,
+      loc: {
+        start: {line: 1, column: 13},
+        end: {line: 1, column: 14}
+      }
+    },
+    {
+      type: tokTypes.parenR,
+      value: undefined,
+      loc: {
+        start: {line: 1, column: 14},
+        end: {line: 1, column: 15}
+      }
+    },
+    {
+      type: tokTypes.eof,
+      value: undefined,
+      loc: {
+        start: {line: 1, column: 15},
+        end: {line: 1, column: 15}
       }
     }
-  }, {
-    locations: true,
-    onToken: actualTokens
-  });
-})();
+  ]
+});
 
 test("function f(f) { 'use strict'; }", {});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-acorn-jsx.git



More information about the Pkg-javascript-commits mailing list