TestTest.h
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:3k
源码类别:

模拟服务器

开发平台:

C/C++

  1. #ifndef CPPUNIT_TESTTEST_H
  2. #define CPPUNIT_TESTTEST_H
  3. #ifndef CPPUNIT_TESTCASE_H
  4. #include "TestCase.h"
  5. #endif
  6. #ifndef CPPUNIT_TESTSUITE_H
  7. #include "TestSuite.h"
  8. #endif
  9. #ifndef CPPUNIT_TESTRESULT_H
  10. #include "TestResult.h"
  11. #endif
  12. #ifndef CPPUNIT_TESTCALLER_H
  13. #include "TestCaller.h"
  14. #endif
  15. class TestTest : public TestCase
  16. {
  17. protected:
  18.     TestCase            *m_failure;
  19.     TestCase            *m_error;
  20.     TestCase            *m_success;
  21. public:
  22.                         TestTest (std::string name) : TestCase (name) {}
  23.     void                testFailure ();
  24.     void                testError ();
  25.     void                testSuccess ();
  26.     static Test         *suite ();
  27.     void                setUp ();
  28.     void                tearDown ();
  29. private:
  30.     class FailureTestCase : public TestCase 
  31.     { public: FailureTestCase (std::string name) : TestCase (name) {} 
  32.       protected: void runTest () { assert (false); }; };
  33.     class ErrorTestCase : public TestCase 
  34.     { public: ErrorTestCase (std::string name) : TestCase (name) {} 
  35.     protected: void runTest () { int zero = 0; int result = 8 / zero; }; };
  36.     class SuccessTestCase : public TestCase 
  37.     { public: SuccessTestCase (std::string name) : TestCase (name) {} 
  38.       protected: void runTest () { assert (true); }; };
  39. };
  40. inline void TestTest::setUp ()
  41.     m_failure   = new FailureTestCase ("failure");
  42.     m_error     = new ErrorTestCase ("error");
  43.     m_success   = new SuccessTestCase ("success");
  44. }
  45. inline void TestTest::tearDown ()
  46.     delete m_failure;
  47.     delete m_error;
  48.     delete m_success;
  49. }
  50. inline void TestTest::testFailure ()
  51. {
  52.     std::auto_ptr<TestResult>    result (m_failure->run ());
  53.     assert (result->runTests () == 1);
  54.     assert (result->testFailures () == 1);
  55.     assert (result->testErrors () == 0);
  56.     assert (!result->wasSuccessful ());
  57. }
  58. inline void TestTest::testError ()
  59. {
  60.     std::auto_ptr<TestResult> result (m_error->run ());
  61.     assert (result->runTests () == 1);
  62.     assert (result->testFailures () == 0);
  63.     assert (result->testErrors () == 1);
  64.     assert (!result->wasSuccessful ());
  65. }
  66. inline void TestTest::testSuccess ()
  67. {
  68.     std::auto_ptr<TestResult> result (m_success->run ());
  69.     assert (result->runTests () == 1);
  70.     assert (result->testFailures () == 0);
  71.     assert (result->testErrors () == 0);
  72.     assert (result->wasSuccessful ());
  73. }
  74. inline Test *TestTest::suite ()
  75. {
  76.     TestSuite *suite = new TestSuite ("TestTest");
  77.     suite->addTest (new TestCaller<TestTest> ("testFailure", testFailure));
  78.     suite->addTest (new TestCaller<TestTest> ("testError", testError));
  79.     suite->addTest (new TestCaller<TestTest> ("testSuccess", testSuccess));
  80.     return suite;
  81. }
  82. #endif