Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit ed1f5c2

Browse files
committed
Fix equality comparisons
1 parent ba5c79c commit ed1f5c2

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/GitHub.App/Models/PullRequestModel.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
namespace GitHub.Models
1010
{
1111
[DebuggerDisplay("{DebuggerDisplay,nq}")]
12-
public sealed class PullRequestModel : NotificationAwareObject, IPullRequestModel
12+
public sealed class PullRequestModel : NotificationAwareObject, IPullRequestModel,
13+
IEquatable<PullRequestModel>,
14+
IComparable<PullRequestModel>
1315
{
1416
public PullRequestModel(int number, string title,
1517
IAccount author, [AllowNull]IAccount assignee,
@@ -45,7 +47,7 @@ public override bool Equals([AllowNull]object obj)
4547

4648
public override int GetHashCode()
4749
{
48-
return Number;
50+
return Number.GetHashCode();
4951
}
5052

5153
bool IEquatable<IPullRequestModel>.Equals([AllowNull]IPullRequestModel other)
@@ -55,11 +57,23 @@ bool IEquatable<IPullRequestModel>.Equals([AllowNull]IPullRequestModel other)
5557
return other != null && Number == other.Number;
5658
}
5759

60+
bool IEquatable<PullRequestModel>.Equals([AllowNull]PullRequestModel other)
61+
{
62+
if (ReferenceEquals(this, other))
63+
return true;
64+
return other != null && Number == other.Number;
65+
}
66+
5867
public int CompareTo([AllowNull]IPullRequestModel other)
5968
{
6069
return other != null ? UpdatedAt.CompareTo(other.UpdatedAt) : 1;
6170
}
6271

72+
public int CompareTo([AllowNull]PullRequestModel other)
73+
{
74+
return other != null ? UpdatedAt.CompareTo(other.UpdatedAt) : 1;
75+
}
76+
6377
public static bool operator >([AllowNull]PullRequestModel lhs, [AllowNull]PullRequestModel rhs)
6478
{
6579
if (ReferenceEquals(lhs, rhs))
@@ -76,7 +90,7 @@ public int CompareTo([AllowNull]IPullRequestModel other)
7690

7791
public static bool operator ==([AllowNull]PullRequestModel lhs, [AllowNull]PullRequestModel rhs)
7892
{
79-
return Equals(lhs, rhs) && ((object)lhs == null || lhs.CompareTo(rhs) == 0);
93+
return ReferenceEquals(lhs, rhs);
8094
}
8195

8296
public static bool operator !=([AllowNull]PullRequestModel lhs, [AllowNull]PullRequestModel rhs)

src/UnitTests/GitHub.App/Models/PullRequestModelTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public void ComparisonEquals()
8080
{
8181
PullRequestModel left = new PullRequestModel(1, "", Substitute.For<IAccount>(), Substitute.For<IAccount>(), Now, Now + TimeSpan.FromMilliseconds(1));
8282
PullRequestModel right = new PullRequestModel(1, "", Substitute.For<IAccount>(), Substitute.For<IAccount>(), Now, Now + TimeSpan.FromMilliseconds(1));
83-
Assert.True(left == right);
84-
Assert.False(left != right);
83+
Assert.False(left == right);
84+
Assert.True(left != right);
8585
Assert.False(left > right);
8686
Assert.False(left < right);
8787
}

0 commit comments

Comments
 (0)