Skip to content

Commit

Permalink
Add tests for the use of various kinds of slicing inside suites.
Browse files Browse the repository at this point in the history
  • Loading branch information
hodgestar committed Nov 17, 2020
1 parent 61aad8a commit 3a0c036
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions genshi/template/tests/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,71 @@ def test_with_statement_with_multiple_items(self):
finally:
os.remove(path)

def test_slice(self):
suite = Suite("x = numbers[0:2]")
data = {"numbers": [0, 1, 2, 3]}
suite.execute(data)
self.assertEqual([0, 1], data["x"])

def test_slice_with_vars(self):
suite = Suite("x = numbers[start:end]")
data = {"numbers": [0, 1, 2, 3], "start": 0, "end": 2}
suite.execute(data)
self.assertEqual([0, 1], data["x"])

def test_slice_copy(self):
suite = Suite("x = numbers[:]")
data = {"numbers": [0, 1, 2, 3]}
suite.execute(data)
self.assertEqual([0, 1, 2, 3], data["x"])

def test_slice_stride(self):
suite = Suite("x = numbers[::stride]")
data = {"numbers": [0, 1, 2, 3, 4], "stride": 2}
suite.execute(data)
self.assertEqual([0, 2, 4], data["x"])

def test_slice_negative_start(self):
suite = Suite("x = numbers[-1:]")
data = {"numbers": [0, 1, 2, 3, 4], "stride": 2}
suite.execute(data)
self.assertEqual([4], data["x"])

def test_slice_negative_end(self):
suite = Suite("x = numbers[:-1]")
data = {"numbers": [0, 1, 2, 3, 4], "stride": 2}
suite.execute(data)
self.assertEqual([0, 1, 2, 3], data["x"])

def test_slice_constant(self):
suite = Suite("x = numbers[1]")
data = {"numbers": [0, 1, 2, 3, 4]}
suite.execute(data)
self.assertEqual(1, data["x"])

def test_slice_call(self):
def f():
return 2
suite = Suite("x = numbers[f()]")
data = {"numbers": [0, 1, 2, 3, 4], "f": f}
suite.execute(data)
self.assertEqual(2, data["x"])

def test_slice_name(self):
suite = Suite("x = numbers[v]")
data = {"numbers": [0, 1, 2, 3, 4], "v": 2}
suite.execute(data)
self.assertEqual(2, data["x"])

def test_slice_attribute(self):
class ValueHolder:
def __init__(self):
self.value = 3
suite = Suite("x = numbers[obj.value]")
data = {"numbers": [0, 1, 2, 3, 4], "obj": ValueHolder()}
suite.execute(data)
self.assertEqual(3, data["x"])


def suite():
suite = unittest.TestSuite()
Expand Down

0 comments on commit 3a0c036

Please sign in to comment.