8namespace CppSpec::Matchers {
12inline std::string describe_pattern(
const std::string& pattern) {
13 return pattern.empty() ? std::string{
"a regex"} :
"\"" + pattern +
"\"";
20template <Util::is_
string_like T>
21[[nodiscard]] std::regex
regex_of(
const T& pattern) {
23 return text ? std::regex(text->begin(), text->end()) : std::regex();
29template <Util::is_
string_like T>
32 return text ? describe_pattern(std::string(*text)) : std::string(
"nullptr");
43template <Util::is_
string_like A>
44class Match :
public MatcherBase<A, std::regex> {
45 std::string pattern_name_ = describe_pattern(
"");
46 bool has_pattern_ =
true;
49 bool has_values() {
return has_pattern_ &&
string_view_of(this->actual()).has_value(); }
53 return std::regex_match(actual->begin(), actual->end(), this->expected());
57 template <Util::is_
string_like T>
64 : MatcherBase<A, std::regex>(expectation,
expected) {}
66 std::string verb()
override {
return "match"; }
67 std::string
description()
override {
return verb() +
" " + pattern_name_; }
76 bool match()
override {
return has_values() && matches(); }
77 bool negated_match()
override {
return has_values() && !matches(); }
80 std::string no_pattern_note() {
return has_pattern_ ? std::string() : std::string(nothing_to_look_for); }
91template <Util::is_
string_like A>
92class MatchPartial :
public MatcherBase<A, std::regex> {
93 std::string pattern_name_ = describe_pattern(
"");
94 bool has_pattern_ =
true;
97 bool has_values() {
return has_pattern_ &&
string_view_of(this->actual()).has_value(); }
102 return std::regex_search(actual->begin(), actual->end(), this->expected());
106 template <Util::is_
string_like T>
113 : MatcherBase<A, std::regex>(expectation,
expected) {}
115 std::string
description()
override {
return "partially match " + pattern_name_; }
124 bool match()
override {
return has_values() && matches(); }
125 bool negated_match()
override {
return has_values() && !matches(); }
128 std::string no_pattern_note() {
return has_pattern_ ? std::string() : std::string(nothing_to_look_for); }
Wraps the target of an expectation.
Definition expectation.hpp:48
std::string failure_message() override
Get the matcher's own message for a failed match; the handler puts any custom message above it.
Definition match.hpp:116
std::string failure_message_when_negated() override
Get the matcher's own message for a failed negated match; the handler puts any custom message above i...
Definition match.hpp:119
std::string description() override
Get the description of the Matcher.
Definition match.hpp:115
std::string failure_message_when_negated() override
Get the matcher's own message for a failed negated match; the handler puts any custom message above i...
Definition match.hpp:71
std::string failure_message() override
Get the matcher's own message for a failed match; the handler puts any custom message above it.
Definition match.hpp:68
std::string description() override
Get the description of the Matcher.
Definition match.hpp:67
virtual std::string failure_message_when_negated()
Get the matcher's own message for a failed negated match; the handler puts any custom message above i...
Definition matcher_base.hpp:190
virtual std::string failure_message()
Get the matcher's own message for a failed match; the handler puts any custom message above it.
Definition matcher_base.hpp:180
Definition have_error.hpp:9
std::regex regex_of(const T &pattern)
Compile a pattern given as text, read through string_view_of(); a null C string compiles to an empty ...
Definition match.hpp:21
std::string pattern_name_of(const T &pattern)
Name a pattern given as text: its quoted text, or nullptr for a null C string.
Definition match.hpp:30
Contains the base class for all Matchers.
std::string no_text_to_search(const A &actual)
The line a matcher that searches text puts below its own message when the actual value is a null C st...
Definition matcher_base.hpp:35
constexpr std::optional< std::string_view > string_view_of(const T &text)
View text as a std::string_view, the one form the string matchers and the printer compare and print.
Definition util.hpp:337