-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
escapes_test.go
139 lines (120 loc) · 3.14 KB
/
escapes_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
package text
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestExtractApplyTermEscapes(t *testing.T) {
cases := []struct {
Name string
Input string
Output string
TermEscapes []EscapeItem
}{
{
"A plain ascii line with escapes",
"This \x1b[31mis an\x1b[0m example.",
"This is an example.",
[]EscapeItem{{"\x1b[31m", 5}, {"\x1b[0m", 10}},
},
{
"Escape at the end",
"This \x1b[31mis an example.\x1b[0m",
"This is an example.",
[]EscapeItem{{"\x1b[31m", 5}, {"\x1b[0m", 19}},
},
{
"A plain wide line with escapes",
"一只敏捷\x1b[31m的狐狸\x1b[0m跳过了一只懒狗。",
"一只敏捷的狐狸跳过了一只懒狗。",
[]EscapeItem{{"\x1b[31m", 4}, {"\x1b[0m", 7}},
},
{
"A normal-wide mixed line with escapes",
"一只 A Quick 敏捷\x1b[31m的狐 Fox 狸\x1b[0m跳过了Dog一只懒狗。",
"一只 A Quick 敏捷的狐 Fox 狸跳过了Dog一只懒狗。",
[]EscapeItem{{"\x1b[31m", 13}, {"\x1b[0m", 21}},
},
{
"Multiple escapes at the same place",
"\x1b[1m\x1b[31mThis \x1b[1m\x1b[31mis an\x1b[0m example.\x1b[1m\x1b[31m",
"This is an example.",
[]EscapeItem{
{"\x1b[1m", 0}, {"\x1b[31m", 0},
{"\x1b[1m", 5}, {"\x1b[31m", 5},
{"\x1b[0m", 10},
{"\x1b[1m", 19}, {"\x1b[31m", 19}},
},
}
for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
cleaned, escapes := ExtractTermEscapes(tc.Input)
assert.Equal(t, tc.Output, cleaned)
assert.Equal(t, tc.TermEscapes, escapes)
augmented := ApplyTermEscapes(cleaned, escapes)
assert.Equal(t, tc.Input, augmented)
})
}
}
func TestApplyTermEscapes(t *testing.T) {
cases := []struct {
Name string
Input string
Output string
TermEscapes []EscapeItem
}{
{
"negative offset",
"This is an example.",
"\x1b[31mThis is an\x1b[0m example.",
[]EscapeItem{{"\x1b[31m", -5}, {"\x1b[0m", 10}},
},
{
"offset too far",
"This is an example.",
"This \x1b[31mis an example.\x1b[0m",
[]EscapeItem{{"\x1b[31m", 5}, {"\x1b[0m", 30}},
},
}
for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
result := ApplyTermEscapes(tc.Input, tc.TermEscapes)
assert.Equal(t, tc.Output, result)
})
}
}
func BenchmarkExtractTermEscapes(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ExtractTermEscapes("\x1b[1m\x1b[31mThis \x1b[1m\x1b[31mis an\x1b[0m example.\x1b[1m\x1b[31m")
}
}
func BenchmarkApplyTermEscapes(b *testing.B) {
cleaned, escapes := ExtractTermEscapes("\x1b[1m\x1b[31mThis \x1b[1m\x1b[31mis an\x1b[0m example.\x1b[1m\x1b[31m")
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ApplyTermEscapes(cleaned, escapes)
}
}
func TestOffsetEscapes(t *testing.T) {
cases := []struct {
input []EscapeItem
offset int
output []EscapeItem
}{
{
[]EscapeItem{{Pos: 0}, {Pos: 2}, {Pos: 20}},
5,
[]EscapeItem{{Pos: 5}, {Pos: 7}, {Pos: 25}},
},
{
[]EscapeItem{{Pos: 0}, {Pos: 2}, {Pos: 20}},
-5,
[]EscapeItem{{Pos: -5}, {Pos: -3}, {Pos: 15}},
},
}
for _, tc := range cases {
result := OffsetEscapes(tc.input, tc.offset)
assert.Equal(t, tc.output, result)
}
}