Skip to content

Commit 49e099a

Browse files
committed
Adding various new things to ParserProtocol.
The state now has a connectionMade method which is called immediately (but makes testing states easier), and is passed the ParserProtocol. ParserProtocol.setNextRule was added, which lets the state specify what the next rule should be.
1 parent 2f8ef27 commit 49e099a

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

ometa/protocol.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ def __init__(self, grammar, senderFactory, stateFactory, bindings):
1111
self.senderFactory = senderFactory
1212
self.stateFactory = stateFactory
1313

14+
def setNextRule(self, rule):
15+
self.currentRule = rule
16+
1417
def connectionMade(self):
1518
self.sender = self.senderFactory(self.transport)
16-
self.bindings['state'] = self.state = self.stateFactory(self.sender)
19+
self.bindings['state'] = self.state = self.stateFactory(self.sender, self)
20+
self.state.connectionMade()
1721
self._setupInterp()
1822

1923
def _setupInterp(self):

ometa/test/test_protocol.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@ def __init__(self, transport):
2121

2222

2323
class StateFactory(object):
24-
def __init__(self, sender):
24+
def __init__(self, sender, parser):
2525
self.sender = sender
26+
self.parser = parser
2627
self.calls = []
2728
self.returnMap = {}
29+
self.connected = False
2830
self.lossReason = None
2931

32+
def connectionMade(self):
33+
self.connected = True
34+
3035
def __call__(self, v):
3136
self.calls.append(v)
3237
return self.returnMap.get(v)
@@ -46,11 +51,22 @@ def test_transportPassed(self):
4651
self.protocol.makeConnection(transport)
4752
self.assertEqual(transport, self.protocol.sender.transport)
4853

54+
def test_parserPassed(self):
55+
"""The protocol is passed to the state."""
56+
transport = object()
57+
self.protocol.makeConnection(transport)
58+
self.assertEqual(self.protocol, self.protocol.state.parser)
59+
4960
def test_senderPassed(self):
5061
"""The sender is passed to the state."""
5162
self.protocol.makeConnection(None)
5263
self.assertEqual(self.protocol.sender, self.protocol.state.sender)
5364

65+
def test_connectionEstablishes(self):
66+
"""connectionMade is called on the state after connection establishment."""
67+
self.protocol.makeConnection(None)
68+
self.assert_(self.protocol.state.connected)
69+
5470
def test_basicParsing(self):
5571
"""Rules can be parsed multiple times for the same effect."""
5672
self.protocol.makeConnection(None)
@@ -91,6 +107,20 @@ def test_ruleSwitchingWithChunks(self):
91107
self.protocol.dataReceived('baa')
92108
self.assertEqual(self.protocol.state.calls, ['a', 'b', 'a'])
93109

110+
def test_ruleSwitchingViaState(self):
111+
"""
112+
The state is able to set the the next rule to be parsed with the parser
113+
passed to it.
114+
"""
115+
self.protocol.makeConnection(None)
116+
self.protocol.dataReceived('aa')
117+
self.assertEqual(self.protocol.state.calls, ['a'])
118+
self.protocol.dataReceived('a')
119+
self.assertEqual(self.protocol.state.calls, ['a'])
120+
self.protocol.state.parser.setNextRule('someB')
121+
self.protocol.dataReceived('abb')
122+
self.assertEqual(self.protocol.state.calls, ['a', 'a', 'b'])
123+
94124
def test_connectionLoss(self):
95125
"""The reason for connection loss is forwarded to the state."""
96126
self.protocol.makeConnection(None)

0 commit comments

Comments
 (0)