1import sys 2import unittest 3 4import antlr3 5 6 7class 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 20class 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 54class 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 66if __name__ == "__main__": 67 unittest.main(testRunner=unittest.TextTestRunner(verbosity=2)) 68