Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion jsonpath2/subscripts/arrayslice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions tests/arrayslice_subscript_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))))