Skip to content

Commit

Permalink
test: refactor tests and improve compiler search without recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
junners committed Jul 15, 2024
1 parent 8bd9c76 commit 12f22c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
13 changes: 3 additions & 10 deletions src/main/libs/Compiler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Flow } from "../models/Flow";
import { FlowElementConnector } from "../models/FlowElementConnector";
import { FlowNode } from "../models/FlowNode";

export class Compiler {
Expand Down Expand Up @@ -52,10 +51,11 @@ export class Compiler {

if (currentElement.connectors && currentElement.connectors.length > 0) {
for (const connector of currentElement.connectors) {
const foundConnector = this.recurse(connector);
const targetReference =
connector?.connectorTargetReference?.targetReference ?? connector.reference;
// Check if the reference exists in the flow elements
const nextElement = flow.elements?.find(
(element) => element instanceof FlowNode && element.name === foundConnector.reference
(element) => element instanceof FlowNode && element.name === targetReference
);
if (nextElement instanceof FlowNode && nextElement.name !== endElementName) {
nextElements.push(nextElement.name);
Expand All @@ -64,11 +64,4 @@ export class Compiler {
}
return nextElements;
}

private recurse(connector: FlowElementConnector): FlowElementConnector {
if (connector.reference || !connector.connector) {
return connector;
}
return this.recurse(new FlowElementConnector("connector", connector.connector, {}));
}
}
12 changes: 8 additions & 4 deletions src/main/models/FlowElementConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ export class FlowElementConnector {
public reference: string;
public childName: string;
public childOf: boolean;
public connector: FlowElementConnector;
public connectorTargetReference: FlowElementConnectorReference;

constructor(type: string, element: object, args) {
this.type = type;
this.element = element;
this.childName = args.childName ? args.childName : undefined;
this.childOf = args.childOf ? args.childOf : undefined;

if (element && element["targetReference"]) {
this.reference = element["targetReference"];
if (element && "targetReference" in element) {
this.reference = element.targetReference as string;
}
if (element && "connector" in element) {
this.connector = element["connector"] as FlowElementConnector;
this.connectorTargetReference = element.connector as FlowElementConnectorReference;
}
}
}

class FlowElementConnectorReference {
public targetReference: string;
}
37 changes: 23 additions & 14 deletions tests/UnconnectedElement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,38 @@ import "mocha";
import * as core from "../src";
import * as path from "path-browserify";

import { ParseFlows } from "../src/main/libs/ParseFlows";
import { ParsedFlow } from "../src/main/models/ParsedFlow";

import { UnconnectedElement } from "../src/main/rules/UnconnectedElement";

describe("UnconnectedElement", () => {
const unconnectedElementRule: UnconnectedElement = new UnconnectedElement();

it("there should be checks for unconnected element", async () => {
const example_uri = path.join(__dirname, "./xmlfiles/Unconnected_Element.flow-meta.xml");
const flows = await core.parse([example_uri]);
const results: core.ScanResult[] = core.scan(flows);
const occurringResults = results[0].ruleResults.filter((rule) => rule.occurs);
expect(occurringResults.find((res) => res.ruleName === "UnconnectedElement"));
const connectedElementTestFile = path.join(
__dirname,
"./xmlfiles/Unconnected_Element.flow-meta.xml"
);
const parsed: ParsedFlow = (await ParseFlows([connectedElementTestFile])).pop() as ParsedFlow;
const ruleResult: core.RuleResult = unconnectedElementRule.execute(parsed.flow as core.Flow);
expect(ruleResult.occurs).to.be.true;
expect(ruleResult.details).to.not.be.empty;
ruleResult.details.forEach((detail) => {
expect(detail.violation.name).to.equal("unused_assignment");
});
});

it("async path there should be checks for unconnected element", async () => {
const connectedElementTestFile = path.join(
__dirname,
"./xmlfiles/Unconnected_Element_Async.flow-meta.xml"
);
const flows = await core.parse([connectedElementTestFile]);
const results: core.ScanResult[] = core.scan(flows);
const occurringResults = results.pop()?.ruleResults.filter((rule) => rule.occurs);
const unconnectedElementResult = occurringResults?.filter(
(res) => res.ruleName === "UnconnectedElement"
);
expect(unconnectedElementResult);
unconnectedElementResult?.forEach((unconnectedElem) => {
expect(unconnectedElem.details.pop()?.name).to.equal("UnconnectedElementTestOnAsync");
const parsed: ParsedFlow = (await ParseFlows([connectedElementTestFile])).pop() as ParsedFlow;
const ruleResult: core.RuleResult = unconnectedElementRule.execute(parsed.flow as core.Flow);
expect(ruleResult.occurs).to.be.true;
ruleResult.details.forEach((ruleDetail) => {
expect(ruleDetail.name).to.equal("UnconnectedElementTestOnAsync");
});
});
});

0 comments on commit 12f22c9

Please sign in to comment.