test(colors): code coverage for palette generation

main v0.3.2
Dominique Merle 1 year ago
parent c9156cab19
commit d64725085e

@ -9,7 +9,8 @@
![LoC](https://img.shields.io/tokei/lines/github/MieuxVoter/majority-judgment-library-go?style=for-the-badge)
[![Discord Chat https://discord.gg/rAAQG9S](https://img.shields.io/discord/705322981102190593.svg?style=for-the-badge)](https://discord.gg/rAAQG9S)
A Golang module to deliberate using Majority Judgment.
A Golang module to deliberate using Majority Judgment to rank proposals/candidates.
Majority Judgment is a simple, subtle and fair voting system.
## Features
@ -42,7 +43,7 @@ package main
import (
"fmt"
"github.com/mieuxvoter/majority-judgment-library-go"
"github.com/mieuxvoter/majority-judgment-library-go/judgment"
"log"
)

@ -18,6 +18,7 @@ Should be shaken out automatically if tests are not exported, right?
## Tests
cd judgment
go test

@ -0,0 +1,6 @@
Explanation of the [score-based algorithm](./SCORE.md).
You can also read the [Contributors guide](./CONTRIBUTING.md).

@ -15,42 +15,39 @@ func hex(s string) color.Color {
return c
}
//func TestBakePalette(t *testing.T) {
// type args struct {
// toLength int
// keyColors color.Palette
// }
// tests := []struct {
// name string
// args args
// want color.Palette
// wantErr bool
// }{
// // TODO: Add test cases.
// }
// for _tt := range tests {
// t.Run(tt.namefunc(t *testing.T) {
// goterr := bakePalette(tt.args.toLengthtt.args.keyColors)
// if (err != nil) != tt.wantErr {
// t.Errorf("bakePalette() error = %vwantErr %v"errtt.wantErr)
// return
// }
// if !reflect.DeepEqual(gottt.want) {
// t.Errorf("bakePalette() got = %vwant %v"gottt.want)
// }
// })
// }
//}
func TestCreatePalette(t *testing.T) {
type args struct {
amountOfColors int
}
tests := []struct {
name string
args args
want color.Palette
name string
args args
want color.Palette
expectedAmountOfColors int
}{
{
name: "Palette of -1 is empty",
args: args{
amountOfColors: -1,
},
want: []color.Color{},
},
{
name: "Palette of 0 is empty",
args: args{
amountOfColors: 0,
},
want: []color.Color{},
},
{
name: "Palette of 1",
args: args{
amountOfColors: 1,
},
want: []color.Color{
hex("#00a249"),
},
},
{
name: "Palette of 2",
args: args{
@ -61,17 +58,62 @@ func TestCreatePalette(t *testing.T) {
hex("#00a249"),
},
},
{
name: "Palette of 3",
args: args{
amountOfColors: 3,
},
want: []color.Color{
hex("#df3222"),
hex("#fab001"),
hex("#00a249"),
},
},
{
name: "Palette of 4",
args: args{
amountOfColors: 4,
},
want: []color.Color{
hex("#df3222"),
hex("#fab001"),
hex("#7bbd3e"),
hex("#017a36"),
},
},
{
name: "Palette of 5",
args: args{
amountOfColors: 5,
},
want: []color.Color{
hex("#df3222"),
hex("#ed6f01"),
hex("#fab001"),
hex("#7bbd3e"),
hex("#00a249"),
},
},
{
name: "Palette of 6",
args: args{
amountOfColors: 6,
},
expectedAmountOfColors: 6,
},
{
name: "Palette of 7",
args: args{
amountOfColors: 7,
},
expectedAmountOfColors: 7,
},
{
name: "Palette of 32",
args: args{
amountOfColors: 32,
},
expectedAmountOfColors: 32,
want: []color.Color{
hex("#df3222"),
hex("#e3401d"),
@ -112,22 +154,24 @@ func TestCreatePalette(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
actual := CreateDefaultPalette(tt.args.amountOfColors)
print(DumpPaletteHexString(actual, ", ", "\"") + "\n")
if nil == tt.want {
return
if tt.expectedAmountOfColors > 0 {
assert.Equal(t, tt.expectedAmountOfColors, len(actual))
}
for i, expectedColor := range tt.want {
// our test values are not as precise as colorful's colors
//assert.Equal(t, expectedColor, actual[i])
// so we use equalish comparisons
p := 300.0
er, eg, eb, ea := expectedColor.RGBA()
ar, ag, ab, aa := actual[i].RGBA()
assert.InDelta(t, er, ar, p)
assert.InDelta(t, eg, ag, p)
assert.InDelta(t, eb, ab, p)
assert.InDelta(t, ea, aa, p)
if nil != tt.want {
for i, expectedColor := range tt.want {
// our test values are not as precise as colorful's colors
//assert.Equal(t, expectedColor, actual[i])
// so we use equalish comparisons
p := 300.0
er, eg, eb, ea := expectedColor.RGBA()
ar, ag, ab, aa := actual[i].RGBA()
assert.InDelta(t, er, ar, p)
assert.InDelta(t, eg, ag, p)
assert.InDelta(t, eb, ab, p)
assert.InDelta(t, ea, aa, p)
}
}
//assert.Equal(t, true, true)
})
}
}

Loading…
Cancel
Save