Use chai asserts.

This commit is contained in:
Cyrus Najmabadi
2014-12-09 19:49:40 -08:00
parent aa30ac8a9c
commit dd2c869d7b
3 changed files with 35 additions and 83 deletions

View File

@@ -112,58 +112,37 @@ 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");
}
export function assertInvariants(node: ts.Node, parent: ts.Node): void {
if (node) {
assert.isFalse(node.pos < 0, "node.pos < 0");
assert.isFalse(node.end < 0, "node.end < 0");
assert.isFalse(node.end < node.pos, "node.end < node.pos");
assert.equal(node.parent, parent, "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");
}
assert.isFalse(node.pos < parent.pos, "node.pos < parent.pos");
assert.isFalse(node.end > parent.end, "node.end > parent.end");
}
ts.forEachChild(node, child => {
checkInvariants(child, node);
assertInvariants(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");
}
assert.isFalse(child.pos < currentPos, "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");
}
assert.isFalse(array.pos < node.pos, "array.pos < node.pos");
assert.isFalse(array.end > node.end, "array.end > node.end");
assert.isFalse(array.pos < currentPos, "array.pos < currentPos");
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");
}
assert.isFalse(array[i].pos < currentPos, "array[i].pos < currentPos");
currentPos = array[i].end
}
@@ -179,9 +158,8 @@ module Utils {
}
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);
}
assert.isFalse(childNodesAndArrays.indexOf(child) < 0,
"Missing child when forEach'ing over node: " + (<any>ts).SyntaxKind[node.kind] + "-" + childName);
}
}
}

View File

@@ -189,7 +189,7 @@ class Test262BaselineRunner extends RunnerBase {
it('satisfies invariants', () => {
var sourceFile = testState.checker.getProgram().getSourceFile(Test262BaselineRunner.getTestFilePath(testState.filename));
Utils.checkInvariants(sourceFile, /*parent:*/ undefined);
Utils.assertInvariants(sourceFile, /*parent:*/ undefined);
});
it('has the expected AST',() => {

View File

@@ -31,25 +31,25 @@ module ts {
// are still ok and we're still appropriately reusing most of the tree.
function compareTrees(oldText: IScriptSnapshot, newText: IScriptSnapshot, textChangeRange: TextChangeRange, expectedReusedElements: number, oldTree?: SourceFile): SourceFile {
oldTree = oldTree || createTree(oldText, /*version:*/ ".");
Utils.checkInvariants(oldTree, /*parent:*/ undefined);
Utils.assertInvariants(oldTree, /*parent:*/ undefined);
// Create a tree for the new text, in a non-incremental fashion.
var newTree = createTree(newText, oldTree.version + ".");
Utils.checkInvariants(newTree, /*parent:*/ undefined);
Utils.assertInvariants(newTree, /*parent:*/ undefined);
// Create a tree for the new text, in an incremental fashion.
var incrementalNewTree = oldTree.update(newText, oldTree.version + ".", /*isOpen:*/ true, textChangeRange);
Utils.checkInvariants(incrementalNewTree, /*parent:*/ undefined);
Utils.assertInvariants(incrementalNewTree, /*parent:*/ undefined);
// We should get the same tree when doign a full or incremental parse.
assertStructuralEquals(newTree, incrementalNewTree);
// There should be no reused nodes between two trees that are fully parsed.
Debug.assert(reusedElements(oldTree, newTree) === 0);
assert.isTrue(reusedElements(oldTree, newTree) === 0);
if (expectedReusedElements !== -1) {
var actualReusedCount = reusedElements(oldTree, incrementalNewTree);
Debug.assert(actualReusedCount === expectedReusedElements, actualReusedCount + " !== " + expectedReusedElements);
assert.equal(actualReusedCount, expectedReusedElements, actualReusedCount + " !== " + expectedReusedElements);
}
return incrementalNewTree;
@@ -60,29 +60,13 @@ module ts {
return;
}
if (!node1 || !node2) {
throw new Error("!node1 || !node2");
}
if (node1.pos !== node2.pos) {
throw new Error("node1.pos !== node2.pos");
}
if (node1.end !== node2.end) {
throw new Error("node1.end !== node2.end");
}
if (node1.kind !== node2.kind) {
throw new Error("node1.kind !== node2.kind");
}
if (node1.flags !== node2.flags) {
throw new Error("node1.flags !== node2.flags");
}
if (node1.parserContextFlags !== node2.parserContextFlags) {
throw new Error("node1.parserContextFlags !== node2.parserContextFlags");
}
assert(node1, "node1");
assert(node2, "node2");
assert.equal(node1.pos, node2.pos, "node1.pos !== node2.pos");
assert.equal(node1.end, node2.end, "node1.end !== node2.end");
assert.equal(node1.kind, node2.kind, "node1.kind !== node2.kind");
assert.equal(node1.flags, node2.flags, "node1.flags !== node2.flags");
assert.equal(node1.parserContextFlags, node2.parserContextFlags, "node1.parserContextFlags !== node2.parserContextFlags");
forEachChild(node1,
child1 => {
@@ -104,21 +88,11 @@ module ts {
return;
}
if (!array1 || !array2) {
throw new Error("!array1 || !array2");
}
if (array1.pos !== array2.pos) {
throw new Error("array1.pos !== array2.pos");
}
if (array1.end !== array2.end) {
throw new Error("array1.end !== array2.end");
}
if (array1.length !== array2.length) {
throw new Error("array1.length !== array2.length");
}
assert(array1, "array1");
assert(array2, "array2");
assert.equal(array1.pos, array2.pos, "array1.pos !== array2.pos");
assert.equal(array1.end, array2.end, "array1.end !== array2.end");
assert.equal(array1.length, array2.length, "array1.length !== array2.length");
for (var i = 0, n = array1.length; i < n; i++) {
assertStructuralEquals(array1[i], array2[i]);