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