Home | History | Annotate | Download | only in unittests
      1 import sys
      2 import unittest
      3 
      4 import antlr3
      5 
      6 
      7 class TestBaseRecognizer(unittest.TestCase):
      8     """Tests for BaseRecognizer class"""
      9 
     10     def testGetRuleInvocationStack(self):
     11         """BaseRecognizer._getRuleInvocationStack()"""
     12 
     13         rules = antlr3.BaseRecognizer._getRuleInvocationStack(__name__)
     14         self.failUnlessEqual(
     15             rules,
     16             ['testGetRuleInvocationStack']
     17             )
     18 
     19 
     20 class TestTokenSource(unittest.TestCase):
     21     """Testcase to the antlr3.TokenSource class"""
     22 
     23 
     24     def testIteratorInterface(self):
     25         """TokenSource.next()"""
     26 
     27         class TrivialToken(object):
     28             def __init__(self, type):
     29                 self.type = type
     30 
     31         class TestSource(antlr3.TokenSource):
     32             def __init__(self):
     33                 self.tokens = [
     34                     TrivialToken(1),
     35                     TrivialToken(2),
     36                     TrivialToken(3),
     37                     TrivialToken(4),
     38                     TrivialToken(antlr3.EOF),
     39                     ]
     40 
     41             def nextToken(self):
     42                 return self.tokens.pop(0)
     43 
     44 
     45         src = TestSource()
     46         tokens = []
     47         for token in src:
     48             tokens.append(token.type)
     49 
     50         self.failUnlessEqual(tokens, [1, 2, 3, 4])
     51 
     52 
     53 
     54 class TestLexer(unittest.TestCase):
     55 
     56     def testInit(self):
     57         """Lexer.__init__()"""
     58 
     59         class TLexer(antlr3.Lexer):
     60             api_version = 'HEAD'
     61 
     62         stream = antlr3.StringStream('foo')
     63         TLexer(stream)
     64 
     65 
     66 if __name__ == "__main__":
     67     unittest.main(testRunner=unittest.TextTestRunner(verbosity=2))
     68