1 #include "UnitTest++/UnitTestPP.h"
2 #include "UnitTest++/TestMacros.h"
3 #include "UnitTest++/TestList.h"
4 #include "UnitTest++/TestResults.h"
5 #include "UnitTest++/TestReporter.h"
6 #include "UnitTest++/ReportAssert.h"
7 #include "RecordingReporter.h"
8 #include "ScopedCurrentTest.h"
9 
10 using namespace UnitTest;
11 using namespace std;
12 
13 namespace {
14 
15 TestList list1;
16 TEST_EX(DummyTest, list1)
17 {
18 }
19 
20 TEST (TestsAreAddedToTheListThroughMacro)
21 {
22     CHECK(list1.GetHead() != 0);
23     CHECK(list1.GetHead()->m_nextTest == 0);
24 }
25 
26 #ifndef UNITTEST_NO_EXCEPTIONS
27 
28 struct ThrowingThingie
29 {
30     ThrowingThingie() : dummy(false)
31     {
32         if (!dummy)
33             throw "Oops";
34     }
35 
36     bool dummy;
37 };
38 
39 TestList list2;
40 TEST_FIXTURE_EX(ThrowingThingie, DummyTestName, list2)
41 {
42 }
43 
44 TEST (ExceptionsInFixtureAreReportedAsHappeningInTheFixture)
45 {
46     RecordingReporter reporter;
47     TestResults result(&reporter);
48 	{
49 		ScopedCurrentTest scopedResults(result);
50 		list2.GetHead()->Run();
51 	}
52 
53     CHECK(strstr(reporter.lastFailedMessage, "xception"));
54     CHECK(strstr(reporter.lastFailedMessage, "fixture"));
55     CHECK(strstr(reporter.lastFailedMessage, "ThrowingThingie"));
56 }
57 
58 #endif
59 
60 struct DummyFixture
61 {
62     int x;
63 };
64 
65 // We're really testing the macros so we just want them to compile and link
66 SUITE(TestSuite1)
67 {
68 	TEST(SimilarlyNamedTestsInDifferentSuitesWork)
69 	{
70 	}
71 
72 	TEST_FIXTURE(DummyFixture, SimilarlyNamedFixtureTestsInDifferentSuitesWork)
73 	{
74 	}
75 }
76 
77 SUITE(TestSuite2)
78 {
79 	TEST(SimilarlyNamedTestsInDifferentSuitesWork)
80 	{
81 	}
82 
83 	TEST_FIXTURE(DummyFixture,SimilarlyNamedFixtureTestsInDifferentSuitesWork)
84 	{
85 	}
86 }
87 
88 TestList macroTestList1;
89 TEST_EX(MacroTestHelper1, macroTestList1)
90 {
91 }
92 
93 TEST(TestAddedWithTEST_EXMacroGetsDefaultSuite)
94 {
95     CHECK(macroTestList1.GetHead() != NULL);
96     CHECK_EQUAL ("MacroTestHelper1", macroTestList1.GetHead()->m_details.testName);
97     CHECK_EQUAL ("DefaultSuite", macroTestList1.GetHead()->m_details.suiteName);
98 }
99 
100 TestList macroTestList2;
101 TEST_FIXTURE_EX(DummyFixture, MacroTestHelper2, macroTestList2)
102 {
103 }
104 
105 TEST(TestAddedWithTEST_FIXTURE_EXMacroGetsDefaultSuite)
106 {
107     CHECK(macroTestList2.GetHead() != NULL);
108     CHECK_EQUAL ("MacroTestHelper2", macroTestList2.GetHead()->m_details.testName);
109     CHECK_EQUAL ("DefaultSuite", macroTestList2.GetHead()->m_details.suiteName);
110 }
111 
112 #ifndef UNITTEST_NO_EXCEPTIONS
113 
114 struct FixtureCtorThrows
115 {
116 	FixtureCtorThrows()	{ throw "exception"; }
117 };
118 
119 TestList throwingFixtureTestList1;
120 TEST_FIXTURE_EX(FixtureCtorThrows, FixtureCtorThrowsTestName, throwingFixtureTestList1)
121 {
122 }
123 
124 TEST(FixturesWithThrowingCtorsAreFailures)
125 {
126 	CHECK(throwingFixtureTestList1.GetHead() != NULL);
127 	RecordingReporter reporter;
128 	TestResults result(&reporter);
129 	{
130 		ScopedCurrentTest scopedResult(result);
131 		throwingFixtureTestList1.GetHead()->Run();
132 	}
133 
134 	int const failureCount = result.GetFailedTestCount();
135 	CHECK_EQUAL(1, failureCount);
136 	CHECK(strstr(reporter.lastFailedMessage, "while constructing fixture"));
137 }
138 
139 struct FixtureDtorThrows
140 {
141 	~FixtureDtorThrows() { throw "exception"; }
142 };
143 
144 TestList throwingFixtureTestList2;
145 TEST_FIXTURE_EX(FixtureDtorThrows, FixtureDtorThrowsTestName, throwingFixtureTestList2)
146 {
147 }
148 
149 TEST(FixturesWithThrowingDtorsAreFailures)
150 {
151 	CHECK(throwingFixtureTestList2.GetHead() != NULL);
152 
153 	RecordingReporter reporter;
154 	TestResults result(&reporter);
155 	{
156 		ScopedCurrentTest scopedResult(result);
157 		throwingFixtureTestList2.GetHead()->Run();
158 	}
159 
160 	int const failureCount = result.GetFailedTestCount();
161 	CHECK_EQUAL(1, failureCount);
162 	CHECK(strstr(reporter.lastFailedMessage, "while destroying fixture"));
163 }
164 
165 const int FailingLine = 123;
166 
167 struct FixtureCtorAsserts
168 {
169 	FixtureCtorAsserts()
170 	{
171 		UnitTest::ReportAssert("assert failure", "file", FailingLine);
172 	}
173 };
174 
175 TestList ctorAssertFixtureTestList;
176 TEST_FIXTURE_EX(FixtureCtorAsserts, CorrectlyReportsAssertFailureInCtor, ctorAssertFixtureTestList)
177 {
178 }
179 
180 TEST(CorrectlyReportsFixturesWithCtorsThatAssert)
181 {
182 	RecordingReporter reporter;
183 	TestResults result(&reporter);
184 	{
185 		ScopedCurrentTest scopedResults(result);
186 		ctorAssertFixtureTestList.GetHead()->Run();
187 	}
188 
189 	const int failureCount = result.GetFailedTestCount();
190 	CHECK_EQUAL(1, failureCount);
191 	CHECK_EQUAL(FailingLine, reporter.lastFailedLine);
192 	CHECK(strstr(reporter.lastFailedMessage, "assert failure"));
193 }
194 
195 #endif
196 
197 }
198 
199 // We're really testing if it's possible to use the same suite in two files
200 // to compile and link successfuly (TestTestSuite.cpp has suite with the same name)
201 // Note: we are outside of the anonymous namespace
202 SUITE(SameTestSuite)
203 {
204 	TEST(DummyTest1)
205 	{
206 	}
207 }
208 
209 #define CUR_TEST_NAME CurrentTestDetailsContainCurrentTestInfo
210 #define INNER_STRINGIFY(X) #X
211 #define STRINGIFY(X) INNER_STRINGIFY(X)
212 
213 TEST(CUR_TEST_NAME)
214 {
215 	const UnitTest::TestDetails* details = CurrentTest::Details();
216 	CHECK_EQUAL(STRINGIFY(CUR_TEST_NAME), details->testName);
217 }
218 
219 #undef CUR_TEST_NAME
220 #undef INNER_STRINGIFY
221 #undef STRINGIFY
222