Skip to content

Commit

Permalink
testsuite
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Feb 24, 2025
1 parent 85c413a commit 26f37d9
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/PHPUnit/TestCollection/TestCollection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ describe('TestCollection', () => {
const shouldBe = async (collection: TestCollection, testsuites: any) => {
const phpUnitXML = new PHPUnitXML();
phpUnitXML.setRoot(phpUnitProject(''));
for (const [name, files] of Object.entries(testsuites)) {
for (const [testsuite, files] of Object.entries(testsuites)) {
const expected: TestDefinition[] = [];
for (const uri of (files as URI[])) {
const testParser = new TestParser(phpUnitXML);
testParser.on(TestType.method, (testDefinition) => expected.push(testDefinition));
testParser.on(TestType.class, (testDefinition) => expected.push(testDefinition));
testParser.on(TestType.namespace, (testDefinition) => expected.push(testDefinition));

await testParser.parseFile(uri.fsPath);
await testParser.parseFile(uri.fsPath, testsuite);
}
const actual: TestDefinition[] = [];
collection.items().get(name)?.items().forEach((item) => actual.push(...item));
collection.items().get(testsuite)?.items().forEach((item) => actual.push(...item));
expect(actual).toEqual(expected);
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/PHPUnit/TestCollection/TestCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ export class TestCollection {
return undefined;
}

protected async parseTests(uri: URI, _testsuite: string) {
protected async parseTests(uri: URI, testsuite: string) {
const { testParser, testDefinitionBuilder } = this.createTestParser();
await testParser.parseFile(uri.fsPath);
await testParser.parseFile(uri.fsPath, testsuite);

return testDefinitionBuilder.get();
}
Expand Down
11 changes: 6 additions & 5 deletions src/PHPUnit/TestParser/TestParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export class TestParser {
this.eventEmitter.on(`${eventName}`, callback);
}

async parseFile(file: string) {
return this.parse(textDecoder.decode(await readFile(file)), file);
async parseFile(file: string, testsuite?: string) {
return this.parse(textDecoder.decode(await readFile(file)), file, testsuite);
}

parse(text: Buffer | string, file: string) {
parse(text: Buffer | string, file: string, testsuite?: string) {
text = text.toString();

// Todo https://github.com/glayzzle/php-parser/issues/170
Expand All @@ -49,19 +49,20 @@ export class TestParser {
}
});

return this.parseAst(ast, file);
return this.parseAst(ast, file, testsuite);
} catch (e) {
console.error(e);

return undefined;
}
}

private parseAst(declaration: Declaration | Node, file: string): TestDefinition[] | undefined {
private parseAst(declaration: Declaration | Node, file: string, testsuite?: string): TestDefinition[] | undefined {
const definition = new PHPDefinition(declaration, { phpUnitXML: this.phpUnitXML, file });

for (const parser of this.parsers) {
const tests = parser.parse(definition);
tests?.forEach((testDefinition) => testDefinition.testsuite = testsuite);
if (tests) {
return this.emit(tests);
}
Expand Down
1 change: 1 addition & 0 deletions src/PHPUnit/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export type TestDefinition = {
start?: Position;
end?: Position;
annotations?: Annotations;
testsuite?: string;
};
4 changes: 2 additions & 2 deletions src/TestCollection/TestCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export class TestCollection extends BaseTestCollection {

protected async parseTests(uri: URI, testsuite: string) {
const { testParser, testDefinitionBuilder } = this.createTestParser();
const testHierarchyBuilder = new TestHierarchyBuilder(this.ctrl, testParser, testsuite);
await testParser.parseFile(uri.fsPath);
const testHierarchyBuilder = new TestHierarchyBuilder(this.ctrl, testParser);
await testParser.parseFile(uri.fsPath, testsuite);

this.removeTestItems(uri);
const testData = this.getTestCases(uri);
Expand Down
9 changes: 4 additions & 5 deletions src/TestCollection/TestHierarchyBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ describe('TestHierarchyBuilder', () => {
), configurationFile);

const testParser = new TestParser(phpUnitXml);
const builder = new TestHierarchyBuilder(ctrl, testParser);
codes.map(({ testsuite, file, code }) => {
const builder = new TestHierarchyBuilder(ctrl, testParser, testsuite.name);
testParser.parse(code, file);

return builder;
}).forEach(builder => builder.get());
testParser.parse(code, file, testsuite.name);
});
builder.get();
};

beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/TestCollection/TestHierarchyBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class TestHierarchyBuilder {
];
private testData = new CustomWeakMap<TestItem, TestCase>();

constructor(private ctrl: TestController, private testParser: TestParser, private testsuite: string = 'default') {
constructor(private ctrl: TestController, private testParser: TestParser) {
this.onInit();
}

Expand Down

0 comments on commit 26f37d9

Please sign in to comment.