-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
94 lines (77 loc) · 1.8 KB
/
template.go
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
package main
const (
cppTemplate = `//////////////////////////////////////////
// for {{.Path}}
//////////////////////////////////////////
#include "{{.Prefix}}{{.FileName}}.hpp"
{{range .Classes -}}
{{if ne .DeclarationFile "" -}}
#include "{{.DeclarationFile}}"
{{- end}}
{{- end}}
{{- range .Classes}}
//////////////////////////////////////////
// for Class {{.Name}}
//////////////////////////////////////////
I{{.Name}} *Mock{{.Name}}::mock = nullptr;
bool Mock{{.Name}}::mockEnable = false;
bool *Mock{{.Name}}::useMock = nullptr;
Mock{{.Name}}::Mock{{.Name}}() {
origin = new {{.Name}}();
callCount = 0;
}
Mock{{.Name}}::~Mock{{.Name}}() {
delete origin;
}
{{- $class := .Name -}}
{{range .Funcs}}
{{.Return}} Mock{{$class}}::{{.Name}}({{.ArgWithTypes}}) {
callCount++;
if (mockEnable && useMock[callCount - 1]) {
return mock->{{.Name}}({{.Args}});
} else {
return origin->{{.Name}}({{.Args}});
}
}
{{- end}}
{{- end}}
`
hppTemplate = `//////////////////////////////////////////
// for {{.Path}}
//////////////////////////////////////////
#pragma once
{{range .Classes -}}
{{if ne .DeclarationFile "" -}}
#include "{{.DeclarationFile}}"
{{- end}}
{{- end -}}
{{range .Classes}}
//////////////////////////////////////////
// for Class {{.Name}}
//////////////////////////////////////////
class I{{.Name}} {
public:
virtual ~I{{.Name}}() = 0;
{{range .Funcs}}
virtual {{.Return}} {{.Name}}({{.ArgWithTypes}}) = 0;
{{- end}}
};
class Mock{{.Name}} {
public:
// for mock
static I{{.Name}}* mock;
static bool mockEnable;
static bool* useMock;
Mock{{.Name}}();
~Mock{{.Name}}();
{{range .Funcs}}
{{.Return}} {{.Name}}({{.ArgWithTypes}});
{{- end}}
private:
// for original
{{.Name}}* origin;
int callCount;
};
{{- end}}
`
)