Skip to content

Commit 7b60f35

Browse files
authored
Simplify XCTestOutputParser Tests (#1392)
To write a new one of these tests you had to define all the participating test names in the TestRunState at the begining of the test. Remove this constraint by returning an enqueued mock test item every time one is requested that hasn't been seen.
1 parent ef0f5de commit 7b60f35

File tree

3 files changed

+212
-228
lines changed

3 files changed

+212
-228
lines changed

test/integration-tests/testexplorer/MockTestRunState.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,26 @@ interface ITestItemFinder {
4444
}
4545

4646
export class DarwinTestItemFinder implements ITestItemFinder {
47-
constructor(public tests: TestItem[]) {}
47+
tests: TestItem[] = [];
4848
getIndex(id: string): number {
49-
return this.tests.findIndex(item => item.name === id);
49+
const index = this.tests.findIndex(item => item.name === id);
50+
if (index === -1) {
51+
this.tests.push({ name: id, status: TestStatus.enqueued, output: [] });
52+
return this.tests.length - 1;
53+
}
54+
return index;
5055
}
5156
}
5257

5358
export class NonDarwinTestItemFinder implements ITestItemFinder {
54-
constructor(public tests: TestItem[]) {}
59+
tests: TestItem[] = [];
5560
getIndex(id: string): number {
56-
return this.tests.findIndex(item => item.name.endsWith(id));
61+
const index = this.tests.findIndex(item => item.name.endsWith(id));
62+
if (index === -1) {
63+
this.tests.push({ name: id, status: TestStatus.enqueued, output: [] });
64+
return this.tests.length - 1;
65+
}
66+
return index;
5767
}
5868
}
5969

@@ -75,14 +85,11 @@ export class TestRunState implements ITestRunState {
7585
return this.testItemFinder.tests;
7686
}
7787

78-
constructor(testNames: string[], darwin: boolean) {
79-
const tests = testNames.map(name => {
80-
return { name: name, status: TestStatus.enqueued, output: [] };
81-
});
88+
constructor(darwin: boolean) {
8289
if (darwin) {
83-
this.testItemFinder = new DarwinTestItemFinder(tests);
90+
this.testItemFinder = new DarwinTestItemFinder();
8491
} else {
85-
this.testItemFinder = new NonDarwinTestItemFinder(tests);
92+
this.testItemFinder = new NonDarwinTestItemFinder();
8693
}
8794
}
8895

test/integration-tests/testexplorer/SwiftTestingOutputParser.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ class TestEventStream {
4141

4242
suite("SwiftTestingOutputParser Suite", () => {
4343
let outputParser: SwiftTestingOutputParser;
44+
let testRunState: TestRunState;
4445

4546
beforeEach(() => {
4647
outputParser = new SwiftTestingOutputParser(
4748
() => {},
4849
() => {},
4950
() => {}
5051
);
52+
testRunState = new TestRunState(true);
5153
});
5254

5355
type ExtractPayload<T> = T extends { payload: infer E } ? E : never;
@@ -76,7 +78,6 @@ suite("SwiftTestingOutputParser Suite", () => {
7678
}
7779

7880
test("Passed test", async () => {
79-
const testRunState = new TestRunState(["MyTests.MyTests/testPass()"], true);
8081
const events = new TestEventStream([
8182
testEvent("runStarted"),
8283
testEvent("testCaseStarted", "MyTests.MyTests/testPass()"),
@@ -97,7 +98,6 @@ suite("SwiftTestingOutputParser Suite", () => {
9798
});
9899

99100
test("Skipped test", async () => {
100-
const testRunState = new TestRunState(["MyTests.MyTests/testSkip()"], true);
101101
const events = new TestEventStream([
102102
testEvent("runStarted"),
103103
testEvent("testSkipped", "MyTests.MyTests/testSkip()"),
@@ -116,7 +116,6 @@ suite("SwiftTestingOutputParser Suite", () => {
116116
});
117117

118118
async function performTestFailure(messages: EventMessage[]) {
119-
const testRunState = new TestRunState(["MyTests.MyTests/testFail()"], true);
120119
const issueLocation = {
121120
_filePath: "file:///some/file.swift",
122121
line: 1,
@@ -174,7 +173,6 @@ suite("SwiftTestingOutputParser Suite", () => {
174173
});
175174

176175
test("Parameterized test", async () => {
177-
const testRunState = new TestRunState(["MyTests.MyTests/testParameterized()"], true);
178176
const events = new TestEventStream([
179177
{
180178
kind: "test",
@@ -270,10 +268,6 @@ suite("SwiftTestingOutputParser Suite", () => {
270268
});
271269

272270
test("Output is captured", async () => {
273-
const testRunState = new TestRunState(
274-
["MyTests.MyTests/testOutput()", "MyTests.MyTests/testOutput2()"],
275-
true
276-
);
277271
const symbol = TestSymbol.pass;
278272
const makeEvent = (kind: ExtractPayload<EventRecord>["kind"], testId?: string) =>
279273
testEvent(kind, testId, [{ text: kind, symbol }]);

0 commit comments

Comments
 (0)