Skip to content

Commit 19cadcf

Browse files
elbenomjcaisse-intel
authored andcommitted
✅ Add message equivalence matcher for Catch2
Problem: There is no example way to test message equivalence. Solution: Provide a test that implements a Catch2 matcher. A similar concept exists for GoogleTest.
1 parent 224cae0 commit 19cadcf

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

include/msg/message.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,12 @@ template <stdx::ct_string Name, typename... Fields> struct message {
399399
[[nodiscard]] constexpr auto data() const { return storage; }
400400

401401
[[nodiscard]] constexpr auto as_owning() { return owner_t{*this}; }
402+
403+
using const_view_t =
404+
view_t<stdx::span<std::add_const_t<typename Span::value_type>,
405+
stdx::ct_capacity_v<Span>>>;
402406
[[nodiscard]] constexpr auto as_const_view() const {
403-
using cv_t =
404-
view_t<stdx::span<std::add_const_t<typename Span::value_type>,
405-
stdx::ct_capacity_v<Span>>>;
406-
return cv_t{*this};
407+
return const_view_t{*this};
407408
}
408409

409410
private:
@@ -500,6 +501,10 @@ template <stdx::ct_string Name, typename... Fields> struct message {
500501
[[nodiscard]] constexpr auto as_mutable_view() LIFETIMEBOUND {
501502
return view_t{*this};
502503
}
504+
505+
using const_view_t =
506+
view_t<stdx::span<std::add_const_t<typename Storage::value_type>,
507+
stdx::ct_capacity_v<Storage>>>;
503508
[[nodiscard]] constexpr auto as_const_view() const LIFETIMEBOUND {
504509
return view_t{*this};
505510
}

test/msg/message.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <msg/message.hpp>
33

44
#include <catch2/catch_test_macros.hpp>
5+
#include <catch2/matchers/catch_matchers_templated.hpp>
56

67
#include <algorithm>
78
#include <array>
@@ -500,3 +501,40 @@ TEST_CASE("message equivalence (views)", "[message]") {
500501
auto cv2 = other.as_const_view();
501502
CHECK(not equivalent(cv1, cv2));
502503
}
504+
505+
namespace {
506+
template <typename View>
507+
struct MsgEquivMatcher : Catch::Matchers::MatcherGenericBase {
508+
MsgEquivMatcher(View v) : view{v} {}
509+
510+
template <typename OtherMsg> bool match(OtherMsg const &other) const {
511+
return msg::equivalent(view, other);
512+
}
513+
514+
std::string describe() const override {
515+
return "Equivalent: " + Catch::rangeToString(view.data());
516+
}
517+
518+
private:
519+
View view;
520+
};
521+
522+
template <typename Msg>
523+
auto MsgEquiv(Msg const &msg) -> MsgEquivMatcher<typename Msg::const_view_t> {
524+
return {msg.as_const_view()};
525+
}
526+
} // namespace
527+
528+
TEST_CASE("message equivalence matcher", "[message]") {
529+
owning<msg_defn> m{"f1"_field = 0xba11, "f2"_field = 0x42,
530+
"f3"_field = 0xd00d};
531+
auto cv1 = m.as_const_view();
532+
auto mv1 = m.as_mutable_view();
533+
534+
CHECK_THAT(cv1, MsgEquiv(m));
535+
CHECK_THAT(mv1, MsgEquiv(m));
536+
537+
owning<msg_defn> m2{"f1"_field = 0xba12, "f2"_field = 0x42,
538+
"f3"_field = 0xd00d};
539+
CHECK_THAT(m2, not MsgEquiv(m));
540+
}

0 commit comments

Comments
 (0)