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
141 changes: 141 additions & 0 deletions TUnit.Assertions/Conditions/Wrappers/CountWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,145 @@ public GreaterThanAssertion<int> Positive()
});
return new GreaterThanAssertion<int>(countContext, 0);
}

/// <summary>
/// Asserts that the collection count is greater than the expected count.
/// </summary>
public GreaterThanAssertion<int> GreaterThan(
int expected,
[CallerArgumentExpression(nameof(expected))] string? expression = null)
{
_context.ExpressionBuilder.Append($".GreaterThan({expression})");
// Map context to get the count
var countContext = _context.Map<int>(value =>
{
if (value == null)
{
return 0;
}

if (value is ICollection collection)
{
return collection.Count;
}

return value.Cast<object>().Count();
});
return new GreaterThanAssertion<int>(countContext, expected);
}

/// <summary>
/// Asserts that the collection count is less than the expected count.
/// </summary>
public LessThanAssertion<int> LessThan(
int expected,
[CallerArgumentExpression(nameof(expected))] string? expression = null)
{
_context.ExpressionBuilder.Append($".LessThan({expression})");
// Map context to get the count
var countContext = _context.Map<int>(value =>
{
if (value == null)
{
return 0;
}

if (value is ICollection collection)
{
return collection.Count;
}

return value.Cast<object>().Count();
});
return new LessThanAssertion<int>(countContext, expected);
}

/// <summary>
/// Asserts that the collection count is less than or equal to the expected count.
/// </summary>
public LessThanOrEqualAssertion<int> LessThanOrEqualTo(
int expected,
[CallerArgumentExpression(nameof(expected))] string? expression = null)
{
_context.ExpressionBuilder.Append($".LessThanOrEqualTo({expression})");
// Map context to get the count
var countContext = _context.Map<int>(value =>
{
if (value == null)
{
return 0;
}

if (value is ICollection collection)
{
return collection.Count;
}

return value.Cast<object>().Count();
});
return new LessThanOrEqualAssertion<int>(countContext, expected);
}

/// <summary>
/// Asserts that the collection count is between the minimum and maximum values.
/// </summary>
public BetweenAssertion<int> Between(
int minimum,
int maximum,
[CallerArgumentExpression(nameof(minimum))] string? minExpression = null,
[CallerArgumentExpression(nameof(maximum))] string? maxExpression = null)
{
_context.ExpressionBuilder.Append($".Between({minExpression}, {maxExpression})");
// Map context to get the count
var countContext = _context.Map<int>(value =>
{
if (value == null)
{
return 0;
}

if (value is ICollection collection)
{
return collection.Count;
}

return value.Cast<object>().Count();
});
return new BetweenAssertion<int>(countContext, minimum, maximum);
}

/// <summary>
/// Asserts that the collection count is zero (empty collection).
/// </summary>
public CollectionCountAssertion<TValue> Zero()
{
_context.ExpressionBuilder.Append(".Zero()");
return new CollectionCountAssertion<TValue>(_context, 0);
}

/// <summary>
/// Asserts that the collection count is not equal to the expected count.
/// </summary>
public NotEqualsAssertion<int> NotEqualTo(
int expected,
[CallerArgumentExpression(nameof(expected))] string? expression = null)
{
_context.ExpressionBuilder.Append($".NotEqualTo({expression})");
// Map context to get the count
var countContext = _context.Map<int>(value =>
{
if (value == null)
{
return 0;
}

if (value is ICollection collection)
{
return collection.Count;
}

return value.Cast<object>().Count();
});
return new NotEqualsAssertion<int>(countContext, expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1402,9 +1402,15 @@ namespace .
where TValue : .IEnumerable
{
public CountWrapper(.<TValue> context) { }
public .<int> Between(int minimum, int maximum, [.("minimum")] string? minExpression = null, [.("maximum")] string? maxExpression = null) { }
public .<TValue> EqualTo(int expectedCount, [.("expectedCount")] string? expression = null) { }
public .<int> GreaterThan(int expected, [.("expected")] string? expression = null) { }
public .<int> GreaterThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
public .<int> LessThan(int expected, [.("expected")] string? expression = null) { }
public .<int> LessThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
public .<int> NotEqualTo(int expected, [.("expected")] string? expression = null) { }
public .<int> Positive() { }
public .<TValue> Zero() { }
}
public class LengthWrapper : .<string>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1402,9 +1402,15 @@ namespace .
where TValue : .IEnumerable
{
public CountWrapper(.<TValue> context) { }
public .<int> Between(int minimum, int maximum, [.("minimum")] string? minExpression = null, [.("maximum")] string? maxExpression = null) { }
public .<TValue> EqualTo(int expectedCount, [.("expectedCount")] string? expression = null) { }
public .<int> GreaterThan(int expected, [.("expected")] string? expression = null) { }
public .<int> GreaterThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
public .<int> LessThan(int expected, [.("expected")] string? expression = null) { }
public .<int> LessThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
public .<int> NotEqualTo(int expected, [.("expected")] string? expression = null) { }
public .<int> Positive() { }
public .<TValue> Zero() { }
}
public class LengthWrapper : .<string>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1402,9 +1402,15 @@ namespace .
where TValue : .IEnumerable
{
public CountWrapper(.<TValue> context) { }
public .<int> Between(int minimum, int maximum, [.("minimum")] string? minExpression = null, [.("maximum")] string? maxExpression = null) { }
public .<TValue> EqualTo(int expectedCount, [.("expectedCount")] string? expression = null) { }
public .<int> GreaterThan(int expected, [.("expected")] string? expression = null) { }
public .<int> GreaterThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
public .<int> LessThan(int expected, [.("expected")] string? expression = null) { }
public .<int> LessThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
public .<int> NotEqualTo(int expected, [.("expected")] string? expression = null) { }
public .<int> Positive() { }
public .<TValue> Zero() { }
}
public class LengthWrapper : .<string>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1321,9 +1321,15 @@ namespace .
where TValue : .IEnumerable
{
public CountWrapper(.<TValue> context) { }
public .<int> Between(int minimum, int maximum, [.("minimum")] string? minExpression = null, [.("maximum")] string? maxExpression = null) { }
public .<TValue> EqualTo(int expectedCount, [.("expectedCount")] string? expression = null) { }
public .<int> GreaterThan(int expected, [.("expected")] string? expression = null) { }
public .<int> GreaterThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
public .<int> LessThan(int expected, [.("expected")] string? expression = null) { }
public .<int> LessThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
public .<int> NotEqualTo(int expected, [.("expected")] string? expression = null) { }
public .<int> Positive() { }
public .<TValue> Zero() { }
}
public class LengthWrapper : .<string>
{
Expand Down
Loading