122 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-04-24 12:02:32 +02:00
import test from 'ava';
2020-05-06 20:24:37 +02:00
import { Graph, Color } from '../lib';
2020-04-24 12:02:32 +02:00
2020-04-24 12:21:26 +02:00
const result = `digraph foo {
subgraph cluster_foo_baz {
2020-04-24 17:01:26 +02:00
color = #ff0000
style = bold
2020-04-27 18:57:23 +02:00
subgraph cluster_foo_baz_nested {
color = #808080
style = dotted
subgraph cluster_foo_baz_nested_unnamed {
color = #808080
style = dotted
}
}
foo_baz_asd [label="asd"]
foo_baz_test [style="bold", color="#808080"]
2020-04-24 12:02:32 +02:00
}
2020-04-27 18:57:23 +02:00
foo_baz [label="baz"]
foo_foo [label="foo"]
2020-04-24 12:02:32 +02:00
2020-04-24 12:21:26 +02:00
foo_foo -> foo_baz
2020-04-24 12:02:32 +02:00
}`;
2020-04-28 09:59:24 +02:00
const non_directional = `graph foo {
subgraph cluster_foo_bar {
foo_bar_baz [label="baz"]
foo_bar_asd [label="asd"]
foo_bar_baz -- foo_bar_asd
}
foo_foo [label="foo"]
foo_bar_baz -- foo_foo
}`;
2020-04-28 10:58:37 +02:00
const attributes = `digraph attr {
color = #000000
style = bold
overlap = false
splines = true
layout = neato
}`;
2020-04-24 12:02:32 +02:00
test ('serialize', (t) => {
2020-04-24 12:21:26 +02:00
const g = new Graph ('foo');
2020-04-24 12:02:32 +02:00
2020-04-24 12:21:26 +02:00
t.is (g.full_name, 'foo');
2020-04-24 14:05:58 +02:00
g.add_graph ((graph) => {
graph.name = 'baz';
graph.add_node ('asd');
2020-04-27 18:57:23 +02:00
graph.add_node ((n) => {
n.name = 'test';
2020-05-06 20:24:37 +02:00
n.style = 'bold';
2020-04-27 18:57:23 +02:00
n.color = Color.gray;
});
// eslint-disable-next-line no-shadow
graph.add_graph ((g) => {
2020-05-06 20:24:37 +02:00
g.style = 'dotted';
2020-04-27 18:57:23 +02:00
g.color = Color.gray;
g.name = 'nested';
// eslint-disable-next-line no-shadow, max-nested-callbacks
g.add_graph ((g) => {
2020-05-06 20:24:37 +02:00
g.style = 'dotted';
2020-04-27 18:57:23 +02:00
g.color = Color.gray;
});
});
2020-05-06 20:24:37 +02:00
graph.style = 'bold';
2020-04-24 17:01:26 +02:00
graph.color = Color.red;
2020-04-24 12:02:32 +02:00
});
2020-04-27 18:57:23 +02:00
const baz = g.add_node ('baz');
const foo = g.add_node ('foo');
2020-04-28 10:58:37 +02:00
2020-04-27 18:57:23 +02:00
g.add_edge (foo, baz);
2020-04-24 12:02:32 +02:00
const serialized = g.toString ();
t.is (serialized, result);
});
2020-04-28 09:59:24 +02:00
test ('non directional', (t) => {
const g = new Graph ('foo');
2020-04-28 10:58:37 +02:00
2020-04-28 09:59:24 +02:00
g.directional = false;
let n = '';
g.add_graph ((sub) => {
sub.name = 'bar';
n = sub.add_node ('baz');
const n2 = sub.add_node ('asd');
sub.add_edge (n, n2);
});
const f = g.add_node ('foo');
2020-04-28 10:58:37 +02:00
2020-04-28 09:59:24 +02:00
g.add_edge (n, f);
t.is (g.toString (), non_directional);
});
2020-04-28 10:58:37 +02:00
test ('attributes', (t) => {
const g = new Graph ('attr');
2020-05-06 20:24:37 +02:00
g.layout = 'neato';
2020-04-28 10:58:37 +02:00
g.overlap = false;
g.splines = true;
g.color = Color.black;
2020-05-06 20:24:37 +02:00
g.style = 'bold';
2020-04-28 10:58:37 +02:00
t.is (g.toString (), attributes);
});