[Mockpp-commits] mockpp/mockpp/framework CxxTestSupport.h,NONE,1.1 SelectUnittestFramework.h,NONE,1.
Brought to you by:
ewald-arnold
Update of /cvsroot/mockpp/mockpp/mockpp/framework In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32577/mockpp/framework Added Files: CxxTestSupport.h SelectUnittestFramework.h VerifyingTestCaller.cpp VerifyingTestCaller.h VerifyingTestCase.cpp VerifyingTestCase.h Log Message: moved to subdir framework --- NEW FILE: VerifyingTestCaller.cpp --- /** @file @brief Unit testcaller with verification $Id: VerifyingTestCaller.cpp,v 1.1 2005/12/10 19:23:45 ewald-arnold Exp $ ***************************************************************************/ /************************************************************************** begin : Sun Jan 23 2005 copyright : (C) 2002-2005 by Ewald Arnold email : mockpp at ewald-arnold dot de This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. **/ #include <mockpp/mockpp.h> // always first #include <mockpp/VerifyingTestCaller.h> namespace mockpp { // is a template } // namespace mockpp --- NEW FILE: VerifyingTestCaller.h --- /** @file @brief Unit VerifyingTestCaller with verification $Id: VerifyingTestCaller.h,v 1.1 2005/12/10 19:23:45 ewald-arnold Exp $ ***************************************************************************/ /************************************************************************** begin : Sun Jan 23 2005 copyright : (C) 2002-2005 by Ewald Arnold email : mockpp at ewald-arnold dot de This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. **/ #ifndef MOCKPP_VerifyingTestCaller_H #define MOCKPP_VerifyingTestCaller_H #include <mockpp/mockpp.h> // always first #include <mockpp/MockObject.h> #include <mockpp/VerifyingTestCase.h> #ifdef HAVE_CPPUNIT #include <cppunit/TestCaller.h> namespace mockpp { /** VerifyingTestCaller that verifies Verifiable * fields and registered Verifiable objects after the test has run and before the fixture * has been torn down. * @ingroup grp_basic_mo * @ingroup grp_advanced_mo */ template <typename VerifyingFixtureType, bool DoTheVerify> class VerifyingTestCaller : public ::CppUnit::TestCaller<VerifyingFixtureType> { typedef void (VerifyingFixtureType::*TestCaseMethod)(); public: /** Constructor for VerifyingTestCaller. * This constructor does not create a new Fixture instance but accepts * an existing one as parameter. The VerifyingTestCaller will not own the * Fixture object. * \param name name of this VerifyingTestCaller * \param test the method this VerifyingTestCaller calls in runTest() * \param fixture the Fixture to invoke the test method on. */ VerifyingTestCaller(const std::string &name, TestCaseMethod test, VerifyingFixtureType& fixture) : ::CppUnit::TestCaller<VerifyingFixtureType>(name, test, fixture) , testcase(&fixture) { } /** Constructor for VerifyingTestCaller. * This constructor does not create a new Fixture instance but accepts * an existing one as parameter. The VerifyingTestCaller will own the * Fixture object and delete it in its destructor. * \param name name of this VerifyingTestCaller * \param test the method this VerifyingTestCaller calls in runTest() * \param fixture the Fixture to invoke the test method on. */ VerifyingTestCaller(const std::string &name, TestCaseMethod test, VerifyingFixtureType* fixture) : ::CppUnit::TestCaller<VerifyingFixtureType>(name, test, fixture) , testcase(fixture) { } /** Runs the test method with mock object verification afterwards. */ virtual void runTest() { testcase->unregisterVerifiables(); MOCKPP_TRY { ::CppUnit::TestCaller<VerifyingFixtureType>::runTest(); bool doVerify = DoTheVerify; if (doVerify) testcase->verify(); } #ifndef MOCKPP_NO_EXCEPTIONS catch(...) { testcase->unregisterVerifiables(); MOCKPP_RETHROW; } #endif testcase->unregisterVerifiables(); } /** Returns the the callers string representation. */ MOCKPP_STL::string toString() const { return "VerifyingTestCaller " + this->getName(); } private: VerifyingTestCase *testcase; VerifyingTestCaller( const VerifyingTestCaller &other ); VerifyingTestCaller &operator =( const VerifyingTestCaller &other ); }; } // namespace mockpp /** Add a method to the suite and verify after its run. * Substitutes CPPUNIT_TEST for cases where you wish to call \c verify() * automatically after test completion. * \param testMethod Name of the method of the test case to add to the * suite. The signature of the method must be of * type: void testMethod(); * \see CPPUNIT_TEST. */ #define MOCKPP_VERIFYING_TEST( testMethod ) \ CPPUNIT_TEST_SUITE_ADD_TEST( \ ( new mockpp::VerifyingTestCaller<TestFixtureType, true>( \ context.getTestNameFor( #testMethod), \ &TestFixtureType::testMethod, \ context.makeFixture() ) ) ) /** Add a method to the suite (but do NOT verify after its run). * Substitutes CPPUNIT_TEST. * \param testMethod Name of the method of the test case to add to the * suite. The signature of the method must be of * type: void testMethod(); * \see CPPUNIT_TEST. */ #define MOCKPP_TEST( testMethod ) \ CPPUNIT_TEST_SUITE_ADD_TEST( \ ( new mockpp::VerifyingTestCaller<TestFixtureType, false>( \ context.getTestNameFor( #testMethod), \ &TestFixtureType::testMethod, \ context.makeFixture() ) ) ) /** Add a test which verifies after its run and fails if the specified * exception is not caught. * Substitutes CPPUNIT_TEST_EXCEPTION for cases where you wish to call \c verify() * automatically after test completion. * \param testMethod Name of the method of the test case to add to the suite. * \param ExceptionType Type of the exception that must be thrown by the test * method. * \see CPPUNIT_TEST_EXCEPTION. * \deprecated Use one of the assertion macros like MOCKPP_ASSERT_THROWING instead. */ #define MOCKPP_VERIFYING_TEST_EXCEPTION( testMethod, ExceptionType ) \ CPPUNIT_TEST_SUITE_ADD_TEST( \ (new ::CppUnit::ExceptionTestCaseDecorator< ExceptionType >( \ new mockpp::VerifyingTestCaller< TestFixtureType, true >( \ context.getTestNameFor( #testMethod ), \ &TestFixtureType::testMethod, \ context.makeFixture() ) ) ) ) /** Add a test (which does NOT verify after its run) and fails if the specified * exception is not caught. * Substitutes CPPUNIT_TEST_EXCEPTION. * \param testMethod Name of the method of the test case to add to the suite. * \param ExceptionType Type of the exception that must be thrown by the test * method. * \see CPPUNIT_TEST_EXCEPTION. * \deprecated Use one of the assertion macros like MOCKPP_ASSERT_THROWING instead. */ #define MOCKPP_TEST_EXCEPTION( testMethod, ExceptionType ) \ CPPUNIT_TEST_SUITE_ADD_TEST( \ (new ::CppUnit::ExceptionTestCaseDecorator< ExceptionType >( \ new mockpp::VerifyingTestCaller< TestFixtureType, false >( \ context.getTestNameFor( #testMethod ), \ &TestFixtureType::testMethod, \ context.makeFixture() ) ) ) ) #endif // HAVE_CPPUNIT #endif // MOCKPP_VerifyingTestCaller_H --- NEW FILE: VerifyingTestCase.h --- /** @file @brief Unit testcase with verification $Id: VerifyingTestCase.h,v 1.1 2005/12/10 19:23:45 ewald-arnold Exp $ ***************************************************************************/ /************************************************************************** begin : Sun Aug 22 2004 copyright : (C) 2002-2005 by Ewald Arnold email : mockpp at ewald-arnold dot de This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * Original Java Sources: Copyright (c) 2000-2004, jMock.org * **/ #ifndef MOCKPP_VERIFYINGTESTCASE_H #define MOCKPP_VERIFYINGTESTCASE_H #include <mockpp/mockpp.h> // always first #include <mockpp/MockObject.h> #include <mockpp/util/AutoPointer.h> #ifdef HAVE_CPPUNIT #include <cppunit/TestCaller.h> namespace mockpp { /** TestCase that verifies Verifiable * fields and registered Verifiable objects after the test has run and before the fixture * has been torn down. * @ingroup grp_basic_mo * @ingroup grp_advanced_mo */ class VerifyingTestCase : public CppUnit::TestCase , public MockObject { public: /** Constructs the test case. */ VerifyingTestCase(); /** Constructs the test case. * @param name test case name */ VerifyingTestCase( const std::string &name ); /** Constructs the test case. */ virtual ~VerifyingTestCase(); /** Verifies the object and the mock objects it contains. * @attention Having no sub-objetcs to verify also counts as an error * because it is assumed that registration of the objects had been * forgotten. Otherwise there would be no reason to use this class. * If it fails, an AssertionFailedError is thrown */ virtual void verify(); /** Removes all verifiable fields from the list. */ virtual void unregisterVerifiables(); protected: /** Registers another verifiable object to the list. * @param verifiable pointer to the verifiable object */ void registerVerifiable(AutoPointer<Verifiable> verifiable); private: VerifyingTestCase (const VerifyingTestCase &); // forbid VerifyingTestCase& operator=(VerifyingTestCase&); MOCKPP_STL::vector<Verifiable*> registeredVerifiables; }; } // namespace mockpp #endif // MOCKPP_VerifyingTestCaller_H #endif // MOCKPP_VERIFYINGTESTCASE_H #include <mockpp/VerifyingTestCaller.h> --- NEW FILE: VerifyingTestCase.cpp --- /** @file @brief Unit testcase with verification $Id: VerifyingTestCase.cpp,v 1.1 2005/12/10 19:23:45 ewald-arnold Exp $ ***************************************************************************/ /************************************************************************** begin : Sun Aug 22 2004 copyright : (C) 2002-2005 by Ewald Arnold email : mockpp at ewald-arnold dot de This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * Original Java Sources: Copyright (c) 2000-2004, jMock.org * **/ #include <mockpp/mockpp.h> // always first #include <mockpp/VerifyingTestCase.h> #include <mockpp/compat/Exception.h> #include <mockpp/compat/Formatter.h> #include <mockpp/compat/AssertionFailedError.h> #ifdef HAVE_CPPUNIT #include <cppunit/Exception.h> #include <cppunit/SourceLine.h> #include <cppunit/TestResult.h> namespace mockpp { MOCKPP_EXPORT VerifyingTestCase::VerifyingTestCase() : CppUnit::TestCase() , MockObject(MOCKPP_PCHAR("VerifyingTestCase/*"), 0) { } MOCKPP_EXPORT VerifyingTestCase::VerifyingTestCase( const std::string &name) : CppUnit::TestCase(name) #ifdef MOCKPP_ALTERNATIVE_STL , MockObject(MOCKPP_PCHAR("VerifyingTestCase/")+MOCKPP_GET_STRING(name.c_str()), 0) #else , MockObject(MOCKPP_PCHAR("VerifyingTestCase/")+MOCKPP_GET_STRING(name), 0) #endif { } MOCKPP_EXPORT VerifyingTestCase::~VerifyingTestCase() { unregisterVerifiables(); } void MOCKPP_EXPORT VerifyingTestCase::verify() { if (numVerifiables() == 0) { String s = MOCKPP_PCHAR("VerifyingTestCase \"%1\" has no sub-objects to verify.\n") MOCKPP_PCHAR("Did you forget to register?"); s << getVerifiableName(); assertionFailed (__LINE__, __FILE__, s); } MockObject::verify(); } void MOCKPP_EXPORT VerifyingTestCase::registerVerifiable(AutoPointer<Verifiable> in_verifiable) { Verifiable *verifiable = in_verifiable.release(); registeredVerifiables.push_back(verifiable); if (!hasVerifiable(verifiable)) addVerifiable(verifiable); if (verifiable->getParent() == 0) verifiable->setParent(this); } void MOCKPP_EXPORT VerifyingTestCase::unregisterVerifiables() { clearVerifiables(); for (unsigned i = 0; i < registeredVerifiables.size(); ++i) delete registeredVerifiables[i]; registeredVerifiables.clear(); } } // namespace mockpp #endif // HAVE_CPPUNIT --- NEW FILE: SelectUnittestFramework.h --- /*************************************************************************** SelectUnittestFramework.h - setup for unittest framework in use ------------------- begin : Fri Dec 9 2005 copyright : (C) 2002-2005 by Ewald Arnold email : mockpp at ewald-arnold dot de $Id: SelectUnittestFramework.h,v 1.1 2005/12/10 19:23:45 ewald-arnold Exp $ ***************************************************************************/ /************************************************************************** * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2 of the License, * or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * ***************************************************************************/ #ifndef MOCKPP_SELECTUNITTESTFRAMEWORK_H #define MOCKPP_SELECTUNITTESTFRAMEWORK_H #if defined(HAVE_CXXTEST) # define MOCKPP_DERIVE_PUBLIC_UNITFRAMEWORK : public CxxTest::TestSuite #define CXXTEST_HAVE_STD #define CXXTEST_ABORT_TEST_ON_FAIL #ifndef CXXTEST_RUNNING #define CXXTEST_RUNNING #endif #define _CXXTEST_HAVE_STD #define _CXXTEST_HAVE_EH #include <cxxtest/TestListener.h> #include <cxxtest/TestTracker.h> #include <cxxtest/RealDescriptions.h> //#include <cxxtest/TestRunner.h> #include <mockpp/CxxTestSupport.h> #elif defined(HAVE_CPPUNIT) # include <cppunit/extensions/HelperMacros.h> # define MOCKPP_DERIVE_PUBLIC_UNITFRAMEWORK : public CppUnit::TestFixture #else # pragma message ("No unittest framework available at compile time") # define MOCKPP_DERIVE_PUBLIC_UNITFRAMEWORK /**/ #endif // frameworks #endif // MOCKPP_SELECTUNITTESTFRAMEWORK_H --- NEW FILE: CxxTestSupport.h --- /** @file @brief Support for CxxTest similar to CppUnit $Id: CxxTestSupport.h,v 1.1 2005/12/10 19:23:45 ewald-arnold Exp $ **************************************************************************/ /************************************************************************** begin : Fri Dec 9 2005 copyright : (C) 2002-2005 by Ewald Arnold email : mockpp at ewald-arnold dot de This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. **/ #ifndef MOCKPP_CXXTEST_SUPPORT_H #define MOCKPP_CXXTEST_SUPPORT_H //////////////////////////////////////////////////////////////////////////// // #define MOCKPP_CXXTEST_SUITE_REGISTRATION(classname) \ static classname suite_ ## classname; \ \ static CxxTest::List tests_ ## classname = { 0, 0 }; \ \ CxxTest::StaticSuiteDescription suiteDescription_ ## classname( __FILE__, \ __LINE__, \ #classname, \ suite_ ## classname, \ tests_ ## classname) //////////////////////////////////////////////////////////////////////////// // #define MOCKPP_CXXTEST(classname, meth) \ static class TestDescription_ ## classname ## _ ## meth \ : public CxxTest::RealTestDescription { \ public: \ TestDescription_ ## classname ## _ ## meth() \ : CxxTest::RealTestDescription( tests_ ## classname, \ suiteDescription_ ##classname, \ __LINE__, \ #meth ) \ {} \ \ void runTest() \ { \ suite_ ## classname . meth(); \ } \ } testDescription_ ## classname ## _ ## meth //////////////////////////////////////////////////////////////////////////// // // #define MOCKPP_CXXTEST_SUITE_END #endif // MOCKPP_CXXTEST_SUPPORT_H |