00001 /*************************************************************************** 00002 poormock.cpp - solve problem with really simple 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: poormock.cpp,v 1.11 2006/04/05 17:03:39 ewald-arnold Exp $ 00010 00011 ***************************************************************************/ 00012 00013 #include "interface.h" 00014 #include "consumer.h" 00015 00016 #include <iostream> 00017 00018 #ifndef _MSC_VER 00019 using MOCKPP_STD_NS::exit; 00020 #endif 00021 00026 #define ASSERTER(x) \ 00027 if (!(x) ) { \ 00028 MOCKPP_STD_NS::cout << MOCKPP_STD_NS::endl \ 00029 << "Error occured.\n" << #x << MOCKPP_STD_NS::endl \ 00030 << MOCKPP_STD_NS::endl \ 00031 << MOCKPP_STD_NS::flush; \ 00032 exit(1); \ 00033 } 00034 00035 00036 class PoorMock : public Interface 00037 { 00038 public: 00039 00040 PoorMock() 00041 { 00042 counter = 0; 00043 open_counter = 0; 00044 read_counter = 0; 00045 write_counter = 0; 00046 close_counter = 0; 00047 } 00048 00049 virtual void open(const MOCKPP_STL::string & /* name */) 00050 { 00051 open_counter = ++counter; 00052 } 00053 00054 virtual MOCKPP_STL::string read() 00055 { 00056 read_counter = ++counter; 00057 return "dummy"; 00058 } 00059 00060 virtual void write(const MOCKPP_STL::string & /* s */) 00061 { 00062 write_counter = ++counter; 00063 } 00064 00065 virtual unsigned calculate(unsigned input) 00066 { 00067 return input + 1; 00068 } 00069 00070 virtual void close() 00071 { 00072 close_counter = ++counter; 00073 } 00074 00075 unsigned counter; 00076 unsigned open_counter; 00077 unsigned read_counter; 00078 unsigned write_counter; 00079 unsigned close_counter; 00080 }; 00081 00082 00083 int main(int /*argc*/, char ** /*argv*/) 00084 { 00085 PoorMock mock; 00086 00087 MOCKPP_STD_NS::cout << "Tests starting" << MOCKPP_STD_NS::endl; 00088 00089 // Run Consumer object 00090 Consumer consumer(&mock); 00091 consumer.load(); 00092 00093 ASSERTER(mock.open_counter == 1); 00094 ASSERTER(mock.read_counter == 4); 00095 ASSERTER(mock.close_counter == 5); 00096 00097 consumer.process(); 00098 consumer.save(); 00099 00100 ASSERTER(mock.open_counter == 6); 00101 ASSERTER(mock.write_counter == 9); 00102 ASSERTER(mock.close_counter == 10); 00103 00104 MOCKPP_STD_NS::cout << "Tests finished" << MOCKPP_STD_NS::endl; 00105 00106 MOCKPP_STD_NS::cout << "All tests have passed successfully" << MOCKPP_STD_NS::endl; 00107 return 0; 00108 } 00109