Skip to content

Commit aff75b3

Browse files
authored
feat: add collection count assertions for GreaterThan, LessThan, LessThanOrEqualTo, Between, Zero, and NotEqualTo (#3398)
1 parent 86b3b9a commit aff75b3

5 files changed

+165
-0
lines changed

TUnit.Assertions/Conditions/Wrappers/CountWrapper.cs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,145 @@ public GreaterThanAssertion<int> Positive()
8282
});
8383
return new GreaterThanAssertion<int>(countContext, 0);
8484
}
85+
86+
/// <summary>
87+
/// Asserts that the collection count is greater than the expected count.
88+
/// </summary>
89+
public GreaterThanAssertion<int> GreaterThan(
90+
int expected,
91+
[CallerArgumentExpression(nameof(expected))] string? expression = null)
92+
{
93+
_context.ExpressionBuilder.Append($".GreaterThan({expression})");
94+
// Map context to get the count
95+
var countContext = _context.Map<int>(value =>
96+
{
97+
if (value == null)
98+
{
99+
return 0;
100+
}
101+
102+
if (value is ICollection collection)
103+
{
104+
return collection.Count;
105+
}
106+
107+
return value.Cast<object>().Count();
108+
});
109+
return new GreaterThanAssertion<int>(countContext, expected);
110+
}
111+
112+
/// <summary>
113+
/// Asserts that the collection count is less than the expected count.
114+
/// </summary>
115+
public LessThanAssertion<int> LessThan(
116+
int expected,
117+
[CallerArgumentExpression(nameof(expected))] string? expression = null)
118+
{
119+
_context.ExpressionBuilder.Append($".LessThan({expression})");
120+
// Map context to get the count
121+
var countContext = _context.Map<int>(value =>
122+
{
123+
if (value == null)
124+
{
125+
return 0;
126+
}
127+
128+
if (value is ICollection collection)
129+
{
130+
return collection.Count;
131+
}
132+
133+
return value.Cast<object>().Count();
134+
});
135+
return new LessThanAssertion<int>(countContext, expected);
136+
}
137+
138+
/// <summary>
139+
/// Asserts that the collection count is less than or equal to the expected count.
140+
/// </summary>
141+
public LessThanOrEqualAssertion<int> LessThanOrEqualTo(
142+
int expected,
143+
[CallerArgumentExpression(nameof(expected))] string? expression = null)
144+
{
145+
_context.ExpressionBuilder.Append($".LessThanOrEqualTo({expression})");
146+
// Map context to get the count
147+
var countContext = _context.Map<int>(value =>
148+
{
149+
if (value == null)
150+
{
151+
return 0;
152+
}
153+
154+
if (value is ICollection collection)
155+
{
156+
return collection.Count;
157+
}
158+
159+
return value.Cast<object>().Count();
160+
});
161+
return new LessThanOrEqualAssertion<int>(countContext, expected);
162+
}
163+
164+
/// <summary>
165+
/// Asserts that the collection count is between the minimum and maximum values.
166+
/// </summary>
167+
public BetweenAssertion<int> Between(
168+
int minimum,
169+
int maximum,
170+
[CallerArgumentExpression(nameof(minimum))] string? minExpression = null,
171+
[CallerArgumentExpression(nameof(maximum))] string? maxExpression = null)
172+
{
173+
_context.ExpressionBuilder.Append($".Between({minExpression}, {maxExpression})");
174+
// Map context to get the count
175+
var countContext = _context.Map<int>(value =>
176+
{
177+
if (value == null)
178+
{
179+
return 0;
180+
}
181+
182+
if (value is ICollection collection)
183+
{
184+
return collection.Count;
185+
}
186+
187+
return value.Cast<object>().Count();
188+
});
189+
return new BetweenAssertion<int>(countContext, minimum, maximum);
190+
}
191+
192+
/// <summary>
193+
/// Asserts that the collection count is zero (empty collection).
194+
/// </summary>
195+
public CollectionCountAssertion<TValue> Zero()
196+
{
197+
_context.ExpressionBuilder.Append(".Zero()");
198+
return new CollectionCountAssertion<TValue>(_context, 0);
199+
}
200+
201+
/// <summary>
202+
/// Asserts that the collection count is not equal to the expected count.
203+
/// </summary>
204+
public NotEqualsAssertion<int> NotEqualTo(
205+
int expected,
206+
[CallerArgumentExpression(nameof(expected))] string? expression = null)
207+
{
208+
_context.ExpressionBuilder.Append($".NotEqualTo({expression})");
209+
// Map context to get the count
210+
var countContext = _context.Map<int>(value =>
211+
{
212+
if (value == null)
213+
{
214+
return 0;
215+
}
216+
217+
if (value is ICollection collection)
218+
{
219+
return collection.Count;
220+
}
221+
222+
return value.Cast<object>().Count();
223+
});
224+
return new NotEqualsAssertion<int>(countContext, expected);
225+
}
85226
}

TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet10_0.verified.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,9 +1402,15 @@ namespace .
14021402
where TValue : .IEnumerable
14031403
{
14041404
public CountWrapper(.<TValue> context) { }
1405+
public .<int> Between(int minimum, int maximum, [.("minimum")] string? minExpression = null, [.("maximum")] string? maxExpression = null) { }
14051406
public .<TValue> EqualTo(int expectedCount, [.("expectedCount")] string? expression = null) { }
1407+
public .<int> GreaterThan(int expected, [.("expected")] string? expression = null) { }
14061408
public .<int> GreaterThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
1409+
public .<int> LessThan(int expected, [.("expected")] string? expression = null) { }
1410+
public .<int> LessThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
1411+
public .<int> NotEqualTo(int expected, [.("expected")] string? expression = null) { }
14071412
public .<int> Positive() { }
1413+
public .<TValue> Zero() { }
14081414
}
14091415
public class LengthWrapper : .<string>
14101416
{

TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet8_0.verified.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,9 +1402,15 @@ namespace .
14021402
where TValue : .IEnumerable
14031403
{
14041404
public CountWrapper(.<TValue> context) { }
1405+
public .<int> Between(int minimum, int maximum, [.("minimum")] string? minExpression = null, [.("maximum")] string? maxExpression = null) { }
14051406
public .<TValue> EqualTo(int expectedCount, [.("expectedCount")] string? expression = null) { }
1407+
public .<int> GreaterThan(int expected, [.("expected")] string? expression = null) { }
14061408
public .<int> GreaterThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
1409+
public .<int> LessThan(int expected, [.("expected")] string? expression = null) { }
1410+
public .<int> LessThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
1411+
public .<int> NotEqualTo(int expected, [.("expected")] string? expression = null) { }
14071412
public .<int> Positive() { }
1413+
public .<TValue> Zero() { }
14081414
}
14091415
public class LengthWrapper : .<string>
14101416
{

TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet9_0.verified.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,9 +1402,15 @@ namespace .
14021402
where TValue : .IEnumerable
14031403
{
14041404
public CountWrapper(.<TValue> context) { }
1405+
public .<int> Between(int minimum, int maximum, [.("minimum")] string? minExpression = null, [.("maximum")] string? maxExpression = null) { }
14051406
public .<TValue> EqualTo(int expectedCount, [.("expectedCount")] string? expression = null) { }
1407+
public .<int> GreaterThan(int expected, [.("expected")] string? expression = null) { }
14061408
public .<int> GreaterThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
1409+
public .<int> LessThan(int expected, [.("expected")] string? expression = null) { }
1410+
public .<int> LessThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
1411+
public .<int> NotEqualTo(int expected, [.("expected")] string? expression = null) { }
14071412
public .<int> Positive() { }
1413+
public .<TValue> Zero() { }
14081414
}
14091415
public class LengthWrapper : .<string>
14101416
{

TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.Net4_7.verified.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,9 +1321,15 @@ namespace .
13211321
where TValue : .IEnumerable
13221322
{
13231323
public CountWrapper(.<TValue> context) { }
1324+
public .<int> Between(int minimum, int maximum, [.("minimum")] string? minExpression = null, [.("maximum")] string? maxExpression = null) { }
13241325
public .<TValue> EqualTo(int expectedCount, [.("expectedCount")] string? expression = null) { }
1326+
public .<int> GreaterThan(int expected, [.("expected")] string? expression = null) { }
13251327
public .<int> GreaterThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
1328+
public .<int> LessThan(int expected, [.("expected")] string? expression = null) { }
1329+
public .<int> LessThanOrEqualTo(int expected, [.("expected")] string? expression = null) { }
1330+
public .<int> NotEqualTo(int expected, [.("expected")] string? expression = null) { }
13261331
public .<int> Positive() { }
1332+
public .<TValue> Zero() { }
13271333
}
13281334
public class LengthWrapper : .<string>
13291335
{

0 commit comments

Comments
 (0)