This commit is contained in:
Daniel Hanlon 2019-10-14 23:28:42 +01:00
Родитель d43639f6b4
Коммит 83fae985c8
2 изменённых файлов: 30 добавлений и 24 удалений

Просмотреть файл

@ -2,6 +2,7 @@
// Licensed under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Omex.System.Logging;
@ -56,20 +57,19 @@ 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
/// Checks the collection 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>
/// <typeparam name="T">Type of the collection</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="ArgumentException">Thrown if <paramref name="argumentValue"/> is empty or if any 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
public static IEnumerable<T> ExpectsNotEmptyAndAllNotNull<T>([ValidatedNotNull] ICollection<T> argumentValue, string argumentName, uint? tagId) where T : class
{
argumentValue = ExpectsArgument(argumentValue, argumentName, tagId);
if (!ValidateAnyAndAllNotNull(argumentValue, argumentName, tagId))
if (!ValidateNotEmptyAndAllNotNull(argumentValue, argumentName, tagId))
{
ReportArgumentError(argumentName, HasAnyErrorMessage);
}
@ -222,15 +222,14 @@ namespace Microsoft.Omex.System.Validation
/// <summary>
/// Checks that the enumerable argument is not null, is not empty, and does not contain nulls
/// Checks that the collection 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>
/// <typeparam name="T">The type of the collection</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)
public static bool ValidateNotEmptyAndAllNotNull<T>(ICollection<T> argumentValue, string argumentName, uint? tagId)
where T : class
{
if (!ValidateArgument(argumentValue, argumentName, tagId))
@ -238,7 +237,7 @@ namespace Microsoft.Omex.System.Validation
return false;
}
if (!argumentValue.Any() || argumentValue.Any(x => x == null))
if (!argumentValue.Any()|| argumentValue.Any(x => x == null))
{
if (tagId != null)
{

Просмотреть файл

@ -47,13 +47,13 @@ namespace Microsoft.Omex.System.UnitTests.Validation
[Theory]
[InlineData("")]
[InlineData("", "")]
public void ExpectsAnyAndAllNotNull_CorrectArgument_DoesNotLogOrThrowException(params string[] argumentValues)
public void ExpectsNotEmptyAndAllNotNull_CorrectArgument_DoesNotLogOrThrowException(params string[] argumentValues)
{
FailOnErrors = true;
uint? tagId = 1234;
Code.ExpectsAllNotNull(argumentValues, ArgumentName, tagId);
Code.ExpectsNotEmptyAndAllNotNull(argumentValues, ArgumentName, tagId);
CheckLogCount(tagId, false);
}
@ -68,7 +68,7 @@ namespace Microsoft.Omex.System.UnitTests.Validation
[InlineData(new string[] { null }, false)]
[InlineData(new string[] { "", null }, true)]
[InlineData(new string[] { "", null }, false)]
public void ExpectsAnyAndAllNotNull_IncorrectArgument_LogsAndThrowsException(string[] argumentValues, bool log)
public void ExpectsNotEmptyAndAllNotNull_IncorrectArgument_LogsAndThrowsException(string[] argumentValues, bool log)
{
FailOnErrors = !log;
@ -76,11 +76,11 @@ namespace Microsoft.Omex.System.UnitTests.Validation
if (argumentValues is null)
{
Assert.Throws<ArgumentNullException>(() => Code.ExpectsAnyAndAllNotNull(argumentValues, ArgumentName, log ? tagId : null));
Assert.Throws<ArgumentNullException>(() => Code.ExpectsNotEmptyAndAllNotNull(argumentValues, ArgumentName, log ? tagId : null));
}
else
{
Assert.Throws<ArgumentException>(() => Code.ExpectsAnyAndAllNotNull(argumentValues, ArgumentName, log ? tagId : null));
Assert.Throws<ArgumentException>(() => Code.ExpectsNotEmptyAndAllNotNull(argumentValues, ArgumentName, log ? tagId : null));
}
CheckLogCount(tagId, log);
@ -363,41 +363,48 @@ namespace Microsoft.Omex.System.UnitTests.Validation
[InlineData(new string[] { "", null }, false)]
[InlineData(null, true)]
[InlineData(null, false)]
public void ValidateAnyAndAllNotNull_IncorrectArgument_LogsAndReturnsFalse(string[] argumentValue, bool log)
public void ValidateNotEmptyAndAllNotNull_IncorrectArgument_LogsAndReturnsFalse(string[] argumentValue, bool log)
{
FailOnErrors = !log;
uint? tagId = 1234;
Assert.False(Code.ValidateAnyAndAllNotNull(argumentValue, ArgumentName, log ? tagId : null));
Assert.False(Code.ValidateNotEmptyAndAllNotNull(argumentValue, ArgumentName, log ? tagId : null));
CheckLogCount(tagId, log);
}
[Fact]
public void ValidateAnyAndAllNotNull_CorrectArgument_DoesNotLogAndReturnsTrue()
public void ValidateNotEmptyAndAllNotNull_CorrectArgument_DoesNotLogAndReturnsTrue()
{
FailOnErrors = true;
uint? tagId = 1234;
Assert.True(Code.ValidateAnyAndAllNotNull(new[] { "" }, ArgumentName, tagId));
Assert.True(Code.ValidateNotEmptyAndAllNotNull(new[] { "" }, ArgumentName, tagId));
CheckLogCount(tagId, false);
}
[Theory]
[InlineData("")]
[InlineData()]
public void ValidateAllNotNull_CorrectArgument_DoesNotLogAndReturnsTrue(params string[] argumentValue)
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
public void ValidateAllNotNull_CorrectArgument_DoesNotLogOrThrowException(int count)
{
FailOnErrors = true;
uint? tagId = 1234;
Assert.True(Code.ValidateAllNotNull(argumentValue, ArgumentName, tagId));
string[] argumentValues = new string[count];
for (int i = 0; i < count; i++)
{
argumentValues[i] = "";
}
Assert.True(Code.ValidateAllNotNull(argumentValues, ArgumentName, tagId));
CheckLogCount(tagId, false);
}