Update of /cvsroot/mockpp/mockpp/mockpp/examples/tutorial
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28988/mockpp/examples/tutorial
Added Files:
chainmock2.cpp
Log Message:
changes for mock method
--- NEW FILE: chainmock2.cpp ---
/***************************************************************************
chainmock.cpp - solve problem with chaining mocks
-------------------
begin : Sun 2 Jan 2005
copyright : (C) 2002-2005 by Ewald Arnold
email : mockpp at ewald-arnold dot de
$Id: chainmock2.cpp,v 1.1 2005/11/18 10:31:30 ewald-arnold Exp $
***************************************************************************/
#include <mockpp/mockpp.h>
#include "interface.h"
#include "consumer.h"
#include <mockpp/ChainableMockObject.h>
#include <mockpp/chaining/CountedChainableMethod.h>
#include <mockpp/chaining/ChainingMockObjectSupport.h>
#include <exception>
#include <iostream>
using namespace mockpp;
class ChainMock : public Interface
, public mockpp::ChainableMockObject
{
public:
ChainMock()
: mockpp::ChainableMockObject(MOCKPP_PCHAR("ChainMock"), 0)
, open_mocker(MOCKPP_PCHAR("open"), this)
, read_mocker(MOCKPP_PCHAR("read"), this)
, write_mocker(MOCKPP_PCHAR("write"), this)
, close_mocker(MOCKPP_PCHAR("close"), this)
, calculate_mocker(MOCKPP_PCHAR("calculate"), this)
{}
void open(const std::string &filename)
{
open_mocker.forward(filename);
}
std::string read()
{
return read_mocker.forward();
}
void write(const std::string &data)
{
write_mocker.forward(data);
}
unsigned calculate(unsigned input)
{
return calculate_mocker.forward(input);
}
void close()
{
close_mocker.forward();
}
mockpp::ChainableMockMethod<void, std::string> open_mocker;
mockpp::ChainableMockMethod<std::string> read_mocker;
mockpp::ChainableMockMethod<void, std::string> write_mocker;
mockpp::ChainableMockMethod<void> close_mocker;
mockpp::ChainableMockMethod<unsigned, unsigned> calculate_mocker;
};
int main(int /*argc*/, char ** /*argv*/)
{
try
{
ChainMock mock;
mockpp::ChainableMockMethod<void, std::string> &open_chainer (mock.open_mocker);
mockpp::ChainableMockMethod<std::string> &read_chainer (mock.read_mocker);
mockpp::ChainableMockMethod<void, std::string> &write_chainer (mock.write_mocker);
mockpp::ChainableMockMethod<void> &close_chainer (mock.close_mocker);
mockpp::ChainableMockMethod<unsigned, unsigned> &calculate_chainer (mock.calculate_mocker);
// Expectations for reading the file
open_chainer.expects(once())
.with(eq(std::string("file1.lst")))
.before(MOCKPP_PCHAR("reader"));
read_chainer.stubs()
.will(onConsecutiveCalls(new ReturnStub<std::string>("record-1"),
new ReturnStub<std::string>("record-2"),
new ReturnStub<std::string>("record-3")))
.id(MOCKPP_PCHAR("reader"));
close_chainer.expects(once())
.after(MOCKPP_PCHAR("reader"));
// Expectations for processing the file
calculate_chainer.expects(atLeast(3))
.with(eq<unsigned>(5, 5))
.after(MOCKPP_PCHAR("reader"))
.will(returnValue<unsigned>(1));
// Expectations for writing the file back
open_chainer.expects(once())
.with(eq(std::string("file1.lst")))
.before(MOCKPP_PCHAR("writer"));
write_chainer.expects(once())
.with(eq(std::string("record-1/processed")));
write_chainer.expects(once())
.with(eq(std::string("record-2/processed")));
write_chainer.expects(once())
.with(stringContains(std::string("processed")))
.id(MOCKPP_PCHAR("writer"));
close_chainer.expects(once())
.after(MOCKPP_PCHAR("writer"));
// Run Consumer object
std::cout << "Tests starting" << std::endl;
Consumer consumer(&mock);
consumer.load();
consumer.process();
consumer.save();
std::cout << "Tests finished" << std::endl;
// Check pending expectations
mock.verify();
std::cout << "All tests have passed successfully" << std::endl;
}
catch(std::exception &ex)
{
std::cout << std::endl
<< "Error occured.\n" << ex.what() << std::endl
<< std::endl;
return 1;
}
return 0;
}
|