-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathcolors_test.go
86 lines (73 loc) · 1.68 KB
/
colors_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
package common
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHexThreeToSix(t *testing.T) {
result, err := hexThreeToSix("RGB")
assert.NoError(t, err)
assert.Equal(t, "RRGGBB", result)
}
func TestHexThreeToSixErr(t *testing.T) {
for _, input := range []string{"", "R", "RG", "RGBA"} {
t.Run(input, func(t *testing.T) {
_, err := hexThreeToSix(input)
assert.Error(t, err)
})
}
}
func TestHex(t *testing.T) {
testCases := []struct {
input string
red int
green int
blue int
}{
{"010203", 1, 2, 3},
{"#010203", 1, 2, 3},
{"100", 17, 0, 0},
{"#100", 17, 0, 0},
{"FFF", 255, 255, 255},
{"#FFF", 255, 255, 255},
}
for _, tc := range testCases {
red, green, blue, err := hex(tc.input)
assert.NoError(t, err)
assert.Equal(t, tc.red, red)
assert.Equal(t, tc.green, green)
assert.Equal(t, tc.blue, blue)
}
}
func TestHexErr(t *testing.T) {
for _, s := range []string{"", "ZZZ", "0ZZ", "00Z", "1000", "0102GG"} {
t.Run(s, func(t *testing.T) {
_, _, _, err := hex(s)
assert.Error(t, err)
})
}
}
func TestRandomColor(t *testing.T) {
// Get coverage for randomness
for i := 0; i < 100; i++ {
color := RandomColor()
assert.Len(t, color, 7)
assert.True(t, strings.HasPrefix(color, "#"))
}
}
func TestIsValidColor(t *testing.T) {
for _, s := range []string{"FFF", "#FFF", "F0F0F0", "#F0F0F0", "red"} {
t.Run(s, func(t *testing.T) {
result := IsValidColor(s)
assert.True(t, result)
})
}
}
func TestIsValidColorErr(t *testing.T) {
for _, s := range []string{"FF", "#FF", "F0F0", "#F0F0", "ZZZ", "clear"} {
t.Run(s, func(t *testing.T) {
result := IsValidColor(s)
assert.False(t, result)
})
}
}