|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +from twisted.trial import unittest |
| 4 | +from twisted.python.compat import iterbytes |
| 5 | + |
| 6 | + |
| 7 | +from ometa.grammar import OMeta |
| 8 | +from ometa.tube import TrampolinedParser |
| 9 | + |
| 10 | + |
| 11 | +class TrampolinedReceiver(): |
| 12 | + """ |
| 13 | + Receive and store the passed in data. |
| 14 | + """ |
| 15 | + def __init__(self): |
| 16 | + self.received = [] |
| 17 | + |
| 18 | + def receive(self, data): |
| 19 | + self.received.append(data) |
| 20 | + |
| 21 | + |
| 22 | +class TrampolinedParserTestCase(unittest.SynchronousTestCase): |
| 23 | + """ |
| 24 | + Tests for L{ometa.tube.TrampolinedParser} |
| 25 | + """ |
| 26 | + |
| 27 | + def _parseGrammar(self, grammar, name="Grammar"): |
| 28 | + return OMeta(grammar).parseGrammar(name) |
| 29 | + |
| 30 | + def setUp(self): |
| 31 | + _grammar = r""" |
| 32 | + delimiter = '\r\n' |
| 33 | + initial = <(~delimiter anything)*>:val delimiter -> receiver.receive(val) |
| 34 | + """ |
| 35 | + self.grammar = self._parseGrammar(_grammar) |
| 36 | + |
| 37 | + def test_dataNotFullyReceived(self): |
| 38 | + """ |
| 39 | + Since the initial rule inside the grammar is not matched, the receiver |
| 40 | + shouldn't receive any byte. |
| 41 | + """ |
| 42 | + receiver = TrampolinedReceiver() |
| 43 | + trampolinedParser = TrampolinedParser(self.grammar, receiver, {}) |
| 44 | + buf = b'foobarandnotreachdelimiter' |
| 45 | + for c in iterbytes(buf): |
| 46 | + trampolinedParser.receive(c) |
| 47 | + self.assertEqual(receiver.received, []) |
| 48 | + |
| 49 | + |
| 50 | + def test_dataFullyReceived(self): |
| 51 | + """ |
| 52 | + The receiver should receive the data according to the grammar. |
| 53 | + """ |
| 54 | + receiver = TrampolinedReceiver() |
| 55 | + trampolinedParser = TrampolinedParser(self.grammar, receiver, {}) |
| 56 | + buf = b'\r\n'.join((b'foo', b'bar', b'foo', b'bar')) |
| 57 | + for c in iterbytes(buf): |
| 58 | + trampolinedParser.receive(c) |
| 59 | + self.assertEqual(receiver.received, [b'foo', b'bar', b'foo']) |
| 60 | + trampolinedParser.receive('\r\n') |
| 61 | + self.assertEqual(receiver.received, [b'foo', b'bar', b'foo', b'bar']) |
| 62 | + |
| 63 | + |
| 64 | + def test_bindings(self): |
| 65 | + """ |
| 66 | + The passed-in bindings should be accessible inside the grammar. |
| 67 | + """ |
| 68 | + receiver = TrampolinedReceiver() |
| 69 | + grammar = r""" |
| 70 | + initial = digit:d (-> int(d)+SMALL_INT):val -> receiver.receive(val) |
| 71 | + """ |
| 72 | + bindings = {'SMALL_INT': 3} |
| 73 | + TrampolinedParser(self._parseGrammar(grammar), receiver, bindings).receive('0') |
| 74 | + self.assertEqual(receiver.received, [3]) |
0 commit comments