Update of /cvsroot/mockpp/mockpp/mockpp/examples/tutorial
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27173/mockpp/examples/tutorial
Added Files:
visitmock2.cpp
Log Message:
changes formock method
--- NEW FILE: visitmock2.cpp ---
/***************************************************************************
visitmock.cpp - solve problem with visitable mocks
-------------------
begin : Sun 2 Jan 2005
copyright : (C) 2002-2005 by Ewald Arnold
email : mockpp at ewald-arnold dot de
$Id: visitmock2.cpp,v 1.1 2005/11/18 10:23:27 ewald-arnold Exp $
***************************************************************************/
#define MOCKPP_IMPORT_ABBREVIATED
#include <mockpp/mockpp.h> // always first
#include <mockpp/VisitableMockObject.h>
#include <mockpp/CountedVisitableMethod.h>
#include <mockpp/chaining/ChainingMockObjectSupport.h>
#include "interface.h"
#include "consumer.h"
#include <exception>
#include <iostream>
class VisitMock : public Interface
, public mockpp::VisitableMockObject
{
public:
VisitMock()
: mockpp::VisitableMockObject(MOCKPP_PCHAR("VisitMock"), 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 write(const mockpp::ConstraintHolder<std::string> &ch)
{
write_mocker.forward(ch);
}
void calculate(const mockpp::ConstraintHolder<unsigned> &ch)
{
calculate_mocker.forward(ch);
}
void close()
{
close_mocker.forward();
}
mockpp::VisitableMockMethod<void, std::string> open_mocker;
mockpp::VisitableMockMethod<std::string> read_mocker;
mockpp::VisitableMockMethod<void, std::string> write_mocker;
mockpp::VisitableMockMethod<void> close_mocker;
mockpp::VisitableMockMethod<unsigned, unsigned> calculate_mocker;
};
int main(int /*argc*/, char ** /*argv*/)
{
try
{
VisitMock mock;
mockpp::VisitableMockMethod<std::string> &read_controller (mock.read_mocker);
mockpp::VisitableMockMethod<unsigned, unsigned> &calculate_controller (mock.calculate_mocker);
// record program flow while reading data
mock.open("file1.lst");
mock.read();
mock.read();
mock.read();
mock.close();
// provide return values for read()
read_controller.addReturnValue("record-1");
read_controller.addReturnValue("record-2");
read_controller.addReturnValue("record-3");
// processing is not exactly defined
#if defined(_MSC_VER) && _MSC_VER <= 1300
mock.calculate(new mockpp::IsCloseTo<unsigned>(5, 5));
mock.calculate(new mockpp::IsCloseTo<unsigned>(5, 5));
mock.calculate(new mockpp::IsCloseTo<unsigned>(5, 5));
calculate_controller.addResponseValue(10, new mockpp::IsCloseTo<unsigned>(2, 2));
calculate_controller.addResponseValue(20, new mockpp::IsCloseTo<unsigned>(4, 2));
calculate_controller.addResponseValue(30, new mockpp::IsCloseTo<unsigned>(6, 2));
#else
mock.calculate(eq<unsigned>(5, 5));
mock.calculate(eq<unsigned>(5, 5));
mock.calculate(eq<unsigned>(5, 5));
calculate_controller.addResponseValue(10, eq<unsigned>(2, 2));
calculate_controller.addResponseValue(20, eq<unsigned>(4, 2));
calculate_controller.addResponseValue(30, eq<unsigned>(6, 2));
#endif
// record program flow while writing data
mock.open("file1.lst");
mock.write("record-1/processed");
mock.write(stringContains(std::string("processed")));
mock.write(stringContains(std::string("processed")));
mock.close();
// activate mock object
mock.activate();
// 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;
}
|