Improve tests.

This commit is contained in:
Nuno Cruces 2022-03-28 10:50:33 +01:00
parent 42dec67764
commit 238d216de3
3 changed files with 98 additions and 143 deletions

View file

@ -8,21 +8,12 @@ import (
) )
func TestColor_names(t *testing.T) { func TestColor_names(t *testing.T) {
tests := []string{ for _, name := range colornames.Names {
"chocolate", c1 := colornames.Map[name]
"lime", c2 := ParseColor(name)
"olive",
"orange",
"plum",
"salmon",
"tomato",
}
for _, test := range tests {
c1 := colornames.Map[test]
c2 := ParseColor(test)
c3 := ParseColor(UnparseColor(c1)) c3 := ParseColor(UnparseColor(c1))
if !ColorEquals(c1, c2) { if !ColorEquals(c1, c2) {
t.Errorf("ParseColor(%s) = %v; want %v", test, c2, c1) t.Errorf("ParseColor(%q) = %v; want %v", name, c2, c1)
} }
if !ColorEquals(c1, c3) { if !ColorEquals(c1, c3) {
t.Errorf("ParseColor(UnparseColor(%v)) = %v; want %v", c1, c3, c1) t.Errorf("ParseColor(UnparseColor(%v)) = %v; want %v", c1, c3, c1)
@ -31,110 +22,77 @@ func TestColor_names(t *testing.T) {
} }
func TestColor_colors(t *testing.T) { func TestColor_colors(t *testing.T) {
tests := []color.Color{ colors := []color.Color{
color.Black, color.Black,
color.White, color.White,
color.Opaque, color.Opaque,
color.Transparent, color.Transparent,
} }
for _, test := range tests { for _, color := range colors {
c := ParseColor(UnparseColor(test)) c := ParseColor(UnparseColor(color))
if !ColorEquals(c, test) { if !ColorEquals(c, color) {
t.Errorf("ParseColor(UnparseColor(%v)) = %v; want %v", test, c, test) t.Errorf("ParseColor(UnparseColor(%v)) = %v; want %v", color, c, color)
} }
} }
} }
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}},
{"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},
}
func TestColor_strings(t *testing.T) { func TestColor_strings(t *testing.T) {
tests := []struct { for _, test := range colorTests {
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}},
{"rgba(128,128,128,1.0)", color.NRGBA{0x80, 0x80, 0x80, 0xff}},
{"not a color", 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},
}
for _, test := range tests {
c := ParseColor(test.data) c := ParseColor(test.data)
if !ColorEquals(c, test.want) { if !ColorEquals(c, test.want) {
t.Errorf("ParseColor(%s) = %v; want %v", test.data, c, test.want) t.Errorf("ParseColor(%q) = %v; want %v", test.data, c, test.want)
} }
} }
} }
func FuzzParseColor(f *testing.F) { func FuzzParseColor(f *testing.F) {
f.Add("#000") for _, test := range colorTests {
f.Add("#000f") f.Add(test.data)
f.Add("#000000") }
f.Add("#000000ff")
f.Add("#fff")
f.Add("#ffff")
f.Add("#ffffff")
f.Add("#ffffffff")
f.Add("#FFF")
f.Add("#FFFF")
f.Add("#FFFFFF")
f.Add("#FFFFFFFF")
f.Add("#0")
f.Add("#00")
f.Add("#000")
f.Add("#0000")
f.Add("#00000")
f.Add("#000000")
f.Add("#0000000")
f.Add("#00000000")
f.Add("#000000000")
f.Add("#8888")
f.Add("#80808080")
f.Add("rgb(-1,-1,-1)")
f.Add("rgb(128,128,128)")
f.Add("rgb(256,256,256)")
f.Add("rgb(128,128,128,0.5)")
f.Add("rgb(127.5,127.5,127.5)")
f.Add("rgba(128,128,128)")
f.Add("rgba(128,128,128,0)")
f.Add("rgba(128,128,128,1)")
f.Add("rgba(128,128,128,0.0)")
f.Add("rgba(128,128,128,0.5)")
f.Add("rgba(128,128,128,1.0)")
f.Add("rgba(127.5,127.5,127.5,0.5)")
f.Add("not a color")
f.Add("")
for _, name := range colornames.Names { for _, name := range colornames.Names {
f.Add(name) f.Add(name)
} }

View file

@ -3,7 +3,7 @@ package zenutil
// Unescape is internal. // Unescape is internal.
func Unescape(s string) string { func Unescape(s string) string {
// Apply rules described in: // Apply rules described in:
// https://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html#g-strescape // https://docs.gtk.org/glib/func.strescape.html
const ( const (
initial = iota initial = iota

View file

@ -4,54 +4,51 @@ import (
"testing" "testing"
) )
var unescapeTests = []struct {
data string
want string
}{
{``, ""},
{`abc`, "abc"},
{`ab\c`, "abc"},
{`a\bc`, "a\bc"},
{`abc\f`, "abc\f"},
{`abc\n`, "abc\n"},
{`abc\r`, "abc\r"},
{`abc\t`, "abc\t"},
{`abc\v`, "abc\v"},
{`a\1c`, "a\001c"},
{`a\12c`, "a\012c"},
{`a\123c`, "a\123c"},
{`a\1\b`, "a\001\b"},
{`a\12\b`, "a\012\b"},
{`a\123\b`, "a\123\b"},
{`abc\1`, "abc\001"},
{`abc\12`, "abc\012"},
{`abc\123`, "abc\123"},
{`abc\1234`, "abc\1234"},
{`abc\001`, "abc\001"},
{`abc\012`, "abc\012"},
{`abc\123`, "abc\123"},
{`abc\4`, "abc\004"},
{`abc\45`, "abc\045"},
{`abc\456`, "abc\056"},
{`abc\4567`, "abc\0567"},
}
func TestUnescape(t *testing.T) { func TestUnescape(t *testing.T) {
tests := []struct { for _, test := range unescapeTests {
data string if got := Unescape(test.data); got != test.want {
want string t.Errorf("Unescape(%q) = %q; want %q", test.data, got, test.want)
}{
{`abc`, "abc"},
{`ab\c`, "abc"},
{`a\bc`, "a\bc"},
{`abc\f`, "abc\f"},
{`abc\n`, "abc\n"},
{`abc\r`, "abc\r"},
{`abc\t`, "abc\t"},
{`abc\v`, "abc\v"},
{`a\1c`, "a\001c"},
{`a\12c`, "a\012c"},
{`a\123c`, "a\123c"},
{`a\1\b`, "a\001\b"},
{`a\12\b`, "a\012\b"},
{`a\123\b`, "a\123\b"},
{`abc\1`, "abc\001"},
{`abc\12`, "abc\012"},
{`abc\123`, "abc\123"},
}
for _, tt := range tests {
if got := Unescape(tt.data); got != tt.want {
t.Errorf("Unescape(%q) = %q; want %q", tt.data, got, tt.want)
} }
} }
} }
func FuzzUnescape(f *testing.F) { func FuzzUnescape(f *testing.F) {
f.Add(`abc`) for _, test := range unescapeTests {
f.Add(`ab\c`) f.Add(test.data)
f.Add(`a\bc`) }
f.Add(`abc\f`)
f.Add(`abc\n`)
f.Add(`abc\r`)
f.Add(`abc\t`)
f.Add(`abc\v`)
f.Add(`a\1c`)
f.Add(`a\12c`)
f.Add(`a\123c`)
f.Add(`a\1\b`)
f.Add(`a\12\b`)
f.Add(`a\123\b`)
f.Add(`abc\1`)
f.Add(`abc\12`)
f.Add(`abc\123`)
f.Fuzz(func(t *testing.T, e string) { f.Fuzz(func(t *testing.T, e string) {
u := Unescape(e) u := Unescape(e)
switch { switch {