C++Spec 1.1.0
BDD testing for C++
Loading...
Searching...
No Matches
formatters_base.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <cstdio>
5#include <format>
6#include <iostream>
7#include <string>
8
9#include "description.hpp"
10#include "it_base.hpp"
11#include "runnable.hpp"
12#include "term_colors.hpp"
13
14extern "C" {
15#ifdef _WIN32
16#include <io.h>
17#else
18#include <unistd.h>
19#endif
20}
21
22namespace CppSpec {
23class Description;
24class ItBase;
25
26inline bool is_terminal() {
27#ifdef _WIN32
28 return _isatty(_fileno(stdout)) != 0;
29#else
30 return isatty(fileno(stdout)) != 0;
31#endif
32}
33namespace Formatters {
34
35class BaseFormatter {
36 protected:
37 std::ostream& out_stream;
38 int test_counter = 1;
39 bool color_output;
40
41 public:
42 explicit BaseFormatter(std::ostream& out_stream = std::cout, bool color = is_terminal())
43 : out_stream(out_stream), color_output(color) {}
44 BaseFormatter(const BaseFormatter&) = default;
45 BaseFormatter(const BaseFormatter& copy, std::ostream& out_stream)
46 : out_stream(out_stream), test_counter(copy.test_counter), color_output(copy.color_output) {}
47
48 virtual ~BaseFormatter() = default;
49
50 void format(const Runnable& runnable) {
51 if (const auto* description = dynamic_cast<const Description*>(&runnable)) {
52 format(*description);
53 } else if (const auto* it = dynamic_cast<const ItBase*>(&runnable)) {
54 format(*it);
55 }
56 format_children(runnable);
57 }
58
59 void format_children(const Runnable& runnable) {
60 for (const auto& child : runnable.get_children()) {
61 if (const auto* runnable = dynamic_cast<const Runnable*>(child.get())) {
62 this->format(*runnable);
63 }
64 }
65 }
66
67 virtual void format(const Description& /* description */) {}
68 virtual void format(const ItBase& /* it */) {}
69 virtual void cleanup() {}
70
71 BaseFormatter& set_color_output(bool value);
72
73 int get_and_increment_test_counter() { return test_counter++; }
74 void reset_test_counter() { test_counter = 1; }
75
76 const char* set_color(const char* color) {
77 if (!color_output) {
78 return ""; // No color output
79 }
80 return color;
81 }
82
83 const char* status_color(Result::Status status) {
84 if (!color_output) {
85 return ""; // No color output
86 }
87 switch (status) {
88 case Result::Status::Success:
89 return GREEN;
90 case Result::Status::Failure:
91 return RED;
92 case Result::Status::Error:
93 return MAGENTA;
94 case Result::Status::Skipped:
95 return YELLOW;
96 }
97 return ""; // Default to no color
98 }
99
100 const char* reset_color() { return color_output ? RESET : ""; }
101
112 [[nodiscard]] static std::string located_message(const Result& result) {
113 return std::format("{}:{}: {}", result.get_location().file_name(), result.get_location().line(),
114 result.get_message());
115 }
116};
117
118inline BaseFormatter& BaseFormatter::set_color_output(bool value) {
119 this->color_output = value;
120 return *this;
121}
122
123} // namespace Formatters
124} // namespace CppSpec
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
Base class for it expressions.
Definition it_base.hpp:32
Definition result.hpp:13
Base class for all objects in the execution tree.
Definition runnable.hpp:25
Defines the Description class and associated functions.