mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-11-04 05:59:32 +01:00 
			
		
		
		
	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:
		@@ -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 });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user