-
Notifications
You must be signed in to change notification settings - Fork 26
/
badge_test.go
83 lines (72 loc) · 1.88 KB
/
badge_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
package badge
import (
"bytes"
"html/template"
"io/ioutil"
"strings"
"sync"
"testing"
)
func TestBadgeDrawerRender(t *testing.T) {
mockTemplate := strings.TrimSpace(`
{{.Subject}},{{.Status}},{{.Color}},{{with .Bounds}}{{.SubjectX}},{{.SubjectDx}},{{.StatusX}},{{.StatusDx}},{{.Dx}}{{end}}
`)
mockFontSize := 11.0
mockDPI := 72.0
d := &badgeDrawer{
fd: mustNewFontDrawer(mockFontSize, mockDPI),
tmpl: template.Must(template.New("mock-template").Parse(mockTemplate)),
mutex: &sync.Mutex{},
}
output := "XXX,YYY,#c0c0c0,18,34,50,34,68"
var buf bytes.Buffer
err := d.Render("XXX", "YYY", "#c0c0c0", &buf)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
result := buf.String()
if result != output {
t.Errorf("expect %q got %q", output, result)
}
}
func TestBadgeDrawerRenderBytes(t *testing.T) {
mockTemplate := strings.TrimSpace(`
{{.Subject}},{{.Status}},{{.Color}},{{with .Bounds}}{{.SubjectX}},{{.SubjectDx}},{{.StatusX}},{{.StatusDx}},{{.Dx}}{{end}}
`)
mockFontSize := 11.0
mockDPI := 72.0
d := &badgeDrawer{
fd: mustNewFontDrawer(mockFontSize, mockDPI),
tmpl: template.Must(template.New("mock-template").Parse(mockTemplate)),
mutex: &sync.Mutex{},
}
output := "XXX,YYY,#c0c0c0,18,34,50,34,68"
bytes, err := d.RenderBytes("XXX", "YYY", "#c0c0c0")
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if string(bytes) != output {
t.Errorf("expect %q got %q", output, string(bytes))
}
}
func BenchmarkRender(b *testing.B) {
// warm up
Render("XXX", "YYY", ColorBlue, ioutil.Discard)
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := Render("XXX", "YYY", ColorBlue, ioutil.Discard)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkRenderParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := Render("XXX", "YYY", ColorBlue, ioutil.Discard)
if err != nil {
b.Fatal(err)
}
}
})
}