diff --git a/src/catch2/internal/catch_decomposer.hpp b/src/catch2/internal/catch_decomposer.hpp index 7d915f15..814824c6 100644 --- a/src/catch2/internal/catch_decomposer.hpp +++ b/src/catch2/internal/catch_decomposer.hpp @@ -208,6 +208,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/tests/SelfTest/Baselines/automake.sw.approved.txt b/tests/SelfTest/Baselines/automake.sw.approved.txt index 6de24789..7779e17b 100644 --- a/tests/SelfTest/Baselines/automake.sw.approved.txt +++ b/tests/SelfTest/Baselines/automake.sw.approved.txt @@ -81,6 +81,7 @@ Nor would this :test-result: PASS Approximate comparisons with ints :test-result: PASS Approximate comparisons with mixed numeric types :test-result: PASS Arbitrary predicate matcher +:test-result: PASS Assertion macros support bit operators and bool conversions :test-result: PASS Assertions then sections :test-result: PASS Basic use of the Contains range matcher :test-result: PASS Basic use of the Empty range matcher diff --git a/tests/SelfTest/Baselines/compact.sw.approved.txt b/tests/SelfTest/Baselines/compact.sw.approved.txt index 7c126fdf..c965699f 100644 --- a/tests/SelfTest/Baselines/compact.sw.approved.txt +++ b/tests/SelfTest/Baselines/compact.sw.approved.txt @@ -245,6 +245,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/tests/SelfTest/Baselines/console.std.approved.txt b/tests/SelfTest/Baselines/console.std.approved.txt index 1780de5a..9c523a54 100644 --- a/tests/SelfTest/Baselines/console.std.approved.txt +++ b/tests/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: 334 | 260 passed | 70 failed | 4 failed as expected -assertions: 1892 | 1740 passed | 131 failed | 21 failed as expected +test cases: 335 | 261 passed | 70 failed | 4 failed as expected +assertions: 1895 | 1743 passed | 131 failed | 21 failed as expected diff --git a/tests/SelfTest/Baselines/console.sw.approved.txt b/tests/SelfTest/Baselines/console.sw.approved.txt index dd96bbdd..1feae362 100644 --- a/tests/SelfTest/Baselines/console.sw.approved.txt +++ b/tests/SelfTest/Baselines/console.sw.approved.txt @@ -1943,6 +1943,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 ------------------------------------------------------------------------------- @@ -14773,6 +14794,6 @@ Misc.tests.cpp: Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 334 | 244 passed | 86 failed | 4 failed as expected -assertions: 1909 | 1740 passed | 148 failed | 21 failed as expected +test cases: 335 | 245 passed | 86 failed | 4 failed as expected +assertions: 1912 | 1743 passed | 148 failed | 21 failed as expected diff --git a/tests/SelfTest/Baselines/junit.sw.approved.txt b/tests/SelfTest/Baselines/junit.sw.approved.txt index 264f7d95..b2d601e9 100644 --- a/tests/SelfTest/Baselines/junit.sw.approved.txt +++ b/tests/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -341,6 +341,7 @@ Exception.tests.cpp: + diff --git a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt index 60357eb5..60bbe28d 100644 --- a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt +++ b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt @@ -396,6 +396,7 @@ Class.tests.cpp: + diff --git a/tests/SelfTest/Baselines/tap.sw.approved.txt b/tests/SelfTest/Baselines/tap.sw.approved.txt index 6e45900c..9d6eedb3 100644 --- a/tests/SelfTest/Baselines/tap.sw.approved.txt +++ b/tests/SelfTest/Baselines/tap.sw.approved.txt @@ -488,6 +488,12 @@ ok {test-number} - 1, !Predicate(alwaysFalse, "always false") for: 1 not ma ok {test-number} - "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" # Arbitrary predicate matcher ok {test-number} - "This wouldn't pass", !Predicate( [] (std::string const& str) -> bool { return str.front() == str.back(); } ) for: "This wouldn't pass" not matches undescribed predicate +# Assertion macros support bit operators and bool conversions +ok {test-number} - lhs | rhs for: Val: 1 | Val: 2 +# Assertion macros support bit operators and bool conversions +ok {test-number} - !(lhs & rhs) for: !(Val: 1 & Val: 2) +# Assertion macros support bit operators and bool conversions +ok {test-number} - HasBitOperators{ 1 } & HasBitOperators{ 1 } for: Val: 1 & Val: 1 # Assertions then sections ok {test-number} - true # Assertions then sections @@ -3810,5 +3816,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0 ok {test-number} - # xmlentitycheck ok {test-number} - -1..1901 +1..1904 diff --git a/tests/SelfTest/Baselines/teamcity.sw.approved.txt b/tests/SelfTest/Baselines/teamcity.sw.approved.txt index 116c80f9..3594ed22 100644 --- a/tests/SelfTest/Baselines/teamcity.sw.approved.txt +++ b/tests/SelfTest/Baselines/teamcity.sw.approved.txt @@ -195,6 +195,8 @@ Exception.tests.cpp:|nunexpected exception with message:|n "unexpe ##teamcity[testFinished name='Approximate comparisons with mixed numeric types' duration="{duration}"] ##teamcity[testStarted name='Arbitrary predicate matcher'] ##teamcity[testFinished name='Arbitrary predicate matcher' duration="{duration}"] +##teamcity[testStarted name='Assertion macros support bit operators and bool conversions'] +##teamcity[testFinished name='Assertion macros support bit operators and bool conversions' duration="{duration}"] ##teamcity[testStarted name='Assertions then sections'] ##teamcity[testFinished name='Assertions then sections' duration="{duration}"] ##teamcity[testStarted name='Basic use of the Contains range matcher'] diff --git a/tests/SelfTest/Baselines/xml.sw.approved.txt b/tests/SelfTest/Baselines/xml.sw.approved.txt index 2bc11f3b..eb2b2593 100644 --- a/tests/SelfTest/Baselines/xml.sw.approved.txt +++ b/tests/SelfTest/Baselines/xml.sw.approved.txt @@ -2183,6 +2183,33 @@ Nor would this + + + + lhs | rhs + + + Val: 1 | Val: 2 + + + + + !(lhs & rhs) + + + !(Val: 1 & Val: 2) + + + + + HasBitOperators{ 1 } & HasBitOperators{ 1 } + + + Val: 1 & Val: 1 + + + + @@ -17732,7 +17759,7 @@ loose text artifact - + - + diff --git a/tests/SelfTest/UsageTests/Compilation.tests.cpp b/tests/SelfTest/UsageTests/Compilation.tests.cpp index 5a0ab65e..4fc63fa0 100644 --- a/tests/SelfTest/UsageTests/Compilation.tests.cpp +++ b/tests/SelfTest/UsageTests/Compilation.tests.cpp @@ -237,3 +237,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 }); +} +