CppUTest
MockSupport.h
1 /*
2  * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the <organization> nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef D_MockSupport_h
29 #define D_MockSupport_h
30 
31 #include "CppUTestExt/MockFailure.h"
32 #include "CppUTestExt/MockFunctionCall.h"
33 #include "CppUTestExt/MockExpectedFunctionCall.h"
34 #include "CppUTestExt/MockExpectedFunctionsList.h"
35 
36 class UtestShell;
37 class MockSupport;
38 
39 /* This allows access to "the global" mocking support for easier testing */
40 MockSupport& mock(const SimpleString& mockName = "");
41 
43 {
44 public:
45  MockSupport();
46  virtual ~MockSupport();
47 
48  virtual void strictOrder();
49  virtual MockFunctionCall& expectOneCall(const SimpleString& functionName);
50  virtual MockFunctionCall& expectNCalls(int amount, const SimpleString& functionName);
51  virtual MockFunctionCall& actualCall(const SimpleString& functionName);
52  virtual bool hasReturnValue();
53  virtual MockNamedValue returnValue();
54  virtual int intReturnValue();
55  virtual const char* stringReturnValue();
56  virtual double doubleReturnValue();
57  virtual void* pointerReturnValue();
58 
59  bool hasData(const SimpleString& name);
60  void setData(const SimpleString& name, int value);
61  void setData(const SimpleString& name, const char* value);
62  void setData(const SimpleString& name, double value);
63  void setData(const SimpleString& name, void* value);
64  void setDataObject(const SimpleString& name, const SimpleString& type, void* value);
65  MockNamedValue getData(const SimpleString& name);
66 
67  MockSupport* getMockSupportScope(const SimpleString& name);
68 
69  const char* getTraceOutput();
70  /*
71  * The following functions are recursively through the lower MockSupports scopes
72  * This means, if you do mock().disable() it will disable *all* mocking scopes, including mock("myScope").
73  */
74 
75  virtual void disable();
76  virtual void enable();
77  virtual void tracing(bool enabled);
78  virtual void ignoreOtherCalls();
79 
80  virtual void checkExpectations();
81  virtual bool expectedCallsLeft();
82 
83  virtual void clear();
84  virtual void setMockFailureReporter(MockFailureReporter* reporter);
85  virtual void crashOnFailure();
86 
87  virtual void installComparator(const SimpleString& typeName, MockNamedValueComparator& comparator);
88  virtual void installComparators(const MockNamedValueComparatorRepository& repository);
89  virtual void removeAllComparators();
90 
91 protected:
92  MockSupport* clone();
93  virtual MockActualFunctionCall *createActualFunctionCall();
94  virtual void failTest(MockFailure& failure);
95 private:
96  static int callOrder_;
97  static int expectedCallOrder_;
98  bool strictOrdering_;
99  MockFailureReporter *reporter_;
100  MockFailureReporter defaultReporter_;
101  MockExpectedFunctionsList expectations_;
102  bool ignoreOtherCalls_;
103  bool enabled_;
104  MockActualFunctionCall *lastActualFunctionCall_;
105  MockFunctionCallComposite compositeCalls_;
106  MockNamedValueComparatorRepository comparatorRepository_;
107  MockNamedValueList data_;
108 
109  bool tracing_;
110 
111  void checkExpectationsOfLastCall();
112  bool wasLastCallFulfilled();
113  void failTestWithUnexpectedCalls();
114  void failTestWithOutOfOrderCalls();
115 
116  MockNamedValue* retrieveDataFromStore(const SimpleString& name);
117 
118  MockSupport* getMockSupport(MockNamedValueListNode* node);
119 };
120 
121 #endif
122 
Definition: MockNamedValue.h:109
Definition: SimpleString.h:46
Definition: Utest.h:61
Definition: MockExpectedFunctionsList.h:34
Definition: MockNamedValue.h:35
Definition: MockFailure.h:54
Definition: MockNamedValue.h:149
Definition: MockFunctionCall.h:76
Definition: MockSupport.h:42
Definition: MockFunctionCall.h:40
Definition: MockNamedValue.h:69
Definition: MockActualFunctionCall.h:38
Definition: MockNamedValue.h:127
Definition: MockFailure.h:39