Skip to content

Commit

Permalink
Testing (#16)
Browse files Browse the repository at this point in the history
* Create gradset_test.go

* Create gradx_test.go

* Update gradx_test.go

* Update gradx_test.go

* Update grad_test.go

* Update grad_test.go

* Update gradx_test.go

* Create gradfn_test.go

* Update gradset_test.go
  • Loading branch information
mazznoer authored Jul 29, 2020
1 parent d43eb28 commit b2aebdb
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 1 deletion.
78 changes: 77 additions & 1 deletion grad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,83 @@ import (
"testing"
)

func TestBasic(t *testing.T) {
func TestBasic1(t *testing.T) {
// Single color
grad, err := NewGradient().
Colors(color.RGBA{0, 255, 0, 255}).
Build()
testStr(t, grad.At(0).Hex(), "#00ff00")
testStr(t, grad.At(1).Hex(), "#00ff00")

// Domain's length != colors's length
grad, err = NewGradient().
HexColors("#777", "#fff", "#ccc").
Domain(0, 1).
Build()
if err == nil {
t.Errorf("It should error")
}
if grad != nil {
t.Errorf("grad should nil")
}

// Wrong domain
grad, err = NewGradient().
HexColors("#777", "#fff", "#ccc", "#222").
Domain(0, 0.71, 0.70, 1).
Build()
if err == nil {
t.Errorf("It should error")
}
if grad != nil {
t.Errorf("grad should nil")
}

// Invalid hex color
grad, err = NewGradient().
HexColors("#ffg", "#333", "#bbb").
Build()
testStr(t, grad.At(0).Hex(), "#333333")
testStr(t, grad.At(1).Hex(), "#bbbbbb")

// Blend mode
grad, err = NewGradient().
HexColors("#333", "#bbb").
Mode(HCL).
Build()
testStr(t, grad.At(0).Hex(), "#333333")
testStr(t, grad.At(1).Hex(), "#bbbbbb")

grad, err = NewGradient().
HexColors("#333", "#bbb").
Mode(HSV).
Build()
testStr(t, grad.At(0).Hex(), "#333333")
testStr(t, grad.At(1).Hex(), "#bbbbbb")

grad, err = NewGradient().
HexColors("#333", "#bbb").
Mode(LAB).
Build()
testStr(t, grad.At(0).Hex(), "#333333")
testStr(t, grad.At(1).Hex(), "#bbbbbb")

grad, err = NewGradient().
HexColors("#333", "#bbb").
Mode(LRGB).
Build()
testStr(t, grad.At(0).Hex(), "#333333")
testStr(t, grad.At(1).Hex(), "#bbbbbb")

grad, err = NewGradient().
HexColors("#333", "#bbb").
Mode(LUV).
Build()
testStr(t, grad.At(0).Hex(), "#333333")
testStr(t, grad.At(1).Hex(), "#bbbbbb")
}

func TestBasic2(t *testing.T) {
grad, _ := NewGradient().Build()
colors := grad.Colors(2)

Expand Down
23 changes: 23 additions & 0 deletions gradfn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package colorgrad

import (
"testing"
)

func TestFn(t *testing.T) {
testFn(t, Cividis())
testFn(t, Sinebow())
testFn(t, Turbo())

testFn(t, CubehelixDefault())
testFn(t, Cool())
testFn(t, Warm())
testFn(t, Rainbow())
}

func testFn(t *testing.T, grad Gradient) {
n := len(grad.Colors(9))
if n != 9 {
t.Errorf("Expected 9, got %v", n)
}
}
36 changes: 36 additions & 0 deletions gradset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package colorgrad

import (
"testing"
)

func TestPreset(t *testing.T) {
testGrad(t, Inferno(), "#000004", "#fcffa4")
testGrad(t, Magma(), "#000004", "#fcfdbf")
testGrad(t, Plasma(), "#0d0887", "#f0f921")
testGrad(t, Spectral(), "#9e0142", "#5e4fa2")
testGrad(t, Viridis(), "#440154", "#fee825")

testGrad(t, Blues(), "#f7fbff", "#08306b")
testGrad(t, Greens(), "#f7fcf5", "#00441b")
testGrad(t, Greys(), "#ffffff", "#000000")
testGrad(t, Oranges(), "#fff5eb", "#7f2704")
testGrad(t, Purples(), "#fcfbfd", "#3f007d")
testGrad(t, Reds(), "#fff5f0", "#67000d")
}

func testGrad(t *testing.T, grad Gradient, start, end string) {
if grad == nil {
t.Errorf("Grad is nil")
}

a := grad.At(0).Hex()
if a != start {
t.Errorf("Expected %v, got %v", start, a)
}

b := grad.At(1).Hex()
if b != end {
t.Errorf("Expected %v, got %v", end, b)
}
}
35 changes: 35 additions & 0 deletions gradx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package colorgrad

import (
"image/color"
"math"
"testing"
)

func TestX(t *testing.T) {
grad, _ := NewGradient().Build()
grad2 := Classes(grad, 7)
testStr(t, grad2.At(0).Hex(), "#000000")
testStr(t, grad2.At(1).Hex(), "#ffffff")

testStr(t, grad2.At(math.NaN()).Hex(), "#000000")
testStr(t, grad2.At(-0.01).Hex(), "#000000")
testStr(t, grad2.At(1.01).Hex(), "#ffffff")

colors := grad2.Colors(7)
if len(colors) != 7 {
t.Errorf("Expected 7, got %v", len(colors))
}
testStr(t, colors[0].Hex(), "#000000")
testStr(t, colors[6].Hex(), "#ffffff")

colors1 := grad.Colors(5) // []colorful.Color
colors2 := IntoColors(colors1) // []color.Color

for i, c2 := range colors2 {
var c1 color.Color = colors1[i]
if c1 != c2 {
t.Errorf("%v != %v", c1, c2)
}
}
}

0 comments on commit b2aebdb

Please sign in to comment.