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
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion BenchmarkDotNet/BenchmarkProjectGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static void GenerateProgramFile(string projectDir, Benchmark benchmark)
? "System"
: benchmark.Target.Method.ReturnType.Namespace;

var targetTypeName = benchmark.Target.Type.FullName;
var targetTypeName = benchmark.Target.Type.FullName.Replace('+','.');
var targetMethodName = benchmark.Target.Method.Name;

var targetMethodReturnType = isVoid
Expand Down
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.");
}
}
}