77
88namespace CppSpec ::Matchers {
99
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.
12+ inline std::string describe_pattern (const std::string& pattern) {
13+ return pattern.empty () ? std::string{" a regex" } : " \" " + pattern + " \" " ;
14+ }
15+
1016template <typename A>
1117class Match : public MatcherBase <A, std::regex> {
18+ std::string pattern_;
19+
1220 public:
1321 explicit Match (Expectation<A>& expectation, std::string expected)
14- : MatcherBase<A, std::regex>(expectation, std::regex(expected)) {}
22+ : MatcherBase<A, std::regex>(expectation, std::regex(expected)), pattern_(std::move(expected)) {}
1523
1624 explicit Match (Expectation<A>& expectation, std::regex expected)
1725 : MatcherBase<A, std::regex>(expectation, expected) {}
1826
1927 std::string verb () override { return " match" ; }
28+ std::string description () override { return verb () + " " + describe_pattern (pattern_); }
2029
2130 bool match () override {
2231 std::smatch temp_match;
@@ -26,16 +35,19 @@ class Match : public MatcherBase<A, std::regex> {
2635
2736template <typename A>
2837class MatchPartial : public MatcherBase <A, std::regex> {
38+ std::string pattern_;
39+
2940 public:
3041 explicit MatchPartial (Expectation<A>& expectation, std::string expected)
31- : MatcherBase<A, std::regex>(expectation, std::regex(expected)) {}
42+ : MatcherBase<A, std::regex>(expectation, std::regex(expected)), pattern_(std::move(expected)) {}
3243
3344 explicit MatchPartial (Expectation<A>& expectation, std::regex expected)
3445 : MatcherBase<A, std::regex>(expectation, expected) {}
3546
36- std::string description () override { return " partially match " + Pretty::to_word ( this -> expected () ); }
47+ std::string description () override { return " partially match " + describe_pattern (pattern_ ); }
3748
38- bool match () override { return std::regex_match (this ->actual (), this ->expected ()); }
49+ // A partial match is a match anywhere in the string: a search, not a match of the whole.
50+ bool match () override { return std::regex_search (this ->actual (), this ->expected ()); }
3951};
4052
4153} // namespace CppSpec::Matchers
0 commit comments