ts-graphviz 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { Digraph, Edge, Node, Subgraph, attribute as _ } from 'ts-graphviz';
1
+ import { Digraph, Edge, Node, Subgraph, attribute as _, EdgeTargetTuple, withContext } from 'ts-graphviz';
2
2
  import { toDot } from '#test/utils';
3
3
 
4
4
  test('class base', () => {
@@ -21,3 +21,111 @@ test('class base', () => {
21
21
  const dot = toDot(G);
22
22
  expect(dot).toMatchSnapshot();
23
23
  });
24
+
25
+ describe('Custom Class', () => {
26
+ class MyCustomDigraph extends Digraph {
27
+ constructor() {
28
+ super('G', {
29
+ [_.label]: 'This is Custom Digraph',
30
+ });
31
+ }
32
+ }
33
+ class MyCustomNode extends Node {
34
+ constructor(id: string) {
35
+ super(`node_${id}`, {
36
+ [_.label]: `This is Custom Node ${id}`,
37
+ });
38
+ }
39
+ }
40
+
41
+ class MyCustomEdge extends Edge {
42
+ constructor(targets: EdgeTargetTuple) {
43
+ super(targets, {
44
+ [_.label]: 'This is Custom Edge',
45
+ });
46
+ }
47
+ }
48
+
49
+ test('OOP', () => {
50
+ const digraph = new MyCustomDigraph();
51
+ const node1 = new MyCustomNode('A');
52
+ const node2 = new MyCustomNode('B');
53
+ const edge = new MyCustomEdge([node1, node2]);
54
+ digraph.addNode(node1);
55
+ digraph.addNode(node2);
56
+ digraph.addEdge(edge);
57
+ const dot = toDot(digraph);
58
+ expect(dot).toMatchInlineSnapshot(`
59
+ digraph "G" {
60
+ label = "This is Custom Digraph";
61
+ "node_A" [
62
+ label = "This is Custom Node A";
63
+ ];
64
+ "node_B" [
65
+ label = "This is Custom Node B";
66
+ ];
67
+ "node_A" -> "node_B" [
68
+ label = "This is Custom Edge";
69
+ ];
70
+ }
71
+ `);
72
+ });
73
+
74
+ describe('Models Context API', () => {
75
+ test('with method', () => {
76
+ const g = new Digraph();
77
+ g.with({
78
+ Node: MyCustomNode,
79
+ Edge: MyCustomEdge,
80
+ });
81
+ const a = g.createNode('A');
82
+ const b = g.createNode('B');
83
+ expect(a).toBeInstanceOf(MyCustomNode);
84
+ expect(g.createEdge([a, b])).toBeInstanceOf(MyCustomEdge);
85
+ const dot = toDot(g);
86
+ expect(dot).toMatchInlineSnapshot(`
87
+ digraph {
88
+ "node_A" [
89
+ label = "This is Custom Node A";
90
+ ];
91
+ "node_B" [
92
+ label = "This is Custom Node B";
93
+ ];
94
+ "node_A" -> "node_B" [
95
+ label = "This is Custom Edge";
96
+ ];
97
+ }
98
+ `);
99
+ });
100
+
101
+ test('createContext', () => {
102
+ const { digraph } = withContext({
103
+ Digraph: MyCustomDigraph,
104
+ Node: MyCustomNode,
105
+ Edge: MyCustomEdge,
106
+ });
107
+
108
+ const G = digraph((g) => {
109
+ const a = g.node('A'); // MyCustomNode
110
+ const b = g.node('B'); // MyCustomNode
111
+ g.edge([a, b]); // MyCustomEdge
112
+ });
113
+
114
+ const dot = toDot(G);
115
+ expect(dot).toMatchInlineSnapshot(`
116
+ digraph "G" {
117
+ label = "This is Custom Digraph";
118
+ "node_A" [
119
+ label = "This is Custom Node A";
120
+ ];
121
+ "node_B" [
122
+ label = "This is Custom Node B";
123
+ ];
124
+ "node_A" -> "node_B" [
125
+ label = "This is Custom Edge";
126
+ ];
127
+ }
128
+ `);
129
+ });
130
+ });
131
+ });
@@ -0,0 +1,58 @@
1
+ import { Digraph, Edge, Node, RootGraph, Subgraph, fromDot } from 'ts-graphviz';
2
+ import { toDot } from '#test/utils';
3
+
4
+ describe('fromDot function', () => {
5
+ test('RootGraph', () => {
6
+ const G = fromDot('digraph {}');
7
+ expect(G).toBeInstanceOf(RootGraph);
8
+ expect(G).toBeInstanceOf(Digraph);
9
+ expect(G.strict).toStrictEqual(false);
10
+ });
11
+
12
+ test('Node', () => {
13
+ const node = fromDot('a[ label = hoge]', { parse: { startRule: 'Node' } });
14
+ expect(node).toBeInstanceOf(Node);
15
+ expect(node.id).toStrictEqual('a');
16
+ expect(node.attributes.get('label')).toStrictEqual('hoge');
17
+ });
18
+
19
+ test('Edge', () => {
20
+ const edge = fromDot('a -> b [ label = hoge]', { parse: { startRule: 'Edge' } });
21
+ expect(edge).toBeInstanceOf(Edge);
22
+ expect(edge.targets).toMatchObject([{ id: 'a' }, { id: 'b' }]);
23
+ expect(edge.attributes.get('label')).toStrictEqual('hoge');
24
+ });
25
+
26
+ test('Subgraph', () => {
27
+ const subgraph = fromDot(
28
+ `subgraph sub {
29
+ label = hoge;
30
+ }`,
31
+ { parse: { startRule: 'Subgraph' } },
32
+ );
33
+ expect(subgraph).toBeInstanceOf(Subgraph);
34
+ expect(subgraph.id).toStrictEqual('sub');
35
+ expect(subgraph.get('label')).toStrictEqual('hoge');
36
+ });
37
+ });
38
+
39
+ test('partially described by DOT', () => {
40
+ const G = fromDot(
41
+ `digraph {
42
+ node_A [
43
+ label = "This is a Label of Node A";
44
+ ];
45
+ }`,
46
+ );
47
+
48
+ G.edge(['node_A', 'node_B']);
49
+
50
+ expect(toDot(G)).toMatchInlineSnapshot(`
51
+ digraph {
52
+ "node_A" [
53
+ label = "This is a Label of Node A";
54
+ ];
55
+ "node_A" -> "node_B";
56
+ }
57
+ `);
58
+ });