-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathavatar_test.go
106 lines (93 loc) · 1.95 KB
/
avatar_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
package avatar
import (
"bytes"
"image/jpeg"
"image/png"
"io/ioutil"
"os"
"testing"
)
const (
testFontSize = 75.0
)
func TestInitialsAvatar_DrawToBytes(t *testing.T) {
fontFile := os.Getenv("AVATAR_FONT")
if fontFile == "" {
t.Skip("Font file is needed")
}
av := New(fontFile)
stuffs := []struct {
name string
size int
encoding string
}{
{"Swordsmen", 22, "png"},
{"Condor Heroes", 30, "jpeg"},
{"孔子", 22, "png"},
{"Swordsmen", 0, "png"},
{"*", 22, "png"},
}
for _, v := range stuffs {
raw, err := av.DrawToBytes(v.name, v.size, v.encoding)
if err != nil {
if err == ErrUnsupportChar {
t.Skip("ErrUnsupportChar")
}
t.Error(err)
}
switch v.encoding {
case "png":
if _, perr := png.Decode(bytes.NewReader(raw)); perr != nil {
t.Error(perr)
}
case "jpeg":
if _, perr := jpeg.Decode(bytes.NewReader(raw)); perr != nil {
t.Error(perr, v)
}
}
}
}
func TestGetInitials(t *testing.T) {
names := []struct {
full, intitials string
}{
{"John", "J"},
{"Doe", "D"},
{"", ""},
{"John Doe", "JD"},
{"john doe", "jd"},
{"joe@example.com", "j"},
{"John Doe (dj)", "dj"},
}
for _, v := range names {
n := getInitials(v.full)
if n != v.intitials {
t.Errorf("expected %s got %s", v.intitials, n)
}
}
}
func TestParseFont(t *testing.T) {
fileNotExists := "xxxxxxx.ttf"
_, err := parseFont(fileNotExists)
if err == nil {
t.Error("should return error")
}
_, err = newDrawer(fileNotExists, testFontSize)
if err == nil {
t.Error("should return error")
}
fileExistsButNotTTF, _ := ioutil.TempFile(os.TempDir(), "prefix")
defer os.Remove(fileExistsButNotTTF.Name())
_, err = parseFont(fileExistsButNotTTF.Name())
if err == nil {
t.Error("should return error")
}
_, err = newDrawer(fileExistsButNotTTF.Name(), testFontSize)
if err == nil {
t.Error("should return error")
}
_, err = newDrawer("", testFontSize)
if err == nil {
t.Error("should return error")
}
}