Test suite modernization

This commit is contained in:
John McCardle 2026-02-09 08:15:18 -05:00
commit 52fdfd0347
141 changed files with 9947 additions and 4665 deletions

View file

@ -2,140 +2,185 @@
"""
Test the new mcrfpy.Timer object with pause/resume/stop functionality
Updated for new Timer API (#173)
Uses mcrfpy.step() to advance time in headless mode.
"""
import mcrfpy
import sys
# Test counters
print("\n=== Testing mcrfpy.Timer object ===\n")
# Create a minimal scene
timer_test = mcrfpy.Scene("timer_test")
mcrfpy.current_scene = timer_test
all_pass = True
# Test 1: Create a basic timer and verify properties
print("Test 1: Creating Timer object")
call_count = 0
pause_test_count = 0
cancel_test_count = 0
def timer_callback(timer, runtime):
global call_count
call_count += 1
print(f"Timer fired! Count: {call_count}, Runtime: {runtime}ms")
def pause_test_callback(timer, runtime):
global pause_test_count
pause_test_count += 1
print(f"Pause test timer: {pause_test_count}")
timer1 = mcrfpy.Timer("test_timer", timer_callback, 500)
print(f" Created timer: {timer1}")
print(f" Interval: {timer1.interval}ms")
print(f" Active: {timer1.active}")
print(f" Paused: {timer1.paused}")
def cancel_test_callback(timer, runtime):
global cancel_test_count
cancel_test_count += 1
print(f"Cancel test timer: {cancel_test_count} - This should only print once!")
if not timer1.active:
print(" FAIL: Timer should be active after creation")
all_pass = False
else:
print(" PASS: Timer is active")
def run_tests(timer, runtime):
"""Main test function that runs after game loop starts"""
# Stop the timer that called us to prevent re-running
timer.stop()
# Stop timer1 before next test to avoid interference
timer1.stop()
print("\n=== Testing mcrfpy.Timer object ===\n")
# Test 2: Timer fires when stepped
print("\nTest 2: Timer fires with step()")
timer1b = mcrfpy.Timer("test_timer2", timer_callback, 500)
call_count = 0
for i in range(5):
mcrfpy.step(0.51) # 510ms > 500ms interval
timer1b.stop()
# Test 1: Create a basic timer
print("Test 1: Creating Timer object")
timer1 = mcrfpy.Timer("test_timer", timer_callback, 500)
print(f"✓ Created timer: {timer1}")
print(f" Interval: {timer1.interval}ms")
print(f" Active: {timer1.active}")
print(f" Paused: {timer1.paused}")
if call_count >= 3:
print(f" PASS: Timer fired {call_count} times (expected >=3)")
else:
print(f" FAIL: Timer fired {call_count} times (expected >=3)")
all_pass = False
# Test 2: Test pause/resume
print("\nTest 2: Testing pause/resume functionality")
timer2 = mcrfpy.Timer("pause_test", pause_test_callback, 200)
# Test 3: Test pause/resume
print("\nTest 3: Testing pause/resume functionality")
pause_count = 0
# Schedule pause after 250ms
def pause_timer2(t, rt):
print(" Pausing timer2...")
timer2.pause()
print(f" Timer2 paused: {timer2.paused}")
print(f" Timer2 active: {timer2.active}")
def pause_callback(timer, runtime):
global pause_count
pause_count += 1
# Schedule resume after another 400ms
def resume_timer2(t2, rt2):
print(" Resuming timer2...")
timer2.resume()
print(f" Timer2 paused: {timer2.paused}")
print(f" Timer2 active: {timer2.active}")
timer2 = mcrfpy.Timer("pause_test", pause_callback, 200)
mcrfpy.Timer("resume_timer2", resume_timer2, 400, once=True)
# Let it fire once
mcrfpy.step(0.21)
fires_before_pause = pause_count
mcrfpy.Timer("pause_timer2", pause_timer2, 250, once=True)
# Pause it
timer2.pause()
print(f" Timer2 paused: {timer2.paused}")
print(f" Timer2 active: {timer2.active}")
# Test 3: Test cancel/stop
print("\nTest 3: Testing stop functionality")
timer3 = mcrfpy.Timer("cancel_test", cancel_test_callback, 300)
if not timer2.paused:
print(" FAIL: Timer should be paused")
all_pass = False
# Cancel after 350ms (should fire once)
def cancel_timer3(t, rt):
print(" Stopping timer3...")
timer3.stop()
print(" Timer3 stopped")
# Step while paused - should not fire
mcrfpy.step(0.51)
fires_while_paused = pause_count
mcrfpy.Timer("cancel_timer3", cancel_timer3, 350, once=True)
if fires_while_paused == fires_before_pause:
print(f" PASS: Timer did not fire while paused (count={fires_while_paused})")
else:
print(f" FAIL: Timer fired while paused ({fires_before_pause} -> {fires_while_paused})")
all_pass = False
# Test 4: Test interval modification
print("\nTest 4: Testing interval modification")
def interval_test(timer, runtime):
print(f" Interval test fired at {runtime}ms")
# Resume
timer2.resume()
print(f" Timer2 paused after resume: {timer2.paused}")
mcrfpy.step(0.21)
fires_after_resume = pause_count
timer4 = mcrfpy.Timer("interval_test", interval_test, 1000)
print(f" Original interval: {timer4.interval}ms")
timer4.interval = 500
print(f" Modified interval: {timer4.interval}ms")
if fires_after_resume > fires_while_paused:
print(f" PASS: Timer fired after resume (count={fires_after_resume})")
else:
print(f" FAIL: Timer did not fire after resume (count={fires_after_resume})")
all_pass = False
# Test 5: Test remaining time
print("\nTest 5: Testing remaining time")
def check_remaining(t, rt):
if timer1.active:
print(f" Timer1 remaining: {timer1.remaining}ms")
if timer2.active or timer2.paused:
print(f" Timer2 remaining: {timer2.remaining}ms (paused: {timer2.paused})")
# Test 4: Test stop
print("\nTest 4: Testing stop functionality")
cancel_count = 0
remaining_timer = mcrfpy.Timer("check_remaining", check_remaining, 150)
def cancel_callback(timer, runtime):
global cancel_count
cancel_count += 1
# Test 6: Test restart
print("\nTest 6: Testing restart functionality")
restart_count = [0]
timer3 = mcrfpy.Timer("cancel_test", cancel_callback, 300)
def restart_test(timer, runtime):
restart_count[0] += 1
print(f" Restart test: {restart_count[0]}")
if restart_count[0] == 2:
print(" Restarting timer...")
timer.restart()
# Let it fire once
mcrfpy.step(0.31)
fires_before_stop = cancel_count
timer5 = mcrfpy.Timer("restart_test", restart_test, 400)
# Stop it
timer3.stop()
print(f" Timer3 stopped, active: {timer3.active}")
# Final verification after 2 seconds
def final_check(t, rt):
print("\n=== Final Results ===")
print(f"Timer1 call count: {call_count} (expected: ~4)")
print(f"Pause test count: {pause_test_count} (expected: ~6-7, with pause gap)")
print(f"Cancel test count: {cancel_test_count} (expected: 1)")
print(f"Restart test count: {restart_count[0]} (expected: ~5 with restart)")
# Step after stop - should not fire
mcrfpy.step(0.61)
fires_after_stop = cancel_count
# Verify timer states
try:
print(f"\nTimer1 active: {timer1.active}")
print(f"Timer2 active: {timer2.active}")
print(f"Timer3 active: {timer3.active} (should be False after stop)")
print(f"Timer4 active: {timer4.active}")
print(f"Timer5 active: {timer5.active}")
except:
print("Some timers may have been garbage collected")
if fires_after_stop == fires_before_stop:
print(f" PASS: Timer did not fire after stop (count={fires_after_stop})")
else:
print(f" FAIL: Timer fired after stop ({fires_before_stop} -> {fires_after_stop})")
all_pass = False
print("\n✓ All Timer object tests completed!")
sys.exit(0)
# Test 5: Test interval modification
print("\nTest 5: Testing interval modification")
mcrfpy.Timer("final_check", final_check, 2000, once=True)
def interval_test(timer, runtime):
pass
# Create a minimal scene
timer_test = mcrfpy.Scene("timer_test")
timer_test.activate()
timer4 = mcrfpy.Timer("interval_test", interval_test, 1000)
print(f" Original interval: {timer4.interval}ms")
timer4.interval = 500
print(f" Modified interval: {timer4.interval}ms")
# Start tests after game loop begins
mcrfpy.Timer("run_tests", run_tests, 100, once=True)
if timer4.interval == 500:
print(" PASS: Interval modified successfully")
else:
print(" FAIL: Interval modification failed")
all_pass = False
print("Timer object tests starting...")
# Test 6: Test restart
print("\nTest 6: Testing restart functionality")
restart_count = 0
def restart_test(timer, runtime):
global restart_count
restart_count += 1
timer5 = mcrfpy.Timer("restart_test", restart_test, 400)
# Let it fire twice
mcrfpy.step(0.41)
mcrfpy.step(0.41)
# Restart it
timer5.restart()
count_at_restart = restart_count
# Let it fire again
mcrfpy.step(0.41)
if restart_count > count_at_restart:
print(f" PASS: Timer fires after restart (count={restart_count})")
else:
print(f" FAIL: Timer did not fire after restart")
all_pass = False
# Clean up timers
timer2.stop()
timer4.stop()
timer5.stop()
# Final results
print("\n=== Final Results ===")
if all_pass:
print("All Timer object tests PASSED!")
print("PASS")
sys.exit(0)
else:
print("Some Timer object tests FAILED!")
print("FAIL")
sys.exit(1)