-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_test.go
261 lines (246 loc) · 4.47 KB
/
to_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
package to_test
import (
"encoding/json"
to "github.com/rsb/from"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"time"
)
func TestUint_SingleIndirect(t *testing.T) {
t.Parallel()
var foo int32
foo = int32(100)
out, err := to.Uint[uint](&foo)
require.NoError(t, err)
require.Equal(t, uint(100), out)
}
func TestUint_FourLevelsOfIndirect(t *testing.T) {
t.Parallel()
var foo int32
foo = int32(100)
bar := &foo
baz := &bar
biz := &baz
out, err := to.Uint[uint](&biz)
require.NoError(t, err)
require.Equal(t, uint(100), out)
}
func TestUint_Success(t *testing.T) {
t.Parallel()
var jEight json.Number
err := json.Unmarshal([]byte("8"), &jEight)
require.NoError(t, err)
cases := []struct {
name string
in any
expected uint
}{
{
name: "Sunday",
in: time.Sunday,
expected: uint(0),
},
{
name: "Monday",
in: time.Monday,
expected: uint(1),
},
{
name: "Tuesday",
in: time.Tuesday,
expected: uint(2),
},
{
name: "Wednesday",
in: time.Wednesday,
expected: uint(3),
},
{
name: "Thursday",
in: time.Thursday,
expected: uint(4),
},
{
name: "Friday",
in: time.Friday,
expected: uint(5),
},
{
name: "Saturday",
in: time.Saturday,
expected: uint(6),
},
{
name: "January",
in: time.January,
expected: uint(1),
},
{
name: "int",
in: 8,
expected: uint(8),
},
{
name: "int8",
in: int8(8),
expected: uint(8),
},
{
name: "int16",
in: int16(8),
expected: uint(8),
},
{
name: "int32",
in: int32(8),
expected: uint(8),
},
{
name: "int64",
in: int64(8),
expected: uint(8),
},
{
name: "uint",
in: uint(8),
expected: uint(8),
},
{
name: "uint8",
in: uint8(8),
expected: uint(8),
},
{
name: "uint16",
in: uint16(8),
expected: uint(8),
},
{
name: "uint32",
in: uint32(8),
expected: uint(8),
},
{
name: "uint64",
in: uint64(8),
expected: uint(8),
},
{
name: "float32",
in: float32(8.8),
expected: uint(8),
},
{
name: "float64",
in: 8.8,
expected: uint(8),
},
{
name: "json.Number 8",
in: jEight,
expected: uint(8),
},
{
name: "bool true",
in: true,
expected: uint(1),
},
{
name: "bool false",
in: false,
expected: uint(0),
},
{
name: "nil",
in: nil,
expected: uint(0),
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
out, err := to.Uint[uint](tt.in)
require.NoError(t, err)
require.Equal(t, tt.expected, out)
})
}
}
func TestUint_Failures(t *testing.T) {
t.Parallel()
var jMinusEight, jEightDotEight json.Number
err := json.Unmarshal([]byte("-8"), &jMinusEight)
require.NoError(t, err)
err = json.Unmarshal([]byte("8.8"), &jEightDotEight)
require.NoError(t, err)
var uKnown struct{ foo string }
negativeFailureMsg := to.NegativeNumberFailure.Error()
cases := []struct {
name string
in any
msg string
}{
{
name: "int negative number failure",
in: -1,
msg: negativeFailureMsg,
},
{
name: "int8 negative number failure",
in: int8(-1),
msg: negativeFailureMsg,
},
{
name: "int16 negative number failure",
in: int16(-1),
msg: negativeFailureMsg,
},
{
name: "int32 negative number failure",
in: int32(-1),
msg: negativeFailureMsg,
},
{
name: "int64 negative number failure",
in: int64(-1),
msg: negativeFailureMsg,
},
{
name: "float32 negative number failure",
in: float32(-1),
msg: negativeFailureMsg,
},
{
name: "float64 negative number failure",
in: float64(-1),
msg: negativeFailureMsg,
},
{
name: "string negative number failure",
in: "-1",
msg: negativeFailureMsg,
},
{
name: "json negative number failure",
in: jMinusEight,
msg: negativeFailureMsg,
},
{
name: "json float",
in: jEightDotEight,
msg: "Uint failed for json.Number (8.8)",
},
{
name: "unknown type",
in: uKnown,
msg: "unable to cast struct",
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
out, err := to.Uint[uint](tt.in)
require.Equal(t, uint(0), out)
require.Error(t, err)
assert.Contains(t, err.Error(), tt.msg)
})
}
}