micro-test-plus 3.2.2
µ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
40
41// ----------------------------------------------------------------------------
42
43#if defined(MICRO_OS_PLUS_INCLUDE_CONFIG_H)
44#include <micro-os-plus/config.h>
45#endif // MICRO_OS_PLUS_INCLUDE_CONFIG_H
46
48#include <cstring>
49// <iostream> is too heavy for embedded, use printf().
50#include <stdio.h>
51#include <unistd.h>
52
53// ----------------------------------------------------------------------------
54
55#pragma GCC diagnostic ignored "-Waggregate-return"
56#if defined(__clang__)
57#pragma clang diagnostic ignored "-Wc++98-compat"
58#pragma clang diagnostic ignored "-Wexit-time-destructors"
59#pragma clang diagnostic ignored "-Wglobal-constructors"
60#pragma clang diagnostic ignored "-Wunknown-warning-option"
61#endif
62
64{
65 // --------------------------------------------------------------------------
66 // Public API.
67
80 void
81 initialize (int argc, char* argv[], const char* name)
82 {
83#if defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
84 printf ("%s\n", __PRETTY_FUNCTION__);
85#endif
86 runner.initialize (argc, argv, name);
87 }
88
107 int
109 {
110 return runner.exit_code ();
111 }
112
113 // --------------------------------------------------------------------------
114 // Too small to deserve a separate source file.
115 namespace reflection
116 {
117
127 const char*
128 short_name (const char* name)
129 {
130#pragma GCC diagnostic push
131#if defined(__clang__)
132#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
133#endif
134 const char* p = strrchr (name, '/');
135 if (p != nullptr)
136 return p + 1;
137 else
138 return name;
139#pragma GCC diagnostic pop
140 }
141
142 } // namespace reflection
143
144 namespace utility
145 {
146#if defined(__clang__)
147#pragma clang diagnostic push
148#pragma clang diagnostic ignored "-Wdocumentation"
149#endif
167#if defined(__clang__)
168#pragma clang diagnostic pop
169#endif
170 [[nodiscard]] bool
171 is_match (std::string_view input, std::string_view pattern)
172 {
173 if (std::empty (pattern))
174 {
175 return std::empty (input);
176 }
177
178 if (std::empty (input))
179 {
180 return pattern[0] == '*' ? is_match (input, pattern.substr (1))
181 : false;
182 }
183
184 if (pattern[0] != '?' and pattern[0] != '*' and pattern[0] != input[0])
185 {
186 return false;
187 }
188
189 if (pattern[0] == '*')
190 {
191 for (decltype (std::size (input)) i = 0u; i <= std::size (input);
192 ++i)
193 {
194 if (is_match (input.substr (i), pattern.substr (1)))
195 {
196 return true;
197 }
198 }
199 return false;
200 }
201
202 return is_match (input.substr (1), pattern.substr (1));
203 }
204
205 } // namespace utility
206
207 namespace detail
208 {
219 bool value, const reflection::source_location location)
220 : value_{ value }, location_{ location }
221 {
222 if (value_)
223 {
224 current_test_suite->increment_successful ();
225 }
226 else
227 {
228 current_test_suite->increment_failed ();
229 }
230 }
231
241 {
242#if 0 // defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
243 printf ("%s\n", __PRETTY_FUNCTION__);
244#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS
245
246 if (abort_ && !value_)
247 {
248 printf ("\n");
249 reporter.output ();
250 abort ();
251 }
252 }
253
254 } // namespace detail
255
256 // ==========================================================================
257
258#if defined(__GNUC__)
259#pragma GCC diagnostic push
260#if defined(__clang__)
261#pragma clang diagnostic ignored "-Wexit-time-destructors"
262#pragma clang diagnostic ignored "-Wglobal-constructors"
263#endif
264#endif
265
266 // Static instances;
279
293
307
308#if defined(__GNUC__)
309#pragma GCC diagnostic pop
310#endif
311
312 // --------------------------------------------------------------------------
313} // namespace micro_os_plus::micro_test_plus
314
315// ----------------------------------------------------------------------------
const reflection::source_location location_
Stores the source location associated with the report.
Definition detail.h:1806
constexpr bool value() const
Retrieves the result value.
Definition detail.h:1786
~deferred_reporter_base()
Destructor for the deferred reporter base.
bool abort_
Indicates whether the reporting should abort further processing.
Definition detail.h:1801
deferred_reporter_base(bool value, const reflection::source_location location)
Constructs a deferred reporter base.
bool value_
Stores the result value of the report.
Definition detail.h:1795
Local implementation of source location information for diagnostics.
Definition reflection.h:137
Reporter to display test results, including operand values and types for failures.
The test runner for the µTest++ framework.
Definition test-runner.h:97
Base class for all test suites.
Definition test-suite.h:98
int exit_code(void)
Complete the test run and return the exit code.
void initialize(int argc, char *argv[], const char *name="Main")
Initialise the µTest++ framework.
bool is_match(std::string_view input, std::string_view pattern)
Check if a string matches a pattern.
Main C++ header with the declarations for the µTest++ Testing Framework.
const char * short_name(const char *name)
Extract a short type or function name from a fully qualified name.
Primary namespace for the µTest++ testing framework.
test_runner runner
Global instance of test_runner.
test_suite_base * current_test_suite
Global pointer references the currently active test suite.
test_reporter reporter
Global instance of test_reporter.