|
1 | 1 | #include <log/fmt/logger.hpp>
|
2 | 2 | #include <msg/message.hpp>
|
| 3 | +#include <msg/message_destructure.hpp> |
3 | 4 |
|
4 | 5 | #include <catch2/catch_test_macros.hpp>
|
5 | 6 | #include <catch2/matchers/catch_matchers_templated.hpp>
|
@@ -73,6 +74,24 @@ TEST_CASE("construct with field values", "[message]") {
|
73 | 74 | CHECK(0x0042'd00d == data[1]);
|
74 | 75 | }
|
75 | 76 |
|
| 77 | +TEST_CASE("message supports tuple protocol", "[message]") { |
| 78 | + [[maybe_unused]] test_msg msg{"f1"_field = 0xba11, "f2"_field = 0x42, |
| 79 | + "f3"_field = 0xd00d}; |
| 80 | + STATIC_REQUIRE(std::tuple_size_v<test_msg> == 4); |
| 81 | + STATIC_REQUIRE( |
| 82 | + std::is_same_v<std::tuple_element_t<0, test_msg>, std::uint32_t>); |
| 83 | +} |
| 84 | + |
| 85 | +TEST_CASE("destructure owning message", "[message]") { |
| 86 | + test_msg msg{"f1"_field = 0xba11, "f2"_field = 0x42, "f3"_field = 0xd00d}; |
| 87 | + STATIC_REQUIRE(std::tuple_size_v<test_msg> == 4); |
| 88 | + auto const [f1, id, f3, f2] = msg; |
| 89 | + CHECK(0x80 == id); |
| 90 | + CHECK(0xba11 == f1); |
| 91 | + CHECK(0x42 == f2); |
| 92 | + CHECK(0xd00d == f3); |
| 93 | +} |
| 94 | + |
76 | 95 | TEST_CASE("use field names as template args", "[message]") {
|
77 | 96 | auto msg = []<auto F>() {
|
78 | 97 | return test_msg{F = 0xba11};
|
@@ -123,6 +142,18 @@ TEST_CASE("view with read-only external storage", "[message]") {
|
123 | 142 | CHECK(0xd00d == msg.get("f3"_field));
|
124 | 143 | }
|
125 | 144 |
|
| 145 | +TEST_CASE("destructure message view", "[message]") { |
| 146 | + auto const arr = |
| 147 | + typename msg_defn::default_storage_t{0x8000'ba11, 0x0042'd00d}; |
| 148 | + const_view<msg_defn> msg{arr}; |
| 149 | + |
| 150 | + auto const [f1, id, f3, f2] = msg; |
| 151 | + CHECK(0x80 == id); |
| 152 | + CHECK(0xba11 == f1); |
| 153 | + CHECK(0x42 == f2); |
| 154 | + CHECK(0xd00d == f3); |
| 155 | +} |
| 156 | + |
126 | 157 | TEST_CASE("view with external storage (oversized)", "[message]") {
|
127 | 158 | auto const arr = std::array<std::uint32_t, 8>{0x8000'ba11, 0x0042'd00d};
|
128 | 159 | msg_defn::view_t msg{arr};
|
|
0 commit comments