diff options
author | kmokrov <kmokrov@yandex-team.com> | 2023-10-27 10:15:09 +0300 |
---|---|---|
committer | kmokrov <kmokrov@yandex-team.com> | 2023-10-27 10:32:00 +0300 |
commit | d470ad823626c4d1be8216b0c9eabc553e2bea7c (patch) | |
tree | 9523f0dabf308a3757291572402c99b682df7fda | |
parent | 83b0a89a4e2d525f6de990f2e1e1478d188cbbb5 (diff) | |
download | ydb-d470ad823626c4d1be8216b0c9eabc553e2bea7c.tar.gz |
YTORM-214: Add unittests for pr-4688904
-rw-r--r-- | library/cpp/yt/misc/cast-inl.h | 2 | ||||
-rw-r--r-- | library/cpp/yt/misc/unittests/enum_ut.cpp | 31 | ||||
-rw-r--r-- | yt/yt/core/ytree/unittests/serialize_ut.cpp | 25 |
3 files changed, 56 insertions, 2 deletions
diff --git a/library/cpp/yt/misc/cast-inl.h b/library/cpp/yt/misc/cast-inl.h index 71049a47485..ceacda91bd6 100644 --- a/library/cpp/yt/misc/cast-inl.h +++ b/library/cpp/yt/misc/cast-inl.h @@ -4,6 +4,8 @@ #include "cast.h" #endif +#include "enum.h" + #include <util/string/cast.h> #include <util/string/printf.h> diff --git a/library/cpp/yt/misc/unittests/enum_ut.cpp b/library/cpp/yt/misc/unittests/enum_ut.cpp index 1b1b6d0be23..63b8666ae1e 100644 --- a/library/cpp/yt/misc/unittests/enum_ut.cpp +++ b/library/cpp/yt/misc/unittests/enum_ut.cpp @@ -1,5 +1,6 @@ #include <library/cpp/testing/gtest/gtest.h> +#include <library/cpp/yt/misc/cast.h> #include <library/cpp/yt/misc/enum.h> namespace NYT { @@ -42,6 +43,13 @@ DEFINE_ENUM(ECustomString, ((B) (2) ("1_b")) ); +DEFINE_ENUM_WITH_UNDERLYING_TYPE(ECardinal, char, + ((West) (0)) + ((North) (1)) + ((East) (2)) + ((South) (3)) +); + //////////////////////////////////////////////////////////////////////////////// template <class T, size_t N> @@ -249,6 +257,29 @@ TEST(TEnumTest, CustomString) EXPECT_EQ("1_b", ToString(ECustomString::B)); } +TEST(TEnumTest, Cast) +{ + ECardinal cardinal; + { + char validValue = 2; + EXPECT_TRUE(TryEnumCast(validValue, &cardinal)); + EXPECT_EQ(cardinal, ECardinal::East); + } + { + char invalidValue = 100; + EXPECT_FALSE(TryEnumCast(invalidValue, &cardinal)); + } + { + int widerTypeValidValue = 3; + EXPECT_TRUE(TryEnumCast(widerTypeValidValue, &cardinal)); + EXPECT_EQ(cardinal, ECardinal::South); + } + { + int widerTypeInvalueValue = (1 << 8) + 100; + EXPECT_FALSE(TryEnumCast(widerTypeInvalueValue, &cardinal)); + } +} + //////////////////////////////////////////////////////////////////////////////// } // namespace diff --git a/yt/yt/core/ytree/unittests/serialize_ut.cpp b/yt/yt/core/ytree/unittests/serialize_ut.cpp index d0a3f4b733e..898eadf5d94 100644 --- a/yt/yt/core/ytree/unittests/serialize_ut.cpp +++ b/yt/yt/core/ytree/unittests/serialize_ut.cpp @@ -41,8 +41,8 @@ using namespace NYson; //////////////////////////////////////////////////////////////////////////////// DEFINE_ENUM(ETestEnum, - (One) - (FortyTwo) + ((One) (1) ) + ((FortyTwo) (42)) ); DEFINE_BIT_ENUM(ETestBitEnum, @@ -532,6 +532,27 @@ TEST(TSerializationTest, PointersFromEntity) check("intrusive:FromNode", intrusiveFromPullParser); } +TEST(TDeserializeTest, Enums) +{ + { + auto originalYson = BuildYsonNodeFluently() + .BeginList() + .Item().Value(1) + .Item().Value("forty_two") + .EndList(); + auto expectedEnums = std::vector<ETestEnum>{ETestEnum::One, ETestEnum::FortyTwo}; + + std::vector<ETestEnum> deserialized; + Deserialize(deserialized, originalYson); + EXPECT_EQ(deserialized, expectedEnums); + + deserialized.clear(); + EXPECT_THROW( + Deserialize(deserialized, BuildYsonNodeFluently().BeginList().Item().Value(1.42).EndList()), + std::exception); + } +} + //////////////////////////////////////////////////////////////////////////////// } // namespace |