-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfnt_test.go
202 lines (167 loc) · 4.84 KB
/
nfnt_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package imogiri
import (
"bytes"
"github.com/stretchr/testify/assert"
"image/png"
"testing"
)
func TestNFNT_Namet(t *testing.T) {
n := &NFNT{}
assert.Equal(t, "NFNT", n.Name())
}
func TestNFNT_MatrixFormats(t *testing.T) {
n := &NFNT{}
f := n.MatrixFormats()
assert.Equal(t, 12, len(f))
assert.Contains(t, f, "jpg:jpg")
assert.Contains(t, f, "jpg:png")
assert.Contains(t, f, "jpg:gif")
assert.Contains(t, f, "png:jpg")
assert.Contains(t, f, "png:png")
assert.Contains(t, f, "png:gif")
assert.Contains(t, f, "gif:jpg")
assert.Contains(t, f, "gif:png")
assert.Contains(t, f, "gif:gif")
assert.Contains(t, f, "webp:jpg")
assert.Contains(t, f, "webp:png")
assert.Contains(t, f, "webp:gif")
}
func TestNFNT_SupportedActions(t *testing.T) {
n := &NFNT{}
assert.Equal(t, []Action{ResizeAction}, n.SupportedActions())
}
func TestNFNT_Resize_missingFormat(t *testing.T) {
x := ResizeOption{Width: 80, Height: 80, Format: ""}
n := &NFNT{}
s := new(bytes.Buffer)
r := new(bytes.Reader)
err := n.Resize(s, r, x)
assert.NotNil(t, err)
assert.Equal(t, "Please specify the format of the image", err.Error())
}
func TestNFNT_Resize_unknownFormat(t *testing.T) {
x := ResizeOption{Width: 80, Height: 80, FromFormat: "png", Format: "box"}
n := &NFNT{}
s := new(bytes.Buffer)
r := new(bytes.Reader)
err := n.Resize(s, r, x)
assert.NotNil(t, err)
assert.Equal(t, `Format "box" is not supported`, err.Error())
}
func TestNFNT_Resize_unknownFromFormat(t *testing.T) {
x := ResizeOption{Width: 80, Height: 80, FromFormat: "box", Format: "png"}
n := &NFNT{}
s := new(bytes.Buffer)
r := new(bytes.Reader)
err := n.Resize(s, r, x)
assert.NotNil(t, err)
assert.Equal(t, `Format "box" is not supported`, err.Error())
}
func TestNFNT_Resize_invalidImage(t *testing.T) {
x := ResizeOption{Width: 80, Height: 80, Format: "jpg"}
n := &NFNT{}
s := new(bytes.Buffer)
r := new(bytes.Reader)
err := n.Resize(s, r, x)
assert.NotNil(t, err)
assert.Equal(t, `unexpected EOF`, err.Error())
}
func TestNFNT_Resize_conversion(t *testing.T) {
x := ResizeOption{Width: 80, Height: 80, FromFormat: "jpg", Format: "png"}
n := &NFNT{}
s := new(bytes.Buffer)
r := loadFixture("gopher.jpg")
err := n.Resize(s, r, x)
assert.Nil(t, err)
assert.NotEqual(t, 0, s.Len())
mm, err := mimeBuffer(s.Bytes())
assert.Nil(t, err)
assert.Equal(t, "image/png", mm)
cfg, err := png.DecodeConfig(s)
assert.Nil(t, err)
assert.Equal(t, 80, cfg.Width)
assert.Equal(t, 80, cfg.Height)
}
func TestNFNT_Resize(t *testing.T) {
f := map[string]string{
"png": "gopher.png",
"jpg": "gopher.jpg",
"webp": "gopher.webp",
"gif": "gopher.gif",
}
c := []string{"png", "jpg", "gif"}
for k, v := range f {
for i := range c {
x := ResizeOption{Width: 80, Height: 80, FromFormat: k, Format: c[i]}
n := &NFNT{}
s := new(bytes.Buffer)
r := loadFixture(v)
err := n.Resize(s, r, x)
assert.Nil(t, err)
assert.NotEqual(t, 0, s.Len())
mm, err := mimeBuffer(s.Bytes())
assert.Nil(t, err)
switch c[i] {
case "png":
assert.Equal(t, "image/png", mm)
case "jpg":
assert.Equal(t, "image/jpeg", mm)
case "gif":
assert.Equal(t, "image/gif", mm)
}
}
}
}
func BenchmarkNFNT_Resize_JPG(b *testing.B) {
benchmarkResizeFormat(b, "gopher.jpg", "jpg", "")
}
func BenchmarkNFNT_Resize_JPG2PNG(b *testing.B) {
benchmarkResizeFormat(b, "gopher.jpg", "png", "jpg")
}
func BenchmarkNFNT_Resize_JPG2GIF(b *testing.B) {
benchmarkResizeFormat(b, "gopher.jpg", "gif", "jpg")
}
func BenchmarkNFNT_Resize_PNG(b *testing.B) {
benchmarkResizeFormat(b, "gopher.png", "png", "")
}
func BenchmarkNFNT_Resize_PNG2JPG(b *testing.B) {
benchmarkResizeFormat(b, "gopher.png", "jpg", "png")
}
func BenchmarkNFNT_Resize_PNG2GIF(b *testing.B) {
benchmarkResizeFormat(b, "gopher.png", "gif", "png")
}
func BenchmarkNFNT_Resize_WEBP2PNG(b *testing.B) {
benchmarkResizeFormat(b, "gopher.webp", "png", "webp")
}
func BenchmarkNFNT_Resize_WEBP2JPG(b *testing.B) {
benchmarkResizeFormat(b, "gopher.webp", "jpg", "webp")
}
func BenchmarkNFNT_Resize_WEBP2GIF(b *testing.B) {
benchmarkResizeFormat(b, "gopher.webp", "gif", "webp")
}
func BenchmarkNFNT_Resize_animatedGIF(b *testing.B) {
benchmarkResizeFormat(b, "animation.gif", "gif", "")
}
func BenchmarkNFNT_Resize_GIF(b *testing.B) {
benchmarkResizeFormat(b, "gopher.gif", "gif", "")
}
func BenchmarkNFNT_Resize_GIF2PNG(b *testing.B) {
benchmarkResizeFormat(b, "gopher.gif", "png", "gif")
}
func BenchmarkNFNT_Resize_GIF2JPG(b *testing.B) {
benchmarkResizeFormat(b, "gopher.gif", "jpg", "gif")
}
func benchmarkResizeFormat(b *testing.B, fixture, format, fromFormat string) {
if fromFormat == "" {
fromFormat = format
}
n := &NFNT{}
r := loadFixture(fixture)
x := ResizeOption{Width: 80, Height: 80, FromFormat: fromFormat, Format: format}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s := new(bytes.Buffer)
r.Seek(0, 0)
n.Resize(s, r, x)
}
}