Echo Writes Code

console_reporter.cpp

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
#include "crucible/test/console_reporter.hpp"

#include <iostream>

namespace crucible::test::console_reporter {
  namespace {
    constexpr const char MIDLINE[] = "─────";

    constexpr const char BULLET[] = "•";

    constexpr const char SEPARATOR[] = "/";

    constexpr const char BEGIN_SUITE[] = "BEGIN SUITE";

    constexpr const char PASS[] = "PASS";

    constexpr const char FAIL[] = "FAIL";

    constexpr const char RED[] = "\x1b[31m";

    constexpr const char GREEN[] = "\x1b[32m";

    constexpr const char YELLOW[] = "\x1b[33m";

    constexpr const char BRIGHT_RED[] = "\x1b[1;31m";

    constexpr const char BRIGHT_GREEN[] = "\x1b[1;32m";

    constexpr const char BRIGHT_WHITE[] = "\x1b[1;37m";

    constexpr const char RESET[] = "\x1b[0m";

    auto get_outcome_color(outcome::Outcome const &outcome) -> const char * {
      if (outcome.passed()) {
        return GREEN;
      } else {
        return RED;
      }
    }

    auto get_outcome_keyword(outcome::Outcome const &outcome) -> const char * {
      if (outcome.passed()) {
        return PASS;
      } else {
        return FAIL;
      }
    }
  }

  ConsoleReporter::~ConsoleReporter() = default;

  auto ConsoleReporter::handle_suite_start() -> void {
    std::cout
      << BRIGHT_WHITE << MIDLINE << RESET
      << " "
      << BRIGHT_WHITE << BEGIN_SUITE << RESET
      << " "
      << BRIGHT_WHITE << MIDLINE << RESET
      << "\n\n";
  }

  auto ConsoleReporter::handle_suite_end(std::size_t const pass_count, std::size_t const fail_count) -> void {
    std::cout
      << "\n"
      << BRIGHT_WHITE << MIDLINE << RESET
      << " "
      << BRIGHT_GREEN << pass_count << " " << PASS << RESET
      << " "
      << BRIGHT_WHITE << SEPARATOR << RESET
      << " "
      << BRIGHT_RED << fail_count << " " << FAIL << RESET
      << " "
      << BRIGHT_WHITE << MIDLINE << RESET
      << "\n";
  }

  auto ConsoleReporter::handle_scenario_start(scenario::Scenario const &scenario) -> void {
    static_cast<void>(scenario);
  }

  auto ConsoleReporter::handle_scenario_end(scenario::Scenario const &scenario, outcome::Outcome const &outcome) -> void {
    std::cout
      << BULLET
      << " "
      << get_outcome_color(outcome) << get_outcome_keyword(outcome) << RESET
      << " "
      << scenario.get_name()
      << "\n";

    if (outcome.failed()) {
      std::cout
        << "  "
        << BULLET
        << " "
        << YELLOW << outcome.get_details() << RESET
        << "\n";
    }
  }
}