From 9872bc31cf0bbd8a1934907696c925358a8be0e5 Mon Sep 17 00:00:00 2001 From: Dustin Gooding Date: Thu, 14 Nov 2024 21:54:31 -0600 Subject: [PATCH] :sparkles: enable support for logical expressions in EXPECT and ASSERT Problem: - logical expressions fail in EXPECT() and ASSERT() because of operator precedence Solution: - wrap __VA_ARGS__ use in macros with an extra set of parens --- include/GUnit/GAssert.h | 6 +++--- test/GAssert.cpp | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/GUnit/GAssert.h b/include/GUnit/GAssert.h index 13ae2db..6f0743a 100644 --- a/include/GUnit/GAssert.h +++ b/include/GUnit/GAssert.h @@ -213,7 +213,7 @@ void prevent_commas(T&&) {} (::testing::detail::op{ \ ::testing::detail::info{__FILE__, __LINE__, #__VA_ARGS__, \ ::testing::TestPartResult::kNonFatalFailure}} \ - << __VA_ARGS__) + << (__VA_ARGS__)) #define EXPECT(...) \ GUNIT_PREVENT_COMMAS(__VA_ARGS__); \ @@ -223,14 +223,14 @@ void prevent_commas(T&&) {} if (::testing::detail::op{ \ ::testing::detail::info{__FILE__, __LINE__, #__VA_ARGS__, \ ::testing::TestPartResult::kFatalFailure}} \ - << __VA_ARGS__) \ + << (__VA_ARGS__)) \ void(::testing::detail::drop{}); \ else \ return ::testing::detail::ret_void{} == \ (::testing::detail::op{::testing::detail::info{ \ __FILE__, __LINE__, #__VA_ARGS__, \ ::testing::TestPartResult::kFatalFailure}} \ - << __VA_ARGS__) + << (__VA_ARGS__)) #define ASSERT(...) \ GUNIT_PREVENT_COMMAS(__VA_ARGS__); \ diff --git a/test/GAssert.cpp b/test/GAssert.cpp index 16ab0b4..2cdff3c 100644 --- a/test/GAssert.cpp +++ b/test/GAssert.cpp @@ -37,6 +37,8 @@ TEST(GAssert, ShouldSupportExpect) { EXPECT(not convertible{}); EXPECT(false == convertible{}); + + EXPECT(false or true); } TEST(GAssert, ShouldSupportASSERT) { @@ -69,4 +71,6 @@ TEST(GAssert, ShouldSupportASSERT) { ASSERT(convertible{}); ASSERT(false == not convertible{}); + + ASSERT(false or true); }