Debian Codegraph @

tests/CppUTestExt/MockPluginTest.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 #include "CppUTest/TestHarness.h"
28 #include "CppUTest/TestOutput.h"
29 #include "CppUTestExt/MockSupport.h"
30 #include "CppUTestExt/MockSupportPlugin.h"
31 #include "MockFailureReporterForTest.h"
32 #include "CppUTest/TestTestingFixture.h"
33
34 TEST_GROUP(MockPlugin)
35 {
36 StringBufferTestOutput output;
37
38 UtestShell *test;
39 TestResult *result;
40
41 MockSupportPlugin plugin;
42
43 void setup()
44 {
45 test = new UtestShell("group", "name", "file", 1);
46 result = new TestResult(output);
47 }
48
49 void teardown()
50 {
51 delete test;
52 delete result;
53 mock().clear();
54 mock().removeAllComparatorsAndCopiers();
55 }
56 };
57
58 TEST(MockPlugin, checkExpectationsAndClearAtEnd)
59 {
60 MockFailureReporterInstaller failureReporterInstaller;
61
62 MockExpectedCallsListForTest expectations;
63 expectations.addFunction("foobar");
64 MockExpectedCallsDidntHappenFailure expectedFailure(test, expectations);
65
66 mock().expectOneCall("foobar");
67
68 plugin.postTestAction(*test, *result);
69
70 STRCMP_CONTAINS(expectedFailure.getMessage().asCharString(), output.getOutput().asCharString());
71 LONGS_EQUAL(0, mock().expectedCallsLeft());
72 CHECK_NO_MOCK_FAILURE();
73 }
74
75 TEST(MockPlugin, checkExpectationsWorksAlsoWithHierachicalObjects)
76 {
77 MockFailureReporterInstaller failureReporterInstaller;
78
79 MockExpectedCallsListForTest expectations;
80 expectations.addFunction("differentScope::foobar")->onObject((void*) 1);
81 MockExpectedObjectDidntHappenFailure expectedFailure(test, "differentScope::foobar", expectations);
82
83 mock("differentScope").expectOneCall("foobar").onObject((void*) 1);
84 mock("differentScope").actualCall("foobar");
85
86 plugin.postTestAction(*test, *result);
87
88 STRCMP_CONTAINS(expectedFailure.getMessage().asCharString(), output.getOutput().asCharString());
89 CHECK_NO_MOCK_FAILURE();
90 }
91
92 class DummyComparator : public MockNamedValueComparator
93 {
94 public:
95 bool isEqual(const void* object1, const void* object2)
96 {
97 return object1 == object2;
98 }
99 SimpleString valueToString(const void*)
100 {
101 return "string";
102 }
103 };
104
105 TEST(MockPlugin, installComparatorRecordsTheComparatorButNotInstallsItYet)
106 {
107 MockFailureReporterInstaller failureReporterInstaller;
108
109 DummyComparator comparator;
110 plugin.installComparator("myType", comparator);
111 mock().expectOneCall("foo").withParameterOfType("myType", "name", NULLPTR);
112 mock().actualCall("foo").withParameterOfType("myType", "name", NULLPTR);
113
114 MockNoWayToCompareCustomTypeFailure failure(test, "myType");
115 CHECK_EXPECTED_MOCK_FAILURE(failure);
116
117 plugin.clear();
118 }
119
120 class DummyCopier : public MockNamedValueCopier
121 {
122 public:
123 void copy(void* dst, const void* src)
124 {
125 *(int*)dst = *(const int*)src;
126 }
127 };
128
129 TEST(MockPlugin, installCopierRecordsTheCopierButNotInstallsItYet)
130 {
131 MockFailureReporterInstaller failureReporterInstaller;
132
133 DummyCopier copier;
134 plugin.installCopier("myType", copier);
135 mock().expectOneCall("foo").withOutputParameterOfTypeReturning("myType", "name", NULLPTR);
136 mock().actualCall("foo").withOutputParameterOfType("myType", "name", NULLPTR);
137
138 MockNoWayToCopyCustomTypeFailure failure(test, "myType");
139 CHECK_EXPECTED_MOCK_FAILURE(failure);
140
141 plugin.clear();
142 }
143
144 TEST(MockPlugin, preTestActionWillEnableMultipleComparatorsToTheGlobalMockSupportSpace)
145 {
146 DummyComparator comparator;
147 DummyComparator comparator2;
148 plugin.installComparator("myType", comparator);
149 plugin.installComparator("myOtherType", comparator2);
150
151 plugin.preTestAction(*test, *result);
152 mock().expectOneCall("foo").withParameterOfType("myType", "name", &comparator);
153 mock().expectOneCall("foo").withParameterOfType("myOtherType", "name", &comparator);
154 mock().actualCall("foo").withParameterOfType("myType", "name", &comparator);
155 mock().actualCall("foo").withParameterOfType("myOtherType", "name", &comparator);
156
157 mock().checkExpectations();
158 LONGS_EQUAL(0, result->getFailureCount());
159
160 plugin.clear();
161 }
162
163 static void _failTwiceFunction()
164 {
165 mock().expectOneCall("foobar");
166 FAIL("This failed");
167 }
168
169 TEST(MockPlugin, shouldNotFailAgainWhenTestAlreadyFailed)
170 {
171 TestTestingFixture fixture;
172 fixture.installPlugin(&plugin);
173 fixture.setTestFunction(_failTwiceFunction);
174 fixture.runAllTests();
175 fixture.assertPrintContains("1 failures, 1 tests, 1 ran, 2 checks,");
176 }
177