2020-05-17 17:17:39 +02:00

129 lines
2.4 KiB
TypeScript

/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of graphviz-builder which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
import test from 'ava';
import { Graph, Color } from '../lib';
const result = `digraph foo {
subgraph cluster_foo_baz {
color = #ff0000
style = bold
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"]
}
foo_baz [label="baz"]
foo_foo [label="foo"]
foo_foo -> foo_baz
}`;
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
}`;
const attributes = `digraph attr {
color = #000000
style = bold
overlap = false
splines = true
layout = neato
}`;
test ('serialize', (t) => {
const g = new Graph ('foo');
t.is (g.full_name, 'foo');
g.add_graph ((graph) => {
graph.name = 'baz';
graph.add_node ('asd');
graph.add_node ((n) => {
n.name = 'test';
n.style = 'bold';
n.color = Color.gray;
});
// eslint-disable-next-line no-shadow
graph.add_graph ((g) => {
g.style = 'dotted';
g.color = Color.gray;
g.name = 'nested';
// eslint-disable-next-line no-shadow, max-nested-callbacks
g.add_graph ((g) => {
g.style = 'dotted';
g.color = Color.gray;
});
});
graph.style = 'bold';
graph.color = Color.red;
});
const baz = g.add_node ('baz');
const foo = g.add_node ('foo');
g.add_edge (foo, baz);
const serialized = g.toString ();
t.is (serialized, result);
});
test ('non directional', (t) => {
const g = new Graph ('foo');
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');
g.add_edge (n, f);
t.is (g.toString (), non_directional);
});
test ('attributes', (t) => {
const g = new Graph ('attr');
g.layout = 'neato';
g.overlap = false;
g.splines = true;
g.color = Color.black;
g.style = 'bold';
t.is (g.toString (), attributes);
});