10namespace CppSpec::Formatters {
11struct TAP final :
public BaseFormatter {
13 std::ostringstream buffer;
17 std::string result_to_yaml(
const Result& result);
18 void format(
const Description& description)
override;
19 void format(
const ItBase& it)
override;
23inline std::string TAP::result_to_yaml(
const Result& result) {
24 if (result.is_success()) {
28 auto message = result.get_message();
30 std::ostringstream oss;
31 oss <<
" " <<
"---" << std::endl;
32 if (message.contains(
"\n")) {
33 oss <<
" " <<
"message: |" << std::endl;
34 std::string indented_message = message;
35 std::string::size_type pos = 0;
36 while ((pos = indented_message.find(
'\n', pos)) != std::string::npos) {
37 indented_message.replace(pos, 1,
"\n ");
40 oss <<
" " <<
" " << indented_message << std::endl;
42 oss <<
" " <<
"message: " <<
'"' << result.get_message() <<
'"' << std::endl;
45 oss <<
" " <<
"severity: failure" << std::endl;
46 oss <<
" " <<
"at:" << std::endl;
47 oss <<
" " <<
" " <<
"file: " << result.get_location().file_name() << std::endl;
48 oss <<
" " <<
" " <<
"line: " << result.get_location().line() << std::endl;
52 oss <<
" " <<
"..." << std::endl;
56inline void TAP::flush() {
57 std::string str = buffer.str();
58 std::ostringstream oss;
63 if (test_counter != 1) {
67 oss <<
"1.." << test_counter - 1;
74 oss << ((str[0] ==
'\n') ? str.substr(1) : str);
76 std::cout << oss.str() << std::flush;
79 buffer = std::ostringstream();
82inline void TAP::format(
const Description& description) {
83 if (!first && !description.has_parent()) {
92inline void TAP::format(
const ItBase& it) {
95 std::forward_list<std::string> descriptions;
97 descriptions.push_front(it.get_description());
98 for (
const auto* parent = it.get_parent_as<Description>(); parent->has_parent();
99 parent = parent->get_parent_as<Description>()) {
100 descriptions.push_front(parent->get_description());
103 std::string description = Util::join(descriptions,
" ");
105 buffer << status_color(it.get_result().status());
106 buffer << (it.get_result().is_success() ?
"ok" :
"not ok");
107 buffer << reset_color();
108 buffer <<
" " << get_and_increment_test_counter() <<
" - " << description << std::endl;
109 buffer << result_to_yaml(it.get_result());
Definition description.hpp:20
Base class for it expressions.
Definition it_base.hpp:32