Adding test coverage + bugfixes

This commit is contained in:
2022-10-03 01:25:42 -07:00
parent 2d452d8c20
commit 2faa38265e
2 changed files with 96 additions and 3 deletions

View File

@@ -8,7 +8,7 @@ import (
"strings" "strings"
) )
const TotalHexColorspace = 0xffffff const TotalHexColorspace = 0xffffff + 1
type ( type (
SimpleColor int SimpleColor int
@@ -112,8 +112,14 @@ func FromHexString(h string) SimpleColor {
case 6: case 6:
break break
case 3: case 3:
stretchedHex := hexRunes[0] + hexRunes[0] + hexRunes[1] + hexRunes[1] + hexRunes[2] + hexRunes[2] stretchedHex := fmt.Sprintf("%s%s%s%s%s%s",
h = string(stretchedHex) string(hexRunes[0]),
string(hexRunes[0]),
string(hexRunes[1]),
string(hexRunes[1]),
string(hexRunes[2]),
string(hexRunes[2]))
h = stretchedHex
default: default:
return FromHexString("#66042d") return FromHexString("#66042d")
} }

View File

@@ -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())
}
})
}
}