-
Notifications
You must be signed in to change notification settings - Fork 34
/
encode_test.go
351 lines (318 loc) · 10.5 KB
/
encode_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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package fixedwidth
import (
"bytes"
"errors"
"fmt"
"log"
"reflect"
"testing"
)
func ExampleMarshal() {
// define some data to encode
people := []struct {
ID int `fixed:"1,5"`
FirstName string `fixed:"6,15"`
LastName string `fixed:"16,25"`
Grade float64 `fixed:"26,30"`
Age uint `fixed:"31,33"`
Alive bool `fixed:"34,39"`
}{
{1, "Ian", "Lopshire", 99.5, 20, true},
}
data, err := Marshal(people)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", data)
// Output:
// 1 Ian Lopshire 99.5020 true
}
func ExampleMarshal_configurableFormatting() {
// define some data to encode
people := []struct {
ID int `fixed:"1,5,right,#"`
FirstName string `fixed:"6,15,right,#"`
LastName string `fixed:"16,25,right,#"`
Grade float64 `fixed:"26,30,right,#"`
Age uint `fixed:"31,33,right,#"`
Alive bool `fixed:"34,39,right,#"`
}{
{1, "Ian", "Lopshire", 99.5, 20, true},
}
data, err := Marshal(people)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", data)
// Output:
// ####1#######Ian##Lopshire99.50#20##true
}
func TestMarshal(t *testing.T) {
type H struct {
F1 interface{} `fixed:"1,5"`
F2 interface{} `fixed:"6,10"`
}
type H2 struct {
F1 bool `fixed:"1,1"`
}
tagHelper := struct {
Valid string `fixed:"1,5"`
NoTags string
InvalidTags string `fixed:"5"`
}{"foo", "foo", "foo"}
marshalError := errors.New("marshal error")
var invtype func()
for _, tt := range []struct {
name string
i interface{}
o []byte
shouldErr bool
}{
{"single line", H{"foo", 1}, []byte("foo 1 "), false},
{"multiple line", []H{{"foo", 1}, {"bar", 2}}, []byte("foo 1 \nbar 2 "), false},
{"multiple line (diff struct)", []interface{}{H{"foo", 1}, H2{false}}, []byte("foo 1 \nf"), false},
{"empty slice", []H{}, nil, false},
{"pointer", &H{"foo", 1}, []byte("foo 1 "), false},
{"nil", nil, nil, false},
{"invalid type", invtype, nil, true},
{"invalid type in struct", H{"foo", invtype}, nil, true},
{"marshal error", EncodableString{"", marshalError}, nil, true},
{"invalid tags", tagHelper, []byte("foo "), false},
} {
t.Run(tt.name, func(t *testing.T) {
o, err := Marshal(tt.i)
if tt.shouldErr != (err != nil) {
t.Errorf("Marshal() shouldErr expected %v, have %v (%v)", tt.shouldErr, err != nil, err)
}
if !tt.shouldErr && !bytes.Equal(o, tt.o) {
t.Errorf("Marshal() expected %q, have %q", string(tt.o), string(o))
}
// All tests should also pass with codepoint indices enabled.
t.Run("use codepoint indices", func(t *testing.T) {
buff := bytes.NewBuffer(nil)
enc := NewEncoder(buff)
enc.SetUseCodepointIndices(true)
err := enc.Encode(tt.i)
if tt.shouldErr != (err != nil) {
t.Errorf("Marshal() shouldErr expected %v, have %v (%v)", tt.shouldErr, err != nil, err)
}
if !tt.shouldErr && !bytes.Equal(buff.Bytes(), tt.o) {
t.Errorf("Marshal() expected %q, have %q", string(tt.o), string(o))
}
})
})
}
}
func TestMarshal_useCodepointIndices(t *testing.T) {
type H struct {
F1 string `fixed:"1,5"`
F2 string `fixed:"6,10"`
F3 string `fixed:"11,15"`
}
type HF struct {
F1 string `fixed:"1,5,right,#"`
F2 string `fixed:"6,10,left,#"`
F3 string `fixed:"11,15"`
}
for _, tt := range []struct {
name string
i interface{}
o []byte
}{
{name: "base case", i: H{"føø", "bår", "båz"}, o: []byte(`føø bår båz `)},
{name: "overflow", i: H{"føøøøøøøøøø", "bååååååååår", "bååååååååz"}, o: []byte(`føøøøbååååbåååå`)},
{name: "formatted", i: HF{"føø", "bår", "båz"}, o: []byte(`##føøbår##båz `)},
{name: "multibformatted overflow", i: HF{"føøøøøøøøøø", "bååååååååår", "bååååååååz"}, o: []byte(`føøøøbååååbåååå`)},
} {
t.Run(tt.name, func(t *testing.T) {
buff := bytes.NewBuffer(nil)
enc := NewEncoder(buff)
enc.SetUseCodepointIndices(true)
if err := enc.Encode(tt.i); err != nil {
t.Errorf("Marshal() unexpected error: %v", err)
return
}
if o := buff.Bytes(); !bytes.Equal(o, tt.o) {
t.Errorf("Marshal() expected %q, have %q", string(tt.o), string(o))
}
})
}
}
func TestMarshal_format(t *testing.T) {
type H struct {
F1 string `fixed:"1,5,left"`
F2 string `fixed:"6,10,left,#"`
F3 string `fixed:"11,15,right"`
F4 string `fixed:"16,20,right,#"`
F5 string `fixed:"21,25,default"`
F6 string `fixed:"26,30,default,#"`
}
for _, tt := range []struct {
name string
v interface{}
want []byte
shouldErr bool
}{
{
name: "base case",
v: H{"foo", "bar", "biz", "baz", "bor", "box"},
want: []byte(`foo ` + `bar##` + ` biz` + `##baz` + `bor ` + `box##`),
shouldErr: false,
},
{
name: "empty",
v: H{"", "", "", "", "", ""},
want: []byte(` ` + `#####` + ` ` + `#####` + ` ` + `#####`),
shouldErr: false,
},
{
name: "overflow",
v: H{"12345678", "12345678", "12345678", "12345678", "12345678", "12345678"},
want: []byte(`12345` + `12345` + `12345` + `12345` + `12345` + `12345`),
shouldErr: false,
},
{
name: "pad right",
v: struct {
F1 string `fixed:"1,5,right,#"`
}{"foo"},
want: []byte(`##foo`),
shouldErr: false,
},
{
name: "pad left",
v: struct {
F1 string `fixed:"1,5,left,#"`
}{"foo"},
want: []byte(`foo##`),
shouldErr: false,
},
} {
t.Run(tt.name, func(t *testing.T) {
have, err := Marshal(tt.v)
if tt.shouldErr != (err != nil) {
t.Errorf("Marshal() err want %v, have %v (%v)", tt.shouldErr, err != nil, err)
}
if !bytes.Equal(tt.want, have) {
t.Errorf("Marshal() want %q, have %q", string(tt.want), string(have))
}
})
}
}
func TestMarshal_backwardCompatibility(t *testing.T) {
// Overlapping intervals can, in effect, be used to coalesce a value. This tests
// ensures this special does not break.
t.Run("interval overlap coalesce", func(t *testing.T) {
type H struct {
F1 string `fixed:"1,5"`
F2 string `fixed:"1,5"`
}
have, err := Marshal(H{F1: "val"})
if err != nil {
t.Fatalf("Marshal() unexpected error: %v", err)
}
if want := []byte(`val `); !bytes.Equal(have, want) {
t.Errorf("Marshal() want %q, have %q", string(want), string(have))
}
have, err = Marshal(H{F2: "val"})
if err != nil {
t.Fatalf("Marshal() unexpected error: %v", err)
}
if want := []byte(`val `); !bytes.Equal(have, want) {
t.Errorf("Marshal() want %q, have %q", string(want), string(have))
}
})
}
func TestEncoder_regressions(t *testing.T) {
// Ensure the encoder doesn't panic when encoding an empty value at the end of a line
// that contains multi-byte characters.
// See: https://github.com/ianlopshire/go-fixedwidth/issues/58
t.Run("issue 58", func(t *testing.T) {
var v struct {
Foo string `fixed:"1,1"`
Bar string `fixed:"2,2,right"`
}
v.Foo = "Ç"
buf := new(bytes.Buffer)
e := NewEncoder(buf)
e.SetUseCodepointIndices(true)
_ = e.Encode(v)
})
}
func TestNewValueEncoder(t *testing.T) {
for _, tt := range []struct {
name string
i interface{}
o []byte
shouldErr bool
}{
{"nil", nil, []byte(""), false},
{"nil interface", interface{}(nil), []byte(""), false},
{"[]string (invalid)", []string{"a", "b"}, []byte(""), true},
{"[]string interface (invalid)", interface{}([]string{"a", "b"}), []byte(""), true},
{"string", "foo", []byte("foo"), false},
{"string interface", interface{}("foo"), []byte("foo"), false},
{"string empty", "", []byte(""), false},
{"*string", stringp("foo"), []byte("foo"), false},
{"*string empty", stringp(""), []byte(""), false},
{"*string nil", nilString, []byte(""), false},
{"float64", float64(123.4567), []byte("123.46"), false},
{"float64 interface", interface{}(float64(123.4567)), []byte("123.46"), false},
{"float64 zero", float64(0), []byte("0.00"), false},
{"*float64", float64p(123.4567), []byte("123.46"), false},
{"*float64 zero", float64p(0), []byte("0.00"), false},
{"*float64 nil", nilFloat64, []byte(""), false},
{"float32", float32(123.4567), []byte("123.46"), false},
{"float32 interface", interface{}(float32(123.4567)), []byte("123.46"), false},
{"float32 zero", float32(0), []byte("0.00"), false},
{"*float32", float32p(123.4567), []byte("123.46"), false},
{"*float32 zero", float32p(0), []byte("0.00"), false},
{"*float32 nil", nilFloat32, []byte(""), false},
{"int", int(123), []byte("123"), false},
{"int interface", interface{}(int(123)), []byte("123"), false},
{"int zero", int(0), []byte("0"), false},
{"*int", intp(123), []byte("123"), false},
{"*int zero", intp(0), []byte("0"), false},
{"*int nil", nilInt, []byte(""), false},
{"bool positive", bool(true), []byte("true"), false},
{"bool interface positive", interface{}(bool(true)), []byte("true"), false},
{"*bool positive", boolp(true), []byte("true"), false},
{"*bool negative", boolp(false), []byte("false"), false},
{"TextUnmarshaler", EncodableString{"foo", nil}, []byte("foo"), false},
{"TextUnmarshaler interface", interface{}(EncodableString{"foo", nil}), []byte("foo"), false},
{"TextUnmarshaler error", EncodableString{"foo", errors.New("TextUnmarshaler error")}, []byte("foo"), true},
{"uint", uint(123), []byte("123"), false},
{"uint interface", interface{}(uint(123)), []byte("123"), false},
{"uint zero", uint(0), []byte("0"), false},
{"*uint", uintp(123), []byte("123"), false},
{"*uint zero", uintp(0), []byte("0"), false},
{"*uint nil", nilUint, []byte(""), false},
} {
t.Run(tt.name, func(t *testing.T) {
o, err := newValueEncoder(reflect.TypeOf(tt.i), false)(reflect.ValueOf(tt.i))
if tt.shouldErr != (err != nil) {
t.Errorf("newValueEncoder(%s)() shouldErr expected %v, have %v (%v)", reflect.TypeOf(tt.i).Name(), tt.shouldErr, err != nil, err)
}
if !tt.shouldErr && !bytes.Equal([]byte(o.data), tt.o) {
t.Errorf("newValueEncoder(%s)() expected %v, have %v", reflect.TypeOf(tt.i).Name(), tt.o, o)
}
})
}
}
func TestEncoder_SetLineTerminator(t *testing.T) {
buff := new(bytes.Buffer)
enc := NewEncoder(buff)
enc.SetLineTerminator([]byte{'\r', '\n'})
input := []interface{}{
EncodableString{"foo", nil},
EncodableString{"bar", nil},
}
err := enc.Encode(input)
if err != nil {
t.Fatal("Encode() unexpected error")
}
expected := []byte("foo\r\nbar")
if !bytes.Equal(expected, buff.Bytes()) {
t.Errorf("Encode() expected %q, have %q", expected, buff.Bytes())
}
}