aboutsummaryrefslogtreecommitdiff
path: root/lib/parse/ansi_test.go
blob: f916412bbee636df83f8e1cda12c700965c570d5 (plain)
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
package parse_test

import (
	"os"
	"testing"

	"git.sr.ht/~rjarry/aerc/lib/parse"
	"github.com/stretchr/testify/assert"
)

func TestParser(t *testing.T) {
	//nolint:errcheck // we'll fail the test if this fails
	_ = os.Setenv("COLORTERM", "truecolor")
	tests := []struct {
		name           string
		input          string
		expectedString string
		expectedLen    int
	}{
		{
			name:           "no style",
			input:          "hello, world",
			expectedString: "hello, world",
			expectedLen:    12,
		},
		{
			name:           "bold",
			input:          "\x1b[1mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[1mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "dim",
			input:          "\x1b[2mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[2mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "bold and dim",
			input:          "\x1b[1;2mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[1m\x1b[2mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "italic",
			input:          "\x1b[3mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[3mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "underline",
			input:          "\x1b[4mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[4mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "blink",
			input:          "\x1b[5mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[5mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "fast blink",
			input:          "\x1b[6mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[5mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "reverse",
			input:          "\x1b[7mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[7mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "hidden",
			input:          "\x1b[8mhello, world",
			expectedString: "hello, world",
			expectedLen:    12,
		},
		{
			name:           "strikethrough",
			input:          "\x1b[9mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[9mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "bold hello, normal world",
			input:          "\x1b[1mhello, \x1b[21mworld",
			expectedString: "\x1b(B\x1b[m\x1b[1mhello, \x1b(B\x1b[mworld\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "bold hello, normal world v2",
			input:          "\x1b[1mhello, \x1b[mworld",
			expectedString: "\x1b(B\x1b[m\x1b[1mhello, \x1b(B\x1b[mworld\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "8 bit color: foreground",
			input:          "\x1b[30mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[30mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "8 bit color: background",
			input:          "\x1b[41mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[41mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "8 bit color: foreground and background",
			input:          "\x1b[31;41mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[31;41mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "16 bit color: foreground",
			input:          "\x1b[90mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[90mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "16 bit color: background",
			input:          "\x1b[101mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[101mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "16 bit color: foreground and background",
			input:          "\x1b[91;101mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[91;101mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "256 color: foreground",
			input:          "\x1b[38;5;2mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[32mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "256 color: foreground",
			input:          "\x1b[38;5;132mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[38;5;132mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "256 color: background",
			input:          "\x1b[48;5;132mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[48;5;132mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "256 color: foreground and background",
			input:          "\x1b[38;5;20;48;5;20mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[38;5;20;48;5;20mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "256 color: background",
			input:          "\x1b[48;5;2mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[42mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "true color: foreground",
			input:          "\x1b[38;2;0;0;0mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[38;2;0;0;0mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "true color: foreground with color space",
			input:          "\x1b[38;2;;0;0;0mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[38;2;0;0;0mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "true color: foreground with color space and colons",
			input:          "\x1b[38:2::0:0:0mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[38;2;0;0;0mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "true color: background",
			input:          "\x1b[48;2;0;0;0mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[48;2;0;0;0mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "true color: background with color space",
			input:          "\x1b[48;2;;0;0;0mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[48;2;0;0;0mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
		{
			name:           "true color: foreground and background",
			input:          "\x1b[38;2;200;200;200;48;2;0;0;0mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[38;2;200;200;200;48;2;0;0;0mhello, world\x1b(B\x1b[m",
			expectedLen:    12,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			buf := parse.ParseANSI(test.input)
			assert.Equal(t, test.expectedString, buf.String())
			assert.Equal(t, test.expectedLen, buf.Len())
		})
	}
}

func TestTruncate(t *testing.T) {
	tests := []struct {
		name           string
		input          string
		expectedString string
	}{
		{
			name:           "no style, truncate at 5",
			input:          "hello, world",
			expectedString: "hello",
		},
		{
			name:           "bold, truncate at 5",
			input:          "\x1b[1mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[1mhello\x1b(B\x1b[m",
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			buf := parse.ParseANSI(test.input)
			assert.Equal(t, test.expectedString, buf.Truncate(5, 0))
		})
	}
}

func TestTruncateHead(t *testing.T) {
	tests := []struct {
		name           string
		input          string
		expectedString string
		expectedLen    int
	}{
		{
			name:           "no style, truncate head at 5",
			input:          "hello, world",
			expectedString: "world",
		},
		{
			name:           "bold, truncate head at 5",
			input:          "\x1b[1mhello, world",
			expectedString: "\x1b(B\x1b[m\x1b[1mworld\x1b(B\x1b[m",
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			buf := parse.ParseANSI(test.input)
			assert.Equal(t, test.expectedString, buf.TruncateHead(5, 0))
		})
	}
}