Skip to content

Commit

Permalink
test(colors): code coverage for palette generation
Browse files Browse the repository at this point in the history
  • Loading branch information
domi41 committed Mar 14, 2023
1 parent c9156ca commit d647250
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 46 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,7 +43,7 @@ package main

import (
"fmt"
"github.com/mieuxvoter/majority-judgment-library-go"
"github.com/mieuxvoter/majority-judgment-library-go/judgment"
"log"
)

Expand Down
1 change: 1 addition & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Should be shaken out automatically if tests are not exported, right?

## Tests

cd judgment
go test


Expand Down
6 changes: 6 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@



Explanation of the [score-based algorithm](./SCORE.md).

You can also read the [Contributors guide](./CONTRIBUTING.md).
132 changes: 88 additions & 44 deletions judgment/colors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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"),
Expand Down Expand Up @@ -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)
})
}
}

0 comments on commit d647250

Please sign in to comment.