C++Spec 1.1.0
BDD testing for C++
Loading...
Searching...
No Matches
end_with.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <algorithm>
5#include <ranges>
6#include <string>
7
9
10namespace CppSpec::Matchers {
11
22template <typename A, typename E>
23 requires Util::compares_start_and_end<A, E>
24class EndWith : public MatcherBase<A, E> {
25 static constexpr bool compares_text = Util::is_string_like<A> && Util::is_string_like<E>;
26
27 // Whether both values hold something to compare: neither is a null C string.
28 bool has_values() {
29 if constexpr (Util::is_string_like<A>) {
30 if (!string_view_of(this->actual())) {
31 return false;
32 }
33 }
34 if constexpr (Util::is_string_like<E>) {
35 return string_view_of(this->expected()).has_value();
36 } else {
37 return true;
38 }
39 }
40
41 // The actual value against the expected one, both known to be there.
42 bool compare() {
43 if constexpr (compares_text) {
44 return string_view_of(this->actual())->ends_with(*string_view_of(this->expected()));
45 } else {
46 A& actual = this->actual();
47 auto compare_with = [&actual](const auto& expected) {
48 // std::equal reads as many elements of the actual range as the expected one has, so a shorter actual range
49 // fails before it is read.
50 return std::ranges::distance(expected) <= std::ranges::distance(actual) &&
51 std::equal(std::ranges::rbegin(expected), std::ranges::rend(expected), std::ranges::rbegin(actual));
52 };
53 if constexpr (Util::is_string_like<E>) {
54 return compare_with(*string_view_of(this->expected()));
55 } else {
56 return compare_with(this->expected());
57 }
58 }
59 }
60
61 public:
62 EndWith(Expectation<A>& expectation, E start) : MatcherBase<A, E>(expectation, start) {}
63
64 std::string verb() override { return "end with"; }
65 std::string failure_message() override {
66 return MatcherBase<A, E>::failure_message() + no_text_to_search(this->actual()) +
68 }
73
74 bool match() override { return has_values() && compare(); }
75 bool negated_match() override { return has_values() && !compare(); }
76};
77
78} // namespace CppSpec::Matchers
Wraps the target of an expectation.
Definition expectation.hpp:48
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 end_with.hpp:69
std::string failure_message() override
Get the matcher's own message for a failed match; the handler puts any custom message above it.
Definition end_with.hpp:65
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
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 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
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
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