|
| 1 | +import unittest |
| 2 | + |
| 3 | +from ometa.grammar import OMeta |
| 4 | +from ometa.protocol import ParserProtocol |
| 5 | + |
| 6 | + |
| 7 | +testingGrammarSource = """ |
| 8 | +
|
| 9 | +someA = ('a' 'a') -> state('a') |
| 10 | +someB = ('b' 'b') -> state('b') |
| 11 | +
|
| 12 | +initial = someA |
| 13 | +
|
| 14 | +""" |
| 15 | +testGrammar = OMeta(testingGrammarSource).parseGrammar('testGrammar') |
| 16 | + |
| 17 | + |
| 18 | +class SenderFactory(object): |
| 19 | + def __init__(self, transport): |
| 20 | + self.transport = transport |
| 21 | + |
| 22 | + |
| 23 | +class StateFactory(object): |
| 24 | + def __init__(self, sender): |
| 25 | + self.sender = sender |
| 26 | + self.calls = [] |
| 27 | + self.returnMap = {} |
| 28 | + self.lossReason = None |
| 29 | + |
| 30 | + def __call__(self, v): |
| 31 | + self.calls.append(v) |
| 32 | + return self.returnMap.get(v) |
| 33 | + |
| 34 | + def connectionLost(self, reason): |
| 35 | + self.lossReason = reason |
| 36 | + |
| 37 | + |
| 38 | +class ParserProtocolTestCase(unittest.TestCase): |
| 39 | + def setUp(self): |
| 40 | + self.protocol = ParserProtocol( |
| 41 | + testGrammar, SenderFactory, StateFactory, {}) |
| 42 | + |
| 43 | + def test_transportPassed(self): |
| 44 | + """The sender is passed the transport recieved by the protocol.""" |
| 45 | + transport = object() |
| 46 | + self.protocol.makeConnection(transport) |
| 47 | + self.assertEqual(transport, self.protocol.sender.transport) |
| 48 | + |
| 49 | + def test_senderPassed(self): |
| 50 | + """The sender is passed to the state.""" |
| 51 | + self.protocol.makeConnection(None) |
| 52 | + self.assertEqual(self.protocol.sender, self.protocol.state.sender) |
| 53 | + |
| 54 | + def test_basicParsing(self): |
| 55 | + """Rules can be parsed multiple times for the same effect.""" |
| 56 | + self.protocol.makeConnection(None) |
| 57 | + self.protocol.dataReceived('aa') |
| 58 | + self.assertEqual(self.protocol.state.calls, ['a']) |
| 59 | + self.protocol.dataReceived('aa') |
| 60 | + self.assertEqual(self.protocol.state.calls, ['a', 'a']) |
| 61 | + |
| 62 | + def test_parsingChunks(self): |
| 63 | + """Any number of rules can be called from one dataRecived.""" |
| 64 | + self.protocol.makeConnection(None) |
| 65 | + self.protocol.dataReceived('a') |
| 66 | + self.assertEqual(self.protocol.state.calls, []) |
| 67 | + self.protocol.dataReceived('aa') |
| 68 | + self.assertEqual(self.protocol.state.calls, ['a']) |
| 69 | + self.protocol.dataReceived('aaa') |
| 70 | + self.assertEqual(self.protocol.state.calls, ['a', 'a', 'a']) |
| 71 | + |
| 72 | + def test_ruleSwitching(self): |
| 73 | + """The rule being parsed can specify the next rule to be parsed.""" |
| 74 | + self.protocol.makeConnection(None) |
| 75 | + self.protocol.state.returnMap.update(dict(a='someB', b='someA')) |
| 76 | + self.protocol.dataReceived('aa') |
| 77 | + self.assertEqual(self.protocol.state.calls, ['a']) |
| 78 | + self.protocol.dataReceived('bb') |
| 79 | + self.assertEqual(self.protocol.state.calls, ['a', 'b']) |
| 80 | + self.protocol.dataReceived('aa') |
| 81 | + self.assertEqual(self.protocol.state.calls, ['a', 'b', 'a']) |
| 82 | + |
| 83 | + def test_ruleSwitchingWithChunks(self): |
| 84 | + """Any number of rules can be called even during rule switching.""" |
| 85 | + self.protocol.makeConnection(None) |
| 86 | + self.protocol.state.returnMap.update(dict(a='someB', b='someA')) |
| 87 | + self.protocol.dataReceived('a') |
| 88 | + self.assertEqual(self.protocol.state.calls, []) |
| 89 | + self.protocol.dataReceived('ab') |
| 90 | + self.assertEqual(self.protocol.state.calls, ['a']) |
| 91 | + self.protocol.dataReceived('baa') |
| 92 | + self.assertEqual(self.protocol.state.calls, ['a', 'b', 'a']) |
| 93 | + |
| 94 | + def test_connectionLoss(self): |
| 95 | + """The reason for connection loss is forwarded to the state.""" |
| 96 | + self.protocol.makeConnection(None) |
| 97 | + reason = object() |
| 98 | + self.protocol.connectionLost(reason) |
| 99 | + self.assertEqual(self.protocol.state.lossReason, reason) |
0 commit comments