Skip to content

Commit 60c9c59

Browse files
committed
Tests and docs
1 parent d28e6f5 commit 60c9c59

3 files changed

Lines changed: 325 additions & 65 deletions

File tree

RoyalCode.SmartValidations.Tests/BuildInPredicatesTests.MinMax.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,84 @@ public void Comparable_Nulls_Max(int? value, int max, bool expected)
9696
Assert.Equal(expected, result);
9797
}
9898

99+
[Theory]
100+
[MemberData(nameof(Comparable_MinMax_Data))]
101+
public void Comparable_MinMax<T>(T value, T min, T max, bool expected)
102+
where T : IComparable<T>
103+
{
104+
// Arrange
105+
// Act
106+
var result = BuildInPredicates.MinMax(value, min, max);
107+
108+
// Assert
109+
Assert.Equal(expected, result);
110+
}
111+
112+
[Theory]
113+
[MemberData(nameof(Comparable_MinMax_Data))]
114+
[InlineData(null, 0, 0, false)]
115+
[InlineData(null, 0, 1, false)]
116+
[InlineData(null, 1, 3, false)]
117+
public void Comparable_Nulls_MinMax(int? value, int min, int max, bool expected)
118+
{
119+
// Arrange
120+
// Act
121+
var result = BuildInPredicates.MinMax(value, min, max);
122+
123+
// Assert
124+
Assert.Equal(expected, result);
125+
}
126+
127+
[Theory]
128+
[InlineData(null, 0, false)]
129+
[InlineData("", 0, true)]
130+
[InlineData("a", 0, true)]
131+
[InlineData("", 1, false)]
132+
[InlineData("a", 1, true)]
133+
[InlineData("ab", 1, true)]
134+
public void String_MinLength(string? value, int minLength, bool expected)
135+
{
136+
// Arrange
137+
// Act
138+
var result = BuildInPredicates.MinLength(value, minLength);
139+
140+
// Assert
141+
Assert.Equal(expected, result);
142+
}
143+
144+
[Theory]
145+
[InlineData(null, 0, true)]
146+
[InlineData("", 0, true)]
147+
[InlineData("a", 0, false)]
148+
[InlineData("", 1, true)]
149+
[InlineData("a", 1, true)]
150+
[InlineData("ab", 1, false)]
151+
public void String_MaxLength(string? value, int maxLength, bool expected)
152+
{
153+
// Arrange
154+
// Act
155+
var result = BuildInPredicates.MaxLength(value, maxLength);
156+
// Assert
157+
Assert.Equal(expected, result);
158+
}
159+
160+
[Theory]
161+
[InlineData(null, 0, 0, false)]
162+
[InlineData(null, 0, 1, false)]
163+
[InlineData("", 0, 0, true)]
164+
[InlineData("", 0, 1, true)]
165+
[InlineData("a", 0, 0, false)]
166+
[InlineData("a", 0, 1, true)]
167+
[InlineData("ab", 0, 1, false)]
168+
public void String_Length(string? value, int minLength, int maxLength, bool expected)
169+
{
170+
// Arrange
171+
// Act
172+
var result = BuildInPredicates.Length(value, minLength, maxLength);
173+
174+
// Assert
175+
Assert.Equal(expected, result);
176+
}
99177

100178
public static IEnumerable<object[]> Nullable_Comparable_Data()
101179
{
@@ -165,4 +243,13 @@ public static IEnumerable<object[]> Comparable_Data()
165243
yield return [BigInteger.One, BigInteger.One, 0];
166244
yield return [new BigInteger(2), BigInteger.One, 1];
167245
}
246+
247+
public static IEnumerable<object[]> Comparable_MinMax_Data()
248+
{
249+
yield return [0, 1, 3, false];
250+
yield return [1, 1, 3, true];
251+
yield return [2, 1, 3, true];
252+
yield return [3, 1, 3, true];
253+
yield return [4, 1, 3, false];
254+
}
168255
}

RoyalCode.SmartValidations/BuildInPredicates.cs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,34 +456,69 @@ public static bool Max<T>(T? value, T max) where T : struct, IComparable<T>
456456
return value.HasValue && value.Value.CompareTo(max) <= 0;
457457
}
458458

459+
/// <summary>
460+
/// Validates whether the specified value is within the specified range (inclusive).
461+
/// </summary>
462+
/// <typeparam name="T">The value type.</typeparam>
463+
/// <param name="value">The value to validate.</param>
464+
/// <param name="min">The minimum value.</param>
465+
/// <param name="max">The maximum value.</param>
466+
/// <returns>True if the value is within the specified range (inclusive), otherwise false.</returns>
459467
[MethodImpl(MethodImplOptions.AggressiveInlining)]
460468
public static bool MinMax<T>(T value, T min, T max) where T: IComparable<T>
461469
{
462470
return value.CompareTo(min) >= 0 && value.CompareTo(max) <= 0;
463471
}
464472

473+
/// <summary>
474+
/// Validates whether the specified value is within the specified range (inclusive).
475+
/// </summary>
476+
/// <typeparam name="T">The value type.</typeparam>
477+
/// <param name="value">The value to validate.</param>
478+
/// <param name="min">The minimum value.</param>
479+
/// <param name="max">The maximum value.</param>
480+
/// <returns>True if the value is within the specified range (inclusive), otherwise false.</returns>
465481
[MethodImpl(MethodImplOptions.AggressiveInlining)]
466482
public static bool MinMax<T>(T? value, T min, T max) where T : struct, IComparable<T>
467483
{
468484
return value.HasValue && value.Value.CompareTo(min) >= 0 && value.Value.CompareTo(max) <= 0;
469485
}
470486

487+
/// <summary>
488+
/// Validates whether the specified string value has a minimum length.
489+
/// </summary>
490+
/// <param name="value">The string value to validate.</param>
491+
/// <param name="length">The minimum length.</param>
492+
/// <returns>True if the string value has a minimum length, otherwise false.</returns>
471493
[MethodImpl(MethodImplOptions.AggressiveInlining)]
472494
public static bool MinLength(string? value, int length)
473495
{
474-
return value?.Length >= length;
496+
return value is not null && value.Length >= length;
475497
}
476-
498+
499+
/// <summary>
500+
/// Validates whether the specified string value has a maximum length.
501+
/// </summary>
502+
/// <param name="value">The string value to validate.</param>
503+
/// <param name="length">The maximum length.</param>
504+
/// <returns>True if the string value has a maximum length, otherwise false.</returns>
477505
[MethodImpl(MethodImplOptions.AggressiveInlining)]
478506
public static bool MaxLength(string? value, int length)
479507
{
480-
return value?.Length <= length;
508+
return value is null || value.Length <= length;
481509
}
482-
510+
511+
/// <summary>
512+
/// Validates whether the specified string value has a length within the specified range (inclusive).
513+
/// </summary>
514+
/// <param name="value">The string value to validate.</param>
515+
/// <param name="min">The minimum length.</param>
516+
/// <param name="max">The maximum length.</param>
517+
/// <returns>True if the string value has a length within the specified range (inclusive), otherwise false.</returns>
483518
[MethodImpl(MethodImplOptions.AggressiveInlining)]
484519
public static bool Length(string? value, int min, int max)
485520
{
486-
return value?.Length >= min && value.Length <= max;
521+
return value is not null && value.Length >= min && value.Length <= max;
487522
}
488523

489524
#endregion

0 commit comments

Comments
 (0)