forked from goccy/go-graphviz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphviz_test.go
151 lines (139 loc) · 3.7 KB
/
graphviz_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package graphviz_test
import (
"bytes"
"io/ioutil"
"os"
"testing"
"github.com/goccy/go-graphviz"
)
func TestGraphviz_Image(t *testing.T) {
g := graphviz.New()
graph, err := g.Graph()
if err != nil {
t.Fatalf("%+v", err)
}
defer func() {
graph.Close()
g.Close()
}()
n, err := graph.CreateNode("n")
if err != nil {
t.Fatalf("%+v", err)
}
m, err := graph.CreateNode("m")
if err != nil {
t.Fatalf("%+v", err)
}
e, err := graph.CreateEdge("e", n, m)
if err != nil {
t.Fatalf("%+v", err)
}
e.SetLabel("e")
t.Run("png", func(t *testing.T) {
t.Run("Render", func(t *testing.T) {
var buf bytes.Buffer
if err := g.Render(graph, graphviz.PNG, &buf); err != nil {
t.Fatalf("%+v", err)
}
if len(buf.Bytes()) != 4602 {
t.Fatalf("failed to encode png: bytes length is %d", len(buf.Bytes()))
}
})
t.Run("RenderImage", func(t *testing.T) {
image, err := g.RenderImage(graph)
if err != nil {
t.Fatalf("%+v", err)
}
bounds := image.Bounds()
if bounds.Max.X != 83 {
t.Fatal("failed to get image")
}
if bounds.Max.Y != 177 {
t.Fatal("failed to get image")
}
})
})
t.Run("jpg", func(t *testing.T) {
t.Run("Render", func(t *testing.T) {
var buf bytes.Buffer
if err := g.Render(graph, graphviz.JPG, &buf); err != nil {
t.Fatalf("%+v", err)
}
if len(buf.Bytes()) != 3296 {
t.Fatalf("failed to encode jpg: bytes length is %d", len(buf.Bytes()))
}
})
t.Run("RenderImage", func(t *testing.T) {
image, err := g.RenderImage(graph)
if err != nil {
t.Fatalf("%+v", err)
}
bounds := image.Bounds()
if bounds.Max.X != 83 {
t.Fatal("failed to get image")
}
if bounds.Max.Y != 177 {
t.Fatal("failed to get image")
}
})
})
}
func TestParseBytes(t *testing.T) {
type test struct {
input string
expected_valid bool
}
tests := []test{
{input: "graph test { a -- b }", expected_valid: true},
{input: "graph test { a -- b", expected_valid: false},
{input: "graph test { a -- b }", expected_valid: true},
{input: "graph test { a -- }", expected_valid: false},
{input: "graph test { a -- c }", expected_valid: true},
{input: "graph test { a - b }", expected_valid: false},
{input: "graph test { d -- e }", expected_valid: true},
}
for i, test := range tests {
_, err := graphviz.ParseBytes([]byte(test.input))
actual_valid := err == nil
if actual_valid != test.expected_valid {
t.Errorf("Test %d of TestParseBytes failed. Parsing error: %+v", i+1, err)
}
}
}
func TestParseFile(t *testing.T) {
type test struct {
input string
expected_valid bool
}
tests := []test{
{input: "graph test { a -- b }", expected_valid: true},
{input: "graph test { a -- b", expected_valid: false},
{input: "graph test { a -- b }", expected_valid: true},
{input: "graph test { a -- }", expected_valid: false},
{input: "graph test { a -- c }", expected_valid: true},
{input: "graph test { a - b }", expected_valid: false},
{input: "graph test { d -- e }", expected_valid: true},
}
createTempFile := func(t *testing.T, content string) *os.File {
file, err := ioutil.TempFile("", "*")
if err != nil {
t.Fatalf("There was an error creating a temporary file. Error: %+v", err)
return nil
}
_, err = file.WriteString(content)
if err != nil {
t.Fatalf("There was an error writing '%s' to a temporary file. Error: %+v", content, err)
return nil
}
return file
}
for i, test := range tests {
tmpfile := createTempFile(t, test.input)
defer os.Remove(tmpfile.Name())
_, err := graphviz.ParseFile(tmpfile.Name())
actual_valid := err == nil
if actual_valid != test.expected_valid {
t.Errorf("Test %d of TestParseFile failed. Parsing error: %+v", i+1, err)
}
}
}