Exemplo n.º 1
0
        public void CompleteCallInBeforeCall()
        {
            var impl = new SomeInterfaceImplementation();
            var i    = new RecordingInterceptor();
            var h    = new InterceptingAdapterEmittedTypeHanlder(impl, i);
            var o    = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h);

            i.BeforeCallAction = delegate(CallInfo ci)
            {
                ci.ReturnValue = "AnotherValue";
                ci.Completed   = true;
            };

            var result = o.SomeMethod("SomeString", 17);

            // Adapted method should not be called

            Assert.IsNull(impl.SomeMethodS);
            Assert.AreEqual(0, impl.SomeMethodI);

            // Both BeforeCall and AfterCall should have been called

            Assert.IsNotNull(i.BeforeCallInfo);
            Assert.IsNotNull(i.AfterCallInfo);

            // Result should be the value set by interceptor

            Assert.AreEqual("AnotherValue", result);
        }
Exemplo n.º 2
0
        public void InterceptorOnly()
        {
            var i = new RecordingInterceptor();
            var h = new InterceptingAdapterEmittedTypeHanlder(i);

            var o = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h);

            o.SomeMethod("StringParam", 17);

            Assert.IsNotNull(i.BeforeCallInfo);
            Assert.IsNotNull(i.AfterCallInfo);

            //
            // BeforeCall

            Assert.AreEqual(2, i.BeforeCallInfo.Arguments.Length);
            Assert.AreEqual("StringParam", i.BeforeCallInfo.Arguments[0]);
            Assert.AreEqual(17, i.BeforeCallInfo.Arguments[1]);

            Assert.AreEqual(2, i.BeforeCallInfo.ArgumentTypes.Length);
            Assert.AreEqual(typeof(string), i.BeforeCallInfo.ArgumentTypes[0]);
            Assert.AreEqual(typeof(int), i.BeforeCallInfo.ArgumentTypes[1]);

            Assert.AreEqual(typeof(ISomeInterface), i.BeforeCallInfo.MethodOwner);
            Assert.AreEqual("SomeMethod", i.BeforeCallInfo.MethodName);
            Assert.AreEqual(typeof(string), i.BeforeCallInfo.ResultType);

            Assert.IsNull(i.BeforeCallInfo.ReturnValue);
            Assert.IsNull(i.BeforeCallInfo.ThrownException);
            Assert.IsFalse(i.BeforeCallInfo.Completed);

            //
            // AfterCall

            Assert.AreEqual(2, i.AfterCallInfo.Arguments.Length);
            Assert.AreEqual("StringParam", i.AfterCallInfo.Arguments[0]);
            Assert.AreEqual(17, i.AfterCallInfo.Arguments[1]);

            Assert.AreEqual(2, i.AfterCallInfo.ArgumentTypes.Length);
            Assert.AreEqual(typeof(string), i.AfterCallInfo.ArgumentTypes[0]);
            Assert.AreEqual(typeof(int), i.AfterCallInfo.ArgumentTypes[1]);

            Assert.AreEqual(typeof(ISomeInterface), i.AfterCallInfo.MethodOwner);
            Assert.AreEqual("SomeMethod", i.AfterCallInfo.MethodName);
            Assert.AreEqual(typeof(string), i.AfterCallInfo.ResultType);

            Assert.IsNull(i.AfterCallInfo.ReturnValue);
            Assert.IsNull(i.AfterCallInfo.ThrownException);
            Assert.IsTrue(i.AfterCallInfo.Completed);
        }
        public void ExceptionInAdaptedCall()
        {
            var impl = new SomeInterfaceImplementation();
            var i    = new RecordingInterceptor();
            var h    = new InterceptingAdapterEmittedTypeHanlder(impl, i);
            var o    = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h);

            bool exceptionBubbledUp = false;

            try
            {
                o.ThrowException();
            }
            catch (Exception e)
            {
                exceptionBubbledUp = true;
                Assert.IsInstanceOfType(e, typeof(AdaptedException));
                Assert.IsNotNull(e.InnerException);

                Assert.IsNotNull(((AdaptedException)e).Arguments);
                Assert.AreEqual("ThrowException", ((AdaptedException)e).MethodName);
                Assert.AreEqual(0, ((AdaptedException)e).Arguments.Length);

                Assert.IsFalse(((AdaptedException)e).BeforeCall);
                Assert.IsTrue(((AdaptedException)e).DuringCall);
                Assert.IsFalse(((AdaptedException)e).AfterCall);

                Assert.IsInstanceOfType(e.InnerException, typeof(NullReferenceException));
                Assert.IsNull(e.InnerException.InnerException);
            }

            Assert.IsTrue(exceptionBubbledUp);

            Assert.IsNotNull(i.BeforeCallInfo);
            Assert.IsNotNull(i.AfterCallInfo);

            Assert.IsNull(i.BeforeCallInfo.ThrownException);
            Assert.IsNotNull(i.AfterCallInfo.ThrownException);

            Assert.IsInstanceOfType(i.AfterCallInfo.ThrownException, typeof(NullReferenceException));
        }
Exemplo n.º 4
0
        public static T EmitInterfaceInstance <T>(this IClassEmitter classEmitter,
                                                  IEmittedTypeHandler handler,
                                                  Type baseType = null,
                                                  ConstructorInfo baseConstructor = null,
                                                  IEnumerable <CustomAttributeBuilder> attributeBuilders = null)
            where T : class
        {
            if (classEmitter == null)
            {
                throw new ArgumentNullException("classEmitter");
            }

            var interfaceType = typeof(T);

            if (!interfaceType.IsInterface)
            {
                throw new ArgumentException("T type argument should point to an interface type definition.");
            }

            return(classEmitter.EmitInterfaceInstance(handler, interfaceType, baseType, baseConstructor, attributeBuilders) as T);
        }
        public void VoidResultNoArgs()
        {
            var di = _classEmitter.EmitInterfaceInstance <IMethodWithoutArgs>(_eth);

            Assert.IsNotNull(di);

            di.SomeMethod();

            Assert.IsNotNull(_eth.ReflectedType);
            Assert.IsNotNull(_eth.MemberName);
            Assert.IsNotNull(_eth.CallArguments);
            Assert.IsNotNull(_eth.CallArgumentTypes);
            Assert.IsNotNull(_eth.CallResultType);

            Assert.AreEqual(typeof(IMethodWithoutArgs), _eth.ReflectedType);
            Assert.AreEqual("SomeMethod", _eth.MemberName);
            Assert.AreEqual(0, _eth.CallArguments.Length);
            Assert.AreEqual(0, _eth.CallArgumentTypes.Length);
            Assert.AreEqual(typeof(void), _eth.CallResultType);
        }
Exemplo n.º 6
0
        public void EmptyInterface()
        {
            var di = _classEmitter.EmitInterfaceInstance <IEmpty>(_dth);

            Assert.IsNotNull(di);
        }