Skip to content
Browse files

Stop sorting collections

  • Loading branch information...
1 parent b265bfc commit 678b2687ed5ae3b41bad53d906e32016694063a0 @dahlbyk dahlbyk committed with nulltoken Feb 19, 2013
View
18 LibGit2Sharp.Tests/BranchFixture.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using System.IO;
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
@@ -183,7 +184,7 @@ public void CanListAllBranches()
{
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Equal(expectedBranches, repo.Branches.Select(b => b.Name).ToArray());
+ Assert.Equal(expectedBranches, SortedBranches(repo.Branches, b => b.Name));
Assert.Equal(5, repo.Branches.Count());
}
@@ -204,9 +205,8 @@ public void CanListBranchesWithRemoteAndLocalBranchWithSameShortName()
"diff-test-cases", "i-do-numbers", "logo", "master", "origin/master", "track-local",
};
- Assert.Equal(expectedWdBranches, repo.Branches
- .Where(b => !b.IsRemote)
- .Select(b => b.Name).ToArray());
+ Assert.Equal(expectedWdBranches,
+ SortedBranches(repo.Branches.Where(b => !b.IsRemote), b => b.Name));
}
}
@@ -222,7 +222,7 @@ public void CanListAllBranchesWhenGivenWorkingDir()
"origin/test"
};
- Assert.Equal(expectedWdBranches, repo.Branches.Select(b => b.Name).ToArray());
+ Assert.Equal(expectedWdBranches, SortedBranches(repo.Branches, b => b.Name));
}
}
@@ -244,7 +244,8 @@ public void CanListAllBranchesIncludingRemoteRefs()
new { Name = "origin/packed-test", Sha = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", IsRemote = true },
new { Name = "origin/test", Sha = "e90810b8df3e80c413d903f631643c716887138d", IsRemote = true },
};
- Assert.Equal(expectedBranchesIncludingRemoteRefs, repo.Branches.Select(b => new { b.Name, b.Tip.Sha, b.IsRemote }).ToArray());
+ Assert.Equal(expectedBranchesIncludingRemoteRefs,
+ SortedBranches(repo.Branches, b => new { b.Name, b.Tip.Sha, b.IsRemote }));
}
}
@@ -811,5 +812,10 @@ public void RemoteBranchesDoNotTrackAnything()
}
}
}
+
+ private static T[] SortedBranches<T>(IEnumerable<Branch> branches, Func<Branch, T> selector)
+ {
+ return branches.OrderBy(b => b.CanonicalName, StringComparer.Ordinal).Select(selector).ToArray();
+ }
}
}
View
5 LibGit2Sharp.Tests/CommitFixture.cs
@@ -347,7 +347,10 @@ private static void CanEnumerateCommitsFromATag(Func<Tag, object> transformer)
public void CanEnumerateAllCommits()
{
AssertEnumerationOfCommits(
- repo => new Filter { Since = repo.Refs },
+ repo => new Filter
+ {
+ Since = repo.Refs.OrderBy(r => r.CanonicalName, StringComparer.Ordinal),
+ },
new[]
{
"44d5d18", "bb65291", "532740a", "503a16f", "3dfd6fd",
View
2 LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs
@@ -125,7 +125,7 @@ public void CanCompareACommitTreeAgainstATreeWithNoCommonAncestor()
Assert.Equal("readme.txt", changes.Deleted.Single().Path);
Assert.Equal(new[] { "1.txt", subBranchFilePath, "README", "branch_file.txt", "deleted_staged_file.txt", "deleted_unstaged_file.txt", "modified_staged_file.txt", "modified_unstaged_file.txt", "new.txt" },
- changes.Added.Select(x => x.Path));
+ changes.Added.Select(x => x.Path).OrderBy(p => p, StringComparer.Ordinal).ToArray());
Assert.Equal(9, changes.LinesAdded);
Assert.Equal(2, changes.LinesDeleted);
View
3 LibGit2Sharp.Tests/IndexFixture.cs
@@ -40,7 +40,8 @@ public void CanEnumerateIndex()
{
using (var repo = new Repository(StandardTestRepoPath))
{
- Assert.Equal(expectedEntries, repo.Index.Select(e => e.Path).ToArray());
+ Assert.Equal(expectedEntries,
+ repo.Index.Select(e => e.Path).OrderBy(p => p, StringComparer.Ordinal).ToArray());
}
}
View
31 LibGit2Sharp.Tests/NoteFixture.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using System.Linq;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;
@@ -61,19 +62,19 @@ public void CanRetrieveNotesFromAGitObject()
var notes = repo.Notes[new ObjectId("4a202b346bb0fb0db7eff3cffeb3c70babbd2045")];
Assert.NotNull(notes);
- Assert.Equal(3, notes.Count());
- Assert.Equal(expectedMessages, notes.Select(n => n.Message));
+ Assert.Equal(expectedMessages, SortedNotes(notes, n => n.Message));
}
}
[Fact]
public void CanGetListOfNotesNamespaces()
{
- var expectedNamespaces = new[] { "commits", "answer", "answer2" };
+ var expectedNamespaces = new[] { "answer", "answer2", "commits", };
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Equal(expectedNamespaces, repo.Notes.Namespaces);
+ Assert.Equal(expectedNamespaces,
+ repo.Notes.Namespaces.OrderBy(n => n, StringComparer.Ordinal).ToArray());
Assert.Equal(repo.Notes.DefaultNamespace, repo.Notes.Namespaces.First());
}
}
@@ -105,12 +106,12 @@ public void CanAccessNotesFromACommit()
{
var commit = repo.Lookup<Commit>("4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
- Assert.Equal(expectedNamespaces, commit.Notes.Select(n => n.Message));
+ Assert.Equal(expectedNamespaces, SortedNotes(commit.Notes, n => n.Message));
// Make sure that Commit.Notes is not refreshed automatically
repo.Notes.Add(commit.Id, "I'm batman!\n", signatureNullToken, signatureYorah, "batmobile");
- Assert.Equal(expectedNamespaces, commit.Notes.Select(n => n.Message));
+ Assert.Equal(expectedNamespaces, SortedNotes(commit.Notes, m => m.Message));
}
}
@@ -228,16 +229,26 @@ public void RemovingANonExistingNoteDoesntThrow()
[Fact]
public void CanRetrieveTheListOfNotesForAGivenNamespace()
{
- var expectedNotes = new[] { new Tuple<string, string>("1a550e416326cdb4a8e127a04dd69d7a01b11cf4", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"),
- new Tuple<string, string>("272a41cf2b22e57f2bc5bf6ef37b63568cd837e4", "8496071c1b46c854b31185ea97743be6a8774479") };
+ var expectedNotes = new[]
+ {
+ new { Blob = "272a41cf2b22e57f2bc5bf6ef37b63568cd837e4", Target = "8496071c1b46c854b31185ea97743be6a8774479" },
+ new { Blob = "1a550e416326cdb4a8e127a04dd69d7a01b11cf4", Target = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045" },
+ };
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Equal(expectedNotes, repo.Notes["commits"].Select(n => new Tuple<string, string>(n.BlobId.Sha, n.TargetObjectId.Sha)).ToArray());
+ Assert.Equal(expectedNotes,
+ SortedNotes(repo.Notes["commits"], n => new { Blob = n.BlobId.Sha, Target = n.TargetObjectId.Sha }));
Assert.Equal("commits", repo.Notes.DefaultNamespace);
- Assert.Equal(expectedNotes, repo.Notes.Select(n => new Tuple<string, string>(n.BlobId.Sha, n.TargetObjectId.Sha)).ToArray());
+ Assert.Equal(expectedNotes,
+ SortedNotes(repo.Notes, n => new { Blob = n.BlobId.Sha, Target = n.TargetObjectId.Sha }));
}
}
+
+ private static T[] SortedNotes<T>(IEnumerable<Note> notes, Func<Note, T> selector)
+ {
+ return notes.OrderBy(n => n.Message, StringComparer.Ordinal).Select(selector).ToArray();
+ }
}
}
View
7 LibGit2Sharp.Tests/ReferenceFixture.cs
@@ -283,7 +283,7 @@ public void CanListAllReferencesEvenCorruptedOnes()
{
CreateCorruptedDeadBeefHead(repo.Info.Path);
- Assert.Equal(expectedRefs, repo.Refs.Select(r => r.CanonicalName).ToArray());
+ Assert.Equal(expectedRefs, SortedRefs(repo, r => r.CanonicalName));
Assert.Equal(13, repo.Refs.Count());
}
@@ -685,5 +685,10 @@ public void CanTellIfAReferenceIsValid(string refname, bool expectedResult)
Assert.Equal(expectedResult, repo.Refs.IsValidName(refname));
}
}
+
+ private static T[] SortedRefs<T>(IRepository repo, Func<Reference, T> selector)
+ {
+ return repo.Refs.OrderBy(r => r.CanonicalName, StringComparer.Ordinal).Select(selector).ToArray();
+ }
}
}
View
1 LibGit2Sharp.Tests/ResetHeadFixture.cs
@@ -188,7 +188,6 @@ public void HardResetUpdatesTheContentOfTheWorkingDirectory()
using (var repo = new Repository(clone.DirectoryPath))
{
var names = new DirectoryInfo(repo.Info.WorkingDirectory).GetFileSystemInfos().Select(fsi => fsi.Name).ToList();
- names.Sort(StringComparer.Ordinal);
File.Delete(Path.Combine(repo.Info.WorkingDirectory, "README"));
File.WriteAllText(Path.Combine(repo.Info.WorkingDirectory, "WillNotBeRemoved.txt"), "content\n");
View
22 LibGit2Sharp.Tests/TagFixture.cs
@@ -589,7 +589,7 @@ public void CanListTags()
{
using (var repo = new Repository(BareTestRepoPath))
{
- Assert.Equal(expectedTags, repo.Tags.Select(t => t.Name).ToArray());
+ Assert.Equal(expectedTags, SortedTags(repo.Tags, t => t.Name));
Assert.Equal(4, repo.Tags.Count());
}
@@ -609,21 +609,6 @@ public void CanListAllTagsInAEmptyRepository()
}
[Fact]
- // Ported from cgit (https://github.com/git/git/blob/1c08bf50cfcf924094eca56c2486a90e2bf1e6e2/t/t7004-tag.sh#L165)
- public void ListAllTagsShouldOutputThemInAnOrderedWay()
- {
- using (var repo = new Repository(BareTestRepoPath))
- {
- List<string> tagNames = repo.Tags.Select(t => t.Name).ToList();
-
- List<string> sortedTags = expectedTags.ToList();
- sortedTags.Sort();
-
- Assert.Equal(sortedTags, tagNames);
- }
- }
-
- [Fact]
public void CanLookupALightweightTag()
{
using (var repo = new Repository(BareTestRepoPath))
@@ -693,5 +678,10 @@ public void LookupNullTagNameThrows()
Assert.Throws<ArgumentNullException>(() => { Tag t = repo.Tags[null]; });
}
}
+
+ private static T[] SortedTags<T>(IEnumerable<Tag> tags, Func<Tag, T> selector)
+ {
+ return tags.OrderBy(t => t.CanonicalName, StringComparer.Ordinal).Select(selector).ToArray();
+ }
}
}
View
1 LibGit2Sharp/BranchCollection.cs
@@ -93,7 +93,6 @@ public virtual IEnumerator<Branch> GetEnumerator()
{
return Proxy.git_branch_foreach(repo.Handle, GitBranchType.GIT_BRANCH_LOCAL | GitBranchType.GIT_BRANCH_REMOTE, branchToCanoncialName)
.Select(n => this[n])
- .OrderBy(b => b.CanonicalName, StringComparer.Ordinal)
.GetEnumerator();
}
View
1 LibGit2Sharp/Core/Proxy.cs
@@ -2130,7 +2130,6 @@ public static IList<string> BuildListOf(UnSafeNativeMethods.git_strarray strArra
list.Add(name);
}
- list.Sort(StringComparer.Ordinal);
return list;
}
finally
View
16 LibGit2Sharp/Index.cs
@@ -91,21 +91,6 @@ public virtual bool IsFullyMerged
#region IEnumerable<IndexEntry> Members
- private class OrdinalComparer<T> : IComparer<T>
- {
- Func<T, string> accessor;
-
- public OrdinalComparer(Func<T, string> accessor)
- {
- this.accessor = accessor;
- }
-
- public int Compare(T x, T y)
- {
- return string.CompareOrdinal(accessor(x), accessor(y));
- }
- }
-
private List<IndexEntry> AllIndexEntries()
{
var entryCount = Count;
@@ -116,7 +101,6 @@ private List<IndexEntry> AllIndexEntries()
list.Add(this[i]);
}
- list.Sort(new OrdinalComparer<IndexEntry>(i => i.Path));
return list;
}
View
2 LibGit2Sharp/ReferenceCollection.cs
@@ -244,7 +244,7 @@ public virtual IEnumerable<Reference> FromGlob(string pattern)
Ensure.ArgumentNotNullOrEmptyString(pattern, "pattern");
return Proxy.git_reference_foreach_glob(repo.Handle, pattern, GitReferenceType.ListAll, Utf8Marshaler.FromNative)
- .OrderBy(name => name, StringComparer.Ordinal).Select(n => this[n]);
+ .Select(n => this[n]);
}
/// <summary>
View
5 LibGit2Sharp/TreeChanges.cs
@@ -28,8 +28,6 @@ public class TreeChanges : IEnumerable<TreeEntryChanges>
private readonly IDictionary<ChangeKind, Action<TreeChanges, TreeEntryChanges>> fileDispatcher = Build();
private readonly StringBuilder fullPatchBuilder = new StringBuilder();
- private static readonly Comparison<TreeEntryChanges> ordinalComparer =
- (one, other) => string.CompareOrdinal(one.Path, other.Path);
private static IDictionary<ChangeKind, Action<TreeChanges, TreeEntryChanges>> Build()
{
@@ -51,9 +49,6 @@ protected TreeChanges()
internal TreeChanges(DiffListSafeHandle diff)
{
Proxy.git_diff_print_patch(diff, PrintCallBack);
- added.Sort(ordinalComparer);
- deleted.Sort(ordinalComparer);
- modified.Sort(ordinalComparer);
}
private int PrintCallBack(GitDiffDelta delta, GitDiffRange range, GitDiffLineOrigin lineorigin, IntPtr content, UIntPtr contentlen, IntPtr payload)

0 comments on commit 678b268

Please sign in to comment.
Something went wrong with that request. Please try again.