feat(f7.1): flowchart parser (#22)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { parseFlowchart } from "../src/mermaidFlowchartDiff";
|
||||
|
||||
describe("parseFlowchart", () => {
|
||||
it("captures the header and node declarations with labels + shapes", () => {
|
||||
const g = parseFlowchart("flowchart LR\n A[Start] --> B(Middle)\n B --> C{End}");
|
||||
expect(g.header).toBe("flowchart LR");
|
||||
expect(g.nodes.get("A")).toMatchObject({ id: "A", label: "Start", open: "[", close: "]" });
|
||||
expect(g.nodes.get("B")).toMatchObject({ id: "B", label: "Middle", open: "(", close: ")" });
|
||||
expect(g.nodes.get("C")).toMatchObject({ id: "C", label: "End", open: "{", close: "}" });
|
||||
});
|
||||
|
||||
it("captures edges in declaration order with from/to and index", () => {
|
||||
const g = parseFlowchart("graph TD\n A-->B\n B-->C");
|
||||
expect(g.edges.map((e) => [e.from, e.to, e.index])).toEqual([
|
||||
["A", "B", 0],
|
||||
["B", "C", 1],
|
||||
]);
|
||||
});
|
||||
|
||||
it("records a node referenced only in an edge (no explicit declaration)", () => {
|
||||
const g = parseFlowchart("flowchart LR\n A-->B");
|
||||
expect(g.nodes.has("A")).toBe(true);
|
||||
expect(g.nodes.has("B")).toBe(true);
|
||||
expect(g.nodes.get("A")?.label).toBeUndefined();
|
||||
});
|
||||
|
||||
it("handles a labelled edge `A -->|yes| B`", () => {
|
||||
const g = parseFlowchart("flowchart LR\n A -->|yes| B");
|
||||
expect(g.edges[0]).toMatchObject({ from: "A", to: "B", label: "yes" });
|
||||
});
|
||||
|
||||
it("ignores comments and blank lines", () => {
|
||||
const g = parseFlowchart("flowchart LR\n%% a comment\n\n A-->B");
|
||||
expect(g.edges).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user