From 3a0c0360b08b46f543a9ef8d170a86574542af92 Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Wed, 18 Nov 2020 00:32:46 +0200 Subject: [PATCH] Add tests for the use of various kinds of slicing inside suites. --- genshi/template/tests/eval.py | 65 +++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/genshi/template/tests/eval.py b/genshi/template/tests/eval.py index c8eb80f..a8dcd84 100644 --- a/genshi/template/tests/eval.py +++ b/genshi/template/tests/eval.py @@ -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()