-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
173 lines (147 loc) · 4.94 KB
/
parse_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
package color_test
import (
"fmt"
"math/rand"
"testing"
"github.com/shelepuginivan/color"
"github.com/stretchr/testify/assert"
)
func TestParseNamed(t *testing.T) {
cases := []struct {
input string
expected *color.RGB
isErr bool
}{
{"maroon", &color.RGB{128, 0, 0}, false},
{"red", &color.RGB{255, 0, 0}, false},
{"white", &color.RGB{255, 255, 255}, false},
{"black", &color.RGB{0, 0, 0}, false},
{"ReD", &color.RGB{255, 0, 0}, false},
{"notacolor", nil, true},
{"", nil, true},
}
for _, c := range cases {
actual, err := color.ParseNamed(c.input)
if c.isErr {
assert.Nil(t, c.expected)
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, c.expected, actual)
}
}
}
func TestParseHex(t *testing.T) {
cases := []struct {
hex string
expected *color.RGB
err bool
}{
{hex: "000", expected: &color.RGB{0, 0, 0}, err: false},
{hex: "#000", expected: &color.RGB{0, 0, 0}, err: false},
{hex: "000000", expected: &color.RGB{0, 0, 0}, err: false},
{hex: "#000000", expected: &color.RGB{0, 0, 0}, err: false},
{hex: "fff", expected: &color.RGB{255, 255, 255}, err: false},
{hex: "#fff", expected: &color.RGB{255, 255, 255}, err: false},
{hex: "ffffff", expected: &color.RGB{255, 255, 255}, err: false},
{hex: "#ffffff", expected: &color.RGB{255, 255, 255}, err: false},
{hex: "ggggggg", err: true},
{hex: "a", err: true},
{hex: "ab", err: true},
{hex: "ffv", err: true},
{hex: "not a valid hexadecimal string", err: true},
{hex: "#######", err: true},
}
for _, c := range cases {
actual, err := color.ParseHex(c.hex)
if c.err {
assert.Nil(t, actual)
assert.Error(t, err)
} else {
assert.Equal(t, c.expected, actual)
assert.NoError(t, err)
}
}
// Random tests.
for range 1000 {
r := rand.Intn(256)
g := rand.Intn(256)
b := rand.Intn(256)
hex := fmt.Sprintf("%02x%02x%02x", r, g, b)
expected := &color.RGB{uint8(r), uint8(g), uint8(b)}
actual, err := color.ParseHex(hex)
assert.EqualExportedValues(t, expected, actual)
assert.Nil(t, err)
hex = fmt.Sprintf("#%02x%02x%02x", r, g, b)
actual, err = color.ParseHex(hex)
assert.EqualExportedValues(t, expected, actual)
assert.Nil(t, err)
}
}
func TestParseFunc(t *testing.T) {
cases := []struct {
fnstring string
expected color.Color
err bool
}{
{"", nil, true},
{"erkeropgjerpgjerg", nil, true},
{"cmyk(0%, 18%, 39%, 5%)", &color.CMYK{0, 18, 39, 5}, false},
{"cmyk(61% 6% 0% 35%)", &color.CMYK{61, 6, 0, 35}, false},
{"cmyk(0.5, 0.5, 0.5, 0.5)", &color.CMYK{50, 50, 50, 50}, false},
{"cmyk(.65 .34 .32 .11)", &color.CMYK{65, 34, 32, 11}, false},
{"cmyk(.03 .33 .24 none)", &color.CMYK{3, 33, 24, 0}, false},
{"cmyk(not a valid arg)", nil, true},
{"cmyk(65% 19% 78%)", nil, true},
{"cmyk(34% 21% 98% 67% 45%)", nil, true},
{"hsl(112, 92%, 43%)", &color.HSL{112, 92, 43}, false},
{"hsl(221 87% 53%)", &color.HSL{221, 87, 53}, false},
{"hsl(288deg .98, 82%)", &color.HSL{288, 98, 82}, false},
{"hsl(0.3turn 60% 45%)", &color.HSL{108, 60, 45}, false},
{"hsl(0.22689rad 97% 59%)", &color.HSL{13, 97, 59}, false},
{"hsl(a, b, c)", nil, true},
{"hsl(67 .83)", nil, true},
{"hsl(310 79% 66% .17)", nil, true},
{"hsv(343, 80%, 90%)", &color.HSV{343, 80, 90}, false},
{"hsv(209 .72 .83)", &color.HSV{209, 72, 83}, false},
{"hsv(132deg 92% 77%)", &color.HSV{132, 92, 77}, false},
{"hsv(0.767turn, 99%, 71%)", &color.HSV{276, 99, 71}, false},
{"hsv(1.9199rad 9% 49%)", &color.HSV{110, 9, 49}, false},
{"hsv(a, b, c)", nil, true},
{"hsv(67 .83)", nil, true},
{"hsv(310 79% 66% .17)", nil, true},
{"lab(53.27, 80.109, 67.220)", &color.Lab{53.27, 80.109, 67.220}, false},
{"lab(87.73 -86.1846 83.181)", &color.Lab{87.73, -86.1846, 83.181}, false},
{"lab(100.0, 0.526%, -1.04%)", &color.Lab{100.0, 0.00526, -0.0104}, false},
{"lab(string string string)", nil, true},
{"lab(100.0, 86%)", nil, true},
{"lab(1 1 1 1)", nil, true},
{"lch(8.991815706465342, 3.7396951758251333, 82)", &color.Lch{8.991815706465342, 3.7396951758251333, 82}, false},
{"lch(3.234 1.75672 none)", &color.Lch{3.234, 1.75672, 0}, false},
{"lch(again invalid args)", nil, true},
{"lch(3.141592654 2.718281828)", nil, true},
{"lch(0 0 0 0)", nil, true},
{"rgb(255 255 255)", &color.RGB{255, 255, 255}, false},
{"rgb(none none none)", &color.RGB{0, 0, 0}, false},
{"rgb(r g b)", nil, true},
{"rgb(30 40)", nil, true},
{"rgb(11 22 33 44)", nil, true},
{"xyz(1.0985, 1.0000, 0.3558)", color.A, false},
{"xyz(0.9807, 1.0000, 1.1822)", color.C, false},
{"xyz(0.9505 1 1.0888)", color.D65, false},
{"xyz(33% 99% 101%)", &color.XYZ{0.33, 0.99, 1.01}, false},
{"xyz(x y z)", nil, true},
{"xyz(30% 20%)", nil, true},
{"xyz(.11 .12 .13 .14)", nil, true},
}
for _, c := range cases {
actual, err := color.ParseFunc(c.fnstring)
if c.err {
assert.Nil(t, actual)
assert.Error(t, err)
} else {
assert.Equal(t, c.expected, actual)
assert.NoError(t, err)
}
}
}