micro-test-plus 5.0.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
micro_os_plus::micro_test_plus::detail::runnable< Self_T > Class Template Referenceabstract

CRTP base class factoring out callable storage, rule-of-five, and run() logic shared by subtest and suite. More...

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

Inheritance diagram for micro_os_plus::micro_test_plus::detail::runnable< Self_T >:

Public Member Functions

template<typename Callable_T, typename... Args_T>
 runnable (const char *name, class runner &runner, size_t own_index, Callable_T &&callable, Args_T &&... arguments)
 Class template constructor.
 runnable (const runnable &)=delete
 Deleted copy constructor to prevent copying.
 runnable (runnable &&)=delete
 Deleted move constructor to prevent moving.
virtual ~runnable () 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.
runnableoperator= (const runnable &)=delete
 Deleted copy assignment operator to prevent copying.
runnableoperator= (runnable &&)=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 reporter & reporter (void) const noexcept
 Gets the test reporter associated with this test runnable.
virtual void run (void)=0
 Runs the test function by invoking the stored callable with the derived self instance.
class runner & runner (void) const noexcept
 Gets the test runner associated with this test runnable.
const runner_totalstotals () const noexcept
 Gets the totals for the test (const overload).
runner_totalstotals () 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(Self_T &)> 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_ = 0
 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.
runner_totals totals_
 Totals for the test node, including nested cases.

Detailed Description

template<typename Self_T>
class micro_os_plus::micro_test_plus::detail::runnable< Self_T >
Template Parameters
Self_TThe concrete derived class type (CRTP pattern). The stored callable receives a Self_T& reference when the test is executed.

Definition at line 443 of file test.h.

Constructor & Destructor Documentation

◆ runnable() [1/3]

template<typename Self_T>
template<typename Callable_T, typename... Args_T>
micro_os_plus::micro_test_plus::detail::runnable< Self_T >::runnable ( const char * name,
class runner & runner,
size_t own_index,
Callable_T && callable,
Args_T &&... arguments )
Template Parameters
Callable_TThe callable type.
Args_TThe additional argument types.
Parameters
nameThe test name, used in reports.
runnerThe test runner managing this test.
own_indexThe test index within the runner.
callableThe callable invoked when the test runs.
argumentsAdditional arguments forwarded to the callable after the leading Self_T& reference.

Binds the callable and its arguments into the stored callable_ function object. When run() is called, the stored function is invoked with a reference to the derived Self_T instance as its first argument, followed by the bound arguments.

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

194 {
195 // When there are no extra arguments the callable already has the
196 // signature void(Self_T&), so store it directly. Only use std::bind when
197 // additional arguments must be pre-bound, to avoid triggering a GCC ARM
198 // bug in
199 // __is_nothrow_invocable<_Bind<...>, Self_T&> (GCC 15.2.1).
200 if constexpr (sizeof...(arguments) == 0)
201 {
203 }
204 else
205 {
209 }
210
211#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
212#if defined(__GNUC__)
213#pragma GCC diagnostic push
214#if defined(__clang__)
215#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
216#endif
217#endif
219#if defined(__GNUC__)
220#pragma GCC diagnostic pop
221#endif
222#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
223 }
class runner & runner(void) const noexcept
Gets the test runner associated with this test runnable.
runnable_base(const char *name, runner &runner, size_t own_index)
Constructs a runnable_base with a name, runner, and index.
Definition test.cpp:141
size_t own_index_
The test index, counting from 1.
Definition test.h:406
size_t own_index() const noexcept
Returns the positional index of this object within its parent.
CRTP base class factoring out callable storage, rule-of-five, and run() logic shared by subtest and s...
Definition test.h:444
std::function< void(Self_T &)> callable_
Callable storing the test body and any bound arguments. Invoked with a reference to the derived Self_...
Definition test.h:512
const char * name(void) const noexcept
Gets the node name.

References micro_os_plus::micro_test_plus::detail::runnable_base::runnable_base(), micro_os_plus::micro_test_plus::detail::test_node::name(), micro_os_plus::micro_test_plus::detail::runnable_base::own_index(), and micro_os_plus::micro_test_plus::detail::runnable_base::runner().

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

◆ runnable() [2/3]

template<typename Self_T>
micro_os_plus::micro_test_plus::detail::runnable< Self_T >::runnable ( const runnable< Self_T > & )
delete

References runnable().

◆ runnable() [3/3]

template<typename Self_T>
micro_os_plus::micro_test_plus::detail::runnable< Self_T >::runnable ( runnable< Self_T > && )
delete

References runnable().

◆ ~runnable()

template<typename Self_T>
micro_os_plus::micro_test_plus::detail::runnable< Self_T >::~runnable ( )
overridevirtual

No-op in production builds. When tracing is enabled via MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED, emits a trace message identifying the instance being destroyed.

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

233 {
234#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
235#if defined(__GNUC__)
236#pragma GCC diagnostic push
237#if defined(__clang__)
238#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
239#endif
240#endif
242#if defined(__GNUC__)
243#pragma GCC diagnostic pop
244#endif
245#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
246 }
const char * name_
The test node name.
Definition test.h:224

References micro_os_plus::micro_test_plus::detail::test_node::name_.

Member Function Documentation

◆ abort()

void micro_os_plus::micro_test_plus::detail::runnable_base::abort ( const reflection::source_location & sl = reflection::source_location::current ())
inherited
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 202 of file test.cpp.

203 {
204 runner_.abort (sl);
205 }
class runner & runner_
Reference to the test runner that owns this object.
Definition test.h:401

References runner_.

Referenced by operator=().

◆ 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 218 of file test.cpp.

220 {
221 // Transfer ownership into the vector.
222 children_subtests_.push_back (std::move (child_test));
223
224 // Run the child test case immediately.
225 class subtest& subtest = *children_subtests_.back ();
226 subtest.run ();
227
228 // This test executed one more subtest.
229#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
230#if defined(__GNUC__)
231#pragma GCC diagnostic push
232#if defined(__clang__)
233#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
234#endif
235#endif
236 trace::printf ("%s subtest '%s' executed one more subtest\n",
237 __PRETTY_FUNCTION__, name ());
238#if defined(__GNUC__)
239#pragma GCC diagnostic pop
240#endif
241#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
243 // Do not accumulate the totals from the child test into the current test
244 // totals, each subtest shows only its counters.
245
246#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
247#if defined(__GNUC__)
248#pragma GCC diagnostic push
249#if defined(__clang__)
250#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
251#endif
252#endif
253 trace::printf ("%s suite '%s' totals\n", __PRETTY_FUNCTION__,
254 suite.name ());
255#if defined(__GNUC__)
256#pragma GCC diagnostic pop
257#endif
258#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
259
260 // Accumulate the totals from the child test into the suite totals.
261 suite.totals () += subtest.totals ();
262 }
std::vector< std::unique_ptr< subtest > > children_subtests_
Owning collection of direct child subtests.
Definition test.h:427
void increment_executed_subtests(size_t count=1) noexcept
Increments the executed-subtests counter.
runner_totals & totals() noexcept
Gets the totals for the test.

References children_subtests_, micro_os_plus::micro_test_plus::detail::runner_totals::increment_executed_subtests(), micro_os_plus::micro_test_plus::detail::test_node::name(), micro_os_plus::micro_test_plus::subtest::run(), and micro_os_plus::micro_test_plus::detail::test_node::totals().

Referenced by operator=().

◆ 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 164 of file test-inlines.h.

165 {
166 return children_subtests_.size ();
167 }

References children_subtests_.

Referenced by micro_os_plus::micro_test_plus::reporter_tap::end_subtest(), micro_os_plus::micro_test_plus::reporter_tap::end_suite(), and operator=().

◆ 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 142 of file test-inlines.h.

143 {
145 }
size_t current_subtest_index_
The subtest index, counting from 1.
Definition test.h:417

References current_subtest_index_.

Referenced by operator=(), micro_os_plus::micro_test_plus::reporter_tap::output_fail_prefix_(), and micro_os_plus::micro_test_plus::reporter_tap::output_pass_prefix_().

◆ increment_subtest_index()

size_t micro_os_plus::micro_test_plus::detail::runnable_base::increment_subtest_index ( )
inlinenoexceptinherited
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 154 of file test-inlines.h.

155 {
156 return ++current_subtest_index_;
157 }

References current_subtest_index_.

Referenced by operator=().

◆ name()

◆ operator=() [1/2]

template<typename Self_T>
runnable & micro_os_plus::micro_test_plus::detail::runnable< Self_T >::operator= ( const runnable< Self_T > & )
delete

References runnable().

◆ operator=() [2/2]

template<typename Self_T>
runnable & micro_os_plus::micro_test_plus::detail::runnable< Self_T >::operator= ( runnable< Self_T > && )
delete

References runnable().

◆ own_index() [1/2]

size_t micro_os_plus::micro_test_plus::detail::runnable_base::own_index ( ) const
inlinenodiscardnoexceptinherited

◆ 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 132 of file test-inlines.h.

133 {
134 own_index_ = index;
135 }

References own_index_.

◆ reporter()

reporter & micro_os_plus::micro_test_plus::detail::runnable_base::reporter ( void ) const
nodiscardnoexceptinherited
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 190 of file test.cpp.

191 {
192 return runner_.reporter ();
193 }

References runner_.

Referenced by operator=().

◆ run()

template<typename Self_T>
virtual void micro_os_plus::micro_test_plus::detail::runnable< Self_T >::run ( void )
pure virtual

◆ 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 174 of file test-inlines.h.

175 {
176 return runner_;
177 }

References runner_.

Referenced by micro_os_plus::micro_test_plus::detail::runnable< Self_T >::runnable(), runnable_base(), and operator=().

◆ 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 110 of file test-inlines.h.

111 {
112 return totals_;
113 }
runner_totals totals_
Totals for the test node, including nested cases.
Definition test.h:229

References totals_.

◆ totals() [2/2]

Member Data Documentation

◆ callable_

template<typename Self_T>
std::function<void (Self_T&)> micro_os_plus::micro_test_plus::detail::runnable< Self_T >::callable_
protected

Definition at line 512 of file test.h.

◆ children_subtests_

std::vector<std::unique_ptr<subtest> > micro_os_plus::micro_test_plus::detail::runnable_base::children_subtests_
protectedinherited

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.

Referenced by after_subtest_create_(), and children_subtests_count().

◆ current_subtest_index_

size_t micro_os_plus::micro_test_plus::detail::runnable_base::current_subtest_index_ = 0
protectedinherited

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.

Referenced by current_subtest_index(), and increment_subtest_index().

◆ name_

const char* micro_os_plus::micro_test_plus::detail::test_node::name_
protectedinherited
Note
Derived classes may access this member directly in addition to the public name() getter.

Definition at line 224 of file test.h.

Referenced by test_node(), micro_os_plus::micro_test_plus::detail::runnable< Self_T >::~runnable(), micro_os_plus::micro_test_plus::detail::runnable_base::~runnable_base(), ~test_node(), and name().

◆ own_index_

size_t micro_os_plus::micro_test_plus::detail::runnable_base::own_index_
protectedinherited

Definition at line 406 of file test.h.

Referenced by runnable_base(), own_index(), and own_index().

◆ runner_

class runner& micro_os_plus::micro_test_plus::detail::runnable_base::runner_
protectedinherited

Definition at line 401 of file test.h.

Referenced by runnable_base(), abort(), reporter(), and runner().

◆ totals_

runner_totals micro_os_plus::micro_test_plus::detail::test_node::totals_
protectedinherited

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