#!/usr/bin/env python3 """ Simple test for Issue #73: Entity.index() method """ def test_entity_index(timer_name): """Test that Entity.index() method works correctly""" import mcrfpy import sys print("Testing Entity.index() method...") # Create test scene and grid mcrfpy.createScene("test") ui = mcrfpy.sceneUI("test") # Create grid with texture texture = mcrfpy.Texture("assets/kenney_ice.png", 16, 16) grid = mcrfpy.Grid(10, 10, texture, (10, 10), (400, 400)) ui.append(grid) # Clear any existing entities while len(grid.entities) > 0: grid.entities.remove(0) # Create entities entity1 = mcrfpy.Entity((1, 1), texture, 1, grid) entity2 = mcrfpy.Entity((2, 2), texture, 2, grid) entity3 = mcrfpy.Entity((3, 3), texture, 3, grid) grid.entities.append(entity1) grid.entities.append(entity2) grid.entities.append(entity3) print(f"Created {len(grid.entities)} entities") # Test index() method idx1 = entity1.index() idx2 = entity2.index() idx3 = entity3.index() print(f"Entity 1 index: {idx1}") print(f"Entity 2 index: {idx2}") print(f"Entity 3 index: {idx3}") assert idx1 == 0, f"Entity 1 should be at index 0, got {idx1}" assert idx2 == 1, f"Entity 2 should be at index 1, got {idx2}" assert idx3 == 2, f"Entity 3 should be at index 2, got {idx3}" print("✓ All entities report correct indices") # Test removal using index remove_idx = entity2.index() grid.entities.remove(remove_idx) print(f"✓ Removed entity at index {remove_idx}") # Check remaining entities assert len(grid.entities) == 2 assert entity1.index() == 0 assert entity3.index() == 1 # Should have shifted down print("✓ Indices updated correctly after removal") # Test entity not in grid orphan = mcrfpy.Entity((5, 5), texture, 5, None) try: idx = orphan.index() print(f"✗ Orphan entity should raise error but returned {idx}") except RuntimeError as e: print(f"✓ Orphan entity correctly raises error") print("\n✅ Entity.index() test PASSED") sys.exit(0) # Execute the test after a short delay import mcrfpy mcrfpy.setTimer("test", test_entity_index, 100)