aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libpathod/language.py31
-rw-r--r--test/test_language.py18
2 files changed, 38 insertions, 11 deletions
diff --git a/libpathod/language.py b/libpathod/language.py
index 960e9d17..1445ca82 100644
--- a/libpathod/language.py
+++ b/libpathod/language.py
@@ -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):
diff --git a/test/test_language.py b/test/test_language.py
index 0668fba2..7680492f 100644
--- a/test/test_language.py
+++ b/test/test_language.py
@@ -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: