mirror of
https://github.com/taigrr/simplecolorpalettes.git
synced 2026-04-01 20:49:11 -07:00
Adding test coverage + bugfixes
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
87
simplecolor/simplecolors_test.go
Normal file
87
simplecolor/simplecolors_test.go
Normal 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())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user