-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode_test.go
More file actions
131 lines (120 loc) · 2.53 KB
/
Copy pathdecode_test.go
File metadata and controls
131 lines (120 loc) · 2.53 KB
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
package rsv
import (
"testing"
"github.com/mchaynes/rsv/internal"
"github.com/stretchr/testify/assert"
)
func TestUnmarshalRow_Embedded(t *testing.T) {
type C struct {
D string `idx:"0"`
E *int `idx:"1"`
}
type B struct {
C C
}
type A struct {
B B
}
expected := A{
B: B{
C: C{
D: "D",
E: internal.IntPtr(10),
},
},
}
row := []string{"D", "10"}
actual := A{}
err := UnmarshalRow(row, &actual)
assert.NoError(t, err, "embedded struct shouldn't have returned an error")
assert.Equal(t, expected, actual)
}
func TestUnmarshalRow_Numbers(t *testing.T) {
type A struct {
// Mark F64 & F32 to prove that ptrs to floats works
F64 *float64 `idx:"0"`
F32 *float32 `idx:"1"`
I int `idx:"2"`
I64 int64 `idx:"3"`
I32 int32 `idx:"4"`
UI uint `idx:"5"`
UI8 uint8 `idx:"6"`
UI16 uint16 `idx:"7"`
UI32 uint32 `idx:"8"`
UI64 uint64 `idx:"9"`
}
expected := A{
F64: internal.Float64Ptr(64),
F32: internal.Float32Ptr(32),
I: 1,
I64: 64,
I32: 32,
UI: 1,
UI8: 8,
UI16: 16,
UI32: 32,
UI64: 64,
}
row := []string{"64", "32", "1", "64", "32", "1", "8", "16", "32", "64"}
actual := A{}
err := UnmarshalRow(row, &actual)
assert.NoError(t, err, "should have no errors")
assert.Equal(t, expected, actual)
}
func TestUnmarshalRow_Omitempty(t *testing.T) {
type A struct {
F64 *float64 `idx:"0,omitempty"`
S *string `idx:"1,omitempty"`
}
expected := A{
F64: internal.Float64Ptr(64),
S: internal.StringPtr("s"),
}
row := []string{"64", "s"}
actual := A{}
err := UnmarshalRow(row, &actual)
assert.NoError(t, err, "should have no errors")
assert.Equal(t, expected, actual)
// Make all fields nil
expected = A{}
row = []string{"", ""}
actual = A{}
err = UnmarshalRow(row, &actual)
assert.NoError(t, err, "should have no errors")
assert.Equal(t, expected, actual)
}
func TestUnmarshalRow_Panics(t *testing.T) {
defer func() {
if i := recover(); i == nil {
t.Error("test should have panicked")
}
}()
_ = UnmarshalRow([]string{}, struct{}{})
}
func BenchmarkUnmarshalRow(b *testing.B) {
type C struct {
D *int64 `idx:"0"`
E string `idx:"1"`
F *float64 `idx:"2"`
G string `idx:"3"`
H string `idx:"4"`
I string `idx:"5"`
J string `idx:"6"`
}
type B struct {
C C
}
type A struct {
A B
}
row := []string{"1234", "abcdefg", "12.3456", "dkdkdkd", "adkdkd", "dkfjdkafl", "dkajff"}
b.RunParallel(func(pb *testing.PB) {
var a A
for pb.Next() {
err := UnmarshalRow(row, &a)
if err != nil {
b.Error(err)
}
}
})
}