2020-10-05 19:10:33 +02:00
|
|
|
/*
|
|
|
|
* 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>, June 2020
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { GraphStream, Color } from '../../lib/index';
|
|
|
|
|
|
|
|
const simple = `graph foo {
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
const complex = `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
|
|
|
|
foo_foo -> foo_baz_asd [label = "edge attr"]
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
// eslint-disable-next-line max-lines-per-function
|
|
|
|
describe ('stream', () => {
|
2021-05-02 11:30:21 +02:00
|
|
|
it ('stream graph', () => new Promise<void> ((resolve) => {
|
2020-10-05 19:10:33 +02:00
|
|
|
let output = '';
|
|
|
|
const stream = (new GraphStream);
|
|
|
|
stream.on ('data', (data) => {
|
|
|
|
output += data;
|
|
|
|
});
|
|
|
|
stream.on ('end', () => {
|
|
|
|
expect (output)
|
|
|
|
.toEqual (simple);
|
|
|
|
resolve ();
|
|
|
|
});
|
|
|
|
stream.create_graph ('foo', 'u');
|
|
|
|
stream.end_graph ();
|
|
|
|
stream.end ();
|
|
|
|
}));
|
|
|
|
|
|
|
|
// eslint-disable-next-line max-statements
|
2021-05-02 11:30:21 +02:00
|
|
|
it ('complex stream graph', () => new Promise<void> ((resolve) => {
|
2020-10-05 19:10:33 +02:00
|
|
|
let output = '';
|
|
|
|
const stream = (new GraphStream);
|
|
|
|
stream.on ('data', (data) => {
|
|
|
|
output += data;
|
|
|
|
});
|
|
|
|
stream.on ('end', () => {
|
|
|
|
expect (output)
|
|
|
|
.toEqual (complex);
|
|
|
|
resolve ();
|
|
|
|
});
|
|
|
|
stream.create_graph ('foo', 'd');
|
|
|
|
stream.create_graph ('baz');
|
|
|
|
stream.attributes ({ color: Color.red, style: 'bold' });
|
|
|
|
stream.create_graph ('nested');
|
|
|
|
stream.attributes ({ color: Color.gray, style: 'dotted' });
|
|
|
|
stream.create_graph ('unnamed');
|
|
|
|
stream.attributes ({ color: Color.gray, style: 'dotted' });
|
|
|
|
stream.end_graph ();
|
|
|
|
stream.end_graph ();
|
|
|
|
const asd = stream.create_node ('asd');
|
|
|
|
stream.attributes ({ label: 'asd' });
|
|
|
|
stream.end_node ();
|
|
|
|
stream.create_node ('test');
|
|
|
|
stream.attributes ({ style: 'bold', color: Color.gray });
|
|
|
|
stream.end_graph ();
|
|
|
|
const baz = stream.create_node ('baz');
|
|
|
|
stream.attributes ({ label: 'baz' });
|
|
|
|
const foo = stream.create_node ('foo');
|
|
|
|
stream.attributes ({ label: 'foo' });
|
|
|
|
stream.create_edge (foo, baz);
|
|
|
|
stream.create_edge (foo, asd);
|
|
|
|
stream.attributes ({ label: 'edge attr' });
|
|
|
|
stream.end_graph ();
|
|
|
|
stream.end ();
|
|
|
|
}));
|
2020-10-05 20:15:22 +02:00
|
|
|
|
|
|
|
// eslint-disable-next-line max-lines-per-function
|
|
|
|
it ('should reject invalid command sequences', () => {
|
|
|
|
/**
|
|
|
|
* command valid invalid
|
|
|
|
* [s] cug, cdg eg, cn, csg, ce, at
|
|
|
|
* cn eg, cn, csg, ce, at cug, cdg
|
|
|
|
* cug eg, cn, csg, ce, at cug, cdg
|
|
|
|
* cdg eg, cn, csg, ce, at cug, cdg
|
|
|
|
* csg eg, cn, csg, ce, at cug, cdg
|
|
|
|
* eg eg, cn, csg, ce cug, cdg, at
|
|
|
|
* at eg, cn, csg, ce cug, cdg, at
|
|
|
|
* ce eg, cn, csg, ce, at cug, cdg
|
|
|
|
*/
|
|
|
|
|
2021-05-02 11:30:21 +02:00
|
|
|
const cn = (g: GraphStream) => {
|
2020-10-05 20:15:22 +02:00
|
|
|
g.create_node ('foo');
|
|
|
|
};
|
2021-05-02 11:30:21 +02:00
|
|
|
const cug = (g: GraphStream) => {
|
2020-10-05 20:15:22 +02:00
|
|
|
g.create_graph ('foo', 'u');
|
|
|
|
};
|
2021-05-02 11:30:21 +02:00
|
|
|
const cdg = (g: GraphStream) => {
|
2020-10-05 20:15:22 +02:00
|
|
|
g.create_graph ('foo', 'd');
|
|
|
|
};
|
2021-05-02 11:30:21 +02:00
|
|
|
const csg = (g: GraphStream) => {
|
2020-10-05 20:15:22 +02:00
|
|
|
g.create_graph ('foo', 's');
|
|
|
|
};
|
2021-05-02 11:30:21 +02:00
|
|
|
const eg = (g: GraphStream) => {
|
2020-10-05 20:15:22 +02:00
|
|
|
g.end_graph ();
|
|
|
|
};
|
2021-05-02 11:30:21 +02:00
|
|
|
const at = (g: GraphStream) => {
|
2020-10-05 20:15:22 +02:00
|
|
|
g.attributes ({ color: 'red' });
|
|
|
|
};
|
2021-05-02 11:30:21 +02:00
|
|
|
const ce = (g: GraphStream) => {
|
2020-10-05 20:15:22 +02:00
|
|
|
g.create_edge ('foo', 'bar');
|
|
|
|
};
|
|
|
|
|
|
|
|
const combinations = [
|
|
|
|
{
|
2021-05-02 11:30:21 +02:00
|
|
|
primary: (g: GraphStream) => {
|
2020-10-05 20:15:22 +02:00
|
|
|
cug (g);
|
|
|
|
cn (g);
|
|
|
|
},
|
|
|
|
secondary: [
|
|
|
|
cug,
|
|
|
|
cdg
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
primary: cug,
|
|
|
|
secondary: [
|
|
|
|
cug,
|
|
|
|
cdg
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
primary: cdg,
|
|
|
|
secondary: [
|
|
|
|
cug,
|
|
|
|
cdg
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2021-05-02 11:30:21 +02:00
|
|
|
primary: (g: GraphStream) => {
|
2020-10-05 20:15:22 +02:00
|
|
|
cug (g);
|
|
|
|
csg (g);
|
|
|
|
},
|
|
|
|
secondary: [
|
|
|
|
cug,
|
|
|
|
cdg
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2021-05-02 11:30:21 +02:00
|
|
|
primary: (g: GraphStream) => {
|
2020-10-05 20:15:22 +02:00
|
|
|
cug (g);
|
|
|
|
csg (g);
|
|
|
|
eg (g);
|
|
|
|
},
|
|
|
|
secondary: [
|
|
|
|
cug,
|
|
|
|
cdg,
|
|
|
|
at
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2021-05-02 11:30:21 +02:00
|
|
|
primary: (g: GraphStream) => {
|
2020-10-05 20:15:22 +02:00
|
|
|
cug (g);
|
|
|
|
at (g);
|
|
|
|
},
|
|
|
|
secondary: [
|
|
|
|
cug,
|
|
|
|
cdg,
|
|
|
|
at
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2021-05-02 11:30:21 +02:00
|
|
|
primary: (g: GraphStream) => {
|
2020-10-05 20:15:22 +02:00
|
|
|
cug (g);
|
|
|
|
ce (g);
|
|
|
|
},
|
|
|
|
secondary: [
|
|
|
|
cug,
|
|
|
|
cdg
|
|
|
|
]
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const comb of combinations) {
|
|
|
|
for (const secondary of comb.secondary) {
|
|
|
|
const g = (new GraphStream);
|
|
|
|
comb.primary (g);
|
|
|
|
expect (() => secondary (g))
|
|
|
|
.toThrowError (
|
|
|
|
/invalid state to execute command.*?expected:.*?actual:/uis
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-10-05 19:10:33 +02:00
|
|
|
});
|