Skip to content

Commit 8c2dea7

Browse files
committed
🐛 Field matcher logs shouldn't coerce args to std::uint32_t
Problem: - When field matchers log their match descriptions, they coerce the field types to std::uint32_t. Logging can deal with other types now. Solution: - Preserve the field types. Only log hex values for integral types.
1 parent a0eb6cd commit 8c2dea7

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

include/msg/field_matchers.hpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stdx/type_traits.hpp>
1111

1212
#include <array>
13+
#include <concepts>
1314
#include <cstddef>
1415
#include <cstdint>
1516
#include <functional>
@@ -141,17 +142,28 @@ struct rel_matcher_t {
141142
}
142143

143144
[[nodiscard]] constexpr auto describe() const {
144-
return stdx::ct_format<"{} {} 0x{:x}">(
145-
Field::name, detail::to_string<RelOp>(),
146-
stdx::ct<static_cast<std::uint32_t>(ExpectedValue)>());
145+
if constexpr (std::integral<typename Field::type>) {
146+
return stdx::ct_format<"{} {} 0x{:x}">(Field::name,
147+
detail::to_string<RelOp>(),
148+
stdx::ct<ExpectedValue>());
149+
} else {
150+
return stdx::ct_format<"{} {} {}">(Field::name,
151+
detail::to_string<RelOp>(),
152+
stdx::ct<ExpectedValue>());
153+
}
147154
}
148155

149156
template <typename MsgType>
150157
[[nodiscard]] constexpr auto describe_match(MsgType const &msg) const {
151-
return stdx::ct_format<"{} (0x{:x}) {} 0x{:x}">(
152-
Field::name, static_cast<std::uint32_t>(extract_field(msg)),
153-
detail::to_string<RelOp>(),
154-
stdx::ct<static_cast<std::uint32_t>(ExpectedValue)>());
158+
if constexpr (std::integral<typename Field::type>) {
159+
return stdx::ct_format<"{} (0x{:x}) {} 0x{:x}">(
160+
Field::name, extract_field(msg), detail::to_string<RelOp>(),
161+
stdx::ct<ExpectedValue>());
162+
} else {
163+
return stdx::ct_format<"{} ({}) {} {}">(
164+
Field::name, extract_field(msg), detail::to_string<RelOp>(),
165+
stdx::ct<ExpectedValue>());
166+
}
155167
}
156168

157169
private:

test/msg/field_matchers.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,56 @@
22
#include <msg/field.hpp>
33
#include <msg/field_matchers.hpp>
44

5+
#include <stdx/ct_string.hpp>
6+
#include <stdx/tuple.hpp>
7+
58
#include <catch2/catch_test_macros.hpp>
69

710
namespace {
811
using namespace msg;
912
using test_field =
1013
field<"test_field", std::uint32_t>::located<at{0_dw, 31_msb, 24_lsb}>;
14+
15+
enum struct E { A, B, C };
16+
17+
using test_enum_field =
18+
field<"enum_field", E>::located<at{0_dw, 31_msb, 24_lsb}>;
1119
} // namespace
1220

21+
TEST_CASE("matcher description", "[field matchers]") {
22+
using namespace stdx::literals;
23+
constexpr auto m = msg::less_than_t<test_field, 5>{};
24+
constexpr auto desc = m.describe();
25+
static_assert(desc.str == "test_field < 0x5"_ctst);
26+
}
27+
28+
TEST_CASE("matcher description of match", "[field matchers]") {
29+
using namespace stdx::literals;
30+
using msg_data = std::array<std::uint32_t, 1>;
31+
32+
constexpr auto m = msg::less_than_t<test_field, 5>{};
33+
constexpr auto desc = m.describe_match(msg_data{0x01ff'ffff});
34+
static_assert(desc.str == "test_field (0x{:x}) < 0x5"_ctst);
35+
static_assert(desc.args == stdx::tuple{1});
36+
}
37+
38+
TEST_CASE("matcher description (enum field)", "[field matchers]") {
39+
using namespace stdx::literals;
40+
constexpr auto m = msg::less_than_t<test_enum_field, E::C>{};
41+
constexpr auto desc = m.describe();
42+
static_assert(desc.str == "enum_field < C"_ctst);
43+
}
44+
45+
TEST_CASE("matcher description of match (enum field)", "[field matchers]") {
46+
using namespace stdx::literals;
47+
using msg_data = std::array<std::uint32_t, 1>;
48+
49+
constexpr auto m = msg::less_than_t<test_enum_field, E::C>{};
50+
constexpr auto desc = m.describe_match(msg_data{0x01ff'ffff});
51+
static_assert(desc.str == "enum_field ({}) < C"_ctst);
52+
static_assert(desc.args == stdx::tuple{E::B});
53+
}
54+
1355
TEST_CASE("negate less_than", "[field matchers]") {
1456
constexpr auto m = msg::less_than_t<test_field, 5>{};
1557
constexpr auto n = match::negate(m);

0 commit comments

Comments
 (0)