visitmock.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: visitmock.cpp,v 1.19 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 
00018 #include <mockpp/chaining/ChainingMockObjectSupport.h>
00019 
00020 #include "interface.h"
00021 #include "consumer.h"
00022 
00023 #include <exception>
00024 #include <iostream>
00025 
00026 
00027 class VisitMock : public Interface
00028                 , public MOCKPP_NS::VisitableMockObject
00029 {
00030   public:
00031 
00032     VisitMock()
00033       : MOCKPP_NS::VisitableMockObject(MOCKPP_PCHAR("VisitMock"), 0)
00034       , MOCKPP_CONSTRUCT_MEMBERS_FOR_VOID_VISITABLE_EXT1(open, ext)
00035       , MOCKPP_CONSTRUCT_MEMBERS_FOR_VISITABLE0(read)
00036       , MOCKPP_CONSTRUCT_MEMBERS_FOR_VOID_VISITABLE_EXT1(write, ext)
00037       , MOCKPP_CONSTRUCT_MEMBERS_FOR_VISITABLE_EXT1(calculate, ext)
00038       , MOCKPP_CONSTRUCT_MEMBERS_FOR_VOID_VISITABLE0(close)
00039     {}
00040 
00041     MOCKPP_VOID_VISITABLE_EXT1(VisitMock, open, const MOCKPP_STL::string &,
00042                                           ext,  MOCKPP_STL::string);
00043 
00044     MOCKPP_VISITABLE0(VisitMock, MOCKPP_STL::string, read);
00045 
00046     MOCKPP_VOID_VISITABLE_EXT1(VisitMock, write, const MOCKPP_STL::string &,
00047                                           ext,   MOCKPP_STL::string);
00048 
00049     MOCKPP_VISITABLE_EXT1(VisitMock, unsigned, calculate, unsigned,
00050                                      unsigned, ext,       unsigned);
00051 
00052     MOCKPP_VOID_VISITABLE0(VisitMock, close);
00053 };
00054 
00055 
00056 int main(int /*argc*/, char ** /*argv*/)
00057 {
00058   try
00059   {
00060     VisitMock mock;
00061     MOCKPP_CONTROLLER_FOR(VisitMock, read) read_controller (&mock);
00062     MOCKPP_CONTROLLER_FOR_EXT(VisitMock, calculate, ext) calculate_controller (&mock);
00063 
00064     // record program flow while reading data
00065     mock.open("file1.lst");
00066     mock.read();
00067     mock.read();
00068     mock.read();
00069     mock.close();
00070 
00071     // provide return values for read()
00072     read_controller.addReturnValue("record-1");
00073     read_controller.addReturnValue("record-2");
00074     read_controller.addReturnValue("record-3");
00075 
00076     // processing is not exactly defined
00077 #if defined(_MSC_VER) && _MSC_VER <= 1300
00078     mock.calculate(new MOCKPP_NS::IsCloseTo<unsigned>(5, 5));
00079     mock.calculate(new MOCKPP_NS::IsCloseTo<unsigned>(5, 5));
00080     mock.calculate(new MOCKPP_NS::IsCloseTo<unsigned>(5, 5));
00081     calculate_controller.addResponseValue(10, new MOCKPP_NS::IsCloseTo<unsigned>(2, 2));
00082     calculate_controller.addResponseValue(20, new MOCKPP_NS::IsCloseTo<unsigned>(4, 2));
00083     calculate_controller.addResponseValue(30, new MOCKPP_NS::IsCloseTo<unsigned>(6, 2));
00084 #else
00085     mock.calculate(eq<unsigned>(5, 5));
00086     mock.calculate(eq<unsigned>(5, 5));
00087     mock.calculate(eq<unsigned>(5, 5));
00088     calculate_controller.addResponseValue(10, eq<unsigned>(2, 2));
00089     calculate_controller.addResponseValue(20, eq<unsigned>(4, 2));
00090     calculate_controller.addResponseValue(30, eq<unsigned>(6, 2));
00091 #endif
00092 
00093     // record program flow while writing data
00094     mock.open("file1.lst");
00095     mock.write("record-1/processed");
00096     mock.write(stringContains(MOCKPP_STL::string("processed")));
00097     mock.write(stringContains(MOCKPP_STL::string("processed")));
00098     mock.close();
00099 
00100     // activate mock object
00101     mock.activate();
00102 
00103     // Run Consumer object
00104     MOCKPP_STD_NS::cout << "Tests starting" << MOCKPP_STD_NS::endl;
00105 
00106     Consumer consumer(&mock);
00107     consumer.load();
00108     consumer.process();
00109     consumer.save();
00110 
00111     MOCKPP_STD_NS::cout << "Tests finished" << MOCKPP_STD_NS::endl;
00112 
00113     // Check pending expectations
00114     mock.verify();
00115 
00116     MOCKPP_STD_NS::cout << "All tests have passed successfully" << MOCKPP_STD_NS::endl;
00117   }
00118   catch(MOCKPP_STD_NS::exception &ex)
00119   {
00120     MOCKPP_STD_NS::cout << MOCKPP_STD_NS::endl
00121               << "Error occured.\n" << ex.what() << MOCKPP_STD_NS::endl
00122               << MOCKPP_STD_NS::endl;
00123     return 1;
00124   }
00125 
00126   return 0;
00127 }
00128 

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