Skip to main content

Utility Functions

Utility functions for advanced string handling in tests. More...

Functions Index

const char *extract_file_name (const char *path) noexcept

Extracts the file name component from a full path. More...

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

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

template <class T>
constexpr T &mut (const T &t) noexcept

Generic mutator to remove const qualification from any type. More...

template <class T = std::string_view, class Delim_T>
auto split (T input, Delim_T delim) -> std::vector< T >

Split a string into a vector of sub-strings. More...

Description

Utility functions for advanced string handling in tests.

These utilities simplify advanced string operations commonly required during test development. They support tasks such as partial string matching, splitting long strings for improved readability, and related manipulations that enhance the clarity and effectiveness of test code.

Functions

extract_file_name()

const char * micro_os_plus::micro_test_plus::utility::extract_file_name (const char * path)
nodiscard noexcept

Extracts the file name component from a full path.

Parameters
[in] path

A null-terminated file path string.

Returns

A pointer to the first character of the file name within path, or path itself if no directory separator is found.

The extract_file_name function extracts the file name from a given file path, handling both Unix-style (/) and Windows-style (\\endiskip) path separators. It returns a pointer to the start of the file name within the input string, or the original string if no separators are found.

This utility is particularly useful for test reporting, allowing for concise display of file names without full paths.

Example
 const char* path = "/home/user/project/test.cpp";
 const char* filename = utility::extract_file_name (path);
 // filename now points to "test.cpp"

Definition at line 85 of file utility.cpp.

85 extract_file_name (const char* path) noexcept
86 {
87 const std::string_view sv{ path };
88 const auto pos = sv.find_last_of ("/\\");
89 if (pos == std::string_view::npos)
90 {
91 return path; // No separators found, return original string.
92 }
93 return sv.substr (pos + 1).data ();
94 }

Referenced by micro_os_plus::micro_test_plus::runner::initialise.

is_match()

bool micro_os_plus::micro_test_plus::utility::is_match (std::string_view input, std::string_view pattern)
nodiscard

Check if a string matches a pattern.

Parameters
[in] input

The string view to be checked.

[in] pattern

The string view containing the pattern to match.

Returns

true if the input string matches the pattern; otherwise, false.

This function enables pattern-based string comparison for tests, supporting both exact matches and wildcard patterns. The pattern may include * to match any sequence of characters and ? to match any single character. This allows for flexible validation of string content in test assertions, accommodating variable or partially known values.

warning

The * wildcard is handled via recursion with a linear loop, giving $O(2^n)$ worst-case complexity in the number of * wildcards (e.g. a pattern such as "a*a*a*b" against a long string of 'a' characters). For the typical short suite-name patterns used in a test framework this is not a concern, but callers should avoid patterns with many consecutive wildcards against long input strings.

Examples
 
 t.expect (mt::utility::is_match ("abc", "a?c")) << "abc matches a?c";
 t.expect (mt::utility::is_match ("abc", "a*c")) << "abc matches a*c";

Definition at line 124 of file utility.cpp.

124 is_match (std::string_view input, std::string_view pattern)
125 {
126 if (std::empty (pattern))
127 {
128 return std::empty (input);
129 }
130
131 if (std::empty (input))
132 {
133 return pattern[0] == '*' ? is_match (input, pattern.substr (1))
134 : false;
135 }
136
137 if (pattern[0] != '?' and pattern[0] != '*' and pattern[0] != input[0])
138 {
139 return false;
140 }
141
142 if (pattern[0] == '*')
143 {
144 for (decltype (std::size (input)) i = 0u; i <= std::size (input);
145 ++i)
146 {
147 if (is_match (input.substr (i), pattern.substr (1)))
148 {
149 return true;
150 }
151 }
152 return false;
153 }
154
155 return is_match (input.substr (1), pattern.substr (1));
156 }

Reference micro_os_plus::micro_test_plus::utility::is_match.

Referenced by micro_os_plus::micro_test_plus::utility::is_match.

mut()

template <class T>
T & micro_os_plus::micro_test_plus::mut (const T & t)
nodiscard constexpr noexcept

Generic mutator to remove const qualification from any type.

Template Parameters
T

The type of the input object.

Parameters
[in] t

The object from which to remove const qualification.

Returns

A non-const reference to the input object.

The mut function template provides a safe and generic mechanism to remove the const qualifier from any type. It returns a non-const reference to the input object, enabling modification of objects that were originally declared as const. This utility is particularly useful in testing scenarios where controlled mutation of test data is required.

Definition at line 325 of file function-comparators-inlines.h.

325 mut (const T& t) noexcept -> T&
326 {
327 return const_cast<T&> (t);
328 }

split()

template <class T = std::string_view, class Delim_T>
std::vector< T > micro_os_plus::micro_test_plus::utility::split (T input, Delim_T delim)
nodiscard

Split a string into a vector of sub-strings.

This function template facilitates string handling in tests by splitting a string into a vector of substrings, using the specified delimiter.

The function iterates through the input string, identifying delimiter positions and extracting substrings between them. Each resulting substring is added to the output vector. This approach supports flexible parsing of delimited data, which is particularly useful for validating string processing logic in test cases.

Example

 
 t.expect (std::vector<std::string_view>{ "a", "b" }
  == mt::utility::split<std::string_view> ("a.b", "."))
  << "a.b splits into [a,b]";
Template Parameters
T

Type of the input string.

Delim_T

Type of the delimiter.

Parameters
[in] input

Input string to split.

[in] delim

Delimiter string.

Returns

A vector containing the resulting sub-strings.

Definition at line 88 of file utility-inlines.h.

88 split (T input, Delim_T delim) -> std::vector<T>
89 {
90 std::vector<T> output{};
91 std::size_t first{};
92 while (first < std::size (input))
93 {
94 const auto second = input.find_first_of (delim, first);
95 if (first != second)
96 {
97 output.emplace_back (input.substr (first, second - first));
98 }
99 if (second == T::npos)
100 {
101 break;
102 }
103 first = second + 1;
104 }
105 return output;
106 }

Generated via doxygen2docusaurus 2.2.0 by Doxygen 1.17.0.