Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support benchmark methods in nested types and check for accessibility #13

Merged
merged 2 commits into from
Jul 2, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Accessibility and genericness checks added for benchmark methods.
  • Loading branch information
krk committed Jun 25, 2015
commit d70b4cd339c3c40a3a1f7b996b4004ba59c25ff4
37 changes: 37 additions & 0 deletions BenchmarkDotNet/BenchmarkRunner.cs
Original file line number Diff line number Diff line change
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.");
}
}
}