Beispiel #1
0
        public void TestInterceptsTypedParamInfoIsCorrect()
        {
            var handler = Substitute.For <IInterceptionHandler>();
            var random  = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
            var val     = random.Next();

            handler.InterceptingAction <int>(Arg.Any <TDelegate <int> >(), Arg.Any <ParamInfo[]>())
            .Returns(x =>
            {
                var @params = (ParamInfo[])x[1];
                Assert.IsFalse(@params[0].IsByRef);
                Assert.IsFalse(@params[1].IsByRef);
                Assert.IsFalse(@params[0].IsOut);
                Assert.IsFalse(@params[1].IsOut);
                Assert.AreEqual(@params[0].Type, typeof(int));
                Assert.AreEqual(@params[1].Type, typeof(int));
                Assert.AreEqual(@params[0].Name, "a");
                Assert.AreEqual(@params[1].Name, "b");
                Assert.AreEqual(@params[0].Value, 5);
                Assert.AreEqual(@params[1].Value, 5);
                return(val);
            });

            var proxyType = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(ITest), typeof(TestClass));
            var proxy     = (ITest)Activator.CreateInstance(proxyType, new object[] { new TestClass(), handler });
            var sum       = proxy.Sum(5, 5);

            handler.Received().InterceptingAction <int>(Arg.Any <TDelegate <int> >(), Arg.Any <ParamInfo[]>());
            Assert.IsTrue(sum == val);
        }
Beispiel #2
0
        public void BehaviourNotChangedWhenNoInterceptionVoid()
        {
            var proxyType    = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(ITestVoid), typeof(TestClassVoidNoInterceptor));
            var testInstance = new TestClassVoidNoInterceptor();
            var proxy        = (ITestVoid)Activator.CreateInstance(proxyType, new object[] { testInstance });
            var random       = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);

            proxy.Sum(10, 5);
        }
Beispiel #3
0
        public void TestInterceptsVoid()
        {
            var handler = Substitute.For <IInterceptionHandler>();

            handler.InterceptingAction(Arg.Any <VoidDelegate>(), Arg.Any <ParamInfo[]>());
            var proxyType = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(ITestVoid), typeof(TestClassVoid));
            var proxy     = (ITestVoid)Activator.CreateInstance(proxyType, new object[] { new TestClassVoid(), handler });

            proxy.Sum(5, 5);
            handler.Received().InterceptingAction(Arg.Any <VoidDelegate>(), Arg.Any <ParamInfo[]>());
        }
Beispiel #4
0
        public void TestInterfaceExplicitImplementationNoInterception()
        {
            var proxyType    = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(ITest), typeof(TestClassExplicitImplementationNoInterceptor));
            var testInstance = new TestClassExplicitImplementationNoInterceptor();

            var proxy  = (ITest)Activator.CreateInstance(proxyType, new object[] { testInstance });
            var random = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
            var val    = random.Next();

            Assert.IsTrue(proxy.Sum(val, 5) == ((ITest)testInstance).Sum(val, 5));
        }
Beispiel #5
0
        public void BehaviourNotChangedWhenNoInterceptionTyped()
        {
            var proxyType = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(ITest), typeof(TestClassNoInterceptor));

            var testInstance = new TestClassNoInterceptor();
            var proxy        = (ITest)Activator.CreateInstance(proxyType, new object[] { testInstance });
            var random       = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
            var val          = random.Next();

            Assert.IsTrue(proxy.Sum(val, 5) == testInstance.Sum(val, 5));
        }
Beispiel #6
0
        public void BehaviourNotChangedWhenNoInterception()
        {
            var proxyType = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(IGenericTest <int>), typeof(TestGenericNoInterception <int>));

            var testInstance = new TestGenericNoInterception <int>();
            var proxy        = (IGenericTest <int>)Activator.CreateInstance(proxyType, new object[] { testInstance });
            var random       = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
            var val          = random.Next();

            Assert.IsTrue(proxy.Function(val) == proxy.Property && proxy.Property == val);
        }
Beispiel #7
0
        public void TestInterfaceExplicitImplementationWithInterception()
        {
            var handler = Substitute.For <IInterceptionHandler>();

            handler.InterceptingAction <int>(Arg.Any <TDelegate <int> >(), Arg.Any <ParamInfo[]>()).ReturnsForAnyArgs(0);

            var proxyType = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(ITest), typeof(TestClassExplicitImplementation));
            var proxy     = (ITest)Activator.CreateInstance(proxyType, new object[] { new TestClassExplicitImplementation(), handler });

            Assert.IsTrue(0 == proxy.Sum(5, 5));
            handler.Received().InterceptingAction <int>(Arg.Any <TDelegate <int> >(), Arg.Any <ParamInfo[]>());
        }
Beispiel #8
0
        public void BehaviourNotChangedWhenNoInterceptionGenericMethod()
        {
            var proxyType = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(IGenericMethodTest), typeof(TestGenericMethodNoInterception));

            var testInstance = new TestGenericMethodNoInterception();
            var proxy        = (IGenericMethodTest)Activator.CreateInstance(proxyType, new object[] { testInstance });
            var random       = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
            var val          = random.Next();
            var res          = proxy.Function(val, val);

            Assert.IsTrue(res.Item1 == val && res.Item2 == val);
        }
Beispiel #9
0
        public void TestInterceptsTyped()
        {
            var handler = Substitute.For <IInterceptionHandler>();
            var random  = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
            var val     = random.Next();

            handler.InterceptingAction <int>(Arg.Any <TDelegate <int> >(), Arg.Any <ParamInfo[]>()).ReturnsForAnyArgs(val);
            var proxyType = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(ITest), typeof(TestClass));
            var proxy     = (ITest)Activator.CreateInstance(proxyType, new object[] { new TestClass(), handler });
            var sum       = proxy.Sum(5, 5);

            handler.Received().InterceptingAction <int>(Arg.Any <TDelegate <int> >(), Arg.Any <ParamInfo[]>());
            Assert.IsTrue(sum == val);
        }
Beispiel #10
0
        public void TestInterceptsGenericMethod()
        {
            var handler = Substitute.For <IInterceptionHandler>();
            var random  = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
            var val     = random.Next();

            handler.InterceptingAction <Tuple <int, int> >(Arg.Any <TDelegate <Tuple <int, int> > >(), Arg.Any <ParamInfo[]>()).ReturnsForAnyArgs(new Tuple <int, int>(val, val));
            var proxyType = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(IGenericMethodTest), typeof(TestGenericMethod));
            var proxy     = (IGenericMethodTest)Activator.CreateInstance(proxyType, new object[] { new TestGenericMethod(), handler });
            var res       = proxy.Function(random.Next(), random.Next());

            handler.Received().InterceptingAction <Tuple <int, int> >(Arg.Any <TDelegate <Tuple <int, int> > >(), Arg.Any <ParamInfo[]>());
            Assert.IsTrue(res.Item1 == val && res.Item2 == val);
        }
Beispiel #11
0
        public void TestIntercepts()
        {
            var handler = Substitute.For <IInterceptionHandler>();
            var random  = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
            var retVal  = random.Next();

            handler.InterceptingAction <int>(Arg.Any <TDelegate <int> >(), Arg.Any <ParamInfo[]>()).ReturnsForAnyArgs(retVal);
            var proxyType = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(IGenericTest <int>), typeof(TestGeneric <int>));
            var proxy     = (IGenericTest <int>)Activator.CreateInstance(proxyType, new object[] { new TestGeneric <int>(), handler });
            var val       = random.Next();

            Assert.IsTrue(proxy.Function(val) == retVal && proxy.Property == default(int));
            handler.Received().InterceptingAction <int>(Arg.Any <TDelegate <int> >(), Arg.Any <ParamInfo[]>());
        }
Beispiel #12
0
        public void BehaviourNotChangedWhenNoInterceptionOut()
        {
            var proxyType = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(IOutTest), typeof(OutNoInterceptionTest));

            var testInstance = new OutNoInterceptionTest();
            var proxy        = (IOutTest)Activator.CreateInstance(proxyType, new object[] { testInstance });
            var random       = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);

            int @out;

            testInstance.Out = random.Next();
            proxy.OutTest(out @out);

            Assert.IsTrue(@out == testInstance.Out);
        }
Beispiel #13
0
        public void BehaviourNotChangedWhenNoInterceptionRef()
        {
            var proxyType = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(IRefTest), typeof(RefNoInterceptionTest));

            var testInstance = new RefNoInterceptionTest();
            var proxy        = (IRefTest)Activator.CreateInstance(proxyType, new object[] { testInstance });
            var random       = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);

            testInstance.Ref = random.Next();
            var @ref = 10;

            proxy.RefTest(ref @ref);

            Assert.IsTrue(@ref == testInstance.Ref);
        }
        public void TestInterceptsMultiWithMultiHandler()
        {
            var handler  = Substitute.For <IInterceptionHandler>();
            var handler2 = Substitute.For <INewInterceptionHandler>();
            var random   = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
            var val      = random.Next();

            handler.InterceptingAction <int>(Arg.Any <TDelegate <int> >(), Arg.Any <ParamInfo[]>()).ReturnsForAnyArgs(x => ((TDelegate <int>)x[0]).Invoke((ParamInfo[])x[1]));
            handler2.InterceptingAction <int>(Arg.Any <TDelegate <int> >(), Arg.Any <ParamInfo[]>()).ReturnsForAnyArgs(x => ((TDelegate <int>)x[0]).Invoke((ParamInfo[])x[1]));
            var proxyType = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(ITest), typeof(TestClassMultiIntercept2Handlers));
            var proxy     = (ITest)Activator.CreateInstance(proxyType, new object[] { new TestClassMultiIntercept2Handlers(), handler, handler2 });
            var sum       = proxy.Sum(val, 5);

            handler.Received().InterceptingAction <int>(Arg.Any <TDelegate <int> >(), Arg.Any <ParamInfo[]>());
            handler2.Received().InterceptingAction <int>(Arg.Any <TDelegate <int> >(), Arg.Any <ParamInfo[]>());
            Assert.IsTrue(sum == val + 5);
        }
Beispiel #15
0
        public void TestRefParamInterception()
        {
            var handler = Substitute.For <IInterceptionHandler>();

            var random = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
            var val    = random.Next();

            handler.InterceptingAction(Arg.Any <TDelegate <int> >(), Arg.Any <ParamInfo[]>()).Returns(x => { Assert.IsTrue(((ParamInfo[])x[1])[0].IsByRef); return(((ParamInfo[])x[1])[0].Value = val); });
            var proxyType = InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(IRefTest), typeof(RefpTest));
            var proxy     = (IRefTest)Activator.CreateInstance(proxyType, new object[] { new RefpTest(), handler });

            var @ref = random.Next();

            proxy.RefTest(ref @ref);

            Assert.IsTrue(@ref == val);
        }
 public void InterfaceBuilderStrategy_CreateInterfaceProxy_Throws_WhenNotInterface()
 {
     Assert.Throws <ArgumentException>(delegate { InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(void), typeof(void)); });
 }
 public void InterfaceBuilderStrategy_CreateInterfaceProxy_Throws_WhenTypeIsNotDerivedFromInterface()
 {
     Assert.Throws <ArgumentException>(delegate { InterfaceBuilderStrategy.CreateInterfaceProxy(typeof(IGenericTest <int>), typeof(TestClass)); });
 }