File: test_ref_args.cpp

package info (click to toggle)
hippomocks 5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 672 kB
  • sloc: cpp: 7,791; ansic: 31; makefile: 28
file content (135 lines) | stat: -rw-r--r-- 2,300 bytes parent folder | download | duplicates (2)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "hippomocks.h"
#include "Framework.h"

using HippoMocks::byRef;

class IRefArg {
public:
	virtual void test() = 0;
};

class IK {
public:
	virtual ~IK() {}
	virtual void f(int &);
	virtual void g(int &) = 0;
	virtual int &h() = 0;
	virtual const std::string &k() = 0;
	virtual void l(IRefArg &refArg) { refArg.test();}
};

TEST (checkRefArgumentsAccepted)
{
	MockRepository mocks;
	IK *iamock = mocks.Mock<IK>();
	int x = 42;
	mocks.ExpectCall(iamock, IK::f).With(x);
	mocks.ExpectCall(iamock, IK::g).With(x);
	iamock->f(x);
	iamock->g(x);
}


TEST (checkRefArgumentsChecked)
{
	MockRepository mocks;
	IK *iamock = mocks.Mock<IK>();
	int x = 1, y = 2;
	mocks.ExpectCall(iamock, IK::f).With(x);
	mocks.ExpectCall(iamock, IK::g).With(y);
	bool exceptionCaught = false;
	try 
	{
		iamock->f(y);
	}
	catch (HippoMocks::ExpectationException)
	{
		exceptionCaught = true;
	}
	CHECK(exceptionCaught);
	mocks.reset();
}

void plusplus(int &x) 
{ 
	x++; 
}
void plusequals2(int &x) 
{
	x+=2; 
}

TEST (checkRefArgumentsPassedAsRef)
{
	MockRepository mocks;
	IK *iamock = mocks.Mock<IK>();
	int x = 1, y = 2;
	mocks.ExpectCall(iamock, IK::f).Do(plusplus);
	mocks.ExpectCall(iamock, IK::g).Do(plusequals2);
	iamock->f(x);
	iamock->g(y);
	CHECK(x == 2);
	CHECK(y == 4);
}

TEST (checkRefReturnValues)
{
	MockRepository mocks;
	IK *iamock = mocks.Mock<IK>();
	int x = 0;
	mocks.ExpectCall(iamock, IK::h).Return(x);
	mocks.ExpectCall(iamock, IK::k).Return("Hello World");
	iamock->h() = 1;
	EQUALS(iamock->k(), "Hello World");
	EQUALS(x, 1);
}

bool operator==(const IRefArg &a, const IRefArg &b)
{
	return (&a == &b);
}

TEST (checkRefArgCheckedAsReference)
{
	MockRepository mocks;
	IK *iamock = mocks.Mock<IK>();
	IRefArg *refArg = mocks.Mock<IRefArg>();

	mocks.ExpectCall(iamock, IK::l).With(byRef(*refArg));
	iamock->l(*refArg);
}

class IB {
public:
   virtual ~IB() {}

   virtual void doSomething() const = 0;

};

class IA {
public:
   virtual ~IA() {}

   virtual const IB & getB() const = 0;
   
};

TEST (checkRefReturnAsReference)
{
   MockRepository mocks;

   IB * b = mocks.Mock<IB>();

   IA * a = mocks.Mock<IA>();

   mocks.OnCall(a, IA::getB).ReturnByRef(*b);
   mocks.ExpectCall(b, IB::doSomething);

   CHECK(b == &a->getB());
   a->getB().doSomething();

   mocks.VerifyAll();
}