-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshaler_public_test.go
83 lines (65 loc) · 1.55 KB
/
marshaler_public_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
package records_test
import (
"encoding/csv"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/huboh/records"
)
const (
csvTestFile = "./testdata/test.csv"
)
type csvTestFileEntries struct {
Age int `csv:"age"`
Name string `csv:"name"`
IsEmployee bool `csv:"isEmployee"`
}
func checkErr(e error, f func(e error)) {
if e != nil {
f(e)
}
}
func TestMarshal(t *testing.T) {
entries := []csvTestFileEntries{
{20, "john", false},
{20, "mary", true},
{24, "saint", false},
{30, "helen", true},
}
csvRecordsExpectation := [][]string{
{"age", "name", "isEmployee"},
{"20", "john", "false"},
{"20", "mary", "true"},
{"24", "saint", "false"},
{"30", "helen", "true"},
}
csvRecords, entriesErr := records.Marshal(entries)
checkErr(entriesErr, func(e error) {
t.Error(e)
})
if diff := cmp.Diff(csvRecords, csvRecordsExpectation); diff != "" {
t.Error(diff)
}
}
func TestUnmarshal(t *testing.T) {
r, err := os.Open(csvTestFile)
checkErr(err, func(e error) {
t.Fatal("could not open test file", e)
})
csvReader := csv.NewReader(r)
csvRecords, err := csvReader.ReadAll()
checkErr(err, func(e error) {
t.Fatal("could not read test file", e)
})
entries := []csvTestFileEntries{}
extriesExpectation := []csvTestFileEntries{
{20, "john", false}, {20, "mary", true}, {24, "saint", false}, {30, "helen", true},
}
entriesErr := records.Unmarshal(csvRecords, &entries)
checkErr(entriesErr, func(e error) {
t.Error(e)
})
if diff := cmp.Diff(entries, extriesExpectation); diff != "" {
t.Error(diff)
}
}