gtest.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. // WARNING: this is a legacy header that tries to mimic the gtest interface while using unittest
  3. // under the hood. Avoid using this interface -- use the genuine gtest instead (the GTEST macro).
  4. // If you're already using GTEST macro and you've found yourself here, you probably meant
  5. // to include `library/cpp/testing/gtest/gtest.h`.
  6. #include "registar.h"
  7. #include <util/generic/ymath.h>
  8. #include <util/generic/ylimits.h>
  9. namespace NUnitTest {
  10. namespace NPrivate {
  11. struct IGTestFactory: public ITestBaseFactory {
  12. ~IGTestFactory() override;
  13. virtual void AddTest(const char* name, void (*body)(TTestContext&), bool forceFork) = 0;
  14. };
  15. IGTestFactory* ByName(const char* name);
  16. }
  17. }
  18. namespace NTesting {
  19. struct TTest {
  20. virtual void SetUp() {
  21. }
  22. virtual void TearDown() {
  23. }
  24. inline TTest* _This() noexcept {
  25. return this;
  26. }
  27. virtual ~TTest() = default;
  28. };
  29. }
  30. namespace testing {
  31. struct Test: public ::NTesting::TTest {
  32. };
  33. }
  34. #define TEST_IMPL(N, NN, FF) \
  35. void Test##N##NN(NUnitTest::TTestContext&); \
  36. namespace NTestSuite##N##NN { \
  37. struct TReg { \
  38. inline TReg() { \
  39. ::NUnitTest::NPrivate::ByName(#N)->AddTest(#NN, &(Test##N##NN), FF); \
  40. } \
  41. }; \
  42. static TReg reg; \
  43. } \
  44. void Test##N##NN(NUnitTest::TTestContext&)
  45. #define TEST_F_IMPL(N, NN, FF) \
  46. namespace NTestSuite##N##NN { \
  47. struct TTestSuite: public N { \
  48. inline TTestSuite() { \
  49. this->_This()->SetUp(); \
  50. } \
  51. inline ~TTestSuite() { \
  52. this->_This()->TearDown(); \
  53. } \
  54. void NN(); \
  55. }; \
  56. } \
  57. TEST_IMPL(N, NN, FF) { \
  58. NTestSuite##N##NN::TTestSuite().NN(); \
  59. } \
  60. void NTestSuite##N##NN::TTestSuite::NN()
  61. #define TEST(A, B) TEST_IMPL(A, B, false)
  62. #define TEST_FORKED(A, B) TEST_IMPL(A, B, true)
  63. #define TEST_F(A, B) TEST_F_IMPL(A, B, false)
  64. #define TEST_F_FORKED(A, B) TEST_F_IMPL(A, B, true)
  65. #define EXPECT_EQ(A, B) UNIT_ASSERT_VALUES_EQUAL(A, B)
  66. #define EXPECT_NE(A, B) UNIT_ASSERT_UNEQUAL(A, B)
  67. #define EXPECT_LE(A, B) UNIT_ASSERT((A) <= (B))
  68. #define EXPECT_LT(A, B) UNIT_ASSERT((A) < (B))
  69. #define EXPECT_GE(A, B) UNIT_ASSERT((A) >= (B))
  70. #define EXPECT_GT(A, B) UNIT_ASSERT((A) > (B))
  71. #define EXPECT_NO_THROW(A) UNIT_ASSERT_NO_EXCEPTION(A)
  72. #define EXPECT_THROW(A, B) UNIT_ASSERT_EXCEPTION(A, B)
  73. #define EXPECT_NEAR(A, B, D) UNIT_ASSERT_DOUBLES_EQUAL(A, B, D)
  74. #define EXPECT_STREQ(A, B) UNIT_ASSERT_VALUES_EQUAL(A, B)
  75. #define EXPECT_DOUBLE_EQ_TOLERANCE(A, B, tolerance) UNIT_ASSERT_C(fabs((A) - (B)) < tolerance * std::numeric_limits<decltype(A)>::epsilon(), TString("\n") + ToString(A) + " <> " + ToString(B))
  76. #define EXPECT_DOUBLE_EQ(A, B) EXPECT_DOUBLE_EQ_TOLERANCE(A, B, 4.0)
  77. //conflicts with util/system/defaults.h
  78. #undef EXPECT_TRUE
  79. #define EXPECT_TRUE(X) UNIT_ASSERT(X)
  80. #undef EXPECT_FALSE
  81. #define EXPECT_FALSE(X) UNIT_ASSERT(!(X))
  82. #define ASSERT_EQ(A, B) EXPECT_EQ(A, B)
  83. #define ASSERT_NE(A, B) EXPECT_NE(A, B)
  84. #define ASSERT_GT(A, B) EXPECT_GT(A, B)
  85. #define ASSERT_LT(A, B) EXPECT_LT(A, B)
  86. #define ASSERT_FALSE(X) EXPECT_FALSE(X)
  87. #define ASSERT_TRUE(X) EXPECT_TRUE(X)
  88. #define ASSERT_THROW(A, B) EXPECT_THROW(A, B)
  89. #define ASSERT_NO_THROW(A) EXPECT_NO_THROW(A)
  90. #define ASSERT_DOUBLE_EQ(A, B) EXPECT_DOUBLE_EQ(A, B)
  91. #define ASSERT_STREQ(A, B) EXPECT_STREQ(A, B)