public AssertThatData GetAssertThatData(InvocationExpressionSyntax invocationExpression)
        {
            using (_assertThatContext.Enter())
            {
                Visit(invocationExpression);
                var symbol = _semanticModel.GetSymbolInfo(invocationExpression).Symbol as IMethodSymbol;
                _assertThatContext.Current.InvocationMember = invocationExpression.Expression as MemberAccessExpressionSyntax;

                var isResolveConstraint = symbol.Parameters.FirstOrDefault(x => ResolveRecognizer.ResolveConstraint(x.Type));
                var isFunc   = ResolveRecognizer.ActualValueDelegate(symbol.Parameters.First().Type);
                var isAction = ResolveRecognizer.TestDelegate(symbol.Parameters.First().Type);

                if (isResolveConstraint is null)
                {
                    return(null);
                }

                if (isFunc)
                {
                    _assertThatContext.Current.Arguments.Add(SyntaxFactory.Argument(
                                                                 WrapInFunc(invocationExpression.ArgumentList.Arguments.First().Expression, symbol.TypeArguments.Select(x => x.Name))));
                }
                else if (isAction)
                {
                    _assertThatContext.Current.Arguments.Add(SyntaxFactory.Argument(
                                                                 WrapInAction(invocationExpression.ArgumentList.Arguments.First().Expression)));
                }
                else
                {
                    _assertThatContext.Current.Arguments.Add(invocationExpression.ArgumentList.Arguments.First());
                }

                var isResolveIndex = symbol.Parameters.IndexOf(isResolveConstraint);
                var constraintData = WalkConstraintExpression(invocationExpression.ArgumentList.Arguments[isResolveIndex].Expression);

                _assertThatContext.Current.AssertMethod             = constraintData.ConstraintMode;
                _assertThatContext.Current.AssertMethodTypeArgument = constraintData.ConstraintGenericType;
                if (constraintData.ConstraintArgument != null)
                {
                    _assertThatContext.Current.Arguments.Add(constraintData.ConstraintArgument);
                }

                return(_assertThatContext.Current);
            }
        }
Exemplo n.º 2
0
        private SyntaxNode RewriteNonGenericThat(InvocationExpressionSyntax invocationExpression, MemberAccessExpressionSyntax invocationMember, IMethodSymbol symbol)
        {
            var isResolve = symbol.Parameters.FirstOrDefault(x => ResolveRecognizer.ResolveConstraint(x.Type));
            var isFunc    = NetStandardRecognizer.IsFuncParameter(symbol.Parameters.First().Type);

            if (isResolve != null)
            {
                return(RewriteThat(invocationExpression));
            }
            if (isFunc && symbol.Parameters.First().Type is INamedTypeSymbol typeSymbol)
            {
                var genericTypeArgs = typeSymbol.TypeArguments.Select(x => x.Name);
                return(WrapInFunc(invocationExpression.ArgumentList.Arguments.First().Expression, invocationExpression, genericTypeArgs, true)
                       .WithExpression(invocationMember.WithName(SyntaxFactory.IdentifierName("True"))));
            }
            else
            {
                return(invocationExpression
                       .WithExpression(invocationMember.WithName(SyntaxFactory.IdentifierName("True")))
                       .WithArgumentList(CutArgs(invocationExpression.ArgumentList, 1)));
            }
        }