-
Notifications
You must be signed in to change notification settings - Fork 20
/
test.cpp
121 lines (99 loc) · 2.69 KB
/
test.cpp
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
#include "lmock.h"
#include <unistd.h>
#include <assert.h>
#include <iostream>
using namespace std;
//mock全局函数(任意全局函数,包括依赖的本模块、其它模块代码,以及第三方库函数,和系统函数)
//原始全局函数
int global(int a, int b) {
return a + b;
}
//对应的桩函数
int fake_global(int a, int b) {
//校验参数正确性,确定被测代码传入了正确的值
assert(a == 3);
assert(b == 2);
//给一个返回值,配合被测代码走特定分支
return a - b;
}
void test_global() {
cout << "global ..." << endl;
//直接调用原函数
assert(global(3, 2) == 5);
//执行mock替换
assert(0 == mock(&global, &fake_global));
//调用mock后的函数,可以看到返回值变了
assert(global(3, 2) == 1);
//结束mock
reset();
//函数行为恢复
assert(global(3, 2) == 5);
}
class A {
public:
int member(int a) {return ++a;}
static int static_member(int a) {return 200;}
virtual int virtual_member() {return 400;}
};
//mock普通成员函数(支持模板类、函数)
int fake_member(A *pTihs, int a) {
return --a;
}
void test_member() {
cout << "member ..." << endl;
A a;
assert(a.member(100) == 101);
mock(&A::member, fake_member);
assert(a.member(100) == 99);
reset();
assert(a.member(100) == 101);
}
//mock静态成员函数
int fake_static_member() {
return 300;
}
void test_static_member() {
cout << "static member ..." << endl;
assert(A::static_member(200) == 200);
mock(&A::static_member, fake_static_member);
assert(A::static_member(100) == 300);
reset();
assert(A::static_member(200) == 200);
}
//mock虚函数
int fake_virtual_member(A *pThis) {
return 500;
}
void test_virtual_member() {
cout << "virtual member ..." << endl;
A a;
assert(a.virtual_member() == 400);
//虚函数mock需要多传一个相关类的对象,任意一个对象即可,跟实际代码中的对象没有关系
A a_obj;
mock(&A::virtual_member, fake_virtual_member, &a_obj);
assert(a.virtual_member() == 500);
reset();
assert(a.virtual_member() == 400);
}
//mock系统函数
int fake_write(int, char*, int) {
return 100;
}
void test_system() {
cout << "system function ..." << endl;
assert(write(5, "hello", 5) == -1);
mock(write, fake_write);
assert(write(5, "hello", 5) == 100);
reset();
assert(write(5, "hello", 5) == -1);
}
//另外第三方库,如mysql,也是可以支持mock的
int main() {
test_global();
test_member();
test_static_member();
test_virtual_member();
test_system();
cout << "Everything is ok." << endl;
return 0;
}