-
Notifications
You must be signed in to change notification settings - Fork 2
/
values_of_test.go
92 lines (85 loc) · 1.69 KB
/
values_of_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
package req
import (
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type ReqP struct{}
type Par struct {
A int `json:"a"`
}
func TestValuesOf(t *testing.T) {
var nilP *ReqP
now := time.Now()
for _, test := range []struct {
v interface{}
get func() interface{}
e url.Values
}{
{v: nil, e: nil},
{v: &Par{A: 1000000000018}, e: url.Values{"a": []string{"1000000000018"}}},
{v: map[string]interface{}{"v": 1000000000018}, e: url.Values{"v": []string{"1000000000018"}}},
{v: nilP, e: nil},
{v: url.Values{}, e: url.Values{}},
{v: (interface{})(nil), e: nil},
{get: func() interface{} {
var m map[string][]string
return &m
}, e: nil},
{get: func() interface{} {
var vv map[string][]string
m := map[string]interface{}{
"a": nil,
"b": &vv,
}
return m
}, e: url.Values{
"a": []string{""},
"b": []string{""},
}},
{v: map[string]interface{}{
"t": now,
}, e: url.Values{
"t": []string{now.Format(time.RFC3339)},
}},
{v: map[string]interface{}{
"v": []interface{}{nil, "v"},
}, e: url.Values{
"v": []string{"", "v"},
}},
{v: map[string]interface{}{
"a": 1,
"b": "b",
}, e: url.Values{
"a": []string{"1"},
"b": []string{"b"},
}},
{v: struct {
A string
B string `json:"b"`
C *string `json:"c,omitempty"`
}{
"A",
"B",
nil,
}, e: url.Values{
"A": []string{"A"},
"b": []string{"B"},
}},
} {
raw := test.v
if test.get != nil {
raw = test.get()
}
v, err := ValuesOf(raw)
assert.NoError(t, err)
assert.Equal(t, test.e, v)
}
_, err := ValuesOf(1)
assert.Error(t, err)
_, err = ValuesOf(1.1)
assert.Error(t, err)
_, err = ValuesOf("")
assert.Error(t, err)
}