Skip to content

Commit 2ed1869

Browse files
authored
Merge pull request #1008 from jgonzalezdr/output-parameters-bug-fix
Bug fix for wrong output parameters being copied
2 parents 52d45fe + ad5942c commit 2ed1869

File tree

5 files changed

+33
-21
lines changed

5 files changed

+33
-21
lines changed

include/CppUTestExt/MockExpectedCallsList.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ class MockExpectedCallsList
4343
virtual int amountOfExpectationsFor(const SimpleString& name) const;
4444
virtual int amountOfUnfulfilledExpectations() const;
4545
virtual bool hasUnfulfilledExpectations() const;
46-
virtual bool hasFulfilledExpectations() const;
47-
virtual bool hasFulfilledExpectationsWithoutIgnoredParameters() const;
4846
virtual bool hasUnfulfilledExpectationsBecauseOfMissingParameters() const;
4947
virtual bool hasExpectationWithName(const SimpleString& name) const;
5048
virtual bool hasCallsOutOfOrder() const;

src/CppUTestExt/MockActualCall.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,17 @@ void MockCheckedActualCall::finalizeOutputParameters(MockCheckedExpectedCall* ex
105105

106106
void MockCheckedActualCall::finalizeCallWhenFulfilled()
107107
{
108-
if (unfulfilledExpectations_.hasFulfilledExpectationsWithoutIgnoredParameters()) {
109-
finalizeOutputParameters(unfulfilledExpectations_.getOneFulfilledExpectationWithIgnoredParameters());
110-
}
108+
// Expectations that don't ignore parameters have higher fulfillment preference than those that ignore parameters
111109

112-
if (unfulfilledExpectations_.hasFulfilledExpectations()) {
113-
fulfilledExpectation_ = unfulfilledExpectations_.removeOneFulfilledExpectation();
110+
fulfilledExpectation_ = unfulfilledExpectations_.removeOneFulfilledExpectation();
111+
if (fulfilledExpectation_) {
112+
finalizeOutputParameters(fulfilledExpectation_);
114113
callHasSucceeded();
114+
} else {
115+
MockCheckedExpectedCall* fulfilledExpectationWithIgnoredParameters = unfulfilledExpectations_.getOneFulfilledExpectationWithIgnoredParameters();
116+
if (fulfilledExpectationWithIgnoredParameters) {
117+
finalizeOutputParameters(fulfilledExpectationWithIgnoredParameters);
118+
}
115119
}
116120
}
117121

src/CppUTestExt/MockExpectedCallsList.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,6 @@ int MockExpectedCallsList::amountOfUnfulfilledExpectations() const
8181
return count;
8282
}
8383

84-
bool MockExpectedCallsList::hasFulfilledExpectations() const
85-
{
86-
return (size() - amountOfUnfulfilledExpectations()) != 0;
87-
}
88-
89-
bool MockExpectedCallsList::hasFulfilledExpectationsWithoutIgnoredParameters() const
90-
{
91-
for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
92-
if (p->expectedCall_->isFulfilledWithoutIgnoredParameters())
93-
return true;
94-
return false;
95-
}
96-
9784
bool MockExpectedCallsList::hasUnfulfilledExpectations() const
9885
{
9986
return amountOfUnfulfilledExpectations() != 0;

tests/CppUTestExt/ExpectedFunctionsListTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ TEST_GROUP(MockExpectedCallsList)
6363
TEST(MockExpectedCallsList, emptyList)
6464
{
6565
CHECK(! list->hasUnfulfilledExpectations());
66-
CHECK(! list->hasFulfilledExpectations());
66+
CHECK(list->size() == list->amountOfUnfulfilledExpectations());
6767
LONGS_EQUAL(0, list->size());
6868
}
6969

tests/CppUTestExt/MockParameterTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,29 @@ TEST(MockParameterTest, outputParameterWithIgnoredParameters)
678678
mock().actualCall("foo").withOutputParameter("bar", &retval).withParameter("other", 1);
679679

680680
LONGS_EQUAL(param, retval);
681+
682+
mock().checkExpectations();
683+
}
684+
685+
/*
686+
* This test checks that the proper output parameters are copied when multiple calls to the same
687+
* function are expected.
688+
*/
689+
TEST(MockParameterTest, properOutputParametersAreCopied)
690+
{
691+
int expectedValue1 = 1;
692+
int expectedValue2 = 2;
693+
mock().expectOneCall("foo").withOutputParameterReturning("param", &expectedValue1, sizeof(expectedValue1)).ignoreOtherParameters();
694+
mock().expectOneCall("foo").withOutputParameterReturning("param", &expectedValue2, sizeof(expectedValue2));
695+
696+
int returnedValue1 = 0;
697+
int returnedValue2 = 0;
698+
mock().actualCall("foo").withOutputParameter("param", &returnedValue1);
699+
mock().actualCall("foo").withOutputParameter("param", &returnedValue2).withParameter("optional", 50);
700+
701+
CHECK_EQUAL_TEXT(expectedValue2, returnedValue1, "Wrong output value in 1st call");
702+
CHECK_EQUAL_TEXT(expectedValue1, returnedValue2, "Wrong output value in 2nd call");
703+
681704
mock().checkExpectations();
682705
}
683706

0 commit comments

Comments
 (0)