-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter_samber_test.go
182 lines (159 loc) · 3.65 KB
/
writer_samber_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
174
175
176
177
178
179
180
181
182
package csv
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func buildTest(forceDoubleQuotes bool, escapeCharEqual bool, escapeCharPlus bool, escapeCharMinus bool, escapeCharAt bool, escapeCharTab bool, escapeCharCR bool) string {
var buff strings.Builder
writer := NewSafeWriter(
&buff,
SafetyOpts{
ForceDoubleQuotes: forceDoubleQuotes,
EscapeCharEqual: escapeCharEqual,
EscapeCharPlus: escapeCharPlus,
EscapeCharMinus: escapeCharMinus,
EscapeCharAt: escapeCharAt,
EscapeCharTab: escapeCharTab,
EscapeCharCR: escapeCharCR,
},
)
must(writer.Write([]string{"userId", "secret", "comment"}))
must(writer.Write([]string{"-21+63", "=A1", "foo, bar"}))
must(writer.Write([]string{"+42", "\tsecret", "\nplop"}))
must(writer.Write([]string{"123", "blablabla", "@foobar"}))
writer.Flush()
must(writer.Error())
return buff.String()
}
func TestNewSafeWriter(t *testing.T) {
is := assert.New(t)
// base case
is.Equal(
`userId,secret,comment
-21+63,=A1,"foo, bar"
+42," secret","
plop"
123,blablabla,@foobar
`,
buildTest(false, false, false, false, false, false, false),
)
// double quotes
is.Equal(
`"userId","secret","comment"
"-21+63","=A1","foo, bar"
"+42"," secret","
plop"
"123","blablabla","@foobar"
`,
buildTest(true, false, false, false, false, false, false),
)
// escape "="
is.Equal(
`userId,secret,comment
-21+63," =A1","foo, bar"
+42," secret","
plop"
123,blablabla,@foobar
`,
buildTest(false, true, false, false, false, false, false),
)
// escape "+"
is.Equal(
`userId,secret,comment
-21+63,=A1,"foo, bar"
" +42"," secret","
plop"
123,blablabla,@foobar
`,
buildTest(false, false, true, false, false, false, false),
)
// escape "-"
is.Equal(
`userId,secret,comment
" -21+63",=A1,"foo, bar"
+42," secret","
plop"
123,blablabla,@foobar
`,
buildTest(false, false, false, true, false, false, false),
)
// escape "@"
is.Equal(
`userId,secret,comment
-21+63,=A1,"foo, bar"
+42," secret","
plop"
123,blablabla," @foobar"
`,
buildTest(false, false, false, false, true, false, false),
)
// escape "\t"
is.Equal(
`userId,secret,comment
-21+63,=A1,"foo, bar"
+42," secret","
plop"
123,blablabla,@foobar
`,
buildTest(false, false, false, false, false, true, false),
)
// escape "\n"
is.Equal(
`userId,secret,comment
-21+63,=A1,"foo, bar"
+42," secret","
plop"
123,blablabla,@foobar
`,
buildTest(false, false, false, false, false, false, true),
)
// escape everything
is.Equal(
`userId,secret,comment
" -21+63"," =A1","foo, bar"
" +42"," secret","
plop"
123,blablabla," @foobar"
`,
buildTest(false, true, true, true, true, true, true),
)
// escape everything + force double quotes
is.Equal(
`"userId","secret","comment"
" -21+63"," =A1","foo, bar"
" +42"," secret","
plop"
"123","blablabla"," @foobar"
`,
buildTest(true, true, true, true, true, true, true),
)
}
func TestNewSafeWriterNoOpts(t *testing.T) {
is := assert.New(t)
var buff strings.Builder
w := NewSafeWriter(&buff, SafetyOpts{})
is.Empty(w.opts)
}
func TestFullSafety(t *testing.T) {
is := assert.New(t)
is.NotEmpty(FullSafety)
is.True(FullSafety.ForceDoubleQuotes)
is.True(FullSafety.EscapeCharEqual)
is.True(EscapeAll.EscapeCharPlus)
is.True(EscapeAll.EscapeCharMinus)
is.True(EscapeAll.EscapeCharAt)
is.True(EscapeAll.EscapeCharTab)
is.True(EscapeAll.EscapeCharCR)
}
func TestEscapeAll(t *testing.T) {
is := assert.New(t)
is.NotEmpty(EscapeAll)
is.False(EscapeAll.ForceDoubleQuotes)
is.True(EscapeAll.EscapeCharEqual)
is.True(EscapeAll.EscapeCharPlus)
is.True(EscapeAll.EscapeCharMinus)
is.True(EscapeAll.EscapeCharAt)
is.True(EscapeAll.EscapeCharTab)
is.True(EscapeAll.EscapeCharCR)
}