Skip to main content

The micro-test-plus.cpp File Reference

C++ source file with implementations for the µTest++ methods. More...

Included Headers

#include <micro-os-plus/micro-test-plus.h>
#include <cstring>
#include <stdio.h>
#include <unistd.h>

Namespaces Index

namespacemicro_os_plus

The primary namespace for the µOS++ framework. More...

namespacemicro_os_plus::micro_test_plus

Primary namespace for the µTest++ testing framework. More...

namespacemicro_os_plus::micro_test_plus::detail

Internal implementation details for the µTest++ framework. More...

namespacemicro_os_plus::micro_test_plus::reflection

Reflection utilities for the µTest++ testing framework. More...

namespacemicro_os_plus::micro_test_plus::utility

Utility functions for the µTest++ testing framework. More...

Functions Index

intexit_code (void)

Complete the test run and return the exit code. More...

voidinitialize (int argc, char *argv[], const char *name="Main")

Initialise the µTest++ framework. More...

boolis_match (std::string_view input, std::string_view pattern)

Check if a string matches a pattern. More...

const char *short_name (const char *name)

Extract a short type or function name from a fully qualified name. More...

Description

C++ source file with implementations for the µTest++ methods.

This source file contains the core implementations for the µTest++ testing framework, including initialisation and shutdown routines, utility functions, and the management of global framework state. It provides the logic for setting up the test environment, registering and executing test suites, reporting results, and supporting utility operations such as file name extraction and pattern-based string matching.

All definitions are contained within the micro_os_plus::micro_test_plus namespace and its nested namespaces, ensuring clear separation from user code and minimising the risk of naming conflicts.

The implementation is optimised for embedded environments, avoiding heavy dependencies and providing lightweight, efficient mechanisms for test execution and reporting.

This file must be included when building the µTest++ library.

Functions

short_name()

const char * micro_os_plus::micro_test_plus::reflection::short_name (const char * name)

Extract a short type or function name from a fully qualified name.

Parameters
nameThe fully qualified name as a C-string.
Returns

A pointer to the short name within the input string.

This function extracts the short name from a given file path by locating the final folder separator ('/'). If a separator is found, it returns a pointer to the character immediately following it, effectively providing the file or folder name. If no separator is present, the original input string is returned. This utility is useful for reporting concise file or folder names in test output.

Definition at line 128 of file micro-test-plus.cpp.

File Listing

The file content with the documentation metadata removed is:

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
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 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE&#95;1&#95;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
108 exit_code (void)
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// ----------------------------------------------------------------------------

Generated via docusaurus-plugin-doxygen by Doxygen 1.14.0.