Debian Codegraph @

tests/CppUTestExt/MockSupportTest.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 "CppUTest/TestTestingFixture.h"
30 #include "CppUTestExt/MockSupport.h"
31 #include "CppUTestExt/MockExpectedCall.h"
32 #include "CppUTestExt/MockFailure.h"
33 #include "MockFailureReporterForTest.h"
34
35 TEST_GROUP(MockSupportTest)
36 {
37 MockExpectedCallsListForTest expectations;
38 MockFailureReporterInstaller failureReporterInstaller;
39
40 void teardown()
41 {
42 mock().checkExpectations();
43 CHECK_NO_MOCK_FAILURE();
44 MockFailureReporterForTest::clearReporter();
45 mock().clear();
46 }
47 };
48
49 TEST(MockSupportTest, setDataForUnsignedIntegerValues)
50 {
51 unsigned int expected_data = 7;
52 mock().setData("data", expected_data);
53 LONGS_EQUAL(expected_data, mock().getData("data").getUnsignedIntValue());
54 }
55
56 TEST(MockSupportTest, setDataForIntegerValues)
57 {
58 int expected_data = 10;
59 mock().setData("data", expected_data);
60 LONGS_EQUAL(expected_data, mock().getData("data").getIntValue());
61 }
62
63 TEST(MockSupportTest, setDataForBooleanValues)
64 {
65 bool expected_data = true;
66 mock().setData("data", expected_data);
67 CHECK_EQUAL(expected_data, mock().getData("data").getBoolValue());
68 }
69
70 TEST(MockSupportTest, hasDataBeenSet)
71 {
72 CHECK(!mock().hasData("data"));
73 mock().setData("data", 10);
74 CHECK(mock().hasData("data"));
75 }
76
77 TEST(MockSupportTest, dataCanBeChanged)
78 {
79 mock().setData("data", 10);
80 mock().setData("data", 15);
81 LONGS_EQUAL(15, mock().getData("data").getIntValue());
82 }
83
84 TEST(MockSupportTest, uninitializedData)
85 {
86 LONGS_EQUAL(0, mock().getData("nonexisting").getIntValue());
87 STRCMP_EQUAL("int", mock().getData("nonexisting").getType().asCharString());
88 }
89
90 TEST(MockSupportTest, setMultipleData)
91 {
92 mock().setData("data", 1);
93 mock().setData("data2", 10);
94 LONGS_EQUAL(1, mock().getData("data").getIntValue());
95 LONGS_EQUAL(10, mock().getData("data2").getIntValue());
96 }
97
98 TEST(MockSupportTest, setDataString)
99 {
100 mock().setData("data", "string");
101 STRCMP_EQUAL("string", mock().getData("data").getStringValue());
102 }
103
104 TEST(MockSupportTest, setDataDouble)
105 {
106 mock().setData("data", 1.0);
107 DOUBLES_EQUAL(1.0, mock().getData("data").getDoubleValue(), 0.05);
108 }
109
110 TEST(MockSupportTest, setDataPointer)
111 {
112 void * ptr = (void*) 0x001;
113 mock().setData("data", ptr);
114 POINTERS_EQUAL(ptr, mock().getData("data").getPointerValue());
115 }
116
117 TEST(MockSupportTest, setConstDataPointer)
118 {
119 const void * ptr = (const void*) 0x001;
120 mock().setData("data", ptr);
121 POINTERS_EQUAL(ptr, mock().getData("data").getConstPointerValue());
122 }
123
124 TEST(MockSupportTest, setDataFunctionPointer)
125 {
126 void (*ptr)() = (void(*)()) 0x001;
127 mock().setData("data", ptr);
128 FUNCTIONPOINTERS_EQUAL(ptr, mock().getData("data").getFunctionPointerValue());
129 }
130
131 TEST(MockSupportTest, setDataObject)
132 {
133 void * ptr = (void*) 0x001;
134 mock().setDataObject("data", "type", ptr);
135 POINTERS_EQUAL(ptr, mock().getData("data").getObjectPointer());
136 STRCMP_EQUAL("type", mock().getData("data").getType().asCharString());
137 }
138
139 TEST(MockSupportTest, setDataConstObject)
140 {
141 void * ptr = (void*) 0x011;
142 mock().setDataConstObject("data", "type", ptr);
143 POINTERS_EQUAL(ptr, mock().getData("data").getConstObjectPointer());
144 STRCMP_EQUAL("type", mock().getData("data").getType().asCharString());
145 }
146
147 TEST(MockSupportTest, tracing)
148 {
149 mock().tracing(true);
150
151 mock().actualCall("boo").withParameter("double", 1.0).withParameter("int", 1).withParameter("string", "string");
152 mock("scope").actualCall("foo").withParameter("double", 1.0).withParameter("int", 1).withParameter("string", "string");
153 mock().checkExpectations();
154
155 STRCMP_CONTAINS("boo", mock().getTraceOutput());
156 STRCMP_CONTAINS("foo", mock().getTraceOutput());
157 }
158
159 TEST(MockSupportTest, tracingWorksHierarchically)
160 {
161 mock("scope").tracing(true);
162 mock().tracing(true);
163
164 mock().actualCall("boo");
165 mock("scope").actualCall("foo");
166 mock().checkExpectations();
167
168 STRCMP_CONTAINS("boo", mock().getTraceOutput());
169 STRCMP_CONTAINS("foo", mock().getTraceOutput());
170 }
171
172 TEST_GROUP(MockSupportTestWithFixture)
173 {
174 TestTestingFixture fixture;
175
176 void teardown()
177 {
178 mock().clear();
179 MockFailureReporterForTest::clearReporter();
180 }
181 };
182
183 static void CHECK_EXPECTED_MOCK_FAILURE_LOCATION_failedTestMethod_()
184 {
185 MockExpectedCallsList list;
186 MockUnexpectedCallHappenedFailure expectedFailure(UtestShell::getCurrent(), "unexpected", list);
187 mock().actualCall("boo");
188 CHECK_EXPECTED_MOCK_FAILURE_LOCATION(expectedFailure, "file", 1);
189 }
190
191 TEST(MockSupportTestWithFixture, CHECK_EXPECTED_MOCK_FAILURE_LOCATION_failed)
192 {
193 mock().setMockFailureStandardReporter(MockFailureReporterForTest::getReporter());
194 fixture.setTestFunction(CHECK_EXPECTED_MOCK_FAILURE_LOCATION_failedTestMethod_);
195 fixture.runAllTests();
196 fixture.assertPrintContains("MockFailures are different.");
197 fixture.assertPrintContains("Expected MockFailure:");
198 fixture.assertPrintContains("Mock Failure: Unexpected call to function: unexpected");
199 fixture.assertPrintContains("Actual MockFailure:");
200 fixture.assertPrintContains("Mock Failure: Unexpected call to function: boo");
201 }
202
203 static void CHECK_NO_MOCK_FAILURE_LOCATION_failedTestMethod_()
204 {
205 mock().actualCall("boo");
206 CHECK_NO_MOCK_FAILURE_LOCATION("file", 1);
207 }
208
209 TEST(MockSupportTestWithFixture, CHECK_NO_MOCK_FAILURE_LOCATION_failed)
210 {
211 mock().setMockFailureStandardReporter(MockFailureReporterForTest::getReporter());
212 fixture.setTestFunction(CHECK_NO_MOCK_FAILURE_LOCATION_failedTestMethod_);
213 fixture.runAllTests();
214 fixture.assertPrintContains("Unexpected mock failure:");
215 fixture.assertPrintContains("Mock Failure: Unexpected call to function: boo");
216 }
217
218 static bool cpputestHasCrashed;
219
220 static void crashMethod()
221 {
222 cpputestHasCrashed = true;
223 }
224
225 static void unexpectedCallTestFunction_(void)
226 {
227 mock().actualCall("unexpected");
228 } // LCOV_EXCL_LINE
229
230 #include "CppUTestExt/OrderedTest.h"
231
232 TEST(MockSupportTestWithFixture, shouldCrashOnFailure)
233 {
234 mock().crashOnFailure(true);
235 UtestShell::setCrashMethod(crashMethod);
236 fixture.setTestFunction(unexpectedCallTestFunction_);
237
238 fixture.runAllTests();
239
240 CHECK(cpputestHasCrashed);
241
242 mock().crashOnFailure(false);
243 UtestShell::resetCrashMethod();
244 }
245
246 TEST(MockSupportTestWithFixture, ShouldNotCrashOnFailureAfterCrashMethodWasReset)
247 {
248 cpputestHasCrashed = false;
249 UtestShell::setCrashMethod(crashMethod);
250 fixture.setTestFunction(unexpectedCallTestFunction_);
251 UtestShell::resetCrashMethod();
252
253 fixture.runAllTests();
254
255 fixture.assertPrintContains("Unexpected call to function: unexpected");
256 CHECK_FALSE(cpputestHasCrashed);
257 }
258
259 TEST(MockSupportTestWithFixture, failedMockShouldFailAgainWhenRepeated)
260 {
261 fixture.setTestFunction(unexpectedCallTestFunction_);
262 int repeatCount = 2;
263 while(repeatCount--)
264 {
265 fixture.runAllTests();
266 fixture.assertPrintContains("Unexpected call to function: unexpected");
267 fixture.assertPrintContains("Errors (1 failures, 1 tests, 1 ran, 0 checks, 0 ignored, 0 filtered out");
268 fixture.flushOutputAndResetResult();
269 }
270 }
271