-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsedFake.cpp
More file actions
84 lines (70 loc) · 2.23 KB
/
UsedFake.cpp
File metadata and controls
84 lines (70 loc) · 2.23 KB
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
#include "UsedFake.h"
#include "CppUTestExt/MockSupport.h"
class UsedFakeInterface {
public:
virtual long add(long a, long b) = 0;
virtual long subtract(long a, long b) = 0;
};
class UsedDummy : public UsedFakeInterface {
public:
virtual long add(long, long) { return 0; }
virtual long subtract(long, long) { return 0; }
static UsedDummy* instance() {
static UsedDummy instance;
return &instance;
}
};
namespace C {
#include "Used.c"
}
class UsedReal : public UsedFakeInterface {
public:
virtual long add(long a, long b) { return C::Used_add(a, b); }
virtual long subtract(long a, long b) { return C::Used_subtract(a, b); }
static UsedReal* instance() {
static UsedReal instance;
return &instance;
}
};
static UsedFakeInterface* fake = UsedDummy::instance();
class UsedMock : public UsedFakeInterface {
public:
virtual long add(long a, long b) {
mock().actualCall("Used_add")
.withParameter("a", (int) a)
.withParameter("b", (int) b);
return (long) mock().returnValue().getIntValue();
}
virtual long subtract(long a, long b) {
mock().actualCall("Used_subtract")
.withParameter("a", (int) a)
.withParameter("b", (int) b);
return mock().returnValue().getIntValue();
}
static UsedMock* instance() {
static UsedMock instance;
return &instance;
}
};
#include <cstdio>
class UsedStub : public UsedFakeInterface {
public:
virtual long add(long a, long b) {
printf("Used_add(%ld, %ld) was called\n", a, b);
return a + b;
}
virtual long subtract(long a, long b) {
printf("Used_subtract(%ld, %ld) was called\n", a, b);
return a - b;
}
static UsedStub* instance() {
static UsedStub instance;
return &instance;
}
};
void UsedFake::setDummy() { fake = UsedDummy::instance(); }
void UsedFake::setMock() { fake = UsedMock::instance(); }
void UsedFake::setStub() { fake = UsedStub::instance(); }
void UsedFake::setReal() { fake = UsedReal::instance(); }
extern "C" long Used_add(long a, long b) { return fake->add(a, b); }
extern "C" long Used_subtract(long a, long b) { return fake->subtract(a, b); }