1 #include "FailureException.h"
2 #include "HelperSuite.h"
3 #include "TestCallerTest.h"
4 #include <cppunit/extensions/HelperMacros.h>
5 
6 
7 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TestCallerTest,
8                                        helperSuiteName() );
9 
10 
11 void
testThrowFailureException()12 TestCallerTest::ExceptionThrower::testThrowFailureException()
13 {
14   throw FailureException();
15 }
16 
17 
18 void
testThrowException()19 TestCallerTest::ExceptionThrower::testThrowException()
20 {
21   throw CPPUNIT_NS::Exception( CPPUNIT_NS::Message( "expected Exception" ) );
22 }
23 
24 
25 void
testThrowNothing()26 TestCallerTest::ExceptionThrower::testThrowNothing()
27 {
28 }
29 
30 
31 
TestCallerTest()32 TestCallerTest::TestCallerTest() :
33     m_testName( "TrackedTestCaseCaller" )
34 {
35 }
36 
37 
~TestCallerTest()38 TestCallerTest::~TestCallerTest()
39 {
40 }
41 
42 
43 void
setUp()44 TestCallerTest::setUp()
45 {
46   m_constructorCount = 0;
47   m_destructorCount = 0;
48   m_setUpCount = 0;
49   m_tearDownCount = 0;
50   m_testCount = 0;
51   TrackedTestCase::setTracker( this );
52   m_testListener = new MockTestListener( "listener1" );
53   m_result = new CPPUNIT_NS::TestResult();
54   m_result->addListener( m_testListener );
55 }
56 
57 
58 void
tearDown()59 TestCallerTest::tearDown()
60 {
61   TrackedTestCase::removeTracker();
62   delete m_result;
63   delete m_testListener;
64 }
65 
66 
67 void
onConstructor()68 TestCallerTest::onConstructor()
69 {
70   m_constructorCount++;
71 }
72 
73 
74 void
onDestructor()75 TestCallerTest::onDestructor()
76 {
77   m_destructorCount++;
78 }
79 
80 
81 void
onSetUp()82 TestCallerTest::onSetUp()
83 {
84   m_setUpCount++;
85 }
86 
87 
88 void
onTearDown()89 TestCallerTest::onTearDown()
90 {
91   m_tearDownCount++;
92 }
93 
94 
95 void
onTest()96 TestCallerTest::onTest()
97 {
98   m_testCount++;
99 }
100 
101 
102 void
testBasicConstructor()103 TestCallerTest::testBasicConstructor()
104 {
105   {
106     CPPUNIT_NS::TestCaller<TrackedTestCase> caller( m_testName,
107                                                  &TrackedTestCase::test );
108     checkTestName( caller.getName() );
109     checkNothingButConstructorCalled();
110 
111     caller.run( m_result );
112 
113     checkRunningSequenceCalled();
114   } // Force destruction of the test caller.
115   CPPUNIT_ASSERT_EQUAL( 1, m_destructorCount );
116 }
117 
118 
119 void
testReferenceConstructor()120 TestCallerTest::testReferenceConstructor()
121 {
122   TrackedTestCase testCase;
123   {
124     CPPUNIT_NS::TestCaller<TrackedTestCase> caller( "TrackedTestCaseCaller",
125                                                  &TrackedTestCase::test,
126                                                  testCase );
127     checkTestName( caller.getName() );
128     checkNothingButConstructorCalled();
129 
130     caller.run( m_result );
131 
132     checkRunningSequenceCalled();
133   } // Force destruction of the test caller.
134   CPPUNIT_ASSERT_EQUAL( 0, m_destructorCount );
135 }
136 
137 
138 void
testPointerConstructor()139 TestCallerTest::testPointerConstructor()
140 {
141   TrackedTestCase *testCase = new TrackedTestCase();
142   {
143     CPPUNIT_NS::TestCaller<TrackedTestCase> caller( m_testName,
144                                                  &TrackedTestCase::test,
145                                                  testCase );
146     checkTestName( caller.getName() );
147     checkNothingButConstructorCalled();
148 
149     caller.run( m_result );
150 
151     checkRunningSequenceCalled();
152   } // Force destruction of the test caller.
153   CPPUNIT_ASSERT_EQUAL( 1, m_destructorCount );
154 }
155 
156 /*
157 // Now done by ExceptionTestCaseDecorator
158 
159 void
160 TestCallerTest::testExpectFailureException()
161 {
162   CPPUNIT_NS::TestCaller<ExceptionThrower,FailureException> caller(
163       m_testName,
164       &ExceptionThrower::testThrowFailureException );
165   m_testListener->setExpectNoFailure();
166   caller.run( m_result );
167   m_testListener->verify();
168 }
169 
170 
171 void
172 TestCallerTest::testExpectException()
173 {
174   CPPUNIT_NS::TestCaller<ExceptionThrower,CPPUNIT_NS::Exception> caller(
175       m_testName,
176       &ExceptionThrower::testThrowException );
177   m_testListener->setExpectNoFailure();
178   caller.run( m_result );
179   m_testListener->verify();
180 }
181 
182 
183 void
184 TestCallerTest::testExpectedExceptionNotCaught()
185 {
186   CPPUNIT_NS::TestCaller<ExceptionThrower,FailureException> caller(
187       m_testName,
188       &ExceptionThrower::testThrowNothing );
189   m_testListener->setExpectedAddFailureCall( 1 );
190   caller.run( m_result );
191   m_testListener->verify();
192 }
193 */
194 
195 void
checkNothingButConstructorCalled()196 TestCallerTest::checkNothingButConstructorCalled()
197 {
198   CPPUNIT_ASSERT_EQUAL( 1, m_constructorCount );
199   CPPUNIT_ASSERT_EQUAL( 0, m_destructorCount );
200   CPPUNIT_ASSERT_EQUAL( 0, m_setUpCount );
201   CPPUNIT_ASSERT_EQUAL( 0, m_tearDownCount );
202   CPPUNIT_ASSERT_EQUAL( 0, m_testCount );
203 }
204 
205 
206 void
checkRunningSequenceCalled()207 TestCallerTest::checkRunningSequenceCalled()
208 {
209   CPPUNIT_ASSERT_EQUAL( 1, m_setUpCount );
210   CPPUNIT_ASSERT_EQUAL( 1, m_testCount );
211   CPPUNIT_ASSERT_EQUAL( 1, m_tearDownCount );
212 }
213 
214 
215 void
checkTestName(std::string testName)216 TestCallerTest::checkTestName( std::string testName )
217 {
218   CPPUNIT_ASSERT( testName == m_testName );
219 }
220