micro-test-plus 4.1.1
µTest++ Testing Framework
Loading...
Searching...
No Matches
micro_os_plus::micro_test_plus::static_suite Class Reference

A test suite designed for static (namespace-scope) registration with a static_runner. More...

#include <micro-os-plus/micro-test-plus.h>

Inheritance diagram for micro_os_plus::micro_test_plus::static_suite:

Public Member Functions

template<typename Callable_T, typename... Args_T>
 static_suite (const char *name, static_runner &runner, Callable_T &&callable, Args_T &&... arguments)
 Class template constructor for static_suite.
 static_suite (const static_suite &)=delete
 Deleted copy constructor to prevent copying.
 static_suite (static_suite &&)=delete
 Deleted move constructor to prevent moving.
virtual ~static_suite () override
 Virtual destructor.
void abort (const reflection::source_location &sl=reflection::source_location::current())
 Aborts test execution via the owning runner.
size_t children_subtests_count (void) const noexcept
 Returns the number of direct child subtests owned by this node.
size_t current_subtest_index () const noexcept
 Returns the index of the most recently created child subtest.
size_t increment_subtest_index () noexcept
 Increments and returns the child subtest sequential index.
const char * name (void) const noexcept
 Gets the node name.
static_suiteoperator= (const static_suite &)=delete
 Deleted copy assignment operator to prevent copying.
static_suiteoperator= (static_suite &&)=delete
 Deleted move assignment operator to prevent moving.
size_t own_index () const noexcept
 Returns the positional index of this object within its parent.
void own_index (size_t index) noexcept
 Sets the positional index of this object within its parent.
class reporterreporter (void) const noexcept
 Gets the test reporter associated with this test runnable.
virtual void run (void) override
 Executes the static suite body using the stored static callable.
class runnerrunner (void) const noexcept
 Gets the test runner associated with this test runnable.
template<typename Callable_T, typename... Args_T>
void test (const char *name, Callable_T &&callable, Args_T &&... arguments)
 Adds a test case to the suite.
const detail::timestampstimings () const noexcept
 Gets the timings for this suite (const overload).
detail::timestampstimings () noexcept
 Gets the timings for this suite.
const runner_totals & totals () const noexcept
 Gets the totals for the test (const overload).
runner_totals & totals () noexcept
 Gets the totals for the test.

Protected Member Functions

void after_subtest_create_ (std::unique_ptr< subtest > child_test, suite &suite)
 Registers a newly constructed child subtest and executes it immediately.

Protected Attributes

std::function< void(suite &)> callable_
 Callable storing the test body and any bound arguments. Invoked with a reference to the derived Self_T instance.
std::vector< std::unique_ptr< subtest > > children_subtests_
 Owning collection of direct child subtests.
size_t current_subtest_index_
 The subtest index, counting from 1.
const char * name_
 The test node name.
size_t own_index_
 The test index, counting from 1.
class runnerrunner_
 Reference to the test runner that owns this object.
std::function< void(static_suite &)> static_callable_
 Callable storing the static suite body and any bound arguments. Invoked with a reference to the concrete static_suite instance.
detail::timestamps timings_
 Timing measurements for this suite's execution.
runner_totals totals_
 Totals for the test node, including nested cases.

Detailed Description

static_suite extends suite to support the pattern where test suites are declared as namespace-scope objects and therefore constructed before or after the static_runner instance, in unspecified static-initialisation order.

Upon construction, the suite automatically registers itself with the supplied static_runner by calling static_runner::register_suite_(). The runner defers execution of all registered static suites until static_runner::run_suites_() is invoked, which typically happens inside the implicit main() provided by the framework.

In addition to the standard callable body inherited from suite, a static_suite may carry a second, statically-registered callable stored in static_callable_. The overridden run() method invokes both bodies in sequence, allowing the suite to integrate both dynamic and static registration patterns.

The class is non-copyable and non-movable to preserve unique ownership and consistent state throughout the test session.

Definition at line 923 of file test.h.

Constructor & Destructor Documentation

◆ static_suite() [1/3]

template<typename Callable_T, typename... Args_T>
micro_os_plus::micro_test_plus::static_suite::static_suite ( const char * name,
static_runner & runner,
Callable_T && callable,
Args_T &&... arguments )
Template Parameters
Callable_TThe type of a callable object.
Args_TThe types of the callable arguments.
Parameters
[in]nameThe test suite name or description, used in reports.
[in]runnerThe static test runner managing this suite.
[in]callableA generic callable object, usually a lambda or function, invoked to perform the test suite.
[in]argumentsA possibly empty list of arguments to be passed to the callable.

Delegates to runnable, which binds the callable with its arguments. After construction, the suite is registered with the static test runner.

Definition at line 507 of file test-inlines.h.

511 : suite{ name, detail::to_runner (runner), nullptr }
512 {
513 if constexpr (sizeof...(arguments) == 0)
514 {
515 static_callable_ = std::forward<Callable_T> (callable);
516 }
517 else
518 {
519 static_callable_ = std::bind (std::forward<Callable_T> (callable),
520 std::placeholders::_1,
521 std::forward<Args_T> (arguments)...);
522 }
523
524#if defined(MICRO_OS_PLUS_TRACE) \
525 && defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
526#if defined(__GNUC__)
527#pragma GCC diagnostic push
528#if defined(__clang__)
529#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
530#endif
531#endif
532 trace::printf ("%s '%s' %zu\n", __PRETTY_FUNCTION__, name, own_index_);
533#if defined(__GNUC__)
534#pragma GCC diagnostic pop
535#endif
536#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
537
539 }
class runner & runner(void) const noexcept
Gets the test runner associated with this test runnable.
size_t own_index_
The test index, counting from 1.
Definition test.h:406
const char * name(void) const noexcept
Gets the node name.
std::function< void(static_suite &)> static_callable_
Callable storing the static suite body and any bound arguments. Invoked with a reference to the concr...
Definition test.h:990
suite(const char *name, class runner &runner, Callable_T &&callable, Args_T &&... arguments)
Constructs a suite with a name, runner, and callable body.
runner & to_runner(static_runner &static_runner_ref) noexcept
Converts a static_runner reference to a runner reference.
Definition runner.cpp:90
void register_static_suite(static_runner &static_runner_ref, static_suite &static_suite_ref)
Registers a static suite with a static runner.
Definition runner.cpp:102

References micro_os_plus::micro_test_plus::suite::suite(), micro_os_plus::micro_test_plus::detail::runnable< suite >::name(), micro_os_plus::micro_test_plus::detail::runnable< suite >::own_index_, micro_os_plus::micro_test_plus::detail::register_static_suite(), and static_callable_.

Referenced by static_suite(), static_suite(), operator=(), and operator=().

◆ static_suite() [2/3]

micro_os_plus::micro_test_plus::static_suite::static_suite ( const static_suite & )
delete

References static_suite().

◆ static_suite() [3/3]

micro_os_plus::micro_test_plus::static_suite::static_suite ( static_suite && )
delete

References static_suite().

◆ ~static_suite()

micro_os_plus::micro_test_plus::static_suite::~static_suite ( )
overridevirtual

No resources are owned directly by static_suite; the destructor performs no explicit clean-up. If tracing is enabled, the suite name is output for diagnostic purposes.

Definition at line 457 of file test.cpp.

458 {
459#if defined(MICRO_OS_PLUS_TRACE) \
460 && defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
461#if defined(__GNUC__)
462#pragma GCC diagnostic push
463#if defined(__clang__)
464#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
465#endif
466#endif
467 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
468#if defined(__GNUC__)
469#pragma GCC diagnostic pop
470#endif
471#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
472 }
const char * name_
The test node name.
Definition test.h:224

References micro_os_plus::micro_test_plus::detail::runnable< suite >::name_.

Member Function Documentation

◆ abort()

Parameters
slThe source location from which the abort is triggered.
Returns
Does not return.

Delegates immediately to runner_.abort(), passing the supplied source location so that the error message identifies the call site before the process is terminated via ::abort().

Definition at line 369 of file test.cpp.

209 {
210 runner_.abort (sl);
211 }
void abort(const reflection::source_location &sl=reflection::source_location::current())
Aborts test execution via the owning runner.
Definition test.cpp:208
CRTP base class factoring out callable storage, rule-of-five, and run() logic shared by subtest and s...
Definition test.h:444

◆ after_subtest_create_()

void micro_os_plus::micro_test_plus::detail::runnable_base::after_subtest_create_ ( std::unique_ptr< subtest > child_test,
suite & suite )
protectedinherited
Parameters
child_testOwning pointer to the newly created subtest.
suiteThe parent suite to which execution results are reported.
Returns
Nothing.

Transfers ownership of child_test into children_subtests_ and immediately invokes subtest::run() on the newly stored subtest. The parent's executed-subtest counter is then incremented. The child's check counters are intentionally not merged into the parent totals; each subtest reports only its own counters. The child's totals are, however, accumulated into suite so that the suite summary reflects all checks performed by its subtests.

Definition at line 394 of file test.cpp.

226 {
227 // Transfer ownership into the vector.
229
230 // Run the child test case immediately.
231 class subtest& subtest = *children_subtests_.back ();
232 subtest.run ();
233
234 // This test executed one more subtest.
235#if defined(MICRO_OS_PLUS_TRACE) \
236 && defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
237#if defined(__GNUC__)
238#pragma GCC diagnostic push
239#if defined(__clang__)
240#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
241#endif
242#endif
243 trace::printf ("%s subtest '%s' executed one more subtest\n",
245#if defined(__GNUC__)
246#pragma GCC diagnostic pop
247#endif
248#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
250 // Do not accumulate the totals from the child test into the current test
251 // totals, each subtest shows only its counters.
252
253#if defined(MICRO_OS_PLUS_TRACE) \
254 && defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
255#if defined(__GNUC__)
256#pragma GCC diagnostic push
257#if defined(__clang__)
258#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
259#endif
260#endif
261 trace::printf ("%s suite '%s' totals\n", __PRETTY_FUNCTION__,
262 suite.name ());
263#if defined(__GNUC__)
264#pragma GCC diagnostic pop
265#endif
266#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
267
268 // Accumulate the totals from the child test into the suite totals.
269 suite.totals () += subtest.totals ();
270 }
virtual void run(void)=0
Runs the test function by invoking the stored callable with the derived self instance.
void increment_executed_subtests(size_t count=1) noexcept
Increments the executed-subtests counter.

◆ children_subtests_count()

size_t micro_os_plus::micro_test_plus::detail::runnable_base::children_subtests_count ( void ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
The number of child subtests.

Returns the number of child subtests owned by this node.

Definition at line 349 of file test-inlines.h.

167 {
168 return children_subtests_.size ();
169 }

◆ current_subtest_index()

size_t micro_os_plus::micro_test_plus::detail::runnable_base::current_subtest_index ( ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
The current child subtest sequential index.

Returns the sequential index of the most recently created child subtest.

Definition at line 329 of file test-inlines.h.

145 {
147 }

◆ increment_subtest_index()

Parameters
None.
Returns
The new index value after incrementing.

Each call to test() invokes this method before constructing the new subtest, so the index values form a strictly increasing, one-based sequence.

Definition at line 339 of file test-inlines.h.

157 {
158 return ++current_subtest_index_;
159 }

Referenced by micro_os_plus::micro_test_plus::suite::test().

◆ name()

const char * micro_os_plus::micro_test_plus::detail::test_node::name ( void ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
A pointer to the null-terminated test node name.

Returns a pointer to the null-terminated name stored in name_.

Definition at line 194 of file test-inlines.h.

93 {
94 return name_;
95 }

Referenced by micro_os_plus::micro_test_plus::static_suite::static_suite(), micro_os_plus::micro_test_plus::suite::suite(), micro_os_plus::micro_test_plus::suite::operator=(), and micro_os_plus::micro_test_plus::suite::test().

◆ operator=() [1/2]

static_suite & micro_os_plus::micro_test_plus::static_suite::operator= ( const static_suite & )
delete

References static_suite().

◆ operator=() [2/2]

static_suite & micro_os_plus::micro_test_plus::static_suite::operator= ( static_suite && )
delete

References static_suite(), and run().

◆ own_index() [1/2]

size_t micro_os_plus::micro_test_plus::detail::runnable_base::own_index ( ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
The one-based own index.

Returns the one-based positional index of this object within its parent.

Definition at line 305 of file test-inlines.h.

125 {
126 return own_index_;
127 }

Referenced by micro_os_plus::micro_test_plus::top_suite::top_suite(), micro_os_plus::micro_test_plus::top_suite::name(), and micro_os_plus::micro_test_plus::suite::test().

◆ own_index() [2/2]

void micro_os_plus::micro_test_plus::detail::runnable_base::own_index ( size_t index)
inlinenoexceptinherited
Note
This overload follows the same-name getter/setter pattern used throughout the framework: the getter is the const overload and the setter is the non-const single-argument overload.
Parameters
indexThe new index value.
Returns
Nothing.

Sets the positional index of this object within its parent.

Definition at line 319 of file test-inlines.h.

135 {
137 }

◆ reporter()

Parameters
None.
Returns
A reference to the test reporter.

Delegates immediately to runner_.reporter(), returning the reporter associated with the owning runner instance.

Definition at line 359 of file test.cpp.

197 {
198 return runner_.reporter ();
199 }
class reporter & reporter(void) const noexcept
Gets the test reporter associated with this test runnable.
Definition test.cpp:196

Referenced by micro_os_plus::micro_test_plus::detail::runnable< subtest >::abort(), and micro_os_plus::micro_test_plus::static_suite::run().

◆ run()

void micro_os_plus::micro_test_plus::static_suite::run ( void )
overridevirtual
Parameters
None.
Returns
Nothing.

Records the suite begin timestamp, notifies the reporter via begin_suite(), invokes the stored static callable with *this, records the suite end timestamp, and notifies the reporter via end_suite().

Reimplemented from micro_os_plus::micro_test_plus::suite.

Definition at line 482 of file test.cpp.

483 {
484#if defined(MICRO_OS_PLUS_TRACE) \
485 && defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED)
486#if defined(__GNUC__)
487#pragma GCC diagnostic push
488#if defined(__clang__)
489#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
490#endif
491#endif
492 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
493#if defined(__GNUC__)
494#pragma GCC diagnostic pop
495#endif
496#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED
497
498 class reporter& reporter = this->reporter ();
499
500 this->timings ().timestamp_begin ();
501 reporter.begin_suite (*this);
502
503 static_callable_ (*this);
504
505 this->timings ().timestamp_end ();
506 reporter.end_suite (*this);
507 }
void timestamp_begin(void) noexcept
Records the begin timestamp using the current system clock.
Definition timings.cpp:110
void timestamp_end(void) noexcept
Records the end timestamp using the current system clock.
Definition timings.cpp:143
detail::timestamps & timings() noexcept
Gets the timings for this suite.

References micro_os_plus::micro_test_plus::reporter::begin_suite(), micro_os_plus::micro_test_plus::reporter::end_suite(), micro_os_plus::micro_test_plus::detail::runnable< suite >::name_, micro_os_plus::micro_test_plus::detail::runnable< suite >::reporter(), static_callable_, micro_os_plus::micro_test_plus::detail::timestamps::timestamp_begin(), micro_os_plus::micro_test_plus::detail::timestamps::timestamp_end(), and micro_os_plus::micro_test_plus::suite::timings().

Referenced by operator=().

◆ runner()

class runner & micro_os_plus::micro_test_plus::detail::runnable_base::runner ( void ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
A reference to the test runner.

Returns a reference to the owning test runner.

Definition at line 380 of file test-inlines.h.

177 {
178 return runner_;
179 }

Referenced by micro_os_plus::micro_test_plus::suite::suite(), and micro_os_plus::micro_test_plus::suite::test().

◆ test()

template<typename Callable_T, typename... Args_T>
void micro_os_plus::micro_test_plus::suite::test ( const char * name,
Callable_T && callable,
Args_T &&... arguments )
inherited
Template Parameters
Callable_TThe type of a callable object.
Args_TThe types of the callable arguments.
Parameters
[in]nameThe test case name or description, used in reports.
[in]callableA generic callable object, usually a lambda, invoked to perform the test.
[in]argumentsA possibly empty list of arguments to be passed to the callable.

The test function template registers and executes a test case within the µTest++ framework. It accepts a descriptive name, a callable object (such as a lambda or function pointer), and an optional list of arguments to be passed to the callable. The test case is reported using the provided name, and its execution is managed by the framework's test runner.

Each test case typically involves evaluating a logical expression, such as comparing a computed result to an expected value. For C++ projects, it is also possible to verify whether evaluating an expression throws exceptions. Each test either succeeds or fails, and for expectations, the test runner maintains counts of successful and failed checks.

This function template enables flexible and expressive test case definitions, supporting both parameterised and non-parameterised tests. It is typically invoked at global scope or within test suite definitions to ensure automatic registration and execution.

A test case is characterised by a name, a function that performs the checks, and optionally, arguments to be passed to that function. The implementation of test invokes the provided function with the given arguments and reports the results to the test runner.

Example
ts.test ("Check answer with comparator", [] (auto& t) {
t.expect (mt::eq (compute_answer (), 42)) << "answer is 42";
});
Primary namespace for the µTest++ testing framework.

Definition at line 438 of file test-inlines.h.

439 {
440#if defined(MICRO_OS_PLUS_TRACE) \
441 && defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED)
442#if defined(__GNUC__)
443#pragma GCC diagnostic push
444#if defined(__clang__)
445#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
446#endif
447#endif
448 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name);
449#if defined(__GNUC__)
450#pragma GCC diagnostic pop
451#endif
452#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED
453
455 auto child_subtest
456 = std::make_unique<subtest> (name, runner (), *this, own_index, 1,
457 std::forward<Callable_T> (callable),
458 std::forward<Args_T> (arguments)...);
459
460 after_subtest_create_ (std::move (child_subtest), *this);
461 }
void after_subtest_create_(std::unique_ptr< subtest > child_test, suite &suite)
Definition test.cpp:224
size_t own_index() const noexcept
Returns the positional index of this object within its parent.

References micro_os_plus::micro_test_plus::detail::runnable< suite >::increment_subtest_index(), micro_os_plus::micro_test_plus::detail::runnable< suite >::name(), micro_os_plus::micro_test_plus::detail::runnable< suite >::own_index(), and micro_os_plus::micro_test_plus::detail::runnable< suite >::runner().

Referenced by operator=().

◆ timings() [1/2]

const detail::timestamps & micro_os_plus::micro_test_plus::suite::timings ( ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
A const reference to the timestamps instance.

Returns a const reference to the timestamps member.

Definition at line 478 of file test-inlines.h.

479 {
480 return timings_;
481 }
detail::timestamps timings_
Timing measurements for this suite's execution.
Definition test.h:818

References timings_.

◆ timings() [2/2]

detail::timestamps & micro_os_plus::micro_test_plus::suite::timings ( )
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
A reference to the timestamps instance.

Returns a reference to the timestamps member.

Definition at line 468 of file test-inlines.h.

469 {
470 return timings_;
471 }

References timings_.

Referenced by micro_os_plus::micro_test_plus::detail::runnable< subtest >::abort(), micro_os_plus::micro_test_plus::reporter_human::end_suite(), micro_os_plus::micro_test_plus::reporter_tap::end_suite(), operator=(), and micro_os_plus::micro_test_plus::static_suite::run().

◆ totals() [1/2]

const runner_totals & micro_os_plus::micro_test_plus::detail::test_node::totals ( ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
A const reference to the runner_totals instance.

Returns a const reference to the runner_totals member.

Definition at line 215 of file test-inlines.h.

113 {
114 return totals_;
115 }

◆ totals() [2/2]

runner_totals & micro_os_plus::micro_test_plus::detail::test_node::totals ( )
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
A reference to the runner_totals instance.

Returns a reference to the runner_totals member.

Definition at line 205 of file test-inlines.h.

103 {
104 return totals_;
105 }

Member Data Documentation

◆ callable_

◆ children_subtests_

Each call to test() appends a new subtest to this vector and runs it immediately. The vector retains ownership for the lifetime of the parent runnable.

Definition at line 427 of file test.h.

◆ current_subtest_index_

This index is used for reporting and tracking the execution order of subtests within a suite, especially when nested subtests are involved. It is incremented for each subtest created, allowing for clear identification of subtests in reports and diagnostics.

Definition at line 417 of file test.h.

◆ name_

◆ own_index_

◆ runner_

Definition at line 401 of file test.h.

◆ static_callable_

std::function<void (static_suite&)> micro_os_plus::micro_test_plus::static_suite::static_callable_
protected

Definition at line 990 of file test.h.

Referenced by static_suite(), and run().

◆ timings_

detail::timestamps micro_os_plus::micro_test_plus::suite::timings_
protectedinherited

Definition at line 818 of file test.h.

Referenced by timings(), and timings().

◆ totals_

Definition at line 229 of file test.h.


The documentation for this class was generated from the following files: