Skip to content

Commit 3a40aa8

Browse files
committed
r8 | charles.nicholson | 2010-03-16 21:27:33 -0500 (Tue, 16 Mar 2010) | 1 line
fixed bug where tests weren't correctly executing while testing the TestRunner (accessing the Details and Results singletons was redirecting failures to bogus RecordingReporters, instead of the real UT++ test engine). Hoisted assert failure reporting into ReportAssert, in preparation for setjmp/longjmp assert handling in the absence of exceptions. Slight renamings and cosmetic fixups
1 parent ab8b818 commit 3a40aa8

16 files changed

+196
-98
lines changed

src/AssertException.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,16 @@
22

33
#ifdef UNITTEST_USE_EXCEPTIONS
44

5-
#include <cstring>
6-
75
namespace UnitTest {
86

9-
AssertException::AssertException(char const* description, char const* filename, int lineNumber)
10-
: m_lineNumber(lineNumber)
7+
AssertException::AssertException()
118
{
12-
using namespace std;
13-
14-
strcpy(m_description, description);
15-
strcpy(m_filename, filename);
169
}
1710

1811
AssertException::~AssertException()
1912
{
2013
}
2114

22-
char const* AssertException::what() const
23-
{
24-
return m_description;
25-
}
26-
27-
char const* AssertException::Filename() const
28-
{
29-
return m_filename;
30-
}
31-
32-
int AssertException::LineNumber() const
33-
{
34-
return m_lineNumber;
35-
}
36-
3715
}
3816

3917
#endif

src/AssertException.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,8 @@ namespace UnitTest {
1212
class AssertException : public std::exception
1313
{
1414
public:
15-
AssertException(char const* description, char const* filename, int lineNumber);
15+
AssertException();
1616
virtual ~AssertException();
17-
18-
virtual char const* what() const;
19-
20-
char const* Filename() const;
21-
int LineNumber() const;
22-
23-
private:
24-
char m_description[512];
25-
char m_filename[256];
26-
int m_lineNumber;
2717
};
2818

2919
}

src/CheckMacros.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,15 @@
130130
if (!caught_) \
131131
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), "Expected exception: \"" #ExpectedExceptionType "\" not thrown"); \
132132
} while(0)
133-
#endif
134133

135134

136135
#define CHECK_ASSERT(expression) \
137-
CHECK_THROW(expression, UnitTest::AssertException);
136+
do \
137+
{ \
138+
UnitTest::ExpectAssert(true); \
139+
CHECK_THROW(expression, UnitTest::AssertException); \
140+
UnitTest::ExpectAssert(false); \
141+
} while(0)
138142

139143
#endif
144+
#endif

src/ExecuteTest.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
namespace UnitTest {
1515

1616
template< typename T >
17-
void ExecuteTest(T& testObject, TestDetails const& details)
17+
void ExecuteTest(T& testObject, TestDetails const& details, bool isMockTest)
1818
{
19-
CurrentTest::Details() = &details;
19+
if (isMockTest == false)
20+
CurrentTest::Details() = &details;
2021

2122
#ifndef UNITTEST_POSIX
2223
UT_TRY
@@ -33,8 +34,7 @@ void ExecuteTest(T& testObject, TestDetails const& details)
3334

3435
UT_CATCH(AssertException, e,
3536
{
36-
CurrentTest::Results()->OnTestFailure(
37-
TestDetails(details.testName, details.suiteName, e.Filename(), e.LineNumber()), e.what());
37+
(void)e;
3838
})
3939
UT_CATCH(std::exception, e,
4040
{
@@ -44,7 +44,7 @@ void ExecuteTest(T& testObject, TestDetails const& details)
4444
})
4545
UT_CATCH_ALL
4646
({
47-
CurrentTest::Results()->OnTestFailure(details, "Unhandled exception: Crash!");
47+
CurrentTest::Results()->OnTestFailure(details, "Unhandled exception: test crashed");
4848
})
4949
}
5050

src/ReportAssert.cpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,52 @@
11
#include "ReportAssert.h"
22
#include "AssertException.h"
3+
#include "CurrentTest.h"
4+
#include "TestResults.h"
5+
#include "TestDetails.h"
36

47
namespace UnitTest {
58

9+
namespace
10+
{
11+
bool& AssertExpectedFlag()
12+
{
13+
static bool s_assertExpected = false;
14+
return s_assertExpected;
15+
}
16+
}
17+
618
void ReportAssert(char const* description, char const* filename, int lineNumber)
719
{
8-
(void)description;
9-
(void)filename;
10-
(void)lineNumber;
20+
ReportAssertEx(CurrentTest::Results(), CurrentTest::Details(), description, filename, lineNumber);
21+
}
22+
23+
void ReportAssertEx(TestResults* testResults,
24+
const TestDetails* testDetails,
25+
char const* description,
26+
char const* filename,
27+
int lineNumber)
28+
{
29+
if (AssertExpectedFlag() == false)
30+
{
31+
TestDetails assertDetails(testDetails->testName, testDetails->suiteName, filename, lineNumber);
32+
testResults->OnTestFailure(assertDetails, description);
33+
}
34+
35+
ExpectAssert(false);
1136

1237
#ifdef UNITTEST_USE_EXCEPTIONS
13-
throw AssertException(description, filename, lineNumber);
38+
throw AssertException();
1439
#endif
1540
}
1641

42+
void ExpectAssert(bool expected)
43+
{
44+
AssertExpectedFlag() = expected;
45+
}
46+
47+
bool AssertExpected()
48+
{
49+
return AssertExpectedFlag();
50+
}
51+
1752
}

src/ReportAssert.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@
33

44
namespace UnitTest {
55

6+
class TestResults;
7+
class TestDetails;
8+
69
void ReportAssert(char const* description, char const* filename, int lineNumber);
7-
10+
11+
void ReportAssertEx(TestResults* testResults,
12+
const TestDetails* testDetails,
13+
char const* description,
14+
char const* filename,
15+
int lineNumber);
16+
17+
void ExpectAssert(bool expected);
18+
bool AssertExpected();
19+
820
}
921

1022
#endif

src/Test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ TestList& Test::GetTestList()
2020

2121
Test::Test(char const* testName, char const* suiteName, char const* filename, int lineNumber)
2222
: m_details(testName, suiteName, filename, lineNumber)
23-
, next(0)
23+
, m_nextTest(0)
2424
, m_timeConstraintExempt(false)
25+
, m_isMockTest(false)
2526
{
2627
}
2728

@@ -31,7 +32,7 @@ Test::~Test()
3132

3233
void Test::Run()
3334
{
34-
ExecuteTest(*this, m_details);
35+
ExecuteTest(*this, m_details, m_isMockTest);
3536
}
3637

3738
void Test::RunImpl() const

src/Test.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ class Test
1616
void Run();
1717

1818
TestDetails const m_details;
19-
Test* next;
19+
Test* m_nextTest;
2020
mutable bool m_timeConstraintExempt;
21+
mutable bool m_isMockTest;
2122

2223
static TestList& GetTestList();
2324

src/TestList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void TestList::Add(Test* test)
2121
}
2222
else
2323
{
24-
m_tail->next = test;
24+
m_tail->m_nextTest = test;
2525
m_tail = test;
2626
}
2727
}

src/TestMacros.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@
8383
({ \
8484
Fixture##Name##Helper fixtureHelper(m_details); \
8585
ctorOk = true; \
86-
UnitTest::ExecuteTest(fixtureHelper, m_details); \
86+
UnitTest::ExecuteTest(fixtureHelper, m_details, false); \
8787
}) \
8888
UT_CATCH (UnitTest::AssertException, e, \
8989
{ \
90-
UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details.testName, m_details.suiteName, e.Filename(), e.LineNumber()), e.what()); \
90+
(void)e; \
9191
}) \
9292
UT_CATCH (std::exception, e, \
9393
{ \

src/TestRunner.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ TestRunner::~TestRunner()
3232
delete m_timer;
3333
}
3434

35+
TestResults* TestRunner::GetTestResults()
36+
{
37+
return m_result;
38+
}
39+
3540
int TestRunner::Finish() const
3641
{
3742
float const secondsElapsed = static_cast<float>(m_timer->GetTimeInMs() / 1000.0);
@@ -51,7 +56,8 @@ bool TestRunner::IsTestInSuite(const Test* const curTest, char const* suiteName)
5156

5257
void TestRunner::RunTest(TestResults* const result, Test* const curTest, int const maxTestTimeInMs) const
5358
{
54-
CurrentTest::Results() = result;
59+
if (curTest->m_isMockTest == false)
60+
CurrentTest::Results() = result;
5561

5662
Timer testTimer;
5763
testTimer.Start();
@@ -70,7 +76,7 @@ void TestRunner::RunTest(TestResults* const result, Test* const curTest, int con
7076
result->OnTestFailure(curTest->m_details, stream.GetText());
7177
}
7278

73-
result->OnTestFinish(curTest->m_details, static_cast<float>(testTimeInMs/1000.0));
79+
result->OnTestFinish(curTest->m_details, static_cast< float >(testTimeInMs / 1000.0));
7480
}
7581

7682
}

src/TestRunner.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class TestRunner
2727
explicit TestRunner(TestReporter& reporter);
2828
~TestRunner();
2929

30-
template <class Predicate>
30+
template< class Predicate >
3131
int RunTestsIf(TestList const& list, char const* suiteName,
3232
const Predicate& predicate, int maxTestTimeInMs) const
3333
{
@@ -38,12 +38,14 @@ class TestRunner
3838
if (IsTestInSuite(curTest, suiteName) && predicate(curTest))
3939
RunTest(m_result, curTest, maxTestTimeInMs);
4040

41-
curTest = curTest->next;
41+
curTest = curTest->m_nextTest;
4242
}
4343

4444
return Finish();
4545
}
4646

47+
TestResults* GetTestResults();
48+
4749
private:
4850
TestReporter* m_reporter;
4951
TestResults* m_result;

0 commit comments

Comments
 (0)