<s> from UnitTest import UnitTest <EOL> from ClassTest import PassMeAClass <EOL> from ClassTest import ExampleChildClass <EOL> from ClassTest import ExampleMultiSuperclassParent1 <EOL> import Factory2 <EOL> import imports . decors <EOL> import imports . enum . Late <EOL> class Handler : <EOL> def __init__ ( self , x ) : <EOL> self . _x = x <EOL> def handle ( self , y ) : <EOL> return self . _x is y <EOL> class Callable ( object ) : <EOL> def __call__ ( self ) : <EOL> return <NUM_LIT:5> <EOL> def aProcedure ( ) : <EOL> x = <NUM_LIT:1> <EOL> if x is <NUM_LIT:2> : <EOL> return "<STR_LIT>" <EOL> def aFunctionWithOnlyDoc ( ) : <EOL> """<STR_LIT>""" <EOL> def aFunctionReturningNone ( ) : <EOL> return None <EOL> def aFunctionReturningParam ( param ) : <EOL> return param <EOL> def aFunctionReturningFunction ( ) : <EOL> return aFunctionReturningParam <EOL> def aFunctionReturningGlobalX1 ( ) : <EOL> return x <EOL> def aFunctionReturningGlobalX2 ( ) : <EOL> return x <EOL> def aFunctionReturningGlobalX3 ( ) : <EOL> a = x <EOL> return a <EOL> def aFunctionReturningLocalX ( ) : <EOL> x = '<STR_LIT>' <EOL> return x <EOL> def aFunctionReturningArgX ( x ) : <EOL> return x <EOL> x = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> def call ( default , arguments , this , label = None ) : <EOL> return ( name , default , arguments , this , label ) <EOL> def functionDefaults ( s = "<STR_LIT>" , l = [ ] ) : <EOL> n = len ( l ) <EOL> s = s + "<STR_LIT>" % n <EOL> l . append ( n ) <EOL> return s , l <EOL> class FunctionTest ( UnitTest ) : <EOL> def testLambda ( self ) : <EOL> f = lambda x : x <EOL> self . assertEqual ( f ( <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> f = lambda x = <NUM_LIT:1> : x <EOL> self . assertEqual ( f ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( f ( <NUM_LIT:2> ) , <NUM_LIT:2> ) <EOL> f = lambda x , y : x * y <EOL> self . assertEqual ( f ( <NUM_LIT:2> , <NUM_LIT:3> ) , <NUM_LIT:6> ) <EOL> f = lambda x , y = <NUM_LIT:4> : x * y <EOL> self . assertEqual ( f ( <NUM_LIT:2> ) , <NUM_LIT:8> ) <EOL> h = Handler ( <NUM_LIT:5> ) <EOL> f = lambda x : h . handle ( x ) <EOL> self . assertTrue ( f ( <NUM_LIT:5> ) ) <EOL> self . assertFalse ( f ( <NUM_LIT:4> ) ) <EOL> f = lambda a , b = <NUM_LIT:1> , ** kw : ( a , b , kw ) <EOL> v = f ( b = <NUM_LIT:2> , c = <NUM_LIT:3> , a = <NUM_LIT:1> ) <EOL> self . assertEqual ( v [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( v [ <NUM_LIT:1> ] , <NUM_LIT:2> ) <EOL> self . assertEqual ( v [ <NUM_LIT:2> ] [ '<STR_LIT:c>' ] , <NUM_LIT:3> ) <EOL> f = lambda a , b = <NUM_LIT:1> , * args : ( a , b , args ) <EOL> v = f ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> self . assertEqual ( v [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] , <NUM_LIT:3> ) <EOL> self . assertEqual ( v [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] , <NUM_LIT:4> ) <EOL> try : <EOL> class ClassWithLambdas1 : <EOL> f1 = [ lambda * args : args [ <NUM_LIT:0> ] ] <EOL> except : <EOL> self . fail ( "<STR_LIT>" ) <EOL> else : <EOL> c = ClassWithLambdas1 ( ) <EOL> self . assertEqual ( c . f1 [ <NUM_LIT:0> ] ( <NUM_LIT:1> ) , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> try : <EOL> class ClassWithLambdas2 : <EOL> f2 = lambda * args : args [ <NUM_LIT:0> ] <EOL> except : <EOL> self . fail ( "<STR_LIT>" ) <EOL> else : <EOL> c = ClassWithLambdas2 ( ) <EOL> self . assertEqual ( c . f2 ( <NUM_LIT:1> ) , c , '<STR_LIT>' ) <EOL> def test_callable ( self ) : <EOL> try : <EOL> self . assertEqual ( Callable ( ) ( ) , <NUM_LIT:5> ) <EOL> except : <EOL> self . fail ( "<STR_LIT>" ) <EOL> self . assertTrue ( callable ( Callable ) ) <EOL> self . assertTrue ( <EOL> callable ( Callable ( ) ) , <EOL> "<STR_LIT>" , <EOL> ) <EOL> def testProcedure ( self ) : <EOL> self . assertTrue ( aFunctionReturningNone ( ) is None , <EOL> "<STR_LIT>" ) <EOL> self . assertTrue ( aProcedure ( ) is None , <EOL> "<STR_LIT>" ) <EOL> def testVariableFunction ( self ) : <EOL> self . assertEqual ( ( aFunctionReturningParam ) ( "<STR_LIT:foo>" ) , "<STR_LIT:foo>" ) <EOL> self . assertEqual ( aFunctionReturningFunction ( ) ( "<STR_LIT:foo>" ) , "<STR_LIT:foo>" ) <EOL> def testLookup ( self ) : <EOL> expected_result1 = '<STR_LIT>' <EOL> expected_result2 = '<STR_LIT>' <EOL> self . assertEqual ( aFunctionReturningGlobalX1 ( ) , expected_result1 ) <EOL> self . assertEqual ( aFunctionReturningGlobalX2 ( ) , expected_result1 ) <EOL> self . assertEqual ( aFunctionReturningGlobalX3 ( ) , expected_result1 ) <EOL> self . assertEqual ( aFunctionReturningLocalX ( ) , expected_result2 ) <EOL> self . assertEqual ( aFunctionReturningArgX ( '<STR_LIT:test>' ) , '<STR_LIT:test>' ) <EOL> def testLookupLate ( self ) : <EOL> self . assertEqual ( late_global , '<STR_LIT>' ) <EOL> def local_lookup1 ( ) : <EOL> self . assertEqual ( late_local , '<STR_LIT>' ) <EOL> self . assertTrue ( callable ( local_lookup2 ) ) <EOL> self . assertEqual ( local_lookup2 ( ) , '<STR_LIT>' ) <EOL> try : <EOL> local_lookup3 ( ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except NotImplementedError , e : <EOL> self . assertTrue ( True ) <EOL> def local_lookup2 ( ) : <EOL> return late_local <EOL> def local_lookup3 ( ) : <EOL> raise NotImplementedError <EOL> late_local = '<STR_LIT>' <EOL> local_lookup1 ( ) <EOL> late = imports . enum . Late . getLate ( ) <EOL> self . assertEqual ( late . value , '<STR_LIT>' ) <EOL> def testNameMapping ( self ) : <EOL> r = call ( <NUM_LIT:1> , <NUM_LIT:2> , this = <NUM_LIT:3> , label = <NUM_LIT:4> ) <EOL> self . assertEqual ( r [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( r [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( r [ <NUM_LIT:2> ] , <NUM_LIT:2> ) <EOL> self . assertEqual ( r [ <NUM_LIT:3> ] , <NUM_LIT:3> ) <EOL> self . assertEqual ( r [ <NUM_LIT:4> ] , <NUM_LIT:4> ) <EOL> try : <EOL> self . assertEqual ( Text ( '<STR_LIT:foo>' ) , '<STR_LIT>' ) <EOL> except : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def testFactory ( self ) : <EOL> Factory2 . gregister ( "<STR_LIT>" , PassMeAClass ) <EOL> Factory2 . gregister ( "<STR_LIT>" , ExampleChildClass ) <EOL> Factory2 . gregister ( "<STR_LIT>" , ExampleMultiSuperclassParent1 ) <EOL> pmc = Factory2 . ggetObject ( "<STR_LIT>" ) <EOL> self . assertEqual ( pmc . foo ( ) , "<STR_LIT>" ) <EOL> try : <EOL> pmc = Factory2 . ggetObject ( "<STR_LIT>" , <NUM_LIT:5> ) <EOL> except : <EOL> self . assertEqual ( False , True , "<STR_LIT>" ) <EOL> else : <EOL> self . assertEqual ( pmc . x , <NUM_LIT:5> ) <EOL> try : <EOL> pmc = Factory2 . ggetObject ( "<STR_LIT>" , <NUM_LIT:5> , <NUM_LIT:7> ) <EOL> except : <EOL> self . assertEqual ( False , True , "<STR_LIT>" ) <EOL> else : <EOL> self . assertEqual ( pmc . prop_a , <NUM_LIT:1> ) <EOL> self . assertEqual ( pmc . prop_b , <NUM_LIT:7> ) <EOL> def testSliceFunc ( self ) : <EOL> s = "<STR_LIT>" <EOL> s = s [ <NUM_LIT:1> : ] . rstrip ( ) <EOL> self . assertEqual ( s , "<STR_LIT>" ) <EOL> def testFunctionDefaults ( self ) : <EOL> s , l = functionDefaults ( ) <EOL> self . assertEqual ( s , '<STR_LIT:0>' ) <EOL> self . assertTrue ( l == [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> s , l = functionDefaults ( ) <EOL> self . assertTrue ( l == [ <NUM_LIT:0> , <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> inittest = <NUM_LIT:1> <EOL> def f ( inittest = inittest ) : <EOL> return inittest <EOL> self . assertEqual ( f ( ) , inittest ) <EOL> def testKwargs ( self ) : <EOL> def f ( ** kwargs ) : <EOL> return kwargs <EOL> self . assertEqual ( f ( ) , { } ) <EOL> self . assertEqual ( f ( a = <NUM_LIT:1> ) , dict ( a = <NUM_LIT:1> ) ) <EOL> def testFunctionDecorating ( self ) : <EOL> log = [ ] <EOL> def deco1 ( f ) : <EOL> def fn ( * args , ** kwargs ) : <EOL> log . append ( "<STR_LIT>" ) <EOL> res = f ( * args , ** kwargs ) <EOL> log . append ( "<STR_LIT>" ) <EOL> return res <EOL> return fn <EOL> def deco2 ( f ) : <EOL> def fn ( * args , ** kwargs ) : <EOL> log . append ( "<STR_LIT>" ) <EOL> res = f ( * args , ** kwargs ) <EOL> log . append ( "<STR_LIT>" ) <EOL> return res <EOL> return fn <EOL> @ deco1 <EOL> def fn1 ( a , b = <NUM_LIT:0> ) : <EOL> return a , b <EOL> @ deco1 <EOL> @ deco2 <EOL> def fn2 ( a , b = <NUM_LIT:0> ) : <EOL> return a , b <EOL> res = fn1 ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> self . assertEqual ( res [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( res [ <NUM_LIT:1> ] , <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( log ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( log [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( log [ <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> log = [ ] <EOL> res = fn2 ( a = <NUM_LIT:3> ) <EOL> self . assertEqual ( res [ <NUM_LIT:0> ] , <NUM_LIT:3> ) <EOL> self . assertEqual ( res [ <NUM_LIT:1> ] , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( log ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( log [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( log [ <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( log [ <NUM_LIT:2> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( log [ <NUM_LIT:3> ] , "<STR_LIT>" ) <EOL> @ imports . decors . othermoduledeco1 <EOL> def fn3 ( x ) : <EOL> return "<STR_LIT:b>" <EOL> self . assertEqual ( fn3 ( "<STR_LIT:b>" ) , "<STR_LIT:abc>" ) <EOL> def shiftdecorator ( si ) : <EOL> def wrapper ( fn ) : <EOL> def decorated ( * args , ** kw ) : <EOL> return fn ( * args , ** kw ) + si <EOL> return decorated <EOL> return wrapper <EOL> def fn4 ( v ) : <EOL> return v <EOL> @ shiftdecorator ( <NUM_LIT:1> ) <EOL> def fn4d1 ( v ) : <EOL> return v <EOL> @ shiftdecorator ( <NUM_LIT:2> ) <EOL> def fn4d2 ( v ) : <EOL> return v <EOL> fn4d3 = shiftdecorator ( <NUM_LIT:2> ) ( fn4 ) <EOL> self . assertEqual ( fn4d1 ( <NUM_LIT:1> ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( fn4d2 ( <NUM_LIT:1> ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( fn4d3 ( <NUM_LIT:1> ) , <NUM_LIT:3> ) <EOL> def testTopLevelContionalFunction ( self ) : <EOL> self . assertEqual ( imports . conditional_func ( ) , "<STR_LIT>" ) <EOL> late_global = '<STR_LIT>' <EOL> def Text ( x ) : <EOL> return str ( "<STR_LIT>" % x ) </s>
