aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/dbg_output/ut/dbg_output_ut.cpp
blob: 54ba4416ec6aceda249dc21cf4282ff01acfb3b0 (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
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
#include <library/cpp/dbg_output/dump.h>
#include <library/cpp/testing/unittest/registar.h>

#include <util/stream/str.h>
#include <util/string/builder.h>
#include <util/string/escape.h>
#include <util/generic/map.h>

namespace {
    struct TX {
        inline TX() {
            N = this;
        }

        TX* N;
    };
}

template <>
struct TDumper<TX> {
    template <class S>
    static inline void Dump(S& s, const TX& x) {
        s << DumpRaw("x") << x.N;
    }
};

namespace TMyNS {
    struct TMyStruct {
        int A, B;
    };
}
DEFINE_DUMPER(TMyNS::TMyStruct, A, B)

Y_UNIT_TEST_SUITE(TContainerPrintersTest) {
    Y_UNIT_TEST(TestVectorInt) {
        TStringStream out;
        out << DbgDump(TVector<int>({1, 2, 3, 4, 5}));
        UNIT_ASSERT_STRINGS_EQUAL(out.Str(), "[1, 2, 3, 4, 5]");
    }

    Y_UNIT_TEST(TestMapCharToCharArray) {
        TStringStream out;

        TMap<char, const char*> m;

        m['a'] = "SMALL LETTER A";
        m['b'] = nullptr;

        out << DbgDump(m);

        UNIT_ASSERT_STRINGS_EQUAL(out.Str(), "{'a' -> \"SMALL LETTER A\", 'b' -> (empty)}");
    }

    Y_UNIT_TEST(TestVectorOfVectors) {
        TStringStream out;
        TVector<TVector<wchar16>> vec(2);
        vec[0].push_back(0);
        vec[1] = {wchar16('a')}; 
        out << DbgDump(vec);
        UNIT_ASSERT_STRINGS_EQUAL(out.Str(), "[[w'\\0'], [w'a']]");
    }

    Y_UNIT_TEST(TestInfinite) {
        UNIT_ASSERT(!!(TStringBuilder() << DbgDumpDeep(TX())));
    }

    Y_UNIT_TEST(TestLabeledDump) {
        TStringStream out;
        int a = 1, b = 2;
        out << LabeledDump(a, b, 1 + 2);
        UNIT_ASSERT_STRINGS_EQUAL(out.Str(), "{\"a\": 1, \"b\": 2, \"1 + 2\": 3}");
    }

    Y_UNIT_TEST(TestStructDumper) {
        TStringStream out;
        out << DbgDump(TMyNS::TMyStruct{3, 4});
        UNIT_ASSERT_STRINGS_EQUAL(out.Str(), "{\"A\": 3, \"B\": 4}");
    }

    Y_UNIT_TEST(TestColors) {
        using TComplex = TMap<TString, TMap<int, char>>;
        TComplex test;
        test["a"][1] = '7';
        test["b"][2] = '6';
        TStringStream out;
        out << DbgDump<TComplex, NDbgDump::NColorScheme::TEyebleed</* Enforce = */ true>>(test);
        UNIT_ASSERT_STRINGS_EQUAL(
            EscapeC(out.Str()),
            "\\x1B[1;32m{\\x1B[1;31m\\x1B[42m\\x1B[1;31m\\x1B[1;33m\\\"a\\\"\\x1B[22;39m\\x1B[22;39m"
            "\\x1B[49m\\x1B[1;32m -> \\x1B[44m\\x1B[1;31m\\x1B[1;32m{\\x1B[1;31m\\x1B[1;31m1"
            "\\x1B[22;39m\\x1B[1;32m -> \\x1B[1;31m'7'\\x1B[22;39m\\x1B[1;32m}"
            "\\x1B[22;39m\\x1B[22;39m\\x1B[49m\\x1B[1;32m, \\x1B[1;31m\\x1B[42m\\x1B[1;31m\\x1B[1;33m"
            "\\\"b\\\"\\x1B[22;39m\\x1B[22;39m\\x1B[49m\\x1B[1;32m -> "
            "\\x1B[44m\\x1B[1;31m\\x1B[1;32m{\\x1B[1;31m\\x1B[1;31m2\\x1B[22;39m\\x1B[1;32m -> "
            "\\x1B[1;31m'6'\\x1B[22;39m\\x1B[1;32m}\\x1B[22;39m\\x1B[22;39m\\x1B[49m\\x1B[1;32m}\\x1B[22;39m");
    }

    Y_UNIT_TEST(SmallIntOrChar) {
        char c = 'e';
        i8 i = -100;
        ui8 u = 10;
        UNIT_ASSERT_VALUES_EQUAL(TStringBuilder() << DbgDump(c), "'e'");
        UNIT_ASSERT_VALUES_EQUAL(TStringBuilder() << DbgDump(i), "-100");
        UNIT_ASSERT_VALUES_EQUAL(TStringBuilder() << DbgDump(u), "10");
    }
}