58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
import pytest
|
|
from src.food import Food
|
|
|
|
def test_food_initialization():
|
|
"""Test food initialization"""
|
|
food = Food(20)
|
|
assert food.block_size == 20
|
|
assert food.color == (255, 0, 0) # Default red color
|
|
assert food.position == (0, 0) # Default position
|
|
|
|
def test_food_spawn():
|
|
"""Test food spawning mechanics"""
|
|
food = Food(20)
|
|
width, height = 800, 600
|
|
snake_body = [(100, 100), (120, 100)] # Mock snake body
|
|
|
|
# Test spawning
|
|
food.spawn(width, height, snake_body)
|
|
|
|
# Check if food is within bounds
|
|
assert 0 <= food.position[0] < width
|
|
assert 0 <= food.position[1] < height
|
|
|
|
# Check if position aligns with grid
|
|
assert food.position[0] % food.block_size == 0
|
|
assert food.position[1] % food.block_size == 0
|
|
|
|
# Check if food doesn't spawn on snake
|
|
assert food.position not in snake_body
|
|
|
|
def test_food_collision():
|
|
"""Test food collision detection"""
|
|
food = Food(20)
|
|
food.position = (100, 100)
|
|
|
|
# Test collision
|
|
assert food.check_collision((100, 100)) == True
|
|
|
|
# Test no collision
|
|
assert food.check_collision((120, 120)) == False
|
|
|
|
def test_food_spawn_full_grid():
|
|
"""Test food spawning when grid is mostly occupied"""
|
|
food = Food(20)
|
|
width, height = 60, 60 # Small grid
|
|
|
|
# Create a snake that occupies most of the grid
|
|
snake_body = [(x, y) for x in range(0, 60, 20) for y in range(0, 40, 20)]
|
|
|
|
# Leave one spot free at (40, 40)
|
|
free_spot = (40, 40)
|
|
assert free_spot not in snake_body
|
|
|
|
# Spawn food
|
|
food.spawn(width, height, snake_body)
|
|
|
|
# Food should spawn in the only free spot
|
|
assert food.position == free_spot |