[Cppunit-cvs] cppunit2/src/opentesttest mockhelper.h,NONE,1.1 remoteinterfacestest.cpp,NONE,1.1
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-09-06 07:43:55
|
Update of /cvsroot/cppunit/cppunit2/src/opentesttest In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18498/src/opentesttest Added Files: mockhelper.h remoteinterfacestest.cpp Log Message: * tests for remote Interfaces implementation in opentest --- NEW FILE: mockhelper.h --- #ifndef OPENTTEST_MOCKHELPER_H_INCLUDED # define OPENTTEST_MOCKHELPER_H_INCLUDED # include <opentest/properties.h> # include <cpput/assertenum.h> /** A simple helper to implement (simple) mock object. * * \code * helper_.logEvent( "call a()" ); // log what is expected to happened * helper_.logEvent( "call b()" ); * helper_.recordActualEvents(); // log what is actually happenning * helper_.logEvent( "call a()" ); * helper_.logEvent( "call c()" ); * MOCKHELPER_ASSERT_VERIFY( helper_ ); // check that the expected events match the actual events * \endcode */ class MockHelper { public: /// At construction, expected events are recorded. MockHelper(); void clearEvents(); bool isRecordingExpectedEvents() const; void recordExpectedEvents(); void recordActualEvents(); void verify( const CppUT::Message &message = CppUT::Message( "Actual events do not match expected events." ) ); void logEvent( const OpenTest::Value &value ); const OpenTest::Value &expectedEvents() const; const OpenTest::Value &actualEvents() const; private: OpenTest::Value expectations_; OpenTest::Value actuals_; bool recordExpectations_; }; #define MOCKHELPER_ASSERT_VERIFY( instance ) \ CPPUT_BEGIN_ASSERTION_MACRO() \ (instance).verify() #define MOCKHELPER_CHECK_VERIFY( instance ) \ CPPUT_BEGIN_CHECKING_MACRO() \ (instance).verify() #define MOCKHELPER_ASSERT_VERIFY_MSG( instance, msg ) \ CPPUT_BEGIN_ASSERTION_MACRO() \ (instance).verify( msg ) #define MOCKHELPER_CHECK_VERIFY_MSG( instance, msg ) \ CPPUT_BEGIN_CHECKING_MACRO() \ (instance).verify( msg ) inline MockHelper::MockHelper() : recordExpectations_( true ) , expectations_( OpenTest::Properties() ) , actuals_( OpenTest::Properties() ) { } inline void MockHelper::clearEvents() { expectations_ = OpenTest::Value(); actuals_ = OpenTest::Value(); } inline bool MockHelper::isRecordingExpectedEvents() const { return recordExpectations_; } inline void MockHelper::recordExpectedEvents() { recordExpectations_ = true; } inline void MockHelper::recordActualEvents() { recordExpectations_ = false; } inline void MockHelper::verify( const CppUT::Message &message ) { CppUT::checkSequenceEqual( expectations_.asProperties().listValues(), actuals_.asProperties().listValues(), message ); clearEvents(); recordExpectedEvents(); } inline void MockHelper::logEvent( const OpenTest::Value &value ) { if ( recordExpectations_ ) expectations_.append( value ); else actuals_.append( value ); } inline const OpenTest::Value & MockHelper::expectedEvents() const { return expectations_; } inline const OpenTest::Value & MockHelper::actualEvents() const { return actuals_; } # endif // OPENTTEST_MOCKHELPER_H_INCLUDED --- NEW FILE: remoteinterfacestest.cpp --- #include <opentest/serializedtesttransport.h> #include <cpput/testfixture.h> #include <cpput/registry.h> #include <cpptl/scopedptr.h> #include "mockhelper.h" namespace CppUT { // converter for assert equal inline std::string toString( const OpenTest::Properties &value ) { return value.toString().c_str(); } inline std::string toString( const OpenTest::Value &value ) { return value.toString().c_str(); } } // namespace CppUT { class MockTestRunner : public OpenTest::TestRunnerInterface , public OpenTest::TestRunnerServer , public MockHelper { public: MockTestRunner() : OpenTest::TestRunnerServer( static_cast<OpenTest::TestRunnerInterface &>(*this) ) { } public: // overridden from OpenTest::TestRunnerInterface virtual void getTestDescriptions() { logEvent( "getTestDescriptions" ); } virtual void getTestPlans() { logEvent( "getTestPlans" ); } virtual void runTests( const OpenTest::TestPlans &plan ) { logEvent( OpenTest::String("runTests( " + CppTL::toString( plan.testPlans_.size() ) + ")") ); } virtual void stopTests() { logEvent( "stopTests" ); } }; class RemoteInterfacesTest : public CppUT::TestFixture { CPPUT_TESTSUITE_BEGIN( RemoteInterfacesTest ); CPPUT_TEST( testSimpleMessage ); CPPUT_TEST( testTestRunnerProxy ); CPPUT_TESTSUITE_END(); public: void setUp(); void tearDown(); void testSimpleMessage(); void testTestRunnerProxy(); private: CppTL::ScopedPtr<OpenTest::SerializedTestTransport> transport_; CppTL::ScopedPtr<OpenTest::TestRunnerProxy> testRunnerProxy_; CppTL::ScopedPtr<MockTestRunner> testRunner_; }; CPPUT_REGISTER_SUITE_TO_DEFAULT( RemoteInterfacesTest ); void RemoteInterfacesTest::setUp() { transport_.reset( new OpenTest::SerializedTestTransport() ); testRunnerProxy_.reset( new OpenTest::TestRunnerProxy() ); testRunnerProxy_->setTransport( *transport_ ); testRunner_.reset( new MockTestRunner() ); } void RemoteInterfacesTest::tearDown() { testRunnerProxy_.reset(); transport_.reset(); testRunner_.reset(); } void RemoteInterfacesTest::testSimpleMessage() { testRunner_->getTestDescriptions(); testRunner_->recordActualEvents(); testRunnerProxy_->getTestDescriptions(); transport_->dispatchReceivedMessages( *testRunner_ ); MOCKHELPER_ASSERT_VERIFY( *testRunner_ ); } void RemoteInterfacesTest::testTestRunnerProxy() { testRunner_->getTestDescriptions(); testRunner_->getTestPlans(); OpenTest::TestPlans plans1; testRunner_->runTests( plans1 ); OpenTest::TestPlans plans2; OpenTest::TestPlan plan1; plan1.id_ = OpenTest::TestId( 5678 ); plan1.testCase_ = OpenTest::TestId( 1234 ); plan1.name_ = "dummy test"; plans2.testPlans_.push_back( plan1 ); testRunner_->runTests( plans2 ); testRunner_->stopTests(); testRunner_->recordActualEvents(); testRunnerProxy_->getTestDescriptions(); testRunnerProxy_->getTestPlans(); testRunnerProxy_->runTests( plans1 ); testRunnerProxy_->runTests( plans2 ); testRunnerProxy_->stopTests(); transport_->dispatchReceivedMessages( *testRunner_ ); MOCKHELPER_ASSERT_VERIFY( *testRunner_ ); } |