Skip to content

Commit

Permalink
Merge pull request #247 from eranpeer/any-verif-specifier
Browse files Browse the repository at this point in the history
Add Any verification specifier
  • Loading branch information
FranckRJ committed Jul 6, 2021
2 parents 80a446b + 0bb1d6e commit ae06073
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
22 changes: 17 additions & 5 deletions include/fakeit/SequenceVerificationExpectation.hpp
Expand Up @@ -27,6 +27,10 @@ namespace fakeit {
_expectedCount = count;
}

void expectAnything() {
_expectAnything = true;
}

void setFileInfo(const char * file, int line, const char * callingMethod) {
_file = file;
_line = line;
Expand All @@ -39,6 +43,7 @@ namespace fakeit {
InvocationsSourceProxy _involvedInvocationSources;
std::vector<Sequence *> _expectedPattern;
int _expectedCount;
bool _expectAnything;

const char * _file;
int _line;
Expand All @@ -53,6 +58,7 @@ namespace fakeit {
_involvedInvocationSources(mocks),
_expectedPattern(expectedPattern), //
_expectedCount(-1), // AT_LEAST_ONCE
_expectAnything(false),
_line(0),
_isVerified(false) {
}
Expand All @@ -66,12 +72,14 @@ namespace fakeit {
MatchAnalysis ma;
ma.run(_involvedInvocationSources, _expectedPattern);

if (isAtLeastVerification() && atLeastLimitNotReached(ma.count)) {
return handleAtLeastVerificationEvent(verificationErrorHandler, ma.actualSequence, ma.count);
}
if (isNotAnythingVerification()) {
if (isAtLeastVerification() && atLeastLimitNotReached(ma.count)) {
return handleAtLeastVerificationEvent(verificationErrorHandler, ma.actualSequence, ma.count);
}

if (isExactVerification() && exactLimitNotMatched(ma.count)) {
return handleExactVerificationEvent(verificationErrorHandler, ma.actualSequence, ma.count);
if (isExactVerification() && exactLimitNotMatched(ma.count)) {
return handleExactVerificationEvent(verificationErrorHandler, ma.actualSequence, ma.count);
}
}

markAsVerified(ma.matchedInvocations);
Expand All @@ -95,6 +103,10 @@ namespace fakeit {
}
}

bool isNotAnythingVerification() {
return !_expectAnything;
}

bool isAtLeastVerification() {
// negative number represents an "AtLeast" search;
return _expectedCount < 0;
Expand Down
5 changes: 5 additions & 0 deletions include/fakeit/SequenceVerificationProgress.hpp
Expand Up @@ -70,6 +70,11 @@ namespace fakeit {

bool operator!() const { return !Terminator(_expectationPtr); }

Terminator Any() {
_expectationPtr->expectAnything();
return Terminator(_expectationPtr);
}

Terminator Never() {
Exactly(0);
return Terminator(_expectationPtr);
Expand Down
35 changes: 34 additions & 1 deletion tests/verification_tests.cpp
Expand Up @@ -64,7 +64,10 @@ struct BasicVerification: tpunit::TestFixture {
TEST(BasicVerification::verify_after_paramter_was_changed_with_argument_matcher), //
TEST(BasicVerification::verify_no_invocations),
TEST(BasicVerification::verify_after_paramter_was_changed_with_Using), //
TEST(BasicVerification::verificationShouldTolerateNullString))
TEST(BasicVerification::verificationShouldTolerateNullString),
TEST(BasicVerification::verify_any_for_no_invocations),
TEST(BasicVerification::verify_any_for_all_invocations),
TEST(BasicVerification::verify_any_for_only_some_invocations))
{
}

Expand Down Expand Up @@ -594,4 +597,34 @@ struct BasicVerification: tpunit::TestFixture {
ASSERT_THROW(Verify(Method(mock, eatConstChar)).Exactly(3), fakeit::VerificationException);
}

void verify_any_for_no_invocations()
{
Mock<SomeInterface> mock;
Verify(Method(mock, func)).Any();
VerifyNoOtherInvocations(mock);
}

void verify_any_for_all_invocations()
{
Mock<SomeInterface> mock;
Fake(Method(mock,func), Method(mock,proc));
SomeInterface &i = mock.get();
i.func(5);
i.proc(10);
Verify(Method(mock, func)).Any();
Verify(Method(mock, proc)).Any();
VerifyNoOtherInvocations(mock);
}

void verify_any_for_only_some_invocations()
{
Mock<SomeInterface> mock;
Fake(Method(mock,func), Method(mock,proc));
SomeInterface &i = mock.get();
i.func(5);
i.proc(10);
Verify(Method(mock, func)).Any();
ASSERT_THROW(VerifyNoOtherInvocations(mock), fakeit::VerificationException);
}

} __BasicVerification;

0 comments on commit ae06073

Please sign in to comment.