Add esprima-derived tests, trivial benchmark
This commit is contained in:
parent
aedcd4e12f
commit
587c790f7b
5
acorn.js
5
acorn.js
@ -194,7 +194,6 @@
|
||||
case _switch:
|
||||
next();
|
||||
node.discriminant = parseParenExpression();
|
||||
node.lexical = false;
|
||||
node.cases = [];
|
||||
expect(_braceL);
|
||||
labels.push({kind: "switch"});
|
||||
@ -346,7 +345,7 @@
|
||||
var decl = startNode();
|
||||
decl.id = parseIdent();
|
||||
if (strict && strictBadWords.test(decl.id.name))
|
||||
raise(id.start, "Binding " + decl.id.name + " in strict mode");
|
||||
raise(decl.id.start, "Binding " + decl.id.name + " in strict mode");
|
||||
decl.init = eat(_eq) ? parseExpression(true, noIn) : null;
|
||||
node.declarations.push(finishNode(decl, "VariableDeclarator"));
|
||||
if (!eat(_comma)) break;
|
||||
@ -650,7 +649,7 @@
|
||||
"typeof": {keyword: "typeof", prefix: true},
|
||||
"void": {keyword: "void", prefix: true},
|
||||
"delete": {keyword: "delete", prefix: true}};
|
||||
var keywords = /^(?:break|case|catch|const|continue|debugger|default|do|else|finally|for|function|if|return|switch|throw|try|var|while|with|null|true|false|instanceof|typeof|void|delete|new|in)$/;
|
||||
var keywords = /^(?:break|case|catch|continue|debugger|default|do|else|finally|for|function|if|return|switch|throw|try|var|while|with|null|true|false|instanceof|typeof|void|delete|new|in)$/;
|
||||
|
||||
var _bracketL = {type: "[", beforeExpr: true}, _bracketR = {type: "]"}, _braceL = {type: "{", beforeExpr: true}, _braceR = {type: "}"}, _parenL = {type: "(", beforeExpr: true}, _parenR = {type: ")"}, _comma = {type: ",", beforeExpr: true}, _semi = {type: ";", beforeExpr: true}, _colon = {type: ":", beforeExpr: true}, _dot = {type: "."}, _question = {type: "?", beforeExpr: true};
|
||||
var puncTypes = {"[": _bracketL, "]": _bracketR, "{": _braceL, "}": _braceR, "(": _parenL,
|
||||
|
||||
18
test/bench.html
Normal file
18
test/bench.html
Normal file
@ -0,0 +1,18 @@
|
||||
<!doctype html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Acorn benchmark</title>
|
||||
<script src="../acorn.js"></script>
|
||||
<script src="jquery-string.js"></script>
|
||||
</head>
|
||||
|
||||
<button onclick="test()">GO</button>
|
||||
|
||||
<script>
|
||||
function test() {
|
||||
for (var i =0, t0 = +new Date; i < 10; ++i) acorn.parse(jquery164);
|
||||
var n = (+new Date - t0) + "ms";
|
||||
console.log(n);
|
||||
document.body.appendChild(document.createElement("pre")).innerHTML = n;
|
||||
}
|
||||
</script>
|
||||
58
test/driver.js
Normal file
58
test/driver.js
Normal file
@ -0,0 +1,58 @@
|
||||
var tests = [];
|
||||
|
||||
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});
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
23
test/index.html
Normal file
23
test/index.html
Normal file
@ -0,0 +1,23 @@
|
||||
<!doctype html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Acorn test suite</title>
|
||||
<script src="../acorn.js"></script>
|
||||
<script src="driver.js"></script>
|
||||
<script src="tests.js"></script>
|
||||
</head>
|
||||
|
||||
<script>
|
||||
var testsRun = 0, failed = 0;
|
||||
function report(state, code, message) {
|
||||
if (state != "ok") {++failed; console.log(code, message);}
|
||||
++testsRun;
|
||||
}
|
||||
window.onload = function(){
|
||||
var t0 = +new Date;
|
||||
runTests(report);
|
||||
console.log(testsRun + " tests run in " + (+new Date - t0) + "ms");
|
||||
if (failed) console.log(failed + " failures.");
|
||||
else console.log("All passed.");
|
||||
};
|
||||
</script>
|
||||
9048
test/jquery-string.js
vendored
Normal file
9048
test/jquery-string.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
11918
test/tests.js
Normal file
11918
test/tests.js
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user