/// <summary>
        /// Create a Assert.Throws assert to check if a method throws and exception.
        /// </summary>
        /// <param name="variableReference">The reference chain to check for exception.</param>
        /// <param name="exception">The expected exception type.</param>
        /// <param name="message">Message if test fails.</param>
        /// <returns>The declared expression statement syntax.</returns>
        public static ExpressionStatementSyntax Throws(VariableReference variableReference, Type exception, string message = null)
        {
            if (variableReference == null)
            {
                throw new ArgumentNullException(nameof(variableReference));
            }

            if (exception == null)
            {
                throw new ArgumentNullException(nameof(exception));
            }

            if (!(variableReference is MethodReference))
            {
                var member = variableReference.GetLastMember();
                if (!(member is MethodReference))
                {
                    throw new ArgumentException($"{variableReference} or last member in chain must be a method");
                }
            }

            var arguments = new List <IArgument>
            {
                new ParenthesizedLambdaArgument(Statement.Expression.Invoke(variableReference).AsExpression()),
                new ValueArgument(message ?? string.Empty)
            };

            return(Statement.Expression.Invoke("Assert", "Throws", arguments, new List <Type>()
            {
                exception
            }).AsStatement());
        }
        /// <summary>
        /// Create the expression statement syntax to invoke a method in a chain (for example <c>myVariable.myProperty.MyMethod()</c>)
        /// </summary>
        /// <param name="reference">The reference chain.</param>
        /// <returns>A invocation object with both statement and expression.</returns>
        public Invocation Invoke(VariableReference reference)
        {
            if (reference == null)
            {
                throw new ArgumentNullException(nameof(reference));
            }

            if (!(reference is MethodReference))
            {
                var member = reference.GetLastMember();
                if (!(member is MethodReference))
                {
                    throw new ArgumentException($"{nameof(reference)} or last member in chain must be a method reference");
                }
            }

            return(new Invocation((InvocationExpressionSyntax)ReferenceGenerator.Create(reference)));
        }
Beispiel #3
0
        /// <summary>
        /// Create the expression statement syntax to assign a reference to another reference. For example a property to a property.
        /// </summary>
        /// <param name="reference">Reference that should be assigned.</param>
        /// <param name="valueReference">Reference that we should assign to another reference.</param>
        /// <param name="castTo">If we should do a cast while assign the variabl.</param>
        /// <returns>The generated assign declaration syntax.</returns>
        public ExpressionStatementSyntax Assign(VariableReference reference, VariableReference valueReference, Type castTo = null)
        {
            if (reference == null)
            {
                throw new ArgumentNullException(nameof(reference));
            }

            if (valueReference == null)
            {
                throw new ArgumentNullException(nameof(valueReference));
            }

            if (reference is MethodReference || reference.GetLastMember() is MethodReference)
            {
                throw new ArgumentException($"{nameof(reference)} to assign can't be a method");
            }

            return(Assign(reference, ReferenceGenerator.Create(valueReference), castTo));
        }