Details.tests.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include <catch2/catch_test_macros.hpp>
  7. #include <catch2/internal/catch_enforce.hpp>
  8. #include <catch2/internal/catch_case_insensitive_comparisons.hpp>
  9. #include <catch2/internal/catch_optional.hpp>
  10. #if defined(_MSC_VER)
  11. #pragma warning(push)
  12. #pragma warning(disable:4702) // unreachable code in the macro expansions
  13. #endif
  14. TEST_CASE("Check that our error handling macros throw the right exceptions", "[!throws][internals][approvals]") {
  15. REQUIRE_THROWS_AS(CATCH_INTERNAL_ERROR(""), std::logic_error);
  16. REQUIRE_THROWS_AS(CATCH_ERROR(""), std::domain_error);
  17. REQUIRE_THROWS_AS(CATCH_RUNTIME_ERROR(""), std::runtime_error);
  18. REQUIRE_THROWS_AS([](){CATCH_ENFORCE(false, "");}(), std::domain_error);
  19. REQUIRE_NOTHROW([](){CATCH_ENFORCE(true, "");}());
  20. }
  21. #if defined(_MSC_VER)
  22. #pragma warning(pop) // unreachable code in the macro expansions
  23. #endif
  24. TEST_CASE("CaseInsensitiveLess is case insensitive", "[comparisons][string-case]") {
  25. Catch::Detail::CaseInsensitiveLess lt;
  26. SECTION( "Degenerate cases" ) {
  27. REQUIRE( lt( "", "a" ) );
  28. REQUIRE_FALSE( lt( "a", "a" ) );
  29. REQUIRE_FALSE( lt( "", "" ) );
  30. }
  31. SECTION("Plain comparisons") {
  32. REQUIRE( lt( "a", "b" ) );
  33. REQUIRE( lt( "a", "B" ) );
  34. REQUIRE( lt( "A", "b" ) );
  35. REQUIRE( lt( "A", "B" ) );
  36. }
  37. }
  38. TEST_CASE( "CaseInsensitiveEqualsTo is case insensitive",
  39. "[comparisons][string-case]" ) {
  40. Catch::Detail::CaseInsensitiveEqualTo eq;
  41. SECTION( "Degenerate cases" ) {
  42. REQUIRE( eq( "", "" ) );
  43. REQUIRE_FALSE( eq( "", "a" ) );
  44. }
  45. SECTION( "Plain comparisons" ) {
  46. REQUIRE( eq( "a", "a" ) );
  47. REQUIRE( eq( "a", "A" ) );
  48. REQUIRE( eq( "A", "a" ) );
  49. REQUIRE( eq( "A", "A" ) );
  50. REQUIRE_FALSE( eq( "a", "b" ) );
  51. REQUIRE_FALSE( eq( "a", "B" ) );
  52. }
  53. }
  54. TEST_CASE("Optional comparison ops", "[optional][approvals]") {
  55. using Catch::Optional;
  56. Optional<int> a, b;
  57. SECTION( "Empty optionals are equal" ) {
  58. REQUIRE( a == b );
  59. REQUIRE_FALSE( a != b );
  60. }
  61. SECTION( "Empty and non-empty optionals are never equal" ) {
  62. a = 1;
  63. REQUIRE_FALSE( a == b );
  64. REQUIRE( a != b );
  65. }
  66. SECTION(
  67. "non-empty optionals are equal if the contained elements are equal") {
  68. a = 1;
  69. b = 2;
  70. REQUIRE( a != b );
  71. REQUIRE_FALSE( a == b );
  72. a = 2;
  73. REQUIRE( a == b );
  74. REQUIRE_FALSE( a != b );
  75. }
  76. }