forked from h2non/imaginary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams_test.go
150 lines (130 loc) · 2.88 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
package main
import (
"net/url"
"testing"
bimg "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)
}
}
}