Skip to content

Commit

Permalink
Move benchmark test data to its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveDesmond-ca committed Nov 29, 2024
1 parent d37add0 commit afdc5d2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
32 changes: 32 additions & 0 deletions BenchmarkMockNet.Tests/BenchmarkData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Reflection;
using BenchmarkMockNet.Benchmarks;
using Xunit;

namespace BenchmarkMockNet.Tests;

public class BenchmarkData : TheoryData<Type, MethodInfo>
{
public BenchmarkData()
{
var methods = typeof(IMockingBenchmark).GetMethods();

foreach (var benchmark in Benchmarks)
{
foreach (var method in methods)
{
Add(benchmark.Key, method);
}
}
}

public static readonly Dictionary<Type, Func<object?, bool>> Benchmarks = new()
{
{ typeof(Construction), result => result is IThing },
{ typeof(Callback), result => result is true },
{ typeof(EmptyMethod), result => result is null },
{ typeof(EmptyReturn), result => result is 0 },
{ typeof(OneParameter), result => result is null },
{ typeof(Return), result => result is 1 },
{ typeof(Verify), result => result is null }
};
}
39 changes: 4 additions & 35 deletions BenchmarkMockNet.Tests/BenchmarkTests.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,13 @@
using System.Reflection;
using BenchmarkMockNet.Benchmarks;
using Xunit;

namespace BenchmarkMockNet.Tests;

public static class BenchmarkTests
public class BenchmarkTests
{
private static readonly MethodInfo[] Methods = typeof(IMockingBenchmark).GetMethods();

private static readonly Dictionary<Type, Func<object?, bool>> Benchmarks = new()
{
{ typeof(Construction), result => result is IThing },
{ typeof(Callback), result => result is true },
{ typeof(EmptyMethod), result => result is null },
{ typeof(EmptyReturn), result => result is 0 },
{ typeof(OneParameter), result => result is null },
{ typeof(Return), result => result is 1 },
{ typeof(Verify), result => result is null }
};

public static TheoryData<Type, MethodInfo> Matrix
{
get
{
var matrix = new TheoryData<Type, MethodInfo>();
foreach (var benchmark in Benchmarks)
{
foreach (var method in Methods)
{
matrix.Add(benchmark.Key, method);
}
}

return matrix;
}
}

[Theory]
[MemberData(nameof(Matrix))]
public static void RunAll(Type type, MethodInfo library)
[ClassData(typeof(BenchmarkData))]
public void RunAll(Type type, MethodInfo library)
{
//arrange
var benchmark = Activator.CreateInstance(type);
Expand All @@ -48,7 +17,7 @@ public static void RunAll(Type type, MethodInfo library)
var result = method?.Invoke(benchmark, []);

//assert
var assertion = Benchmarks[type];
var assertion = BenchmarkData.Benchmarks[type];
Assert.True(assertion(result));
}
}

0 comments on commit afdc5d2

Please sign in to comment.