micro-test-plus 3.2.0
µTest++, a lightweight testing framework for embedded platforms
Loading...
Searching...
No Matches
inlines.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus/)
4 * Copyright (c) 2021 Liviu Ionescu.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from <https://opensource.org/licenses/MIT/>.
11 *
12 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
13 * released under the terms of the Boost Version 1.0 Software License,
14 * which can be obtained from <https://www.boost.org/LICENSE_1_0.txt>.
15 */
16
17#ifndef MICRO_TEST_PLUS_INLINES_H_
18#define MICRO_TEST_PLUS_INLINES_H_
19
20// ----------------------------------------------------------------------------
21
22#ifdef __cplusplus
23
24// ----------------------------------------------------------------------------
25
26#if defined(__GNUC__)
27#pragma GCC diagnostic push
28#pragma GCC diagnostic ignored "-Waggregate-return"
29#pragma GCC diagnostic ignored "-Wpadded"
30#if defined(__clang__)
31#pragma clang diagnostic ignored "-Wc++98-compat"
32#pragma clang diagnostic ignored "-Wc++98-c++11-c++14-c++17-compat-pedantic"
33#endif
34#endif
35
37{
38 // --------------------------------------------------------------------------
39
40 template <typename Callable_T, typename... Args_T>
41 test_suite::test_suite (const char* name, Callable_T&& callable,
42 Args_T&&... arguments)
43 : test_suite_base{ name },
44 callable_{ std::bind (callable, arguments...) }
45 {
46#if defined(MICRO_TEST_PLUS_TRACE)
47 printf ("%s\n", __PRETTY_FUNCTION__);
48#endif // MICRO_TEST_PLUS_TRACE
49
51 }
52
53 // --------------------------------------------------------------------------
54
85 template <typename Callable_T, typename... Args_T>
86 void
87 test_case (const char* name, Callable_T&& callable, Args_T&&... arguments)
88 {
89#if 0 // defined(MICRO_TEST_PLUS_TRACE)
90 printf ("%s\n", __PRETTY_FUNCTION__);
91#endif // MICRO_TEST_PLUS_TRACE
92
94 std::invoke (std::forward<Callable_T> (callable),
95 std::forward<Args_T> (arguments)...);
97 }
98
99 // --------------------------------------------------------------------------
100 namespace detail
101 {
102 // ------------------------------------------------------------------------
103
104 template <class T>
105 auto&
107 {
108 if constexpr (std::is_arithmetic_v<T>)
109 {
110 message_.append (std::to_string (msg));
111 }
112 else
113 {
114 message_.append (msg);
115 }
116 return *this;
117 }
118
119 // ------------------------------------------------------------------------
120
121 template <class Expr_T>
123 const Expr_T& expr, bool abort,
124 const reflection::source_location& location)
125 : deferred_reporter_base{ static_cast<bool> (expr), location },
126 expr_{ expr }
127 {
128#if 0 // defined(MICRO_TEST_PLUS_TRACE)
129 printf ("%s\n", __PRETTY_FUNCTION__);
130#endif // MICRO_TEST_PLUS_TRACE
131 abort_ = abort;
132 }
133
134 template <class Expr_T>
136 {
137 if (value_)
138 {
139 reporter.pass (expr_, message_);
140 }
141 else
142 {
143 reporter.fail (expr_, abort_, message_, location_);
144 }
145 }
146
147 // ------------------------------------------------------------------------
148 } // namespace detail
149
150 // --------------------------------------------------------------------------
151 namespace utility
152 {
167 template <class T = std::string_view, class Delim_T>
168 [[nodiscard]] auto
169 split (T input, Delim_T delim) -> std::vector<T>
170 {
171 std::vector<T> output{};
172 std::size_t first{};
173 while (first < std::size (input))
174 {
175 const auto second = input.find_first_of (delim, first);
176 if (first != second)
177 {
178 output.emplace_back (input.substr (first, second - first));
179 }
180 if (second == T::npos)
181 {
182 break;
183 }
184 first = second + 1;
185 }
186 return output;
187 }
188 } // namespace utility
189
190} // namespace micro_os_plus::micro_test_plus
191
192#if defined(__GNUC__)
193#pragma GCC diagnostic pop
194#endif
195
196// ----------------------------------------------------------------------------
197
198#endif // __cplusplus
199
200// ----------------------------------------------------------------------------
201
202#endif // MICRO_TEST_PLUS_INLINES_H_
203
204// ----------------------------------------------------------------------------
Base class for a deferred reporter, that collects the messages into a string.
Definition detail.h:700
std::string message_
String to collect the expectation message passed via operator<<().
Definition detail.h:726
constexpr deferred_reporter(const Expr_T &expr, bool abort, const reflection::source_location &location)
Definition inlines.h:122
Local implementation of the std::source_location.
Definition reflection.h:58
void pass(Expr_T &expr, std::string &message)
Report a passed condition.
void fail(Expr_T &expr, bool abort, std::string &message, const reflection::source_location &location)
Report a failed condition.
void register_test_suite(test_suite_base *suite)
Called by test suite constructors to register them to the runner.
Base class for all test suites.
Definition test-suite.h:51
void begin_test_case(const char *name)
Mark the beginning of a named test case.
void end_test_case(void)
Mark the end of a test case.
test_suite(const char *name, Callable_T &&callable, Args_T &&... arguments)
Construct a test suite.
Definition inlines.h:41
void test_case(const char *name, Callable_T &&callable, Args_T &&... arguments)
Define and execute a test case.
Definition inlines.h:87
auto split(T input, Delim_T delim) -> std::vector< T >
Split a string into a vector of sub-strings.
Definition inlines.h:169