forked from JanDeDobbeleer/oh-my-posh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathansi_color_test.go
222 lines (187 loc) · 5.34 KB
/
ansi_color_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
package main
import (
"bytes"
"testing"
"github.com/gookit/color"
"github.com/stretchr/testify/assert"
)
const (
inputText = "This is white, <#ff5733>this is orange</>, white again"
)
func TestWriteAndRemoveText(t *testing.T) {
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
renderer.init("pwsh")
text := renderer.writeAndRemoveText("#193549", "#fff", "This is white, ", "This is white, ", inputText)
assert.Equal(t, "<#ff5733>this is orange</>, white again", text)
assert.NotContains(t, renderer.string(), "<#ff5733>")
}
func TestWriteAndRemoveTextColored(t *testing.T) {
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
renderer.init("pwsh")
text := renderer.writeAndRemoveText("#193549", "#ff5733", "this is orange", "<#ff5733>this is orange</>", inputText)
assert.Equal(t, "This is white, , white again", text)
assert.NotContains(t, renderer.string(), "<#ff5733>")
}
func TestWriteColorOverride(t *testing.T) {
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
renderer.init("pwsh")
renderer.write("#193549", "#ff5733", inputText)
assert.NotContains(t, renderer.string(), "<#ff5733>")
}
func TestWriteColorOverrideBackground(t *testing.T) {
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
text := "This is white, <,#000000>this is black</>, white again"
renderer.init("pwsh")
renderer.write("#193549", "#ff5733", text)
assert.NotContains(t, renderer.string(), "000000")
}
func TestWriteColorOverrideBackground16(t *testing.T) {
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
text := "This is default <,white> this background is changed</> default again"
renderer.init("pwsh")
renderer.write("#193549", "#ff5733", text)
assert.NotContains(t, renderer.string(), "white")
assert.NotContains(t, renderer.string(), "</>")
assert.NotContains(t, renderer.string(), "<,")
}
func TestWriteColorOverrideBoth(t *testing.T) {
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
text := "This is white, <#000000,#ffffff>this is black</>, white again"
renderer.init("pwsh")
renderer.write("#193549", "#ff5733", text)
assert.NotContains(t, renderer.string(), "ffffff")
assert.NotContains(t, renderer.string(), "000000")
}
func TestWriteColorOverrideBoth16(t *testing.T) {
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
text := "This is white, <black,white>this is black</>, white again"
renderer.init("pwsh")
renderer.write("#193549", "#ff5733", text)
assert.NotContains(t, renderer.string(), "<black,white>")
assert.NotContains(t, renderer.string(), "</>")
}
func TestWriteColorOverrideDouble(t *testing.T) {
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
text := "<#ffffff>jan</>@<#ffffff>Jans-MBP</>"
renderer.init("pwsh")
renderer.write("#193549", "#ff5733", text)
assert.NotContains(t, renderer.string(), "<#ffffff>")
assert.NotContains(t, renderer.string(), "</>")
}
func TestWriteColorTransparent(t *testing.T) {
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
renderer.init("pwsh")
text := "This is white"
renderer.writeColoredText("#193549", Transparent, text)
t.Log(renderer.string())
}
func TestWriteColorName(t *testing.T) {
// given
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
renderer.init("pwsh")
text := "This is white, <red>this is red</>, white again"
// when
renderer.write("#193549", "red", text)
// then
assert.NotContains(t, renderer.string(), "<red>")
}
func TestWriteColorInvalid(t *testing.T) {
// given
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
renderer.init("pwsh")
text := "This is white, <invalid>this is orange</>, white again"
// when
renderer.write("#193549", "invalid", text)
// then
assert.Contains(t, renderer.string(), "<invalid>")
}
func TestLenWithoutANSI(t *testing.T) {
text := "\x1b[44mhello\x1b[0m"
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
renderer.init("pwsh")
strippedLength := lenWithoutANSI(text, "zsh")
assert.Equal(t, 5, strippedLength)
}
func TestLenWithoutANSIZsh(t *testing.T) {
text := "%{\x1b[44m%}hello%{\x1b[0m%}"
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
renderer.init("zsh")
strippedLength := lenWithoutANSI(text, "zsh")
assert.Equal(t, 5, strippedLength)
}
func TestGetAnsiFromColorStringBg(t *testing.T) {
// given
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
// when
colorCode := renderer.getAnsiFromColorString("blue", true)
// then
assert.Equal(t, color.BgBlue.Code(), colorCode)
}
func TestGetAnsiFromColorStringFg(t *testing.T) {
// given
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
// when
colorCode := renderer.getAnsiFromColorString("red", false)
// then
assert.Equal(t, color.FgRed.Code(), colorCode)
}
func TestGetAnsiFromColorStringHex(t *testing.T) {
// given
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
// when
colorCode := renderer.getAnsiFromColorString("#AABBCC", false)
// then
assert.Equal(t, color.HEX("#AABBCC").Code(), colorCode)
}
func TestGetAnsiFromColorStringInvalidFg(t *testing.T) {
// given
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
// when
colorCode := renderer.getAnsiFromColorString("invalid", false)
// then
assert.Equal(t, "", colorCode)
}
func TestGetAnsiFromColorStringInvalidBg(t *testing.T) {
// given
renderer := &AnsiColor{
buffer: new(bytes.Buffer),
}
// when
colorCode := renderer.getAnsiFromColorString("invalid", true)
// then
assert.Equal(t, "", colorCode)
}