C++Spec 1.1.0
BDD testing for C++
Loading...
Searching...
No Matches
matcher_base.hpp
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <source_location>
13#include <string>
14#include <string_view>
15
17#include "it_base.hpp"
18#include "pretty_matchers.hpp"
19
20namespace CppSpec {
21
22template <class T>
23class Expectation; // Forward declaration of Expectation
24
25namespace Matchers {
26
34template <typename A>
35[[nodiscard]] std::string no_text_to_search(const A& actual) {
36 if constexpr (Util::is_string_like<A>) {
37 if (!string_view_of(actual)) {
38 return "\nnullptr is not text, so there is nothing to search";
39 }
40 }
41 return {};
42}
43
48inline constexpr std::string_view nothing_to_look_for = "\nnullptr is not text, so there is nothing to look for";
49
56template <typename E>
57[[nodiscard]] std::string no_text_to_look_for(const E& expected) {
58 if constexpr (Util::is_string_like<E>) {
60 return std::string(nothing_to_look_for);
61 }
62 }
63 return {};
64}
65
76template <typename Actual, typename Expected>
77class MatcherBase : public Pretty {
78 std::string custom_failure_message;
79
80 // The custom message, when one was set, on the line above the matcher's own. An empty own message is replaced by
81 // a diagnostic here, so a custom message never sits above a blank line.
82 [[nodiscard]] std::string with_custom_message(std::string own) const {
83 if (own.empty()) {
85 }
86 return custom_failure_message.empty() ? own : custom_failure_message + "\n" + own;
87 }
88
89 protected:
90 Expected expected_; // The expected object contained by the matcher
91
92 // A reference to the Expectation (i.e. `expect(2)`)
93 Expectation<Actual>& expectation_;
94
95 public:
96 // Copy constructor
97 MatcherBase(MatcherBase<Actual, Expected> const& copy) = default;
98
99 // Constructor when matcher has no 'object' to match for
100 explicit MatcherBase(Expectation<Actual>& expectation)
101 // We want the parent of the matcher to be the `it` block, not the Expectation.
102 : expectation_(expectation) {}
103
104 // Constructor when the matcher has an object to match for. This is the most
105 // commonly used constructor
106 MatcherBase(Expectation<Actual>& expectation, Expected expected) : expected_(expected), expectation_(expectation) {}
107
108 virtual ~MatcherBase() = default;
109
110 /*--------- Helper functions -------------*/
111
112 virtual std::string failure_message();
113 virtual std::string failure_message_when_negated();
114 virtual std::string description();
115 virtual std::string verb() { return "match"; }
116
117 // Get the 'actual' object from the Expectation
118 constexpr Actual& actual() { return expectation_.get_target(); }
119
120 // Get the 'expected' object from the Matcher
121 Expected& expected() { return expected_; }
122
123 // Get the Expectation itself
124 Expectation<Actual>& expectation() { return expectation_; }
125
126 // Set the message to give on match failure
127 virtual MatcherBase& set_message(std::string message);
128
133 [[nodiscard]] std::string full_failure_message() { return with_custom_message(failure_message()); }
134
139 [[nodiscard]] std::string full_failure_message_when_negated() {
140 return with_custom_message(failure_message_when_negated());
141 }
142
144 static constexpr std::string_view empty_failure_message =
145 "Failure message is empty. Does your matcher define the appropriate failure_message[_when_negated] method to "
146 "return a string?";
147
148 [[nodiscard]] std::source_location get_location() const { return expectation_.get_location(); }
149
150 /*--------- Primary functions -------------*/
151
152 // Run the matcher
154
155 virtual bool match() = 0;
156 virtual bool negated_match() { return !match(); }
157
158 // typedef Expected
159 using expected_t = Expected;
160};
161
168template <typename A, typename E>
169MatcherBase<A, E>& MatcherBase<A, E>::set_message(std::string message) {
170 this->custom_failure_message = std::move(message);
171 return *this;
172}
173
179template <typename A, typename E>
181 return std::format("expected {} to {}", Pretty::to_word(actual()), description());
182}
183
189template <typename A, typename E>
191 return std::format("expected {} to not {}", Pretty::to_word(actual()), description());
192}
193
199template <typename A, typename E>
201 // std::string pretty_expected = this->to_sentence(expected_);
202 // ss << "match " <<
203 // this->name_to_sentence(Util::demangle(typeid(*this).name()))
204 // << "(" << pretty_expected.substr(1, pretty_expected.length()) << ")";
205 // to_sentence() leads with the space that separates it from the verb.
206 return std::format("{}{}", verb(), Pretty::to_sentence(expected_));
207}
208
216template <typename A, typename E>
218 Result result = expectation_.positive() ? PositiveExpectationHandler::handle_matcher(*this)
220
221 result.set_type(Util::demangle(typeid(*this).name()));
222
223 // If our items didn't match, we obviously failed.
224 // Only report the failure if we aren't actively ignoring it.
225 // A handler that does not build its message with full_failure_message*() can still report an empty one.
226 if (result.is_failure() && result.get_message().empty()) {
227 result.set_message(std::string(empty_failure_message));
228 }
229
230 if (expectation_.ignored()) {
231 result.set_status(Result::Status::Skipped);
232 }
233
234 ItBase* parent = expectation_.get_it();
235 if (parent != nullptr) {
236 // If we need a description for our test, generate it
237 // unless we're ignoring the output.
238 if (parent->needs_description() && !expectation_.ignored()) {
239 parent->set_description(
240 (expectation_.positive() ? PositiveExpectationHandler::verb() : NegativeExpectationHandler::verb()) + " " +
241 this->description());
242 }
243 parent->add_result(result);
244 }
245 return result;
246}
247
248} // namespace Matchers
249} // namespace CppSpec
Wraps the target of an expectation.
Definition expectation.hpp:48
virtual A & get_target() &=0
Get the target of the expectation.
Base class for it expressions.
Definition it_base.hpp:32
bool needs_description() noexcept
Get whether the object needs a description string.
Definition it_base.hpp:58
ItBase & set_description(std::string_view description) noexcept
Set the description string.
Definition it_base.hpp:70
std::string full_failure_message()
What a failed match reports: the custom message, when one was set, on the line above the matcher's ow...
Definition matcher_base.hpp:133
virtual std::string description()
Get the description of the Matcher.
Definition matcher_base.hpp:200
Result run()
Run the Matcher object.
Definition matcher_base.hpp:217
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
static constexpr std::string_view empty_failure_message
What a failure reports in place of a matcher's own message that is empty.
Definition matcher_base.hpp:144
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
std::string full_failure_message_when_negated()
What a failed negated match reports: the custom message, when one was set, on the line above the matc...
Definition matcher_base.hpp:139
virtual MatcherBase & set_message(std::string message)
Set a custom failure message.
Definition matcher_base.hpp:169
Definition result.hpp:13
Definition have_error.hpp:9
Checks whether T is text that CppSpec::string_view_of() can view: a C string, or a type that converts...
Definition util.hpp:201
Contains the primary handlers for running 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::string_view nothing_to_look_for
The line a matcher that searches text puts below its own message when the text or pattern it looks fo...
Definition matcher_base.hpp:48
std::string no_text_to_look_for(const E &expected)
The line nothing_to_look_for when the expected value is a null C string.
Definition matcher_base.hpp:57
static Result handle_matcher(Matcher &matcher)
runs a negative expectation
Definition handler.hpp:65
static Result handle_matcher(Matcher &matcher)
runs a positive expectation
Definition handler.hpp:42
A helper base class that assists in pretty-printing various objects.
Definition pretty_matchers.hpp:144
static std::string to_word(const T &item)
Formats an object as a string when operator<< is available.
Definition pretty_matchers.hpp:273
static std::string to_sentence(const T &item)
Take a single object and format it as a sentance.
Definition pretty_matchers.hpp:255
std::string demangle(const char *name)
Definition util.hpp:77
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