X02-DisabledMacros.cpp
1 2 // Copyright Catch2 Authors 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE.txt or copy at 5 // https://www.boost.org/LICENSE_1_0.txt) 6 7 // SPDX-License-Identifier: BSL-1.0 8 9 /**\file 10 * Test that CATCH_CONFIG_DISABLE turns off TEST_CASE autoregistration 11 * and expressions in assertion macros are not run. 12 */ 13 14 15 #include <catch2/catch_test_macros.hpp> 16 #include <catch2/benchmark/catch_benchmark.hpp> 17 #include <catch2/matchers/catch_matchers.hpp> 18 #include <catch2/matchers/catch_matchers_predicate.hpp> 19 20 #include <iostream> 21 22 struct foo { 23 foo(){ 24 REQUIRE_NOTHROW( print() ); 25 } 26 void print() const { 27 std::cout << "This should not happen\n"; 28 } 29 }; 30 31 #if defined(__clang__) 32 #pragma clang diagnostic push 33 #pragma clang diagnostic ignored "-Wglobal-constructors" 34 #endif 35 // Construct foo, but `foo::print` should not be run 36 static foo f; 37 38 39 #if defined(__clang__) 40 // The test is unused since the registration is disabled 41 #pragma clang diagnostic ignored "-Wunused-function" 42 #endif 43 44 // This test should not be run, because it won't be registered 45 TEST_CASE( "Disabled Macros" ) { 46 CHECK( 1 == 2 ); 47 REQUIRE( 1 == 2 ); 48 std::cout << "This should not happen\n"; 49 FAIL(); 50 51 // Test that static assertions don't fire when macros are disabled 52 STATIC_CHECK( 0 == 1 ); 53 STATIC_REQUIRE( !true ); 54 55 CAPTURE( 1 ); 56 CAPTURE( 1, "captured" ); 57 58 REQUIRE_THAT( 1, 59 Catch::Matchers::Predicate( []( int ) { return false; } ) ); 60 BENCHMARK( "Disabled benchmark" ) { REQUIRE( 1 == 2 ); }; 61 } 62 63 #if defined(__clang__) 64 #pragma clang diagnostic pop 65 #endif