C++Spec 1.1.0
BDD testing for C++
Loading...
Searching...
No Matches
contain.hpp
Go to the documentation of this file.
1
2#pragma once
3#include <algorithm>
4#include <ranges>
5#include <string>
6#include <type_traits>
7
8#include "matcher_base.hpp"
9
10namespace CppSpec::Matchers {
11
16template <typename A, typename E, typename U>
17class ContainBase : public MatcherBase<A, E> {
18 A actual_;
19
20 public:
21 std::string verb() override { return "contain"; }
22 std::string description() override;
23 std::string failure_message() override;
24 std::string failure_message_when_negated() override;
25 virtual bool diffable() { return true; }
26
27 ContainBase(Expectation<A>& expectation, std::initializer_list<U> expected)
28 : MatcherBase<A, std::vector<U>>(expectation, std::vector<U>(expected)), actual_(this->actual()) {};
29 ContainBase(Expectation<A>& expectation, U expected)
30 : MatcherBase<A, U>(expectation, expected), actual_(this->actual()) {};
31
32 protected:
33 bool actual_collection_includes(U expected_item);
34
40
46
48 std::string null_text_notes();
49
50 private:
51 bool collection_includes(U expected_item);
52};
53
54template <typename A, typename E, typename U>
56 // std::vector<E> described_items;
57 return Pretty::improve_hash_formatting(verb() + Pretty::to_sentence(this->expected()));
58}
59
60template <typename A, typename E, typename U>
62 return Pretty::improve_hash_formatting(MatcherBase<A, E>::failure_message()) + null_text_notes();
63}
64
65template <typename A, typename E, typename U>
69
70template <typename A, typename E, typename U>
72 return no_text_to_search(this->actual()) +
73 (expected_is_null_text() ? std::string(nothing_to_look_for) : std::string());
74}
75
76// Text contains a piece of text or a character, both read through string_view_of(); any other collection contains
77// an element.
78template <typename A, typename E, typename U>
79bool ContainBase<A, E, U>::actual_collection_includes(U expected_item) {
80 if constexpr (Util::is_string_like<A>) {
81 const auto text = string_view_of(this->actual());
82 if constexpr (Util::is_string_like<U>) {
83 const auto part = string_view_of(expected_item);
84 return text && part && text->contains(*part);
85 } else {
86 return text && text->contains(expected_item);
87 }
88 } else {
89 return collection_includes(expected_item);
90 }
91}
92
93template <typename A, typename E, typename U>
95 if constexpr (Util::is_string_like<A>) {
96 return !string_view_of(this->actual());
97 } else {
98 return false;
99 }
100}
101
102// In a collection that is not text, a null C string is an element like any other, and is looked for as one.
103template <typename A, typename E, typename U>
106 if constexpr (std::same_as<E, U>) {
107 return !string_view_of(this->expected());
108 } else {
109 return std::ranges::any_of(this->expected(), [](const U& item) { return !string_view_of(item); });
110 }
111 } else {
112 return false;
113 }
114}
115
116// The element type is checked from the range's type alone, so an empty collection is searched without reading an
117// element.
118template <typename A, typename E, typename U>
119bool ContainBase<A, E, U>::collection_includes(U expected_item) {
121 "Expected item is not comparable against what is inside container.");
122 auto& actual = this->actual();
123 return std::ranges::find(actual, expected_item) != std::ranges::end(actual);
124}
125
130template <typename A, typename E, typename U>
131class Contain : public ContainBase<A, E, U> {
132 enum class Predicate { any, all, none };
133
134 public:
135 bool match() override;
136 bool negated_match() override;
137 using ContainBase<A, E, U>::ContainBase;
138
139 protected:
140 bool perform_match(Predicate predicate, Predicate hash_subset_predicate);
141};
142
143template <typename A, typename E, typename U>
144bool Contain<A, E, U>::match() {
145 return !this->actual_is_null_text() && !this->expected_is_null_text() &&
146 perform_match(Predicate::all, Predicate::all);
147}
148
149template <typename A, typename E, typename U>
150bool Contain<A, E, U>::negated_match() {
151 return !this->actual_is_null_text() && !this->expected_is_null_text() &&
152 perform_match(Predicate::none, Predicate::any);
153}
154
155// TODO: support std::map<E,_>
156template <typename A, typename E, typename U>
157bool Contain<A, E, U>::perform_match(Predicate predicate, Predicate /*hash_subset_predicate*/) {
158 bool retval = true; // start off true
159 for (U expected_item : this->expected()) {
160 retval = retval && this->actual_collection_includes(expected_item);
161
162 // Based on our main predicate
163 switch (predicate) {
164 case Predicate::all:
165 if (retval) { // if the collection includes the item,
166 continue; // continue the loop
167 } else { // otherwise
168 return false; // immediately return false
169 }
170 case Predicate::none:
171 if (retval) { // if the collection includes the item
172 return false; // immediately return false
173 } else { // otherwise
174 continue; // continue the loop
175 }
176 case Predicate::any:
177 if (retval) { // if the collection includes the item
178 return true; // immediately return true
179 } else { // otherwise
180 continue; // continue the loop
181 }
182 }
183 }
184 return true;
185}
186
191template <typename A, typename U>
192class Contain<A, U, U> : public ContainBase<A, U, U> {
193 public:
194 Contain(Expectation<A>& expectation, U expected) : ContainBase<A, U, U>(expectation, expected) {};
195
196 bool match() override;
197 bool negated_match() override;
198};
199
200template <typename A, typename U>
201bool Contain<A, U, U>::match() {
202 return this->actual_collection_includes(this->expected());
203}
204
205template <typename A, typename U>
206bool Contain<A, U, U>::negated_match() {
207 return !this->actual_is_null_text() && !this->expected_is_null_text() && !match();
208}
209
210} // namespace CppSpec::Matchers
Wraps the target of an expectation.
Definition expectation.hpp:48
Definition contain.hpp:17
std::string failure_message() override
Get the matcher's own message for a failed match; the handler puts any custom message above it.
Definition contain.hpp:61
bool expected_is_null_text()
Whether the actual value is text and a piece of text to look for in it is a null C string,...
Definition contain.hpp:104
std::string null_text_notes()
The lines that say why there is no text to search or nothing to look for, or an empty string.
Definition contain.hpp:71
bool actual_is_null_text()
Whether the actual value is a null C string, which is no text, so the matcher fails it negated as wel...
Definition contain.hpp:94
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 contain.hpp:66
std::string description() override
Get the description of the Matcher.
Definition contain.hpp:55
Definition contain.hpp:131
the base class for all Matcher classes and objects
Definition matcher_base.hpp:77
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
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
static std::string to_sentence(const T &item)
Take a single object and format it as a sentance.
Definition pretty_matchers.hpp:255
Helper class for static assertions that has a built-in error string.
Definition util.hpp:86
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