Skip to content

Commit 0258fec

Browse files
committed
Refactored Guard.IsCloseTo unit tests
1 parent 8c01abc commit 0258fec

File tree

1 file changed

+43
-56
lines changed

1 file changed

+43
-56
lines changed

UnitTests/UnitTests.Shared/Diagnostics/Test_Guard.Comparable.Numeric.cs

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Diagnostics.CodeAnalysis;
76
using Microsoft.Toolkit.Diagnostics;
87
using Microsoft.VisualStudio.TestTools.UnitTesting;
98

@@ -13,88 +12,76 @@ public partial class Test_Guard
1312
{
1413
[TestCategory("Guard")]
1514
[TestMethod]
16-
public void Test_Guard_IsCloseToInt_Ok()
15+
[DataRow(0, 20, 10u, false)]
16+
[DataRow(0, 6, 5u, false)]
17+
[DataRow(0, int.MaxValue, 500u, false)]
18+
[DataRow(-500, -530, 10u, false)]
19+
[DataRow(1000, 800, 100u, false)]
20+
[DataRow(int.MaxValue, int.MaxValue - 10, 7u, false)]
21+
[DataRow(int.MinValue, int.MaxValue, (uint)int.MaxValue, false)]
22+
[DataRow(0, 5, 10u, true)]
23+
[DataRow(0, 5, 5u, true)]
24+
[DataRow(0, int.MaxValue, (uint)int.MaxValue, true)]
25+
[DataRow(-500, -530, 50u, true)]
26+
[DataRow(1000, 800, 200u, true)]
27+
[DataRow(int.MaxValue, int.MaxValue - 10, 10u, true)]
28+
public void Test_Guard_IsCloseToInt(int value, int target, uint delta, bool isClose)
1729
{
18-
Guard.IsCloseTo(0, 5, 10, nameof(Test_Guard_IsCloseToInt_Ok));
19-
Guard.IsCloseTo(0, 5, 5, nameof(Test_Guard_IsCloseToInt_Ok));
20-
Guard.IsCloseTo(0, int.MaxValue, int.MaxValue, nameof(Test_Guard_IsCloseToInt_Ok));
21-
Guard.IsCloseTo(-500, -530, 50, nameof(Test_Guard_IsCloseToInt_Ok));
22-
Guard.IsCloseTo(1000, 800, 200, nameof(Test_Guard_IsCloseToInt_Ok));
23-
Guard.IsCloseTo(int.MaxValue, int.MaxValue - 10, 10, nameof(Test_Guard_IsCloseToInt_Ok));
24-
}
25-
26-
[TestCategory("Guard")]
27-
[TestMethod]
28-
[SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1000", Justification = "Value tuple")]
29-
public void Test_Guard_IsCloseToInt_Fail()
30-
{
31-
foreach (var item in new (int Value, int Target, uint Delta)[]
32-
{
33-
(0, 20, 10),
34-
(0, 6, 5),
35-
(0, int.MaxValue, 500),
36-
(-500, -530, 10),
37-
(1000, 800, 100),
38-
(int.MaxValue, int.MaxValue - 10, 7),
39-
(int.MinValue, int.MaxValue, int.MaxValue)
40-
})
30+
void Test(int value, int target)
4131
{
42-
bool fail = false;
32+
bool isFailed = false;
4333

4434
try
4535
{
46-
Guard.IsCloseTo(item.Value, item.Target, item.Delta, nameof(Test_Guard_IsCloseToInt_Fail));
36+
Guard.IsCloseTo(value, target, delta, nameof(Test_Guard_IsCloseToInt));
4737
}
4838
catch (ArgumentException)
4939
{
50-
fail = true;
40+
isFailed = true;
5141
}
5242

53-
Assert.IsTrue(fail, $"IsCloseTo didn't fail with {item}");
43+
Assert.AreEqual(isClose, !isFailed);
5444
}
55-
}
5645

57-
[TestCategory("Guard")]
58-
[TestMethod]
59-
public void Test_Guard_IsCloseToFloat_Ok()
60-
{
61-
Guard.IsCloseTo(0f, 5, 10, nameof(Test_Guard_IsCloseToFloat_Ok));
62-
Guard.IsCloseTo(0f, 5, 5, nameof(Test_Guard_IsCloseToFloat_Ok));
63-
Guard.IsCloseTo(0f, float.MaxValue, float.MaxValue, nameof(Test_Guard_IsCloseToFloat_Ok));
64-
Guard.IsCloseTo(-500f, -530, 50, nameof(Test_Guard_IsCloseToFloat_Ok));
65-
Guard.IsCloseTo(1000f, 800, 200, nameof(Test_Guard_IsCloseToFloat_Ok));
66-
Guard.IsCloseTo(float.MaxValue, float.MaxValue - 10, 10, nameof(Test_Guard_IsCloseToFloat_Ok));
46+
Test(value, target);
47+
Test(target, value);
6748
}
6849

6950
[TestCategory("Guard")]
7051
[TestMethod]
71-
[SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1000", Justification = "Value tuple")]
72-
public void Test_Guard_IsCloseToFloat_Fail()
52+
[DataRow(0f, 20f, 10f, false)]
53+
[DataRow(0f, 6f, 5f, false)]
54+
[DataRow(0f, float.MaxValue, 500f, false)]
55+
[DataRow(-500f, -530f, 10f, false)]
56+
[DataRow(1000f, 800f, 100f, false)]
57+
[DataRow(float.MaxValue, float.MaxValue / 2, 7f, false)]
58+
[DataRow(float.MinValue, float.MaxValue, float.MaxValue, false)]
59+
[DataRow(0f, 5f, 10f, true)]
60+
[DataRow(0f, 5f, 5f, true)]
61+
[DataRow(0f, float.MaxValue, float.MaxValue, true)]
62+
[DataRow(-500f, -530f, 50f, true)]
63+
[DataRow(1000f, 800f, 200f, true)]
64+
[DataRow(float.MaxValue, float.MaxValue - 10, 10f, true)]
65+
public void Test_Guard_IsCloseToFloat(float value, float target, float delta, bool isClose)
7366
{
74-
foreach (var item in new (float Value, float Target, float Delta)[]
75-
{
76-
(0, 20, 10),
77-
(0, 6, 5),
78-
(0, float.MaxValue, 500),
79-
(-500, -530, 10),
80-
(1000, 800, 100),
81-
(float.MaxValue, float.MaxValue / 2, 7),
82-
(float.MinValue, float.MaxValue, float.MaxValue)
83-
})
67+
void Test(float value, float target)
8468
{
85-
bool fail = false;
69+
bool isFailed = false;
8670

8771
try
8872
{
89-
Guard.IsCloseTo(item.Value, item.Target, item.Delta, nameof(Test_Guard_IsCloseToFloat_Fail));
73+
Guard.IsCloseTo(value, target, delta, nameof(Test_Guard_IsCloseToInt));
9074
}
9175
catch (ArgumentException)
9276
{
93-
fail = true;
77+
isFailed = true;
9478
}
9579

96-
Assert.IsTrue(fail, $"IsCloseTo didn't fail with {item}");
80+
Assert.AreEqual(isClose, !isFailed);
9781
}
82+
83+
Test(value, target);
84+
Test(target, value);
9885
}
9986
}
10087
}

0 commit comments

Comments
 (0)