Refactor 11 more tests to mcrfpy.step() pattern
Converted from Timer-based async to step()-based sync: - test_simple_callback.py - test_empty_animation_manager.py - test_frame_clipping.py - test_frame_clipping_advanced.py - test_grid_children.py - test_color_helpers.py - test_no_arg_constructors.py - test_properties_quick.py - test_simple_drawable.py - test_python_object_cache.py - WORKING_automation_test_example.py Only 4 tests remain with Timer-based patterns (2 are headless detection tests that may require special handling). 🤖 Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Frack <frack@goblincorps.dev> Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
bb86cece2b
commit
be450286f8
12 changed files with 867 additions and 866 deletions
|
|
@ -4,6 +4,7 @@ Test for Python object cache - verifies that derived Python classes
|
|||
maintain their identity when stored in and retrieved from collections.
|
||||
|
||||
Issue #112: Object Splitting - Preserve Python derived types in collections
|
||||
Refactored to use mcrfpy.step() for synchronous execution.
|
||||
"""
|
||||
|
||||
import mcrfpy
|
||||
|
|
@ -16,136 +17,128 @@ test_results = []
|
|||
def test(condition, message):
|
||||
global test_passed
|
||||
if condition:
|
||||
test_results.append(f"✓ {message}")
|
||||
test_results.append(f"+ {message}")
|
||||
else:
|
||||
test_results.append(f"✗ {message}")
|
||||
test_results.append(f"x {message}")
|
||||
test_passed = False
|
||||
|
||||
def run_tests(timer, runtime):
|
||||
"""Timer callback to run tests after game loop starts"""
|
||||
global test_passed
|
||||
|
||||
print("\n=== Testing Python Object Cache ===")
|
||||
|
||||
# Test 1: Create derived Frame class
|
||||
class MyFrame(mcrfpy.Frame):
|
||||
def __init__(self, x=0, y=0):
|
||||
super().__init__(pos=(x, y), size=(100, 100))
|
||||
self.custom_data = "I am a custom frame"
|
||||
self.test_value = 42
|
||||
|
||||
# Test 2: Create instance and add to scene
|
||||
frame = MyFrame(50, 50)
|
||||
scene_ui = test_scene.children
|
||||
scene_ui.append(frame)
|
||||
|
||||
# Test 3: Retrieve from collection and check type
|
||||
retrieved = scene_ui[0]
|
||||
test(type(retrieved) == MyFrame, "Retrieved object maintains derived type")
|
||||
test(isinstance(retrieved, MyFrame), "isinstance check passes")
|
||||
test(hasattr(retrieved, 'custom_data'), "Custom attribute exists")
|
||||
if hasattr(retrieved, 'custom_data'):
|
||||
test(retrieved.custom_data == "I am a custom frame", "Custom attribute value preserved")
|
||||
if hasattr(retrieved, 'test_value'):
|
||||
test(retrieved.test_value == 42, "Numeric attribute value preserved")
|
||||
|
||||
# Test 4: Check object identity (same Python object)
|
||||
test(retrieved is frame, "Retrieved object is the same Python object")
|
||||
test(id(retrieved) == id(frame), "Object IDs match")
|
||||
|
||||
# Test 5: Multiple retrievals return same object
|
||||
retrieved2 = scene_ui[0]
|
||||
test(retrieved2 is retrieved, "Multiple retrievals return same object")
|
||||
|
||||
# Test 6: Test with other UI types
|
||||
class MySprite(mcrfpy.Sprite):
|
||||
def __init__(self):
|
||||
# Use default texture by passing None
|
||||
super().__init__(texture=None, sprite_index=0)
|
||||
self.sprite_data = "custom sprite"
|
||||
|
||||
sprite = MySprite()
|
||||
sprite.x = 200
|
||||
sprite.y = 200
|
||||
scene_ui.append(sprite)
|
||||
|
||||
retrieved_sprite = scene_ui[1]
|
||||
test(type(retrieved_sprite) == MySprite, "Sprite maintains derived type")
|
||||
if hasattr(retrieved_sprite, 'sprite_data'):
|
||||
test(retrieved_sprite.sprite_data == "custom sprite", "Sprite custom data preserved")
|
||||
|
||||
# Test 7: Test with Caption
|
||||
class MyCaption(mcrfpy.Caption):
|
||||
def __init__(self, text):
|
||||
# Use default font by passing None
|
||||
super().__init__(text=text, font=None)
|
||||
self.caption_id = "test_caption"
|
||||
|
||||
caption = MyCaption("Test Caption")
|
||||
caption.x = 10
|
||||
caption.y = 10
|
||||
scene_ui.append(caption)
|
||||
|
||||
retrieved_caption = scene_ui[2]
|
||||
test(type(retrieved_caption) == MyCaption, "Caption maintains derived type")
|
||||
if hasattr(retrieved_caption, 'caption_id'):
|
||||
test(retrieved_caption.caption_id == "test_caption", "Caption custom data preserved")
|
||||
|
||||
# Test 8: Test removal and re-addition
|
||||
# Use del to remove by index (Python standard), or .remove(element) to remove by value
|
||||
print(f"before remove: {len(scene_ui)=}")
|
||||
del scene_ui[-1] # Remove last element by index
|
||||
print(f"after remove: {len(scene_ui)=}")
|
||||
|
||||
scene_ui.append(frame)
|
||||
retrieved3 = scene_ui[-1] # Get last element
|
||||
test(retrieved3 is frame, "Object identity preserved after removal/re-addition")
|
||||
|
||||
# Test 9: Test with Grid
|
||||
class MyGrid(mcrfpy.Grid):
|
||||
def __init__(self, w, h):
|
||||
super().__init__(grid_size=(w, h))
|
||||
self.grid_name = "custom_grid"
|
||||
|
||||
grid = MyGrid(10, 10)
|
||||
grid.x = 300
|
||||
grid.y = 100
|
||||
scene_ui.append(grid)
|
||||
|
||||
retrieved_grid = scene_ui[-1]
|
||||
test(type(retrieved_grid) == MyGrid, "Grid maintains derived type")
|
||||
if hasattr(retrieved_grid, 'grid_name'):
|
||||
test(retrieved_grid.grid_name == "custom_grid", "Grid custom data preserved")
|
||||
|
||||
# Test 10: Test with nested collections (Frame with children)
|
||||
parent = MyFrame(400, 400)
|
||||
child = MyFrame(10, 10)
|
||||
child.custom_data = "I am a child"
|
||||
parent.children.append(child)
|
||||
scene_ui.append(parent)
|
||||
|
||||
retrieved_parent = scene_ui[-1]
|
||||
test(type(retrieved_parent) == MyFrame, "Parent frame maintains type")
|
||||
if len(retrieved_parent.children) > 0:
|
||||
retrieved_child = retrieved_parent.children[0]
|
||||
test(type(retrieved_child) == MyFrame, "Child frame maintains type in nested collection")
|
||||
if hasattr(retrieved_child, 'custom_data'):
|
||||
test(retrieved_child.custom_data == "I am a child", "Child custom data preserved")
|
||||
|
||||
# Print results
|
||||
print("\n=== Test Results ===")
|
||||
for result in test_results:
|
||||
print(result)
|
||||
|
||||
print(f"\n{'PASS' if test_passed else 'FAIL'}: {sum(1 for r in test_results if r.startswith('✓'))}/{len(test_results)} tests passed")
|
||||
|
||||
sys.exit(0 if test_passed else 1)
|
||||
|
||||
# Create test scene
|
||||
test_scene = mcrfpy.Scene("test_scene")
|
||||
test_scene.activate()
|
||||
mcrfpy.step(0.01)
|
||||
|
||||
# Schedule tests to run after game loop starts
|
||||
test_timer = mcrfpy.Timer("test", run_tests, 100, once=True)
|
||||
print("\n=== Testing Python Object Cache ===")
|
||||
|
||||
print("Python object cache test initialized. Running tests...")
|
||||
# Test 1: Create derived Frame class
|
||||
class MyFrame(mcrfpy.Frame):
|
||||
def __init__(self, x=0, y=0):
|
||||
super().__init__(pos=(x, y), size=(100, 100))
|
||||
self.custom_data = "I am a custom frame"
|
||||
self.test_value = 42
|
||||
|
||||
# Test 2: Create instance and add to scene
|
||||
frame = MyFrame(50, 50)
|
||||
scene_ui = test_scene.children
|
||||
scene_ui.append(frame)
|
||||
|
||||
# Test 3: Retrieve from collection and check type
|
||||
retrieved = scene_ui[0]
|
||||
test(type(retrieved) == MyFrame, "Retrieved object maintains derived type")
|
||||
test(isinstance(retrieved, MyFrame), "isinstance check passes")
|
||||
test(hasattr(retrieved, 'custom_data'), "Custom attribute exists")
|
||||
if hasattr(retrieved, 'custom_data'):
|
||||
test(retrieved.custom_data == "I am a custom frame", "Custom attribute value preserved")
|
||||
if hasattr(retrieved, 'test_value'):
|
||||
test(retrieved.test_value == 42, "Numeric attribute value preserved")
|
||||
|
||||
# Test 4: Check object identity (same Python object)
|
||||
test(retrieved is frame, "Retrieved object is the same Python object")
|
||||
test(id(retrieved) == id(frame), "Object IDs match")
|
||||
|
||||
# Test 5: Multiple retrievals return same object
|
||||
retrieved2 = scene_ui[0]
|
||||
test(retrieved2 is retrieved, "Multiple retrievals return same object")
|
||||
|
||||
# Test 6: Test with other UI types
|
||||
class MySprite(mcrfpy.Sprite):
|
||||
def __init__(self):
|
||||
# Use default texture by passing None
|
||||
super().__init__(texture=None, sprite_index=0)
|
||||
self.sprite_data = "custom sprite"
|
||||
|
||||
sprite = MySprite()
|
||||
sprite.x = 200
|
||||
sprite.y = 200
|
||||
scene_ui.append(sprite)
|
||||
|
||||
retrieved_sprite = scene_ui[1]
|
||||
test(type(retrieved_sprite) == MySprite, "Sprite maintains derived type")
|
||||
if hasattr(retrieved_sprite, 'sprite_data'):
|
||||
test(retrieved_sprite.sprite_data == "custom sprite", "Sprite custom data preserved")
|
||||
|
||||
# Test 7: Test with Caption
|
||||
class MyCaption(mcrfpy.Caption):
|
||||
def __init__(self, text):
|
||||
# Use default font by passing None
|
||||
super().__init__(text=text, font=None)
|
||||
self.caption_id = "test_caption"
|
||||
|
||||
caption = MyCaption("Test Caption")
|
||||
caption.x = 10
|
||||
caption.y = 10
|
||||
scene_ui.append(caption)
|
||||
|
||||
retrieved_caption = scene_ui[2]
|
||||
test(type(retrieved_caption) == MyCaption, "Caption maintains derived type")
|
||||
if hasattr(retrieved_caption, 'caption_id'):
|
||||
test(retrieved_caption.caption_id == "test_caption", "Caption custom data preserved")
|
||||
|
||||
# Test 8: Test removal and re-addition
|
||||
# Use del to remove by index (Python standard), or .remove(element) to remove by value
|
||||
print(f"before remove: {len(scene_ui)=}")
|
||||
del scene_ui[-1] # Remove last element by index
|
||||
print(f"after remove: {len(scene_ui)=}")
|
||||
|
||||
scene_ui.append(frame)
|
||||
retrieved3 = scene_ui[-1] # Get last element
|
||||
test(retrieved3 is frame, "Object identity preserved after removal/re-addition")
|
||||
|
||||
# Test 9: Test with Grid
|
||||
class MyGrid(mcrfpy.Grid):
|
||||
def __init__(self, w, h):
|
||||
super().__init__(grid_size=(w, h))
|
||||
self.grid_name = "custom_grid"
|
||||
|
||||
grid = MyGrid(10, 10)
|
||||
grid.x = 300
|
||||
grid.y = 100
|
||||
scene_ui.append(grid)
|
||||
|
||||
retrieved_grid = scene_ui[-1]
|
||||
test(type(retrieved_grid) == MyGrid, "Grid maintains derived type")
|
||||
if hasattr(retrieved_grid, 'grid_name'):
|
||||
test(retrieved_grid.grid_name == "custom_grid", "Grid custom data preserved")
|
||||
|
||||
# Test 10: Test with nested collections (Frame with children)
|
||||
parent = MyFrame(400, 400)
|
||||
child = MyFrame(10, 10)
|
||||
child.custom_data = "I am a child"
|
||||
parent.children.append(child)
|
||||
scene_ui.append(parent)
|
||||
|
||||
retrieved_parent = scene_ui[-1]
|
||||
test(type(retrieved_parent) == MyFrame, "Parent frame maintains type")
|
||||
if len(retrieved_parent.children) > 0:
|
||||
retrieved_child = retrieved_parent.children[0]
|
||||
test(type(retrieved_child) == MyFrame, "Child frame maintains type in nested collection")
|
||||
if hasattr(retrieved_child, 'custom_data'):
|
||||
test(retrieved_child.custom_data == "I am a child", "Child custom data preserved")
|
||||
|
||||
# Print results
|
||||
print("\n=== Test Results ===")
|
||||
for result in test_results:
|
||||
print(result)
|
||||
|
||||
print(f"\n{'PASS' if test_passed else 'FAIL'}: {sum(1 for r in test_results if r.startswith('+'))}/{len(test_results)} tests passed")
|
||||
|
||||
sys.exit(0 if test_passed else 1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue