Initial test harness for incremental parser tests.

This commit is contained in:
Cyrus Najmabadi
2014-12-09 16:39:52 -08:00
parent c17eb7df18
commit fa4b68fa6c
6 changed files with 252 additions and 80 deletions

View File

@@ -111,6 +111,85 @@ module Utils {
}
});
}
export function checkInvariants(node: ts.Node, parent: ts.Node): void {
if(node) {
if (node.pos < 0) {
throw new Error("node.pos < 0");
}
if (node.end < 0) {
throw new Error("node.end < 0");
}
if (node.end < node.pos) {
throw new Error("node.end < node.pos");
}
if (node.parent !== parent) {
throw new Error("node.parent !== parent");
}
if (parent) {
// Make sure each child is contained within the parent.
if (node.pos < parent.pos) {
throw new Error("node.pos < parent.pos");
}
if (node.end > parent.end) {
throw new Error("node.end > parent.end");
}
}
ts.forEachChild(node, child => {
checkInvariants(child, node);
});
// Make sure each of the children is in order.
var currentPos = 0;
ts.forEachChild(node,
child => {
if (child.pos < currentPos) {
throw new Error("child.pos < currentPos");
}
currentPos = child.end;
},
(array: ts.NodeArray<ts.Node>) => {
if (array.pos < node.pos) {
throw new Error("array.pos < node.pos");
}
if (array.end > node.end) {
throw new Error("array.end > node.end");
}
if (array.pos < currentPos) {
throw new Error("array.pos < currentPos");
}
for (var i = 0, n = array.length; i < n; i++) {
if (array[i].pos < currentPos) {
throw new Error("array[i].pos < currentPos");
}
currentPos = array[i].end
}
currentPos = array.end;
});
var childNodesAndArrays: any[] = [];
ts.forEachChild(node, child => { childNodesAndArrays.push(child) }, array => { childNodesAndArrays.push(array) });
for (var childName in node) {
if (childName === "parent" || childName === "nextContainer" || childName === "modifiers" || childName === "externalModuleIndicator") {
continue;
}
var child = (<any>node)[childName];
if (isNodeOrArray(child)) {
if (childNodesAndArrays.indexOf(child) < 0) {
throw new Error("Child when forEach'ing over node. " + (<any>ts).SyntaxKind[node.kind] + "-" + childName);
}
}
}
}
}
function isNodeOrArray(a: any): boolean {
return a !== undefined && typeof a.pos === "number";
}
}
module Harness.Path {

View File

@@ -21,81 +21,6 @@ class Test262BaselineRunner extends RunnerBase {
return Test262BaselineRunner.basePath + "/" + filename;
}
private static checkInvariants(node: ts.Node, parent: ts.Node): void {
if (node) {
if (node.pos < 0) {
throw new Error("node.pos < 0");
}
if (node.end < 0) {
throw new Error("node.end < 0");
}
if (node.end < node.pos) {
throw new Error("node.end < node.pos");
}
if (node.parent !== parent) {
throw new Error("node.parent !== parent");
}
if (parent) {
// Make sure each child is contained within the parent.
if (node.pos < parent.pos) {
throw new Error("node.pos < parent.pos");
}
if (node.end > parent.end) {
throw new Error("node.end > parent.end");
}
}
ts.forEachChild(node, child => {
Test262BaselineRunner.checkInvariants(child, node);
});
// Make sure each of the children is in order.
var currentPos = 0;
ts.forEachChild(node,
child => {
if (child.pos < currentPos) {
throw new Error("child.pos < currentPos");
}
currentPos = child.end;
},
(array: ts.NodeArray<ts.Node>) => {
if (array.pos < node.pos) {
throw new Error("array.pos < node.pos");
}
if (array.end > node.end) {
throw new Error("array.end > node.end");
}
if (array.pos < currentPos) {
throw new Error("array.pos < currentPos");
}
for (var i = 0, n = array.length; i < n; i++) {
if (array[i].pos < currentPos) {
throw new Error("array[i].pos < currentPos");
}
currentPos = array[i].end
}
currentPos = array.end;
});
var childNodesAndArrays: any[] = [];
ts.forEachChild(node, child => { childNodesAndArrays.push(child) }, array => { childNodesAndArrays.push(array) });
for (var childName in node) {
if (childName === "parent" || childName === "nextContainer" || childName === "modifiers" || childName === "externalModuleIndicator") {
continue;
}
var child = (<any>node)[childName];
if (Test262BaselineRunner.isNodeOrArray(child)) {
if (childNodesAndArrays.indexOf(child) < 0) {
throw new Error("Child when forEach'ing over node. " + (<any>ts).SyntaxKind[node.kind] + "-" + childName);
}
}
}
}
}
private static serializeSourceFile(file: ts.SourceFile): string {
function getKindName(k: number): string {
return (<any>ts).SyntaxKind[k]
@@ -264,7 +189,7 @@ class Test262BaselineRunner extends RunnerBase {
it('satisfies invariants', () => {
var sourceFile = testState.checker.getProgram().getSourceFile(Test262BaselineRunner.getTestFilePath(testState.filename));
Test262BaselineRunner.checkInvariants(sourceFile, /*parent:*/ undefined);
Utils.checkInvariants(sourceFile, /*parent:*/ undefined);
});
it('has the expected AST',() => {