Skip to content

Commit

Permalink
Add tests for plyaer in_line
Browse files Browse the repository at this point in the history
Signed-off-by: yzamir <[email protected]>
  • Loading branch information
yaacov committed Aug 26, 2023
1 parent 806e65b commit 7b3872f
Showing 1 changed file with 84 additions and 11 deletions.
95 changes: 84 additions & 11 deletions rose/server/player_test.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7b3872f

Please sign in to comment.