File: composite_test.go

package info (click to toggle)
golang-github-emicklei-dot 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 300 kB
  • sloc: makefile: 9
file content (105 lines) | stat: -rw-r--r-- 2,438 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package dotx

import (
	"os"
	"strings"
	"testing"

	"github.com/emicklei/dot"
)

func TestExampleSubsystemSameGraph(t *testing.T) {
	g := dot.NewGraph(dot.Directed)

	c1 := g.Node("component")

	sub := NewComposite("subsystem", g, SameGraph)
	sub.Input("in1", c1)
	sub.Input("in2", c1)
	sub.Output("out2", c1)

	sc1 := sub.Node("subcomponent 1")
	sc2 := sub.Node("subcomponent 2")
	sub.Input("in1", sc1)
	sub.Input("in2", sc2)
	sub.Output("out2", sc2)

	sc1.Edge(sc2)

	sub2 := NewComposite("subsystem2", sub.Graph, SameGraph)
	sub2.Input("in3", sc1)
	sub2.Output("out3", sc2)

	sub3 := sub2.Node("subcomponent 3")
	sub2.Input("in3", sub3)

	os.WriteFile("TestExampleSubsystemSameGraph.dot", []byte(g.String()), 0666)
}

func TestExampleSubsystemExternalGraph(t *testing.T) {
	g := dot.NewGraph(dot.Directed)

	c1 := g.Node("component")

	sub := NewComposite("subsystem", g, ExternalGraph)
	sub.Input("in1", c1)
	sub.Input("in2", c1)
	sub.Output("out2", c1)

	sub.Export(func(g *dot.Graph) {
		sc1 := sub.Node("subcomponent 1")
		sc2 := sub.Node("subcomponent 2")
		sub.Input("in1", sc1)
		sub.Input("in2", sc2)
		sub.Output("out2", sc2)
		sc1.Edge(sc2)

		sub2 := NewComposite("subsystem2", sub.Graph, ExternalGraph)
		sub2.Export(func(g *dot.Graph) {
			sub2.Input("in3", sc1)
			sub2.Output("out3", sc2)
			sub3 := sub2.Node("subcomponent 3")
			sub2.Input("in3", sub3)
		})
	})

	os.WriteFile("TestExampleSubsystemExternalGraph.dot", []byte(g.String()), 0666)
}

func TestAttrOnSubsystem(t *testing.T) {
	s := NewComposite("test", dot.NewGraph(), SameGraph)
	s.Attr("shape", "box3d")
	if !strings.Contains(s.String(), "test") { // dont care about structure, dot has tested that
		t.Fail()
	}
}

func TestWarninOnExport(t *testing.T) {
	s := NewComposite("/////fail", dot.NewGraph(), SameGraph)
	s.Export(func(g *dot.Graph) {})
}

func TestCompositeWithUnusedIOSameGraph(t *testing.T) {
	g := dot.NewGraph(dot.Directed)

	c1 := g.Node("component")
	sub := NewComposite("subsystem", g, SameGraph)
	sub.Input("in", c1)
	sub.Output("out", c1)

	os.WriteFile("TestCompositeWithUnusedIOSameGraph.dot", []byte(g.String()), 0666)
}

func TestConnectToComposites(t *testing.T) {
	g := dot.NewGraph()
	c1 := NewComposite("c1", g, SameGraph)
	c2 := NewComposite("c2", g, SameGraph)
	e := c1.Input("in", c2)
	if e.From().ID() != c2.outerNode.ID() {
		t.Fail()
	}
	f := c1.Output("out", c2)
	if f.To().ID() != c2.outerNode.ID() {
		t.Fail()
	}
}