/// <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); }
/// <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); }
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(); }