Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Testably.Expectations.Core;
using Testably.Expectations.Core.Constraints;
using Testably.Expectations.Core.EvaluationContext;
using Testably.Expectations.Results;
// ReSharper disable once CheckNamespace

namespace Testably.Expectations;

/// <summary>
/// A container for specific instances of a quantified result.
/// </summary>
public class QuantifiedCollectionResult
{
/// <summary>
/// A quantifiable result matching items against the expected
/// <see cref="QuantifiedCollectionResult{TResult}.Quantity" />.
/// </summary>
public class Sync<TResult, TItem, TCollection>(
TResult result,
ExpectationBuilder expectationBuilder,
CollectionQuantifier quantity)
: QuantifiedCollectionResult<TResult>(result, expectationBuilder, quantity)
where TCollection : IEnumerable<TItem>
where TResult : IThat<TCollection>
{
/// <summary>
/// ...are of type <typeparamref name="TExpected" />.
/// </summary>
public AndOrExpectationResult<TCollection, IThat<TCollection>> Be<TExpected>()
=> new(ExpectationBuilder
.AddConstraint(
new BeOfTypeConstraint<TItem, TCollection, TExpected>(
Quantity,
(a, c) => Quantity.GetEvaluator<TItem, TCollection>(a, c)))
.AppendGenericMethodStatement<TExpected>(nameof(Be)),
Result);
}

#if NET6_0_OR_GREATER
/// <summary>
/// A quantifiable result matching items against the expected
/// <see cref="QuantifiedCollectionResult{TResult}.Quantity" />.
/// </summary>
public class Async<TResult, TItem, TCollection>(
TResult result,
ExpectationBuilder expectationBuilder,
CollectionQuantifier quantity)
: QuantifiedCollectionResult<TResult>(result, expectationBuilder, quantity)
where TCollection : IAsyncEnumerable<TItem>
where TResult : IThat<TCollection>
{
/// <summary>
/// ...are of type <typeparamref name="TExpected" />.
/// </summary>
public AndOrExpectationResult<TCollection, IThat<TCollection>> Be<TExpected>()
=> new(ExpectationBuilder
.AddConstraint(
new BeOfTypeConstraint<TItem, TCollection, TExpected>(
Quantity,
(a, c) => Quantity.GetAsyncEvaluator<TItem, TCollection>(a, c)))
.AppendGenericMethodStatement<TExpected>(nameof(Be)),
Result);
}
#endif

private readonly struct BeOfTypeConstraint<TItem, TCollection, TExpected>(
CollectionQuantifier quantifier,
Func<TCollection, IEvaluationContext, ICollectionEvaluator<TItem>> evaluatorFactory)
: IAsyncContextConstraint<TCollection>
{
public async Task<ConstraintResult> IsMetBy(TCollection actual,
IEvaluationContext context)
{
ICollectionEvaluator<TItem> evaluator = evaluatorFactory(actual, context);
CollectionEvaluatorResult result = await evaluator
.CheckCondition(default(TItem), (a, _) => a is TExpected)
.ConfigureAwait(false);

if (result.IsSuccess)
{
return new ConstraintResult.Success<TCollection>(actual, ToString());
}

return new ConstraintResult.Failure(ToString(),
$"{result.Error} items were");
}

public override string ToString()
=> $"have {quantifier} of type {typeof(TExpected).Name}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public static partial class ThatAsyncEnumerableShould
/// <summary>
/// Verifies that all items in the enumerable...
/// </summary>
public static QuantifiedCollectionResult<IThat<IAsyncEnumerable<TItem>>> All<TItem>(
public static QuantifiedCollectionResult.Async
<IThat<IAsyncEnumerable<TItem>>, TItem, IAsyncEnumerable<TItem>> All<TItem>(
this IThat<IAsyncEnumerable<TItem>> source)
=> new(source,
source.ExpectationBuilder.AppendMethodStatement(nameof(All)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public static partial class ThatAsyncEnumerableShould
/// <summary>
/// Verifies that at least <paramref name="minimum" /> items...
/// </summary>
public static QuantifiedCollectionResult<IThat<IAsyncEnumerable<TItem>>> AtLeast<TItem>(
public static QuantifiedCollectionResult.Async
<IThat<IAsyncEnumerable<TItem>>, TItem, IAsyncEnumerable<TItem>> AtLeast<TItem>(
this IThat<IAsyncEnumerable<TItem>> source,
int minimum, [CallerArgumentExpression("minimum")] string doNotPopulateThisValue = "")
=> new(source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public static partial class ThatAsyncEnumerableShould
/// <summary>
/// Verifies that at most <paramref name="maximum" /> items...
/// </summary>
public static QuantifiedCollectionResult<IThat<IAsyncEnumerable<TItem>>> AtMost<TItem>(
public static QuantifiedCollectionResult.Async
<IThat<IAsyncEnumerable<TItem>>, TItem, IAsyncEnumerable<TItem>> AtMost<TItem>(
this IThat<IAsyncEnumerable<TItem>> source,
int maximum, [CallerArgumentExpression("maximum")] string doNotPopulateThisValue = "")
=> new(source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ public static partial class ThatAsyncEnumerableShould
/// <summary>
/// Verifies that between <paramref name="minimum" />...
/// </summary>
public static BetweenResult<QuantifiedCollectionResult<IThat<IAsyncEnumerable<TItem>>>>
public static BetweenResult<QuantifiedCollectionResult.Async
<IThat<IAsyncEnumerable<TItem>>, TItem, IAsyncEnumerable<TItem>>>
Between<TItem>(
this IThat<IAsyncEnumerable<TItem>> source,
int minimum,
[CallerArgumentExpression("minimum")] string doNotPopulateThisValue = "")
{
source.ExpectationBuilder.AppendMethodStatement(nameof(Between), doNotPopulateThisValue);
return new BetweenResult<QuantifiedCollectionResult<IThat<IAsyncEnumerable<TItem>>>>(
return new BetweenResult<QuantifiedCollectionResult.Async
<IThat<IAsyncEnumerable<TItem>>, TItem, IAsyncEnumerable<TItem>>>(
source.ExpectationBuilder,
maximum => new QuantifiedCollectionResult<IThat<IAsyncEnumerable<TItem>>>(source,
source.ExpectationBuilder,
CollectionQuantifier.Between(minimum, maximum)));
maximum => new QuantifiedCollectionResult.Async
<IThat<IAsyncEnumerable<TItem>>, TItem, IAsyncEnumerable<TItem>>(source,
source.ExpectationBuilder,
CollectionQuantifier.Between(minimum, maximum)));
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public static partial class ThatAsyncEnumerableShould
/// <summary>
/// Verifies that no items in the enumerable...
/// </summary>
public static QuantifiedCollectionResult<IThat<IAsyncEnumerable<TItem>>> None<TItem>(
public static QuantifiedCollectionResult.Async
<IThat<IAsyncEnumerable<TItem>>, TItem, IAsyncEnumerable<TItem>> None<TItem>(
this IThat<IAsyncEnumerable<TItem>> source)
=> new(source,
source.ExpectationBuilder.AppendMethodStatement(nameof(None)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ public static partial class ThatEnumerableShould
/// <summary>
/// Verifies that all items in the enumerable...
/// </summary>
public static QuantifiedCollectionResult<IThat<IEnumerable<TItem>>> All<TItem>(
this IThat<IEnumerable<TItem>> source)
public static QuantifiedCollectionResult.Sync
<IThat<IEnumerable<TItem>>, TItem, IEnumerable<TItem>> All<TItem>(
this IThat<IEnumerable<TItem>> source)
{
source.ExpectationBuilder.AppendMethodStatement(nameof(All));
return new QuantifiedCollectionResult<IThat<IEnumerable<TItem>>>(
return new QuantifiedCollectionResult.Sync<
IThat<IEnumerable<TItem>>, TItem, IEnumerable<TItem>>(
source,
source.ExpectationBuilder,
CollectionQuantifier.All);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ public static partial class ThatEnumerableShould
/// <summary>
/// Verifies that at least <paramref name="minimum" /> items...
/// </summary>
public static QuantifiedCollectionResult<IThat<IEnumerable<TItem>>> AtLeast<TItem>(
this IThat<IEnumerable<TItem>> source,
int minimum, [CallerArgumentExpression("minimum")] string doNotPopulateThisValue = "")
public static QuantifiedCollectionResult.Sync
<IThat<IEnumerable<TItem>>, TItem, IEnumerable<TItem>> AtLeast<TItem>(
this IThat<IEnumerable<TItem>> source,
int minimum, [CallerArgumentExpression("minimum")] string doNotPopulateThisValue = "")
{
source.ExpectationBuilder.AppendMethodStatement(nameof(AtLeast), doNotPopulateThisValue);
return new QuantifiedCollectionResult<IThat<IEnumerable<TItem>>>(
return new QuantifiedCollectionResult.Sync<IThat<IEnumerable<TItem>>, TItem,
IEnumerable<TItem>>(
source,
source.ExpectationBuilder,
CollectionQuantifier.AtLeast(minimum));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ public static partial class ThatEnumerableShould
/// <summary>
/// Verifies that at most <paramref name="maximum" /> items...
/// </summary>
public static QuantifiedCollectionResult<IThat<IEnumerable<TItem>>> AtMost<TItem>(
public static QuantifiedCollectionResult.Sync
<IThat<IEnumerable<TItem>>, TItem, IEnumerable<TItem>> AtMost<TItem>(
this IThat<IEnumerable<TItem>> source,
int maximum, [CallerArgumentExpression("maximum")] string doNotPopulateThisValue = "")
{
source.ExpectationBuilder.AppendMethodStatement(nameof(AtMost), doNotPopulateThisValue);
return new QuantifiedCollectionResult<IThat<IEnumerable<TItem>>>(
return new QuantifiedCollectionResult.Sync<IThat<IEnumerable<TItem>>, TItem, IEnumerable<TItem>>(
source,
source.ExpectationBuilder,
CollectionQuantifier.AtMost(maximum));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ public static partial class ThatEnumerableShould
/// <summary>
/// Verifies that between <paramref name="minimum" />...
/// </summary>
public static BetweenResult<QuantifiedCollectionResult<IThat<IEnumerable<TItem>>>>
public static BetweenResult<QuantifiedCollectionResult.Sync
<IThat<IEnumerable<TItem>>, TItem, IEnumerable<TItem>>>
Between<TItem>(
this IThat<IEnumerable<TItem>> source,
int minimum,
[CallerArgumentExpression("minimum")] string doNotPopulateThisValue = "")
{
source.ExpectationBuilder.AppendMethodStatement(nameof(Between), doNotPopulateThisValue);
return new BetweenResult<QuantifiedCollectionResult<IThat<IEnumerable<TItem>>>>(
return new BetweenResult<QuantifiedCollectionResult.Sync<IThat<IEnumerable<TItem>>, TItem, IEnumerable<TItem>>>(
source.ExpectationBuilder,
maximum => new QuantifiedCollectionResult<IThat<IEnumerable<TItem>>>(
maximum => new QuantifiedCollectionResult.Sync<IThat<IEnumerable<TItem>>, TItem, IEnumerable<TItem>>(
source,
source.ExpectationBuilder,
CollectionQuantifier.Between(minimum, maximum)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public static partial class ThatEnumerableShould
/// <summary>
/// Verifies that no items in the enumerable...
/// </summary>
public static QuantifiedCollectionResult<IThat<IEnumerable<TItem>>> None<TItem>(
public static QuantifiedCollectionResult.Sync<IThat<IEnumerable<TItem>>, TItem, IEnumerable<TItem>> None<TItem>(
this IThat<IEnumerable<TItem>> source)
{
source.ExpectationBuilder.AppendMethodStatement(nameof(None));
return new QuantifiedCollectionResult<IThat<IEnumerable<TItem>>>(
return new QuantifiedCollectionResult.Sync<IThat<IEnumerable<TItem>>, TItem, IEnumerable<TItem>>(
source,
source.ExpectationBuilder,
CollectionQuantifier.None);
Expand Down
Loading
Loading