1 | #ifndef BANDIT_BANDIT_RUN_POLICY_H |
2 | #define BANDIT_BANDIT_RUN_POLICY_H |
3 | |
4 | #include <bandit/filter_chain.h> |
5 | #include <bandit/run_policies/run_policy.h> |
6 | |
7 | namespace bandit { |
8 | namespace detail { |
9 | struct bandit_run_policy : public run_policy { |
10 | bandit_run_policy(filter_chain_t filter_chain, |
11 | bool break_on_failure, bool dry_run) |
12 | : run_policy(), |
13 | filter_chain_(filter_chain), |
14 | break_on_failure_(break_on_failure), dry_run_(dry_run) {} |
15 | |
16 | bool should_run(const std::string& it_name, const contextstack_t& contexts) const override { |
17 | if (dry_run_) { |
18 | return false; |
19 | } |
20 | |
21 | if (break_on_failure_ && has_encountered_failure()) { |
22 | return false; |
23 | } |
24 | |
25 | // Never run if a context has been marked as skip |
26 | // using 'describe_skip' |
27 | if (has_context_with_hard_skip(contexts)) { |
28 | return false; |
29 | } |
30 | |
31 | // Now go through the filter chain |
32 | for (auto filter : filter_chain_) { |
33 | bool match = context_matches_pattern(contexts, filter.pattern) || matches_pattern(it_name, filter.pattern); |
34 | bool skip_applies = filter.skip && match; |
35 | bool only_applies = !filter.skip && !match; |
36 | if (skip_applies || only_applies) { |
37 | return false; |
38 | } |
39 | } |
40 | |
41 | return true; |
42 | } |
43 | |
44 | private: |
45 | bool has_context_with_hard_skip(const contextstack_t& contexts) const { |
46 | contextstack_t::const_iterator it; |
47 | for (it = contexts.begin(); it != contexts.end(); it++) { |
48 | if ((*it)->hard_skip()) { |
49 | return true; |
50 | } |
51 | } |
52 | |
53 | return false; |
54 | } |
55 | |
56 | bool context_matches_pattern(const contextstack_t& contexts, const std::string& pattern) const { |
57 | for (auto context : contexts) { |
58 | if (matches_pattern(context->name(), pattern)) { |
59 | return true; |
60 | } |
61 | } |
62 | |
63 | return false; |
64 | } |
65 | |
66 | bool matches_pattern(const std::string& name, const std::string& pattern) const { |
67 | return name.find(pattern) != std::string::npos; |
68 | } |
69 | |
70 | private: |
71 | const filter_chain_t filter_chain_; |
72 | bool break_on_failure_; |
73 | bool dry_run_; |
74 | }; |
75 | } |
76 | } |
77 | #endif |
78 | |