Skip to content

Commit

Permalink
Merge pull request #35 from rleigh-codelibre/slice-type-additions
Browse files Browse the repository at this point in the history
astutil: Add missing slice types
  • Loading branch information
hodgestar authored Nov 18, 2020
2 parents b41e4d8 + 3a0c036 commit fc87332
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
6 changes: 1 addition & 5 deletions genshi/template/astutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,17 +734,13 @@ def _process_slice(node):
self.visit(node.step)
elif isinstance(node, _ast.Index):
self.visit(node.value)
elif isinstance(node, _ast_Constant):
self.visit_Constant(node)
elif isinstance(node, _ast.UnaryOp):
self.visit_UnaryOp(node)
elif isinstance(node, _ast.ExtSlice):
self.visit(node.dims[0])
for dim in node.dims[1:]:
self._write(', ')
self.visit(dim)
else:
raise NotImplementedError('Slice type not implemented')
self.visit(node)
_process_slice(node.slice)
self._write(']')

Expand Down
90 changes: 90 additions & 0 deletions genshi/template/tests/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,31 @@ def test_slice_negative_end(self):
res = expr.evaluate({'numbers': list(range(5))})
self.assertEqual([0, 1, 2, 3], res)

def test_slice_constant(self):
expr = Expression("numbers[1]")
res = expr.evaluate({"numbers": list(range(5))})
self.assertEqual(res, 1)

def test_slice_call(self):
def f():
return 2
expr = Expression("numbers[f()]")
res = expr.evaluate({"numbers": list(range(5)), "f": f})
self.assertEqual(res, 2)

def test_slice_name(self):
expr = Expression("numbers[v]")
res = expr.evaluate({"numbers": list(range(5)), "v": 2})
self.assertEqual(res, 2)

def test_slice_attribute(self):
class ValueHolder:
def __init__(self):
self.value = 3
expr = Expression("numbers[obj.value]")
res = expr.evaluate({"numbers": list(range(5)), "obj": ValueHolder()})
self.assertEqual(res, 3)

def test_access_undefined(self):
expr = Expression("nothing", filename='index.html', lineno=50,
lookup='lenient')
Expand Down Expand Up @@ -941,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 fc87332

Please sign in to comment.