Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add basic Tuple<T1, T2> type
  • Loading branch information
nulltoken committed Nov 28, 2011
1 parent d526b18 commit 1032a14
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 4 deletions.
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/LazyFixture.cs
@@ -1,5 +1,5 @@
using System;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;
using LibGit2Sharp.Tests.TestHelpers;
using NUnit.Framework;

Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
Expand Up @@ -64,6 +64,7 @@
<Compile Include="TestHelpers\SignatureExtensions.cs" />
<Compile Include="TestHelpers\TemporaryCloneOfTestRepo.cs" />
<Compile Include="TreeFixture.cs" />
<Compile Include="TupleFixture.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LibGit2Sharp\LibGit2Sharp.csproj">
Expand Down
56 changes: 56 additions & 0 deletions LibGit2Sharp.Tests/TupleFixture.cs
@@ -0,0 +1,56 @@
using LibGit2Sharp.Core.Compat;
using LibGit2Sharp.Tests.TestHelpers;
using NUnit.Framework;

namespace LibGit2Sharp.Tests
{
[TestFixture]
public class TupleFixture
{
const int integer = 2;
const string stringy = "hello";

private readonly Tuple<int, string> sut = new Tuple<int, string>(integer, stringy);

[Test]
public void Properties()
{
sut.Item1.ShouldEqual(integer);
sut.Item2.ShouldEqual(stringy);
}

[Test]
public void GetHashCodeIsTheSame()
{
var sut2 = new Tuple<int, string>(integer, stringy);

sut.GetHashCode().ShouldEqual(sut2.GetHashCode());
}

[Test]
public void GetHashCodeIsDifferent()
{
var sut2 = new Tuple<int, string>(integer + 1, stringy);

sut.GetHashCode().ShouldNotEqual(sut2.GetHashCode());
}

[Test]
public void Equals()
{
var sut2 = new Tuple<int, string>(integer, stringy);

sut.Equals(sut2).ShouldBeTrue();
Equals(sut, sut2).ShouldBeTrue();
}

[Test]
public void NotEquals()
{
var sut2 = new Tuple<int, string>(integer + 1, stringy);

sut.Equals(sut2).ShouldBeFalse();
Equals(sut, sut2).ShouldBeFalse();
}
}
}
1 change: 1 addition & 0 deletions LibGit2Sharp/Branch.cs
Expand Up @@ -2,6 +2,7 @@
using System.Globalization;
using System.Linq;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;

namespace LibGit2Sharp
{
Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp/Commit.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;

namespace LibGit2Sharp
{
Expand Down
@@ -1,6 +1,6 @@
using System;

namespace LibGit2Sharp.Core
namespace LibGit2Sharp.Core.Compat
{
/// <summary>
/// Provides support for lazy initialization.
Expand Down
65 changes: 65 additions & 0 deletions LibGit2Sharp/Core/Compat/Tuple.cs
@@ -0,0 +1,65 @@
using System.Collections.Generic;

namespace LibGit2Sharp.Core.Compat
{
/// <summary>
/// Represents a 2-tuple, or pair.
/// </summary>
/// <typeparam name = "T1">The type of the tuple's first component.</typeparam>
/// <typeparam name = "T2">The type of the tuple's second component.</typeparam>
public class Tuple<T1, T2>
{
private readonly KeyValuePair<T1, T2> kvp;

/// <summary>
/// Initializes a new instance of the <see cref="Tuple{T1,T2}"/> class.
/// </summary>
/// <param name="item1">The value of the tuple's first component.</param>
/// <param name="item2">The value of the tuple's second component.</param>
public Tuple(T1 item1, T2 item2)
{
kvp = new KeyValuePair<T1, T2>(item1, item2);
}

/// <summary>
/// Gets the value of the current <see cref = "Tuple{T1,T2}" /> object's second component.
/// </summary>
public T2 Item2
{
get { return kvp.Value; }
}

/// <summary>
/// Gets the value of the current <see cref = "Tuple{T1,T2}" /> object's first component.
/// </summary>
public T1 Item1
{
get { return kvp.Key; }
}

/// <summary>
/// Returns the hash code for the current <see cref = "Tuple{T1,T2}" /> object.
/// </summary>
/// <returns>A 32-bit signed integer hash code.</returns>
public override int GetHashCode()
{
return kvp.GetHashCode();
}

/// <summary>
/// Returns a value that indicates whether the current <see cref = "Tuple{T1,T2}" /> object is equal to a specified object.
/// </summary>
/// <param name = "obj">The object to compare with this instance.</param>
/// <returns>true if the current instance is equal to the specified object; otherwise, false.</returns>
public override bool Equals(object obj)
{
if (!(obj is Tuple<T1, T2>))
{
return false;
}
return kvp.Equals(((Tuple<T1, T2>)obj).kvp);
}


}
}
1 change: 1 addition & 0 deletions LibGit2Sharp/DirectReference.cs
@@ -1,4 +1,5 @@
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;

namespace LibGit2Sharp
{
Expand Down
5 changes: 3 additions & 2 deletions LibGit2Sharp/LibGit2Sharp.csproj
Expand Up @@ -52,6 +52,7 @@
<Compile Include="CommitCollection.cs" />
<Compile Include="Configuration.cs" />
<Compile Include="ConfigurationLevel.cs" />
<Compile Include="Core\Compat\Tuple.cs" />
<Compile Include="Core\EnumExtensions.cs" />
<Compile Include="DetachedHead.cs" />
<Compile Include="LibGit2Exception.cs" />
Expand All @@ -67,7 +68,7 @@
<Compile Include="FileStatus.cs" />
<Compile Include="Core\GitTime.cs" />
<Compile Include="Core\IntPtrExtensions.cs" />
<Compile Include="Core\Lazy.cs" />
<Compile Include="Core\Compat\Lazy.cs" />
<Compile Include="Core\NativeMethods.cs" />
<Compile Include="Core\SafeHandleExtensions.cs" />
<Compile Include="Core\ObjectSafeWrapper.cs" />
Expand Down Expand Up @@ -132,6 +133,6 @@
<ItemGroup>
<NativeBinaries Include="$(MSBuildProjectDirectory)\..\Lib\NativeBinaries\**\*.*" />
</ItemGroup>
<Copy SourceFiles="@(NativeBinaries)" DestinationFiles="@(NativeBinaries->'$(OutputPath)NativeBinaries\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="true" />
<Copy SourceFiles="@(NativeBinaries)" DestinationFiles="@(NativeBinaries->'$(OutputPath)NativeBinaries\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="true" />
</Target>
</Project>
1 change: 1 addition & 0 deletions LibGit2Sharp/NamedReference.cs
@@ -1,5 +1,6 @@
using System;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;

namespace LibGit2Sharp
{
Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp/Reference.cs
Expand Up @@ -2,6 +2,7 @@
using System.Globalization;
using System.Runtime.InteropServices;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;

namespace LibGit2Sharp
{
Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp/Repository.cs
@@ -1,6 +1,7 @@
using System;
using System.IO;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;

namespace LibGit2Sharp
{
Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp/TagAnnotation.cs
@@ -1,6 +1,7 @@
using System;
using System.Runtime.InteropServices;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;

namespace LibGit2Sharp
{
Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp/TreeEntry.cs
@@ -1,6 +1,7 @@
using System;
using System.Runtime.InteropServices;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;

namespace LibGit2Sharp
{
Expand Down

0 comments on commit 1032a14

Please sign in to comment.