Skip to content

Commit 5654f8a

Browse files
Fixes tests to complete 100% code coverage for CommonUtiliriesCSharp.UnitTests
1 parent e900c61 commit 5654f8a

3 files changed

Lines changed: 29 additions & 42 deletions

File tree

CommonUtilitiesCSharp.UnitTests/Core/CommandTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ public void Constructor_Throws_ForNullExecute()
1818
[Test]
1919
public void Constructor_Throws_ForNullCanExecute()
2020
{
21-
static void execute(TCommandParameter? param) { }
21+
static void execute(TCommandParameter? param) { throw new NotSupportedException(); }
2222

23+
Assert.Throws<NotSupportedException>(() => execute(default));
2324
Assert.Throws<ArgumentNullException>(() => CreateCommand(execute, null!));
2425
}
2526

@@ -28,6 +29,7 @@ public void Constructor_Throws_ForNullExecuteWithPredicate()
2829
{
2930
static bool canExecute(TCommandParameter? param) { throw new NotSupportedException(); }
3031

32+
Assert.Throws<NotSupportedException>(() => canExecute(default));
3133
Assert.Throws<ArgumentNullException>(() => CreateCommand(null!, canExecute));
3234
}
3335
#endregion Constructor
@@ -160,6 +162,7 @@ public override void Execute_IsInvokedWithParameter_WhenParameterIsPassedAndPred
160162
#endregion Execute
161163

162164
#region CanExecute
165+
[Test]
163166
public override void CanExecute_ReturnsTrue_WhenThereIsNoPredicate()
164167
{
165168
var command = CreateCommand(parameter => { });
@@ -326,6 +329,7 @@ public override void Execute_IsInvokedWithParameter_WhenParameterIsPassedAndPred
326329
#endregion Execute
327330

328331
#region CanExecute
332+
[Test]
329333
public override void CanExecute_ReturnsTrue_WhenThereIsNoPredicate()
330334
{
331335
var command = CreateCommand(parameter => { });

CommonUtilitiesCSharp.UnitTests/Core/ObservableObjectTests.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ public void OnPropertyChanged_CanBeOverriden()
5151
Assert.That(mockObject.HasPropertyChangedBeenCalled, Is.False);
5252

5353
mockObject.Value = "test2";
54-
55-
Assert.That(mockObject.HasPropertyChangedBeenCalled, Is.True);
54+
55+
Assert.Multiple(() =>
56+
{
57+
Assert.That(mockObject.Value, Is.EqualTo("test2"));
58+
Assert.That(mockObject.HasPropertyChangedBeenCalled, Is.True);
59+
});
5660
}
5761

5862
[Test]
@@ -94,6 +98,7 @@ public void OnPropertyChanged_RaisesPropertyChangedEvent_WhenInvokedWithName()
9498

9599
Assert.Multiple(() =>
96100
{
101+
Assert.That(mockObject.IndirectValue, Is.EqualTo("test2"));
97102
Assert.That(propertiesChanged, Has.Count.EqualTo(2));
98103
Assert.That(propertiesChanged[0], Is.EqualTo(nameof(MockObservableObject.IndirectValue)));
99104
Assert.That(propertiesChanged[1], Is.EqualTo(nameof(MockObservableObject.Value)));

CommonUtilitiesCSharp.UnitTests/Extensions/IEnumerableExtensionTests.cs

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,56 @@ namespace CommonUtilitiesCSharp.UnitTests.Extensions
44
{
55
public class IEnumerableExtensionTests
66
{
7-
private class MockObject
8-
{
9-
public MockObject(string value)
10-
{
11-
Value = value;
12-
}
13-
14-
public string? Value { get; set; }
15-
16-
public override bool Equals(object? obj)
17-
{
18-
return obj is MockObject @object && Equals(@object);
19-
}
20-
21-
public bool Equals(MockObject? obj)
22-
{
23-
return obj != null &&
24-
Value == obj.Value;
25-
}
26-
27-
public override int GetHashCode()
28-
{
29-
return nameof(MockObject).GetHashCode() * ((Value?.GetHashCode() ?? -1) + 7);
30-
}
31-
}
32-
337
#region ForEach
348
[Test]
359
public void ForEach_ExecutesActionOnAllItems_ForMultipleItems()
3610
{
37-
var items = new MockObject[] { new("a"), new("b"), new("c") };
11+
var items = new int[] { 1, 2, 3 };
12+
var result = new List<int>();
3813

39-
items.ForEach(item => item.Value += ".");
14+
items.ForEach(item => result.Add(item + 1));
4015

41-
Assert.That(items, Is.EquivalentTo(new MockObject[] { new("a."), new("b."), new("c.") }));
16+
Assert.That(result, Is.EquivalentTo(new int[] { 2, 3, 4 }));
4217
}
4318

4419
[Test]
4520
public void ForEach_ExecutesActionOnAllItems_ForSingleItem()
4621
{
47-
var items = new MockObject[] { new("a") };
22+
var items = new int[] { 1 };
23+
var result = new List<int>();
4824

49-
items.ForEach(item => item.Value += ".");
25+
items.ForEach(item => result.Add(item + 1));
5026

51-
Assert.That(items, Is.EquivalentTo(new MockObject[] { new("a.") }));
27+
Assert.That(result, Is.EquivalentTo(new int[] { 2 }));
5228
}
5329

5430
[Test]
5531
public void ForEach_ExecutesActionOnAllItems_ForZeroItems()
5632
{
57-
var items = Array.Empty<MockObject>();
33+
var items = Array.Empty<int>();
34+
var result = new List<int>();
5835

59-
items.ForEach(item => item.Value += ".");
36+
items.ForEach(item => result.Add(item + 1));
6037

61-
Assert.That(items, Is.EquivalentTo(Array.Empty<MockObject>()));
38+
Assert.That(result, Is.EquivalentTo(Array.Empty<int>()));
6239
}
6340

6441
[Test]
6542
public void ForEach_ThrowsArgumentNullException_ForNullEnumerable()
6643
{
67-
IEnumerable<string> items = null!;
44+
IEnumerable<int> items = null!;
45+
var result = new List<int>();
6846

69-
Assert.Throws<ArgumentNullException>(() => items.ForEach(item => item += "."));
47+
Assert.Throws<ArgumentNullException>(() => items.ForEach(item => result.Add(item + 1)));
7048
}
7149

7250
[Test]
7351
public void ForEach_ThrowsArgumentNullException_ForNullAction()
7452
{
75-
var items = new string[] { "a", "b", "c" };
53+
var items = new int[] { 1, 2, 3 };
7654

7755
Assert.Throws<ArgumentNullException>(() => items.ForEach(null!));
7856
}
7957
#endregion ForEach
8058
}
81-
}
59+
}

0 commit comments

Comments
 (0)