diff --git a/src/parser/resolver/types/abstract-collector.ts b/src/parser/resolver/types/abstract-collector.ts index 0cb8a487..64a8fcfc 100644 --- a/src/parser/resolver/types/abstract-collector.ts +++ b/src/parser/resolver/types/abstract-collector.ts @@ -5,7 +5,7 @@ import { FileNameStrategy } from "./resolver-strategy/filename-resolver.js"; type TypeName = string; export type TypeInfo = { - node: SyntaxNode; + node?: SyntaxNode; namespace: string; typeName: string; classType: ClassType; diff --git a/src/parser/resolver/types/resolver-strategy/types-query-strategy.test.ts b/src/parser/resolver/types/resolver-strategy/types-query-strategy.test.ts index 8f609609..983c7ee0 100644 --- a/src/parser/resolver/types/resolver-strategy/types-query-strategy.test.ts +++ b/src/parser/resolver/types/resolver-strategy/types-query-strategy.test.ts @@ -5,6 +5,7 @@ import { Configuration } from "../../../configuration.js"; import { type ParsedFile } from "../../../metrics/metric.js"; import { CSharpCollector } from "../c-sharp-collector.js"; import { type AbstractCollector, type TypeInfo } from "../abstract-collector.js"; +import { PHPCollector } from "../php-collector.js"; import { TypesQueryStrategy } from "./types-query-strategy.js"; async function getConfiguration(filePath: string): Promise { @@ -23,8 +24,6 @@ async function getConfiguration(filePath: string): Promise { describe("Types Query strategy", () => { describe("function getTypesFromFile()", () => { it("should calculate types declarations for interfaces", async () => { - /* Since we add the "node" field to TypeInfo, we have to mock that node to run this test. - This makes the test very complicated... // Given const filePath = "resources/c-sharp/relation-between-interfaces-in-one-file/Program.cs"; const parsedFile: ParsedFile = (await parse( @@ -33,7 +32,7 @@ describe("Types Query strategy", () => { )) as ParsedFile; const csharpCollector: AbstractCollector = new CSharpCollector(); - const result: Map = new Map(); + const result: Map = new Map(); result.set("mainNamespace.FirstInterface", { namespace: "mainNamespace", @@ -60,13 +59,119 @@ describe("Types Query strategy", () => { implementedFrom: [], }); // When + const typesFromFile: Map = new TypesQueryStrategy().getTypesFromFile( + parsedFile, + ".", + csharpCollector.getTypesQuery(), + ); + // Then + for (const [key, value] of typesFromFile.entries()) { + expect(value).toEqual(expect.objectContaining(result.get(key))); + } + }); + + it("should calculate correct types from multiple namespaces within one file for c-sharp", async () => { + // Given + const filePath = "resources/c-sharp/coupling-examples/BlubController.cs"; + const parsedFile: ParsedFile = (await parse( + filePath, + await getConfiguration(filePath), + )) as ParsedFile; + const csharpCollector: AbstractCollector = new CSharpCollector(); + + const result: Map = new Map(); + + result.set("App.CouplingExamplesOne.BlubControllerOne1", { + namespace: "App.CouplingExamplesOne", + typeName: "BlubControllerOne1", + classType: "class", + sourceFile: filePath, + namespaceDelimiter: ".", + implementedFrom: [], + }); + result.set("App.CouplingExamplesOne.BlubControllerOne2", { + namespace: "App.CouplingExamplesOne", + typeName: "BlubControllerOne2", + classType: "class", + sourceFile: filePath, + namespaceDelimiter: ".", + implementedFrom: [], + }); + result.set("App.CouplingExamplesTwo.BlubControllerTwo1", { + namespace: "App.CouplingExamplesTwo", + typeName: "BlubControllerTwo1", + classType: "class", + sourceFile: filePath, + namespaceDelimiter: ".", + implementedFrom: [], + }); + result.set("App.CouplingExamplesTwo.BlubControllerTwo2", { + namespace: "App.CouplingExamplesTwo", + typeName: "BlubControllerTwo2", + classType: "class", + sourceFile: filePath, + namespaceDelimiter: ".", + implementedFrom: [], + }); + // When const typesFromFile = new TypesQueryStrategy().getTypesFromFile( parsedFile, ".", csharpCollector.getTypesQuery(), ); // Then - expect(typesFromFile).toStrictEqual(result); */ + for (const [key, value] of typesFromFile.entries()) { + expect(value).toEqual(expect.objectContaining(result.get(key))); + } + }); + + it("should calculate correct types from multiple namespaces within one file for php", async () => { + // Given + const filePath = "resources/php/coupling-examples/BlubController.php"; + const parsedFile: ParsedFile = (await parse( + filePath, + await getConfiguration(filePath), + )) as ParsedFile; + const phpCollector: PHPCollector = new PHPCollector(); + + const result: Map = new Map(); + + result.set("App\\CouplingExamplesOne\\BlubControllerOne1", { + namespace: "App\\CouplingExamplesOne", + typeName: "BlubControllerOne1", + classType: "class", + sourceFile: filePath, + namespaceDelimiter: "\\", + implementedFrom: ["ControllerInterface"], + }); + result.set("App\\CouplingExamplesOne\\BlubControllerOne2", { + namespace: "App\\CouplingExamplesOne", + typeName: "BlubControllerOne2", + classType: "class", + sourceFile: filePath, + namespaceDelimiter: "\\", + implementedFrom: [], + }); + result.set("App\\CouplingExamplesTwo\\BlubControllerTwo1", { + namespace: "App\\CouplingExamplesTwo", + typeName: "BlubControllerTwo1", + classType: "class", + sourceFile: filePath, + namespaceDelimiter: "\\", + extendedFrom: "BlubControllerOne1", + implementedFrom: [], + }); + + // When + const typesFromFile = new TypesQueryStrategy().getTypesFromFile( + parsedFile, + "\\", + phpCollector.getTypesQuery(), + ); + // Then + for (const [key, value] of typesFromFile.entries()) { + expect(value).toEqual(expect.objectContaining(result.get(key))); + } }); }); });