Skip to content

Conversation

lwgray
Copy link
Owner

@lwgray lwgray commented Dec 13, 2024

Description:
This PR adds robust error handling for list indexing operations in the Think language interpreter, along with comprehensive test coverage to verify the behavior.

Changes

  • Add validation for list indices before access:
    • Type checking (must be integer or whole number)
    • Bounds checking (both positive and negative indices)
    • Validation of nested list access
  • Improve error messages to provide clearer feedback
  • Move error handling to be proactive rather than reactive

New Tests

Added two test suites to verify error handling:

def test_list_indexing_errors(self, interpreter, parser, capture_output):
    """Test error cases for list indexing."""
    # Test index out of bounds (positive)
    # Test index out of bounds (negative) 
    # Test non-integer index
    # Test invalid type for index

def test_nested_list_indexing_errors(self, interpreter, parser, capture_output):
    """Test error cases for nested list indexing."""
    # Test accessing index of non-list
    # Test out of bounds on nested list

Testing

  • All existing tests pass
  • New test coverage verifies error cases:
    • Out of bounds indices
    • Non-integer indices
    • Invalid type indices
    • Nested indexing errors

Example Error Messages

Invalid index/key: 5 is out of range for list of length 3
Invalid index/key: List indices must be integers, got str
Cannot index into type: int

Breaking Changes

None. This PR only improves error handling without changing existing behavior.

This commit adds comprehensive error handling for list indexing operations
in the Think language interpreter, along with extensive test coverage.

Key changes:
- Add proactive validation of list indices before access
- Validate numeric types and whole numbers for indices
- Add explicit bounds checking for list access
- Improve error messages for invalid indexing operations

New tests:
- test_list_indexing_errors: Tests basic indexing error cases
 - Out of bounds access (positive and negative indices)
 - Non-integer indices
 - Invalid types for indices
- test_nested_list_indexing_errors: Tests nested indexing errors
 - Accessing index of non-list values
 - Out of bounds on nested lists

The error handling now catches issues before Python's built-in errors,
providing clearer error messages specific to the Think language.
@lwgray lwgray self-assigned this Dec 13, 2024
@lwgray lwgray merged commit 26afeb2 into develop Dec 13, 2024
3 checks passed
@lwgray lwgray deleted the test_list_indexing branch December 13, 2024 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant