-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflatfile_test.go
72 lines (66 loc) · 1.55 KB
/
flatfile_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
package flatfile
import (
"bufio"
"io"
"strings"
"testing"
)
type testType struct {
Data string `flatfile:"col=1,len=10"`
Number int `flatfile:"col=11,len=1"`
}
func TestFlatFileNew(t *testing.T) {
reader := bufio.NewReader(strings.NewReader(""))
_, err := New(reader, &testType{})
if err != nil {
t.Errorf("%s", err.Error())
}
}
func TestFlatFileNew_Err(t *testing.T) {
reader := bufio.NewReader(strings.NewReader(""))
_, err := New(reader, testType{})
if err == nil {
t.Errorf("Expected not a pointer error")
}
t.Log(err)
}
func TestFlatFileRead(t *testing.T) {
testCases := []struct {
desc string
lineData string
got *testType
want testType
wantErr error
}{
{
desc: "Read nothing",
lineData: "",
got: &testType{},
want: testType{Data: "", Number: 0},
wantErr: io.EOF,
},
{
desc: "Read some data",
lineData: "DATA!DATA!9",
got: &testType{},
want: testType{Data: "DATA!DATA!", Number: 9},
wantErr: nil,
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
reader := bufio.NewReader(strings.NewReader(tC.lineData))
file, err := New(reader, tC.got)
if err != nil {
t.Errorf("Unexpected error %s", err.Error())
}
err = file.Read()
if err != nil && err != tC.wantErr {
t.Errorf("Unexpected error %s wantErr %v", err.Error(), tC.wantErr)
}
if tC.got.Data != tC.want.Data || tC.got.Number != tC.want.Number {
t.Errorf("flatfile.Read() got %v want %v with error %v", tC.got, tC.want, tC.wantErr)
}
})
}
}