Skip to content

Commit

Permalink
Simplify XCTestOutputParser Tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
plemarquand committed Feb 16, 2025
1 parent 2c43d6f commit 6cf03d7
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 228 deletions.
27 changes: 17 additions & 10 deletions test/integration-tests/testexplorer/MockTestRunState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,26 @@ interface ITestItemFinder {
}

export class DarwinTestItemFinder implements ITestItemFinder {
constructor(public tests: TestItem[]) {}
tests: TestItem[] = [];
getIndex(id: string): number {
return this.tests.findIndex(item => item.name === id);
const index = this.tests.findIndex(item => item.name === id);
if (index === -1) {
this.tests.push({ name: id, status: TestStatus.enqueued, output: [] });
return this.tests.length - 1;
}
return index;
}
}

export class NonDarwinTestItemFinder implements ITestItemFinder {
constructor(public tests: TestItem[]) {}
tests: TestItem[] = [];
getIndex(id: string): number {
return this.tests.findIndex(item => item.name.endsWith(id));
const index = this.tests.findIndex(item => item.name.endsWith(id));
if (index === -1) {
this.tests.push({ name: id, status: TestStatus.enqueued, output: [] });
return this.tests.length - 1;
}
return index;
}
}

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

constructor(testNames: string[], darwin: boolean) {
const tests = testNames.map(name => {
return { name: name, status: TestStatus.enqueued, output: [] };
});
constructor(darwin: boolean) {
if (darwin) {
this.testItemFinder = new DarwinTestItemFinder(tests);
this.testItemFinder = new DarwinTestItemFinder();
} else {
this.testItemFinder = new NonDarwinTestItemFinder(tests);
this.testItemFinder = new NonDarwinTestItemFinder();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ class TestEventStream {

suite("SwiftTestingOutputParser Suite", () => {
let outputParser: SwiftTestingOutputParser;
let testRunState: TestRunState;

beforeEach(() => {
outputParser = new SwiftTestingOutputParser(
() => {},
() => {},
() => {}
);
testRunState = new TestRunState(true);
});

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

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

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

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

test("Parameterized test", async () => {
const testRunState = new TestRunState(["MyTests.MyTests/testParameterized()"], true);
const events = new TestEventStream([
{
kind: "test",
Expand Down Expand Up @@ -270,10 +268,6 @@ suite("SwiftTestingOutputParser Suite", () => {
});

test("Output is captured", async () => {
const testRunState = new TestRunState(
["MyTests.MyTests/testOutput()", "MyTests.MyTests/testOutput2()"],
true
);
const symbol = TestSymbol.pass;
const makeEvent = (kind: ExtractPayload<EventRecord>["kind"], testId?: string) =>
testEvent(kind, testId, [{ text: kind, symbol }]);
Expand Down
Loading

0 comments on commit 6cf03d7

Please sign in to comment.