forked from cpputest/cpputest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGTestConvertor.cpp
More file actions
315 lines (270 loc) · 9.68 KB
/
Copy pathGTestConvertor.cpp
File metadata and controls
315 lines (270 loc) · 9.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright (c) 2011, Michael Feathers, James Grenning and Bas Vodde
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef CPPUTEST_USE_REAL_GTEST
#undef new
/* Enormous hack!
*
* This sucks enormously. We need to do two things in GTest that seem to not be possible without
* this hack. Hopefully there is *another way*.
*
* We need to access the factory in the TestInfo in order to be able to create tests. The factory
* is private and there seems to be no way to access it...
*
* We need to be able to call the Test SetUp and TearDown methods, but they are protected for
* some reason. We can't subclass either as the tests are created with the TEST macro.
*
* If anyone knows how to get the above things done *without* these ugly #defines, let me know!
*
*/
#define private public
#define protected public
#include "gtest/gtest.h"
#include "gtest/gtest-spi.h"
#include "gtest/gtest-death-test.h"
#include "gmock/gmock.h"
#ifndef RUN_ALL_TESTS
#define GTEST_VERSION_GTEST_1_7
#else
#ifdef ADD_FAILURE_AT
#define GTEST_VERSION_GTEST_1_6
#else
#define GTEST_VERSION_GTEST_1_5
#endif
#endif
/*
* We really need some of its internals as they don't have a public interface.
*
*/
#define GTEST_IMPLEMENTATION_ 1
#include "src/gtest-internal-inl.h"
#include "CppUTestExt/GTestConvertor.h"
#include "CppUTest/TestRegistry.h"
#include "CppUTest/TestFailure.h"
#include "CppUTest/TestResult.h"
/* Store some of the flags as we'll need to reset them each test to avoid leaking memory */
#ifdef GTEST_VERSION_GTEST_1_7
#define GTEST_STRING std::string
#define GTEST_NO_STRING_VALUE ""
#else
#define GTEST_STRING ::testing::internal::String
#define GTEST_NO_STRING_VALUE NULL
#endif
static GTEST_STRING GTestFlagcolor;
static GTEST_STRING GTestFlagfilter;
static GTEST_STRING GTestFlagoutput;
static GTEST_STRING GTestFlagdeath_test_style;
static GTEST_STRING GTestFlaginternal_run_death_test;
#ifndef GTEST_VERSION_GTEST_1_5
static GTEST_STRING GTestFlagstream_result_to;
#endif
static void storeValuesOfGTestFLags()
{
GTestFlagcolor = ::testing::GTEST_FLAG(color);
GTestFlagfilter = ::testing::GTEST_FLAG(filter);
GTestFlagoutput = ::testing::GTEST_FLAG(output);
GTestFlagdeath_test_style = ::testing::GTEST_FLAG(death_test_style);
GTestFlaginternal_run_death_test = ::testing::internal::GTEST_FLAG(internal_run_death_test);
#ifndef GTEST_VERSION_GTEST_1_5
GTestFlagstream_result_to = ::testing::GTEST_FLAG(stream_result_to);
#endif
}
static void resetValuesOfGTestFlags()
{
::testing::GTEST_FLAG(color) = GTestFlagcolor;
::testing::GTEST_FLAG(filter) = GTestFlagfilter;
::testing::GTEST_FLAG(output) = GTestFlagoutput;
::testing::GTEST_FLAG(death_test_style) = GTestFlagdeath_test_style;
::testing::internal::GTEST_FLAG(internal_run_death_test) = GTestFlaginternal_run_death_test;
#ifndef GTEST_VERSION_GTEST_1_5
::testing::GTEST_FLAG(stream_result_to) = GTestFlagstream_result_to;
#endif
}
void setGTestFLagValuesToNULLToAvoidMemoryLeaks()
{
#ifndef GTEST_VERSION_GTEST_1_7
::testing::GTEST_FLAG(color) = GTEST_NO_STRING_VALUE;
::testing::GTEST_FLAG(filter) = GTEST_NO_STRING_VALUE;
::testing::GTEST_FLAG(output) = GTEST_NO_STRING_VALUE;
::testing::GTEST_FLAG(death_test_style) = GTEST_NO_STRING_VALUE;
::testing::internal::GTEST_FLAG(internal_run_death_test) = GTEST_NO_STRING_VALUE;
#ifndef GTEST_VERSION_GTEST_1_5
::testing::GTEST_FLAG(stream_result_to) = GTEST_NO_STRING_VALUE;
#endif
#endif
}
/* Left a global to avoid a header dependency to gtest.h */
class GTestDummyResultReporter : public ::testing::ScopedFakeTestPartResultReporter
{
public:
GTestDummyResultReporter () : ::testing::ScopedFakeTestPartResultReporter(INTERCEPT_ALL_THREADS, NULL) {}
virtual void ReportTestPartResult(const ::testing::TestPartResult& /*result*/) {}
};
class GTestResultReporter : public ::testing::ScopedFakeTestPartResultReporter
{
public:
GTestResultReporter () : ::testing::ScopedFakeTestPartResultReporter(INTERCEPT_ALL_THREADS, NULL) {}
virtual void ReportTestPartResult(const ::testing::TestPartResult& result)
{
FailFailure failure(UtestShell::getCurrent(), result.file_name(), result.line_number(), result.message());
UtestShell::getCurrent()->getTestResult()->addFailure(failure);
/*
* When using GMock, it throws an exception fromt he destructor leaving
* the system in an unstable state.
* Therefore, when the test fails because of failed gmock expectation
* then don't throw the exception, but let it return. Usually this should
* already be at the end of the test, so it doesn't matter much
*/
if (!SimpleString(result.message()).contains("Actual: never called") &&
!SimpleString(result.message()).contains("Actual function call count doesn't match"))
throw CppUTestFailedException();
}
};
GTestShell::GTestShell(::testing::TestInfo* testinfo, GTestShell* next) : testinfo_(testinfo), next_(next)
{
setGroupName(testinfo->test_case_name());
setTestName(testinfo->name());
}
GTestShell* GTestShell::nextGTest()
{
return next_;
}
class GTestUTest: public Utest {
public:
GTestUTest(::testing::TestInfo* testinfo) : testinfo_(testinfo), test_(NULL)
{
}
void testBody()
{
try {
test_->TestBody();
}
catch (CppUTestFailedException& ex)
{
}
}
void setup()
{
resetValuesOfGTestFlags();
#ifdef GTEST_VERSION_GTEST_1_5
test_ = testinfo_->impl()->factory_->CreateTest();
#else
test_ = testinfo_->factory_->CreateTest();
#endif
::testing::UnitTest::GetInstance()->impl()->set_current_test_info(testinfo_);
try {
test_->SetUp();
}
catch (CppUTestFailedException& ex)
{
}
}
void teardown()
{
try {
test_->TearDown();
}
catch (CppUTestFailedException& ex)
{
}
::testing::UnitTest::GetInstance()->impl()->set_current_test_info(NULL);
delete test_;
setGTestFLagValuesToNULLToAvoidMemoryLeaks();
::testing::internal::DeathTest::set_last_death_test_message(GTEST_NO_STRING_VALUE);
}
private:
::testing::Test* test_;
::testing::TestInfo* testinfo_;
};
Utest* GTestShell::createTest()
{
return new GTestUTest(testinfo_);
};
void GTestConvertor::simulateGTestFailureToPreAllocateAllTheThreadLocalData()
{
GTestDummyResultReporter *dummyReporter = new GTestDummyResultReporter();
ASSERT_TRUE(false);
delete dummyReporter;
}
GTestConvertor::GTestConvertor(bool shouldSimulateFailureAtCreationToAllocateThreadLocalData) : first_(NULL)
{
if (shouldSimulateFailureAtCreationToAllocateThreadLocalData)
simulateGTestFailureToPreAllocateAllTheThreadLocalData();
reporter_ = new GTestResultReporter();
}
GTestConvertor::~GTestConvertor()
{
delete reporter_;
while (first_) {
GTestShell* next = first_->nextGTest();
delete first_;
first_ = next;
}
}
void GTestConvertor::addNewTestCaseForTestInfo(::testing::TestInfo* testinfo)
{
first_ = new GTestShell(testinfo, first_);
TestRegistry::getCurrentRegistry()->addTest(first_);
}
void GTestConvertor::addAllTestsFromTestCaseToTestRegistry(::testing::TestCase* testcase)
{
int currentTestCount = 0;
::testing::TestInfo* currentTest = (::testing::TestInfo*) testcase->GetTestInfo(currentTestCount);
while (currentTest) {
addNewTestCaseForTestInfo(currentTest);
currentTestCount++;
currentTest = (::testing::TestInfo*) testcase->GetTestInfo(currentTestCount);
}
}
void GTestConvertor::createDummyInSequenceToAndFailureReporterAvoidMemoryLeakInGMock()
{
#ifdef CPPUTEST_USE_REAL_GMOCK
::testing::InSequence seq;
::testing::internal::GetFailureReporter();
#endif
}
void GTestConvertor::addAllGTestToTestRegistry()
{
createDummyInSequenceToAndFailureReporterAvoidMemoryLeakInGMock();
storeValuesOfGTestFLags();
#ifdef CPPUTEST_USE_REAL_GMOCK
int argc = 2;
const char * argv[] = {"NameOfTheProgram", "--gmock_catch_leaked_mocks=0"};
::testing::InitGoogleMock(&argc, (char**) argv);
#endif
::testing::UnitTest* unitTests = ::testing::UnitTest::GetInstance();
int currentUnitTestCount = 0;
::testing::TestCase* currentTestCase = (::testing::TestCase*) unitTests->GetTestCase(currentUnitTestCount);
while (currentTestCase) {
addAllTestsFromTestCaseToTestRegistry(currentTestCase);
currentUnitTestCount++;
currentTestCase = (::testing::TestCase*) unitTests->GetTestCase(currentUnitTestCount);
}
}
#else
extern int totallyDummySymbolToNotCauseAnyLinkerWarningsAboutEmptyCompilationUnits;
int totallyDummySymbolToNotCauseAnyLinkerWarningsAboutEmptyCompilationUnits = 0;
#endif