diff --git a/Makefile.am b/Makefile.am index dcd433580..2ca10672a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -172,9 +172,9 @@ CppUTestExtTests_SOURCES = \ tests/CppUTestExt/TestMockCheatSheet.cpp \ tests/CppUTestExt/TestMockSupport_c.cpp \ tests/CppUTestExt/TestGMock.cpp \ + tests/CppUTestExt/TestGTestConvertor.cpp \ tests/CppUTestExt/TestMockExpectedFunctionCall.cpp \ tests/CppUTestExt/TestMockSupport_cCFile.c \ - tests/CppUTestExt/TestGTest.cpp \ tests/CppUTestExt/TestMockExpectedFunctionsList.cpp \ tests/CppUTestExt/TestMemoryReportAllocator.cpp \ tests/CppUTestExt/TestMockFailure.cpp \ @@ -183,6 +183,10 @@ CppUTestExtTests_SOURCES = \ tests/CppUTestExt/TestMemoryReporterPlugin.cpp \ tests/CppUTestExt/TestMockPlugin.cpp +if INCLUDE_GMOCKTESTS +CppUTestExtTests_SOURCES += \ + tests/CppUTestExt/TestGTest.cpp +endif RUN_CPPUTEST_TESTS = ./$(CPPUTEST_TESTS) -r #RUN_CPPUTEST_EXT_TESTS = ./$(CPPUTEST_EXT_TESTS) -r diff --git a/configure.ac b/configure.ac index e9e800b3b..38ac2d4db 100644 --- a/configure.ac +++ b/configure.ac @@ -73,7 +73,7 @@ if test "x$USE_MAINTAINER_MODE" = "xyes"; then # FLag -Werror. CFLAGS=-Werror AC_MSG_CHECKING([whether CC and CXX supports -Werror]) -AC_COMPILE_IFELSE([AC_LANG_PROGRAM([])], [AC_MSG_RESULT([yes]); CPPUTEST_CWARNINGFLAGS="${CPPUTEST_CWARNINGFLAGS} -Werror"; CPPUTEST_CXXWARNINGFLAGS="${CPPUTEST_CXXWARNINGFLAGS} -Werror" ], [AC_MSG_RESULT([no])]) +AC_COMPILE_IFELSE([AC_LANG_PROGRAM([])], [AC_MSG_RESULT([yes]); CPPUTEST_CWARNINGFLAGS_AS_ERROR="-Werror"; CPPUTEST_CXXWARNINGFLAGS_AS_ERROR="-Werror" ], [AC_MSG_RESULT([no])]) CFLAGS="$saved_cflags" fi @@ -299,6 +299,56 @@ AC_ARG_ENABLE([gmock], [AC_HELP_STRING([--enable-gmock], [enable using GMock. Re AM_CONDITIONAL([INCLUDE_CPPUTEST_EXT], [test "x${cpputest_ext}" = xyes]) +# Check availability of GMock. + +AC_ARG_VAR([GMOCK_HOME], Directory where gmock is installed so the gmock tests can run) +AC_MSG_CHECKING([for the availability of gmock via the GMOCK_HOME variable]) + +AM_CONDITIONAL([INCLUDE_GMOCKTESTS], [test -n ${GMOCK_HOME}]) +if test -n ${GMOCK_HOME}; then + + # Gmock requires C++11 + use_std_cpp11=yes + + # Set the compiler and linker options for GMock + CPPUTEST_CPPFLAGS="${CPPUTEST_CPPFLAGS} -DGTEST_USE_OWN_TR1_TUPLE=1" + CPPUTEST_CPPFLAGS="${CPPUTEST_CPPFLAGS} -I${GMOCK_HOME}/include" + if test -e ${GMOCK_HOME}/lib/libgmock.la; then \ + CPPUTEST_LDADD="${CPPUTEST_LDADD} ${GMOCK_HOME}/lib/libgmock.la"; \ + elif test -e ${GMOCK_HOME}/libgmock.a; then \ + CPPUTEST_LDADD="${CPPUTEST_LDADD} ${GMOCK_HOME}/libgmock.a"; \ + else \ + AC_MSG_ERROR([ +------------------------------------- +GMOCK_HOME was set, but couldn't find the compiled library. +Did you compile it? +-------------------------------------]); + fi + + GTEST_HOME=${GMOCK_HOME}/gtest + CPPUTEST_CPPFLAGS="${CPPUTEST_CPPFLAGS} -I${GTEST_HOME}/include -I${GTEST_HOME}" + + if test -e ${GTEST_HOME}/lib/libgtest.la; then \ + CPPUTEST_LDADD="${CPPUTEST_LDADD} ${GTEST_HOME}/lib/libgtest.la"; \ + elif test -e ${GTEST_HOME}/libgtest.a; then \ + CPPUTEST_LDADD="${CPPUTEST_LDADD} ${GTEST_HOME}/libgtest.a"; \ + else \ + AC_MSG_ERROR([ +------------------------------------- +GTEST_HOME was set, but couldn't find the compiled library. +Did you compile it? +-------------------------------------]); + fi + + AC_MSG_RESULT([yes]) +else + # Without GTest, we can safely have warnings as errors. With GTest not as it has warnings :) + CPPUTEST_CWARNINGFLAGS="${CPPUTEST_CWARNINGFLAGS} ${CPPUTEST_CWARNINGFLAGS_AS_ERROR}" + CPPUTEST_CXXWARNINGFLAGS="${CPPUTEST_CXXWARNINGFLAGS} ${CPPUTEST_CWARNINGFLAGS_AS_ERROR}" + AC_MSG_RESULT([no]) +fi + + # Dealing with not having a Standard C library... (usually for Kernel development) if test "x${use_std_c}" = xno; then use_std_cpp=no @@ -351,6 +401,7 @@ if test "x${coverage}" = xyes; then fi fi + AM_CONDITIONAL([INCLUDE_GMOCKTESTS_EXT], [test "x${use_gmock}" = xyes]) if test "x${use_gmock}" = xyes; then @@ -377,8 +428,8 @@ Did you compile it? -------------------------------------]); fi AC_DEFINE([CPPUTEST_USE_REAL_GMOCK], [1], [Use GMock]) -else - CPPUTEST_CPPFLAGS="${CPPUTEST_CPPFLAGS} -I${srcdir}/include/CppUTestExt/CppUTestGMock" +#else +# CPPUTEST_CPPFLAGS="${CPPUTEST_CPPFLAGS} -I${srcdir}/include/CppUTestExt/CppUTestGMock" fi if test "x${real_gtest}" = xyes; then @@ -390,7 +441,7 @@ Compiling with --enable-real-gtest requires you to set the GTEST_HOME to the GTest source directory -------------------------------------]) fi - CPPUTEST_CPPFLAGS="${CPPUTEST_CPPFLAGS} -I${GTEST_HOME}/include -I${GTEST_HOME} -Isomething" + CPPUTEST_CPPFLAGS="${CPPUTEST_CPPFLAGS} -I${GTEST_HOME}/include -I${GTEST_HOME}" if test -e ${GTEST_HOME}/lib/libgtest.la; then \ CPPUTEST_LDADD="${CPPUTEST_LDADD} ${GTEST_HOME}/lib/libgtest.la"; \ @@ -408,8 +459,8 @@ Did you compile it? # Turn warnings off. Gtest doesn't like it! CPPUTEST_CWARNINGFLAGS="" CPPUTEST_CXXWARNINGFLAGS="" -else - CPPUTEST_CPPFLAGS="${CPPUTEST_CPPFLAGS} -I${srcdir}/include/CppUTestExt/CppUTestGTest" +#else +# CPPUTEST_CPPFLAGS="${CPPUTEST_CPPFLAGS} -I${srcdir}/include/CppUTestExt/CppUTestGTest" fi CPPUTEST_CFLAGS="${CPPUTEST_CFLAGS} ${CPPUTEST_CWARNINGFLAGS}" diff --git a/include/CppUTestExt/GMock.h b/include/CppUTestExt/GMock.h index 90ea531c0..6a93ede67 100644 --- a/include/CppUTestExt/GMock.h +++ b/include/CppUTestExt/GMock.h @@ -28,9 +28,7 @@ #ifndef GMOCK_H_ #define GMOCK_H_ -#ifdef CPPUTEST_USE_REAL_GMOCK #undef new -#endif #undef RUN_ALL_TESTS @@ -39,10 +37,8 @@ #include "gmock/gmock.h" #undef RUN_ALL_TESTS -#ifdef CPPUTEST_USE_REAL_GMOCK using testing::Return; using testing::NiceMock; -#endif #ifdef CPPUTEST_USE_NEW_MACROS #include "CppUTest/MemoryLeakDetectorNewMacros.h" diff --git a/include/CppUTestExt/GTest.h b/include/CppUTestExt/GTest.h new file mode 100644 index 000000000..308f23d5a --- /dev/null +++ b/include/CppUTestExt/GTest.h @@ -0,0 +1,42 @@ +/* + * 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 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 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. + */ + +#ifndef GTEST__H_ +#define GTEST__H_ + +#undef new + +#undef RUN_ALL_TESTS + +#include "gtest/gtest.h" +#undef RUN_ALL_TESTS + +#ifdef CPPUTEST_USE_NEW_MACROS +#include "CppUTest/MemoryLeakDetectorNewMacros.h" +#endif + +#endif diff --git a/include/CppUTestExt/GTestConvertor.h b/include/CppUTestExt/GTestConvertor.h index 622f97359..6f06d7716 100644 --- a/include/CppUTestExt/GTestConvertor.h +++ b/include/CppUTestExt/GTestConvertor.h @@ -28,11 +28,25 @@ #ifndef GTESTCONVERTOR_H_ #define GTESTCONVERTOR_H_ -#include "CppUTest/Utest.h" +/* + * Usage: + * + * This file must only be included in the main. The whole implementation is inline so that this can + * be compiled on usage and not on CppUTest compile-time. This avoids a hard dependency with CppUTest + * and with GTest + * + * Add the following lines to your main: + * + * GTestConvertor convertor; + * convertor.addAllGTestToTestRegistry(); + * + * + */ -#ifdef CPPUTEST_USE_REAL_GTEST +#include "CppUTest/Utest.h" class GTestResultReporter; +class GTestFlags; namespace testing { class TestInfo; @@ -44,34 +58,301 @@ class GTestShell : public UtestShell { ::testing::TestInfo* testinfo_; GTestShell* next_; + GTestFlags* flags_; public: - GTestShell(::testing::TestInfo* testinfo, GTestShell* next); + GTestShell(::testing::TestInfo* testinfo, GTestShell* next, GTestFlags* flags); virtual Utest* createTest(); - GTestShell* nextGTest(); + GTestShell* nextGTest() + { + return next_; + } +}; + +#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 + +#undef RUN_ALL_TESTS + +#include "GMock.h" +#include "gtest/gtest-spi.h" +#include "gtest/gtest-death-test.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 "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 + +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()->failWith(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(); + } }; +class GTestFlags +{ +public: + 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 + } + + 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 + } + +private: + GTEST_STRING GTestFlagcolor; + GTEST_STRING GTestFlagfilter; + GTEST_STRING GTestFlagoutput; + GTEST_STRING GTestFlagdeath_test_style; + GTEST_STRING GTestFlaginternal_run_death_test; + #ifndef GTEST_VERSION_GTEST_1_5 + GTEST_STRING GTestFlagstream_result_to; + #endif +}; class GTestConvertor { public: - GTestConvertor(bool shouldSimulateFailureAtCreationToAllocateThreadLocalData = true); - virtual ~GTestConvertor(); + GTestConvertor(bool shouldSimulateFailureAtCreationToAllocateThreadLocalData = true) + { + if (shouldSimulateFailureAtCreationToAllocateThreadLocalData) + simulateGTestFailureToPreAllocateAllTheThreadLocalData(); + reporter_ = new GTestResultReporter(); + } - virtual void addAllGTestToTestRegistry(); + virtual ~GTestConvertor() + { + delete reporter_; + + while (first_) { + GTestShell* next = first_->nextGTest(); + delete first_; + first_ = next; + } + } + + virtual void addAllGTestToTestRegistry() + { + createDummyInSequenceToAndFailureReporterAvoidMemoryLeakInGMock(); + flags_.storeValuesOfGTestFLags(); + + int argc = 2; + const char * argv[] = {"NameOfTheProgram", "--gmock_catch_leaked_mocks=0"}; + ::testing::InitGoogleMock(&argc, (char**) argv); + + ::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); + } + } protected: - virtual void simulateGTestFailureToPreAllocateAllTheThreadLocalData(); + virtual void simulateGTestFailureToPreAllocateAllTheThreadLocalData() + { + GTestDummyResultReporter *dummyReporter = new GTestDummyResultReporter(); + ASSERT_TRUE(false); + delete dummyReporter; + } - virtual void addNewTestCaseForTestInfo(::testing::TestInfo* testinfo); - virtual void addAllTestsFromTestCaseToTestRegistry(::testing::TestCase* testcase); + virtual void addNewTestCaseForTestInfo(::testing::TestInfo* testinfo) + { + first_ = new GTestShell(testinfo, first_, &flags_); + TestRegistry::getCurrentRegistry()->addTest(first_); + } + + virtual void 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); + } + } + + virtual void createDummyInSequenceToAndFailureReporterAvoidMemoryLeakInGMock() + { + ::testing::InSequence seq; + ::testing::internal::GetFailureReporter(); + } - virtual void createDummyInSequenceToAndFailureReporterAvoidMemoryLeakInGMock(); private: GTestResultReporter* reporter_; GTestShell* first_; + GTestFlags flags_; }; -#endif +class GTestUTest: public Utest { +public: + GTestUTest(::testing::TestInfo* testinfo, GTestFlags* flags) : testinfo_(testinfo), test_(NULL), flags_(flags) + { + + } + + void testBody() + { + try { + test_->TestBody(); + } + catch (CppUTestFailedException& ex) + { + } + } + + void setup() + { + flags_->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_; + + flags_->setGTestFLagValuesToNULLToAvoidMemoryLeaks(); + ::testing::internal::DeathTest::set_last_death_test_message(GTEST_NO_STRING_VALUE); + } + +private: + ::testing::Test* test_; + ::testing::TestInfo* testinfo_; + GTestFlags* flags_; +}; + +inline GTestShell::GTestShell(::testing::TestInfo* testinfo, GTestShell* next, GTestFlags* flags) + : testinfo_(testinfo), next_(next), flags_(flags) +{ + setGroupName(testinfo->test_case_name()); + setTestName(testinfo->name()); +} + +inline Utest* GTestShell::createTest() +{ + return new GTestUTest(testinfo_, flags_); +}; #endif + diff --git a/tests/CppUTestExt/AllTests.cpp b/tests/CppUTestExt/AllTests.cpp index edc11e2ae..8b6bc77d6 100644 --- a/tests/CppUTestExt/AllTests.cpp +++ b/tests/CppUTestExt/AllTests.cpp @@ -33,10 +33,8 @@ int main(int ac, const char** av) { -#ifdef CPPUTEST_USE_REAL_GTEST GTestConvertor convertor; convertor.addAllGTestToTestRegistry(); -#endif MemoryReporterPlugin plugin; MockSupportPlugin mockPlugin; diff --git a/tests/CppUTestExt/TestGTest.cpp b/tests/CppUTestExt/TestGTest.cpp index f8133900a..26ac4fbf6 100644 --- a/tests/CppUTestExt/TestGTest.cpp +++ b/tests/CppUTestExt/TestGTest.cpp @@ -25,11 +25,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifdef CPPUTEST_USE_REAL_GTEST -#undef new -#endif - -#include "gtest/gtest.h" +#include "CppUTestExt/GTest.h" static bool g_GTestEqual_has_been_called = false; TEST(GTestSimpleTest, GTestEqual) @@ -63,8 +59,6 @@ TEST(GTestSimpleTest, GTestExpectStreq) EXPECT_STREQ("hello world", "hello world"); } -#ifdef CPPUTEST_USE_REAL_GTEST - /* Death tests are IMHO not a good idea at all. But for compatibility reason, we'll support it */ static void crashMe () @@ -78,8 +72,6 @@ TEST(GTestSimpleTest, GTestDeathTest) ASSERT_DEATH(crashMe(), "Crash me!"); } -#endif - class GTestTestingFixtureTest : public testing::Test { protected: bool setup_was_called; @@ -115,45 +107,6 @@ TEST_F(GTestTestingFixtureTest, teardownMustBeCalledOrElseThisWillLeak) #include "CppUTest/TestRegistry.h" #include "CppUTest/TestOutput.h" #include "CppUTest/TestTestingFixture.h" -#include "CppUTestExt/GTestConvertor.h" - -#ifdef CPPUTEST_USE_REAL_GTEST - -TEST_GROUP(GTestConvertor) -{ -}; - -TEST(GTestConvertor, correctNumberOfTestCases) -{ - LONGS_EQUAL(2, ::testing::UnitTest::GetInstance()->total_test_case_count()); - CHECK(::testing::UnitTest::GetInstance()->GetTestCase(0)); - CHECK(::testing::UnitTest::GetInstance()->GetTestCase(1)); - CHECK(::testing::UnitTest::GetInstance()->GetTestCase(2) == NULL); -} - -TEST(GTestConvertor, correctNumberOfTestsInTheTestCases) -{ - const ::testing::TestCase* firstTestCase = ::testing::UnitTest::GetInstance()->GetTestCase(0); - const ::testing::TestCase* secondTestCase = ::testing::UnitTest::GetInstance()->GetTestCase(1); - - STRCMP_EQUAL("GTestSimpleTest", firstTestCase->name()); - STRCMP_EQUAL("GTestTestingFixtureTest", secondTestCase->name()); - LONGS_EQUAL(7, firstTestCase->total_test_count()); - LONGS_EQUAL(2, secondTestCase->total_test_count()); -} - -TEST(GTestConvertor, testsGetAddedToCurrentTestRegistry) -{ - TestTestingFixture fixture; - TestRegistry::getCurrentRegistry()->unDoLastAddTest(); - - GTestConvertor convertor(false); - convertor.addAllGTestToTestRegistry(); - - LONGS_EQUAL(9, TestRegistry::getCurrentRegistry()->countTests()); -} - -#endif TEST_GROUP(gtest) { @@ -275,3 +228,4 @@ TEST(gtestMacros, ASSERT_TRUEFails) { testFailureWith(_failMethodASSERT_TRUE); } + diff --git a/tests/CppUTestExt/TestGTestConvertor.cpp b/tests/CppUTestExt/TestGTestConvertor.cpp new file mode 100644 index 000000000..3e927e2df --- /dev/null +++ b/tests/CppUTestExt/TestGTestConvertor.cpp @@ -0,0 +1,64 @@ +/* + * 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 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 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. + */ + +#include "CppUTestExt/GTestConvertor.h" + +#if 0 +TEST_GROUP(GTestConvertor) +{ +}; + +TEST(GTestConvertor, correctNumberOfTestCases) +{ + LONGS_EQUAL(2, ::testing::UnitTest::GetInstance()->total_test_case_count()); + CHECK(::testing::UnitTest::GetInstance()->GetTestCase(0)); + CHECK(::testing::UnitTest::GetInstance()->GetTestCase(1)); + CHECK(::testing::UnitTest::GetInstance()->GetTestCase(2) == NULL); +} + +TEST(GTestConvertor, correctNumberOfTestsInTheTestCases) +{ + const ::testing::TestCase* firstTestCase = ::testing::UnitTest::GetInstance()->GetTestCase(0); + const ::testing::TestCase* secondTestCase = ::testing::UnitTest::GetInstance()->GetTestCase(1); + + STRCMP_EQUAL("GTestSimpleTest", firstTestCase->name()); + STRCMP_EQUAL("GTestTestingFixtureTest", secondTestCase->name()); + LONGS_EQUAL(7, firstTestCase->total_test_count()); + LONGS_EQUAL(2, secondTestCase->total_test_count()); +} + +TEST(GTestConvertor, testsGetAddedToCurrentTestRegistry) +{ + TestTestingFixture fixture; + TestRegistry::getCurrentRegistry()->unDoLastAddTest(); + + GTestConvertor convertor(false); + convertor.addAllGTestToTestRegistry(); + + LONGS_EQUAL(9, TestRegistry::getCurrentRegistry()->countTests()); +} +#endif