Skip to content

Commit

Permalink
refactor: Ensure consistent usage of Parameter and Argument nomenclature
Browse files Browse the repository at this point in the history
  • Loading branch information
DrBarnabus committed Feb 10, 2024
1 parent b63c522 commit 2822047
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/Mimic/Core/Extensions/DelegateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ internal static void ValidateDelegateParameterCount(this Delegate delegateFuncti
{
var methodInfo = delegateFunction.GetMethodInfo();

int actualNumberOfArguments = methodInfo.GetParameters().Length;
int actualNumberOfParameters = methodInfo.GetParameters().Length;
if (methodInfo.IsStatic && (methodInfo.IsDefined(typeof(ExtensionAttribute)) || delegateFunction.Target != null))
actualNumberOfArguments--;
actualNumberOfParameters--;

if (actualNumberOfArguments > 0 && actualNumberOfArguments != expectedNumberOfParameters)
throw MimicException.WrongCallbackParameterCount(expectedNumberOfParameters, actualNumberOfArguments);
if (actualNumberOfParameters > 0 && actualNumberOfParameters != expectedNumberOfParameters)
throw MimicException.WrongCallbackParameterCount(expectedNumberOfParameters, actualNumberOfParameters);
}

internal static void ValidateDelegateReturnType(this Delegate delegateFunction, Type expectedReturnType)
Expand Down
10 changes: 5 additions & 5 deletions src/Mimic/Exceptions/MimicException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ internal static MimicException WrongCallbackParameterCount(int expectedCount, in
return new MimicException($"Setup on method with {expectedCount} expected parameter(s) cannot invoke a callback method with {actualCount} parameter(s)");
}

internal static MimicException WrongCallbackArgumentTypes(ParameterInfo[] expectedArgumentTypes, ParameterInfo[] actualArgumentTypes)
internal static MimicException WrongCallbackParameterTypes(ParameterInfo[] expectedParameters, ParameterInfo[] actualParameters)
{
string expectedArgumentTypeList = GetArgumentTypeList(expectedArgumentTypes);
string actualArgumentTypesList = GetArgumentTypeList(actualArgumentTypes);
string expectedParameterTypeList = GetParameterTypeList(expectedParameters);
string actualParameterTypeList = GetParameterTypeList(actualParameters);

return new MimicException($"Setup on method with arguments ({expectedArgumentTypeList}) cannot invoke a callback method with the wrong argument types ({actualArgumentTypesList})");
return new MimicException($"Setup on method with parameter(s) ({expectedParameterTypeList}) cannot invoke a callback method with the wrong parameter type(s) ({actualParameterTypeList})");

static string GetArgumentTypeList(ParameterInfo[] parameters)
static string GetParameterTypeList(ParameterInfo[] parameters)
{
var stringBuilder = new ValueStringBuilder(stackalloc char[256]);

Expand Down
6 changes: 3 additions & 3 deletions src/Mimic/Setup/MethodCallSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public void SetCallbackBehaviour(Delegate callbackFunction)
{
callbackFunction.ValidateDelegateParameterCount(MethodInfo.GetParameters().Length);

var expectedArguments = MethodInfo.GetParameters();
if (!callbackFunction.CompareParameterTypesTo(expectedArguments.Select(p => p.ParameterType).ToArray()))
throw MimicException.WrongCallbackArgumentTypes(expectedArguments, callbackFunction.GetMethodInfo().GetParameters());
var expectedParameters = MethodInfo.GetParameters();
if (!callbackFunction.CompareParameterTypesTo(expectedParameters.Select(p => p.ParameterType).ToArray()))
throw MimicException.WrongCallbackParameterTypes(expectedParameters, callbackFunction.GetMethodInfo().GetParameters());

var callbackReturnType = callbackFunction.GetMethodInfo().ReturnType;
if (callbackReturnType != typeof(void))
Expand Down

0 comments on commit 2822047

Please sign in to comment.