Skip to content

Commit a71aa66

Browse files
committed
Simplify introduced GetHashCode and Equals
1 parent 94e529e commit a71aa66

File tree

3 files changed

+11
-54
lines changed

3 files changed

+11
-54
lines changed

Rubberduck.Core/UI/UnitTesting/ViewModels/TestMethodViewModel.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@ public TestResult Result
2626

2727
// Delegate Navigability to encapsulated TestMethod
2828
public NavigateCodeEventArgs GetNavigationArgs() => Method.GetNavigationArgs();
29-
3029

31-
public override string ToString()
32-
{
33-
return $"{Method.Declaration.QualifiedName}: {Result.Outcome} ({Result.Duration}ms) {Result.Output}";
34-
}
30+
public override string ToString() => $"{Method.Declaration.QualifiedName}: {Result.Outcome} ({Result.Duration}ms) {Result.Output}";
31+
public override bool Equals(object obj) => obj is TestMethodViewModel other && Method.Equals(other.Method);
32+
public override int GetHashCode() => Method.GetHashCode();
3533

3634
public object[] ToArray()
3735
{
@@ -45,17 +43,5 @@ public object[] ToArray()
4543
_result.Duration
4644
};
4745
}
48-
49-
public override bool Equals(object obj)
50-
{
51-
var model = obj as TestMethodViewModel;
52-
return model != null &&
53-
EqualityComparer<TestMethod>.Default.Equals(Method, model.Method);
54-
}
55-
56-
public override int GetHashCode()
57-
{
58-
return 1003453392 + EqualityComparer<TestMethod>.Default.GetHashCode(Method);
59-
}
6046
}
6147
}

Rubberduck.UnitTesting/UnitTesting/TestMethod.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Diagnostics.CodeAnalysis;
43
using System.Linq;
54
using Rubberduck.Parsing;
65
using Rubberduck.Parsing.Annotations;
76
using Rubberduck.Parsing.Symbols;
8-
using Rubberduck.Parsing.VBA;
97
using Rubberduck.VBEditor;
10-
using Rubberduck.VBEditor.ComManagement.TypeLibsAPI;
118
using Rubberduck.Interaction.Navigation;
129

1310
namespace Rubberduck.UnitTesting
@@ -37,19 +34,8 @@ public NavigateCodeEventArgs GetNavigationArgs()
3734
return new NavigateCodeEventArgs(new QualifiedSelection(Declaration.QualifiedName.QualifiedModuleName, Declaration.Context.GetSelection()));
3835
}
3936

40-
public bool Equals(TestMethod other)
41-
{
42-
return other != null && Declaration.QualifiedName.Equals(other.Declaration.QualifiedName);
43-
}
44-
45-
public override bool Equals(object obj)
46-
{
47-
return obj is TestMethod method && method.Declaration.QualifiedName.Equals(Declaration.QualifiedName);
48-
}
49-
50-
public override int GetHashCode()
51-
{
52-
return Declaration.QualifiedName.GetHashCode();
53-
}
37+
public bool Equals(TestMethod other) => other != null && Declaration.QualifiedName.Equals(other.Declaration.QualifiedName);
38+
public override bool Equals(object obj) => obj is TestMethod method && Equals(method);
39+
public override int GetHashCode() => Declaration.QualifiedName.GetHashCode();
5440
}
5541
}
Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using Rubberduck.VBEditor;
32

43
namespace Rubberduck.UnitTesting
54
{
@@ -16,27 +15,13 @@ public TestResult(TestOutcome outcome, string output = "", long duration = 0)
1615
public TestOutcome Outcome { get; private set; }
1716
public string Output { get; private set; }
1817

18+
public override int GetHashCode() => HashCode.Compute(Outcome, Output);
19+
public override string ToString() => $"{Outcome} ({Duration} ms) {Output}";
1920
public override bool Equals(object obj)
2021
{
21-
if (obj is TestResult other)
22-
{
23-
return Outcome == other.Outcome
22+
return obj is TestResult other
23+
&& Outcome == other.Outcome
2424
&& Output == other.Output;
25-
}
26-
return false;
27-
}
28-
29-
public override int GetHashCode()
30-
{
31-
var hashCode = -1268493841;
32-
hashCode = hashCode * -1521134295 + Outcome.GetHashCode();
33-
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Output);
34-
return hashCode;
35-
}
36-
37-
public override string ToString()
38-
{
39-
return $"{Outcome} ({Duration} ms) {Output}";
4025
}
4126
}
4227
}

0 commit comments

Comments
 (0)