RE: [Cppunit-devel] Mock C++
Brought to you by:
blep
From: Philippe F. <P....@OB...> - 2002-08-14 16:02:05
|
> 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; > }; Your example is not exactly crystal clear to me. Tell me if I understood correctly: 1. class ProjectBuilder has methods beginGroup(), endGroup() and addSouceFile() 2. to record the call being made, you use the addActual() function 3. to record the arguments of the functions, you use addActual() on ExpectationStringSequence that are specific to a function. 4. the setExpect stuff is here to match expect vs actual 5. you do not handle return values. If I understood right, your framework does not look very generic to me, and requires many manual calls. I think we can do better. To record the called made to a function, we could add a simple macro just after every function calls. Compilers usually have a __FUNCTION__ that contains the name of the function, so this could be automatic. It would record the function being called. Using a dictionnary behind that, you would be able to store and retrieve specific information about each function call. If we assume all objects can be compared using their string representation, we can store the expected and actual arguments of every function. The only thing I haven't been able to figure out yet is how to handle return values. I'll read some C++ books and see if I can find a solution. regards, Philippe |