Support bitand and bitor in REQUIRE/CHECK

This means that bit-flag-like types with conversion to bool can be
asserted on, like so `REQUIRE(var & Flags::AddNewline)`.
This commit is contained in:
Martin Hořeňovský 2020-04-21 11:00:08 +02:00
parent 37cbf4a4fe
commit 4e4171420d
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
8 changed files with 96 additions and 7 deletions

View File

@ -200,6 +200,14 @@ namespace Catch {
auto operator <= ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const { auto operator <= ( RhsT const& rhs ) -> BinaryExpr<LhsT, RhsT const&> const {
return { static_cast<bool>(m_lhs <= rhs), m_lhs, "<=", rhs }; return { static_cast<bool>(m_lhs <= rhs), m_lhs, "<=", rhs };
} }
template <typename RhsT>
auto operator | (RhsT const& rhs) -> BinaryExpr<LhsT, RhsT const&> const {
return { static_cast<bool>(m_lhs | rhs), m_lhs, "|", rhs };
}
template <typename RhsT>
auto operator & (RhsT const& rhs) -> BinaryExpr<LhsT, RhsT const&> const {
return { static_cast<bool>(m_lhs & rhs), m_lhs, "&", rhs };
}
template<typename RhsT> template<typename RhsT>
auto operator && ( RhsT const& ) -> BinaryExpr<LhsT, RhsT const&> const { auto operator && ( RhsT const& ) -> BinaryExpr<LhsT, RhsT const&> const {

View File

@ -244,6 +244,9 @@ Matchers.tests.cpp:<line number>: passed: 1, Predicate<int>(alwaysTrue, "always
Matchers.tests.cpp:<line number>: passed: 1, !Predicate<int>(alwaysFalse, "always false") for: 1 not matches predicate: "always false" Matchers.tests.cpp:<line number>: passed: 1, !Predicate<int>(alwaysFalse, "always false") for: 1 not matches predicate: "always false"
Matchers.tests.cpp:<line number>: passed: "Hello olleH", Predicate<std::string>( [] (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:<line number>: passed: "Hello olleH", Predicate<std::string>( [] (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:<line number>: passed: "This wouldn't pass", !Predicate<std::string>( [] (std::string const& str) -> bool { return str.front() == str.back(); } ) for: "This wouldn't pass" not matches undescribed predicate Matchers.tests.cpp:<line number>: passed: "This wouldn't pass", !Predicate<std::string>( [] (std::string const& str) -> bool { return str.front() == str.back(); } ) for: "This wouldn't pass" not matches undescribed predicate
Compilation.tests.cpp:<line number>: passed: lhs | rhs for: Val: 1 | Val: 2
Compilation.tests.cpp:<line number>: passed: !(lhs & rhs) for: !(Val: 1 & Val: 2)
Compilation.tests.cpp:<line number>: passed: HasBitOperators{ 1 } & HasBitOperators{ 1 } for: Val: 1 & Val: 1
Tricky.tests.cpp:<line number>: passed: true Tricky.tests.cpp:<line number>: passed: true
Tricky.tests.cpp:<line number>: passed: true Tricky.tests.cpp:<line number>: passed: true
Tricky.tests.cpp:<line number>: passed: true Tricky.tests.cpp:<line number>: passed: true

View File

@ -1380,6 +1380,6 @@ due to unexpected exception with message:
Why would you throw a std::string? Why would you throw a std::string?
=============================================================================== ===============================================================================
test cases: 309 | 235 passed | 70 failed | 4 failed as expected test cases: 310 | 236 passed | 70 failed | 4 failed as expected
assertions: 1692 | 1540 passed | 131 failed | 21 failed as expected assertions: 1695 | 1543 passed | 131 failed | 21 failed as expected

View File

@ -1934,6 +1934,27 @@ Matchers.tests.cpp:<line number>: PASSED:
with expansion: with expansion:
"This wouldn't pass" not matches undescribed predicate "This wouldn't pass" not matches undescribed predicate
-------------------------------------------------------------------------------
Assertion macros support bit operators and bool conversions
-------------------------------------------------------------------------------
Compilation.tests.cpp:<line number>
...............................................................................
Compilation.tests.cpp:<line number>: PASSED:
REQUIRE( lhs | rhs )
with expansion:
Val: 1 | Val: 2
Compilation.tests.cpp:<line number>: PASSED:
REQUIRE_FALSE( lhs & rhs )
with expansion:
!(Val: 1 & Val: 2)
Compilation.tests.cpp:<line number>: PASSED:
REQUIRE( HasBitOperators{ 1 } & HasBitOperators{ 1 } )
with expansion:
Val: 1 & Val: 1
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Assertions then sections Assertions then sections
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -13522,6 +13543,6 @@ Misc.tests.cpp:<line number>
Misc.tests.cpp:<line number>: PASSED: Misc.tests.cpp:<line number>: PASSED:
=============================================================================== ===============================================================================
test cases: 309 | 219 passed | 86 failed | 4 failed as expected test cases: 310 | 220 passed | 86 failed | 4 failed as expected
assertions: 1709 | 1540 passed | 148 failed | 21 failed as expected assertions: 1712 | 1543 passed | 148 failed | 21 failed as expected

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact <testsuitesloose text artifact
> >
<testsuite name="<exe-name>" errors="17" failures="132" tests="1710" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> <testsuite name="<exe-name>" errors="17" failures="132" tests="1713" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<properties> <properties>
<property name="filters" value="~[!nonportable]~[!benchmark]~[approvals]"/> <property name="filters" value="~[!nonportable]~[!benchmark]~[approvals]"/>
<property name="random-seed" value="1"/> <property name="random-seed" value="1"/>
@ -340,6 +340,7 @@ Exception.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="Approximate comparisons with mixed numeric types" time="{duration}"/> <testcase classname="<exe-name>.global" name="Approximate comparisons with mixed numeric types" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Arbitrary predicate matcher/Function pointer" time="{duration}"/> <testcase classname="<exe-name>.global" name="Arbitrary predicate matcher/Function pointer" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Arbitrary predicate matcher/Lambdas + different type" time="{duration}"/> <testcase classname="<exe-name>.global" name="Arbitrary predicate matcher/Lambdas + different type" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Assertion macros support bit operators and bool conversions" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Assertions then sections" time="{duration}"/> <testcase classname="<exe-name>.global" name="Assertions then sections" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Assertions then sections/A section" time="{duration}"/> <testcase classname="<exe-name>.global" name="Assertions then sections/A section" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Assertions then sections/A section/Another section" time="{duration}"/> <testcase classname="<exe-name>.global" name="Assertions then sections/A section/Another section" time="{duration}"/>

View File

@ -372,6 +372,7 @@ Class.tests.cpp:<line number>
<testCase name="#809" duration="{duration}"/> <testCase name="#809" duration="{duration}"/>
<testCase name="#833" duration="{duration}"/> <testCase name="#833" duration="{duration}"/>
<testCase name="#872" duration="{duration}"/> <testCase name="#872" duration="{duration}"/>
<testCase name="Assertion macros support bit operators and bool conversions" duration="{duration}"/>
<testCase name="Lambdas in assertions" duration="{duration}"/> <testCase name="Lambdas in assertions" duration="{duration}"/>
<testCase name="Optionally static assertions" duration="{duration}"/> <testCase name="Optionally static assertions" duration="{duration}"/>
</file> </file>

View File

@ -2177,6 +2177,33 @@ Nor would this
</Section> </Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<TestCase name="Assertion macros support bit operators and bool conversions" tags="[bitops][compilation]" filename="projects/<exe-name>/UsageTests/Compilation.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Compilation.tests.cpp" >
<Original>
lhs | rhs
</Original>
<Expanded>
Val: 1 | Val: 2
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE_FALSE" filename="projects/<exe-name>/UsageTests/Compilation.tests.cpp" >
<Original>
!(lhs &amp; rhs)
</Original>
<Expanded>
!(Val: 1 &amp; Val: 2)
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Compilation.tests.cpp" >
<Original>
HasBitOperators{ 1 } &amp; HasBitOperators{ 1 }
</Original>
<Expanded>
Val: 1 &amp; Val: 1
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="Assertions then sections" tags="[Tricky]" filename="projects/<exe-name>/UsageTests/Tricky.tests.cpp" > <TestCase name="Assertions then sections" tags="[Tricky]" filename="projects/<exe-name>/UsageTests/Tricky.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Tricky.tests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Tricky.tests.cpp" >
<Original> <Original>
@ -16180,7 +16207,7 @@ loose text artifact
</Section> </Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<OverallResults successes="1540" failures="149" expectedFailures="21"/> <OverallResults successes="1543" failures="149" expectedFailures="21"/>
</Group> </Group>
<OverallResults successes="1540" failures="148" expectedFailures="21"/> <OverallResults successes="1543" failures="148" expectedFailures="21"/>
</Catch> </Catch>

View File

@ -234,3 +234,31 @@ namespace { namespace CompilationTests {
}} // 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 });
}