Skip to content

Commit 393b3bc

Browse files
committed
meta fixture ensuring GetEnumerators are testable
fix other non-virtual GetEnumerators the simplest thing that works simpler still when in Rome…
1 parent 907d222 commit 393b3bc

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

LibGit2Sharp.Tests/MetaFixture.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,30 @@ private static bool IsStatic(Type type)
196196
{
197197
return type.IsAbstract && type.IsSealed;
198198
}
199+
200+
// Related to https://github.com/libgit2/libgit2sharp/issues/644 and https://github.com/libgit2/libgit2sharp/issues/645
201+
[Fact]
202+
public void GetEnumeratorMethodsInLibGit2SharpMustBeVirtualForTestability()
203+
{
204+
var nonVirtualGetEnumeratorMethods = Assembly.GetAssembly(typeof(IRepository))
205+
.GetExportedTypes()
206+
.Where(t =>
207+
t.Namespace == typeof (IRepository).Namespace &&
208+
!t.IsSealed &&
209+
!t.IsAbstract &&
210+
t.GetInterfaces().Any(i => i.IsAssignableFrom(typeof(IEnumerable<>))))
211+
.Select(t => t.GetMethod("GetEnumerator"))
212+
.Where(m =>
213+
m.ReturnType.Name == "IEnumerator`1" &&
214+
(!m.IsVirtual || m.IsFinal))
215+
.ToList();
216+
217+
foreach (var method in nonVirtualGetEnumeratorMethods)
218+
{
219+
Debug.WriteLine(String.Format("GetEnumerator in type '{0}' isn't virtual.", method.DeclaringType));
220+
}
221+
222+
Assert.Empty(nonVirtualGetEnumeratorMethods);
223+
}
199224
}
200225
}

LibGit2Sharp/BlameHunkCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public virtual BlameHunk HunkForLine(int line)
8383
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
8484
/// </returns>
8585
/// <filterpriority>2</filterpriority>
86-
public IEnumerator<BlameHunk> GetEnumerator()
86+
public virtual IEnumerator<BlameHunk> GetEnumerator()
8787
{
8888
return hunks.GetEnumerator();
8989
}

LibGit2Sharp/ReflogCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ internal ReflogCollection(Repository repo, string canonicalName)
5555
/// </para>
5656
/// </summary>
5757
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
58-
public IEnumerator<ReflogEntry> GetEnumerator()
58+
public virtual IEnumerator<ReflogEntry> GetEnumerator()
5959
{
6060
var entries = new List<ReflogEntry>();
6161

LibGit2Sharp/StashCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal StashCollection(Repository repo)
4040
/// </para>
4141
/// </summary>
4242
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
43-
public IEnumerator<Stash> GetEnumerator()
43+
public virtual IEnumerator<Stash> GetEnumerator()
4444
{
4545
return Proxy.git_stash_foreach(repo.Handle,
4646
(index, message, commitId) => new Stash(repo, new ObjectId(commitId), index)).GetEnumerator();

0 commit comments

Comments
 (0)