-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassthrough_test.go
86 lines (71 loc) · 1.79 KB
/
passthrough_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
package csv
import (
"io"
"strings"
"testing"
)
// TestWriterToReader tests that feeding records to a Writer and then into a Reader should result in unchanged records
func TestWriterToReader(t *testing.T) {
cases := [][][]string{
{{""}},
{{""}, {""}},
{{"foo"}, {"bar", "baz"}},
}
for i := 0; i < len(cases); i++ {
b := strings.Builder{}
w := NewWriter(&b)
err := w.WriteAll(cases[i])
if err != nil {
t.Fatal(err)
}
err = w.Flush()
if err != nil {
t.Fatal(err)
}
r := NewReader(strings.NewReader(b.String()))
records, err := r.ReadAll()
if err != io.EOF {
t.Fatalf("case %v expected %v got %v", i, io.EOF, err)
}
if len(records) != len(cases[i]) {
t.Fatalf("case %v expected result %v got %v", i, cases[i], records)
}
for j := 0; j < len(records); j++ {
if len(records[j]) != len(cases[i][j]) {
t.Fatalf("case %v expected result %v got %v", i, cases[i], records)
}
for k := 0; k < len(records[j]); k++ {
if records[j][k] != cases[i][j][k] {
t.Fatalf("case %v expected result %v got %v", i, cases[i], records)
}
}
}
}
}
// TestReaderToWriter tests cases where feeding CSV to a Reader and then into a Writer should result in unchanged CSV
func TestReaderToWriter(t *testing.T) {
cases := []string{
"\"\"\r\n",
"\"foo\",\"bar\"\r\n\"baz\"\r\n",
}
for i := 0; i < len(cases); i++ {
r := NewReader(strings.NewReader(cases[i]))
records, err := r.ReadAll()
if err != io.EOF {
t.Fatalf("case %v expected %v got %v", i, io.EOF, err)
}
b := strings.Builder{}
w := NewWriter(&b)
err = w.WriteAll(records)
if err != nil {
t.Fatal(err)
}
err = w.Flush()
if err != nil {
t.Fatal(err)
}
if b.String() != cases[i] {
t.Fatalf("case %v expected result '%s' got '%s'", i, cases[i], b.String())
}
}
}