forked from ianlopshire/go-fixedwidth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decode_test.go
234 lines (213 loc) · 6.71 KB
/
decode_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
package fixedwidth
import (
"encoding"
"fmt"
"io"
"log"
"reflect"
"testing"
)
func ExampleUnmarshal() {
// define the format
var people struct {
ID int `fixed:"1,5"`
FirstName string `fixed:"6,15"`
LastName string `fixed:"16,25"`
Grade float64 `fixed:"26,30"`
}
// define some fixed-with data to parse
data := []byte("1 Ian Lopshire 99.50")
err := Unmarshal(data, &people)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", people)
}
func TestUnmarshal(t *testing.T) {
// allTypes contains a field with all current supported types.
type allTypes struct {
String string `fixed:"1,5"`
Int int `fixed:"6,10"`
Float float64 `fixed:"11,15"`
TextUnmarshaler EncodableString `fixed:"16,20"`
}
for _, tt := range []struct {
name string
rawValue []byte
target interface{}
expected interface{}
shouldErr bool
}{
{
name: "Slice Case (no trailing new line)",
rawValue: []byte("foo 123 1.2 bar"),
target: &[]allTypes{},
expected: &[]allTypes{
{"foo", 123, 1.2, EncodableString{"bar", nil}},
},
shouldErr: true,
},
{
name: "Slice Case (trailing new line)",
rawValue: []byte("foo 123 1.2 bar"),
target: &[]allTypes{},
expected: &[]allTypes{
{"foo", 123, 1.2, EncodableString{"bar", nil}},
},
shouldErr: true,
},
{
name: "Slice Case (blank line mid file)",
rawValue: []byte("foo 123 1.2 bar"),
target: &[]allTypes{},
expected: &[]allTypes{
{"foo", 123, 1.2, EncodableString{"bar", nil}},
},
shouldErr: true,
},
{
name: "Basic Struct Case",
rawValue: []byte("foo 123 1.2 bar"),
target: &allTypes{},
expected: &allTypes{"foo", 123, 1.2, EncodableString{"bar", nil}},
shouldErr: false,
},
{
name: "Unmarshal Error",
rawValue: []byte("foo nan ddd bar"),
target: &allTypes{},
expected: &allTypes{},
shouldErr: true,
},
{
name: "Empty Line",
rawValue: []byte(""),
target: &allTypes{},
expected: &allTypes{},
shouldErr: true,
},
{
name: "Invalid Target",
rawValue: []byte("foo 123 1.2 bar"),
target: allTypes{},
expected: allTypes{},
shouldErr: true,
},
} {
t.Run(tt.name, func(t *testing.T) {
err := Unmarshal(tt.rawValue, tt.target)
if tt.shouldErr != (err != nil) {
t.Errorf("Unmarshal() err want %v, have %v (%v)", tt.shouldErr, err != nil, err)
}
if !tt.shouldErr && !reflect.DeepEqual(tt.target, tt.expected) {
t.Errorf("Unmarshal() want %+v, have %+v", tt.expected, tt.target)
}
})
}
t.Run("Field Length 1", func(t *testing.T) {
var st = struct {
F1 string `fixed:"1,1"`
}{}
err := Unmarshal([]byte("v"), &st)
if err != nil {
t.Errorf("Unmarshal() err %v", err)
}
if st.F1 != "v" {
t.Errorf("Unmarshal() want %v, have %v", "v", st.F1)
}
})
t.Run("Invalid Unmarshal Errors", func(t *testing.T) {
for _, tt := range []struct {
name string
v interface{}
shouldErr bool
}{
{"Invalid Unmarshal Nil", nil, true},
{"Invalid Unmarshal Not Pointer 1", struct{}{}, true},
{"Invalid Unmarshal Not Pointer 2", []struct{}{}, true},
{"Valid Unmarshal slice", &[]struct{}{}, true},
{"Valid Unmarshal struct", &struct{}{}, true},
} {
t.Run(tt.name, func(t *testing.T) {
err := Unmarshal([]byte{}, tt.v)
if tt.shouldErr != (err != nil) {
t.Errorf("Unmarshal() err want %v, have %v (%v)", tt.shouldErr, err != nil, err)
}
})
}
})
}
func TestNewValueSetter(t *testing.T) {
for _, tt := range []struct {
name string
raw []byte
expected interface{}
shouldErr bool
}{
{"invalid type", []byte("foo"), true, true},
{"textUnmarshaler implementation", []byte("foo"), &EncodableString{"foo", nil}, false},
{"textUnmarshaler implementation if addressed", []byte("foo"), EncodableString{"foo", nil}, false},
{"textUnmarshaler implementation as interface", []byte("foo"), encoding.TextUnmarshaler(&EncodableString{"foo", nil}), false},
{"textUnmarshaler implementation in interface", []byte("foo"), interface{}(&EncodableString{"foo", nil}), false},
{"textUnmarshaler implementation if addressed in interface", []byte("foo"), interface{}(EncodableString{"foo", nil}), false},
{"string", []byte("foo"), string("foo"), false},
{"string empty", []byte(""), string(""), false},
{"string interface", []byte("foo"), interface{}(string("foo")), false},
{"string interface empty", []byte(""), interface{}(string("")), false},
{"*string", []byte("foo"), stringp("foo"), false},
{"*string empty", []byte(""), (*string)(nil), false},
{"int", []byte("1"), int(1), false},
{"int zero", []byte("0"), int(0), false},
{"int empty", []byte(""), int(0), false},
{"*int", []byte("1"), intp(1), false},
{"*int zero", []byte("0"), intp(0), false},
{"*int empty", []byte(""), (*int)(nil), false},
{"int Invalid", []byte("foo"), int(0), true},
{"float64", []byte("1.23"), float64(1.23), false},
{"*float64", []byte("1.23"), float64p(1.23), false},
{"*float64 zero", []byte("0"), float64p(0), false},
{"*float64 empty", []byte(""), (*float64)(nil), false},
{"float64 Invalid", []byte("foo"), float64(0), true},
{"float32", []byte("1.23"), float32(1.23), false},
{"float32 Invalid", []byte("foo"), float32(0), true},
{"int8", []byte("1"), int8(1), false},
{"int16", []byte("1"), int16(1), false},
{"int32", []byte("1"), int32(1), false},
{"int64", []byte("1"), int64(1), false},
} {
t.Run(tt.name, func(t *testing.T) {
// ensure we have an addressable target
var i = reflect.Indirect(reflect.New(reflect.TypeOf(tt.expected)))
err := newValueSetter(i.Type())(i, tt.raw)
if tt.shouldErr != (err != nil) {
t.Errorf("newValueSetter(%s)() err want %v, have %v (%v)", reflect.TypeOf(tt.expected).Name(), tt.shouldErr, err != nil, err.Error())
}
if !tt.shouldErr && !reflect.DeepEqual(tt.expected, i.Interface()) {
t.Errorf("newValueSetter(%s)() want %s, have %s", reflect.TypeOf(tt.expected).Name(), tt.expected, i)
}
})
}
}
// Verify the behavior of Decoder.Decode at the end of a file. See
// https://github.com/ianlopshire/go-fixedwidth/issues/6 for more details.
func TestDecode_EOF(t *testing.T) {
d := NewDecoder([]byte(""))
type S struct {
Field1 string `fixed:"1,1"`
Field2 string `fixed:"2,2"`
Field3 string `fixed:"3,3"`
}
var s S
err := d.Decode(&s)
if err != io.EOF {
t.Errorf("Decode should have returned an EOF error. Returned: %v", err)
}
d = NewDecoder([]byte("ABC"))
err = d.Decode(&s)
if err != nil {
t.Errorf("Unexpected error from decode")
}
if !reflect.DeepEqual(&s, &S{Field1: "A", Field2: "B", Field3: "C"}) {
t.Errorf("Unexpected result from Decode: %#v", s)
}
}