예제 #1
0
        /// <summary>
        /// Checks whether the value contained in the IEnsureArg instance is less than or equal to the
        /// specified other value.
        /// </summary>
        /// <typeparam name="T">The type of the value contained in the IEnsureArg instance.</typeparam>
        /// <param name="ensureArg">
        /// The IEnsureArg instance, usually created from an Ensure.Arg() call.
        /// </param>
        /// <param name="other">The value to compare against.</param>
        /// <param name="exceptionMessage">
        /// Optional exception message to use if the null check fails. This exception message will
        /// override the message supplied in the Ensure.Arg() call, if any.
        /// </param>
        /// <returns>The ensureArg instance.</returns>
        /// <exception cref="System.ArgumentNullException">ensureArg.Value is null.</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// ensureArg.Value is greater than other.
        /// </exception>
        public static IEnsureArg <T> IsLessThanOrEqualTo <T>(
            this IEnsureArg <T> ensureArg,
            T other,
            string exceptionMessage = null) where T : IComparable <T>
        {
            ensureArg.ValidateEnsureArgIsNotNull();

            if (!ensureArg.Value.IsLessThanOrEqualTo(other))
            {
                ensureArg.ThrowArgumentOutOfRangeException(other, exceptionMessage);
            }

            return(ensureArg);
        }
예제 #2
0
        /// <summary>
        /// Checks whether the value contained in the IEnsureArg instance is between the specified min
        /// and max values inclusively.
        /// </summary>
        /// <example>
        /// <para>int a = 2;</para>
        /// <para>Ensure.Arg(a).IsBetween(1, 3); // Doesn't throw.</para>
        /// <para>Ensure.Arg(a).IsBetween(2, 3); // Throws ArgumentOutOfRangeException.</para>
        /// <para>Ensure.Arg(a).IsBetween(1, 2); // Throws ArgumentOutOfRangeException.</para>
        /// <para>Ensure.Arg(a).IsBetweenOrEqualTo(1, 3); // Doesn't throw.</para>
        /// <para>Ensure.Arg(a).IsBetweenOrEqualTo(2, 3); // Doesn't throw.</para>
        /// <para>Ensure.Arg(a).IsBetweenOrEqualTo(1, 2); // Doesn't throw.</para>
        /// <para>Ensure.Arg(a).IsBetweenOrEqualTo(3, 4); // Throws ArgumentOutOfRangeException.</para>
        /// </example>
        /// <typeparam name="T">The type of the value contained in the IEnsureArg instance.</typeparam>
        /// <param name="ensureArg">
        /// The IEnsureArg instance, usually created from an Ensure.Arg() call.
        /// </param>
        /// <param name="min">The minimum value.</param>
        /// <param name="max">The maximum value.</param>
        /// <param name="exceptionMessage">
        /// Optional exception message to use if the null check fails. This exception message will
        /// override the message supplied in the Ensure.Arg() call, if any.
        /// </param>
        /// <returns>The ensureArg instance.</returns>
        /// <exception cref="System.ArgumentNullException">ensureArg.Value is null.</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// ensureArg.Value is outside the range of min and max.
        /// </exception>
        public static IEnsureArg <T> IsBetweenOrEqualTo <T>(
            this IEnsureArg <T> ensureArg,
            T min,
            T max,
            string exceptionMessage = null) where T : IComparable <T>
        {
            ensureArg.ValidateEnsureArgIsNotNull();

            if (!ensureArg.Value.IsBetweenOrEqualTo(min, max))
            {
                ensureArg.ThrowArgumentOutOfRangeException(min, max, exceptionMessage);
            }

            return(ensureArg);
        }
예제 #3
0
        public void When_an_ArgumentOutOfRangeException_is_thrown_with_a_null_value()
        {
            // Arrange.
            IEnsureArg <object> ensureArg = Substitute.For <IEnsureArg <object> >();

            ensureArg.ArgumentName.Returns((string)null);

            // Act.
            Action action = () =>
                            ensureArg.ThrowArgumentOutOfRangeException(null, null, null);

            // Assert.
            action.ShouldThrow <ArgumentOutOfRangeException>()
            .And
            .Message.Should().NotBeNull();
        }
예제 #4
0
        public void When_an_ArgumentOutOfRangeException_is_thrown_named_place_holders_are_correct()
        {
            // Arrange.
            IEnsureArg <int> ensureArg = Substitute.For <IEnsureArg <int> >();

            ensureArg.Value.Returns(3);
            ensureArg.ArgumentName.Returns("myInt");

            // Act.
            Action action = () =>
                            ensureArg.ThrowArgumentOutOfRangeException(1, 5, "Expected {argName} to be between {min} and {max} but was {arg}");

            // Assert.
            action.ShouldThrow <ArgumentOutOfRangeException>()
            .And
            .Message.Should().StartWith("Expected myInt to be between 1 and 5 but was 3");
        }
예제 #5
0
        public void When_an_ArgumentOutOfRangeException_is_thrown_for_an_other_value_with_a_message()
        {
            // Arrange.
            IEnsureArg <object> ensureArg = Substitute.For <IEnsureArg <object> >();

            ensureArg.ArgumentName.Returns("value");
            ensureArg.ExceptionMessage.Returns("{argName} needed to be some {other} but was {arg} ");
            ensureArg.Value.Returns("awesome");

            // Act.
            Action action = () =>
                            ensureArg.ThrowArgumentOutOfRangeException("otherValue");

            // Assert.
            action.ShouldThrow <ArgumentOutOfRangeException>()
            .And
            .Message.Should().StartWith("value needed to be some otherValue but was awesome");
        }