micro-test-plus 3.2.2
The µTest++ Testing Framework
Loading...
Searching...
No Matches
micro-test-plus.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 *
11 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
12 * released under the terms of the Boost Version 1.0 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
18#if defined(MICRO_OS_PLUS_INCLUDE_CONFIG_H)
19#include <micro-os-plus/config.h>
20#endif // MICRO_OS_PLUS_INCLUDE_CONFIG_H
21
23#include <cstring>
24// <iostream> is too heavy for embedded, use printf().
25#include <stdio.h>
26#include <unistd.h>
27
28// ----------------------------------------------------------------------------
29
30#pragma GCC diagnostic ignored "-Waggregate-return"
31#if defined(__clang__)
32#pragma clang diagnostic ignored "-Wc++98-compat"
33#pragma clang diagnostic ignored "-Wexit-time-destructors"
34#pragma clang diagnostic ignored "-Wglobal-constructors"
35#pragma clang diagnostic ignored "-Wunknown-warning-option"
36#endif
37
39{
40 // --------------------------------------------------------------------------
41 // Public API.
42
55 void
56 initialize (int argc, char* argv[], const char* name)
57 {
58#if defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
59 printf ("%s\n", __PRETTY_FUNCTION__);
60#endif
61 runner.initialize (argc, argv, name);
62 }
63
76 int
77 exit_code (void)
78 {
79 return runner.exit_code ();
80 }
81
82 // --------------------------------------------------------------------------
83 // Too small to deserve a separate source file.
84 namespace reflection
85 {
86
87 const char*
88 short_name (const char* name)
89 {
90#pragma GCC diagnostic push
91#if defined(__clang__)
92#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
93#endif
94 const char* p = strrchr (name, '/');
95 if (p != nullptr)
96 return p + 1;
97 else
98 return name;
99#pragma GCC diagnostic pop
100 }
101
102 } // namespace reflection
103
104 namespace utility
105 {
106#if defined(__clang__)
107#pragma clang diagnostic push
108#pragma clang diagnostic ignored "-Wdocumentation"
109#endif
124#if defined(__clang__)
125#pragma clang diagnostic pop
126#endif
127 [[nodiscard]] bool
128 is_match (std::string_view input, std::string_view pattern)
129 {
130 if (std::empty (pattern))
131 {
132 return std::empty (input);
133 }
134
135 if (std::empty (input))
136 {
137 return pattern[0] == '*' ? is_match (input, pattern.substr (1))
138 : false;
139 }
140
141 if (pattern[0] != '?' and pattern[0] != '*' and pattern[0] != input[0])
142 {
143 return false;
144 }
145
146 if (pattern[0] == '*')
147 {
148 for (decltype (std::size (input)) i = 0u; i <= std::size (input);
149 ++i)
150 {
151 if (is_match (input.substr (i), pattern.substr (1)))
152 {
153 return true;
154 }
155 }
156 return false;
157 }
158
159 return is_match (input.substr (1), pattern.substr (1));
160 }
161
162 } // namespace utility
163
164 namespace detail
165 {
167 bool value, const reflection::source_location location)
168 : value_{ value }, location_{ location }
169 {
170 if (value_)
171 {
172 current_test_suite->increment_successful ();
173 }
174 else
175 {
176 current_test_suite->increment_failed ();
177 }
178 }
179
181 {
182#if 0 // defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
183 printf ("%s\n", __PRETTY_FUNCTION__);
184#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS
185
186 if (abort_ && !value_)
187 {
188 printf ("\n");
189 reporter.output ();
190 abort ();
191 }
192 }
193
194 } // namespace detail
195
196 // ==========================================================================
197
198#if defined(__GNUC__)
199#pragma GCC diagnostic push
200#if defined(__clang__)
201#pragma clang diagnostic ignored "-Wexit-time-destructors"
202#pragma clang diagnostic ignored "-Wglobal-constructors"
203#endif
204#endif
205
206 // Static instances;
209
211
212#if defined(__GNUC__)
213#pragma GCC diagnostic pop
214#endif
215
216 // --------------------------------------------------------------------------
217} // namespace micro_os_plus::micro_test_plus
218
219// ----------------------------------------------------------------------------
deferred_reporter_base(bool value, const reflection::source_location location)
Local implementation of the std::source_location.
Definition reflection.h:57
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:51
Base class for all test suites.
Definition test-suite.h:50
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)