Skip to content

Commit 27cf52e

Browse files
committed
WIP Fix behavior of names in graph objects
- Nodes must have a name - Edges must have a tail and head - Subgraphs may have no name The subgraph test currently fails because for some reason, the attributes for one of the subgraphs are being set on the containing graph as well.
1 parent ef48cbe commit 27cf52e

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

packages/viz/src/wrapper.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ function readGraph(module, graphPointer, graphData) {
195195

196196
if (graphData.nodes) {
197197
graphData.nodes.forEach(nodeData => {
198+
if (typeof nodeData.name === "undefined") {
199+
throw new Error("nodes must have a name");
200+
}
201+
198202
const nodePointer = module.ccall("viz_add_node", "number", ["number", "string"], [graphPointer, String(nodeData.name)]);
199203

200204
if (nodeData.attributes) {
@@ -205,6 +209,14 @@ function readGraph(module, graphPointer, graphData) {
205209

206210
if (graphData.edges) {
207211
graphData.edges.forEach(edgeData => {
212+
if (typeof edgeData.tail === "undefined") {
213+
throw new Error("edges must have a tail");
214+
}
215+
216+
if (typeof edgeData.head === "undefined") {
217+
throw new Error("edges must have a head");
218+
}
219+
208220
const edgePointer = module.ccall("viz_add_edge", "number", ["number", "string", "string"], [graphPointer, String(edgeData.tail), String(edgeData.head)]);
209221

210222
if (edgeData.attributes) {
@@ -215,7 +227,7 @@ function readGraph(module, graphPointer, graphData) {
215227

216228
if (graphData.subgraphs) {
217229
graphData.subgraphs.forEach(subgraphData => {
218-
const subgraphPointer = module.ccall("viz_add_subgraph", "number", ["number", "string"], [graphPointer, String(subgraphData.name)]);
230+
const subgraphPointer = module.ccall("viz_add_subgraph", "number", ["number", "string"], [graphPointer, typeof subgraphData.name !== "undefined" ? String(subgraphData.name) : 0]);
219231

220232
readGraph(module, subgraphPointer, subgraphData);
221233
});

packages/viz/test/graph-objects.test.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,99 @@ describe("Viz", function() {
207207
lp="136.52,50.4",
208208
pos="e,157.62,42 115.49,42 124.55,42 135.85,42 146.16,42"];
209209
}
210+
`,
211+
errors: []
212+
});
213+
});
214+
215+
it("throws for a node without a name", function() {
216+
assert.throws(() => {
217+
viz.render({
218+
nodes: [
219+
{}
220+
]
221+
});
222+
});
223+
});
224+
225+
it("throws for an edge without tail or head", function() {
226+
assert.throws(() => {
227+
viz.render({
228+
edges: [
229+
{}
230+
]
231+
});
232+
});
233+
234+
assert.throws(() => {
235+
viz.render({
236+
edges: [
237+
{ tail: "a" }
238+
]
239+
});
240+
});
241+
242+
assert.throws(() => {
243+
viz.render({
244+
edges: [
245+
{ head: "b" }
246+
]
247+
});
248+
});
249+
});
250+
251+
it("accepts a subgraph without a name", function() {
252+
const result = viz.render({
253+
edges: [
254+
{ tail: "a", head: "b" }
255+
],
256+
subgraphs: [
257+
{
258+
graphAttributes: {
259+
cluster: true,
260+
color: "red"
261+
},
262+
nodes: [
263+
{ name: "a" }
264+
]
265+
},
266+
{
267+
graphAttributes: {
268+
cluster: true,
269+
color: "green"
270+
},
271+
nodes: [
272+
{ name: "b" }
273+
]
274+
}
275+
]
276+
});
277+
278+
assert.deepStrictEqual(result, {
279+
status: "success",
280+
output: `digraph {
281+
graph [bb="0,0,86,140"];
282+
node [label="\\N"];
283+
{
284+
graph [bb="8,80,78,132",
285+
cluster=true,
286+
color=red
287+
];
288+
a [height=0.5,
289+
pos="43,106",
290+
width=0.75];
291+
}
292+
{
293+
graph [bb="8,8,78,60",
294+
cluster=true,
295+
color=green
296+
];
297+
b [height=0.5,
298+
pos="43,34",
299+
width=0.75];
300+
}
301+
a -> b [pos="e,43,52.104 43,87.697 43,80.407 43,71.726 43,63.536"];
302+
}
210303
`,
211304
errors: []
212305
});

0 commit comments

Comments
 (0)