コード例 #1
0
 public PropertySetterConfiguration(
     ParsedCallExpression parsedCallExpression,
     Func <ParsedCallExpression, IVoidArgumentValidationConfiguration> voidArgumentValidationConfigurationFactory)
 {
     this.parsedSetterExpression = parsedCallExpression;
     this.voidArgumentValidationConfigurationFactory = voidArgumentValidationConfigurationFactory;
 }
コード例 #2
0
ファイル: RootModule.cs プロジェクト: zyhou/FakeItEasy
 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.");
        }
コード例 #10
0
 private ExpressionCallMatcher CreateMatcher(ParsedCallExpression callSpecification)
 {
     return(new ExpressionCallMatcher(callSpecification, this.constraintFactory, this.methodInfoManager));
 }
コード例 #11
0
 private void AssertThatMemberCanBeIntercepted(ParsedCallExpression parsed)
 {
     this.interceptionAsserter.AssertThatMethodCanBeInterceptedOnInstance(
         parsed.CalledMethod,
         parsed.CallTarget);
 }
コード例 #12
0
        private IVoidArgumentValidationConfiguration CreateVoidArgumentValidationConfiguration(FakeManager fake, ParsedCallExpression parsedCallExpression)
        {
            var rule = this.ruleFactory.Invoke(parsedCallExpression);

            return(this.configurationFactory.CreateConfiguration(fake, rule));
        }
コード例 #13
0
 private IVoidArgumentValidationConfiguration CreateArgumentValidationConfiguration(
     ParsedCallExpression parsedSetter) =>
 this.voidArgumentValidationConfigurationFactory(parsedSetter);
コード例 #14
0
ファイル: RootModule.cs プロジェクト: alex-valchuk/FakeItEasy
 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);
 }