Skip to content

Commit

Permalink
feature: Use colors in reporters.
Browse files Browse the repository at this point in the history
Reporters now use colors for output by default.
Colors can be turned off with the "--no-color" option from
the command line.

Addresses issue: #8
added colorizer

colorizer called from reporters

options parser "--no-color" option

colors can be turned off with "--no-colors"
  • Loading branch information
joakimkarlsson committed Jul 29, 2013
1 parent 597896e commit 5a632b4
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 29 deletions.
1 change: 0 additions & 1 deletion .vimrc
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
set makeprg=make\ -C../build-clang;\ cd\ $PWD
8 changes: 7 additions & 1 deletion bandit/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ namespace bandit { namespace detail {
return options_[REPORTER].arg;
}

bool no_color() const
{
return options_[NO_COLOR];
}

private:
enum option_index { UNKNOWN, VERSION, HELP, REPORTER };
enum option_index { UNKNOWN, VERSION, HELP, REPORTER, NO_COLOR };
static const option::Descriptor* usage()
{
static const option::Descriptor usage[] =
Expand All @@ -49,6 +54,7 @@ namespace bandit { namespace detail {
{VERSION, 0, "", "version", option::Arg::None, " --version, \tPrint version of bandit"},
{HELP, 0, "", "help", option::Arg::None, " --help, \tPrint usage and exit."},
{REPORTER, 0, "", "reporter", option::Arg::Optional, " --reporter, \tSelect reporter (dots, singleline)"},
{NO_COLOR, 0, "", "no-color", option::Arg::None, " --no-color, \tSuppress colors in output"},
{0, 0, 0, 0, 0, 0}
};

Expand Down
33 changes: 33 additions & 0 deletions bandit/reporters/colorizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef BANDIT_REPORTERS_COLORIZER_H
#define BANDIT_REPORTERS_COLORIZER_H

namespace bandit { namespace detail {

struct colorizer
{
colorizer(bool colors_enabled = true)
: colors_enabled_(colors_enabled)
{}

const char* green() const
{
return colors_enabled_ ? "\033[1;32m" : "";
}

const char* red() const
{
return colors_enabled_ ? "\033[1;31m" : "";
}

const char* reset() const
{
return colors_enabled_ ? "\033[0m" : "";
}

private:
bool colors_enabled_;
};

}}

#endif
18 changes: 10 additions & 8 deletions bandit/reporters/dots_reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ namespace bandit {

struct dots_reporter : public progress_reporter
{
dots_reporter(std::ostream& stm, const failure_formatter& failure_formatter)
: progress_reporter(failure_formatter), stm_(stm)
dots_reporter(std::ostream& stm, const failure_formatter& failure_formatter,
const detail::colorizer& colorizer)
: progress_reporter(failure_formatter), stm_(stm), colorizer_(colorizer)
{}

dots_reporter(const failure_formatter& failure_formatter)
: progress_reporter(failure_formatter), stm_(std::cout)
dots_reporter(const failure_formatter& failure_formatter, const detail::colorizer& colorizer)
: progress_reporter(failure_formatter), stm_(std::cout), colorizer_(colorizer)
{}

void test_run_complete()
Expand All @@ -20,7 +21,7 @@ namespace bandit {
stm_ << std::endl;

test_run_summary summary(specs_run_, specs_failed_, specs_succeeded_, failures_,
test_run_errors_);
test_run_errors_, colorizer_);
summary.write(stm_);
stm_.flush();
}
Expand All @@ -39,26 +40,27 @@ namespace bandit {
void it_succeeded(const char* desc)
{
progress_reporter::it_succeeded(desc);
stm_ << ".";
stm_ << colorizer_.green() << "." << colorizer_.reset();
stm_.flush();
}

void it_failed(const char* desc, const assertion_exception& ex)
{
progress_reporter::it_failed(desc, ex);
stm_ << "F";
stm_ << colorizer_.red() << "F" << colorizer_.reset();
stm_.flush();
}

void it_unknown_error(const char* desc)
{
progress_reporter::it_unknown_error(desc);
stm_ << "E";
stm_ << colorizer_.red() << "E" << colorizer_.reset();
stm_.flush();
}

private:
std::ostream& stm_;
const detail::colorizer& colorizer_;
};
}

Expand Down
1 change: 1 addition & 0 deletions bandit/reporters/reporters.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef BANDIT_REPORTERS_H
#define BANDIT_REPORTERS_H

#include <bandit/reporters/colorizer.h>
#include <bandit/reporters/progress_reporter.h>
#include <bandit/reporters/test_run_summary.h>
#include <bandit/reporters/dots_reporter.h>
Expand Down
17 changes: 10 additions & 7 deletions bandit/reporters/single_line_reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ namespace bandit {

struct single_line_reporter : public progress_reporter
{
single_line_reporter(std::ostream& stm,
const failure_formatter& failure_formatter)
: progress_reporter(failure_formatter), stm_(stm)
single_line_reporter(std::ostream& stm, const failure_formatter& failure_formatter,
const detail::colorizer& colorizer)
: progress_reporter(failure_formatter), stm_(stm), colorizer_(colorizer)
{}

single_line_reporter(const failure_formatter& failure_formatter)
: progress_reporter(failure_formatter), stm_(std::cout)
single_line_reporter(const failure_formatter& failure_formatter,
const detail::colorizer& colorizer)
: progress_reporter(failure_formatter), stm_(std::cout), colorizer_(colorizer)
{}

void test_run_complete()
Expand All @@ -21,7 +22,7 @@ namespace bandit {
stm_ << std::endl;

test_run_summary summary(specs_run_, specs_failed_, specs_succeeded_, failures_,
test_run_errors_);
test_run_errors_, colorizer_);
summary.write(stm_);
}

Expand Down Expand Up @@ -73,13 +74,15 @@ namespace bandit {

if(specs_failed_)
{
stm_ << " " << specs_succeeded_ << " succeeded. " << specs_failed_ << " failed.";
stm_ << " " << specs_succeeded_ << " succeeded. " << colorizer_.red() << specs_failed_ <<
" failed." << colorizer_.reset();
}
stm_.flush();
}

private:
std::ostream& stm_;
const detail::colorizer& colorizer_;
};
}

Expand Down
15 changes: 11 additions & 4 deletions bandit/reporters/test_run_summary.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@ namespace bandit {
struct test_run_summary
{
test_run_summary(int specs_run, int specs_failed, int specs_succeeded,
const std::list<std::string>& failures, const std::list<std::string>& test_run_errors)
const std::list<std::string>& failures, const std::list<std::string>& test_run_errors,
const detail::colorizer& colorizer)
: specs_run_(specs_run), specs_succeeded_(specs_succeeded), specs_failed_(specs_failed),
failures_(failures), test_run_errors_(test_run_errors)
failures_(failures), test_run_errors_(test_run_errors), colorizer_(colorizer)
{}

void write(std::ostream& stm)
{
if(specs_run_ == 0 && test_run_errors_.size() == 0)
{
stm << "Could not find any tests." << std::endl;
stm << colorizer_.red() << "Could not find any tests." << colorizer_.reset() << std::endl;
return;
}

if(specs_failed_ == 0 && test_run_errors_.size() == 0)
{
stm << colorizer_.green() << "Success!" << colorizer_.reset() << std::endl;
}

if(test_run_errors_.size() > 0)
{
std::for_each(test_run_errors_.begin(), test_run_errors_.end(),
Expand All @@ -30,7 +36,7 @@ namespace bandit {

if(specs_failed_ > 0)
{
stm << "There were failures!" << std::endl;
stm << colorizer_.red() << "There were failures!" << colorizer_.reset() << std::endl;
std::for_each(failures_.begin(), failures_.end(),
[&](const std::string& failure) {
stm << failure << std::endl;
Expand Down Expand Up @@ -59,6 +65,7 @@ namespace bandit {
int specs_failed_;
std::list<std::string> failures_;
std::list<std::string> test_run_errors_;
const detail::colorizer& colorizer_;
};
}

Expand Down
12 changes: 6 additions & 6 deletions bandit/runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ namespace bandit {
namespace detail {

inline listener_ptr create_reporter(const options& opt,
const failure_formatter* formatter)
const failure_formatter* formatter, const colorizer& colorizer)
{
std::string name(opt.reporter() ? opt.reporter() : "");

if(name == "singleline")
{
return unique_ptr<listener>(new single_line_reporter(*formatter));
return unique_ptr<listener>(new single_line_reporter(*formatter, colorizer));
}

return unique_ptr<listener>(new dots_reporter(*formatter));
return unique_ptr<listener>(new dots_reporter(*formatter, colorizer));
}

typedef std::function<listener_ptr (const std::string&, const failure_formatter*)> reporter_factory_fn;
typedef std::function<listener* (listener*)> register_reporter_fn;
}

inline int run(const options& opt, const detail::spec_registry& specs,
contextstack_t& context_stack,
listener& listener)
contextstack_t& context_stack, listener& listener)
{
if(opt.help())
{
Expand Down Expand Up @@ -58,7 +57,8 @@ namespace bandit {
{
options opt(argc, argv);
failure_formatter_ptr formatter(new default_failure_formatter());
listener_ptr reporter(create_reporter(opt, formatter.get()));
bandit::detail::colorizer colorizer(!opt.no_color());
listener_ptr reporter(create_reporter(opt, formatter.get(), colorizer));

registered_listener(reporter.get());

Expand Down
9 changes: 9 additions & 0 deletions specs/options.spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ go_bandit([](){

AssertThat(opt.version(), IsTrue());
});

it("parses the '--no-color' option", [&](){
const char* args[] {"executable", "--no-color"};
argv_helper argv(2, args);

options opt(argv.argc(), argv.argv());

AssertThat(opt.no_color(), IsTrue());
});

describe("with no arguments", [&](){
const char* args[] = {"executable"};
Expand Down
43 changes: 43 additions & 0 deletions specs/reporters/colorizer.spec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <specs/specs.h>

go_bandit([](){

describe("colorizer: ", [&](){

describe("colors enabled", [&](){
bandit::detail::colorizer colorizer;

it("can set color to green", [&](){
AssertThat(colorizer.green(), Equals("\033[1;32m"));
});

it("set color to red", [&](){
AssertThat(colorizer.red(), Equals("\033[1;31m"));
});
it("resets color", [&](){
AssertThat(colorizer.reset(), Equals("\033[0m"));
});

});

describe("colors disabled", [&](){

bandit::detail::colorizer colorizer(false);

it("ignores setting color to green", [&](){
AssertThat(colorizer.green(), Equals(""));
});

it("ignores setting color to red", [&](){
AssertThat(colorizer.red(), Equals(""));
});

it("ignores resetting colors", [&](){
AssertThat(colorizer.reset(), Equals(""));
});

});

});

});
4 changes: 3 additions & 1 deletion specs/reporters/dots_reporter.spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ go_bandit([](){
unique_ptr<std::stringstream> stm;
unique_ptr<bandit::dots_reporter> reporter;
default_failure_formatter formatter;
bandit::detail::colorizer colorizer(false);

before_each([&](){
stm = unique_ptr<std::stringstream>(new std::stringstream());
reporter = unique_ptr<bandit::dots_reporter>(
new dots_reporter(*(stm.get()), formatter));
new dots_reporter(*(stm.get()), formatter, colorizer));
});

auto output = [&](){ return stm->str(); };
Expand Down Expand Up @@ -40,6 +41,7 @@ go_bandit([](){
});

it("reports a successful test run", [&](){
AssertThat(output(), Contains("Success!"));
AssertThat(output(), EndsWith("Test run complete. 1 tests run. 1 succeeded.\n"));
});

Expand Down
3 changes: 2 additions & 1 deletion specs/reporters/single_line_reporter.spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ go_bandit([](){
unique_ptr<std::stringstream> stm;
unique_ptr<bandit::single_line_reporter> reporter;
default_failure_formatter formatter;
bandit::detail::colorizer colorizer(false);

before_each([&](){
stm = unique_ptr<std::stringstream>(new std::stringstream());
reporter = unique_ptr<bandit::single_line_reporter>(
new single_line_reporter(*(stm.get()), formatter));
new single_line_reporter(*(stm.get()), formatter, colorizer));
});

auto output = [&](){ return stm->str(); };
Expand Down

0 comments on commit 5a632b4

Please sign in to comment.