NetBurner 3.1
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/MockCheckedActualCall.h"
33 #include "CppUTestExt/MockCheckedExpectedCall.h"
34 #include "CppUTestExt/MockExpectedCallsList.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 = "", MockFailureReporter* failureReporterForThisCall = NULL);
41 
42 class MockSupport
43 {
44 public:
45  MockSupport(const SimpleString& mockName = "");
46  virtual ~MockSupport();
47 
48  virtual void strictOrder();
49  virtual MockExpectedCall& expectOneCall(const SimpleString& functionName);
50  virtual void expectNoCall(const SimpleString& functionName);
51  virtual MockExpectedCall& expectNCalls(unsigned int amount, const SimpleString& functionName);
52  virtual MockActualCall& actualCall(const SimpleString& functionName);
53  virtual bool hasReturnValue();
54  virtual MockNamedValue returnValue();
55  virtual bool boolReturnValue();
56  virtual bool returnBoolValueOrDefault(bool defaultValue);
57  virtual int intReturnValue();
58  virtual int returnIntValueOrDefault(int defaultValue);
59  virtual unsigned int unsignedIntReturnValue();
60  virtual long int longIntReturnValue();
61  virtual long int returnLongIntValueOrDefault(long int defaultValue);
62  virtual unsigned long int unsignedLongIntReturnValue();
63  virtual unsigned long int returnUnsignedLongIntValueOrDefault(unsigned long int defaultValue);
64  virtual unsigned int returnUnsignedIntValueOrDefault(unsigned int defaultValue);
65  virtual const char* stringReturnValue();
66  virtual const char* returnStringValueOrDefault(const char * defaultValue);
67  virtual double returnDoubleValueOrDefault(double defaultValue);
68  virtual double doubleReturnValue();
69  virtual void* pointerReturnValue();
70  virtual void* returnPointerValueOrDefault(void * defaultValue);
71  virtual const void* returnConstPointerValueOrDefault(const void * defaultValue);
72  virtual const void* constPointerReturnValue();
73  virtual void (*returnFunctionPointerValueOrDefault(void (*defaultValue)()))();
74  virtual void (*functionPointerReturnValue())();
75 
76  bool hasData(const SimpleString& name);
77  void setData(const SimpleString& name, bool value);
78  void setData(const SimpleString& name, int value);
79  void setData(const SimpleString& name, unsigned int value);
80  void setData(const SimpleString& name, const char* value);
81  void setData(const SimpleString& name, double value);
82  void setData(const SimpleString& name, void* value);
83  void setData(const SimpleString& name, const void* value);
84  void setData(const SimpleString& name, void (*value)());
85  void setDataObject(const SimpleString& name, const SimpleString& type, void* value);
86  MockNamedValue getData(const SimpleString& name);
87 
88  MockSupport* getMockSupportScope(const SimpleString& name);
89 
90  const char* getTraceOutput();
91  /*
92  * The following functions are recursively through the lower MockSupports scopes
93  * This means, if you do mock().disable() it will disable *all* mocking scopes, including mock("myScope").
94  */
95 
96  virtual void disable();
97  virtual void enable();
98  virtual void tracing(bool enabled);
99  virtual void ignoreOtherCalls();
100 
101  virtual void checkExpectations();
102  virtual bool expectedCallsLeft();
103 
104  virtual void clear();
105  virtual void crashOnFailure(bool shouldFail = true);
106 
107  /*
108  * Each mock() call will set the activeReporter to standard, unless a special reporter is passed for this call.
109  */
110 
111  virtual void setMockFailureStandardReporter(MockFailureReporter* reporter);
112  virtual void setActiveReporter(MockFailureReporter* activeReporter);
113  virtual void setDefaultComparatorsAndCopiersRepository();
114 
115  virtual void installComparator(const SimpleString& typeName, MockNamedValueComparator& comparator);
116  virtual void installCopier(const SimpleString& typeName, MockNamedValueCopier& copier);
117  virtual void installComparatorsAndCopiers(const MockNamedValueComparatorsAndCopiersRepository& repository);
118  virtual void removeAllComparatorsAndCopiers();
119 
120 protected:
121  MockSupport* clone(const SimpleString& mockName);
122  virtual MockCheckedActualCall *createActualCall();
123  virtual void failTest(MockFailure& failure);
124  void countCheck();
125 
126 private:
127  unsigned int actualCallOrder_;
128  unsigned int expectedCallOrder_;
129  bool strictOrdering_;
130  MockFailureReporter *activeReporter_;
131  MockFailureReporter *standardReporter_;
132  MockFailureReporter defaultReporter_;
133  MockExpectedCallsList expectations_;
134  MockExpectedCallsList unExpectations_;
135  bool ignoreOtherCalls_;
136  bool enabled_;
137  MockCheckedActualCall *lastActualFunctionCall_;
138  MockExpectedCallComposite compositeCalls_;
139  MockNamedValueComparatorsAndCopiersRepository comparatorsAndCopiersRepository_;
140  MockNamedValueList data_;
141  const SimpleString mockName_;
142 
143  bool tracing_;
144 
145  void checkExpectationsOfLastActualCall();
146  bool wasLastActualCallFulfilled();
147  void failTestWithUnexpectedCalls();
148  void failTestWithOutOfOrderCalls();
149 
150  MockNamedValue* retrieveDataFromStore(const SimpleString& name);
151 
152  MockSupport* getMockSupport(MockNamedValueListNode* node);
153 
154  bool hasntExpectationWithName(const SimpleString& functionName);
155  bool hasntUnexpectationWithName(const SimpleString& functionName);
156  bool hasCallsOutOfOrder();
157 
158  SimpleString appendScopeToName(const SimpleString& functionName);
159 
160 };
161 
162 #endif
163 
#define NULL
Definition: nm_bsp.h:76