Skip to content

Commit d28e6f5

Browse files
committed
tests and docs.
1 parent a3695a5 commit d28e6f5

4 files changed

Lines changed: 199 additions & 3 deletions

File tree

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
using System.Numerics;
2+
3+
namespace RoyalCode.SmartValidations.Tests;
4+
5+
public partial class BuildInPredicatesTests
6+
{
7+
[Theory]
8+
[MemberData(nameof(Comparable_Data))]
9+
public void Comparable_Min<T>(T value, T min, int compare)
10+
where T : IComparable<T>
11+
{
12+
// Arrange
13+
bool expected = compare >= 0;
14+
15+
// Act
16+
var result = BuildInPredicates.Min(value, min);
17+
18+
// Assert
19+
Assert.Equal(expected, result);
20+
}
21+
22+
[Theory]
23+
[MemberData(nameof(Nullable_Comparable_Data))]
24+
public void Nullable_Comparable_Min<T>(T? value, T min, int compare)
25+
where T : struct, IComparable<T>
26+
{
27+
// Arrange
28+
bool expected = compare >= 0;
29+
30+
// Act
31+
var result = BuildInPredicates.Min(value, min);
32+
33+
// Assert
34+
Assert.Equal(expected, result);
35+
}
36+
37+
[Theory]
38+
[InlineData(null, 0, false)]
39+
[InlineData(null, 1, false)]
40+
[InlineData(0, 0, true)]
41+
[InlineData(2, 1, true)]
42+
[InlineData(1, 2, false)]
43+
public void Comparable_Nulls_Min(int? value, int min, bool expected)
44+
{
45+
// Arrange
46+
// Act
47+
var result = BuildInPredicates.Min(value, min);
48+
49+
// Assert
50+
Assert.Equal(expected, result);
51+
}
52+
53+
[Theory]
54+
[MemberData(nameof(Comparable_Data))]
55+
public void Comparable_Max<T>(T value, T max, int compare)
56+
where T : IComparable<T>
57+
{
58+
// Arrange
59+
bool expected = compare <= 0;
60+
61+
// Act
62+
var result = BuildInPredicates.Max(value, max);
63+
64+
// Assert
65+
Assert.Equal(expected, result);
66+
}
67+
68+
[Theory]
69+
[MemberData(nameof(Nullable_Comparable_Data))]
70+
public void Nullable_Comparable_Max<T>(T? value, T max, int compare)
71+
where T : struct, IComparable<T>
72+
{
73+
// Arrange
74+
bool expected = compare <= 0;
75+
76+
// Act
77+
var result = BuildInPredicates.Max(value, max);
78+
79+
// Assert
80+
Assert.Equal(expected, result);
81+
}
82+
83+
[Theory]
84+
[InlineData(null, 0, false)]
85+
[InlineData(null, 1, false)]
86+
[InlineData(0, 0, true)]
87+
[InlineData(2, 1, false)]
88+
[InlineData(1, 2, true)]
89+
public void Comparable_Nulls_Max(int? value, int max, bool expected)
90+
{
91+
// Arrange
92+
// Act
93+
var result = BuildInPredicates.Max(value, max);
94+
95+
// Assert
96+
Assert.Equal(expected, result);
97+
}
98+
99+
100+
public static IEnumerable<object[]> Nullable_Comparable_Data()
101+
{
102+
foreach(var data in Comparable_Data())
103+
yield return data;
104+
105+
yield return [(byte?)0, (byte)1, -1];
106+
yield return [(byte?)1, (byte)1, 0];
107+
yield return [(byte?)2, (byte)1, 1];
108+
109+
yield return [(short?)0, (short)1, -1];
110+
yield return [(short?)1, (short)1, 0];
111+
yield return [(short?)2, (short)1, 1];
112+
113+
yield return [(int?)0, 1, -1];
114+
yield return [(int?)1, 1, 0];
115+
yield return [(int?)2, 1, 2];
116+
117+
yield return [(long?)0, 1L, -1];
118+
yield return [(long?)1, 1L, 0];
119+
yield return [(long?)2, 1L, 1];
120+
121+
yield return [(float?)0, 1f, -1];
122+
yield return [(float?)1, 1f, 0];
123+
yield return [(float?)2, 1f, 1];
124+
125+
yield return [(double?)0, 1d, -1];
126+
yield return [(double?)1, 1d, 0];
127+
yield return [(double?)2, 1d, 1];
128+
129+
yield return [(decimal?)0, 1M, -1];
130+
yield return [(decimal?)1, 1M, 0];
131+
yield return [(decimal?)2, 1M, 1];
132+
}
133+
134+
public static IEnumerable<object[]> Comparable_Data()
135+
{
136+
yield return [(byte)0, (byte)1, -1];
137+
yield return [(byte)1, (byte)1, 0];
138+
yield return [(byte)2, (byte)1, 1];
139+
140+
yield return [(short)0, (short)1, -1];
141+
yield return [(short)1, (short)1, 0];
142+
yield return [(short)2, (short)1, 1];
143+
144+
yield return [0, 1, -1];
145+
yield return [1, 1, 0];
146+
yield return [2, 1, 1];
147+
148+
yield return [0L, 1L, -1];
149+
yield return [1L, 1L, 0];
150+
yield return [2L, 1L, 1];
151+
152+
yield return [0f, 1f, -1];
153+
yield return [1f, 1f, 0];
154+
yield return [2f, 1f, 1];
155+
156+
yield return [0d, 1d, -1];
157+
yield return [1d, 1d, 0];
158+
yield return [2d, 1d, 1];
159+
160+
yield return [0M, 1M, -1];
161+
yield return [1M, 1M, 0];
162+
yield return [2M, 1M, 1];
163+
164+
yield return [new BigInteger(0), BigInteger.One, -1];
165+
yield return [BigInteger.One, BigInteger.One, 0];
166+
yield return [new BigInteger(2), BigInteger.One, 1];
167+
}
168+
}

RoyalCode.SmartValidations.Tests/BuildInPredicatesTests.NotEmpty.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void Number_NotEmpty<T>(T value, bool expected) where T : INumber<T>
1717
}
1818

1919
[Theory]
20-
[MemberData(nameof(Numbers_AndNulls_Data))]
20+
[MemberData(nameof(Nullable_Numbers_Data))]
2121
public void NullableNumber_NotEmpty<T>(T? value, bool expected) where T : struct, INumber<T>
2222
{
2323
// Arrange
@@ -77,7 +77,7 @@ public void NullableInt_NotEmpty(int? value, bool expected)
7777
Assert.False(result8);
7878
}
7979

80-
public static IEnumerable<object[]> Numbers_AndNulls_Data()
80+
public static IEnumerable<object[]> Nullable_Numbers_Data()
8181
{
8282
foreach (var value in Numbers_Data())
8383
{

RoyalCode.SmartValidations.Tests/BuildInPredicatesTests.NullOrNotEmpty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void Number_NullOrNotEmpty<T>(T value, bool expected)
1818
}
1919

2020
[Theory]
21-
[MemberData(nameof(Numbers_AndNulls_Data))]
21+
[MemberData(nameof(Nullable_Numbers_Data))]
2222
public void NullableNumber_NullOrNotEmpty<T>(T? value, bool expected)
2323
where T : struct, INumber<T>
2424
{

RoyalCode.SmartValidations/BuildInPredicates.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,24 +404,52 @@ public static bool BothNotEqual<T>(T? value1, T? value2)
404404

405405
#region Min Max
406406

407+
/// <summary>
408+
/// Validates whether the specified value is greater than or equal to the minimum value.
409+
/// </summary>
410+
/// <typeparam name="T">The value type.</typeparam>
411+
/// <param name="value">The value to validate.</param>
412+
/// <param name="min">The minimum value.</param>
413+
/// <returns>True if the value is greater than or equal to the minimum value, otherwise false.</returns>
407414
[MethodImpl(MethodImplOptions.AggressiveInlining)]
408415
public static bool Min<T>(T value, T min) where T: IComparable<T>
409416
{
410417
return value.CompareTo(min) >= 0;
411418
}
412419

420+
/// <summary>
421+
/// Validates whether the specified value is greater than or equal to the minimum value.
422+
/// </summary>
423+
/// <typeparam name="T">The value type.</typeparam>
424+
/// <param name="value">The value to validate.</param>
425+
/// <param name="min">The minimum value.</param>
426+
/// <returns>True if the value is greater than or equal to the minimum value, otherwise false.</returns>
413427
[MethodImpl(MethodImplOptions.AggressiveInlining)]
414428
public static bool Min<T>(T? value, T min) where T : struct, IComparable<T>
415429
{
416430
return value.HasValue && value.Value.CompareTo(min) >= 0;
417431
}
418432

433+
/// <summary>
434+
/// Validates whether the specified value is less than or equal to the maximum value.
435+
/// </summary>
436+
/// <typeparam name="T">The value type.</typeparam>
437+
/// <param name="value">The value to validate.</param>
438+
/// <param name="max">The maximum value.</param>
439+
/// <returns>True if the value is less than or equal to the maximum value, otherwise false.</returns>
419440
[MethodImpl(MethodImplOptions.AggressiveInlining)]
420441
public static bool Max<T>(T value, T max) where T: IComparable<T>
421442
{
422443
return value.CompareTo(max) <= 0;
423444
}
424445

446+
/// <summary>
447+
/// Validates whether the specified value is less than or equal to the maximum value.
448+
/// </summary>
449+
/// <typeparam name="T">The value type.</typeparam>
450+
/// <param name="value">The value to validate.</param>
451+
/// <param name="max">The maximum value.</param>
452+
/// <returns>True if the value is less than or equal to the maximum value, otherwise false.</returns>
425453
[MethodImpl(MethodImplOptions.AggressiveInlining)]
426454
public static bool Max<T>(T? value, T max) where T : struct, IComparable<T>
427455
{

0 commit comments

Comments
 (0)