示例#1
0
        /// <summary>
        /// Checks that the collection's count is less than the specified number.
        /// </summary>
        /// <typeparam name="T">The type of the collection.</typeparam>
        /// <param name="argument">The argument under validation.</param>
        /// <param name="lessThanCount">The comparison value for the Less Than Check.</param>
        /// <param name="message">An optional exception message that overrides the default.</param>
        /// <returns>An <see cref="ICollectionAndArgument{T}"/> providing access to the argument's value and further checks.</returns>
        /// <exception cref="ArgumentNullException">If the collection is null.</exception>
        /// <exception cref="ArgumentException">If the collection's count is not less than the specified number.</exception>
        public static ICollectionAndArgument <T> CountLessThan <T>(this CollectionArgument <T> argument, int lessThanCount, string message = null)
            where T : ICollection
        {
            if (Equals(argument.Value, null))
            {
                throw new ArgumentNullException(argument.Name, message ?? $"{argument.Name} count must be less than {lessThanCount} actual value null");
            }

            if (argument.Value.Count >= lessThanCount)
            {
                throw new ArgumentException(message ?? $"{argument.Name} count must be less than {lessThanCount} actual value {argument.Value.Count}", argument.Name);
            }

            return(argument);
        }
示例#2
0
        /// <summary>
        /// Checks that the collection is not empty.
        /// </summary>
        /// <typeparam name="T">The type of collection.</typeparam>
        /// <param name="argument">The argument under validation.</param>
        /// <param name="message">An optional exception message that overrides the default.</param>
        /// <returns>An <see cref="ICollectionAndArgument{T}"/> providing access to the argument's value and further checks.</returns>
        /// <exception cref="ArgumentNullException">If the collection is null.</exception>
        /// <exception cref="ArgumentException">If the collection is empty.</exception>
        public static ICollectionAndArgument <T> NotEmpty <T>(this CollectionArgument <T> argument, string message = null)
            where T : ICollection
        {
            if (Equals(argument.Value, null))
            {
                throw new ArgumentNullException(argument.Name, message ?? $"{argument.Name} cannot be null.");
            }

            if (Equals(argument.Value.Count, 0))
            {
                throw new ArgumentException(message ?? $"{argument.Name} cannot be empty.", argument.Name);
            }

            return(argument);
        }
示例#3
0
        /// <summary>
        /// Checks that the collection contains a specified number of items.
        /// </summary>
        /// <typeparam name="T">The type of collections.</typeparam>
        /// <param name="argument">The argument under validation.</param>
        /// <param name="expectedCount">The expected count of items in the collection.</param>
        /// <param name="message">An optional exception message that overrides the default.</param>
        /// <returns>An <see cref="ICollectionAndArgument{T}"/> providing access to the argument's value and further checks.</returns>
        /// <exception cref="ArgumentNullException">If the collection is null.</exception>
        /// <exception cref="ArgumentException">If the collection does not contain the specified number of items.</exception>
        public static ICollectionAndArgument <T> Count <T>(this CollectionArgument <T> argument, int expectedCount, string message = null)
            where T : ICollection
        {
            if (Equals(argument.Value, null))
            {
                throw new ArgumentNullException(argument.Name, message ?? $"{argument.Name} expected count: {expectedCount} actual: null");
            }

            if (Equals(argument.Value.Count, expectedCount) == false)
            {
                throw new ArgumentException(message ?? $"{argument.Name} expected count: {expectedCount} actual: {argument.Value.Count}", argument.Name);
            }

            return(argument);
        }
示例#4
0
        /// <summary>
        /// Checks that the collection contains a specified item.
        /// </summary>
        /// <typeparam name="T">The type of the collection.</typeparam>
        /// <param name="argument">The argument under validation.</param>
        /// <param name="item">The item to appear in the collection.</param>
        /// <param name="message">An optional exception message that overrides the default.</param>
        /// <returns>An <see cref="ICollectionAndArgument{T}"/> providing access to the argument's value and further checks.</returns>
        /// <exception cref="ArgumentException">If the collection does not contain the specified item.</exception>
        public static ICollectionAndArgument <T> ContainingItem <T>(this CollectionArgument <T> argument, object item, string message = null)
            where T : ICollection
        {
            if (Equals(argument.Value, null))
            {
                throw new ArgumentNullException(argument.Name, message ?? $"{argument.Name} does not contain provided item");
            }

            if (argument.Value.Cast <object>().All(i => Equals(i, item) == false))
            {
                throw new ArgumentException(message ?? $"{argument.Name} does not contain provided item", argument.Name);
            }

            return(argument);
        }