Skip to content

Commit 2715134

Browse files
committed
Add unit tests for number types to Between Inclusive and Exclusive.
1 parent 8725613 commit 2715134

File tree

2 files changed

+101
-78
lines changed

2 files changed

+101
-78
lines changed

CodingFlow.FluentValidation.UnitTests/BetweenExclusiveValidatorTests.cs

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1-
using CodingFlow.FluentValidation.Validators;
1+
using System.Numerics;
2+
using CodingFlow.FluentValidation.Validators;
23
using FluentAssertions;
34
using static CodingFlow.FluentValidation.Validations;
45

56
namespace CodingFlow.FluentValidation.UnitTests;
67

78
public class BetweenExclusiveValidatorTests
89
{
9-
[TestCase(5)]
10-
public void Between_Integer_Valid(int input)
10+
private static readonly object[] BetweenExclusive_Valid_Cases = [
11+
new short[] { 5, 4, 6 },
12+
new ushort[] { 5, 4, 6 },
13+
new sbyte[] { 5, 4, 6 },
14+
new byte[] { 5, 4, 6 },
15+
new decimal[] { 5.5m, 5.4m, 5.6m },
16+
];
17+
18+
[TestCase(5, 4, 6)]
19+
[TestCase(5u, 4u, 6u)]
20+
[TestCase(5L, 4L, 6L)]
21+
[TestCase(5ul, 4ul, 6ul)]
22+
[TestCase(5.5f, 5.4f, 5.6f)]
23+
[TestCase(5.5d, 5.4d, 5.6d)]
24+
[TestCaseSource(nameof(BetweenExclusive_Valid_Cases))]
25+
public void BetweenExclusive_Valid<T>(T input, T minimum, T maximum)
26+
where T : INumber<T>
1127
{
1228
var result = RuleFor(input)
13-
.BetweenExclusive(4, 6)
29+
.BetweenExclusive(minimum, maximum)
1430
.Result();
1531

1632
result.Should().BeEquivalentTo(new ValidationResult
@@ -20,42 +36,32 @@ public void Between_Integer_Valid(int input)
2036
});
2137
}
2238

23-
[TestCase(5f)]
24-
public void Between_Float_Valid(float input)
25-
{
26-
var result = RuleFor(input)
27-
.BetweenExclusive(4f, 6f)
28-
.Result();
39+
private static readonly object[] BetweenExclusive_Invalid_Cases = [
40+
new short[] { 4, 4, 6 },
41+
new short[] { 6, 4, 6 },
2942

30-
result.Should().BeEquivalentTo(new ValidationResult
31-
{
32-
IsValid = true,
33-
Errors = []
34-
});
35-
}
43+
new ushort[] { 4, 4, 6 },
44+
new ushort[] { 6, 4, 6 },
3645

37-
[TestCase(4, 4, 6)]
38-
[TestCase(6, 4, 6)]
39-
[TestCase(-1, 4, 6)]
40-
public void Between_Integer_Invalid(int input, int minimum, int maximum)
41-
{
42-
var result = RuleFor(input)
43-
.BetweenExclusive(minimum, maximum)
44-
.Result();
46+
new sbyte[] { 4, 4, 6 },
47+
new sbyte[] { 6, 4, 6 },
4548

46-
result.Should().BeEquivalentTo(new ValidationResult
47-
{
48-
IsValid = false,
49-
Errors = [
50-
new() { Message = $"Value '{input}' of type System.Int32 is not between {minimum} and {maximum}."}
51-
]
52-
});
53-
}
49+
new byte[] { 4, 4, 6 },
50+
new byte[] { 6, 4, 6 },
51+
52+
new decimal[] { 5.4m, 5.4m, 5.6m },
53+
new decimal[] { 5.6m, 5.4m, 5.6m },
54+
];
5455

55-
[TestCase(4f, 4f, 6f)]
56-
[TestCase(6f, 4f, 6f)]
57-
[TestCase(-1f, 4f, 6f)]
58-
public void Between_Float_Invalid(float input, float minimum, float maximum)
56+
[TestCase(4, 4, 6)] [TestCase(6, 4, 6)]
57+
[TestCase(4u, 4u, 6u)] [TestCase(6u, 4u, 6u)]
58+
[TestCase(4L, 4L, 6L)] [TestCase(6L, 4L, 6L)]
59+
[TestCase(4ul, 4ul, 6ul)] [TestCase(6ul, 4ul, 6ul)]
60+
[TestCase(5.4f, 5.4f, 5.6f)] [TestCase(5.6f, 5.4f, 5.6f)]
61+
[TestCase(5.4d, 5.4d, 5.6d)] [TestCase(5.6d, 5.4d, 5.6d)]
62+
[TestCaseSource(nameof(BetweenExclusive_Invalid_Cases))]
63+
public void BetweenExclusive_Invalid<T>(T input, T minimum, T maximum)
64+
where T : INumber<T>
5965
{
6066
var result = RuleFor(input)
6167
.BetweenExclusive(minimum, maximum)
@@ -65,7 +71,7 @@ public void Between_Float_Invalid(float input, float minimum, float maximum)
6571
{
6672
IsValid = false,
6773
Errors = [
68-
new() { Message = $"Value '{input}' of type System.Single is not between {minimum} and {maximum}."}
74+
new() { Message = $"Value '{input}' of type {typeof(T)} is not between {minimum} and {maximum}."}
6975
]
7076
});
7177
}

CodingFlow.FluentValidation.UnitTests/BetweenInclusiveValidatorTests.cs

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
1-
using CodingFlow.FluentValidation.Validators;
1+
using System.ComponentModel.DataAnnotations;
2+
using System.Numerics;
3+
using CodingFlow.FluentValidation.Validators;
24
using FluentAssertions;
35
using static CodingFlow.FluentValidation.Validations;
46

57
namespace CodingFlow.FluentValidation.UnitTests;
68

79
public class BetweenInclusiveValidatorTests
810
{
9-
[TestCase(4)]
10-
[TestCase(5)]
11-
[TestCase(6)]
12-
public void BetweenInclusive_Integer_Valid(int input)
13-
{
14-
var result = RuleFor(input)
15-
.BetweenInclusive(4, 6)
16-
.Result();
11+
private static readonly object[] BetweenInclusive_Valid_Cases = [
12+
new short[] { 5, 4, 6 },
13+
new short[] { 4, 4, 6 },
14+
new short[] { 6, 4, 6 },
1715

18-
result.Should().BeEquivalentTo(new ValidationResult
19-
{
20-
IsValid = true,
21-
Errors = []
22-
});
23-
}
16+
new ushort[] { 5, 4, 6 },
17+
new ushort[] { 4, 4, 6 },
18+
new ushort[] { 6, 4, 6 },
19+
20+
new sbyte[] { 5, 4, 6 },
21+
new sbyte[] { 4, 4, 6 },
22+
new sbyte[] { 6, 4, 6 },
23+
24+
new byte[] { 5, 4, 6 },
25+
new byte[] { 4, 4, 6 },
26+
new byte[] { 6, 4, 6 },
2427

25-
[TestCase(4f)]
26-
[TestCase(5f)]
27-
[TestCase(6f)]
28-
public void BetweenInclusive_Float_Valid(float input)
28+
new decimal[] { 5.5m, 5.4m, 5.6m },
29+
new decimal[] { 5.4m, 5.4m, 5.6m },
30+
new decimal[] { 5.6m, 5.4m, 5.6m },
31+
];
32+
33+
[TestCase(5, 4, 6)] [TestCase(4, 4, 6)] [TestCase(6, 4, 6)]
34+
[TestCase(5u, 4u, 6u)] [TestCase(4u, 4u, 6u)] [TestCase(6u, 4u, 6u)]
35+
[TestCase(5L, 4L, 6L)] [TestCase(4L, 4L, 6L)] [TestCase(6L, 4L, 6L)]
36+
[TestCase(5ul, 4ul, 6ul)] [TestCase(4ul, 4ul, 6ul)] [TestCase(6ul, 4ul, 6ul)]
37+
[TestCase(5.5f, 5.4f, 5.6f)] [TestCase(5.4f, 5.4f, 5.6f)] [TestCase(5.6f, 5.4f, 5.6f)]
38+
[TestCase(5.5d, 5.4d, 5.6d)] [TestCase(5.4d, 5.4d, 5.6d)] [TestCase(5.6d, 5.4d, 5.6d)]
39+
[TestCaseSource(nameof(BetweenInclusive_Valid_Cases))]
40+
public void BetweenInclusive_Valid<T>(T input, T minimum, T maximum)
41+
where T : INumber<T>
2942
{
3043
var result = RuleFor(input)
31-
.BetweenInclusive(4f, 6f)
44+
.BetweenInclusive(minimum, maximum)
3245
.Result();
3346

3447
result.Should().BeEquivalentTo(new ValidationResult
@@ -38,28 +51,32 @@ public void BetweenInclusive_Float_Valid(float input)
3851
});
3952
}
4053

41-
[TestCase(3, 4, 6)]
42-
[TestCase(7, 4, 6)]
43-
[TestCase(-1, 4, 6)]
44-
public void BetweenInclusive_Integer_Invalid(int input, int minimum, int maximum)
45-
{
46-
var result = RuleFor(input)
47-
.BetweenInclusive(minimum, maximum)
48-
.Result();
54+
private static readonly object[] BetweenInclusive_Invalid_Cases = [
55+
new short[] { 3, 4, 6 },
56+
new short[] { 7, 4, 6 },
4957

50-
result.Should().BeEquivalentTo(new ValidationResult
51-
{
52-
IsValid = false,
53-
Errors = [
54-
new() { Message = $"Value '{input}' of type System.Int32 is not equal to or between {minimum} and {maximum}."}
55-
]
56-
});
57-
}
58+
new ushort[] { 3, 4, 6 },
59+
new ushort[] { 7, 4, 6 },
60+
61+
new sbyte[] { 3, 4, 6 },
62+
new sbyte[] { 7, 4, 6 },
63+
64+
new byte[] { 3, 4, 6 },
65+
new byte[] { 7, 4, 6 },
66+
67+
new decimal[] { 5.39m, 5.4m, 5.6m },
68+
new decimal[] { 5.61m, 5.4m, 5.6m },
69+
];
5870

59-
[TestCase(3f, 4f, 6f)]
60-
[TestCase(7f, 4f, 6f)]
61-
[TestCase(-1f, 4f, 6f)]
62-
public void BetweenInclusive_Float_Invalid(float input, float minimum, float maximum)
71+
[TestCase(3, 4, 6)] [TestCase(7, 4, 6)]
72+
[TestCase(3u, 4u, 6u)] [TestCase(7u, 4u, 6u)]
73+
[TestCase(3L, 4L, 6L)] [TestCase(7L, 4L, 6L)]
74+
[TestCase(3ul, 4ul, 6ul)] [TestCase(7ul, 4ul, 6ul)]
75+
[TestCase(5.39f, 5.4f, 5.6f)] [TestCase(5.61f, 5.4f, 5.6f)]
76+
[TestCase(5.39d, 5.4d, 5.6d)] [TestCase(5.61d, 5.4d, 5.6d)]
77+
[TestCaseSource(nameof(BetweenInclusive_Invalid_Cases))]
78+
public void BetweenInclusive_Invalid<T>(T input, T minimum, T maximum)
79+
where T : INumber<T>
6380
{
6481
var result = RuleFor(input)
6582
.BetweenInclusive(minimum, maximum)
@@ -69,7 +86,7 @@ public void BetweenInclusive_Float_Invalid(float input, float minimum, float max
6986
{
7087
IsValid = false,
7188
Errors = [
72-
new() { Message = $"Value '{input}' of type System.Single is not equal to or between {minimum} and {maximum}."}
89+
new() { Message = $"Value '{input}' of type {typeof(T)} is not equal to or between {minimum} and {maximum}."}
7390
]
7491
});
7592
}

0 commit comments

Comments
 (0)