forked from banditcpp/bandit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.h
209 lines (173 loc) · 6.61 KB
/
controller.h
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#ifndef BANDIT_CONTROLLER_H
#define BANDIT_CONTROLLER_H
#include <memory>
#include <bandit/adapters.h>
#include <bandit/colorizers/interface.h>
#include <bandit/failure_formatters/interface.h>
#include <bandit/reporters/interface.h>
#include <bandit/run_policies/interface.h>
namespace bandit {
namespace detail {
struct controller_t {
controller_t() : adapter(new adapter::snowhouse), report_timing(false) {}
context::stack_t& get_contexts() {
return context_stack;
}
assertion_adapter_t& get_adapter() {
throw_if_nullptr(adapter.get(), "assertion adapter", "bandit::detail::controller_t::set_adapter()");
return *adapter;
}
void set_adapter(assertion_adapter_t* adapter_) {
adapter.reset(adapter_);
}
colorizer_t& get_colorizer() {
throw_if_nullptr(colorizer.get(), "colorizer", "bandit::detail::controller_t::set_colorizer()");
return *colorizer;
}
void set_colorizer(colorizer_t* colorizer_) {
colorizer.reset(colorizer_);
}
failure_formatter_t& get_formatter() {
throw_if_nullptr(formatter.get(), "formatter", "bandit::detail::controller_t::set_formatter()");
return *formatter;
}
void set_formatter(failure_formatter_t* formatter_) {
formatter.reset(formatter_);
}
reporter_t& get_reporter() {
throw_if_nullptr(reporter.get(), "reporter", "bandit::detail::controller_t::set_reporter()");
return *reporter;
}
void set_reporter(reporter_t* reporter_) {
reporter.reset(reporter_);
}
run_policy_t& get_policy() {
throw_if_nullptr(run_policy.get(), "run policy", "bandit::detail::controller_t::set_policy()");
return *run_policy;
}
void set_policy(run_policy_t* run_policy_) {
run_policy.reset(run_policy_);
}
bool get_report_timing() {
return report_timing;
}
void set_report_timing(bool report_timing_) {
report_timing = report_timing_;
}
void describe(const std::string& desc, const std::function<void()>& func, bool hard_skip) {
reporter->context_starting(desc);
context_stack.back()->execution_is_starting();
context::bandit ctxt(desc, hard_skip);
context_stack.push_back(&ctxt);
try {
func();
} catch (const bandit::detail::test_run_error& error) {
reporter->test_run_error(desc, error);
}
context_stack.pop_back();
reporter->context_ended(desc);
}
void before_each(const std::function<void()>& func) {
context_stack.throw_if_empty("before_each");
context_stack.back()->register_before_each(func);
}
void after_each(const std::function<void()>& func) {
context_stack.throw_if_empty("after_each");
context_stack.back()->register_after_each(func);
}
void it(const std::string& desc, const std::function<void()>& func, bool hard_skip) {
context_stack.throw_if_empty("it");
if (hard_skip || !run_policy->should_run(desc, context_stack)) {
reporter->it_skip(desc);
} else {
reporter->it_starting(desc);
context_stack.back()->execution_is_starting();
bool success = false;
context::interface* last_successful_before_each_context = nullptr;
try_with_adapter(desc, true, [&] {
for (auto context : context_stack) {
context->run_before_eaches();
last_successful_before_each_context = context;
}
func();
success = true;
});
try_with_adapter(desc, success, [&] {
bool do_run_after_each = false;
std::for_each(context_stack.rbegin(), context_stack.rend(), [&](context::interface* context) {
if (context == last_successful_before_each_context) {
do_run_after_each = true;
}
if (do_run_after_each) {
context->run_after_eaches();
}
});
if (success) {
reporter->it_succeeded(desc);
}
});
}
}
// A function is required to initialize a static controller variable in a header file
// and this struct aims at encapsulating this function
static void register_controller(controller_t* controller) {
if (controller == nullptr) {
throw std::runtime_error("Invalid null controller passed to bandit::detail::register_controller()");
}
get_controller_address() = controller;
}
static controller_t& registered_controller() {
auto controller = get_controller_address();
throw_if_nullptr(controller, "controller", "bandit::detail::register_controller()");
return *controller;
}
private:
static controller_t*& get_controller_address() {
static controller_t* controller_ = nullptr;
return controller_;
}
static void throw_if_nullptr(const void* ptr, const std::string& name, const std::string& setter) {
if (ptr == nullptr) {
throw std::runtime_error("No " + name + " set. Please set it using " + setter);
}
}
void try_with_adapter(const std::string& desc, bool allow_fail, const std::function<void()>& do_it) {
if (allow_fail) {
try {
adapter->adapt_exceptions([&] { do_it(); });
} catch (const bandit::detail::assertion_exception& ex) {
reporter->it_failed(desc, ex);
run_policy->encountered_failure();
} catch (const std::exception& ex) {
std::string err = std::string("exception: ") + ex.what();
reporter->it_failed(desc, bandit::detail::assertion_exception(err));
run_policy->encountered_failure();
} catch (...) {
reporter->it_unknown_error(desc);
run_policy->encountered_failure();
}
} else {
try {
do_it();
} catch (...) {
/* ignore */
}
}
}
context::stack_t context_stack;
std::unique_ptr<assertion_adapter_t> adapter;
std::unique_ptr<colorizer_t> colorizer;
std::unique_ptr<failure_formatter_t> formatter;
std::unique_ptr<reporter_t> reporter;
std::unique_ptr<run_policy_t> run_policy;
bool report_timing;
};
inline void register_controller(controller_t* controller) {
controller_t::register_controller(controller);
}
inline controller_t& registered_controller() {
return controller_t::registered_controller();
}
}
}
#endif