| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package term |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"regexp" |
| 10 |
"strconv" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
type testcol struct { |
| 15 |
str stringer |
| 16 |
exp string |
| 17 |
expNoCol string |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
func ExampleColor() { |
| 22 |
g := Green("Green world") |
| 23 |
fmt.Println("Hello", g) |
| 24 |
fmt.Println(Red("Warning!")) |
| 25 |
if string(g) == "Green world" { |
| 26 |
fmt.Println(Blinking(string(g))) |
| 27 |
} |
| 28 |
var col fmt.Stringer |
| 29 |
atk := 20 |
| 30 |
switch { |
| 31 |
case atk == 0: |
| 32 |
col = Blue("5 FADE OUT") |
| 33 |
case atk < 4: |
| 34 |
col = Green("4 DOUBLE TAKE") |
| 35 |
case atk < 10: |
| 36 |
col = Yellow("3 ROUND HOUSE") |
| 37 |
case atk < 50: |
| 38 |
col = Red("2 FAST PACE") |
| 39 |
case atk >= 50: |
| 40 |
col = Blinking("1 COCKED PISTOL") |
| 41 |
} |
| 42 |
fmt.Println("Defcon: ", col) |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
func TestColorType(t *testing.T) { |
| 52 |
|
| 53 |
var res string |
| 54 |
colors := []testcol{ |
| 55 |
{Green("Green"), FgGreen, "Green"}, |
| 56 |
{Blue("Blue"), FgBlue, "Blue"}, |
| 57 |
{Red("Red"), FgRed, "Red"}, |
| 58 |
{Yellow("Yellow"), FgYellow, "Yellow"}, |
| 59 |
{Magenta("Magenta"), FgMagenta, "Magenta"}, |
| 60 |
{Cyan("Cyan"), FgCyan, "Cyan"}, |
| 61 |
{White("White"), FgWhite, "White"}, |
| 62 |
{Black("Black"), FgBlack, "Black"}, |
| 63 |
{Random("Random"), "", "Random"}, |
| 64 |
|
| 65 |
{BGreen("BGreen"), BgGreen, "BGreen"}, |
| 66 |
{BBlue("BBlue"), BgBlue, "BBlue"}, |
| 67 |
{BRed("BRed"), BgRed, "BRed"}, |
| 68 |
{BYellow("BYellow"), BgYellow, "BYellow"}, |
| 69 |
{BRandom("BRandom"), "", "BRandom"}, |
| 70 |
{BMagenta("BMagenta"), BgMagenta, "BMagenta"}, |
| 71 |
{BCyan("BCyan"), BgCyan, "BCyan"}, |
| 72 |
{BWhite("BWhite"), BgWhite, "BWhite"}, |
| 73 |
{BBlack("BBlack"), BgBlack, "BBlack"}} |
| 74 |
|
| 75 |
ColorDisable() |
| 76 |
for i := 0; i < 100; i++ { |
| 77 |
for _, col := range colors { |
| 78 |
if col.expNoCol != col.str.String() { |
| 79 |
t.Errorf("ColorDisable failed got: %q want: %q", col.str.String(), col.expNoCol) |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
ColorEnable() |
| 85 |
for i := 0; i < 100; i++ { |
| 86 |
for _, col := range colors { |
| 87 |
pattern := fmt.Sprintf("%sm.*(39|49)m", col.exp) |
| 88 |
match, err := regexp.MatchString(pattern, col.str.String()) |
| 89 |
if err != nil || !match { |
| 90 |
t.Errorf("TestColorType received wrong basecolor got: %q want: %q err: %v", col.str.String(), col.exp, err) |
| 91 |
} |
| 92 |
res += col.str.String() + " " |
| 93 |
} |
| 94 |
} |
| 95 |
t.Log(res) |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
func TestColorFmt(t *testing.T) { |
| 100 |
nr := 37 |
| 101 |
array := []string{"opel", "ascona"} |
| 102 |
colors := []testcol{ |
| 103 |
{Green(fmt.Sprintf("Green nr: %d Array: %v", nr, array)), Greenf("Green nr: %d Array: %v", nr, array), ""}, |
| 104 |
{Blue(fmt.Sprintf("Blue nr: %d Array: %v", nr, array)), Bluef("Blue nr: %d Array: %v", nr, array), ""}, |
| 105 |
{Red(fmt.Sprintf("Red nr: %d Array: %v", nr, array)), Redf("Red nr: %d Array: %v", nr, array), ""}, |
| 106 |
{Yellow(fmt.Sprintf("Yellow nr: %d Array: %v", nr, array)), Yellowf("Yellow nr: %d Array: %v", nr, array), ""}, |
| 107 |
{Magenta(fmt.Sprintf("Magenta nr: %d Array: %v", nr, array)), Magentaf("Magenta nr: %d Array: %v", nr, array), ""}, |
| 108 |
{Cyan(fmt.Sprintf("Cyan nr: %d Array: %v", nr, array)), Cyanf("Cyan nr: %d Array: %v", nr, array), ""}, |
| 109 |
{White(fmt.Sprintf("White nr: %d Array: %v", nr, array)), Whitef("White nr: %d Array: %v", nr, array), ""}, |
| 110 |
{Black(fmt.Sprintf("Black nr: %d Array: %v", nr, array)), Blackf("Black nr: %d Array: %v", nr, array), ""}, |
| 111 |
|
| 112 |
{BGreen(fmt.Sprintf("BGreen nr: %d Array: %v", nr, array)), BGreenf("BGreen nr: %d Array: %v", nr, array), ""}, |
| 113 |
{BBlue(fmt.Sprintf("BBlue nr: %d Array: %v", nr, array)), BBluef("BBlue nr: %d Array: %v", nr, array), ""}, |
| 114 |
{BRed(fmt.Sprintf("BRed nr: %d Array: %v", nr, array)), BRedf("BRed nr: %d Array: %v", nr, array), ""}, |
| 115 |
{BYellow(fmt.Sprintf("BYellow nr: %d Array: %v", nr, array)), BYellowf("BYellow nr: %d Array: %v", nr, array), ""}, |
| 116 |
{BMagenta(fmt.Sprintf("BMagenta nr: %d Array: %v", nr, array)), BMagentaf("BMagenta nr: %d Array: %v", nr, array), ""}, |
| 117 |
{BCyan(fmt.Sprintf("BCyan nr: %d Array: %v", nr, array)), BCyanf("BCyan nr: %d Array: %v", nr, array), ""}, |
| 118 |
{BWhite(fmt.Sprintf("BWhite nr: %d Array: %v", nr, array)), BWhitef("BWhite nr: %d Array: %v", nr, array), ""}, |
| 119 |
{BBlack(fmt.Sprintf("BBlack nr: %d Array: %v", nr, array)), BBlackf("BBlack nr: %d Array: %v", nr, array), ""}} |
| 120 |
|
| 121 |
for _, col := range colors { |
| 122 |
if got, want := col.str.String(), col.exp; got != want { |
| 123 |
t.Errorf("got: %v want: %v", got, want) |
| 124 |
continue |
| 125 |
} |
| 126 |
t.Log(col.exp) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
func TestModType(t *testing.T) { |
| 132 |
var res string |
| 133 |
var mods = []testcol{ |
| 134 |
{Blinking("Blinking"), Blink, "Blinking"}, |
| 135 |
{Underline("Underline"), Underln, "Underline"}, |
| 136 |
{Bold("Bold"), Bld, "Bold"}, |
| 137 |
|
| 138 |
{Italic("Italic"), Ital, "Italic"}} |
| 139 |
for _, mod := range mods { |
| 140 |
pattern := mod.exp + "m.*" + NoMode + "m" |
| 141 |
if match, err := regexp.MatchString(pattern, mod.str.String()); err != nil || !match { |
| 142 |
t.Errorf("TestModTupe received wrong modstring got: %q want: %q err: %v", mod.str.String(), mod.exp, err) |
| 143 |
} |
| 144 |
res += mod.str.String() + "\n" |
| 145 |
} |
| 146 |
ColorDisable() |
| 147 |
for _, mod := range mods { |
| 148 |
if mod.expNoCol != mod.str.String() { |
| 149 |
t.Errorf("ColorDisable failed got: %q want: %q:", mod.str.String(), mod.expNoCol) |
| 150 |
} |
| 151 |
} |
| 152 |
ColorEnable() |
| 153 |
t.Log(res) |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
func TestColor256(t *testing.T) { |
| 158 |
var rstr string |
| 159 |
for c := 0; c <= 1; c++ { |
| 160 |
for i := 0; i < 256; i++ { |
| 161 |
col, err := NewColor256(strconv.Itoa(i), strconv.Itoa(i), "") |
| 162 |
if err != nil { |
| 163 |
t.Fatal("err: ", err) |
| 164 |
} |
| 165 |
if !colorEnable { |
| 166 |
if col.String() != strconv.Itoa(i) { |
| 167 |
t.Errorf("ColorDisable failed got: %q want: %q", col.String(), strconv.Itoa(i)) |
| 168 |
} |
| 169 |
continue |
| 170 |
} |
| 171 |
pattern := F256 + ";5;" + strconv.Itoa(i) + ";" + Bg256 + ";5;m" + strconv.Itoa(i) |
| 172 |
pattern += ".*" + FgDefault + ";5;" + BgDefault + ";5;" + "m" |
| 173 |
if match, err := regexp.MatchString(pattern, col.String()); err != nil || !match { |
| 174 |
t.Errorf("TestColor256 received wrong foreground 256color string got: %q want: %d err: %v", col.String(), i, err) |
| 175 |
} |
| 176 |
rstr += string(col) + " " |
| 177 |
} |
| 178 |
rstr += "\n" |
| 179 |
for i := 0; i < 256; i++ { |
| 180 |
col, err := NewColor256(strconv.Itoa(i), "", strconv.Itoa(i)) |
| 181 |
if err != nil { |
| 182 |
t.Fatal("err: ", err) |
| 183 |
} |
| 184 |
if !colorEnable { |
| 185 |
if col.String() != strconv.Itoa(i) { |
| 186 |
t.Errorf("ColorDisable failed got: %q want: %q", col.String(), strconv.Itoa(i)) |
| 187 |
} |
| 188 |
continue |
| 189 |
} |
| 190 |
pattern := F256 + ";5;;" + Bg256 + ";5;" + strconv.Itoa(i) + "m" + strconv.Itoa(i) |
| 191 |
pattern += ".*" + FgDefault + ";5;" + BgDefault + ";5;" + "m" |
| 192 |
if match, err := regexp.MatchString(pattern, col.String()); err != nil || !match { |
| 193 |
t.Errorf("TestColor256 received wrong background 256color string got: %q want: %d err: %v", col.String(), i, err) |
| 194 |
} |
| 195 |
rstr += string(col) + " " |
| 196 |
} |
| 197 |
ColorDisable() |
| 198 |
} |
| 199 |
ColorEnable() |
| 200 |
t.Log("256 cols:\n", rstr) |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
func BenchmarkCombo(b *testing.B) { |
| 205 |
for i := 0; i < b.N; i++ { |
| 206 |
NewCombo("RedBlinking", FgRed, Blink) |
| 207 |
NewCombo("GreenUnderline", FgGreen, Underln) |
| 208 |
NewCombo("BlueItalic", FgBlue, Ital) |
| 209 |
NewCombo("RedGreen", FgRed, BgGreen) |
| 210 |
NewCombo("BlueRedUnderlineItalic", FgBlue, BgRed, Underln, Ital) |
| 211 |
NewCombo("FgBlueFgRedBgYellowBgBlue", FgBlue, FgRed, BgYellow, BgBlue) |
| 212 |
NewCombo("UnderlnUnderlnItalicItalic", Underln, Underln, Ital, Ital) |
| 213 |
NewCombo("GreenYellowUnderlineBold", BgYellow, FgGreen, Underln, Bld) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
func TestCombo(t *testing.T) { |
| 219 |
var res string |
| 220 |
combo := []testcol{ |
| 221 |
{NewCombo("RedBlinking", FgRed, Blink), "\x1b[31;5mRedBlinking\x1b[39;0m", "RedBlinking"}, |
| 222 |
{NewCombo("GreenUnderline", FgGreen, Underln), "\x1b[32;4mGreenUnderline\x1b[39;0m", "GreenUnderline"}, |
| 223 |
{NewCombo("BlueItalic", FgBlue, Ital), "\x1b[34;3mBlueItalic\x1b[39;0m", "BlueItalic"}, |
| 224 |
{NewCombo("RedGreen", FgRed, BgGreen), "\x1b[31;42mRedGreen\x1b[39;49m", "RedGreen"}, |
| 225 |
{NewCombo("BlueRedUnderlineItalic", FgBlue, BgRed, Underln, Ital), "\x1b[34;41;4;3mBlueRedUnderlineItalic\x1b[39;49;0m", "BlueRedUnderlineItalic"}, |
| 226 |
{NewCombo("FgBlueFgRedBgYellowBgBlue", FgBlue, FgRed, BgYellow, BgBlue), "\x1b[34;43mFgBlueFgRedBgYellowBgBlue\x1b[39;49m", "FgBlueFgRedBgYellowBgBlue"}, |
| 227 |
{NewCombo("UnderlnUnderlnItalicItalic", Underln, Underln, Ital, Ital), "\x1b[4;3mUnderlnUnderlnItalicItalic\x1b[0m", "UnderlnUnderlnItalicItalic"}, |
| 228 |
{NewCombo("GreenYellowUnderlineBold", BgYellow, FgGreen, Underln, Bld), "\x1b[43;32;4;1mGreenYellowUnderlineBold\x1b[39;49;0m", "GreenYellowUnderlineBold"}} |
| 229 |
for _, com := range combo { |
| 230 |
if fmt.Sprintf("%q", com.str) != fmt.Sprintf("%q", com.exp) { |
| 231 |
t.Errorf("TestCombo received wrong combination string string got: %q want: %q ", com.str, com.exp) |
| 232 |
} |
| 233 |
res += com.str.String() + "\n" |
| 234 |
} |
| 235 |
ColorDisable() |
| 236 |
combo = []testcol{ |
| 237 |
{NewCombo("RedBlinking", FgRed, Blink), "\x1b[31;5mRedBlinking\x1b[39;0m", "RedBlinking"}, |
| 238 |
{NewCombo("GreenUnderline", FgGreen, Underln), "\x1b[32;4mGreenUnderline\x1b[39;0m", "GreenUnderline"}, |
| 239 |
{NewCombo("BlueItalic", FgBlue, Ital), "\x1b[34;3mBlueItalic\x1b[39;0m", "BlueItalic"}, |
| 240 |
{NewCombo("RedGreen", FgRed, BgGreen), "\x1b[31;42mRedGreen\x1b[39;49m", "RedGreen"}, |
| 241 |
{NewCombo("BlueRedUnderlineItalic", FgBlue, BgRed, Underln, Ital), "\x1b[34;41;4;3mBlueRedUnderlineItalic\x1b[39;49;0m", "BlueRedUnderlineItalic"}, |
| 242 |
{NewCombo("FgBlueFgRedBgYellowBgBlue", FgBlue, FgRed, BgYellow, BgBlue), "\x1b[34;43mFgBlueFgRedBgYellowBgBlue\x1b[39;49m", "FgBlueFgRedBgYellowBgBlue"}, |
| 243 |
{NewCombo("UnderlnUnderlnItalicItalic", Underln, Underln, Ital, Ital), "\x1b[4;3mUnderlnUnderlnItalicItalic\x1b[0m", "UnderlnUnderlnItalicItalic"}, |
| 244 |
{NewCombo("GreenYellowUnderlineBold", BgYellow, FgGreen, Underln, Bld), "\x1b[43;32;4;1mGreenYellowUnderlineBold\x1b[39;49;0m", "GreenYellowUnderlineBold"}} |
| 245 |
for _, com := range combo { |
| 246 |
if com.expNoCol != com.str.String() { |
| 247 |
t.Errorf("ColorDisable failed got: %q want: %q", com.str.String(), com.expNoCol) |
| 248 |
} |
| 249 |
} |
| 250 |
ColorEnable() |
| 251 |
t.Log(res) |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
func TestColorRGB(t *testing.T) { |
| 258 |
var red, green, blue uint8 |
| 259 |
var rstr string |
| 260 |
for c := 0; c <= 1; c++ { |
| 261 |
|
| 262 |
for ; red <= 254; red++ { |
| 263 |
col := NewColorRGB("#", red, green, blue) |
| 264 |
if !colorEnable { |
| 265 |
if "#" != col.String() { |
| 266 |
t.Errorf("ColorDisable failed got: %q want: #", col.String()) |
| 267 |
} |
| 268 |
continue |
| 269 |
} |
| 270 |
rstr = col.String() |
| 271 |
} |
| 272 |
rstr = "\n" |
| 273 |
|
| 274 |
for ; green <= 254; green++ { |
| 275 |
col := NewColorRGB("#", red, green, blue) |
| 276 |
if !colorEnable { |
| 277 |
if "#" != col.String() { |
| 278 |
t.Errorf("ColorDisable failed got: %q want: #", col.String()) |
| 279 |
} |
| 280 |
continue |
| 281 |
} |
| 282 |
rstr += col.String() |
| 283 |
|
| 284 |
} |
| 285 |
rstr += "\n" |
| 286 |
|
| 287 |
for ; blue <= 254; blue++ { |
| 288 |
col := NewColorRGB("#", red, green, blue) |
| 289 |
if !colorEnable { |
| 290 |
if "#" != col.String() { |
| 291 |
t.Errorf("ColorDisable failed got: %q want: #", col.String()) |
| 292 |
} |
| 293 |
continue |
| 294 |
} |
| 295 |
rstr += col.String() |
| 296 |
|
| 297 |
} |
| 298 |
rstr += "\n" |
| 299 |
for grey := uint8(0); grey <= 254; grey++ { |
| 300 |
col := NewColorRGB("#", grey, grey, grey) |
| 301 |
if !colorEnable { |
| 302 |
if "#" != col.String() { |
| 303 |
t.Errorf("ColorDisable failed got: %q want: #", col.String()) |
| 304 |
} |
| 305 |
continue |
| 306 |
} |
| 307 |
rstr += col.String() |
| 308 |
} |
| 309 |
ColorDisable() |
| 310 |
} |
| 311 |
ColorEnable() |
| 312 |
t.Log(rstr) |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
func TestTheTest(t *testing.T) { |
| 317 |
t.Log(TestTerm()) |
| 318 |
t.Log("ColorDisable") |
| 319 |
ColorDisable() |
| 320 |
t.Log(TestTerm()) |
| 321 |
ColorEnable() |
| 322 |
} |