Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Dahlby <dahlbyk@gmail.com>2015-06-19 01:00:14 +0300
committernulltoken <emeric.fermas@gmail.com>2015-06-19 01:02:50 +0300
commit025b273cb463904efb217fd70b48e4fe5c0ed29b (patch)
treea0d7d3c88c3e4b686d5b2d3f6efb19f86052d800
parent73d7ea76ac8196b6049ddc9ec1f0f3784677b69e (diff)
Ensure public extension methods always target interfaces or enums
-rw-r--r--LibGit2Sharp.Tests/MetaFixture.cs34
1 files changed, 34 insertions, 0 deletions
diff --git a/LibGit2Sharp.Tests/MetaFixture.cs b/LibGit2Sharp.Tests/MetaFixture.cs
index 685ff5f4..86ecdf3c 100644
--- a/LibGit2Sharp.Tests/MetaFixture.cs
+++ b/LibGit2Sharp.Tests/MetaFixture.cs
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
+using System.Globalization;
using System.Linq;
using System.Reflection;
+using System.Runtime.CompilerServices;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
@@ -344,6 +346,38 @@ namespace LibGit2Sharp.Tests
Assert.Equal("", sb.ToString());
}
+
+ [Fact]
+ public void PublicExtensionMethodsShouldonlyTargetInterfacesOrEnums()
+ {
+ IEnumerable<string> mis =
+ from m in GetInvalidPublicExtensionMethods()
+ select m.DeclaringType + "." + m.Name;
+
+ var sb = new StringBuilder();
+
+ foreach (var method in mis.Distinct())
+ {
+ sb.AppendFormat("'{0}' is a public extension method that doesn't target an interface or an enum.{1}",
+ method, Environment.NewLine);
+ }
+
+ Assert.Equal("", sb.ToString());
+ }
+
+ // Inspired from http://stackoverflow.com/a/299526
+
+ static IEnumerable<MethodInfo> GetInvalidPublicExtensionMethods()
+ {
+ var query = from type in (Assembly.GetAssembly(typeof(IRepository))).GetTypes()
+ where type.IsSealed && !type.IsGenericType && !type.IsNested && type.IsPublic
+ from method in type.GetMethods(BindingFlags.Static | BindingFlags.Public)
+ where method.IsDefined(typeof(ExtensionAttribute), false)
+ let parameterType = method.GetParameters()[0].ParameterType
+ where parameterType != null && !parameterType.IsInterface && !parameterType.IsEnum
+ select method;
+ return query;
+ }
}
internal static class TypeExtensions