Skip to content

Commit

Permalink
Merge pull request #13 from krk/master
Browse files Browse the repository at this point in the history
Support benchmark methods in nested types and check for accessibility
  • Loading branch information
AndreyAkinshin committed Jul 2, 2015
2 parents a5cabaa + 04ce12b commit d750b90
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
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.");
}
}
}

0 comments on commit d750b90

Please sign in to comment.