micro-test-plus 3.2.0
µTest++, a lightweight testing framework for embedded platforms
Loading...
Searching...
No Matches
micro-test-plus.cpp
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. All rights reserved.
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// ----------------------------------------------------------------------------
18
19#if defined(MICRO_OS_PLUS_INCLUDE_CONFIG_H)
20#include <micro-os-plus/config.h>
21#endif // MICRO_OS_PLUS_INCLUDE_CONFIG_H
22
24#include <cstring>
25// <iostream> is too heavy for embedded, use printf().
26#include <stdio.h>
27#include <unistd.h>
28
29// ----------------------------------------------------------------------------
30
31#pragma GCC diagnostic ignored "-Waggregate-return"
32#if defined(__clang__)
33#pragma clang diagnostic ignored "-Wc++98-compat"
34#pragma clang diagnostic ignored "-Wexit-time-destructors"
35#pragma clang diagnostic ignored "-Wglobal-constructors"
36#pragma clang diagnostic ignored "-Wunknown-warning-option"
37#endif
38
40{
41 // --------------------------------------------------------------------------
42 // Public API.
43
56 void
57 initialize (int argc, char* argv[], const char* name)
58 {
59#if defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
60 printf ("%s\n", __PRETTY_FUNCTION__);
61#endif
62 runner.initialize (argc, argv, name);
63 }
64
77 int
78 exit_code (void)
79 {
80 return runner.exit_code ();
81 }
82
83 // --------------------------------------------------------------------------
84 // Too small to deserve a separate source file.
85 namespace reflection
86 {
87
88 const char*
89 short_name (const char* name)
90 {
91#pragma GCC diagnostic push
92#if defined(__clang__)
93#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
94#endif
95 const char* p = strrchr (name, '/');
96 if (p != nullptr)
97 return p + 1;
98 else
99 return name;
100#pragma GCC diagnostic pop
101 }
102
103 } // namespace reflection
104
105 namespace utility
106 {
122 [[nodiscard]] bool
123 is_match (std::string_view input, std::string_view pattern)
124 {
125 if (std::empty (pattern))
126 {
127 return std::empty (input);
128 }
129
130 if (std::empty (input))
131 {
132 return pattern[0] == '*' ? is_match (input, pattern.substr (1))
133 : false;
134 }
135
136 if (pattern[0] != '?' and pattern[0] != '*' and pattern[0] != input[0])
137 {
138 return false;
139 }
140
141 if (pattern[0] == '*')
142 {
143 for (decltype (std::size (input)) i = 0u; i <= std::size (input);
144 ++i)
145 {
146 if (is_match (input.substr (i), pattern.substr (1)))
147 {
148 return true;
149 }
150 }
151 return false;
152 }
153
154 return is_match (input.substr (1), pattern.substr (1));
155 }
156
157 } // namespace utility
158
159 namespace detail
160 {
162 bool value, const reflection::source_location location)
163 : value_{ value }, location_{ location }
164 {
165 if (value_)
166 {
168 }
169 else
170 {
172 }
173 }
174
176 {
177#if 0 // defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
178 printf ("%s\n", __PRETTY_FUNCTION__);
179#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS
180
181 if (abort_ && !value_)
182 {
183 printf ("\n");
184 reporter.output ();
185 abort ();
186 }
187 }
188
189 } // namespace detail
190
191 // ==========================================================================
192
193#if defined(__GNUC__)
194#pragma GCC diagnostic push
195#if defined(__clang__)
196#pragma clang diagnostic ignored "-Wexit-time-destructors"
197#pragma clang diagnostic ignored "-Wglobal-constructors"
198#endif
199#endif
200
201 // Static instances;
204
206
207#if defined(__GNUC__)
208#pragma GCC diagnostic pop
209#endif
210
211 // --------------------------------------------------------------------------
212} // namespace micro_os_plus::micro_test_plus
213
214// ----------------------------------------------------------------------------
deferred_reporter_base(bool value, const reflection::source_location location)
Local implementation of the std::source_location.
Definition reflection.h:58
Reporter to display the test results. For failed tests it prints the actual values of the operands,...
The test runner. It maintains a list of test suites which automatically register themselves in their ...
Definition test-runner.h:52
int exit_code(void)
Return 0 if the all tests were successful, 1 otherwise.
void initialize(int argc, char *argv[], const char *name)
Pass the main arguments explicitly, if the default constructor was used.
Base class for all test suites.
Definition test-suite.h:51
void increment_failed(void)
Count one more failed test conditions.
void increment_successful(void)
Count one more passed test conditions.
int exit_code(void)
Complete the test and return the exit code.
void initialize(int argc, char *argv[], const char *name="Main")
Initialize the test framework.
bool is_match(std::string_view input, std::string_view pattern)
Check if a string matches a pattern.
const char * short_name(const char *name)