From efcd62a32935bee486c876742829ce33d10e0b23 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sun, 27 Jan 2013 22:13:40 -0600 Subject: [PATCH] Fix test for non-virtual methods in types with public ctor * Exclude delegates from types checked * Missing virtual modifiers --- LibGit2Sharp.Tests/MetaFixture.cs | 28 +++++++++++++++------------- LibGit2Sharp/IndexEntry.cs | 10 +++++----- LibGit2Sharp/TreeDefinition.cs | 12 ++++++------ 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/LibGit2Sharp.Tests/MetaFixture.cs b/LibGit2Sharp.Tests/MetaFixture.cs index 46f60da20..153bea8bc 100644 --- a/LibGit2Sharp.Tests/MetaFixture.cs +++ b/LibGit2Sharp.Tests/MetaFixture.cs @@ -10,7 +10,15 @@ namespace LibGit2Sharp.Tests { public class MetaFixture { - private static readonly Type[] excludedTypes = new[] { typeof(Repository) }; + private static readonly Type[] excludedTypes = new[] + { + typeof(Credentials), + typeof(Filter), + typeof(ObjectId), + typeof(Repository), + typeof(RepositoryOptions), + typeof(Signature), + }; // Related to https://github.com/libgit2/libgit2sharp/pull/251 [Fact] @@ -59,19 +67,13 @@ public void TypesInLibGit2SharpMustBeExtensibleInATestingContext() var nonTestableTypes = new Dictionary>(); IEnumerable libGit2SharpTypes = Assembly.GetAssembly(typeof(Repository)).GetExportedTypes() - .Where(t => !excludedTypes.Contains(t) && t.Namespace == typeof(Repository).Namespace); + .Where(t => !excludedTypes.Contains(t) && t.Namespace == typeof(Repository).Namespace && !t.IsSubclassOf(typeof(Delegate))); foreach (Type type in libGit2SharpTypes) { if (type.IsInterface || type.IsEnum || IsStatic(type)) continue; - ConstructorInfo[] publicConstructor = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance); - if (publicConstructor.Any()) - { - continue; - } - var nonVirtualMethodNamesForType = GetNonVirtualPublicMethodsNames(type).ToList(); if (nonVirtualMethodNamesForType.Any()) { @@ -79,7 +81,7 @@ public void TypesInLibGit2SharpMustBeExtensibleInATestingContext() continue; } - if (!HasEmptyProtectedConstructor(type)) + if (!HasEmptyPublicOrProtectedConstructor(type)) { nonTestableTypes.Add(type, new List()); } @@ -131,11 +133,11 @@ private static IEnumerable GetNonVirtualPublicMethodsNames(Type type) return from mi in publicMethods where !mi.IsVirtual && !mi.IsStatic select mi.ToString(); } - private static bool HasEmptyProtectedConstructor(Type type) + private static bool HasEmptyPublicOrProtectedConstructor(Type type) { - ConstructorInfo[] nonPublicConstructors = type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance); - - return nonPublicConstructors.Any(ci => !ci.IsPrivate && !ci.IsAssembly && !ci.IsFinal && !ci.GetParameters().Any()); + ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + + return constructors.Any(ci => ci.GetParameters().Length == 0 && (ci.IsPublic || ci.IsFamily || ci.IsFamilyOrAssembly)); } private static bool IsStatic(Type type) diff --git a/LibGit2Sharp/IndexEntry.cs b/LibGit2Sharp/IndexEntry.cs index a17536990..b112dab1a 100644 --- a/LibGit2Sharp/IndexEntry.cs +++ b/LibGit2Sharp/IndexEntry.cs @@ -22,7 +22,7 @@ public class IndexEntry : IEquatable /// compared against the known from the and the file in the working directory. /// [Obsolete("This method will be removed in the next release. Please use Repository.Index.RetrieveStatus(filePath) overload instead.")] - public FileStatus State + public virtual FileStatus State { get { return state(); } } @@ -30,22 +30,22 @@ public FileStatus State /// /// Gets the relative path to the file within the working directory. /// - public string Path { get; private set; } + public virtual string Path { get; private set; } /// /// Gets the file mode. /// - public Mode Mode { get; private set; } + public virtual Mode Mode { get; private set; } /// /// Gets the stage number. /// - public StageLevel StageLevel { get; private set; } + public virtual StageLevel StageLevel { get; private set; } /// /// Gets the id of the pointed at by this index entry. /// - public ObjectId Id { get; private set; } + public virtual ObjectId Id { get; private set; } internal static IndexEntry BuildFromPtr(Repository repo, IndexEntrySafeHandle handle) { diff --git a/LibGit2Sharp/TreeDefinition.cs b/LibGit2Sharp/TreeDefinition.cs index 0f939c07c..d0d1d31e7 100644 --- a/LibGit2Sharp/TreeDefinition.cs +++ b/LibGit2Sharp/TreeDefinition.cs @@ -51,7 +51,7 @@ private void AddEntry(string targetTreeEntryName, TreeEntryDefinition treeEntryD /// /// The path within this . /// The current . - public TreeDefinition Remove(string treeEntryPath) + public virtual TreeDefinition Remove(string treeEntryPath) { Ensure.ArgumentNotNullOrEmptyString(treeEntryPath, "treeEntryPath"); @@ -92,7 +92,7 @@ public TreeDefinition Remove(string treeEntryPath) /// The path within this . /// The to be stored at the described location. /// The current . - public TreeDefinition Add(string targetTreeEntryPath, TreeEntryDefinition treeEntryDefinition) + public virtual TreeDefinition Add(string targetTreeEntryPath, TreeEntryDefinition treeEntryDefinition) { Ensure.ArgumentNotNullOrEmptyString(targetTreeEntryPath, "targetTreeEntryPath"); Ensure.ArgumentNotNull(treeEntryDefinition, "treeEntryDefinition"); @@ -132,7 +132,7 @@ public TreeDefinition Add(string targetTreeEntryPath, TreeEntryDefinition treeEn /// The to be stored at the described location. /// The file related attributes. /// The current . - public TreeDefinition Add(string targetTreeEntryPath, Blob blob, Mode mode) + public virtual TreeDefinition Add(string targetTreeEntryPath, Blob blob, Mode mode) { Ensure.ArgumentNotNull(blob, "blob"); Ensure.ArgumentConformsTo(mode, @@ -151,7 +151,7 @@ public TreeDefinition Add(string targetTreeEntryPath, Blob blob, Mode mode) /// is a standard, non-bare, repository. The path will then be considered as a path relative to the root of the working directory. /// The file related attributes. /// The current . - public TreeDefinition Add(string targetTreeEntryPath, string filePath, Mode mode) + public virtual TreeDefinition Add(string targetTreeEntryPath, string filePath, Mode mode) { Ensure.ArgumentNotNullOrEmptyString(filePath, "filePath"); @@ -166,7 +166,7 @@ public TreeDefinition Add(string targetTreeEntryPath, string filePath, Mode mode /// The path within this . /// The to be stored at the described location. /// The current . - public TreeDefinition Add(string targetTreeEntryPath, Tree tree) + public virtual TreeDefinition Add(string targetTreeEntryPath, Tree tree) { Ensure.ArgumentNotNull(tree, "tree"); @@ -281,7 +281,7 @@ private void WrapTree(string entryName, TreeEntryDefinition treeEntryDefinition) /// /// The path within this . /// The found if any; null otherwise. - public TreeEntryDefinition this[string treeEntryPath] + public virtual TreeEntryDefinition this[string treeEntryPath] { get {