00001 /*************************************************************************** 00002 poormock.cpp - solve problem with really simple mocks 00003 00004 ------------------- 00005 begin : Sun 2 Jan 2005 00006 copyright : (C) 2002-2010 by Ewald Arnold 00007 email : mockpp at ewald-arnold dot de 00008 00009 $Id: poormock.cpp 1491 2010-01-02 22:21:45Z ewald-arnold $ 00010 00011 ***************************************************************************/ 00012 00013 #include "interface.h" 00014 #include "consumer.h" 00015 00016 #include <iostream> 00017 00018 #include <cstdlib> 00019 00020 #ifndef _MSC_VER 00021 using MOCKPP_STD_NS::exit; 00022 #endif 00023 00028 #define ASSERTER(x) \ 00029 if (!(x) ) { \ 00030 MOCKPP_STD_NS::cout << MOCKPP_STD_NS::endl \ 00031 << "Error occured.\n" << #x << MOCKPP_STD_NS::endl \ 00032 << MOCKPP_STD_NS::endl \ 00033 << MOCKPP_STD_NS::flush; \ 00034 exit(1); \ 00035 } 00036 00037 00038 class PoorMock : public Interface 00039 { 00040 public: 00041 00042 PoorMock() 00043 { 00044 counter = 0; 00045 open_counter = 0; 00046 read_counter = 0; 00047 write_counter = 0; 00048 close_counter = 0; 00049 } 00050 00051 virtual void open(const MOCKPP_STL::string & /* name */) 00052 { 00053 open_counter = ++counter; 00054 } 00055 00056 virtual MOCKPP_STL::string read() 00057 { 00058 read_counter = ++counter; 00059 return "dummy"; 00060 } 00061 00062 virtual void write(const MOCKPP_STL::string & /* s */) 00063 { 00064 write_counter = ++counter; 00065 } 00066 00067 virtual unsigned calculate(unsigned input) 00068 { 00069 return input + 1; 00070 } 00071 00072 virtual void close() 00073 { 00074 close_counter = ++counter; 00075 } 00076 00077 unsigned counter; 00078 unsigned open_counter; 00079 unsigned read_counter; 00080 unsigned write_counter; 00081 unsigned close_counter; 00082 }; 00083 00084 00085 int main(int /*argc*/, char ** /*argv*/) 00086 { 00087 PoorMock mock; 00088 00089 MOCKPP_STD_NS::cout << "Tests starting" << MOCKPP_STD_NS::endl; 00090 00091 // Run Consumer object 00092 Consumer consumer(&mock); 00093 consumer.load(); 00094 00095 ASSERTER(mock.open_counter == 1); 00096 ASSERTER(mock.read_counter == 4); 00097 ASSERTER(mock.close_counter == 5); 00098 00099 consumer.process(); 00100 consumer.save(); 00101 00102 ASSERTER(mock.open_counter == 6); 00103 ASSERTER(mock.write_counter == 9); 00104 ASSERTER(mock.close_counter == 10); 00105 00106 MOCKPP_STD_NS::cout << "Tests finished" << MOCKPP_STD_NS::endl; 00107 00108 MOCKPP_STD_NS::cout << "All tests have passed successfully" << MOCKPP_STD_NS::endl; 00109 return 0; 00110 } 00111