Re: [Cppunit-devel] Mock C++
Brought to you by:
blep
From: Baptiste L. <gai...@fr...> - 2002-08-14 07:46:27
|
I actually implemented this as part of a library that come on top of CppUnit (but not yet released). The library implemented advanced assertion (equality with automatic conversion, collection equality...). The mock part of the library is build upon those assertion which make things much easier. Here is a example of a mock object in C++. Notes that it is slighty different than the java implementation. Compare this to the implementation of MockTestCase in cppunit test suite and you'll see how much easier it is... class MockProjectBuilder : public AbstractProjectBuilder , public XtlUt::MockObject { public: /*! Constructs a MockProjectBuilder object. */ MockProjectBuilder( const std::string &name ); /// Destructor. virtual ~MockProjectBuilder(); virtual void beginGroup( const std::string &groupName ); virtual void endGroup(); virtual void addSourceFile( const std::string &relativePath ); void setExpectNoGroup(); void addExpectedGroupName( const std::string &expectedGroupName ); void addExpectedEndGroup(); void setExpectNoSourceFile(); void addExpectedSourceFile( const std::string &expectedRelativePath ); void setExpectNoMethodCalls(); private: /// Prevents the use of the copy constructor. MockProjectBuilder( const MockProjectBuilder &other ); /// Prevents the use of the copy operator. void operator =( const MockProjectBuilder &other ); private: XtlUt::ExpectationCounter _endGroupCalls; XtlUt::ExpectationStringSequence _addSourceFileValues; XtlUt::ExpectationStringSequence _addGroupNames; XtlUt::ExpectationStringSequence _methodCalls; }; MockProjectBuilder::MockProjectBuilder( const std::string &name ) : MockObject( name ) , _addGroupNames( "beginGroup() parameter" ) , _endGroupCalls( "endGroup() calls" ) , _addSourceFileValues( "addSourceFile() parameter" ) , _methodCalls( "sequence of method calls" ) { add( _addGroupNames ); add( _endGroupCalls ); add( _addSourceFileValues ); add( _methodCalls ); } MockProjectBuilder::~MockProjectBuilder() { } void MockProjectBuilder::beginGroup( const std::string &groupName ) { _methodCalls.addActual( "beginGroup()" ); _addGroupNames.addActual( groupName ); } void MockProjectBuilder::endGroup() { _methodCalls.addActual( "endGroup()" ); _endGroupCalls.inc(); } void MockProjectBuilder::addSourceFile( const std::string &relativePath ) { _methodCalls.addActual( "addSourceFile()" ); _addSourceFileValues.addActual( relativePath ); } void MockProjectBuilder::setExpectNoGroup() { _addGroupNames.setExpectNothing(); _endGroupCalls.setExpectNothing(); } void MockProjectBuilder::addExpectedGroupName( const std::string &expectedGroupName ) { _addGroupNames.addExpected( expectedGroupName ); _methodCalls.addExpected( "beginGroup()" ); _endGroupCalls.setExpected( _addGroupNames.expectedValueCount() ); } void MockProjectBuilder::addExpectedEndGroup() { _methodCalls.addExpected( "endGroup()" ); } void MockProjectBuilder::setExpectNoSourceFile() { _addSourceFileValues.setExpectNothing(); } void MockProjectBuilder::addExpectedSourceFile( const std::string &expectedRelativePath ) { _addSourceFileValues.addExpected( expectedRelativePath ); _methodCalls.addExpected( "addSourceFile()" ); } void MockProjectBuilder::setExpectNoMethodCalls() { _methodCalls.setExpectNothing(); } Baptiste. ----- Original Message ----- From: "Philippe FREMY" <P....@OB...> To: <cpp...@li...> Sent: Tuesday, August 13, 2002 11:15 AM Subject: [Cppunit-devel] Mock C++ > > Hi, > > Have you guys thought about doing Mock objects for C++ ? It comes naturally > after Junit. > > More info about mocks: > > http://www.easymock.org/readme.html > http://www.mockobjects.com > > We probably can not reach the versatility of Java here, but we can sure do > something. > > regards, > > Philippe --- Baptiste Lepilleur <gai...@fr...> http://gaiacrtn.free.fr/ |