1import os 2import sys 3import antlr3 4import testbase 5import unittest 6from cStringIO import StringIO 7import difflib 8 9class t020fuzzy(testbase.ANTLRTest): 10 def setUp(self): 11 self.compileGrammar('t020fuzzyLexer.g') 12 13 14 def testValid(self): 15 inputPath = os.path.splitext(__file__)[0] + '.input' 16 stream = antlr3.StringStream(open(inputPath).read()) 17 lexer = self.getLexer(stream) 18 19 while True: 20 token = lexer.nextToken() 21 if token.type == antlr3.EOF: 22 break 23 24 25 output = lexer.output.getvalue() 26 27 outputPath = os.path.splitext(__file__)[0] + '.output' 28 testOutput = open(outputPath).read() 29 30 success = (output == testOutput) 31 if not success: 32 d = difflib.Differ() 33 r = d.compare(output.splitlines(1), testOutput.splitlines(1)) 34 self.fail( 35 ''.join([l.encode('ascii', 'backslashreplace') for l in r]) 36 ) 37 38 39if __name__ == '__main__': 40 unittest.main() 41