McRogueFace/tests/issue_96_uicollection_extend_test.py
John McCardle cd0bd5468b Squashed commit of the following: [alpha_streamline_1]
the low-hanging fruit of pre-existing issues and standardizing the
Python interfaces

Special thanks to Claude Code, ~100k output tokens for this merge

    🤖 Generated with [Claude Code](https://claude.ai/code)
    Co-Authored-By: Claude <noreply@anthropic.com>

commit 99f301e3a0
Author: John McCardle <mccardle.john@gmail.com>
Date:   Sat Jul 5 16:25:32 2025 -0400

    Add position tuple support and pos property to UI elements

    closes #83, closes #84

    - Issue #83: Add position tuple support to constructors
      - Frame and Sprite now accept both (x, y) and ((x, y)) forms
      - Also accept Vector objects as position arguments
      - Caption and Entity already supported tuple/Vector forms
      - Uses PyVector::from_arg for flexible position parsing

    - Issue #84: Add pos property to Frame and Sprite
      - Added pos getter that returns a Vector
      - Added pos setter that accepts Vector or tuple
      - Provides consistency with Caption and Entity which already had pos properties
      - All UI elements now have a uniform way to get/set positions as Vectors

    Both features improve API consistency and make it easier to work with positions.

commit 2f2b488fb5
Author: John McCardle <mccardle.john@gmail.com>
Date:   Sat Jul 5 16:18:10 2025 -0400

    Standardize sprite_index property and add scale_x/scale_y to UISprite

    closes #81, closes #82

    - Issue #81: Standardized property name to sprite_index across UISprite and UIEntity
      - Added sprite_index as the primary property name
      - Kept sprite_number as a deprecated alias for backward compatibility
      - Updated repr() methods to use sprite_index
      - Updated animation system to recognize both names

    - Issue #82: Added scale_x and scale_y properties to UISprite
      - Enables non-uniform scaling of sprites
      - scale property still works for uniform scaling
      - Both properties work with the animation system

    All existing code using sprite_number continues to work due to backward compatibility.

commit 5a003a9aa5
Author: John McCardle <mccardle.john@gmail.com>
Date:   Sat Jul 5 16:09:52 2025 -0400

    Fix multiple low priority issues

    closes #12, closes #80, closes #95, closes #96, closes #99

    - Issue #12: Set tp_new to NULL for GridPoint and GridPointState to prevent instantiation from Python
    - Issue #80: Renamed Caption.size to Caption.font_size for semantic clarity
    - Issue #95: Fixed UICollection repr to show actual derived types instead of generic UIDrawable
    - Issue #96: Added extend() method to UICollection for API consistency with UIEntityCollection
    - Issue #99: Exposed read-only properties for Texture (sprite_width, sprite_height, sheet_width, sheet_height, sprite_count, source) and Font (family, source)

    All issues have corresponding tests that verify the fixes work correctly.

commit e5affaf317
Author: John McCardle <mccardle.john@gmail.com>
Date:   Sat Jul 5 15:50:09 2025 -0400

    Fix critical issues: script loading, entity types, and color properties

    - Issue #37: Fix Windows scripts subdirectory not checked
      - Updated executeScript() to use executable_path() from platform.h
      - Scripts now load correctly when working directory differs from executable

    - Issue #76: Fix UIEntityCollection returns wrong type
      - Updated UIEntityCollectionIter::next() to check for stored Python object
      - Derived Entity classes now preserve their type when retrieved from collections

    - Issue #9: Recreate RenderTexture when resized (already fixed)
      - Confirmed RenderTexture recreation already implemented in set_size() and set_float_member()
      - Uses 1.5x padding and 4096 max size limit

    - Issue #79: Fix Color r, g, b, a properties return None
      - Implemented get_member() and set_member() in PyColor.cpp
      - Color component properties now work correctly with proper validation

    - Additional fix: Grid.at() method signature
      - Changed from METH_O to METH_VARARGS to accept two arguments

    All fixes include comprehensive tests to verify functionality.

    closes #37, closes #76, closes #9, closes #79
2025-07-05 18:56:02 -04:00

205 lines
No EOL
6.8 KiB
Python

#!/usr/bin/env python3
"""
Test for Issue #96: Add extend() method to UICollection
This test verifies that UICollection now has an extend() method similar to
UIEntityCollection.extend().
"""
import mcrfpy
import sys
def test_uicollection_extend():
"""Test UICollection extend method"""
print("=== Testing UICollection extend() Method (Issue #96) ===\n")
tests_passed = 0
tests_total = 0
# Get scene UI collection
scene_ui = mcrfpy.sceneUI("test")
# Test 1: Basic extend with list
print("--- Test 1: Extend with list ---")
tests_total += 1
try:
# Create a list of UI elements
elements = [
mcrfpy.Frame(10, 10, 100, 100),
mcrfpy.Caption((150, 50), "Test1", mcrfpy.Font("assets/JetbrainsMono.ttf")),
mcrfpy.Sprite(200, 100)
]
# Extend the collection
scene_ui.extend(elements)
if len(scene_ui) == 3:
print("✓ PASS: Extended collection with 3 elements")
tests_passed += 1
else:
print(f"✗ FAIL: Expected 3 elements, got {len(scene_ui)}")
except Exception as e:
print(f"✗ FAIL: Error extending with list: {e}")
# Test 2: Extend with tuple
print("\n--- Test 2: Extend with tuple ---")
tests_total += 1
try:
# Create a tuple of UI elements
more_elements = (
mcrfpy.Grid(10, 10),
mcrfpy.Frame(300, 10, 100, 100)
)
# Extend the collection
scene_ui.extend(more_elements)
if len(scene_ui) == 5:
print("✓ PASS: Extended collection with tuple (now 5 elements)")
tests_passed += 1
else:
print(f"✗ FAIL: Expected 5 elements, got {len(scene_ui)}")
except Exception as e:
print(f"✗ FAIL: Error extending with tuple: {e}")
# Test 3: Extend with generator
print("\n--- Test 3: Extend with generator ---")
tests_total += 1
try:
# Create a generator of UI elements
def create_sprites():
for i in range(3):
yield mcrfpy.Sprite(50 + i*50, 200)
# Extend with generator
scene_ui.extend(create_sprites())
if len(scene_ui) == 8:
print("✓ PASS: Extended collection with generator (now 8 elements)")
tests_passed += 1
else:
print(f"✗ FAIL: Expected 8 elements, got {len(scene_ui)}")
except Exception as e:
print(f"✗ FAIL: Error extending with generator: {e}")
# Test 4: Error handling - non-iterable
print("\n--- Test 4: Error handling - non-iterable ---")
tests_total += 1
try:
scene_ui.extend(42) # Not iterable
print("✗ FAIL: Should have raised TypeError for non-iterable")
except TypeError as e:
print(f"✓ PASS: Correctly raised TypeError: {e}")
tests_passed += 1
except Exception as e:
print(f"✗ FAIL: Wrong exception type: {e}")
# Test 5: Error handling - wrong element type
print("\n--- Test 5: Error handling - wrong element type ---")
tests_total += 1
try:
scene_ui.extend([1, 2, 3]) # Wrong types
print("✗ FAIL: Should have raised TypeError for non-UIDrawable elements")
except TypeError as e:
print(f"✓ PASS: Correctly raised TypeError: {e}")
tests_passed += 1
except Exception as e:
print(f"✗ FAIL: Wrong exception type: {e}")
# Test 6: Extend empty iterable
print("\n--- Test 6: Extend with empty list ---")
tests_total += 1
try:
initial_len = len(scene_ui)
scene_ui.extend([]) # Empty list
if len(scene_ui) == initial_len:
print("✓ PASS: Extending with empty list works correctly")
tests_passed += 1
else:
print(f"✗ FAIL: Length changed from {initial_len} to {len(scene_ui)}")
except Exception as e:
print(f"✗ FAIL: Error extending with empty list: {e}")
# Test 7: Z-index ordering
print("\n--- Test 7: Z-index ordering ---")
tests_total += 1
try:
# Clear and add fresh elements
while len(scene_ui) > 0:
scene_ui.remove(0)
# Add some initial elements
frame1 = mcrfpy.Frame(0, 0, 50, 50)
scene_ui.append(frame1)
# Extend with more elements
new_elements = [
mcrfpy.Frame(60, 0, 50, 50),
mcrfpy.Caption((120, 25), "Test", mcrfpy.Font("assets/JetbrainsMono.ttf"))
]
scene_ui.extend(new_elements)
# Check z-indices are properly assigned
z_indices = [scene_ui[i].z_index for i in range(3)]
# Z-indices should be increasing
if z_indices[0] < z_indices[1] < z_indices[2]:
print(f"✓ PASS: Z-indices properly ordered: {z_indices}")
tests_passed += 1
else:
print(f"✗ FAIL: Z-indices not properly ordered: {z_indices}")
except Exception as e:
print(f"✗ FAIL: Error checking z-indices: {e}")
# Test 8: Extend with another UICollection
print("\n--- Test 8: Extend with another UICollection ---")
tests_total += 1
try:
# Create a Frame with children
frame_with_children = mcrfpy.Frame(200, 200, 100, 100)
frame_with_children.children.append(mcrfpy.Sprite(10, 10))
frame_with_children.children.append(mcrfpy.Caption((10, 50), "Child", mcrfpy.Font("assets/JetbrainsMono.ttf")))
# Try to extend scene_ui with the frame's children collection
initial_len = len(scene_ui)
scene_ui.extend(frame_with_children.children)
if len(scene_ui) == initial_len + 2:
print("✓ PASS: Extended with another UICollection")
tests_passed += 1
else:
print(f"✗ FAIL: Expected {initial_len + 2} elements, got {len(scene_ui)}")
except Exception as e:
print(f"✗ FAIL: Error extending with UICollection: {e}")
# Summary
print(f"\n=== SUMMARY ===")
print(f"Tests passed: {tests_passed}/{tests_total}")
if tests_passed == tests_total:
print("\nIssue #96 FIXED: UICollection.extend() implemented successfully!")
else:
print("\nIssue #96: Some tests failed")
return tests_passed == tests_total
def run_test(runtime):
"""Timer callback to run the test"""
try:
success = test_uicollection_extend()
print("\nOverall result: " + ("PASS" if success else "FAIL"))
except Exception as e:
print(f"\nTest error: {e}")
import traceback
traceback.print_exc()
print("\nOverall result: FAIL")
sys.exit(0)
# Set up the test scene
mcrfpy.createScene("test")
mcrfpy.setScene("test")
# Schedule test to run after game loop starts
mcrfpy.setTimer("test", run_test, 100)