forked from narqo/go-badge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
badge_test.go
59 lines (52 loc) · 1.22 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
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 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)
}
}
})
}