X02-DisabledMacros.cpp 1.6 KB

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