error_code_ut.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <yt/yt/core/test_framework/framework.h>
  2. #include <library/cpp/yt/error/error.h>
  3. #include <library/cpp/yt/error/error_code.h>
  4. #include <library/cpp/yt/string/format.h>
  5. #include <ostream>
  6. ////////////////////////////////////////////////////////////////////////////////
  7. YT_DEFINE_ERROR_ENUM(
  8. ((Global1) (-5))
  9. ((Global2) (-6))
  10. );
  11. namespace NExternalWorld {
  12. ////////////////////////////////////////////////////////////////////////////////
  13. YT_DEFINE_ERROR_ENUM(
  14. ((X) (-11))
  15. ((Y) (-22))
  16. ((Z) (-33))
  17. );
  18. ////////////////////////////////////////////////////////////////////////////////
  19. } // namespace NExternalWorld
  20. namespace NYT {
  21. void PrintTo(const TErrorCodeRegistry::TErrorCodeInfo& errorCodeInfo, std::ostream* os)
  22. {
  23. *os << ToString(errorCodeInfo);
  24. }
  25. namespace NInternalLittleWorld {
  26. ////////////////////////////////////////////////////////////////////////////////
  27. YT_DEFINE_ERROR_ENUM(
  28. ((A) (-1))
  29. ((B) (-2))
  30. ((C) (-3))
  31. ((D) (-4))
  32. );
  33. ////////////////////////////////////////////////////////////////////////////////
  34. } // namespace NMyOwnLittleWorld
  35. namespace {
  36. ////////////////////////////////////////////////////////////////////////////////
  37. YT_DEFINE_ERROR_ENUM(
  38. ((Kek) (-57))
  39. ((Haha) (-179))
  40. ((Muahaha) (-1543))
  41. ((Kukarek) (-2007))
  42. );
  43. TString TestErrorCodeFormatter(int code)
  44. {
  45. return Format("formatted%v", code);
  46. }
  47. YT_DEFINE_ERROR_CODE_RANGE(-4399, -4200, "NYT::Test", TestErrorCodeFormatter);
  48. DEFINE_ENUM(EDifferentTestErrorCode,
  49. ((ErrorNumberOne) (-10000))
  50. ((ErrorNumberTwo) (-10001))
  51. ((ErrorNumberThree) (-10002))
  52. );
  53. TString DifferentTestErrorCodeFormatter(int code)
  54. {
  55. return TEnumTraits<EDifferentTestErrorCode>::ToString(static_cast<EDifferentTestErrorCode>(code));
  56. }
  57. YT_DEFINE_ERROR_CODE_RANGE(-10005, -10000, "NYT::DifferentTest", DifferentTestErrorCodeFormatter);
  58. TEST(TErrorCodeRegistryTest, Basic)
  59. {
  60. #ifdef _unix_
  61. EXPECT_EQ(
  62. TErrorCodeRegistry::Get()->Get(-1543),
  63. (TErrorCodeRegistry::TErrorCodeInfo{"NYT::(anonymous namespace)", "Muahaha"}));
  64. #else
  65. EXPECT_EQ(
  66. TErrorCodeRegistry::Get()->Get(-1543),
  67. (TErrorCodeRegistry::TErrorCodeInfo{"NYT::`anonymous namespace'", "Muahaha"}));
  68. #endif
  69. EXPECT_EQ(
  70. TErrorCodeRegistry::Get()->Get(-3),
  71. (TErrorCodeRegistry::TErrorCodeInfo{"NYT::NInternalLittleWorld", "C"}));
  72. EXPECT_EQ(
  73. TErrorCodeRegistry::Get()->Get(-33),
  74. (TErrorCodeRegistry::TErrorCodeInfo{"NExternalWorld", "Z"}));
  75. EXPECT_EQ(
  76. TErrorCodeRegistry::Get()->Get(-5),
  77. (TErrorCodeRegistry::TErrorCodeInfo{"", "Global1"}));
  78. EXPECT_EQ(
  79. TErrorCodeRegistry::Get()->Get(-4300),
  80. (TErrorCodeRegistry::TErrorCodeInfo{"NYT::Test", "formatted-4300"}));
  81. EXPECT_EQ(
  82. TErrorCodeRegistry::Get()->Get(-10002),
  83. (TErrorCodeRegistry::TErrorCodeInfo{"NYT::DifferentTest", "ErrorNumberThree"}));
  84. EXPECT_EQ(
  85. TErrorCodeRegistry::Get()->Get(-10005),
  86. (TErrorCodeRegistry::TErrorCodeInfo{"NYT::DifferentTest", "EDifferentTestErrorCode(-10005)"}));
  87. EXPECT_EQ(
  88. TErrorCodeRegistry::Get()->Get(-111),
  89. (TErrorCodeRegistry::TErrorCodeInfo{"NUnknown", "ErrorCode-111"}));
  90. }
  91. DEFINE_ENUM(ETestEnumOne,
  92. ((VariantOne) (0))
  93. ((VariantTwo) (1))
  94. );
  95. DEFINE_ENUM(ETestEnumTwo,
  96. ((DifferentVariantOne) (0))
  97. ((DifferentVariantTwo) (1))
  98. );
  99. template <class T, class K>
  100. concept EquallyComparable = requires(T a, K b)
  101. {
  102. { static_cast<T>(0) == static_cast<K>(0) };
  103. };
  104. TEST(TErrorCodeTest, ImplicitCastTest)
  105. {
  106. // assert TErrorCode is in scope
  107. using NYT::TErrorCode;
  108. bool equallyComparable = EquallyComparable<ETestEnumOne, ETestEnumTwo>;
  109. EXPECT_FALSE(equallyComparable);
  110. }
  111. ////////////////////////////////////////////////////////////////////////////////
  112. } // namespace
  113. } // namespace NYT