Skip to content

Commit dd2c869

Browse files
Use chai asserts.
1 parent aa30ac8 commit dd2c869

3 files changed

Lines changed: 35 additions & 83 deletions

File tree

src/harness/harness.ts

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -112,58 +112,37 @@ module Utils {
112112
});
113113
}
114114

115-
export function checkInvariants(node: ts.Node, parent: ts.Node): void {
116-
if(node) {
117-
if (node.pos < 0) {
118-
throw new Error("node.pos < 0");
119-
}
120-
if (node.end < 0) {
121-
throw new Error("node.end < 0");
122-
}
123-
if (node.end < node.pos) {
124-
throw new Error("node.end < node.pos");
125-
}
126-
if (node.parent !== parent) {
127-
throw new Error("node.parent !== parent");
128-
}
115+
export function assertInvariants(node: ts.Node, parent: ts.Node): void {
116+
if (node) {
117+
assert.isFalse(node.pos < 0, "node.pos < 0");
118+
assert.isFalse(node.end < 0, "node.end < 0");
119+
assert.isFalse(node.end < node.pos, "node.end < node.pos");
120+
assert.equal(node.parent, parent, "node.parent !== parent");
121+
129122
if (parent) {
130123
// Make sure each child is contained within the parent.
131-
if (node.pos < parent.pos) {
132-
throw new Error("node.pos < parent.pos");
133-
}
134-
if (node.end > parent.end) {
135-
throw new Error("node.end > parent.end");
136-
}
124+
assert.isFalse(node.pos < parent.pos, "node.pos < parent.pos");
125+
assert.isFalse(node.end > parent.end, "node.end > parent.end");
137126
}
138127

139128
ts.forEachChild(node, child => {
140-
checkInvariants(child, node);
129+
assertInvariants(child, node);
141130
});
142131

143132
// Make sure each of the children is in order.
144133
var currentPos = 0;
145134
ts.forEachChild(node,
146135
child => {
147-
if (child.pos < currentPos) {
148-
throw new Error("child.pos < currentPos");
149-
}
136+
assert.isFalse(child.pos < currentPos, "child.pos < currentPos");
150137
currentPos = child.end;
151138
},
152139
(array: ts.NodeArray<ts.Node>) => {
153-
if (array.pos < node.pos) {
154-
throw new Error("array.pos < node.pos");
155-
}
156-
if (array.end > node.end) {
157-
throw new Error("array.end > node.end");
158-
}
140+
assert.isFalse(array.pos < node.pos, "array.pos < node.pos");
141+
assert.isFalse(array.end > node.end, "array.end > node.end");
142+
assert.isFalse(array.pos < currentPos, "array.pos < currentPos");
159143

160-
if (array.pos < currentPos) {
161-
throw new Error("array.pos < currentPos");
162-
}
163144
for (var i = 0, n = array.length; i < n; i++) {
164-
if (array[i].pos < currentPos) {
165-
throw new Error("array[i].pos < currentPos");
166-
}
145+
assert.isFalse(array[i].pos < currentPos, "array[i].pos < currentPos");
167146
currentPos = array[i].end
168147
}
169148

@@ -179,9 +158,8 @@ module Utils {
179158
}
180159
var child = (<any>node)[childName];
181160
if (isNodeOrArray(child)) {
182-
if (childNodesAndArrays.indexOf(child) < 0) {
183-
throw new Error("Child when forEach'ing over node. " + (<any>ts).SyntaxKind[node.kind] + "-" + childName);
184-
}
161+
assert.isFalse(childNodesAndArrays.indexOf(child) < 0,
162+
"Missing child when forEach'ing over node: " + (<any>ts).SyntaxKind[node.kind] + "-" + childName);
185163
}
186164
}
187165
}

src/harness/test262Runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class Test262BaselineRunner extends RunnerBase {
189189

190190
it('satisfies invariants', () => {
191191
var sourceFile = testState.checker.getProgram().getSourceFile(Test262BaselineRunner.getTestFilePath(testState.filename));
192-
Utils.checkInvariants(sourceFile, /*parent:*/ undefined);
192+
Utils.assertInvariants(sourceFile, /*parent:*/ undefined);
193193
});
194194

195195
it('has the expected AST',() => {

tests/cases/unittests/incrementalParser.ts

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,25 @@ module ts {
3131
// are still ok and we're still appropriately reusing most of the tree.
3232
function compareTrees(oldText: IScriptSnapshot, newText: IScriptSnapshot, textChangeRange: TextChangeRange, expectedReusedElements: number, oldTree?: SourceFile): SourceFile {
3333
oldTree = oldTree || createTree(oldText, /*version:*/ ".");
34-
Utils.checkInvariants(oldTree, /*parent:*/ undefined);
34+
Utils.assertInvariants(oldTree, /*parent:*/ undefined);
3535

3636
// Create a tree for the new text, in a non-incremental fashion.
3737
var newTree = createTree(newText, oldTree.version + ".");
38-
Utils.checkInvariants(newTree, /*parent:*/ undefined);
38+
Utils.assertInvariants(newTree, /*parent:*/ undefined);
3939

4040
// Create a tree for the new text, in an incremental fashion.
4141
var incrementalNewTree = oldTree.update(newText, oldTree.version + ".", /*isOpen:*/ true, textChangeRange);
42-
Utils.checkInvariants(incrementalNewTree, /*parent:*/ undefined);
42+
Utils.assertInvariants(incrementalNewTree, /*parent:*/ undefined);
4343

4444
// We should get the same tree when doign a full or incremental parse.
4545
assertStructuralEquals(newTree, incrementalNewTree);
4646

4747
// There should be no reused nodes between two trees that are fully parsed.
48-
Debug.assert(reusedElements(oldTree, newTree) === 0);
48+
assert.isTrue(reusedElements(oldTree, newTree) === 0);
4949

5050
if (expectedReusedElements !== -1) {
5151
var actualReusedCount = reusedElements(oldTree, incrementalNewTree);
52-
Debug.assert(actualReusedCount === expectedReusedElements, actualReusedCount + " !== " + expectedReusedElements);
52+
assert.equal(actualReusedCount, expectedReusedElements, actualReusedCount + " !== " + expectedReusedElements);
5353
}
5454

5555
return incrementalNewTree;
@@ -60,29 +60,13 @@ module ts {
6060
return;
6161
}
6262

63-
if (!node1 || !node2) {
64-
throw new Error("!node1 || !node2");
65-
}
66-
67-
if (node1.pos !== node2.pos) {
68-
throw new Error("node1.pos !== node2.pos");
69-
}
70-
71-
if (node1.end !== node2.end) {
72-
throw new Error("node1.end !== node2.end");
73-
}
74-
75-
if (node1.kind !== node2.kind) {
76-
throw new Error("node1.kind !== node2.kind");
77-
}
78-
79-
if (node1.flags !== node2.flags) {
80-
throw new Error("node1.flags !== node2.flags");
81-
}
82-
83-
if (node1.parserContextFlags !== node2.parserContextFlags) {
84-
throw new Error("node1.parserContextFlags !== node2.parserContextFlags");
85-
}
63+
assert(node1, "node1");
64+
assert(node2, "node2");
65+
assert.equal(node1.pos, node2.pos, "node1.pos !== node2.pos");
66+
assert.equal(node1.end, node2.end, "node1.end !== node2.end");
67+
assert.equal(node1.kind, node2.kind, "node1.kind !== node2.kind");
68+
assert.equal(node1.flags, node2.flags, "node1.flags !== node2.flags");
69+
assert.equal(node1.parserContextFlags, node2.parserContextFlags, "node1.parserContextFlags !== node2.parserContextFlags");
8670

8771
forEachChild(node1,
8872
child1 => {
@@ -104,21 +88,11 @@ module ts {
10488
return;
10589
}
10690

107-
if (!array1 || !array2) {
108-
throw new Error("!array1 || !array2");
109-
}
110-
111-
if (array1.pos !== array2.pos) {
112-
throw new Error("array1.pos !== array2.pos");
113-
}
114-
115-
if (array1.end !== array2.end) {
116-
throw new Error("array1.end !== array2.end");
117-
}
118-
119-
if (array1.length !== array2.length) {
120-
throw new Error("array1.length !== array2.length");
121-
}
91+
assert(array1, "array1");
92+
assert(array2, "array2");
93+
assert.equal(array1.pos, array2.pos, "array1.pos !== array2.pos");
94+
assert.equal(array1.end, array2.end, "array1.end !== array2.end");
95+
assert.equal(array1.length, array2.length, "array1.length !== array2.length");
12296

12397
for (var i = 0, n = array1.length; i < n; i++) {
12498
assertStructuralEquals(array1[i], array2[i]);

0 commit comments

Comments
 (0)