/// <summary> /// Ensures the given <see cref="bool"/> value equals <c>false</c> /// </summary> /// <param name="argument">The context describing the value</param> /// <param name="message">An optional message to use when validation fails</param> /// <param name="paramName">An optional parameter name to use when validation fails</param> /// <returns>The given <see cref="bool"/> if validation passes</returns> /// <exception cref="ArgumentException">If the given <see cref="bool"/> not equals <c>false</c></exception> /// <exception cref="ArgumentNullException">If <paramref name="argument.Value"/> is <c>null</c></exception> public static bool IsFalse(this ArgumentContext <bool> argument, string message = null, string paramName = null) { return(Ensure.Condition( argument.Value, a => Equals(a, false), message ?? $"Expected <{argument}> to be false.", paramName)); }
/// <summary> /// Ensures the given <see cref="ICollection{T}"/> is not empty /// </summary> /// <typeparam name="T">The <see cref="Type"/> of items in the collection</typeparam> /// <param name="argument">The context describing the value</param> /// <param name="message">An optional message to use when validation fails</param> /// <param name="paramName">An optional parameter name to use when validation fails</param> /// <returns>The given <see cref="ICollection{T}"/> if validation passes</returns> /// <exception cref="ArgumentException">If the collection has no items</exception> /// <exception cref="ArgumentNullException">If <paramref name="argument.Value"/> is <c>null</c></exception> public static ICollection <T> IsNotEmpty <T>(this ArgumentContext <ICollection <T> > argument, string message = null, string paramName = null) { return(Ensure.Condition( argument.Value, a => a.Count > 0, message ?? $"Expected the collection to have an item.", paramName)); }
/// <summary> /// Ensures the given array has at least one item /// </summary> /// <typeparam name="T">The <see cref="Type"/> of items in the array</typeparam> /// <param name="argument">The context describing the array</param> /// <param name="message">An optional message to use when validation fails</param> /// <param name="paramName">An optional parameter name to use when validation fails</param> /// <returns>The given if validation passes</returns> /// <exception cref="ArgumentException">If the <see cref="Array"/> has no items</exception> /// <exception cref="ArgumentNullException">If <paramref name="argument.Value"/> is <c>null</c></exception> public static T[] IsNotEmpty <T>(this ArgumentContext <T[]> argument, string message = null, string paramName = null) { return(Ensure.Condition( argument.Value, a => a.Length > 0, message ?? "Expected the array to have an item.", paramName)); }
/// <summary> /// Ensures the given <see cref="ICollection{T}"/> has an exact amount of items, see <paramref name="count"/> /// </summary> /// <typeparam name="T">The <see cref="Type"/> of items in the collection</typeparam> /// <param name="argument">The context describing the value</param> /// <param name="count">The amount of items expected in the <see cref="ICollection{T}"/></param> /// <param name="message">An optional message to use when validation fails</param> /// <param name="paramName">An optional parameter name to use when validation fails</param> /// <returns>The given <see cref="ICollection{T}"/> if validation passes</returns> /// <exception cref="ArgumentException">If the amount of items in the <see cref="ICollection{T}"/> differs from <paramref name="count"/></exception> /// <exception cref="ArgumentNullException">If <paramref name="argument.Value"/> or <paramref name="count"/> is <c>null</c></exception> public static ICollection <T> CountIs <T>(this ArgumentContext <ICollection <T> > argument, int count, string message = null, string paramName = null) { Ensure.NotNull(count, "Count"); return(Ensure.Condition( argument.Value, a => a.Count.Equals(count), message ?? $"Expected the collection to have <{count}> items, it has <{argument.Value.Count}>.", paramName)); }
/// <summary> /// Ensures the given <see cref="ICollection{T}"/> contains <typeparamref name="T"/> <paramref name="value"/> /// </summary> /// <typeparam name="T">The <see cref="Type"/> of items in the collection</typeparam> /// <param name="argument">The context describing the value</param> /// <param name="value">The value to find in the collection</param> /// <param name="message">An optional message to use when validation fails</param> /// <param name="paramName">An optional parameter name to use when validation fails</param> /// <returns>The given <see cref="ICollection{T}"/> if validation passes</returns> /// <exception cref="ArgumentException">If the <see cref="ICollection{T}"/> does not contain <paramref name="value"/></exception> /// <exception cref="ArgumentNullException">If <paramref name="argument.Value"/> or <paramref name="value"/> is <c>null</c></exception> public static ICollection <T> Contains <T>(this ArgumentContext <ICollection <T> > argument, T value, string message = null, string paramName = null) { Ensure.NotNull(value, "Value"); return(Ensure.Condition( argument.Value, a => a.Contains(value), message ?? $"Expected to find <{value}> in the collection of <{typeof(T).Name}>", paramName)); }
/// <summary> /// Ensures the given <see cref="bool?"/> value equals <c>true</c> or <c>null</c> /// </summary> /// <param name="argument">The context describing the value</param> /// <param name="message">An optional message to use when validation fails</param> /// <param name="paramName">An optional parameter name to use when validation fails</param> /// <returns>The given <see cref="bool?"/> if validation passes</returns> /// <exception cref="ArgumentException">If the given <see cref="bool?"/> is not <c>null</c> and not equals <c>true</c></exception> public static bool?IsTrueOrNull(this ArgumentContext <bool?> argument, string message = null, string paramName = null) { return(Ensure.Condition( argument.Value, a => a == null | Equals(a, true), message ?? $"Expected <{argument}> to be true.", paramName, true)); }
/// <summary> /// Ensures the array contains <typeparamref name="T"/> <paramref name="value"/> /// </summary> /// <typeparam name="T">The <see cref="Type"/> of items in the array</typeparam> /// <param name="argument">The context describing the array</param> /// <param name="value">The value to find in the array</param> /// <param name="message">An optional message to use when validation fails</param> /// <param name="paramName">An optional parameter name to use when validation fails</param> /// <returns>The given array if validation passes</returns> /// <exception cref="ArgumentException">If the <see cref="Array"/> does not contain <paramref name="value"/></exception> /// <exception cref="ArgumentNullException">If <paramref name="argument.Value"/> or <paramref name="value"/> is <c>null</c></exception> public static T[] Contains <T>(this ArgumentContext <T[]> argument, T value, string message = null, string paramName = null) { Ensure.NotNull(value, "Value"); return(Ensure.Condition( argument.Value, a => a.Contains(value), message ?? $"Expected the array to contain <{value}>", paramName)); }
/// <summary> /// Ensures the array has <paramref name="count"/> amount of items /// </summary> /// <typeparam name="T">The <see cref="Type"/> of items in the array</typeparam> /// <param name="argument">The context describing the array</param> /// <param name="count">The amount of items the array should have</param> /// <param name="message">An optional message to use when validation fails</param> /// <param name="paramName">An optional parameter name to use when validation fails</param> /// <returns>The given array if validation passes</returns> /// <exception cref="ArgumentException">If the <see cref="Array"/> length does not equal <paramref name="count"/></exception> /// <exception cref="ArgumentNullException">If <paramref name="argument.Value"/> or <paramref name="count"/> is <c>null</c></exception> public static T[] CountIs <T>(this ArgumentContext <T[]> argument, int count, string message = null, string paramName = null) { Ensure.NotNull(count, "Count"); return(Ensure.Condition( argument.Value, a => Equals(a.Length, count), message ?? $"Expected the array to have <{count}> items, it has <{argument.Value.Length}> items.", paramName)); }