McRogueFace/tests/unit/test_entity_collection_remove.py

100 lines
3.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""
Test for UIEntityCollection.remove() accepting Entity instances
Tests the new behavior where remove() takes an Entity object instead of an index
"""
import mcrfpy
import sys
def test_remove_by_entity():
"""Test removing entities by passing the entity object"""
2026-02-09 08:15:18 -05:00
# Create a test scene and grid
scene_name = "test_entity_remove"
2026-01-03 10:59:52 -05:00
_scene = mcrfpy.Scene(scene_name)
2026-02-09 08:15:18 -05:00
# Create a grid (entities need a grid)
2026-02-09 08:15:18 -05:00
grid = mcrfpy.Grid(grid_size=(30, 30), pos=(0, 0), size=(300, 300))
_scene.children.append(grid)
# Get the entity collection
entities = grid.entities
2026-02-09 08:15:18 -05:00
# Create some test entities - position is set via constructor tuple (grid coords)
# Entity.x/.y requires grid attachment, so append first, then check
entity1 = mcrfpy.Entity((5, 5))
entity2 = mcrfpy.Entity((10, 10))
entity3 = mcrfpy.Entity((15, 15))
# Add entities to the collection
entities.append(entity1)
entities.append(entity2)
entities.append(entity3)
2026-02-09 08:15:18 -05:00
print(f"Initial entity count: {len(entities)}")
assert len(entities) == 3, "Should have 3 entities"
2026-02-09 08:15:18 -05:00
# Test 1: Remove an entity that exists
print("\nTest 1: Remove existing entity")
entities.remove(entity2)
assert len(entities) == 2, "Should have 2 entities after removal"
assert entity1 in entities, "Entity1 should still be in collection"
assert entity2 not in entities, "Entity2 should not be in collection"
assert entity3 in entities, "Entity3 should still be in collection"
2026-02-09 08:15:18 -05:00
print(" Successfully removed entity2")
# Test 2: Try to remove an entity that's not in the collection
print("\nTest 2: Remove non-existent entity")
try:
entities.remove(entity2) # Already removed
assert False, "Should have raised ValueError"
except ValueError as e:
2026-02-09 08:15:18 -05:00
print(f" Got expected ValueError: {e}")
# Test 3: Try to remove with wrong type
print("\nTest 3: Remove with wrong type")
try:
entities.remove(42) # Not an Entity
assert False, "Should have raised TypeError"
except TypeError as e:
2026-02-09 08:15:18 -05:00
print(f" Got expected TypeError: {e}")
# Test 4: Try to remove with None
print("\nTest 4: Remove with None")
try:
entities.remove(None)
assert False, "Should have raised TypeError"
except TypeError as e:
2026-02-09 08:15:18 -05:00
print(f" Got expected TypeError: {e}")
# Test 5: Verify grid property is cleared (C++ internal)
print("\nTest 5: Grid property handling")
# Create a new entity and add it
2026-02-09 08:15:18 -05:00
entity4 = mcrfpy.Entity((20, 20))
entities.append(entity4)
# Note: grid property is managed internally in C++ and not exposed to Python
2026-02-09 08:15:18 -05:00
# Remove it - this clears the C++ grid reference internally
entities.remove(entity4)
2026-02-09 08:15:18 -05:00
print(" Grid property handling (managed internally in C++)")
# Test 6: Remove all entities one by one
print("\nTest 6: Remove all entities")
entities.remove(entity1)
entities.remove(entity3)
assert len(entities) == 0, "Collection should be empty"
2026-02-09 08:15:18 -05:00
print(" Successfully removed all entities")
print("\nAll tests passed!")
return True
if __name__ == "__main__":
try:
success = test_remove_by_entity()
sys.exit(0 if success else 1)
except Exception as e:
2026-02-09 08:15:18 -05:00
print(f"\nTest failed with exception: {e}")
import traceback
traceback.print_exc()
2026-02-09 08:15:18 -05:00
sys.exit(1)