Skip to content

Add test cases to unit_test_sprites.cpp#164

Open
kyriesk wants to merge 2 commits into
thoth-tech:mainfrom
kyriesk:unit-test/sprite-collision
Open

Add test cases to unit_test_sprites.cpp#164
kyriesk wants to merge 2 commits into
thoth-tech:mainfrom
kyriesk:unit-test/sprite-collision

Conversation

@kyriesk
Copy link
Copy Markdown

@kyriesk kyriesk commented May 1, 2026

Description

This pull request adds comprehensive unit tests for three collision-related functions in the sprite module that were previously untested in the unit test suite. The tests adapt test scenarios from test_sprites.cpp (sktest) into proper unit tests using the Catch2 framework.

The new tests ensure that:

  • calculate_collision_direction correctly determines the direction of collision between sprites and geometric shapes
  • resolve_collision properly resolves collisions by moving objects out of collision states
  • sprite_ray_collision accurately detects when sprites intersect with rays

Fixes #(issue related to missing collision tests)

Type of change

  • New feature (non-breaking change which adds functionality)

Changes Made

  • Added 5 new test cases to coresdk/src/test/unit_tests/unit_test_sprites.cpp:

    1. collision direction between sprites (6 sub-tests)

      • No collision detection
      • Collision detected in multiple directions (left, right, top, bottom)
    2. collision direction between sprite and rectangle (2 sub-tests)

      • No collision detection
      • Collision detected
    3. collision resolution between sprites (3 sub-tests)

      • Successful collision resolution
      • No resolution when sprites don't collide
      • No resolution with zero direction vector
    4. collision resolution between sprite and rectangle (2 sub-tests)

      • Successful collision resolution
      • No resolution when sprite and rectangle don't collide
    5. sprite ray collision detection (6 sub-tests)

      • Ray intersects sprite
      • Ray misses sprite
      • Ray origin at sprite center
      • Different ray directions
      • Ray pointing away from sprite
      • Multiple sprites along same ray
  • Added required headers: collisions.h and geometry.h

How Has This Been Tested?

The tests have been written following the existing patterns in unit_test_sprites.cpp and use:

  • Catch2 TEST_CASE and SECTION macros
  • Standard sprite creation and manipulation functions
  • Position-based collision scenarios with known outcomes
  • Assertions using REQUIRE and REQUIRE_FALSE macros

All tests use existing resources (rocket_sprt.png, background.png, frog.png) that are already available in the test resources directory.

All tests were run and verified to pass:

cd projects/cmake
cmake --preset Linux
cmake --build build/
cd ../../bin
./skunit_tests "[collision]"

Testing Checklist

  • Tested with skunit_tests

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented test scenarios with descriptive names
  • Tests follow existing unit test patterns in the codebase
  • My changes generate no new warnings
  • Tests are isolated and properly clean up resources (free sprites)
  • I have requested a review from ... on the Pull Request

Additional Notes

  • All collision direction tests verify both vector magnitude and direction (when collisions occur)
  • All collision resolution tests verify position changes before and after resolution
  • Ray collision tests cover edge cases including ray origin at sprite center and rays pointing away
  • Tests follow the pattern of setting up controlled sprite positions and verifying expected collision behaviors

Copy link
Copy Markdown

@222448082Ashen 222448082Ashen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General Information

  • Type of Change: New feature (Unit Testing)

Code Quality

  • Repository: Correct. The PR is made to splashkit-core.
  • Readability: High. The code uses descriptive TEST_CASE names and SECTION labels, making the test scenarios easy to understand.
  • Maintainability: High. The tests leverage existing SplashKit functions and assets, making them easy to maintain as the core library evolves.

Functionality

  • Correctness: The tests effectively validate multiple aspects of sprite collisions:
    • Directional Detection: Correctly identifies if and from which direction a collision occurs between sprites and between a sprite and a rectangle.
    • Collision Resolution: Verifies that resolve_collision correctly moves sprites out of a collision state and returns the appropriate success/failure boolean.
    • Ray Casting: Tests the sprite_ray_collision function for both intersecting and non-intersecting scenarios.
  • Impact on Existing Functionality: No impact on existing code as this PR only adds new test cases.

Testing

  • Test Coverage: This PR provides valuable coverage for the collision-related functions in the sprite module, which are critical for game development workflows.
  • Test Results: All logic within the tests is sound. The tests correctly load existing assets (rocket_sprt.png, background.png) and clean up resources using free_sprite().

Documentation

  • Documentation: Inline documentation is sufficient; the test structure itself serves as documentation for the expected behavior of the collision functions.

Pull Request Details

  • PR Description: The addition of these tests clearly addresses a gap in the current unit test suite.
  • Checklist Completion: All relevant items reviewed.

Copy link
Copy Markdown

@kottochii kottochii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the changes and had a read through. The test cases make sense.

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.

3 participants