visitmock2.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002           visitmock.cpp  -  solve problem with visitable 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: visitmock2.cpp,v 1.6 2006/04/05 17:03:39 ewald-arnold Exp $
00010 
00011  ***************************************************************************/
00012 
00013 #define MOCKPP_IMPORT_ABBREVIATED
00014 #include <mockpp/mockpp.h> // always first
00015 
00016 #include <mockpp/visiting/VisitableMockObject.h>
00017 #include <mockpp/visiting/CountedVisitableMethod.h>
00018 
00019 #include <mockpp/chaining/ChainingMockObjectSupport.h>
00020 
00021 #include "interface.h"
00022 #include "consumer.h"
00023 
00024 #include <exception>
00025 #include <iostream>
00026 
00027 
00028 class VisitMock : public Interface
00029                 , public MOCKPP_NS::VisitableMockObject
00030 {
00031   public:
00032 
00033     VisitMock()
00034       : MOCKPP_NS::VisitableMockObject(MOCKPP_PCHAR("VisitMock"), 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 write(const MOCKPP_NS::ConstraintHolder<MOCKPP_STL::string> &ch)
00063     {
00064       write_mocker.forward(ch);
00065     }
00066 
00067     void calculate(const MOCKPP_NS::ConstraintHolder<unsigned> &ch)
00068     {
00069       calculate_mocker.forward(ch);
00070     }
00071 
00072     void close()
00073     {
00074       close_mocker.forward();
00075     }
00076 
00077     MOCKPP_NS::VisitableMockMethod<void, MOCKPP_STL::string>   open_mocker;
00078     MOCKPP_NS::VisitableMockMethod<MOCKPP_STL::string>         read_mocker;
00079     MOCKPP_NS::VisitableMockMethod<void, MOCKPP_STL::string>   write_mocker;
00080     MOCKPP_NS::VisitableMockMethod<void>                close_mocker;
00081     MOCKPP_NS::VisitableMockMethod<unsigned, unsigned>  calculate_mocker;
00082 };
00083 
00084 
00085 int main(int /*argc*/, char ** /*argv*/)
00086 {
00087   try
00088   {
00089     VisitMock mock;
00090     MOCKPP_NS::VisitableMockMethod<MOCKPP_STL::string> &read_controller (mock.read_mocker);
00091     MOCKPP_NS::VisitableMockMethod<unsigned, unsigned> &calculate_controller (mock.calculate_mocker);
00092 
00093     // record program flow while reading data
00094     mock.open("file1.lst");
00095     mock.read();
00096     mock.read();
00097     mock.read();
00098     mock.close();
00099 
00100     // provide return values for read()
00101     read_controller.addReturnValue("record-1");
00102     read_controller.addReturnValue("record-2");
00103     read_controller.addReturnValue("record-3");
00104 
00105     // processing is not exactly defined
00106 #if defined(_MSC_VER) && _MSC_VER <= 1300
00107     mock.calculate(new MOCKPP_NS::IsCloseTo<unsigned>(5, 5));
00108     mock.calculate(new MOCKPP_NS::IsCloseTo<unsigned>(5, 5));
00109     mock.calculate(new MOCKPP_NS::IsCloseTo<unsigned>(5, 5));
00110     calculate_controller.addResponseValue(10, new MOCKPP_NS::IsCloseTo<unsigned>(2, 2));
00111     calculate_controller.addResponseValue(20, new MOCKPP_NS::IsCloseTo<unsigned>(4, 2));
00112     calculate_controller.addResponseValue(30, new MOCKPP_NS::IsCloseTo<unsigned>(6, 2));
00113 #else
00114     mock.calculate(eq<unsigned>(5, 5));
00115     mock.calculate(eq<unsigned>(5, 5));
00116     mock.calculate(eq<unsigned>(5, 5));
00117     calculate_controller.addResponseValue(10, eq<unsigned>(2, 2));
00118     calculate_controller.addResponseValue(20, eq<unsigned>(4, 2));
00119     calculate_controller.addResponseValue(30, eq<unsigned>(6, 2));
00120 #endif
00121 
00122     // record program flow while writing data
00123     mock.open("file1.lst");
00124     mock.write("record-1/processed");
00125     mock.write(stringContains(MOCKPP_STL::string("processed")));
00126     mock.write(stringContains(MOCKPP_STL::string("processed")));
00127     mock.close();
00128 
00129     // activate mock object
00130     mock.activate();
00131 
00132     // Run Consumer object
00133     MOCKPP_STD_NS::cout << "Tests starting" << MOCKPP_STD_NS::endl;
00134 
00135     Consumer consumer(&mock);
00136     consumer.load();
00137     consumer.process();
00138     consumer.save();
00139 
00140     MOCKPP_STD_NS::cout << "Tests finished" << MOCKPP_STD_NS::endl;
00141 
00142     // Check pending expectations
00143     mock.verify();
00144 
00145     MOCKPP_STD_NS::cout << "All tests have passed successfully" << MOCKPP_STD_NS::endl;
00146   }
00147   catch(MOCKPP_STD_NS::exception &ex)
00148   {
00149     MOCKPP_STD_NS::cout << MOCKPP_STD_NS::endl
00150               << "Error occured.\n" << ex.what() << MOCKPP_STD_NS::endl
00151               << MOCKPP_STD_NS::endl;
00152     return 1;
00153   }
00154 
00155   return 0;
00156 }
00157 

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