Debian Codegraph @

test/unit/provider-by-factory-test.cpp

raw symbols SCIP
1 /*
2 * %injeqt copyright begin%
3 * Copyright 2014 Rafał Malinowski ([email protected])
4 * %injeqt copyright end%
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "expect.h"
22
23 #include <injeqt/exception/instantiation-failed.h>
24 #include <injeqt/module.h>
25
26 #include "internal/factory-method.h"
27 #include "internal/injector-core.h"
28 #include "internal/provider-by-default-constructor.h"
29 #include "internal/provider-by-factory.h"
30 #include "internal/types-by-name.h"
31
32 #include <QtTest/QtTest>
33 #include <memory>
34
35 using namespace injeqt::v1;
36 using namespace injeqt::internal;
37
38 class by_factory_type : public QObject
39 {
40 Q_OBJECT
41
42 public:
43 Q_INVOKABLE by_factory_type() {}
44
45 };
46
47 class factory_type : public QObject
48 {
49 Q_OBJECT
50
51 public:
52 Q_INVOKABLE factory_type() {}
53 Q_INVOKABLE by_factory_type * create() { return new by_factory_type{}; }
54
55 };
56
57 class invalid_factory_type : public QObject
58 {
59 Q_OBJECT
60
61 public:
62 Q_INVOKABLE invalid_factory_type() {}
63 Q_INVOKABLE by_factory_type * create() { return nullptr; }
64
65 };
66
67 class factory_module : public module
68 {
69
70 public:
71 factory_module() { add_type<factory_type>(); }
72 virtual ~factory_module() {}
73
74 };
75
76 class provider_by_factory_test : public QObject
77 {
78 Q_OBJECT
79
80 public:
81 provider_by_factory_test();
82
83 private slots:
84 void should_return_always_the_same_object();
85 void should_throw_instantiation_failed_when_invalid_factory();
86
87 private:
88 types_by_name known_types;
89
90 };
91
92 provider_by_factory_test::provider_by_factory_test()
93 {
94 known_types = types_by_name{std::vector<type>{
95 make_type<by_factory_type>(),
96 make_type<factory_type>(),
97 make_type<invalid_factory_type>()
98 }};
99 }
100
101 void provider_by_factory_test::should_return_always_the_same_object()
102 {
103 auto fm = make_factory_method(known_types, make_type<by_factory_type>(), make_type<factory_type>());
104 auto fp = std::unique_ptr<provider_by_factory>{new provider_by_factory{fm}};
105 auto p = fp.get();
106
107 auto configuration = std::vector<std::unique_ptr<provider>>{};
108 configuration.emplace_back(std::move(fp));
109 configuration.emplace_back(std::unique_ptr<provider_by_default_constructor>{new provider_by_default_constructor{make_default_constructor_method(make_type<factory_type>())}});
110 auto injector = injector_core{types_by_name{}, std::move(configuration)};
111
112 QCOMPARE(p->provided_type(), make_type<by_factory_type>());
113 QCOMPARE(p->required_types(), types{make_type<factory_type>()});
114 QCOMPARE(p->factory(), fm);
115
116 auto o = p->provide(injector);
117 QCOMPARE(p->provide(injector), o);
118 QCOMPARE(p->provide(injector), o);
119 QCOMPARE(o->metaObject(), &by_factory_type::staticMetaObject);
120 }
121
122 void provider_by_factory_test::should_throw_instantiation_failed_when_invalid_factory()
123 {
124 auto fm = make_factory_method(known_types, make_type<by_factory_type>(), make_type<invalid_factory_type>());
125 auto fp = std::unique_ptr<provider_by_factory>{new provider_by_factory{fm}};
126 auto p = fp.get();
127
128 auto configuration = std::vector<std::unique_ptr<provider>>{};
129 configuration.emplace_back(std::move(fp));
130 configuration.emplace_back(std::unique_ptr<provider_by_default_constructor>{new provider_by_default_constructor{make_default_constructor_method(make_type<invalid_factory_type>())}});
131 auto injector = injector_core{types_by_name{}, std::move(configuration)};
132
133 QCOMPARE(p->provided_type(), make_type<by_factory_type>());
134 QCOMPARE(p->required_types(), types{make_type<invalid_factory_type>()});
135 QCOMPARE(p->factory(), fm);
136
137 expect<exception::instantiation_failed>({"by_factory_type"}, [&](){
138 p->provide(injector);
139 });
140 }
141
142 QTEST_APPLESS_MAIN(provider_by_factory_test)
143 #include "provider-by-factory-test.moc"