-
-
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 1 commit
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 |
|---|---|---|
|
|
@@ -12,37 +12,18 @@ namespace Mockolate; | |
| public partial class MockRegistration | ||
| { | ||
| /// <summary> | ||
| /// Counts the invocations of method <paramref name="methodName" /> with matching <paramref name="parameters" /> on the | ||
| /// <paramref name="subject" />. | ||
| /// Counts the invocations of methods matching the <paramref name="methodMatch" /> on the <paramref name="subject" />. | ||
| /// </summary> | ||
| public VerificationResult<T> Method<T>(T subject, string methodName, params NamedParameter[] parameters) | ||
| public VerificationResult<T> Method<T>(T subject, IMethodMatch methodMatch) | ||
| => new( | ||
|
Comment on lines
14
to
18
|
||
| subject, | ||
| Interactions, | ||
| Interactions.Interactions | ||
| .OfType<MethodInvocation>() | ||
| .Where(method => method.Name.Equals(methodName) && | ||
| method.Parameters.Length == parameters.Length && | ||
| !parameters | ||
| .Where((parameter, i) => !parameter.Matches(method.Parameters[i])) | ||
| .Any()) | ||
| .Where(methodMatch.Matches) | ||
| .Cast<IInteraction>() | ||
| .ToArray(), | ||
| $"invoked method {methodName.SubstringAfterLast('.')}({string.Join(", ", parameters.Select(x => x.Parameter.ToString()))})"); | ||
|
|
||
| /// <summary> | ||
| /// Counts the invocations of method <paramref name="methodName" /> with matching <paramref name="parameters" /> on the | ||
| /// <paramref name="subject" />. | ||
| /// </summary> | ||
| public VerificationResult<T> Method<T>(T subject, string methodName, IParameters parameters) => new(subject, | ||
| Interactions, | ||
| Interactions.Interactions | ||
| .OfType<MethodInvocation>() | ||
| .Where(method => method.Name.Equals(methodName) && | ||
| parameters.Matches(method.Parameters)) | ||
| .Cast<IInteraction>() | ||
| .ToArray(), | ||
| $"invoked method {methodName.SubstringAfterLast('.')}({parameters})"); | ||
| $"invoked method {methodMatch}"); | ||
|
vbreuss marked this conversation as resolved.
|
||
|
|
||
| /// <summary> | ||
| /// Counts the getter accesses of property <paramref name="propertyName" /> on the <paramref name="subject" />. | ||
|
|
||
| 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 method) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| => method.Name.Equals(methodName) && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method.Parameters.Length == parameters.Length && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .Where((parameter, i) => !parameter.Matches(method.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 method) | ||
| => method.Name.Equals(methodName) && | ||
| parameters.Matches(method.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() | ||
|
|
@@ -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> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.