From d64725085eec957695ae137d80df17877d2e7100 Mon Sep 17 00:00:00 2001 From: domi41 Date: Tue, 14 Mar 2023 07:11:41 +0100 Subject: [PATCH] test(colors): code coverage for palette generation --- README.md | 5 +- docs/CONTRIBUTING.md | 1 + docs/README.md | 6 ++ judgment/colors_test.go | 132 ++++++++++++++++++++++++++-------------- 4 files changed, 98 insertions(+), 46 deletions(-) create mode 100644 docs/README.md diff --git a/README.md b/README.md index 3599060..fe55a52 100644 --- a/README.md +++ b/README.md @@ -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" ) diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index c44f256..94d5df8 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -18,6 +18,7 @@ Should be shaken out automatically if tests are not exported, right? ## Tests + cd judgment go test diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..88ca8cd --- /dev/null +++ b/docs/README.md @@ -0,0 +1,6 @@ + + + +Explanation of the [score-based algorithm](./SCORE.md). + +You can also read the [Contributors guide](./CONTRIBUTING.md). diff --git a/judgment/colors_test.go b/judgment/colors_test.go index 53e50a5..cef2166 100644 --- a/judgment/colors_test.go +++ b/judgment/colors_test.go @@ -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) }) } }