• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import antlr3
2import testbase
3import unittest
4import os
5import sys
6from io import StringIO
7import textwrap
8
9class t012lexerXML(testbase.ANTLRTest):
10    def setUp(self):
11        self.compileGrammar('t012lexerXMLLexer.g')
12
13
14    def lexerClass(self, base):
15        class TLexer(base):
16            def emitErrorMessage(self, msg):
17                # report errors to /dev/null
18                pass
19
20            def reportError(self, re):
21                # no error recovery yet, just crash!
22                raise re
23
24        return TLexer
25
26
27    def testValid(self):
28        inputPath = os.path.splitext(__file__)[0] + '.input'
29        with open(inputPath) as f:
30            data = f.read()
31        stream = antlr3.StringStream(data)
32        lexer = self.getLexer(stream)
33
34        while True:
35            token = lexer.nextToken()
36            if token.type == self.lexerModule.EOF:
37                break
38
39
40        output = lexer.outbuf.getvalue()
41
42        outputPath = os.path.splitext(__file__)[0] + '.output'
43
44        with open(outputPath) as f:
45            testOutput = f.read()
46
47        self.assertEqual(output, testOutput)
48
49
50    def testMalformedInput1(self):
51        input = textwrap.dedent("""\
52        <?xml version='1.0'?>
53        <document d>
54        </document>
55        """)
56
57        stream = antlr3.StringStream(input)
58        lexer = self.getLexer(stream)
59
60        try:
61            while True:
62                token = lexer.nextToken()
63                # Should raise NoViableAltException before hitting EOF
64                if token.type == antlr3.EOF:
65                    self.fail()
66
67        except antlr3.NoViableAltException as exc:
68            self.assertEqual(exc.unexpectedType, '>')
69            self.assertEqual(exc.charPositionInLine, 11)
70            self.assertEqual(exc.line, 2)
71
72
73    def testMalformedInput2(self):
74        input = textwrap.dedent("""\
75        <?tml version='1.0'?>
76        <document>
77        </document>
78        """)
79
80        stream = antlr3.StringStream(input)
81        lexer = self.getLexer(stream)
82
83        try:
84            while True:
85                token = lexer.nextToken()
86                # Should raise NoViableAltException before hitting EOF
87                if token.type == antlr3.EOF:
88                    self.fail()
89
90        except antlr3.MismatchedSetException as exc:
91            self.assertEqual(exc.unexpectedType, 't')
92            self.assertEqual(exc.charPositionInLine, 2)
93            self.assertEqual(exc.line, 1)
94
95
96    def testMalformedInput3(self):
97        input = textwrap.dedent("""\
98        <?xml version='1.0'?>
99        <docu ment attr="foo">
100        </document>
101        """)
102
103        stream = antlr3.StringStream(input)
104        lexer = self.getLexer(stream)
105
106        try:
107            while True:
108                token = lexer.nextToken()
109                # Should raise NoViableAltException before hitting EOF
110                if token.type == antlr3.EOF:
111                    self.fail()
112
113        except antlr3.NoViableAltException as exc:
114            self.assertEqual(exc.unexpectedType, 'a')
115            self.assertEqual(exc.charPositionInLine, 11)
116            self.assertEqual(exc.line, 2)
117
118
119if __name__ == '__main__':
120    unittest.main()
121