C++Spec 1.0.0
BDD testing for C++
Loading...
Searching...
No Matches
contain.hpp
Go to the documentation of this file.
1
2#pragma once
3#include "matcher_base.hpp"
4
5namespace CppSpec::Matchers {
6
11template <typename A, typename E, typename U>
12class ContainBase : public MatcherBase<A, E> {
13 A actual_;
14
15 public:
16 std::string verb() override { return "contain"; }
17 std::string description() override;
18 std::string failure_message() override;
19 std::string failure_message_when_negated() override;
20 virtual bool diffable() { return true; }
21
22 ContainBase(Expectation<A>& expectation, std::initializer_list<U> expected)
23 : MatcherBase<A, std::vector<U>>(expectation, std::vector<U>(expected)), actual_(this->actual()) {};
24 ContainBase(Expectation<A>& expectation, U expected)
25 : MatcherBase<A, U>(expectation, expected), actual_(this->actual()) {};
26
27 protected:
28 bool actual_collection_includes(U expected_item);
29};
30
31template <typename A, typename E, typename U>
33 // std::vector<E> described_items;
34 return Pretty::improve_hash_formatting(verb() + Pretty::to_sentence(this->expected()));
35}
36
37template <typename A, typename E, typename U>
39 return Pretty::improve_hash_formatting(MatcherBase<A, E>::failure_message());
40}
41
42template <typename A, typename E, typename U>
44 return Pretty::improve_hash_formatting(MatcherBase<A, E>::failure_message_when_negated());
45}
46
47template <typename A, typename E, typename U>
48bool ContainBase<A, E, U>::actual_collection_includes(U expected_item) {
49 auto actual = this->actual();
50 auto last = *(actual.begin());
51 static_assert(Util::verbose_assert<std::is_convertible_v<U, decltype(last)>>::value,
52 "Expected item is not comparable against what is inside container.");
53 return std::ranges::find(actual, expected_item) != actual.end();
54}
55
60template <typename A, typename E, typename U>
61class Contain : public ContainBase<A, E, U> {
62 enum class Predicate { any, all, none };
63
64 public:
65 bool match() override;
66 bool negated_match() override;
67 using ContainBase<A, E, U>::ContainBase;
68
69 protected:
70 bool perform_match(Predicate predicate, Predicate hash_subset_predicate);
71};
72
73template <typename A, typename E, typename U>
74bool Contain<A, E, U>::match() {
75 return perform_match(Predicate::all, Predicate::all);
76}
77
78template <typename A, typename E, typename U>
79bool Contain<A, E, U>::negated_match() {
80 return perform_match(Predicate::none, Predicate::any);
81}
82
83// TODO: support std::map<E,_>
84template <typename A, typename E, typename U>
85bool Contain<A, E, U>::perform_match(Predicate predicate, Predicate /*hash_subset_predicate*/) {
86 bool retval = true; // start off true
87 for (U expected_item : this->expected()) {
88 retval = retval && this->actual_collection_includes(expected_item);
89
90 // Based on our main predicate
91 switch (predicate) {
92 case Predicate::all:
93 if (retval) { // if the collection includes the item,
94 continue; // continue the loop
95 } else { // otherwise
96 return false; // immediately return false
97 }
98 case Predicate::none:
99 if (retval) { // if the collection includes the item
100 return false; // immediately return false
101 } else { // otherwise
102 continue; // continue the loop
103 }
104 case Predicate::any:
105 if (retval) { // if the collection includes the item
106 return true; // immediately return true
107 } else { // otherwise
108 continue; // continue the loop
109 }
110 }
111 }
112 return true;
113}
114
119template <typename A, typename U>
120class Contain<A, U, U> : public ContainBase<A, U, U> {
121 public:
122 Contain(Expectation<A>& expectation, U expected) : ContainBase<A, U, U>(expectation, expected) {};
123
124 bool match() override;
125};
126
127template <typename A, typename U>
128bool Contain<A, U, U>::match() {
129 return this->actual_collection_includes(this->expected());
130}
131
132} // namespace CppSpec::Matchers
Wraps the target of an expectation.
Definition expectation.hpp:47
Definition contain.hpp:12
std::string failure_message() override
Get message to give on match failure.
Definition contain.hpp:38
std::string failure_message_when_negated() override
Get message to give on match failure when negated.
Definition contain.hpp:43
std::string description() override
Get the description of the Matcher.
Definition contain.hpp:32
Definition contain.hpp:61
the base class for all Matcher classes and objects
Definition matcher_base.hpp:37
virtual std::string failure_message_when_negated()
Get message to give on match failure when negated.
Definition matcher_base.hpp:125
virtual std::string failure_message()
Get message to give on match failure.
Definition matcher_base.hpp:112
Definition have_error.hpp:9
Contains the base class for all Matchers.
static std::string to_sentence(const T &item)
Take a single object and format it as a sentance.
Definition pretty_matchers.hpp:106
Helper class for static assertions that has a built-in error string.
Definition util.hpp:47