/* * 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 , 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', () => { it ('stream graph', () => new Promise ((resolve) => { 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 it ('complex stream graph', () => new Promise ((resolve) => { 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 (); })); // 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 */ const cn = (g: GraphStream) => { g.create_node ('foo'); }; const cug = (g: GraphStream) => { g.create_graph ('foo', 'u'); }; const cdg = (g: GraphStream) => { g.create_graph ('foo', 'd'); }; const csg = (g: GraphStream) => { g.create_graph ('foo', 's'); }; const eg = (g: GraphStream) => { g.end_graph (); }; const at = (g: GraphStream) => { g.attributes ({ color: 'red' }); }; const ce = (g: GraphStream) => { g.create_edge ('foo', 'bar'); }; const combinations = [ { primary: (g: GraphStream) => { cug (g); cn (g); }, secondary: [ cug, cdg ] }, { primary: cug, secondary: [ cug, cdg ] }, { primary: cdg, secondary: [ cug, cdg ] }, { primary: (g: GraphStream) => { cug (g); csg (g); }, secondary: [ cug, cdg ] }, { primary: (g: GraphStream) => { cug (g); csg (g); eg (g); }, secondary: [ cug, cdg, at ] }, { primary: (g: GraphStream) => { cug (g); at (g); }, secondary: [ cug, cdg, at ] }, { primary: (g: GraphStream) => { 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 ); } } }); });