?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. #include "UnitTest++/Config.h"
  2.  
  3. #ifndef UNITTEST_NO_DEFERRED_REPORTER
  4.  
  5. #include "UnitTest++/UnitTestPP.h"
  6. #include "UnitTest++/DeferredTestReporter.h"
  7. #include <cstring>
  8.  
  9. namespace UnitTest
  10. {
  11.  
  12.    namespace
  13.    {
  14.  
  15. #ifndef UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM
  16.       MemoryOutStream& operator <<(MemoryOutStream& lhs, const std::string& rhs)
  17.       {
  18.          lhs << rhs.c_str();
  19.          return lhs;
  20.       }
  21. #endif
  22.  
  23.       struct MockDeferredTestReporter : public DeferredTestReporter
  24.       {
  25.          virtual void ReportSummary(int, int, int, float)
  26.          {}
  27.       };
  28.  
  29.       struct DeferredTestReporterFixture
  30.       {
  31.          DeferredTestReporterFixture()
  32.             : testName("UniqueTestName")
  33.             , testSuite("UniqueTestSuite")
  34.             , fileName("filename.h")
  35.             , lineNumber(12)
  36.             , details(testName.c_str(), testSuite.c_str(), fileName.c_str(), lineNumber)
  37.          {}
  38.  
  39.          MockDeferredTestReporter reporter;
  40.          std::string const testName;
  41.          std::string const testSuite;
  42.          std::string const fileName;
  43.          int const lineNumber;
  44.          TestDetails const details;
  45.       };
  46.  
  47.       TEST_FIXTURE(DeferredTestReporterFixture, ReportTestStartCreatesANewDeferredTest)
  48.       {
  49.          reporter.ReportTestStart(details);
  50.          CHECK_EQUAL(1, (int)reporter.GetResults().size());
  51.       }
  52.  
  53.       TEST_FIXTURE(DeferredTestReporterFixture, ReportTestStartCapturesTestNameAndSuite)
  54.       {
  55.          reporter.ReportTestStart(details);
  56.  
  57.          DeferredTestResult const& result = reporter.GetResults().at(0);
  58.          CHECK_EQUAL(testName.c_str(), result.testName.c_str());
  59.          CHECK_EQUAL(testSuite.c_str(), result.suiteName.c_str());
  60.       }
  61.  
  62.       TEST_FIXTURE(DeferredTestReporterFixture, ReportTestEndCapturesTestTime)
  63.       {
  64.          float const elapsed = 123.45f;
  65.          reporter.ReportTestStart(details);
  66.          reporter.ReportTestFinish(details, elapsed);
  67.  
  68.          DeferredTestResult const& result = reporter.GetResults().at(0);
  69.          CHECK_CLOSE(elapsed, result.timeElapsed, 0.0001f);
  70.       }
  71.  
  72.       TEST_FIXTURE(DeferredTestReporterFixture, ReportFailureSavesFailureDetails)
  73.       {
  74.          char const* failure = "failure";
  75.  
  76.          reporter.ReportTestStart(details);
  77.          reporter.ReportFailure(details, failure);
  78.  
  79.          DeferredTestResult const& result = reporter.GetResults().at(0);
  80.          CHECK(result.failed == true);
  81.          CHECK_EQUAL(fileName.c_str(), result.failureFile.c_str());
  82.       }
  83.  
  84.       TEST_FIXTURE(DeferredTestReporterFixture, ReportFailureSavesFailureDetailsForMultipleFailures)
  85.       {
  86.          char const* failure1 = "failure 1";
  87.          char const* failure2 = "failure 2";
  88.  
  89.          reporter.ReportTestStart(details);
  90.          reporter.ReportFailure(details, failure1);
  91.          reporter.ReportFailure(details, failure2);
  92.  
  93.          DeferredTestResult const& result = reporter.GetResults().at(0);
  94.          CHECK_EQUAL(2, (int)result.failures.size());
  95.          CHECK_EQUAL(failure1, result.failures[0].failureStr);
  96.          CHECK_EQUAL(failure2, result.failures[1].failureStr);
  97.       }
  98.  
  99.       TEST_FIXTURE(DeferredTestReporterFixture, DeferredTestReporterTakesCopyOfFailureMessage)
  100.       {
  101.          reporter.ReportTestStart(details);
  102.  
  103.          char failureMessage[128];
  104.          char const* goodStr = "Real failure message";
  105.          char const* badStr = "Bogus failure message";
  106.  
  107.          using namespace std;
  108.  
  109.          strcpy(failureMessage, goodStr);
  110.          reporter.ReportFailure(details, failureMessage);
  111.          strcpy(failureMessage, badStr);
  112.  
  113.          DeferredTestResult const& result = reporter.GetResults().at(0);
  114.          DeferredTestFailure const& failure = result.failures.at(0);
  115.          CHECK_EQUAL(goodStr, failure.failureStr);
  116.       }
  117.  
  118.    }
  119. }
  120.  
  121. #endif
  122.