chainmock2.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002           chainmock.cpp  -  solve problem with chaining mocks
00003 
00004                              -------------------
00005     begin                : Sun 2 Jan 2005
00006     copyright            : (C) 2002-2006 by Ewald Arnold
00007     email                : mockpp at ewald-arnold dot de
00008 
00009   $Id: chainmock2.cpp,v 1.6 2006/04/05 17:03:39 ewald-arnold Exp $
00010 
00011  ***************************************************************************/
00012 
00013 #include <mockpp/mockpp.h>
00014 
00015 #include "interface.h"
00016 #include "consumer.h"
00017 
00018 #include <mockpp/chaining/ChainableMockObject.h>
00019 
00020 #include <mockpp/chaining/CountedChainableMethod.h>
00021 #include <mockpp/chaining/ChainingMockObjectSupport.h>
00022 
00023 #include <exception>
00024 #include <iostream>
00025 
00026 USING_NAMESPACE_MOCKPP
00027 
00028 class ChainMock : public Interface
00029                 , public MOCKPP_NS::ChainableMockObject
00030 {
00031   public:
00032 
00033     ChainMock()
00034       : MOCKPP_NS::ChainableMockObject(MOCKPP_PCHAR("ChainMock"), 0)
00035       , open_mocker(MOCKPP_PCHAR("open"), this)
00036       , read_mocker(MOCKPP_PCHAR("read"), this)
00037       , write_mocker(MOCKPP_PCHAR("write"), this)
00038       , close_mocker(MOCKPP_PCHAR("close"), this)
00039       , calculate_mocker(MOCKPP_PCHAR("calculate"), this)
00040     {}
00041 
00042     void open(const MOCKPP_STL::string &filename)
00043     {
00044       open_mocker.forward(filename);
00045     }
00046 
00047     MOCKPP_STL::string read()
00048     {
00049       return read_mocker.forward();
00050     }
00051 
00052     void write(const MOCKPP_STL::string &data)
00053     {
00054       write_mocker.forward(data);
00055     }
00056 
00057     unsigned calculate(unsigned input)
00058     {
00059       return calculate_mocker.forward(input);
00060     }
00061 
00062     void close()
00063     {
00064       close_mocker.forward();
00065     }
00066 
00067     MOCKPP_NS::ChainableMockMethod<void, MOCKPP_STL::string>   open_mocker;
00068     MOCKPP_NS::ChainableMockMethod<MOCKPP_STL::string>         read_mocker;
00069     MOCKPP_NS::ChainableMockMethod<void, MOCKPP_STL::string>   write_mocker;
00070     MOCKPP_NS::ChainableMockMethod<void>                close_mocker;
00071     MOCKPP_NS::ChainableMockMethod<unsigned, unsigned>  calculate_mocker;
00072 };
00073 
00074 
00075 int main(int /*argc*/, char ** /*argv*/)
00076 {
00077   try
00078   {
00079     ChainMock mock;
00080 
00081     MOCKPP_NS::ChainableMockMethod<void, MOCKPP_STL::string>  &open_chainer  (mock.open_mocker);
00082     MOCKPP_NS::ChainableMockMethod<MOCKPP_STL::string>        &read_chainer  (mock.read_mocker);
00083     MOCKPP_NS::ChainableMockMethod<void, MOCKPP_STL::string>  &write_chainer (mock.write_mocker);
00084     MOCKPP_NS::ChainableMockMethod<void>               &close_chainer (mock.close_mocker);
00085     MOCKPP_NS::ChainableMockMethod<unsigned, unsigned> &calculate_chainer (mock.calculate_mocker);
00086 
00087     // Expectations for reading the file
00088     open_chainer.expects(once())
00089                 .with(eq(MOCKPP_STL::string("file1.lst")))
00090                 .before(MOCKPP_PCHAR("reader"));
00091 
00092     read_chainer.stubs()
00093                 .will(onConsecutiveCalls(new ReturnStub<MOCKPP_STL::string>("record-1"),
00094                                          new ReturnStub<MOCKPP_STL::string>("record-2"),
00095                                          new ReturnStub<MOCKPP_STL::string>("record-3")))
00096                 .id(MOCKPP_PCHAR("reader"));
00097 
00098     close_chainer.expects(once())
00099                  .after(MOCKPP_PCHAR("reader"));
00100 
00101 
00102     // Expectations for processing the file
00103     calculate_chainer.expects(atLeast(3))
00104                      .with(eq<unsigned>(5, 5))
00105                      .after(MOCKPP_PCHAR("reader"))
00106                      .will(returnValue<unsigned>(1));
00107 
00108     // Expectations for writing the file back
00109     open_chainer.expects(once())
00110                 .with(eq(MOCKPP_STL::string("file1.lst")))
00111                 .before(MOCKPP_PCHAR("writer"));
00112 
00113     write_chainer.expects(once())
00114                  .with(eq(MOCKPP_STL::string("record-1/processed")));
00115     write_chainer.expects(once())
00116                  .with(eq(MOCKPP_STL::string("record-2/processed")));
00117     write_chainer.expects(once())
00118                  .with(stringContains(MOCKPP_STL::string("processed")))
00119                  .id(MOCKPP_PCHAR("writer"));
00120 
00121     close_chainer.expects(once())
00122                  .after(MOCKPP_PCHAR("writer"));
00123 
00124 
00125     // Run Consumer object
00126     MOCKPP_STD_NS::cout << "Tests starting" << MOCKPP_STD_NS::endl;
00127 
00128     Consumer consumer(&mock);
00129     consumer.load();
00130     consumer.process();
00131     consumer.save();
00132 
00133     MOCKPP_STD_NS::cout << "Tests finished" << MOCKPP_STD_NS::endl;
00134 
00135     // Check pending expectations
00136     mock.verify();
00137     MOCKPP_STD_NS::cout << "All tests have passed successfully" << MOCKPP_STD_NS::endl;
00138   }
00139   catch(MOCKPP_STD_NS::exception &ex)
00140   {
00141     MOCKPP_STD_NS::cout << MOCKPP_STD_NS::endl
00142               << "Error occured.\n" << ex.what() << MOCKPP_STD_NS::endl
00143               << MOCKPP_STD_NS::endl;
00144     return 1;
00145   }
00146 
00147   return 0;
00148 }
00149 
00150 
00151 

Generated on Sat Apr 8 21:05:52 2006 for mockpp-tutorial by  doxygen 1.4.4