micro-test-plus 6.0.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
micro_os_plus::micro_test_plus::test_case Class Referencefinal

A named, runnable test case that lives inside a suite. More...

#include "micro-os-plus/micro-test-plus.h"

Inheritance diagram for micro_os_plus::micro_test_plus::test_case:

Public Member Functions

 test_case (const test_case &)=delete
 Deleted copy constructor to prevent copying.
 test_case (test_case &&)=delete
 Deleted move constructor to prevent moving.
 ~test_case () override
 Virtual destructor.
template<class Expr_T>
requires extension::checkable<Expr_T>
detail::check_result && expect (const Expr_T &expr, const reflection::source_location &sl=reflection::source_location::current(), detail::check_result &&result=detail::check_result{})
 Evaluates a condition and reports the result.
const char * name (void) const noexcept
 Returns the node name.
size_t nesting_depth (void) const noexcept
 Returns the nesting depth of this test case.
test_case & operator= (const test_case &)=delete
 Deleted copy assignment operator to prevent copying.
test_case & operator= (test_case &&)=delete
 Deleted move assignment operator to prevent moving.
template<class Expr_T>
requires extension::checkable<Expr_T>
detail::check_result && require (const Expr_T &expr, const reflection::source_location &sl=reflection::source_location::current(), detail::check_result &&result=detail::check_result{})
 Checks a condition and, if false, stops the test case.
template<typename Callable_T, typename... Args_T>
requires detail::test_body<Callable_T, Args_T...>
void test (const located_name &name, Callable_T &&callable, Args_T &&... arguments)
 Adds a nested test case to this test case, and runs it.
template<typename T, typename Callable_T>
void test_each (const located_name &name, std::initializer_list< T > values, Callable_T &&callable)
 Adds a nested test case for each value of a braced list, and runs them.
template<typename Values_T, typename Callable_T>
requires (detail::test_values_range<Values_T> or detail::test_values_tuple<Values_T>)
void test_each (const located_name &name, Values_T &&values, Callable_T &&callable)
 Adds a nested test case for each value of a range or a tuple, and runs them.
const counters & totals (void) const noexcept
 Returns the totals of the node.
Deprecated Functions
template<class Expr_T>
requires detail::legacy_checkable<Expr_T>
detail::check_result && expect (const Expr_T &expr, const reflection::source_location &sl=reflection::source_location::current(), detail::check_result &&result=detail::check_result{})
 Deprecated expect() of a suspicious condition.
template<class Expr_T>
requires detail::legacy_checkable<Expr_T>
detail::check_result && require (const Expr_T &expr, const reflection::source_location &sl=reflection::source_location::current(), detail::check_result &&result=detail::check_result{})
 Deprecated require() of a suspicious condition.
template<class Expr_T>
requires extension::checkable<Expr_T>
detail::check_result && assume (const Expr_T &expr, const reflection::source_location &sl=reflection::source_location::current(), detail::check_result &&result=detail::check_result{})
 Deprecated name of require().
Deprecated Functions of runnable_base
size_t own_index (void) const noexcept
 Deprecated positional index of this node within its parent.
size_t current_subtest_index (void) const noexcept
 Deprecated number of the most recent child test point.
size_t children_subtests_count (void) const noexcept
 Deprecated number of direct child test cases of this node.

Protected Member Functions

bool accepts_test_case (const suite &target, const located_name &name)
 Checks whether a test case can be created on a suite.
void after_test_case_create (class test_case &test_case, suite &suite)
 Counts a newly constructed child test case and executes it immediately.
template<typename Callable_T, typename... Args_T>
void create_test_case (const located_name &name, suite &parent_suite, size_t nesting_depth, Callable_T &&callable, Args_T &&... arguments)
 Creates a child test case, and runs it.
suite * exchange_running_suite (suite *running) noexcept
 Replaces the suite whose body is running in the owning runner, and returns the previous one.
bool has_running_test_case_of (const suite &target) const noexcept
 Checks whether the innermost running test case belongs to a suite.
bool is_aborted (void) const noexcept
 Checks whether the test execution was aborted by the owning runner.
bool is_listing (void) const noexcept
 Checks whether the owning runner lists the test cases instead of running them.
size_t next_test_point_number (void) noexcept
 Advances to the next child test point, and returns its number.
void reject_body_exception (void)
 Reports the exception that escaped the body of a suite while the test cases are listed, and records it.
class test_case * running_test_case (void) const noexcept
 Returns the innermost test case being run by the owning runner, if any.
class reporter & test_reporter (void) const noexcept
 Returns the test reporter associated with this test runnable.
runner & test_runner (void) const noexcept
 Returns the test runner that owns this test runnable.

Protected Attributes

size_t child_test_cases_count_ = 0
 The number of direct child test cases.
const char * name_
 The node name.
size_t own_index_
 The test index, counting from 1.
runner & runner_
 Reference to the test runner that owns this object.
size_t test_point_number_ = 0
 The number of the most recent child test point.
counters totals_
 Totals for the node.

Private Member Functions

 test_case (const located_name &name, runner &test_runner, suite &parent_suite, size_t own_index, size_t nesting_depth, detail::callable_ref< test_case > body)
 Constructs a test case with a name, runner, parent suite, index, nesting depth, and body.
void abort (const reflection::source_location &sl=reflection::source_location::current()) const
 Aborts test execution via the owning runner, for a failed requirement.
void fail_unexpected_exception (const char *what)
 Reports an exception that escaped the test case body as a failed check.
bool is_misdirected (void) const noexcept
 Tells whether another test case is running.
detail::check_result && report_misdirected (detail::check_kind kind, const reflection::source_location &sl, detail::check_result &&result)
 Reports a check on this test case as a failure of the running one.
void report_misdirected_test (const located_name &name)
 Reports a nested test case started on this test case as a failure of the running one.
void report_rejected_test (const std::string &message, const reflection::source_location &location)
 Reports a rejected call as a failed check of this test case.
void run (void) override
 Executes the test case body by invoking the referred callable.

Private Attributes

detail::callable_ref< test_case > callable_
 Reference to the test case body, with any bound arguments.
const located_name & located_name_
 The name and the location of the call to test() that created this test case.
size_t nesting_depth_
 The nesting depth of this test case within the suite.
suite & parent_suite_
 Reference to the parent suite that owns this test case.

Friends

class detail::runnable_base
 The base of the nodes, which creates the test cases.
class micro_test_plus::runner
 The runner, which reports the rejected calls as failed checks of the running test case.

Detailed Description

test_case represents a single, named test case or a nested group of checks within a parent suite or test_case. It is constructed by calling suite::test() or test_case::test(), both of which create the object, immediately execute its callable body via run(), and register the result with the parent node. The object is local to the test() call, and is destroyed when it returns, together with the body and the copies of its arguments, which are also local to that call; the test case only refers to them, so no memory is allocated for them.

The body of the test case is supplied as a callable (typically a lambda) that receives a test_case& reference as its first argument. Inside the body, expect() and require() are used to evaluate conditions and record the results. Test cases may be nested to an arbitrary depth.

The constructor is private, since its parameters are set by the framework; the test cases are created only by suite::test() and test_case::test().

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

Definition at line 1361 of file test.h.

Constructor & Destructor Documentation

◆ test_case() [1/3]

micro_os_plus::micro_test_plus::test_case::test_case ( const located_name & name,
runner & test_runner,
suite & parent_suite,
size_t own_index,
size_t nesting_depth,
detail::callable_ref< test_case > body )
inlineprivate

Private, since the index, the nesting depth, and the parent suite are internal; a test case constructed elsewhere would not be registered with its parent, and would never run.

Parameters
nameThe test case name or description, used in reports, and the location of the call to test() that created the test case.
test_runnerThe test runner managing this test case.
parent_suiteThe suite that owns this test case.
own_indexThe one-based positional index within the parent.
nesting_depthThe depth of nesting; 1 for top-level test cases.
bodyA reference to the body, with its arguments already bound, invoked when the test case executes; it must outlive the test case.

Stores the reference to the body, the reference to the name and the location of the call to test(), which outlive the test case, the parent suite, and the nesting depth; the node name points into the name.

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

544 callable_{ body }, located_name_{ name },
545 parent_suite_{ parent_suite }, nesting_depth_{ nesting_depth }
546 {
547#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
548 trace::printf ("%s '%s' %" MICRO_OS_PLUS_MICRO_TEST_PLUS_PRI_SIZE
550 __PRETTY_FUNCTION__, name_, own_index_, nesting_depth_);
551#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
552 }
runner & test_runner(void) const noexcept
Returns the test runner that owns this test runnable.
size_t own_index_
The test index, counting from 1.
Definition test.h:859
const char * name(void) const noexcept
Returns the node name.
size_t own_index(void) const noexcept
Deprecated positional index of this node within its parent.
size_t nesting_depth_
The nesting depth of this test case within the suite.
Definition test.h:2015
suite & parent_suite_
Reference to the parent suite that owns this test case.
Definition test.h:2007
friend class detail::runnable_base
The base of the nodes, which creates the test cases.
Definition test.h:1382
const located_name & located_name_
The name and the location of the call to test() that created this test case.
Definition test.h:1998
detail::callable_ref< test_case > callable_
Reference to the test case body, with any bound arguments.
Definition test.h:1985
size_t nesting_depth(void) const noexcept
Returns the nesting depth of this test case.
#define MICRO_OS_PLUS_MICRO_TEST_PLUS_PRI_SIZE
Portable printf() conversion for the size_t values.
Definition detail.h:102

References callable_, detail::runnable_base, located_name_, MICRO_OS_PLUS_MICRO_TEST_PLUS_PRI_SIZE, micro_test_plus::runner, micro_os_plus::micro_test_plus::detail::runnable_base::name(), micro_os_plus::micro_test_plus::detail::runnable_base::name_, nesting_depth(), nesting_depth_, micro_os_plus::micro_test_plus::detail::runnable_base::own_index(), micro_os_plus::micro_test_plus::detail::runnable_base::own_index_, parent_suite_, and micro_os_plus::micro_test_plus::detail::runnable_base::test_runner().

Referenced by test_case(), test_case(), is_misdirected(), operator=(), operator=(), report_misdirected(), and report_misdirected_test().

◆ test_case() [2/3]

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

A copy would duplicate the counters.

References test_case().

◆ test_case() [3/3]

micro_os_plus::micro_test_plus::test_case::test_case ( test_case && )
delete

Nodes are referred to by address (e.g. by their children and by the runner), so they must not be relocated.

References test_case().

◆ ~test_case()

micro_os_plus::micro_test_plus::test_case::~test_case ( )
override

Called when the test() function that created the test case returns; the nested test cases were already destroyed by then.

No resources are owned by test_case; it only refers to its body, which is destroyed after it, when the test() call that created both returns. If tracing is enabled, the name is output for diagnostic purposes.

Definition at line 512 of file test.cpp.

513 {
514#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
515 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
516#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
517 }

References micro_os_plus::micro_test_plus::detail::runnable_base::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 ()) const
privateinherited

Private, since it is used only by the check result, a friend, for a failed requirement without exceptions; the user code calls runner::abort().

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

Delegates to the private runner::abort(), as caused by a failed requirement, passing the supplied source location, so that the report identifies it. The runner then calls the abort handler, if any, and terminates the process via ::abort().

Definition at line 306 of file test.cpp.

307 {
308 runner_.abort (sl, true);
309 }
runner & runner_
Reference to the test runner that owns this object.
Definition test.h:848
void abort(const reflection::source_location &sl=reflection::source_location::current())
Aborts test execution immediately.
Definition runner.cpp:1004

References micro_os_plus::micro_test_plus::runner::abort(), and runner_.

Referenced by micro_os_plus::micro_test_plus::detail::check_result::~check_result(), and run().

◆ accepts_test_case()

bool micro_os_plus::micro_test_plus::detail::runnable_base::accepts_test_case ( const suite & target,
const located_name & name )
nodiscardprotectedinherited

Protected, since it is used only by suite::test(). A test case can be created only on the suite whose body is running (the top suite, between runner::add_top_suite() and runner::run_suites(), or the suite being run). A call on any other suite, for example on the top suite from the body of another suite, on a static suite before the runner was initialised, or after runner::run_suites() returned, would run the test case where its results cannot be reported correctly. Such a call is rejected: the failure is reported as a failed check of the running test case, if one runs, otherwise in the running suite, or, when no suite runs, on stderr, and the rejection is remembered by the runner, which makes the exit code non-zero. When the session was declined (option errors, or --help), or aborted, the test cases are ignored silently, as after_test_case_create() ignores them.

Parameters
targetThe suite on which the test case is created.
nameThe name and the location of the test case, for the message.
Return values
trueThe test case can be created.
falseThe test case must not be created.

The suite whose body runs accepts the test case. When the session was declined, or aborted, the test case is ignored, as after_test_case_create() ignores it.

Otherwise the call is rejected. If a test case runs (and the test cases are not only listed), the failure is reported as a check of that test case, so that it stays in its report. Otherwise, if a suite runs, the failure is reported as a check of that suite, with the same numbering and counting as the unexpected exceptions of the suite body. Otherwise (the runner was not initialised, has not started the suites, or has finished them, or the test cases are listed) the failure is given to runner::reject_call(), which writes the message on stderr and, where the reporter can still write it, as a comment line, for example in the list, and runner::mark_rejected() makes the exit code non-zero, as for the suites added too late.

Definition at line 239 of file test.cpp.

241 {
242 if (runner_.running_suite_ == &target)
243 {
244 return true;
245 }
246
249 {
250 return false;
251 }
252
253 std::string message{ "test case '" };
254 message.append (name.name ());
255 message.append ("' started on suite '");
256 message.append (target.name ());
257 message.append ("', which is not running");
258
259 if (runner_.running_test_case_ != nullptr
261 {
263 name.location ());
264 return false;
265 }
266
267 suite* running = runner_.running_suite_;
268 if (running != nullptr && !runner_.options_.list_tests)
269 {
270 running->next_test_point_number ();
271 running->test_reporter ().fail_suite (message, *running);
272 running->totals_.increment_failed_checks ();
273 return false;
274 }
275
276 runner_.reject_call (message, &name.location ());
277 return false;
278 }
bool help
Whether to display the usage and the list of the options, instead of running the tests.
bool list_tests
Whether the test cases are listed instead of run.
class suite * running_suite_
The suite whose body is running, if any.
Definition runner.h:1409
bool has_option_errors(void) const noexcept
Checks whether an option error was found by initialise().
void reject_call(const std::string &message, const reflection::source_location *location=nullptr)
Reports a rejected call, and records it.
Definition runner.cpp:660
runner_options options_
The options of the test session.
Definition runner.h:1306
test_case * running_test_case_
The innermost test case being run, if any.
Definition runner.h:1397
bool aborted_
Whether the test execution was aborted.
Definition runner.h:1386
void report_rejected_test(const std::string &message, const reflection::source_location &location)
Reports a rejected call as a failed check of this test case.
Definition test.cpp:493

References micro_os_plus::micro_test_plus::runner::aborted_, micro_os_plus::micro_test_plus::detail::reporter::fail_suite(), micro_os_plus::micro_test_plus::runner::has_option_errors(), micro_os_plus::micro_test_plus::runner_options::help, micro_os_plus::micro_test_plus::counters::increment_failed_checks(), micro_os_plus::micro_test_plus::runner_options::list_tests, micro_os_plus::micro_test_plus::located_name::location(), name(), micro_os_plus::micro_test_plus::located_name::name(), next_test_point_number(), micro_os_plus::micro_test_plus::runner::options_, micro_os_plus::micro_test_plus::runner::reject_call(), micro_os_plus::micro_test_plus::test_case::report_rejected_test(), runner_, micro_os_plus::micro_test_plus::runner::running_suite_, micro_os_plus::micro_test_plus::runner::running_test_case_, test_reporter(), and totals_.

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

◆ after_test_case_create()

void micro_os_plus::micro_test_plus::detail::runnable_base::after_test_case_create ( class test_case & test_case,
suite & suite )
protectedinherited

The child test case is a local object of create_test_case(), and is destroyed when that function returns.

Parameters
test_caseThe newly created test_case.
suiteThe parent suite to which execution results are reported.
Returns
Nothing.

Counts test_case, which is a local object of the calling create_test_case(), and immediately invokes test_case::run() on it. The test case is not kept; only child_test_cases_count_ is needed later, for the TAP plan.

If the filter patterns do not select the test case (its own name at its nesting level, or the name of suite at level 0, which matters only for the top suite, since the other suites run only when selected), its body is not called: the reporter is notified with skip_test_case() and the parent's skipped test cases counter is incremented. The test case keeps its index and its place among the children, so the TAP plan of the parent remains complete.

Otherwise, while the test case runs, the runner keeps it as its innermost running test case (see suite::test()). The parent's executed test cases counter is incremented after the run, and, if the child failed (a failed check, or a failed nested test case), its failed test cases counter too, so that the parent fails as well, as in node-tap. The child's check counters are intentionally not merged into the parent totals; each test case 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 test cases.

While the test cases are listed (see runner_options::list_tests), the selected test case is listed by the runner, and neither run nor counted, except in child_test_cases_count_.

After the test execution was aborted (possible only when the abort handler threw an exception, which the application caught), the report is complete, so the test case is neither run nor reported. The same applies when the runner refused to run the tests because of errors in the options, or because the output file could not be opened (see runner::has_option_errors()), or because the options requested the usage (--help).

Definition at line 350 of file test.cpp.

352 {
355 {
356 return;
357 }
358
360
361 // While the test cases are listed, the selected ones are listed and
362 // the others ignored; none is run.
364 {
365 if (runner_.is_selected (suite.name (), 0)
366 && runner_.is_selected (test_case.name (),
367 test_case.nesting_depth ()))
368 {
369 runner_.list_test_case (suite, test_case);
370 }
371 return;
372 }
373
374 // Skip the test cases not selected by the filter; the outer test
375 // cases, if any, were selected, otherwise this one was not created.
376 if (!runner_.is_selected (suite.name (), 0)
377 || !runner_.is_selected (test_case.name (),
378 test_case.nesting_depth ()))
379 {
380 test_reporter ().skip_test_case (test_case);
382 return;
383 }
384
385 // Run the child test case immediately, as the innermost running test
386 // case of the runner. The outer one is restored by the guard, also
387 // when an exception leaves run() (for example thrown by an abort
388 // handler), so that the runner never keeps a destroyed test case.
389 {
390 struct running_guard
391 {
392 class test_case*& running;
393 class test_case* outer;
394
395 ~running_guard ()
396 {
397 running = outer;
398 }
400
401 runner_.running_test_case_ = &test_case;
402 // A non-virtual call, so that run() is inlined here; each nested
403 // test case adds this call chain to the stack.
404 test_case.test_case::run ();
405 }
406
407 // This test executed one more test case.
408#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
409 trace::printf ("%s test case '%s' executed one more test case\n",
410 __PRETTY_FUNCTION__, name ());
411#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
413
414 // Do not accumulate the totals from the child test into the current test
415 // totals, each test case shows only its counters. But a failed child
416 // fails this test too, as in node-tap; the failure reaches all outer
417 // tests, one level at a time, when they end.
418 if (!test_case.totals ().passed ())
419 {
421 }
422
423#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
424 trace::printf ("%s suite '%s' totals\n", __PRETTY_FUNCTION__,
425 suite.name ());
426#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
427
428 // Accumulate the totals from the child test into the suite totals.
429 suite.totals_ += test_case.totals ();
430 }
void increment_skipped_test_cases(size_t count=1) noexcept
Increments the skipped test cases counter.
void increment_executed_test_cases(size_t count=1) noexcept
Increments the executed test cases counter.
void increment_failed_test_cases(size_t count=1) noexcept
Increments the failed test cases counter.
virtual void skip_test_case(const test_case &test_case)=0
Reports a test case skipped by the filter.
class reporter & test_reporter(void) const noexcept
Returns the test reporter associated with this test runnable.
Definition test.cpp:155
size_t child_test_cases_count_
The number of direct child test cases.
Definition test.h:883
void list_test_case(const class suite &suite, const class test_case &test_case)
Lists a test case, instead of running it.
Definition runner.cpp:563
bool is_selected(const char *name, size_t level) const
Checks whether a suite or a test case is selected by the filter pattern of its nesting level.
Definition runner.cpp:530

References micro_os_plus::micro_test_plus::runner::aborted_, child_test_cases_count_, micro_os_plus::micro_test_plus::runner::has_option_errors(), micro_os_plus::micro_test_plus::runner_options::help, micro_os_plus::micro_test_plus::counters::increment_executed_test_cases(), micro_os_plus::micro_test_plus::counters::increment_failed_test_cases(), micro_os_plus::micro_test_plus::counters::increment_skipped_test_cases(), micro_os_plus::micro_test_plus::runner::is_selected(), micro_os_plus::micro_test_plus::runner::list_test_case(), micro_os_plus::micro_test_plus::runner_options::list_tests, name(), micro_os_plus::micro_test_plus::test_case::nesting_depth(), micro_os_plus::micro_test_plus::runner::options_, micro_os_plus::micro_test_plus::counters::passed(), runner_, micro_os_plus::micro_test_plus::runner::running_test_case_, micro_os_plus::micro_test_plus::detail::reporter::skip_test_case(), test_reporter(), totals(), and totals_.

Referenced by create_test_case(), and run().

◆ assume()

template<class Expr_T>
requires extension::checkable<Expr_T>
detail::check_result && micro_os_plus::micro_test_plus::test_case::assume ( const Expr_T & expr,
const reflection::source_location & sl = reflection::source_location::current (),
detail::check_result && result = detail::check_result{} )

The same as require(). The name was misleading: in other frameworks (JUnit, Hypothesis), an unmet assumption skips the test, while here it fails it.

Template Parameters
Expr_TThe type of the custom expression.
Parameters
exprLogical expression to evaluate.
slOptional source location, defaulting to the current line.
resultThe check result, a temporary created by the caller as the default argument, which the applications do not pass; it is destroyed at the end of the statement.
Returns
A reference to the check result, to which optional messages can be appended with operator<<; the check is reported when it is destroyed, at the end of the statement.
Warning
The result must not be named, stored, or kept in any variable. A reference such as auto&& r = t.expect (...) compiles, but dangles after the statement; the message operator and value() can be called only on a temporary, so the named result cannot be used, and forcing it with std::move() is undefined. The check must be reported at the end of the statement, and not later. Write the message in the same statement, with operator<<.
Deprecated
use require() (since 6.0.0)

Forwards to require(), with the same check result temporary, which must be passed on, since it is destroyed at the end of the statement of the caller.

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

785 {
786 return require (expr, sl, std::move (result));
787 }
detail::check_result && require(const Expr_T &expr, const reflection::source_location &sl=reflection::source_location::current(), detail::check_result &&result=detail::check_result{})
Checks a condition and, if false, stops the test case.

References require().

◆ children_subtests_count()

size_t micro_os_plus::micro_test_plus::detail::runnable_base::children_subtests_count ( void ) const
inlinenodiscardnoexceptinherited

Used only for the TAP plan, by the reporters, which access it directly. The test cases skipped by the filter are included.

Parameters
None.
Returns
The number of child test cases.
Deprecated
internal to the framework (since 6.0.0)

Returns the number of direct child test cases, including those skipped by the filter.

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

242 {
244 }

References child_test_cases_count_.

Referenced by operator=().

◆ create_test_case()

template<typename Callable_T, typename... Args_T>
void micro_os_plus::micro_test_plus::detail::runnable_base::create_test_case ( const located_name & name,
suite & parent_suite,
size_t nesting_depth,
Callable_T && callable,
Args_T &&... arguments )
inlineprotectedinherited

The common body of suite::test() and test_case::test(), which differ only in the parent suite and the nesting depth of the test cases they create.

Always inlined into them, even without optimisations, since each nested test case adds one call chain to the stack, and a separate frame for this function would make it longer.

The test case runs and ends within the call, so, without additional arguments, it refers to the callable of the caller, which is neither copied nor moved, and is called in place, so a callable that cannot be copied is accepted, and an lvalue callable keeps the changes of its state. With additional arguments, the callable and the arguments are bound into a body of copies, by bind_callable().

Template Parameters
Callable_TThe type of a callable object.
Args_TThe types of the callable arguments.
Parameters
nameThe test case name or description, used in reports, and the location of the call to test().
parent_suiteThe suite to which the test case belongs.
nesting_depthThe depth of nesting of the test case; 1 for the direct children of a suite.
callableA generic callable object, usually a lambda, invoked to perform the test.
argumentsA possibly empty list of arguments to be passed to the callable.
Returns
Nothing.

Without additional arguments, the child test_case is a local object that refers to the callable of the caller, which is neither copied nor moved, and is called in place (an lvalue callable keeps the changes of its state). A function is referred to through its pointer, and the callable and the additional arguments are bound into a local body, as copies, when there are arguments. The child is constructed with the next test point number of this node as its index, then passed to after_test_case_create(), which counts and runs it.

The child test case and its body, with the copies of the arguments, are destroyed when this function returns, so the memory used by a test session does not grow with the number of test cases; the stack holds one test case and one body for each level of nesting being run. Since the body is not stored in a std::function, it is never allocated, whatever the size of the lambda captures and of the arguments.

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

296 {
298
299 if constexpr (sizeof...(arguments) == 0
300 && !std::is_function_v<
301 std::remove_reference_t<Callable_T>>)
302 {
303 // The test case runs and ends within this call, so it refers
304 // to the callable of the caller, which is not copied, and is
305 // called in place.
306 class test_case child_test_case{
307 name, runner_, parent_suite,
308 own_index, nesting_depth, callable_ref<class test_case>{ callable }
309 };
310
311 after_test_case_create (child_test_case, parent_suite);
312 }
313 else
314 {
316 std::forward<Callable_T> (callable),
317 std::forward<Args_T> (arguments)...);
318 class test_case child_test_case{
319 name, runner_, parent_suite,
320 own_index, nesting_depth, callable_ref<class test_case>{ body }
321 };
322
323 after_test_case_create (child_test_case, parent_suite);
324 }
325 }
size_t next_test_point_number(void) noexcept
Advances to the next child test point, and returns its number.
void after_test_case_create(class test_case &test_case, suite &suite)
Counts a newly constructed child test case and executes it immediately.
Definition test.cpp:350
auto bind_callable(Callable_T &&callable, Args_T &&... arguments)
Creates the function object that holds the body of a test case or suite.

References after_test_case_create(), micro_os_plus::micro_test_plus::detail::bind_callable(), name(), next_test_point_number(), own_index(), and runner_.

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

◆ current_subtest_index()

size_t micro_os_plus::micro_test_plus::detail::runnable_base::current_subtest_index ( void ) const
inlinenodiscardnoexceptinherited

Used only for the TAP output, by the reporters, which access it directly.

Parameters
None.
Returns
The one-based test point number, or 0 before the first test point.
Deprecated
internal to the framework (since 6.0.0)

Returns the number of the most recent child test point.

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

230 {
231 return test_point_number_;
232 }
size_t test_point_number_
The number of the most recent child test point.
Definition test.h:871

References test_point_number_.

Referenced by operator=().

◆ exchange_running_suite()

suite * micro_os_plus::micro_test_plus::detail::runnable_base::exchange_running_suite ( suite * running)
protectednoexceptinherited

Protected, since it is used only by suite::run(), to mark the suite whose body is being called, and to restore the previous value when the body returns.

Parameters
runningThe suite that starts to run, or the previous one, to restore it.
Returns
The suite that was marked as running before the call, or nullptr when none was.

Sets the pointer of the runner, a friend, and returns the previous value, so that the caller can restore it when the body returns.

Definition at line 212 of file test.cpp.

213 {
214 suite* previous = runner_.running_suite_;
215 runner_.running_suite_ = running;
216 return previous;
217 }

References runner_.

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

◆ expect()

template<class Expr_T>
requires detail::legacy_checkable<Expr_T>
detail::check_result && micro_os_plus::micro_test_plus::test_case::expect ( const Expr_T & expr,
const reflection::source_location & sl = reflection::source_location::current(),
detail::check_result && result = detail::check_result{} )

Accepts the arguments excluded from extension::checkable, such as a string literal or a function name, which are always true, so the check passes silently; usually the description was written instead of the condition, or the function was not called. Write the condition, or ne (p, nullptr) for a pointer.

Template Parameters
Expr_TThe type of the argument.
Parameters
exprThe argument.
slOptional source location, defaulting to the current line.
resultThe check result, a temporary created by the caller as the default argument, which the applications do not pass.
Returns
A reference to the check result.
Deprecated
the argument is always true; write the condition (since 6.0.0)

References micro_os_plus::micro_test_plus::reflection::source_location::current().

◆ fail_unexpected_exception()

void micro_os_plus::micro_test_plus::test_case::fail_unexpected_exception ( const char * what)
private

The failure is reported at the location of the call to test() that created the test case, since the location of the throw is not known.

Parameters
whatThe explanatory string of a std::exception, or nullptr for an exception of another type.
Returns
Nothing.

The failure is reported as a check: the test point number is incremented first, as check_result does, so that the TAP output numbers it after the checks already reported, and the failed checks counter is incremented, so that the test case, and through it its parents, fail.

The message is unexpected exception: followed by what, or unexpected exception of unknown type when what is nullptr; the type of a std::exception is not shown, since its name is available only via RTTI, and only in an implementation-defined (usually mangled) form. There is no expression to display.

Definition at line 535 of file test.cpp.

536 {
538
539 detail::failure_info failure{ .kind = detail::check_kind::expectation,
540 .message = "unexpected exception",
541 .has_expression = false,
542 .location = located_name_.location () };
543 if (what != nullptr)
544 {
545 failure.message.append (": ");
546 failure.message.append (what);
547 }
548 else
549 {
550 failure.message.append (" of unknown type");
551 }
552
553 const std::string expression{};
554 test_reporter ().fail (failure, expression, *this);
556 }
void increment_failed_checks(size_t count=1) noexcept
Increments the failed-checks counter.
void fail(const failure_info &failure, const std::string &expression, const test_case &test_case)
Reports a failed condition.
Definition reporter.cpp:771
const reflection::source_location & location(void) const noexcept
Returns the location where the name was given.

References micro_os_plus::micro_test_plus::detail::expectation, micro_os_plus::micro_test_plus::detail::reporter::fail(), micro_os_plus::micro_test_plus::counters::increment_failed_checks(), located_name_, micro_os_plus::micro_test_plus::located_name::location(), micro_os_plus::micro_test_plus::detail::failure_info::message, micro_os_plus::micro_test_plus::detail::runnable_base::next_test_point_number(), micro_os_plus::micro_test_plus::detail::runnable_base::test_reporter(), and micro_os_plus::micro_test_plus::detail::runnable_base::totals_.

Referenced by run().

◆ has_running_test_case_of()

bool micro_os_plus::micro_test_plus::detail::runnable_base::has_running_test_case_of ( const suite & target) const
nodiscardprotectednoexceptinherited

Protected, since it is used only by suite::test(), to forward a test case started on the suite to the running test case only when that is a test case of the same suite; the test cases started on another suite are rejected.

Parameters
targetThe suite to check.
Return values
trueA test case is running, and its parent suite is target.
falseNo test case is running, or it belongs to another suite.

Reads the pointer of the runner, a friend, and the parent suite of the test case, which runnable_base may access.

Definition at line 199 of file test.cpp.

201 {
202 const test_case* running = runner_.running_test_case_;
203 return running != nullptr && &running->parent_suite_ == &target;
204 }

References micro_os_plus::micro_test_plus::test_case::parent_suite_, and runner_.

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

◆ is_aborted()

bool micro_os_plus::micro_test_plus::detail::runnable_base::is_aborted ( void ) const
nodiscardprotectednoexceptinherited

Protected, since it is used only by test_case::run() and suite::run(), to pass on an exception thrown by the abort handler, instead of reporting it as unexpected.

Parameters
None.
Returns
true once runner::abort() was called.

Returns the flag kept by the owning runner, set by runner::abort() and never cleared.

Definition at line 177 of file test.cpp.

178 {
179 return runner_.aborted_;
180 }

References runner_.

Referenced by run(), micro_os_plus::micro_test_plus::suite::run(), and micro_os_plus::micro_test_plus::test_case::run().

◆ is_listing()

bool micro_os_plus::micro_test_plus::detail::runnable_base::is_listing ( void ) const
nodiscardprotectednoexceptinherited

Protected, since it is used only by suite::run(), which then calls the suite body without reporting the suite, so that its test cases are created, and listed instead of run.

Parameters
None.
Returns
true if runner_options::list_tests was set.

Returns runner_options::list_tests of the session options kept by the owning runner.

Definition at line 188 of file test.cpp.

189 {
191 }

References runner_.

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

◆ is_misdirected()

bool micro_os_plus::micro_test_plus::test_case::is_misdirected ( void ) const
inlinenodiscardprivatenoexcept

A check on this test case while a nested one runs, for example from a nested body that captured the outer test case by reference, would be written inside the subtest of the nested one, and break its TAP plan.

Parameters
None.
Return values
trueAnother test case is running.
falseThis test case is running, or none is.

Another test case is running when this one is not the running test case recorded by the runner, and the runner has one.

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

606 {
607 const test_case* running = running_test_case ();
608 return running != nullptr && running != this;
609 }
class test_case * running_test_case(void) const noexcept
Returns the innermost test case being run by the owning runner, if any.
Definition test.cpp:166
test_case(const located_name &name, runner &test_runner, suite &parent_suite, size_t own_index, size_t nesting_depth, detail::callable_ref< test_case > body)
Constructs a test case with a name, runner, parent suite, index, nesting depth, and body.

References test_case(), and micro_os_plus::micro_test_plus::detail::runnable_base::running_test_case().

Referenced by expect(), require(), and test().

◆ name()

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

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

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

196 {
197 return name_;
198 }

References name_.

Referenced by runnable_base(), micro_os_plus::micro_test_plus::runner::runner(), micro_os_plus::micro_test_plus::runner::runner(), micro_os_plus::micro_test_plus::static_suite::static_suite(), micro_os_plus::micro_test_plus::suite::suite(), micro_os_plus::micro_test_plus::test_case::test_case(), micro_os_plus::micro_test_plus::top_suite::top_suite(), accepts_test_case(), micro_os_plus::micro_test_plus::runner::add_top_suite(), after_test_case_create(), micro_os_plus::micro_test_plus::detail::reporter_human::begin_suite(), micro_os_plus::micro_test_plus::detail::reporter_tap::begin_suite(), micro_os_plus::micro_test_plus::detail::reporter_human::begin_test_case(), micro_os_plus::micro_test_plus::detail::reporter_tap::begin_test_case(), create_test_case(), micro_os_plus::micro_test_plus::test_case::detail::runnable_base, micro_os_plus::micro_test_plus::detail::reporter_human::display_open_heading(), micro_os_plus::micro_test_plus::detail::reporter_tap::display_open_heading(), micro_os_plus::micro_test_plus::detail::reporter_human::display_open_names(), micro_os_plus::micro_test_plus::detail::reporter_human::end_suite(), micro_os_plus::micro_test_plus::detail::reporter_tap::end_suite(), micro_os_plus::micro_test_plus::detail::reporter_human::end_test_case(), micro_os_plus::micro_test_plus::detail::reporter_tap::end_test_case(), micro_os_plus::micro_test_plus::detail::reporter_human::fail_suite(), micro_os_plus::micro_test_plus::detail::reporter_tap::fail_suite(), micro_os_plus::micro_test_plus::runner::list_test_case(), micro_os_plus::micro_test_plus::top_suite::micro_test_plus::runner, operator=(), micro_os_plus::micro_test_plus::suite::operator=(), micro_os_plus::micro_test_plus::test_case::operator=(), micro_os_plus::micro_test_plus::test_case::report_misdirected(), micro_os_plus::micro_test_plus::test_case::report_misdirected_test(), reporter_tap, run(), micro_os_plus::micro_test_plus::detail::reporter_human::skip_suite(), micro_os_plus::micro_test_plus::detail::reporter_tap::skip_suite(), micro_os_plus::micro_test_plus::detail::reporter_human::skip_test_case(), micro_os_plus::micro_test_plus::detail::reporter_tap::skip_test_case(), micro_os_plus::micro_test_plus::suite::static_suite, micro_os_plus::micro_test_plus::suite::test(), micro_os_plus::micro_test_plus::test_case::test(), micro_os_plus::micro_test_plus::suite::test_each(), micro_os_plus::micro_test_plus::suite::test_each(), micro_os_plus::micro_test_plus::test_case::test_each(), micro_os_plus::micro_test_plus::test_case::test_each(), and micro_os_plus::micro_test_plus::detail::reporter_tap::write_quiet_failures().

◆ nesting_depth()

size_t micro_os_plus::micro_test_plus::test_case::nesting_depth ( void ) const
inlinenodiscardnoexcept

◆ next_test_point_number()

size_t micro_os_plus::micro_test_plus::detail::runnable_base::next_test_point_number ( void )
inlineprotectednoexceptinherited

Called for each check (by the check result, when the check is reported) and for each nested test case created.

Parameters
None.
Returns
The one-based number of the new test point.

Each call to test() invokes this member function before constructing the new test_case, and each check invokes it from the check result destructor, when the check is reported, so the numbers form a strictly increasing, one-based sequence of TAP test points, in output order.

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

256 {
257 return ++test_point_number_;
258 }

References test_point_number_.

Referenced by micro_os_plus::micro_test_plus::detail::check_result::~check_result(), accepts_test_case(), create_test_case(), micro_os_plus::micro_test_plus::suite::fail_unexpected_exception(), micro_os_plus::micro_test_plus::test_case::fail_unexpected_exception(), micro_os_plus::micro_test_plus::runner::reject_call(), and run().

◆ operator=() [1/2]

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

A copy would duplicate the counters.

References test_case().

◆ operator=() [2/2]

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

Nodes are referred to by address (e.g. by their children and by the runner), so they must not be relocated.

References test_case(), micro_os_plus::micro_test_plus::reflection::source_location::current(), expect(), micro_os_plus::micro_test_plus::detail::runnable_base::name(), test(), and test_each().

◆ own_index()

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

Used only for the TAP output, by the reporters, which access it directly. The setter of v5.0.1, and increment_subtest_index(), were removed, since they allowed the application code to corrupt the numbering of the reports.

Parameters
None.
Returns
The one-based own index.
Deprecated
internal to the framework (since 6.0.0)

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

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

219 {
220 return own_index_;
221 }

References own_index_.

Referenced by runnable_base(), micro_os_plus::micro_test_plus::test_case::test_case(), create_test_case(), micro_os_plus::micro_test_plus::test_case::detail::runnable_base, operator=(), and reporter_tap.

◆ reject_body_exception()

void micro_os_plus::micro_test_plus::detail::runnable_base::reject_body_exception ( void )
protectedinherited

Protected, since it is used only by suite::run(), when there is no test point to fail. It gives the message, which names the suite, to runner::reject_call(), which writes it on stderr and in the listing, and makes the exit code non-zero.

Parameters
None.
Returns
Nothing.

Builds the message here, and not in suite::run(), to keep the frame of that function, which is on the stack of every session, unchanged. Delegates to the private runner::reject_call(), which writes the message on stderr and in the listing, and marks the runner, so that the exit code of runner::run_suites() is not 0.

Definition at line 289 of file test.cpp.

290 {
291 std::string message{ "unexpected exception in the body of suite '" };
292 message.append (name_);
293 message.append ("'");
294
295 runner_.reject_call (message);
296 }

References name_, micro_os_plus::micro_test_plus::runner::reject_call(), and runner_.

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

◆ report_misdirected()

detail::check_result && micro_os_plus::micro_test_plus::test_case::report_misdirected ( detail::check_kind kind,
const reflection::source_location & sl,
detail::check_result && result )
private

Initialises result as a failed check of the running test case, with a message that names the two test cases; the message of the caller, if any, is appended after it. A failed requirement stops the running test case. Non-template, defined in src/test.cpp, so that the call sites of expect() and require() only call it.

Parameters
kindThe kind of the check.
slThe source location of the check.
resultThe check result, a temporary created by the caller.
Returns
A reference to the check result.

The check is attributed to the running test case, as a failure whose message names the two test cases, so that the checks stay in the subtest of the test case that runs, and its plan stays valid. The failing condition is a constant, so that the check is always reported as failed, even if the condition of the caller held.

Definition at line 444 of file test.cpp.

447 {
448 test_case& running = *running_test_case ();
449
450 std::string message{ "[check on test case '" };
451 message.append (name ());
452 message.append ("' while '");
453 message.append (running.name ());
454 message.append ("' runs] ");
455
456 result.init (false, kind, sl, running,
457 running.test_reporter ().expression_,
458 running.test_reporter ().writes_passed_checks ());
459
460 return std::move (std::move (result) << message);
461 }

References test_case(), micro_os_plus::micro_test_plus::detail::reporter::expression_, micro_os_plus::micro_test_plus::detail::runnable_base::name(), micro_os_plus::micro_test_plus::detail::runnable_base::running_test_case(), micro_os_plus::micro_test_plus::detail::runnable_base::test_reporter(), and micro_os_plus::micro_test_plus::detail::reporter::writes_passed_checks().

Referenced by expect(), and require().

◆ report_misdirected_test()

void micro_os_plus::micro_test_plus::test_case::report_misdirected_test ( const located_name & name)
private

Called by test() when another test case is running. The nested test case is not run; a failed check, with a message that names the three test cases, is reported in the running test case, with the numbering and the counting of the other checks, by report_rejected_test(). Non-template, defined in src/test.cpp.

Parameters
nameThe name and the location of the rejected test case.
Returns
Nothing.

Reports a failed check in the running test case, with a message that names the rejected test case, this one, and the running one, so that the numbering of the test points, the counting, and the plan of the running test case stay valid, at the location of the rejected test() call.

Definition at line 472 of file test.cpp.

473 {
474 test_case& running = *running_test_case ();
475
476 std::string message{ "[test case '" };
477 message.append (name.name ());
478 message.append ("' started on test case '");
479 message.append (this->name ());
480 message.append ("' while '");
481 message.append (running.name ());
482 message.append ("' runs]");
483
484 running.report_rejected_test (message, name.location ());
485 }

References test_case(), micro_os_plus::micro_test_plus::located_name::location(), micro_os_plus::micro_test_plus::detail::runnable_base::name(), micro_os_plus::micro_test_plus::located_name::name(), report_rejected_test(), and micro_os_plus::micro_test_plus::detail::runnable_base::running_test_case().

Referenced by test().

◆ report_rejected_test()

void micro_os_plus::micro_test_plus::test_case::report_rejected_test ( const std::string & message,
const reflection::source_location & location )
private

Called on the running test case, by report_misdirected_test() and by runnable_base::accepts_test_case(), when a test case is started where it cannot be reported, and by runner::reject_call(), for the other calls rejected while the test case runs. The failed check is counted and numbered as any other check of this test case, so that its plan stays valid. Non-template, defined in src/test.cpp.

Parameters
messageThe message of the failure.
locationThe location of the rejected call.
Returns
Nothing.

The check is built as the ones of report_misdirected(), always failed, and is reported when it goes out of scope.

Definition at line 493 of file test.cpp.

495 {
496 detail::check_result result;
497 result.init (false, detail::check_kind::expectation, location, *this,
498 test_reporter ().expression_,
499 test_reporter ().writes_passed_checks ());
500 std::move (result) << message;
501 }

References micro_os_plus::micro_test_plus::detail::expectation, micro_os_plus::micro_test_plus::detail::check_result::init(), and micro_os_plus::micro_test_plus::detail::runnable_base::test_reporter().

Referenced by micro_os_plus::micro_test_plus::detail::runnable_base::accepts_test_case(), micro_os_plus::micro_test_plus::runner::reject_call(), and report_misdirected_test().

◆ require()

template<class Expr_T>
requires detail::legacy_checkable<Expr_T>
detail::check_result && micro_os_plus::micro_test_plus::test_case::require ( const Expr_T & expr,
const reflection::source_location & sl = reflection::source_location::current(),
detail::check_result && result = detail::check_result{} )

Accepts the arguments excluded from extension::checkable, as the deprecated overload of expect() does.

Template Parameters
Expr_TThe type of the argument.
Parameters
exprThe argument.
slOptional source location, defaulting to the current line.
resultThe check result, a temporary created by the caller as the default argument, which the applications do not pass.
Returns
A reference to the check result.
Deprecated
the argument is always true; write the condition (since 6.0.0)

References micro_os_plus::micro_test_plus::reflection::source_location::current().

◆ run()

void micro_os_plus::micro_test_plus::test_case::run ( void )
inlineoverrideprivatevirtual

Private, since the test case is run only once, by after_test_case_create(), right after its creation.

Always inlined there, with a non-virtual call, so that, without optimisations, it adds no frame to the call chain of each nested test case; the out-of-line copy is used only through the virtual table.

Parameters
None.
Returns
Nothing.

Invokes the referred body with this test case, surrounded by reporter calls to begin and end the test case.

When exceptions are enabled, an exception that escapes the callable is caught and reported as a failed check of this test case, and the test case ends normally, so that the run continues with the next test case; the checks performed before the exception remain reported. Since the nested test cases catch their own exceptions, an exception is reported by the innermost test case whose body it escapes.

The detail::failed_requirement exception, thrown after a failed require(), only stops the body; the failure was already reported and counted.

An exception thrown by the abort handler, after the abort of the test execution was reported, is passed on, without reporting it, and without ending the test case, since the report is already complete; it reaches the application, which set the handler to return control this way.

Defined here, as inline, so that it can be always inlined into after_test_case_create().

Implements micro_os_plus::micro_test_plus::detail::runnable_base.

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

828 {
829#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED)
830 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
831#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED
832
833 detail::reporter& reporter = this->test_reporter ();
834
835 // For now, test cases do not record the time.
836 // this->stopwatch_.start ();
837 reporter.begin_test_case (*this);
838
839#if defined(__cpp_exceptions)
840 try
841 {
842 // Invoke the callable, passing the self reference; the arguments
843 // of test() are bound to it, by bind_callable().
844 callable_ (*this);
845 }
846 catch (const detail::failed_requirement&)
847 {
848 // A failed require() stopped the test case; already reported.
849 }
850 catch (const std::exception& e)
851 {
852 if (is_aborted ())
853 {
854 // Thrown by the abort handler; the report is complete.
855 throw;
856 }
857 fail_unexpected_exception (e.what ());
858 }
859 catch (...)
860 {
861 if (is_aborted ())
862 {
863 // Thrown by the abort handler; the report is complete.
864 throw;
865 }
867 }
868#else
869 // Invoke the callable, passing the self reference; the arguments of
870 // test() are bound to it, by bind_callable().
871 callable_ (*this);
872#endif // defined(__cpp_exceptions)
873
874 // this->stopwatch_.stop ();
875 reporter.end_test_case (*this);
876 }
bool is_aborted(void) const noexcept
Checks whether the test execution was aborted by the owning runner.
Definition test.cpp:177
void fail_unexpected_exception(const char *what)
Reports an exception that escaped the test case body as a failed check.
Definition test.cpp:535

References micro_os_plus::micro_test_plus::detail::reporter::begin_test_case(), callable_, micro_os_plus::micro_test_plus::detail::reporter::end_test_case(), fail_unexpected_exception(), micro_os_plus::micro_test_plus::detail::runnable_base::is_aborted(), micro_os_plus::micro_test_plus::detail::runnable_base::name_, and micro_os_plus::micro_test_plus::detail::runnable_base::test_reporter().

◆ running_test_case()

test_case * micro_os_plus::micro_test_plus::detail::runnable_base::running_test_case ( void ) const
nodiscardprotectednoexceptinherited

Protected, since it is used only by suite::test(), to run a test case started on the suite from inside a running test case as a nested test case of it.

Parameters
None.
Returns
A pointer to the running test case, or nullptr when no test case is running.

Returns the pointer kept by the owning runner, set by after_test_case_create() while a test case runs.

Definition at line 166 of file test.cpp.

167 {
169 }

References runner_.

Referenced by micro_os_plus::micro_test_plus::test_case::is_misdirected(), micro_os_plus::micro_test_plus::test_case::report_misdirected(), micro_os_plus::micro_test_plus::test_case::report_misdirected_test(), run(), and micro_os_plus::micro_test_plus::suite::test().

◆ test()

template<typename Callable_T, typename... Args_T>
requires detail::test_body<Callable_T, Args_T...>
void micro_os_plus::micro_test_plus::test_case::test ( const located_name & name,
Callable_T && callable,
Args_T &&... arguments )

Valid only while this test case is the innermost running one, for example from its body, or from the body of a nested test case that does not use the parameter of the outer one. When called on this test case while another one runs (an outer test case captured by a nested body), the nested test case would be reported inside the other one, with the numbering of this one; it is therefore not run, and a failed check is reported in the running test case.

Template Parameters
Callable_TThe type of a callable object.
Args_TThe types of the callable arguments.
Parameters
nameThe test case name or description, used in reports; a string literal, a const char*, a std::string, or a std::string_view, converted implicitly to located_name, which copies it and also captures the location of the call.
callableA generic callable object, usually a lambda, invoked to perform the test.
argumentsA possibly empty list of arguments to be passed to the callable.
Returns
Nothing.

Creates and runs the child test case with create_test_case(), in the parent suite of this test case, one level of nesting deeper than this test case. When another test case is running, the call is rejected with report_misdirected_test(), and the child is not run.

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

566 {
567#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED)
568 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name.name ());
569#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED
570
571 if (is_misdirected ()) [[unlikely]]
572 {
574 return;
575 }
576
578 std::forward<Callable_T> (callable),
579 std::forward<Args_T> (arguments)...);
580 }
void create_test_case(const located_name &name, suite &parent_suite, size_t nesting_depth, Callable_T &&callable, Args_T &&... arguments)
Creates a child test case, and runs it.
bool is_misdirected(void) const noexcept
Tells whether another test case is running.
void report_misdirected_test(const located_name &name)
Reports a nested test case started on this test case as a failure of the running one.
Definition test.cpp:472

References micro_os_plus::micro_test_plus::detail::runnable_base::create_test_case(), is_misdirected(), micro_os_plus::micro_test_plus::detail::runnable_base::name(), micro_os_plus::micro_test_plus::located_name::name(), nesting_depth_, parent_suite_, and report_misdirected_test().

Referenced by operator=().

◆ test_each() [1/2]

template<typename T, typename Callable_T>
void micro_os_plus::micro_test_plus::test_case::test_each ( const located_name & name,
std::initializer_list< T > values,
Callable_T && callable )

As the overload for the ranges, for a list written in place, like ts.test_each ("Sizes", { 1, 16, 512 }, ...), whose type cannot be deduced otherwise.

Template Parameters
TThe type of the values.
Callable_TThe type of a callable object.
Parameters
nameThe common name of the test cases.
valuesThe list of values.
callableA generic callable object, usually a lambda, invoked with the test case and each value.
Returns
Nothing.
Since
6.0.0

The list is iterated as a range.

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

1109 {
1110 detail::test_each_value (*this, name, values, callable);
1111 }
void test_each_value(Self_T &self, const located_name &name, Values_T &&values, Callable_T &callable)
Adds and runs a test case for each value.

References micro_os_plus::micro_test_plus::detail::runnable_base::name(), and micro_os_plus::micro_test_plus::detail::test_each_value().

◆ test_each() [2/2]

template<typename Values_T, typename Callable_T>
requires (detail::test_values_range<Values_T> or detail::test_values_tuple<Values_T>)
void micro_os_plus::micro_test_plus::test_case::test_each ( const located_name & name,
Values_T && values,
Callable_T && callable )

For each value, in order, a test case is added and run, as by test(), with the value as the argument of the callable, which is called as callable (test_case, value). The values may be a range (a container, an array, a span, or a view), or a tuple, whose values may have different types, for a generic callable:

ts.test_each ("Sizes", std::vector<int>{ 1, 16, 512 },
[] (auto& t, int size) { t.expect (mt::gt (size, 0)); });
ts.test_each ("Types", std::tuple{ 42, 1.5, "text" },
[] (auto& t, const auto& value) {
t.expect (mt::eq (value, value));
});

Each test case is named after the common name, followed by the value in parentheses, written as in the messages of the checks (Sizes (16)); a value that cannot be written (see extension::printable), such as an application type without an operator<<, is replaced by its position, starting at 1 (Records (#2)). The location of all the test cases is that of the call.

Template Parameters
Values_TThe type of the range or tuple of values.
Callable_TThe type of a callable object.
Parameters
nameThe common name of the test cases; a string literal, a const char*, a std::string, or a std::string_view, converted implicitly to located_name, which also captures the location of the call.
valuesThe range or tuple of values.
callableA generic callable object, usually a lambda, invoked with the test case and each value.
Returns
Nothing.
Since
6.0.0

Forwards to detail::test_each_value(), which adds the test cases with test().

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

1096 {
1097 detail::test_each_value (*this, name, std::forward<Values_T> (values),
1098 callable);
1099 }

References micro_os_plus::micro_test_plus::detail::runnable_base::name(), test_each(), and micro_os_plus::micro_test_plus::detail::test_each_value().

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

◆ test_reporter()

reporter & micro_os_plus::micro_test_plus::detail::runnable_base::test_reporter ( void ) const
nodiscardprotectednoexceptinherited

Protected, since the reporter is internal to the framework; it is used by the suites and the test cases, which derive from this class, and by the check result, which is a friend.

Not named reporter(), which would hide the class detail::reporter in the derived classes.

Parameters
None.
Returns
A reference to the test reporter.

Returns the reporter owned by the runner, through its reporter_ pointer, which is valid after runner::initialise().

Definition at line 155 of file test.cpp.

156 {
157 return *runner_.reporter_;
158 }
std::unique_ptr< detail::reporter > reporter_
Owning pointer to the test reporter used for outputting test results.
Definition runner.h:1275

References runner_.

Referenced by micro_os_plus::micro_test_plus::detail::check_result::~check_result(), accepts_test_case(), after_test_case_create(), micro_os_plus::micro_test_plus::test_case::expect(), micro_os_plus::micro_test_plus::suite::fail_unexpected_exception(), micro_os_plus::micro_test_plus::test_case::fail_unexpected_exception(), micro_os_plus::micro_test_plus::test_case::report_misdirected(), micro_os_plus::micro_test_plus::test_case::report_rejected_test(), micro_os_plus::micro_test_plus::test_case::require(), run(), micro_os_plus::micro_test_plus::suite::run(), and micro_os_plus::micro_test_plus::test_case::run().

◆ test_runner()

runner & micro_os_plus::micro_test_plus::detail::runnable_base::test_runner ( void ) const
inlinenodiscardprotectednoexceptinherited

Protected, since the runner is internal to the framework; it is used by the suites and the test cases, which derive from this class and pass it to their nested test cases, and by the runner, which is a friend and selects the static suites it runs.

Not named runner(), which would hide the class runner in the derived classes.

Parameters
None.
Returns
A reference to the test runner.

Returns a reference to the owning test runner.

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

266 {
267 return runner_;
268 }

References runner_.

Referenced by runnable_base(), micro_os_plus::micro_test_plus::static_suite::static_suite(), micro_os_plus::micro_test_plus::suite::suite(), micro_os_plus::micro_test_plus::test_case::test_case(), micro_os_plus::micro_test_plus::top_suite::top_suite(), micro_os_plus::micro_test_plus::test_case::detail::runnable_base, micro_os_plus::micro_test_plus::static_suite::link_or_reject(), micro_os_plus::micro_test_plus::top_suite::micro_test_plus::runner, reporter_tap, run(), and micro_os_plus::micro_test_plus::suite::static_suite.

◆ totals()

const counters & micro_os_plus::micro_test_plus::detail::runnable_base::totals ( void ) const
inlinenodiscardnoexceptinherited

The reference is read-only, for the const and the non-const nodes alike, so that a test case or a suite cannot modify its own counters, which decide the exit code. The counters are updated only by the framework, which accesses the protected member directly.

Parameters
None.
Returns
A const reference to the counters instance.

Returns a const reference to the counters member.

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

206 {
207 return totals_;
208 }

References totals_.

Referenced by after_test_case_create(), micro_os_plus::micro_test_plus::detail::reporter_human::end_suite(), micro_os_plus::micro_test_plus::detail::reporter_tap::end_suite(), micro_os_plus::micro_test_plus::detail::reporter_human::end_test_case(), micro_os_plus::micro_test_plus::detail::reporter_tap::end_test_case(), micro_os_plus::micro_test_plus::detail::reporter::format_suite_totals(), micro_os_plus::micro_test_plus::detail::reporter::format_test_case_totals(), operator=(), and micro_os_plus::micro_test_plus::runner::run_suites().

◆ detail::runnable_base

friend class detail::runnable_base
friend

◆ micro_test_plus::runner

friend class micro_test_plus::runner
friend

runner::reject_call() calls the private report_rejected_test(), at the location of the rejected call, if known, or otherwise at the location of the test case, which it reads from the private located_name_.

Definition at line 1373 of file test.h.

Referenced by test_case(), and detail::runnable_base.

Member Data Documentation

◆ callable_

detail::callable_ref<test_case> micro_os_plus::micro_test_plus::test_case::callable_
private

Invoked by run() with a reference to this test case. The body is a local object of runnable_base::create_test_case(), which outlives the test case.

Definition at line 1985 of file test.h.

Referenced by test_case(), and run().

◆ child_test_cases_count_

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

Incremented for each call to test(), which creates a child test_case and runs it immediately. The child test cases are not kept after they run, so that the memory does not grow with the number of test cases; only their number is needed later, for the TAP plan.

Definition at line 883 of file test.h.

Referenced by after_test_case_create(), children_subtests_count(), micro_os_plus::micro_test_plus::detail::reporter_tap::end_suite(), and micro_os_plus::micro_test_plus::detail::reporter_tap::end_test_case().

◆ located_name_

const located_name& micro_os_plus::micro_test_plus::test_case::located_name_
private

The argument of test(), which lives until the end of that call, after the test case ends. The name of the node points into it; the location is used to report the failures that are not associated with a check, such as an exception escaping the body, and the calls rejected while the test case runs.

Definition at line 1998 of file test.h.

Referenced by test_case(), fail_unexpected_exception(), and micro_os_plus::micro_test_plus::runner::reject_call().

◆ name_

◆ nesting_depth_

size_t micro_os_plus::micro_test_plus::test_case::nesting_depth_
private

1 for the direct children of a suite; used for indentation.

Definition at line 2015 of file test.h.

Referenced by test_case(), nesting_depth(), and test().

◆ own_index_

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

The position of this object within its parent, displayed by the TAP reporter in the ok/not ok lines. Set directly by the runner, which numbers the suites when it runs them, and by the top suite, which, when added, is always the first one.

Definition at line 859 of file test.h.

Referenced by runnable_base(), micro_os_plus::micro_test_plus::suite::suite(), micro_os_plus::micro_test_plus::test_case::test_case(), micro_os_plus::micro_test_plus::top_suite::top_suite(), micro_os_plus::micro_test_plus::detail::reporter_tap::end_suite(), micro_os_plus::micro_test_plus::detail::reporter_tap::end_test_case(), own_index(), micro_os_plus::micro_test_plus::detail::reporter_tap::skip_suite(), and micro_os_plus::micro_test_plus::detail::reporter_tap::skip_test_case().

◆ parent_suite_

suite& micro_os_plus::micro_test_plus::test_case::parent_suite_
private

The suite into which the totals of the nested test cases are accumulated.

Definition at line 2007 of file test.h.

Referenced by test_case(), micro_os_plus::micro_test_plus::detail::runnable_base::has_running_test_case_of(), and test().

◆ runner_

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

Provides access to the reporter, to the filter, and to the state of the session (the running test case, and the abort and listing flags), and is used when aborting.

Definition at line 848 of file test.h.

Referenced by runnable_base(), abort(), accepts_test_case(), after_test_case_create(), create_test_case(), exchange_running_suite(), has_running_test_case_of(), is_aborted(), is_listing(), reject_body_exception(), running_test_case(), test_reporter(), and test_runner().

◆ test_point_number_

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

Test points are numbered from 1; the value is 0 before the first one. This number counts the checks and the nested test cases of this object, in execution order, as required by the TAP ok/not ok lines. It is incremented for each check (by the check result) and for each nested test case created.

Definition at line 871 of file test.h.

Referenced by current_subtest_index(), micro_os_plus::micro_test_plus::detail::reporter_tap::end_suite(), micro_os_plus::micro_test_plus::detail::reporter_tap::fail_suite(), next_test_point_number(), micro_os_plus::micro_test_plus::detail::reporter_tap::output_fail_prefix(), micro_os_plus::micro_test_plus::detail::reporter_tap::output_pass_prefix(), and micro_os_plus::micro_test_plus::detail::reporter_tap::pass_without_text().

◆ totals_

counters micro_os_plus::micro_test_plus::detail::runnable_base::totals_
protectedinherited

For the suites, the totals include the checks of all their test cases; for a test case, they include only its own checks, plus the counts of its direct nested test cases.

The check counters are updated by the check result, which is a friend; the test case counters by after_test_case_create(), when a nested test case ends. For a suite, the totals of each of its test cases are also added, by the same function.

Definition at line 838 of file test.h.

Referenced by micro_os_plus::micro_test_plus::detail::check_result::~check_result(), accepts_test_case(), after_test_case_create(), micro_os_plus::micro_test_plus::suite::fail_unexpected_exception(), micro_os_plus::micro_test_plus::test_case::fail_unexpected_exception(), micro_os_plus::micro_test_plus::runner::reject_call(), and totals().


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