Skip to content

Commit

Permalink
test: add more tests for types-query-strategy.ts #366
Browse files Browse the repository at this point in the history
  • Loading branch information
benedikt-mehl-mw committed May 28, 2024
1 parent 238eafd commit 0f1b9ba
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/parser/resolver/types/abstract-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Configuration> {
Expand All @@ -23,8 +24,6 @@ async function getConfiguration(filePath: string): Promise<Configuration> {
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(
Expand All @@ -33,7 +32,7 @@ describe("Types Query strategy", () => {
)) as ParsedFile;
const csharpCollector: AbstractCollector = new CSharpCollector();

const result: Map<string, TypeInfo> = new Map<string, TypeInfo>();
const result: Map<FQTN, TypeInfo> = new Map<string, TypeInfo>();

result.set("mainNamespace.FirstInterface", {
namespace: "mainNamespace",
Expand All @@ -60,13 +59,119 @@ describe("Types Query strategy", () => {
implementedFrom: [],
});
// When
const typesFromFile: Map<FQTN, TypeInfo> = 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<FQTN, TypeInfo> = new Map<string, TypeInfo>();

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<FQTN, TypeInfo> = new Map<string, TypeInfo>();

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)));
}
});
});
});

0 comments on commit 0f1b9ba

Please sign in to comment.