/// <summary>
        /// Syntax for <c><![CDATA[Assert.ThrowsException<exceptionType>(expression))]]></c>.
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="exceptionType"></param>
        /// <param name="additionalArguments"></param>
        /// <returns></returns>
        public static InvocationExpressionSyntax ThrowsExceptionNaked(
            ExpressionSyntax expression,
            ExceptionSyntaxDetails details,
            SeparatedSyntaxList <ArgumentSyntax>?additionalArguments = null)
        {
            if (details == null)
            {
                throw new ArgumentNullException(nameof(details));
            }
            if (details.Inconclusive)
            {
                throw new InvalidOperationException();
            }

            // Assert.ThrowsException<<ExceptionType>>(() => /* whatever */));

            return(SyntaxFactory.InvocationExpression(
                       SyntaxFactory.MemberAccessExpression(
                           SyntaxKind.SimpleMemberAccessExpression,
                           SyntaxFactory.IdentifierName("Assert"),
                           SyntaxFactory.GenericName(SyntaxFactory.Identifier("ThrowsException"))
                           .WithTypeArgumentList(
                               SyntaxFactory.TypeArgumentList(
                                   SyntaxFactory.SingletonSeparatedList <TypeSyntax>(
                                       SyntaxFactory.IdentifierName(details.TypeName))))))
                   .WithArgumentList(
                       BuildArgumentList(expression, additionalArguments)));
        }
Exemplo n.º 2
0
        public static ArgumentListSyntax GetParentInvocationArguments(
            this MemberAccessExpressionSyntax memberAccess,
            ExceptionSyntaxDetails details, int numArgumentsRequired,
            Func <ArgumentSyntax, int, bool> check = null)
        {
            if (details == null)
            {
                throw new ArgumentNullException(nameof(details));
            }

            if (memberAccess?.Parent is InvocationExpressionSyntax invocation &&
                invocation.ArgumentList?.Arguments.Count == numArgumentsRequired)
            {
                for (int i = 0; i < invocation.ArgumentList.Arguments.Count; i++)
                {
                    if (check != null && !check(invocation.ArgumentList.Arguments[i], i))
                    {
                        details.SetInconclusive(invocation.ArgumentList.Arguments[i]?.ToString());
                        return(null);
                    }
                }

                return(invocation.ArgumentList);
            }

            details.SetInconclusive(memberAccess?.ToString());
            return(null);
        }
Exemplo n.º 3
0
        public static ArgumentListSyntax TransformParentInvocationArguments(
            this MemberAccessExpressionSyntax memberAccess,
            ExceptionSyntaxDetails details, int numArgumentsRequired,
            Func <ArgumentSyntax, int, ArgumentSyntax> transform
            )
        {
            if (details == null)
            {
                throw new ArgumentNullException(nameof(details));
            }
            if (transform == null)
            {
                throw new ArgumentNullException(nameof(transform));
            }

            if (memberAccess?.Parent is InvocationExpressionSyntax invocation &&
                invocation.ArgumentList?.Arguments.Count == numArgumentsRequired)
            {
                var result = new SeparatedSyntaxList <ArgumentSyntax>();
                for (int i = 0; i < invocation.ArgumentList.Arguments.Count; i++)
                {
                    var transformed = transform(invocation.ArgumentList.Arguments[i], i);
                    if (transformed == null)
                    {
                        details.SetInconclusive(invocation.ArgumentList.Arguments[i]?.ToString());
                        return(null);
                    }
                    result = result.Add(transformed);
                }
                return(SyntaxFactory.ArgumentList(result));
            }

            details.SetInconclusive(memberAccess?.ToString());
            return(null);
        }
        /// <summary>
        /// Syntax for <c><![CDATA[Assert.InstanceOfType(Assert.ThrowsException<Exception>(expression, typeof(exceptionType))]]></c>.
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="exceptionType"></param>
        /// <param name="additionalArguments"></param>
        /// <returns></returns>
        public static InvocationExpressionSyntax ThrowsExceptionInstanceOfSyntax(
            ExpressionSyntax expression,
            ExceptionSyntaxDetails details, SeparatedSyntaxList <ArgumentSyntax>?additionalArguments = null)
        {
            if (details == null)
            {
                throw new ArgumentNullException(nameof(details));
            }
            if (details.Inconclusive)
            {
                throw new InvalidOperationException();
            }

            // TODO: This doesn't work in practice, because Assert.ThrowsException<Exception>(),
            // will fail if not exactly System.Exception is thrown.
            // We would need to do something like this:
            // try {
            //     /* test */
            //     Assert.Fail($"Expected exception of type {exceptionType}.");
            // } catch (Exception ex) when (!(ex is AssertFailException)) {
            //     Assert.IsInstanceOfType(ex, exceptionType);
            // }
            return(SyntaxFactory.InvocationExpression(
                       SyntaxFactory.MemberAccessExpression(
                           SyntaxKind.SimpleMemberAccessExpression,
                           SyntaxFactory.IdentifierName("Assert"),
                           SyntaxFactory.IdentifierName("IsInstanceOfType")))
                   .WithArgumentList(
                       SyntaxFactory.ArgumentList(
                           SyntaxFactory.SeparatedList <ArgumentSyntax>(
                               new SyntaxNodeOrToken[]
            {
                SyntaxFactory.Argument(
                    SyntaxFactory.InvocationExpression(
                        SyntaxFactory.MemberAccessExpression(
                            SyntaxKind.SimpleMemberAccessExpression,
                            SyntaxFactory.IdentifierName("Assert"),
                            SyntaxFactory.GenericName(
                                SyntaxFactory.Identifier("ThrowsException"))
                            .WithTypeArgumentList(
                                SyntaxFactory.TypeArgumentList(
                                    SyntaxFactory
                                    .SingletonSeparatedList <TypeSyntax>(
                                        SyntaxFactory.IdentifierName("Exception"))))))
                    .WithArgumentList(BuildArgumentList(expression, additionalArguments))),
                SyntaxFactory.Token(SyntaxKind.CommaToken),
                SyntaxFactory.Argument(
                    SyntaxFactory.TypeOfExpression(
                        SyntaxFactory.IdentifierName(details.TypeName)))
            }))));
        }
        public static InvocationExpressionSyntax ThrowsExceptionSyntax(
            ExpressionSyntax expression,
            ExceptionSyntaxDetails details,
            SeparatedSyntaxList <ArgumentSyntax>?additionalArguments = null)
        {
            if (details == null)
            {
                throw new ArgumentNullException(nameof(details));
            }
            if (details.Inconclusive)
            {
                throw new InvalidOperationException();
            }

            if (details.MatchType == MatchType.None)
            {
                return(ThrowsExceptionNaked(expression, details, additionalArguments));
            }

            return(ThrowsExceptionWithMatch(expression, details, additionalArguments));
        }
        public static InvocationExpressionSyntax ThrowsExceptionWithMatch(
            ExpressionSyntax expression, ExceptionSyntaxDetails details,
            SeparatedSyntaxList <ArgumentSyntax>?additionalArguments = null)
        {
            string type = "StringAssert";
            string method;
            var    matchTypeArgument = details.MatchTypeArguments.Arguments[0];

            switch (details.MatchType)
            {
            case MatchType.Matches:
                method            = "Matches";
                matchTypeArgument = SyntaxFactory.Argument(
                    SyntaxExtensions.CreateInstance(typeof(Regex).FullName,
                                                    details.MatchTypeArguments.Arguments[0])).NormalizeWhitespace();
                break;

            case MatchType.EqualTo:
                type   = "Assert";
                method = "AreEqual";
                break;

            case MatchType.Contains:
                method = "Contains";
                break;

            case MatchType.StartsWith:
                method = "StartsWith";
                break;

            case MatchType.EndsWith:
                method = "EndsWith";
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            var matchTargetArgument = SyntaxFactory.IdentifierName(details.MatchTarget);

            switch (details.MatchTarget)
            {
            case "Property":
                matchTargetArgument = SyntaxFactory.IdentifierName(
                    details.MatchTargetArguments.Arguments[0].Expression.ToString());
                break;
            }

            var argumentList = new SeparatedSyntaxList <ArgumentSyntax>();

            argumentList = argumentList.Add(
                SyntaxFactory.Argument(
                    SyntaxFactory.MemberAccessExpression(
                        SyntaxKind.SimpleMemberAccessExpression,
                        ThrowsExceptionNaked(expression, details, additionalArguments),
                        matchTargetArgument
                        )));
            argumentList = argumentList.Add(matchTypeArgument);

            return(AssertOperation(type, method)
                   .WithArgumentList(SyntaxFactory.ArgumentList(argumentList)));
        }