-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: allow verifying method setup calls #493
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using Mockolate.Interactions; | ||
|
|
||
| namespace Mockolate.Setup; | ||
|
|
||
| /// <summary> | ||
| /// A method match to verify method invocations from a setup. | ||
| /// </summary> | ||
| public interface IMethodMatch | ||
| { | ||
| /// <summary> | ||
| /// Checks if the <paramref name="methodInvocation" /> matches. | ||
| /// </summary> | ||
| bool Matches(MethodInvocation methodInvocation); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Linq; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using Mockolate.Interactions; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using Mockolate.Internals; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using Mockolate.Parameters; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace Mockolate.Setup; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Matches a method by name and parameters. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <remarks> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// During verification, the <paramref name="methodName" /> is compared to the method name of the method invocation, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// and the <paramref name="parameters" /> are matched one by one against the corresponding parameter in the method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// invocation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// </remarks> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public readonly struct MethodParameterMatch(string methodName, NamedParameter[] parameters) : IMethodMatch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <inheritdoc cref="IMethodMatch.Matches(MethodInvocation)" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public bool Matches(MethodInvocation methodInvocation) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| => methodInvocation.Name.Equals(methodName) && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| methodInvocation.Parameters.Length == parameters.Length && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .Where((parameter, i) => !parameter.Matches(methodInvocation.Parameters[i])) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .Any(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+20
to
+25
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| => methodInvocation.Name.Equals(methodName) && | |
| methodInvocation.Parameters.Length == parameters.Length && | |
| !parameters | |
| .Where((parameter, i) => !parameter.Matches(methodInvocation.Parameters[i])) | |
| .Any(); | |
| { | |
| if (!methodInvocation.Name.Equals(methodName)) | |
| { | |
| return false; | |
| } | |
| if (methodInvocation.Parameters.Length != parameters.Length) | |
| { | |
| return false; | |
| } | |
| for (int index = 0; index < parameters.Length; index++) | |
| { | |
| if (!parameters[index].Matches(methodInvocation.Parameters[index])) | |
| { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using Mockolate.Interactions; | ||
| using Mockolate.Internals; | ||
| using Mockolate.Parameters; | ||
|
|
||
| namespace Mockolate.Setup; | ||
|
|
||
| /// <summary> | ||
| /// Matches a method by name and parameters. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// During verification, the <paramref name="methodName" /> is compared to the method name of the method invocation, | ||
| /// and the <paramref name="parameters" /> are matched against the parameters in the method invocation. | ||
| /// </remarks> | ||
| public readonly struct MethodParametersMatch(string methodName, IParameters parameters) : IMethodMatch | ||
| { | ||
| /// <inheritdoc cref="IMethodMatch.Matches(MethodInvocation)" /> | ||
| public bool Matches(MethodInvocation methodInvocation) | ||
| => methodInvocation.Name.Equals(methodName) && | ||
| parameters.Matches(methodInvocation.Parameters); | ||
|
|
||
| /// <inheritdoc cref="object.ToString()" /> | ||
| public override string ToString() | ||
| => $"{methodName.SubstringAfterLast('.')}({parameters})"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ namespace Mockolate.Setup; | |
| /// <summary> | ||
| /// Base class for method setups. | ||
| /// </summary> | ||
| public abstract class MethodSetup : IInteractiveMethodSetup | ||
| public abstract class MethodSetup(IMethodMatch methodMatch) : IInteractiveMethodSetup, IVerifiableMethodSetup | ||
| { | ||
|
vbreuss marked this conversation as resolved.
|
||
| /// <inheritdoc cref="IInteractiveMethodSetup.HasReturnCalls()" /> | ||
| bool IInteractiveMethodSetup.HasReturnCalls() | ||
|
|
@@ -25,7 +25,7 @@ T IInteractiveMethodSetup.SetRefParameter<T>(string parameterName, T value, Mock | |
|
|
||
| /// <inheritdoc cref="IInteractiveMethodSetup.Matches(MethodInvocation)" /> | ||
| bool IInteractiveMethodSetup.Matches(MethodInvocation methodInvocation) | ||
| => IsMatch(methodInvocation); | ||
| => methodMatch.Matches(methodInvocation); | ||
|
|
||
| /// <inheritdoc cref="IInteractiveMethodSetup.SkipBaseClass()" /> | ||
| bool? IInteractiveMethodSetup.SkipBaseClass() | ||
|
|
@@ -48,6 +48,10 @@ void IInteractiveMethodSetup.Invoke(MethodInvocation methodInvocation, MockBehav | |
| public void TriggerCallbacks(object?[] parameters) | ||
| => TriggerParameterCallbacks(parameters); | ||
|
|
||
| /// <inheritdoc cref="IVerifiableMethodSetup.GetMatch()" /> | ||
| public IMethodMatch GetMatch() | ||
| => methodMatch; | ||
|
Comment on lines
+51
to
+53
|
||
|
|
||
| /// <summary> | ||
| /// Gets the flag indicating if the base class implementation should be skipped. | ||
| /// </summary> | ||
|
|
@@ -89,11 +93,6 @@ public void TriggerCallbacks(object?[] parameters) | |
| protected abstract TResult GetReturnValue<TResult>(MethodInvocation invocation, MockBehavior behavior, | ||
| Func<TResult> defaultValueGenerator); | ||
|
|
||
| /// <summary> | ||
| /// Checks if the <paramref name="invocation" /> matches the setup. | ||
| /// </summary> | ||
| protected abstract bool IsMatch(MethodInvocation invocation); | ||
|
|
||
| /// <summary> | ||
| /// Triggers any configured parameter callbacks for the method setup with the specified <paramref name="parameters" />. | ||
| /// </summary> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MockRegistration.Method<T>is a public API, and (per the API snapshot updates) the overloads that accept(string methodName, params NamedParameter[])/(string methodName, IParameters)were removed in favor ofIMethodMatch. Consider keeping the old overloads (possibly[Obsolete]) and delegating them to the new overload to avoid a breaking change for consumers usingMockRegistrationdirectly.