Skip to content

Commit 04853b9

Browse files
committed
Refactor test method names for clarity and consistency; add new tests for overflow handling
1 parent 5810358 commit 04853b9

11 files changed

Lines changed: 197 additions & 181 deletions

DotNetExtensions/Core/EnumExtensions.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Numerics;
12
using System.Runtime.CompilerServices;
23

34
namespace DotNetExtensions.Core;
@@ -29,10 +30,13 @@ public static bool HasAnyFlag<TEnum>(this TEnum value, TEnum flag) where TEnum :
2930
return Unsafe.SizeOf<TEnum>() switch
3031
#pragma warning restore CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
3132
{
32-
sizeof(byte) => (Unsafe.As<TEnum, byte>(ref value) & Unsafe.As<TEnum, byte>(ref flag)) != 0,
33-
sizeof(ushort) => (Unsafe.As<TEnum, ushort>(ref value) & Unsafe.As<TEnum, ushort>(ref flag)) != 0,
34-
sizeof(uint) => (Unsafe.As<TEnum, uint>(ref value) & Unsafe.As<TEnum, uint>(ref flag)) != 0,
35-
sizeof(ulong) => (Unsafe.As<TEnum, ulong>(ref value) & Unsafe.As<TEnum, ulong>(ref flag)) != 0
33+
sizeof(byte) => CheckFlag<byte>(),
34+
sizeof(ushort) => CheckFlag<ushort>(),
35+
sizeof(uint) => CheckFlag<uint>(),
36+
sizeof(ulong) => CheckFlag<ulong>()
3637
};
38+
39+
bool CheckFlag<T>() where T : struct, IBinaryInteger<T> =>
40+
(Unsafe.As<TEnum, T>(ref value) & Unsafe.As<TEnum, T>(ref flag)) != T.Zero;
3741
}
3842
}

DotNetExtensions/DotNetExtensions.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
</PropertyGroup>
99

1010
<PropertyGroup>
11-
<Version>5.4.0</Version>
11+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
12+
<WarningsAsErrors>nullable</WarningsAsErrors>
13+
</PropertyGroup>
14+
15+
<PropertyGroup>
16+
<Version>5.5.0</Version>
1217
<Authors>Diogo Medeiros</Authors>
1318
<Company>Diogo Medeiros</Company>
1419
<Description>This is a collection of extension methods for .NET Core.</Description>
Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
1-
using System.Collections.Immutable;
21
using DotNetExtensions.Collections;
32

43
namespace DotNetExtensionsTests.Collections;
54

65
public partial class EnumerableExtensionsTests
76
{
87
[TestMethod]
9-
public void TestCombinationsWithZeroLength()
8+
public void Combinations_LengthZero_ReturnsSingleEmptyCombination()
109
{
11-
int[] source = [1, 2, 3, 4, 5];
12-
var result = source
13-
.Combinations(0)
14-
.Select(x => x.ToImmutableArray())
15-
.ToImmutableArray();
16-
17-
Assert.AreEqual(1, result.Length);
18-
Assert.IsTrue(result[0] is []);
10+
var result = DefaultSource.Combinations(0).ToList();
11+
Assert.HasCount(1, result);
12+
Assert.IsEmpty(result[0]);
1913
}
2014

2115
[TestMethod]
22-
public void TestCombinationsWithOneLength()
16+
public void Combinations_LengthOne_ReturnsAllSingleElementCombinations()
2317
{
24-
int[] source = [1, 2, 3, 4, 5];
25-
var result = source.Combinations(1)
26-
.Select(x => x.ToImmutableArray())
27-
.ToImmutableArray();
28-
29-
Assert.AreEqual(5, result.Length);
18+
var result = DefaultSource.Combinations(1).ToList();
19+
Assert.HasCount(5, result);
3020
Assert.IsTrue(result[0] is [1]);
3121
Assert.IsTrue(result[1] is [2]);
3222
Assert.IsTrue(result[2] is [3]);
@@ -35,14 +25,10 @@ public void TestCombinationsWithOneLength()
3525
}
3626

3727
[TestMethod]
38-
public void TestCombinationsWithTwoLength()
28+
public void Combinations_LengthTwo_ReturnsAllTwoElementCombinations()
3929
{
40-
int[] source = [1, 2, 3, 4, 5];
41-
var result = source.Combinations(2)
42-
.Select(x => x.ToImmutableArray())
43-
.ToImmutableArray();
44-
45-
Assert.AreEqual(10, result.Length);
30+
var result = DefaultSource.Combinations(2).ToList();
31+
Assert.HasCount(10, result);
4632
Assert.IsTrue(result[0] is [1, 2]);
4733
Assert.IsTrue(result[1] is [1, 3]);
4834
Assert.IsTrue(result[2] is [1, 4]);
@@ -56,14 +42,10 @@ public void TestCombinationsWithTwoLength()
5642
}
5743

5844
[TestMethod]
59-
public void TestCombinationsWithThreeLength()
45+
public void Combinations_LengthThree_ReturnsAllThreeElementCombinations()
6046
{
61-
int[] source = [1, 2, 3, 4, 5];
62-
var result = source.Combinations(3)
63-
.Select(x => x.ToImmutableArray())
64-
.ToImmutableArray();
65-
66-
Assert.AreEqual(10, result.Length);
47+
var result = DefaultSource.Combinations(3).ToList();
48+
Assert.HasCount(10, result);
6749
Assert.IsTrue(result[0] is [1, 2, 3]);
6850
Assert.IsTrue(result[1] is [1, 2, 4]);
6951
Assert.IsTrue(result[2] is [1, 2, 5]);
@@ -77,11 +59,15 @@ public void TestCombinationsWithThreeLength()
7759
}
7860

7961
[TestMethod]
80-
public void TestCombinationsWithLengthGreaterThanSource()
62+
public void Combinations_LengthGreaterThanSource_ReturnsEmpty()
8163
{
82-
int[] source = [1, 2, 3, 4, 5];
83-
var result = source.Combinations(6).ToImmutableArray();
64+
var result = DefaultSource.Combinations(6).ToList();
65+
Assert.IsEmpty(result);
66+
}
8467

85-
Assert.AreEqual(0, result.Length);
68+
[TestMethod]
69+
public void Combinations_NegativeLength_ThrowsArgumentOutOfRangeException()
70+
{
71+
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => DefaultSource.Combinations(-1));
8672
}
8773
}

DotNetExtensionsTests/Collections/DictionaryExtensionsTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace DotNetExtensionsTests.Collections;
66
public class DictionaryExtensionsTests
77
{
88
[TestMethod]
9-
public void TestGetOrAddNewKey()
9+
public void GetOrAdd_NewKey_AddsAndReturnsValue()
1010
{
1111
// Arrange
1212
var dictionary = new Dictionary<string, int>();
@@ -23,12 +23,12 @@ public void TestGetOrAddNewKey()
2323
}
2424

2525
[TestMethod]
26-
public void TestGetOrAddExistingKey()
26+
public void GetOrAdd_ExistingKey_ReturnsExistingValue()
2727
{
2828
// Arrange
2929
var dictionary = new Dictionary<string, int> { { "existingKey", 100 } };
3030
const string key = "existingKey";
31-
const int valueToAdd = 42; // This value should not be used since the key already exists.
31+
const int valueToAdd = 42; // Should not be used.
3232

3333
// Act
3434
var result = dictionary.GetOrAdd(key, valueToAdd);
@@ -39,7 +39,7 @@ public void TestGetOrAddExistingKey()
3939
}
4040

4141
[TestMethod]
42-
public void TestTryUpdateExistingKey()
42+
public void TryUpdate_ExistingKey_UpdatesValueAndReturnsTrue()
4343
{
4444
// Arrange
4545
var dictionary = new Dictionary<string, int> { { "existingKey", 100 } };
@@ -55,7 +55,7 @@ public void TestTryUpdateExistingKey()
5555
}
5656

5757
[TestMethod]
58-
public void TestTryUpdateNonExistingKey()
58+
public void TryUpdate_NonExistingKey_ReturnsFalseAndDoesNotAdd()
5959
{
6060
// Arrange
6161
var dictionary = new Dictionary<string, int>();
Lines changed: 56 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,150 @@
1-
using System.Collections.Immutable;
21
using DotNetExtensions.Collections;
32

43
namespace DotNetExtensionsTests.Collections;
54

65
[TestClass]
76
public partial class EnumerableExtensionsTests
87
{
8+
private static readonly int[] DefaultSource = [1, 2, 3, 4, 5];
9+
910
[TestMethod]
10-
public void TestPairwiseWithElements()
11+
public void Pairwise_SourceHasMultipleElements_ReturnsExpectedPairs()
1112
{
12-
int[] source = [1, 2, 3, 4, 5];
13-
var result = source.Pairwise().ToImmutableArray();
13+
var result = DefaultSource.Pairwise().ToList();
1414

15-
Assert.AreEqual(4, result.Length);
15+
Assert.HasCount(4, result);
1616
Assert.AreEqual((1, 2), result[0]);
1717
Assert.AreEqual((2, 3), result[1]);
1818
Assert.AreEqual((3, 4), result[2]);
1919
Assert.AreEqual((4, 5), result[3]);
2020
}
2121

2222
[TestMethod]
23-
public void TestPairwiseWithOneElement()
23+
public void Pairwise_SourceHasOneElement_ReturnsEmpty()
2424
{
2525
int[] source = [1];
26-
var result = source.Pairwise().ToImmutableArray();
26+
var result = source.Pairwise().ToList();
2727

28-
Assert.AreEqual(0, result.Length);
28+
Assert.IsEmpty(result);
2929
}
3030

3131
[TestMethod]
32-
public void TestPairwiseWithNoElements()
32+
public void Pairwise_SourceIsEmpty_ReturnsEmpty()
3333
{
3434
int[] source = [];
35-
var result = source.Pairwise().ToArray();
35+
var result = source.Pairwise().ToList();
3636

37-
Assert.AreEqual(0, result.Length);
37+
Assert.IsEmpty(result);
3838
}
3939

4040
[TestMethod]
41-
public void TestAdjacentWithValidLength()
41+
public void Adjacent_ValidLength_ReturnsSlidingWindows()
4242
{
43-
int[] source = [1, 2, 3, 4, 5];
44-
var result = source.Adjacent(3).ToArray();
43+
var result = DefaultSource.Adjacent(3).ToList();
4544

46-
Assert.AreEqual(3, result.Length);
47-
Assert.IsTrue(result[0] is [1, 2, 3]);
48-
Assert.IsTrue(result[1] is [2, 3, 4]);
49-
Assert.IsTrue(result[2] is [3, 4, 5]);
45+
Assert.HasCount(3, result);
46+
CollectionAssert.AreEqual(DefaultSource[..3], result[0]);
47+
CollectionAssert.AreEqual(DefaultSource[1..4], result[1]);
48+
CollectionAssert.AreEqual(DefaultSource[2..5], result[2]);
5049
}
5150

5251
[TestMethod]
53-
public void TestAdjacentWithInvalidLength()
52+
public void Adjacent_LengthGreaterThanSource_ReturnsEmpty()
5453
{
55-
int[] source = [1, 2, 3, 4, 5];
56-
var result = source.Adjacent(6).ToArray();
54+
var result = DefaultSource.Adjacent(6).ToList();
5755

58-
Assert.AreEqual([], result);
56+
Assert.IsEmpty(result);
5957
}
6058

6159
[TestMethod]
62-
public void TestAdjacentWithZeroLength()
60+
public void Adjacent_LengthZero_ReturnsEmpty()
6361
{
64-
int[] source = [1, 2, 3, 4, 5];
65-
var result = source.Adjacent(0).ToArray();
62+
var result = DefaultSource.Adjacent(0).ToList();
6663

67-
Assert.AreEqual([], result);
64+
Assert.IsEmpty(result);
6865
}
6966

7067
[TestMethod]
71-
public void TestAdjacentWithLengthOne()
68+
public void Adjacent_LengthOne_ReturnsSingleElementWindows()
7269
{
73-
int[] source = [1, 2, 3, 4, 5];
74-
var result = source.Adjacent(1).ToArray();
70+
var result = DefaultSource.Adjacent(1).ToList();
7571

76-
var expected = ImmutableArray.Create<int[]>([1], [2], [3], [4], [5]);
77-
CollectionAssert.AreEqual(expected, result);
72+
Assert.HasCount(5, result);
73+
CollectionAssert.AreEqual(DefaultSource[..1], result[0]);
74+
CollectionAssert.AreEqual(DefaultSource[1..2], result[1]);
75+
CollectionAssert.AreEqual(DefaultSource[2..3], result[2]);
76+
CollectionAssert.AreEqual(DefaultSource[3..4], result[3]);
7877
}
7978

8079
[TestMethod]
81-
public void TestAdjacentWithLengthEqualToSource()
80+
public void Adjacent_LengthEqualsSource_ReturnsWholeSourceAsWindow()
8281
{
83-
int[] source = [1, 2, 3, 4, 5];
84-
var result = source.Adjacent(5).ToArray();
82+
var result = DefaultSource.Adjacent(5).ToList();
8583

86-
Assert.AreEqual(1, result.Length);
87-
CollectionAssert.AreEqual(source, result[0]);
84+
Assert.HasCount(1, result);
85+
CollectionAssert.AreEqual(DefaultSource, result[0]);
8886
}
8987

9088
[TestMethod]
91-
public void TestSkipAtWithValidIndex()
89+
public void SkipAt_ValidIndex_RemovesElementAtIndex()
9290
{
93-
int[] source = [1, 2, 3, 4, 5];
94-
var result = source.SkipAt(4).ToArray();
95-
var expected = ImmutableArray.Create(1, 2, 3, 4);
91+
var result = DefaultSource.SkipAt(4).ToList();
9692

97-
CollectionAssert.AreEqual(expected, result);
93+
Assert.HasCount(4, result);
94+
CollectionAssert.AreEqual(DefaultSource[..4], result);
9895
}
9996

10097
[TestMethod]
101-
public void TestSkipAtWithInvalidIndex()
98+
public void SkipAt_IndexOutOfRange_ThrowsArgumentOutOfRangeException()
10299
{
103100
int[] source = [1, 2, 3, 4, 5];
104-
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => source.SkipAt(5).ToArray());
101+
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => source.SkipAt(5).ToList());
105102
}
106103

107104
[TestMethod]
108-
public void TestSkipAtWithNegativeIndex()
105+
public void SkipAt_NegativeIndex_ThrowsArgumentOutOfRangeException()
109106
{
110-
int[] source = [1, 2, 3, 4, 5];
111-
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => source.SkipAt(-1).ToArray());
107+
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => DefaultSource.SkipAt(-1));
112108
}
113109

114110
[TestMethod]
115-
public void TestSkipAtWithEmptySource()
111+
public void SkipAt_EmptySource_ThrowsArgumentOutOfRangeException()
116112
{
117113
int[] source = [];
118-
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => source.SkipAt(0).ToArray());
114+
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => source.SkipAt(0).ToList());
119115
}
120116

121117
[TestMethod]
122-
public void TestSkipAtOrDefaultWithValidIndex()
118+
public void SkipAtOrDefault_ValidIndex_RemovesElementAtIndex()
123119
{
124-
int[] source = [1, 2, 3, 4, 5];
125-
var result = source.SkipAtOrDefault(2).ToArray();
126-
var expected = new[] { 1, 2, 4, 5 };
120+
var result = DefaultSource.SkipAtOrDefault(2).ToList();
121+
int[] expected = [1, 2, 4, 5];
127122

128123
CollectionAssert.AreEqual(expected, result);
129124
}
130125

131126
[TestMethod]
132-
public void TestSkipAtOrDefaultWithNegativeIndex()
127+
public void SkipAtOrDefault_NegativeIndex_ReturnsOriginalSequence()
133128
{
134-
int[] source = [1, 2, 3, 4, 5];
135-
var result = source.SkipAtOrDefault(-1).ToArray();
129+
var result = DefaultSource.SkipAtOrDefault(-1).ToList();
136130

137-
CollectionAssert.AreEqual(source, result);
131+
CollectionAssert.AreEqual(DefaultSource, result);
138132
}
139133

140134
[TestMethod]
141-
public void TestSkipAtOrDefaultWithOutOfRangeIndex()
135+
public void SkipAtOrDefault_IndexOutOfRange_ReturnsOriginalSequence()
142136
{
143-
int[] source = [1, 2, 3, 4, 5];
144-
var result = source.SkipAtOrDefault(5).ToArray();
137+
var result = DefaultSource.SkipAtOrDefault(5).ToList();
145138

146-
CollectionAssert.AreEqual(source, result);
139+
CollectionAssert.AreEqual(DefaultSource, result);
147140
}
148141

149142
[TestMethod]
150-
public void TestSkipAtOrDefaultWithEmptySource()
143+
public void SkipAtOrDefault_EmptySource_ReturnsEmpty()
151144
{
152145
int[] source = [];
153-
var result = source.SkipAtOrDefault(0).ToArray();
146+
var result = source.SkipAtOrDefault(0).ToList();
154147

155-
Assert.AreEqual(source, result);
148+
Assert.IsEmpty(result);
156149
}
157150
}

0 commit comments

Comments
 (0)