cppunit.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002              cppunit.cpp  -  example usage of cppunit
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: cppunit.cpp,v 1.16 2006/03/15 21:34:04 ewald-arnold Exp $
00010 
00011  ***************************************************************************/
00012 
00013 #include <mockpp/mockpp.h> // always first
00014 
00015 #include <mockpp/Throwable.h>
00016 #include <mockpp/visiting/VisitableMockObject.h>
00017 #include <mockpp/chaining/ChainableMockObject.h>
00018 #include <mockpp/chaining/ChainingMockObjectSupport.h>
00019 
00020 #if defined(MOCKPP_USE_CPPUNIT)
00021 
00022 #include <cppunit/extensions/TestFactoryRegistry.h>
00023 #include <cppunit/ui/text/TestRunner.h>
00024 #include <cppunit/extensions/HelperMacros.h>
00025 
00026 #endif
00027 
00028 #include "Calculator.h"
00029 
00030 using MOCKPP_NS::eq;
00031 using MOCKPP_NS::once;
00032 using MOCKPP_NS::exactly;
00033 using MOCKPP_NS::returnValue;
00034 using MOCKPP_NS::throwException;
00035 
00039 class ChainDemoMock : public MOCKPP_NS::ChainableMockObject
00040 {
00041   public:
00042 
00043     ChainDemoMock()
00044       : MOCKPP_NS::ChainableMockObject(MOCKPP_PCHAR("ChainDemoMock"), 0)
00045       , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE_EXT2(add, ext)
00046       , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE0(network_read)
00047     {}
00048 
00049     MOCKPP_CHAINABLE_EXT2(ChainDemoMock, int, add, int, int,
00050                                          int, ext, int, int);
00051 
00052     MOCKPP_CHAINABLE0(ChainDemoMock, int, network_read);
00053 };
00054 
00055 
00059 class VisitDemoMock : public MOCKPP_NS::VisitableMockObject
00060 {
00061   public:
00062 
00063     VisitDemoMock()
00064       : MOCKPP_NS::VisitableMockObject(MOCKPP_PCHAR("VisitDemoMock"), 0)
00065       , MOCKPP_CONSTRUCT_MEMBERS_FOR_VISITABLE_EXT2(add, ext)
00066       , MOCKPP_CONSTRUCT_MEMBERS_FOR_VISITABLE0(network_read)
00067     {}
00068 
00069     MOCKPP_VISITABLE_EXT2(VisitDemoMock, int, add, int, int,
00070                                          int, ext, int, int);
00071 
00072     MOCKPP_VISITABLE0(VisitDemoMock, int, network_read);
00073 };
00074 
00078 class Calculator_test 
00079 #if defined(MOCKPP_USE_CPPUNIT)
00080     : public CppUnit::TestFixture
00081 #endif
00082 
00083 {
00084  public:
00085 
00086 #if defined(MOCKPP_USE_CPPUNIT)
00087 
00088   CPPUNIT_TEST_SUITE( Calculator_test );
00089      CPPUNIT_TEST(test_add);
00090      CPPUNIT_TEST(test_sub);
00091      CPPUNIT_TEST(test_add_v);
00092      CPPUNIT_TEST(test_add_c);
00093      CPPUNIT_TEST(test_network_read_v);
00094      CPPUNIT_TEST(test_network_read_c);
00095    CPPUNIT_TEST_SUITE_END();
00096 
00097 #endif
00098 
00099  public:
00100 
00101    void test_add();
00102    void test_add_v();
00103    void test_add_c();
00104    void test_sub();
00105    void test_network_read_v();
00106    void test_network_read_c();
00107 };
00108 
00109 #if defined(MOCKPP_USE_CPPUNIT)
00110 
00111 // Automatically register tests
00112 CPPUNIT_TEST_SUITE_REGISTRATION(Calculator_test);
00113 
00114 #endif
00115 
00116 // Test add(int) method
00117 void Calculator_test::test_add()
00118 {
00119   Calculator calc(100);
00120   MOCKPP_ASSERT_EQUALS(123, calc.add(23));
00121 }
00122 
00123 // Test add(int, int) method
00124 void Calculator_test::test_add_c()
00125 {
00126   ChainDemoMock mock;
00127   MOCKPP_CHAINER_FOR_EXT(ChainDemoMock, add, ext) add_chainer (&mock);
00128 
00129   add_chainer.stubs()
00130              .with(eq(1), eq(2))
00131              .will(returnValue(3));
00132 
00133   add_chainer.stubs()
00134              .with(eq(99), eq(11))
00135              .will(returnValue(110));
00136 
00137   add_chainer.stubs()
00138              .will(returnValue(-1));
00139 
00140   MOCKPP_ASSERT_EQUALS(3, mock.add(1, 2));
00141   MOCKPP_ASSERT_EQUALS(110, mock.add(99, 11));
00142   MOCKPP_ASSERT_EQUALS(-1, mock.add(99, 12));
00143   MOCKPP_ASSERT_EQUALS(-1, mock.add(98, 11));
00144 
00145   mock.verify();
00146 }
00147 
00148 
00149 // Test add(int, int) method
00150 void Calculator_test::test_add_v()
00151 {
00152   VisitDemoMock mock;
00153   MOCKPP_CONTROLLER_FOR_EXT(VisitDemoMock, add, ext) add_controller (&mock);
00154 
00155   add_controller.addResponseValue(3, 1, 2);      // 1 and 2 are expected
00156   add_controller.addResponseValue(110, 99, 11);  // 99 and 11 are expected
00157   add_controller.setDefaultReturnValue(-1);
00158 
00159   mock.add(1, 2);
00160   mock.add(99, 11);
00161   mock.add(99, 12);
00162   mock.add(98, 11);
00163 
00164   mock.activate();
00165 
00166   MOCKPP_ASSERT_EQUALS(3, mock.add(1, 2));
00167   MOCKPP_ASSERT_EQUALS(110, mock.add(99, 11));
00168   MOCKPP_ASSERT_EQUALS(-1, mock.add(99, 12));
00169   MOCKPP_ASSERT_EQUALS(-1, mock.add(98, 11));
00170 
00171   mock.verify();
00172 }
00173 
00174 
00175 // Test read() method
00176 void Calculator_test::test_network_read_c()
00177 {
00178     ChainDemoMock mock;
00179     MOCKPP_CHAINER_FOR(ChainDemoMock, network_read) read_chainer (&mock);
00180 
00181     read_chainer.stubs(exactly(10))
00182               .will(returnValue(0));
00183 
00184     read_chainer.stubs(once())
00185                 .will(throwException<int>(NetworkError()));
00186 
00187     read_chainer.stubs()
00188               .will(returnValue(1));
00189 
00190     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00191     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00192     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00193     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00194     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00195     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00196     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00197     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00198     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00199     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00200 
00201     try
00202     {
00203       mock.network_read();
00204       MOCKPP_FAIL(MOCKPP_PCHAR("should have thrown here"));
00205     }
00206     catch(const NetworkError &/*ex*/)
00207     {
00208     }
00209 
00210     MOCKPP_ASSERT_EQUALS(1, mock.network_read());
00211     MOCKPP_ASSERT_EQUALS(1, mock.network_read());
00212 
00213     mock.verify();
00214 }
00215 
00216 
00217 // Test read() method
00218 void Calculator_test::test_network_read_v()
00219 {
00220   VisitDemoMock mock;
00221     MOCKPP_CONTROLLER_FOR(VisitDemoMock, network_read) read_controller (&mock);
00222 
00223     mock.network_read();
00224     mock.network_read();
00225     mock.network_read();
00226     mock.network_read();
00227     mock.network_read();
00228     mock.network_read();
00229     mock.network_read();
00230     mock.network_read();
00231     mock.network_read();
00232     mock.network_read();
00233 
00234     mock.network_read();
00235 
00236     mock.network_read();
00237     mock.network_read();
00238 
00239     read_controller.setDefaultReturnValue(1);
00240     read_controller.addReturnValue(0, 10);
00241     read_controller.addThrowable(MOCKPP_NS::make_throwable(NetworkError()));
00242 
00243     mock.activate();
00244 
00245     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00246     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00247     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00248     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00249     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00250     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00251     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00252     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00253     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00254     MOCKPP_ASSERT_EQUALS(0, mock.network_read());
00255 
00256     try
00257     {
00258       mock.network_read();
00259       MOCKPP_FAIL(MOCKPP_PCHAR("should have thrown here"));
00260     }
00261     catch(const NetworkError &/*ex*/)
00262     {
00263     }
00264 
00265     MOCKPP_ASSERT_EQUALS(1, mock.network_read());
00266     MOCKPP_ASSERT_EQUALS(1, mock.network_read());
00267 
00268     mock.verify();
00269 }
00270 
00271 
00272 // Test sub() method
00273 void Calculator_test::test_sub()
00274 {
00275   Calculator calc(100);
00276   MOCKPP_ASSERT_EQUALS(78, calc.sub(22));
00277 }
00278 
00279 
00280 int main(int argc, char ** /*argv*/)
00281 {
00282   if (argc != 1)
00283     global_error = argc;  // disturb tests
00284 
00285 #if defined(MOCKPP_USE_CPPUNIT)
00286   // create tests and run them
00287   CppUnit::TextUi::TestRunner runner;
00288   CppUnit::TestFactory &registry = CppUnit::TestFactoryRegistry::getRegistry();
00289   runner.addTest(registry.makeTest());
00290   return runner.run() ? 0 : 1;
00291 #else
00292   return 0;
00293 #endif
00294 }
00295 

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