Skip to content

Expected calls matching several actual calls #1018

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

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
dae0892
Preparation of mock library in order to add support for expected call…
jgonzalezdr Jun 21, 2016
ed56f39
Basic implementation of "multi-matching" expectedCalls.
jgonzalezdr Jun 26, 2016
8ee564a
Added C language new calls for multi-matching expected calls.
jgonzalezdr Jun 26, 2016
0d2a162
Removed MockExpectedCallComposite.
jgonzalezdr Jun 26, 2016
e94cd93
Added test that checks that non-fulfilled calls have higher matching …
jgonzalezdr Jun 27, 2016
f42377e
When creating the list of potentially matching expected calls, non-fu…
jgonzalezdr Jun 27, 2016
162d117
Renamed MockExpectedCallsDidntHappenFailure to MockExpectedCallsNotFu…
jgonzalezdr Jun 27, 2016
94c5c69
Re-enabled strict ordering checks when the expected calls have no opt…
jgonzalezdr Jun 27, 2016
84593a9
Fixed declaration of MockExpectedCallsListForTest::addFunction to avo…
jgonzalezdr Jun 27, 2016
49f3b7d
Fixed declaration of call order related methods and attributes to use…
jgonzalezdr Jun 27, 2016
60828dc
Updated some unit tests and added a new one to improve code coverage.
jgonzalezdr Jun 28, 2016
a12e83b
Modified MockSupport::expectNoCall() method implementation to use a m…
jgonzalezdr Jun 28, 2016
6b5db95
Removed superfluous constructor and method in MockExpectedCall that w…
jgonzalezdr Jun 28, 2016
412ca5e
Fixed the mock scope not being properly reported when failing because…
jgonzalezdr Jun 30, 2016
a80efda
Added a class to store a queue of actual calls, needed to add a log o…
jgonzalezdr Jul 3, 2016
0f4623a
Modified MockCheckedActualCall to store a reference to its associated…
jgonzalezdr Jul 3, 2016
bc33181
Added functionality to MockCheckedActualCall and MockActualCallsQueue…
jgonzalezdr Jul 3, 2016
044a693
Added actual calls reporting to MockFailure, and preliminary non-func…
jgonzalezdr Jul 4, 2016
b3a20a4
Fixed compilation for Dos platform.
jgonzalezdr Jul 4, 2016
eb3aae1
Implemented a log of actual calls in MockSupport, such that mock fail…
jgonzalezdr Jul 5, 2016
fb5653f
Added functionality to MockSupport to limit the maximum size of the a…
jgonzalezdr Jul 5, 2016
3999548
Added C language new call for setMaxCallLogSize.
jgonzalezdr Jul 5, 2016
faf07f9
Added unit test for MockActualCallsList::hasFinalizedMatchingExpectat…
jgonzalezdr Jul 5, 2016
e912650
Improved unit test for MockSupport::setMaxCallLogSize().
jgonzalezdr Jul 7, 2016
111f0f0
Improved unit tests for MockExpectedCall.
jgonzalezdr Jul 7, 2016
ef47ef7
Removed old commented code that was left over.
jgonzalezdr Jul 7, 2016
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
8 changes: 8 additions & 0 deletions CppUTest.dsp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions CppUTest.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,28 @@
/>
</FileConfiguration>
</File>
<File
RelativePath="src\CppUTestExt\MockActualCallsQueue.cpp"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
AdditionalIncludeDirectories=""
PreprocessorDefinitions=""
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
AdditionalIncludeDirectories=""
PreprocessorDefinitions=""
/>
</FileConfiguration>
</File>
<File
RelativePath="src\CppUTestExt\MockExpectedCall.cpp"
>
Expand Down Expand Up @@ -927,6 +949,10 @@
RelativePath=".\include\CppUTestExt\MockActualCall.h"
>
</File>
<File
RelativePath="include\CppUTestExt\MockActualCallsQueue.h"
>
</File>
<File
RelativePath="include\CppUTestExt\MockCheckedActualCall.h"
>
Expand Down
2 changes: 2 additions & 0 deletions CppUTest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<ClCompile Include="src\CppUTestExt\MemoryReporterPlugin.cpp" />
<ClCompile Include="src\CppUTestExt\MemoryReportFormatter.cpp" />
<ClCompile Include="src\CppUTestExt\MockActualCall.cpp" />
<ClCompile Include="src\CppUTestExt\MockActualCallsQueue.cpp" />
<ClCompile Include="src\CppUTestExt\MockExpectedCall.cpp" />
<ClCompile Include="src\CppUTestExt\MockExpectedCallsList.cpp" />
<ClCompile Include="src\CppUTestExt\MockFailure.cpp" />
Expand Down Expand Up @@ -159,6 +160,7 @@
<ClInclude Include="include\CppUTestExt\MemoryReporterPlugin.h" />
<ClInclude Include="include\CppUTestExt\MemoryReportFormatter.h" />
<ClInclude Include="include\CppUTestExt\MockActualCall.h" />
<ClInclude Include="include\CppUTestExt\MockActualCallsQueue.h" />
<ClInclude Include="include\CppUTestExt\MockCheckedActualCall.h" />
<ClInclude Include="include\CppUTestExt\MockCheckedExpectedCall.h" />
<ClInclude Include="include\CppUTestExt\MockExpectedCall.h" />
Expand Down
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ lib_libCppUTestExt_a_SOURCES = \
src/CppUTestExt/MemoryReporterPlugin.cpp \
src/CppUTestExt/MemoryReportFormatter.cpp \
src/CppUTestExt/MockActualCall.cpp \
src/CppUTestExt/MockActualCallsQueue.cpp \
src/CppUTestExt/MockExpectedCall.cpp \
src/CppUTestExt/MockExpectedCallsList.cpp \
src/CppUTestExt/MockFailure.cpp \
Expand All @@ -118,6 +119,7 @@ include_cpputestext_HEADERS = \
include/CppUTestExt/MockActualCall.h \
include/CppUTestExt/MockCheckedActualCall.h \
include/CppUTestExt/MockCheckedExpectedCall.h \
include/CppUTestExt/MockActualCallsQueue.h \
include/CppUTestExt/MockExpectedCall.h \
include/CppUTestExt/MockExpectedCallsList.h \
include/CppUTestExt/MockFailure.h \
Expand Down Expand Up @@ -192,6 +194,7 @@ CppUTestExtTests_SOURCES = \
tests/CppUTestExt/MockComparatorCopierTest.cpp \
tests/CppUTestExt/MockExpectedCallTest.cpp \
tests/CppUTestExt/ExpectedFunctionsListTest.cpp \
tests/CppUTestExt/ActualCallsQueueTest.cpp \
tests/CppUTestExt/MockFailureReporterForTest.cpp \
tests/CppUTestExt/MockFailureTest.cpp \
tests/CppUTestExt/MockHierarchyTest.cpp \
Expand Down
5 changes: 3 additions & 2 deletions include/CppUTest/SimpleString.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ SimpleString BracketsFormattedHexStringFrom(cpputest_longlong value);
SimpleString BracketsFormattedHexStringFrom(cpputest_ulonglong value);
SimpleString BracketsFormattedHexStringFrom(signed char value);
SimpleString BracketsFormattedHexString(SimpleString hexString);


SimpleString AppendStringOnANewLine(const SimpleString& inputString, const SimpleString& stringToAppend,
const SimpleString& linePrefix = "");
SimpleString ReplaceWithTextNoneWhenEmpty(const SimpleString& inputString, const SimpleString& linePrefix);

#if CPPUTEST_USE_STD_CPP_LIB

Expand Down
1 change: 0 additions & 1 deletion include/CppUTestExt/MockActualCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class MockActualCall
virtual ~MockActualCall();

virtual MockActualCall& withName(const SimpleString& name)=0;
virtual MockActualCall& withCallOrder(int callOrder)=0;
MockActualCall& withParameter(const SimpleString& name, bool value) { return withBoolParameter(name, value); }
MockActualCall& withParameter(const SimpleString& name, int value) { return withIntParameter(name, value); }
MockActualCall& withParameter(const SimpleString& name, unsigned int value) { return withUnsignedIntParameter(name, value); }
Expand Down
170 changes: 170 additions & 0 deletions include/CppUTestExt/MockActualCallsQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Copyright (c) 2016, Jesus Gonzalez
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef D_MockActualCallsQueue_h
#define D_MockActualCallsQueue_h

class MockCheckedActualCall;
class SimpleString;

/**
* This class implements a FIFO queue of MockCheckedActualCall calls.
*
* @note The queue can be created as owner of its stored calls or not. If the queue is the owner, then the ownership of the calls added
* to the queue is transferred to the queue, and therefore it's responsible for managing their life cycle (i.e. deleting them).
*
* @note The queue has a size limit. When the queue reaches its maximum size, adding new calls on the back will remove calls from the front.
*/
class MockActualCallsQueue
{

public:
/**
* Constructor.
*
* Creates an empty queue.
*
* @param owner [in] Indicates if the ownership of the calls added to the queue shall be transferred to the queue
* @param maxSize [in] Maximum size for the queue
*/
MockActualCallsQueue(bool owner, unsigned int maxSize = (unsigned int) -1);

/**
* Destructor.
*
* Discards all items, and if the queue is the owner of the calls, then deletes their associated calls.
*/
virtual ~MockActualCallsQueue();

/**
* Returns the number of calls stored in the queue.
*
* @return Size of the queue
*/
virtual unsigned int size() const;

/**
* Checks if the queue is empty.
*
* @return @c true if the queue is empty, @c false otherwise
*/
virtual bool isEmpty() const;

/**
* Clears the queue, discarding all items, and if the queue is the owner of its calls, then also deleting their associated calls.
*/
virtual void clear();

/**
* Appends the call to the back of the queue.
*
* @note If the queue is the owner of its calls, then it becomes the owner of the passed object.
*
* @note If the queue is at its size limit, then the first item is discarded, and if the queue is the owner of its calls, then the
* associated call is also deleted.
*
* @param call [in] Pointer to the call to be appended
*/
virtual void pushBack(MockCheckedActualCall* call);

/**
* Removes a call from the front of the queue, and returns it.
*
* @note If the queue is the owner of its calls, then the caller becomes the owner of the returned object.
*
* @return Pointer to the call that was in front of the queue, or NULL if the queue was empty
*/
virtual MockCheckedActualCall* popFront();

/**
* Appends all the calls from @c otherQueue to the back of this queue.
*
* @note If this queue is the owner of calls, then it becomes the owner all the calls from @c otherQueue. This means that either
* this queue shall not be owner of calls, or that it's owner of calls and the calls from @c otherQueue do not have an owner
* yet (i.e. @c otherQueue is not owner of calls).
*
* @param otherQueue [in] Another queue
*/
virtual void pushBackAll(const MockActualCallsQueue& otherQueue);

/**
* Gets the maximum size for the queue.
* @return Maximum size
*/
virtual unsigned int getMaxSize();

/**
* Sets the maximum size for the queue.
*
* @note If the current size is greater than @c maxSize, then calls are removed in order from the front of the queue until the
* current size is equal to the maximum size.
*
* @param maxSize [in] Maximum size
*/
virtual void setMaxSize(unsigned int maxSize);

/**
* Returns a string representation of the queue as a list with the description of all its stored calls, each on a new line.
*
* @param linePrefix String to be added before each individual call description
* @return String representation of the queue
*/
virtual SimpleString toString(const SimpleString& linePrefix) const;

/**
* Returns a string representation of the queue as a list with the description of its stored calls for function @c functionName,
* each on a new line.
*
* @param functionName Name of the function to be filtered through
* @param linePrefix String to be added before each individual call description
* @return String representation of the queue
*/
virtual SimpleString toStringFilterByFunction(const SimpleString& functionName, const SimpleString& linePrefix) const;

protected:
class MockActualCallsQueueNode
{
public:
MockCheckedActualCall* call_;

MockActualCallsQueueNode* next_;
MockActualCallsQueueNode(MockCheckedActualCall* call) : call_(call), next_(NULL) {}
};

private:
MockActualCallsQueueNode* head_;
MockActualCallsQueueNode* tail_;

unsigned int currentSize_;
unsigned int maxSize_;

bool owner_;

virtual void deleteFromFront();
};

#endif
32 changes: 21 additions & 11 deletions include/CppUTestExt/MockCheckedActualCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
#include "CppUTestExt/MockActualCall.h"
#include "CppUTestExt/MockExpectedCallsList.h"

class MockSupport;

class MockCheckedActualCall : public MockActualCall
{
public:
MockCheckedActualCall(int callOrder, MockFailureReporter* reporter, const MockExpectedCallsList& expectations);
MockCheckedActualCall(unsigned int callOrder, MockFailureReporter* reporter, const MockSupport& mockSupport);
virtual ~MockCheckedActualCall();

virtual MockActualCall& withName(const SimpleString& name) _override;
virtual MockActualCall& withCallOrder(int) _override;
virtual MockActualCall& withBoolParameter(const SimpleString& name, bool value) _override;
virtual MockActualCall& withIntParameter(const SimpleString& name, int value) _override;
virtual MockActualCall& withUnsignedIntParameter(const SimpleString& name, unsigned int value) _override;
Expand Down Expand Up @@ -95,17 +96,21 @@ class MockCheckedActualCall : public MockActualCall
virtual void checkExpectations();

virtual void setMockFailureReporter(MockFailureReporter* reporter);

virtual SimpleString toString() const;

const SimpleString& getName() const;

protected:
void setName(const SimpleString& name);
SimpleString getName() const;
virtual UtestShell* getTest() const;
virtual void callHasSucceeded();
virtual void finalizeOutputParameters(MockCheckedExpectedCall* call);
virtual void finalizeCallWhenFulfilled();
virtual void finalizeCallWhenMatchIsFound();
virtual void failTest(const MockFailure& failure);
virtual void checkInputParameter(const MockNamedValue& actualParameter);
virtual void checkOutputParameter(const MockNamedValue& outputParameter);
virtual void callIsInProgress();
virtual void discardCurrentlyMatchingExpectations();

enum ActualCallState {
CALL_IN_PROGRESS,
Expand All @@ -114,16 +119,21 @@ class MockCheckedActualCall : public MockActualCall
};
virtual void setState(ActualCallState state);

virtual const SimpleString& getScopeName() const;

void setMatchingExpectedCall(MockCheckedExpectedCall* call);

private:
SimpleString functionName_;
int callOrder_;
unsigned int callOrder_;
MockFailureReporter* reporter_;

ActualCallState state_;
MockCheckedExpectedCall* fulfilledExpectation_;
bool expectationsChecked_;
MockCheckedExpectedCall* matchingExpectation_;

MockExpectedCallsList unfulfilledExpectations_;
const MockExpectedCallsList& allExpectations_;
MockExpectedCallsList potentiallyMatchingExpectations_;
const MockSupport& mockSupport_;

class MockOutputParametersListNode
{
Expand All @@ -150,7 +160,6 @@ class MockActualCallTrace : public MockActualCall
virtual ~MockActualCallTrace();

virtual MockActualCall& withName(const SimpleString& name) _override;
virtual MockActualCall& withCallOrder(int) _override;
virtual MockActualCall& withBoolParameter(const SimpleString& name, bool value) _override;
virtual MockActualCall& withIntParameter(const SimpleString& name, int value) _override;
virtual MockActualCall& withUnsignedIntParameter(const SimpleString& name, unsigned int value) _override;
Expand Down Expand Up @@ -201,6 +210,8 @@ class MockActualCallTrace : public MockActualCall

virtual MockActualCall& onObject(const void* objectPtr) _override;

MockActualCall& withCallOrder(unsigned int callOrder);

const char* getTraceOutput();
void clear();
static MockActualCallTrace& instance();
Expand All @@ -215,7 +226,6 @@ class MockIgnoredActualCall: public MockActualCall
{
public:
virtual MockActualCall& withName(const SimpleString&) _override { return *this;}
virtual MockActualCall& withCallOrder(int) _override { return *this; }
virtual MockActualCall& withBoolParameter(const SimpleString&, bool) _override { return *this; }
virtual MockActualCall& withIntParameter(const SimpleString&, int) _override { return *this; }
virtual MockActualCall& withUnsignedIntParameter(const SimpleString&, unsigned int) _override { return *this; }
Expand Down
Loading