Пример #1
0
 /// <summary>
 /// Validates that the IEnsureArg instance is not null.
 /// </summary>
 /// <typeparam name="T">The type parameter of the IEnsureArg instance.</typeparam>
 /// <param name="ensureArg">The IEnsureArg instance to check.</param>
 public static void ValidateEnsureArgIsNotNull <T>(this IEnsureArg <T> ensureArg)
 {
     if (ensureArg == null)
     {
         throw new ArgumentNullException("ensureArg");
     }
 }
Пример #2
0
        public static IEnsureArg <MyConvariantTestBaseClass> HasValidBaseProperty(this IEnsureArg <MyConvariantTestBaseClass> ensureArg)
        {
            if (ensureArg.Value.BaseProperty != "BaseProperty")
            {
                throw new ArgumentException();
            }

            return(ensureArg);
        }
Пример #3
0
        public static IEnsureArg <IUser> IsAuthenticated(this IEnsureArg <IUser> ensureArg, string exceptionMessage = null)
        {
            ensureArg.ValidateEnsureArgIsNotNull();

            if (!ensureArg.Value.IsAuthenticated)
            {
                ensureArg.ThrowArgumentException(exceptionMessage);
            }

            return(ensureArg);
        }
Пример #4
0
        public void When_Ensure_Arg_is_called_with_value_param()
        {
            // Arrange.
            object testObject = new object();

            // Act.
            IEnsureArg <object> ensureArg = Ensure.Arg(testObject);

            // Assert.
            ensureArg.Value.Should().BeSameAs(testObject);
        }
Пример #5
0
        /// <summary>
        /// Checks whether the value contained in the IEnsureArg instance is not null. If it is null
        /// then an ArgumentNullException will be thrown.
        /// </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="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>
        public static IEnsureArg <T> IsNotNull <T>(this IEnsureArg <T> ensureArg, string exceptionMessage = null)
            where T : class
        {
            ensureArg.ValidateEnsureArgIsNotNull();

            if (ensureArg.Value == null)
            {
                ensureArg.ThrowArgumentNullException(exceptionMessage);
            }

            return(ensureArg);
        }
Пример #6
0
        /// <summary>
        /// Checks whether the value contained in the IEnsureArg instance is not null. If it is null
        /// then an ArgumentNullException will be thrown.
        /// </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="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>
        public static IEnsureArg <T?> IsNotNull <T>(this IEnsureArg <T?> ensureArg, string exceptionMessage = null)
            where T : struct
        {
            ensureArg.ValidateEnsureArgIsNotNull();

            if (!ensureArg.Value.HasValue)
            {
                ensureArg.ThrowArgumentNullException(exceptionMessage);
            }

            return(ensureArg);
        }
        public static IEnsureArg <ushort> IsNotDefaultValue(
            this IEnsureArg <ushort> ensureArg,
            string exceptionMessage = null)
        {
            ensureArg.ValidateEnsureArgIsNotNull();
            if (!ensureArg.Value.IsNotDefaultValue())
            {
                ensureArg.ThrowArgumentException(exceptionMessage);
            }

            return(ensureArg);
        }
Пример #8
0
        public void When_And_is_called_the_same_EnsureArg_is_returned()
        {
            // Arrange.
            object value = new object();

            IEnsureArg <object> ensureArg = Ensure.Arg(value);

            // Act.
            IEnsureArg <object> returnedEnsureArg = ensureArg.And();

            // Assert.
            ensureArg.Should().BeSameAs(returnedEnsureArg);
        }
Пример #9
0
        /// <summary>
        /// Checks whether the value contained in the IEnsureArg instance is null or contains no
        /// items. If it is null then an ArgumentNullException will be thrown. If the collection is
        /// empty then an ArgumentException will be thrown.
        /// </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="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.ArgumentException">
        /// ensureArg.Value contain no items in the collection.
        /// </exception>
        public static IEnsureArg <IEnumerable <T> > IsNotNullOrEmpty <T>(
            this IEnsureArg <IEnumerable <T> > ensureArg,
            string exceptionMessage = null)
        {
            ensureArg.IsNotNull(exceptionMessage);

            if (ensureArg.Value.IsNullOrEmpty())
            {
                ensureArg.ThrowArgumentException(exceptionMessage);
            }

            return(ensureArg);
        }
        public void When_IsNotNull_is_called_it_returns_the_EnsureArg_created_from_Ensure_Arg()
        {
            // Arrange.
            object value = new object();

            IEnsureArg <object> ensureArg = Ensure.Arg(value);

            // Act.
            IEnsureArg <object> returnedEnsureArg = ensureArg.IsNotNull();

            // Assert.
            ensureArg.Should().BeSameAs(returnedEnsureArg);
        }
Пример #11
0
        public void When_Ensure_Arg_is_called_with_value_and_name_and_message_params()
        {
            // Arrange.
            object testObject = new object();

            // Act.
            IEnsureArg <object> ensureArg = Ensure.Arg(testObject, "thisIsATestObject", "my custom message");

            // Assert.
            ensureArg.Value.Should().BeSameAs(testObject);
            ensureArg.ArgumentName.Should().Be("thisIsATestObject");
            ensureArg.ExceptionMessage.Should().Be("my custom message");
        }
Пример #12
0
        /// <summary>
        /// Checks whether the value contained in the IEnsureArg instance is not null, empty, or
        /// consists only of white-space characters. If it is null then an ArgumentNullException will
        /// be thrown. If the string is empty or contains only white-space characters then an
        /// ArgumentException will be thrown.
        /// </summary>
        /// <param name="ensureArg">
        /// The IEnsureArg instance, usually created from an Ensure.Arg() call.
        /// </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.ArgumentException">
        /// ensureArg.Value is an empty string ("") or only contains white-space characters.
        /// </exception>
        public static IEnsureArg <string> IsNotNullOrWhiteSpace(
            this IEnsureArg <string> ensureArg,
            string exceptionMessage = null)
        {
            ensureArg.IsNotNull(exceptionMessage);

            if (string.IsNullOrWhiteSpace(ensureArg.Value))
            {
                ensureArg.ThrowArgumentException(exceptionMessage);
            }

            return(ensureArg);
        }
Пример #13
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);
        }
Пример #14
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);
        }
Пример #15
0
        /// <summary>
        /// Throws a System.ComponentModel.InvalidEnumArgumentException with the specified message.
        /// The parameter name for the exception will be the value of ensureArg.ArgumentName.
        /// </summary>
        /// <typeparam name="TEnum">The type parameter of the IEnsureArg instance.</typeparam>
        /// <param name="ensureArg">The IEnsureArg instance to throw an exception for.</param>
        /// <param name="exceptionMessage">
        /// The message to use in the exception. If no exception message is supplied then
        /// ensureArg.ExceptionMessage will be used.
        /// </param>
        public static void ThrowInvalidEnumArgumentException <TEnum>(
            this IEnsureArg <TEnum> ensureArg,
            string exceptionMessage)
            where TEnum : struct, IComparable, IFormattable // Closest we can get to System.Enum and be CLSCompliant.
        {
            string message = ensureArg.FormatInvalidEnumArgumentException(exceptionMessage);

            if (message == null)
            {
                int enumValue = Convert.ToInt32(ensureArg.Value, CultureInfo.InvariantCulture);
                throw new InvalidEnumArgumentException(ensureArg.ArgumentName, enumValue, ensureArg.Value.GetType());
            }

            throw new InvalidEnumArgumentException(message);
        }
Пример #16
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();
        }
Пример #17
0
        public void When_a_InvalidEnumArgumentException_is_thrown_with_a_message()
        {
            // Arrange.
            IEnsureArg <MyTestEnum> ensureArg = Substitute.For <IEnsureArg <MyTestEnum> >();

            ensureArg.Value.Returns((MyTestEnum)(-1));
            ensureArg.ArgumentName.Returns("myEnum");

            // Act.
            Action action = () =>
                            ensureArg.ThrowInvalidEnumArgumentException("my custom message for an enum of type {enumType.Name}");

            // Assert.
            action.ShouldThrow <InvalidEnumArgumentException>()
            .And
            .Message.Should().Be("my custom message for an enum of type MyTestEnum");
        }
Пример #18
0
        public void When_an_ArgumentException_is_thrown()
        {
            // Arrange.
            IEnsureArg <object> ensureArg = Substitute.For <IEnsureArg <object> >();

            ensureArg.Value.Returns(new object());
            ensureArg.ArgumentName.Returns("myObject");

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

            // Assert.
            action.ShouldThrow <ArgumentException>()
            .And
            .ParamName.Should().Be("myObject");
        }
Пример #19
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");
        }
Пример #20
0
        public void When_Ensure_Arg_is_called_with_a_null_argument_name()
        {
            // Arrange.
            object testObject = null;

            IEnsureArg <object> ensureArg = Ensure.Arg(testObject, null);

            // Act.
            Action action = () =>
                            ensureArg.IsNotNull();

            // Assert.
            ensureArg.ArgumentName.Should().Be(null);

            action.ShouldThrow <ArgumentNullException>()
            .And.ParamName.Should().BeNull();
        }
Пример #21
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");
        }
Пример #22
0
        /// <summary>
        /// Creates an exception message appropriate for an ArgumentException given the
        /// context of the ensureArg instance.
        /// </summary>
        /// <typeparam name="T">The type parameter of the IEnsureArg instance.</typeparam>
        /// <param name="ensureArg">The IEnsureArg instance to format an exception message for.</param>
        /// <param name="exceptionMessage">An exception message to use which may provide place holders.
        /// The available place holders are: {argName} = ensureArg.ArgumentName, {arg} = ensureArg.Value.
        /// If this parameter is null the ensureArg.Exception message will be used.</param>
        /// <returns>A formatted exception message or null if no exception message was found.</returns>
        public static string FormatArgumentExceptionMessage <T>(this IEnsureArg <T> ensureArg, string exceptionMessage)
        {
            ensureArg.ValidateEnsureArgIsNotNull();

            string message = exceptionMessage ?? ensureArg.ExceptionMessage;

            if (message != null)
            {
                return(formatter.Format(
                           CultureInfo.InvariantCulture,
                           message,
                           new
                {
                    argName = ensureArg.ArgumentName,
                    arg = ensureArg.Value
                }));
            }

            return(null);
        }
Пример #23
0
        public static IEnsureArg <TEnum> IsValidEnumValue <TEnum>(
            this IEnsureArg <TEnum> ensureArg,
            string exceptionMessage = null)
            where TEnum : struct, IComparable, IFormattable // Closest we can get to System.Enum and be CLSCompliant.
        {
            ensureArg.ValidateEnsureArgIsNotNull();

            Type enumType = ensureArg.Value.GetType();

            if (!enumType.IsEnum)
            {
                throw new ArgumentException("TEnum must be an enumerated type", "TEnum");
            }

            if (!Enum.IsDefined(enumType, ensureArg.Value))
            {
                ensureArg.ThrowInvalidEnumArgumentException(exceptionMessage);
            }

            return(ensureArg);
        }
Пример #24
0
 public static IEnsureArg <Apple> IsRipe(this IEnsureArg <Apple> ensureArg)
 {
     return(ensureArg);
 }
Пример #25
0
 public static IEnsureArg <Apple> IsNotEaten(this IEnsureArg <Apple> ensureArg)
 {
     return(ensureArg);
 }
 /// <summary>
 /// Allows a more English like fluent API for chaining multiple IEnsureArg extension methods
 /// together. This method does nothing but return the passed in IEnsureArg instance.
 /// </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>
 /// <returns>The ensureArg instance.</returns>
 public static IEnsureArg <T> And <T>(this IEnsureArg <T> ensureArg)
 {
     return(ensureArg);
 }
Пример #27
0
        /// <summary>
        /// Throws a System.ArgumentOutOfRangeException with the specified message. The parameter name
        /// for the exception will be the value of ensureArg.ArgumentName.
        /// </summary>
        /// <typeparam name="T">The type parameter of the IEnsureArg instance.</typeparam>
        /// <param name="ensureArg">The IEnsureArg instance to throw an exception for.</param>
        /// <param name="min">The minimum value ensureArg.Value was outside to cause this error to be thrown.</param>
        /// <param name="max">The maximum value ensureArg.Value was outside to cause this error to be thrown.</param>
        /// <param name="exceptionMessage">
        /// The message to use in the exception. If no exception message is supplied then
        /// ensureArg.ExceptionMessage will be used.
        /// </param>
        public static void ThrowArgumentOutOfRangeException <T>(this IEnsureArg <T> ensureArg, T min, T max, string exceptionMessage = null)
        {
            string message = ensureArg.FormatArgumentOutOfRangeException(min, max, exceptionMessage);

            throw new ArgumentOutOfRangeException(ensureArg.ArgumentName, ensureArg.Value, message);
        }
Пример #28
0
 /// <summary>
 /// Creates an exception message appropriate for an ArgumentNullException given the
 /// context of the ensureArg instance.
 /// </summary>
 /// <typeparam name="T">The type parameter of the IEnsureArg instance.</typeparam>
 /// <param name="ensureArg">The IEnsureArg instance to format an exception message for.</param>
 /// <param name="exceptionMessage">An exception message to use which may provide place holders.
 /// The available place holders are: {argName} = ensureArg.ArgumentName, {arg} = ensureArg.Value.
 /// If this parameter is null the ensureArg.Exception message will be used.</param>
 /// <returns>A formatted exception message or null if no exception message was found.</returns>
 public static string FormatArgumentNullExceptionMessage <T>(this IEnsureArg <T> ensureArg, string exceptionMessage)
 {
     ensureArg.ValidateEnsureArgIsNotNull();
     return(ensureArg.FormatArgumentExceptionMessage(exceptionMessage));
 }
Пример #29
0
 /// <summary>
 /// Throws a System.ArgumentNullException with the specified message. The parameter name for
 /// the exception will be the value of ensureArg.ArgumentName.
 /// </summary>
 /// <typeparam name="T">The type parameter of the IEnsureArg instance.</typeparam>
 /// <param name="ensureArg">The IEnsureArg instance to throw an exception for.</param>
 /// <param name="exceptionMessage">
 /// The message to use in the exception. If no exception message is supplied then
 /// ensureArg.ExceptionMessage will be used.
 /// </param>
 public static void ThrowArgumentNullException <T>(this IEnsureArg <T> ensureArg, string exceptionMessage)
 {
     throw new ArgumentNullException(ensureArg.ArgumentName, ensureArg.FormatArgumentNullExceptionMessage(exceptionMessage));
 }