Skip to content

Add unit test suite for all game modules#1

Open
yashamin11 wants to merge 1 commit into
adwaitraste:masterfrom
yashamin11:add-test-suite
Open

Add unit test suite for all game modules#1
yashamin11 wants to merge 1 commit into
adwaitraste:masterfrom
yashamin11:add-test-suite

Conversation

@yashamin11
Copy link
Copy Markdown

Summary

Adds test_melonland.py — a full unit test suite covering every module in the project. Pygame is mocked via unittest.mock so tests run headlessly with no display or asset files required.


Test Cases

TestGlobalSettings — verifies the central config values

Test What it checks
test_title Game title is "MelonLand"
test_number_of_levels There are exactly 5 levels
test_grid_factors xFactor and yFactor are both 80
test_grid_dimensions Grid is 10×10
test_screen_width screenWidth = (cols+1) × xFactor = 880
test_screen_height screenHeight = (rows+2) × yFactor = 960
test_fixed_obstacle_score fobsScore = 5
test_moving_obstacle_score mobsScore = 10
test_completion_bonus completeScore = 25
test_time_penalty timePenalty = 2
test_dying_penalty dyingPenalty = 50
test_color_black/white/green/red All colour tuples are correct RGB values
test_*_image_path Asset paths contain the expected filename
test_start/end_screen_messages UI strings contain the right keywords

TestPlayer — verifies movement, scoring, and collision logic

Test What it checks
test_initial_score_is_zero Player starts with score 0
test_initial_not_invincible isInvincible defaults to False
test_initial_grid_coords_zero gridX and gridY start at 0
test_player_number_stored Player stores its number (1 or 2)
test_velocity_equals_grid_factor xVel/yVel match xFactor/yFactor
test_move_up_decrements_y Moving up subtracts yVel from y
test_move_down_increments_y Moving down adds yVel to y
test_move_left_decrements_x Moving left subtracts xVel from x
test_move_right_increments_x Moving right adds xVel to x
test_move_up_updates_gridY gridY tracks position correctly after moving up
test_move_down_updates_gridY gridY tracks position correctly after moving down
test_left_right_do_not_change_gridY Horizontal moves don't affect gridY
test_p1_up_step1_earns_fobs_score Player 1 moving into an odd row earns +5
test_p1_up_step2_earns_mobs_score Player 1 moving into an even row earns +10
test_p1_down_step1_loses_mobs_score Player 1 moving backwards into odd row loses 10
test_p1_down_step2_loses_fobs_score Player 1 moving backwards into even row loses 5
test_p2_down_step1_earns_fobs_score Player 2 moving into an odd row earns +5
test_p2_down_step2_earns_mobs_score Player 2 moving into an even row earns +10
test_p2_up_step1_loses_mobs_score Player 2 moving backwards into odd row loses 10
test_p2_up_step2_loses_fobs_score Player 2 moving backwards into even row loses 5
test_is_collided_full_overlap Two completely overlapping objects collide
test_is_collided_partial_overlap_x Objects overlapping by 1px on x-axis collide
test_no_collision_obstacle_right_of_player Object fully to the right does not collide
test_no_collision_obstacle_left_of_player Object fully to the left does not collide
test_no_collision_obstacle_below_player Object fully below does not collide
test_no_collision_obstacle_above_player Object fully above does not collide
test_touching_x_edge_is_collision Objects sharing an edge (touching) count as a collision
test_touching_y_edge_is_collision Same as above on the y-axis
test_invincible_player_never_collides Invincible player is immune to all collisions
test_invincible_player_no_collision_even_when_overlapping Invincibility holds even when completely inside an obstacle

TestFixedObstacle — verifies void obstacle placement and size

Test What it checks
test_position_at_origin Obstacle at row=0, col=0 has x=0, y=0
test_position_scaled_by_factor x = col × xFactor, y = row × yFactor
test_x_uses_col x coordinate derived from col, not row
test_y_uses_row y coordinate derived from row, not col
test_width_equals_x_factor width fills exactly one grid cell
test_height_equals_y_factor height fills exactly one grid cell
test_row_and_col_stored row and col attributes saved correctly
test_color_is_black Obstacle colour is (0, 0, 0)

TestMovingObstacle — verifies knife movement and screen wrapping

Test What it checks
test_initial_position_scaled Position = col × xFactor, row × yFactor
test_width_and_height_equal_factor Fills exactly one grid cell
test_row_col_direc_stored row, col, direc attributes saved correctly
test_velocity_stored obsVel attribute saved correctly
test_move_right_increments_x direc=1 adds obsVel to x each step
test_move_left_decrements_x direc=0 subtracts obsVel from x each step
test_move_does_not_change_y Movement is horizontal only
test_move_accumulates_over_multiple_steps 5 steps of vel=3 moves x by 15 total
test_right_mover_wraps_when_past_right_edge x > screenWidth resets to -xFactor
test_right_mover_no_wrap_exactly_at_boundary x == screenWidth does not trigger wrap
test_left_mover_wraps_when_past_left_edge x < -xFactor resets to screenWidth
test_left_mover_no_wrap_exactly_at_boundary x == -xFactor does not trigger wrap
test_right_mover_ignores_left_exit Right-moving knife ignores left-side overflow
test_left_mover_ignores_right_exit Left-moving knife ignores right-side overflow

TestLevel — verifies level setup and obstacle management

Test What it checks
test_difficulty_equals_lvl_plus_one diff = lvl + 1 for all 5 levels
test_initial_obstacle_lists_empty No obstacles exist before createObstacles()
test_players_reference_stored Level holds the same players list passed in
test_level_number_stored lvl attribute saved correctly
test_obstacle_count_level_1 Level 1 creates 8 fixed + 10 moving obstacles
test_obstacle_count_level_2 Level 2 creates 12 fixed + 15 moving obstacles
test_obstacle_count_level_5 Level 5 creates 24 fixed + 30 moving obstacles
test_higher_level_has_more_obstacles Level 3 has more obstacles than Level 1
test_obstacle_types_are_correct_classes fixedObstacles contain FixedObstacle, movingObstacles contain MovingObstacle
test_fixed_obstacles_placed_on_even_rows All fixed obstacles land on rows 2, 4, 6, 8
test_moving_obstacles_placed_on_odd_rows All moving obstacles land on rows 1, 3, 5, 7, 9
test_moving_obstacles_velocity_equals_difficulty Knife speed scales with level difficulty
test_create_obstacles_called_twice_doubles_count createObstacles() appends, doesn't reset
test_destroy_after_create_empties_lists destroyObstacles() clears both lists
test_destroy_on_empty_level_is_safe Calling destroy with no obstacles doesn't crash
test_create_after_destroy_repopulates Create → destroy → create gives correct counts again

How to run

python3 -m pytest test_melonland.py -v
# or
python3 -m unittest test_melonland -v

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant