-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEdge_test.go
116 lines (100 loc) · 2.72 KB
/
Edge_test.go
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
106
107
108
109
110
111
112
113
114
115
116
package dot
import (
"fmt"
"math"
"testing"
"github.com/wwmoraes/dot/dottest"
)
func TestEdge_String(t *testing.T) {
// TODO String needs to be implemented, and will break this test when done so
graph, err := New()
if err != nil {
t.Fatal("graph is nil, expected a valid instance")
}
edge := graph.Node("n1").Edge(graph.Node("n2"))
if got, want := dottest.MustGetFlattenSerializableString(t, edge), `"n1"->"n2";`; got != want {
t.Errorf("got [\n%v\n] want [\n%v\n]", got, want)
}
}
func TestEdge_ObjectInterface(t *testing.T) {
graph, err := New()
if err != nil {
t.Fatal("graph is nil, expected a valid instance")
}
graph.Node("n1").Edge(graph.Node("n2"))
if got, want := dottest.MustGetFlattenSerializableString(t, graph), `digraph {"n1";"n2";"n1"->"n2";}`; got != want {
t.Errorf("got [\n%v\n] want [\n%v\n]", got, want)
}
}
func TestEdge_StyleHelpers(t *testing.T) {
type test struct {
input string
want string
}
tests := []test{
{input: "solid", want: `digraph {"%[1]s";"%[2]s";"%[1]s"->"%[2]s"[style="solid"];}`},
{input: "bold", want: `digraph {"%[1]s";"%[2]s";"%[1]s"->"%[2]s"[style="bold"];}`},
{input: "dashed", want: `digraph {"%[1]s";"%[2]s";"%[1]s"->"%[2]s"[style="dashed"];}`},
{input: "dotted", want: `digraph {"%[1]s";"%[2]s";"%[1]s"->"%[2]s"[style="dotted"];}`},
}
for _, tc := range tests {
di, err := New()
if err != nil {
t.Fatal("graph is nil, expected a valid instance")
}
n1 := di.Node("A")
n2 := di.Node("B")
switch tc.input {
case "solid":
di.Edge(n1, n2).Solid()
case "bold":
di.Edge(n1, n2).Bold()
case "dashed":
di.Edge(n1, n2).Dashed()
case "dotted":
di.Edge(n1, n2).Dotted()
}
if got, want := dottest.MustGetFlattenSerializableString(t, di), fmt.Sprintf(tc.want, n1.ID(), n2.ID()); got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
}
func TestEdge_WriteTo(t *testing.T) {
tests := []struct {
name string
limit int
wantErr error
wantString string
}{
{
name: "zero data written",
limit: 0,
wantErr: dottest.ErrLimit,
wantString: "",
},
{
name: "partially written - edge",
limit: 1,
wantErr: dottest.ErrLimit,
wantString: `"n1"->"n2"`,
},
{
name: "fully written",
limit: math.MaxInt32,
wantErr: nil,
wantString: `"n1"->"n2"[label="test"];`,
},
}
graph, err := New()
if err != nil {
t.Fatal("graph is nil, expected a valid instance")
}
edge := graph.Edge(graph.Node("n1"), graph.Node("n2"))
edge.SetAttributeString("label", "test")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wantN := int64(len(tt.wantString))
dottest.TestByteWrite(t, edge, tt.limit, tt.wantErr, wantN, tt.wantString)
})
}
}