-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwriter_test.go
125 lines (113 loc) · 3.08 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
package goltsv
import (
"bytes"
"reflect"
"testing"
)
// TODO(ymotongpoo): Output remains for testing ordered LTSV
var writeTests = []struct {
Input map[string]string
Output string
UseCRLF bool
}{
{
Input: map[string]string{"hoge": "foo"},
Output: "hoge:foo\n",
},
{
Input: map[string]string{"hoge": "foo"},
Output: "hoge:foo\r\n",
UseCRLF: true,
},
{
Input: map[string]string{"perl": "5.17.8", "ruby": "2.0", "python": "3.3.0"},
Output: "perl:5.17.8 ruby:2.0 python:3.3.0\n",
},
{
Input: map[string]string{"sushi": "寿司", "tennpura": "天ぷら", "ramen": "ラーメン", "gyoza": "餃子"},
Output: "sushi:寿司 tennpura:天ぷら ramen:ラーメン gyoza:餃子ushi:寿司 tennpura:天ぷら ramen:ラーメン gyoza:餃子\n",
},
}
// TODO(ymotongpoo): Output remains for testing ordered LTSV
var writeAllTests = []struct {
Input []map[string]string
Output string
UseCRLF bool
}{
{
Input: []map[string]string{
{"hoge": "foo", "bar": "baz"},
{"perl": "5.17.8", "ruby": "2.0", "python": "2.6"},
{"sushi": "寿司", "tennpura": "天ぷら", "ramen": "ラーメン", "gyoza": "餃子"}},
Output: `hoge:foo bar:baz
perl:5.17.8 ruby:2.0 python:2.6
sushi:寿司 tennpura:天ぷら ramen:ラーメン gyoza:餃子\n`,
},
}
func TestWrite(t *testing.T) {
for n, tt := range writeTests {
b := &bytes.Buffer{}
f := NewWriter(b)
f.UseCRLF = tt.UseCRLF
err := f.Write(tt.Input)
err = f.Flush()
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
}
// Note: In Go, map doesn't guarantee order of elements, so comparing
// original map and map generated from output string by LTSVReader.
r := NewReader(b)
out, err := r.Read()
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
}
if !reflect.DeepEqual(out, tt.Input) {
t.Errorf("#%d: out=%q want %q", n, b.String(), tt.Output)
}
}
}
func TestWriteAll(t *testing.T) {
for n, tt := range writeAllTests {
b := &bytes.Buffer{}
f := NewWriter(b)
f.UseCRLF = tt.UseCRLF
err := f.WriteAll(tt.Input)
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
}
// Note: In Go, map doesn't guarantee order of elements, so comparing
// original map and map generated from output string by LTSVReader.
r := NewReader(b)
out, err := r.ReadAll()
if !reflect.DeepEqual(out, tt.Input) {
t.Errorf("#%d: out=%q want %q", n, b.String(), tt.Output)
}
}
}
func TestFlush(t *testing.T) {
b := &bytes.Buffer{}
f := NewWriter(b)
inputs := make([]map[string]string, 0)
for _, tt := range writeTests {
f.UseCRLF = tt.UseCRLF
err := f.Write(tt.Input)
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
}
inputs = append(inputs, tt.Input)
}
err := f.Flush()
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
}
// Note: In Go, map doesn't guarantee order of elements, so comparing
// original map and map generated from output string by LTSVReader.
r := NewReader(b)
out, err := r.ReadAll()
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
}
if !reflect.DeepEqual(out, inputs) {
t.Errorf("out=%q want %q", b.String(), inputs)
}
}