package main import ( "testing" ) func Test_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 TestWord_MatchesGuess(t *testing.T) { type fields struct { Word string Score int } type args struct { guess string numMatchingChars int } tests := []struct { name string fields fields args args want bool }{ { name: "no letters in common", fields: fields{Word: "aaa"}, args: args{guess: "bbb", numMatchingChars: 0}, want: true, }, { name: "one letter in common", fields: fields{Word: "aaa"}, args: args{guess: "abb", numMatchingChars: 1}, want: true, }, { name: "one letter in common where expecting 2", fields: fields{Word: "aaa"}, args: args{guess: "aba", numMatchingChars: 1}, want: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { w := Word{ Word: tt.fields.Word, Score: tt.fields.Score, } if got := w.MatchesGuess(tt.args.guess, tt.args.numMatchingChars); got != tt.want { t.Errorf("MatchesGuess() = %v, want %v", got, tt.want) } }) } } 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"}, }, }, } 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) if len(g.Words) != len(tt.want) { t.Errorf("Expected %d Words, got %d", len(tt.want), len(g.Words)) } for word := range tt.want { if _, ok := g.Words[word]; !ok { t.Errorf("Word %s was removed, expected it to remain", word) } } }) } }