1import antlr3
2import testbase
3import unittest
4
5class t003lexer(testbase.ANTLRTest):
6    def setUp(self):
7        self.compileGrammar()
8
9
10    def lexerClass(self, base):
11        class TLexer(base):
12            def emitErrorMessage(self, msg):
13                # report errors to /dev/null
14                pass
15
16            def reportError(self, re):
17                # no error recovery yet, just crash!
18                raise re
19
20        return TLexer
21
22
23    def testValid(self):
24        stream = antlr3.StringStream('0fooze1')
25        lexer = self.getLexer(stream)
26
27        token = lexer.nextToken()
28        self.failUnlessEqual(token.type, self.lexerModule.ZERO)
29
30        token = lexer.nextToken()
31        self.failUnlessEqual(token.type, self.lexerModule.FOOZE)
32
33        token = lexer.nextToken()
34        self.failUnlessEqual(token.type, self.lexerModule.ONE)
35
36        token = lexer.nextToken()
37        self.failUnlessEqual(token.type, self.lexerModule.EOF)
38
39
40    def testMalformedInput(self):
41        stream = antlr3.StringStream('2')
42        lexer = self.getLexer(stream)
43
44        try:
45            token = lexer.nextToken()
46            self.fail()
47
48        except antlr3.NoViableAltException, exc:
49            self.failUnlessEqual(exc.unexpectedType, '2')
50
51
52if __name__ == '__main__':
53    unittest.main()
54