Skip to content

Commit

Permalink
Added equality tests for commits and edges
Browse files Browse the repository at this point in the history
  • Loading branch information
haacked committed Mar 15, 2012
1 parent 846590c commit 02c044e
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 5 deletions.
18 changes: 15 additions & 3 deletions SeeGitApp/Models/CommitEdge.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using QuickGraph;

namespace SeeGit
{
[DebuggerDisplay("{Source.Sha}..{Target.Sha}")]
public class CommitEdge : Edge<CommitVertex>
public class CommitEdge : Edge<CommitVertex>, IEquatable<CommitEdge>
{
private string _id;

Expand Down Expand Up @@ -45,7 +46,7 @@ public bool Equals(CommitEdge other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other.Source, other.Target);
return Source == other.Source && Target == other.Target;
}

public override int GetHashCode()
Expand All @@ -57,5 +58,16 @@ public override int GetHashCode()
return result;
}
}

public static bool operator ==(CommitEdge edge, CommitEdge other)
{
if (ReferenceEquals(edge, null)) return false;
return edge.Equals(other);
}

public static bool operator !=(CommitEdge edge, CommitEdge other)
{
return !(edge == other);
}
}
}
16 changes: 14 additions & 2 deletions SeeGitApp/Models/CommitVertex.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using SeeGit.Models;

namespace SeeGit
{
[DebuggerDisplay("{Sha}: {Message}")]
public class CommitVertex : GitObject<CommitVertex>
public class CommitVertex : GitObject<CommitVertex>, IEquatable<CommitVertex>
{
public CommitVertex(string sha, string message)
{
Expand Down Expand Up @@ -53,5 +54,16 @@ public override int GetHashCode()
return result;
}
}

public static bool operator ==(CommitVertex commit, CommitVertex other)
{
if (ReferenceEquals(commit, null)) return false;
return commit.Equals(other);
}

public static bool operator !=(CommitVertex commit, CommitVertex other)
{
return !(commit == other);
}
}
}
78 changes: 78 additions & 0 deletions UnitTests/Models/CommitEdgeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using SeeGit;
using Xunit;

namespace UnitTests.Models
{
public class CommitEdgeTests
{
public class TheEqualsMethod
{
[Fact]
public void ReturnsTrueIfSourceAndTargetAreSame()
{
var edge = new CommitEdge(new CommitVertex("sha-child", "child commit"),
new CommitVertex("sha-parent", "parent commit"));
var other = new CommitEdge(new CommitVertex("sha-child", "another child commit"),
new CommitVertex("sha-parent", "another parent commit"));

Assert.True(edge.Equals(other));
}

[Fact]
public void ReturnsFalseIfSourceAndTargetAreSame()
{
var edge = new CommitEdge(new CommitVertex("sha1-child", "child commit"),
new CommitVertex("sha-parent", "parent commit"));
var other = new CommitEdge(new CommitVertex("sha-child", "another child commit"),
new CommitVertex("sha-parent", "another parent commit"));

Assert.False(edge.Equals(other));
}

[Fact]
public void ReturnsFalseWhenComparedToNull()
{
var edge = new CommitEdge(new CommitVertex("sha1-child", "child commit"),
new CommitVertex("sha-parent", "parent commit"));

Assert.False(edge.Equals(null));
}
}

public class TheEqualityOperator
{
[Fact]
public void ReturnsTrueIfShaAreSame()
{
var edge = new CommitEdge(new CommitVertex("sha-child", "child commit"),
new CommitVertex("sha-parent", "parent commit"));
var other = new CommitEdge(new CommitVertex("sha-child", "another child commit"),
new CommitVertex("sha-parent", "another parent commit"));

Assert.True(edge == other);
}

[Fact]
public void ReturnsFalseIfEitherShaIsDifferent()
{
var edge = new CommitEdge(new CommitVertex("sha-child", "child commit"),
new CommitVertex("sha-parent", "parent commit"));
var other = new CommitEdge(new CommitVertex("sha1-child", "another child commit"),
new CommitVertex("sha-parent", "another parent commit"));

Assert.False(edge == other);
Assert.True(edge != other);
}

[Fact]
public void ReturnsFalseWhenComparedToNull()
{
var edge = new CommitEdge(new CommitVertex("sha-child", "child commit"),
new CommitVertex("sha-parent", "parent commit"));

Assert.False(edge == null);
Assert.True(edge != null);
}
}
}
}
77 changes: 77 additions & 0 deletions UnitTests/Models/CommitVertexTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using SeeGit;
using Xunit;

namespace UnitTests.Models
{
public class CommitVertexTests
{
public class TheCtor
{
[Fact]
public void SetsProperties()
{
var commit = new CommitVertex("shasha", "commit message");

Assert.Equal("shasha", commit.Sha);
Assert.Equal("commit message", commit.Message);
Assert.NotNull(commit.Branches);
}
}

public class TheEqualsMethod
{
[Fact]
public void ReturnsTrueIfShasAreSame()
{
var commit = new CommitVertex("sha", "whatever");
var other = new CommitVertex("sha", "doesn't matter");

Assert.True(commit.Equals(other));
}

[Fact]
public void ReturnsFalseIfShasAreDifferent()
{
var commit = new CommitVertex("sha1", "whatever");
var other = new CommitVertex("sha2", "doesn't matter");

Assert.False(commit.Equals(other));
}

[Fact]
public void ReturnsFalseWhenComparedToNull()
{
Assert.False(new CommitVertex("sha", "message").Equals(null));
}
}

public class TheEqualityOperator
{
[Fact]
public void ReturnsTrueIfShasAreSame()
{
var commit = new CommitVertex("sha", "whatever");
var other = new CommitVertex("sha", "doesn't matter");

Assert.True(commit == other);
}

[Fact]
public void ReturnsFalseIfShasAreDifferent()
{
var commit = new CommitVertex("sha", "whatever");
var other = new CommitVertex("sha1", "doesn't matter");

Assert.False(commit == other);
Assert.True(commit != other);
}

[Fact]
public void ReturnsFalseWhenComparedToNull()
{
Assert.False(new CommitVertex("sha", "message") == null);
Assert.True(new CommitVertex("sha", "message") != null);
}
}
}
}
11 changes: 11 additions & 0 deletions UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,18 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="GraphSharp, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\Lib\GraphSharp.dll</HintPath>
</Reference>
<Reference Include="GraphSharp.Controls, Version=1.0.4115.29430, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\Lib\GraphSharp.Controls.dll</HintPath>
</Reference>
<Reference Include="Moq">
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
</Reference>
<Reference Include="QuickGraph, Version=3.2.40122.0, Culture=neutral, PublicKeyToken=f3fb40175eec2af3, processorArchitecture=MSIL">
<HintPath>..\Lib\QuickGraph.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -47,6 +56,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Models\BranchCollectionTests.cs" />
<Compile Include="Models\CommitEdgeTests.cs" />
<Compile Include="Models\CommitVertexTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit 02c044e

Please sign in to comment.