-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathansitags_test.go
More file actions
339 lines (251 loc) · 7.67 KB
/
Copy pathansitags_test.go
File metadata and controls
339 lines (251 loc) · 7.67 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package ansitags
import (
"bufio"
"bytes"
"io/ioutil"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
type TestCase struct {
Input string `yaml:"input"`
Expected string `yaml:"expected"`
}
func TestParseTagOpenerMismatches(t *testing.T) {
testTable := loadTestFile("testdata/ansitags_test_tag_openers.yaml")
for name, testCase := range testTable {
t.Run(name, func(t *testing.T) {
output := Parse(testCase.Input)
assert.Equal(t, testCase.Expected, output)
//fmt.Println(output)
//bytes, _ := json.Marshal(output)
//fmt.Println(string(bytes))
})
}
}
func TestParseAliases(t *testing.T) {
testTable := loadTestFile("testdata/ansitags_test_aliases.yaml")
LoadAliases("aliases.yaml")
for name, testCase := range testTable {
t.Run(name, func(t *testing.T) {
output := Parse(testCase.Input)
assert.Equal(t, testCase.Expected, output)
//fmt.Println(output)
//bytes, _ := json.Marshal(output)
//fmt.Println(string(bytes))
})
}
}
func TestParseClear(t *testing.T) {
testTable := loadTestFile("testdata/ansitags_test_clear.yaml")
for name, testCase := range testTable {
t.Run(name, func(t *testing.T) {
output := Parse(testCase.Input)
assert.Equal(t, testCase.Expected, output)
//fmt.Println(output)
//bytes, _ := json.Marshal(output)
//fmt.Println(string(bytes))
})
}
}
func TestParsePosition(t *testing.T) {
testTable := loadTestFile("testdata/ansitags_test_position.yaml")
for name, testCase := range testTable {
t.Run(name, func(t *testing.T) {
output := Parse(testCase.Input)
assert.Equal(t, testCase.Expected, output)
//fmt.Println(output)
//bytes, _ := json.Marshal(output)
//fmt.Println(string(bytes))
})
}
}
func TestParseColor(t *testing.T) {
testTable := loadTestFile("testdata/ansitags_test_color.yaml")
for name, testCase := range testTable {
t.Run(name, func(t *testing.T) {
output := Parse(testCase.Input)
assert.Equal(t, testCase.Expected, output)
//fmt.Println(output)
//bytes, _ := json.Marshal(output)
//fmt.Println(string(bytes))
})
}
}
func TestHtmlMode(t *testing.T) {
testTable := loadTestFile("testdata/ansitags_test_html.yaml")
for name, testCase := range testTable {
t.Run(name, func(t *testing.T) {
output := Parse(testCase.Input, HTML)
assert.Equal(t, testCase.Expected, output)
//fmt.Println(output)
//bytes, _ := json.Marshal(output)
//fmt.Println(string(bytes))
})
}
}
func TestParseStripped(t *testing.T) {
testTable := loadTestFile("testdata/ansitags_test_strip.yaml")
for name, testCase := range testTable {
t.Run(name, func(t *testing.T) {
output := Parse(testCase.Input, StripTags)
assert.Equal(t, testCase.Expected, output)
//fmt.Println(output)
//bytes, _ := json.Marshal(output)
//fmt.Println(string(bytes))
})
}
}
func TestParseMono(t *testing.T) {
testTable := loadTestFile("testdata/ansitags_test_monochrome.yaml")
for name, testCase := range testTable {
t.Run(name, func(t *testing.T) {
output := Parse(testCase.Input, Monochrome)
assert.Equal(t, testCase.Expected, output)
//fmt.Println(output)
//bytes, _ := json.Marshal(output)
//fmt.Println(string(bytes))
})
}
}
func TestParseLarge(t *testing.T) {
testString := loadRawFile("testdata/ansitags_test_streaming.yaml")
_ = Parse(testString)
}
func loadTestFile(filename string) map[string]TestCase {
data := make(map[string]TestCase, 100)
if yfile, err := ioutil.ReadFile(filename); err != nil {
panic(err)
} else {
if err := yaml.Unmarshal(yfile, &data); err != nil {
panic(err)
}
}
return data
}
func loadRawFile(filename string) string {
yfile, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
return string(yfile)
}
func TestSetAliasValidDefaultGroup(t *testing.T) {
alias := "testAlias256"
value := 123
// Ensure clean state
delete(colorAliases, alias)
// Set alias in default (color256) group
if err := SetAlias(alias, value); err != nil {
t.Fatalf("expected no error, got %v", err)
}
if got := colorAliases[alias]; got != value {
t.Errorf("colorAliases[%q] = %d; want %d", alias, got, value)
}
}
func TestSetAliasInvalidValue(t *testing.T) {
alias := "invalidAlias"
invalidValue := -5
// Expect error for out-of-range value
if err := SetAlias(alias, invalidValue); err == nil {
t.Errorf("expected error for invalid value %d, got none", invalidValue)
}
}
func TestSetAliasesValidDefaultGroup(t *testing.T) {
aliases := map[string]int{
"multi256_1": 200,
"multi256_2": 201,
}
delete(colorAliases, "multi256_1")
delete(colorAliases, "multi256_2")
// Bulk set in default (color256) group
if err := SetAliases(aliases); err != nil {
t.Fatalf("expected no error, got %v", err)
}
for alias, expected := range aliases {
if got := colorAliases[alias]; got != expected {
t.Errorf("colorAliases[%q] = %d; want %d", alias, got, expected)
}
}
}
func TestGetAliases(t *testing.T) {
// Default aliases should all be present as int values
aliases := GetAliases()
for name, num := range defaultColorAliases {
got, ok := aliases[name]
if !ok {
t.Errorf("GetAliases() missing default alias %q", name)
continue
}
if got != num {
t.Errorf("GetAliases()[%q] = %v; want %d", name, got, num)
}
}
// Adding an alias via SetAlias should be reflected
if err := SetAlias("getaliases-test", 42); err != nil {
t.Fatalf("SetAlias returned unexpected error: %v", err)
}
aliases = GetAliases()
if got, ok := aliases["getaliases-test"]; !ok || got != 42 {
t.Errorf("GetAliases()[%q] = %v, ok=%v; want %d, true", "getaliases-test", got, ok, 42)
}
// GetAliases must return a copy — mutations must not affect internal state
aliases["getaliases-test"] = 999
if got := GetAliases()["getaliases-test"]; got != 42 {
t.Errorf("mutating returned map affected internal state: got %v, want %d", got, 42)
}
}
func TestSetAliasesInvalidValue(t *testing.T) {
aliases := map[string]int{
"badAlias1": -1,
"badAlias2": 300,
}
delete(colorAliases, "badAlias1")
delete(colorAliases, "badAlias2")
// Expect error and no partial application
if err := SetAliases(aliases); err == nil {
t.Fatalf("expected error for invalid alias value, got none")
}
}
// cpu: Apple M3 Max
// Name # Run Avg Runtime Bytes Allocated # of Allocat
// BenchmarkParseStreaming-14 39277 29422 ns/op 27360 B/op 491 allocs/o
func BenchmarkParseStreaming(b *testing.B) {
testStr := "This is text"
for i := 0; i < 5; i++ { // heavily nested tags
testStr = testStr + "<ansi fg=black bg=\"white\">" + testStr + "</ansi>"
}
reader := strings.NewReader(testStr)
input := bufio.NewReader(reader)
var outputBuffer bytes.Buffer
output := bufio.NewWriter(&outputBuffer)
for n := 0; n < b.N; n++ {
ParseStreaming(input, output)
reader.Reset(testStr)
}
}
// cpu: Apple M3 Max
// Name # Run Avg Runtime Bytes Allocated # of Allocat
// BenchmarkParse-14 37892 30006 ns/op 33892 B/op 497 allocs/op
func BenchmarkParse(b *testing.B) {
testStr := "This is text"
for i := 0; i < 5; i++ { // heavily nested tags
testStr = testStr + "<ansi fg=black bg=\"white\">" + testStr + "</ansi>"
}
for n := 0; n < b.N; n++ {
Parse(testStr)
}
}
// cpu: Apple M3 Max
// Name # Run Avg Runtime Bytes Allocated # of Allocat
// BenchmarkParseHTML-14 40748 28125 ns/op 32295 B/op 455 allocs/op
func BenchmarkParseHTML(b *testing.B) {
testStr := "This is text"
for i := 0; i < 5; i++ { // heavily nested tags
testStr = testStr + "<ansi fg=black bg=\"white\">" + testStr + "</ansi>"
}
for n := 0; n < b.N; n++ {
Parse(testStr, HTML)
}
}