зеркало из https://github.com/microsoft/Omex.git
Add new validation methods for AnyAndAllNotNull, add accompanying unit tests
This commit is contained in:
Родитель
91edc20011
Коммит
7a79d31f01
|
@ -55,6 +55,29 @@ namespace Microsoft.Omex.System.Validation
|
|||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Checks the enumeable argument and throws an exception if it is null, contains no values, or contains any null values
|
||||
/// </summary>
|
||||
/// <remarks>Be careful to not pass enumerables that can be enumerated only once</remarks>
|
||||
/// <typeparam name="T">Type of the enumerable</typeparam>
|
||||
/// <param name="argumentValue">The argument value.</param>
|
||||
/// <param name="argumentName">Name of the argument.</param>
|
||||
/// <param name="tagId">Tag Id to log, leave null if no logging is needed.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if any argument <paramref name="argumentValue"/> element is null.</exception>
|
||||
/// <exception cref="ArgumentNullException">Thrown if the supplied argument <paramref name="argumentValue"/> is null.</exception>
|
||||
public static IEnumerable<T> ExpectsAnyAndAllNotNull<T>([ValidatedNotNull] IEnumerable<T> argumentValue, string argumentName, uint? tagId) where T : class
|
||||
{
|
||||
argumentValue = ExpectsArgument(argumentValue, argumentName, tagId);
|
||||
|
||||
if (!ValidateAnyAndAllNotNull(argumentValue, argumentName, tagId))
|
||||
{
|
||||
ReportArgumentError(argumentName, HasAnyErrorMessage);
|
||||
}
|
||||
|
||||
return argumentValue;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Checks the argument value and throws an exception if it is null or contains no values.
|
||||
/// </summary>
|
||||
|
@ -198,6 +221,38 @@ namespace Microsoft.Omex.System.Validation
|
|||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Checks that the enumerable argument is not null, is not empty, and does not contain nulls
|
||||
/// </summary>
|
||||
/// <remarks>Be careful to not pass enumerables that can be enumerated only once</remarks>
|
||||
/// <typeparam name="T">The type of the enumerable</typeparam>
|
||||
/// <param name="argumentValue">The argument value.</param>
|
||||
/// <param name="argumentName">Name of the argument.</param>
|
||||
/// <param name="tagId">Tag Id to log, leave null if no logging is needed</param>
|
||||
/// <returns>True if the argument <paramref name="argumentValue"/> is not null, is not empty, and does not contain nulls; false otherwise.</returns>
|
||||
public static bool ValidateAnyAndAllNotNull<T>(IEnumerable<T> argumentValue, string argumentName, uint? tagId)
|
||||
where T : class
|
||||
{
|
||||
if (!ValidateArgument(argumentValue, argumentName, tagId))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!argumentValue.Any() || argumentValue.Any(x => x == null))
|
||||
{
|
||||
if (tagId != null)
|
||||
{
|
||||
UntaggedLogging.LogTraceTag(tagId.Value, Categories.ArgumentValidation, Levels.Error,
|
||||
ValidationFailed, AllErrorMessage, argumentName);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Checks that the enumerable argument is not null and doesn't contain any nulls
|
||||
/// </summary>
|
||||
|
|
|
@ -44,6 +44,49 @@ namespace Microsoft.Omex.System.UnitTests.Validation
|
|||
}
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData("", "")]
|
||||
public void ExpectsAnyAndAllNotNull_CorrectArgument_DoesNotLogOrThrowException(params string[] argumentValues)
|
||||
{
|
||||
FailOnErrors = true;
|
||||
|
||||
uint? tagId = 1234;
|
||||
|
||||
Code.ExpectsAllNotNull(argumentValues, ArgumentName, tagId);
|
||||
|
||||
CheckLogCount(tagId, false);
|
||||
}
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData(null, true)]
|
||||
[InlineData(null, false)]
|
||||
[InlineData(new string[0], true)]
|
||||
[InlineData(new string[0], false)]
|
||||
[InlineData(new string[] { null }, true)]
|
||||
[InlineData(new string[] { null }, false)]
|
||||
[InlineData(new string[] { "", null }, true)]
|
||||
[InlineData(new string[] { "", null }, false)]
|
||||
public void ExpectsAnyAndAllNotNull_IncorrectArgument_LogsAndThrowsException(string[] argumentValues, bool log)
|
||||
{
|
||||
FailOnErrors = !log;
|
||||
|
||||
uint? tagId = 1234;
|
||||
|
||||
if (argumentValues is null)
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => Code.ExpectsAnyAndAllNotNull(argumentValues, ArgumentName, log ? tagId : null));
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => Code.ExpectsAnyAndAllNotNull(argumentValues, ArgumentName, log ? tagId : null));
|
||||
}
|
||||
|
||||
CheckLogCount(tagId, log);
|
||||
}
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(1)]
|
||||
|
@ -311,6 +354,55 @@ namespace Microsoft.Omex.System.UnitTests.Validation
|
|||
}
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData(new string[0], true)]
|
||||
[InlineData(new string[0], false)]
|
||||
[InlineData(new string[] { null }, true)]
|
||||
[InlineData(new string[] { null }, false)]
|
||||
[InlineData(new string[] { "", null }, true)]
|
||||
[InlineData(new string[] { "", null }, false)]
|
||||
[InlineData(null, true)]
|
||||
[InlineData(null, false)]
|
||||
public void ValidateAnyAndAllNotNull_IncorrectArgument_LogsAndReturnsFalse(string[] array, bool log)
|
||||
{
|
||||
FailOnErrors = !log;
|
||||
|
||||
uint? tagId = 1234;
|
||||
|
||||
Assert.False(Code.ValidateAnyAndAllNotNull(array, ArgumentName, log ? tagId : null));
|
||||
|
||||
CheckLogCount(tagId, log);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void ValidateAnyAndAllNotNull_CorrectArgument_DoesNotLogAndReturnsTrue()
|
||||
{
|
||||
FailOnErrors = true;
|
||||
|
||||
uint? tagId = 1234;
|
||||
|
||||
Assert.True(Code.ValidateAnyAndAllNotNull(new[] { "" }, ArgumentName, tagId));
|
||||
|
||||
CheckLogCount(tagId, false);
|
||||
}
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData()]
|
||||
public void ValidateAllNotNull_CorrectArgument_DoesNotLogAndReturnsTrue(params string[] array)
|
||||
{
|
||||
FailOnErrors = true;
|
||||
|
||||
uint? tagId = 1234;
|
||||
|
||||
Assert.True(Code.ValidateAllNotNull(array, ArgumentName, tagId));
|
||||
|
||||
CheckLogCount(tagId, false);
|
||||
}
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, true)]
|
||||
[InlineData(0, false)]
|
||||
|
@ -337,19 +429,27 @@ namespace Microsoft.Omex.System.UnitTests.Validation
|
|||
}
|
||||
}
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
Assert.False(Code.ValidateAllNotNull(argumentValues, ArgumentName, log ? tagId : null));
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.False(Code.ValidateAllNotNull(argumentValues, ArgumentName, log ? tagId : null));
|
||||
}
|
||||
Assert.False(Code.ValidateAllNotNull(argumentValues, ArgumentName, log ? tagId : null));
|
||||
|
||||
CheckLogCount(tagId, log);
|
||||
}
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData("", null)]
|
||||
public void ValidateAny_CorrectArgument_DoesNotLogAndReturnsTrue(params string[] array)
|
||||
{
|
||||
FailOnErrors = true;
|
||||
|
||||
uint? tagId = 1234;
|
||||
|
||||
Assert.True(Code.ValidateAny(array, ArgumentName, tagId));
|
||||
|
||||
CheckLogCount(tagId, false);
|
||||
}
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData(true, true)]
|
||||
[InlineData(true, false)]
|
||||
|
|
Загрузка…
Ссылка в новой задаче