Skip to content

Commit 6a47e26

Browse files
authored
Allow override method/property for ParamsSource (#2832)
* Allow override method/property for ParamsSource * add tests for allow ParamsSource override
1 parent 4071cae commit 6a47e26

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/BenchmarkDotNet/Running/BenchmarkConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,15 @@ private static object Map(object providedValue, Type type)
308308

309309
private static (MemberInfo source, object[] values) GetValidValuesForParamsSource(Type sourceType, string sourceName)
310310
{
311-
var paramsSourceMethod = sourceType.GetAllMethods().SingleOrDefault(method => method.Name == sourceName && method.IsPublic);
311+
var paramsSourceMethod = sourceType.GetAllMethods().FirstOrDefault(method => method.Name == sourceName && method.IsPublic);
312312

313313
if (paramsSourceMethod != default)
314314
return (paramsSourceMethod, ToArray(
315315
paramsSourceMethod.Invoke(paramsSourceMethod.IsStatic ? null : Activator.CreateInstance(sourceType), null),
316316
paramsSourceMethod,
317317
sourceType));
318318

319-
var paramsSourceProperty = sourceType.GetAllProperties().SingleOrDefault(property => property.Name == sourceName && property.GetMethod.IsPublic);
319+
var paramsSourceProperty = sourceType.GetAllProperties().FirstOrDefault(property => property.Name == sourceName && property.GetMethod.IsPublic);
320320

321321
if (paramsSourceProperty != default)
322322
return (paramsSourceProperty, ToArray(

tests/BenchmarkDotNet.IntegrationTests/ParamSourceTests.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace BenchmarkDotNet.IntegrationTests
1616
{
17-
public class ParamSourceTests: BenchmarkTestExecutor
17+
public class ParamSourceTests : BenchmarkTestExecutor
1818
{
1919
public ParamSourceTests(ITestOutputHelper output) : base(output) { }
2020

@@ -204,5 +204,43 @@ public void SourceWithExplicitCastToTarget_InProcessToolchain_Throws()
204204
// public void SourceWithExplicitCastToTarget_Succeeds(IToolchain toolchain) => CanExecuteWithExtraInfo(typeof(SourceWithExplicitCastToTarget), toolchain);
205205
Assert.ThrowsAny<Exception>(() => CanExecuteWithExtraInfo(typeof(SourceWithExplicitCastToTarget), InProcessEmitToolchain.Instance));
206206
}
207+
208+
public abstract class OverridePropertyBase
209+
{
210+
public abstract int[] GetSourceProperty { get; }
211+
212+
[ParamsSource(nameof(GetSourceProperty))]
213+
public int ParamsTarget { get; set; }
214+
}
215+
216+
public class OverrideProperty : OverridePropertyBase
217+
{
218+
public override int[] GetSourceProperty => new int[] { 1, 2, 3 };
219+
220+
[Benchmark]
221+
public int Benchmark() => ParamsTarget;
222+
}
223+
224+
[Theory, MemberData(nameof(GetToolchains), DisableDiscoveryEnumeration = true)]
225+
public void OverrideProperty_Succeeds(IToolchain toolchain) => CanExecuteWithExtraInfo(typeof(OverrideProperty), toolchain);
226+
227+
public abstract class OverrideMethodBase
228+
{
229+
public abstract int[] GetSourceMethod();
230+
231+
[ParamsSource(nameof(GetSourceMethod))]
232+
public int ParamsTarget { get; set; }
233+
}
234+
235+
public class OverrideMethod : OverrideMethodBase
236+
{
237+
public override int[] GetSourceMethod() => new int[] { 1, 2, 3 };
238+
239+
[Benchmark]
240+
public int Benchmark() => ParamsTarget;
241+
}
242+
243+
[Theory, MemberData(nameof(GetToolchains), DisableDiscoveryEnumeration = true)]
244+
public void OverrideMethod_Succeeds(IToolchain toolchain) => CanExecuteWithExtraInfo(typeof(OverrideMethod), toolchain);
207245
}
208246
}

0 commit comments

Comments
 (0)