Start making Action objects more sophisticated.

This commit is contained in:
Aldo Cortesi 2012-10-05 11:23:30 +13:00
parent 495daf2b64
commit e83392bfc8
2 changed files with 38 additions and 11 deletions

View File

@ -401,9 +401,23 @@ class Method:
return spec
class PauseAt:
class _Action:
"""
An action that operates on the raw data stream of the message. All
actions have one thing in common: an offset that specifies where the
action should take place.
"""
def __init__(self, offset):
self.offset = offset
def __cmp__(self, other):
return cmp(self.offset, other.offset)
class PauseAt(_Action):
def __init__(self, offset, seconds):
self.offset, self.seconds = offset, seconds
_Action.__init__(self, offset)
self.seconds = seconds
@classmethod
def expr(klass):
@ -422,12 +436,12 @@ class PauseAt:
r.actions.append((self.offset, "pause", self.seconds))
class DisconnectAt:
def __init__(self, value):
self.value = value
class DisconnectAt(_Action):
def __init__(self, offset):
_Action.__init__(self, offset)
def accept(self, settings, r):
r.actions.append((self.value, "disconnect"))
r.actions.append((self.offset, "disconnect"))
@classmethod
def expr(klass):
@ -436,9 +450,10 @@ class DisconnectAt:
return e.setParseAction(lambda x: klass(*x))
class InjectAt:
class InjectAt(_Action):
def __init__(self, offset, value):
self.offset, self.value = offset, value
_Action.__init__(self, offset)
self.value = value
@classmethod
def expr(klass):

View File

@ -168,6 +168,18 @@ class TestMisc:
s.serve(d)
class Test_Action:
def test_cmp(self):
a = language._Action(0)
b = language._Action(1)
c = language._Action(0)
assert a < b
assert a == c
l = [b, a]
l.sort()
assert l[0].offset == 0
class TestDisconnects:
def test_parse_response(self):
assert (0, "disconnect") in language.parse_response({}, "400:d0").actions
@ -177,14 +189,14 @@ class TestDisconnects:
e = language.DisconnectAt.expr()
v = e.parseString("d0")[0]
assert isinstance(v, language.DisconnectAt)
assert v.value == 0
assert v.offset == 0
v = e.parseString("d100")[0]
assert v.value == 100
assert v.offset == 100
e = language.DisconnectAt.expr()
v = e.parseString("dr")[0]
assert v.value == "r"
assert v.offset == "r"
class TestInject: