-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathutils_internal_test.go
142 lines (138 loc) · 3.02 KB
/
utils_internal_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
package stream
import (
"net/url"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_decodeJSONHook(t *testing.T) {
now, _ := time.Parse(TimeLayout, "2006-01-02T15:04:05.999999")
testCases := []struct {
f reflect.Type
typ reflect.Type
data any
expected any
shouldError bool
}{
{
f: reflect.TypeOf(123),
data: 123,
expected: 123,
},
{
f: reflect.TypeOf(""),
typ: reflect.TypeOf(Duration{}),
data: "1m2s",
expected: Duration{time.Minute + time.Second*2},
},
{
f: reflect.TypeOf(""),
typ: reflect.TypeOf(Duration{}),
data: "test",
shouldError: true,
},
{
f: reflect.TypeOf(""),
typ: reflect.TypeOf(Time{}),
data: now.Format(TimeLayout),
expected: Time{now},
},
{
f: reflect.TypeOf(""),
typ: reflect.TypeOf(Time{}),
data: "test",
shouldError: true,
},
{
f: reflect.TypeOf(float64(0)),
typ: reflect.TypeOf(Duration{}),
data: float64(42),
shouldError: false,
expected: Duration{time.Second * 42},
},
{
f: reflect.TypeOf(float64(0)),
typ: reflect.TypeOf(Duration{}),
data: struct{}{},
shouldError: true,
},
{
f: reflect.TypeOf(string("")),
typ: reflect.TypeOf(Data{}),
data: "test",
shouldError: false,
expected: Data{ID: "test"},
},
{
f: reflect.TypeOf(map[string]any{}),
typ: reflect.TypeOf(Data{}),
data: map[string]any{
"id": "test",
"extra": "data",
},
shouldError: false,
expected: Data{ID: "test", Extra: map[string]any{"extra": "data"}},
},
{
f: reflect.TypeOf(float64(0)),
typ: reflect.TypeOf(Data{}),
data: struct{}{},
shouldError: true,
},
}
for _, tc := range testCases {
out, err := decodeJSONHook(tc.f, tc.typ, tc.data)
if tc.shouldError {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, tc.expected, out)
}
}
}
func Test_parseIntValue(t *testing.T) {
testCases := []struct {
values url.Values
shouldError bool
expected int
expectedFlag bool
}{
{
values: url.Values{},
shouldError: false,
expected: 0,
expectedFlag: false,
},
{
values: url.Values{"test": []string{"a"}},
shouldError: true,
expected: 0,
expectedFlag: false,
},
{
values: url.Values{"test": []string{"123"}},
shouldError: false,
expected: 123,
expectedFlag: true,
},
{
values: url.Values{"test": []string{"123.5"}},
shouldError: true,
expected: 0,
expectedFlag: false,
},
}
for _, tc := range testCases {
v, ok, err := parseIntValue(tc.values, "test")
if tc.shouldError {
require.Error(t, err)
assert.False(t, ok)
} else {
require.NoError(t, err)
assert.Equal(t, tc.expectedFlag, ok)
assert.Equal(t, tc.expected, v)
}
}
}