poormock-se.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002     poormock-se.cpp  -  solve problem with simple mocks, Second Edition
00003 
00004                              -------------------
00005     begin                : Fri Feb 18 2005
00006     copyright            : (C) 2002-2006 by Ewald Arnold
00007     email                : mockpp at ewald-arnold dot de
00008 
00009   $Id: poormock-se.cpp,v 1.9 2006/04/05 17:03:39 ewald-arnold Exp $
00010 
00011  ***************************************************************************/
00012 
00013 #include <mockpp/mockpp.h> // always first
00014 
00015 #include "interface.h"
00016 #include "consumer.h"
00017 
00018 #include <iostream>
00019 
00020 #include <mockpp/TrackingCounter.h>
00021 #include <mockpp/MockObject.h>
00022 
00023 
00024 class PoorMockSecondEdition : public Interface
00025                             , public MOCKPP_NS::MockObject
00026 {
00027   private:
00028 
00029     MOCKPP_NS::TrackingCounterMaster counter;
00030 
00031   public:
00032 
00033     PoorMockSecondEdition()
00034       : MOCKPP_NS::MockObject(MOCKPP_PCHAR("MockObject"))
00035       , counter(MOCKPP_PCHAR("counter"), this)
00036       , open_counter(MOCKPP_PCHAR("open_counter"), counter)
00037       , read_counter(MOCKPP_PCHAR("read_counter"), counter)
00038       , write_counter(MOCKPP_PCHAR("write_counter"), counter)
00039       , close_counter(MOCKPP_PCHAR("close_counter"), counter)
00040     {
00041     }
00042 
00043     virtual void open(const MOCKPP_STL::string & /* name */)
00044     {
00045       open_counter++;
00046     }
00047 
00048     virtual MOCKPP_STL::string read()
00049     {
00050       read_counter++;
00051       return "dummy";
00052     }
00053 
00054     virtual void write(const MOCKPP_STL::string & /* s */)
00055     {
00056       write_counter++;
00057     }
00058 
00059     virtual unsigned calculate(unsigned input)
00060     {
00061       return input + 1;
00062     }
00063 
00064     virtual void close()
00065     {
00066       close_counter++;
00067     }
00068 
00069     MOCKPP_NS::TrackingCounterClient open_counter;
00070     MOCKPP_NS::TrackingCounterClient read_counter;
00071     MOCKPP_NS::TrackingCounterClient write_counter;
00072     MOCKPP_NS::TrackingCounterClient close_counter;
00073 };
00074 
00075 
00076 int main(int /*argc*/, char ** /*argv*/)
00077 {
00078   PoorMockSecondEdition mock;
00079 
00080   MOCKPP_STD_NS::cout << "Tests starting" << MOCKPP_STD_NS::endl;
00081 
00082   try
00083   {
00084     // Run Consumer object
00085     Consumer consumer(&mock);
00086     mock.open_counter.setExpected(1);
00087     mock.read_counter.setExpected(4);
00088     mock.close_counter.setExpected(5);
00089     consumer.load();
00090     mock.verify();
00091 
00092     consumer.process();
00093     mock.open_counter.setExpected(6);
00094     mock.write_counter.setExpected(9);
00095     mock.close_counter.setExpected(10);
00096     consumer.save();
00097     mock.verify();
00098 
00099     MOCKPP_STD_NS::cout << "Tests finished" << MOCKPP_STD_NS::endl;
00100 
00101   }
00102   catch(MOCKPP_STD_NS::exception &ex)
00103   {
00104     MOCKPP_STD_NS::cout << MOCKPP_STD_NS::endl
00105               << "Error occured.\n" << ex.what() << MOCKPP_STD_NS::endl
00106               << MOCKPP_STD_NS::endl;
00107     return 1;
00108   }
00109 
00110   MOCKPP_STD_NS::cout << "All tests have passed successfully" << MOCKPP_STD_NS::endl;
00111   return 0;
00112 }
00113 

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