Ejemplo n.º 1
0
        public static IEnsureArg <IUser> IsAuthenticated(this IEnsureArg <IUser> ensureArg, string exceptionMessage = null)
        {
            ensureArg.ValidateEnsureArgIsNotNull();

            if (!ensureArg.Value.IsAuthenticated)
            {
                ensureArg.ThrowArgumentException(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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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");
        }