From 455fcb1fe7a11de2e44d9fb4a8407fa5be41c1c5 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 27 Jan 2019 17:03:07 +0100 Subject: [PATCH] update 'internal/color' and its tests --- internal/color/color.go | 10 ++++++- internal/color/color_test.go | 58 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/internal/color/color.go b/internal/color/color.go index 8d4ad84..3f6bca8 100644 --- a/internal/color/color.go +++ b/internal/color/color.go @@ -6,7 +6,15 @@ import ( "strconv" ) +type NameError struct { + Err error + Name string +} + +func (err *NameError) Error() string { return fmt.Sprintf("%s '%s'", err.Err, err.Name) } + var ErrInvalidHexSize = fmt.Errorf("expect a size of 3 or 6 (without the '#' prefix)") +var ErrUnknownColorName = fmt.Errorf("unknown color name") // T represents a color type T uint32 @@ -16,7 +24,7 @@ type T uint32 func FromName(t Theme, s string) (T, error) { value, ok := t[s] if !ok { - return 0, fmt.Errorf("unknown color name '%s'", s) + return 0, &NameError{ErrUnknownColorName, s} } return value, nil } diff --git a/internal/color/color_test.go b/internal/color/color_test.go index 7938f91..f4790ce 100644 --- a/internal/color/color_test.go +++ b/internal/color/color_test.go @@ -1,6 +1,7 @@ package color import ( + "io" "strconv" "testing" ) @@ -170,3 +171,60 @@ func TestFromName(t *testing.T) { } } + +func TestParse(t *testing.T) { + // fetch custom theme + theme := getTheme() + + tests := []struct { + Name string + err error + Color T + }{ + {"", io.ErrUnexpectedEOF, 0x0}, + {"unknown color", &NameError{ErrUnknownColorName, "unknown color"}, 0x0}, + + {"white", nil, 0xffffff}, + {"black", nil, 0x0}, + {"red", nil, 0xff0000}, + {"green", nil, 0x00ff00}, + {"blue", nil, 0x0000ff}, + + {"#000", nil, 0x000000}, + {"#000000", nil, 0x000000}, + + {"#f00", nil, 0xff0000}, + {"#ff0000", nil, 0xff0000}, + + {"#0f0", nil, 0x00ff00}, + {"#00ff00", nil, 0x00ff00}, + + {"#00f", nil, 0x0000ff}, + {"#0000ff", nil, 0x0000ff}, + + {"#4cb5ae", nil, 0x4cb5ae}, + } + + for i, test := range tests { + + color, err := Parse(theme, test.Name) + if err != nil { + if test.err == nil { + t.Errorf("[%d] unexpected error <%s>", i, err) + } else if err.Error() != test.err.Error() { + t.Errorf("[%d] got error <%s> expected <%s>", i, err, test.err) + } + break + } + + if test.err != nil { + t.Errorf("[%d] expected error <%s>", i, test.err) + break + } + + if color != test.Color { + t.Errorf("[%d] invalid color %x expected %x", i, color, test.Color) + } + + } +}