C++Spec 1.1.0
BDD testing for C++
Loading...
Searching...
No Matches
progress.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <forward_list>
5#include <list>
6#include <ranges>
7#include <string>
8
9#include "term_colors.hpp"
10#include "verbose.hpp"
11
12namespace CppSpec::Formatters {
13
14// The TAP format makes things a little tricky
15class Progress : public BaseFormatter {
16 std::list<std::string> baked_failure_messages;
17
18 std::string prep_failure_helper(const ItBase& it);
19
20 public:
21 using BaseFormatter::BaseFormatter;
22
23 ~Progress() override {
24 format_failure_messages(); // Print any failures that we have
25 }
26 void format(const ItBase& it) override;
27
28 void format_failure_messages();
29 void prep_failure(const ItBase& it);
30
31 static char status_char(Result::Status status) {
32 switch (status) {
33 case Result::Status::Success:
34 return '.';
35 case Result::Status::Failure:
36 return 'F';
37 case Result::Status::Error:
38 return 'E';
39 case Result::Status::Skipped:
40 return 'S';
41 }
42 return '.'; // Default to success if status is unknown
43 }
44};
45
47inline std::string Progress::prep_failure_helper(const ItBase& it) {
48 // a singly-linked list to act as a LIFO queue
49 std::forward_list<std::string> list;
50
51 // a temporary stream for our helper formatter to write to
52 std::ostringstream temp_stream;
53
54 // A lambda to clean up some code reuse
55 auto push_and_clear = [&]() {
56 list.push_front(temp_stream.str()); // push the stream's contents onto the
57 temp_stream.clear(); // clear the stream's flags
58 temp_stream.str(""); // reset it to empty
59 };
60
61 // Format the 'it' example first
62 temp_stream << it.padding() << it.get_description() << std::endl;
63 push_and_clear();
64
65 // Verbose already has the majority of what we need
66 // defined, so we'll use it.
67 Verbose helper_formatter = Verbose(*this, temp_stream);
68
69 // Ascend the tree to the root, formatting the nodes and
70 // enqueing each formatted string as we go.
71 const auto* parent = it.get_parent_as<Description>();
72
73 do {
74 helper_formatter.format(*parent); // Format the node
75 push_and_clear();
76 } while ((parent = dynamic_cast<const Description*>(parent->get_parent())) != nullptr);
77
78 return Util::join(list); // squash the list of strings and return it.
79}
80
81inline void Progress::prep_failure(const ItBase& it) {
82 // Only the expectations that failed or raised an error are listed, each led by its file and line; a passing or
83 // skipped one adds no line.
84 std::list<std::string> raw_failure_messages;
85 std::ranges::transform(
86 it.get_results() | std::views::filter([](const Result& r) { return r.is_failure() || r.is_error(); }),
87 std::back_inserter(raw_failure_messages), &BaseFormatter::located_message);
88
89 std::ostringstream string_builder; // oss is used as the local string builder
90 string_builder << set_color(RED); // if we're doing color, make it red
91 string_builder << "Test number " << test_counter << " failed:"; // Tell us what test # failed
92 string_builder << reset_color(); // reset the color
93 string_builder << prep_failure_helper(it);
94 string_builder << set_color(RED);
95 string_builder << Util::join_endl(raw_failure_messages);
96 string_builder << reset_color(); // reset the color
97 string_builder << std::endl;
98
99 raw_failure_messages.clear();
100 baked_failure_messages.push_back(string_builder.str());
101}
102
103inline void Progress::format(const ItBase& it) {
104 out_stream << status_color(it.get_result().status());
105 out_stream << status_char(it.get_result().status());
106 out_stream << reset_color();
107 out_stream << std::flush;
108
109 if (it.get_result().status() == Result::Status::Failure) {
110 prep_failure(it);
111 }
112 get_and_increment_test_counter();
113}
114
115inline void Progress::format_failure_messages() {
116 if (baked_failure_messages.empty()) {
117 out_stream << std::endl; // If we don't have any failures, just print a blank line.
118 return;
119 }
120
121 // If we have any failures to format
122 for (const std::string& message : baked_failure_messages) {
123 out_stream << std::endl;
124 out_stream << message; // separated by a blank line
125 }
126 baked_failure_messages.clear(); // Finally, clear the failures list.
127}
128
129static Progress progress;
130
131} // namespace CppSpec::Formatters
Definition description.hpp:22
static std::string located_message(const Result &result)
Prefix a result's message with the file and line of the expectation that reported it.
Definition formatters_base.hpp:112
Definition progress.hpp:15
Base class for it expressions.
Definition it_base.hpp:32