C++ header file with inline implementations for the µTest++ test hierarchy. More...
Namespaces Index
Functions Index
template <typename Self_T, typename Callable_T, typename... Args_T> |
| auto | bind_callable (Callable_T &&callable, Args_T &&... arguments) |
|
Creates the function object that holds the body of a test case or suite. More...
|
|
template <typename Callable_T, typename... Args_T> |
| constexpr void | reject_suite_body (void) noexcept |
|
Stops the compilation with a message that tells what the body of a suite takes. More...
|
|
template <typename Callable_T, typename... Args_T> |
| constexpr void | reject_test_body (void) noexcept |
|
Stops the compilation with a message that tells what the body of a test case takes. More...
|
|
template <class Self_T, class Values_T, class Callable_T> |
| 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. More...
|
|
Description
C++ header file with inline implementations for the µTest++ test hierarchy.
This header provides the inline and template implementations for the test hierarchy used within the µTest++ framework: the accessors of the base classes, the constructors that bind the callable objects and their arguments, the test() member functions that create and run test cases, and the expect() and require() checks.
When additional arguments are given, a detail::bound_callable holds copies of the callable and of the arguments. The suites store this object in a std::function, while the test cases only refer to it, through a detail::callable_ref, as a local object of the test() call that runs them. A static_suite additionally inserts itself into the list of suites upon construction.
All definitions reside within the micro_os_plus::micro_test_plus namespace.
This file is included by test.h; the applications include micro-os-plus/micro-test-plus.h, not this file.
Functions
bind_callable()
template <typename Self_T, typename Callable_T, typename... Args_T>
| auto micro_os_plus::micro_test_plus::detail::bind_callable (Callable_T && callable, Args_T &&... arguments) |
|
nodiscard
|
Creates the function object that holds the body of a test case or suite.
Without additional arguments, the callable already has the signature void (Self_T&), and a copy of it is returned, without a wrapper.
Otherwise, a bound_callable stores copies of the callable and of the arguments (decayed, as by auto), since a suite may run after the caller's temporaries are gone; the callable receives the copies as non-const lvalues. A reference parameter thus refers to the copy; to refer to the caller's object, pass a pointer, or std::ref(), which converts to a T& parameter (an auto& parameter receives the std::reference_wrapper itself).
The returned object holds a copy of the callable (decayed, as by auto) when there are no additional arguments; otherwise it is a bound_callable that holds copies of the callable and of the arguments, and passes the arguments after the leading reference, which is passed when the body is invoked. No memory is allocated, other than by the copies themselves.
A suite stores the object in a std::function, since it runs later; a test case, which runs immediately, refers to it with a callable_ref.
- Template Parameters
-
| Self_T | The type referred to by the first parameter of the callable (test_case or suite). |
| Callable_T | The type of the callable object. |
| Args_T | The types of the additional callable arguments. |
- Parameters
-
| callable | A generic callable object, usually a lambda. |
| arguments | A possibly empty list of arguments forwarded to the callable after the leading Self_T& reference. |
- Returns
The function object, invocable with a Self_T& argument.
Definition at line 344 of file test-inlines.h.
345 {
346 if constexpr (sizeof...(arguments) == 0)
347 {
348 return std::forward<Callable_T> (callable);
349 }
350 else
351 {
353 std::decay_t<Args_T>...>{
354 std::forward<Callable_T> (callable),
355 std::forward<Args_T> (arguments)...
356 };
357 }
358 }
Reference micro_os_plus::micro_test_plus::detail::bind_callable.
Referenced by micro_os_plus::micro_test_plus::detail::bind_callable and micro_os_plus::micro_test_plus::detail::runnable_base::create_test_case.
reject_suite_body()
template <typename Callable_T, typename... Args_T>
| void micro_os_plus::micro_test_plus::detail::reject_suite_body (void) |
|
constexpr
noexcept
|
Stops the compilation with a message that tells what the body of a suite takes.
Always fails the static assertion that applies, since it is called only when suite_body is not satisfied. A body that cannot be copied, or a body written for a test case, is recognised, to say so.
Called by the overloads of runner::add_suite() and of the static_suite constructor that are selected when the callable is not a valid body, as reject_test_body() is for the test cases.
- Template Parameters
-
| Callable_T | The type of the callable. |
| Args_T | The types of the additional arguments. |
- Parameters
None.
- Returns
Nothing.
Definition at line 448 of file test-inlines.h.
449 {
450 if constexpr (not std::is_copy_constructible_v<std::decay_t<Callable_T>>
451 or not(std::is_copy_constructible_v<std::decay_t<Args_T>>
452 and ...))
453 {
455 "the body of a suite and its additional arguments "
456 "must be copyable, since the suite runs later");
457 }
458 else if constexpr (std::is_invocable_v<std::decay_t<Callable_T>&,
460 std::decay_t<Args_T>&...>)
461 {
463 "the body of a suite takes a suite& as its first "
464 "parameter, not a test_case&");
465 }
466 else
467 {
469 "the body of a suite takes a suite& as its first "
470 "parameter, followed by the additional arguments");
471 }
472 }
References micro_os_plus::micro_test_plus::detail::dependent_false_v and micro_os_plus::micro_test_plus::detail::reject_suite_body.
Referenced by micro_os_plus::micro_test_plus::runner::add_suite and micro_os_plus::micro_test_plus::detail::reject_suite_body.
reject_test_body()
template <typename Callable_T, typename... Args_T>
| void micro_os_plus::micro_test_plus::detail::reject_test_body (void) |
|
constexpr
noexcept
|
Stops the compilation with a message that tells what the body of a test case takes.
Always fails the static assertion that applies, since it is called only when test_body is not satisfied. A body written for a suite is recognised, to say so.
Called by the overloads of test_case::test() and suite::test() that are selected when the callable is not a valid body, so that the diagnostic names the mistake in the terms of the application, instead of the concepts and the instantiation stack. Never called at run time.
- Template Parameters
-
| Callable_T | The type of the callable. |
| Args_T | The types of the additional arguments. |
- Parameters
None.
- Returns
Nothing.
Definition at line 422 of file test-inlines.h.
423 {
424 if constexpr (std::is_invocable_v<std::decay_t<Callable_T>&, suite&,
425 std::decay_t<Args_T>&...>)
426 {
428 "the body of a test case takes a test_case& as its "
429 "first parameter, not a suite&");
430 }
431 else
432 {
434 "the body of a test case takes a test_case& as its "
435 "first parameter, followed by the additional "
436 "arguments passed to test ()");
437 }
438 }
References micro_os_plus::micro_test_plus::detail::dependent_false_v and micro_os_plus::micro_test_plus::detail::reject_test_body.
Referenced by micro_os_plus::micro_test_plus::detail::reject_test_body.
test_each_value()
template <class Self_T, class Values_T, class Callable_T>
| void micro_os_plus::micro_test_plus::detail::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.
For each value, the name is built in a local string, which lives while the test case runs, since a test case runs within the call to test(), and the test case is added with test(), with the location of the call to test_each(). The callable receives each value as a const lvalue, referring to the element itself; neither the values nor the callable are copied. The values of a range are iterated by a range-based for statement, and those of a tuple are expanded by std::apply(), in order.
The common implementation of test_case::test_each() and suite::test_each().
- Template Parameters
-
| Self_T | The type of the test case or suite. |
| Values_T | The type of the range or tuple of values. |
| Callable_T | The type of the callable object. |
- Parameters
-
| self | The test case or suite to which the test cases are added. |
| name | The common name of the test cases. |
| values | The range or tuple of values. |
| callable | The callable object, invoked with the test case and each value. |
- Returns
Nothing.
Definition at line 1029 of file test-inlines.h.
1030 Callable_T& callable)
1031 {
1032 std::size_t position = 0;
1033
1034 const auto run_one
1035 = [&self, &name, &callable, &position] (const auto& value)
1036 {
1037 ++position;
1038
1039 std::string text{ name.name () };
1040 text += " (";
1042 std::remove_cvref_t<decltype (value)>>)
1043 {
1045 }
1046 else
1047 {
1048 text += '#';
1050 }
1051 text += ')';
1052
1053
1054
1056 [&callable, &value] (auto& test_self)
1057 {
1058 if constexpr (std::is_member_pointer_v<
1059 std::remove_cvref_t<Callable_T>>)
1060 {
1061 std::invoke (callable, test_self, value);
1062 }
1063 else
1064 {
1065 callable (test_self, value);
1066 }
1067 });
1069
1071 {
1072 for (const auto& value : values)
1073 {
1075 }
1076 }
1077 else
1078 {
1079 std::apply ([&run_one] (const auto&... elements)
1080 { (run_one (elements), ...); }, values);
1081 }
1082 }
References micro_os_plus::micro_test_plus::detail::plain_text::append, micro_os_plus::micro_test_plus::located_name::location, micro_os_plus::micro_test_plus::located_name::name and micro_os_plus::micro_test_plus::detail::test_each_value.
Referenced by 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::test_each_value.
File Listing
The file content with the documentation metadata removed is:
44#ifndef MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_TEST_INLINES_H_
45#define MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_TEST_INLINES_H_
49#if defined(__cplusplus)
57#if __has_include("micro-os-plus/diag/trace.h")
58#include "micro-os-plus/diag/trace.h"
68#pragma GCC diagnostic push
70#pragma GCC diagnostic ignored "-Waggregate-return"
71#pragma GCC diagnostic ignored "-Wformat"
72#pragma GCC diagnostic ignored "-Wformat-extra-args"
73#pragma GCC diagnostic ignored "-Wpadded"
75#pragma clang diagnostic ignored "-Wunknown-warning-option"
76#pragma clang diagnostic ignored "-Wc++98-compat"
77#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
78#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
80#pragma GCC diagnostic ignored "-Wredundant-tags"
97 template <typename Pointer_T>
98 requires (std::is_same_v<Pointer_T, const char*>
99 or std::is_same_v<Pointer_T, char*>
100 or std::is_null_pointer_v<Pointer_T>)
103 : name_{ name != nullptr ? name : "" }, location_{ location }
115 template <std::size_t N>
118 : name_{ std::string_view{ name, N }.substr (
119 0, std::string_view{ name, N }.find ('\0')) },
171 return name_.c_str ();
210#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
290 template <typename Callable_T, typename... Args_T>
293 suite& parent_suite, size_t nesting_depth,
294 Callable_T&& callable,
295 Args_T&&... arguments)
299 if constexpr (sizeof...(arguments) == 0
300 && !std::is_function_v<
301 std::remove_reference_t<Callable_T>>)
316 std::forward<Callable_T> (callable),
317 std::forward<Args_T> (arguments)...);
342 template <typename Self_T, typename Callable_T, typename... Args_T>
346 if constexpr (sizeof...(arguments) == 0)
348 return std::forward<Callable_T> (callable);
353 std::decay_t<Args_T>...>{
354 std::forward<Callable_T> (callable),
355 std::forward<Args_T> (arguments)...
367 template <typename Self_T, typename Callable_T, typename... Args_T>
368 template <typename C, typename... A>
369 requires (sizeof...(A) > 0)
371 C&& callable, A&&... arguments)
372 : callable_ (std::forward<C> (callable)),
381 template <typename Self_T, typename Callable_T, typename... Args_T>
385 invoke (self, std::index_sequence_for<Args_T...>{});
396 template <typename Self_T, typename Callable_T, typename... Args_T>
397 template <std::size_t... Is>
400 Self_T& self, std::index_sequence<Is...>)
402 if constexpr (std::is_member_pointer_v<Callable_T>)
420 template <typename Callable_T, typename... Args_T>
424 if constexpr (std::is_invocable_v<std::decay_t<Callable_T>&, suite&,
425 std::decay_t<Args_T>&...>)
428 "the body of a test case takes a test_case& as its "
429 "first parameter, not a suite&");
434 "the body of a test case takes a test_case& as its "
435 "first parameter, followed by the additional "
436 "arguments passed to test ()");
446 template <typename Callable_T, typename... Args_T>
450 if constexpr (not std::is_copy_constructible_v<std::decay_t<Callable_T>>
451 or not(std::is_copy_constructible_v<std::decay_t<Args_T>>
455 "the body of a suite and its additional arguments "
456 "must be copyable, since the suite runs later");
458 else if constexpr (std::is_invocable_v<std::decay_t<Callable_T>&,
460 std::decay_t<Args_T>&...>)
463 "the body of a suite takes a suite& as its first "
464 "parameter, not a test_case&");
469 "the body of a suite takes a suite& as its first "
470 "parameter, followed by the additional arguments");
479 template <typename Self_T>
480 template <typename Callable_T>
481 requires (!std::is_same_v<std::remove_cv_t<Callable_T>,
482 callable_ref<Self_T>>)
485 static_cast<const volatile void*> (std::addressof (callable))) },
486 invoke_{ &invoke_callable<Callable_T> }
494 template <typename Self_T>
512 template <typename Self_T>
513 template <typename Callable_T>
517 if constexpr (std::is_member_pointer_v<Callable_T>)
519 std::invoke (*static_cast<Callable_T*> (callable), self);
523 (*static_cast<Callable_T*> (callable)) (self);
547#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
561 template <typename Callable_T, typename... Args_T>
565 Args_T&&... arguments)
567#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED)
568 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name.name ());
578 std::forward<Callable_T> (callable),
579 std::forward<Args_T> (arguments)...);
582#if !defined(__DOXYGEN__)
588 template <typename Callable_T, typename... Args_T>
592 [[maybe_unused]] Callable_T&& callable,
593 [[maybe_unused]] Args_T&&... arguments)
608 return running != nullptr && running != this;
620 template <class Expr_T>
635 return std::move (result);
638#if !defined(__DOXYGEN__)
645 template <class Expr_T>
655 "expect() does not take a wrapper of a bool, whose "
656 "condition tests the wrapper, not the value; write "
657 "has_value () or != nullptr, or use eq ()");
661 static_assert (extension::checkable<Expr_T>,
662 "expect() takes a Boolean condition or a comparison, "
663 "such as eq (); a value of this type is neither");
665 return std::move (result);
680 template <class Expr_T>
696 return std::move (result);
699#if !defined(__DOXYGEN__)
707 template <class Expr_T>
717 "require() does not take a wrapper of a bool, whose "
718 "condition tests the wrapper, not the value; write "
719 "has_value () or != nullptr, or use eq ()");
723 static_assert (extension::checkable<Expr_T>,
724 "require() takes a Boolean condition or a comparison, "
725 "such as eq (); a value of this type is neither");
727 return std::move (result);
731#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
732#if !defined(__DOXYGEN__)
742 template <class Expr_T>
751 return std::move (result);
759 template <class Expr_T>
769 return std::move (result);
780 template <class Expr_T>
786 return require (expr, sl, std::move (result));
829#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED)
830 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
839#if defined(__cpp_exceptions)
850 catch (const std::exception& e)
891 template <typename Callable_T, typename... Args_T>
893 Callable_T&& callable, Args_T&&... arguments)
896 std::forward<Callable_T> (callable),
897 std::forward<Args_T> (arguments)...) },
902#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
909#pragma GCC diagnostic push
911#if defined(__clang__)
912#pragma clang diagnostic ignored "-Wdocumentation"
965 template <typename Callable_T, typename... Args_T>
969 Args_T&&... arguments)
971#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED)
972 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name.name ());
981 running->test (name, std::forward<Callable_T> (callable),
982 std::forward<Args_T> (arguments)...);
992 std::forward<Args_T> (arguments)...);
995#if !defined(__DOXYGEN__)
1001 template <typename Callable_T, typename... Args_T>
1005 [[maybe_unused]] Callable_T&& callable,
1006 [[maybe_unused]] Args_T&&... arguments)
1027 template <class Self_T, class Values_T, class Callable_T>
1030 Callable_T& callable)
1032 std::size_t position = 0;
1035 = [&self, &name, &callable, &position] (const auto& value)
1039 std::string text{ name.name () };
1042 std::remove_cvref_t<decltype (value)>>)
1056 [&callable, &value] (auto& test_self)
1058 if constexpr (std::is_member_pointer_v<
1059 std::remove_cvref_t<Callable_T>>)
1061 std::invoke (callable, test_self, value);
1065 callable (test_self, value);
1072 for (const auto& value : values)
1079 std::apply ([&run_one] (const auto&... elements)
1080 { (run_one (elements), ...); }, values);
1090 template <typename Values_T, typename Callable_T>
1095 Callable_T&& callable)
1105 template <typename T, typename Callable_T>
1108 std::initializer_list<T> values, Callable_T&& callable)
1118 template <typename Values_T, typename Callable_T>
1123 Callable_T&& callable)
1133 template <typename T, typename Callable_T>
1136 Callable_T&& callable)
1141#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
1177 template <typename Callable_T, typename... Args_T>
1181 Callable_T&& callable, Args_T&&... arguments)
1183 std::forward<Callable_T> (callable),
1184 std::forward<Args_T> (arguments)... }
1186#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
1188 __PRETTY_FUNCTION__, name_, own_index_);
1194#if !defined(__DOXYGEN__)
1202 template <typename Callable_T, typename... Args_T>
1203 requires (!detail::static_suite_body<Callable_T, Args_T...>
1204 && !detail::suite_body<Callable_T, Args_T...>)
1206 [[maybe_unused]] Callable_T&& callable,
1207 [[maybe_unused]] Args_T&&... arguments)
1208 : suite{ std::move (name), test_runner, [] (suite&) noexcept {} }
1214#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
1226 template <typename Callable_T, typename... Args_T>
1229 Callable_T&& callable, Args_T&&... arguments)
1230 : suite{ std::move (name), test_runner,
1231 [stored_callable = std::forward<Callable_T> (callable)] (
1232 suite& self, auto&... stored_arguments) mutable
1234 stored_callable (static_cast<static_suite&> (self), stored_arguments...);
1236 std::forward<Args_T> (arguments)... }
1238#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
1240 __PRETTY_FUNCTION__, name_, own_index_);
1252#if defined(__GNUC__)
1253#pragma GCC diagnostic pop
1254#pragma GCC diagnostic pop
Generated via doxygen2docusaurus 2.2.8 by Doxygen 1.18.0.