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 required properties #2579

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/BenchmarkDotNet/Code/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ internal static string Generate(BuildPartition buildPartition)
.Replace("$OverheadImplementation$", provider.OverheadImplementation)
.Replace("$ConsumeField$", provider.ConsumeField)
.Replace("$JobSetDefinition$", GetJobsSetDefinition(benchmark))
.Replace("$ParamsInitializer$", GetParamsInitializer(benchmark))
.Replace("$ParamsContent$", GetParamsContent(benchmark))
.Replace("$ArgumentsDefinition$", GetArgumentsDefinition(benchmark))
.Replace("$DeclareArgumentFields$", GetDeclareArgumentFields(benchmark))
Expand Down Expand Up @@ -186,7 +187,15 @@ private static DeclarationsProvider GetDeclarationsProvider(Descriptor descripto
return new NonVoidDeclarationsProvider(descriptor);
}

private static string GetParamsInitializer(BenchmarkCase benchmarkCase)
=> string.Join(
", ",
benchmarkCase.Parameters.Items
.Where(parameter => !parameter.IsArgument && !parameter.IsStatic)
.Select(parameter => $"{parameter.Name} = default"));

// internal for tests

internal static string GetParamsContent(BenchmarkCase benchmarkCase)
=> string.Join(
string.Empty,
Expand Down
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Templates/BenchmarkType.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
public static void Run(BenchmarkDotNet.Engines.IHost host, System.String benchmarkName)
{
BenchmarkDotNet.Autogenerated.Runnable_$ID$ instance = new BenchmarkDotNet.Autogenerated.Runnable_$ID$(); // do NOT change name "instance" (used in SmartParamameter)
BenchmarkDotNet.Autogenerated.Runnable_$ID$ instance = new BenchmarkDotNet.Autogenerated.Runnable_$ID$ { $ParamsInitializer$ }; // do NOT change name "instance" (used in SmartParamameter)
$ParamsContent$

host.WriteLine();
Expand Down
23 changes: 23 additions & 0 deletions tests/BenchmarkDotNet.IntegrationTests/ParamsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,28 @@ public void Test()
throw new ArgumentException($"{nameof(StaticParamProperty)} has wrong value: {StaticParamProperty}!");
}
}

#if NET8_0_OR_GREATER
[Fact]
public void ParamsSupportRequiredProperty()
{
var summary = CanExecute<ParamsTestRequiredProperty>();
var standardOutput = GetCombinedStandardOutput(summary);

foreach (var param in new[] { "a", "b" })
{
Assert.Contains($"// ### New Parameter {param} ###", standardOutput);
}
}

public class ParamsTestRequiredProperty
{
[Params("a", "b")]
public required string ParamProperty { get; set; }

[Benchmark]
public void Benchmark() => Console.WriteLine($"// ### New Parameter {ParamProperty} ###");
}
#endif
}
}