Commit d0a4e367 authored by Taddeus Kroes's avatar Taddeus Kroes

Added representation functions for statement and apply_filter for block, along with unit tests.

parent 7b1f59f5
......@@ -17,6 +17,13 @@ class Statement:
return self.stype == other.stype and self.name == other.name \
and self.args == other.args
def __str__(self): # pragma: nocover
return '<Statement type=%s name=%s args=%s>' \
% (self.stype, self.name, self.args)
def __repr__(self): # pragma: nocover
return str(self)
def is_comment(self):
return self.stype == 'comment'
......@@ -120,6 +127,11 @@ class Block:
self.statements = before + replacement + after
self.pointer = start + len(replacement)
def apply_filter(self, callback):
"""Apply a filter to the statement list. If the callback returns True,
the statement will remain in the list.."""
self.statements = filter(callback, self.statements)
def find_leaders(statements):
"""Determine the leaders, which are:
......
......@@ -11,9 +11,13 @@ class TestUtils(unittest.TestCase):
self.statements = [add, S('command', 'j', 'foo'), add, add, \
S('label', 'foo')]
self.block = B([S('command', 'foo'), \
S('command', 'bar'),
S('comment', 'bar'),
S('command', 'baz')])
def tearDown(self):
del self.statements
del self.block
def test_find_leaders(self):
self.assertEqual(find_leaders(self.statements), [0, 2, 4])
......@@ -46,9 +50,12 @@ class TestUtils(unittest.TestCase):
self.assertEqual(S('command', 'j', 'foo').jump_target(), 'foo')
self.assertRaises(Exception, S('command', 'foo').jump_target)
def test_iter(self):
self.assertEqual(self.block.statements, list(self.block))
def test_read(self):
self.assertEqual(self.block.read(), S('command', 'foo'))
self.assertEqual(self.block.read(), S('command', 'bar'))
self.assertEqual(self.block.read(), S('comment', 'bar'))
self.assertEqual(self.block.read(), S('command', 'baz'))
def test_end(self):
......@@ -61,13 +68,18 @@ class TestUtils(unittest.TestCase):
def test_peek(self):
self.assertEqual(self.block.peek(), S('command', 'foo'))
self.assertEqual(self.block.peek(2), [S('command', 'foo'), \
S('command', 'bar')])
S('comment', 'bar')])
self.block.read()
self.assertEqual(self.block.peek(), S('command', 'bar'))
self.assertEqual(self.block.peek(), S('comment', 'bar'))
def test_replace(self):
self.block.replace(1, [S('command', 'foobar')])
self.assertEqual(self.block.pointer, 1)
self.assertEqual(self.block.statements, [S('command', 'foobar'), \
S('command', 'bar'), \
S('comment', 'bar'), \
S('command', 'baz')])
def test_apply_filter(self):
self.block.apply_filter(lambda s: s.is_command())
self.assertEqual(self.block.statements, [S('command', 'foo'), \
S('command', 'baz')])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment