1import unittest 2from simpleparse.parser import Parser 3from simpleparse.common import strings 4from simpleparse import dispatchprocessor 5 6 7parseTests = [ 8 # each production should match the whole of all of the first, 9 # and not match any of the second... 10 ("string_triple_single", [ 11 """'''this and that'''""", 12 """'''this \\''' '''""", 13 """''''''""", 14 """''''\\''''""", 15 ],[]), 16 ("string_triple_double", [ 17 '''"""this and that"""''', 18 '''"""this \\""" """''', 19 '''""""""''', 20 '''""""\\""""''', 21 ],[]), 22 ("string_double_quote", [ 23 '"\\p"', 24 '"\\""', 25 ],[]), 26 ("string",[ 27 "'this'", 28 '"that"', 29 r'"\b\f\n\r"', 30 r'"\x32\xff\xcf"', 31 r'"\032\033\055\077"', 32 r'"\t\v\\\a\b\f\n\r"', 33 r'"\t"', 34 r'"\v"', 35 r'"\""', 36 ], []), 37] 38 39 40class CommonTests(unittest.TestCase): 41 def testBasic( self ): 42 proc = dispatchprocessor.DispatchProcessor() 43 setattr(proc, "string", strings.StringInterpreter()) 44 for production, yestable, notable in parseTests: 45 p = Parser( "x := %s"%production, 'x') 46 for data in yestable: 47 if production == 'string': 48 success, results, next = p.parse( data, processor=proc) 49 else: 50 success, results, next = p.parse( data) 51 assert success and (next == len(data)), """Did not parse string %s as a %s result=%s"""%( repr(data), production, (success, results, next)) 52 assert results, """Didn't get any results for string %s as a %s result=%s"""%( repr(data), production, (success, results, next)) 53 if production == 'string': 54 expected = eval( data, {},{}) 55 assert results[0] == expected, """Got different interpreted value for data %s, we got %s, expected %s"""%( repr(data), repr(results[0]), repr(expected)) 56 for data in notable: 57 success, results, next = p.parse( data) 58 assert not success, """Parsed %s of %s as a %s result=%s"""%( repr(data), production, (success, results, next)) 59 60 61 62def getSuite(): 63 return unittest.makeSuite(CommonTests, 'test') 64 65if __name__ == "__main__": 66 unittest.main(defaultTest="getSuite") 67