Skip to content

Commit c4bc752

Browse files
authored
Support required properties (#2579)
* Support required properties * Remove !
1 parent ce6eb7f commit c4bc752

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/BenchmarkDotNet/Code/CodeGenerator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ internal static string Generate(BuildPartition buildPartition)
5656
.Replace("$OverheadImplementation$", provider.OverheadImplementation)
5757
.Replace("$ConsumeField$", provider.ConsumeField)
5858
.Replace("$JobSetDefinition$", GetJobsSetDefinition(benchmark))
59+
.Replace("$ParamsInitializer$", GetParamsInitializer(benchmark))
5960
.Replace("$ParamsContent$", GetParamsContent(benchmark))
6061
.Replace("$ArgumentsDefinition$", GetArgumentsDefinition(benchmark))
6162
.Replace("$DeclareArgumentFields$", GetDeclareArgumentFields(benchmark))
@@ -186,7 +187,15 @@ private static DeclarationsProvider GetDeclarationsProvider(Descriptor descripto
186187
return new NonVoidDeclarationsProvider(descriptor);
187188
}
188189

190+
private static string GetParamsInitializer(BenchmarkCase benchmarkCase)
191+
=> string.Join(
192+
", ",
193+
benchmarkCase.Parameters.Items
194+
.Where(parameter => !parameter.IsArgument && !parameter.IsStatic)
195+
.Select(parameter => $"{parameter.Name} = default"));
196+
189197
// internal for tests
198+
190199
internal static string GetParamsContent(BenchmarkCase benchmarkCase)
191200
=> string.Join(
192201
string.Empty,

src/BenchmarkDotNet/Templates/BenchmarkType.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
public static void Run(BenchmarkDotNet.Engines.IHost host, System.String benchmarkName)
55
{
6-
BenchmarkDotNet.Autogenerated.Runnable_$ID$ instance = new BenchmarkDotNet.Autogenerated.Runnable_$ID$(); // do NOT change name "instance" (used in SmartParamameter)
6+
BenchmarkDotNet.Autogenerated.Runnable_$ID$ instance = new BenchmarkDotNet.Autogenerated.Runnable_$ID$ { $ParamsInitializer$ }; // do NOT change name "instance" (used in SmartParamameter)
77
$ParamsContent$
88

99
host.WriteLine();

tests/BenchmarkDotNet.IntegrationTests/ParamsTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,28 @@ public void Test()
217217
throw new ArgumentException($"{nameof(StaticParamProperty)} has wrong value: {StaticParamProperty}!");
218218
}
219219
}
220+
221+
#if NET8_0_OR_GREATER
222+
[Fact]
223+
public void ParamsSupportRequiredProperty()
224+
{
225+
var summary = CanExecute<ParamsTestRequiredProperty>();
226+
var standardOutput = GetCombinedStandardOutput(summary);
227+
228+
foreach (var param in new[] { "a", "b" })
229+
{
230+
Assert.Contains($"// ### New Parameter {param} ###", standardOutput);
231+
}
232+
}
233+
234+
public class ParamsTestRequiredProperty
235+
{
236+
[Params("a", "b")]
237+
public required string ParamProperty { get; set; }
238+
239+
[Benchmark]
240+
public void Benchmark() => Console.WriteLine($"// ### New Parameter {ParamProperty} ###");
241+
}
242+
#endif
220243
}
221244
}

0 commit comments

Comments
 (0)