diff --git a/include/internal/catch_decomposer.h b/include/internal/catch_decomposer.h index 08075f0e..af1cbcfb 100644 --- a/include/internal/catch_decomposer.h +++ b/include/internal/catch_decomposer.h @@ -200,6 +200,14 @@ namespace Catch { auto operator <= ( RhsT const& rhs ) -> BinaryExpr const { return { static_cast(m_lhs <= rhs), m_lhs, "<=", rhs }; } + template + auto operator | (RhsT const& rhs) -> BinaryExpr const { + return { static_cast(m_lhs | rhs), m_lhs, "|", rhs }; + } + template + auto operator & (RhsT const& rhs) -> BinaryExpr const { + return { static_cast(m_lhs & rhs), m_lhs, "&", rhs }; + } template auto operator && ( RhsT const& ) -> BinaryExpr const { diff --git a/projects/SelfTest/Baselines/compact.sw.approved.txt b/projects/SelfTest/Baselines/compact.sw.approved.txt index 41d38556..4bf889a5 100644 --- a/projects/SelfTest/Baselines/compact.sw.approved.txt +++ b/projects/SelfTest/Baselines/compact.sw.approved.txt @@ -244,6 +244,9 @@ Matchers.tests.cpp:: passed: 1, Predicate(alwaysTrue, "always Matchers.tests.cpp:: passed: 1, !Predicate(alwaysFalse, "always false") for: 1 not matches predicate: "always false" Matchers.tests.cpp:: passed: "Hello olleH", Predicate( [] (std::string const& str) -> bool { return str.front() == str.back(); }, "First and last character should be equal") for: "Hello olleH" matches predicate: "First and last character should be equal" Matchers.tests.cpp:: passed: "This wouldn't pass", !Predicate( [] (std::string const& str) -> bool { return str.front() == str.back(); } ) for: "This wouldn't pass" not matches undescribed predicate +Compilation.tests.cpp:: passed: lhs | rhs for: Val: 1 | Val: 2 +Compilation.tests.cpp:: passed: !(lhs & rhs) for: !(Val: 1 & Val: 2) +Compilation.tests.cpp:: passed: HasBitOperators{ 1 } & HasBitOperators{ 1 } for: Val: 1 & Val: 1 Tricky.tests.cpp:: passed: true Tricky.tests.cpp:: passed: true Tricky.tests.cpp:: passed: true diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index 8e11339e..f53b62ac 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -1380,6 +1380,6 @@ due to unexpected exception with message: Why would you throw a std::string? =============================================================================== -test cases: 309 | 235 passed | 70 failed | 4 failed as expected -assertions: 1692 | 1540 passed | 131 failed | 21 failed as expected +test cases: 310 | 236 passed | 70 failed | 4 failed as expected +assertions: 1695 | 1543 passed | 131 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index ae96fe75..4d51f103 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -1934,6 +1934,27 @@ Matchers.tests.cpp:: PASSED: with expansion: "This wouldn't pass" not matches undescribed predicate +------------------------------------------------------------------------------- +Assertion macros support bit operators and bool conversions +------------------------------------------------------------------------------- +Compilation.tests.cpp: +............................................................................... + +Compilation.tests.cpp:: PASSED: + REQUIRE( lhs | rhs ) +with expansion: + Val: 1 | Val: 2 + +Compilation.tests.cpp:: PASSED: + REQUIRE_FALSE( lhs & rhs ) +with expansion: + !(Val: 1 & Val: 2) + +Compilation.tests.cpp:: PASSED: + REQUIRE( HasBitOperators{ 1 } & HasBitOperators{ 1 } ) +with expansion: + Val: 1 & Val: 1 + ------------------------------------------------------------------------------- Assertions then sections ------------------------------------------------------------------------------- @@ -13522,6 +13543,6 @@ Misc.tests.cpp: Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 309 | 219 passed | 86 failed | 4 failed as expected -assertions: 1709 | 1540 passed | 148 failed | 21 failed as expected +test cases: 310 | 220 passed | 86 failed | 4 failed as expected +assertions: 1712 | 1543 passed | 148 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index d47ca8ac..448aa069 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -340,6 +340,7 @@ Exception.tests.cpp: + diff --git a/projects/SelfTest/Baselines/sonarqube.sw.approved.txt b/projects/SelfTest/Baselines/sonarqube.sw.approved.txt index 59e4512f..663f6fb9 100644 --- a/projects/SelfTest/Baselines/sonarqube.sw.approved.txt +++ b/projects/SelfTest/Baselines/sonarqube.sw.approved.txt @@ -372,6 +372,7 @@ Class.tests.cpp: + diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index 3f610208..fa7a1676 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -2177,6 +2177,33 @@ Nor would this + + + + lhs | rhs + + + Val: 1 | Val: 2 + + + + + !(lhs & rhs) + + + !(Val: 1 & Val: 2) + + + + + HasBitOperators{ 1 } & HasBitOperators{ 1 } + + + Val: 1 & Val: 1 + + + + @@ -16180,7 +16207,7 @@ loose text artifact - + - + diff --git a/projects/SelfTest/UsageTests/Compilation.tests.cpp b/projects/SelfTest/UsageTests/Compilation.tests.cpp index 0baf2842..7ec17448 100644 --- a/projects/SelfTest/UsageTests/Compilation.tests.cpp +++ b/projects/SelfTest/UsageTests/Compilation.tests.cpp @@ -234,3 +234,31 @@ namespace { namespace CompilationTests { }} // namespace CompilationTests +namespace { + struct HasBitOperators { + int value; + + friend HasBitOperators operator| (HasBitOperators lhs, HasBitOperators rhs) { + return { lhs.value | rhs.value }; + } + friend HasBitOperators operator& (HasBitOperators lhs, HasBitOperators rhs) { + return { lhs.value & rhs.value }; + } + explicit operator bool() const { + return !!value; + } + + friend std::ostream& operator<<(std::ostream& out, HasBitOperators val) { + out << "Val: " << val.value; + return out; + } + }; +} + +TEST_CASE("Assertion macros support bit operators and bool conversions", "[compilation][bitops]") { + HasBitOperators lhs{ 1 }, rhs{ 2 }; + REQUIRE(lhs | rhs); + REQUIRE_FALSE(lhs & rhs); + REQUIRE(HasBitOperators{ 1 } & HasBitOperators{ 1 }); +} +