-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_plaintext_test.go
246 lines (235 loc) Β· 6.02 KB
/
utils_plaintext_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
package glog
import (
"testing"
)
func TestReplaceEmojis(t *testing.T) {
tests := []struct {
input string
replace string
expected string
}{
{
input: "This string has no emojis.",
replace: " ",
expected: "This string has no emojis.",
},
{
input: "ππππ",
replace: " ",
expected: " ",
},
{
input: "This string contains emojis ππππ",
replace: " ",
expected: "This string contains emojis ",
},
{
input: "This string contains non-printable characters \x02",
replace: " ",
expected: "This string contains non-printable characters \x02",
},
{
input: "This string contains ANSI escape codes \x1b[31m\x1b[1mred and bold\x1b[0m.",
replace: " ",
expected: "This string contains ANSI escape codes \x1b[31m\x1b[1mred and bold\x1b[0m.",
},
}
for _, tt := range tests {
output := ReplaceEmojis(tt.input, tt.replace)
if output != tt.expected {
t.Errorf("ReplaceEmojis(%q, %q): expected %q, but got %q", tt.input, tt.replace, tt.expected, output)
}
}
}
func TestStripANSI(t *testing.T) {
tests := []struct {
input string
expected string
}{
{
input: "This string has no ANSI escape codes.",
expected: "This string has no ANSI escape codes.",
},
{
input: "\x1b[31m\x1b[1mThis string has only ANSI escape codes\x1b[0m.",
expected: "This string has only ANSI escape codes.",
},
{
input: "This string has mixed \x1b[32m\x1b[1mred and bold\x1b[0m text.",
expected: "This string has mixed red and bold text.",
},
{
input: "\x1b[33mThis string has multiple \x1b[33myellow\x1b[0m ANSI escape codes.",
expected: "This string has multiple yellow ANSI escape codes.",
},
{
input: "This string contains ANSI escape codes \x1b[31m\x1b[1mred and bold\x1b[0m.",
expected: "This string contains ANSI escape codes red and bold.",
},
{
input: "This string contains a non-printable character \x02.",
expected: "This string contains a non-printable character \x02.",
},
{
input: "This string contains an emoji π.",
expected: "This string contains an emoji π.",
},
{
input: "\033[1;31mhello world\033[0m",
expected: "hello world",
},
{
input: "hello π world",
expected: "hello π world",
},
{
input: "\033[1;31mhello π world\033[0m",
expected: "hello π world",
},
{
input: "π",
expected: "π",
},
{
input: "\x1b[1;31mhello π world \033[0;32mπ!\033[0m",
expected: "hello π world π!",
},
}
for _, test := range tests {
output := StripANSI(test.input)
if output != test.expected {
t.Errorf("StripANSI: expected %q, but got %q", test.expected, output)
}
}
}
func TestRemoveNonPrintable(t *testing.T) {
tests := []struct {
input string
expected string
}{
{
input: "This string contains a non-printable character \x02.",
expected: "This string contains a non-printable character .",
},
{
input: "This string contains an emoji π.",
expected: "This string contains an emoji π.",
},
{
input: "This string contains a whitespace character \t and an emoji π.",
expected: "This string contains a whitespace character \t and an emoji π.",
},
{
input: "This\x00string\x1b[32mis not\x1b[0m printable.",
expected: "Thisstringis not printable.",
},
{
input: "\x0aThis is\na test.\r\n",
expected: "\nThis is\na test.\r\n",
},
{
input: "\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
expected: "\t\v\f",
},
}
for _, test := range tests {
output := RemoveNonPrintable(test.input)
if output != test.expected {
t.Errorf("RemoveNonPrintable: expected %q, but got %q for input %q", test.expected, output, test.input)
}
}
}
func TestPlaintextString(t *testing.T) {
testCases := []struct {
input string
expected string
}{
{
input: "",
expected: "",
},
{
input: "\x1b[32mThis is green.\x1b[0m",
expected: "This is green.",
},
{
input: "π Hello, π!",
expected: "π Hello, π!",
},
{
input: "This\x00string\x1b[32mis not\x1b[0m printable.",
expected: "Thisstringis not printable.",
},
{
input: " This string has leading/trailing whitespace. \n\t",
expected: " This string has leading/trailing whitespace. \n\t",
},
{
input: "\x1b[32mπ\x1b[0m \x00Hello,\x1b[32m π!\x1b[0m",
expected: "π Hello, π!",
},
}
for _, tc := range testCases {
output := PlaintextString(tc.input)
if output != tc.expected {
t.Errorf("PlaintextString(%q): expected %q, but got %q", tc.input, tc.expected, output)
}
}
}
func TestPlaintextStringLength(t *testing.T) {
tests := []struct {
input string
expected int
}{
{
input: "This is a normal string.",
expected: 24,
},
{
input: "This string contains ANSI escape codes \x1b[31m\x1b[1mred and bold\x1b[0m.",
expected: 52,
},
{
input: "This string contains non-printable characters \x00\x07.",
expected: 47,
},
{
input: "This string contains non-printable and whitespace\t characters \x00\x07.",
expected: 63,
},
{
input: "This string contains both ANSI escape codes \x1b[32mand \x1b[31mnon-printable\x1b[0m characters.",
expected: 73,
},
{
input: "This string contains emojis πππ.",
expected: 32,
},
{
input: "This string contains a mixture of ANSI escape codes \x1b[33mand emojis ππ.",
expected: 66,
},
{
input: "\033[1;31mhello world\033[0m",
expected: 11,
},
{
input: "hello π world",
expected: 13,
},
{
input: "\033[1;31mhello π world\033[0m",
expected: 13,
},
{
input: "\x1b[1;31mhello π world \033[0;32mπ!\033[0m",
expected: 16,
},
}
for _, test := range tests {
output := PlaintextStringLength(test.input)
if output != test.expected {
t.Errorf("PlaintextStringLength(%q): expected %d, but got %d", test.input, test.expected, output)
}
}
}