<s> '''<STR_LIT>''' <EOL> from test . test_support import run_unittest <EOL> import unittest , sys <EOL> from types import ClassType , FunctionType , MethodType , BuiltinFunctionType <EOL> import pyclbr <EOL> from unittest import TestCase <EOL> StaticMethodType = type ( staticmethod ( lambda : None ) ) <EOL> ClassMethodType = type ( classmethod ( lambda c : None ) ) <EOL> from commands import getstatus <EOL> class PyclbrTest ( TestCase ) : <EOL> def assertListEq ( self , l1 , l2 , ignore ) : <EOL> '''<STR_LIT>''' <EOL> missing = ( set ( l1 ) ^ set ( l2 ) ) - set ( ignore ) <EOL> if missing : <EOL> print >> sys . stderr , "<STR_LIT>" % ( l1 , l2 , ignore ) <EOL> self . fail ( "<STR_LIT>" % missing . pop ( ) ) <EOL> def assertHasattr ( self , obj , attr , ignore ) : <EOL> '''<STR_LIT>''' <EOL> if attr in ignore : return <EOL> if not hasattr ( obj , attr ) : print "<STR_LIT>" , attr <EOL> self . failUnless ( hasattr ( obj , attr ) , <EOL> '<STR_LIT>' % ( obj , attr ) ) <EOL> def assertHaskey ( self , obj , key , ignore ) : <EOL> '''<STR_LIT>''' <EOL> if key in ignore : return <EOL> if not obj . has_key ( key ) : <EOL> print >> sys . stderr , "<STR_LIT>" , key <EOL> self . failUnless ( obj . has_key ( key ) ) <EOL> def assertEqualsOrIgnored ( self , a , b , ignore ) : <EOL> '''<STR_LIT>''' <EOL> if a not in ignore and b not in ignore : <EOL> self . assertEquals ( a , b ) <EOL> def checkModule ( self , moduleName , module = None , ignore = ( ) ) : <EOL> '''<STR_LIT>''' <EOL> if module == None : <EOL> module = __import__ ( moduleName , globals ( ) , { } , [ '<STR_LIT>' ] ) <EOL> dict = pyclbr . readmodule_ex ( moduleName ) <EOL> def ismethod ( oclass , obj , name ) : <EOL> classdict = oclass . __dict__ <EOL> if isinstance ( obj , FunctionType ) : <EOL> if not isinstance ( classdict [ name ] , StaticMethodType ) : <EOL> return False <EOL> else : <EOL> if not isinstance ( obj , MethodType ) : <EOL> return False <EOL> if obj . im_self is not None : <EOL> if ( not isinstance ( classdict [ name ] , ClassMethodType ) or <EOL> obj . im_self is not oclass ) : <EOL> return False <EOL> else : <EOL> if not isinstance ( classdict [ name ] , FunctionType ) : <EOL> return False <EOL> objname = obj . __name__ <EOL> if objname . startswith ( "<STR_LIT>" ) and not objname . endswith ( "<STR_LIT>" ) : <EOL> objname = "<STR_LIT>" % ( obj . im_class . __name__ , objname ) <EOL> return objname == name <EOL> for name , value in dict . items ( ) : <EOL> if name in ignore : <EOL> continue <EOL> self . assertHasattr ( module , name , ignore ) <EOL> py_item = getattr ( module , name ) <EOL> if isinstance ( value , pyclbr . Function ) : <EOL> self . assert_ ( isinstance ( py_item , ( FunctionType , BuiltinFunctionType ) ) ) <EOL> if py_item . __module__ != moduleName : <EOL> continue <EOL> self . assertEquals ( py_item . __module__ , value . module ) <EOL> else : <EOL> self . failUnless ( isinstance ( py_item , ( ClassType , type ) ) ) <EOL> if py_item . __module__ != moduleName : <EOL> continue <EOL> real_bases = [ base . __name__ for base in py_item . __bases__ ] <EOL> pyclbr_bases = [ getattr ( base , '<STR_LIT:name>' , base ) <EOL> for base in value . super ] <EOL> try : <EOL> self . assertListEq ( real_bases , pyclbr_bases , ignore ) <EOL> except : <EOL> print >> sys . stderr , "<STR_LIT>" % py_item <EOL> raise <EOL> actualMethods = [ ] <EOL> for m in py_item . __dict__ . keys ( ) : <EOL> if ismethod ( py_item , getattr ( py_item , m ) , m ) : <EOL> actualMethods . append ( m ) <EOL> foundMethods = [ ] <EOL> for m in value . methods . keys ( ) : <EOL> if m [ : <NUM_LIT:2> ] == '<STR_LIT>' and m [ - <NUM_LIT:2> : ] != '<STR_LIT>' : <EOL> foundMethods . append ( '<STR_LIT:_>' + name + m ) <EOL> else : <EOL> foundMethods . append ( m ) <EOL> try : <EOL> self . assertListEq ( foundMethods , actualMethods , ignore ) <EOL> self . assertEquals ( py_item . __module__ , value . module ) <EOL> self . assertEqualsOrIgnored ( py_item . __name__ , value . name , <EOL> ignore ) <EOL> except : <EOL> print >> sys . stderr , "<STR_LIT>" % py_item <EOL> raise <EOL> def defined_in ( item , module ) : <EOL> if isinstance ( item , ClassType ) : <EOL> return item . __module__ == module . __name__ <EOL> if isinstance ( item , FunctionType ) : <EOL> return item . func_globals is module . __dict__ <EOL> return False <EOL> for name in dir ( module ) : <EOL> item = getattr ( module , name ) <EOL> if isinstance ( item , ( ClassType , FunctionType ) ) : <EOL> if defined_in ( item , module ) : <EOL> self . assertHaskey ( dict , name , ignore ) <EOL> def test_easy ( self ) : <EOL> self . checkModule ( '<STR_LIT>' ) <EOL> self . checkModule ( '<STR_LIT>' ) <EOL> self . checkModule ( '<STR_LIT>' ) <EOL> self . checkModule ( '<STR_LIT>' ) <EOL> def test_decorators ( self ) : <EOL> self . checkModule ( '<STR_LIT>' ) <EOL> def test_others ( self ) : <EOL> cm = self . checkModule <EOL> cm ( '<STR_LIT>' , ignore = ( '<STR_LIT>' , ) ) <EOL> cm ( '<STR_LIT>' , ignore = ( '<STR_LIT>' , ) ) <EOL> cm ( '<STR_LIT>' ) <EOL> cm ( '<STR_LIT>' , ignore = ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , ) ) <EOL> cm ( '<STR_LIT>' ) <EOL> cm ( '<STR_LIT>' , ignore = ( '<STR_LIT>' , ) ) <EOL> cm ( '<STR_LIT>' ) <EOL> cm ( '<STR_LIT>' , ignore = ( '<STR_LIT>' , ) ) <EOL> cm ( '<STR_LIT>' ) <EOL> cm ( '<STR_LIT>' ) <EOL> cm ( '<STR_LIT>' ) <EOL> cm ( '<STR_LIT>' ) <EOL> def test_main ( ) : <EOL> run_unittest ( PyclbrTest ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
