aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/object_factory/object_factory_ut.cpp
blob: 06fb0739ff0ab2bd475e4db6d60570cc8fe0c09d (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <library/cpp/object_factory/object_factory.h>
#include <library/cpp/testing/unittest/registar.h>

#include <util/generic/noncopyable.h>
#include <util/generic/string.h>
#include <util/generic/ptr.h>

using namespace NObjectFactory;

struct TArgument {
    TString Name;
    void* Discarded;
};

class ICommonInterface {
public:
    virtual ~ICommonInterface() {
    }

    virtual TString GetValue() const = 0;
};

class TDirectOrder: public ICommonInterface {
public:
    TDirectOrder(const TString& provider, float factor, TArgument& argument)
        : Provider(provider)
        , Factor(factor)
        , Argument(argument)
    {
    }

    TString GetValue() const override {
        return Provider + ToString(Factor) + Argument.Name;
    }

private:
    const TString Provider;
    const float Factor;
    const TArgument Argument;
};

class TInverseOrder: public ICommonInterface {
public:
    TInverseOrder(const TString& provider, float factor, TArgument& argument)
        : Provider(provider)
        , Factor(factor)
        , Argument(argument)
    {
    }

    TString GetValue() const override {
        return Argument.Name + ToString(Factor) + Provider;
    }

private:
    const TString Provider;
    const float Factor;
    const TArgument Argument;
};

struct TDirectOrderCreator: public IFactoryObjectCreator<ICommonInterface, const TString&, float, TArgument&> {
    ICommonInterface* Create(const TString& provider, float factor, TArgument& argument) const override {
        ++CallsCounter;
        return new TDirectOrder(provider, factor, argument);
    }

    static int CallsCounter;
};
int TDirectOrderCreator::CallsCounter = 0;

using TTestFactory = TParametrizedObjectFactory<ICommonInterface, TString, const TString&, float, TArgument&>;

static TTestFactory::TRegistrator<TDirectOrder> Direct("direct", new TDirectOrderCreator);
static TTestFactory::TRegistrator<TInverseOrder> Inverse("inverse");



class IMoveableOnlyInterface {
public:
    virtual ~IMoveableOnlyInterface() {
    }

    virtual TString GetValue() const = 0;
};

class TMoveableOnly: public IMoveableOnlyInterface, public TMoveOnly {
public:
    TMoveableOnly(TString&& value)
        : Value(value)
    {}

    TString GetValue() const override {
        return Value;
    }

private:
    const TString Value;
};


using TMoveableOnlyFactory = TParametrizedObjectFactory<IMoveableOnlyInterface, TString, TString&&>;

static TMoveableOnlyFactory::TRegistrator<TMoveableOnly> MoveableOnlyReg("move");



class TMoveableOnly2: public IMoveableOnlyInterface, public TMoveOnly {
public:
    TMoveableOnly2(THolder<TString>&& value)
        : Value(std::move(value))
    {}

    TString GetValue() const override {
        return *Value;
    }

private:
    const THolder<TString> Value;
};


using TMoveableOnly2Factory = TParametrizedObjectFactory<IMoveableOnlyInterface, TString, THolder<TString>&&>;

static TMoveableOnly2Factory::TRegistrator<TMoveableOnly2> MoveableOnly2Reg("move2");

class TDirectOrderDifferentSignature : public TDirectOrder {
public:
    TDirectOrderDifferentSignature(const TString& provider, TArgument& argument) :
        TDirectOrder(provider, 0.01f, argument)
    {
    }

};

struct TDirectOrderDSCreator: public IFactoryObjectCreator<ICommonInterface, const TString&, float, TArgument&> {
    ICommonInterface* Create(const TString& provider, float factor, TArgument& argument) const override {
        Y_UNUSED(factor);
        return new TDirectOrderDifferentSignature(provider, argument);
    }
};


static TTestFactory::TRegistrator<TDirectOrderDifferentSignature> DirectDs("direct_ds", new TDirectOrderDSCreator);

Y_UNIT_TEST_SUITE(TestObjectFactory) {
    Y_UNIT_TEST(TestParametrized) {
        TArgument directArg{"Name", nullptr};
        TArgument inverseArg{"Fake", nullptr};
        THolder<ICommonInterface> direct(TTestFactory::Construct("direct", "prov", 0.42, directArg));
        THolder<ICommonInterface> inverse(TTestFactory::Construct("inverse", "prov2", 1, inverseArg));

        UNIT_ASSERT(!!direct);
        UNIT_ASSERT(!!inverse);

        UNIT_ASSERT(direct->GetValue() == "prov0.42Name");
        UNIT_ASSERT(inverse->GetValue() == "Fake1prov2");

        UNIT_ASSERT_EQUAL(TDirectOrderCreator::CallsCounter, 1);
    }

    Y_UNIT_TEST(TestMoveableOnly) {
        TString v = "value1";

        THolder<IMoveableOnlyInterface> moveableOnly(TMoveableOnlyFactory::Construct("move", std::move(v)));

        UNIT_ASSERT(!!moveableOnly);

        UNIT_ASSERT(moveableOnly->GetValue() == "value1");
    }

    Y_UNIT_TEST(TestMoveableOnly2) {
        THolder<TString> v = MakeHolder<TString>("value2");

        THolder<IMoveableOnlyInterface> moveableOnly2(TMoveableOnly2Factory::Construct("move2", std::move(v)));

        UNIT_ASSERT(!!moveableOnly2);

        UNIT_ASSERT(moveableOnly2->GetValue() == "value2");
    }

    Y_UNIT_TEST(TestDifferentSignature) {
        TArgument directArg{"Name", nullptr};
        THolder<ICommonInterface> directDs(TTestFactory::Construct("direct_ds", "prov", 0.42, directArg));

        UNIT_ASSERT(!!directDs);

        UNIT_ASSERT_EQUAL(directDs->GetValue(), "prov0.01Name");
    }
}