Skip to content

Commit 423eb6f

Browse files
committed
🐛 [EXPECT/ASSERT] Fix support for boolean expression
Problem: - EXPECT/ASSERT doesn't work properly with single boolean expression which is not followed by an operator. Solution: - Add logic in the comp destructor to recognise this use case and fire if needed.
1 parent 60c2201 commit 423eb6f

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

example/GAssert.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
#include "GUnit/GAssert.h"
99

1010
int main() {
11+
const auto b = true;
1112
const auto i = 42;
13+
14+
EXPECT(true);
15+
EXPECT(b);
1216
EXPECT(42 == i);
1317
EXPECT(42 >= 0) << "message";
18+
1419
ASSERT(42.0 == 42.);
1520
}

include/GUnit/GAssert.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,26 @@ class msg : public decltype(Message()) {
5353

5454
class op {
5555
template <class TLhs>
56-
class comp {
56+
class comp : public decltype(Message()) {
5757
public:
5858
explicit comp(const info& info, const TLhs& lhs) : info_{info}, lhs_{lhs} {}
59+
~comp() {
60+
if (!followed_ && std::is_same<bool, TLhs>::value) {
61+
const AssertionResult gtest_ar =
62+
(internal::CmpHelperEQ(info_.expr.c_str(), "true", lhs_, true));
63+
if (!gtest_ar) {
64+
internal::AssertHelper(info_.failure, info_.file, info_.line,
65+
gtest_ar.failure_message()) = *this;
66+
}
67+
}
68+
}
5969

6070
template <class TRhs,
6171
std::enable_if_t<std::is_floating_point<TLhs>::value ||
6272
std::is_floating_point<TRhs>::value,
6373
int> = 0>
6474
auto operator==(const TRhs& rhs) const {
75+
followed_ = true;
6576
return msg<TLhs, TRhs, internal::CmpHelperFloatingPointEQ<TLhs>>{
6677
info_, "==", lhs_, rhs};
6778
}
@@ -71,43 +82,50 @@ class op {
7182
!std::is_floating_point<TRhs>::value,
7283
int> = 0>
7384
auto operator==(const TRhs& rhs) const {
85+
followed_ = true;
7486
return msg<const TLhs&, const TRhs&, internal::CmpHelperEQ>{
7587
info_, "==", lhs_, rhs};
7688
}
7789

7890
template <class TRhs>
7991
auto operator!=(const TRhs& rhs) const {
92+
followed_ = true;
8093
return msg<const TLhs&, const TRhs&, internal::CmpHelperNE>{
8194
info_, "!=", lhs_, rhs};
8295
}
8396

8497
template <class TRhs>
8598
auto operator>(const TRhs& rhs) const {
99+
followed_ = true;
86100
return msg<const TLhs&, const TRhs&, internal::CmpHelperGT>{info_, ">",
87101
lhs_, rhs};
88102
}
89103

90104
template <class TRhs>
91105
auto operator>=(const TRhs& rhs) const {
106+
followed_ = true;
92107
return msg<const TLhs&, const TRhs&, internal::CmpHelperGE>{
93108
info_, ">=", lhs_, rhs};
94109
}
95110

96111
template <class TRhs>
97112
auto operator<=(const TRhs& rhs) const {
113+
followed_ = true;
98114
return msg<const TLhs&, const TRhs&, internal::CmpHelperLE>{
99115
info_, "<=", lhs_, rhs};
100116
}
101117

102118
template <class TRhs>
103119
auto operator<(const TRhs& rhs) const {
120+
followed_ = true;
104121
return msg<const TLhs&, const TRhs&, internal::CmpHelperLT>{info_, "<",
105122
lhs_, rhs};
106123
}
107124

108125
private:
109126
info info_{};
110127
TLhs lhs_{};
128+
mutable bool followed_{false};
111129
};
112130

113131
public:

include/GUnit/GSteps.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ class Steps : public ::testing::EmptyTestEventListener {
388388
return Step<-1, true>{*this, {"Then", File::c_str(), line}, pattern};
389389
}
390390

391-
// clang-format off
391+
// clang-format off
392392
#if defined(__clang__)
393393
#pragma clang diagnostic ignored "-Wdollar-in-identifier-extension"
394394
#endif

test/GAssert.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
TEST(GAssert, ShouldSupportExpect) {
1212
auto i = 42;
13+
const auto b = true;
14+
15+
EXPECT(true);
16+
EXPECT(!false) << "message";
17+
EXPECT(b);
1318

1419
EXPECT(i == 42);
1520
EXPECT(42 == i);
@@ -27,7 +32,9 @@ TEST(GAssert, ShouldSupportExpect) {
2732
}
2833

2934
TEST(GAssert, ShouldSupportASSERT) {
30-
auto i = 42;
35+
const auto i = 42;
36+
37+
ASSERT(true);
3138

3239
ASSERT(i == 42);
3340
ASSERT(42 == i);

0 commit comments

Comments
 (0)