[Pkg-javascript-commits] [node-acorn-jsx] 03/484: Make tests runnable under node
Bastien Roucariès
rouca at moszumanska.debian.org
Sat Aug 19 14:19:56 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 03d95cda2cd4f553001b111502406ff0bc3ed003
Author: Marijn Haverbeke <marijnh at gmail.com>
Date: Mon Sep 24 11:50:03 2012 +0200
Make tests runnable under node
---
test/driver.js | 103 +++++++++++++++++++++++++++++----------------------------
test/run.js | 14 ++++++++
test/tests.js | 5 +++
3 files changed, 72 insertions(+), 50 deletions(-)
diff --git a/test/driver.js b/test/driver.js
index d587fd3..78cc8e4 100644
--- a/test/driver.js
+++ b/test/driver.js
@@ -1,58 +1,61 @@
-var tests = [];
+(function(exports) {
+ var tests = [];
+ var acorn = typeof require == "undefined" ? window.acorn : require("../acorn.js");
-function test(code, ast, options) {
- tests.push({code: code, ast: ast, options: options});
-}
-function testFail(code, message, options) {
- tests.push({code: code, error: message, options: options});
-}
+ 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});
+ };
-function runTests(callback) {
- var opts = {linePositions: true};
- for (var i = 0; i < tests.length; ++i) {
- var test = tests[i];
- try {
- var ast = acorn.parse(test.code, test.options || opts);
- if (test.error) callback("fail", test.code,
- "Expected error message: " + test.error + "\nBut parsing succeeded.");
- else {
- var mis = misMatch(test.ast, ast);
- if (!mis) callback("ok", test.code);
- else callback("fail", test.code, mis);
- }
- } catch(e) {
- if (test.error && e instanceof SyntaxError) {
- if (e.message == test.error) callback("ok", test.code);
- else callback("fail", test.code,
- "Expected error message: " + test.error + "\nGot error message: " + e.message);
- } else {
- callback("error", test.code, e.message || e.toString());
+ exports.runTests = function(callback) {
+ var opts = {linePositions: true};
+ for (var i = 0; i < tests.length; ++i) {
+ var test = tests[i];
+ try {
+ var ast = acorn.parse(test.code, test.options || opts);
+ if (test.error) callback("fail", test.code,
+ "Expected error message: " + test.error + "\nBut parsing succeeded.");
+ else {
+ var mis = misMatch(test.ast, ast);
+ if (!mis) callback("ok", test.code);
+ else callback("fail", test.code, mis);
+ }
+ } catch(e) {
+ if (test.error && e instanceof SyntaxError) {
+ if (e.message == test.error) callback("ok", test.code);
+ else callback("fail", test.code,
+ "Expected error message: " + test.error + "\nGot error message: " + e.message);
+ } else {
+ callback("error", test.code, e.message || e.toString());
+ }
}
}
- }
-}
+ };
-function ppJSON(v) { return JSON.stringify(v, null, 2); }
-function addPath(str, pt) {
- if (str.charAt(str.length-1) == ")")
- return str.slice(0, str.length-1) + "/" + pt + ")";
- return str + " (" + pt + ")";
-}
+ function ppJSON(v) { return JSON.stringify(v, null, 2); }
+ function addPath(str, pt) {
+ if (str.charAt(str.length-1) == ")")
+ return str.slice(0, str.length-1) + "/" + pt + ")";
+ return str + " (" + pt + ")";
+ }
-function misMatch(exp, act) {
- if (!exp || !act || (typeof exp != "object") || (typeof act != "object")) {
- if (exp !== act) return ppJSON(exp) + " !== " + ppJSON(act);
- } else if (exp.splice) {
- if (!act.slice) return ppJSON(exp) + " != " + ppJSON(act);
- if (act.length != exp.length) return "array length mismatch " + exp.length + " != " + act.length;
- for (var i = 0; i < act.length; ++i) {
- var mis = misMatch(exp[i], act[i]);
- if (mis) return addPath(mis, i);
- }
- } else {
- for (var prop in exp) {
- var mis = misMatch(exp[prop], act[prop]);
- if (mis) return addPath(mis, prop);
+ function misMatch(exp, act) {
+ if (!exp || !act || (typeof exp != "object") || (typeof act != "object")) {
+ if (exp !== act) return ppJSON(exp) + " !== " + ppJSON(act);
+ } else if (exp.splice) {
+ if (!act.slice) return ppJSON(exp) + " != " + ppJSON(act);
+ if (act.length != exp.length) return "array length mismatch " + exp.length + " != " + act.length;
+ for (var i = 0; i < act.length; ++i) {
+ var mis = misMatch(exp[i], act[i]);
+ if (mis) return addPath(mis, i);
+ }
+ } else {
+ for (var prop in exp) {
+ var mis = misMatch(exp[prop], act[prop]);
+ if (mis) return addPath(mis, prop);
+ }
}
}
-}
+})(typeof exports == "undefined" ? window : exports);
diff --git a/test/run.js b/test/run.js
new file mode 100644
index 0000000..282b133
--- /dev/null
+++ b/test/run.js
@@ -0,0 +1,14 @@
+var driver = require("./driver.js");
+require("./tests.js");
+
+var testsRun = 0, failed = 0;
+function report(state, code, message) {
+ if (state != "ok") {++failed; console.log(code, message);}
+ ++testsRun;
+}
+
+var t0 = +new Date;
+driver.runTests(report);
+console.log(testsRun + " tests run in " + (+new Date - t0) + "ms");
+if (failed) console.log(failed + " failures.");
+else console.log("All passed.");
diff --git a/test/tests.js b/test/tests.js
index 157537b..d3d7779 100644
--- a/test/tests.js
+++ b/test/tests.js
@@ -1,6 +1,11 @@
// Tests largely based on those of Esprima
// (http://esprima.org/test/)
+if (typeof exports != "undefined") {
+ var test = require("./driver.js").test;
+ var testFail = require("./driver.js").testFail;
+}
+
test("this\n", {
type: "Program",
start: {
--
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