package main import ( "fmt" "github.com/stretchr/testify/assert" "testing" ) func TestGame_scoreWordsByCommonLetterLocations(t *testing.T) { type args struct { words []string } tests := []struct { name string args args want map[string]int }{ { name: "no letters in common", args: args{words: []string{"aaa", "bbb"}}, want: map[string]int{"aaa": 3, "bbb": 3}, }, { name: "one letter in common", args: args{words: []string{"aaa", "abb"}}, want: map[string]int{"aaa": 4, "abb": 4}, }, { name: "three Words with some letters in common", args: args{words: []string{"aaa", "abb", "aab"}}, want: map[string]int{"aaa": 6, "abb": 6, "aab": 7}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { game := NewGame() for _, word := range tt.args.words { game.Words[word] = &Word{Word: word} } err := game.scoreWordsByCommonLetterLocations() if err != nil { t.Errorf("Error scoring Words: %s\n", err) t.FailNow() } for word, score := range tt.want { if game.Words[word].Score != score { t.Errorf("Word %s has score %d, expected %d", word, game.Words[word].Score, score) } } }) } } func TestGame_FilterWords(t *testing.T) { type fields struct { Words map[string]*Word Length int } type args struct { guess string score int } tests := []struct { name string fields fields args args want map[string]*Word }{ { name: "no words filtered with score = 0", fields: fields{ Words: map[string]*Word{ "aaa": {Word: "aaa"}, "bbb": {Word: "bbb"}, }, }, args: args{guess: "ccc", score: 0}, want: map[string]*Word{ "aaa": {Word: "aaa"}, "bbb": {Word: "bbb"}, }, }, { name: "no words filtered with score = 1", fields: fields{ Words: map[string]*Word{ "aac": {Word: "aac"}, "bbc": {Word: "bbc"}, }, }, args: args{guess: "ccc", score: 1}, want: map[string]*Word{ "aac": {Word: "aac"}, "bbc": {Word: "bbc"}, }, }, { name: "some words filtered", fields: fields{ Words: map[string]*Word{ "aac": {Word: "aac"}, "bbc": {Word: "bbc"}, "ccc": {Word: "ccc"}, }, }, args: args{guess: "ccc", score: 1}, want: map[string]*Word{ "aac": {Word: "aac"}, "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 { t.Run(tt.name, func(t *testing.T) { g := Game{ Words: tt.fields.Words, Length: tt.fields.Length, } g.FilterWords(tt.args.guess, tt.args.score) fmt.Printf("Want: %v\n", tt.want) fmt.Printf("Got: %v\n", g.Words) for word := range tt.want { if _, ok := g.Words[word]; !ok { 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, wantedLossCount %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()") }) } }