|
@@ -1,6 +1,8 @@
|
|
package main
|
|
package main
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "fmt"
|
|
|
|
+ "github.com/stretchr/testify/assert"
|
|
"testing"
|
|
"testing"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -156,6 +158,33 @@ func TestGame_FilterWords(t *testing.T) {
|
|
"bbc": {Word: "bbc"},
|
|
"bbc": {Word: "bbc"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
|
|
+ {
|
|
|
|
+ name: "real world case",
|
|
|
|
+ fields: fields{
|
|
|
|
+ Words: map[string]*Word{
|
|
|
|
+ "pleads": {Word: "pleads", Score: 13},
|
|
|
|
+ "crimes": {Word: "crimes", Score: 16},
|
|
|
|
+ "fierce": {Word: "fierce", Score: 12},
|
|
|
|
+ "wagons": {Word: "wagons", Score: 14},
|
|
|
|
+ "shiner": {Word: "shiner", Score: 14},
|
|
|
|
+ "wastes": {Word: "wastes", Score: 20},
|
|
|
|
+ "tables": {Word: "tables", Score: 17},
|
|
|
|
+ "ripped": {Word: "ripped", Score: 14},
|
|
|
|
+ "silver": {Word: "silver", Score: 16},
|
|
|
|
+ "insane": {Word: "insane", Score: 13},
|
|
|
|
+ "visage": {Word: "visage", Score: 15},
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ args: args{guess: "wastes", score: 1},
|
|
|
|
+ want: map[string]*Word{
|
|
|
|
+ "pleads": {Word: "pleads", Score: 13},
|
|
|
|
+ "insane": {Word: "insane", Score: 13},
|
|
|
|
+ "shiner": {Word: "shiner", Score: 14},
|
|
|
|
+ "visage": {Word: "visage", Score: 15},
|
|
|
|
+ "silver": {Word: "silver", Score: 16},
|
|
|
|
+ "ripped": {Word: "ripped", Score: 14},
|
|
|
|
+ },
|
|
|
|
+ },
|
|
}
|
|
}
|
|
for _, tt := range tests {
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
@@ -164,15 +193,113 @@ func TestGame_FilterWords(t *testing.T) {
|
|
Length: tt.fields.Length,
|
|
Length: tt.fields.Length,
|
|
}
|
|
}
|
|
g.FilterWords(tt.args.guess, tt.args.score)
|
|
g.FilterWords(tt.args.guess, tt.args.score)
|
|
- if len(g.Words) != len(tt.want) {
|
|
|
|
- t.Errorf("Expected %d Words, got %d", len(tt.want), len(g.Words))
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ fmt.Printf("Want: %v\n", tt.want)
|
|
|
|
+ fmt.Printf("Got: %v\n", g.Words)
|
|
|
|
+
|
|
for word := range tt.want {
|
|
for word := range tt.want {
|
|
if _, ok := g.Words[word]; !ok {
|
|
if _, ok := g.Words[word]; !ok {
|
|
t.Errorf("Word %s was removed, expected it to remain", word)
|
|
t.Errorf("Word %s was removed, expected it to remain", word)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ assert.Equal(t, len(tt.want), len(g.Words), "Wrong number of matches")
|
|
|
|
+
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func TestGame_getBestGuess(t *testing.T) {
|
|
|
|
+ type fields struct {
|
|
|
|
+ Words map[string]*Word
|
|
|
|
+ Length int
|
|
|
|
+ }
|
|
|
|
+ tests := []struct {
|
|
|
|
+ name string
|
|
|
|
+ fields fields
|
|
|
|
+ want string
|
|
|
|
+ wantErr bool
|
|
|
|
+ }{
|
|
|
|
+ {
|
|
|
|
+ name: "simple case",
|
|
|
|
+ fields: fields{
|
|
|
|
+ Words: map[string]*Word{
|
|
|
|
+ "aaa": {Word: "aaa", Score: 3},
|
|
|
|
+ "aab": {Word: "aab", Score: 2},
|
|
|
|
+ "caa": {Word: "caa", Score: 2},
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ want: "aaa",
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ name: "real-world case",
|
|
|
|
+ fields: fields{
|
|
|
|
+ Words: map[string]*Word{
|
|
|
|
+ "pleads": {Word: "pleads", Score: 13},
|
|
|
|
+ "crimes": {Word: "crimes", Score: 16},
|
|
|
|
+ "fierce": {Word: "fierce", Score: 12},
|
|
|
|
+ "wagons": {Word: "wagons", Score: 14},
|
|
|
|
+ "shiner": {Word: "shiner", Score: 14},
|
|
|
|
+ "wastes": {Word: "wastes", Score: 20},
|
|
|
|
+ "tables": {Word: "tables", Score: 17},
|
|
|
|
+ "ripped": {Word: "ripped", Score: 14},
|
|
|
|
+ "silver": {Word: "silver", Score: 16},
|
|
|
|
+ "visage": {Word: "visage", Score: 15},
|
|
|
|
+ "insane": {Word: "insane", Score: 13},
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ want: "wastes",
|
|
|
|
+ },
|
|
|
|
+ }
|
|
|
|
+ for _, tt := range tests {
|
|
|
|
+ t.Run(tt.name, func(t *testing.T) {
|
|
|
|
+ g := Game{
|
|
|
|
+ Words: tt.fields.Words,
|
|
|
|
+ Length: tt.fields.Length,
|
|
|
|
+ }
|
|
|
|
+ got, err := g.getBestGuess()
|
|
|
|
+ if (err != nil) != tt.wantErr {
|
|
|
|
+ t.Errorf("getBestGuess() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if got != tt.want {
|
|
|
|
+ t.Errorf("getBestGuess() got = %v, want %v", got, tt.want)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func TestGame_getSortedScores(t *testing.T) {
|
|
|
|
+ type fields struct {
|
|
|
|
+ Words map[string]*Word
|
|
|
|
+ }
|
|
|
|
+ tests := []struct {
|
|
|
|
+ name string
|
|
|
|
+ fields fields
|
|
|
|
+ want []string
|
|
|
|
+ }{
|
|
|
|
+ {
|
|
|
|
+ name: "sort words by scores",
|
|
|
|
+ fields: fields{
|
|
|
|
+ Words: map[string]*Word{
|
|
|
|
+ "pleads": {Word: "pleads", Score: 13},
|
|
|
|
+ "crimes": {Word: "crimes", Score: 16},
|
|
|
|
+ "fierce": {Word: "fierce", Score: 12},
|
|
|
|
+ "shiner": {Word: "shiner", Score: 14},
|
|
|
|
+ "wastes": {Word: "wastes", Score: 20},
|
|
|
|
+ "tables": {Word: "tables", Score: 17},
|
|
|
|
+ "visage": {Word: "visage", Score: 15},
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ want: []string{"wastes", "tables", "crimes", "visage", "shiner", "pleads", "fierce"},
|
|
|
|
+ },
|
|
|
|
+ }
|
|
|
|
+ for _, tt := range tests {
|
|
|
|
+ t.Run(tt.name, func(t *testing.T) {
|
|
|
|
+ g := Game{
|
|
|
|
+ Words: tt.fields.Words,
|
|
|
|
+ }
|
|
|
|
+ assert.Equalf(t, tt.want, g.getSortedScores(), "getSortedScores()")
|
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|