From dbdddd870a5769f2b6c06959d18e04244d2a190f Mon Sep 17 00:00:00 2001 From: David Brown Date: Tue, 16 Jul 2019 11:05:27 -0700 Subject: [PATCH] Fix #31 Fixes start of arrayslice This makes sure start of arrayslice is actually 0 before we start the processing of arrayslice subscripts. Signed-off-by: David Brown --- jsonpath2/subscripts/arrayslice.py | 4 +++- tests/arrayslice_subscript_test.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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))))