コード例 #1
0
        public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)
        {
            var symbol           = _semanticModel.GetSymbolInfo(node).Symbol as IMethodSymbol;
            var innerInvocation  = base.VisitInvocationExpression(node) as InvocationExpressionSyntax;
            var invocationMember = innerInvocation.Expression as MemberAccessExpressionSyntax;

            if (AssertRecognizer.IsTrueMethod(symbol) || AssertRecognizer.TrueMethod(symbol))
            {
                return(innerInvocation
                       .WithExpression(invocationMember.WithName(SyntaxFactory.IdentifierName("True")))
                       .WithArgumentList(CutArgs(innerInvocation.ArgumentList, 2)));
            }

            if (AssertRecognizer.IsFalseMethod(symbol) || AssertRecognizer.FalseMethod(symbol))
            {
                return(innerInvocation
                       .WithExpression(invocationMember.WithName(SyntaxFactory.IdentifierName("False")))
                       .WithArgumentList(CutArgs(innerInvocation.ArgumentList, 2)));
            }

            if (AssertRecognizer.AreEqualMethod(symbol) && innerInvocation.ArgumentList.Arguments.Count == 2)
            {
                return(innerInvocation.WithExpression(invocationMember.WithName(SyntaxFactory.IdentifierName("Equal"))));
            }

            if (AssertRecognizer.AreNotEqualMethod(symbol) && innerInvocation.ArgumentList.Arguments.Count == 2)
            {
                return(innerInvocation.WithExpression(invocationMember.WithName(SyntaxFactory.IdentifierName("NotEqual"))));
            }

            if (AssertRecognizer.IsNullMethod(symbol))
            {
                return(innerInvocation
                       .WithExpression(invocationMember.WithName(SyntaxFactory.IdentifierName("Null")))
                       .WithArgumentList(CutArgs(innerInvocation.ArgumentList, 1)));
            }

            if (AssertRecognizer.IsNotNullMethod(symbol))
            {
                return(innerInvocation
                       .WithExpression(invocationMember.WithName(SyntaxFactory.IdentifierName("NotNull")))
                       .WithArgumentList(CutArgs(innerInvocation.ArgumentList, 1)));
            }

            if (AssertRecognizer.NullMethod(symbol))
            {
                return(innerInvocation
                       .WithArgumentList(CutArgs(innerInvocation.ArgumentList, 1)));
            }

            if (AssertRecognizer.NotNullMethod(symbol))
            {
                return(innerInvocation
                       .WithArgumentList(CutArgs(innerInvocation.ArgumentList, 1)));
            }

            if (AssertRecognizer.AreSameMethod(symbol))
            {
                return(innerInvocation
                       .WithExpression(invocationMember.WithName(SyntaxFactory.IdentifierName("Same")))
                       .WithArgumentList(CutArgs(innerInvocation.ArgumentList, 2)));
            }

            if (AssertRecognizer.AreNotSameMethod(symbol))
            {
                return(innerInvocation
                       .WithExpression(invocationMember.WithName(SyntaxFactory.IdentifierName("NotSame")))
                       .WithArgumentList(CutArgs(innerInvocation.ArgumentList, 2)));
            }

            if (AssertRecognizer.IsEmptyMethod(symbol) && symbol is IMethodSymbol isEmptyMethodSymbol)
            {
                if (NetStandardRecognizer.IsIEnumerableParameter(isEmptyMethodSymbol.Parameters.First().Type))
                {
                    return(innerInvocation
                           .WithExpression(invocationMember.WithName(SyntaxFactory.IdentifierName("Empty")))
                           .WithArgumentList(CutArgs(innerInvocation.ArgumentList, 1)));
                }

                if (NetStandardRecognizer.IsStringParameter(isEmptyMethodSymbol.Parameters.First().Type))
                {
                    var args = CutArgs(innerInvocation.ArgumentList, 1).Arguments
                               .Insert(0, SyntaxFactory.Argument(GetStringEmpty()));

                    var invocation = innerInvocation
                                     .WithExpression(invocationMember.WithName(SyntaxFactory.IdentifierName("Equal")))
                                     .WithArgumentList(SyntaxFactory.ArgumentList(args));
                    return(invocation);
                }
            }

            if (AssertRecognizer.IsNotEmptyMethod(symbol) && symbol is IMethodSymbol isNotEmptyMethodSymbol)
            {
                if (NetStandardRecognizer.IsIEnumerableParameter(isNotEmptyMethodSymbol.Parameters.First().Type))
                {
                    return(innerInvocation
                           .WithExpression(invocationMember.WithName(SyntaxFactory.IdentifierName("NotEmpty")))
                           .WithArgumentList(CutArgs(innerInvocation.ArgumentList, 1)));
                }

                if (NetStandardRecognizer.IsStringParameter(isNotEmptyMethodSymbol.Parameters.First().Type))
                {
                    var args = CutArgs(innerInvocation.ArgumentList, 1).Arguments
                               .Insert(0, SyntaxFactory.Argument(GetStringEmpty()));

                    var invocation = innerInvocation
                                     .WithExpression(invocationMember.WithName(SyntaxFactory.IdentifierName("NotEqual")))
                                     .WithArgumentList(SyntaxFactory.ArgumentList(args));
                    return(invocation);
                }
            }

            if (AssertRecognizer.ContainsMethod(symbol))
            {
                return(innerInvocation
                       .WithArgumentList(CutArgs(innerInvocation.ArgumentList, 2)));
            }

            if (AssertRecognizer.ZeroMethod(symbol))
            {
                return(WithRenameWithFirstParamter(innerInvocation, invocationMember, "Equal", GetZero()));
            }

            if (AssertRecognizer.NotZeroMethod(symbol))
            {
                return(WithRenameWithFirstParamter(innerInvocation, invocationMember, "NotEqual", GetZero()));
            }

            if (AssertRecognizer.PassMethod(symbol))
            {
                return(WithRenameWithFirstParamter(innerInvocation, invocationMember, "True", GetTrue()));
            }

            if (AssertRecognizer.FailMethod(symbol))
            {
                return(WithRenameWithFirstParamter(innerInvocation, invocationMember, "True", GetFalse()));
            }

            if (AssertRecognizer.ThrowsMethod(symbol))
            {
                return(WrapInAction(innerInvocation.ArgumentList.Arguments.First().Expression, innerInvocation));
            }

            if (AssertRecognizer.DoesNotThrowMethod(symbol))
            {
                return(GetInnerLambda(innerInvocation.ArgumentList.Arguments.First().Expression, innerInvocation));
            }

            if (AssertRecognizer.ThrowsAsyncMethod(symbol))
            {
                return(SyntaxFactory.AwaitExpression(innerInvocation.WithArgumentList(CutArgs(innerInvocation.ArgumentList, 1))));
            }

            if (AssertRecognizer.DoesNotThrowAsyncMethod(symbol))
            {
                return(GetInnerLambda(innerInvocation.ArgumentList.Arguments.First().Expression, innerInvocation));
            }

            if (AssertRecognizer.IsInstanceOfMethod(symbol))
            {
                return(RewriteInstanceOf(innerInvocation, invocationMember));
            }

            if (AssertRecognizer.IsNotInstanceOfMethod(symbol))
            {
                return(RewriteNotInstanceOf(innerInvocation, invocationMember));
            }

            if (AssertRecognizer.IsAssignableFromMethod(symbol))
            {
                return(RewriteIsAssignableFromMethod(innerInvocation, invocationMember, "True"));
            }

            if (AssertRecognizer.IsNotAssignableFromMethod(symbol))
            {
                return(RewriteIsAssignableFromMethod(innerInvocation, invocationMember, "False"));
            }

            if (AssertRecognizer.ThatNotGenericMethod(symbol))
            {
                return(RewriteNonGenericThat(innerInvocation, invocationMember, symbol));
            }

            if (AssertRecognizer.ThatMethod(symbol))
            {
                return(RewriteThat(innerInvocation));
            }

            return(innerInvocation);
        }