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

Stop searching for setup when first match found #1004

Merged
merged 2 commits into from
Apr 21, 2020
Merged
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
19 changes: 3 additions & 16 deletions src/Moq/SetupCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ public Setup FindMatchFor(Invocation invocation)
return null;
}

Setup matchingSetup = null;

lock (this.setups)
{
// Iterating in reverse order because newer setups are more relevant than (i.e. override) older ones
Expand All @@ -138,25 +136,14 @@ public Setup FindMatchFor(Invocation invocation)
var setup = this.setups[i];
if (setup.IsOverridden) continue;

// the following conditions are repetitive, but were written that way to avoid
// unnecessary expensive calls to `setup.Matches`; cheap tests are run first.
if (matchingSetup == null && setup.Matches(invocation))
{
matchingSetup = setup;
if (setup.Method == invocation.Method)
{
break;
}
}
else if (setup.Method == invocation.Method && setup.Matches(invocation))
if (setup.Matches(invocation))
{
matchingSetup = setup;
break;
return setup;
}
}
}

return matchingSetup;
return null;
}

public IEnumerable<Setup> GetInnerMockSetups()
Expand Down
2 changes: 1 addition & 1 deletion tests/Moq.Tests/ItIsAnyTypeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class ItIsAnyTypeFixture
public void Setup_without_It_IsAnyType()
{
var mock = new Mock<IX>();
mock.Setup(x => x.Method<object>());
mock.Setup(x => x.Method<bool>());
mock.Setup(x => x.Method<int>());
mock.Setup(x => x.Method<object>());
mock.Setup(x => x.Method<string>());

mock.Object.Method<bool>();
Expand Down