47 for(
int i_(0); i_ < indent_; ++i_)
48 for(
int j_(0); j_ < INDENT_SPACE; ++j_)
55 const std::string& label_,
60 label_width_ =
static_cast<int>(label_.size()) + 2;
64 const int width_(WIDTH - 2 - INDENT_SPACE * indent_);
65 int left_width_((width_ - label_width_) / 2);
66 int right_width_(width_ - label_width_ - left_width_);
69 printIndent(os_, indent_);
73 for(
int i_(0); i_ < left_width_; ++i_)
78 os_ <<
' ' << label_ <<
' ';
82 for(
int i_(0); i_ < right_width_; ++i_)
90 TerminalDriver& driver_,
91 const std::string& label_,
94 int label_width_(
static_cast<int>(label_.size()));
95 int free_space_width_(WIDTH - 4 - INDENT_SPACE * indent_ - label_width_ - 8);
97 printIndent(os_, indent_);
100 for(
int i_(0); i_ < free_space_width_; ++i_)
109 driver_.setForeground(*os_.rdbuf(),
Color::RED);
112 driver_.cleanAttributes(*os_.rdbuf());
113 os_ <<
']' << std::endl;
116 void printTotalResultLine(
118 const std::string& label_,
119 const std::string& ok_,
120 const std::string& failed_,
121 const std::string& total_) {
122 int label_width_(
static_cast<int>(label_.size()));
123 int ok_width_(
static_cast<int>(ok_.size()));
124 int failed_width_(
static_cast<int>(failed_.size()));
132 os_ <<
" " << label_;
133 int i_(2 + label_width_);
134 for(; i_ < OK_COLUMN; ++i_)
137 for(i_ += ok_width_; i_ < FAILED_COLUMN; ++i_)
140 for(i_ += failed_width_; i_ < TOTAL_COLUMN; ++i_)
142 os_ << total_ << std::endl;
145 void printTotalResult(
147 const std::string& label_,
151 std::ostringstream ok_str_;
152 ok_str_ << std::setw(8) << ok_;
153 std::ostringstream failed_str_;
154 failed_str_ << std::setw(8) << failed_;
155 std::ostringstream total_str_;
156 total_str_ << std::setw(8) << total_;
157 printTotalResultLine(
158 os_, label_, ok_str_.str(), failed_str_.str(), total_str_.str());
161 void printTotalErrors(
163 const std::string& label_,
165 std::ostringstream errors_str_;
166 errors_str_ << std::setw(8) << errors_;
167 printTotalResultLine(os_, label_,
" ",
" ", errors_str_.str());
172 struct ReporterConsole::Impl :
public AssertBufferListener {
174 class AssertBuffer :
public AssertBufferStr {
181 const AssertBuffer&) =
delete;
183 const AssertBuffer&) =
delete;
191 Color color_)
override;
193 Color color_)
override;
195 Style style_)
override;
199 static int selectHandle(
205 TerminalDriver term_driver;
206 ReporterStatistics statistics;
215 std::pair<int, char> stacked_hr;
222 const Impl&) =
delete;
224 const Impl&) =
delete;
230 bool hide_location_);
233 void printStackedHR();
234 void resetStackedHR();
236 void printAdditionalMessage(
238 const std::string& message_);
242 const Context& context_,
243 const AssertBufferAssertData& data_,
244 const std::string& message_)
override;
246 const Context& context_,
247 const AssertBufferAssertData& data_,
248 const std::string& message_)
override;
250 const Context& context_,
251 const AssertBufferAssertData& data_)
override;
253 const Context& context_,
254 const std::string& message_)
override;
256 const Context& context_,
257 const std::string& message_)
override;
259 const Context& context_)
override;
262 ReporterConsole::Impl::AssertBuffer::AssertBuffer(
264 AssertBufferStr(pimpl_),
269 ReporterConsole::Impl::AssertBuffer::~AssertBuffer() =
default;
271 void ReporterConsole::Impl::AssertBuffer::setForeground(
273 pimpl->term_driver.setForeground(*
this, color_);
276 void ReporterConsole::Impl::AssertBuffer::setBackground(
278 pimpl->term_driver.setBackground(*
this, color_);
281 void ReporterConsole::Impl::AssertBuffer::setTextStyle(
283 pimpl->term_driver.setTextStyle(*
this, style_);
286 void ReporterConsole::Impl::AssertBuffer::resetAttributes() {
287 pimpl->term_driver.cleanAttributes(*
this);
290 ReporterConsole::Impl::Impl(
291 ReporterConsole* owner_,
294 bool hide_location_) :
297 term_driver(selectHandle(os_)),
300 hide_location(hide_location_),
303 assert_buffer(std::make_shared<AssertBuffer>(this)) {
304 assert(os !=
nullptr);
308 ReporterConsole::Impl::~Impl() =
default;
310 int ReporterConsole::Impl::selectHandle(
312 if(os_ == &std::cout)
314 else if(os_ == &std::cerr)
320 void ReporterConsole::Impl::printStackedHR() {
321 if(stacked_hr.first >= 0)
322 printHR(*os, stacked_hr.second,
"", stacked_hr.first);
323 stacked_hr = {-1,
' '};
326 void ReporterConsole::Impl::resetStackedHR() {
327 stacked_hr = {-1,
' '};
330 void ReporterConsole::Impl::printAdditionalMessage(
332 const std::string& message_) {
333 if(verbose || !condition_) {
334 std::istringstream iss_(message_);
336 while(std::getline(iss_, line_))
337 *os <<
" " << line_ << std::endl;
341 void ReporterConsole::Impl::assertionOpeningMessage(
342 const Context& context_,
343 const AssertBufferAssertData& data_,
344 const std::string& message_) {
348 if(verbose || !data_.condition) {
350 term_driver.setTextStyle(*os->rdbuf(),
Style::BOLD);
355 *os << data_.file <<
':' << data_.line;
359 << context_.object_path->getCurrentPath() <<
": " << message_
363 term_driver.cleanAttributes(*os->rdbuf());
367 void ReporterConsole::Impl::assertionAdditionalMessage(
368 const Context& context_,
369 const AssertBufferAssertData& data_,
370 const std::string& message_) {
371 printAdditionalMessage(data_.condition, message_);
374 void ReporterConsole::Impl::assertionClose(
375 const Context& context_,
376 const AssertBufferAssertData& data_) {
380 void ReporterConsole::Impl::errorOpeningMessage(
381 const Context& context_,
382 const std::string& message_) {
386 *os <<
"error in " << context_.object_path->getCurrentPath() <<
": " << message_
390 void ReporterConsole::Impl::errorAdditionalMessage(
391 const Context& context_,
392 const std::string& message_) {
393 printAdditionalMessage(
false, message_);
396 void ReporterConsole::Impl::errorClose(
397 const Context& context_) {
404 bool hide_location_) :
405 pimpl(new Impl(this, os_, verbose_, hide_location_)) {
415 const std::string& name_,
422 const std::string& name_,
426 pimpl->resetStackedHR();
427 printHR(*pimpl->os,
'=', params_.
mixWithName(name_), pimpl->indent);
435 const std::string& name_,
438 if(pimpl->level == 0)
440 pimpl->printStackedHR();
448 const std::string& name_) {
455 const std::string& file_,
458 pimpl->statistics.reportAssertion(condition_);
461 pimpl->assert_buffer->openAssertion({condition_, file_, lineno_});
462 return pimpl->assert_buffer;
468 pimpl->statistics.reportError();
471 pimpl->assert_buffer->openError();
472 return pimpl->assert_buffer;
477 const std::string& name_,
484 const std::string& name_,
487 pimpl->statistics.reportCase(result_);
490 pimpl->printStackedHR();
491 printResultLine(*pimpl->os, pimpl->term_driver, params_.
mixWithName(name_), result_, pimpl->indent);
495 if(pimpl->level == 0)
501 const std::string& name_,
504 pimpl->statistics.reportSuite(result_);
507 pimpl->resetStackedHR();
508 printHR(*pimpl->os,
'-',
"", pimpl->indent);
509 printResultLine(*pimpl->os, pimpl->term_driver,
"Suite total", result_, pimpl->indent);
512 pimpl->stacked_hr = {pimpl->indent,
'='};
521 const std::string& name_,
524 pimpl->resetStackedHR();
525 printHR(*pimpl->os,
'=',
"Test results", 0);
526 printTotalResultLine(
535 pimpl->statistics.getSuitesOK(),
536 pimpl->statistics.getSuitesFailed(),
537 pimpl->statistics.getSuites());
541 pimpl->statistics.getCasesOK(),
542 pimpl->statistics.getCasesFailed(),
543 pimpl->statistics.getCases());
547 pimpl->statistics.getAssertionsOK(),
548 pimpl->statistics.getAssertionsFailed(),
549 pimpl->statistics.getAssertions());
550 printTotalErrors(*pimpl->os,
"Errors", pimpl->statistics.getErrors());
551 printResultLine(*pimpl->os, pimpl->term_driver,
"Test total", result_, 0);
552 printHR(*pimpl->os,
'=',
"", 0);