?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. #include "TestRunner.h"
  2. #include "TestResults.h"
  3. #include "TestReporter.h"
  4. #include "TestReporterStdout.h"
  5. #include "TimeHelpers.h"
  6. #include "MemoryOutStream.h"
  7.  
  8. #include <cstring>
  9.  
  10.  
  11. namespace UnitTest {
  12.  
  13.    int RunAllTests()
  14.    {
  15.       TestReporterStdout reporter;
  16.       TestRunner runner(reporter);
  17.       return runner.RunTestsIf(Test::GetTestList(), NULL, True(), 0);
  18.    }
  19.  
  20.  
  21.    TestRunner::TestRunner(TestReporter& reporter)
  22.       : m_reporter(&reporter)
  23.       , m_result(new TestResults(&reporter))
  24.       , m_timer(new Timer)
  25.    {
  26.       m_timer->Start();
  27.    }
  28.  
  29.    TestRunner::~TestRunner()
  30.    {
  31.       delete m_result;
  32.       delete m_timer;
  33.    }
  34.  
  35.    TestResults* TestRunner::GetTestResults()
  36.    {
  37.       return m_result;
  38.    }
  39.  
  40.    int TestRunner::Finish() const
  41.    {
  42.       float const secondsElapsed = static_cast<float>(m_timer->GetTimeInMs() / 1000.0);
  43.       m_reporter->ReportSummary(m_result->GetTotalTestCount(),
  44.                                 m_result->GetFailedTestCount(),
  45.                                 m_result->GetFailureCount(),
  46.                                 secondsElapsed);
  47.  
  48.       return m_result->GetFailureCount();
  49.    }
  50.  
  51.    bool TestRunner::IsTestInSuite(const Test* const curTest, char const* suiteName) const
  52.    {
  53.       using namespace std;
  54.       return (suiteName == NULL) || !strcmp(curTest->m_details.suiteName, suiteName);
  55.    }
  56.  
  57.    void TestRunner::RunTest(TestResults* const result, Test* const curTest, int const maxTestTimeInMs) const
  58.    {
  59.       if (curTest->m_isMockTest == false)
  60.          CurrentTest::Results() = result;
  61.  
  62.       Timer testTimer;
  63.       testTimer.Start();
  64.  
  65.       result->OnTestStart(curTest->m_details);
  66.  
  67.       curTest->Run();
  68.  
  69.       double const testTimeInMs = testTimer.GetTimeInMs();
  70.       if (maxTestTimeInMs > 0 && testTimeInMs > maxTestTimeInMs && !curTest->m_details.timeConstraintExempt)
  71.       {
  72.          MemoryOutStream stream;
  73.          stream << "Global time constraint failed. Expected under " << maxTestTimeInMs <<
  74.             "ms but took " << testTimeInMs << "ms.";
  75.  
  76.          result->OnTestFailure(curTest->m_details, stream.GetText());
  77.       }
  78.  
  79.       result->OnTestFinish(curTest->m_details, static_cast< float >(testTimeInMs / 1000.0));
  80.    }
  81.  
  82. }
  83.