forked from JohannesKaufmann/html-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhitespace_test.go
247 lines (232 loc) · 5.05 KB
/
whitespace_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package collapse
import (
"regexp"
"strings"
"testing"
)
// This is the alternative (but slower) function that uses regex.
func _regexReplaceAnyWhitespaceWithSpace(text string) string {
var rAnyWhitespace = regexp.MustCompile(`[ \r\n\t]+`)
return rAnyWhitespace.ReplaceAllString(text, " ")
}
func TestReplaceAnyWhitespaceWithSpace(t *testing.T) {
runs := []struct {
desc string
input string
expected string
}{
{
desc: "empty",
input: "",
expected: "",
},
{
desc: "one space",
input: " ",
expected: " ",
},
{
desc: "two spaces",
input: " ",
expected: " ",
},
{
desc: "many spaces",
input: " ",
expected: " ",
},
{
desc: "one newline",
input: "\n",
expected: " ",
},
{
desc: "many newlines",
input: "\n\n\n\n",
expected: " ",
},
{
desc: "combination of newlines and spaces",
input: "\n a \nb \nc\n",
expected: " a b c ",
},
{
desc: "special dash",
input: " \u2013 ",
expected: " \u2013 ",
},
{
desc: "no spaces in text",
input: "abcdef",
expected: "abcdef",
},
{
desc: "one space in text",
input: "abc def",
expected: "abc def",
},
{
desc: "two spaces in text",
input: "abc def",
expected: "abc def",
},
{
desc: "one newline in text",
input: "abc\ndef",
expected: "abc def",
},
{
desc: "two newlines in text",
input: "abc\n\ndef",
expected: "abc def",
},
{
desc: "a newline and space in text",
input: "abc \ndef",
expected: "abc def",
},
{
desc: "one space before text",
input: " abcdef",
expected: " abcdef",
},
{
desc: "two spaces before text",
input: " abcdef",
expected: " abcdef",
},
{
desc: "one space after text",
input: "abcdef ",
expected: "abcdef ",
},
{
desc: "two spaces after text",
input: "abcdef ",
expected: "abcdef ",
},
{
desc: "multiple spaces before & one space after",
input: " or ",
expected: " or ",
},
{
desc: "multiple spaces before & multiple spaces after",
input: " or ",
expected: " or ",
},
{
desc: "one space before & multiple spaces after",
input: " or ",
expected: " or ",
},
}
for _, run := range runs {
t.Run(run.desc, func(t *testing.T) {
t.Run("Regex Version", func(t *testing.T) {
output := _regexReplaceAnyWhitespaceWithSpace(run.input)
if output != run.expected {
t.Errorf("expected %q but got %q", run.expected, output)
}
})
t.Run("New Version", func(t *testing.T) {
output := replaceAnyWhitespaceWithSpace(run.input)
if output != run.expected {
t.Errorf("expected %q but got %q", run.expected, output)
}
// Instead of writing all tests twice...
output2 := replaceAnyWhitespaceWithSpace(strings.ReplaceAll(run.input, " ", "\n"))
if output2 != run.expected {
t.Errorf("for newlines: expected %q but got %q", run.expected, output2)
}
})
})
}
}
func FuzzReplaceAnyWhitespaceWithSpace(f *testing.F) {
f.Add("abc def")
f.Add(" ")
f.Add("abc\n\ndef")
f.Fuzz(func(t *testing.T, orig string) {
output1 := _regexReplaceAnyWhitespaceWithSpace(orig)
output2 := replaceAnyWhitespaceWithSpace(orig)
if output1 != output2 {
t.Errorf("input:%q => regex: %q function: %q", orig, output1, output2)
}
})
}
func TestReplaceAnyWhitespaceWithSpace_Allocs(t *testing.T) {
const N = 1000
runs := []struct {
desc string
input string
expectedAllocs float64
}{
{
desc: "empty string",
input: "",
expectedAllocs: 0,
},
{
desc: "one space",
input: " ",
expectedAllocs: 0,
},
{
desc: "no spaces",
input: "abcdef",
expectedAllocs: 0,
},
{
desc: "one space at start",
input: " abcdef",
expectedAllocs: 0,
},
{
desc: "one space at end",
input: "abcdef ",
expectedAllocs: 0,
},
{
desc: "one space in middle",
input: "abc def",
expectedAllocs: 0,
},
{
desc: "one space at start, middle and end",
input: " abc def ",
expectedAllocs: 0,
},
{
desc: "one space at start, middle and end",
input: "some longer text with spaces",
expectedAllocs: 0,
},
{
desc: "multiple spaces",
input: "abc def",
expectedAllocs: 1,
},
{
desc: "multiple newlines & spaces",
input: "\n\nab cdef \n",
expectedAllocs: 1,
},
{
desc: "longer string",
input: strings.Repeat("Lorem Ipsum is simply dummy text", 10),
expectedAllocs: 0,
},
}
for _, run := range runs {
t.Run(run.desc, func(t *testing.T) {
avg := testing.AllocsPerRun(N, func() {
output := replaceAnyWhitespaceWithSpace(run.input)
_ = output
})
if avg != run.expectedAllocs {
t.Errorf("expected %f allocations but got %f", run.expectedAllocs, avg)
}
})
}
}