Skip to content

Commit

Permalink
feat: UnitTestsControl should handle default parameter values
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Nov 8, 2023
1 parent f4d9241 commit aab2154
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,13 @@ await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
await initializeReturnTask;
}

var methodParameters = test.Method.GetParameters();
if (methodParameters.Length > methodArguments.Length)
{
methodArguments = ExpandArgumentsWithDefaultValues(methodArguments, methodParameters);
}
returnValue = test.Method.Invoke(instance, methodArguments);

sw.Stop();

cts.TrySetResult(true);
Expand Down Expand Up @@ -977,6 +983,30 @@ void Run()
}
}

private static object[] ExpandArgumentsWithDefaultValues(object[] methodArguments, ParameterInfo[] methodParameters)
{
var expandedArguments = new List<object>(methodParameters.Length);
for (int i = 0; i < methodArguments.Length; i++)
{
expandedArguments.Add(methodArguments[i]);
}
// Try to get default values for the rest
for (int i = 0; i < methodParameters.Length - methodArguments.Length; i++)
{
var parameter = methodParameters[methodArguments.Length + i];
if (!parameter.HasDefaultValue)
{
throw new InvalidOperationException("Missing parameter does not have default value");
}
else
{
expandedArguments.Add(parameter.DefaultValue);
}
}

return expandedArguments.ToArray();
}

private TimeSpan GetTestTimeout(UnitTestMethodInfo test)
{
if (test.Method.GetCustomAttribute(typeof(TimeoutAttribute)) is TimeoutAttribute methodAttribute)
Expand Down

0 comments on commit aab2154

Please sign in to comment.