Skip to main content

test-reporter-basic.cpp File

C++ source file with implementations for the µTest++ basic test reporter methods. More...

Included Headers

Namespaces Index

namespacemicro_os_plus

The primary namespace for the µOS++ framework. More...

namespacemicro_test_plus

Primary namespace for the µTest++ testing framework. More...

Description

C++ source file with implementations for the µTest++ basic test reporter methods.

This source file contains the implementations for test_reporter_basic, the default concrete implementation of the test_reporter abstract interface. It formats and presents test results using printf-based standard output, accumulating output in an internal string buffer and supporting colour-coded diagnostics and multiple verbosity levels.

All definitions reside within the micro_os_plus::micro_test_plus namespace, ensuring clear separation from user code and minimising the risk of naming conflicts.

This file must be included when building the µTest++ library.

File Listing

The file content with the documentation metadata removed is:

1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can be
9 * obtained from https://opensource.org/licenses/mit.
10 *
11 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
12 * released under the terms of the Boost Version 1 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
36
37// ----------------------------------------------------------------------------
38
39#if defined(MICRO_OS_PLUS_INCLUDE_CONFIG_H)
40#include <micro-os-plus/config.h>
41#endif // MICRO_OS_PLUS_INCLUDE_CONFIG_H
42
44
45// ----------------------------------------------------------------------------
46
47#pragma GCC diagnostic ignored "-Waggregate-return"
48#if defined(__clang__)
49#pragma clang diagnostic ignored "-Wunknown-warning-option"
50#pragma clang diagnostic ignored "-Wc++98-compat"
51#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
52#endif
53
55{
56 // --------------------------------------------------------------------------
57
59 {
61 }
62
75 void
77 {
78 *this << colors_.pass;
80 {
81 *this << " ";
82 }
83 *this << " ✓ ";
84 *this << colors_.none;
85 if (!message.empty ())
86 {
87 *this << message.c_str ();
88 }
89 }
90
106 void
108 {
109 *this << endl;
110
111 flush ();
112 }
113
125 void
127 std::string& message, const bool hasExpression,
128 const reflection::source_location& location)
129 {
130 *this << colors_.fail;
132 {
133 *this << " ";
134 }
135 *this << " ✗ ";
136 *this << colors_.none;
137 if (!message.empty ())
138 {
139 *this << message.c_str ();
140 *this << " ";
141 }
142 *this << colors_.fail << "FAILED" << colors_.none;
143#pragma GCC diagnostic push
144#if defined(__clang__)
145#pragma clang diagnostic ignored "-Wsign-conversion"
146#elif defined(__GNUC__)
147#pragma GCC diagnostic ignored "-Wnarrowing"
148#pragma GCC diagnostic ignored "-Wsign-conversion"
149#endif
150 *this << " (" << reflection::short_name (location.file_name ()) << ":"
152 location.line ()
153 };
154#pragma GCC diagnostic pop
155 if (hasExpression)
156 {
157 *this << ", ";
158 }
159 }
160
170 void
172 [[maybe_unused]] const reflection::source_location& location, bool abort)
173 {
174 *this << ")";
175 if (abort)
176 {
177 *this << " aborted...";
178 }
179 *this << endl;
180
181 flush ();
182 }
183
192 void
194 {
195 out_.append ("\n");
196 flush ();
197 }
198
206 void
208 {
209 fflush (stdout); // Sync STDOUT.
210 }
211
223 void
224 test_reporter_basic::begin_test_case ([[maybe_unused]] const char* name)
225 {
226 is_in_test_case_ = true;
227
228 if (!out_.empty () && (verbosity == verbosity::verbose))
229 {
231 {
232 printf ("\n");
233 }
234 output ();
235 add_empty_line = true;
236 }
237
238 out_.clear ();
239
240 flush ();
241 }
242
255 void
256 test_reporter_basic::end_test_case ([[maybe_unused]] const char* name)
257 {
259 {
260 if (current_test_suite->current_test_case.failed_checks > 0)
261 {
262 if (true /* add_empty_line */)
263 {
264 printf ("\n");
265 }
266#pragma GCC diagnostic push
267#if defined(__clang__)
268#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
269#endif
270 printf (" • %s - test case started\n", name);
271 output ();
272 printf (
273 " %s✗%s %s - test case %sFAILED%s (%zu check%s passed, %zu "
274 "failed)\n",
275 colors_.fail, colors_.none, name, colors_.fail, colors_.none,
276 current_test_suite->current_test_case.successful_checks,
277 current_test_suite->current_test_case.successful_checks == 1
278 ? ""
279 : "s",
280 current_test_suite->current_test_case.failed_checks);
281#pragma GCC diagnostic pop
282 add_empty_line = true;
283 }
284 else
285 {
287 {
288 printf ("\n");
289 }
291 {
292#pragma GCC diagnostic push
293#if defined(__clang__)
294#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
295#endif
296 printf (" • %s - test case started\n", name);
297 output ();
298 printf (
299 " %s✓%s %s - test case passed (%zu check%s)\n",
300 colors_.pass, colors_.none, name,
301 current_test_suite->current_test_case.successful_checks,
302 current_test_suite->current_test_case.successful_checks
303 == 1
304 ? ""
305 : "s");
306#pragma GCC diagnostic pop
307 add_empty_line = true;
308 }
309 else
310 {
311#pragma GCC diagnostic push
312#if defined(__clang__)
313#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
314#endif
315 printf (
316 " %s✓%s %s - test case passed (%zu check%s)\n",
317 colors_.pass, colors_.none, name,
318 current_test_suite->current_test_case.successful_checks,
319 current_test_suite->current_test_case.successful_checks
320 == 1
321 ? ""
322 : "s");
323#pragma GCC diagnostic pop
324
325 add_empty_line = false;
326 }
327 }
328 }
329
330 out_.clear ();
331 flush ();
332
333 is_in_test_case_ = false;
334 }
335
346 void
348 {
350 {
351 flush ();
352 printf ("\n");
353 }
354
356 {
357 add_empty_line = false;
358 return;
359 }
360
361#pragma GCC diagnostic push
362#if defined(__clang__)
363#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
364#endif
365 printf ("• %s - test suite started\n", name);
366#pragma GCC diagnostic pop
367
368 add_empty_line = true;
369 }
370
383 void
385 {
387 {
388 return;
389 }
390
391 if (suite.test_cases_count () > 0 && verbosity != verbosity::quiet)
392 {
393 printf ("\n");
394 add_empty_line = true;
395 }
396
397 // Also fail if none passed.
398 if (suite.failed_checks () == 0 && suite.successful_checks () != 0)
399 {
400#pragma GCC diagnostic push
401#if defined(__clang__)
402#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
403#endif
404 printf (
405 "%s✓%s %s - test suite passed (%zu check%s in %zu test case%s)\n",
406 colors_.pass, colors_.none, suite.name (),
407 suite.successful_checks (),
408 suite.successful_checks () == 1 ? "" : "s",
409 suite.test_cases_count (),
410 suite.test_cases_count () == 1 ? "" : "s");
411#pragma GCC diagnostic pop
412 }
413 else
414 {
415#pragma GCC diagnostic push
416#if defined(__clang__)
417#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
418#endif
419 printf ("%s✗%s %s - test suite %sFAILED%s (%zu check%s passed, %zu "
420 "failed, "
421 "in %zu test case%s)\n",
422 colors_.fail, colors_.none, suite.name (), colors_.fail,
423 colors_.none, suite.successful_checks (),
424 suite.successful_checks () == 1 ? "" : "s",
425 suite.failed_checks (), suite.test_cases_count (),
426 suite.test_cases_count () == 1 ? "" : "s");
427#pragma GCC diagnostic pop
428 }
429 flush ();
430 }
431
432 void
433 test_reporter_basic::begin_test ([[maybe_unused]] size_t test_suites_count)
434 {
435 // Nothing to do.
436 }
437
438#pragma GCC diagnostic push
439#pragma GCC diagnostic ignored "-Wshadow"
440 void
442 {
443 // Nothing to do.
444 }
445#pragma GCC diagnostic pop
446
456 void
458 {
459 printf ("%s", out_.c_str ()); // No `\n` here.
460 out_.clear ();
461 }
462
463 // --------------------------------------------------------------------------
464} // namespace micro_os_plus::micro_test_plus
465
466// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.14.0.