aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/json/ut/proto.h
blob: 53454cd950e9f32049aaf5ec87f801f2b960d0a2 (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
#pragma once

#include <util/generic/hash_set.h>
#include <util/generic/string.h> 

#include <util/system/defaults.h>

namespace NProtobufJsonTest {
    template <typename TProto>
    inline void
    FillFlatProto(TProto* proto,
                  const THashSet<TString>& skippedFields = THashSet<TString>()) {
#define DEFINE_FIELD(name, value)                         \
    if (skippedFields.find(#name) == skippedFields.end()) \
        proto->Set##name(value);
#include <library/cpp/protobuf/json/ut/fields.incl>
#undef DEFINE_FIELD
    }

    template <typename TRepeatedField, typename TValue>
    inline void
    AddValue(TRepeatedField* field, TValue value) {
        field->Add(value);
    }

    inline void
    AddValue(google::protobuf::RepeatedPtrField<TString>* field, const TString& value) {
        *(field->Add()) = value;
    }

    inline void
    FillRepeatedProto(TFlatRepeated* proto,
                      const THashSet<TString>& skippedFields = THashSet<TString>()) {
#define DEFINE_REPEATED_FIELD(name, type, ...)                         \
    if (skippedFields.find(#name) == skippedFields.end()) {            \
        type values[] = {__VA_ARGS__};                                 \
        for (size_t i = 0, end = Y_ARRAY_SIZE(values); i < end; ++i) { \
            AddValue(proto->Mutable##name(), values[i]);               \
        }                                                              \
    }
#include <library/cpp/protobuf/json/ut/repeated_fields.incl>
#undef DEFINE_REPEATED_FIELD
    }

    template <typename TProto>
    inline void
    FillCompositeProto(TProto* proto, const THashSet<TString>& skippedFields = THashSet<TString>()) {
        FillFlatProto(proto->MutablePart(), skippedFields);
    }

#define UNIT_ASSERT_PROTOS_EQUAL(lhs, rhs)                                               \
    do {                                                                                 \
        if (lhs.SerializeAsString() != rhs.SerializeAsString()) {                        \
            Cerr << ">>>>>>>>>> lhs != rhs:" << Endl;                                    \
            Cerr << lhs.DebugString() << Endl;                                           \
            Cerr << rhs.DebugString() << Endl;                                           \
            UNIT_ASSERT_STRINGS_EQUAL(lhs.DebugString(), rhs.DebugString());             \
            UNIT_ASSERT_STRINGS_EQUAL(lhs.SerializeAsString(), rhs.SerializeAsString()); \
        }                                                                                \
    } while (false);

}