1import unittest, string 2from simpleparse.parser import Parser 3from simpleparse.common import chartypes, timezone_names 4assert chartypes 5from simpleparse import dispatchprocessor 6 7try: 8 fulltrans = string.maketrans(b"",b"") 9 translate = string.translate 10except AttributeError: 11 fulltrans = bytes.maketrans(b"",b"") 12 translate = bytes.translate 13 14class CommonTests(unittest.TestCase): 15 def doBasicTest(self, definition, parserName, testValue, expected, ): 16 result = Parser( definition).parse( testValue, parserName ) 17 assert result == expected, '''\nexpected:%s\n got:%s\n'''%( expected, result ) 18 def _testSet( self, set, singleName, multiName ): 19 """Test multi-line definitions""" 20 decl = """single := %s multiple := %s"""%( singleName, multiName ) 21 p = Parser(decl) 22 notset = translate( fulltrans, fulltrans, set ) 23 for char in set: 24 if isinstance(char,int): 25 char = chr(char) 26 success, children, next = p.parse( char, singleName) 27 assert success and (next == 1), """Parser for %s couldn't parse %s"""%( singleName, char ) 28 for char in notset: 29 if isinstance(char,int): 30 char = chr(char) 31 success, children, next = p.parse( char, singleName) 32 assert (not success) and (next == 0), """Parser for %s parsed %s"""%( singleName, char ) 33 success, children, next = p.parse( char, multiName) 34 assert (not success) and (next == 0), """Parser for %s parsed %s"""%( multiName, char ) 35 success, children, next = p.parse( set, multiName) 36 assert success and (next == len(set)), """Parser for %s couldn't parse full set of chars, failed at %s"""%( multiName, set[next:] ) 37 def testBasic( self ): 38 for set, single, multiple in ( 39 ("digits", "digit", "digits"), 40 ("ascii_uppercase", "uppercasechar", "uppercase"), 41 ("ascii_lowercase", "lowercasechar", "lowercase"), 42 ("ascii_letters", "letter", "letters"), 43 ("whitespace", "whitespacechar", "whitespace"), 44 ("octdigits", "octdigit", "octdigits"), 45 ("hexdigits", "hexdigit", "hexdigits"), 46 ("printable", "printablechar", "printable"), 47 ("punctuation", "punctuationchar", "punctuation"), 48 49 ("ascii_lowercase", "ascii_lowercasechar", "ascii_lowercase"), 50 ("ascii_uppercase", "ascii_uppercasechar", "ascii_uppercase"), 51 ): 52 try: 53 set = getattr( string, set) 54 self._testSet( 55 set.encode('ascii'), 56 single, 57 multiple, 58 ) 59 except AttributeError: 60 raise 61 except TypeError as err: 62 err.args += (set,single,multiple) 63 raise 64 def testEOF( self ): 65 p = Parser( """this := 'a',EOF""", 'this') 66 success, children, next = p.parse( 'a' ) 67 assert success, """EOF didn't match at end of string""" 68 def testEOFFail( self ): 69 p = Parser( """this := 'a',EOF""", 'this') 70 success, children, next = p.parse( 'a ' ) 71 assert not success, """EOF matched before end of string""" 72 73 def testTZ( self ): 74 names = list(timezone_names.timezone_mapping.keys()) 75 names.sort() # tests that the items don't match shorter versions... 76 decl = Parser("""this := (timezone_name, ' '?)+""", 'this') 77 proc = dispatchprocessor.DispatchProcessor() 78 proc.timezone_name = timezone_names.TimeZoneNameInterpreter() 79 text = ' '.join(names) 80 success, result, next = decl.parse( text, processor = proc ) 81 assert success, """Unable to complete parsing the timezone names, stopped parsing at char %s %s"""%(next, text[next:]) 82 assert result == list(map( timezone_names.timezone_mapping.get, names)), """Got different results for interpretation than expected (expected first, recieved second)\n%s\n%s"""%(list(map( timezone_names.timezone_mapping.get, names)), result) 83 84 85 86 87def getSuite(): 88 return unittest.makeSuite(CommonTests, 'test') 89 90if __name__ == "__main__": 91 unittest.main(defaultTest="getSuite") 92