1 #include "FailureException.h"
2 #include "MockTestCase.h"
3 #include <cppunit/TestPath.h>
4 
5 
MockTestCase(std::string name)6 MockTestCase::MockTestCase( std::string name )
7     : CPPUNIT_NS::TestCase( name )
8     , m_hasSetUpExpectation( false )
9     , m_expectedSetUpCall( 0 )
10     , m_actualSetUpCall( 0 )
11     , m_hasTearDownExpectation( false )
12     , m_expectedTearDownCall( 0 )
13     , m_actualTearDownCall( 0 )
14     , m_expectRunTestCall( false )
15     , m_expectedRunTestCallCount( 0 )
16     , m_actualRunTestCallCount( 0 )
17     , m_expectCountTestCasesCall( false )
18     , m_expectedCountTestCasesCallCount( 0 )
19     , m_actualCountTestCasesCallCount( 0 )
20     , m_setUpThrow( false )
21     , m_tearDownThrow( false )
22     , m_runTestThrow( false )
23     , m_passingTest( NULL )
24 {
25 }
26 
27 
~MockTestCase()28 MockTestCase::~MockTestCase()
29 {
30 }
31 
32 
33 int
countTestCases() const34 MockTestCase::countTestCases() const
35 {
36   MockTestCase *mutableThis = CPPUNIT_CONST_CAST(MockTestCase *, this );
37   ++mutableThis->m_actualCountTestCasesCallCount;
38   if ( m_expectCountTestCasesCall )
39   {
40     CPPUNIT_ASSERT_MESSAGE( getName() + ": unexpected MockTestCase::countTestCases() call",
41                             m_actualCountTestCasesCallCount <= m_expectedCountTestCasesCallCount );
42   }
43 
44   return SuperClass::countTestCases();
45 }
46 
47 
48 void
setUp()49 MockTestCase::setUp()
50 {
51   if ( m_hasSetUpExpectation )
52   {
53     ++m_actualSetUpCall;
54     CPPUNIT_ASSERT_MESSAGE( getName() + ": unexpected MockTestCase::setUp() call",
55                             m_actualSetUpCall <= m_expectedSetUpCall );
56   }
57 
58   if ( m_setUpThrow )
59     throw FailureException();
60 }
61 
62 void
tearDown()63 MockTestCase::tearDown()
64 {
65   if ( m_hasTearDownExpectation )
66   {
67     ++m_actualTearDownCall;
68     CPPUNIT_ASSERT_MESSAGE( getName() + ": unexpected MockTestCase::tearDown() call",
69                             m_actualTearDownCall <= m_expectedTearDownCall );
70   }
71 
72   if ( m_tearDownThrow )
73     throw FailureException();
74 }
75 
76 
77 void
runTest()78 MockTestCase::runTest()
79 {
80   ++m_actualRunTestCallCount;
81   if ( m_expectRunTestCall )
82   {
83     CPPUNIT_ASSERT_MESSAGE( getName() + ": unexpected MockTestCase::runTest() call",
84                             m_actualRunTestCallCount <= m_expectedRunTestCallCount );
85   }
86 
87   if ( m_runTestThrow )
88     throw FailureException();
89 }
90 
91 /*
92 bool
93 MockTestCase::findTestPath( const CPPUNIT_NS::Test *test,
94                             CPPUNIT_NS::TestPath &testPath )
95 {
96   if ( m_passingTest == test )
97   {
98     testPath.add( this );
99     return true;
100   }
101 
102   return false;
103 }
104 */
105 
106 void
setExpectedSetUpCall(int callCount)107 MockTestCase::setExpectedSetUpCall( int callCount )
108 {
109   m_hasSetUpExpectation = true;
110   m_expectedSetUpCall = callCount;
111 }
112 
113 
114 void
setExpectedTearDownCall(int)115 MockTestCase::setExpectedTearDownCall( int )
116 {
117 }
118 
119 
120 void
setExpectedRunTestCall(int callCount)121 MockTestCase::setExpectedRunTestCall( int callCount )
122 {
123   m_expectRunTestCall = true;
124   m_expectedRunTestCallCount = callCount ;
125 }
126 
127 
128 void
setExpectedCountTestCasesCall(int callCount)129 MockTestCase::setExpectedCountTestCasesCall( int callCount )
130 {
131   m_expectCountTestCasesCall = true;
132   m_expectedCountTestCasesCallCount = callCount;
133 }
134 
135 
136 void
makeSetUpThrow()137 MockTestCase::makeSetUpThrow()
138 {
139   m_setUpThrow = true;
140 }
141 
142 
143 void
makeTearDownThrow()144 MockTestCase::makeTearDownThrow()
145 {
146   m_tearDownThrow = true;
147 }
148 
149 
150 void
makeRunTestThrow()151 MockTestCase::makeRunTestThrow()
152 {
153   m_runTestThrow = true;
154 }
155 
156 
157 void
verify()158 MockTestCase::verify()
159 {
160   if ( m_hasSetUpExpectation )
161   {
162     CPPUNIT_ASSERT_EQUAL_MESSAGE( getName() + ": bad MockTestCase::setUp() "
163                                   "call count",
164                                   m_expectedSetUpCall,
165                                   m_actualSetUpCall );
166   }
167 
168   if ( m_hasTearDownExpectation )
169   {
170     CPPUNIT_ASSERT_EQUAL_MESSAGE( getName() + ": bad MockTestCase::tearDown() "
171                                   "call count",
172                                   m_expectedTearDownCall,
173                                   m_actualTearDownCall );
174   }
175 
176   if ( m_expectCountTestCasesCall )
177   {
178     CPPUNIT_ASSERT_EQUAL_MESSAGE( getName() + ": bad MockTestCase::countTestCases() "
179                                   "call count",
180                                   m_expectedCountTestCasesCallCount,
181                                   m_actualCountTestCasesCallCount );
182   }
183   if ( m_expectRunTestCall )
184   {
185     CPPUNIT_ASSERT_EQUAL_MESSAGE( getName() + ": bad MockTestCase::runTest() "
186                                   "call count",
187                                   m_expectedRunTestCallCount,
188                                   m_actualRunTestCallCount );
189   }
190 }
191