1#ifndef BANDIT_RUNNER_H
2#define BANDIT_RUNNER_H
3
4#include <bandit/options.h>
5#include <bandit/registration/registrar.h>
6#include <bandit/reporters/reporters.h>
7#include <bandit/failure_formatters/failure_formatters.h>
8#include <bandit/run_policies/run_policies.h>
9#include <bandit/version.h>
10
11namespace bandit {
12 namespace detail {
13 inline run_policy_ptr create_run_policy(const options& opt) {
14 return run_policy_ptr(new bandit_run_policy(opt.filter_chain(), opt.break_on_failure(), opt.dry_run()));
15 }
16
17 inline listener_ptr create_reporter(const options& opt,
18 const failure_formatter* formatter, const colorizer& colorizer) {
19 switch (opt.reporter()) {
20 case options::reporters::SINGLELINE:
21 return listener_ptr(new single_line_reporter(*formatter, colorizer));
22 case options::reporters::XUNIT:
23 return listener_ptr(new xunit_reporter(*formatter));
24 case options::reporters::SPEC:
25 return listener_ptr(new spec_reporter(*formatter, colorizer));
26 case options::reporters::CRASH:
27 return listener_ptr(new crash_reporter(*formatter));
28 case options::reporters::DOTS:
29 return listener_ptr(new dots_reporter(*formatter, colorizer));
30 case options::reporters::INFO:
31 default:
32 return listener_ptr(new info_reporter(*formatter, colorizer));
33 }
34 }
35
36 typedef std::function<listener_ptr(const std::string&, const failure_formatter*)> reporter_factory_fn;
37 typedef std::function<detail::listener*(detail::listener*)> register_reporter_fn;
38
39 inline failure_formatter_ptr create_formatter(const options& opt) {
40 switch (opt.formatter()) {
41 case options::formatters::VS:
42 return failure_formatter_ptr(new visual_studio_failure_formatter());
43 case options::formatters::DEFAULT:
44 default:
45 return failure_formatter_ptr(new default_failure_formatter());
46 }
47 }
48 }
49
50 inline int run(const detail::options& opt, const detail::spec_registry& specs,
51 detail::contextstack_t& context_stack, detail::listener& listener) {
52 if (opt.help() || !opt.parsed_ok()) {
53 opt.print_usage();
54 return !opt.parsed_ok();
55 }
56
57 if (opt.version()) {
58 std::cout << "bandit version " << BANDIT_VERSION << std::endl;
59 return 0;
60 }
61
62 listener.test_run_starting();
63
64 bool hard_skip = false;
65 detail::bandit_context global_context("", hard_skip);
66 context_stack.push_back(&global_context);
67
68 for (auto func : specs) {
69 func();
70 };
71
72 listener.test_run_complete();
73
74 return listener.did_we_pass() ? 0 : 1;
75 }
76
77 inline int run(int argc, char* argv[], bool allow_further = true) {
78 detail::options opt(argc, argv);
79 if (!allow_further &&
80 (opt.has_further_arguments() || opt.has_unknown_options())) {
81 opt.print_usage();
82 return 1;
83 }
84 detail::failure_formatter_ptr formatter(create_formatter(opt));
85 bandit::detail::colorizer colorizer(!opt.no_color());
86 detail::listener_ptr reporter(create_reporter(opt, formatter.get(), colorizer));
87
88 detail::register_listener(reporter.get());
89
90 detail::run_policy_ptr run_policy = create_run_policy(opt);
91 register_run_policy(run_policy.get());
92
93 return run(opt, detail::specs(), detail::context_stack(), *reporter);
94 }
95}
96#endif
97