aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/yson_string/unittests/convert_ut.cpp
blob: 3a64f63896c02c81ddf6c06b0d340d53af95adcb (plain) (blame)
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
#include <library/cpp/testing/gtest/gtest.h>

#include <library/cpp/testing/gtest_extensions/assertions.h>

#include <library/cpp/yt/yson_string/convert.h>

#include <thread>

namespace NYT::NYson {
namespace {

////////////////////////////////////////////////////////////////////////////////

template <class T, class R = T, class U>
void Check(const U& value)
{
    auto str = ConvertToYsonString(static_cast<T>(value));
    auto anotherValue = ConvertFromYsonString<R>(str);
    EXPECT_EQ(static_cast<T>(value), anotherValue);
}

TEST(TConvertTest, Basic)
{
    Check<i8>(13);
    Check<i32>(13);
    Check<i64>(13);
    Check<i8>(-13);
    Check<i32>(-13);
    Check<i64>(-13);
    Check<ui8>(13);
    Check<ui32>(13);
    Check<ui64>(13);
    Check<TString>("");
    Check<TString>("hello");
    Check<TStringBuf, TString>("hello");
    Check<const char*, TString>("hello");
    Check<float>(3.14);
    Check<double>(3.14);
    Check<bool>(true);
    Check<bool>(false);
    Check<TInstant>(TInstant::Now());
    Check<TDuration>(TDuration::Seconds(123));
    Check<TGuid>(TGuid::FromString("12345678-12345678-abcdabcd-fefefefe"));
}

TEST(TConvertTest, InRange)
{
    EXPECT_EQ(ConvertFromYsonString<i16>(ConvertToYsonString(static_cast<i64>(-123))), -123);
    EXPECT_EQ(ConvertFromYsonString<ui16>(ConvertToYsonString(static_cast<ui64>(123))), 123U);
}

TEST(TConvertTest, OutOfRange)
{
    EXPECT_THROW_MESSAGE_HAS_SUBSTR(
        ConvertFromYsonString<i8>(ConvertToYsonString(static_cast<i64>(128))),
        TYsonLiteralParseException,
        "is out of expected range");
    EXPECT_THROW_MESSAGE_HAS_SUBSTR(
        ConvertFromYsonString<ui8>(ConvertToYsonString(static_cast<ui64>(256))),
        TYsonLiteralParseException,
        "is out of expected range");
}

TEST(TConvertTest, MalformedValues)
{
    EXPECT_THROW_MESSAGE_HAS_SUBSTR(
        ConvertFromYsonString<TInstant>(ConvertToYsonString(TStringBuf("sometime"))),
        TYsonLiteralParseException,
        "Error parsing \"instant\" value");
    EXPECT_THROW_MESSAGE_HAS_SUBSTR(
        ConvertFromYsonString<TGuid>(ConvertToYsonString(TStringBuf("1-2-3-g"))),
        TYsonLiteralParseException,
        "Error parsing \"guid\" value");
}

////////////////////////////////////////////////////////////////////////////////

} // namespace
} // namespace NYT::NYson