assertions.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #include <util/generic/string.h>
  3. #include <gtest/gtest.h>
  4. #include <gmock/gmock.h>
  5. /**
  6. * Check that the given statement throws an exception of the given type,
  7. * and that the thrown exception message contains the given substring.
  8. */
  9. #define EXPECT_THROW_MESSAGE_HAS_SUBSTR(statement, expectedException, substring) \
  10. _Y_GTEST_EXPECT_THROW_MESSAGE_HAS_SUBSTR_IMPL_(statement, expectedException, substring, GTEST_NONFATAL_FAILURE_)
  11. /**
  12. * Check that the given statement throws an exception of the given type,
  13. * and that the thrown exception message contains the given substring.
  14. */
  15. #define ASSERT_THROW_MESSAGE_HAS_SUBSTR(statement, expectedException, substring) \
  16. _Y_GTEST_EXPECT_THROW_MESSAGE_HAS_SUBSTR_IMPL_(statement, expectedException, substring, GTEST_FATAL_FAILURE_)
  17. // Improve default macros. New implementation shows better exception messages.
  18. // See https://github.com/google/googletest/issues/2878
  19. #undef EXPECT_THROW
  20. #define EXPECT_THROW(statement, expectedException) \
  21. _Y_GTEST_EXPECT_THROW_IMPL_(statement, expectedException, GTEST_NONFATAL_FAILURE_)
  22. #undef ASSERT_THROW
  23. #define ASSERT_THROW(statement, expectedException) \
  24. _Y_GTEST_EXPECT_THROW_IMPL_(statement, expectedException, GTEST_FATAL_FAILURE_)
  25. #undef EXPECT_NO_THROW
  26. #define EXPECT_NO_THROW(statement) \
  27. _Y_GTEST_EXPECT_NO_THROW_IMPL_(statement, GTEST_NONFATAL_FAILURE_)
  28. #undef ASSERT_NO_THROW
  29. #define ASSERT_NO_THROW(statement) \
  30. _Y_GTEST_EXPECT_NO_THROW_IMPL_(statement, GTEST_FATAL_FAILURE_)
  31. // Implementation details
  32. namespace NGTest::NInternal {
  33. TString FormatErrorWrongException(const char* statement, const char* type);
  34. TString FormatErrorWrongException(const char* statement, const char* type, TString contains);
  35. TString FormatErrorUnexpectedException(const char* statement);
  36. bool ExceptionMessageContains(const std::exception& err, TString contains);
  37. }
  38. #define _Y_GTEST_EXPECT_THROW_IMPL_(statement, expectedException, fail) \
  39. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  40. if (::TString gtestMsg = ""; ::testing::internal::AlwaysTrue()) { \
  41. bool gtestCaughtExpected = false; \
  42. try { \
  43. GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
  44. } catch (expectedException const&) { \
  45. gtestCaughtExpected = true; \
  46. } catch (...) { \
  47. gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \
  48. #statement, #expectedException); \
  49. goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
  50. } if (!gtestCaughtExpected) { \
  51. gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \
  52. #statement, #expectedException); \
  53. goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
  54. } \
  55. } else \
  56. GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
  57. fail(gtestMsg.c_str())
  58. #define _Y_GTEST_EXPECT_THROW_MESSAGE_HAS_SUBSTR_IMPL_(statement, expectedException, substring, fail) \
  59. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  60. if (::TString gtestMsg = ""; ::testing::internal::AlwaysTrue()) { \
  61. bool gtestCaughtExpected = false; \
  62. ::TString gtestSubstring{substring}; \
  63. try { \
  64. GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
  65. } catch (expectedException const& gtestError) { \
  66. if (!::NGTest::NInternal::ExceptionMessageContains(gtestError, gtestSubstring)) { \
  67. gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \
  68. #statement, #expectedException, gtestSubstring); \
  69. goto GTEST_CONCAT_TOKEN_(gtest_label_testthrowsubstr_, __LINE__); \
  70. } \
  71. gtestCaughtExpected = true; \
  72. } catch (...) { \
  73. gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \
  74. #statement, #expectedException, gtestSubstring); \
  75. goto GTEST_CONCAT_TOKEN_(gtest_label_testthrowsubstr_, __LINE__); \
  76. } if (!gtestCaughtExpected) { \
  77. gtestMsg = ::NGTest::NInternal::FormatErrorWrongException( \
  78. #statement, #expectedException, gtestSubstring); \
  79. goto GTEST_CONCAT_TOKEN_(gtest_label_testthrowsubstr_, __LINE__); \
  80. } \
  81. } else \
  82. GTEST_CONCAT_TOKEN_(gtest_label_testthrowsubstr_, __LINE__): \
  83. fail(gtestMsg.c_str())
  84. #define _Y_GTEST_EXPECT_NO_THROW_IMPL_(statement, fail) \
  85. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  86. if (::TString gtestMsg = ""; ::testing::internal::AlwaysTrue()) { \
  87. try { \
  88. GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
  89. } catch (...) { \
  90. gtestMsg = ::NGTest::NInternal::FormatErrorUnexpectedException(#statement); \
  91. goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \
  92. } \
  93. } else \
  94. GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \
  95. fail(gtestMsg.c_str())