Skip to content

Commit aea499e

Browse files
Merge pull request microsoft#1422 from Microsoft/incrementalTests
Initial test harness for incremental parser tests.
2 parents c17eb7d + dd2c869 commit aea499e

5 files changed

Lines changed: 794 additions & 80 deletions

File tree

Jakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ var harnessSources = [
8484
].map(function (f) {
8585
return path.join(harnessDirectory, f);
8686
}).concat([
87+
"incrementalParser.ts",
8788
"services/colorization.ts",
8889
"services/documentRegistry.ts",
8990
"services/preProcessFile.ts"

src/harness/harness.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,63 @@ module Utils {
111111
}
112112
});
113113
}
114+
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+
122+
if (parent) {
123+
// Make sure each child is contained within the parent.
124+
assert.isFalse(node.pos < parent.pos, "node.pos < parent.pos");
125+
assert.isFalse(node.end > parent.end, "node.end > parent.end");
126+
}
127+
128+
ts.forEachChild(node, child => {
129+
assertInvariants(child, node);
130+
});
131+
132+
// Make sure each of the children is in order.
133+
var currentPos = 0;
134+
ts.forEachChild(node,
135+
child => {
136+
assert.isFalse(child.pos < currentPos, "child.pos < currentPos");
137+
currentPos = child.end;
138+
},
139+
(array: ts.NodeArray<ts.Node>) => {
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");
143+
144+
for (var i = 0, n = array.length; i < n; i++) {
145+
assert.isFalse(array[i].pos < currentPos, "array[i].pos < currentPos");
146+
currentPos = array[i].end
147+
}
148+
149+
currentPos = array.end;
150+
});
151+
152+
var childNodesAndArrays: any[] = [];
153+
ts.forEachChild(node, child => { childNodesAndArrays.push(child) }, array => { childNodesAndArrays.push(array) });
154+
155+
for (var childName in node) {
156+
if (childName === "parent" || childName === "nextContainer" || childName === "modifiers" || childName === "externalModuleIndicator") {
157+
continue;
158+
}
159+
var child = (<any>node)[childName];
160+
if (isNodeOrArray(child)) {
161+
assert.isFalse(childNodesAndArrays.indexOf(child) < 0,
162+
"Missing child when forEach'ing over node: " + (<any>ts).SyntaxKind[node.kind] + "-" + childName);
163+
}
164+
}
165+
}
166+
}
167+
168+
function isNodeOrArray(a: any): boolean {
169+
return a !== undefined && typeof a.pos === "number";
170+
}
114171
}
115172

116173
module Harness.Path {

src/harness/test262Runner.ts

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -21,81 +21,6 @@ class Test262BaselineRunner extends RunnerBase {
2121
return Test262BaselineRunner.basePath + "/" + filename;
2222
}
2323

24-
private static checkInvariants(node: ts.Node, parent: ts.Node): void {
25-
if (node) {
26-
if (node.pos < 0) {
27-
throw new Error("node.pos < 0");
28-
}
29-
if (node.end < 0) {
30-
throw new Error("node.end < 0");
31-
}
32-
if (node.end < node.pos) {
33-
throw new Error("node.end < node.pos");
34-
}
35-
if (node.parent !== parent) {
36-
throw new Error("node.parent !== parent");
37-
}
38-
if (parent) {
39-
// Make sure each child is contained within the parent.
40-
if (node.pos < parent.pos) {
41-
throw new Error("node.pos < parent.pos");
42-
}
43-
if (node.end > parent.end) {
44-
throw new Error("node.end > parent.end");
45-
}
46-
}
47-
48-
ts.forEachChild(node, child => {
49-
Test262BaselineRunner.checkInvariants(child, node);
50-
});
51-
52-
// Make sure each of the children is in order.
53-
var currentPos = 0;
54-
ts.forEachChild(node,
55-
child => {
56-
if (child.pos < currentPos) {
57-
throw new Error("child.pos < currentPos");
58-
}
59-
currentPos = child.end;
60-
},
61-
(array: ts.NodeArray<ts.Node>) => {
62-
if (array.pos < node.pos) {
63-
throw new Error("array.pos < node.pos");
64-
}
65-
if (array.end > node.end) {
66-
throw new Error("array.end > node.end");
67-
}
68-
69-
if (array.pos < currentPos) {
70-
throw new Error("array.pos < currentPos");
71-
}
72-
for (var i = 0, n = array.length; i < n; i++) {
73-
if (array[i].pos < currentPos) {
74-
throw new Error("array[i].pos < currentPos");
75-
}
76-
currentPos = array[i].end
77-
}
78-
79-
currentPos = array.end;
80-
});
81-
82-
var childNodesAndArrays: any[] = [];
83-
ts.forEachChild(node, child => { childNodesAndArrays.push(child) }, array => { childNodesAndArrays.push(array) });
84-
85-
for (var childName in node) {
86-
if (childName === "parent" || childName === "nextContainer" || childName === "modifiers" || childName === "externalModuleIndicator") {
87-
continue;
88-
}
89-
var child = (<any>node)[childName];
90-
if (Test262BaselineRunner.isNodeOrArray(child)) {
91-
if (childNodesAndArrays.indexOf(child) < 0) {
92-
throw new Error("Child when forEach'ing over node. " + (<any>ts).SyntaxKind[node.kind] + "-" + childName);
93-
}
94-
}
95-
}
96-
}
97-
}
98-
9924
private static serializeSourceFile(file: ts.SourceFile): string {
10025
function getKindName(k: number): string {
10126
return (<any>ts).SyntaxKind[k]
@@ -264,7 +189,7 @@ class Test262BaselineRunner extends RunnerBase {
264189

265190
it('satisfies invariants', () => {
266191
var sourceFile = testState.checker.getProgram().getSourceFile(Test262BaselineRunner.getTestFilePath(testState.filename));
267-
Test262BaselineRunner.checkInvariants(sourceFile, /*parent:*/ undefined);
192+
Utils.assertInvariants(sourceFile, /*parent:*/ undefined);
268193
});
269194

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

src/services/services.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,7 @@ module ts {
16791679
var scriptSnapshot = this.hostCache.getScriptSnapshot(filename);
16801680

16811681
var start = new Date().getTime();
1682-
sourceFile = createSourceFileFromScriptSnapshot(filename, scriptSnapshot, getDefaultCompilerOptions(), version, /*isOpen*/ true);
1682+
sourceFile = createLanguageServiceSourceFile(filename, scriptSnapshot, getDefaultCompilerOptions(), version, /*isOpen*/ true);
16831683
this.host.log("SyntaxTreeCache.Initialize: createSourceFile: " + (new Date().getTime() - start));
16841684

16851685
var start = new Date().getTime();
@@ -1692,7 +1692,7 @@ module ts {
16921692

16931693
var start = new Date().getTime();
16941694
sourceFile = !editRange
1695-
? createSourceFileFromScriptSnapshot(filename, scriptSnapshot, getDefaultCompilerOptions(), version, /*isOpen*/ true)
1695+
? createLanguageServiceSourceFile(filename, scriptSnapshot, getDefaultCompilerOptions(), version, /*isOpen*/ true)
16961696
: this.currentSourceFile.update(scriptSnapshot, version, /*isOpen*/ true, editRange);
16971697
this.host.log("SyntaxTreeCache.Initialize: updateSourceFile: " + (new Date().getTime() - start));
16981698

@@ -1718,7 +1718,7 @@ module ts {
17181718
}
17191719
}
17201720

1721-
function createSourceFileFromScriptSnapshot(filename: string, scriptSnapshot: IScriptSnapshot, settings: CompilerOptions, version: string, isOpen: boolean) {
1721+
export function createLanguageServiceSourceFile(filename: string, scriptSnapshot: IScriptSnapshot, settings: CompilerOptions, version: string, isOpen: boolean): SourceFile {
17221722
return SourceFileObject.createSourceFileObject(filename, scriptSnapshot, settings.target, version, isOpen);
17231723
}
17241724

@@ -1769,7 +1769,7 @@ module ts {
17691769
var bucket = getBucketForCompilationSettings(compilationSettings, /*createIfMissing*/ true);
17701770
var entry = lookUp(bucket, filename);
17711771
if (!entry) {
1772-
var sourceFile = createSourceFileFromScriptSnapshot(filename, scriptSnapshot, compilationSettings, version, isOpen);
1772+
var sourceFile = createLanguageServiceSourceFile(filename, scriptSnapshot, compilationSettings, version, isOpen);
17731773

17741774
bucket[filename] = entry = {
17751775
sourceFile: sourceFile,

0 commit comments

Comments
 (0)