Skip to content

Commit

Permalink
Remove Mock-specific crash-on-fail
Browse files Browse the repository at this point in the history
Mapped crashOnFailure() method onto the global setting to allow runtime control of the behaviour
  • Loading branch information
Steve Hill (Wireless) committed Dec 8, 2022
1 parent 1de12bd commit 89b6b1a
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 60 deletions.
2 changes: 0 additions & 2 deletions include/CppUTest/Utest.h
Expand Up @@ -184,8 +184,6 @@ class UtestShell

virtual SimpleString getMacroName() const;
TestResult *getTestResult();
static const TestTerminatorWithoutExceptions normalTestTerminatorWithoutExceptions_;
static const CrashingTestTerminatorWithoutExceptions crashingTestTerminatorWithoutExceptions_;
private:
const char *group_;
const char *name_;
Expand Down
6 changes: 1 addition & 5 deletions include/CppUTestExt/MockFailure.h
Expand Up @@ -38,16 +38,12 @@ class MockFailure;

class MockFailureReporter
{
protected:
bool crashOnFailure_;
public:
MockFailureReporter() : crashOnFailure_(false){}
MockFailureReporter() {}
virtual ~MockFailureReporter() {}

virtual void failTest(const MockFailure& failure);
virtual UtestShell* getTestToFail();

virtual void crashOnFailure(bool shouldCrash) { crashOnFailure_ = shouldCrash; }
};

class MockFailure : public TestFailure
Expand Down
3 changes: 0 additions & 3 deletions src/CppUTest/Utest.cpp
Expand Up @@ -603,9 +603,6 @@ UtestShell* UtestShell::getCurrent()
return currentTest_;
}

const TestTerminatorWithoutExceptions UtestShell::normalTestTerminatorWithoutExceptions_;
const CrashingTestTerminatorWithoutExceptions UtestShell::crashingTestTerminatorWithoutExceptions_;
const TestTerminator *UtestShell::currentTestTerminatorWithoutExceptions_ = &UtestShell::normalTestTerminatorWithoutExceptions_;
const TestTerminator &UtestShell::getCurrentTestTerminator()
{
return *currentTestTerminator_;
Expand Down
25 changes: 1 addition & 24 deletions src/CppUTestExt/MockFailure.cpp
Expand Up @@ -31,33 +31,10 @@
#include "CppUTestExt/MockExpectedCallsList.h"
#include "CppUTestExt/MockNamedValue.h"

class MockFailureReporterTestTerminator : public TestTerminator
{
public:
MockFailureReporterTestTerminator(bool crashOnFailure) : crashOnFailure_(crashOnFailure)
{
}

virtual void exitCurrentTest() const _override
{
if (crashOnFailure_)
UT_CRASH();

UtestShell::getCurrentTestTerminator().exitCurrentTest();
} // LCOV_EXCL_LINE

virtual ~MockFailureReporterTestTerminator() _destructor_override
{
}
private:
bool crashOnFailure_;

};

void MockFailureReporter::failTest(const MockFailure& failure)
{
if (!getTestToFail()->hasFailed())
getTestToFail()->failWith(failure, MockFailureReporterTestTerminator(crashOnFailure_));
getTestToFail()->failWith(failure, UtestShell::getCurrentTestTerminator());
} // LCOV_EXCL_LINE

UtestShell* MockFailureReporter::getTestToFail()
Expand Down
5 changes: 4 additions & 1 deletion src/CppUTestExt/MockSupport.cpp
Expand Up @@ -64,7 +64,10 @@ MockSupport::~MockSupport()

void MockSupport::crashOnFailure(bool shouldCrash)
{
activeReporter_->crashOnFailure(shouldCrash);
if (shouldCrash)
UtestShell::setCrashOnFail();
else
UtestShell::restoreDefaultTestTerminator();
}

void MockSupport::setMockFailureStandardReporter(MockFailureReporter* reporter)
Expand Down
26 changes: 1 addition & 25 deletions src/CppUTestExt/MockSupport_c.cpp
Expand Up @@ -34,37 +34,13 @@

typedef void (*cpputest_cpp_function_pointer)(); /* Cl2000 requires cast to C++ function */

class MockFailureReporterTestTerminatorForInCOnlyCode : public TestTerminator
{
public:
MockFailureReporterTestTerminatorForInCOnlyCode(bool crashOnFailure) : crashOnFailure_(crashOnFailure)
{
}

virtual void exitCurrentTest() const _override
{
if (crashOnFailure_)
UT_CRASH();

UtestShell::getCurrentTestTerminatorWithoutExceptions().exitCurrentTest();
} // LCOV_EXCL_LINE
// LCOV_EXCL_START
virtual ~MockFailureReporterTestTerminatorForInCOnlyCode() _destructor_override
{
}
// LCOV_EXCL_STOP
private:
bool crashOnFailure_;

};

class MockFailureReporterForInCOnlyCode : public MockFailureReporter
{
public:
void failTest(const MockFailure& failure) _override
{
if (!getTestToFail()->hasFailed())
getTestToFail()->failWith(failure, MockFailureReporterTestTerminatorForInCOnlyCode(crashOnFailure_));
getTestToFail()->failWith(failure, UtestShell::getCurrentTestTerminatorWithoutExceptions());
} // LCOV_EXCL_LINE

};
Expand Down

0 comments on commit 89b6b1a

Please sign in to comment.