From 2faa38265e2f613fd467f685e5d07c990fd9af0e Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Mon, 3 Oct 2022 01:25:42 -0700 Subject: [PATCH] Adding test coverage + bugfixes --- simplecolor/simplecolors.go | 12 +++-- simplecolor/simplecolors_test.go | 87 ++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 simplecolor/simplecolors_test.go diff --git a/simplecolor/simplecolors.go b/simplecolor/simplecolors.go index dcbbb91..25f7838 100644 --- a/simplecolor/simplecolors.go +++ b/simplecolor/simplecolors.go @@ -8,7 +8,7 @@ import ( "strings" ) -const TotalHexColorspace = 0xffffff +const TotalHexColorspace = 0xffffff + 1 type ( SimpleColor int @@ -112,8 +112,14 @@ func FromHexString(h string) SimpleColor { case 6: break case 3: - stretchedHex := hexRunes[0] + hexRunes[0] + hexRunes[1] + hexRunes[1] + hexRunes[2] + hexRunes[2] - h = string(stretchedHex) + stretchedHex := fmt.Sprintf("%s%s%s%s%s%s", + string(hexRunes[0]), + string(hexRunes[0]), + string(hexRunes[1]), + string(hexRunes[1]), + string(hexRunes[2]), + string(hexRunes[2])) + h = stretchedHex default: return FromHexString("#66042d") } diff --git a/simplecolor/simplecolors_test.go b/simplecolor/simplecolors_test.go new file mode 100644 index 0000000..9c8e56a --- /dev/null +++ b/simplecolor/simplecolors_test.go @@ -0,0 +1,87 @@ +package simplecolor + +import ( + "testing" +) + +func TestCreateColor(t *testing.T) { + tc := []struct { + ID string + Value int + Result int + }{ + {ID: "white", Value: TotalHexColorspace - 1, Result: TotalHexColorspace - 1}, + {ID: "WraparoundBlack", Value: TotalHexColorspace, Result: 0}, + {ID: "black", Value: 0, Result: 0}, + } + for _, c := range tc { + t.Run(c.ID, func(t *testing.T) { + color := New(c.Value) + if int(color) != c.Result { + t.Errorf("Expected Value %d, but got %d", c.Result, color) + } + }) + } +} + +func TestToHex(t *testing.T) { + tc := []struct { + ID string + R uint32 + G uint32 + B uint32 + Result string + }{ + {ID: "red", R: 0xFF, G: 0x00, B: 0x00, Result: "#FF0000"}, + {ID: "black", R: 0, G: 0, B: 0, Result: "#000000"}, + } + for _, c := range tc { + t.Run(c.ID, func(t *testing.T) { + color := FromRGBA(c.R, c.G, c.B, 0) + if color.ToHex() != c.Result { + t.Errorf("Expected Value %s, but got %s", c.Result, color.ToHex()) + } + }) + } +} + +func TestRGB(t *testing.T) { + tc := []struct { + ID string + R uint32 + G uint32 + B uint32 + Result int + }{ + {ID: "white", R: 255, G: 255, B: 255, Result: TotalHexColorspace - 1}, + {ID: "black", R: 0, G: 0, B: 0, Result: 0}, + } + for _, c := range tc { + t.Run(c.ID, func(t *testing.T) { + color := FromRGBA(c.R, c.G, c.B, 0) + if int(color) != c.Result { + t.Errorf("Expected Value %d, but got %d", c.Result, color) + } + }) + } +} + +func TestFromHex(t *testing.T) { + tc := []struct { + ID string + Input string + Result string + Error error + }{ + {ID: "redshort", Input: "#F00", Result: "#FF0000", Error: nil}, + {ID: "redlong", Input: "#F00000", Result: "#F00000", Error: nil}, + } + for _, c := range tc { + t.Run(c.ID, func(t *testing.T) { + color := FromHexString(c.Input) + if color.ToHex() != c.Result { + t.Errorf("Expected Value %s, but got %s", c.Result, color.ToHex()) + } + }) + } +}