mirror of
https://github.com/unittest-cpp/unittest-cpp
synced 2026-09-26 16:19:30 +03:00
r18 | charles.nicholson | 2010-03-18 15:42:23 -0500 (Thu, 18 Mar 2010) | 1 line
add scea 'TestReporterMulti' as 'CompositeTestReporter', add tests
This commit is contained in:
parent
32288a53b9
commit
88b09d46e8
7 changed files with 293 additions and 3 deletions
6
Makefile
6
Makefile
|
|
@ -25,7 +25,8 @@ src = src/AssertException.cpp \
|
|||
src/DeferredTestReporter.cpp \
|
||||
src/DeferredTestResult.cpp \
|
||||
src/XmlTestReporter.cpp \
|
||||
src/CurrentTest.cpp
|
||||
src/CurrentTest.cpp \
|
||||
src/CompositeTestReporter.cpp
|
||||
|
||||
ifeq ($(MSYSTEM), MINGW32)
|
||||
src += src/Win32/TimeHelpers.cpp
|
||||
|
|
@ -49,7 +50,8 @@ test_src = src/tests/Main.cpp \
|
|||
src/tests/TestMemoryOutStream.cpp \
|
||||
src/tests/TestDeferredTestReporter.cpp \
|
||||
src/tests/TestXmlTestReporter.cpp \
|
||||
src/tests/TestCurrentTest.cpp
|
||||
src/tests/TestCurrentTest.cpp \
|
||||
src/tests/TestCompositeTestReporter.cpp
|
||||
|
||||
objects = $(patsubst %.cpp, %.o, $(src))
|
||||
test_objects = $(patsubst %.cpp, %.o, $(test_src))
|
||||
|
|
|
|||
67
src/CompositeTestReporter.cpp
Normal file
67
src/CompositeTestReporter.cpp
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#include "CompositeTestReporter.h"
|
||||
#include <cstddef>
|
||||
|
||||
namespace UnitTest {
|
||||
|
||||
CompositeTestReporter::CompositeTestReporter()
|
||||
: m_reporterCount(0)
|
||||
{
|
||||
}
|
||||
|
||||
int CompositeTestReporter::GetReporterCount() const
|
||||
{
|
||||
return m_reporterCount;
|
||||
}
|
||||
|
||||
bool CompositeTestReporter::AddReporter(TestReporter* reporter)
|
||||
{
|
||||
if (m_reporterCount == kMaxReporters)
|
||||
return false;
|
||||
|
||||
m_reporters[m_reporterCount++] = reporter;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CompositeTestReporter::RemoveReporter(TestReporter* reporter)
|
||||
{
|
||||
for (int index = 0; index < m_reporterCount; ++index)
|
||||
{
|
||||
if (m_reporters[index] == reporter)
|
||||
{
|
||||
m_reporters[index] = m_reporters[m_reporterCount - 1];
|
||||
--m_reporterCount;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void CompositeTestReporter::ReportFailure(TestDetails const& details, char const* failure)
|
||||
{
|
||||
for (int index = 0; index < m_reporterCount; ++index)
|
||||
m_reporters[index]->ReportFailure(details, failure);
|
||||
}
|
||||
|
||||
void CompositeTestReporter::ReportTestStart(TestDetails const& test)
|
||||
{
|
||||
for (int index = 0; index < m_reporterCount; ++index)
|
||||
m_reporters[index]->ReportTestStart(test);
|
||||
}
|
||||
|
||||
void CompositeTestReporter::ReportTestFinish(TestDetails const& test, float secondsElapsed)
|
||||
{
|
||||
for (int index = 0; index < m_reporterCount; ++index)
|
||||
m_reporters[index]->ReportTestFinish(test, secondsElapsed);
|
||||
}
|
||||
|
||||
void CompositeTestReporter::ReportSummary(int totalTestCount,
|
||||
int failedTestCount,
|
||||
int failureCount,
|
||||
float secondsElapsed)
|
||||
{
|
||||
for (int index = 0; index < m_reporterCount; ++index)
|
||||
m_reporters[index]->ReportSummary(totalTestCount, failedTestCount, failureCount, secondsElapsed);
|
||||
}
|
||||
|
||||
}
|
||||
34
src/CompositeTestReporter.h
Normal file
34
src/CompositeTestReporter.h
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef UNITTEST_COMPOSITETESTREPORTER_H
|
||||
#define UNITTEST_COMPOSITETESTREPORTER_H
|
||||
|
||||
#include "TestReporter.h"
|
||||
|
||||
namespace UnitTest {
|
||||
|
||||
class UNITTEST_LINKAGE CompositeTestReporter : public TestReporter
|
||||
{
|
||||
public:
|
||||
CompositeTestReporter();
|
||||
|
||||
int GetReporterCount() const;
|
||||
bool AddReporter(TestReporter* reporter);
|
||||
bool RemoveReporter(TestReporter* reporter);
|
||||
|
||||
virtual void ReportTestStart(TestDetails const& test);
|
||||
virtual void ReportFailure(TestDetails const& test, char const* failure);
|
||||
virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed);
|
||||
virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed);
|
||||
|
||||
private:
|
||||
enum { kMaxReporters = 16 };
|
||||
TestReporter* m_reporters[kMaxReporters];
|
||||
int m_reporterCount;
|
||||
|
||||
// revoked
|
||||
CompositeTestReporter(const CompositeTestReporter&);
|
||||
CompositeTestReporter& operator =(const CompositeTestReporter&);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace UnitTest {
|
||||
|
||||
|
||||
TestReporter::~TestReporter()
|
||||
{
|
||||
}
|
||||
|
|
|
|||
176
src/tests/TestCompositeTestReporter.cpp
Normal file
176
src/tests/TestCompositeTestReporter.cpp
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
#include "../../unittestpp.h"
|
||||
#include "../CompositeTestReporter.h"
|
||||
|
||||
using namespace UnitTest;
|
||||
|
||||
namespace {
|
||||
|
||||
TEST(ZeroReportersByDefault)
|
||||
{
|
||||
CHECK_EQUAL(0, CompositeTestReporter().GetReporterCount());
|
||||
}
|
||||
|
||||
struct MockReporter : TestReporter
|
||||
{
|
||||
MockReporter()
|
||||
: testStartCalled(false)
|
||||
, testStartDetails(NULL)
|
||||
, failureCalled(false)
|
||||
, failureDetails(NULL)
|
||||
, failureStr(NULL)
|
||||
, testFinishCalled(false)
|
||||
, testFinishDetails(NULL)
|
||||
, testFinishSecondsElapsed(-1.0f)
|
||||
, summaryCalled(false)
|
||||
, summaryTotalTestCount(-1)
|
||||
, summaryFailureCount(-1)
|
||||
, summarySecondsElapsed(-1.0f)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void ReportTestStart(TestDetails const& test)
|
||||
{
|
||||
testStartCalled = true;
|
||||
testStartDetails = &test;
|
||||
}
|
||||
|
||||
virtual void ReportFailure(TestDetails const& test, char const* failure)
|
||||
{
|
||||
failureCalled = true;
|
||||
failureDetails = &test;
|
||||
failureStr = failure;
|
||||
}
|
||||
|
||||
virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed)
|
||||
{
|
||||
testFinishCalled = true;
|
||||
testFinishDetails = &test;
|
||||
testFinishSecondsElapsed = secondsElapsed;
|
||||
}
|
||||
|
||||
virtual void ReportSummary(int totalTestCount,
|
||||
int failedTestCount,
|
||||
int failureCount,
|
||||
float secondsElapsed)
|
||||
{
|
||||
summaryCalled = true;
|
||||
summaryTotalTestCount = totalTestCount;
|
||||
summaryFailedTestCount = failedTestCount;
|
||||
summaryFailureCount = failureCount;
|
||||
summarySecondsElapsed = secondsElapsed;
|
||||
}
|
||||
|
||||
bool testStartCalled;
|
||||
TestDetails const* testStartDetails;
|
||||
|
||||
bool failureCalled;
|
||||
TestDetails const* failureDetails;
|
||||
const char* failureStr;
|
||||
|
||||
bool testFinishCalled;
|
||||
TestDetails const* testFinishDetails;
|
||||
float testFinishSecondsElapsed;
|
||||
|
||||
bool summaryCalled;
|
||||
int summaryTotalTestCount;
|
||||
int summaryFailedTestCount;
|
||||
int summaryFailureCount;
|
||||
float summarySecondsElapsed;
|
||||
};
|
||||
|
||||
TEST(AddReporter)
|
||||
{
|
||||
MockReporter r;
|
||||
CompositeTestReporter c;
|
||||
|
||||
CHECK(c.AddReporter(&r));
|
||||
CHECK_EQUAL(1, c.GetReporterCount());
|
||||
}
|
||||
|
||||
TEST(RemoveReporter)
|
||||
{
|
||||
MockReporter r;
|
||||
CompositeTestReporter c;
|
||||
|
||||
c.AddReporter(&r);
|
||||
CHECK(c.RemoveReporter(&r));
|
||||
CHECK_EQUAL(0, c.GetReporterCount());
|
||||
}
|
||||
|
||||
struct Fixture
|
||||
{
|
||||
Fixture()
|
||||
{
|
||||
c.AddReporter(&r0);
|
||||
c.AddReporter(&r1);
|
||||
}
|
||||
|
||||
MockReporter r0, r1;
|
||||
CompositeTestReporter c;
|
||||
};
|
||||
|
||||
TEST_FIXTURE(Fixture, ReportTestStartCallsReportTestStartOnAllAggregates)
|
||||
{
|
||||
TestDetails t("", "", "", 0);
|
||||
c.ReportTestStart(t);
|
||||
|
||||
CHECK(r0.testStartCalled);
|
||||
CHECK_EQUAL(&t, r0.testStartDetails);
|
||||
CHECK(r1.testStartCalled);
|
||||
CHECK_EQUAL(&t, r1.testStartDetails);
|
||||
}
|
||||
|
||||
TEST_FIXTURE(Fixture, ReportFailureCallsReportFailureOnAllAggregates)
|
||||
{
|
||||
TestDetails t("", "", "", 0);
|
||||
const char* failStr = "fail";
|
||||
c.ReportFailure(t, failStr);
|
||||
|
||||
CHECK(r0.failureCalled);
|
||||
CHECK_EQUAL(&t, r0.failureDetails);
|
||||
CHECK_EQUAL(failStr, r0.failureStr);
|
||||
|
||||
CHECK(r1.failureCalled);
|
||||
CHECK_EQUAL(&t, r1.failureDetails);
|
||||
CHECK_EQUAL(failStr, r1.failureStr);
|
||||
}
|
||||
|
||||
TEST_FIXTURE(Fixture, ReportTestFinishCallsReportTestFinishOnAllAggregates)
|
||||
{
|
||||
TestDetails t("", "", "", 0);
|
||||
const float s = 1.2345f;
|
||||
c.ReportTestFinish(t, s);
|
||||
|
||||
CHECK(r0.testFinishCalled);
|
||||
CHECK_EQUAL(&t, r0.testFinishDetails);
|
||||
CHECK_CLOSE(s, r0.testFinishSecondsElapsed, 0.00001f);
|
||||
|
||||
CHECK(r1.testFinishCalled);
|
||||
CHECK_EQUAL(&t, r1.testFinishDetails);
|
||||
CHECK_CLOSE(s, r1.testFinishSecondsElapsed, 0.00001f);
|
||||
}
|
||||
|
||||
TEST_FIXTURE(Fixture, ReportSummaryCallsReportSummaryOnAllAggregates)
|
||||
{
|
||||
TestDetails t("", "", "", 0);
|
||||
const int testCount = 3;
|
||||
const int failedTestCount = 4;
|
||||
const int failureCount = 5;
|
||||
const float secondsElapsed = 3.14159f;
|
||||
|
||||
c.ReportSummary(testCount, failedTestCount, failureCount, secondsElapsed);
|
||||
|
||||
CHECK(r0.summaryCalled);
|
||||
CHECK_EQUAL(testCount, r0.summaryTotalTestCount);
|
||||
CHECK_EQUAL(failedTestCount, r0.summaryFailedTestCount);
|
||||
CHECK_EQUAL(failureCount, r0.summaryFailureCount);
|
||||
CHECK_CLOSE(secondsElapsed, r0.summarySecondsElapsed, 0.00001f);
|
||||
|
||||
CHECK(r1.summaryCalled);
|
||||
CHECK_EQUAL(testCount, r1.summaryTotalTestCount);
|
||||
CHECK_EQUAL(failedTestCount, r1.summaryFailedTestCount);
|
||||
CHECK_EQUAL(failureCount, r1.summaryFailureCount);
|
||||
CHECK_CLOSE(secondsElapsed, r1.summarySecondsElapsed, 0.00001f);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -357,6 +357,10 @@
|
|||
RelativePath=".\TestChecks.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\TestCompositeTestReporter.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\TestCurrentTest.cpp"
|
||||
>
|
||||
|
|
|
|||
|
|
@ -325,6 +325,14 @@
|
|||
RelativePath=".\Checks.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\CompositeTestReporter.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\CompositeTestReporter.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\config.h"
|
||||
>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue