-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathedge_test.go
135 lines (116 loc) · 3.92 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package dot
import (
"testing"
)
func TestEdgeStyleHelpers(t *testing.T) {
type test struct {
input string
want string
}
tests := []test{
{input: "solid", want: `digraph {n1[label="A"];n2[label="B"];n1->n2[style="solid"];}`},
{input: "bold", want: `digraph {n1[label="A"];n2[label="B"];n1->n2[style="bold"];}`},
{input: "dashed", want: `digraph {n1[label="A"];n2[label="B"];n1->n2[style="dashed"];}`},
{input: "dotted", want: `digraph {n1[label="A"];n2[label="B"];n1->n2[style="dotted"];}`},
}
for _, tc := range tests {
di := NewGraph(Directed)
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 := flatten(di.String()), tc.want; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
}
func TestEdgeWithTwoPorts(t *testing.T) {
di := NewGraph(Directed)
n1 := di.Node("A")
n1.Attr("label", HTML("<table><tr><td port='port_a'>A</td></tr></table>"))
n2 := di.Node("B")
n2.Attr("label", HTML("<table><tr><td port='port_b'>B</td></tr></table>"))
di.EdgeWithPorts(n1, n2, "port_a", "port_b")
want := "digraph {n1[label=<<table><tr><td port='port_a'>A</td></tr></table>>];n2[label=<<table><tr><td port='port_b'>B</td></tr></table>>];n1:port_a->n2:port_b;}"
if got, want := flatten(di.String()), want; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
func TestEdgeWithNoPorts(t *testing.T) {
di := NewGraph(Directed)
n1 := di.Node("A")
n1.Attr("label", HTML("<table><tr><td>A</td></tr></table>"))
n2 := di.Node("B")
n2.Attr("label", HTML("<table><tr><td>B</td></tr></table>"))
di.EdgeWithPorts(n1, n2, "", "")
want := "digraph {n1[label=<<table><tr><td>A</td></tr></table>>];n2[label=<<table><tr><td>B</td></tr></table>>];n1->n2;}"
if got, want := flatten(di.String()), want; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
func TestEdgeWithFirstPort(t *testing.T) {
di := NewGraph(Directed)
n1 := di.Node("A")
n1.Attr("label", HTML("<table><tr><td port='port_a'>A</td></tr></table>"))
n2 := di.Node("B")
n2.Attr("label", HTML("<table><tr><td>B</td></tr></table>"))
di.EdgeWithPorts(n1, n2, "port_a", "")
want := "digraph {n1[label=<<table><tr><td port='port_a'>A</td></tr></table>>];n2[label=<<table><tr><td>B</td></tr></table>>];n1:port_a->n2;}"
if got, want := flatten(di.String()), want; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
func TestEdgeWithSecondPort(t *testing.T) {
di := NewGraph(Directed)
n1 := di.Node("A")
n1.Attr("label", HTML("<table><tr><td>A</td></tr></table>"))
n2 := di.Node("B")
n2.Attr("label", HTML("<table><tr><td port='port_b'>B</td></tr></table>"))
di.EdgeWithPorts(n1, n2, "", "port_b")
want := "digraph {n1[label=<<table><tr><td>A</td></tr></table>>];n2[label=<<table><tr><td port='port_b'>B</td></tr></table>>];n1->n2:port_b;}"
if got, want := flatten(di.String()), want; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
func TestEdgeSetLabel(t *testing.T) {
di := NewGraph(Directed)
n1 := di.Node("A")
n2 := di.Node("B")
e := n1.Edge(n2).Label("ab")
v := e.Value("label")
if s, ok := v.(string); !ok {
t.Fail()
} else {
if s != "ab" {
t.Fail()
}
}
}
func TestNonStringAttribute(t *testing.T) {
di := NewGraph(Directed)
di.Node("A").Attr("shoesize", 42)
if got, want := flatten(di.String()), `digraph {n1[label="A",shoesize="42"];}`; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
func TestEdgeGetAttributes(t *testing.T) {
di := NewGraph(Directed)
n1 := di.Node("A")
n2 := di.Node("B")
e := di.Edge(n1, n2).Label("edge-label").Attr("foo", "bar")
attrs := e.GetAttributes()
if v, ok := attrs["label"]; !ok || v != "edge-label" {
t.Errorf("expected label=edge-label, got %v", attrs)
}
if v, ok := attrs["foo"]; !ok || v != "bar" {
t.Errorf("expected foo=bar, got %v", attrs)
}
}