-
Notifications
You must be signed in to change notification settings - Fork 170
/
CatchFakeit.hpp
153 lines (114 loc) · 4.44 KB
/
CatchFakeit.hpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#pragma once
#include "fakeit/DefaultFakeit.hpp"
#include "fakeit/EventHandler.hpp"
#include "mockutils/to_string.hpp"
#if __has_include("catch2/catch.hpp")
# include "catch2/catch.hpp"
#else
# include "catch.hpp"
#endif
namespace fakeit {
struct VerificationException : public FakeitException {
virtual ~VerificationException() = default;
void setFileInfo(const char *file, int line, const char *callingMethod) {
_file = file;
_callingMethod = callingMethod;
_line = line;
}
const char *file() const {
return _file;
}
int line() const {
return _line;
}
const char *callingMethod() const {
return _callingMethod;
}
private:
const char *_file;
int _line;
const char *_callingMethod;
};
struct NoMoreInvocationsVerificationException : public VerificationException {
NoMoreInvocationsVerificationException(std::string format) : //
_format(format) { //
}
virtual std::string what() const override {
return _format;
}
private:
std::string _format;
};
struct SequenceVerificationException : public VerificationException {
SequenceVerificationException(const std::string &format) : //
_format(format) //
{
}
virtual std::string what() const override {
return _format;
}
private:
std::string _format;
};
class CatchAdapter : public EventHandler {
EventFormatter &_formatter;
std::string formatLineNumber(std::string file, int num) {
#ifndef __GNUG__
return file + std::string("(") + fakeit::to_string(num) + std::string(")");
#else
return file + std::string(":") + fakeit::to_string(num);
#endif
}
public:
virtual ~CatchAdapter() = default;
CatchAdapter(EventFormatter &formatter)
: _formatter(formatter) {}
void fail(
std::string vetificationType,
Catch::SourceLineInfo sourceLineInfo,
std::string failingExpression,
std::string fomattedMessage,
Catch::ResultWas::OfType resultWas = Catch::ResultWas::OfType::ExpressionFailed ){
Catch::AssertionHandler catchAssertionHandler( vetificationType, sourceLineInfo, failingExpression, Catch::ResultDisposition::Normal );
INTERNAL_CATCH_TRY { \
CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \
catchAssertionHandler.handleMessage(resultWas, fomattedMessage); \
CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS \
} INTERNAL_CATCH_CATCH(catchAssertionHandler) { \
INTERNAL_CATCH_REACT(catchAssertionHandler) \
}
}
virtual void handle(const UnexpectedMethodCallEvent &evt) override {
std::string format = _formatter.format(evt);
fail("UnexpectedMethodCall",::Catch::SourceLineInfo("Unknown file",0),"",format, Catch::ResultWas::OfType::ExplicitFailure);
}
virtual void handle(const SequenceVerificationEvent &evt) override {
std::string format(formatLineNumber(evt.file(), evt.line()) + ": " + _formatter.format(evt));
std::string expectedPattern {DefaultEventFormatter::formatExpectedPattern(evt.expectedPattern())};
fail("Verify",::Catch::SourceLineInfo(evt.file(),evt.line()),expectedPattern,format);
}
virtual void handle(const NoMoreInvocationsVerificationEvent &evt) override {
std::string format(formatLineNumber(evt.file(), evt.line()) + ": " + _formatter.format(evt));
fail("VerifyNoMoreInvocations",::Catch::SourceLineInfo(evt.file(),evt.line()),"",format);
}
};
class CatchFakeit : public DefaultFakeit {
public:
virtual ~CatchFakeit() = default;
CatchFakeit() : _formatter(), _catchAdapter(_formatter) {}
static CatchFakeit &getInstance() {
static CatchFakeit instance;
return instance;
}
protected:
fakeit::EventHandler &accessTestingFrameworkAdapter() override {
return _catchAdapter;
}
EventFormatter &accessEventFormatter() override {
return _formatter;
}
private:
DefaultEventFormatter _formatter;
CatchAdapter _catchAdapter;
};
}