Skip to content

Commit

Permalink
Accessibility and genericness checks added for benchmark methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
krk committed Jun 25, 2015
1 parent 14ddf43 commit d70b4cd
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions BenchmarkDotNet/BenchmarkRunner.cs
Expand Up @@ -187,6 +187,9 @@ private static IEnumerable<Benchmark> CompetitionToBenchmarks(object competition
{
var target = new BenchmarkTarget(targetType, methodInfo, benchmarkAttribute.Description);
AssertBenchmarkMethodHasCorrectSignature(methodInfo);
AssertBenchmarkMethodIsAccessible(methodInfo);
AssertBenchmarkMethodIsNotDeclaredInGeneric(methodInfo);
AssertBenchmarkMethodIsNotGeneric(methodInfo);
foreach (var task in BenchmarkTask.Resolve(methodInfo, defaultSettings))
yield return new Benchmark(target, task);
}
Expand All @@ -198,5 +201,39 @@ private static void AssertBenchmarkMethodHasCorrectSignature(MethodInfo methodIn
if (methodInfo.GetParameters().Any())
throw new InvalidOperationException($"Benchmark method {methodInfo.Name} has incorrect signature.\nMethod shouldn't have any arguments.");
}
private static void AssertBenchmarkMethodIsAccessible(MethodInfo methodInfo)
{
if (!methodInfo.IsPublic)
throw new InvalidOperationException($"Benchmark method {methodInfo.Name} has incorrect access modifiers.\nMethod must be public.");

var declaringType = methodInfo.DeclaringType;

while (declaringType != null)
{
if (!declaringType.IsPublic && !declaringType.IsNestedPublic)
throw new InvalidOperationException($"Benchmark method {methodInfo.Name} defined within type {declaringType.FullName} has incorrect access modifiers.\nDeclaring type must be public.");

declaringType = declaringType.DeclaringType;
}
}

private static void AssertBenchmarkMethodIsNotDeclaredInGeneric(MethodInfo methodInfo)
{
var declaringType = methodInfo.DeclaringType;

while (declaringType != null)
{
if (declaringType.IsGenericType)
throw new InvalidOperationException($"Benchmark method {methodInfo.Name} defined within generic type {declaringType.FullName}.\nBenchmark methods in generic types are not supported.");

declaringType = declaringType.DeclaringType;
}
}

private static void AssertBenchmarkMethodIsNotGeneric(MethodInfo methodInfo)
{
if (methodInfo.IsGenericMethod)
throw new InvalidOperationException($"Benchmark method {methodInfo.Name} is generic.\nGeneric benchmark methods are not supported.");
}
}
}

0 comments on commit d70b4cd

Please sign in to comment.