1import antlr3
2import testbase
3import unittest
4import os
5import sys
6from cStringIO import StringIO
7import difflib
8
9class t018llstar(testbase.ANTLRTest):
10    def setUp(self):
11        self.compileGrammar()
12
13
14    def testValid(self):
15        inputPath = os.path.splitext(__file__)[0] + '.input'
16        cStream = antlr3.StringStream(open(inputPath).read())
17        lexer = self.getLexer(cStream)
18        tStream = antlr3.CommonTokenStream(lexer)
19        parser = self.getParser(tStream)
20        parser.program()
21
22        output = parser.output.getvalue()
23
24        outputPath = os.path.splitext(__file__)[0] + '.output'
25        testOutput = open(outputPath).read()
26
27        success = (output == testOutput)
28        if not success:
29            d = difflib.Differ()
30            r = d.compare(output.splitlines(1), testOutput.splitlines(1))
31            self.fail(
32                ''.join([l.encode('ascii', 'backslashreplace') for l in r])
33                )
34
35if __name__ == '__main__':
36    unittest.main()
37
38
39
40## # run an infinite loop with randomly mangled input
41## while True:
42##     print "ping"
43
44##     input = open(inputPath).read()
45
46##     import random
47##     input = list(input) # make it mutable
48##     for _ in range(3):
49##         p1 = random.randrange(len(input))
50##         p2 = random.randrange(len(input))
51
52##         c1 = input[p1]
53##         input[p1] = input[p2]
54##         input[p2] = c1
55##     input = ''.join(input) # back to string
56
57
58##     try:
59##         cStream = antlr3.StringStream(input)
60##         lexer = Lexer(cStream)
61##         tStream = antlr3.CommonTokenStream(lexer)
62##         parser = TestParser(tStream)
63##         parser.program()
64
65##     except antlr3.RecognitionException, exc:
66##         print exc
67##         for l in input.splitlines()[0:exc.line]:
68##             print l
69##         print ' '*exc.charPositionInLine + '^'
70
71##     except BaseException, exc:
72##         print '\n'.join(['%02d: %s' % (idx+1, l) for idx, l in enumerate(input.splitlines())])
73##         print "%s at %d:%d" % (exc, cStream.line, cStream.charPositionInLine)
74##         print
75
76##         raise
77