Skip to content

Commit c24d2f4

Browse files
committed
tests and docs
1 parent 60c9c59 commit c24d2f4

3 files changed

Lines changed: 508 additions & 8 deletions

File tree

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
namespace RoyalCode.SmartValidations.Tests;
2+
3+
public partial class BuildInPredicatesTests
4+
{
5+
[Theory]
6+
[MemberData(nameof(Comparable_Data))]
7+
public void Comparable_LessThan<T>(T value, T other, int compare)
8+
where T : IComparable<T>
9+
{
10+
// Arrange
11+
bool expected = compare < 0;
12+
13+
// Act
14+
var result = BuildInPredicates.LessThan(value, other);
15+
16+
// Assert
17+
Assert.Equal(expected, result);
18+
}
19+
20+
[Theory]
21+
[MemberData(nameof(Nullable_Comparable_Data))]
22+
public void Nullable_Comparable_LessThan<T>(T? value, T? other, int compare)
23+
where T : struct, IComparable<T>
24+
{
25+
// Arrange
26+
bool expected = compare < 0;
27+
28+
// Act
29+
var result = BuildInPredicates.LessThan(value, other);
30+
31+
// Assert
32+
Assert.Equal(expected, result);
33+
}
34+
35+
[Theory]
36+
[InlineData(null, null, 0)]
37+
[InlineData(null, 0, -1)]
38+
[InlineData(0, null, 1)]
39+
[InlineData(0, 0, 0)]
40+
[InlineData(1, 0, 1)]
41+
[InlineData(0, 1, -1)]
42+
public void Comparable_Nulls_LessThan(int? value, int? other, int compare)
43+
{
44+
// Arrange
45+
bool expected = compare < 0;
46+
47+
// Act
48+
var result = BuildInPredicates.LessThan(value, other);
49+
50+
// Assert
51+
Assert.Equal(expected, result);
52+
}
53+
54+
[Theory]
55+
[MemberData(nameof(Comparable_Data))]
56+
public void Comparable_LessThanOrEqual<T>(T value, T other, int compare)
57+
where T : IComparable<T>
58+
{
59+
// Arrange
60+
bool expected = compare <= 0;
61+
62+
// Act
63+
var result = BuildInPredicates.LessThanOrEqual(value, other);
64+
65+
// Assert
66+
Assert.Equal(expected, result);
67+
}
68+
69+
[Theory]
70+
[MemberData(nameof(Nullable_Comparable_Data))]
71+
public void Nullable_Comparable_LessThanOrEqual<T>(T? value, T? other, int compare)
72+
where T : struct, IComparable<T>
73+
{
74+
// Arrange
75+
bool expected = compare <= 0;
76+
77+
// Act
78+
var result = BuildInPredicates.LessThanOrEqual(value, other);
79+
80+
// Assert
81+
Assert.Equal(expected, result);
82+
}
83+
84+
[Theory]
85+
[InlineData(null, null, 0)]
86+
[InlineData(null, 0, -1)]
87+
[InlineData(0, null, 1)]
88+
[InlineData(0, 0, 0)]
89+
[InlineData(1, 0, 1)]
90+
[InlineData(0, 1, -1)]
91+
public void Comparable_Nulls_LessThanOrEqual(int? value, int? other, int compare)
92+
{
93+
// Arrange
94+
bool expected = compare <= 0;
95+
96+
// Act
97+
var result = BuildInPredicates.LessThanOrEqual(value, other);
98+
99+
// Assert
100+
Assert.Equal(expected, result);
101+
}
102+
103+
[Theory]
104+
[MemberData(nameof(Comparable_Data))]
105+
public void Comparable_GreaterThan<T>(T value, T other, int compare)
106+
where T : IComparable<T>
107+
{
108+
// Arrange
109+
bool expected = compare > 0;
110+
111+
// Act
112+
var result = BuildInPredicates.GreaterThan(value, other);
113+
114+
// Assert
115+
Assert.Equal(expected, result);
116+
}
117+
118+
[Theory]
119+
[MemberData(nameof(Nullable_Comparable_Data))]
120+
public void Nullable_Comparable_GreaterThan<T>(T? value, T? other, int compare)
121+
where T : struct, IComparable<T>
122+
{
123+
// Arrange
124+
bool expected = compare > 0;
125+
126+
// Act
127+
var result = BuildInPredicates.GreaterThan(value, other);
128+
129+
// Assert
130+
Assert.Equal(expected, result);
131+
}
132+
133+
[Theory]
134+
[InlineData(null, null, 0)]
135+
[InlineData(null, 0, -1)]
136+
[InlineData(0, null, 1)]
137+
[InlineData(0, 0, 0)]
138+
[InlineData(1, 0, 1)]
139+
[InlineData(0, 1, -1)]
140+
public void Comparable_Nulls_GreaterThan(int? value, int? other, int compare)
141+
{
142+
// Arrange
143+
bool expected = compare > 0;
144+
145+
// Act
146+
var result = BuildInPredicates.GreaterThan(value, other);
147+
148+
// Assert
149+
Assert.Equal(expected, result);
150+
}
151+
152+
[Theory]
153+
[MemberData(nameof(Comparable_Data))]
154+
public void Comparable_GreaterThanOrEqual<T>(T value, T other, int compare)
155+
where T : IComparable<T>
156+
{
157+
// Arrange
158+
bool expected = compare >= 0;
159+
160+
// Act
161+
var result = BuildInPredicates.GreaterThanOrEqual(value, other);
162+
163+
// Assert
164+
Assert.Equal(expected, result);
165+
}
166+
167+
[Theory]
168+
[MemberData(nameof(Nullable_Comparable_Data))]
169+
public void Nullable_Comparable_GreaterThanOrEqual<T>(T? value, T? other, int compare)
170+
where T : struct, IComparable<T>
171+
{
172+
// Arrange
173+
bool expected = compare >= 0;
174+
175+
// Act
176+
var result = BuildInPredicates.GreaterThanOrEqual(value, other);
177+
178+
// Assert
179+
Assert.Equal(expected, result);
180+
}
181+
182+
[Theory]
183+
[InlineData(null, null, 0)]
184+
[InlineData(null, 0, -1)]
185+
[InlineData(0, null, 1)]
186+
[InlineData(0, 0, 0)]
187+
[InlineData(1, 0, 1)]
188+
[InlineData(0, 1, -1)]
189+
public void Comparable_Nulls_GreaterThanOrEqual(int? value, int? other, int compare)
190+
{
191+
// Arrange
192+
bool expected = compare >= 0;
193+
194+
// Act
195+
var result = BuildInPredicates.GreaterThanOrEqual(value, other);
196+
197+
// Assert
198+
Assert.Equal(expected, result);
199+
}
200+
}

RoyalCode.SmartValidations/BuildInPredicates.cs

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -520,34 +520,114 @@ public static bool Length(string? value, int min, int max)
520520
{
521521
return value is not null && value.Length >= min && value.Length <= max;
522522
}
523-
523+
524524
#endregion
525-
525+
526526
#region Less/Greater Than Or Equal
527-
527+
528+
/// <summary>
529+
/// Validates whether the specified value is less than the other value.
530+
/// </summary>
531+
/// <typeparam name="T">The value type.</typeparam>
532+
/// <param name="value">The value to validate.</param>
533+
/// <param name="other">The other value to compare with.</param>
534+
/// <returns>True if the value is less than the other value, otherwise false.</returns>
528535
[MethodImpl(MethodImplOptions.AggressiveInlining)]
529536
public static bool LessThan<T>(T value, T other) where T: IComparable<T>
530537
{
531538
return value.CompareTo(other) < 0;
532539
}
533-
540+
541+
/// <summary>
542+
/// Validates whether the specified value is less than the other value.
543+
/// </summary>
544+
/// <typeparam name="T">The value type.</typeparam>
545+
/// <param name="value">The value to validate.</param>
546+
/// <param name="other">The other value to compare with.</param>
547+
/// <returns>True if the value is less than the other value, otherwise false.</returns>
548+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
549+
public static bool LessThan<T>(T? value, T? other) where T : struct, IComparable<T>
550+
{
551+
return value is null && other.HasValue || (value.HasValue && other.HasValue && value.Value.CompareTo(other.Value) < 0);
552+
}
553+
554+
/// <summary>
555+
/// Validates whether the specified value is less than or equal to the other value.
556+
/// </summary>
557+
/// <typeparam name="T">The value type.</typeparam>
558+
/// <param name="value">The value to validate.</param>
559+
/// <param name="other">The other value to compare with.</param>
560+
/// <returns>True if the value is less than or equal to the other value, otherwise false.</returns>
534561
[MethodImpl(MethodImplOptions.AggressiveInlining)]
535562
public static bool LessThanOrEqual<T>(T value, T other) where T: IComparable<T>
536563
{
537564
return value.CompareTo(other) <= 0;
538565
}
539-
566+
567+
/// <summary>
568+
/// Validates whether the specified value is less than or equal to the other value.
569+
/// </summary>
570+
/// <typeparam name="T">The value type.</typeparam>
571+
/// <param name="value">The value to validate.</param>
572+
/// <param name="other">The other value to compare with.</param>
573+
/// <returns>True if the value is less than or equal to the other value, otherwise false.</returns>
574+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
575+
public static bool LessThanOrEqual<T>(T? value, T? other) where T : struct, IComparable<T>
576+
{
577+
return value is null || (other.HasValue && value.Value.CompareTo(other.Value) <= 0);
578+
}
579+
580+
/// <summary>
581+
/// Validates whether the specified value is greater than the other value.
582+
/// </summary>
583+
/// <typeparam name="T">The value type.</typeparam>
584+
/// <param name="value">The value to validate.</param>
585+
/// <param name="other">The other value to compare with.</param>
586+
/// <returns>True if the value is greater than the other value, otherwise false.</returns>
540587
[MethodImpl(MethodImplOptions.AggressiveInlining)]
541588
public static bool GreaterThan<T>(T value, T other) where T: IComparable<T>
542589
{
543590
return value.CompareTo(other) > 0;
544591
}
545-
592+
593+
/// <summary>
594+
/// Validates whether the specified value is greater than the other value.
595+
/// </summary>
596+
/// <typeparam name="T">The value type.</typeparam>
597+
/// <param name="value">The value to validate.</param>
598+
/// <param name="other">The other value to compare with.</param>
599+
/// <returns>True if the value is greater than the other value, otherwise false.</returns>
600+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
601+
public static bool GreaterThan<T>(T? value, T? other) where T : struct, IComparable<T>
602+
{
603+
return (other is null && value.HasValue) || (value.HasValue && other.HasValue && value.Value.CompareTo(other.Value) > 0);
604+
}
605+
606+
/// <summary>
607+
/// Validates whether the specified value is greater than or equal to the other value.
608+
/// </summary>
609+
/// <typeparam name="T">The value type.</typeparam>
610+
/// <param name="value">The value to validate.</param>
611+
/// <param name="other">The other value to compare with.</param>
612+
/// <returns>True if the value is greater than or equal to the other value, otherwise false.</returns>
546613
[MethodImpl(MethodImplOptions.AggressiveInlining)]
547614
public static bool GreaterThanOrEqual<T>(T value, T other) where T: IComparable<T>
548615
{
549616
return value.CompareTo(other) >= 0;
550617
}
551-
618+
619+
/// <summary>
620+
/// Validates whether the specified value is greater than or equal to the other value.
621+
/// </summary>
622+
/// <typeparam name="T">The value type.</typeparam>
623+
/// <param name="value">The value to validate.</param>
624+
/// <param name="other">The other value to compare with.</param>
625+
/// <returns>True if the value is greater than or equal to the other value, otherwise false.</returns>
626+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
627+
public static bool GreaterThanOrEqual<T>(T? value, T? other) where T : struct, IComparable<T>
628+
{
629+
return other is null || (value.HasValue && value.Value.CompareTo(other.Value) >= 0);
630+
}
631+
552632
#endregion
553633
}

0 commit comments

Comments
 (0)