public void OutComplexValueType()
            {
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(ISpyOut).GetMethod("OutComplexValueType");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                ISpyOut          result = WrapAndCreateType <ISpyOut, SpyOut>(dictionary);
                ComplexValueType outValue;

                result.OutComplexValueType(out outValue);

                Assert.Equal(byte.MaxValue, outValue.Byte);
                Assert.Equal('a', outValue.Char);
                Assert.Equal(decimal.MaxValue, outValue.Decimal);
                Assert.Equal(double.MaxValue, outValue.Double);
                Assert.Equal(float.MaxValue, outValue.Float);
                Assert.Equal(int.MaxValue, outValue.Int);
                Assert.Equal(long.MaxValue, outValue.Long);
                Assert.Equal(short.MaxValue, outValue.Short);
                Assert.Equal("Hello, world!", outValue.String);
                Assert.Equal(uint.MaxValue, outValue.UInt);
                Assert.Equal(ulong.MaxValue, outValue.ULong);
                Assert.Equal(ushort.MaxValue, outValue.UShort);
            }
            public void ReturnsValueType()
            {
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(ISpyReturn).GetMethod("ReturnsValueType");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                ISpyReturn result   = WrapAndCreateType <ISpyReturn, SpyReturn>(dictionary);
                int        retValue = result.ReturnsValueType();

                Assert.Equal(SpyReturn.ValueReturn, retValue);
            }
            public void ReturnsClassType()
            {
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(SpyReturn).GetMethod("ReturnsClassType");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                SpyReturn result   = WrapAndCreateType <SpyReturn>(dictionary, 42);
                object    retValue = result.ReturnsClassType();

                Assert.Same(SpyReturn.ObjectReturn, retValue);
            }
            public void Exception()
            {
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(ISpyReturn).GetMethod("Exception");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                ISpyReturn result = WrapAndCreateType <ISpyReturn, SpyReturn>(dictionary);

                Assert.Throws <ArgumentException>(delegate
                {
                    result.Exception();
                });
            }
            public void RefClassParameter()
            {
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(ISpyRef).GetMethod("RefClassParameter");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                ISpyRef result   = WrapAndCreateType <ISpyRef, SpyRef>(dictionary);
                string  refValue = "Hello, ";

                result.RefClassParameter(ref refValue);

                Assert.Equal("Hello, world!", refValue);
            }
            public void OutReferenceType()
            {
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(ISpyOut).GetMethod("OutReferenceType");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                ISpyOut result = WrapAndCreateType <ISpyOut, SpyOut>(dictionary);
                string  outReference;

                result.OutReferenceType(out outReference);

                Assert.Equal("Hello, world!", outReference);
            }
            public void RefValueType()
            {
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(SpyRef).GetMethod("RefValueType");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                SpyRef result   = WrapAndCreateType <SpyRef>(dictionary);
                int    refValue = 21;

                result.RefValueType(ref refValue);

                Assert.Equal(42, refValue);
            }
            public void OutInt64()
            {
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(ISpyOut).GetMethod("OutInt64");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                ISpyOut result = WrapAndCreateType <ISpyOut, SpyOut>(dictionary);
                long    outValue;

                result.OutInt64(out outValue);

                Assert.Equal(long.MaxValue, outValue);
            }
        public void OneHandler()
        {
            Recorder.Records.Clear();
            RecordingHandler     handler    = new RecordingHandler();
            StubMethodInvocation invocation = new StubMethodInvocation();
            HandlerPipeline      pipeline   = new HandlerPipeline(handler);

            pipeline.Invoke(invocation, delegate
            {
                Recorder.Records.Add("method");
                return(null);
            });

            Assert.Equal(3, Recorder.Records.Count);
            Assert.Equal("Before Method", Recorder.Records[0]);
            Assert.Equal("method", Recorder.Records[1]);
            Assert.Equal("After Method", Recorder.Records[2]);
        }
            public void NoReturnValue()
            {
                Recorder.Records.Clear();
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(ISpyReturn).GetMethod("NoReturnValue");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                ISpyReturn result = WrapAndCreateType <ISpyReturn, SpyReturn>(dictionary);

                result.NoReturnValue();

                Assert.Equal(3, Recorder.Records.Count);
                Assert.Equal("Before Method", Recorder.Records[0]);
                Assert.Equal("In Method", Recorder.Records[1]);
                Assert.Equal("After Method", Recorder.Records[2]);
            }
            public void InReferenceParameter()
            {
                Recorder.Records.Clear();
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(ISpyIn).GetMethod("InReferenceParameter");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                ISpyIn result = WrapAndCreateType <ISpyIn, SpyIn>(dictionary);

                result.InReferenceParameter("Hello");

                Assert.Equal(3, Recorder.Records.Count);
                Assert.Equal("Before Method", Recorder.Records[0]);
                Assert.Equal("In Method: Hello", Recorder.Records[1]);
                Assert.Equal("After Method", Recorder.Records[2]);
            }
            public void WhereClauseOnMethod()
            {
                Recorder.Records.Clear();
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(INonGenericSpy).GetMethod("WhereMethod");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                INonGenericSpy result = WrapAndCreateType <INonGenericSpy, NonGenericSpy>(dictionary);

                result.WhereMethod(new Foo());

                Assert.Equal(3, Recorder.Records.Count);
                Assert.Equal("Before Method", Recorder.Records[0]);
                Assert.Equal("In method with data " + typeof(Foo).FullName, Recorder.Records[1]);
                Assert.Equal("After Method", Recorder.Records[2]);
            }
            public void ReturnsDataOfGenericType()
            {
                Recorder.Records.Clear();
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(INonGenericSpy).GetMethod("GenericReturn");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                INonGenericSpy result = WrapAndCreateType <INonGenericSpy, NonGenericSpy>(dictionary);
                int            value  = result.GenericReturn <int, double>(256.9);

                Assert.Equal(3, Recorder.Records.Count);
                Assert.Equal("Before Method", Recorder.Records[0]);
                Assert.Equal("In method with data 256.9", Recorder.Records[1]);
                Assert.Equal("After Method", Recorder.Records[2]);
                Assert.Equal(default(int), value);
            }
            public void NonGenericMethod()
            {
                Recorder.Records.Clear();
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(IGenericSpy <>).GetMethod("MethodWhichTakesGenericData");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                IGenericSpy <int> result = WrapAndCreateType <IGenericSpy <int>, GenericSpy <int> >(dictionary);

                result.MethodWhichTakesGenericData(24);

                Assert.Equal(3, Recorder.Records.Count);
                Assert.Equal("Before Method", Recorder.Records[0]);
                Assert.Equal("In method with data 24", Recorder.Records[1]);
                Assert.Equal("After Method", Recorder.Records[2]);
            }
            public void GenericMethod()
            {
                Recorder.Records.Clear();
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(GenericSpy <>).GetMethod("GenericMethod");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                GenericSpy <int> result = WrapAndCreateType <GenericSpy <int> >(dictionary);

                result.GenericMethod(46, "2");

                Assert.Equal(3, Recorder.Records.Count);
                Assert.Equal("Before Method", Recorder.Records[0]);
                Assert.Equal("In method with data 46 and 2", Recorder.Records[1]);
                Assert.Equal("After Method", Recorder.Records[2]);
            }
            public void TwoParameters()
            {
                Recorder.Records.Clear();
                RecordingHandler handler = new RecordingHandler();
                MethodBase       method  = typeof(SpyIn).GetMethod("TwoParameters");
                Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
                List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

                handlers.Add(handler);
                dictionary.Add(method, handlers);

                SpyIn  result   = WrapAndCreateType <SpyIn>(dictionary);
                string retValue = result.TwoParameters(42, "Hello ");

                Assert.Equal("Hello 42", retValue);
                Assert.Equal(3, Recorder.Records.Count);
                Assert.Equal("Before Method", Recorder.Records[0]);
                Assert.Equal("In Method", Recorder.Records[1]);
                Assert.Equal("After Method", Recorder.Records[2]);
            }
        public void TwoHandlers()
        {
            Recorder.Records.Clear();
            RecordingHandler     handler1   = new RecordingHandler("1");
            RecordingHandler     handler2   = new RecordingHandler("2");
            StubMethodInvocation invocation = new StubMethodInvocation();
            HandlerPipeline      pipeline   = new HandlerPipeline(handler1, handler2);

            pipeline.Invoke(invocation, delegate
            {
                Recorder.Records.Add("method");
                return(null);
            });

            Assert.Equal(5, Recorder.Records.Count);
            Assert.Equal("Before Method (1)", Recorder.Records[0]);
            Assert.Equal("Before Method (2)", Recorder.Records[1]);
            Assert.Equal("method", Recorder.Records[2]);
            Assert.Equal("After Method (2)", Recorder.Records[3]);
            Assert.Equal("After Method (1)", Recorder.Records[4]);
        }
        public void CanInterceptInterface()
        {
            Recorder.Records.Clear();
            SpyInterfaceClass rawObject = new SpyInterfaceClass();
            RecordingHandler  handler   = new RecordingHandler();
            MethodBase        method    = typeof(ISpyInterface).GetMethod("InterceptedMethod");
            Dictionary <MethodBase, List <IInterceptionHandler> > dictionary = new Dictionary <MethodBase, List <IInterceptionHandler> >();
            List <IInterceptionHandler> handlers = new List <IInterceptionHandler>();

            handlers.Add(handler);
            dictionary.Add(method, handlers);

            ISpyInterface result = RemotingInterceptor.Wrap <ISpyInterface>(rawObject, dictionary);

            result.InterceptedMethod();

            Assert.Equal(3, Recorder.Records.Count);
            Assert.Equal("Before Method", Recorder.Records[0]);
            Assert.Equal("In Method", Recorder.Records[1]);
            Assert.Equal("After Method", Recorder.Records[2]);
        }