def setUp(self): self.p = PySourceAsText()
def old_parser(filename): from generate_code.gen_asciiart import PySourceAsText p = PySourceAsText() p.Parse(filename) return p, ''
def testNestedClasses01(self): """ class ParseMeTest: a = 100 class A: pass def __init__(self): self.b = [] class B: ba = 99 class C: def __init__(self): self.cc = 88 def Hi(self): pass class D: pass """ """ result should be ParseMeTest: ParseMeTest_A: ParseMeTest_B: ParseMeTest_B_C: D: """ self.p = PySourceAsText( ast=False) # override the setUp() so we force using the old parser FILE = PYTHON_CODE_EXAMPLES_TO_PARSE + 'testmodule05.py' self.p.Parse(FILE) gotevent1 = 0 gotevent2 = 0 gotevent3 = 0 gotevent4 = 0 gotevent5 = 0 gotevent6 = 0 gotevent7 = 0 for classname, classentry in self.p.classlist.items(): if classname == 'ParseMeTest': gotevent1 = 1 assert classentry.classesinheritsfrom == [] assert len(classentry.attrs) == 2 for attrobj in classentry.attrs: attrname = attrobj.attrname if attrname == 'a': gotevent2 = 1 if attrname == 'b': gotevent3 = 1 for adef in classentry.defs: pass assert len(classentry.defs) == 2 assert '__init__' in classentry.defs assert 'Hi' in classentry.defs if classname == 'ParseMeTest_A': gotevent4 = 1 assert len(classentry.attrs) == 0 assert len(classentry.defs) == 0 if classname == 'ParseMeTest_B': gotevent5 = 1 assert len(classentry.attrs) == 1 assert len(classentry.defs) == 0 if classname == 'ParseMeTest_B_C': gotevent6 = 1 assert len(classentry.attrs) == 1 assert len(classentry.defs) == 1 for attrobj in classentry.attrs: attrname = attrobj.attrname assert attrname == 'cc' if classname == 'D': gotevent7 = 1 assert len(classentry.attrs) == 0 assert len(classentry.defs) == 0 assert gotevent1 assert gotevent2 assert gotevent3 assert gotevent4 assert gotevent5 assert gotevent6 assert gotevent7
def testModuleVarsAndFunctions01(self): """ class ParseMeTest: def __init__(self): self.a = None self.b = None x=20 class ParseMeTest2: pass def y(): pass class ParseMeTest3: pass if __name__ == '__main__': y() class ParseMeTest4: pass """ self.p = PySourceAsText( ast=False) # override the setUp() so we force using the old parser CHECK_MODULE_LEVEL_PARSING = 1 FILE = PYTHON_CODE_EXAMPLES_TO_PARSE + "testmodule03.py" self.p.optionModuleAsClass = CHECK_MODULE_LEVEL_PARSING self.p.Parse(FILE) gotevent1 = 0 gotevent2 = 0 gotevent3 = 0 gotevent4 = 0 gotevent5 = 0 gotevent6 = 0 gotevent7 = 0 gotevent8 = 0 for classname, classentry in list(self.p.classlist.items()): if classname == "ParseMeTest": gotevent1 = 1 assert classentry.classesinheritsfrom == [] assert len(classentry.attrs) == 2 for attrobj in classentry.attrs: attrname = attrobj.attrname if attrname == "a": gotevent2 = 1 if attrname == "b": gotevent3 = 1 for adef in classentry.defs: pass assert len(classentry.defs) == 1 assert "__init__" in classentry.defs if classname == "ParseMeTest2": gotevent4 = 1 assert len(classentry.attrs) == 0 assert len(classentry.defs) == 0 if classname == "ParseMeTest3": gotevent5 = 1 assert len(classentry.attrs) == 0 assert len(classentry.defs) == 0 if classname == "ParseMeTest4": gotevent6 = 1 assert len(classentry.attrs) == 0 assert len(classentry.defs) == 0 # module level extra logic if CHECK_MODULE_LEVEL_PARSING: if classname == "Module_" + os.path.splitext( os.path.basename(FILE))[0]: gotevent7 = 1 assert classentry.classesinheritsfrom == [] for adef in classentry.defs: pass assert len(classentry.defs) == 1 assert "y" in classentry.defs assert len(classentry.attrs) == 1 for attrobj in classentry.attrs: attrname = attrobj.attrname if attrname == "x": gotevent8 = 1 assert gotevent1 assert gotevent2 assert gotevent3 assert gotevent4 assert gotevent5 assert gotevent6 if CHECK_MODULE_LEVEL_PARSING: assert gotevent7 assert gotevent8
def testModuleVarsAndFunctions02(self): """ class ParseMeTest: a = 100 b = [1,2,3] def __init__(self): self.c = [] self.d = [1,2,3] self.e = (1,2,3) self.f = (1*10) x=[1,2] y=[] """ self.p = PySourceAsText( ast=False) # override the setUp() so we force using the old parser CHECK_MODULE_LEVEL_PARSING = 1 FILE = PYTHON_CODE_EXAMPLES_TO_PARSE + "testmodule04.py" self.p.optionModuleAsClass = CHECK_MODULE_LEVEL_PARSING self.p.Parse(FILE) gotevent1 = 0 gotevent2 = 0 gotevent3 = 0 gotevent4 = 0 gotevent5 = 0 gotevent6 = 0 gotevent7 = 0 gotevent8 = 0 gotevent9 = 0 for classname, classentry in list(self.p.classlist.items()): if classname == "ParseMeTest": gotevent1 = 1 assert classentry.classesinheritsfrom == [] assert classentry.ismodulenotrealclass == 0 for attrobj in classentry.attrs: attrname = attrobj.attrname # print attrname if attrname == "a": gotevent2 = 1 assert "static" in attrobj.attrtype if attrname == "b": gotevent3 = 1 assert "static" in attrobj.attrtype if attrname == "c": gotevent4 = 1 assert "static" not in attrobj.attrtype if attrname == "d": gotevent3 = 1 assert "static" not in attrobj.attrtype if attrname == "e": gotevent5 = 1 assert "static" not in attrobj.attrtype if attrname == "f": gotevent6 = 1 assert "static" not in attrobj.attrtype assert len( classentry.attrs) == 6, ("Only got " + repr(len(classentry.attrs)) + " attributes") for adef in classentry.defs: pass assert len(classentry.defs) == 1 assert "__init__" in classentry.defs # module level extra logic if CHECK_MODULE_LEVEL_PARSING: if classname == "Module_" + os.path.splitext( os.path.basename(FILE))[0]: gotevent7 = 1 assert classentry.classesinheritsfrom == [] assert classentry.ismodulenotrealclass for adef in classentry.defs: pass assert len(classentry.defs) == 0 for attrobj in classentry.attrs: attrname = attrobj.attrname # print 'attr', attrname if attrname == "x": gotevent8 = 1 if attrname == "y": gotevent9 = 1 assert len( classentry.attrs) == 2, ("Only got " + repr(len(classentry.attrs)) + " attributes") assert gotevent1 assert gotevent2 assert gotevent3 assert gotevent4 assert gotevent5 assert gotevent6 if CHECK_MODULE_LEVEL_PARSING: assert gotevent7 assert gotevent8 assert gotevent9
def testModuleVarsAndFunctions02(self): """ class ParseMeTest: a = 100 b = [1,2,3] def __init__(self): self.c = [] self.d = [1,2,3] self.e = (1,2,3) self.f = (1*10) x=[1,2] y=[] """ self.p = PySourceAsText(ast=False) # override the setUp() so we force using the old parser CHECK_MODULE_LEVEL_PARSING = 1 FILE = PYTHON_CODE_EXAMPLES_TO_PARSE + 'testmodule04.py' self.p.optionModuleAsClass = CHECK_MODULE_LEVEL_PARSING self.p.Parse(FILE) gotevent1 = 0 gotevent2 = 0 gotevent3 = 0 gotevent4 = 0 gotevent5 = 0 gotevent6 = 0 gotevent7 = 0 gotevent8 = 0 gotevent9 = 0 for classname, classentry in self.p.classlist.items(): if classname == 'ParseMeTest': gotevent1 = 1 assert classentry.classesinheritsfrom == [] assert classentry.ismodulenotrealclass == 0 for attrobj in classentry.attrs: attrname = attrobj.attrname #print attrname if attrname == 'a': gotevent2 = 1 assert 'static' in attrobj.attrtype if attrname == 'b': gotevent3 = 1 assert 'static' in attrobj.attrtype if attrname == 'c': gotevent4 = 1 assert 'static' not in attrobj.attrtype if attrname == 'd': gotevent3 = 1 assert 'static' not in attrobj.attrtype if attrname == 'e': gotevent5 = 1 assert 'static' not in attrobj.attrtype if attrname == 'f': gotevent6 = 1 assert 'static' not in attrobj.attrtype assert len(classentry.attrs) == 6, 'Only got ' + `len(classentry.attrs)` + ' attributes' for adef in classentry.defs: pass assert len(classentry.defs) == 1 assert '__init__' in classentry.defs # module level extra logic if CHECK_MODULE_LEVEL_PARSING: if classname == 'Module_'+os.path.splitext(os.path.basename(FILE))[0]: gotevent7 = 1 assert classentry.classesinheritsfrom == [] assert classentry.ismodulenotrealclass for adef in classentry.defs: pass assert len(classentry.defs) == 0 for attrobj in classentry.attrs: attrname = attrobj.attrname #print 'attr', attrname if attrname == 'x': gotevent8 = 1 if attrname == 'y': gotevent9 = 1 assert len(classentry.attrs) == 2, 'Only got ' + `len(classentry.attrs)` + ' attributes' assert gotevent1 assert gotevent2 assert gotevent3 assert gotevent4 assert gotevent5 assert gotevent6 if CHECK_MODULE_LEVEL_PARSING: assert gotevent7 assert gotevent8 assert gotevent9