forked from mitchellh/mapstructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapstructure_index_test.go
181 lines (145 loc) · 4.51 KB
/
mapstructure_index_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
package mapstructure
import "testing"
type IndexStructRaw struct {
IntIndex []int
UintIndex []uint
FloatIndex []float64
StringIndex []string
}
var raw = IndexStructRaw{
IntIndex: []int{-1, 0, 1, 2},
UintIndex: []uint{0, 1, 2, 3},
FloatIndex: []float64{0.1, 0.2},
StringIndex: []string{"a", "b"},
}
type IndexStructTo struct {
IntIndex0Int int `mapstructure:"intindex,index=0"`
IntIndex0Int8 int8 `mapstructure:"intindex,index=0"`
IntIndex0Int16 int16 `mapstructure:"intindex,index=0"`
IntIndex0Int32 int32 `mapstructure:"intindex,index=0"`
IntIndex0Int64 int64 `mapstructure:"intindex,index=0"`
IntIndex1 int32 `mapstructure:"intindex,index=1"`
IntIndex2 int32 `mapstructure:"intindex,index=2"`
IntIndex3 int32 `mapstructure:"intindex,index=3"`
UintIndex0Uint uint `mapstructure:"uintindex,index=0"`
UintIndex0Uint8 uint8 `mapstructure:"uintindex,index=0"`
UintIndex0Uint16 uint16 `mapstructure:"uintindex,index=0"`
UintIndex0Uint32 uint32 `mapstructure:"uintindex,index=0"`
UintIndex0Uint64 uint64 `mapstructure:"uintindex,index=0"`
UintIndex1 uint32 `mapstructure:"uintindex,index=1"`
UintIndex2 uint32 `mapstructure:"uintindex,index=2"`
UintIndex3 uint32 `mapstructure:"uintindex,index=3"`
FloatIndex0Float32 float32 `mapstructure:"floatindex,index=0"`
FloatIndex0Float64 float64 `mapstructure:"floatindex,index=0"`
FloatIndex1 float64 `mapstructure:"floatindex,index=1"`
StringIndex0 string `mapstructure:"stringindex,index=0"`
StringIndex1 string `mapstructure:"stringindex,index=1"`
}
func TestStructureIndex(t *testing.T) {
out := &IndexStructTo{}
decoder, err := NewDecoder(&DecoderConfig{
Metadata: nil,
Result: out,
})
err = decoder.Decode(raw)
if err != nil {
t.Errorf("NewDecoder error: %v", err)
}
if out.IntIndex0Int != -1 {
t.Errorf("IntIndex0Int error: %v", out.IntIndex0Int)
}
if out.IntIndex0Int8 != -1 {
t.Errorf("IntIndex0Int8 error: %v", out.IntIndex0Int8)
}
if out.IntIndex0Int16 != -1 {
t.Errorf("IntIndex0Int16 error: %v", out.IntIndex0Int16)
}
if out.IntIndex0Int32 != -1 {
t.Errorf("IntIndex0Int32 error: %v", out.IntIndex0Int32)
}
if out.IntIndex0Int64 != -1 {
t.Errorf("IntIndex0Int64 error: %v", out.IntIndex0Int64)
}
if out.IntIndex1 != 0 {
t.Errorf("IntIndex1 error: %v", out.IntIndex1)
}
if out.IntIndex2 != 1 {
t.Errorf("IntIndex2 error: %v", out.IntIndex2)
}
if out.IntIndex3 != 2 {
t.Errorf("IntIndex3 error: %v", out.IntIndex3)
}
if out.UintIndex0Uint != 0 {
t.Errorf("UintIndex0Uint error: %v", out.UintIndex0Uint)
}
if out.UintIndex0Uint8 != 0 {
t.Errorf("UintIndex0Uint8 error: %v", out.UintIndex0Uint8)
}
if out.UintIndex0Uint16 != 0 {
t.Errorf("UintIndex0Uint16 error: %v", out.UintIndex0Uint16)
}
if out.UintIndex0Uint32 != 0 {
t.Errorf("UintIndex0Uint32 error: %v", out.UintIndex0Uint32)
}
if out.UintIndex0Uint64 != 0 {
t.Errorf("UintIndex0Uint64 error: %v", out.UintIndex0Uint64)
}
if out.UintIndex1 != 1 {
t.Errorf("UintIndex1 error: %v", out.UintIndex1)
}
if out.UintIndex2 != 2 {
t.Errorf("UintIndex2 error: %v", out.UintIndex2)
}
if out.UintIndex3 != 3 {
t.Errorf("UintIndex3 error: %v", out.UintIndex3)
}
if out.FloatIndex0Float32 < 0.1-0.000001 || out.FloatIndex0Float32 > 0.1+0.000001 {
t.Errorf("FloatIndex0Float32 error: %v", out.FloatIndex0Float32)
}
if out.FloatIndex0Float64 < 0.1-0.000001 || out.FloatIndex0Float64 > 0.1+0.000001 {
t.Errorf("FloatIndex0Float64 error: %v", out.FloatIndex0Float64)
}
if out.FloatIndex1 < 0.2-0.000001 || out.FloatIndex1 > 0.2+0.000001 {
t.Errorf("FloatIndex1 error: %v", out.FloatIndex1)
}
if out.StringIndex0 != "a" {
t.Errorf("StringIndex0 error: %v", out.StringIndex0)
}
if out.StringIndex1 != "b" {
t.Errorf("StringIndex1 error: %v", out.StringIndex1)
}
}
func TestStructureIndexTooLarge(t *testing.T) {
out := struct {
Value int32 `mapstructure:"intindex,index=100"`
}{}
decoder, err := NewDecoder(&DecoderConfig{
Metadata: nil,
Result: &out,
})
if err != nil {
t.Errorf("NewDecoder error: %v", err)
return
}
err = decoder.Decode(raw)
if err == nil {
t.Error("Decode error success, but need err")
}
}
func TestStructureIndexDiffValue(t *testing.T) {
out := struct {
Value int32 `mapstructure:"uintindex,index=0"`
}{}
decoder, err := NewDecoder(&DecoderConfig{
Metadata: nil,
Result: &out,
})
if err != nil {
t.Errorf("NewDecoder error: %v", err)
return
}
err = decoder.Decode(raw)
if err == nil {
t.Error("Decode error success, but need err")
}
}