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,25 +22,24 @@ 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)
} }
} }
} }
func TestColor_strings(t *testing.T) { var colorTests = []struct {
tests := []struct {
data string data string
want color.Color want color.Color
}{ }{
{"#000", color.Black}, {"#000", color.Black},
{"#000f", color.Black}, {"#000f", color.Black},
{"#000000", color.Black}, {"#000000", color.Black},
@ -72,6 +62,7 @@ func TestColor_strings(t *testing.T) {
{"rgba(128,128,128,0.0)", color.NRGBA{0x80, 0x80, 0x80, 0x00}}, {"rgba(128,128,128,0.0)", color.NRGBA{0x80, 0x80, 0x80, 0x00}},
{"rgba(128,128,128,1.0)", color.NRGBA{0x80, 0x80, 0x80, 0xff}}, {"rgba(128,128,128,1.0)", color.NRGBA{0x80, 0x80, 0x80, 0xff}},
{"not a color", nil}, {"not a color", nil},
{"", nil},
{"#0", nil}, {"#0", nil},
{"#00", nil}, {"#00", nil},
{"#000", color.Black}, {"#000", color.Black},
@ -87,54 +78,21 @@ func TestColor_strings(t *testing.T) {
{"rgb(127.5,127.5,127.5)", nil}, {"rgb(127.5,127.5,127.5)", nil},
{"rgba(127.5,127.5,127.5,0.5)", nil}, {"rgba(127.5,127.5,127.5,0.5)", nil},
{"rgba(128,128,128)", nil}, {"rgba(128,128,128)", nil},
} }
for _, test := range tests {
func TestColor_strings(t *testing.T) {
for _, test := range colorTests {
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,11 +4,11 @@ import (
"testing" "testing"
) )
func TestUnescape(t *testing.T) { var unescapeTests = []struct {
tests := []struct {
data string data string
want string want string
}{ }{
{``, ""},
{`abc`, "abc"}, {`abc`, "abc"},
{`ab\c`, "abc"}, {`ab\c`, "abc"},
{`a\bc`, "a\bc"}, {`a\bc`, "a\bc"},
@ -26,32 +26,29 @@ func TestUnescape(t *testing.T) {
{`abc\1`, "abc\001"}, {`abc\1`, "abc\001"},
{`abc\12`, "abc\012"}, {`abc\12`, "abc\012"},
{`abc\123`, "abc\123"}, {`abc\123`, "abc\123"},
} {`abc\1234`, "abc\1234"},
for _, tt := range tests { {`abc\001`, "abc\001"},
if got := Unescape(tt.data); got != tt.want { {`abc\012`, "abc\012"},
t.Errorf("Unescape(%q) = %q; want %q", tt.data, got, tt.want) {`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) {
for _, test := range unescapeTests {
if got := Unescape(test.data); got != test.want {
t.Errorf("Unescape(%q) = %q; want %q", test.data, got, test.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 {