Skip to content

Commit a996c0c

Browse files
committed
add back just enough acorn-babel to get npm run test working
brought back driver.js and run.js, minus html stuff, and with a tweak to get the plugin injected into acorn
1 parent bb00491 commit a996c0c

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

test/driver.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
var tests = [];
2+
3+
exports.test = function(code, ast, options) {
4+
tests.push({code: code, ast: ast, options: options});
5+
};
6+
exports.testFail = function(code, message, options) {
7+
tests.push({code: code, error: message, options: options});
8+
};
9+
exports.testAssert = function(code, assert, options) {
10+
tests.push({code: code, assert: assert, options: options});
11+
};
12+
13+
exports.runTests = function(config, callback) {
14+
var parse = config.parse;
15+
16+
for (var i = 0; i < tests.length; ++i) {
17+
var test = tests[i];
18+
if (config.filter && !config.filter(test)) continue;
19+
try {
20+
var testOpts = test.options || {locations: true};
21+
var expected = {};
22+
if (expected.onComment = testOpts.onComment) {
23+
testOpts.onComment = []
24+
}
25+
if (expected.onToken = testOpts.onToken) {
26+
testOpts.onToken = [];
27+
}
28+
testOpts.plugins = { jsx: true };
29+
var ast = parse(test.code, testOpts);
30+
if (test.error) {
31+
if (config.loose) {
32+
callback("ok", test.code);
33+
} else {
34+
callback("fail", test.code, "Expected error message: " + test.error + "\nBut parsing succeeded.");
35+
}
36+
}
37+
else if (test.assert) {
38+
var error = test.assert(ast);
39+
if (error) callback("fail", test.code,
40+
"\n Assertion failed:\n " + error);
41+
else callback("ok", test.code);
42+
} else {
43+
var mis = misMatch(test.ast, ast);
44+
for (var name in expected) {
45+
if (mis) break;
46+
if (expected[name]) {
47+
mis = misMatch(expected[name], testOpts[name]);
48+
testOpts[name] = expected[name];
49+
}
50+
}
51+
if (mis) callback("fail", test.code, mis);
52+
else callback("ok", test.code);
53+
}
54+
} catch(e) {
55+
if (!(e instanceof SyntaxError)) {
56+
throw e;
57+
}
58+
if (test.error) {
59+
if (e.message == test.error) callback("ok", test.code);
60+
else callback("fail", test.code,
61+
"Expected error message: " + test.error + "\nGot error message: " + e.message);
62+
} else {
63+
callback("error", test.code, e.stack || e.toString());
64+
}
65+
}
66+
}
67+
};
68+
69+
function ppJSON(v) { return v instanceof RegExp ? v.toString() : JSON.stringify(v, null, 2); }
70+
function addPath(str, pt) {
71+
if (str.charAt(str.length-1) == ")")
72+
return str.slice(0, str.length-1) + "/" + pt + ")";
73+
return str + " (" + pt + ")";
74+
}
75+
76+
var misMatch = exports.misMatch = function(exp, act) {
77+
if (!exp || !act || (typeof exp != "object") || (typeof act != "object")) {
78+
if (exp !== act) return ppJSON(exp) + " !== " + ppJSON(act);
79+
} else if (exp instanceof RegExp || act instanceof RegExp) {
80+
var left = ppJSON(exp), right = ppJSON(act);
81+
if (left !== right) return left + " !== " + right;
82+
} else if (exp.splice) {
83+
if (!act.slice) return ppJSON(exp) + " != " + ppJSON(act);
84+
if (act.length != exp.length) return "array length mismatch " + exp.length + " != " + act.length;
85+
for (var i = 0; i < act.length; ++i) {
86+
var mis = misMatch(exp[i], act[i]);
87+
if (mis) return addPath(mis, i);
88+
}
89+
} else {
90+
for (var prop in exp) {
91+
var mis = misMatch(exp[prop], act[prop]);
92+
if (mis) return addPath(mis, prop);
93+
}
94+
}
95+
};
96+
97+
function mangle(ast) {
98+
if (typeof ast != "object" || !ast) return;
99+
if (ast.slice) {
100+
for (var i = 0; i < ast.length; ++i) mangle(ast[i]);
101+
} else {
102+
var loc = ast.start && ast.end && {start: ast.start, end: ast.end};
103+
if (loc) { delete ast.start; delete ast.end; }
104+
for (var name in ast) if (ast.hasOwnProperty(name)) mangle(ast[name]);
105+
if (loc) ast.loc = loc;
106+
}
107+
}

test/run.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var driver = require("./driver.js");
2+
require("..");
3+
require("./tests-jsx.js");
4+
5+
function group(name) {
6+
if (typeof console === "object" && console.group) {
7+
console.group(name);
8+
}
9+
}
10+
11+
function groupEnd() {
12+
if (typeof console === "object" && console.groupEnd) {
13+
console.groupEnd(name);
14+
}
15+
}
16+
17+
function log(title, message) {
18+
if (typeof console === "object") console.log(title, message);
19+
}
20+
21+
var stats, modes = {
22+
Normal: {
23+
config: {
24+
parse: require("acorn").parse
25+
}
26+
}
27+
};
28+
29+
function report(state, code, message) {
30+
if (state != "ok") {++stats.failed; log(code, message);}
31+
++stats.testsRun;
32+
}
33+
34+
group("Errors");
35+
36+
for (var name in modes) {
37+
group(name);
38+
var mode = modes[name];
39+
stats = mode.stats = {testsRun: 0, failed: 0};
40+
var t0 = +new Date;
41+
driver.runTests(mode.config, report);
42+
mode.stats.duration = +new Date - t0;
43+
groupEnd();
44+
}
45+
46+
groupEnd();
47+
48+
function outputStats(name, stats) {
49+
log(name + ":", stats.testsRun + " tests run in " + stats.duration + "ms; " +
50+
(stats.failed ? stats.failed + " failures." : "all passed."));
51+
}
52+
53+
var total = {testsRun: 0, failed: 0, duration: 0};
54+
55+
group("Stats");
56+
57+
for (var name in modes) {
58+
var stats = modes[name].stats;
59+
outputStats(name + " parser", stats);
60+
for (var key in stats) total[key] += stats[key];
61+
}
62+
63+
outputStats("Total", total);
64+
65+
groupEnd();
66+
67+
if (total.failed && typeof process === "object") {
68+
process.stdout.write("", function() {
69+
process.exit(1);
70+
});
71+
}

0 commit comments

Comments
 (0)