Skip to content

Commit 62b3708

Browse files
authored
Merge pull request #452 from Thurnheer/test-handler-and-callback-with-custom-view
Add test for custom view types
2 parents c6fb656 + 5f6a1f8 commit 62b3708

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

test/msg/callback.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,26 @@ TEST_CASE("callback handles message (raw)", "[handler]") {
8585
CHECK(dispatched);
8686
}
8787

88+
namespace {
89+
template <typename T>
90+
using uint8_view =
91+
typename T::template view_t<typename T::access_t::template span_t<uint8_t>>;
92+
template <typename T>
93+
using const_uint8_view = typename T::template view_t<
94+
typename T::access_t::template span_t<uint8_t const>>;
95+
} // namespace
96+
97+
TEST_CASE("callback handles message (custom raw format)", "[handler]") {
98+
auto callback = msg::callback<"cb">(
99+
id_match, [](const_uint8_view<msg_defn>) { dispatched = true; });
100+
auto const msg_match = std::array<uint8_t, 32>{0x00u, 0xbau, 0x11u, 0x80u,
101+
0x00u, 0x42u, 0xd0u, 0x0du};
102+
103+
dispatched = false;
104+
CHECK(callback.handle(msg_match));
105+
CHECK(dispatched);
106+
}
107+
88108
TEST_CASE("callback handles message (typed)", "[handler]") {
89109
auto callback = msg::callback<"cb">(
90110
id_match, [](msg::const_view<msg_defn>) { dispatched = true; });

test/msg/handler.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,33 @@ TEST_CASE("match and dispatch only one callback", "[handler]") {
109109
CHECK(dispatched);
110110
}
111111

112+
namespace {
113+
template <typename T>
114+
using uint8_view =
115+
typename T::template view_t<typename T::access_t::template span_t<uint8_t>>;
116+
template <typename T>
117+
using const_uint8_view = typename T::template view_t<
118+
typename T::access_t::template span_t<uint8_t const>>;
119+
} // namespace
120+
121+
TEST_CASE("match and dispatch only one callback with uint8_t storage",
122+
"[handler]") {
123+
auto callback1 = msg::callback<"cb1">(
124+
id_match<0x80>, [&](const_uint8_view<msg_defn>) { CHECK(false); });
125+
auto callback2 = msg::callback<"cb2">(
126+
id_match<0x44>, [](const_uint8_view<msg_defn>) { dispatched = true; });
127+
auto const msg = std::array<uint8_t, 8>{0x00u, 0xbau, 0x11u, 0x44u,
128+
0x00u, 0x42u, 0xd0u, 0x0du};
129+
130+
auto callbacks = stdx::make_tuple(callback1, callback2);
131+
static auto handler =
132+
msg::handler<decltype(callbacks), decltype(msg)>{callbacks};
133+
134+
dispatched = false;
135+
handler.handle(msg);
136+
CHECK(dispatched);
137+
}
138+
112139
TEST_CASE("dispatch with extra args", "[handler]") {
113140
auto callback = msg::callback<"cb", int>(
114141
id_match<0x80>, [](msg::const_view<msg_defn>, int value) {

test/msg/message.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,10 @@ TEST_CASE("construct with 8-bit storage", "[message]") {
366366
CHECK(std::equal(std::begin(expected), std::end(expected), std::begin(data),
367367
std::end(data)));
368368
}
369+
370+
TEST_CASE("view with external custom storage (oversized)", "[message]") {
371+
auto const arr = std::array<std::uint8_t, 32>{0x00, 0xba, 0x11, 0x80,
372+
0x00, 0x42, 0xd0, 0x0d};
373+
msg_defn::view_t msg{arr};
374+
CHECK(0x80 == msg.get("id"_field));
375+
}

0 commit comments

Comments
 (0)