|
micro-test-plus 6.0.0
µTest++ Testing Framework
|
Utility functions for handling strings in tests. More...
Functions | |
| const char * | micro_os_plus::micro_test_plus::utility::extract_file_name (const char *path) noexcept |
| Extracts the file name component from a full path. | |
| bool | micro_os_plus::micro_test_plus::utility::is_match (std::string_view input, std::string_view pattern) |
| Checks whether a string matches a pattern with * and ? wildcards. | |
| std::vector< std::string_view > | micro_os_plus::micro_test_plus::utility::split (std::string_view input, std::string_view delimiters) |
| Splits a string view into a vector of string views. | |
| template<class T, class Delim_T> requires requires (T s, Delim_T d) { T::npos; s.find_first_of (d, std::size_t{}); s.substr (std::size_t{}, std::size_t{}); } | |
| auto | micro_os_plus::micro_test_plus::utility::split (T input, Delim_T delim) -> std::vector< T > |
| Splits a string into a vector of sub-strings. | |
The utility namespace has three functions for the strings that the tests often check:
|
nodiscardnoexcept |
| path | A null-terminated file path string, or nullptr. |
The extract_file_name function extracts the file name from a given file path, handling both Unix-style (/) and Windows-style (the backslash) 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.
The reports use it to display concise file names without full paths.
A nullptr path, which a default-constructed source location may provide, is accepted and yields an empty string, instead of reaching the std::string_view constructor, which does not allow it.
Definition at line 95 of file utility.cpp.
References extract_file_name().
Referenced by micro_os_plus::micro_test_plus::runner::abort(), micro_os_plus::micro_test_plus::detail::reporter_human::abort_session(), micro_os_plus::micro_test_plus::detail::reporter_tap::abort_session(), micro_os_plus::micro_test_plus::runner::add_top_suite(), extract_file_name(), micro_os_plus::micro_test_plus::runner::initialise(), micro_os_plus::micro_test_plus::detail::reporter_human::output_fail_prefix(), micro_os_plus::micro_test_plus::detail::reporter_tap::output_fail_suffix(), micro_os_plus::micro_test_plus::reflection::short_name(), and micro_os_plus::micro_test_plus::detail::reporter::write_info().
|
nodiscard |
| input | The string view to be checked. |
| pattern | The string view containing the pattern to match. |
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 (including an empty one) and ? to match any single character. This allows for flexible validation of string content in test checks, accommodating variable or partially known values.
The match is iterative, with no recursion, so the stack usage is constant, which matters on embedded targets. When a mismatch occurs after a *, only the most recent * is retried, one character further in the input; the earlier ones never need to be, since any match they could produce is also found by the most recent one. The worst-case complexity is O(n * m), for an input of length n and a pattern of length m.
The function is also used by the runner to select the suites and the test cases matching the --filter patterns, for each of the alternatives separated by |.
Definition at line 147 of file utility.cpp.
References is_match().
Referenced by is_match(), and micro_os_plus::micro_test_plus::runner::is_selected().
|
nodiscard |
This overload accepts string literals and const char* arguments, which the template cannot handle, and also any other type convertible to std::string_view. The returned views refer to the characters of the input, which must outlive them.
| input | Input string to split. |
| delimiters | Delimiter characters; any of them separates sub-strings. |
The function iterates through the input, identifying the positions of any of the delimiter characters (via find_first_of()) and extracting the views between them. Empty views, such as those between consecutive delimiters, are omitted. It is the overload selected for string literals and const char* arguments, which the template cannot handle.
Definition at line 215 of file utility.cpp.
References split().
|
nodiscard |
This function template splits a string into a vector of sub-strings, using the specified delimiter characters.
The function iterates through the input string, identifying the positions of any of the delimiter characters (via find_first_of()) and extracting the sub-strings between them. Each non-empty sub-string is added to the output vector; empty sub-strings, such as those between consecutive delimiters, are omitted. This approach supports flexible parsing of delimited data, which is useful for validating string processing logic in test cases.
This template preserves the type of the argument: for a std::string_view it returns views into the input, for a std::string it returns copies. It takes part in overload resolution only for the string-like types, i.e. those having npos, find_first_of(), and substr(); for string literals and const char* arguments the non-template overload is selected.
| T | Type of the input string. |
| Delim_T | Type of the delimiter. |
| input | Input string to split. |
| delim | Delimiter characters; any of them separates sub-strings. |
Definition at line 91 of file utility-inlines.h.
References split().