1 package color
2
3 import (
4 "reflect"
5 "testing"
6 )
7
8 func TestFormat(t *testing.T) {
9 want := "\x1b[102;95mHello World\x1b[0m"
10 got := Format(BgHiGreen, FgHiMagenta, "Hello World")
11
12 if got != want {
13 t.Errorf("Expecting %s, got '%s'\n", want, got)
14 }
15 }
16
17 func TestMalformedFormat(t *testing.T) {
18 want := "\x1b[102mHello World(EXTRA color.Attribute=95)\x1b[0m"
19 got := Format(BgHiGreen, "Hello World", FgHiMagenta)
20
21 if got != want {
22 t.Errorf("Expecting %s, got '%s'\n", want, got)
23 }
24 }
25
26 func TestMalformedSliceFormat(t *testing.T) {
27 want := "\x1b[102mHello World(EXTRA color.Attribute=[95 41])\x1b[0m"
28 got := Format(BgHiGreen, "Hello World", []Attribute{FgHiMagenta, BgRed})
29
30 if got != want {
31 t.Errorf("Expecting %s, got '%s'\n", want, got)
32 }
33 }
34
35 func TestFormatSlice(t *testing.T) {
36 format := []Attribute{BgHiGreen, FgHiMagenta}
37
38 want := "\x1b[102;95mHello World\x1b[0m"
39 got := Format(format, "Hello World")
40
41 if got != want {
42 t.Errorf("Expecting %s, got '%s'\n", want, got)
43 }
44 }
45
46 func TestEmpty(t *testing.T) {
47 want := ""
48 got := Format()
49
50 if got != want {
51 t.Errorf("Expecting %s, got '%s'\n", want, got)
52 }
53 }
54
55 func TestEmptyColorString(t *testing.T) {
56 want := ""
57 got := Format(BgBlack)
58
59 if got != want {
60 t.Errorf("Expecting %s, got '%s'\n", want, got)
61 }
62 }
63
64 func TestNoFormat(t *testing.T) {
65 want := "\x1b[mHello World\x1b[0m"
66 got := Format("Hello World")
67
68 if got != want {
69 t.Errorf("Expecting %s, got '%s'\n", want, got)
70 }
71 }
72
73 func TestFormatStartingWithNumber(t *testing.T) {
74 want := "\x1b[102;95m100 forks\x1b[0m"
75 number := 100
76
77 if reflect.TypeOf(number).String() != "int" {
78 t.Errorf("Must be integer; not a similar like Attribute")
79 }
80
81 got := Format(BgHiGreen, FgHiMagenta, number, " forks")
82
83 if got != want {
84 t.Errorf("Expecting %s, got '%s'\n", want, got)
85 }
86 }
87
88 func TestFormatCtrlChar(t *testing.T) {
89 if want, got := "\x1b[ma%b\x1b[0m", Format("a%b"); got != want {
90 t.Errorf(`expected Format(a%%b) to be %q, got %q instead`, want, got)
91 }
92 if want, got := "\\x1b[34;46ma%b\\x1b[0m", Escape(Format(FgBlue, BgCyan, "a%b")); got != want {
93 t.Errorf(`expected escaped formatted a%%b to be %q, got %q instead`, want, got)
94 }
95 }
96
97 func TestEscape(t *testing.T) {
98 unescaped := "\x1b[32mGreen"
99 escaped := "\\x1b[32mGreen"
100 got := Escape(unescaped)
101
102 if got != escaped {
103 t.Errorf("Expecting %s, got '%s'\n", escaped, got)
104 }
105 }
106
107 func TestStripAttributes(t *testing.T) {
108 want := "this is a regular string"
109 got := StripAttributes(FgCyan, []Attribute{FgBlack}, "this is a regular string")
110
111 if got != want {
112 t.Errorf("StripAttributes(input) = %s, wanted %s", got, want)
113 }
114 }
115
116 func TestStripAttributesEmpty(t *testing.T) {
117 if got := StripAttributes(); got != "" {
118 t.Errorf("StripAttributes() should work")
119 }
120 }
121
122 func TestStripAttributesFirstParam(t *testing.T) {
123 want := "foo (EXTRA color.Attribute=32)"
124 got := StripAttributes("foo ", FgGreen)
125
126 if got != want {
127 t.Errorf(`expected StripAttributes = %v, got %v instead`, want, got)
128 }
129 }
130
131 func TestStripAttributesSame(t *testing.T) {
132 want := "this is a regular string"
133 got := StripAttributes(want)
134
135 if got != want {
136 t.Errorf("StripAttributes(%s) = %s, wanted %s", want, got, want)
137 }
138 }
139
140 func TestStripAttributesWithExtraColorAttribute(t *testing.T) {
141 want := "this is a regular string (EXTRA color.Attribute=91) with an invalid color Attribute field"
142 got := StripAttributes(BgCyan, []Attribute{FgBlack}, "this is a regular string ", FgHiRed, " with an invalid color Attribute field")
143
144 if got != want {
145 t.Errorf("StripAttributes(input) = %s, wanted %s", got, want)
146 }
147 }
148
View as plain text