micro-test-plus 4.1.1
µ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
[in]nameThe test name, used in reports.
[in]runnerThe test runner managing this test.
[in]own_indexThe test index within the runner.
[in]callableThe callable invoked when the test runs.
[in]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 192 of file test-inlines.h.

196 {
197 // When there are no extra arguments the callable already has the
198 // signature void(Self_T&), so store it directly. Only use std::bind when
199 // additional arguments must be pre-bound, to avoid triggering a GCC ARM
200 // bug in
201 // __is_nothrow_invocable<_Bind<...>, Self_T&> (GCC 15.2.1).
202 if constexpr (sizeof...(arguments) == 0)
203 {
205 }
206 else
207 {
211 }
212
213#if defined(MICRO_OS_PLUS_TRACE) \
214 && defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
215#if defined(__GNUC__)
216#pragma GCC diagnostic push
217#if defined(__clang__)
218#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
219#endif
220#endif
222#if defined(__GNUC__)
223#pragma GCC diagnostic pop
224#endif
225#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
226 }
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:145
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::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 235 of file test-inlines.h.

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

References ~runnable(), and micro_os_plus::micro_test_plus::detail::test_node::name_.

Referenced by ~runnable().

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

209 {
210 runner_.abort (sl);
211 }
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 224 of file test.cpp.

226 {
227 // Transfer ownership into the vector.
228 children_subtests_.push_back (std::move (child_test));
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",
244 __PRETTY_FUNCTION__, name ());
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 }
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 166 of file test-inlines.h.

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

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

145 {
147 }
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 156 of file test-inlines.h.

157 {
158 return ++current_subtest_index_;
159 }

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
Parameters
None.
Returns
The one-based own index.

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

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

125 {
126 return own_index_;
127 }

References own_index_.

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

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

135 {
136 own_index_ = index;
137 }

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

197 {
198 return runner_.reporter ();
199 }

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

177 {
178 return runner_;
179 }

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

113 {
114 return totals_;
115 }
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: