Skip to content
This repository was archived by the owner on Jan 29, 2026. It is now read-only.

Commit b61bab4

Browse files
authored
Refactor unit tests (#201)
1 parent b172bae commit b61bab4

7 files changed

Lines changed: 187 additions & 234 deletions

tests/proxy_creation_tests.cpp

Lines changed: 33 additions & 47 deletions
Large diffs are not rendered by default.

tests/proxy_dispatch_tests.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#pragma warning(pop)
1313
#endif // defined(_MSC_VER) && !defined(__clang__)
1414

15-
namespace {
15+
namespace proxy_dispatch_tests_details {
1616

1717
struct CommaTester {
1818
public:
@@ -35,7 +35,9 @@ struct PtrToMemTester {
3535

3636
PRO_DEF_FREE_AS_MEM_DISPATCH(FreeMemToString, std::to_string, ToString);
3737

38-
} // namespace
38+
} // namespace proxy_dispatch_tests_details
39+
40+
namespace details = proxy_dispatch_tests_details;
3941

4042
TEST(ProxyDispatchTests, TestOpPlus) {
4143
struct TestFacade : pro::facade_builder::add_convention<pro::operator_dispatch<"+">, int(), int(int val)>::build {};
@@ -297,7 +299,7 @@ TEST(ProxyDispatchTests, TestOpRightShiftAssignment) {
297299

298300
TEST(ProxyDispatchTests, TestOpComma) {
299301
struct TestFacade : pro::facade_builder::add_convention<pro::operator_dispatch<",">, int(int val)>::build {};
300-
CommaTester v{3};
302+
details::CommaTester v{3};
301303
pro::proxy<TestFacade> p = &v;
302304
ASSERT_EQ((*p, 6), 9);
303305
}
@@ -572,14 +574,14 @@ TEST(ProxyDispatchTests, TestRhsOpRightShiftAssignment) {
572574

573575
TEST(ProxyDispatchTests, TestRhsOpComma) {
574576
struct TestFacade : pro::facade_builder::add_convention<pro::operator_dispatch<",", true>, int(int val)>::build {};
575-
CommaTester v{3};
577+
details::CommaTester v{3};
576578
pro::proxy<TestFacade> p = &v;
577579
ASSERT_EQ((7, *p), 21);
578580
}
579581

580582
TEST(ProxyDispatchTests, TestRhsOpPtrToMem) {
581583
struct TestFacade : pro::facade_builder::add_convention<pro::operator_dispatch<"->*", true>, int(int val)>::build {};
582-
PtrToMemTester v{3};
584+
details::PtrToMemTester v{3};
583585
pro::proxy<TestFacade> p = &v;
584586
ASSERT_EQ(2->**p, 6);
585587
}
@@ -618,7 +620,7 @@ TEST(ProxyDispatchTests, TestImplciitConversion) {
618620
}
619621

620622
TEST(ProxyDispatchTests, TestFreeAsMemDispatch) {
621-
struct TestFacade : pro::facade_builder::add_convention<FreeMemToString, std::string() const>::build {};
623+
struct TestFacade : pro::facade_builder::add_convention<details::FreeMemToString, std::string() const>::build {};
622624
int v = 123;
623625
pro::proxy<TestFacade> p = &v;
624626
ASSERT_EQ(p->ToString(), "123");

tests/proxy_integration_tests.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
#include <vector>
1212
#include "proxy.h"
1313

14-
namespace {
15-
16-
namespace spec {
14+
namespace proxy_integration_tests_details {
1715

1816
PRO_DEF_MEM_DISPATCH(MemDraw, Draw);
1917
PRO_DEF_MEM_DISPATCH(MemArea, Area);
@@ -29,8 +27,6 @@ struct Logger : pro::facade_builder
2927
::add_convention<MemLog, void(const char*), void(const char*, const std::exception&)>
3028
::build {};
3129

32-
} // namespace spec
33-
3430
class Rectangle {
3531
public:
3632
explicit Rectangle(double width, double height) : width_(width), height_(height) {}
@@ -61,7 +57,7 @@ class Point {
6157
constexpr double Area() const noexcept { return 0; }
6258
};
6359

64-
std::string PrintDrawableToString(pro::proxy<spec::Drawable> p) {
60+
std::string PrintDrawableToString(pro::proxy<Drawable> p) {
6561
std::stringstream result;
6662
result << std::fixed << std::setprecision(5) << "shape = ";
6763
p->Draw(result);
@@ -92,19 +88,19 @@ std::vector<std::string> ParseCommand(const std::string& s) {
9288
return result;
9389
}
9490

95-
pro::proxy<spec::Drawable> MakeDrawableFromCommand(const std::string& s) {
91+
pro::proxy<Drawable> MakeDrawableFromCommand(const std::string& s) {
9692
std::vector<std::string> parsed = ParseCommand(s);
9793
if (!parsed.empty()) {
9894
if (parsed[0u] == "Rectangle") {
9995
if (parsed.size() == 3u) {
10096
static std::pmr::unsynchronized_pool_resource rectangle_memory_pool;
10197
std::pmr::polymorphic_allocator<> alloc{&rectangle_memory_pool};
102-
return pro::allocate_proxy<spec::Drawable, Rectangle>(alloc, std::stod(parsed[1u]), std::stod(parsed[2u]));
98+
return pro::allocate_proxy<Drawable, Rectangle>(alloc, std::stod(parsed[1u]), std::stod(parsed[2u]));
10399
}
104100
} else if (parsed[0u] == "Circle") {
105101
if (parsed.size() == 2u) {
106102
Circle circle{std::stod(parsed[1u])};
107-
return pro::make_proxy<spec::Drawable>(circle);
103+
return pro::make_proxy<Drawable>(circle);
108104
}
109105
} else if (parsed[0u] == "Point") {
110106
if (parsed.size() == 1u) {
@@ -132,31 +128,33 @@ class StreamLogger {
132128
std::ostream* out_;
133129
};
134130

135-
} // namespace
131+
} // namespace proxy_integration_tests_details
132+
133+
namespace details = proxy_integration_tests_details;
136134

137135
TEST(ProxyIntegrationTests, TestDrawable) {
138-
pro::proxy<spec::Drawable> p = MakeDrawableFromCommand("Rectangle 2 3");
139-
std::string s = PrintDrawableToString(std::move(p));
136+
pro::proxy<details::Drawable> p = details::MakeDrawableFromCommand("Rectangle 2 3");
137+
std::string s = details::PrintDrawableToString(std::move(p));
140138
ASSERT_EQ(s, "shape = {Rectangle: width = 2.00000, height = 3.00000}, area = 6.00000");
141139

142-
p = MakeDrawableFromCommand("Circle 1");
143-
s = PrintDrawableToString(std::move(p));
140+
p = details::MakeDrawableFromCommand("Circle 1");
141+
s = details::PrintDrawableToString(std::move(p));
144142
ASSERT_EQ(s, "shape = {Circle: radius = 1.00000}, area = 3.14159");
145143

146-
p = MakeDrawableFromCommand("Point");
147-
s = PrintDrawableToString(std::move(p));
144+
p = details::MakeDrawableFromCommand("Point");
145+
s = details::PrintDrawableToString(std::move(p));
148146
ASSERT_EQ(s, "shape = {Point}, area = 0.00000");
149147

150148
try {
151-
p = MakeDrawableFromCommand("Triangle 2 3");
149+
p = details::MakeDrawableFromCommand("Triangle 2 3");
152150
} catch (const std::runtime_error& e) {
153151
ASSERT_STREQ(e.what(), "Invalid command");
154152
}
155153
}
156154

157155
TEST(ProxyIntegrationTests, TestLogger) {
158156
std::ostringstream out;
159-
auto logger = pro::make_proxy<spec::Logger, StreamLogger>(out);
157+
auto logger = pro::make_proxy<details::Logger, details::StreamLogger>(out);
160158
logger->Log("hello");
161159
try {
162160
throw std::runtime_error{"runtime error!"};

tests/proxy_invocation_tests.cpp

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
#endif // defined(_MSC_VER) && !defined(__clang__)
2222
#include "utils.h"
2323

24-
namespace {
25-
26-
namespace spec {
24+
namespace proxy_invocation_tests_details {
2725

2826
template <class... Os>
2927
struct MovableCallable : pro::facade_builder
@@ -113,8 +111,6 @@ struct SharedStringable : pro::facade_builder
113111
::add_direct_convention<FreeGetWeak<SharedStringable>, pro::proxy<Weak<SharedStringable>>() const&>
114112
::build {};
115113

116-
} // namespace spec
117-
118114
template <class F, bool NE, class... Args>
119115
concept CallableFacade =
120116
requires(pro::proxy<F> p, Args... args) {
@@ -123,16 +119,18 @@ concept CallableFacade =
123119
};
124120

125121
// Static assertions for facade Callable
126-
static_assert(!CallableFacade<spec::Callable<int(double)>, false, std::nullptr_t>); // Wrong arguments
127-
static_assert(CallableFacade<spec::Callable<int(double)>, false, float>); // Invoking without specifying a dispatch
128-
static_assert(CallableFacade<spec::Callable<int(double), void(int) noexcept>, true, int>); // Invoking noexcept overloads
129-
static_assert(CallableFacade<spec::Callable<int(double), void(int) noexcept>, false, double>); // Invoking overloads that may throw
122+
static_assert(!CallableFacade<Callable<int(double)>, false, std::nullptr_t>); // Wrong arguments
123+
static_assert(CallableFacade<Callable<int(double)>, false, float>); // Invoking without specifying a dispatch
124+
static_assert(CallableFacade<Callable<int(double), void(int) noexcept>, true, int>); // Invoking noexcept overloads
125+
static_assert(CallableFacade<Callable<int(double), void(int) noexcept>, false, double>); // Invoking overloads that may throw
130126

131127
template <class... Args>
132128
std::vector<std::type_index> GetTypeIndices()
133129
{ return {std::type_index{typeid(Args)}...}; }
134130

135-
} // namespace
131+
} // namespace proxy_invocation_tests_details
132+
133+
namespace details = proxy_invocation_tests_details;
136134

137135
TEST(ProxyInvocationTests, TestArgumentForwarding) {
138136
std::string arg1 = "My string";
@@ -146,7 +144,7 @@ TEST(ProxyInvocationTests, TestArgumentForwarding) {
146144
arg2_received = std::move(v);
147145
return expected_result;
148146
};
149-
pro::proxy<spec::Callable<int(std::string, std::vector<int>)>> p = &f;
147+
pro::proxy<details::Callable<int(std::string, std::vector<int>)>> p = &f;
150148
int result = (*p)(arg1, std::move(arg2));
151149
ASSERT_TRUE(p.has_value());
152150
ASSERT_EQ(arg1_received, arg1);
@@ -159,7 +157,7 @@ TEST(ProxyInvocationTests, TestThrow) {
159157
const char* expected_error_message = "My exception";
160158
auto f = [&] { throw std::runtime_error{ expected_error_message }; };
161159
bool exception_thrown = false;
162-
pro::proxy<spec::Callable<void()>> p = &f;
160+
pro::proxy<details::Callable<void()>> p = &f;
163161
try {
164162
(*p)();
165163
} catch (const std::runtime_error& e) {
@@ -172,7 +170,7 @@ TEST(ProxyInvocationTests, TestThrow) {
172170

173171
TEST(ProxyInvocationTests, TestMultipleDispatches_Unique) {
174172
std::list<int> l = { 1, 2, 3 };
175-
pro::proxy<spec::Iterable<int>> p = &l;
173+
pro::proxy<details::Iterable<int>> p = &l;
176174
ASSERT_EQ(Size(*p), 3);
177175
int sum = 0;
178176
auto accumulate_sum = [&](int x) { sum += x; };
@@ -182,12 +180,12 @@ TEST(ProxyInvocationTests, TestMultipleDispatches_Unique) {
182180

183181
TEST(ProxyInvocationTests, TestMultipleDispatches_Duplicated) {
184182
struct DuplicatedIterable : pro::facade_builder
185-
::add_convention<spec::FreeForEach, void(std::function<void(int&)>)>
186-
::add_convention<spec::FreeSize, std::size_t()>
187-
::add_convention<spec::FreeForEach, void(std::function<void(int&)>)>
183+
::add_convention<details::FreeForEach, void(std::function<void(int&)>)>
184+
::add_convention<details::FreeSize, std::size_t()>
185+
::add_convention<details::FreeForEach, void(std::function<void(int&)>)>
188186
::build {};
189187
static_assert(sizeof(pro::details::facade_traits<DuplicatedIterable>::meta) ==
190-
sizeof(pro::details::facade_traits<spec::Iterable<int>>::meta));
188+
sizeof(pro::details::facade_traits<details::Iterable<int>>::meta));
191189
std::list<int> l = { 1, 2, 3 };
192190
pro::proxy<DuplicatedIterable> p = &l;
193191
ASSERT_EQ(Size(*p), 3);
@@ -199,7 +197,7 @@ TEST(ProxyInvocationTests, TestMultipleDispatches_Duplicated) {
199197

200198
TEST(ProxyInvocationTests, TestRecursiveDefinition) {
201199
std::list<int> l = { 1, 2, 3 };
202-
pro::proxy<spec::Container<int>> p = &l;
200+
pro::proxy<details::Container<int>> p = &l;
203201
ASSERT_EQ(Size(*p), 3);
204202
int sum = 0;
205203
auto accumulate_sum = [&](int x) { sum += x; };
@@ -218,45 +216,45 @@ TEST(ProxyInvocationTests, TestOverloadResolution) {
218216
::build {};
219217
std::vector<std::type_index> side_effect;
220218
auto p = pro::make_proxy<OverloadedCallable>([&](auto&&... args)
221-
{ side_effect = GetTypeIndices<std::decay_t<decltype(args)>...>(); });
219+
{ side_effect = details::GetTypeIndices<std::decay_t<decltype(args)>...>(); });
222220
(*p)(123);
223-
ASSERT_EQ(side_effect, GetTypeIndices<int>());
221+
ASSERT_EQ(side_effect, details::GetTypeIndices<int>());
224222
(*p)(1.23);
225-
ASSERT_EQ(side_effect, GetTypeIndices<double>());
223+
ASSERT_EQ(side_effect, details::GetTypeIndices<double>());
226224
char foo[2];
227225
(*p)(foo);
228-
ASSERT_EQ(side_effect, GetTypeIndices<char*>());
226+
ASSERT_EQ(side_effect, details::GetTypeIndices<char*>());
229227
(*p)("lalala");
230-
ASSERT_EQ(side_effect, GetTypeIndices<const char*>());
228+
ASSERT_EQ(side_effect, details::GetTypeIndices<const char*>());
231229
(*p)("lalala", 0);
232-
ASSERT_EQ(side_effect, (GetTypeIndices<std::string, int>()));
230+
ASSERT_EQ(side_effect, (details::GetTypeIndices<std::string, int>()));
233231
ASSERT_FALSE((std::is_invocable_v<decltype(*p), std::vector<int>>));
234232
}
235233

236234
TEST(ProxyInvocationTests, TestNoexcept) {
237235
std::vector<std::type_index> side_effect;
238-
auto p = pro::make_proxy<spec::Callable<void(int) noexcept, void(double)>>([&](auto&&... args) noexcept
239-
{ side_effect = GetTypeIndices<std::decay_t<decltype(args)>...>(); });
236+
auto p = pro::make_proxy<details::Callable<void(int) noexcept, void(double)>>([&](auto&&... args) noexcept
237+
{ side_effect = details::GetTypeIndices<std::decay_t<decltype(args)>...>(); });
240238
static_assert(noexcept((*p)(123)));
241239
(*p)(123);
242-
ASSERT_EQ(side_effect, GetTypeIndices<int>());
240+
ASSERT_EQ(side_effect, details::GetTypeIndices<int>());
243241
static_assert(!noexcept((*p)(1.23)));
244242
(*p)(1.23);
245-
ASSERT_EQ(side_effect, GetTypeIndices<double>());
243+
ASSERT_EQ(side_effect, details::GetTypeIndices<double>());
246244
ASSERT_FALSE((std::is_invocable_v<decltype(*p), char*>));
247245
}
248246

249247
TEST(ProxyInvocationTests, TestFunctionPointer) {
250-
struct TestFacade : spec::Callable<std::vector<std::type_index>()> {};
251-
pro::proxy<TestFacade> p{ &GetTypeIndices<int, double> };
248+
struct TestFacade : details::Callable<std::vector<std::type_index>()> {};
249+
pro::proxy<TestFacade> p{ &details::GetTypeIndices<int, double> };
252250
auto ret = (*p)();
253-
ASSERT_EQ(ret, (GetTypeIndices<int, double>()));
251+
ASSERT_EQ(ret, (details::GetTypeIndices<int, double>()));
254252
}
255253

256254
TEST(ProxyInvocationTests, TestMemberDispatchDefault) {
257255
std::vector<std::string> container1{ "hello", "world", "!"};
258256
std::list<std::string> container2{ "hello", "world" };
259-
pro::proxy<spec::ResourceDictionary> p = &container1;
257+
pro::proxy<details::ResourceDictionary> p = &container1;
260258
ASSERT_EQ(p->at(0), "hello");
261259
p = &container2;
262260
{
@@ -274,13 +272,13 @@ TEST(ProxyInvocationTests, TestMemberDispatchDefault) {
274272
TEST(ProxyInvocationTests, TestFreeDispatchDefault) {
275273
{
276274
int side_effect = 0;
277-
auto p = pro::make_proxy<spec::WeakCallable<void()>>([&] { side_effect = 1; });
275+
auto p = pro::make_proxy<details::WeakCallable<void()>>([&] { side_effect = 1; });
278276
(*p)();
279277
ASSERT_EQ(side_effect, 1);
280278
}
281279
{
282280
bool exception_thrown = false;
283-
auto p = pro::make_proxy<spec::WeakCallable<void()>>(123);
281+
auto p = pro::make_proxy<details::WeakCallable<void()>>(123);
284282
try {
285283
(*p)();
286284
} catch (const std::runtime_error& e) {
@@ -293,7 +291,7 @@ TEST(ProxyInvocationTests, TestFreeDispatchDefault) {
293291

294292
TEST(ProxyInvocationTests, TestObserverDispatch) {
295293
int test_val = 123;
296-
pro::proxy<spec::SharedStringable> p{std::make_shared<int>(test_val)};
294+
pro::proxy<details::SharedStringable> p{std::make_shared<int>(test_val)};
297295
auto weak = GetWeak(p);
298296
ASSERT_TRUE(weak.has_value());
299297
{

0 commit comments

Comments
 (0)