-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_test.go
142 lines (121 loc) · 3.16 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
package csvutil
import (
"bytes"
"encoding/csv"
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewEncoderValid(t *testing.T) {
type valid struct {
StrField string `csv:"string"`
IntField int `csv:"integer"`
}
var buf bytes.Buffer
_, err := NewEncoder(&buf, valid{})
assert.Nil(t, err)
assert.Equal(t, "string,integer\n", buf.String())
}
func TestNewEncoderFromCSVWriter(t *testing.T) {
type valid struct {
StrField string `csv:"string"`
IntField int `csv:"integer"`
}
var buf bytes.Buffer
w := csv.NewWriter(&buf)
w.Comma = '\t'
_, err := NewEncoderFromCSVWriter(w, valid{})
assert.Nil(t, err)
assert.Equal(t, "string\tinteger\n", buf.String())
}
func TestNewEncoderMissingField(t *testing.T) {
type ignoredFieldStruct struct {
StrField string `csv:"string"`
IgnoredField string
IntField int `csv:"integer"`
}
var buf bytes.Buffer
_, err := NewEncoder(&buf, ignoredFieldStruct{})
assert.Nil(t, err)
assert.Equal(t, "string,integer\n", buf.String())
}
type B struct{}
func (b *B) UnmarshalText(buf []byte) error {
return nil
}
func TestNewEncoderMissingMarshalerInterface(t *testing.T) {
type ignoredFieldStruct struct {
CustomField B `csv:"custom"`
}
var buf bytes.Buffer
_, err := NewEncoder(&buf, ignoredFieldStruct{})
assert.Equal(t, errors.New("unsuported field type found that does not implement"+
" the encoding.TextMarshaler interface: custom"), err)
}
func TestEncodeValid(t *testing.T) {
type valid struct {
StrField string `csv:"string"`
IntField int `csv:"integer"`
}
x := valid{
StrField: "foo",
IntField: 100,
}
var buf bytes.Buffer
enc, err := NewEncoder(&buf, valid{})
assert.Nil(t, err)
err = enc.Write(x)
assert.Nil(t, err)
assert.Equal(t, "string,integer\nfoo,100\n", buf.String())
}
func TestWriteWithMissingField(t *testing.T) {
type ignoredFieldStruct struct {
StrField string `csv:"string"`
IgnoredField string
IntField int `csv:"integer"`
}
x := ignoredFieldStruct{
StrField: "foo",
IntField: 100,
}
var buf bytes.Buffer
enc, err := NewEncoder(&buf, ignoredFieldStruct{})
assert.Nil(t, err)
err = enc.Write(x)
assert.Nil(t, err)
assert.Equal(t, "string,integer\nfoo,100\n", buf.String())
}
func TestWriteCustomMarshaler(t *testing.T) {
type ignoredFieldStruct struct {
StrField string `csv:"string"`
TimeField *time.Time `csv:"time"`
}
x := ignoredFieldStruct{
StrField: "foo",
TimeField: &defaultTime,
}
var buf bytes.Buffer
enc, err := NewEncoder(&buf, ignoredFieldStruct{})
assert.Nil(t, err)
err = enc.Write(x)
assert.Nil(t, err)
assert.Equal(t, fmt.Sprintf("string,time\nfoo,%s\n", defaultTimeStr), buf.String())
// test passing in a pointer
buf.Reset()
enc, err = NewEncoder(&buf, ignoredFieldStruct{})
assert.Nil(t, err)
err = enc.Write(&x)
assert.Nil(t, err)
assert.Equal(t, fmt.Sprintf("string,time\nfoo,%s\n", defaultTimeStr), buf.String())
}
func TestWriteFailNil(t *testing.T) {
type valid struct {
StrField string `csv:"string"`
}
var buf bytes.Buffer
enc, err := NewEncoder(&buf, valid{})
assert.Nil(t, err)
assert.Equal(t, enc.Write(nil), errors.New("Source struct passed in cannot be nil"))
}