Skip to content

Commit 075dcff

Browse files
committed
test: add order test case
1 parent 1474219 commit 075dcff

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-19
lines changed

include/autowired/autowired.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <algorithm>
55
#include <atomic>
66
#include <functional>
7-
#include <limits>
7+
#include <iostream>
88
#include <map>
99
#include <optional>
1010
#include <queue>
@@ -28,7 +28,7 @@ class AutoWired {
2828
bool need_init{false};
2929
bool need_de_init{false};
3030
bool need_auto_wired{false};
31-
int32_t order{std::numeric_limits<int32_t>::max()};
31+
int32_t order{0};
3232

3333
public:
3434
static auto WithCustomName(const std::string& custom_name) {
@@ -85,8 +85,7 @@ class AutoWired {
8585
}
8686
};
8787

88-
inline const static RegisterOptions default_register_options{
89-
"", false, false, false, std::numeric_limits<int32_t>::max()};
88+
inline const static RegisterOptions default_register_options{"", false, false, false, 0};
9089
using RegisterOptionsSetFunction = std::function<void(RegisterOptions&)>;
9190

9291
public:
@@ -120,7 +119,7 @@ class AutoWired {
120119

121120
template <typename T>
122121
void Wired(T** t_ptr, std::string_view custom_name = "") {
123-
auto name = Utility::GetClassTypeName<T>(custom_name);
122+
auto name = Utility::GetPrettyClassTypeName<T>(custom_name);
124123

125124
// if target class not present, core dump will be.
126125
*t_ptr = static_cast<T*>(class_.at(name).instance);
@@ -132,7 +131,7 @@ class AutoWired {
132131

133132
template <typename T>
134133
T* GetClass(std::string_view custom_name = "") {
135-
auto name = Utility::GetClassTypeName<T>(custom_name);
134+
auto name = Utility::GetPrettyClassTypeName<T>(custom_name);
136135

137136
if (!class_.count(name)) {
138137
if constexpr (internal::has_get_auto_wired_register_options_v<T>) {
@@ -147,15 +146,15 @@ class AutoWired {
147146

148147
template <typename T>
149148
T* GetClassMust(std::string_view custom_name = "") {
150-
auto name = Utility::GetClassTypeName<T>(custom_name);
149+
auto name = Utility::GetPrettyClassTypeName<T>(custom_name);
151150

152151
// if target class not present, core dump will be.
153152
return static_cast<T*>(class_.at(name).instance);
154153
}
155154

156155
template <typename T>
157156
T* GetClassOrNullPtr(std::string_view custom_name = "") {
158-
auto name = Utility::GetClassTypeName<T>(custom_name);
157+
auto name = Utility::GetPrettyClassTypeName<T>(custom_name);
159158

160159
if (class_.count(name) == 0) {
161160
return nullptr;
@@ -178,9 +177,10 @@ class AutoWired {
178177

179178
template <typename Anc, typename Des>
180179
void AddClassRelations(const std::string& anc_custom_name = "", const std::string& des_custom_name = "") {
181-
auto left_name = Utility::GetClassTypeName<Anc>(anc_custom_name);
182-
auto right_name = Utility::GetClassTypeName<Des>(des_custom_name);
183-
AddClassRelations(left_name, right_name);
180+
auto anc_name = Utility::GetPrettyClassTypeName<Anc>(anc_custom_name);
181+
auto des_name = Utility::GetPrettyClassTypeName<Des>(des_custom_name);
182+
183+
AddClassRelations(anc_name, des_name);
184184
}
185185

186186
// Ancestors -> Descendants
@@ -189,17 +189,17 @@ class AutoWired {
189189
// This is most likely the case where `des` depends on some `anc`,
190190
// but `des` does not have a successor dependency and is padded
191191
// directly into the `AutoWired` instance construct
192-
if (class_.count(anc) == 0 || class_.count(des) == 0) {
193-
return;
194-
}
192+
// if (class_.count(anc) == 0 || class_.count(des) == 0) {
193+
// return;
194+
// }
195195

196196
graph_[anc].push_back(des);
197197
++degree_[des];
198198
}
199199

200200
template <typename Pre, typename After>
201201
AutoWired& UseMocker(After* a, const std::string& custom_name = "") {
202-
auto name = Utility::GetClassTypeName<Pre>(custom_name);
202+
auto name = Utility::GetPrettyClassTypeName<Pre>(custom_name);
203203
registerImpl<Pre>(dynamic_cast<Pre*>(a));
204204
return *this;
205205
}
@@ -232,7 +232,7 @@ class AutoWired {
232232

233233
auto [has_loop, topological_order] = GetTopologicalOrder();
234234
if (has_loop) {
235-
throw std::runtime_error("Dependencies has loop, please initialize manually");
235+
throw std::runtime_error("Dependencies has loop, please init manually");
236236
}
237237

238238
for (auto& name : topological_order) {
@@ -304,7 +304,7 @@ class AutoWired {
304304
private:
305305
template <typename T>
306306
void registerImpl(T* t_ptr, RegisterOptions options = default_register_options) {
307-
auto name = Utility::GetClassTypeName<T>(options.custom_name);
307+
auto name = Utility::GetPrettyClassTypeName<T>(options.custom_name);
308308
if (class_.count(name)) {
309309
return;
310310
}

include/autowired/macros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private:
2323
using ClassTypeForAutoWired = ClassType; \
2424
\
2525
public: \
26-
ClassType(auto_wired::AutoWired& w = auto_wired::AutoWired::Instance()) : auto_wired_flag_(1)
26+
ClassType([[maybe_unused]] auto_wired::AutoWired& w = auto_wired::AutoWired::Instance()) : auto_wired_flag_(1)
2727

2828
#define AUTO_WIRED_DECLARE_CLASS(name, ...) \
2929
, name(w.GetClassRefWithAddClassRelations<ClassTypeForAutoWired, std::remove_reference_t<decltype(name)>>( \

test/autowired_v1_test.cc

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,35 @@ class B {
3838
A& a;
3939
};
4040

41+
class BB {
42+
public:
43+
AUTO_WIRED_DECLARE_BEGIN(BB, auto_wired::AutoWired::RegisterOptions::WithOrder(-1))
44+
AUTO_WIRED_DECLARE_CLASS(a)
45+
AUTO_WIRED_DECLARE_END()
46+
47+
private:
48+
A& a;
49+
};
50+
51+
class BBB {
52+
public:
53+
AUTO_WIRED_DECLARE_BEGIN(BBB, auto_wired::AutoWired::RegisterOptions::WithOrder(999))
54+
AUTO_WIRED_DECLARE_CLASS(a)
55+
AUTO_WIRED_DECLARE_END()
56+
57+
private:
58+
A& a;
59+
};
60+
4161
class C {
4262
public:
4363
AUTO_WIRED_DECLARE_BEGIN(C,
4464
auto_wired::AutoWired::RegisterOptions::WithNeedInit(),
4565
auto_wired::AutoWired::RegisterOptions::WithNeedDeInit())
4666
AUTO_WIRED_DECLARE_CLASS(a)
4767
AUTO_WIRED_DECLARE_CLASS(b)
68+
AUTO_WIRED_DECLARE_CLASS(bb)
69+
AUTO_WIRED_DECLARE_CLASS(bbb)
4870
AUTO_WIRED_DECLARE_END()
4971

5072
void Init() {
@@ -63,6 +85,8 @@ class C {
6385
private:
6486
A& a;
6587
B& b;
88+
BB& bb;
89+
BBB& bbb;
6690
};
6791

6892
class D {
@@ -87,7 +111,6 @@ TEST_F(AutoWiredV1Test, auto_wired_v1_test) {
87111
EXPECT_EQ(c.c, 3);
88112

89113
w.InitAll();
90-
91114
EXPECT_EQ(test_flag, 4);
92115

93116
w.DeInitAll();
@@ -109,6 +132,21 @@ TEST_F(AutoWiredV1Test, auto_wired_all) {
109132

110133
w.DeInitAll();
111134
EXPECT_EQ(test_flag, 55);
135+
136+
{
137+
auto [has_loop, topological_order] = w.GetTopologicalOrder();
138+
EXPECT_FALSE(has_loop);
139+
140+
EXPECT_EQ(topological_order,
141+
std::vector<std::string>({
142+
std::string("auto_wired::v1::test::A"),
143+
std::string("auto_wired::v1::test::BB"),
144+
std::string("auto_wired::v1::test::B"),
145+
std::string("auto_wired::v1::test::BBB"),
146+
std::string("auto_wired::v1::test::C"),
147+
std::string("auto_wired::v1::test::D"),
148+
}));
149+
}
112150
}
113151

114152
} // namespace auto_wired::v1::test

0 commit comments

Comments
 (0)