basicmock.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002           basicmock.cpp  -  solve problem with basic 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: basicmock.cpp,v 1.11 2006/04/05 17:03:39 ewald-arnold Exp $
00010 
00011  ***************************************************************************/
00012 
00013 #include <mockpp/mockpp.h> // always first
00014 
00015 #include <mockpp/MockObject.h>
00016 #include <mockpp/ExpectationList.h>
00017 #include <mockpp/ReturnObjectList.h>
00018 #include <mockpp/ExpectationCounter.h>
00019 
00020 #include <mockpp/constraint/ConstraintList.h>
00021 
00022 #include <mockpp/chaining/ChainingMockObjectSupport.h>
00023 
00024 #include "interface.h"
00025 #include "consumer.h"
00026 
00027 #include <exception>
00028 #include <iostream>
00029 
00030 USING_NAMESPACE_MOCKPP
00031 
00032 
00033 class BasicMock : public Interface
00034                 , public MOCKPP_NS::MockObject
00035 {
00036   public:
00037 
00038     BasicMock()
00039       : MOCKPP_NS::MockObject(MOCKPP_PCHAR("BasicMock"), 0)
00040       , open_name(MOCKPP_PCHAR("BasicMock/open_name"), this)
00041       , close_counter(MOCKPP_PCHAR("BasicMock/close_counter"), this)
00042       , write_data(MOCKPP_PCHAR("BasicMock/write_data"), this)
00043       , read_data(MOCKPP_PCHAR("BasicMock/read_data"), this)
00044       , calculate_output(MOCKPP_PCHAR("BasicMock/calculate_output"), this)
00045       , calculate_input(MOCKPP_PCHAR("BasicMock/calculate_input"), this)
00046     {
00047     }
00048 
00049     virtual void open(const MOCKPP_STL::string &name)
00050     {
00051       open_name.addActual(name);
00052     }
00053 
00054     virtual MOCKPP_STL::string read()
00055     {
00056       return read_data.nextReturnObject();
00057     }
00058 
00059     virtual void write(const MOCKPP_STL::string &s)
00060     {
00061       write_data.addActual(s);
00062     }
00063 
00064     virtual unsigned calculate(unsigned input)
00065     {
00066       calculate_input.addActual(input);
00067       return calculate_output.nextReturnObject();
00068     }
00069 
00070     virtual void close()
00071     {
00072       close_counter.inc();
00073     }
00074 
00075     MOCKPP_NS::ExpectationList<MOCKPP_STL::string>  open_name;
00076     MOCKPP_NS::ExpectationCounter            close_counter;
00077     MOCKPP_NS::ExpectationList<MOCKPP_STL::string>  write_data;
00078     MOCKPP_NS::ReturnObjectList<MOCKPP_STL::string> read_data;
00079     MOCKPP_NS::ReturnObjectList<unsigned>    calculate_output;
00080     MOCKPP_NS::ConstraintList<unsigned >     calculate_input;
00081 };
00082 
00083 
00084 int main(int /*argc*/, char ** /*argv*/)
00085 {
00086   try
00087   {
00088     BasicMock mock;
00089 
00090     // open() on reading and writing
00091     mock.open_name.addExpected("file1.lst");
00092     mock.open_name.addExpected("file1.lst");
00093 
00094     // close() on reading and writing
00095     mock.close_counter.setExpected(2);
00096 
00097     // Read exactly 3 lines
00098     mock.read_data.addObjectToReturn("record-1");
00099     mock.read_data.addObjectToReturn("record-2");
00100     mock.read_data.addObjectToReturn("record-3");
00101 
00102     // Write exactly 3 lines
00103     mock.write_data.addExpected("record-1/processed");
00104     mock.write_data.addExpected("record-2/processed");
00105     mock.write_data.addExpected("record-3/processed");
00106 
00107     // processing is not exactly defined
00108     mock.calculate_input.addExpected(eq<unsigned>(5,5));
00109     mock.calculate_input.addExpected(eq<unsigned>(5,5));
00110     mock.calculate_input.addExpected(eq<unsigned>(5,5));
00111     mock.calculate_output.addObjectToReturn(10);
00112     mock.calculate_output.addObjectToReturn(20);
00113     mock.calculate_output.addObjectToReturn(30);
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 }

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