zenity/internal/zenutil/color_test.go

139 lines
3.6 KiB
Go
Raw Normal View History

2021-03-27 20:23:28 -04:00
package zenutil
import (
"image/color"
"testing"
"golang.org/x/image/colornames"
)
2022-03-30 06:09:26 -04:00
func TestColorEquals(t *testing.T) {
2022-12-15 09:29:56 -05:00
t.Parallel()
2022-03-30 06:09:26 -04:00
if ColorEquals(nil, nil) == false {
t.Error("ColorEquals(nil, nil) == false")
}
if ColorEquals(nil, color.Black) == true {
t.Error("ColorEquals(nil, color.Black) == true")
}
if ColorEquals(color.Black, nil) == true {
t.Error("ColorEquals(color.Black, nil) == true")
}
if ColorEquals(color.Black, color.Black) == false {
t.Error("ColorEquals(color.Black, color.Black) == false")
}
if ColorEquals(color.Black, color.White) == true {
t.Error("ColorEquals(color.Black, color.White) == true")
}
if ColorEquals(color.Black, colornames.Red) == true {
t.Error("ColorEquals(color.Black, colornames.Red) == true")
}
if ColorEquals(color.Black, colornames.Green) == true {
t.Error("ColorEquals(color.Black, colornames.Green) == true")
}
if ColorEquals(color.Black, colornames.Blue) == true {
t.Error("ColorEquals(color.Black, colornames.Blue) == true")
}
if ColorEquals(color.Black, color.Transparent) == true {
t.Error("ColorEquals(color.Black, color.Transparent) == true")
}
}
2021-06-18 11:16:04 -04:00
func TestColor_names(t *testing.T) {
2022-12-15 09:29:56 -05:00
t.Parallel()
2022-03-28 05:50:33 -04:00
for _, name := range colornames.Names {
c1 := colornames.Map[name]
c2 := ParseColor(name)
2021-06-18 11:16:04 -04:00
c3 := ParseColor(UnparseColor(c1))
2021-07-07 08:24:46 -04:00
if !ColorEquals(c1, c2) {
2022-03-28 05:50:33 -04:00
t.Errorf("ParseColor(%q) = %v; want %v", name, c2, c1)
2021-03-27 20:23:28 -04:00
}
2021-07-07 08:24:46 -04:00
if !ColorEquals(c1, c3) {
t.Errorf("ParseColor(UnparseColor(%v)) = %v; want %v", c1, c3, c1)
2021-06-18 11:16:04 -04:00
}
}
}
func TestColor_colors(t *testing.T) {
2022-12-15 09:29:56 -05:00
t.Parallel()
2022-03-28 05:50:33 -04:00
colors := []color.Color{
2021-06-18 11:16:04 -04:00
color.Black,
color.White,
color.Opaque,
color.Transparent,
}
2022-03-28 05:50:33 -04:00
for _, color := range colors {
c := ParseColor(UnparseColor(color))
if !ColorEquals(c, color) {
t.Errorf("ParseColor(UnparseColor(%v)) = %v; want %v", color, c, color)
2021-06-18 11:16:04 -04:00
}
}
}
2022-03-28 05:50:33 -04:00
var colorTests = []struct {
data string
want color.Color
}{
{"#000", color.Black},
{"#000f", color.Black},
{"#000000", color.Black},
{"#000000ff", color.Black},
{"#fff", color.White},
{"#ffff", color.White},
{"#ffffff", color.White},
{"#ffffffff", color.White},
{"#FFF", color.Opaque},
{"#FFFF", color.Opaque},
{"#FFFFFF", color.Opaque},
{"#FFFFFFFF", color.Opaque},
{"#0000", color.Transparent},
{"#00000000", color.Transparent},
{"#8888", color.NRGBA{0x88, 0x88, 0x88, 0x88}},
{"#80808080", color.NRGBA{0x80, 0x80, 0x80, 0x80}},
{"rgb(128,128,128)", color.NRGBA{0x80, 0x80, 0x80, 0xff}},
{"rgba(128,128,128,0)", color.NRGBA{0x80, 0x80, 0x80, 0x00}},
{"rgba(128,128,128,1)", color.NRGBA{0x80, 0x80, 0x80, 0xff}},
{"rgba(128,128,128,0.0)", color.NRGBA{0x80, 0x80, 0x80, 0x00}},
2022-03-30 06:09:26 -04:00
{"rgba(128,128,128,0.5)", color.NRGBA{0x80, 0x80, 0x80, 0x80}},
2022-03-28 05:50:33 -04:00
{"rgba(128,128,128,1.0)", color.NRGBA{0x80, 0x80, 0x80, 0xff}},
{"not a color", nil},
{"", nil},
{"#0", nil},
{"#00", nil},
{"#000", color.Black},
{"#0000", color.Transparent},
{"#00000", nil},
{"#000000", color.Black},
{"#0000000", nil},
{"#00000000", color.Transparent},
{"#000000000", nil},
{"rgb(-1,-1,-1)", nil},
{"rgb(256,256,256)", nil},
{"rgb(128,128,128,0.5)", nil},
{"rgb(127.5,127.5,127.5)", nil},
{"rgba(127.5,127.5,127.5,0.5)", nil},
{"rgba(128,128,128)", nil},
}
2022-03-30 06:09:26 -04:00
func TestParseColor(t *testing.T) {
2022-12-15 09:29:56 -05:00
t.Parallel()
2022-03-28 05:50:33 -04:00
for _, test := range colorTests {
2021-06-18 11:16:04 -04:00
c := ParseColor(test.data)
2021-07-07 08:24:46 -04:00
if !ColorEquals(c, test.want) {
2022-03-28 05:50:33 -04:00
t.Errorf("ParseColor(%q) = %v; want %v", test.data, c, test.want)
2021-03-27 20:23:28 -04:00
}
}
}
2022-03-26 11:36:53 -04:00
func FuzzParseColor(f *testing.F) {
2022-03-28 05:50:33 -04:00
for _, test := range colorTests {
f.Add(test.data)
}
2022-03-26 11:36:53 -04:00
for _, name := range colornames.Names {
f.Add(name)
}
f.Fuzz(func(t *testing.T, s string) {
ParseColor(s)
})
}