1#ifndef BANDIT_SPEC_REPORTER_H
2#define BANDIT_SPEC_REPORTER_H
3
4#include <bandit/reporters/colored_reporter.h>
5#include <bandit/reporters/test_run_summary.h>
6
7namespace bandit {
8 namespace detail {
9 struct spec_reporter : public colored_reporter {
10 spec_reporter(std::ostream& stm, const failure_formatter& failure_formatter,
11 const colorizer& colorizer)
12 : colored_reporter(stm, failure_formatter, colorizer), indentation_(0) {}
13
14 spec_reporter(const failure_formatter& failure_formatter, const colorizer& colorizer)
15 : spec_reporter(std::cout, failure_formatter, colorizer) {}
16
17 spec_reporter& operator=(const spec_reporter&) {
18 return *this;
19 }
20
21 void test_run_complete() override {
22 progress_reporter::test_run_complete();
23
24 stm_ << std::endl;
25
26 test_run_summary summary(specs_run_, specs_failed_, specs_succeeded_, specs_skipped_, failures_,
27 test_run_errors_, colorizer_);
28 summary.write(stm_);
29 stm_.flush();
30 }
31
32 void test_run_error(const std::string& desc, const struct test_run_error& err) override {
33 progress_reporter::test_run_error(desc, err);
34
35 std::stringstream ss;
36 ss << std::endl;
37 ss << "Failed to run \"" << current_context_name() << "\": error \"" << err.what() << "\"" << std::endl;
38
39 test_run_errors_.push_back(ss.str());
40 }
41
42 void context_starting(const std::string& desc) override {
43 progress_reporter::context_starting(desc);
44
45 stm_ << indent();
46 stm_ << "describe " << desc << std::endl;
47 increase_indent();
48 stm_.flush();
49 }
50
51 void context_ended(const std::string& desc) override {
52 progress_reporter::context_ended(desc);
53 decrease_indent();
54 }
55
56 void it_starting(const std::string& desc) override {
57 progress_reporter::it_starting(desc);
58 stm_ << indent() << "- it " << desc << " ... ";
59 stm_.flush();
60 }
61
62 void it_succeeded(const std::string& desc) override {
63 progress_reporter::it_succeeded(desc);
64 stm_ << colorizer_.green();
65 stm_ << "OK";
66 stm_ << colorizer_.reset();
67 stm_ << std::endl;
68 stm_.flush();
69 }
70
71 void it_failed(const std::string& desc, const assertion_exception& ex) override {
72 progress_reporter::it_failed(desc, ex);
73 stm_ << colorizer_.red();
74 stm_ << "FAILED";
75 stm_ << colorizer_.reset();
76 stm_ << std::endl;
77 stm_.flush();
78 }
79
80 void it_unknown_error(const std::string& desc) override {
81 progress_reporter::it_unknown_error(desc);
82 stm_ << colorizer_.red();
83 stm_ << "ERROR";
84 stm_ << colorizer_.reset();
85 stm_ << std::endl;
86 stm_.flush();
87 }
88
89 void it_skip(const std::string& desc) override {
90 progress_reporter::it_skip(desc);
91 stm_ << indent() << "- it " << desc << " ... SKIPPED" << std::endl;
92 stm_.flush();
93 }
94
95 private:
96 void increase_indent() {
97 indentation_++;
98 }
99
100 void decrease_indent() {
101 indentation_--;
102 }
103
104 std::string indent() {
105 return std::string(indentation_, '\t');
106 }
107
108 private:
109 int indentation_;
110 };
111 }
112}
113#endif
114