Esempio n. 1
0
        public void CanInterceptConstrainedInheritedInterfaceMethod2()
        {
            IMethodInvocation invocation = null;

            var behavior = new DelegateInterceptionBehavior((inputs, getNext) =>
            {
                invocation = inputs;
                return(getNext()(inputs, getNext));
            });

            var instance =
                Intercept.NewInstance <Class3 <ICollection <string> > >(
                    new VirtualMethodInterceptor(),
                    new[] { behavior });

            instance.TestMethod <List <string>, DerivedType>(
                new DerivedType(),
                new BaseType[0],
                Enumerable.Empty <DerivedType>(),
                new List <string> [0]);

            Assert.IsNotNull(invocation);
            Assert.AreSame(typeof(List <string>), ((MethodInfo)invocation.MethodBase).ReturnType);
            Assert.AreEqual(4, invocation.MethodBase.GetParameters().Count());
            Assert.AreSame(typeof(BaseType), invocation.MethodBase.GetParameters().ElementAt(0).ParameterType);
            Assert.AreSame(typeof(IEnumerable <BaseType>), invocation.MethodBase.GetParameters().ElementAt(1).ParameterType);
            Assert.AreSame(typeof(IEnumerable <DerivedType>), invocation.MethodBase.GetParameters().ElementAt(2).ParameterType);
            Assert.AreSame(typeof(List <string>[]), invocation.MethodBase.GetParameters().ElementAt(3).ParameterType);
        }
Esempio n. 2
0
        public void CanInterceptAbstractClassWithVirtualMethodInterceptor()
        {
            bool invoked = false;
            IInterceptionBehavior interceptionBehavior =
                new DelegateInterceptionBehavior((mi, next) => { invoked = true; return(mi.CreateMethodReturn(100)); });

            AbstractClass instance =
                Intercept.NewInstance <AbstractClass>(
                    new VirtualMethodInterceptor(),
                    new[] { interceptionBehavior });

            int value = instance.DoSomething();

            Assert.AreEqual(100, value);
            Assert.IsTrue(invoked);
        }
        public void CanInterceptConstrainedInheritedInterfaceMethod3()
        {
            IMethodInvocation invocation;

            var behavior = new DelegateInterceptionBehavior((inputs, getNext) =>
            {
                invocation = inputs;
                return(getNext()(inputs, getNext));
            });

            var instance =
                Intercept.NewInstance <ClassA2 <BaseType> >(
                    new VirtualMethodInterceptor(),
                    new[] { behavior });


            invocation = null;

            instance.Test <HashSet <BaseType>, List <Guid> >(new ISet <BaseType> [0], new List <Guid>());

            Assert.IsNotNull(invocation);
            Assert.AreSame(typeof(KeyValuePair <HashSet <BaseType>, IEnumerable <ISet <BaseType> >[]>), ((MethodInfo)invocation.MethodBase).ReturnType);
            Assert.AreEqual(2, invocation.MethodBase.GetParameters().Count());
            Assert.AreSame(typeof(ISet <BaseType>[]), invocation.MethodBase.GetParameters().ElementAt(0).ParameterType);
            Assert.AreSame(typeof(List <Guid>), invocation.MethodBase.GetParameters().ElementAt(1).ParameterType);


            invocation = null;

            instance.CompareTo((object)this);

            Assert.IsNotNull(invocation);
            Assert.AreSame(typeof(int), ((MethodInfo)invocation.MethodBase).ReturnType);
            Assert.AreEqual(1, invocation.MethodBase.GetParameters().Count());
            Assert.AreSame(typeof(object), invocation.MethodBase.GetParameters().ElementAt(0).ParameterType);


            invocation = null;

            ((IComparable <Guid>)instance).CompareTo(Guid.Empty);

            Assert.IsNotNull(invocation);
            Assert.AreSame(typeof(int), ((MethodInfo)invocation.MethodBase).ReturnType);
            Assert.AreEqual(1, invocation.MethodBase.GetParameters().Count());
            Assert.AreSame(typeof(Guid), invocation.MethodBase.GetParameters().ElementAt(0).ParameterType);
        }
Esempio n. 4
0
        public void CanInterceptTargetWithInstanceInterceptorUsingGenericVersion()
        {
            bool invoked = false;
            IInterceptionBehavior interceptionBehavior =
                new DelegateInterceptionBehavior((mi, next) => { invoked = true; return(mi.CreateMethodReturn(100)); });

            IInterface proxy =
                Intercept.ThroughProxyWithAdditionalInterfaces <IInterface>(
                    new BaseClass(10),
                    new InterfaceInterceptor(),
                    new[] { interceptionBehavior },
                    Type.EmptyTypes);

            int value = proxy.DoSomething();

            Assert.AreEqual(100, value);
            Assert.IsTrue(invoked);
        }
Esempio n. 5
0
        public void CanInterceptNewInstanceWithTypeInterceptorUsingGenericVersion()
        {
            bool invoked = false;
            IInterceptionBehavior interceptionBehavior =
                new DelegateInterceptionBehavior((mi, next) => { invoked = true; return(mi.CreateMethodReturn(100)); });

            BaseClass instance =
                Intercept.NewInstanceWithAdditionalInterfaces <BaseClass>(
                    new VirtualMethodInterceptor(),
                    new[] { interceptionBehavior },
                    Type.EmptyTypes,
                    10);

            int value = instance.DoSomething();

            Assert.AreEqual(100, value);
            Assert.IsTrue(invoked);
        }
Esempio n. 6
0
        public void GeneratedProxyImplementsUserProvidedAdditionalInterfaces()
        {
            bool invoked = false;
            IInterceptionBehavior interceptionBehavior =
                new DelegateInterceptionBehavior((mi, next) => { invoked = true; return(mi.CreateMethodReturn(100)); });

            IInterface proxy = (IInterface)
                               Intercept.ThroughProxyWithAdditionalInterfaces(
                typeof(IInterface),
                new BaseClass(10),
                new InterfaceInterceptor(),
                new[] { interceptionBehavior },
                new[] { typeof(ISomeInterface) });

            int value = ((ISomeInterface)proxy).DoSomethingElse();

            Assert.AreEqual(100, value);
            Assert.IsTrue(invoked);
        }
Esempio n. 7
0
        public void CanInterceptInterfaceWithGenericMethod()
        {
            var target = new IntegrationFixture.ClassWithGenericMethod();

            bool behaviorWasCalled = false;
            var  behavior          = new DelegateInterceptionBehavior((inputs, getNext) =>
            {
                behaviorWasCalled = true;
                return(getNext()(inputs, getNext));
            });

            var proxy = Intercept.ThroughProxy <IntegrationFixture.IInterfaceWithGenericMethod>(
                target, new InterfaceInterceptor(),
                new[] { behavior });

            proxy.DoSomething <string>();

            Assert.IsTrue(behaviorWasCalled);
        }
Esempio n. 8
0
        public void CanInterceptConstrainedInheritedInterfaceMethod()
        {
            IMethodInvocation invocation = null;

            var behavior = new DelegateInterceptionBehavior((inputs, getNext) =>
            {
                invocation = inputs;
                return(getNext()(inputs, getNext));
            });

            var instance =
                Intercept.NewInstance <DerivedNonGenericClass>(
                    new VirtualMethodInterceptor(),
                    new[] { behavior });

            instance.Test <List <string> >();

            Assert.IsNotNull(invocation);
            Assert.AreSame(typeof(List <string>), ((MethodInfo)invocation.MethodBase).ReturnType);
            Assert.AreEqual(0, invocation.MethodBase.GetParameters().Count());
        }
Esempio n. 9
0
        public void InterfaceInterceptorSetsTargetToTargetObject()
        {
            object suppliedTarget = null;

            var behavior = new DelegateInterceptionBehavior((inputs, getNext) =>
            {
                suppliedTarget = inputs.Target;
                return(getNext()(inputs, getNext));
            });

            var actualTarget = new ImplementsInterfaceOne();

            var proxy = Intercept.ThroughProxy <IInterfaceOne>(
                actualTarget, new InterfaceInterceptor(),
                new[] { behavior });

            proxy.TargetMethod();

            Assert.IsTrue(actualTarget.TargetMethodCalled);
            Assert.AreSame(actualTarget, suppliedTarget);
        }
Esempio n. 10
0
        public void CanInterceptVirtualMethodsOnGenericNestedClassInGenericClass()
        {
            IMethodInvocation invocation;

            var behavior = new DelegateInterceptionBehavior((inputs, getNext) =>
            {
                invocation = inputs;
                return(getNext()(inputs, getNext));
            });

            var instance =
                Intercept.NewInstance <GenericClassWithNestedGenericClass <BaseType> .GenericNestedClass <DerivedType> >(
                    new VirtualMethodInterceptor(),
                    new[] { behavior });

            invocation = null;

            instance.Test <List <DerivedType> >();

            Assert.IsNotNull(invocation);
            Assert.AreSame(typeof(List <DerivedType>[]), ((MethodInfo)invocation.MethodBase).ReturnType);
            Assert.AreEqual(0, invocation.MethodBase.GetParameters().Count());
        }
Esempio n. 11
0
        public void CanInterceptGenericMethodWithConstraintRelatedToInterfaceOnGenericInterfaceWithConstraint()
        {
            IMethodInvocation invocation = null;

            var behavior = new DelegateInterceptionBehavior((inputs, getNext) =>
            {
                invocation = inputs;
                return(getNext()(inputs, getNext));
            });

            var instance =
                Intercept.NewInstance <GenericClassWithConstraint <IEnumerable> >(
                    new VirtualMethodInterceptor(),
                    new[] { behavior });

            instance.GenericMethodWithConstraintsOnTheInterfaceParameter <string>(null, null);

            Assert.IsNotNull(invocation);
            Assert.AreSame(typeof(string), ((MethodInfo)invocation.MethodBase).ReturnType);
            Assert.AreEqual(2, invocation.MethodBase.GetParameters().Count());
            Assert.AreSame(typeof(IEnumerable), invocation.MethodBase.GetParameters().ElementAt(0).ParameterType);
            Assert.AreSame(typeof(string), invocation.MethodBase.GetParameters().ElementAt(1).ParameterType);
        }