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) {
tests := []string{
"chocolate",
"lime",
"olive",
"orange",
"plum",
"salmon",
"tomato",
}
for _, test := range tests {
c1 := colornames.Map[test]
c2 := ParseColor(test)
for _, name := range colornames.Names {
c1 := colornames.Map[name]
c2 := ParseColor(name)
c3 := ParseColor(UnparseColor(c1))
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) {
t.Errorf("ParseColor(UnparseColor(%v)) = %v; want %v", c1, c3, c1)
@ -31,22 +22,21 @@ func TestColor_names(t *testing.T) {
}
func TestColor_colors(t *testing.T) {
tests := []color.Color{
colors := []color.Color{
color.Black,
color.White,
color.Opaque,
color.Transparent,
}
for _, test := range tests {
c := ParseColor(UnparseColor(test))
if !ColorEquals(c, test) {
t.Errorf("ParseColor(UnparseColor(%v)) = %v; want %v", test, c, test)
for _, color := range colors {
c := ParseColor(UnparseColor(color))
if !ColorEquals(c, color) {
t.Errorf("ParseColor(UnparseColor(%v)) = %v; want %v", color, c, color)
}
}
}
func TestColor_strings(t *testing.T) {
tests := []struct {
var colorTests = []struct {
data string
want color.Color
}{
@ -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,1.0)", color.NRGBA{0x80, 0x80, 0x80, 0xff}},
{"not a color", nil},
{"", nil},
{"#0", nil},
{"#00", nil},
{"#000", color.Black},
@ -88,53 +79,20 @@ func TestColor_strings(t *testing.T) {
{"rgba(127.5,127.5,127.5,0.5)", nil},
{"rgba(128,128,128)", nil},
}
for _, test := range tests {
func TestColor_strings(t *testing.T) {
for _, test := range colorTests {
c := ParseColor(test.data)
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) {
f.Add("#000")
f.Add("#000f")
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 _, test := range colorTests {
f.Add(test.data)
}
for _, name := range colornames.Names {
f.Add(name)
}

View file

@ -3,7 +3,7 @@ package zenutil
// Unescape is internal.
func Unescape(s string) string {
// 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 (
initial = iota

View file

@ -4,11 +4,11 @@ import (
"testing"
)
func TestUnescape(t *testing.T) {
tests := []struct {
var unescapeTests = []struct {
data string
want string
}{
{``, ""},
{`abc`, "abc"},
{`ab\c`, "abc"},
{`a\bc`, "a\bc"},
@ -26,32 +26,29 @@ func TestUnescape(t *testing.T) {
{`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"},
}
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 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) {
f.Add(`abc`)
f.Add(`ab\c`)
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`)
for _, test := range unescapeTests {
f.Add(test.data)
}
f.Fuzz(func(t *testing.T, e string) {
u := Unescape(e)
switch {