Skip to content

Commit c8ec109

Browse files
committed
Made expectation objects optional.
If onObject() is called on a expected call, it is considered that the expectation matches only the specific object passed; but if it's not called, it's considered that the expectation matches any object. Therefore, when onObject() is called on an actual call, both the expectations that target the passed object and the expectations that do not target a specific object will match.
1 parent f3563d5 commit c8ec109

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

‎include/CppUTestExt/MockCheckedExpectedCall.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class MockCheckedExpectedCall : public MockExpectedCall
135135
MockNamedValueList* outputParameters_;
136136
MockNamedValue returnValue_;
137137
void* objectPtr_;
138+
bool isSpecificObjectExpected_;
138139
bool wasPassedToObject_;
139140
unsigned int actualCalls_;
140141
unsigned int expectedCalls_;

‎src/CppUTestExt/MockActualCall.cpp‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,19 +527,22 @@ MockActualCall& MockCheckedActualCall::onObject(const void* objectPtr)
527527
return *this;
528528
}
529529

530-
setState(CALL_IN_PROGRESS);
531-
discardCurrentlyMatchingExpectations();
530+
// Currently matching expectations are not discarded because the passed object
531+
// is ignored if not specifically set in the expectation
532532

533533
potentiallyMatchingExpectations_.onlyKeepExpectationsOnObject(objectPtr);
534534

535-
if (potentiallyMatchingExpectations_.isEmpty()) {
535+
if ((!matchingExpectation_) && potentiallyMatchingExpectations_.isEmpty()) {
536536
MockUnexpectedObjectFailure failure(getTest(), getName(), objectPtr, allExpectations_);
537537
failTest(failure);
538538
return *this;
539539
}
540540

541541
potentiallyMatchingExpectations_.wasPassedToObject();
542-
completeCallWhenMatchIsFound();
542+
543+
if (!matchingExpectation_) {
544+
completeCallWhenMatchIsFound();
545+
}
543546

544547
return *this;
545548
}

‎src/CppUTestExt/MockExpectedCall.cpp‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ SimpleString MockCheckedExpectedCall::getName() const
5454
MockCheckedExpectedCall::MockCheckedExpectedCall()
5555
: ignoreOtherParameters_(false), isActualCallMatchFinalized_(false),
5656
initialExpectedCallOrder_(NO_EXPECTED_CALL_ORDER), finalExpectedCallOrder_(NO_EXPECTED_CALL_ORDER),
57-
outOfOrder_(false), returnValue_(""), objectPtr_(NULL), wasPassedToObject_(true),
57+
outOfOrder_(false), returnValue_(""), objectPtr_(NULL), isSpecificObjectExpected_(false), wasPassedToObject_(true),
5858
actualCalls_(0), expectedCalls_(1)
5959
{
6060
inputParameters_ = new MockNamedValueList();
@@ -64,7 +64,7 @@ MockCheckedExpectedCall::MockCheckedExpectedCall()
6464
MockCheckedExpectedCall::MockCheckedExpectedCall(unsigned int numCalls)
6565
: ignoreOtherParameters_(false), isActualCallMatchFinalized_(false),
6666
initialExpectedCallOrder_(NO_EXPECTED_CALL_ORDER), finalExpectedCallOrder_(NO_EXPECTED_CALL_ORDER),
67-
outOfOrder_(false), returnValue_(""), objectPtr_(NULL), wasPassedToObject_(true),
67+
outOfOrder_(false), returnValue_(""), objectPtr_(NULL), isSpecificObjectExpected_(false), wasPassedToObject_(true),
6868
actualCalls_(0), expectedCalls_(numCalls)
6969
{
7070
inputParameters_ = new MockNamedValueList();
@@ -291,7 +291,7 @@ void MockCheckedExpectedCall::wasPassedToObject()
291291

292292
void MockCheckedExpectedCall::resetActualCallMatchingState()
293293
{
294-
wasPassedToObject_ = (objectPtr_ == NULL);
294+
wasPassedToObject_ = !isSpecificObjectExpected_;
295295
isActualCallMatchFinalized_ = false;
296296

297297
MockNamedValueListNode* p;
@@ -339,7 +339,7 @@ bool MockCheckedExpectedCall::hasOutputParameter(const MockNamedValue& parameter
339339
SimpleString MockCheckedExpectedCall::callToString()
340340
{
341341
SimpleString str;
342-
if (objectPtr_)
342+
if (isSpecificObjectExpected_)
343343
str = StringFromFormat("(object address: %p)::", objectPtr_);
344344

345345
str += getName();
@@ -409,7 +409,7 @@ bool MockCheckedExpectedCall::relatesTo(const SimpleString& functionName)
409409

410410
bool MockCheckedExpectedCall::relatesToObject(const void* objectPtr) const
411411
{
412-
return objectPtr_ == objectPtr;
412+
return (!isSpecificObjectExpected_) || (objectPtr_ == objectPtr);
413413
}
414414

415415
MockCheckedExpectedCall::MockExpectedFunctionParameter* MockCheckedExpectedCall::item(MockNamedValueListNode* node)
@@ -504,6 +504,7 @@ MockExpectedCall& MockCheckedExpectedCall::andReturnValue(void (*value)())
504504

505505
MockExpectedCall& MockCheckedExpectedCall::onObject(void* objectPtr)
506506
{
507+
isSpecificObjectExpected_ = true;
507508
wasPassedToObject_ = false;
508509
objectPtr_ = objectPtr;
509510
return *this;

‎tests/CppUTestExt/MockCallTest.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,31 @@ TEST(MockCallTest, OnObject)
366366
mock().actualCall("boo").onObject(objectPtr);
367367
}
368368

369+
TEST(MockCallTest, OnObjectIgnored_MatchingAlreadyWhenObjectPassed)
370+
{
371+
void* objectPtr = (void*) 0x001;
372+
mock().expectOneCall("boo");
373+
mock().actualCall("boo").onObject(objectPtr);
374+
}
375+
376+
TEST(MockCallTest, OnObjectIgnored_NotMatchingYetWhenObjectPassed)
377+
{
378+
void* objectPtr = (void*) 0x001;
379+
mock().expectOneCall("boo").withBoolParameter("p", true);
380+
mock().actualCall("boo").onObject(objectPtr).withBoolParameter("p", true);
381+
}
382+
383+
TEST(MockCallTest, OnObjectIgnored_InitialMatchDiscarded)
384+
{
385+
void* objectPtr1 = (void*) 0x001;
386+
void* objectPtr2 = (void*) 0x002;
387+
388+
mock().expectOneCall("boo");
389+
mock().expectOneCall("boo").withBoolParameter("p", true);
390+
mock().actualCall("boo").onObject(objectPtr2).withBoolParameter("p", true);;
391+
mock().actualCall("boo").onObject(objectPtr1);
392+
}
393+
369394
TEST(MockCallTest, OnObjectFails)
370395
{
371396
MockFailureReporterInstaller failureReporterInstaller;

0 commit comments

Comments
 (0)