From 7b3872f64c9246f946f4dfeedbdbdf4172d11448 Mon Sep 17 00:00:00 2001 From: yzamir Date: Sat, 26 Aug 2023 21:01:03 +0300 Subject: [PATCH] Add tests for plyaer in_line Signed-off-by: yzamir --- rose/server/player_test.py | 95 +++++++++++++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 11 deletions(-) diff --git a/rose/server/player_test.py b/rose/server/player_test.py index 3af2785b..3415ccaf 100644 --- a/rose/server/player_test.py +++ b/rose/server/player_test.py @@ -1,15 +1,88 @@ -from . import player +import pytest +from rose.common import actions, config +from rose.server.player import Player -def test_in_lane(): - p = player.Player("A", car=0, lane=0) - for x in (0, 1, 2): - p.x = x - assert p.in_lane() +# Sample Test Setup +@pytest.fixture +def player(): + return Player("John", "car1", 1) -def test_not_in_lane(): - p = player.Player("A", car=0, lane=1) - for x in (0, 1, 2): - p.x = x - assert not p.in_lane() +def test_player_initialization(player): + assert player.name == "John" + assert player.car == "car1" + assert player.lane == 1 + assert player.x is not None # This checks that x is initialized + assert player.y == config.matrix_height // 3 * 2 + assert player.action == actions.NONE + assert player.response_time == 1.0 + assert player.score == 0 + + +def test_player_reset(player): + player.score = 50 # Modify player to make sure reset works + player.reset() + assert player.x == player.lane * config.cells_per_player + 1 + assert player.y == config.matrix_height // 3 * 2 + assert player.action == actions.NONE + assert player.response_time == 1.0 + assert player.score == 0 + + +def test_player_in_lane_true(player): + # This test assumes the player is in their lane after reset. + # So, we test the default state after initialization. + assert player.in_lane() is True + + +def test_player_in_lane_false(player): + # Modify player's position to be out of their lane + other_lane = (player.lane + 1) % config.max_players + player.x = other_lane * config.cells_per_player + 1 + assert player.in_lane() is False + + +@pytest.mark.parametrize("lane,x,expected", [ + (0, 0, True), # Lane 0, x=0 + (0, 1, True), # Lane 0, x=1 + (0, 2, True), # Lane 0, x=2 + (0, 3, False), # Lane 0, x=3 + (0, 4, False), # Lane 0, x=4 + (0, 5, False), # Lane 0, x=5 + (1, 0, False), # Lane 1, x=0 + (1, 1, False), # Lane 1, x=1 + (1, 2, False), # Lane 1, x=2 + (1, 3, True), # Lane 1, x=3 + (1, 4, True), # Lane 1, x=4 + (1, 5, True) # Lane 1, x=5 +]) +def test_player_in_lane_various_positions(player, lane, x, expected): + player.lane = lane + player.x = x + assert player.in_lane() == expected + + +def test_player_comparison(): + player1 = Player("John", "car1", 1) + player2 = Player("Doe", "car2", 2) + + # Initial scores are the same + assert not player1 < player2 + assert not player2 < player1 + + # Modify scores + player1.score = 10 + player2.score = 20 + + assert player1 < player2 + + +def test_player_state(player): + state = player.state() + assert state['name'] == player.name + assert state['car'] == player.car + assert state['x'] == player.x + assert state['y'] == player.y + assert state['lane'] == player.lane + assert state['score'] == player.score