Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions include/msg/field_matchers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdx/type_traits.hpp>

#include <array>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <functional>
Expand Down Expand Up @@ -141,17 +142,28 @@ struct rel_matcher_t {
}

[[nodiscard]] constexpr auto describe() const {
return stdx::ct_format<"{} {} 0x{:x}">(
Field::name, detail::to_string<RelOp>(),
stdx::ct<static_cast<std::uint32_t>(ExpectedValue)>());
if constexpr (std::integral<typename Field::type>) {
return stdx::ct_format<"{} {} 0x{:x}">(Field::name,
detail::to_string<RelOp>(),
stdx::ct<ExpectedValue>());
} else {
return stdx::ct_format<"{} {} {}">(Field::name,
detail::to_string<RelOp>(),
stdx::ct<ExpectedValue>());
}
}

template <typename MsgType>
[[nodiscard]] constexpr auto describe_match(MsgType const &msg) const {
return stdx::ct_format<"{} (0x{:x}) {} 0x{:x}">(
Field::name, static_cast<std::uint32_t>(extract_field(msg)),
detail::to_string<RelOp>(),
stdx::ct<static_cast<std::uint32_t>(ExpectedValue)>());
if constexpr (std::integral<typename Field::type>) {
return stdx::ct_format<"{} (0x{:x}) {} 0x{:x}">(
Field::name, extract_field(msg), detail::to_string<RelOp>(),
stdx::ct<ExpectedValue>());
} else {
return stdx::ct_format<"{} ({}) {} {}">(
Field::name, extract_field(msg), detail::to_string<RelOp>(),
stdx::ct<ExpectedValue>());
}
}

private:
Expand Down
42 changes: 42 additions & 0 deletions test/msg/field_matchers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,56 @@
#include <msg/field.hpp>
#include <msg/field_matchers.hpp>

#include <stdx/ct_string.hpp>
#include <stdx/tuple.hpp>

#include <catch2/catch_test_macros.hpp>

namespace {
using namespace msg;
using test_field =
field<"test_field", std::uint32_t>::located<at{0_dw, 31_msb, 24_lsb}>;

enum struct E { A, B, C };

using test_enum_field =
field<"enum_field", E>::located<at{0_dw, 31_msb, 24_lsb}>;
} // namespace

TEST_CASE("matcher description", "[field matchers]") {
using namespace stdx::literals;
constexpr auto m = msg::less_than_t<test_field, 5>{};
constexpr auto desc = m.describe();
static_assert(desc.str == "test_field < 0x5"_ctst);
}

TEST_CASE("matcher description of match", "[field matchers]") {
using namespace stdx::literals;
using msg_data = std::array<std::uint32_t, 1>;

constexpr auto m = msg::less_than_t<test_field, 5>{};
constexpr auto desc = m.describe_match(msg_data{0x01ff'ffff});
static_assert(desc.str == "test_field (0x{:x}) < 0x5"_ctst);
static_assert(desc.args == stdx::tuple{1});
}

TEST_CASE("matcher description (enum field)", "[field matchers]") {
using namespace stdx::literals;
constexpr auto m = msg::less_than_t<test_enum_field, E::C>{};
constexpr auto desc = m.describe();
static_assert(desc.str == "enum_field < C"_ctst);
}

TEST_CASE("matcher description of match (enum field)", "[field matchers]") {
using namespace stdx::literals;
using msg_data = std::array<std::uint32_t, 1>;

constexpr auto m = msg::less_than_t<test_enum_field, E::C>{};
constexpr auto desc = m.describe_match(msg_data{0x01ff'ffff});
static_assert(desc.str == "enum_field ({}) < C"_ctst);
static_assert(desc.args == stdx::tuple{E::B});
}

TEST_CASE("negate less_than", "[field matchers]") {
constexpr auto m = msg::less_than_t<test_field, 5>{};
constexpr auto n = match::negate(m);
Expand Down
Loading