forked from mohae/struct2csv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriter_test.go
173 lines (163 loc) · 6.5 KB
/
writer_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
package struct2csv
import (
"bytes"
"testing"
)
var rowTests = []struct {
Input [][]string
UseCRLF bool
Output string
}{
{Input: [][]string{}, UseCRLF: false, Output: ""},
{Input: [][]string{}, UseCRLF: true, Output: ""},
{Input: [][]string{[]string{""}}, UseCRLF: false, Output: "\n"},
{Input: [][]string{[]string{""}}, UseCRLF: true, Output: "\r\n"},
{Input: [][]string{[]string{"abc"}}, UseCRLF: false, Output: "abc\n"},
{Input: [][]string{[]string{"def"}}, UseCRLF: true, Output: "def\r\n"},
{Input: [][]string{[]string{"Col1"}, []string{"abc"}}, UseCRLF: false, Output: "Col1\nabc\n"},
{Input: [][]string{[]string{"ColA"}, []string{"def"}}, UseCRLF: true, Output: "ColA\r\ndef\r\n"},
{Input: [][]string{[]string{"a", "b", "c"}}, UseCRLF: false, Output: "a,b,c\n"},
{Input: [][]string{[]string{"d", "e", "f"}}, UseCRLF: true, Output: "d,e,f\r\n"},
{Input: [][]string{[]string{"Col1", "Col2", "Col3"}, []string{"a", "b", "c"}}, UseCRLF: false, Output: "Col1,Col2,Col3\na,b,c\n"},
{Input: [][]string{[]string{"ColA", "ColB", "ColC"}, []string{"d", "e", "f"}}, UseCRLF: true, Output: "ColA,ColB,ColC\r\nd,e,f\r\n"},
}
func TestRows(t *testing.T) {
for i, test := range rowTests {
b := &bytes.Buffer{}
w := NewWriter(b)
w.SetUseCRLF(test.UseCRLF)
err := w.WriteAll(test.Input)
if err != nil {
t.Errorf("%d: unexpected error: %s", i, err)
}
out := b.String()
if out != test.Output {
t.Errorf("%d: out=%q want %q", i, out, test.Output)
}
}
for i, test := range rowTests {
buff := &bytes.Buffer{}
w := NewWriter(buff)
w.SetUseCRLF(test.UseCRLF)
for j, v := range test.Input {
err := w.Write(v)
if err != nil {
t.Errorf("%d:%d: unexpected error: %s", i, j, err)
}
}
w.Flush()
out := buff.String()
if out != test.Output {
t.Errorf("%d: out=%q want %q", i, out, test.Output)
}
}
}
var structTests = []struct {
Input []Basic
UseTags bool
Tag string
Output string
Error string
}{
{[]Basic{}, true, "csv", "\n", "struct2csv: the slice of structs was empty"},
{[]Basic{}, true, "json", "\n", "struct2csv: the slice of structs was empty"},
{[]Basic{}, true, "", "\n", "struct2csv: the slice of structs was empty"},
{[]Basic{}, false, "", "\n", "struct2csv: the slice of structs was empty"},
{[]Basic{
Basic{Name: "Fyodor Dostoyevsky", List: []string{"Brothers Karamazov", "Crime and Punishment"}},
}, true, "csv", "Nom,Liste\nFyodor Dostoyevsky,\"Brothers Karamazov,Crime and Punishment\"\n", ""},
{[]Basic{
Basic{Name: "Fyodor Dostoyevsky", List: []string{"Brothers Karamazov", "Crime and Punishment"}},
}, true, "json", "name,list\nFyodor Dostoyevsky,\"Brothers Karamazov,Crime and Punishment\"\n", ""},
{[]Basic{
Basic{Name: "Fyodor Dostoyevsky", List: []string{"Brothers Karamazov", "Crime and Punishment"}},
}, true, "", "Nom,Liste\nFyodor Dostoyevsky,\"Brothers Karamazov,Crime and Punishment\"\n", ""},
{[]Basic{
Basic{Name: "Fyodor Dostoyevsky", List: []string{"Brothers Karamazov", "Crime and Punishment"}},
}, false, "", "Name,List\nFyodor Dostoyevsky,\"Brothers Karamazov,Crime and Punishment\"\n", ""},
{[]Basic{
Basic{Name: "Anatoly Rybakov", List: []string{"Children of the Arbat", "Fear", "Dust and Ashes"}},
Basic{Name: "Vladimir Nabokov", List: []string{"Lolita", "Pnin", "Pale Fire"}},
}, true, "csv",
"Nom,Liste\nAnatoly Rybakov,\"Children of the Arbat,Fear,Dust and Ashes\"\nVladimir Nabokov,\"Lolita,Pnin,Pale Fire\"\n", ""},
{[]Basic{
Basic{Name: "Anatoly Rybakov", List: []string{"Children of the Arbat", "Fear", "Dust and Ashes"}},
Basic{Name: "Vladimir Nabokov", List: []string{"Lolita", "Pnin", "Pale Fire"}},
}, true, "json",
"name,list\nAnatoly Rybakov,\"Children of the Arbat,Fear,Dust and Ashes\"\nVladimir Nabokov,\"Lolita,Pnin,Pale Fire\"\n", ""},
{[]Basic{
Basic{Name: "Anatoly Rybakov", List: []string{"Children of the Arbat", "Fear", "Dust and Ashes"}},
Basic{Name: "Vladimir Nabokov", List: []string{"Lolita", "Pnin", "Pale Fire"}},
}, true, "",
"Nom,Liste\nAnatoly Rybakov,\"Children of the Arbat,Fear,Dust and Ashes\"\nVladimir Nabokov,\"Lolita,Pnin,Pale Fire\"\n", ""},
{[]Basic{
Basic{Name: "Anatoly Rybakov", List: []string{"Children of the Arbat", "Fear", "Dust and Ashes"}},
Basic{Name: "Vladimir Nabokov", List: []string{"Lolita", "Pnin", "Pale Fire"}},
}, false, "",
"Name,List\nAnatoly Rybakov,\"Children of the Arbat,Fear,Dust and Ashes\"\nVladimir Nabokov,\"Lolita,Pnin,Pale Fire\"\n", ""},
}
func TestStructs(t *testing.T) {
for i, test := range structTests {
buff := &bytes.Buffer{}
w := NewWriter(buff)
w.SetTag(test.Tag)
w.SetUseTags(test.UseTags)
err := w.WriteStructs(test.Input)
if err != nil {
if err.Error() != test.Error {
t.Errorf("%d: expected error %q got %q", i, test.Error, err)
}
continue
}
s := buff.String()
if s != test.Output {
t.Errorf("%d: got %s, want %s", i, s, test.Output)
}
}
for i, test := range structTests {
if len(test.Input) == 0 {
continue
}
buff := &bytes.Buffer{}
w := NewWriter(buff)
w.SetTag(test.Tag)
w.SetUseTags(test.UseTags)
err := w.WriteColNames(test.Input[0])
if err != nil {
t.Errorf("%d: unexpected error: %s", i, err)
continue
}
for j, row := range test.Input {
err = w.WriteStruct(row)
if err != nil {
t.Errorf("%d:%d unexpected error: %s", i, j, err)
continue
}
}
w.Flush()
s := buff.String()
if s != test.Output {
t.Errorf("%d: got %s, want %s", i, s, test.Output)
}
}
}
func TestWriteStructsComplex(t *testing.T) {
expected := "MapMap,MapSlice,Map2DSlice,MapBasic,MapBasicSlice,MapBasic2DSlice\n" +
"\"Region 1:(colo1:rack1,colo2:rack2),Region 2:(colo11:rack11,colo12:rack12)\"," +
"\"Canada:(Alberta,British Columbia,Quebec),USA:(California,Florida,New York)\"," +
"\"Canada:((Calgary,Edmonton,Fort McMurray),(Winnipeg)),USA:((San Diego,Los Angeles))\"," +
"\"Gibson:(William Gibson,(Neuromancer,Count Zero,Mona Lisa Overdrive)),Herbert:(Frank Herbert,(Destination Void,Jesus Incident,Lazurus Effect))\"," +
"\"SciFi:((William Gibson,(Neuromancer,Count Zero,Mona Lisa Overdrive)),(Frank Herbert,(Destination Void,Jesus Incident,Lazurus Effect)))\"," +
"\"Sci-Fi:(((William Gibson,(Neuromancer,Count Zero,Mona Lisa Overdrive)),(Frank Herbert,(Destination Void,Jesus Incident,Lazurus Effect))),((Douglas Adams,(Restaurant at the End of the Universe))))\"\n"
buff := &bytes.Buffer{}
w := NewWriter(buff)
err := w.WriteStructs(complexTests)
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
s := buff.String()
if s != expected {
t.Errorf("got %q, want %q", s, expected)
}
}