Exemple #1
0
        /// <summary>
        /// Verifies the behavior of the command when invoked with <see cref="Guid.Empty" />.
        /// </summary>
        /// <param name="command">The command whose behavior must be examined.</param>
        /// <remarks>
        /// <para>
        /// This method encapsulates the behavior which is expected when a method or constructor is
        /// invoked with <see cref="Guid.Empty" /> as one of the method arguments. In that case it's
        /// expected that invoking <paramref name="command" /> with Guid.Empty throws an
        /// <see cref="ArgumentException" />, causing the Verify method to succeed. If other
        /// exceptions are thrown, or no exception is thrown when invoking the command, the Verify
        /// method throws an exception.
        /// </para>
        /// </remarks>
        public void Verify(IGuardClauseCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            if (command.RequestedType != typeof(Guid))
            {
                return;
            }

            try
            {
                command.Execute(Guid.Empty);
            }
            catch (ArgumentException)
            {
                return;
            }
            catch (Exception e)
            {
                throw command.CreateException("\"Guid.Empty\"", e);
            }

            throw command.CreateException("\"Guid.Empty\"");
        }
        /// <summary>
        /// Verifies the behavior of the command when invoked with <see cref="Double" /> less than 0.
        /// </summary>
        /// <param name="command">The command whose behavior must be examined.</param>
        /// <remarks>
        /// <para>
        /// This method encapsulates the behavior which is expected when a method or constructor is
        /// invoked with <see cref="Double" /> less than 0 as one of the method arguments. In that case it's
        /// expected that invoking <paramref name="command" /> with Double less than 0 throws an
        /// <see cref="ArgumentException" />, causing the Verify method to succeed. If other
        /// exceptions are thrown, or no exception is thrown when invoking the command, the Verify
        /// method throws an exception.
        /// </para>
        /// </remarks>
        public void Verify(IGuardClauseCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (command.RequestedType != typeof(decimal))
            {
                return;
            }

            try
            {
                command.Execute(Convert.ToDecimal(Convert.ToDouble(new Random().Next(int.MinValue, -1))));
            }
            catch (ArgumentOutOfRangeException)
            {
                return;
            }
            catch (Exception e)
            {
                throw command.CreateException("\"Decimal.Negative\"", e);
            }

            throw command.CreateException("\"Decimal.Negative\"");
        }
        /// <summary>
        /// Verifies that the command behaves correct when invoked with a null argument.
        /// </summary>
        /// <param name="command">The command whose behavior must be examined.</param>
        /// <remarks>
        /// <para>
        /// The Verify method attempts to invoke the <paramref name="command" /> instance's
        /// <see cref="IGuardClauseCommand.Execute" /> with <see langword="null" />. The expected
        /// result is that this action throws an <see cref="ArgumentNullException" /> with proper parameter name,
        /// in which case the expected behavior is considered verified. If any other exception is thrown, or
        /// if no exception is thrown at all, the verification fails and an exception is thrown.
        /// </para>
        /// <para>
        /// The behavior is only asserted if the command's
        /// <see cref="IGuardClauseCommand.RequestedType" /> is nullable. In case of value types,
        /// no action is performed.
        /// </para>
        /// </remarks>
        public void Verify(IGuardClauseCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (!command.RequestedType.IsClass &&
                !command.RequestedType.IsInterface)
            {
                return;
            }

            try
            {
                command.Execute(null);
            }
            catch (ArgumentNullException e)
            {
                if (string.Equals(e.ParamName, command.RequestedParameterName, StringComparison.Ordinal))
                {
                    return;
                }
                throw command.CreateException("null", e);
            }
            catch (Exception e)
            {
                throw command.CreateException("null", e);
            }

            throw command.CreateException("null");
        }
        /// <summary>
        /// Verifies that the command behaves correct when invoked with a null argument.
        /// </summary>
        /// <param name="command">The command whose behavior must be examined.</param>
        /// <remarks>
        /// <para>
        /// The Verify method attempts to invoke the <paramref name="command" /> instance's
        /// <see cref="IGuardClauseCommand.Execute" /> with <see langword="null" />. The expected
        /// result is that this action throws an <see cref="ArgumentNullException" />, in which
        /// case the expected behavior is considered verified. If any other exception is thrown, or
        /// if no exception is thrown at all, the verification fails and an exception is thrown.
        /// </para>
        /// <para>
        /// The behavior is only asserted if the command's
        /// <see cref="IGuardClauseCommand.RequestedType" /> is nullable. In case of value types,
        /// no action is performed.
        /// </para>
        /// </remarks>
        public void Verify(IGuardClauseCommand command)
        {
            if (command == null)
                throw new ArgumentNullException("command");

            if (!command.RequestedType.IsClass
                && !command.RequestedType.IsInterface)
            {
                return;
            }

            try
            {
                command.Execute(null);
            }
            catch (ArgumentNullException)
            {
                return;
            }
            catch (Exception e)
            {
                throw command.CreateException("null", e);
            }

            throw command.CreateException("null");
        }
Exemple #5
0
        /// <summary>
        /// Verifies that the command behaves correct when invoked with a null argument.
        /// </summary>
        /// <param name="command">The command whose behavior must be examined.</param>
        /// <remarks>
        /// <para>
        /// The Verify method attempts to invoke the <paramref name="command" /> instance's
        /// <see cref="IGuardClauseCommand.Execute" /> with <see langword="null" />. The expected
        /// result is that this action throws an <see cref="ArgumentNullException" />, in which
        /// case the expected behavior is considered verified. If any other exception is thrown, or
        /// if no exception is thrown at all, the verification fails and an exception is thrown.
        /// </para>
        /// <para>
        /// The behavior is only asserted if the command's
        /// <see cref="IGuardClauseCommand.RequestedType" /> is nullable. In case of value types,
        /// no action is performed.
        /// </para>
        /// </remarks>
        public void Verify(IGuardClauseCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            if (!command.RequestedType.IsClass &&
                !command.RequestedType.IsInterface)
            {
                return;
            }

            try
            {
                command.Execute(null);
            }
            catch (ArgumentNullException)
            {
                return;
            }
            catch (Exception e)
            {
                throw command.CreateException("null", e);
            }

            throw command.CreateException("null");
        }
        /// <summary>
        /// Verifies that the command behaves correct when invoked with a null argument.
        /// </summary>
        /// <param name="command">The command whose behavior must be examined.</param>
        /// <remarks>
        /// <para>
        /// The Verify method attempts to invoke the <paramref name="command" /> instance's
        /// <see cref="IGuardClauseCommand.Execute" /> with <see langword="null" />. The expected
        /// result is that this action throws an <see cref="ArgumentNullException" /> with proper parameter name,
        /// in which case the expected behavior is considered verified. If a <see cref="string" /> argument is provided and
        /// an <see cref="ArgumentException"/> is thrown the expected behavior is also considered verified. If any other
        /// exception is thrown, or if no exception is thrown at all, the verification fails and an exception is thrown.
        /// </para>
        /// <para>
        /// The behavior is only asserted if the command's
        /// <see cref="IGuardClauseCommand.RequestedType" /> is nullable. In case of value types,
        /// no action is performed.
        /// </para>
        /// </remarks>
        public void Verify(IGuardClauseCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (!command.RequestedType.IsClass &&
                !command.RequestedType.IsInterface)
            {
                return;
            }

            try
            {
                command.Execute(null);
            }
            catch (Exception exception) when
                (exception is ArgumentNullException ||
                (exception is ArgumentException && command.RequestedType == typeof(string)))
            {
                var castedException = exception is ArgumentNullException
                    ? exception as ArgumentNullException
                    : exception as ArgumentException;

                if (string.Equals(castedException !.ParamName, command.RequestedParameterName, StringComparison.Ordinal))
                {
                    return;
                }

                throw command.CreateException(
                          "<nullOrEmpty>",
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "Guard Clause prevented it, however the thrown exception contains invalid parameter name. Ensure you pass correct parameter name to the ArgumentNullException constructor.{0}. Expected parameter name: {1}{0}Actual parameter name: {2}",
                              Environment.NewLine,
                              command.RequestedParameterName,
                              castedException.ParamName),
                          exception);
            }
            catch (Exception e)
            {
                throw command.CreateException("null", e);
            }

            throw command.CreateException("null");
        }
Exemple #7
0
        public void Verify(IGuardClauseCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (!command.RequestedType.IsClass &&
                !command.RequestedType.IsInterface)
            {
                return;
            }

            try
            {
                command.Execute(null);
            }
            catch (ArgumentNullException e)
            {
                if (string.Equals(e.ParamName, command.RequestedParameterName, StringComparison.Ordinal))
                {
                    return;
                }

                throw command.CreateException(
                          "<null>",
                          string.Format(CultureInfo.InvariantCulture,
                                        "Guard Clause prevented it, however the thrown exception contains invalid parameter name. " +
                                        "Ensure you pass correct parameter name to the ArgumentNullException constructor.{0}" +
                                        "Expected parameter name: {1}{0}Actual parameter name: {2}",
                                        Environment.NewLine,
                                        command.RequestedParameterName,
                                        e.ParamName),
                          e);
            }
            catch (ArgumentOutOfRangeException)
            {
                return;
            }

            throw command.CreateException("null");
        }
Exemple #8
0
        /// <summary>
        /// Verifies the behavior of the command when invoked with <see cref="Int32" /> less than 0.
        /// </summary>
        /// <param name="command">The command whose behavior must be examined.</param>
        /// <remarks>
        /// <para>
        /// This method encapsulates the behavior which is expected when a method or constructor is
        /// invoked with <see cref="Int32" /> less than 0 as one of the method arguments. In that case it's
        /// expected that invoking <paramref name="command" /> with Int32 less than 0 throws an
        /// <see cref="ArgumentException" />, causing the Verify method to succeed. If other
        /// exceptions are thrown, or no exception is thrown when invoking the command, the Verify
        /// method throws an exception.
        /// </para>
        /// </remarks>
        public void Verify(IGuardClauseCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (command.RequestedType != typeof(int))
            {
                return;
            }

            try
            {
                var random = new Random();
                if (_exceptions.Length != 0)
                {
                    var value = random.Next(int.MinValue, -1);
                    while (_exceptions.Contains(value))
                    {
                        value = random.Next(int.MinValue, -1);
                    }
                    command.Execute(value);
                }
                else
                {
                    command.Execute(random.Next(int.MinValue, -1));
                }
            }
            catch (ArgumentOutOfRangeException)
            {
                return;
            }
            catch (Exception e)
            {
                throw command.CreateException("\"Int32.Negative\"", e);
            }

            throw command.CreateException("\"Int32.Negative\"");
        }
        /// <summary>
        /// Verifies the behavior of the command when invoked with <see cref="Guid.Empty" />.
        /// </summary>
        /// <param name="command">The command whose behavior must be examined.</param>
        /// <remarks>
        /// <para>
        /// This method encapsulates the behavior which is expected when a method or constructor is
        /// invoked with <see cref="Guid.Empty" /> as one of the method arguments. In that case it's
        /// expected that invoking <paramref name="command" /> with Guid.Empty throws an
        /// <see cref="ArgumentException" />, causing the Verify method to succeed. If other
        /// exceptions are thrown, or no exception is thrown when invoking the command, the Verify
        /// method throws an exception.
        /// </para>
        /// </remarks>
        public void Verify(IGuardClauseCommand command)
        {
            if (command == null)
                throw new ArgumentNullException("command");
            
            if (command.RequestedType != typeof(Guid))
                return;

            try
            {
                command.Execute(Guid.Empty);
            }
            catch (ArgumentException)
            {
                return;
            }
            catch (Exception e)
            {
                throw command.CreateException("\"Guid.Empty\"", e);
            }

            throw command.CreateException("\"Guid.Empty\"");
        }
    public void Verify(IGuardClauseCommand command)
    {
        if (!command.RequestedType.IsClass &&
            !command.RequestedType.IsInterface)
        {
            return;
        }

        try
        {
            command.Execute(" ");
        }
        catch (ArgumentException)
        {
            return;
        }
        catch (Exception e)
        {
            throw command.CreateException("whitespace", e);
        }

        throw command.CreateException("whitespace");
    }