Debian Codegraph @

tests/CppUTestExt/MockFailureTest.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/MockFailure.h"
30 #include "CppUTestExt/MockCheckedExpectedCall.h"
31 #include "CppUTestExt/MockExpectedCallsList.h"
32 #include "MockFailureReporterForTest.h"
33
34 TEST_GROUP(MockFailureTest)
35 {
36 MockFailureReporter reporter;
37
38 MockExpectedCallsList *list;
39 MockCheckedExpectedCall* call1;
40 MockCheckedExpectedCall* call2;
41 MockCheckedExpectedCall* call3;
42
43 void setup ()
44 {
45 list = new MockExpectedCallsList;
46 call1 = new MockCheckedExpectedCall;
47 call2 = new MockCheckedExpectedCall;
48 call3 = new MockCheckedExpectedCall;
49 }
50 void teardown ()
51 {
52 delete list;
53 delete call1;
54 delete call2;
55 delete call3;
56 CHECK_NO_MOCK_FAILURE();
57 MockFailureReporterForTest::clearReporter();
58 }
59
60 void addAllToList()
61 {
62 list->addExpectedCall(call1);
63 list->addExpectedCall(call2);
64 list->addExpectedCall(call3);
65 }
66
67 void checkUnexpectedNthCallMessage(unsigned int count, const char* expectedOrdinal)
68 {
69 MockExpectedCallsList callList;
70 MockCheckedExpectedCall expectedCallSingle(1);
71 MockCheckedExpectedCall expectedCallMulti(count-1);
72
73 expectedCallSingle.withName("bar");
74 expectedCallMulti.withName("bar");
75
76 if (count > 1) {
77 callList.addExpectedCall(&expectedCallSingle);
78 expectedCallSingle.callWasMade(1);
79 }
80
81 if (count > 2) {
82 callList.addExpectedCall(&expectedCallMulti);
83 for (unsigned int i = 1; i < (count - 1); i++) {
84 expectedCallMulti.callWasMade(i+1);
85 }
86 }
87
88 MockUnexpectedCallHappenedFailure failure(UtestShell::getCurrent(), "bar", callList);
89
90 SimpleString expectedMessage = StringFromFormat("Mock Failure: Unexpected additional (%s) call to function: bar\n\tEXPECTED", expectedOrdinal);
91 STRCMP_CONTAINS(expectedMessage.asCharString(), failure.getMessage().asCharString());
92 }
93 };
94
95 TEST(MockFailureTest, noErrorFailureSomethingGoneWrong)
96 {
97 MockFailure failure(UtestShell::getCurrent());
98 STRCMP_EQUAL("Test failed with MockFailure without an error! Something went seriously wrong.", failure.getMessage().asCharString());
99 }
100
101 TEST(MockFailureTest, unexpectedCallHappened)
102 {
103 MockUnexpectedCallHappenedFailure failure(UtestShell::getCurrent(), "foobar", *list);
104 STRCMP_EQUAL("Mock Failure: Unexpected call to function: foobar\n"
105 "\tEXPECTED calls that WERE NOT fulfilled:\n"
106 "\t\t<none>\n"
107 "\tEXPECTED calls that WERE fulfilled:\n"
108 "\t\t<none>", failure.getMessage().asCharString());
109 }
110
111 TEST(MockFailureTest, expectedCallDidNotHappen)
112 {
113 call1->withName("foobar");
114 call2->withName("world").withParameter("boo", 2).withParameter("hello", "world");
115 call3->withName("haphaphap");
116 call3->callWasMade(1);
117 addAllToList();
118
119 MockExpectedCallsDidntHappenFailure failure(UtestShell::getCurrent(), *list);
120 STRCMP_EQUAL("Mock Failure: Expected call WAS NOT fulfilled.\n"
121 "\tEXPECTED calls that WERE NOT fulfilled:\n"
122 "\t\tfoobar -> no parameters (expected 1 call, called 0 times)\n"
123 "\t\tworld -> int boo: <2 (0x2)>, const char* hello: <world> (expected 1 call, called 0 times)\n"
124 "\tEXPECTED calls that WERE fulfilled:\n"
125 "\t\thaphaphap -> no parameters (expected 1 call, called 1 time)", failure.getMessage().asCharString());
126 }
127
128 TEST(MockFailureTest, MockUnexpectedNthAdditionalCallFailure)
129 {
130 checkUnexpectedNthCallMessage(2, "2nd");
131 checkUnexpectedNthCallMessage(3, "3rd");
132 checkUnexpectedNthCallMessage(4, "4th");
133 checkUnexpectedNthCallMessage(11, "11th");
134 checkUnexpectedNthCallMessage(12, "12th");
135 checkUnexpectedNthCallMessage(13, "13th");
136 checkUnexpectedNthCallMessage(14, "14th");
137 checkUnexpectedNthCallMessage(21, "21st");
138 checkUnexpectedNthCallMessage(22, "22nd");
139 checkUnexpectedNthCallMessage(23, "23rd");
140 }
141
142 TEST(MockFailureTest, MockUnexpectedInputParameterFailure)
143 {
144 call1->withName("foo").withParameter("boo", 2);
145 call2->withName("foo").withParameter("boo", 3.3);
146 call3->withName("unrelated");
147 addAllToList();
148
149 MockNamedValue actualParameter("bar");
150 actualParameter.setValue(2);
151
152 MockUnexpectedInputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, *list);
153 STRCMP_EQUAL("Mock Failure: Unexpected parameter name to function \"foo\": bar\n"
154 "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n"
155 "\t\tfoo -> int boo: <2 (0x2)> (expected 1 call, called 0 times)\n"
156 "\t\tfoo -> double boo: <3.3> (expected 1 call, called 0 times)\n"
157 "\tEXPECTED calls that WERE fulfilled related to function: foo\n"
158 "\t\t<none>\n"
159 "\tACTUAL unexpected parameter passed to function: foo\n"
160 "\t\tint bar: <2 (0x2)>", failure.getMessage().asCharString());
161 }
162
163 TEST(MockFailureTest, MockUnexpectedOutputParameterFailure)
164 {
165 int out1;
166 int out2;
167 call1->withName("foo").withOutputParameterReturning("boo", &out1, sizeof(out1));
168 call2->withName("foo").withOutputParameterReturning("boo", &out2, sizeof(out2));
169 call3->withName("unrelated");
170 addAllToList();
171
172 MockNamedValue actualParameter("bar");
173 actualParameter.setValue((void *)0x123);
174
175 MockUnexpectedOutputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, *list);
176 STRCMP_EQUAL("Mock Failure: Unexpected output parameter name to function \"foo\": bar\n"
177 "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n"
178 "\t\tfoo -> const void* boo: <output> (expected 1 call, called 0 times)\n"
179 "\t\tfoo -> const void* boo: <output> (expected 1 call, called 0 times)\n"
180 "\tEXPECTED calls that WERE fulfilled related to function: foo\n"
181 "\t\t<none>\n"
182 "\tACTUAL unexpected output parameter passed to function: foo\n"
183 "\t\tvoid* bar", failure.getMessage().asCharString());
184 }
185
186 TEST(MockFailureTest, MockUnexpectedUnmodifiedOutputParameterFailure)
187 {
188 int out1;
189 call1->withName("foo").withOutputParameterReturning("boo", &out1, sizeof(out1));
190 call2->withName("foo").withUnmodifiedOutputParameter("boo");
191 call3->withName("unrelated");
192 addAllToList();
193
194 MockNamedValue actualParameter("bar");
195 actualParameter.setValue((void *)0x123);
196
197 MockUnexpectedOutputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, *list);
198 STRCMP_EQUAL("Mock Failure: Unexpected output parameter name to function \"foo\": bar\n"
199 "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n"
200 "\t\tfoo -> const void* boo: <output> (expected 1 call, called 0 times)\n"
201 "\t\tfoo -> const void* boo: <output> (expected 1 call, called 0 times)\n"
202 "\tEXPECTED calls that WERE fulfilled related to function: foo\n"
203 "\t\t<none>\n"
204 "\tACTUAL unexpected output parameter passed to function: foo\n"
205 "\t\tvoid* bar", failure.getMessage().asCharString());
206 }
207
208 TEST(MockFailureTest, MockUnexpectedParameterValueFailure)
209 {
210 call1->withName("foo").withParameter("boo", 2);
211 call2->withName("foo").withParameter("boo", 10);
212 call3->withName("unrelated");
213 addAllToList();
214
215 MockNamedValue actualParameter("boo");
216 actualParameter.setValue(20);
217
218 MockUnexpectedInputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, *list);
219 STRCMP_EQUAL("Mock Failure: Unexpected parameter value to parameter \"boo\" to function \"foo\": <20 (0x14)>\n"
220 "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n"
221 "\t\tfoo -> int boo: <2 (0x2)> (expected 1 call, called 0 times)\n"
222 "\t\tfoo -> int boo: <10 (0xa)> (expected 1 call, called 0 times)\n"
223 "\tEXPECTED calls that WERE fulfilled related to function: foo\n"
224 "\t\t<none>\n"
225 "\tACTUAL unexpected parameter passed to function: foo\n"
226 "\t\tint boo: <20 (0x14)>", failure.getMessage().asCharString());
227 }
228
229 TEST(MockFailureTest, MockExpectedParameterDidntHappenFailure)
230 {
231 call1->withName("foo").withParameter("bar", 2).withParameter("boo", "str");
232 call2->withName("foo").withParameter("bar", 10).withParameter("boo", "bleh");
233 call2->callWasMade(1);
234 call2->inputParameterWasPassed("bar");
235 call2->inputParameterWasPassed("boo");
236 call3->withName("unrelated");
237 addAllToList();
238
239 MockExpectedParameterDidntHappenFailure failure(UtestShell::getCurrent(), "foo", *list);
240 STRCMP_EQUAL("Mock Failure: Expected parameter for function \"foo\" did not happen.\n"
241 "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n"
242 "\t\tfoo -> int bar: <2 (0x2)>, const char* boo: <str> (expected 1 call, called 0 times)\n"
243 "\tEXPECTED calls that WERE fulfilled related to function: foo\n"
244 "\t\tfoo -> int bar: <10 (0xa)>, const char* boo: <bleh> (expected 1 call, called 1 time)\n"
245 "\tMISSING parameters that didn't happen:\n"
246 "\t\tint bar, const char* boo", failure.getMessage().asCharString());
247 }
248
249 TEST(MockFailureTest, MockNoWayToCompareCustomTypeFailure)
250 {
251 MockNoWayToCompareCustomTypeFailure failure(UtestShell::getCurrent(), "myType");
252 STRCMP_EQUAL("MockFailure: No way to compare type <myType>. Please install a MockNamedValueComparator.", failure.getMessage().asCharString());
253 }
254
255 TEST(MockFailureTest, MockUnexpectedObjectFailure)
256 {
257 call1->withName("foo").onObject((void*) 0x02);
258 call2->withName("foo").onObject((void*) 0x03);
259 call2->callWasMade(1);
260 call2->wasPassedToObject();
261 call3->withName("unrelated");
262 addAllToList();
263
264 MockUnexpectedObjectFailure failure(UtestShell::getCurrent(), "foo", (void*)0x1, *list);
265 STRCMP_EQUAL(StringFromFormat (
266 "MockFailure: Function called on an unexpected object: foo\n"
267 "\tActual object for call has address: <%p>\n"
268 "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n"
269 "\t\t(object address: %p)::foo -> no parameters (expected 1 call, called 0 times)\n"
270 "\tEXPECTED calls that WERE fulfilled related to function: foo\n"
271 "\t\t(object address: %p)::foo -> no parameters (expected 1 call, called 1 time)",
272 (void*) 0x01, (void*) 0x02, (void*) 0x03).asCharString(), failure.getMessage().asCharString());
273 }
274
275 TEST(MockFailureTest, MockExpectedObjectDidntHappenFailure)
276 {
277 call1->withName("foo").onObject((void*) 0x02);
278 call2->withName("foo").onObject((void*) 0x03);
279 call2->callWasMade(1);
280 call2->wasPassedToObject();
281 call3->withName("unrelated");
282 addAllToList();
283
284 MockExpectedObjectDidntHappenFailure failure(UtestShell::getCurrent(), "foo", *list);
285 STRCMP_EQUAL(StringFromFormat(
286 "Mock Failure: Expected call on object for function \"foo\" but it did not happen.\n"
287 "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n"
288 "\t\t(object address: %p)::foo -> no parameters (expected 1 call, called 0 times)\n"
289 "\tEXPECTED calls that WERE fulfilled related to function: foo\n"
290 "\t\t(object address: %p)::foo -> no parameters (expected 1 call, called 1 time)",
291 (void*) 0x2, (void*) 0x3).asCharString(), failure.getMessage().asCharString());
292 }