public void CreateInvocationDelegate_CalledWhenNoInnerInvocations_ExpectArgumentExceptionWithCorrectParamName(
     MethodInfo method, ServiceMethodInvocationContext context)
 {
     new ServiceMethodInvocationChain(new IServiceMethodInvocation[0])
     .Invoking(x => x.CreateInvocationDelegate(method, context))
     .ShouldThrow <ArgumentException>().And.ParamName.Should().Be("serviceMethod");
 }
Example #2
0
 private static void ExpectCreateInvocationDelegateThrowsArgumentExceptionForMethod(
     MethodInfo invalidServiceMethod, ServiceMethodInvocationContext context)
 {
     new SyncServiceMethodInvocation().Invoking(
         x => x.CreateInvocationDelegate(invalidServiceMethod, context))
     .ShouldThrow <ArgumentException>()
     .And.ParamName.Should().Be("serviceMethod", invalidServiceMethod + " is not of the correct signature");
 }
        public void CreateInvocationDelegate_CalledMultipleTimes_ExpectEnumerableOfInnerInvocationsIsOnlyEnumeratedOnce(
            MethodInfo method1, ServiceMethodInvocationContext context1, MethodInfo method2, ServiceMethodInvocationContext context2)
        {
            var innerInvocations = Mock.Enumerable <IServiceMethodInvocation>();
            var chain            = new ServiceMethodInvocationChain(innerInvocations);

            chain.Invoking(x => x.CreateInvocationDelegate(method1, context1)).ShouldThrow <Exception>();
            chain.Invoking(x => x.CreateInvocationDelegate(method2, context2)).ShouldThrow <Exception>();

            innerInvocations.AssertWasCalled(x => x.GetEnumerator(), x => x.Repeat.Once());
        }
Example #4
0
        public Delegate CreateInvocationDelegate(MethodInfo serviceMethod, ServiceMethodInvocationContext context)
        {
            if (!this.CanCreateInvocationDelegateFor(serviceMethod))
            {
                throw new ArgumentException(
                          "Method " + serviceMethod + " should have a single parameter, be marked as async or return a Task, and should not be static",
                          "serviceMethod");
            }

            MethodInfo delegateCreator = CreateSpecialisedDelegateCreator(serviceMethod);

            return((Delegate)delegateCreator.Invoke(null, new object[] { serviceMethod, context }));
        }
		public static MethodCallExpression CreateCallExpression(
			MethodInfo serviceMethod, ServiceMethodInvocationContext context, ParameterExpression request)
		{
			return Expression.Call(
				Expression.Convert(
					Expression.Invoke(Expression.Constant(context.ServiceFactory)),
					serviceMethod.DeclaringType),
				serviceMethod,
				Expression.Convert(
					Expression.Invoke(
						Expression.Constant(context.RequestBinder),
						request),
					serviceMethod.TypeOfFirstParameter()));
		}
 public static MethodCallExpression CreateCallExpression(
     MethodInfo serviceMethod, ServiceMethodInvocationContext context, ParameterExpression request)
 {
     return(Expression.Call(
                Expression.Convert(
                    Expression.Invoke(Expression.Constant(context.ServiceFactory)),
                    serviceMethod.DeclaringType),
                serviceMethod,
                Expression.Convert(
                    Expression.Invoke(
                        Expression.Constant(context.RequestBinder),
                        request),
                    serviceMethod.TypeOfFirstParameter())));
 }
Example #7
0
        private static Func <object, CancellationToken, Task <object> > CreateInvocationDelegate <T>(
            MethodInfo serviceMethod, ServiceMethodInvocationContext context)
        {
            ParameterExpression request = Expression.Parameter(typeof(object), "request");
            ParameterExpression cancel  = Expression.Parameter(typeof(CancellationToken), "cancel");
            Func <object, CancellationToken, Task <T> > lambda = Expression.Lambda <Func <object, CancellationToken, Task <T> > >(
                serviceMethod.NumberOfParameters() == 1?
                AsyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request):
                AsyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request, cancel),
                request,
                cancel).Compile();

            return(async(r, c) => await lambda(r, c));
        }
        public void CreateInvocationDelegate_CalledWhenAtLeastOneInnerInvocationCanCreateDelegate_ExpectReturnedDelegateIsFromInnerInvocation(
            [WithinInclusiveRange(0, 10)] int numberOfFalseInnerInvocations,
            [WithinInclusiveRange(1, 10)] int numberOfTrueInnerInvocations,
            MethodInfo method,
            ServiceMethodInvocationContext context)
        {
            Action constructedDelegate = () => { };
            var    falseInvocations    = CreateNumberOfStubServiceMethodInvocationsUnableToCreateDelegate(numberOfFalseInnerInvocations, method);
            var    trueInvocations     = CreateNumberOfStubServiceMethodInvocationsAbleToCreateDelegate(numberOfTrueInnerInvocations, method);

            trueInvocations.ForEach(inv => inv.Stub(
                                        x => x.CreateInvocationDelegate(Arg <MethodInfo> .Is.Same(method), Arg <ServiceMethodInvocationContext> .Is.Same(context)))
                                    .Return(constructedDelegate));

            var chain = new ServiceMethodInvocationChain(falseInvocations.Concat(trueInvocations).Shuffle());

            chain.CreateInvocationDelegate(method, context).Should().BeSameAs(constructedDelegate);
        }
		public Delegate CreateInvocationDelegate(MethodInfo serviceMethod, ServiceMethodInvocationContext context)
		{
			if (!this.CanCreateInvocationDelegateFor(serviceMethod))
			{
				throw new ArgumentException(
					"Method " + serviceMethod + " should have a single parameter, not return a Task nor be marked async, and should not be static",
					"serviceMethod");
			}

			ParameterExpression request = Expression.Parameter(typeof(object), "request");
			if (serviceMethod.ReturnType == typeof(void))
			{
				return Expression.Lambda<Func<object, object>>(
					Expression.Block(
						SyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request),
						Expression.Constant(context.DefaultResponse, typeof(object))),
					request).Compile();
			}

			return Expression.Lambda<Func<object, object>>(
				SyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request),
				request).Compile();
		}
		public Delegate CreateInvocationDelegate(MethodInfo serviceMethod, ServiceMethodInvocationContext context)
		{
			if (!this.CanCreateInvocationDelegateFor(serviceMethod))
			{
				throw new ArgumentException(
					"Method " + serviceMethod + " should have a single parameter, be marked as async or return a Task, and should not be static",
					"serviceMethod");
			}

			ParameterExpression request = Expression.Parameter(typeof(object), "request");
			ParameterExpression cancel = Expression.Parameter(typeof(CancellationToken), "cancel");
			Action<object, CancellationToken> lambda = Expression.Lambda<Action<object, CancellationToken>>(
				serviceMethod.NumberOfParameters() == 1?
					AsyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request):
					AsyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request, cancel),
				request,
				cancel).Compile();

			return new Func<object, CancellationToken, Task<object>>((r, c) =>
				{
					lambda(r, c);
					return TaskEx.FromResult(context.DefaultResponse);
				});
		}
        public Delegate CreateInvocationDelegate(MethodInfo serviceMethod, ServiceMethodInvocationContext context)
        {
            if (!this.CanCreateInvocationDelegateFor(serviceMethod))
            {
                throw new ArgumentException(
                          "Method " + serviceMethod + " should have a single parameter, not return a Task nor be marked async, and should not be static",
                          "serviceMethod");
            }

            ParameterExpression request = Expression.Parameter(typeof(object), "request");

            if (serviceMethod.ReturnType == typeof(void))
            {
                return(Expression.Lambda <Func <object, object> >(
                           Expression.Block(
                               SyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request),
                               Expression.Constant(context.DefaultResponse, typeof(object))),
                           request).Compile());
            }

            return(Expression.Lambda <Func <object, object> >(
                       SyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request),
                       request).Compile());
        }
        public Delegate CreateInvocationDelegate(MethodInfo serviceMethod, ServiceMethodInvocationContext context)
        {
            if (!this.CanCreateInvocationDelegateFor(serviceMethod))
            {
                throw new ArgumentException(
                          "Method " + serviceMethod + " should have a single parameter, be marked as async or return a Task, and should not be static",
                          "serviceMethod");
            }

            ParameterExpression request = Expression.Parameter(typeof(object), "request");
            ParameterExpression cancel  = Expression.Parameter(typeof(CancellationToken), "cancel");
            Action <object, CancellationToken> lambda = Expression.Lambda <Action <object, CancellationToken> >(
                serviceMethod.NumberOfParameters() == 1?
                AsyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request):
                AsyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request, cancel),
                request,
                cancel).Compile();

            return(new Func <object, CancellationToken, Task <object> >((r, c) =>
            {
                lambda(r, c);
                return TaskEx.FromResult(context.DefaultResponse);
            }));
        }
		private static void ExpectCreateInvocationDelegateThrowsArgumentExceptionForMethod(
			MethodInfo invalidServiceMethod, ServiceMethodInvocationContext context)
		{
			new AsyncTaskServiceMethodInvocation().Invoking(
				x => x.CreateInvocationDelegate(invalidServiceMethod, context))
					.ShouldThrow<ArgumentException>()
					.And.ParamName.Should().Be("serviceMethod", invalidServiceMethod + " is not of the correct signature");
		}
		public void CreateInvocationDelegate_CalledForAnyMethodInThisType_ExpectArgumentExceptionWithCorrectParamName(
			Type serviceType, ServiceMethodInvocationContext context)
		{
			serviceType.GetAllDeclaredMethods().ForEach(m => ExpectCreateInvocationDelegateThrowsArgumentExceptionForMethod(m, context));
		}
		public void CreateInvocationDelegate_CalledWithNullServiceMethod_ExpectArgumentNullExceptionWithCorrectParamName(
			ServiceMethodInvocationContext context)
		{
			new AsyncTaskServiceMethodInvocation().Invoking(x => x.CreateInvocationDelegate(null, context))
				.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("serviceMethod");
		}
		public static MethodCallExpression CreateCallExpression(
			MethodInfo serviceMethod, ServiceMethodInvocationContext context, ParameterExpression request)
		{
			return SyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request);
		}
		public void CreateInvocationDelegate_CalledWhenNoInnerInvocationsCanCreateDelegate_ExpectArgumentExceptionWithCorrectParamName(
			[WithinInclusiveRange(1, 10)] int numberOfInnerInvocations, MethodInfo method, ServiceMethodInvocationContext context)
		{
			var innerInvocations = CreateNumberOfStubServiceMethodInvocationsUnableToCreateDelegate(numberOfInnerInvocations, method);
			new ServiceMethodInvocationChain(innerInvocations)
				.Invoking(x => x.CreateInvocationDelegate(method, context))
					.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("serviceMethod");
		}
 public static MethodCallExpression CreateCallExpression(
     MethodInfo serviceMethod, ServiceMethodInvocationContext context, ParameterExpression request)
 {
     return(SyncServiceMethodCall.CreateCallExpression(serviceMethod, context, request));
 }
		public void CreateInvocationDelegate_CalledMultipleTimes_ExpectEnumerableOfInnerInvocationsIsOnlyEnumeratedOnce(
			MethodInfo method1, ServiceMethodInvocationContext context1, MethodInfo method2, ServiceMethodInvocationContext context2)
		{
			var innerInvocations = Mock.Enumerable<IServiceMethodInvocation>();
			var chain = new ServiceMethodInvocationChain(innerInvocations);
			chain.Invoking(x => x.CreateInvocationDelegate(method1, context1)).ShouldThrow<Exception>();
			chain.Invoking(x => x.CreateInvocationDelegate(method2, context2)).ShouldThrow<Exception>();

			innerInvocations.AssertWasCalled(x => x.GetEnumerator(), x => x.Repeat.Once());
		}
		public void CreateInvocationDelegate_CalledWhenAtLeastOneInnerInvocationCanCreateDelegate_ExpectReturnedDelegateIsFromInnerInvocation(
			[WithinInclusiveRange(0, 10)] int numberOfFalseInnerInvocations,
			[WithinInclusiveRange(1, 10)] int numberOfTrueInnerInvocations,
			MethodInfo method,
			ServiceMethodInvocationContext context)
		{
			Action constructedDelegate = () => { };
			var falseInvocations = CreateNumberOfStubServiceMethodInvocationsUnableToCreateDelegate(numberOfFalseInnerInvocations, method);
			var trueInvocations = CreateNumberOfStubServiceMethodInvocationsAbleToCreateDelegate(numberOfTrueInnerInvocations, method);
			trueInvocations.ForEach(inv => inv.Stub(
				x => x.CreateInvocationDelegate(Arg<MethodInfo>.Is.Same(method), Arg<ServiceMethodInvocationContext>.Is.Same(context)))
					.Return(constructedDelegate));

			var chain = new ServiceMethodInvocationChain(falseInvocations.Concat(trueInvocations).Shuffle());
			chain.CreateInvocationDelegate(method, context).Should().BeSameAs(constructedDelegate);
		}
Example #21
0
 public void CreateInvocationDelegate_CalledForAnyMethodInThisType_ExpectArgumentExceptionWithCorrectParamName(
     Type serviceType, ServiceMethodInvocationContext context)
 {
     serviceType.GetAllDeclaredMethods().ForEach(m => ExpectCreateInvocationDelegateThrowsArgumentExceptionForMethod(m, context));
 }
        public void CreateInvocationDelegate_CalledWhenNoInnerInvocationsCanCreateDelegate_ExpectArgumentExceptionWithCorrectParamName(
            [WithinInclusiveRange(1, 10)] int numberOfInnerInvocations, MethodInfo method, ServiceMethodInvocationContext context)
        {
            var innerInvocations = CreateNumberOfStubServiceMethodInvocationsUnableToCreateDelegate(numberOfInnerInvocations, method);

            new ServiceMethodInvocationChain(innerInvocations)
            .Invoking(x => x.CreateInvocationDelegate(method, context))
            .ShouldThrow <ArgumentException>().And.ParamName.Should().Be("serviceMethod");
        }
Example #23
0
 public void CreateInvocationDelegate_CalledWithNullServiceMethod_ExpectArgumentNullExceptionWithCorrectParamName(
     ServiceMethodInvocationContext context)
 {
     new SyncServiceMethodInvocation().Invoking(x => x.CreateInvocationDelegate(null, context))
     .ShouldThrow <ArgumentNullException>().And.ParamName.Should().Be("serviceMethod");
 }
		public void CreateInvocationDelegate_CalledWhenNoInnerInvocations_ExpectArgumentExceptionWithCorrectParamName(
			MethodInfo method, ServiceMethodInvocationContext context)
		{
			new ServiceMethodInvocationChain(new IServiceMethodInvocation[0])
				.Invoking(x => x.CreateInvocationDelegate(method, context))
					.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("serviceMethod");
		}