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