aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/util/is_equal_ut.cpp
blob: 96e77c95c61776698834c449780017bf96c34526 (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
#include "is_equal.h"
#include <library/cpp/protobuf/util/ut/sample_for_is_equal.pb.h>

#include <library/cpp/testing/unittest/registar.h>

#include <google/protobuf/descriptor.h>

Y_UNIT_TEST_SUITE(ProtobufIsEqual) { 
    const ::google::protobuf::Descriptor* Descr = TSampleForIsEqual::descriptor();
    const ::google::protobuf::FieldDescriptor* NameDescr = Descr->field(0);
    const ::google::protobuf::FieldDescriptor* InnerDescr = Descr->field(1);

    Y_UNIT_TEST(CheckDescriptors) { 
        UNIT_ASSERT(Descr);
        UNIT_ASSERT(NameDescr);
        UNIT_ASSERT_VALUES_EQUAL(NameDescr->name(), "Name");
        UNIT_ASSERT_VALUES_EQUAL(InnerDescr->name(), "Inner");
    }

    Y_UNIT_TEST(IsEqual1) { 
        TSampleForIsEqual a;
        TSampleForIsEqual b;

        a.SetName("aaa");
        b.SetName("bbb");

        TString path;

        bool equal = NProtoBuf::IsEqual(a, b, &path);
        UNIT_ASSERT(!equal);
        UNIT_ASSERT_VALUES_EQUAL("Name", path);

        UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *NameDescr));
    }

    Y_UNIT_TEST(IsEqual2) { 
        TSampleForIsEqual a;
        TSampleForIsEqual b;

        a.MutableInner()->SetBrbrbr("aaa");
        b.MutableInner()->SetBrbrbr("bbb");

        TString path;

        bool equal = NProtoBuf::IsEqual(a, b, &path);
        UNIT_ASSERT(!equal);
        UNIT_ASSERT_VALUES_EQUAL("Inner/Brbrbr", path);

        bool equalField = NProtoBuf::IsEqualField(a, b, *InnerDescr);
        UNIT_ASSERT(!equalField);
    }

    Y_UNIT_TEST(IsEqual3) { 
        TSampleForIsEqual a;
        TSampleForIsEqual b;

        a.SetName("aaa");
        a.MutableInner()->SetBrbrbr("bbb");

        b.SetName("aaa");
        b.MutableInner()->SetBrbrbr("bbb");

        TString path;

        UNIT_ASSERT(NProtoBuf::IsEqual(a, b));
        UNIT_ASSERT(NProtoBuf::IsEqualField(a, b, *NameDescr));
        UNIT_ASSERT(NProtoBuf::IsEqualField(a, b, *InnerDescr));

        b.MutableInner()->SetBrbrbr("ccc");
        UNIT_ASSERT(!NProtoBuf::IsEqual(a, b));
        UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *InnerDescr));

        b.SetName("ccc");
        UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *NameDescr));
    }

    Y_UNIT_TEST(IsEqualDefault) { 
        TSampleForIsEqual a;
        TSampleForIsEqual b;

        a.SetName("");
        UNIT_ASSERT(NProtoBuf::IsEqualDefault(a, b));
        UNIT_ASSERT(!NProtoBuf::IsEqual(a, b));

        UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *NameDescr));
        UNIT_ASSERT(NProtoBuf::IsEqualFieldDefault(a, b, *NameDescr));
    }
}