Source code for picogl.examples.test_mvp_standalone

#!/usr/bin/env python3
"""
Standalone test to verify the mvp_parameters fix

This script tests the fix without importing the Qt-dependent modules.
"""


[docs] def test_mvp_parameters_fix(): """Test that mvp_parameters can be initialized and used""" print("๐Ÿงช Testing mvp_parameters fix...") try: # Define MvpParameters class locally to avoid Qt imports from dataclasses import dataclass @dataclass class MvpParameters: """MVP Parameters""" rotation_x = None rotation_y = None pan_x = None pan_y = None # Create mvp_parameters instance mvp = MvpParameters() print(f" Initial values:") print(f" rotation_x: {mvp.rotation_x} (type: {type(mvp.rotation_x)})") print(f" rotation_y: {mvp.rotation_y} (type: {type(mvp.rotation_y)})") print(f" pan_x: {mvp.pan_x} (type: {type(mvp.pan_x)})") print(f" pan_y: {mvp.pan_y} (type: {type(mvp.pan_y)})") # Initialize values to avoid None (this is the fix) mvp.rotation_x = 0.0 mvp.rotation_y = 0.0 mvp.pan_x = 0.0 mvp.pan_y = 0.0 print(f" After initialization:") print(f" rotation_x: {mvp.rotation_x} (type: {type(mvp.rotation_x)})") print(f" rotation_y: {mvp.rotation_y} (type: {type(mvp.rotation_y)})") print(f" pan_x: {mvp.pan_x} (type: {type(mvp.pan_x)})") print(f" pan_y: {mvp.pan_y} (type: {type(mvp.pan_y)})") # Test the problematic operation that was causing the TypeError print(" Testing arithmetic operations...") # This was the line causing the error: # self.mvp_parameters.rotation_x += delta.x() * 0.5 delta_x = 10.0 delta_y = 15.0 # Before the fix, this would cause: TypeError: unsupported operand type(s) for +=: 'NoneType' and 'float' mvp.rotation_x += delta_x * 0.5 mvp.rotation_y += delta_y * 0.5 mvp.pan_x += delta_x * 0.01 mvp.pan_y += delta_y * 0.01 print(f" After arithmetic operations:") print(f" rotation_x: {mvp.rotation_x} (expected: 5.0)") print(f" rotation_y: {mvp.rotation_y} (expected: 7.5)") print(f" pan_x: {mvp.pan_x} (expected: 0.1)") print(f" pan_y: {mvp.pan_y} (expected: 0.15)") # Verify the results assert mvp.rotation_x == 5.0, f"Expected 5.0, got {mvp.rotation_x}" assert mvp.rotation_y == 7.5, f"Expected 7.5, got {mvp.rotation_y}" assert mvp.pan_x == 0.1, f"Expected 0.1, got {mvp.pan_x}" assert mvp.pan_y == 0.15, f"Expected 0.15, got {mvp.pan_y}" print(" โœ… All arithmetic operations completed successfully!") print(" โœ… No TypeError occurred!") return True except Exception as e: print(f" โŒ Test failed: {e}") import traceback traceback.print_exc() return False
[docs] def test_error_reproduction(): """Test that reproduces the original error without the fix""" print("๐Ÿงช Testing error reproduction (without fix)...") try: from dataclasses import dataclass @dataclass class MvpParameters: """MVP Parameters (without fix)""" rotation_x = None rotation_y = None pan_x = None pan_y = None # Create mvp_parameters instance mvp = MvpParameters() print(f" Initial values (None):") print(f" rotation_x: {mvp.rotation_x} (type: {type(mvp.rotation_x)})") # This should cause the original error print(" Attempting arithmetic operation on None value...") try: delta_x = 10.0 mvp.rotation_x += delta_x * 0.5 # This should fail print(" โŒ Unexpected: Operation succeeded when it should have failed!") return False except TypeError as e: print(f" โœ… Expected error occurred: {e}") print(" โœ… This confirms the original issue!") return True except Exception as e: print(f" โŒ Test failed: {e}") return False
[docs] def test_fix_verification(): """Test that the fix prevents the error""" print("๐Ÿงช Testing fix verification...") try: from dataclasses import dataclass @dataclass class MvpParameters: """MVP Parameters (with fix)""" rotation_x = None rotation_y = None pan_x = None pan_y = None # Create mvp_parameters instance mvp = MvpParameters() # Apply the fix: initialize values to avoid None mvp.rotation_x = 0.0 mvp.rotation_y = 0.0 mvp.pan_x = 0.0 mvp.pan_y = 0.0 print(f" After fix application:") print(f" rotation_x: {mvp.rotation_x} (type: {type(mvp.rotation_x)})") # This should now work without error print(" Attempting arithmetic operation after fix...") delta_x = 10.0 delta_y = 15.0 mvp.rotation_x += delta_x * 0.5 mvp.rotation_y += delta_y * 0.5 print(f" โœ… Operation succeeded!") print(f" rotation_x: {mvp.rotation_x}") print(f" rotation_y: {mvp.rotation_y}") return True except Exception as e: print(f" โŒ Test failed: {e}") return False
[docs] def main(): """Run all tests""" print("๐Ÿš€ Testing MVP Parameters Fix (Standalone)...") print("=" * 70) tests = [ test_mvp_parameters_fix, test_error_reproduction, test_fix_verification, ] passed = 0 total = len(tests) for test in tests: if test(): passed += 1 print() print("=" * 70) print(f"๐Ÿ“Š Test Results: {passed}/{total} tests passed") if passed == total: print("๐ŸŽ‰ All tests passed! The mvp_parameters fix is working correctly.") print("โœ… The TypeError should no longer occur in the Qt cube examples.") print("") print("๐Ÿ“‹ Summary of the fix:") print( " 1. The original error: TypeError: unsupported operand type(s) for +=: 'NoneType' and 'float'" ) print(" 2. The cause: mvp_parameters.rotation_x was None by default") print( " 3. The fix: Initialize mvp_parameters values to 0.0 in QtCubeRenderer.__init__" ) print(" 4. The result: Arithmetic operations now work without error") return 0 else: print("โŒ Some tests failed. Please check the implementation.") return 1
if __name__ == "__main__": import sys sys.exit(main())