def test_find(self): data = [1, 2, 3, 4, 5] self.assertEqual(find(Slice(step=2), data), [1, 3, 5]) self.assertEqual(find(Slice(start=0, end=4, step=2), data), [1, 3]) self.assertEqual(find(Slice(start=-4, end=8, step=4), data), [2]) self.assertEqual(find(Slice(start=4, end=-4, step=3), data), []) self.assertEqual(find(Slice(), {"data": 1}), [{"data": 1}])
def p_slice(p): """index : int_empty ':' int_empty ':' int_empty | int_empty ':' int_empty""" if len(p) == 6: p[0] = Slice(start=p[1], end=p[3], step=p[5]) else: p[0] = Slice(start=p[1], end=p[3])
def test_str_all_leaf(self): self.assertEqual(str(Root()), "$") self.assertEqual(str(This()), "@") self.assertEqual(str(Index(1)), "[1]") self.assertEqual(str(Index("*")), "[*]") self.assertEqual(str(AllIndex()), "[*]") self.assertEqual(str(Fields("name", "other")), "name,other") self.assertEqual(str(Fields("name")), "name") self.assertEqual(str(Slice(0, 3, 2)), "[0:3:2]") self.assertEqual(str(Slice()), "[::]") self.assertEqual(str(Len()), "`len`") self.assertEqual(str(Sum()), "`sum`") self.assertEqual(str(Avg()), "`avg`") self.assertEqual(str(Sorted()), "`sorted`") self.assertEqual(str(And(Fields("name"), Fields("id"))), "name&id") self.assertEqual(str(Or(Fields("name"), Fields("id"))), "name|id")
def test_repr_all_leaf(self): self.assertEqual(repr(Root()), "Root()") self.assertEqual(repr(This()), "This()") self.assertEqual(repr(Index(2)), "Index(2)") self.assertEqual(repr(AllIndex()), "AllIndex()") self.assertEqual(repr(Fields("name", "other")), "Fields('name','other')") self.assertEqual(repr(Slice(0, 3, 2)), "Slice(start=0,end=3,step=2)") self.assertEqual(repr(Len()), "Len()") self.assertEqual(repr(Sum()), "Sum()") self.assertEqual(repr(Avg()), "Avg()") self.assertEqual(repr(Sorted()), "Sorted()") self.assertEqual(repr(And(Fields("name"), Fields("id"))), "And(Fields('name'),Fields('id'))") self.assertEqual(repr(Or(Fields("name"), Fields("id"))), "Or(Fields('name'),Fields('id'))")
def test_find_with_exception(self): assert_with_error(self, Slice(), JPathNodeError)
def test_find_in_string(self): data = "This is an string" self.assertEqual(find(Slice(start=1, end=4), data), ["his"]) self.assertEqual(find(Slice(start=1, end=6, step=2), data), ["hsi"]) self.assertEqual(find(Slice(), data), [data])
def test_equal(self): assert Slice(2, 6, 3) == Slice(2, 6, 3) assert Slice() == Slice() assert not Slice(2, 6, 3) == Slice(2, 0, 3)