C++Spec 1.0.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
16template <typename A>
17class Match : public MatcherBase<A, std::regex> {
18 std::string pattern_;
19
20 public:
21 explicit Match(Expectation<A>& expectation, std::string expected)
22 : MatcherBase<A, std::regex>(expectation, std::regex(expected)), pattern_(std::move(expected)) {}
23
24 explicit Match(Expectation<A>& expectation, std::regex expected)
25 : MatcherBase<A, std::regex>(expectation, expected) {}
26
27 std::string verb() override { return "match"; }
28 std::string description() override { return verb() + " " + describe_pattern(pattern_); }
29
30 bool match() override {
31 std::smatch temp_match;
32 return std::regex_match(this->actual(), temp_match, this->expected());
33 }
34};
35
36template <typename A>
37class MatchPartial : public MatcherBase<A, std::regex> {
38 std::string pattern_;
39
40 public:
41 explicit MatchPartial(Expectation<A>& expectation, std::string expected)
42 : MatcherBase<A, std::regex>(expectation, std::regex(expected)), pattern_(std::move(expected)) {}
43
44 explicit MatchPartial(Expectation<A>& expectation, std::regex expected)
45 : MatcherBase<A, std::regex>(expectation, expected) {}
46
47 std::string description() override { return "partially match " + describe_pattern(pattern_); }
48
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()); }
51};
52
53} // namespace CppSpec::Matchers
Wraps the target of an expectation.
Definition expectation.hpp:48
std::string description() override
Get the description of the Matcher.
Definition match.hpp:47
std::string description() override
Get the description of the Matcher.
Definition match.hpp:28
Definition have_error.hpp:9
Contains the base class for all Matchers.