chainmock.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: chainmock.cpp,v 1.16 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/mockpp_pti.h>
00019 #include <mockpp/chaining/ChainableMockObject.h>
00020 #include <mockpp/chaining/ChainingMockObjectSupport.h>
00021 
00022 #include <exception>
00023 #include <iostream>
00024 
00025 USING_NAMESPACE_MOCKPP
00026 
00027 
00028 typedef MOCKPP_NS::Invocation1<MOCKPP_STL::string> InvocationString;
00029 typedef MOCKPP_NS::Invocation1<unsigned> InvocationUInt;
00030 
00031 /*
00032 MOCKPP_PTI_IMPLEMENT(MOCKPP_NS::Invocation0);
00033 MOCKPP_PTI_IMPLEMENT(InvocationString);
00034 MOCKPP_PTI_IMPLEMENT(InvocationUInt);
00035 */
00036 
00037 class ChainMock : public Interface
00038                 , public MOCKPP_NS::ChainableMockObject
00039 {
00040   public:
00041 
00042     ChainMock()
00043       : MOCKPP_NS::ChainableMockObject(MOCKPP_PCHAR("ChainMock"), 0)
00044       , MOCKPP_CONSTRUCT_MEMBERS_FOR_VOID_CHAINABLE_EXT1(open, ext)
00045       , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE0(read)
00046       , MOCKPP_CONSTRUCT_MEMBERS_FOR_VOID_CHAINABLE_EXT1(write, ext)
00047       , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE_EXT1(calculate, ext)
00048       , MOCKPP_CONSTRUCT_MEMBERS_FOR_VOID_CHAINABLE0(close)
00049     {}
00050 
00051     MOCKPP_VOID_CHAINABLE_EXT1(ChainMock, open, const MOCKPP_STL::string &,
00052                                           ext,  MOCKPP_STL::string);
00053 
00054     MOCKPP_CHAINABLE0(ChainMock, MOCKPP_STL::string, read);
00055 
00056     MOCKPP_VOID_CHAINABLE_EXT1(ChainMock, write, const MOCKPP_STL::string &,
00057                                           ext,   MOCKPP_STL::string);
00058 
00059     MOCKPP_CHAINABLE_EXT1(ChainMock, unsigned, calculate, unsigned,
00060                                      unsigned, ext,       unsigned);
00061 
00062     MOCKPP_VOID_CHAINABLE0(ChainMock, close);
00063 };
00064 
00065 
00066 int main(int /*argc*/, char ** /*argv*/)
00067 {
00068   try
00069   {
00070     ChainMock mock;
00071     MOCKPP_CHAINER_FOR_EXT(ChainMock, open, ext) open_chainer (&mock);
00072     MOCKPP_CHAINER_FOR(ChainMock, read) read_chainer (&mock);
00073     MOCKPP_CHAINER_FOR_EXT(ChainMock, write, ext) write_chainer (&mock);
00074     MOCKPP_CHAINER_FOR(ChainMock, close) close_chainer (&mock);
00075     MOCKPP_CHAINER_FOR_EXT(ChainMock, calculate, ext) calculate_chainer (&mock);
00076 
00077     // Expectations for reading the file
00078     open_chainer.expects(once())
00079                 .with(eq(MOCKPP_STL::string("file1.lst")))
00080                 .before(MOCKPP_PCHAR("reader"));
00081 
00082     read_chainer.stubs()
00083                 .will(onConsecutiveCalls(new ReturnStub<MOCKPP_STL::string>("record-1"),
00084                                          new ReturnStub<MOCKPP_STL::string>("record-2"),
00085                                          new ReturnStub<MOCKPP_STL::string>("record-3")))
00086                 .id(MOCKPP_PCHAR("reader"));
00087 
00088     close_chainer.expects(once())
00089                  .after(MOCKPP_PCHAR("reader"));
00090 
00091 
00092     // Expectations for processing the file
00093     calculate_chainer.expects(atLeast(3))
00094                      .with(eq<unsigned>(5, 5))
00095                      .after(MOCKPP_PCHAR("reader"))
00096                      .will(returnValue<unsigned>(1));
00097 
00098     // Expectations for writing the file back
00099     open_chainer.expects(once())
00100                 .with(eq(MOCKPP_STL::string("file1.lst")))
00101                 .before(MOCKPP_PCHAR("writer"));
00102 
00103     write_chainer.expects(once())
00104                  .with(eq(MOCKPP_STL::string("record-1/processed")));
00105     write_chainer.expects(once())
00106                  .with(eq(MOCKPP_STL::string("record-2/processed")));
00107     write_chainer.expects(once())
00108                  .with(stringContains(MOCKPP_STL::string("processed")))
00109                  .id(MOCKPP_PCHAR("writer"));
00110 
00111     close_chainer.expects(once())
00112                  .after(MOCKPP_PCHAR("writer"));
00113 
00114 
00115     // Run Consumer object
00116     MOCKPP_STD_NS::cout << "Tests starting" << MOCKPP_STD_NS::endl;
00117 
00118     Consumer consumer(&mock);
00119     consumer.load();
00120     consumer.process();
00121     consumer.save();
00122 
00123     MOCKPP_STD_NS::cout << "Tests finished" << MOCKPP_STD_NS::endl;
00124 
00125     // Check pending expectations
00126     mock.verify();
00127     MOCKPP_STD_NS::cout << "All tests have passed successfully" << MOCKPP_STD_NS::endl;
00128   }
00129   catch(MOCKPP_STD_NS::exception &ex)
00130   {
00131     MOCKPP_STD_NS::cout << MOCKPP_STD_NS::endl
00132               << "Error occured.\n" << ex.what() << MOCKPP_STD_NS::endl
00133               << MOCKPP_STD_NS::endl;
00134     return 1;
00135   }
00136 
00137   return 0;
00138 }
00139 
00140 
00141 

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