verifying.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002            verifying.cpp  -  example usage of verifying test case
00003 
00004                              -------------------
00005     begin                : Sat 22 Jan 2005
00006     copyright            : (C) 2002-2006 by Ewald Arnold
00007     email                : mockpp at ewald-arnold dot de
00008 
00009   $Id: verifying.cpp,v 1.16 2006/04/05 17:03:39 ewald-arnold Exp $
00010 
00011  ***************************************************************************/
00012 
00013 #include <mockpp/mockpp.h> // always first
00014 
00015 #include <iostream>
00016 
00017 #ifdef MOCKPP_USE_CPPUNIT
00018 
00019 #include <mockpp/framework/VerifyingTestCase.h>
00020 
00021 #include <mockpp/ExpectationValue.h>
00022 
00023 #include <mockpp/compat/AssertionFailedError.h>
00024 
00025 #include <cppunit/extensions/TestFactoryRegistry.h>
00026 #include <cppunit/ui/text/TestRunner.h>
00027 #include <cppunit/extensions/HelperMacros.h>
00028 
00029 #include "Calculator.h"
00030 
00034 class VerifyingCalculator_test : public MOCKPP_NS::VerifyingTestCase
00035 {
00036  public:
00037 
00038    CPPUNIT_TEST_SUITE( VerifyingCalculator_test );
00039      MOCKPP_TEST(test_ok_no_verify);
00040      MOCKPP_VERIFYING_TEST(test_ok);
00041      MOCKPP_VERIFYING_TEST_EXCEPTION(test_fail, MOCKPP_NS::AssertionFailedError);
00042      MOCKPP_TEST_EXCEPTION(test_fail_no_verify, MOCKPP_NS::AssertionFailedError);
00043    CPPUNIT_TEST_SUITE_END();
00044 
00045  public:
00046 
00047    void test_ok();
00048    void test_fail();
00049    void test_ok_no_verify();
00050    void test_fail_no_verify();
00051 };
00052 
00053 
00054 // Automatically register tests
00055 CPPUNIT_TEST_SUITE_REGISTRATION(VerifyingCalculator_test);
00056 
00057 
00058 void VerifyingCalculator_test::test_ok_no_verify()
00059 {
00060   MOCKPP_NS::ExpectationValue<int> ev (MOCKPP_PCHAR("ev"), this);
00061   ev.setExpected(123);
00062   ev.setActual(123);
00063   verify();
00064 }
00065 
00066 
00067 void VerifyingCalculator_test::test_ok()
00068 {
00069   MOCKPP_NS::ExpectationValue<int> *ev = new MOCKPP_NS::ExpectationValue<int>(MOCKPP_PCHAR("ev"));
00070   registerVerifiable(ev);
00071   ev->setExpected(123);
00072   ev->setActual(123);
00073   ev->verify();
00074 
00075   MOCKPP_NS::ExpectationValue<int> ev2 (MOCKPP_PCHAR("ev2"), this);
00076   ev2.setExpected(123);
00077   ev2.setActual(123);
00078   ev2.verify();
00079 
00080   MOCKPP_NS::ExpectationValue<int> ev3 (MOCKPP_PCHAR("ev3"));
00081   ev3.setExpected(123);
00082   ev3.setActual(123);
00083   ev3.verify();
00084 
00085   MOCKPP_NS::ExpectationValue<int> *ev4 = new MOCKPP_NS::ExpectationValue<int>(MOCKPP_PCHAR("ev"));
00086   registerVerifiable(ev4);
00087   ev4->setExpected(123);
00088   ev4->setActual(123);
00089   ev4->verify();
00090 
00091   verify();
00092 }
00093 
00094 
00095 void VerifyingCalculator_test::test_fail_no_verify()
00096 {
00097   MOCKPP_NS::ExpectationValue<int> ev(MOCKPP_PCHAR("ev"), 0) ;
00098   ev.setExpected(123);
00099   ev.setActual(123456);
00100 }
00101 
00102 
00103 void VerifyingCalculator_test::test_fail()
00104 {
00105   MOCKPP_NS::ExpectationValue<int> *ev = new MOCKPP_NS::ExpectationValue<int>(MOCKPP_PCHAR("ev"));
00106   registerVerifiable(ev);
00107   ev->setExpected(123);
00108   // missing actual
00109 }
00110 
00111 #endif // MOCKPP_USE_CPPUNIT
00112 
00113 int main(int argc, char ** /*argv*/)
00114 {
00115 #ifdef MOCKPP_USE_CPPUNIT
00116   if (argc != 1)
00117     global_error = argc;  // disturb tests
00118 
00119   // create tests and run them
00120   CppUnit::TextUi::TestRunner runner;
00121   CppUnit::TestFactory &registry = CppUnit::TestFactoryRegistry::getRegistry();
00122   runner.addTest(registry.makeTest());
00123   return runner.run() ? 0 : 1;
00124 #else
00125   MOCKPP_STD_NS::cout << "CppUnit was not available at compile time.\n";
00126   return 0;
00127 #endif
00128 }

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