28 #ifndef GTESTCONVERTOR_H_ 29 #define GTESTCONVERTOR_H_ 31 #include "CppUTest/Utest.h" 34 #error "Please include this file before you include any other GTest files" 52 class GTestResultReporter;
53 class GTestFlagsThatAllocateMemory;
61 class GTestShell :
public UtestShell
63 ::testing::TestInfo* testinfo_;
65 GTestFlagsThatAllocateMemory* flags_;
67 GTestShell(::testing::TestInfo* testinfo, GTestShell* next, GTestFlagsThatAllocateMemory* flags);
69 virtual Utest* createTest() _override;
71 GTestShell* nextGTest()
92 #define private public 93 #define protected public 95 #include "CppUTestExt/GTest.h" 96 #include "CppUTestExt/GMock.h" 97 #include "gtest/gtest-spi.h" 98 #include "gtest/gtest-death-test.h" 103 #define GTEST_IMPLEMENTATION_ 1 104 #include "src/gtest-internal-inl.h" 106 #include "CppUTest/TestRegistry.h" 107 #include "CppUTest/TestFailure.h" 108 #include "CppUTest/TestResult.h" 111 #ifdef GTEST_VERSION_GTEST_1_7 112 #define GTEST_STRING std::string 113 #define GTEST_NO_STRING_VALUE "" 115 #define GTEST_STRING ::testing::internal::String 116 #define GTEST_NO_STRING_VALUE NULL 121 class GTestFlagsThatAllocateMemory
124 void storeValuesOfGTestFLags()
126 GTestFlagcolor = ::testing::GTEST_FLAG(color);
127 GTestFlagfilter = ::testing::GTEST_FLAG(filter);
128 GTestFlagoutput = ::testing::GTEST_FLAG(output);
129 GTestFlagdeath_test_style = ::testing::GTEST_FLAG(death_test_style);
130 GTestFlaginternal_run_death_test = ::testing::internal::GTEST_FLAG(internal_run_death_test);
131 #ifndef GTEST_VERSION_GTEST_1_5 132 GTestFlagstream_result_to = ::testing::GTEST_FLAG(stream_result_to);
136 void resetValuesOfGTestFlags()
138 ::testing::GTEST_FLAG(color) = GTestFlagcolor;
139 ::testing::GTEST_FLAG(filter) = GTestFlagfilter;
140 ::testing::GTEST_FLAG(output) = GTestFlagoutput;
141 ::testing::GTEST_FLAG(death_test_style) = GTestFlagdeath_test_style;
142 ::testing::internal::GTEST_FLAG(internal_run_death_test) = GTestFlaginternal_run_death_test;
143 #ifndef GTEST_VERSION_GTEST_1_5 144 ::testing::GTEST_FLAG(stream_result_to) = GTestFlagstream_result_to;
148 void setGTestFLagValuesToNULLToAvoidMemoryLeaks()
150 #ifndef GTEST_VERSION_GTEST_1_7 151 ::testing::GTEST_FLAG(color) = GTEST_NO_STRING_VALUE;
152 ::testing::GTEST_FLAG(filter) = GTEST_NO_STRING_VALUE;
153 ::testing::GTEST_FLAG(output) = GTEST_NO_STRING_VALUE;
154 ::testing::GTEST_FLAG(death_test_style) = GTEST_NO_STRING_VALUE;
155 ::testing::internal::GTEST_FLAG(internal_run_death_test) = GTEST_NO_STRING_VALUE;
156 #ifndef GTEST_VERSION_GTEST_1_5 157 ::testing::GTEST_FLAG(stream_result_to) = GTEST_NO_STRING_VALUE;
163 GTEST_STRING GTestFlagcolor;
164 GTEST_STRING GTestFlagfilter;
165 GTEST_STRING GTestFlagoutput;
166 GTEST_STRING GTestFlagdeath_test_style;
167 GTEST_STRING GTestFlaginternal_run_death_test;
168 #ifndef GTEST_VERSION_GTEST_1_5 169 GTEST_STRING GTestFlagstream_result_to;
176 GTestConvertor(
bool shouldSimulateFailureAtCreationToAllocateThreadLocalData =
true);
177 virtual ~GTestConvertor();
179 virtual void addAllGTestToTestRegistry();
181 virtual void simulateGTestFailureToPreAllocateAllTheThreadLocalData();
183 virtual void addNewTestCaseForTestInfo(::testing::TestInfo* testinfo);
184 virtual void addAllTestsFromTestCaseToTestRegistry(::testing::TestCase* testcase);
186 virtual void createDummyInSequenceToAndFailureReporterAvoidMemoryLeakInGMock();
188 GTestResultReporter* reporter_;
190 GTestFlagsThatAllocateMemory flags_;
193 class GTestDummyResultReporter :
public ::testing::ScopedFakeTestPartResultReporter
196 GTestDummyResultReporter () : ::
testing::ScopedFakeTestPartResultReporter(INTERCEPT_ALL_THREADS,
NULL) {}
197 virtual void ReportTestPartResult(const ::testing::TestPartResult& ) {}
200 class GMockTestTerminator :
public TestTerminator
203 GMockTestTerminator(const ::testing::TestPartResult& result) : result_(result)
207 virtual void exitCurrentTest()
const 225 if (!SimpleString(result_.message()).contains(
"Actual: never called") &&
226 !SimpleString(result_.message()).contains(
"Actual function call count doesn't match"))
227 throw CppUTestFailedException();
230 virtual ~GMockTestTerminator()
234 const ::testing::TestPartResult& result_;
238 class GTestResultReporter :
public ::testing::ScopedFakeTestPartResultReporter
241 GTestResultReporter () : ::
testing::ScopedFakeTestPartResultReporter(INTERCEPT_ALL_THREADS,
NULL) {}
243 virtual void ReportTestPartResult(const ::testing::TestPartResult& result)
245 FailFailure failure(UtestShell::getCurrent(), result.file_name(), result.line_number(), result.message());
246 UtestShell::getCurrent()->failWith(failure, GMockTestTerminator(result));
250 inline GTestShell::GTestShell(::testing::TestInfo* testinfo, GTestShell* next, GTestFlagsThatAllocateMemory* flags) : testinfo_(testinfo), next_(next), flags_(flags)
252 setGroupName(testinfo->test_case_name());
253 setTestName(testinfo->name());
256 class GTestUTest:
public Utest {
258 GTestUTest(::testing::TestInfo* testinfo, GTestFlagsThatAllocateMemory* flags) : testinfo_(testinfo), test_(
NULL), flags_(flags)
268 catch (CppUTestFailedException& ex)
275 flags_->resetValuesOfGTestFlags();
277 #ifdef GTEST_VERSION_GTEST_1_5 278 test_ = testinfo_->impl()->factory_->CreateTest();
280 test_ = testinfo_->factory_->CreateTest();
283 ::testing::UnitTest::GetInstance()->impl()->set_current_test_info(testinfo_);
287 catch (CppUTestFailedException& ex)
297 catch (CppUTestFailedException& ex)
300 ::testing::UnitTest::GetInstance()->impl()->set_current_test_info(
NULL);
303 flags_->setGTestFLagValuesToNULLToAvoidMemoryLeaks();
304 ::testing::internal::DeathTest::set_last_death_test_message(GTEST_NO_STRING_VALUE);
308 ::testing::Test* test_;
309 ::testing::TestInfo* testinfo_;
310 GTestFlagsThatAllocateMemory* flags_;
313 inline Utest* GTestShell::createTest()
315 return new GTestUTest(testinfo_, flags_);
318 inline void GTestConvertor::simulateGTestFailureToPreAllocateAllTheThreadLocalData()
320 GTestDummyResultReporter *dummyReporter =
new GTestDummyResultReporter();
322 delete dummyReporter;
325 inline GTestConvertor::GTestConvertor(
bool shouldSimulateFailureAtCreationToAllocateThreadLocalData) : first_(
NULL)
327 if (shouldSimulateFailureAtCreationToAllocateThreadLocalData)
328 simulateGTestFailureToPreAllocateAllTheThreadLocalData();
329 reporter_ =
new GTestResultReporter();
332 inline GTestConvertor::~GTestConvertor()
337 GTestShell* next = first_->nextGTest();
343 inline void GTestConvertor::addNewTestCaseForTestInfo(::testing::TestInfo* testinfo)
345 first_ =
new GTestShell(testinfo, first_, &flags_);
346 TestRegistry::getCurrentRegistry()->addTest(first_);
349 inline void GTestConvertor::addAllTestsFromTestCaseToTestRegistry(::testing::TestCase* testcase)
351 int currentTestCount = 0;
352 ::testing::TestInfo* currentTest = (::testing::TestInfo*) testcase->GetTestInfo(currentTestCount);
353 while (currentTest) {
354 addNewTestCaseForTestInfo(currentTest);
356 currentTest = (::testing::TestInfo*) testcase->GetTestInfo(currentTestCount);
360 inline void GTestConvertor::createDummyInSequenceToAndFailureReporterAvoidMemoryLeakInGMock()
362 ::testing::InSequence seq;
363 ::testing::internal::GetFailureReporter();
366 inline void GTestConvertor::addAllGTestToTestRegistry()
368 createDummyInSequenceToAndFailureReporterAvoidMemoryLeakInGMock();
369 flags_.storeValuesOfGTestFLags();
372 const char * argv[] = {
"NameOfTheProgram",
"--gmock_catch_leaked_mocks=0"};
373 ::testing::InitGoogleMock(&argc, (
char**) argv);
375 ::testing::UnitTest* unitTests = ::testing::UnitTest::GetInstance();
377 int currentUnitTestCount = 0;
378 ::testing::TestCase* currentTestCase = (::testing::TestCase*) unitTests->GetTestCase(currentUnitTestCount);
379 while (currentTestCase) {
380 addAllTestsFromTestCaseToTestRegistry(currentTestCase);
381 currentUnitTestCount++;
382 currentTestCase = (::testing::TestCase*) unitTests->GetTestCase(currentUnitTestCount);
Definition: GTestConvertor.h:55
#define NULL
Definition: nm_bsp.h:76