From f21574baa688197ec2e4bbfd77c1508d6ca5ba1c Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Wed, 30 Mar 2022 11:09:26 +0100 Subject: [PATCH] Tests. --- internal/zenutil/color_test.go | 33 ++++++++++++++++++++++++++++++- internal/zenutil/unescape.go | 2 +- internal/zenutil/unescape_test.go | 19 ++++++++++++------ 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/internal/zenutil/color_test.go b/internal/zenutil/color_test.go index 52bc15c..25fc577 100644 --- a/internal/zenutil/color_test.go +++ b/internal/zenutil/color_test.go @@ -7,6 +7,36 @@ import ( "golang.org/x/image/colornames" ) +func TestColorEquals(t *testing.T) { + 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") + } +} + func TestColor_names(t *testing.T) { for _, name := range colornames.Names { c1 := colornames.Map[name] @@ -60,6 +90,7 @@ var colorTests = []struct { {"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,0.5)", color.NRGBA{0x80, 0x80, 0x80, 0x80}}, {"rgba(128,128,128,1.0)", color.NRGBA{0x80, 0x80, 0x80, 0xff}}, {"not a color", nil}, {"", nil}, @@ -80,7 +111,7 @@ var colorTests = []struct { {"rgba(128,128,128)", nil}, } -func TestColor_strings(t *testing.T) { +func TestParseColor(t *testing.T) { for _, test := range colorTests { c := ParseColor(test.data) if !ColorEquals(c, test.want) { diff --git a/internal/zenutil/unescape.go b/internal/zenutil/unescape.go index e542960..eede67f 100644 --- a/internal/zenutil/unescape.go +++ b/internal/zenutil/unescape.go @@ -18,7 +18,7 @@ func Unescape(s string) string { state := initial for _, b := range []byte(s) { switch state { - case initial: + default: switch b { case '\\': state = escape1 diff --git a/internal/zenutil/unescape_test.go b/internal/zenutil/unescape_test.go index f1e74e4..98c342f 100644 --- a/internal/zenutil/unescape_test.go +++ b/internal/zenutil/unescape_test.go @@ -10,6 +10,7 @@ var unescapeTests = []struct { }{ {``, ""}, {`abc`, "abc"}, + {`abc\`, "abc"}, {`ab\c`, "abc"}, {`a\bc`, "a\bc"}, {`abc\f`, "abc\f"}, @@ -20,6 +21,9 @@ var unescapeTests = []struct { {`a\1c`, "a\001c"}, {`a\12c`, "a\012c"}, {`a\123c`, "a\123c"}, + {`a\1\c`, "a\001c"}, + {`a\12\c`, "a\012c"}, + {`a\123\c`, "a\123c"}, {`a\1\b`, "a\001\b"}, {`a\12\b`, "a\012\b"}, {`a\123\b`, "a\123\b"}, @@ -27,13 +31,16 @@ var unescapeTests = []struct { {`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\0123`, "abc\0123"}, + {`abc\0012`, "abc\0012"}, {`abc\4567`, "abc\0567"}, + {`abc\5678`, "abc\1678"}, + {`abc\6789`, "abc\06789"}, + {`abc\7890`, "abc\007890"}, + {"ab\xcdef", "ab\xcdef"}, + {"ab\\\xcdef", "ab\xcdef"}, + {"ab\xcd\\ef", "ab\xcdef"}, + {"ab\\0\xcdef", "ab\000\xcdef"}, } func TestUnescape(t *testing.T) {