Skip to content

Commit f865e22

Browse files
committed
Migrated unit tests to TUnit
1 parent 4731940 commit f865e22

17 files changed

Lines changed: 357 additions & 398 deletions

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,5 @@ FodyWeavers.xsd
525525

526526
### VisualStudio Patch ###
527527
# Additional files built by Visual Studio
528-
report/
529528

530529
# End of https://www.toptal.com/developers/gitignore/api/visualstudio,rider,dotnetcore,macos

DotNetExtensionsTests/Collections/AdjacentTests.cs

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,44 @@ namespace DotNetExtensionsTests.Collections;
44

55
public partial class EnumerableExtensionsTests
66
{
7-
[TestMethod]
8-
public void Adjacent_ValidLength_ReturnsSlidingWindows()
7+
[Test]
8+
[Arguments(3, 3)]
9+
[Arguments(4, 2)]
10+
[Arguments(5, 1)]
11+
public async Task Adjacent_ValidLength_ReturnsSlidingWindows(int length, int expectedCount)
912
{
10-
var result = _defaultSource.Adjacent(3).ToList();
13+
var result = _defaultSource.Adjacent(length).ToList();
1114

12-
Assert.HasCount(3, result);
13-
CollectionAssert.AreEqual(_defaultSource[..3], result[0]);
14-
CollectionAssert.AreEqual(_defaultSource[1..4], result[1]);
15-
CollectionAssert.AreEqual(_defaultSource[2..5], result[2]);
15+
using var _ = Assert.Multiple();
16+
await Assert.That(result).Count().IsEqualTo(expectedCount);
17+
foreach (var (i, item) in result.Index())
18+
await Assert.That(item).IsEquivalentTo(_defaultSource[i..(i + length)]);
1619
}
1720

18-
[TestMethod]
19-
public void Adjacent_LengthGreaterThanSource_ReturnsEmpty()
21+
[Test]
22+
public async Task Adjacent_LengthGreaterThanSource_ReturnsEmpty()
2023
{
21-
var result = _defaultSource.Adjacent(6).ToList();
22-
23-
Assert.IsEmpty(result);
24+
var result = _defaultSource.Adjacent(6);
25+
await Assert.That(result).IsEmpty();
2426
}
2527

26-
[TestMethod]
27-
public void Adjacent_LengthZero_ReturnsEmpty()
28+
[Test]
29+
public async Task Adjacent_LengthZero_ReturnsEmpty()
2830
{
29-
var result = _defaultSource.Adjacent(0).ToList();
30-
31-
Assert.IsEmpty(result);
31+
var result = _defaultSource.Adjacent(0);
32+
await Assert.That(result).IsEmpty();
3233
}
3334

34-
[TestMethod]
35-
public void Adjacent_LengthOne_ReturnsSingleElementWindows()
35+
[Test]
36+
public async Task Adjacent_LengthEqualsSource_ReturnsWholeSourceAsWindow()
3637
{
37-
var result = _defaultSource.Adjacent(1).ToList();
38-
39-
Assert.HasCount(5, result);
40-
CollectionAssert.AreEqual(_defaultSource[..1], result[0]);
41-
CollectionAssert.AreEqual(_defaultSource[1..2], result[1]);
42-
CollectionAssert.AreEqual(_defaultSource[2..3], result[2]);
43-
CollectionAssert.AreEqual(_defaultSource[3..4], result[3]);
38+
var result = _defaultSource.Adjacent(_defaultSource.Length);
39+
await Assert.That(result).IsEquivalentTo([_defaultSource]);
4440
}
4541

46-
[TestMethod]
47-
public void Adjacent_LengthEqualsSource_ReturnsWholeSourceAsWindow()
42+
[Test]
43+
public void Adjacent_LengthNegative_ThrowsArgumentOutOfRangeException()
4844
{
49-
var result = _defaultSource.Adjacent(5).ToList();
50-
51-
Assert.HasCount(1, result);
52-
CollectionAssert.AreEqual(_defaultSource, result[0]);
45+
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => _defaultSource.Adjacent(-1));
5346
}
5447
}

DotNetExtensionsTests/Collections/CombinationsTests.cs

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,75 @@ namespace DotNetExtensionsTests.Collections;
44

55
public partial class EnumerableExtensionsTests
66
{
7-
[TestMethod]
8-
public void Combinations_LengthZero_ReturnsSingleEmptyCombination()
7+
[Test]
8+
public async Task Combinations_LengthZero_ReturnsSingleEmptyCombination()
99
{
1010
var result = _defaultSource.Combinations(0).ToList();
11-
Assert.HasCount(1, result);
12-
Assert.IsEmpty(result[0]);
11+
12+
using var _ = Assert.Multiple();
13+
await Assert.That(result).HasSingleItem();
14+
await Assert.That(result[0]).IsEmpty();
1315
}
1416

15-
[TestMethod]
16-
public void Combinations_LengthOne_ReturnsAllSingleElementCombinations()
17+
[Test]
18+
public async Task Combinations_LengthOne_ReturnsAllSingleElementCombinations()
1719
{
18-
var result = _defaultSource.Combinations(1).ToList();
19-
Assert.HasCount(5, result);
20-
Assert.IsTrue(result[0] is [1]);
21-
Assert.IsTrue(result[1] is [2]);
22-
Assert.IsTrue(result[2] is [3]);
23-
Assert.IsTrue(result[3] is [4]);
24-
Assert.IsTrue(result[4] is [5]);
20+
const int length = 1;
21+
var result = _defaultSource.Combinations(length).ToList();
22+
23+
using var _ = Assert.Multiple();
24+
await Assert.That(result).Count().IsEqualTo(_defaultSource.Length);
25+
26+
foreach (var (i, item) in result.Index())
27+
await Assert.That(item).IsEquivalentTo(_defaultSource[i..(i + length)]);
2528
}
2629

27-
[TestMethod]
28-
public void Combinations_LengthTwo_ReturnsAllTwoElementCombinations()
30+
[Test]
31+
public async Task Combinations_LengthTwo_ReturnsAllTwoElementCombinations()
2932
{
3033
var result = _defaultSource.Combinations(2).ToList();
31-
Assert.HasCount(10, result);
32-
Assert.IsTrue(result[0] is [1, 2]);
33-
Assert.IsTrue(result[1] is [1, 3]);
34-
Assert.IsTrue(result[2] is [1, 4]);
35-
Assert.IsTrue(result[3] is [1, 5]);
36-
Assert.IsTrue(result[4] is [2, 3]);
37-
Assert.IsTrue(result[5] is [2, 4]);
38-
Assert.IsTrue(result[6] is [2, 5]);
39-
Assert.IsTrue(result[7] is [3, 4]);
40-
Assert.IsTrue(result[8] is [3, 5]);
41-
Assert.IsTrue(result[9] is [4, 5]);
34+
35+
using var _ = Assert.Multiple();
36+
await Assert.That(result).Count().IsEqualTo(10);
37+
await Assert.That(result[0]).IsEquivalentTo([1, 2]);
38+
await Assert.That(result[1]).IsEquivalentTo([1, 3]);
39+
await Assert.That(result[2]).IsEquivalentTo([1, 4]);
40+
await Assert.That(result[3]).IsEquivalentTo([1, 5]);
41+
await Assert.That(result[4]).IsEquivalentTo([2, 3]);
42+
await Assert.That(result[5]).IsEquivalentTo([2, 4]);
43+
await Assert.That(result[6]).IsEquivalentTo([2, 5]);
44+
await Assert.That(result[7]).IsEquivalentTo([3, 4]);
45+
await Assert.That(result[8]).IsEquivalentTo([3, 5]);
46+
await Assert.That(result[9]).IsEquivalentTo([4, 5]);
4247
}
4348

44-
[TestMethod]
45-
public void Combinations_LengthThree_ReturnsAllThreeElementCombinations()
49+
[Test]
50+
public async Task Combinations_LengthThree_ReturnsAllThreeElementCombinations()
4651
{
4752
var result = _defaultSource.Combinations(3).ToList();
48-
Assert.HasCount(10, result);
49-
Assert.IsTrue(result[0] is [1, 2, 3]);
50-
Assert.IsTrue(result[1] is [1, 2, 4]);
51-
Assert.IsTrue(result[2] is [1, 2, 5]);
52-
Assert.IsTrue(result[3] is [1, 3, 4]);
53-
Assert.IsTrue(result[4] is [1, 3, 5]);
54-
Assert.IsTrue(result[5] is [1, 4, 5]);
55-
Assert.IsTrue(result[6] is [2, 3, 4]);
56-
Assert.IsTrue(result[7] is [2, 3, 5]);
57-
Assert.IsTrue(result[8] is [2, 4, 5]);
58-
Assert.IsTrue(result[9] is [3, 4, 5]);
53+
54+
using var _ = Assert.Multiple();
55+
await Assert.That(result).Count().IsEqualTo(10);
56+
await Assert.That(result[0]).IsEquivalentTo([1, 2, 3]);
57+
await Assert.That(result[1]).IsEquivalentTo([1, 2, 4]);
58+
await Assert.That(result[2]).IsEquivalentTo([1, 2, 5]);
59+
await Assert.That(result[3]).IsEquivalentTo([1, 3, 4]);
60+
await Assert.That(result[4]).IsEquivalentTo([1, 3, 5]);
61+
await Assert.That(result[5]).IsEquivalentTo([1, 4, 5]);
62+
await Assert.That(result[6]).IsEquivalentTo([2, 3, 4]);
63+
await Assert.That(result[7]).IsEquivalentTo([2, 3, 5]);
64+
await Assert.That(result[8]).IsEquivalentTo([2, 4, 5]);
65+
await Assert.That(result[9]).IsEquivalentTo([3, 4, 5]);
5966
}
6067

61-
[TestMethod]
62-
public void Combinations_LengthGreaterThanSource_ReturnsEmpty()
68+
[Test]
69+
public async Task Combinations_LengthGreaterThanSource_ReturnsEmpty()
6370
{
6471
var result = _defaultSource.Combinations(6).ToList();
65-
Assert.IsEmpty(result);
72+
await Assert.That(result).IsEmpty();
6673
}
6774

68-
[TestMethod]
75+
[Test]
6976
public void Combinations_NegativeLength_ThrowsArgumentOutOfRangeException()
7077
{
7178
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => _defaultSource.Combinations(-1));

DotNetExtensionsTests/Collections/DictionaryExtensionsTests.cs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,61 @@
22

33
namespace DotNetExtensionsTests.Collections;
44

5-
[TestClass]
65
public class DictionaryExtensionsTests
76
{
8-
[TestMethod]
9-
public void GetOrAdd_NewKey_AddsAndReturnsValue()
7+
[Test]
8+
public async Task GetOrAdd_NewKey_AddsAndReturnsValue()
109
{
1110
// Arrange
12-
var dictionary = new Dictionary<string, int>();
11+
Dictionary<string, int> dictionary = [];
1312
const string key = "newKey";
1413
const int valueToAdd = 42;
1514

1615
// Act
1716
var result = dictionary.GetOrAdd(key, valueToAdd);
1817

1918
// Assert
20-
Assert.IsTrue(dictionary.ContainsKey(key), "The key should be added to the dictionary.");
21-
Assert.AreEqual(valueToAdd, dictionary[key], "The value associated with the key should be the value provided.");
22-
Assert.AreEqual(valueToAdd, result, "The returned value should match the value added to the dictionary.");
19+
using var _ = Assert.Multiple();
20+
await Assert.That(dictionary).ContainsKeyWithValue(key, valueToAdd);
21+
await Assert.That(result).IsEqualTo(valueToAdd);
2322
}
2423

25-
[TestMethod]
26-
public void GetOrAdd_ExistingKey_ReturnsExistingValue()
24+
[Test]
25+
public async Task GetOrAdd_ExistingKey_ReturnsExistingValue()
2726
{
2827
// Arrange
29-
var dictionary = new Dictionary<string, int> { { "existingKey", 100 } };
3028
const string key = "existingKey";
31-
const int valueToAdd = 42; // Should not be used.
29+
const int value = 100;
30+
var dictionary = new Dictionary<string, int> { { key, value } };
3231

3332
// Act
34-
var result = dictionary.GetOrAdd(key, valueToAdd);
33+
var result = dictionary.GetOrAdd(key, 42);
3534

3635
// Assert
37-
Assert.AreEqual(100, dictionary[key], "The value in the dictionary should remain unchanged.");
38-
Assert.AreEqual(100, result, "The returned value should be the value already in the dictionary.");
36+
using var _ = Assert.Multiple();
37+
await Assert.That(dictionary).ContainsKeyWithValue(key, value);
38+
await Assert.That(result).IsEqualTo(value);
3939
}
4040

41-
[TestMethod]
42-
public void TryUpdate_ExistingKey_UpdatesValueAndReturnsTrue()
41+
[Test]
42+
public async Task TryUpdate_ExistingKey_UpdatesValueAndReturnsTrue()
4343
{
4444
// Arrange
45-
var dictionary = new Dictionary<string, int> { { "existingKey", 100 } };
4645
const string key = "existingKey";
4746
const int newValue = 200;
47+
var dictionary = new Dictionary<string, int> { { key, 100 } };
4848

4949
// Act
5050
var result = dictionary.TryUpdate(key, newValue);
5151

5252
// Assert
53-
Assert.IsTrue(result, "The method should return true for an existing key.");
54-
Assert.AreEqual(newValue, dictionary[key], "The value in the dictionary should be updated to the new value.");
53+
using var _ = Assert.Multiple();
54+
await Assert.That(dictionary).ContainsKeyWithValue(key, newValue);
55+
await Assert.That(result).IsTrue();
5556
}
5657

57-
[TestMethod]
58-
public void TryUpdate_NonExistingKey_ReturnsFalseAndDoesNotAdd()
58+
[Test]
59+
public async Task TryUpdate_NonExistingKey_ReturnsFalseAndDoesNotAdd()
5960
{
6061
// Arrange
6162
var dictionary = new Dictionary<string, int>();
@@ -66,7 +67,7 @@ public void TryUpdate_NonExistingKey_ReturnsFalseAndDoesNotAdd()
6667
var result = dictionary.TryUpdate(key, value);
6768

6869
// Assert
69-
Assert.IsFalse(result, "The method should return false for a non-existing key.");
70-
Assert.IsFalse(dictionary.ContainsKey(key), "The dictionary should remain unchanged for a non-existing key.");
70+
await Assert.That(dictionary).DoesNotContainKey(key);
71+
await Assert.That(result).IsFalse();
7172
}
7273
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
namespace DotNetExtensionsTests.Collections;
22

3-
[TestClass]
43
public partial class EnumerableExtensionsTests
54
{
6-
private int[] _defaultSource = null!;
5+
private static int[] _defaultSource = null!;
76

8-
[TestInitialize]
9-
public void Setup() => _defaultSource = [1, 2, 3, 4, 5];
7+
[Before(Class)]
8+
public static void Setup() => _defaultSource = [1, 2, 3, 4, 5];
109
}

0 commit comments

Comments
 (0)