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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "crucible/test/console_reporter.hpp"

namespace crucible
{
  namespace
  {
    constexpr char const BULLET[] { "•" };

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

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

    constexpr char const BLUE[] { "\x1b[34m" };

    constexpr char const CYAN[] { "\x1b[36m" };

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

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

    constexpr char const BRIGHT_YELLOW[] { "\x1b[1;33m" };

    constexpr char const BRIGHT_BLUE[] { "\x1b[1;34m" };

    constexpr char const BRIGHT_MAGENTA[] { "\x1b[1;35m" };

    constexpr char const BRIGHT_CYAN[] { "\x1b[1;36m" };

    constexpr char const BRIGHT[] { "\x1b[1m" };

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

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

      return RED;
    }

    auto get_outcome_keyword(Outcome const &outcome) -> std::string
    {
      if (outcome.passed())
      {
        return "Pass";
      }

      return "Fail";
    }

    auto replicate(std::string const &pattern, std::size_t const amount) -> std::string
    {
      std::string result {};

      for (std::size_t i { 0 }; i < amount; ++i)
      {
        result += pattern;
      }

      return result;
    }

    auto write_underlined(std::string const &text, std::string const &style = "") -> void
    {
      auto const underline { replicate("─", text.size()) };

      std::cout
        << style << text << RESET << "\n"
        << underline << "\n";
    }

    auto indent_by(std::string const &text, std::size_t const amount) -> std::string
    {
      std::string const indent(amount, ' ');

      auto chunk_end { text.find('\n') };
      if (chunk_end == std::string::npos)
      {
        return indent + text;
      }

      std::string result;
      std::string::size_type chunk_start { 0 };

      while (chunk_end != std::string::npos)
      {
        if (chunk_end - chunk_start > 0)
        {
          result += indent + text.substr(chunk_start, chunk_end - chunk_start);
        }

        result += "\n";

        chunk_start = chunk_end + 1;
        chunk_end = text.find('\n', chunk_start);
      }

      if (chunk_start < text.size())
      {
        result += indent + text.substr(chunk_start);
      }

      return result;
    }
  }

  ConsoleReporter::~ConsoleReporter() = default;

  auto ConsoleReporter::handle_empty_suite(std::string const &name) -> void
  {
    handle_suite_start(name);

    std::cout
      << BULLET
      << " "
      << BRIGHT_YELLOW << "No tests defined!" << RESET
      << "\n";
  }

  auto ConsoleReporter::handle_suite_start(std::string const &name) -> void
  {
    auto const full_header { "Test suite for `" + name + "`" };

    std::size_t const space_required { full_header.size() + 2 };
    std::string horizontal_bars { replicate("─", space_required) };

    std::cout
      <<"\n"
      << BRIGHT << "╭" << horizontal_bars << "╮" << RESET << "\n"
      << BRIGHT << "│ " << full_header << " │" << RESET << "\n"
      << BRIGHT << "╰" << horizontal_bars << "╯" << RESET << "\n"
      << "\n";
  }

  auto ConsoleReporter::handle_suite_end(std::size_t const pass_count, std::size_t const fail_count, std::size_t const scenario_count, std::size_t fixture_count) -> void
  {
    if (!my_failures.empty())
    {
      write_underlined("Failures", BRIGHT_RED);

      for (auto const &pair: my_failures)
      {
        auto const &name { pair.first };
        auto const &text { pair.second };

        std::cout
          << BULLET << " Scenario: " << CYAN << name << RESET << "\n"
          << BULLET << " Failure text:\n" << indent_by(text, 2) << "\n\n";
      }
    }

    write_underlined("Summary", BRIGHT);

    std::cout
      << BULLET << " " << BRIGHT_CYAN << "Scenarios: " << scenario_count << RESET
      << "\n"
      << BULLET << " " << BRIGHT_BLUE << "Fixtures: " << fixture_count << RESET
      << "\n"
      << BULLET << " " << BRIGHT_GREEN << "Passed: " << pass_count << RESET
      << "\n"
      << BULLET << " " << BRIGHT_RED << "Failed: " << fail_count << RESET
      << "\n\n";
  }

  auto ConsoleReporter::handle_group_start(std::string const &group) -> void
  {
    write_underlined("Group `" + group + "`", BRIGHT_MAGENTA);
  }

  auto ConsoleReporter::handle_group_end(std::string const &group) -> void
  {
    static_cast<void>(group);
    std::cout << "\n";
  }

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

    if (outcome.failed())
    {
      my_failures.emplace(scenario.get_group() + "::" + scenario.get_name(), outcome.get_details());
    }
  }

  auto ConsoleReporter::handle_fixture_end(Fixture const &fixture, Outcome const &outcome) -> void
  {
    if (not my_show_fixtures and outcome.passed())
    {
      return;
    }

    std::cout << BULLET << " " << get_outcome_color(outcome) << get_outcome_keyword(outcome) << RESET << " " << BLUE << fixture.get_name() << RESET << " [" << BLUE << fixture.get_variant_name() << RESET << "]\n";

    if (outcome.failed())
    {
      my_failures.emplace(fixture.get_group() + "::" + fixture.get_name(), outcome.get_details());
    }
  }

  auto ConsoleReporter::set_show_fixtures(bool const show_fixtures) -> void
  {
    my_show_fixtures = show_fixtures;
  }
}