From 3b139ae51a52382d4a86764d7b44e1c3d2eb3eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Sat, 5 Nov 2022 00:42:09 +0100 Subject: [PATCH] Decomposer checks for 0 when assuming an int arg was 0 literal --- src/catch2/internal/catch_decomposer.hpp | 15 ++++++- .../IntrospectiveTests/Details.tests.cpp | 43 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/catch2/internal/catch_decomposer.hpp b/src/catch2/internal/catch_decomposer.hpp index ab563701..3448803f 100644 --- a/src/catch2/internal/catch_decomposer.hpp +++ b/src/catch2/internal/catch_decomposer.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -196,6 +197,9 @@ namespace Catch { int> = 0> \ friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ ->BinaryExpr { \ + if ( rhs != 0 ) { \ + throw_test_failure_exception(); \ + } \ return { \ static_cast( lhs.m_lhs op 0 ), lhs.m_lhs, #op##_sr, rhs }; \ } \ @@ -210,6 +214,9 @@ namespace Catch { int> = 0> \ friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ ->BinaryExpr { \ + if ( lhs.m_lhs != 0 ) { \ + throw_test_failure_exception(); \ + } \ return { static_cast( 0 op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ } CATCH_INTERNAL_DEFINE_EXPRESSION_EQUALITY_OPERATOR( eq, == ) @@ -247,7 +254,9 @@ namespace Catch { int> = 0> \ friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ ->BinaryExpr { \ - /* TODO: do we want to assert that Rhs is 0? */ \ + if ( rhs != 0 ) { \ + throw_test_failure_exception(); \ + } \ return { \ static_cast( lhs.m_lhs op 0 ), lhs.m_lhs, #op##_sr, rhs }; \ } \ @@ -259,7 +268,9 @@ namespace Catch { int> = 0> \ friend auto operator op( ExprLhs&& lhs, RhsT rhs ) \ ->BinaryExpr { \ - /* TODO: do we want to assert that lhs is 0? */ \ + if ( lhs.m_lhs != 0 ) { \ + throw_test_failure_exception(); \ + } \ return { static_cast( 0 op rhs ), lhs.m_lhs, #op##_sr, rhs }; \ } diff --git a/tests/SelfTest/IntrospectiveTests/Details.tests.cpp b/tests/SelfTest/IntrospectiveTests/Details.tests.cpp index 8a87dc24..a5a43926 100644 --- a/tests/SelfTest/IntrospectiveTests/Details.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/Details.tests.cpp @@ -11,6 +11,8 @@ #include #include +#include + #if defined(_MSC_VER) #pragma warning(push) #pragma warning(disable:4702) // unreachable code in the macro expansions @@ -86,3 +88,44 @@ TEST_CASE("Optional comparison ops", "[optional][approvals]") { REQUIRE_FALSE( a != b ); } } + +TEST_CASE( "Decomposer checks that the argument is 0 when handling " + "only-0-comparable types", + "[decomposition][approvals]" ) { + TypeWithLit0Comparisons t{}; + + CATCH_INTERNAL_START_WARNINGS_SUPPRESSION + CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS + + REQUIRE_THROWS( Catch::Decomposer{} <= t == 42 ); + REQUIRE_THROWS( Catch::Decomposer{} <= 42 == t ); + REQUIRE_NOTHROW( Catch::Decomposer{} <= t == 0 ); + REQUIRE_NOTHROW( Catch::Decomposer{} <= 0 == t ); + + REQUIRE_THROWS( Catch::Decomposer{} <= t != 42 ); + REQUIRE_THROWS( Catch::Decomposer{} <= 42 != t ); + REQUIRE_NOTHROW( Catch::Decomposer{} <= t != 0 ); + REQUIRE_NOTHROW( Catch::Decomposer{} <= 0 != t ); + + REQUIRE_THROWS( Catch::Decomposer{} <= t < 42 ); + REQUIRE_THROWS( Catch::Decomposer{} <= 42 < t ); + REQUIRE_NOTHROW( Catch::Decomposer{} <= t < 0 ); + REQUIRE_NOTHROW( Catch::Decomposer{} <= 0 < t ); + + REQUIRE_THROWS( Catch::Decomposer{} <= t <= 42 ); + REQUIRE_THROWS( Catch::Decomposer{} <= 42 <= t ); + REQUIRE_NOTHROW( Catch::Decomposer{} <= t <= 0 ); + REQUIRE_NOTHROW( Catch::Decomposer{} <= 0 <= t ); + + REQUIRE_THROWS( Catch::Decomposer{} <= t > 42 ); + REQUIRE_THROWS( Catch::Decomposer{} <= 42 > t ); + REQUIRE_NOTHROW( Catch::Decomposer{} <= t > 0 ); + REQUIRE_NOTHROW( Catch::Decomposer{} <= 0 > t ); + + REQUIRE_THROWS( Catch::Decomposer{} <= t >= 42 ); + REQUIRE_THROWS( Catch::Decomposer{} <= 42 >= t ); + REQUIRE_NOTHROW( Catch::Decomposer{} <= t >= 0 ); + REQUIRE_NOTHROW( Catch::Decomposer{} <= 0 >= t ); + + CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION +}