Skip to content

Commit 88b09d4

Browse files
committed
r18 | charles.nicholson | 2010-03-18 15:42:23 -0500 (Thu, 18 Mar 2010) | 1 line
add scea 'TestReporterMulti' as 'CompositeTestReporter', add tests
1 parent 32288a5 commit 88b09d4

7 files changed

+293
-3
lines changed

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ src = src/AssertException.cpp \
2525
src/DeferredTestReporter.cpp \
2626
src/DeferredTestResult.cpp \
2727
src/XmlTestReporter.cpp \
28-
src/CurrentTest.cpp
28+
src/CurrentTest.cpp \
29+
src/CompositeTestReporter.cpp
2930

3031
ifeq ($(MSYSTEM), MINGW32)
3132
src += src/Win32/TimeHelpers.cpp
@@ -49,7 +50,8 @@ test_src = src/tests/Main.cpp \
4950
src/tests/TestMemoryOutStream.cpp \
5051
src/tests/TestDeferredTestReporter.cpp \
5152
src/tests/TestXmlTestReporter.cpp \
52-
src/tests/TestCurrentTest.cpp
53+
src/tests/TestCurrentTest.cpp \
54+
src/tests/TestCompositeTestReporter.cpp
5355

5456
objects = $(patsubst %.cpp, %.o, $(src))
5557
test_objects = $(patsubst %.cpp, %.o, $(test_src))

src/CompositeTestReporter.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "CompositeTestReporter.h"
2+
#include <cstddef>
3+
4+
namespace UnitTest {
5+
6+
CompositeTestReporter::CompositeTestReporter()
7+
: m_reporterCount(0)
8+
{
9+
}
10+
11+
int CompositeTestReporter::GetReporterCount() const
12+
{
13+
return m_reporterCount;
14+
}
15+
16+
bool CompositeTestReporter::AddReporter(TestReporter* reporter)
17+
{
18+
if (m_reporterCount == kMaxReporters)
19+
return false;
20+
21+
m_reporters[m_reporterCount++] = reporter;
22+
return true;
23+
}
24+
25+
bool CompositeTestReporter::RemoveReporter(TestReporter* reporter)
26+
{
27+
for (int index = 0; index < m_reporterCount; ++index)
28+
{
29+
if (m_reporters[index] == reporter)
30+
{
31+
m_reporters[index] = m_reporters[m_reporterCount - 1];
32+
--m_reporterCount;
33+
return true;
34+
}
35+
}
36+
37+
return false;
38+
}
39+
40+
void CompositeTestReporter::ReportFailure(TestDetails const& details, char const* failure)
41+
{
42+
for (int index = 0; index < m_reporterCount; ++index)
43+
m_reporters[index]->ReportFailure(details, failure);
44+
}
45+
46+
void CompositeTestReporter::ReportTestStart(TestDetails const& test)
47+
{
48+
for (int index = 0; index < m_reporterCount; ++index)
49+
m_reporters[index]->ReportTestStart(test);
50+
}
51+
52+
void CompositeTestReporter::ReportTestFinish(TestDetails const& test, float secondsElapsed)
53+
{
54+
for (int index = 0; index < m_reporterCount; ++index)
55+
m_reporters[index]->ReportTestFinish(test, secondsElapsed);
56+
}
57+
58+
void CompositeTestReporter::ReportSummary(int totalTestCount,
59+
int failedTestCount,
60+
int failureCount,
61+
float secondsElapsed)
62+
{
63+
for (int index = 0; index < m_reporterCount; ++index)
64+
m_reporters[index]->ReportSummary(totalTestCount, failedTestCount, failureCount, secondsElapsed);
65+
}
66+
67+
}

src/CompositeTestReporter.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef UNITTEST_COMPOSITETESTREPORTER_H
2+
#define UNITTEST_COMPOSITETESTREPORTER_H
3+
4+
#include "TestReporter.h"
5+
6+
namespace UnitTest {
7+
8+
class UNITTEST_LINKAGE CompositeTestReporter : public TestReporter
9+
{
10+
public:
11+
CompositeTestReporter();
12+
13+
int GetReporterCount() const;
14+
bool AddReporter(TestReporter* reporter);
15+
bool RemoveReporter(TestReporter* reporter);
16+
17+
virtual void ReportTestStart(TestDetails const& test);
18+
virtual void ReportFailure(TestDetails const& test, char const* failure);
19+
virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed);
20+
virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed);
21+
22+
private:
23+
enum { kMaxReporters = 16 };
24+
TestReporter* m_reporters[kMaxReporters];
25+
int m_reporterCount;
26+
27+
// revoked
28+
CompositeTestReporter(const CompositeTestReporter&);
29+
CompositeTestReporter& operator =(const CompositeTestReporter&);
30+
};
31+
32+
}
33+
34+
#endif

src/TestReporter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace UnitTest {
44

5-
65
TestReporter::~TestReporter()
76
{
87
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#include "../../unittestpp.h"
2+
#include "../CompositeTestReporter.h"
3+
4+
using namespace UnitTest;
5+
6+
namespace {
7+
8+
TEST(ZeroReportersByDefault)
9+
{
10+
CHECK_EQUAL(0, CompositeTestReporter().GetReporterCount());
11+
}
12+
13+
struct MockReporter : TestReporter
14+
{
15+
MockReporter()
16+
: testStartCalled(false)
17+
, testStartDetails(NULL)
18+
, failureCalled(false)
19+
, failureDetails(NULL)
20+
, failureStr(NULL)
21+
, testFinishCalled(false)
22+
, testFinishDetails(NULL)
23+
, testFinishSecondsElapsed(-1.0f)
24+
, summaryCalled(false)
25+
, summaryTotalTestCount(-1)
26+
, summaryFailureCount(-1)
27+
, summarySecondsElapsed(-1.0f)
28+
{
29+
}
30+
31+
virtual void ReportTestStart(TestDetails const& test)
32+
{
33+
testStartCalled = true;
34+
testStartDetails = &test;
35+
}
36+
37+
virtual void ReportFailure(TestDetails const& test, char const* failure)
38+
{
39+
failureCalled = true;
40+
failureDetails = &test;
41+
failureStr = failure;
42+
}
43+
44+
virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed)
45+
{
46+
testFinishCalled = true;
47+
testFinishDetails = &test;
48+
testFinishSecondsElapsed = secondsElapsed;
49+
}
50+
51+
virtual void ReportSummary(int totalTestCount,
52+
int failedTestCount,
53+
int failureCount,
54+
float secondsElapsed)
55+
{
56+
summaryCalled = true;
57+
summaryTotalTestCount = totalTestCount;
58+
summaryFailedTestCount = failedTestCount;
59+
summaryFailureCount = failureCount;
60+
summarySecondsElapsed = secondsElapsed;
61+
}
62+
63+
bool testStartCalled;
64+
TestDetails const* testStartDetails;
65+
66+
bool failureCalled;
67+
TestDetails const* failureDetails;
68+
const char* failureStr;
69+
70+
bool testFinishCalled;
71+
TestDetails const* testFinishDetails;
72+
float testFinishSecondsElapsed;
73+
74+
bool summaryCalled;
75+
int summaryTotalTestCount;
76+
int summaryFailedTestCount;
77+
int summaryFailureCount;
78+
float summarySecondsElapsed;
79+
};
80+
81+
TEST(AddReporter)
82+
{
83+
MockReporter r;
84+
CompositeTestReporter c;
85+
86+
CHECK(c.AddReporter(&r));
87+
CHECK_EQUAL(1, c.GetReporterCount());
88+
}
89+
90+
TEST(RemoveReporter)
91+
{
92+
MockReporter r;
93+
CompositeTestReporter c;
94+
95+
c.AddReporter(&r);
96+
CHECK(c.RemoveReporter(&r));
97+
CHECK_EQUAL(0, c.GetReporterCount());
98+
}
99+
100+
struct Fixture
101+
{
102+
Fixture()
103+
{
104+
c.AddReporter(&r0);
105+
c.AddReporter(&r1);
106+
}
107+
108+
MockReporter r0, r1;
109+
CompositeTestReporter c;
110+
};
111+
112+
TEST_FIXTURE(Fixture, ReportTestStartCallsReportTestStartOnAllAggregates)
113+
{
114+
TestDetails t("", "", "", 0);
115+
c.ReportTestStart(t);
116+
117+
CHECK(r0.testStartCalled);
118+
CHECK_EQUAL(&t, r0.testStartDetails);
119+
CHECK(r1.testStartCalled);
120+
CHECK_EQUAL(&t, r1.testStartDetails);
121+
}
122+
123+
TEST_FIXTURE(Fixture, ReportFailureCallsReportFailureOnAllAggregates)
124+
{
125+
TestDetails t("", "", "", 0);
126+
const char* failStr = "fail";
127+
c.ReportFailure(t, failStr);
128+
129+
CHECK(r0.failureCalled);
130+
CHECK_EQUAL(&t, r0.failureDetails);
131+
CHECK_EQUAL(failStr, r0.failureStr);
132+
133+
CHECK(r1.failureCalled);
134+
CHECK_EQUAL(&t, r1.failureDetails);
135+
CHECK_EQUAL(failStr, r1.failureStr);
136+
}
137+
138+
TEST_FIXTURE(Fixture, ReportTestFinishCallsReportTestFinishOnAllAggregates)
139+
{
140+
TestDetails t("", "", "", 0);
141+
const float s = 1.2345f;
142+
c.ReportTestFinish(t, s);
143+
144+
CHECK(r0.testFinishCalled);
145+
CHECK_EQUAL(&t, r0.testFinishDetails);
146+
CHECK_CLOSE(s, r0.testFinishSecondsElapsed, 0.00001f);
147+
148+
CHECK(r1.testFinishCalled);
149+
CHECK_EQUAL(&t, r1.testFinishDetails);
150+
CHECK_CLOSE(s, r1.testFinishSecondsElapsed, 0.00001f);
151+
}
152+
153+
TEST_FIXTURE(Fixture, ReportSummaryCallsReportSummaryOnAllAggregates)
154+
{
155+
TestDetails t("", "", "", 0);
156+
const int testCount = 3;
157+
const int failedTestCount = 4;
158+
const int failureCount = 5;
159+
const float secondsElapsed = 3.14159f;
160+
161+
c.ReportSummary(testCount, failedTestCount, failureCount, secondsElapsed);
162+
163+
CHECK(r0.summaryCalled);
164+
CHECK_EQUAL(testCount, r0.summaryTotalTestCount);
165+
CHECK_EQUAL(failedTestCount, r0.summaryFailedTestCount);
166+
CHECK_EQUAL(failureCount, r0.summaryFailureCount);
167+
CHECK_CLOSE(secondsElapsed, r0.summarySecondsElapsed, 0.00001f);
168+
169+
CHECK(r1.summaryCalled);
170+
CHECK_EQUAL(testCount, r1.summaryTotalTestCount);
171+
CHECK_EQUAL(failedTestCount, r1.summaryFailedTestCount);
172+
CHECK_EQUAL(failureCount, r1.summaryFailureCount);
173+
CHECK_CLOSE(secondsElapsed, r1.summarySecondsElapsed, 0.00001f);
174+
}
175+
176+
}

src/tests/test-unittestpp_vs2005.vcproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,10 @@
357357
RelativePath=".\TestChecks.cpp"
358358
>
359359
</File>
360+
<File
361+
RelativePath=".\TestCompositeTestReporter.cpp"
362+
>
363+
</File>
360364
<File
361365
RelativePath=".\TestCurrentTest.cpp"
362366
>

src/unittestpp_vs2005.vcproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@
325325
RelativePath=".\Checks.h"
326326
>
327327
</File>
328+
<File
329+
RelativePath=".\CompositeTestReporter.cpp"
330+
>
331+
</File>
332+
<File
333+
RelativePath=".\CompositeTestReporter.h"
334+
>
335+
</File>
328336
<File
329337
RelativePath="..\config.h"
330338
>

0 commit comments

Comments
 (0)