X01-PrefixedMacros.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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's prefixed macros compile and run properly.
  8. *
  9. * We intentionally do not provide full coverage of all macros, but we
  10. * test a smattering and can add other ones as they have regressions.
  11. */
  12. #include <catch2/catch_test_macros.hpp>
  13. #include <catch2/matchers/catch_matchers_predicate.hpp>
  14. #include <catch2/matchers/catch_matchers_string.hpp>
  15. #include <type_traits>
  16. #include <stdexcept>
  17. namespace {
  18. [[noreturn]]
  19. void this_throws() {
  20. throw std::runtime_error("Some msg");
  21. }
  22. void this_doesnt_throw() {}
  23. }
  24. CATCH_TEST_CASE("PrefixedMacros") {
  25. using namespace Catch::Matchers;
  26. CATCH_REQUIRE( 1 == 1 );
  27. CATCH_REQUIRE_FALSE( 1 != 1 );
  28. CATCH_REQUIRE_THROWS(this_throws());
  29. CATCH_REQUIRE_THROWS_AS(this_throws(), std::runtime_error);
  30. CATCH_REQUIRE_THROWS_WITH(this_throws(), "Some msg");
  31. CATCH_REQUIRE_THROWS_MATCHES(this_throws(), std::runtime_error, Predicate<std::runtime_error>([](std::runtime_error const&) { return true; }));
  32. CATCH_REQUIRE_NOTHROW(this_doesnt_throw());
  33. CATCH_CHECK( 1 == 1 );
  34. CATCH_CHECK_FALSE( 1 != 1 );
  35. CATCH_CHECKED_IF( 1 == 1 ) {
  36. CATCH_SUCCEED("don't care");
  37. } CATCH_CHECKED_ELSE ( 1 == 1 ) {
  38. CATCH_SUCCEED("don't care");
  39. }
  40. CATCH_CHECK_NOFAIL(1 == 2);
  41. CATCH_CHECK_THROWS(this_throws());
  42. CATCH_CHECK_THROWS_AS(this_throws(), std::runtime_error);
  43. CATCH_CHECK_THROWS_WITH(this_throws(), "Some msg");
  44. CATCH_CHECK_THROWS_MATCHES(this_throws(), std::runtime_error, Predicate<std::runtime_error>([](std::runtime_error const&) { return true; }));
  45. CATCH_CHECK_NOTHROW(this_doesnt_throw());
  46. CATCH_REQUIRE_THAT("abcd", Equals("abcd"));
  47. CATCH_CHECK_THAT("bdef", Equals("bdef"));
  48. CATCH_INFO( "some info" );
  49. CATCH_UNSCOPED_INFO( "some info" );
  50. CATCH_WARN( "some warn" );
  51. CATCH_SECTION("some section") {
  52. int i = 1;
  53. CATCH_CAPTURE( i );
  54. CATCH_CAPTURE( i, i + 1 );
  55. CATCH_DYNAMIC_SECTION("Dynamic section: " << i) {
  56. CATCH_FAIL_CHECK( "failure" );
  57. }
  58. }
  59. CATCH_STATIC_REQUIRE( std::is_void<void>::value );
  60. CATCH_STATIC_REQUIRE_FALSE( std::is_void<int>::value );
  61. CATCH_STATIC_CHECK( std::is_void<void>::value );
  62. CATCH_STATIC_CHECK_FALSE( std::is_void<int>::value );
  63. CATCH_FAIL("");
  64. }
  65. // Missing:
  66. //
  67. // #define CATCH_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ )
  68. // #define CATCH_METHOD_AS_TEST_CASE( method, ... ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, __VA_ARGS__ )
  69. // #define CATCH_REGISTER_TEST_CASE( Function, ... ) INTERNAL_CATCH_REGISTER_TESTCASE( Function, __VA_ARGS__ )
  70. //
  71. // // "BDD-style" convenience wrappers
  72. // #define CATCH_SCENARIO( ... ) CATCH_TEST_CASE( "Scenario: " __VA_ARGS__ )
  73. // #define CATCH_SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, "Scenario: " __VA_ARGS__ )
  74. // #define CATCH_GIVEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Given: " << desc )
  75. // #define CATCH_WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " When: " << desc )
  76. // #define CATCH_AND_WHEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( "And when: " << desc )
  77. // #define CATCH_THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " Then: " << desc )
  78. // #define CATCH_AND_THEN( desc ) INTERNAL_CATCH_DYNAMIC_SECTION( " And: " << desc )
  79. //