-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcolor_test.go
206 lines (191 loc) · 5.79 KB
/
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
package config
import (
"encoding/json"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
yaml "gopkg.in/yaml.v2"
)
type testStruct struct {
C Color `json:"c" yaml:"c,omitempty"`
}
func TestMarshalYAML(t *testing.T) {
for exp, color := range map[string]testStruct{
`{}`: {},
`c: ""`: {Color{Type: ColorTypeNone}},
`c: black`: {Color{Type: ColorTypeName, Name: Black}},
`c: '#ffeedd'`: {Color{Type: ColorType24Bit, ValueR: 0xff, ValueG: 0xee, ValueB: 0xdd}},
`c: 31`: {Color{Type: ColorType8Bit, Value8: 31}},
} {
color := color
buf, err := yaml.Marshal(&color)
if err != nil {
t.Errorf("failed to marshal a color %q to yaml with error %q", "black", err)
t.FailNow()
}
exp += "\n" // NOTE: yaml will have trailing newline
act := string(buf)
if exp != act {
t.Errorf("expect that a marshaled color be %q, but %q", exp, act)
}
}
_, err := yaml.Marshal(testStruct{Color{Type: ColorType("foobar")}})
if err == nil {
t.Errorf("invalid ColorType %q accepted in marshaling", "foobar")
}
}
func TestMarshalJSON(t *testing.T) {
for exp, color := range map[string]testStruct{
`{"c":""}`: {C: Color{Type: ColorTypeNone}},
`{"c":"black"}`: {C: Color{Type: ColorTypeName, Name: Black}},
`{"c":"#ffeedd"}`: {C: Color{Type: ColorType24Bit, ValueR: 0xff, ValueG: 0xee, ValueB: 0xdd}},
`{"c":31}`: {C: Color{Type: ColorType8Bit, Value8: 31}},
} {
color := color
buf, err := json.Marshal(&color)
if err != nil {
t.Errorf("failed to marshal a color to json with error %q", err)
t.FailNow()
}
act := string(buf)
if exp != act {
t.Errorf("expect that a marshaled color be %q, but %q", exp, act)
}
}
_, err := json.Marshal(testStruct{Color{Type: ColorType("foobar")}})
if err == nil {
t.Errorf("invalid ColorType %q accepted in marshaling", "foobar")
}
}
func TestUnmarshalYAML(t *testing.T) {
const yamlTemplate = "act: %s"
for value, exp := range map[string]Color{
`""`: {Type: ColorTypeNone},
`0x1F`: {Type: ColorType8Bit, Value8: 0x1F},
`"0x1F"`: {Type: ColorType8Bit, Value8: 0x1F},
`25`: {Type: ColorType8Bit, Value8: 25},
`"red"`: {Type: ColorTypeName, Name: Red},
`red`: {Type: ColorTypeName, Name: Red},
`"#ffeedd"`: {Type: ColorType24Bit, ValueR: 0xff, ValueG: 0xee, ValueB: 0xdd},
`"rGb(255, 0xEe, 127)"`: {Type: ColorType24Bit, ValueR: 255, ValueG: 0xEE, ValueB: 127},
`rGb(255, 0XEe, 127)`: {Type: ColorType24Bit, ValueR: 255, ValueG: 0xEE, ValueB: 127},
} {
var obj struct {
Act Color `yaml:"act"`
}
err := yaml.Unmarshal([]byte(fmt.Sprintf(yamlTemplate, value)), &obj)
if err != nil {
t.Errorf("failed to unmarshal a color %q as yaml with error %q", value, err)
continue
}
if obj.Act != exp {
t.Errorf("expect that a marshaled color be %#v, but %#v", exp, obj.Act)
}
}
var obj struct {
Act *Color `yaml:"act"`
}
err := yaml.Unmarshal([]byte{}, &obj)
if err != nil {
t.Errorf("failed to unmarshal a empty color as yaml with error %q", err)
}
if obj.Act != nil {
t.Errorf("expect that a marshaled color be nil, but %#v", obj.Act)
}
for _, value := range []string{
`{}`,
`[]`,
`256`,
`rgb(256,0,0)`,
`rgb(0,256,0)`,
`rgb(0,0,256)`,
} {
var obj struct {
Act Color `yaml:"act"`
}
err := yaml.Unmarshal([]byte(fmt.Sprintf(yamlTemplate, value)), &obj)
if err == nil {
t.Errorf("invalid color %q accepted in unmarshaling as %#v", value, obj)
}
}
}
func TestUnmarshalJSON(t *testing.T) {
const jsonTemplate = `{"act":%s}`
for value, exp := range map[string]Color{
`""`: {Type: ColorTypeNone},
`"0x1F"`: {Type: ColorType8Bit, Value8: 0x1F},
`25`: {Type: ColorType8Bit, Value8: 25},
`"red"`: {Type: ColorTypeName, Name: Red},
`"#ffeedd"`: {Type: ColorType24Bit, ValueR: 0xff, ValueG: 0xee, ValueB: 0xdd},
`"rGb(255, 0xEe, 127)"`: {Type: ColorType24Bit, ValueR: 255, ValueG: 0xEE, ValueB: 127},
} {
var obj struct {
Act Color `json:"act"`
}
err := json.Unmarshal([]byte(fmt.Sprintf(jsonTemplate, value)), &obj)
if err != nil {
t.Errorf("failed to unmarshal a color %q as json with error %q", value, err)
continue
}
if obj.Act != exp {
t.Errorf("expect that a marshaled color be %#v, but %#v", exp, obj.Act)
}
}
for _, value := range []string{
`{}`,
`[]`,
`256`,
`"rgb(256,0,0)"`,
`"rgb(0,256,0)"`,
`"rgb(0,0,256)"`,
} {
var obj struct {
Act Color `json:"act"`
}
err := json.Unmarshal([]byte(fmt.Sprintf(jsonTemplate, value)), &obj)
if err == nil {
t.Errorf("invalid color %q accepted in unmarshaling as %#v", value, obj)
}
}
}
func TestB(t *testing.T) {
const esc = "\x1b["
for _, c := range []Color{
{Type: ColorTypeName, Name: Black},
{Type: ColorType8Bit, Value8: 11},
{Type: ColorType24Bit, ValueR: 33, ValueG: 22, ValueB: 11},
} {
// B() func must return Back Color (formed ESC+[+3x)
if !strings.HasPrefix(c.B().String(), esc+"4") {
t.Errorf("invalid front color: %s", strings.TrimPrefix(c.B().String(), esc))
}
}
for _, c := range []Color{
{},
{Type: ColorTypeName, Name: "invalidColor"},
} {
c := c
assert.Equal(t, "", c.B().String())
}
}
func TestF(t *testing.T) {
const esc = "\x1b["
for _, c := range []Color{
{Type: ColorTypeName, Name: Black},
{Type: ColorType8Bit, Value8: 11},
{Type: ColorType24Bit, ValueR: 33, ValueG: 22, ValueB: 11},
} {
// F() func must return Front Color (formed ESC+[+3x)
if !strings.HasPrefix(c.F().String(), esc+"3") {
t.Errorf("invalid front color: %s", strings.TrimPrefix(c.F().String(), esc))
}
}
for _, c := range []Color{
{},
{Type: ColorTypeName, Name: "invalidColor"},
} {
c := c
assert.Equal(t, "", c.F().String())
}
}