Skip to content

Commit feed150

Browse files
committed
Make Signature IEquatable
1 parent 8197210 commit feed150

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

LibGit2Sharp.Tests/TagFixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class TagFixture : BaseFixture
1111
{
1212
private readonly string[] expectedTags = new[] { "e90810b", "lw", "point_to_blob", "test", };
1313

14-
private static readonly Signature signatureTim = new Signature("Tim Clem", "[email protected]", DateTimeOffset.UtcNow);
14+
private static readonly Signature signatureTim = new Signature("Tim Clem", "[email protected]", TruncateSubSeconds(DateTimeOffset.UtcNow));
1515
private static readonly Signature signatureNtk = new Signature("nulltoken", "[email protected]", Epoch.ToDateTimeOffset(1300557894, 60));
1616
private const string tagTestSha = "b25fa35b38051e4ae45d4222e795f9df2e43f1d1";
1717
private const string commitE90810BSha = "e90810b8df3e80c413d903f631643c716887138d";
@@ -151,6 +151,7 @@ public void CanAddAnAnnotatedTagFromSha()
151151
Assert.NotNull(newTag);
152152
Assert.True(newTag.IsAnnotated);
153153
Assert.Equal(tagTestSha, newTag.Target.Sha);
154+
Assert.Equal(signatureTim, newTag.Annotation.Tagger);
154155
}
155156
}
156157

LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Text.RegularExpressions;
5+
using LibGit2Sharp.Core;
56
using Xunit;
67

78
namespace LibGit2Sharp.Tests.TestHelpers
@@ -21,7 +22,13 @@ static BaseFixture()
2122
public static string StandardTestRepoPath { get; private set; }
2223
public static DirectoryInfo ResourcesDirectory { get; private set; }
2324

24-
public static readonly Signature DummySignature = new Signature("Author N. Ame", "[email protected]", DateTimeOffset.Now);
25+
public static readonly Signature DummySignature = new Signature("Author N. Ame", "[email protected]", TruncateSubSeconds(DateTimeOffset.Now));
26+
27+
protected static DateTimeOffset TruncateSubSeconds(DateTimeOffset dto)
28+
{
29+
int seconds = dto.ToSecondsSinceEpoch();
30+
return Epoch.ToDateTimeOffset(seconds, (int) dto.Offset.TotalMinutes);
31+
}
2532

2633
private static void SetUpTestEnvironment()
2734
{

LibGit2Sharp/Signature.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ namespace LibGit2Sharp
88
/// <summary>
99
/// A signature
1010
/// </summary>
11-
public class Signature
11+
public class Signature : IEquatable<Signature>
1212
{
1313
private readonly DateTimeOffset when;
1414
private readonly string name;
1515
private readonly string email;
1616

17+
private static readonly LambdaEqualityHelper<Signature> equalityHelper =
18+
new LambdaEqualityHelper<Signature>(x => x.Name, x => x.Email, x => x.When);
19+
1720
internal Signature(IntPtr signaturePtr)
1821
{
1922
var handle = new GitSignature();
@@ -65,5 +68,56 @@ public DateTimeOffset When
6568
{
6669
get { return when; }
6770
}
71+
72+
/// <summary>
73+
/// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "Signature" />.
74+
/// </summary>
75+
/// <param name = "obj">The <see cref = "Object" /> to compare with the current <see cref = "Signature" />.</param>
76+
/// <returns>True if the specified <see cref = "Object" /> is equal to the current <see cref = "Signature" />; otherwise, false.</returns>
77+
public override bool Equals(object obj)
78+
{
79+
return Equals(obj as Signature);
80+
}
81+
82+
/// <summary>
83+
/// Determines whether the specified <see cref = "Signature" /> is equal to the current <see cref = "Signature" />.
84+
/// </summary>
85+
/// <param name = "other">The <see cref = "Signature" /> to compare with the current <see cref = "Signature" />.</param>
86+
/// <returns>True if the specified <see cref = "Signature" /> is equal to the current <see cref = "Signature" />; otherwise, false.</returns>
87+
public bool Equals(Signature other)
88+
{
89+
return equalityHelper.Equals(this, other);
90+
}
91+
92+
/// <summary>
93+
/// Returns the hash code for this instance.
94+
/// </summary>
95+
/// <returns>A 32-bit signed integer hash code.</returns>
96+
public override int GetHashCode()
97+
{
98+
return equalityHelper.GetHashCode(this);
99+
}
100+
101+
/// <summary>
102+
/// Tests if two <see cref = "Signature" /> are equal.
103+
/// </summary>
104+
/// <param name = "left">First <see cref = "Signature" /> to compare.</param>
105+
/// <param name = "right">Second <see cref = "Signature" /> to compare.</param>
106+
/// <returns>True if the two objects are equal; false otherwise.</returns>
107+
public static bool operator ==(Signature left, Signature right)
108+
{
109+
return Equals(left, right);
110+
}
111+
112+
/// <summary>
113+
/// Tests if two <see cref = "Signature" /> are different.
114+
/// </summary>
115+
/// <param name = "left">First <see cref = "Signature" /> to compare.</param>
116+
/// <param name = "right">Second <see cref = "Signature" /> to compare.</param>
117+
/// <returns>True if the two objects are different; false otherwise.</returns>
118+
public static bool operator !=(Signature left, Signature right)
119+
{
120+
return !Equals(left, right);
121+
}
68122
}
69123
}

0 commit comments

Comments
 (0)