public PropertySetterConfiguration(
     ParsedCallExpression parsedCallExpression,
     Func <ParsedCallExpression, IVoidArgumentValidationConfiguration> voidArgumentValidationConfigurationFactory)
 {
     this.parsedSetterExpression = parsedCallExpression;
     this.voidArgumentValidationConfigurationFactory = voidArgumentValidationConfigurationFactory;
 }
예제 #2
0
 public ICallMatcher CreateCallMatcher(ParsedCallExpression callSpecification)
 {
     return(new ExpressionCallMatcher(
                callSpecification,
                this.serviceLocator.Resolve <ExpressionArgumentConstraintFactory>(),
                this.serviceLocator.Resolve <MethodInfoManager>()));
 }
예제 #3
0
        private static string GetExpressionDescription(ParsedCallExpression parsedCallExpression)
        {
            var writer            = ServiceLocator.Resolve <StringBuilderOutputWriter.Factory>().Invoke();
            var constraintFactory = ServiceLocator.Resolve <ExpressionArgumentConstraintFactory>();

            CallConstraintDescriber.DescribeCallOn(writer, parsedCallExpression.CalledMethod, parsedCallExpression.ArgumentsExpressions.Select(constraintFactory.GetArgumentConstraint));
            return(writer.Builder.ToString());
        }
예제 #4
0
        private static string GetExpressionDescription(ParsedCallExpression parsedCallExpression)
        {
            var matcher = new ExpressionCallMatcher(
                parsedCallExpression,
                ServiceLocator.Current.Resolve <ExpressionArgumentConstraintFactory>(),
                ServiceLocator.Current.Resolve <MethodInfoManager>());

            return(matcher.DescriptionOfMatchingCall);
        }
예제 #5
0
        public static ParsedCallExpression BuildSetterFromGetter <TValue>(
            ParsedCallExpression parsedCallExpression)
        {
            var propertyName = GetPropertyName(parsedCallExpression);

            if (propertyName is null)
            {
                var expressionDescription = GetExpressionDescription(parsedCallExpression);
                throw new ArgumentException("Expression '" + expressionDescription +
                                            "' must refer to a property or indexer getter, but doesn't.");
            }

            var parameterTypes = new Type[parsedCallExpression.ArgumentsExpressions.Length + 1];

            for (int i = 0; i < parsedCallExpression.ArgumentsExpressions.Length; ++i)
            {
                parameterTypes[i] = parsedCallExpression.ArgumentsExpressions[i].ArgumentInformation.ParameterType;
            }

            parameterTypes[parsedCallExpression.ArgumentsExpressions.Length] = parsedCallExpression.CalledMethod.ReturnType;

            // The DeclaringType may be null, so fall back to the faked object type.
            // (It's unlikely to happen, though, and would require the client code to have passed a specially
            // constructed expression to ACallToSet; perhaps a dynamically generated method created
            // via lightweight code generation.)
            var callTargetType = parsedCallExpression.CalledMethod.DeclaringType
                                 ?? Fake.GetFakeManager(parsedCallExpression.CallTarget).FakeObjectType;
            var setPropertyName   = "set_" + propertyName;
            var indexerSetterInfo = callTargetType.GetMethod(setPropertyName, parameterTypes);

            if (indexerSetterInfo is null)
            {
                if (parsedCallExpression.ArgumentsExpressions.Any())
                {
                    var expressionDescription = GetExpressionDescription(parsedCallExpression);
                    throw new ArgumentException("Expression '" + expressionDescription +
                                                "' refers to an indexed property that does not have a setter.");
                }

                throw new ArgumentException($"The property {propertyName} does not have a setter.");
            }

            var originalParameterInfos = indexerSetterInfo.GetParameters();

            var newParsedSetterValueExpression = new ParsedArgumentExpression(
                BuildArgumentThatMatchesAnything <TValue>(),
                originalParameterInfos.Last());

            var arguments = new ParsedArgumentExpression[originalParameterInfos.Length];

            Array.Copy(parsedCallExpression.ArgumentsExpressions, arguments, originalParameterInfos.Length - 1);
            arguments[originalParameterInfos.Length - 1] = newParsedSetterValueExpression;

            return(new ParsedCallExpression(indexerSetterInfo, parsedCallExpression.CallTarget, arguments));
        }
예제 #6
0
        private static string GetPropertyName(ParsedCallExpression parsedCallExpression)
        {
            var calledMethod = parsedCallExpression.CalledMethod;

            if (HasThis(calledMethod) && calledMethod.IsSpecialName)
            {
                var methodName = calledMethod.Name;
                if (methodName.StartsWith("get_", StringComparison.Ordinal))
                {
                    return(methodName.Substring(4));
                }
            }

            return(null);
        }
예제 #7
0
        private static ParsedCallExpression BuildSetterFromGetter <TValue>(
            ParsedCallExpression parsedCallExpression)
        {
            var propertyName = GetPropertyName(parsedCallExpression);

            if (propertyName == null)
            {
                var expressionDescription = GetExpressionDescription(parsedCallExpression);
                throw new ArgumentException("Expression '" + expressionDescription +
                                            "' must refer to a property or indexer getter, but doesn't.");
            }

            var parsedArgumentExpressions = parsedCallExpression.ArgumentsExpressions ?? new ParsedArgumentExpression[0];
            var parameterTypes            = parsedArgumentExpressions
                                            .Select(p => p.ArgumentInformation.ParameterType)
                                            .Concat(new[] { parsedCallExpression.CalledMethod.ReturnType })
                                            .ToArray();

            var indexerSetterInfo = parsedCallExpression.CallTarget.GetType()
                                    .GetMethod("set_" + propertyName, parameterTypes);

            if (indexerSetterInfo == null)
            {
                if (parsedArgumentExpressions.Any())
                {
                    var expressionDescription = GetExpressionDescription(parsedCallExpression);
                    throw new ArgumentException("Expression '" + expressionDescription +
                                                "' refers to an indexed property that does not have a setter.");
                }

                throw new ArgumentException(
                          "The property '" + propertyName + "' does not have a setter.");
            }

            var originalParameterInfos = indexerSetterInfo.GetParameters();

            var newParsedSetterValueExpression = new ParsedArgumentExpression(
                BuildArgumentThatMatchesAnything <TValue>(),
                originalParameterInfos.Last());

            var arguments = parsedArgumentExpressions
                            .Take(originalParameterInfos.Length - 1)
                            .Concat(new[] { newParsedSetterValueExpression });

            return(new ParsedCallExpression(indexerSetterInfo, parsedCallExpression.CallTarget, arguments));
        }
예제 #8
0
        private static string GetPropertyName(ParsedCallExpression parsedCallExpression)
        {
            var calledMethod = parsedCallExpression.CalledMethod;

            if (HasThis(calledMethod) && calledMethod.IsSpecialName)
            {
                var methodName = calledMethod.Name;
                if (methodName.StartsWith("get_", StringComparison.Ordinal))
                {
                    return(methodName.Substring(4));
                }
            }

            var expressionDescription = GetExpressionDescription(parsedCallExpression);

            throw new ArgumentException(ExceptionMessages.IsNotAGetter(expressionDescription));
        }
예제 #9
0
        private static string GetPropertyName(ParsedCallExpression parsedCallExpression)
        {
            var calledMethod = parsedCallExpression.CalledMethod;

            if (HasThis(calledMethod) && calledMethod.IsSpecialName)
            {
                var methodName = calledMethod.Name;
                if (methodName.StartsWith("get_", StringComparison.Ordinal))
                {
                    return(methodName.Substring(4));
                }
            }

            var expressionDescription = GetExpressionDescription(parsedCallExpression);

            throw new ArgumentException("Expression '" + expressionDescription +
                                        "' must refer to a property or indexer getter, but doesn't.");
        }
 private ExpressionCallMatcher CreateMatcher(ParsedCallExpression callSpecification)
 {
     return(new ExpressionCallMatcher(callSpecification, this.constraintFactory, this.methodInfoManager));
 }
 private void AssertThatMemberCanBeIntercepted(ParsedCallExpression parsed)
 {
     this.interceptionAsserter.AssertThatMethodCanBeInterceptedOnInstance(
         parsed.CalledMethod,
         parsed.CallTarget);
 }
        private IVoidArgumentValidationConfiguration CreateVoidArgumentValidationConfiguration(FakeManager fake, ParsedCallExpression parsedCallExpression)
        {
            var rule = this.ruleFactory.Invoke(parsedCallExpression);

            return(this.configurationFactory.CreateConfiguration(fake, rule));
        }
 private IVoidArgumentValidationConfiguration CreateArgumentValidationConfiguration(
     ParsedCallExpression parsedSetter) =>
 this.voidArgumentValidationConfigurationFactory(parsedSetter);
예제 #14
0
 public ICallMatcher CreateCallMatcher(ParsedCallExpression callSpecification) => new ExpressionCallMatcher(
     callSpecification,
     this.expressionArgumentConstraintFactory,
     this.methodInfoManager);
예제 #15
0
 private void AssertThatMemberCanBeIntercepted(ParsedCallExpression parsedCall)
 {
     this.interceptionAsserter.AssertThatMethodCanBeInterceptedOnInstance(parsedCall.CalledMethod, this.manager.Object);
 }