diff --git a/jsonpath2/subscripts/arrayslice.py b/jsonpath2/subscripts/arrayslice.py index d0cdeb0..cacc581 100644 --- a/jsonpath2/subscripts/arrayslice.py +++ b/jsonpath2/subscripts/arrayslice.py @@ -35,7 +35,9 @@ def match(self, root_value: object, current_value: object) -> Generator[MatchDat """Match an array slice between values.""" if isinstance(current_value, Sequence) and not isinstance(current_value, str): start = None if (self.start is None) else ( - self.start + (len(current_value) if (self.start < 0) else 0)) + self.start + (( + len(current_value) if abs(self.start) < len(current_value) else abs(self.start) + ) if (self.start < 0) else 0)) end = None if (self.end is None) else ( self.end + (len(current_value) if (self.end < 0) else 0)) diff --git a/tests/arrayslice_subscript_test.py b/tests/arrayslice_subscript_test.py index fed8319..c446652 100644 --- a/tests/arrayslice_subscript_test.py +++ b/tests/arrayslice_subscript_test.py @@ -75,3 +75,17 @@ def test_arrayslice_not_list(self): root_value = None self.assertTrue(not isinstance(root_value, list)) self.assertEqual([], list(subscript.match(root_value, root_value))) + + def test_arrayslice8(self): + """Test the arrayslice with configuration 1000 (base 2).""" + subscript = ArraySliceSubscript(-15, None, None) + self.assertEqual('-15:', subscript.tojsonpath()) + self.assertEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], list(map( + lambda match_data: match_data.current_value, subscript.match(self.root_value, self.current_value)))) + + def test_arrayslice9(self): + """Test the arrayslice with configuration 1001 (base 2).""" + subscript = ArraySliceSubscript(None, -15, None) + self.assertEqual(':-15', subscript.tojsonpath()) + self.assertEqual([], list(map( + lambda match_data: match_data.current_value, subscript.match(self.root_value, self.current_value))))