forked from jweir/csv
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecode_test.go
More file actions
145 lines (116 loc) · 2.66 KB
/
decode_test.go
File metadata and controls
145 lines (116 loc) · 2.66 KB
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
package csv
import (
"reflect"
"testing"
)
type Q struct {
String string
Int int
unxported int
Bool bool `true:"Yes" false:"No"`
Float32 float32
Float64 float64
Complex64 complex64 `csv:"C64"`
}
func TestUnmarshal(t *testing.T) {
doc := []byte(`String,Int,unexported,Bool,Float32,Float64,C64
John,23,1,Yes,32.2,64.1,1
Jane,27,2,No,33.1,65.1,2
Bill,28,3,Yes,34.7,65.1,3`)
pp := []Q{}
Unmarshal(doc, &pp)
if len(pp) != 3 {
t.Errorf("Incorrect record length: %d", len(pp))
}
assert := func(e, a interface{}) {
if e != a {
t.Errorf("expected (%s) got (%s)", e, a)
}
}
strs := []string{"John", "Jane", "Bill"}
ints := []int{23, 27, 28}
bools := []bool{true, false, true}
f32s := []float32{32.2, 33.1, 34.7}
f64s := []float64{64.1, 65.1, 65.1}
for i, p := range pp {
assert(strs[i], p.String)
assert(ints[i], p.Int)
assert(bools[i], p.Bool)
assert(f32s[i], p.Float32)
assert(f64s[i], p.Float64)
}
}
func TestMarshalErrors(t *testing.T) {
doc := []byte(`Name,Age`)
err := Unmarshal(doc, []Q{})
if err == nil {
t.Error("No error generated for non-pointer")
}
err = Unmarshal(doc, &Q{})
if err == nil {
t.Error("No error generated for non-slice")
}
pp := []Q{}
err = Unmarshal(doc, &pp)
if err != nil {
t.Error("Error returned when not expected:", err)
}
}
func TestExportedFields(t *testing.T) {
type s struct {
Name string
Age int `csv:"Age"`
priv int `csv:"-"`
}
fs := exportedFields(reflect.TypeOf(s{}))
if len(fs) != 2 {
t.Errorf("Incorrect number of exported fields 2 expected got %d", len(fs))
}
if fs[0].Name != "Name" || fs[1].Name != "Age" {
t.Error("Incorrect returned fields")
}
}
type T struct {
Name string
age string // unexported, should not be included
Addr string `csv:"Address"`
NoMatch int // public, but no match in the CSV headers
}
func TestMapFields(t *testing.T) {
dec := &decoder{Type: reflect.TypeOf(T{})}
cols := []string{
"Name",
"age", // should not match since the 'age' field is not exported
"Address",
}
dec.mapFieldsToCols(cols)
fm := dec.cfields
if len(fm) != 2 {
t.Errorf("Expected length of 2, got %d", len(fm))
}
for i, n := range []int{0, 2} {
if fm[i].colIndex != n {
t.Errorf("expected colIndex of %d got %d", fm[i].colIndex, n)
}
}
}
type Um struct {
V string
}
type U struct {
Name Um
}
func (u *Um) UnmarshalCSV(val string, row *Row) error {
v, _ := row.Named("Age")
u.V = row.At(0) + " " + v
return nil
}
func TestCustomUnMarshaller(t *testing.T) {
doc := `Name,Age
Jay,23`
oo := []U{}
Unmarshal([]byte(doc), &oo)
if oo[0].Name.V != "Jay 23" {
t.Errorf("custom unmarshal did not work (%s)", oo[0].Name.V)
}
}