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