Debian Codegraph @

tests/CppUTestExt/MockActualCallTest.cpp

raw symbols SCIP
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 #include "CppUTest/TestHarness.h"
29 #include "CppUTestExt/MockCheckedActualCall.h"
30 #include "CppUTestExt/MockCheckedExpectedCall.h"
31 #include "CppUTestExt/MockExpectedCallsList.h"
32 #include "CppUTestExt/MockFailure.h"
33 #include "MockFailureReporterForTest.h"
34
35 TEST_GROUP(MockCheckedActualCall)
36 {
37 MockExpectedCallsList* emptyList;
38 MockExpectedCallsList* list;
39 MockFailureReporter* reporter;
40
41 void setup()
42 {
43 emptyList = new MockExpectedCallsList;
44 list = new MockExpectedCallsList;
45 reporter = MockFailureReporterForTest::getReporter();
46 }
47
48 void teardown()
49 {
50 CHECK_NO_MOCK_FAILURE();
51
52 MockFailureReporterForTest::clearReporter();
53
54 delete emptyList;
55 delete list;
56 }
57 };
58
59 TEST(MockCheckedActualCall, unExpectedCall)
60 {
61 MockCheckedActualCall actualCall(1, reporter, *emptyList);
62 actualCall.withName("unexpected");
63
64 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list);
65 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
66 }
67
68 TEST(MockCheckedActualCall, unExpectedCallWithAParameter)
69 {
70 MockCheckedActualCall actualCall(1, reporter, *emptyList);
71 actualCall.withName("unexpected").withParameter("bar", 0);
72
73 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list);
74 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
75 }
76
77 TEST(MockCheckedActualCall, unExpectedCallWithAnOutputParameter)
78 {
79 MockCheckedActualCall actualCall(1, reporter, *emptyList);
80 actualCall.withName("unexpected").withOutputParameter("bar", NULLPTR);
81
82 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list);
83 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
84 }
85
86 TEST(MockCheckedActualCall, unExpectedCallOnObject)
87 {
88 int object;
89
90 MockCheckedActualCall actualCall(1, reporter, *emptyList);
91 actualCall.withName("unexpected").onObject(&object);
92
93 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list);
94 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
95
96 CHECK(actualCall.hasFailed()); // Checks that onObject() doesn't "reset" call state
97 }
98
99 TEST(MockCheckedActualCall, actualCallWithNoReturnValueAndMeaninglessCallOrderForCoverage)
100 {
101 MockCheckedActualCall actualCall(1, reporter, *emptyList);
102 actualCall.withName("noreturn").withCallOrder(0).returnValue();
103
104 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "noreturn", *list);
105 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
106 }
107
108 TEST(MockCheckedActualCall, unExpectedParameterName)
109 {
110 MockCheckedExpectedCall call1;
111 call1.withName("func");
112 list->addExpectedCall(&call1);
113
114 MockCheckedActualCall actualCall(1, reporter, *list);
115 actualCall.withName("func").withParameter("integer", 1);
116
117 MockNamedValue parameter("integer");
118 parameter.setValue(1);
119
120 MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "func", parameter, *list);
121 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
122 }
123
124 TEST(MockCheckedActualCall, multipleSameFunctionsExpectingAndHappenGradually)
125 {
126 MockCheckedExpectedCall* call1 = new MockCheckedExpectedCall();
127 MockCheckedExpectedCall* call2 = new MockCheckedExpectedCall();
128 call1->withName("func");
129 call2->withName("func");
130 list->addExpectedCall(call1);
131 list->addExpectedCall(call2);
132
133 LONGS_EQUAL(2, list->amountOfUnfulfilledExpectations());
134
135 MockCheckedActualCall actualCall1(1, reporter, *list);
136 actualCall1.withName("func");
137 actualCall1.checkExpectations();
138
139 LONGS_EQUAL(1, list->amountOfUnfulfilledExpectations());
140
141 MockCheckedActualCall actualCall2(2, reporter, *list);
142 actualCall2.withName("func");
143 actualCall2.checkExpectations();
144
145 LONGS_EQUAL(0, list->amountOfUnfulfilledExpectations());
146
147 list->deleteAllExpectationsAndClearList();
148 }
149
150 TEST(MockCheckedActualCall, MockIgnoredActualCallWorksAsItShould)
151 {
152 MockIgnoredActualCall actual;
153 actual.withName("func");
154 actual.withCallOrder(1);
155
156 CHECK(false == actual.returnBoolValue());
157 CHECK(true == actual.returnBoolValueOrDefault(true));
158 CHECK(false == actual.returnBoolValueOrDefault(false));
159 CHECK(0 == actual.returnUnsignedLongIntValue());
160 CHECK(0 == actual.returnIntValue());
161 CHECK(1ul == actual.returnUnsignedLongIntValueOrDefault(1ul));
162 CHECK(1 == actual.returnIntValueOrDefault(1));
163 CHECK(0 == actual.returnLongIntValue());
164 CHECK(1l == actual.returnLongIntValueOrDefault(1l));
165 CHECK(0 == actual.returnUnsignedIntValue());
166 CHECK(1u == actual.returnUnsignedIntValueOrDefault(1u));
167 #ifdef CPPUTEST_USE_LONG_LONG
168 CHECK(0 == actual.returnLongLongIntValue());
169 CHECK(1ll == actual.returnLongLongIntValueOrDefault(1ll));
170 CHECK(0 == actual.returnUnsignedLongLongIntValue());
171 CHECK(1ull == actual.returnUnsignedLongLongIntValueOrDefault(1ull));
172 #endif
173 DOUBLES_EQUAL(0.0, actual.returnDoubleValue(), 0.0);
174 DOUBLES_EQUAL(1.5, actual.returnDoubleValueOrDefault(1.5), 0.0);
175 STRCMP_EQUAL("bla", actual.returnStringValueOrDefault("bla"));
176 STRCMP_EQUAL("", actual.returnStringValue());
177 CHECK(NULLPTR == actual.returnPointerValue());
178 CHECK((void*) 0x2 == actual.returnPointerValueOrDefault((void*) 0x2));
179 CHECK(NULLPTR == actual.returnConstPointerValue());
180 CHECK((const void*) 0x2 == actual.returnConstPointerValueOrDefault((const void*) 0x2));
181 CHECK(NULLPTR == actual.returnFunctionPointerValue());
182 CHECK((void(*)()) 1 == actual.returnFunctionPointerValueOrDefault((void(*)()) 0x1));
183 CHECK_FALSE(actual.hasReturnValue());
184 CHECK(actual.returnValue().equals(MockNamedValue("")));
185 }
186
187 TEST(MockCheckedActualCall, remainderOfMockActualCallTraceWorksAsItShould)
188 {
189 int value;
190 const int const_value = 1;
191 const unsigned char mem_buffer[] = { 0xFE, 0x15 };
192 void (*function_value)() = (void (*)())0xDEAD;
193 MockActualCallTrace actual;
194 actual.withName("func");
195 actual.withCallOrder(1);
196 actual.onObject(&value);
197
198 actual.withBoolParameter("bool", true);
199 actual.withUnsignedIntParameter("unsigned_int", (unsigned int) 1);
200 actual.withUnsignedLongIntParameter("unsigned_long", (unsigned long)1);
201 actual.withLongIntParameter("long_int", (long int) 1);
202 #ifdef CPPUTEST_USE_LONG_LONG
203 actual.withLongLongIntParameter("long_long_int", (long long int) 1);
204 actual.withUnsignedLongLongIntParameter("unsigned_long_long_int", (unsigned long long int) 1);
205 #endif
206 actual.withPointerParameter("pointer", &value);
207 actual.withConstPointerParameter("const_pointer", &const_value);
208 actual.withFunctionPointerParameter("function_pointer", function_value);
209 actual.withMemoryBufferParameter("mem_buffer", mem_buffer, sizeof(mem_buffer));
210 actual.withParameterOfType("int", "named_type", &const_value);
211
212 SimpleString expectedString("\nFunction name:func");
213 expectedString += " withCallOrder:1";
214 expectedString += " onObject:0x";
215 expectedString += HexStringFrom(&value);
216 expectedString += " bool:true";
217 expectedString += " unsigned_int:1 (0x1)";
218 expectedString += " unsigned_long:1 (0x1)";
219 expectedString += " long_int:1 (0x1)";
220 #ifdef CPPUTEST_USE_LONG_LONG
221 expectedString += " long_long_int:1 (0x1)";
222 expectedString += " unsigned_long_long_int:1 (0x1)";
223 #endif
224 expectedString += " pointer:0x";
225 expectedString += HexStringFrom(&value);
226 expectedString += " const_pointer:0x";
227 expectedString += HexStringFrom(&const_value);
228 expectedString += " function_pointer:0x";
229 expectedString += HexStringFrom(function_value);
230 expectedString += " mem_buffer:Size = 2 | HexContents = FE 15";
231 expectedString += " int named_type:0x";
232 expectedString += HexStringFrom(&const_value);
233 STRCMP_EQUAL(expectedString.asCharString(), actual.getTraceOutput());
234
235 CHECK_FALSE(actual.hasReturnValue());
236 CHECK(actual.returnValue().equals(MockNamedValue("")));
237 CHECK(false == actual.returnBoolValue());
238 CHECK(false == actual.returnBoolValueOrDefault(true));
239 CHECK(0 == actual.returnLongIntValue());
240 CHECK(0 == actual.returnUnsignedLongIntValue());
241 CHECK(0 == actual.returnIntValue());
242 CHECK(0 == actual.returnUnsignedLongIntValueOrDefault(1ul));
243 CHECK(0 == actual.returnIntValueOrDefault(1));
244 CHECK(0 == actual.returnLongIntValue());
245 CHECK(0 == actual.returnLongIntValueOrDefault(1l));
246 #ifdef CPPUTEST_USE_LONG_LONG
247 CHECK(0 == actual.returnLongLongIntValue());
248 CHECK(0 == actual.returnLongLongIntValueOrDefault(1ll));
249 CHECK(0 == actual.returnUnsignedLongLongIntValue());
250 CHECK(0 == actual.returnUnsignedLongLongIntValueOrDefault(1ull));
251 #endif
252 CHECK(0 == actual.returnUnsignedIntValue());
253 CHECK(0 == actual.returnUnsignedIntValueOrDefault(1u));
254 DOUBLES_EQUAL(0.0, actual.returnDoubleValue(), 0.0);
255 DOUBLES_EQUAL(0.0, actual.returnDoubleValueOrDefault(1.0), 0.0);
256 STRCMP_EQUAL("", actual.returnStringValueOrDefault("bla"));
257 STRCMP_EQUAL("", actual.returnStringValue());
258 CHECK(NULLPTR == actual.returnPointerValue());
259 CHECK(NULLPTR == actual.returnPointerValueOrDefault((void*) NULLPTR));
260 CHECK(NULLPTR == actual.returnConstPointerValue());
261 CHECK(NULLPTR == actual.returnConstPointerValueOrDefault((const void*) NULLPTR));
262 CHECK(NULLPTR == actual.returnFunctionPointerValue());
263 CHECK(NULLPTR == actual.returnFunctionPointerValueOrDefault((void (*)()) NULLPTR));
264 }
265
266 TEST(MockCheckedActualCall, MockActualCallTraceClear)
267 {
268 MockActualCallTrace actual;
269 actual.withName("func");
270 actual.clear();
271 STRCMP_EQUAL("", actual.getTraceOutput());
272 }