// TestResultString tests that mancala.Result::String returns the correct // string representation. func TestResultString(t *testing.T) { t.Parallel() base := mancala.NewResult( mancala.Board{}, 0, 0, 0, nil, ).Board().String() + "\n\n" base += "Result of last move:\n" base += "Last move: 0\n" base += "Move count: 0" testCases := []struct { result mancala.Result expected string }{ { result: mancala.NewResult( mancala.Board{}, 0, 0, 0, nil, ), expected: base, }, { result: mancala.NewResult( mancala.Board{}, 0, 0, 0, errors.New("error"), ), expected: base + "\nError: error", }, } for _, testCase := range testCases { actual := testCase.result.String() if actual != testCase.expected { t.Errorf( "(\n%v).String() = \n%s, want \n%s", testCase.result, actual, testCase.expected, ) } } }
// TestResultBoard tests that mancala.Result::Board returns the mancala.Board. func TestResultBoard(t *testing.T) { t.Parallel() actual := mancala.NewResult(mancala.NewBoard(), -1, 0, -1, nil).Board() expected := mancala.NewBoard() if actual != expected { t.Errorf("result.Board() = %v, want %v", actual, expected) } }
// TestResultErr tests that mancala.Result::Err returns the error. func TestResultErr(t *testing.T) { t.Parallel() actual := mancala.NewResult( mancala.NewBoard(), -1, 0, -1, errors.New("error"), ).Err() expected := errors.New("error") if actual.Error() != expected.Error() { t.Errorf("result.Err() = %v, want %v", actual, expected) } }
// TestResultLastMove tests that manacala.Result::LastMove returns the last // move. func TestResultLastMove(t *testing.T) { t.Parallel() expected := mancala.HouseIndex(1) actual := mancala.NewResult( mancala.NewBoard(), -1, 0, expected, nil, ).LastMove() if actual != expected { t.Errorf("result.LastMove() = %d, want %d", actual, expected) } }
// TestResultMoveCount tests that mancala.Result::MoveCount returns the move // count. func TestResultMoveCount(t *testing.T) { t.Parallel() expected := 1 actual := mancala.NewResult( mancala.NewBoard(), -1, expected, -1, nil, ).MoveCount() if actual != expected { t.Errorf("result.TurnCount() = %d, want %d", actual, expected) } }
// TestResultWinner tests that mancala.Result::Winner returns the winning // mancala.Player's mancala.PlayerIndex. func TestResultWinner(t *testing.T) { t.Parallel() expected := mancala.PlayerIndex(mancala.Player1) actual := mancala.NewResult( mancala.NewBoard(), expected, 0, -1, nil, ).Winner() if actual != expected { t.Errorf("result.Winner() = %v, want %v", actual, expected) } }
// TestFinishInitializedGame tests that mancala.FinishInitializedGame returns // the correct mancala.Result from an already in progress mancala.Game described // by a shortPlayer. func TestFinishInitializedGame(t *testing.T) { t.Parallel() turnOffSet := 10 player1 := newShortPlayer(mancala.Player1 + turnOffSet) player2 := newShortPlayer(mancala.Player2 + turnOffSet) referee := mancala.NewInitializedReferee( []mancala.Board{}, mancala.NewInitializedBoard( 2, 15, mancala.Grid{ mancala.Row{0, 2, 1, 1, 1, 1}, mancala.Row{1, 10, 4, 0, 1, 9}, }, ), ) actual := mancala.FinishInitializedGame( referee, player1, player2, mancala.Player2, turnOffSet+1, ) expected := mancala.NewResult( player1.expectedBoard(), player1.expectedWinner(), player1.expectedMoveCount(), player1.expectedLastMove(), nil, ) if actual != expected { t.Errorf( "FinishInitializedGame(\n\t%v,\n\t%v,\n\t%v,\n\t%v,\n)"+ " = \n%v, want \n%v", referee, player1, player2, mancala.Player2, actual, expected, ) } }
// TestFinishGame tests that mancala.FinishGame returns the mancala.Result from // the beginning of a mancala.Game described by a shortPlayer. func TestFinishGame(t *testing.T) { t.Parallel() player1 := newShortPlayer(mancala.Player1) player2 := newShortPlayer(mancala.Player2) actual := mancala.FinishGame(player1, player2) expected := mancala.NewResult( player1.expectedBoard(), player1.expectedWinner(), player1.expectedMoveCount(), player1.expectedLastMove(), nil, ) if actual != expected { t.Errorf( "FinishGame(\n\t%v, \n\t%v,\n) = \n%v, want \n%v", player1, player2, actual, expected, ) } }
// TestGamePlayMove tests that mancala.Game::PlayMove returns the mancala.Result // after a move in a shortPlayer mancala.Game and updates the mancala.Game // state. func TestGamePlayMove(t *testing.T) { t.Parallel() game := mancala.NewGame(badPlayer{}, errPlayer{}) actual := game.PlayMove().Err() expected := fmt.Errorf( "move at house %d is out of range [%d, %d]", -1, 0, mancala.RowWidth-1, ) if actual.Error() != expected.Error() { t.Errorf( "game.PlayMove().Err().Error() = %s, want %s", actual.Error(), expected.Error(), ) } game = mancala.NewGame(errPlayer{}, badPlayer{}) actual = game.PlayMove().Err() expected = errors.New("error") if actual.Error() != expected.Error() { t.Errorf( "game.PlayMove().Err().Error() = %s, want %s", actual.Error(), expected.Error(), ) } game = mancala.NewGame( newShortPlayer(mancala.Player1), newShortPlayer(mancala.Player2), ) actualResult := game.PlayMove() expectedResult := mancala.NewResult( mancala.NewInitializedBoard( 0, 0, mancala.Grid{ mancala.Row{4, 4, 4, 0, 5, 5}, mancala.Row{5, 5, 4, 4, 4, 4}, }, ), -1, 1, 3, nil, ) if actualResult != expectedResult { t.Errorf( "game.PlayMove() = \n%v, want \n%v", actualResult, expectedResult, ) } actualPlayer := game.CurrentPlayer() expectedPlayer := mancala.PlayerIndex(mancala.Player2) if actualPlayer != expectedPlayer { t.Errorf( "game.CurrentPlayer() = %v, want %v", actualPlayer, expectedPlayer, ) } }