Skip to content

Commit 325599e

Browse files
committed
🎨 Move setting result to compile time
1 parent 25a3629 commit 325599e

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

include/GUnit/GAssert.h

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class msg : public decltype(Message()) {
5757
std::string comp_{};
5858
TLhs lhs_;
5959
TRhs rhs_;
60-
bool result_{};
60+
bool result_{false};
6161
};
6262

6363
template <class TShouldError>
@@ -66,22 +66,10 @@ class op {
6666
class comp : public decltype(Message()) {
6767
public:
6868
explicit comp(const info& info, const TLhs& lhs) : info_{info}, lhs_{lhs} {
69-
if (std::is_same<bool, TLhs>::value) {
70-
result_ = internal::CmpHelperEQ("", "", lhs_, true);
71-
}
69+
set_result(lhs_);
7270
}
7371

74-
~comp() {
75-
if (TShouldError::value && !followed_ &&
76-
std::is_same<bool, TLhs>::value) {
77-
const AssertionResult gtest_ar =
78-
(internal::CmpHelperEQ(info_.expr.c_str(), "true", lhs_, true));
79-
if (!gtest_ar) {
80-
internal::AssertHelper(info_.failure, info_.file, info_.line,
81-
gtest_ar.failure_message()) = *this;
82-
}
83-
}
84-
}
72+
~comp() { assert_error(lhs_); }
8573

8674
template <class TRhs,
8775
std::enable_if_t<std::is_floating_point<TLhs>::value ||
@@ -142,10 +130,31 @@ class op {
142130
operator bool() const { return result_; }
143131

144132
private:
133+
void set_result(const bool&) {
134+
result_ = internal::CmpHelperEQ("", "", lhs_, true);
135+
}
136+
137+
template <class T>
138+
void set_result(const T&) {}
139+
140+
void assert_error(const bool&) {
141+
if (TShouldError::value && !followed_) {
142+
const AssertionResult gtest_ar =
143+
(internal::CmpHelperEQ(info_.expr.c_str(), "true", lhs_, true));
144+
if (!gtest_ar) {
145+
internal::AssertHelper(info_.failure, info_.file, info_.line,
146+
gtest_ar.failure_message()) = *this;
147+
}
148+
}
149+
}
150+
151+
template <class T>
152+
void assert_error(const T&) {}
153+
145154
info info_{};
146155
TLhs lhs_{};
147156
mutable bool followed_{false};
148-
bool result_{};
157+
bool result_{false};
149158
};
150159

151160
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

0 commit comments

Comments
 (0)