forked from h2non/imaginary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params_test.go
215 lines (191 loc) · 4.68 KB
/
params_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
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"net/url"
"testing"
"gopkg.in/h2non/bimg.v1"
)
const fixture = "fixtures/large.jpg"
func TestReadParams(t *testing.T) {
q := url.Values{}
q.Set("width", "100")
q.Add("height", "80")
q.Add("noreplicate", "1")
q.Add("opacity", "0.2")
q.Add("text", "hello")
q.Add("background", "255,10,20")
params := readParams(q)
assert := params.Width == 100 &&
params.Height == 80 &&
params.NoReplicate == true &&
params.Opacity == 0.2 &&
params.Text == "hello" &&
params.Background[0] == 255 &&
params.Background[1] == 10 &&
params.Background[2] == 20
if assert == false {
t.Error("Invalid params")
}
}
func TestParseParam(t *testing.T) {
intCases := []struct {
value string
expected int
}{
{"1", 1},
{"0100", 100},
{"-100", 100},
{"99.02", 99},
{"99.9", 100},
}
for _, test := range intCases {
val := parseParam(test.value, "int")
if val != test.expected {
t.Errorf("Invalid param: %s != %d", test.value, test.expected)
}
}
floatCases := []struct {
value string
expected float64
}{
{"1.1", 1.1},
{"01.1", 1.1},
{"-1.10", 1.10},
{"99.999999", 99.999999},
}
for _, test := range floatCases {
val := parseParam(test.value, "float")
if val != test.expected {
t.Errorf("Invalid param: %#v != %#v", val, test.expected)
}
}
boolCases := []struct {
value string
expected bool
}{
{"true", true},
{"false", false},
{"1", true},
{"1.1", false},
{"-1", false},
{"0", false},
{"0.0", false},
{"no", false},
{"yes", false},
}
for _, test := range boolCases {
val := parseParam(test.value, "bool")
if val != test.expected {
t.Errorf("Invalid param: %#v != %#v", val, test.expected)
}
}
}
func TestParseColor(t *testing.T) {
cases := []struct {
value string
expected []uint8
}{
{"200,100,20", []uint8{200, 100, 20}},
{"0,280,200", []uint8{0, 255, 200}},
{" -1, 256 , 50", []uint8{0, 255, 50}},
{" a, 20 , &hel0", []uint8{0, 20, 0}},
{"", []uint8{}},
}
for _, color := range cases {
c := parseColor(color.value)
l := len(color.expected)
if len(c) != l {
t.Errorf("Invalid color length: %#v", c)
}
if l == 0 {
continue
}
assert := c[0] == color.expected[0] &&
c[1] == color.expected[1] &&
c[2] == color.expected[2]
if assert == false {
t.Errorf("Invalid color schema: %#v <> %#v", color.expected, c)
}
}
}
func TestParseExtend(t *testing.T) {
cases := []struct {
value string
expected bimg.Extend
}{
{"white", bimg.ExtendWhite},
{"black", bimg.ExtendBlack},
{"copy", bimg.ExtendCopy},
{"mirror", bimg.ExtendMirror},
{"background", bimg.ExtendBackground},
{" BACKGROUND ", bimg.ExtendBackground},
{"invalid", bimg.ExtendBlack},
{"", bimg.ExtendBlack},
}
for _, extend := range cases {
c := parseExtendMode(extend.value)
if c != extend.expected {
t.Errorf("Invalid extend value : %d != %d", c, extend.expected)
}
}
}
func TestGravity(t *testing.T) {
cases := []struct {
gravityValue string
smartCropValue bool
}{
{gravityValue: "foo", smartCropValue: false},
{gravityValue: "smart", smartCropValue: true},
}
for _, td := range cases {
io := readParams(url.Values{"gravity": []string{td.gravityValue}})
if (io.Gravity == bimg.GravitySmart) != td.smartCropValue {
t.Errorf("Expected %t to be %t, test data: %+v", io.Gravity == bimg.GravitySmart, td.smartCropValue, td)
}
}
}
func TestReadMapParams(t *testing.T) {
cases := []struct {
params map[string]interface{}
expected ImageOptions
}{
{
map[string]interface{}{
"width": 100,
"opacity": 0.1,
"type": "webp",
"embed": true,
"gravity": "west",
"color": "255,200,150",
},
ImageOptions{
Width: 100,
Opacity: 0.1,
Type: "webp",
Embed: true,
Gravity: bimg.GravityWest,
Color: []uint8{255, 200, 150},
},
},
}
for _, test := range cases {
opts := readMapParams(test.params)
if opts.Width != test.expected.Width {
t.Errorf("Invalid width: %d != %d", opts.Width, test.expected.Width)
}
if opts.Opacity != test.expected.Opacity {
t.Errorf("Invalid opacity: %#v != %#v", opts.Opacity, test.expected.Opacity)
}
if opts.Type != test.expected.Type {
t.Errorf("Invalid type: %s != %s", opts.Type, test.expected.Type)
}
if opts.Embed != test.expected.Embed {
t.Errorf("Invalid embed: %#v != %#v", opts.Embed, test.expected.Embed)
}
if opts.Gravity != test.expected.Gravity {
t.Errorf("Invalid gravity: %#v != %#v", opts.Gravity, test.expected.Gravity)
}
if opts.Color[0] != test.expected.Color[0] || opts.Color[1] != test.expected.Color[1] || opts.Color[2] != test.expected.Color[2] {
t.Errorf("Invalid color: %#v != %#v", opts.Color, test.expected.Color)
}
}
}