From 88b09d46e81303d65d7098da226ccc67a6dedb3c Mon Sep 17 00:00:00 2001 From: Patrick Johnmeyer Date: Wed, 9 Jan 2013 23:44:22 -0600 Subject: [PATCH] r18 | charles.nicholson | 2010-03-18 15:42:23 -0500 (Thu, 18 Mar 2010) | 1 line add scea 'TestReporterMulti' as 'CompositeTestReporter', add tests --- Makefile | 6 +- src/CompositeTestReporter.cpp | 67 +++++++++ src/CompositeTestReporter.h | 34 +++++ src/TestReporter.cpp | 1 - src/tests/TestCompositeTestReporter.cpp | 176 ++++++++++++++++++++++++ src/tests/test-unittestpp_vs2005.vcproj | 4 + src/unittestpp_vs2005.vcproj | 8 ++ 7 files changed, 293 insertions(+), 3 deletions(-) create mode 100644 src/CompositeTestReporter.cpp create mode 100644 src/CompositeTestReporter.h create mode 100644 src/tests/TestCompositeTestReporter.cpp diff --git a/Makefile b/Makefile index b590a3f..205dedc 100644 --- a/Makefile +++ b/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)) diff --git a/src/CompositeTestReporter.cpp b/src/CompositeTestReporter.cpp new file mode 100644 index 0000000..2a3d22e --- /dev/null +++ b/src/CompositeTestReporter.cpp @@ -0,0 +1,67 @@ +#include "CompositeTestReporter.h" +#include + +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); +} + +} diff --git a/src/CompositeTestReporter.h b/src/CompositeTestReporter.h new file mode 100644 index 0000000..a4806f4 --- /dev/null +++ b/src/CompositeTestReporter.h @@ -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 diff --git a/src/TestReporter.cpp b/src/TestReporter.cpp index c783af1..78c765f 100644 --- a/src/TestReporter.cpp +++ b/src/TestReporter.cpp @@ -2,7 +2,6 @@ namespace UnitTest { - TestReporter::~TestReporter() { } diff --git a/src/tests/TestCompositeTestReporter.cpp b/src/tests/TestCompositeTestReporter.cpp new file mode 100644 index 0000000..d302577 --- /dev/null +++ b/src/tests/TestCompositeTestReporter.cpp @@ -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); +} + +} diff --git a/src/tests/test-unittestpp_vs2005.vcproj b/src/tests/test-unittestpp_vs2005.vcproj index 8747e87..b11a266 100644 --- a/src/tests/test-unittestpp_vs2005.vcproj +++ b/src/tests/test-unittestpp_vs2005.vcproj @@ -357,6 +357,10 @@ RelativePath=".\TestChecks.cpp" > + + diff --git a/src/unittestpp_vs2005.vcproj b/src/unittestpp_vs2005.vcproj index 8b1b042..b9d0e16 100644 --- a/src/unittestpp_vs2005.vcproj +++ b/src/unittestpp_vs2005.vcproj @@ -325,6 +325,14 @@ RelativePath=".\Checks.h" > + + + +