123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- 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}
- }
- game.scoreWordsByCommonLetterLocations()
- 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 := g.getBestGuess()
- 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()")
- })
- }
- }
- func TestGame_printSortedScores(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.printSortedScores(), "getSortedScores()")
- })
- }
- }
|