Kaynağa Gözat

test: simplify simulator test definitions

Alex White 5 ay önce
ebeveyn
işleme
d4872c0203
1 değiştirilmiş dosya ile 42 ekleme ve 89 silme
  1. 42 89
      simulator_test.go

+ 42 - 89
simulator_test.go

@@ -7,19 +7,14 @@ import (
 
 func TestSimulation_SimulateOneInitialWord(t *testing.T) {
 	type fields struct {
-		Game            *Game
-		BestGuess       string
-		BestGuessRounds int
-		SuccessCount    int
-		FailCount       int
-		TotalRounds     int
+		Words []string
 	}
 	type args struct {
-		game        *Game
 		initialWord string
 	}
 	tests := []struct {
 		name              string
+		words             []string
 		fields            fields
 		args              args
 		wantedLossCount   int
@@ -27,24 +22,9 @@ func TestSimulation_SimulateOneInitialWord(t *testing.T) {
 		wantedTotalRounds int
 	}{
 		{
-			name: "simple game with no losses",
-			fields: fields{
-				Game: &Game{
-					Words: map[string]*Word{
-						"aaa": {Word: "aaa"},
-						"aab": {Word: "aab"},
-						"abb": {Word: "abb"},
-					},
-				},
-			},
+			name:  "simple game with no losses",
+			words: []string{"aaa", "aab", "abb"},
 			args: args{
-				game: &Game{
-					Words: map[string]*Word{
-						"aaa": {Word: "aaa"},
-						"aab": {Word: "aab"},
-						"abb": {Word: "abb"},
-					},
-				},
 				initialWord: "aaa",
 			},
 			wantedLossCount:   0,
@@ -52,28 +32,9 @@ func TestSimulation_SimulateOneInitialWord(t *testing.T) {
 			wantedTotalRounds: 5,
 		},
 		{
-			name: "simple failed game",
-			fields: fields{
-				Game: &Game{
-					Words: map[string]*Word{
-						"aaa": {Word: "aaa"},
-						"aab": {Word: "aab"},
-						"aac": {Word: "aac"},
-						"aad": {Word: "aad"},
-						"aae": {Word: "aae"},
-					},
-				},
-			},
+			name:  "simple failed game",
+			words: []string{"aaa", "aab", "aac", "aad", "aae"},
 			args: args{
-				game: &Game{
-					Words: map[string]*Word{
-						"aaa": {Word: "aaa"},
-						"aab": {Word: "aab"},
-						"aac": {Word: "aac"},
-						"aad": {Word: "aad"},
-						"aae": {Word: "aae"},
-					},
-				},
 				initialWord: "aaa",
 			},
 			wantedLossCount:   1,
@@ -83,25 +44,27 @@ func TestSimulation_SimulateOneInitialWord(t *testing.T) {
 	}
 	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:            tt.fields.Game,
-				BestGuess:       tt.fields.BestGuess,
-				BestGuessRounds: tt.fields.BestGuessRounds,
-				SuccessCount:    tt.fields.SuccessCount,
-				FailCount:       tt.fields.FailCount,
-				TotalRounds:     tt.fields.TotalRounds,
+				Game: game,
 			}
-			lossCount, maxRounds, totalRounds := s.SimulateOneInitialWord(tt.args.game, tt.args.initialWord)
-			assert.Equalf(t, tt.wantedLossCount, lossCount, "loss count for %v => %v", tt.args.game, tt.args.initialWord)
-			assert.Equalf(t, tt.wantedMaxRounds, maxRounds, "max rounds for %v => %v)", tt.args.game, tt.args.initialWord)
-			assert.Equalf(t, tt.wantedTotalRounds, totalRounds, "total rounds for %v => %v)", tt.args.game, tt.args.initialWord)
+
+			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 {
-		Game            *Game
 		BestGuess       string
 		BestGuessRounds int
 		SuccessCount    int
@@ -109,12 +72,12 @@ func TestSimulation_SimulateOneGame(t *testing.T) {
 		TotalRounds     int
 	}
 	type args struct {
-		simulatedGame *Game
-		initialWord   string
-		answer        string
+		initialWord string
+		answer      string
 	}
 	tests := []struct {
 		name      string
+		words     []string
 		fields    fields
 		args      args
 		won       bool
@@ -122,15 +85,9 @@ func TestSimulation_SimulateOneGame(t *testing.T) {
 	}{
 
 		{
-			name: "initial word is answer",
+			name:  "initial word is answer",
+			words: []string{"aaa", "aab", "abb"},
 			args: args{
-				simulatedGame: &Game{
-					Words: map[string]*Word{
-						"aaa": {Word: "aaa"},
-						"aab": {Word: "aab"},
-						"abb": {Word: "abb"},
-					},
-				},
 				initialWord: "aaa",
 				answer:      "aaa",
 			},
@@ -138,15 +95,9 @@ func TestSimulation_SimulateOneGame(t *testing.T) {
 			numRounds: 1,
 		},
 		{
-			name: "one guess to get answer",
+			name:  "one guess to get answer",
+			words: []string{"aaa", "aab", "abb"},
 			args: args{
-				simulatedGame: &Game{
-					Words: map[string]*Word{
-						"aaa": {Word: "aaa"},
-						"aab": {Word: "aab"},
-						"abb": {Word: "abb"},
-					},
-				},
 				initialWord: "aaa",
 				answer:      "aab",
 			},
@@ -154,17 +105,9 @@ func TestSimulation_SimulateOneGame(t *testing.T) {
 			numRounds: 2,
 		},
 		{
-			name: "one guess to get answer",
+			name:  "one guess to get answer",
+			words: []string{"aaa", "aab", "aac", "aad", "aae"},
 			args: args{
-				simulatedGame: &Game{
-					Words: map[string]*Word{
-						"aaa": {Word: "aaa"},
-						"aab": {Word: "aab"},
-						"aac": {Word: "aac"},
-						"aad": {Word: "aad"},
-						"aae": {Word: "aae"},
-					},
-				},
 				initialWord: "aaa",
 				answer:      "aae",
 			},
@@ -174,10 +117,20 @@ func TestSimulation_SimulateOneGame(t *testing.T) {
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			s := Simulation{}
-			got, got1 := s.SimulateOneGame(tt.args.simulatedGame, tt.args.initialWord, tt.args.answer)
-			assert.Equalf(t, tt.won, got, "SimulateOneGame(%v, %v, %v)", tt.args.simulatedGame, tt.args.initialWord, tt.args.answer)
-			assert.Equalf(t, tt.numRounds, got1, "SimulateOneGame(%v, %v, %v)", tt.args.simulatedGame, tt.args.initialWord, tt.args.answer)
+
+			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)
 		})
 	}
 }