Skip to content

Commit

Permalink
Break up test files
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaines committed Sep 6, 2023
1 parent 85bddd5 commit 320e7c9
Show file tree
Hide file tree
Showing 5 changed files with 742 additions and 703 deletions.
68 changes: 68 additions & 0 deletions packages/viz/test/context-info.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import assert from "node:assert/strict";
import * as VizPackage from "../src/standalone.mjs";

describe("Viz", function() {
let viz;

beforeEach(async function() {
viz = await VizPackage.instance();
});

describe("graphvizVersion", function() {
it("returns the Graphviz version", function() {
assert.strictEqual(viz.graphvizVersion, "8.1.0");
});
});

describe("formats", function() {
it("returns the list of formats", function() {
assert.deepStrictEqual(viz.formats, [
"canon",
"cmap",
"cmapx",
"cmapx_np",
"dot",
"dot_json",
"eps",
"fig",
"gv",
"imap",
"imap_np",
"ismap",
"json",
"json0",
"mp",
"pic",
"plain",
"plain-ext",
"pov",
"ps",
"ps2",
"svg",
"tk",
"xdot",
"xdot1.2",
"xdot1.4",
"xdot_json"
]);
});
});

describe("engines", function() {
it("returns the list of layout engines", function() {
assert.deepStrictEqual(viz.engines, [
"circo",
"dot",
"fdp",
"neato",
"nop",
"nop1",
"nop2",
"osage",
"patchwork",
"sfdp",
"twopi"
]);
});
});
});
215 changes: 215 additions & 0 deletions packages/viz/test/graph-objects.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
import assert from "node:assert/strict";
import * as VizPackage from "../src/standalone.mjs";

describe("Viz", function() {
let viz;

beforeEach(async function() {
viz = await VizPackage.instance();
});

describe("rendering graph objects", function() {
it("empty graph", function() {
const result = viz.render({});

assert.deepStrictEqual(result, {
status: "success",
output: `digraph {
graph [bb="0,0,0,0"];
node [label="\\N"];
}
`,
errors: []
});
});

it("attributes in options override options in input", function() {
const result = viz.render(
{
nodeAttributes: {
shape: "rectangle"
}
},
{
nodeAttributes: {
shape: "circle"
}
}
);

assert.deepStrictEqual(result, {
status: "success",
output: `digraph {
graph [bb="0,0,0,0"];
node [label="\\N",
shape=circle
];
}
`,
errors: []
});
});

it("just edges", function() {
const result = viz.render({
edges: [
{ tail: "a", head: "b" }
]
});

assert.deepStrictEqual(result, {
status: "success",
output: `digraph {
graph [bb="0,0,54,108"];
node [label="\\N"];
a [height=0.5,
pos="27,90",
width=0.75];
b [height=0.5,
pos="27,18",
width=0.75];
a -> b [pos="e,27,36.104 27,71.697 27,64.237 27,55.322 27,46.965"];
}
`,
errors: []
});
});

it("undirected graph", function() {
const result = viz.render({
directed: false,
edges: [
{ tail: "a", head: "b" }
]
});

assert.deepStrictEqual(result, {
status: "success",
output: `graph {
graph [bb="0,0,54,108"];
node [label="\\N"];
a [height=0.5,
pos="27,90",
width=0.75];
b [height=0.5,
pos="27,18",
width=0.75];
a -- b [pos="27,71.697 27,60.846 27,46.917 27,36.104"];
}
`,
errors: []
});
});

it("html attributes", function() {
const result = viz.render({
nodes: [
{
name: "a",
attributes: {
label: { html: "<b>A</b>" }
}
}
]
});

assert.deepStrictEqual(result, {
status: "success",
output: `digraph {
graph [bb="0,0,54,36"];
a [height=0.5,
label=<<b>A</b>>,
pos="27,18",
width=0.75];
}
`,
errors: []
});
});

it("default attributes, nodes, edges, and nested subgraphs", function() {
const result = viz.render({
graphAttributes: {
rankdir: "LR"
},
nodeAttributes: {
shape: "circle"
},
nodes: [
{ name: "a", attributes: { label: "A", color: "red" } },
{ name: "b", attributes: { label: "B", color: "green" } }
],
edges: [
{ tail: "a", head: "b", attributes: { label: "1" } },
{ tail: "b", head: "c", attributes: { label: "2" } }
],
subgraphs: [
{
name: "cluster_1",
nodes: [
{ name: "c", attributes: { label: "C", color: "blue" } }
],
edges: [
{ tail: "c", head: "d", attributes: { label: "3" } }
],
subgraphs: [
{
name: "cluster_2",
nodes: [
{ name: "d", attributes: { label: "D", color: "orange" } }
]
}
]
}
]
});

assert.deepStrictEqual(result, {
status: "success",
output: `digraph {
graph [bb="0,0,297.04,84",
rankdir=LR
];
node [shape=circle];
subgraph cluster_1 {
graph [bb="150.02,8,289.04,76"];
subgraph cluster_2 {
graph [bb="229.02,16,281.04,68"];
d [color=orange,
height=0.50029,
label=D,
pos="255.03,42",
width=0.50029];
}
c [color=blue,
height=0.5,
label=C,
pos="176.02,42",
width=0.5];
c -> d [label=3,
lp="215.52,50.4",
pos="e,236.63,42 194.5,42 203.75,42 215.35,42 225.84,42"];
}
a [color=red,
height=0.50029,
label=A,
pos="18.01,42",
width=0.50029];
b [color=green,
height=0.5,
label=B,
pos="97.021,42",
width=0.5];
a -> b [label=1,
lp="57.521,50.4",
pos="e,78.615,42 36.485,42 45.741,42 57.335,42 67.826,42"];
b -> c [label=2,
lp="136.52,50.4",
pos="e,157.62,42 115.49,42 124.75,42 136.34,42 146.83,42"];
}
`,
errors: []
});
});
});
});
89 changes: 89 additions & 0 deletions packages/viz/test/render-unwrapped.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import assert from "node:assert/strict";
import { JSDOM } from "jsdom";
import * as VizPackage from "../src/standalone.mjs";

describe("Viz", function() {
let viz;

beforeEach(async function() {
viz = await VizPackage.instance();
});

describe("renderString", function() {
it("returns the output for the first graph, even if subsequent graphs have errors", function() {
const result = viz.renderString("graph a { } graph {");

assert.strictEqual(result, "graph a {\n\tgraph [bb=\"0,0,0,0\"];\n\tnode [label=\"\\N\"];\n}\n");
});

it("throws an error if the first graph has a syntax error", function() {
assert.throws(() => { viz.renderString("graph {"); }, /^Error: syntax error/);
});

it("throws an error for layout errors", function() {
assert.throws(() => { viz.renderString("graph { layout=invalid }"); }, /^Error: Layout type: "invalid" not recognized/);
});

it("throws an error if there are no graphs in the input", function() {
assert.throws(() => { viz.renderString(""); }, /^Error: render failed/);
});

it("throws an error with the first render error message", function() {
assert.throws(() => { viz.renderString("graph { layout=invalid; x=1.2.3=y }"); }, /^Error: Layout type: "invalid" not recognized/);
});

it("throws for invalid format option", function() {
assert.throws(() => { viz.renderString("graph { }", { format: "invalid" }); }, /^Error: Format: "invalid" not recognized/);
});

it("throws for invalid engine option", function() {
assert.throws(() => { viz.renderString("graph { }", { engine: "invalid" }); }, /^Error: Layout type: "invalid" not recognized/);
});

it("accepts a non-ASCII character", function() {
assert.match(viz.renderString("digraph { a [label=図] }"), /label=図/);
});

it("a graph with unterminated string followed by another call with a valid graph", function() {
assert.throws(() => { viz.renderString("graph { a[label=\"blah"); }, /^Error: syntax error/);
assert.ok(viz.renderString("graph { a }"));
});
});

describe("renderSVGElement", function() {
it("returns an SVG element", function() {
try {
const window = (new JSDOM()).window;
global.DOMParser = window.DOMParser;

const svg = viz.renderSVGElement("digraph { a -> b }");
assert.deepStrictEqual(svg.querySelector(".node title").textContent, "a");
assert.deepStrictEqual(svg.querySelector(".edge title").textContent, "a->b");
} finally {
delete global.DOMParser;
}
});

it("throws an error for syntax errors", function() {
assert.throws(() => { viz.renderSVGElement(`graph {`); }, /^Error: syntax error/);
});

it("throws an error if there are no graphs in the input", function() {
assert.throws(() => { viz.renderSVGElement(""); }, /^Error: render failed/);
});
});

describe("renderJSON", function() {
it("returns an object", function() {
assert.deepStrictEqual(viz.renderJSON("digraph a { }").name, "a");
});

it("throws an error for syntax errors", function() {
assert.throws(() => { viz.renderJSON(`graph {`); }, /^Error: syntax error/);
});

it("throws an error if there are no graphs in the input", function() {
assert.throws(() => { viz.renderJSON(""); }, /^Error: render failed/);
});
});
});
Loading

0 comments on commit 320e7c9

Please sign in to comment.