def add_def_init( self, runtime_init, env ): type_is( PepRuntimeInit, runtime_init ) type_is( PepEnvironment, env ) fn = runtime_init.init_fn.user_function clazz = runtime_init.instance.clazz clazz_type_and_name = ( PepConstructingUserClass( clazz ), PepSymbol("self") ) init_style_arg_types_and_names = ( (clazz_type_and_name,) + fn.arg_types_and_names[1:] ) init_style_fn = PepRuntimeUserFunction( PepUserFunction( fn.name, fn.ret_type, init_style_arg_types_and_names, fn.body_stmts ), runtime_init.init_fn.args, runtime_init.instance.clazz.name ) return self.add_function( init_style_fn, env )
def runtime_namespace( self, instance, insert_placeholders ): #type_implements( PepInstance, instance ) type_is( bool, insert_placeholders ) ret = PepInstanceNamespace( instance, self.namespace ) for var_type, var_name in self.member_variables: ret[var_name] = PepVariable( var_type, "" ) return ret
def __init__( self, clazz, name ): type_isinstance( PepTypeMatcher, clazz ) type_is( str, name ) PepValue.__init__( self ) self.clazz = clazz self.name = name self.namespace = self.clazz.runtime_namespace( self, False )
def Throws_on_instance_of_subclass__test(): with assert_raises( AssertionError ) as cm: type_is( B, sob ) assert_regexp_matches( str( cm.exception ), "^type_is check failed" ) assert_regexp_matches( str( cm.exception ), "B but found" ) assert_regexp_matches( str( cm.exception ), "SonOfB.$" )
def Throws_when_primitive_type_does_not_match__test(): with assert_raises( AssertionError ) as cm: type_is( int, "foo" ) assert_regexp_matches( str( cm.exception ), "^type_is check failed" ) assert_regexp_matches( str( cm.exception ), "int but found" ) assert_regexp_matches( str( cm.exception ), "str.$" )
def Throws_when_class_does_not_match__test(): with assert_raises( AssertionError ) as cm: type_is( B, a ) assert_regexp_matches( str( cm.exception ), "^type_is check failed" ) assert_regexp_matches( str( cm.exception ), "B but found" ) assert_regexp_matches( str( cm.exception ), "A.$" )
def Does_nothing_when_primitive_type_matches__test(): type_is( int, 3 )
def Does_nothing_when_class_matches__test(): type_is( A, a )
def Fails_when_first_arg_is_not_a_type_but_an_instance__test(): with assert_raises( AssertionError ): type_is( a, a )
def Fails_when_first_arg_is_not_a_type_but_an_int__test(): with assert_raises( AssertionError ) as cm: type_is( 3, a ) assert_regexp_matches( str( cm.exception ), "^Wrong arguments" )