public void Can_Utilize_Custom_Interceptors_Example_Of_How_To_Extend_MethodWrapper() { var wrapper = new CustomWrapper(); var proxy = new Proxy<IFoo>() .InterceptMethod(f => f.Return()) .With(wrapper) .Save(); Assert.Equal(-1, proxy.Return()); }
public void Can_Utilize_Inline_Interceptors_Example_Of_How_To_Use_Nested_Closure() { var ack = 0; var target = new Foo(); var proxy = new Proxy<IFoo>() .Target(target) .InterceptMethod(f => f.Return()) .With(m => { m.OnBefore(() => ack++); m.OnReturn(() => -1); }) .Save(); Assert.Equal(-1, proxy.Return()); Assert.Equal(1, ack); }
public void Can_Skip_Target() { var foo = new Foo(); var proxy = new Proxy<IFoo>() .Target(foo) .Intercept(f => f.Return()) .OnInvoke(mi => -2) // returns back, target won't be called .Save(); Assert.Equal(-2, proxy.Return()); Assert.False(foo.WasExecuted); }
public void Can_Intercept_Multiple_Methods_Using_A_Filter() { var ack = 0; var foo = new Foo(); var proxy = new Proxy<IFoo>() .Target(foo) .InterceptWhere(mi => mi.Name.EndsWith("Go")) //.InterceptWhere(mi => mi.IsGenericMethod ) .OnBefore(mi => ack++) .Save(); // intercepted proxy.Go(); proxy.GenericGo<int>(-1); proxy.OverloadedGo(-1); proxy.OverloadedGo(string.Empty); // not intercepted proxy.Name = string.Empty; proxy.Return(); Assert.Equal(4, ack); }
public void Can_Intercept_Multiple_Members_Using_A_Single_Fluent_Instruction() { var ack = 0; var foo = new Foo(); var proxy = new Proxy<IFoo>() .Target(foo) .InterceptMethod(f => f.Go()) .OnBefore(() => Assert.Equal(0, ack)) .OnAfter(() => ack++) .InterceptGetter(f => f.Name) .OnBefore(() => Assert.Equal(1, ack)) .OnAfter(() => ack++) .InterceptSetter(f => f.Description) .OnBefore(() => Assert.Equal(2, ack)) .OnAfter(() => ack++) .Save(); // intercepted proxy.Go(); var name = proxy.Name; proxy.Description = string.Empty; // not intercpted proxy.Return(); proxy.Name = string.Empty; var desc = proxy.Description; Assert.Equal(3, ack); }
public void Can_Intercept_Many_Methods_With_One_Advice() { var ack = 0; var foo = new Foo(); var proxy = new Proxy<IFoo>() .Target(foo) .InterceptMethods( f => f.Return(), f => f.GenericGo<int>(It.Any<int>()), f => f.Go()) .OnBefore(mi => ack++) .Save(); // intercepted proxy.Go(); proxy.Return(); proxy.GenericGo<int>(-1); // not intercepted proxy.Name = string.Empty; proxy.OverloadedGo(-1); proxy.OverloadedGo(string.Empty); Assert.Equal(3, ack); }
public void Can_Callback_OnReturn_And_Replace_Return_Value() { var foo = new Foo(); var proxy = new Proxy<IFoo>() .Intercept(f => f.Return()) .OnInvoke(mi => foo.Return()) .OnReturn((mi, r) => r.Equals(1) ? -1 : -2) .Save(); Assert.Equal(-1, proxy.Return()); Assert.True(foo.WasExecuted); }