Skip to content

Fixing a unit test that's exercising the wrong case #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions UnitTest++/CheckMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ExceptionMacros.h"
#include "Checks.h"
#include "AssertException.h"
#include "RequiredCheckException.h"
#include "MemoryOutStream.h"
#include "TestDetails.h"
#include "CurrentTest.h"
Expand Down Expand Up @@ -37,10 +38,11 @@
#define CHECK(value) \
UNITTEST_MULTILINE_MACRO_BEGIN \
UT_TRY \
({ \
({ \
if (!UnitTest::Check(value)) \
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \
}) \
UT_RETHROW (UnitTest::RequiredCheckException) \
UT_CATCH (std::exception, e, \
{ \
UnitTest::MemoryOutStream message; \
Expand All @@ -58,9 +60,10 @@
#define CHECK_EQUAL(expected, actual) \
UNITTEST_MULTILINE_MACRO_BEGIN \
UT_TRY \
({ \
({ \
UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
}) \
UT_RETHROW (UnitTest::RequiredCheckException) \
UT_CATCH (std::exception, e, \
{ \
UnitTest::MemoryOutStream message; \
Expand All @@ -69,7 +72,7 @@
message.GetText()); \
}) \
UT_CATCH_ALL \
({ \
({ \
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
"Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \
}) \
Expand All @@ -78,9 +81,10 @@
#define CHECK_CLOSE(expected, actual, tolerance) \
UNITTEST_MULTILINE_MACRO_BEGIN \
UT_TRY \
({ \
({ \
UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
}) \
UT_RETHROW (UnitTest::RequiredCheckException) \
UT_CATCH (std::exception, e, \
{ \
UnitTest::MemoryOutStream message; \
Expand All @@ -89,7 +93,7 @@
message.GetText()); \
}) \
UT_CATCH_ALL \
({ \
({ \
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
"Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \
}) \
Expand All @@ -101,6 +105,7 @@
({ \
UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
}) \
UT_RETHROW (UnitTest::RequiredCheckException) \
UT_CATCH (std::exception, e, \
{ \
UnitTest::MemoryOutStream message; \
Expand All @@ -121,6 +126,7 @@
({ \
UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
}) \
UT_RETHROW (UnitTest::RequiredCheckException) \
UT_CATCH (std::exception, e, \
{ \
UnitTest::MemoryOutStream message; \
Expand All @@ -141,6 +147,7 @@
({ \
UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
}) \
UT_RETHROW (UnitTest::RequiredCheckException) \
UT_CATCH (std::exception, e, \
{ \
UnitTest::MemoryOutStream message; \
Expand Down
2 changes: 2 additions & 0 deletions UnitTest++/ExceptionMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
#ifndef UNITTEST_NO_EXCEPTIONS
#define UT_TRY(x) try x
#define UT_THROW(x) throw x
#define UT_RETHROW(ExceptionType) catch(ExceptionType&) { throw; }
#define UT_CATCH(ExceptionType, ExceptionName, CatchBody) catch(ExceptionType& ExceptionName) CatchBody
#define UT_CATCH_ALL(CatchBody) catch(...) CatchBody
#else
#define UT_TRY(x) x
#define UT_THROW(x)
#define UT_RETHROW()
#define UT_CATCH(ExceptionType, ExceptionName, CatchBody)
#define UT_CATCH_ALL(CatchBody)
#endif
Expand Down
2 changes: 2 additions & 0 deletions UnitTest++/ExecuteTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "TestResults.h"
#include "MemoryOutStream.h"
#include "AssertException.h"
#include "RequiredCheckException.h"
#include "CurrentTest.h"

#ifdef UNITTEST_NO_EXCEPTIONS
Expand Down Expand Up @@ -38,6 +39,7 @@ namespace UnitTest {
testObject.RunImpl();
})
#endif
UT_CATCH(RequiredCheckException, e, { (void)e; })
UT_CATCH(AssertException, e, { (void)e; })
UT_CATCH(std::exception, e,
{
Expand Down
18 changes: 18 additions & 0 deletions UnitTest++/RequireMacros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef UNITTEST_REQUIREMACROS_H
#define UNITTEST_REQUIREMACROS_H

#include "RequiredCheckTestReporter.h"

#ifdef REQUIRE
#error UnitTest++ redefines REQUIRE
#endif

#ifndef UNITTEST_NO_EXCEPTIONS
#define REQUIRE for(UnitTest::RequiredCheckTestReporter decoratedReporter(*UnitTest::CurrentTest::Results()); decoratedReporter.Next(); )
#endif

#ifdef UNITTEST_NO_EXCEPTIONS
#define REQUIRE
#endif

#endif
17 changes: 17 additions & 0 deletions UnitTest++/RequiredCheckException.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "RequiredCheckException.h"

#ifndef UNITTEST_NO_EXCEPTIONS

namespace UnitTest {

RequiredCheckException::RequiredCheckException()
{
}

RequiredCheckException::~RequiredCheckException() throw()
{
}

}

#endif
23 changes: 23 additions & 0 deletions UnitTest++/RequiredCheckException.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef UNITTEST_REQUIREDCHECKEXCEPTION_H
#define UNITTEST_REQUIREDCHECKEXCEPTION_H

#include "Config.h"
#ifndef UNITTEST_NO_EXCEPTIONS

#include "HelperMacros.h"
#include <exception>

namespace UnitTest {

class UNITTEST_LINKAGE RequiredCheckException : public std::exception
{
public:
RequiredCheckException();
virtual ~RequiredCheckException() throw();
};

}

#endif

#endif
26 changes: 26 additions & 0 deletions UnitTest++/RequiredCheckTestReporter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "RequiredCheckTestReporter.h"

#include "CurrentTest.h"
#include "TestResults.h"

namespace UnitTest {

RequiredCheckTestReporter::RequiredCheckTestReporter(TestResults& results)
: m_results(results)
, m_originalTestReporter(results.m_testReporter)
, m_throwingReporter(results.m_testReporter)
, m_continue(0)
{
m_results.m_testReporter = &m_throwingReporter;
}

RequiredCheckTestReporter::~RequiredCheckTestReporter()
{
m_results.m_testReporter = m_originalTestReporter;
}

bool RequiredCheckTestReporter::Next()
{
return m_continue++ == 0;
}
}
30 changes: 30 additions & 0 deletions UnitTest++/RequiredCheckTestReporter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef UNITTEST_REQUIRED_CHECK_TEST_REPORTER_H
#define UNITTEST_REQUIRED_CHECK_TEST_REPORTER_H

#include "HelperMacros.h"
#include "ThrowingTestReporter.h"

namespace UnitTest {

class TestResults;

// This RAII class decorates the current TestReporter with
// a version that throws after reporting a failure.
class UNITTEST_LINKAGE RequiredCheckTestReporter
{
public:
explicit RequiredCheckTestReporter(TestResults& results);
~RequiredCheckTestReporter();

bool Next();

private:
TestResults& m_results;
TestReporter* m_originalTestReporter;
ThrowingTestReporter m_throwingReporter;
int m_continue;
};
}

#endif

3 changes: 3 additions & 0 deletions UnitTest++/TestResults.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace UnitTest {

class RequiredCheckTestReporter;
class TestReporter;
class TestDetails;

Expand All @@ -22,6 +23,8 @@ namespace UnitTest {
int GetFailureCount() const;

private:
friend class RequiredCheckTestReporter;

TestReporter* m_testReporter;
int m_totalTestCount;
int m_failedTestCount;
Expand Down
51 changes: 51 additions & 0 deletions UnitTest++/ThrowingTestReporter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "ThrowingTestReporter.h"
#include "RequiredCheckException.h"

namespace UnitTest {

ThrowingTestReporter::ThrowingTestReporter(TestReporter* decoratedReporter)
: m_decoratedReporter(decoratedReporter)
{}

//virtual
ThrowingTestReporter::~ThrowingTestReporter()
{}

//virtual
void ThrowingTestReporter::ReportTestStart(TestDetails const& test)
{
if(m_decoratedReporter)
{
m_decoratedReporter->ReportTestStart(test);
}
}

//virtual
void ThrowingTestReporter::ReportFailure(TestDetails const& test, char const* failure)
{
if(m_decoratedReporter)
{
m_decoratedReporter->ReportFailure(test, failure);
}
throw RequiredCheckException();
}

//virtual
void ThrowingTestReporter::ReportTestFinish(TestDetails const& test, float secondsElapsed)
{
if(m_decoratedReporter)
{
m_decoratedReporter->ReportTestFinish(test, secondsElapsed);
}
}

//virtual
void ThrowingTestReporter::ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed)
{
if(m_decoratedReporter)
{
m_decoratedReporter->ReportSummary(totalTestCount, failedTestCount, failureCount, secondsElapsed);
}
}

}
26 changes: 26 additions & 0 deletions UnitTest++/ThrowingTestReporter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef UNITTEST_THROWINGTESTREPORTER_H
#define UNITTEST_THROWINGTESTREPORTER_H

#include "TestReporter.h"

namespace UnitTest {

// A TestReporter that throws when ReportFailure is called. Otherwise it
// forwards the calls to a decorated TestReporter
class ThrowingTestReporter : public TestReporter
{
public:
explicit ThrowingTestReporter(TestReporter* reporter);

virtual ~ThrowingTestReporter();
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:
TestReporter* m_decoratedReporter;
};
}

#endif
1 change: 1 addition & 0 deletions UnitTest++/UnitTestPP.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Config.h"
#include "TestMacros.h"
#include "CheckMacros.h"
#include "RequireMacros.h"
#include "TestRunner.h"
#include "TimeConstraint.h"
#include "ReportAssert.h"
Expand Down
Loading