File: MockFunctor.h

package info (click to toggle)
cppunit 1.13.2-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,980 kB
  • ctags: 3,579
  • sloc: cpp: 18,338; sh: 14,983; ansic: 3,011; makefile: 423; xml: 58; csh: 6
file content (90 lines) | stat: -rw-r--r-- 1,761 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef MOCKFUNCTOR_H
#define MOCKFUNCTOR_H

#include <cppunit/TestAssert.h>
#include <cppunit/Protector.h>
#include "FailureException.h"
#include "MockProtector.h"


class MockFunctor : public CPPUNIT_NS::Functor
{
public:
  MockFunctor()
    : m_shouldSucceed( true )
    , m_shouldThrow( false )
    , m_shouldThrowFailureException( false )
    , m_hasExpectation( false )
    , m_actualCallCount( 0 )
    , m_expectedCallCount( 0 )
  {
  }


  bool operator()() const
  {
    ++CPPUNIT_CONST_CAST(MockFunctor *,this)->m_actualCallCount;

    if ( m_shouldThrow )
    {
      if ( m_shouldThrowFailureException )
        throw FailureException();
      throw MockProtectorException();
    }

    return m_shouldSucceed;
  }

  void setThrowFailureException()
  {
    m_shouldThrow = true;
    m_shouldThrowFailureException = true;
    ++m_expectedCallCount;
    m_hasExpectation = true;
  }

  void setThrowMockProtectorException()
  {
    m_shouldThrow = true;
    m_shouldThrowFailureException = false;
    ++m_expectedCallCount;
    m_hasExpectation = true;
  }

  void setShouldFail()
  {
    m_shouldSucceed = false;
  }

  void setShouldSucceed()
  {
    m_shouldSucceed = true;
  }

  void setExpectedCallCount( int callCount =1 )
  {
    m_expectedCallCount = callCount;
    m_hasExpectation = true;
  }

  void verify()
  {
    if ( m_hasExpectation )
    {
      CPPUNIT_ASSERT_EQUAL_MESSAGE( "MockFunctor: bad call count",
                                    m_expectedCallCount,
                                    m_actualCallCount );
    }
  }

private:
  bool m_shouldSucceed;
  bool m_shouldThrow;
  bool m_shouldThrowFailureException;
  bool m_hasExpectation;
  int m_actualCallCount;
  int m_expectedCallCount;
};


#endif // MOCKFUNCTOR_H