Skip to content

Commit c4502de

Browse files
committed
Fix TAP and Verbose formatters and be_within_spec test
1 parent 6815f9a commit c4502de

File tree

6 files changed

+40
-30
lines changed

6 files changed

+40
-30
lines changed

include/formatters/tap.hpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ struct TAP final : public BaseFormatter {
1212
bool first = true;
1313
std::ostringstream buffer;
1414

15+
~TAP() { flush(); }
16+
1517
std::string result_to_yaml(const Result& result);
1618
void format(Description& description) override;
1719
void format(ItBase& it) override;
20+
void flush();
1821
};
1922

2023
inline std::string TAP::result_to_yaml(const Result& result) {
@@ -36,14 +39,14 @@ inline std::string TAP::result_to_yaml(const Result& result) {
3639
return oss.str();
3740
}
3841

39-
inline void TAP::format(Description& description) {
40-
if (!first && !description.has_parent()) {
41-
std::string str = buffer.str();
42-
std::ostringstream oss;
43-
if (str[0] == '\n') {
44-
oss << str[0];
45-
}
42+
inline void TAP::flush() {
43+
std::string str = buffer.str();
44+
std::ostringstream oss;
45+
if (str[0] == '\n') {
46+
oss << str[0];
47+
}
4648

49+
if (test_counter != 1) {
4750
if (color_output) {
4851
oss << GREEN;
4952
}
@@ -52,13 +55,19 @@ inline void TAP::format(Description& description) {
5255
oss << RESET;
5356
}
5457
oss << std::endl;
58+
}
5559

56-
oss << ((str[0] == '\n') ? str.substr(1) : str);
60+
oss << ((str[0] == '\n') ? str.substr(1) : str);
5761

58-
std::cout << oss.str() << std::flush;
59-
first = false;
60-
test_counter = 1;
61-
buffer = std::ostringstream();
62+
std::cout << oss.str() << std::flush;
63+
first = false;
64+
reset_test_counter();
65+
buffer = std::ostringstream();
66+
}
67+
68+
inline void TAP::format(Description& description) {
69+
if (!first && !description.has_parent()) {
70+
flush();
6271
}
6372

6473
if (first) {

include/formatters/verbose.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace CppSpec::Formatters {
1212

1313
class Verbose : public BaseFormatter {
1414
bool first = true;
15-
std::list<std::string> failure_messages{};
1615

1716
public:
1817
Verbose() = default;
@@ -51,8 +50,7 @@ inline void Verbose::format(ItBase& it) {
5150
if (color_output) {
5251
out_stream << RED; // make them red
5352
}
54-
out_stream << Util::join(failure_messages,
55-
"\n"); // separated by a blank line
53+
out_stream << result.get_message() << std::endl;
5654
if (color_output) {
5755
out_stream << RESET;
5856
}

include/matchers/errors/fail.hpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ namespace CppSpec::Matchers {
77
template <typename A>
88
class Fail : public MatcherBase<A, void*> {
99
public:
10-
static_assert(is_result_v<A>, ".fail must() be matched against a Result.");
10+
static_assert(is_result_v<A>, ".fail must() be matched against a Matcher.");
1111
explicit Fail(Expectation<A>& expectation) : MatcherBase<A, void*>(expectation, nullptr) {}
12-
13-
bool match() { return !this->actual().status(); }
12+
std::string verb() override { return "fail"; }
13+
bool match() override { return this->actual().is_failure(); }
1414
};
1515

1616
template <typename A>
@@ -19,7 +19,13 @@ class FailWith : public MatcherBase<A, std::string> {
1919
static_assert(is_result_v<A>, ".fail_with() must be matched against a Result.");
2020
FailWith(Expectation<A>& expectation, std::string expected) : MatcherBase<A, std::string>(expectation, expected) {}
2121

22-
bool match() { return (!this->actual().status()) && this->actual().get_message() == this->expected(); }
22+
std::string verb() override { return "fail with"; }
23+
std::string description() override { return std::format(R"(fail with "{}")", this->expected()); }
24+
25+
bool match() override {
26+
auto message = this->actual().get_message();
27+
return this->actual().is_failure() && message == this->expected();
28+
}
2329
};
2430

2531
} // namespace CppSpec::Matchers

include/matchers/matcher_base.hpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,14 @@ Result MatcherBase<A, E>::run() {
169169

170170
// If our items didn't match, we obviously failed.
171171
// Only report the failure if we aren't actively ignoring it.
172-
if (result.is_failure()) {
173-
if (expectation_.ignore_failure()) {
174-
result = Result::success(result.get_location());
175-
} else if (result.get_message().empty()) {
176-
result.set_message(
177-
"Failure message is empty. Does your matcher define the "
178-
"appropriate failure_message[_when_negated] method to "
179-
"return a string?");
180-
}
172+
if (result.is_failure() && result.get_message().empty()) {
173+
result.set_message(
174+
"Failure message is empty. Does your matcher define the "
175+
"appropriate failure_message[_when_negated] method to "
176+
"return a string?");
181177
}
182178

183-
if (parent) {
179+
if (parent && !expectation_.ignore_failure()) {
184180
parent->add_result(result);
185181
}
186182
return result;

include/runnable.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ class Runnable {
145145
* @brief Generate padding (indentation) fore the current object.
146146
* @return A string of spaces for use in pretty-printing.
147147
*/
148+
// TODO: Refactor this into Runnable::depth
148149
inline std::string Runnable::padding() const noexcept {
149150
return this->has_parent() ? this->get_parent()->padding() + " " : "";
150151
}

spec/matchers/be_within_spec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe be_within_spec("expect(actual).to_be_within(delta).of(expected)", $ {
7575

7676
it("fails when actual is outside the given percent variance", _ {
7777
auto base_formatter = Formatters::BaseFormatter();
78-
auto ex = ExpectationValue(self, 20.1, std::source_location::current());
78+
auto ex = ExpectationValue(self, 20.1, std::source_location::current()).ignore();
7979
auto matcher = Matchers::BeWithinHelper(ex, 10).percent_of(10.0);
8080
expect(matcher.run()).to_fail_with("expected 20.1 to be within 10% of 10");
8181
});

0 commit comments

Comments
 (0)