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
{ \

0 commit comments

Comments
 (0)