Skip to main content

The Utility Functions Reference

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

Functions Index

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, 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.

The µTest++ framework includes a collection of utility functions designed to simplify advanced string operations commonly required during test development. These functions support tasks such as partial string matching, splitting long strings for improved readability, and other manipulations that enhance the clarity and effectiveness of test code.

Functions

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.

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

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

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 }

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 321 of file function-comparators-inlines.h.

321 mut (const T& t) noexcept -> T&
322 {
323 return const_cast<T&> (t);
324 }

split()

template <class T, 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.

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.

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
 
 mt::expect (std::vector<std::string_view>{ "a", "b" }
  == mt::utility::split<std::string_view> ("a.b", "."))
  << "a.b splits into [a,b]";

Definition at line 275 of file micro-test-plus-inlines.h.

275 split (T input, Delim_T delim) -> std::vector<T>
276 {
277 std::vector<T> output{};
278 std::size_t first{};
279 while (first < std::size (input))
280 {
281 const auto second = input.find_first_of (delim, first);
282 if (first != second)
283 {
284 output.emplace_back (input.substr (first, second - first));
285 }
286 if (second == T::npos)
287 {
288 break;
289 }
290 first = second + 1;
291 }
292 return output;
293 }

Generated via doxygen2docusaurus by Doxygen 1.14.0.