package main import ( "github.com/stretchr/testify/assert" "testing" ) func TestSimulation_SimulateOneInitialWord(t *testing.T) { type fields struct { Words []string } type args struct { initialWord string } tests := []struct { name string words []string fields fields args args wantedLossCount int wantedMaxRounds int wantedTotalRounds int }{ { name: "simple game with no losses", words: []string{"aaa", "aab", "abb"}, args: args{ initialWord: "aaa", }, wantedLossCount: 0, wantedMaxRounds: 2, wantedTotalRounds: 5, }, { name: "simple failed game", words: []string{"aaa", "aab", "aac", "aad", "aae"}, args: args{ initialWord: "aaa", }, wantedLossCount: 1, wantedMaxRounds: 5, wantedTotalRounds: 15, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { game := &Game{} game.Words = make(map[string]*Word) for _, word := range tt.words { game.Words[word] = &Word{Word: word} } s := Simulation{ Game: game, } lossCount, maxRounds, totalRounds := s.SimulateOneInitialWord(game, tt.args.initialWord) assert.Equalf(t, tt.wantedLossCount, lossCount, "loss count for %v => %v", game, tt.args.initialWord) assert.Equalf(t, tt.wantedMaxRounds, maxRounds, "max rounds for %v => %v)", game, tt.args.initialWord) assert.Equalf(t, tt.wantedTotalRounds, totalRounds, "total rounds for %v => %v)", game, tt.args.initialWord) }) } } func TestSimulation_SimulateOneGame(t *testing.T) { type fields struct { BestGuess string BestGuessRounds int SuccessCount int FailCount int TotalRounds int } type args struct { initialWord string answer string } tests := []struct { name string words []string fields fields args args won bool numRounds int }{ { name: "initial word is answer", words: []string{"aaa", "aab", "abb"}, args: args{ initialWord: "aaa", answer: "aaa", }, won: true, numRounds: 1, }, { name: "one guess to get answer", words: []string{"aaa", "aab", "abb"}, args: args{ initialWord: "aaa", answer: "aab", }, won: true, numRounds: 2, }, { name: "one guess to get answer", words: []string{"aaa", "aab", "aac", "aad", "aae"}, args: args{ initialWord: "aaa", answer: "aae", }, won: false, numRounds: 5, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { game := &Game{} game.Words = make(map[string]*Word) for _, word := range tt.words { game.Words[word] = &Word{Word: word} } s := Simulation{ Game: game, } got, got1 := s.SimulateOneGame(game, tt.args.initialWord, tt.args.answer) assert.Equalf(t, tt.won, got, "SimulateOneGame(%v, %v, %v)", game, tt.args.initialWord, tt.args.answer) assert.Equalf(t, tt.numRounds, got1, "SimulateOneGame(%v, %v, %v)", game, tt.args.initialWord, tt.args.answer) }) } }