1 #ifndef MOCKFUNCTOR_H 2 #define MOCKFUNCTOR_H 3 4 #include <cppunit/TestAssert.h> 5 #include <cppunit/Protector.h> 6 #include "FailureException.h" 7 #include "MockProtector.h" 8 9 10 class MockFunctor : public CPPUNIT_NS::Functor 11 { 12 public: MockFunctor()13 MockFunctor() 14 : m_shouldSucceed( true ) 15 , m_shouldThrow( false ) 16 , m_shouldThrowFailureException( false ) 17 , m_hasExpectation( false ) 18 , m_actualCallCount( 0 ) 19 , m_expectedCallCount( 0 ) 20 { 21 } 22 23 operator()24 bool operator()() const 25 { 26 ++CPPUNIT_CONST_CAST(MockFunctor *,this)->m_actualCallCount; 27 28 if ( m_shouldThrow ) 29 { 30 if ( m_shouldThrowFailureException ) 31 throw FailureException(); 32 throw MockProtectorException(); 33 } 34 35 return m_shouldSucceed; 36 } 37 setThrowFailureException()38 void setThrowFailureException() 39 { 40 m_shouldThrow = true; 41 m_shouldThrowFailureException = true; 42 ++m_expectedCallCount; 43 m_hasExpectation = true; 44 } 45 setThrowMockProtectorException()46 void setThrowMockProtectorException() 47 { 48 m_shouldThrow = true; 49 m_shouldThrowFailureException = false; 50 ++m_expectedCallCount; 51 m_hasExpectation = true; 52 } 53 setShouldFail()54 void setShouldFail() 55 { 56 m_shouldSucceed = false; 57 } 58 setShouldSucceed()59 void setShouldSucceed() 60 { 61 m_shouldSucceed = true; 62 } 63 64 void setExpectedCallCount( int callCount =1 ) 65 { 66 m_expectedCallCount = callCount; 67 m_hasExpectation = true; 68 } 69 verify()70 void verify() 71 { 72 if ( m_hasExpectation ) 73 { 74 CPPUNIT_ASSERT_EQUAL_MESSAGE( "MockFunctor: bad call count", 75 m_expectedCallCount, 76 m_actualCallCount ); 77 } 78 } 79 80 private: 81 bool m_shouldSucceed; 82 bool m_shouldThrow; 83 bool m_shouldThrowFailureException; 84 bool m_hasExpectation; 85 int m_actualCallCount; 86 int m_expectedCallCount; 87 }; 88 89 90 #endif // MOCKFUNCTOR_H 91