-
Notifications
You must be signed in to change notification settings - Fork 15
/
params_test.go
210 lines (193 loc) · 6.14 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
package siesta
import (
"net/url"
"testing"
"time"
)
func TestParamsSimple(t *testing.T) {
v := url.Values{}
p := Params{}
v.Set("company", "VividCortex")
v.Set("founded", "2012")
v.Set("startup", "true")
v.Set("duration", "10ms")
v.Set("float", "12.89")
v.Set("uint64", "1234")
v.Set("int64", "-9876")
v.Set("uint", "2345")
v.Set("nonexistent", "8765")
v.Set("valueless", "")
v.Set("falseBool", "f")
company := p.String("company", "", "the company name")
founded := p.Int("founded", 0, "when it was founded")
startup := p.Bool("startup", false, "whether it's a startup")
duration := p.Duration("duration", 0, "how long it's been")
floatVar := p.Float64("float", 0, "some float64")
uint64Var := p.Uint64("uint64", 0, "some uint64")
int64Var := p.Int64("int64", 0, "some int64")
uintVar := p.Uint("uint", 0, "some uint")
valueless := p.Bool("valueless", false, "some bool")
falseBool := p.Bool("falseBool", true, "a bool with value false")
err := p.Parse(v)
if err != nil {
t.Error(err)
} else if *company != "VividCortex" {
t.Errorf("expected VividCortex, got %s", *company)
} else if *founded != 2012 {
t.Errorf("expected 2012, got %d", *founded)
} else if !*startup {
t.Errorf("expected true, got %t", *startup)
} else if *duration != 10*time.Millisecond {
t.Errorf("expected 10ms, got %s", *duration)
} else if *floatVar != 12.89 {
t.Errorf("expected 12.89, got %f", *floatVar)
} else if *uint64Var != 1234 {
t.Errorf("expected 1234, got %d", *uint64Var)
} else if *int64Var != -9876 {
t.Errorf("expected -9876, got %d", *int64Var)
} else if *uintVar != 2345 {
t.Errorf("expected 2345, got %d", *uintVar)
} else if *valueless != true {
t.Errorf("expected true, got %t", *valueless)
} else if *falseBool != false {
t.Errorf("expected false, got %t", *falseBool)
}
usage := p.Usage()
var expected map[string][3]string = map[string][3]string{
"company": [3]string{"company", "string", "the company name"},
"founded": [3]string{"founded", "int", "when it was founded"},
"startup": [3]string{"startup", "bool", "whether it's a startup"},
"duration": [3]string{"duration", "duration", "how long it's been"},
"float": [3]string{"float", "float64", "some float64"},
"uint64": [3]string{"uint64", "uint64", "some uint64"},
"int64": [3]string{"int64", "int64", "some int64"},
"uint": [3]string{"uint", "uint", "some uint"},
"valueless": [3]string{"valueless", "bool", "some bool"},
"falseBool": [3]string{"falseBool", "bool", "a bool with value false"},
}
compareUsageMaps(t, usage, expected)
}
func compareUsageMaps(t *testing.T, got, expected map[string][3]string) {
seen := make(map[string]bool)
for k, v := range got {
seen[k] = true
v2, ok := expected[k]
if !ok {
t.Errorf("%s doesn't exist in expected", k)
} else if v2 != v {
t.Errorf("%s: got '%s', expected '%s'", k, v, v2)
}
}
for k, _ := range expected {
if !seen[k] {
_, ok := got[k]
if !ok {
t.Errorf("%s doesn't exist in got", k)
}
}
}
}
func compareSlices(a, b []interface{}) bool {
if len(a) != len(b) {
return false
}
for i, aVal := range a {
if aVal != b[i] {
return false
}
}
return true
}
func TestParamsSlices(t *testing.T) {
v := url.Values{}
p := Params{}
v.Add("company", "VividCortex")
v.Add("company", "Inc,,comma,")
company := p.SliceString("company", "", "the company name")
v.Add("founded", "2012")
v.Add("founded", "2012,,2102,")
founded := p.SliceInt("founded", 0, "when it was founded")
v.Add("startup", "true")
v.Add("startup", "false,,true,")
startup := p.SliceBool("startup", false, "whether it's a startup")
v.Add("float", "12.89")
v.Add("float", "1.25,,12.625,")
floatVar := p.SliceFloat64("float", 0, "some float64")
v.Add("uint64", "1234")
v.Add("uint64", "1234,,5678,")
v.Add("uint64", "18446744073709551615") // 2^63-1
uint64Var := p.SliceUint64("uint64", 0, "some uint64")
v.Add("int64", "-9876")
v.Add("int64", "-9876,,8765,")
int64Var := p.SliceInt64("int64", 0, "some int64")
v.Add("uint", "2345")
v.Add("uint", "2345,,3456,")
uintVar := p.SliceUint("uint", 0, "some uint")
v.Add("duration", "10ms")
v.Add("duration", "10s,,12ms,")
duration := p.SliceDuration("duration", 0, "how long it's been")
err := p.Parse(v)
if err != nil {
t.Error(err)
}
companies := []string{"VividCortex", "Inc", "", "comma", ""}
for i, v := range *company {
if v != companies[i] {
t.Errorf("expected %s, got %s", companies[i], v)
}
}
foundings := []int{2012, 2012, 2102}
for i, v := range *founded {
if v != foundings[i] {
t.Errorf("expected %d, got %d", foundings[i], v)
}
}
startups := []bool{true, false, true}
for i, v := range *startup {
if v != startups[i] {
t.Errorf("expected %t, got %t", startups[i], v)
}
}
floats := []float64{12.89, 1.25, 12.625}
for i, v := range *floatVar {
if v != floats[i] {
t.Errorf("expected %f, got %f", floats[i], v)
}
}
uint64s := []uint64{1234, 1234, 5678, 18446744073709551615}
for i, v := range *uint64Var {
if v != uint64s[i] {
t.Errorf("expected %d, got %d", uint64s[i], v)
}
}
int64s := []int64{-9876, -9876, 8765}
for i, v := range *int64Var {
if v != int64s[i] {
t.Errorf("expected %d, got %d", int64s[i], v)
}
}
uints := []uint{2345, 2345, 3456}
for i, v := range *uintVar {
if v != uints[i] {
t.Errorf("expected %d, got %d", uints[i], v)
}
}
durations := []time.Duration{10 * time.Millisecond, 10 * time.Second, 12 * time.Millisecond}
for i, v := range *duration {
if v != durations[i] {
t.Errorf("expected %s, got %s", durations[i], v)
}
}
usage := p.Usage()
var expected map[string][3]string = map[string][3]string{
"company": [3]string{"company", "[]string", "the company name"},
"founded": [3]string{"founded", "[]int", "when it was founded"},
"startup": [3]string{"startup", "[]bool", "whether it's a startup"},
"duration": [3]string{"duration", "[]duration", "how long it's been"},
"float": [3]string{"float", "[]float64", "some float64"},
"uint64": [3]string{"uint64", "[]uint64", "some uint64"},
"int64": [3]string{"int64", "[]int64", "some int64"},
"uint": [3]string{"uint", "[]uint", "some uint"},
}
compareUsageMaps(t, usage, expected)
}