C++Spec 1.1.0
BDD testing for C++
Loading...
Searching...
No Matches
equal.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <string>
5
6#include "matcher_base.hpp"
7
8namespace CppSpec::Matchers {
9
16template <typename A, typename E>
17class Equal : public MatcherBase<A, E> {
18 public:
19 Equal(Expectation<A>& expectation, E expected) : MatcherBase<A, E>(expectation, expected) {}
20
26
27 std::string verb() override { return "equal"; }
28 std::string failure_message() override;
29 std::string failure_message_when_negated() override;
30 bool diffable();
31
32 protected:
33 bool match() override;
34 bool expected_is_a_literal();
35 std::string comparison();
36 std::string actual_inspected();
37 std::string simple_failure_message();
38 // std::string detailed_failure_message();
39};
40
41template <typename A, typename E>
43 // if (expected_is_a_literal()) {
44 // return simple_failure_message();
45 // } else {
46 // return detailed_failure_message();
47 // }
48 return simple_failure_message();
49}
50
51template <typename A, typename E>
53 std::stringstream ss;
54 ss << "expected not " << Pretty::inspect_object(MatcherBase<A, E>::expected()) << std::endl;
55 ss << " got " << actual_inspected() << std::endl;
56 ss << comparison();
57 return ss.str();
58}
59
60template <typename A, typename E>
61std::string Equal<A, E>::simple_failure_message() {
62 std::stringstream ss;
63 ss << "expected " << Pretty::inspect_object(MatcherBase<A, E>::expected()) << std::endl;
64 ss << " got " << actual_inspected() << std::endl;
65 ss << comparison();
66 return ss.str();
67}
68
69template <typename A, typename E>
70bool Equal<A, E>::diffable() {
71 return !expected_is_a_literal();
72}
73
74// A null C string is a value of its own: it equals another null C string and differs from every piece of text.
75template <typename A, typename E>
76bool Equal<A, E>::match() {
77 if constexpr (compares_text) {
78 return string_view_of(this->actual()) == string_view_of(this->expected());
79 } else {
80 return this->expected() == this->actual();
81 }
82}
83
84template <typename A, typename E>
85std::string Equal<A, E>::comparison() {
86 if constexpr (compares_text) {
87 if (!string_view_of(this->actual()) || !string_view_of(this->expected())) {
88 return "A null C string equals only another null C string";
89 }
90 }
91 return "Compared using `==`";
92}
93
94template <typename A, typename E>
95bool Equal<A, E>::expected_is_a_literal() {
96 return (typeid(E) == typeid(bool));
97}
98
99// template <typename E>
100// std::string Equal<bool, E>::actual_inspected() {
101// std::stringstream ss;
102// ss << std::boolalpha << BaseMatcher<bool,E>::actual();
103// return ss.str();
104// }
105
106template <typename A, typename E>
107std::string Equal<A, E>::actual_inspected() {
108 return Pretty::inspect_object(MatcherBase<A, E>::actual());
109}
110
111// template <typename A, bool E>
112// std::string Equal<A, bool>::simple_failure_message() {
113// std::stringstream ss;
114// ss << "\nexpected " << std::boolalpha <<
115// BaseMatcher<A,bool>::expected() << "\n"
116// " got " << Equal<A,bool>::actual_inspected() << "\n";
117// return ss.str();
118// }
119
120// template <>
121// std::string Equal<bool>::inspect_object(bool o) {
122// std::stringstream ss;
123// ss << "#<#" << Util::demangle(typeid(o).name()) <<
124// "> => " << std::boolalpha << o;
125// return ss.str();
126// }
127
128} // namespace CppSpec::Matchers
Wraps the target of an expectation.
Definition expectation.hpp:48
static constexpr bool compares_text
Whether the two values are compared as text, through string_view_of(), whatever form each is in,...
Definition equal.hpp:25
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 equal.hpp:52
std::string failure_message() override
Get the matcher's own message for a failed match; the handler puts any custom message above it.
Definition equal.hpp:42
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.
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