Skip to content

Commit d36dd0b

Browse files
committed
Refactor around verb
1 parent 6a400c7 commit d36dd0b

20 files changed

+132
-175
lines changed

include/expectations/expectation.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class Expectation : public Child {
100100
Result to_be_less_than(E rhs, std::string msg = "");
101101

102102
template <typename E>
103-
Matchers::BeWithin<A, E> to_be_within(E expected, std::string msg = "");
103+
Matchers::BeWithinHelper<A, E> to_be_within(E expected, std::string msg = "");
104104

105105
/*-------- to... ----------*/
106106

@@ -302,8 +302,8 @@ Result Expectation<A>::to_equal(E expected, std::string msg) {
302302
*/
303303
template <typename A>
304304
template <typename E>
305-
Matchers::BeWithin<A, E> Expectation<A>::to_be_within(E expected, std::string msg) {
306-
Matchers::BeWithin<A, E> matcher(*this, expected);
305+
Matchers::BeWithinHelper<A, E> Expectation<A>::to_be_within(E expected, std::string msg) {
306+
Matchers::BeWithinHelper<A, E> matcher(*this, expected);
307307
matcher.set_message(msg);
308308
return matcher;
309309
}

include/matchers/be_nullptr.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class BeNullptr : MatcherBase<A, std::nullptr_t> {
1212
public:
1313
explicit BeNullptr(Expectation<A> &expectation) : MatcherBase<A, std::nullptr_t>(expectation) {}
1414

15+
std::string verb() override { return "be"; }
1516
std::string description() override { return "be nullptr"; }
1617
bool match() override { return this->actual() == nullptr; }
1718
};

include/matchers/contain.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class ContainBase : public MatcherBase<A, E> {
1313
A actual_;
1414

1515
public:
16+
std::string verb() override { return "contain"; }
1617
std::string description() override;
1718
std::string failure_message() override;
1819
std::string failure_message_when_negated() override;
@@ -30,7 +31,7 @@ class ContainBase : public MatcherBase<A, E> {
3031
template <typename A, typename E, typename U>
3132
std::string ContainBase<A, E, U>::description() {
3233
// std::vector<E> described_items;
33-
return Pretty::improve_hash_formatting("contain" + Pretty::to_sentence(this->expected()));
34+
return Pretty::improve_hash_formatting(verb() + Pretty::to_sentence(this->expected()));
3435
}
3536

3637
template <typename A, typename E, typename U>
@@ -49,7 +50,7 @@ bool ContainBase<A, E, U>::actual_collection_includes(U expected_item) {
4950
auto last = *(actual.begin());
5051
static_assert(Util::verbose_assert<std::is_same_v<decltype(last), U>>::value,
5152
"Expected item is not the same type as what is inside container.");
52-
return std::find(actual.begin(), actual.end(), expected_item) != actual.end();
53+
return std::ranges::find(actual, expected_item) != actual.end();
5354
}
5455

5556
/**

include/matchers/equal.hpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Equal : public MatcherBase<A, E> {
1818
public:
1919
Equal(Expectation<A> &expectation, E expected) : MatcherBase<A, E>(expectation, expected) {}
2020

21-
std::string description() override;
21+
std::string verb() override { return "equal"; }
2222
std::string failure_message() override;
2323
std::string failure_message_when_negated() override;
2424
bool diffable();
@@ -31,13 +31,6 @@ class Equal : public MatcherBase<A, E> {
3131
// std::string detailed_failure_message();
3232
};
3333

34-
template <typename A, typename E>
35-
std::string Equal<A, E>::description() {
36-
std::stringstream ss;
37-
ss << "equal" << Pretty::to_sentence<E>(this->expected());
38-
return ss.str();
39-
}
40-
4134
template <typename A, typename E>
4235
std::string Equal<A, E>::failure_message() {
4336
// if (expected_is_a_literal()) {

include/matchers/errors/fail.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
/** @file */
21
#pragma once
32

4-
#include <string>
5-
63
#include "matchers/matcher_base.hpp"
74

85
namespace CppSpec::Matchers {

include/matchers/errors/have_error.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33

44
#include "matchers/matcher_base.hpp"
55

6-
76
namespace CppSpec::Matchers {
87

98
template <typename T>
109
concept expected = requires(T t) {
11-
{ t.error() } -> std::same_as<typename T::error_type&>;
10+
{ t.error() } -> std::same_as<typename T::error_type &>;
1211
};
1312

1413
template <expected T>
1514
class HaveError : public MatcherBase<T, void *> {
1615
public:
1716
HaveError(Expectation<T> &expectation) : MatcherBase<T, void *>(expectation) {}
1817

19-
std::string description() override {return "have an error"; }
18+
std::string verb() override { return "have an error"; }
19+
std::string description() override { return verb(); }
2020

2121
bool match() override { return (!this->actual().has_value()); }
2222
};

include/matchers/errors/have_value.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
2-
#include <optional>
32

3+
#include "matchers/equal.hpp"
44
#include "matchers/matcher_base.hpp"
55

66
namespace CppSpec::Matchers {
@@ -15,16 +15,16 @@ class HaveValue : public MatcherBase<T, void *> {
1515
public:
1616
HaveValue(Expectation<T> &expectation) : MatcherBase<T, void *>(expectation) {}
1717

18-
std::string description() override {return "have a value"; }
18+
std::string verb() override { return "have"; }
19+
std::string description() override { return "have a value"; }
1920

2021
bool match() override { return (this->actual().has_value()); }
2122
};
2223

2324
template <optional T, typename E>
2425
class HaveValueEqualTo : public Equal<T, E> {
2526
public:
26-
static_assert(std::is_same_v<typename T::value_type, E>,
27-
"the contained value_type must match the expected type");
27+
static_assert(std::is_same_v<typename T::value_type, E>, "the contained value_type must match the expected type");
2828
HaveValueEqualTo(Expectation<T> &expectation, E expected) : Equal<T, E>(expectation, expected) {}
2929

3030
bool match() { return (this->actual().value() == this->expected()); }

include/matchers/errors/throw.hpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
/** @file */
21
#pragma once
32

4-
#include <functional>
5-
#include <type_traits>
6-
73
#include "matchers/matcher_base.hpp"
84
#include "util.hpp"
95

@@ -14,6 +10,7 @@ class Throw : public MatcherBase<A, void *> {
1410
public:
1511
explicit Throw(Expectation<A> &expectation) : MatcherBase<A, void *>(expectation, nullptr) {}
1612
bool match() override;
13+
std::string verb() override { return "throw"; }
1714
std::string description() override;
1815
std::string failure_message() override;
1916
std::string failure_message_when_negated() override;
@@ -32,26 +29,18 @@ bool Throw<A, Ex>::match() {
3229

3330
template <typename A, typename Ex>
3431
std::string Throw<A, Ex>::description() {
35-
std::stringstream ss;
36-
ss << "throw " << Util::demangle(typeid(Ex).name());
37-
return ss.str();
32+
return std::format("throw {}", Util::demangle(typeid(Ex).name()));
3833
}
3934

4035
template <typename A, typename Ex>
4136
std::string Throw<A, Ex>::failure_message() {
42-
std::stringstream ss;
43-
ss << "expected the given function ([] -> " << Util::demangle(typeid(A).name()) << " {...}) to "
44-
<< this->description();
45-
return ss.str();
37+
return std::format("expected the given function ([] -> {} {{...}}) to {}", Util::demangle(typeid(A).name()),
38+
description());
4639
}
4740

4841
template <typename A, typename Ex>
4942
std::string Throw<A, Ex>::failure_message_when_negated() {
50-
std::stringstream ss;
51-
ss << "expected the given function ([] -> " << Util::demangle(typeid(A).name()) << " {...}) not to "
52-
<< this->description();
53-
return ss.str();
43+
return std::format("expected the given function ([] -> {} {{...}}) not to {}", Util::demangle(typeid(A).name()),
44+
description());
5445
}
55-
5646
} // namespace CppSpec::Matchers
57-

include/matchers/matcher_base.hpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class MatcherBase : public Runnable, public Pretty {
6565
virtual std::string failure_message();
6666
virtual std::string failure_message_when_negated();
6767
virtual std::string description();
68+
virtual std::string verb() { return "match"; }
6869

6970
// Get the 'actual' object from the Expectation
7071
constexpr Actual &actual() { return expectation_.get_target(); }
@@ -113,9 +114,7 @@ std::string MatcherBase<A, E>::failure_message() {
113114
if (not custom_failure_message.empty()) {
114115
return this->custom_failure_message;
115116
}
116-
std::stringstream ss;
117-
ss << "expected " << Pretty::to_word(actual()) << " to " << description();
118-
return ss.str();
117+
return std::format("expected {} to {}", Pretty::to_word(actual()), description());
119118
}
120119

121120
/**
@@ -128,9 +127,7 @@ std::string MatcherBase<A, E>::failure_message_when_negated() {
128127
if (not custom_failure_message.empty()) {
129128
return this->custom_failure_message;
130129
}
131-
std::stringstream ss;
132-
ss << "expected " << Pretty::to_word(actual()) << " to not " << description();
133-
return ss.str();
130+
return std::format("expected {} to not {}", Pretty::to_word(actual()), description());
134131
}
135132

136133
/**
@@ -140,13 +137,11 @@ std::string MatcherBase<A, E>::failure_message_when_negated() {
140137
*/
141138
template <typename A, typename E>
142139
std::string MatcherBase<A, E>::description() {
143-
std::stringstream ss;
144-
//std::string pretty_expected = this->to_sentence(expected_);
145-
// ss << "match " <<
146-
// this->name_to_sentence(Util::demangle(typeid(*this).name()))
147-
// << "(" << pretty_expected.substr(1, pretty_expected.length()) << ")";
148-
ss << "match" << Pretty::to_sentence(expected_);
149-
return ss.str();
140+
// std::string pretty_expected = this->to_sentence(expected_);
141+
// ss << "match " <<
142+
// this->name_to_sentence(Util::demangle(typeid(*this).name()))
143+
// << "(" << pretty_expected.substr(1, pretty_expected.length()) << ")";
144+
return std::format("{} {}", verb(), Pretty::to_sentence(expected_));
150145
}
151146

152147
/**
@@ -158,15 +153,14 @@ std::string MatcherBase<A, E>::description() {
158153
*/
159154
template <typename A, typename E>
160155
Result MatcherBase<A, E>::run(Formatters::BaseFormatter &printer) {
161-
auto *par = static_cast<ItBase *>(this->get_parent());
156+
auto *parent = static_cast<ItBase *>(this->get_parent());
162157
// If we need a description for our test, generate it
163158
// unless we're ignoring the output.
164-
if (par->needs_description() && !expectation_.ignore_failure()) {
159+
if (parent->needs_description() && !expectation_.ignore_failure()) {
165160
std::stringstream ss;
166161
ss << (expectation_.sign() ? PositiveExpectationHandler::verb() : NegativeExpectationHandler::verb()) << " "
167162
<< this->description();
168-
std::string ss_str = ss.str();
169-
par->set_description(ss_str);
163+
parent->set_description(ss.str());
170164
}
171165

172166
Result matched = expectation_.sign() ? PositiveExpectationHandler::handle_matcher(*this)

include/matchers/numeric/be_between.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class BeBetween : public MatcherBase<A, E> {
3434
}
3535

3636
bool match() override;
37+
std::string verb() override { return "be between"; }
3738
std::string description() override;
3839
};
3940

@@ -63,11 +64,7 @@ bool BeBetween<A, E>::match() {
6364

6465
template <typename A, typename E>
6566
std::string BeBetween<A, E>::description() {
66-
std::stringstream ss;
67-
ss << "be between " << min << " and " << max << " (" << (mode == RangeMode::exclusive ? "exclusive" : "inclusive")
68-
<< ")";
69-
return ss.str();
67+
return std::format("be between {} and {} ({})", min, max, (mode == RangeMode::exclusive ? "exclusive" : "inclusive"));
7068
}
7169

7270
} // namespace CppSpec::Matchers
73-

0 commit comments

Comments
 (0)