Skip to content

Commit

Permalink
#534 Add `Satisfy(Expression<Predicate<IEnumerable<TData>>> predicate…
Browse files Browse the repository at this point in the history
…Expression)` verification extension method
  • Loading branch information
YevgeniyShunevych committed Aug 24, 2021
1 parent 2f1608d commit 195ee13
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
40 changes: 38 additions & 2 deletions src/Atata/Verification/IDataVerificationProviderExtensions.cs
Expand Up @@ -22,7 +22,9 @@ public static class IDataVerificationProviderExtensions
/// <param name="verifier">The verification provider.</param>
/// <param name="predicateExpression">The predicate expression.</param>
/// <returns>The owner instance.</returns>
public static TOwner Satisfy<TData, TOwner>(this IDataVerificationProvider<TData, TOwner> verifier, Expression<Predicate<TData>> predicateExpression)
public static TOwner Satisfy<TData, TOwner>(
this IDataVerificationProvider<TData, TOwner> verifier,
Expression<Predicate<TData>> predicateExpression)
{
predicateExpression.CheckNotNull(nameof(predicateExpression));

Expand All @@ -43,7 +45,11 @@ public static class IDataVerificationProviderExtensions
/// <param name="args">The message arguments.</param>
/// <returns>The owner instance.</returns>
/// TODO: Atata v2. Change type of predicate from "Predicate" to "Func".
public static TOwner Satisfy<TData, TOwner>(this IDataVerificationProvider<TData, TOwner> should, Predicate<TData> predicate, string message, params TData[] args)
public static TOwner Satisfy<TData, TOwner>(
this IDataVerificationProvider<TData, TOwner> should,
Predicate<TData> predicate,
string message,
params TData[] args)
{
should.CheckNotNull(nameof(should));
predicate.CheckNotNull(nameof(predicate));
Expand Down Expand Up @@ -85,6 +91,36 @@ void ExecuteVerification()
return VerificationUtils.Verify(should, ExecuteVerification, message, args);
}

/// <summary>
/// Verifies that collection satisfies the specified predicate expression.
/// </summary>
/// <typeparam name="TData">The type of the data.</typeparam>
/// <typeparam name="TOwner">The type of the owner.</typeparam>
/// <param name="verifier">The verification provider.</param>
/// <param name="predicateExpression">The predicate expression.</param>
/// <returns>The owner instance.</returns>
public static TOwner Satisfy<TData, TOwner>(
this IDataVerificationProvider<IEnumerable<IObjectProvider<TData>>, TOwner> verifier,
Expression<Predicate<IEnumerable<TData>>> predicateExpression)
{
predicateExpression.CheckNotNull(nameof(predicateExpression));

var predicate = predicateExpression.Compile();
string expressionAsString = ObjectExpressionStringBuilder.ExpressionToString(predicateExpression);

return verifier.Satisfy(predicate, $"satisfy {expressionAsString}");
}

/// <summary>
/// Verifies that collection satisfies the specified predicate.
/// </summary>
/// <typeparam name="TData">The type of the data.</typeparam>
/// <typeparam name="TOwner">The type of the owner.</typeparam>
/// <param name="should">The verification provider.</param>
/// <param name="predicate">The predicate.</param>
/// <param name="message">The message that should sound in a way of "{Something} should {message}".</param>
/// <param name="args">The message arguments.</param>
/// <returns>The owner instance.</returns>
public static TOwner Satisfy<TData, TOwner>(
this IDataVerificationProvider<IEnumerable<IObjectProvider<TData>>, TOwner> should,
Predicate<IEnumerable<TData>> predicate,
Expand Down
Expand Up @@ -10,22 +10,26 @@ public static class DataVerificationProviderExtensionMethodTests
{
public class Satisfy_Expression : ExtensionMethodTestFixture<string, Satisfy_Expression>
{
static Satisfy_Expression()
{
static Satisfy_Expression() =>
For("abc123")
.Pass(x => x.Satisfy(x => x.Contains("abc") && x.Contains("123")))
.Fail(x => x.Satisfy(x => x == "xyz"));
}
}

public class Satisfy_Function : ExtensionMethodTestFixture<int, Satisfy_Function>
{
static Satisfy_Function()
{
static Satisfy_Function() =>
For(5)
.Pass(x => x.Satisfy(x => x > 1 && x < 10, "..."))
.Fail(x => x.Satisfy(x => x == 7, "..."));
}
}

public class Satisfy_IEnumerable_Expression : ExtensionMethodTestFixture<Subject<string>[], Satisfy_IEnumerable_Expression>
{
static Satisfy_IEnumerable_Expression() =>
For(new[] { "a".ToSubject(), "b".ToSubject(), "c".ToSubject() })
.Pass(x => x.Satisfy(x => x.Contains("a") && x.Contains("c")))
.Fail(x => x.Satisfy(x => x.Any(y => y.Contains("z"))));
}

public abstract class ExtensionMethodTestFixture<TObject, TFixture>
Expand Down

0 comments on commit 195ee13

Please sign in to comment.