C++Spec 1.1.0
BDD testing for C++
Loading...
Searching...
No Matches
match.hpp
Go to the documentation of this file.
1
2#pragma once
3#include <regex>
4#include <string>
5
7
8namespace CppSpec::Matchers {
9
10// A std::regex keeps no copy of its source, so a pattern given as text is named by that text; one given as a
11// std::regex is named only as a regex.
12inline std::string describe_pattern(const std::string& pattern) {
13 return pattern.empty() ? std::string{"a regex"} : "\"" + pattern + "\"";
14}
15
20template <Util::is_string_like T>
21[[nodiscard]] std::regex regex_of(const T& pattern) {
22 const auto text = string_view_of(pattern);
23 return text ? std::regex(text->begin(), text->end()) : std::regex();
24}
25
29template <Util::is_string_like T>
30[[nodiscard]] std::string pattern_name_of(const T& pattern) {
31 const auto text = string_view_of(pattern);
32 return text ? describe_pattern(std::string(*text)) : std::string("nullptr");
33}
34
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;
47
48 // Whether there is text to match and a pattern to match it with.
49 bool has_values() { return has_pattern_ && string_view_of(this->actual()).has_value(); }
50
51 bool matches() {
52 const auto actual = string_view_of(this->actual());
53 return std::regex_match(actual->begin(), actual->end(), this->expected());
54 }
55
56 public:
57 template <Util::is_string_like T>
58 explicit Match(Expectation<A>& expectation, const T& expected)
59 : MatcherBase<A, std::regex>(expectation, regex_of(expected)),
60 pattern_name_(pattern_name_of(expected)),
61 has_pattern_(string_view_of(expected).has_value()) {}
62
63 explicit Match(Expectation<A>& expectation, std::regex expected)
64 : MatcherBase<A, std::regex>(expectation, expected) {}
65
66 std::string verb() override { return "match"; }
67 std::string description() override { return verb() + " " + pattern_name_; }
68 std::string failure_message() override {
69 return MatcherBase<A, std::regex>::failure_message() + no_text_to_search(this->actual()) + no_pattern_note();
70 }
71 std::string failure_message_when_negated() override {
73 no_pattern_note();
74 }
75
76 bool match() override { return has_values() && matches(); }
77 bool negated_match() override { return has_values() && !matches(); }
78
79 private:
80 std::string no_pattern_note() { return has_pattern_ ? std::string() : std::string(nothing_to_look_for); }
81};
82
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;
95
96 // Whether there is text to search and a pattern to search it for.
97 bool has_values() { return has_pattern_ && string_view_of(this->actual()).has_value(); }
98
99 // A partial match is a match anywhere in the string: a search, not a match of the whole.
100 bool matches() {
101 const auto actual = string_view_of(this->actual());
102 return std::regex_search(actual->begin(), actual->end(), this->expected());
103 }
104
105 public:
106 template <Util::is_string_like T>
107 explicit MatchPartial(Expectation<A>& expectation, const T& expected)
108 : MatcherBase<A, std::regex>(expectation, regex_of(expected)),
109 pattern_name_(pattern_name_of(expected)),
110 has_pattern_(string_view_of(expected).has_value()) {}
111
112 explicit MatchPartial(Expectation<A>& expectation, std::regex expected)
113 : MatcherBase<A, std::regex>(expectation, expected) {}
114
115 std::string description() override { return "partially match " + pattern_name_; }
116 std::string failure_message() override {
117 return MatcherBase<A, std::regex>::failure_message() + no_text_to_search(this->actual()) + no_pattern_note();
118 }
119 std::string failure_message_when_negated() override {
121 no_pattern_note();
122 }
123
124 bool match() override { return has_values() && matches(); }
125 bool negated_match() override { return has_values() && !matches(); }
126
127 private:
128 std::string no_pattern_note() { return has_pattern_ ? std::string() : std::string(nothing_to_look_for); }
129};
130
131} // namespace CppSpec::Matchers
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