-
Notifications
You must be signed in to change notification settings - Fork 15
/
parameter_test.go
268 lines (258 loc) · 6.87 KB
/
parameter_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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package clif
import (
"fmt"
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)
func mustParseTime(l, t string) time.Time {
if r, err := time.Parse(l, t); err != nil {
panic(err.Error())
} else {
return r
}
}
type parameterResult struct {
asString []string
asInt []int
asFloat []float64
asBool []bool
asTime []time.Time
asTimeErr error
asJson map[string]interface{}
asJsonErr error
}
var testsParameterRead = []struct {
from string
to parameterResult
assignErr error
}{
{
from: "",
to: parameterResult{
asString: []string{""},
asInt: []int{0},
asFloat: []float64{0},
asBool: []bool{false},
asTime: nil,
asTimeErr: fmt.Errorf("parsing time \"\" as \"2006-01-02 15:04:05\": cannot parse \"\" as \"2006\""),
asJson: nil,
asJsonErr: fmt.Errorf("unexpected end of JSON input"),
},
assignErr: nil,
},
{
from: "foo",
to: parameterResult{
asString: []string{"foo"},
asInt: []int{0},
asFloat: []float64{0},
asBool: []bool{false},
asTime: nil,
asTimeErr: fmt.Errorf("parsing time \"foo\" as \"2006-01-02 15:04:05\": cannot parse \"foo\" as \"2006\""),
asJson: nil,
asJsonErr: fmt.Errorf("unexpected end of JSON input"),
},
assignErr: nil,
},
{
from: "10",
to: parameterResult{
asString: []string{"10"},
asInt: []int{10},
asFloat: []float64{10},
asBool: []bool{true},
asTime: nil,
asTimeErr: fmt.Errorf("parsing time \"10\" as \"2006-01-02 15:04:05\": cannot parse \"10\" as \"2006\""),
asJson: nil,
asJsonErr: fmt.Errorf("unexpected end of JSON input"),
},
assignErr: nil,
},
{
from: "123.234",
to: parameterResult{
asString: []string{"123.234"},
asInt: []int{123},
asFloat: []float64{123.234},
asBool: []bool{true},
asTime: nil,
asTimeErr: fmt.Errorf("parsing time \"123.234\" as \"2006-01-02 15:04:05\": cannot parse \"234\" as \"2006\""),
asJson: nil,
asJsonErr: fmt.Errorf("unexpected end of JSON input"),
},
assignErr: nil,
},
{
from: "true",
to: parameterResult{
asString: []string{"true"},
asInt: []int{1},
asFloat: []float64{1},
asBool: []bool{true},
asTime: nil,
asTimeErr: fmt.Errorf("parsing time \"true\" as \"2006-01-02 15:04:05\": cannot parse \"true\" as \"2006\""),
asJson: nil,
asJsonErr: fmt.Errorf("unexpected end of JSON input"),
},
assignErr: nil,
},
{
from: "2010-11-20 13:14:15",
to: parameterResult{
asString: []string{"2010-11-20 13:14:15"},
asInt: []int{0},
asFloat: []float64{0},
asBool: []bool{false},
asTime: []time.Time{mustParseTime("2006-01-02 15:04:05", "2010-11-20 13:14:15")},
asTimeErr: nil,
asJson: nil,
asJsonErr: fmt.Errorf("unexpected end of JSON input"),
},
assignErr: nil,
},
{
from: `{"foo":1.3,"bar":true,"baz":"bazoing"}`,
to: parameterResult{
asString: []string{`{"foo":1.3,"bar":true,"baz":"bazoing"}`},
asInt: []int{0},
asFloat: []float64{0},
asBool: []bool{false},
asTime: nil,
asTimeErr: fmt.Errorf("parsing time \"{\"foo\":1.3,\"bar\":true,\"baz\":\"bazoing\"}\" as \"2006-01-02 15:04:05\": cannot parse \"{\"foo\":1.3,\"bar\":true,\"baz\":\"bazoing\"}\" as \"2006\""),
asJson: map[string]interface{}{
"foo": 1.3,
"bar": true,
"baz": "bazoing",
},
asJsonErr: nil,
},
assignErr: nil,
},
}
func TestParameterReadString(t *testing.T) {
Convey("Read parameter as string", t, func() {
for i, test := range testsParameterRead {
Convey(fmt.Sprintf("%d) As string: \"%s\"", i, test.from), func() {
p := ¶meter{
Values: make([]string, 0),
}
err := p.Assign(test.from)
So(err, ShouldResemble, test.assignErr)
if test.assignErr == nil {
So(p.Strings(), ShouldResemble, test.to.asString)
if test.to.asString != nil {
So(p.String(), ShouldResemble, test.to.asString[0])
}
}
})
}
})
}
func TestParameterReadInt(t *testing.T) {
Convey("Read parameter as int", t, func() {
for i, test := range testsParameterRead {
Convey(fmt.Sprintf("%d) As int: \"%s\" -> %d", i, test.from, test.to.asInt[0]), func() {
p := ¶meter{
Values: make([]string, 0),
}
err := p.Assign(test.from)
So(err, ShouldResemble, test.assignErr)
if test.assignErr == nil {
So(p.Ints(), ShouldResemble, test.to.asInt)
if test.to.asInt != nil {
So(p.Int(), ShouldResemble, test.to.asInt[0])
}
}
})
}
})
}
func TestParameterReadFloat(t *testing.T) {
Convey("Read parameter as float", t, func() {
for i, test := range testsParameterRead {
Convey(fmt.Sprintf("%d) As float: \"%s\" -> %g", i, test.from, test.to.asFloat[0]), func() {
p := ¶meter{
Values: make([]string, 0),
}
err := p.Assign(test.from)
So(err, ShouldResemble, test.assignErr)
if test.assignErr == nil {
So(p.Floats(), ShouldResemble, test.to.asFloat)
if test.to.asFloat != nil {
So(p.Float(), ShouldResemble, test.to.asFloat[0])
}
}
})
}
})
}
func TestParameterReadBool(t *testing.T) {
Convey("Read parameter as bool", t, func() {
for i, test := range testsParameterRead {
Convey(fmt.Sprintf("%d) As bool: \"%s\" -> %v", i, test.from, test.to.asBool[0]), func() {
p := ¶meter{
Values: make([]string, 0),
}
err := p.Assign(test.from)
So(err, ShouldResemble, test.assignErr)
if test.assignErr == nil {
So(p.Bools(), ShouldResemble, test.to.asBool)
if test.to.asBool != nil {
So(p.Bool(), ShouldResemble, test.to.asBool[0])
}
}
})
}
})
}
func TestParameterReadTime(t *testing.T) {
Convey("Read parameter as time", t, func() {
for i, test := range testsParameterRead {
Convey(fmt.Sprintf("%d) As time: \"%s\"", i, test.from), func() {
p := ¶meter{
Values: make([]string, 0),
}
err := p.Assign(test.from)
So(err, ShouldResemble, test.assignErr)
if test.assignErr == nil {
tt, err := p.Times()
So(tt, ShouldResemble, test.to.asTime)
if test.to.asTimeErr != nil {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldResemble, test.to.asTimeErr.Error())
} else {
So(err, ShouldBeNil)
}
if test.to.asTime != nil {
t0, err := p.Time()
So(t0, ShouldResemble, &test.to.asTime[0])
So(err, ShouldBeNil)
}
}
})
}
})
}
func TestParameterJson(t *testing.T) {
Convey("Read parameter as json", t, func() {
for i, test := range testsParameterRead {
Convey(fmt.Sprintf("%d) As json: \"%s\"", i, test.from), func() {
p := ¶meter{
Values: make([]string, 0),
}
err := p.Assign(test.from)
So(err, ShouldResemble, test.assignErr)
if test.assignErr == nil {
jj, err := p.Json()
So(jj, ShouldResemble, test.to.asJson)
if test.to.asJsonErr != nil {
So(err, ShouldNotBeNil)
} else {
So(err, ShouldBeNil)
}
}
})
}
})
}