summaryrefslogtreecommitdiff
path: root/examples/cppunittest/MockTestCase.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/cppunittest/MockTestCase.cpp')
-rw-r--r--examples/cppunittest/MockTestCase.cpp173
1 files changed, 173 insertions, 0 deletions
diff --git a/examples/cppunittest/MockTestCase.cpp b/examples/cppunittest/MockTestCase.cpp
new file mode 100644
index 0000000..6749272
--- /dev/null
+++ b/examples/cppunittest/MockTestCase.cpp
@@ -0,0 +1,173 @@
+#include "FailureException.h"
+#include "MockTestCase.h"
+
+
+MockTestCase::MockTestCase( std::string name )
+ : CppUnit::TestCase( name )
+ , m_hasSetUpExpectation( false )
+ , m_expectedSetUpCall( 0 )
+ , m_actualSetUpCall( 0 )
+ , m_hasTearDownExpectation( false )
+ , m_expectedTearDownCall( 0 )
+ , m_actualTearDownCall( 0 )
+ , m_expectRunTestCall( false )
+ , m_expectedRunTestCallCount( 0 )
+ , m_actualRunTestCallCount( 0 )
+ , m_expectCountTestCasesCall( false )
+ , m_expectedCountTestCasesCallCount( 0 )
+ , m_actualCountTestCasesCallCount( 0 )
+ , m_setUpThrow( false )
+ , m_tearDownThrow( false )
+ , m_runTestThrow( false )
+{
+}
+
+
+MockTestCase::~MockTestCase()
+{
+}
+
+
+int
+MockTestCase::countTestCases() const
+{
+ ++m_actualCountTestCasesCallCount;
+ if ( m_expectCountTestCasesCall )
+ {
+ CPPUNIT_ASSERT_MESSAGE( getName() + ": unexpected MockTestCase::countTestCases() call",
+ m_actualCountTestCasesCallCount <= m_expectedCountTestCasesCallCount );
+ }
+
+ return SuperClass::countTestCases();
+}
+
+
+void
+MockTestCase::setUp()
+{
+ if ( m_hasSetUpExpectation )
+ {
+ ++m_actualSetUpCall;
+ CPPUNIT_ASSERT_MESSAGE( getName() + ": unexpected MockTestCase::setUp() call",
+ m_actualSetUpCall <= m_expectedSetUpCall );
+ }
+
+ if ( m_setUpThrow )
+ throw FailureException();
+}
+
+void
+MockTestCase::tearDown()
+{
+ if ( m_hasTearDownExpectation )
+ {
+ ++m_actualTearDownCall;
+ CPPUNIT_ASSERT_MESSAGE( getName() + ": unexpected MockTestCase::tearDown() call",
+ m_actualTearDownCall <= m_expectedTearDownCall );
+ }
+
+ if ( m_tearDownThrow )
+ throw FailureException();
+}
+
+
+void
+MockTestCase::runTest()
+{
+ ++m_actualRunTestCallCount;
+ if ( m_expectRunTestCall )
+ {
+ CPPUNIT_ASSERT_MESSAGE( getName() + ": unexpected MockTestCase::runTest() call",
+ m_actualRunTestCallCount <= m_expectedRunTestCallCount );
+ }
+
+ if ( m_runTestThrow )
+ throw FailureException();
+}
+
+
+void
+MockTestCase::setExpectedSetUpCall( int callCount )
+{
+ m_hasSetUpExpectation = true;
+ m_expectedSetUpCall = callCount;
+}
+
+
+void
+MockTestCase::setExpectedTearDownCall( int callCount )
+{
+}
+
+
+void
+MockTestCase::setExpectedRunTestCall( int callCount )
+{
+ m_expectRunTestCall = true;
+ m_expectedRunTestCallCount = callCount ;
+}
+
+
+void
+MockTestCase::setExpectedCountTestCasesCall( int callCount )
+{
+ m_expectCountTestCasesCall = true;
+ m_expectedCountTestCasesCallCount = callCount;
+}
+
+
+void
+MockTestCase::makeSetUpThrow()
+{
+ m_setUpThrow = true;
+}
+
+
+void
+MockTestCase::makeTearDownThrow()
+{
+ m_tearDownThrow = true;
+}
+
+
+void
+MockTestCase::makeRunTestThrow()
+{
+ m_runTestThrow = true;
+}
+
+
+void
+MockTestCase::verify()
+{
+ if ( m_hasSetUpExpectation )
+ {
+ CPPUNIT_ASSERT_EQUAL_MESSAGE( m_expectedSetUpCall,
+ m_actualSetUpCall,
+ getName() + ": bad MockTestCase::setUp() "
+ "call count" );
+ }
+
+ if ( m_hasTearDownExpectation )
+ {
+ CPPUNIT_ASSERT_EQUAL_MESSAGE( m_expectedTearDownCall,
+ m_actualTearDownCall,
+ getName() + ": bad MockTestCase::setUp() "
+ "call count" );
+ }
+
+ if ( m_expectCountTestCasesCall )
+ {
+ CPPUNIT_ASSERT_EQUAL_MESSAGE( m_expectedCountTestCasesCallCount,
+ m_actualCountTestCasesCallCount,
+ getName() + ": bad MockTestCase::countTestCases() "
+ "call count" );
+ }
+ if ( m_expectRunTestCall )
+ {
+ CPPUNIT_ASSERT_EQUAL_MESSAGE( m_expectedRunTestCallCount,
+ m_actualRunTestCallCount,
+ getName() + ": bad MockTestCase::runTest() "
+ "call count" );
+ }
+}